soc: codecs: rt5639: Implement i2c shutdown
[linux-2.6.git] / sound / soc / tegra / tegra_pcm.c
index ff86e5e..4f19bd4 100644 (file)
@@ -2,7 +2,7 @@
  * tegra_pcm.c - Tegra PCM driver
  *
  * Author: Stephen Warren <swarren@nvidia.com>
- * Copyright (C) 2010 - NVIDIA, Inc.
+ * Copyright (C) 2010-2012 - NVIDIA, Inc.
  *
  * Based on code copyright/by:
  *
@@ -29,6 +29,7 @@
  *
  */
 
+#include <asm/mach-types.h>
 #include <linux/module.h>
 #include <linux/dma-mapping.h>
 #include <linux/slab.h>
@@ -47,12 +48,15 @@ static const struct snd_pcm_hardware tegra_pcm_hardware = {
                                  SNDRV_PCM_INFO_PAUSE |
                                  SNDRV_PCM_INFO_RESUME |
                                  SNDRV_PCM_INFO_INTERLEAVED,
-       .formats                = SNDRV_PCM_FMTBIT_S16_LE,
-       .channels_min           = 2,
+       .formats                = SNDRV_PCM_FMTBIT_S8 |
+                                 SNDRV_PCM_FMTBIT_S16_LE |
+                                 SNDRV_PCM_FMTBIT_S24_LE |
+                                 SNDRV_PCM_FMTBIT_S32_LE,
+       .channels_min           = 1,
        .channels_max           = 2,
-       .period_bytes_min       = 1024,
-       .period_bytes_max       = PAGE_SIZE,
-       .periods_min            = 2,
+       .period_bytes_min       = 128,
+       .period_bytes_max       = PAGE_SIZE * 2,
+       .periods_min            = 1,
        .periods_max            = 8,
        .buffer_bytes_max       = PAGE_SIZE * 8,
        .fifo_size              = 4,
@@ -66,9 +70,14 @@ static void tegra_pcm_queue_dma(struct tegra_runtime_data *prtd)
        unsigned long addr;
 
        dma_req = &prtd->dma_req[prtd->dma_req_idx];
-       prtd->dma_req_idx = 1 - prtd->dma_req_idx;
+       if (++prtd->dma_req_idx >= prtd->dma_req_count)
+               prtd->dma_req_idx -= prtd->dma_req_count;
+
+       if (prtd->avp_dma_addr)
+               addr = prtd->avp_dma_addr + prtd->dma_pos;
+       else
+               addr = buf->addr + prtd->dma_pos;
 
-       addr = buf->addr + prtd->dma_pos;
        prtd->dma_pos += dma_req->size;
        if (prtd->dma_pos >= prtd->dma_pos_end)
                prtd->dma_pos = 0;
@@ -115,6 +124,10 @@ static void setup_dma_tx_request(struct tegra_dma_req *req,
        req->source_wrap = 0;
        req->dest_bus_width = dmap->width;
        req->req_sel = dmap->req_sel;
+       req->use_smmu = false;
+#if TEGRA30_USE_SMMU
+       req->use_smmu = true;
+#endif
 }
 
 static void setup_dma_rx_request(struct tegra_dma_req *req,
@@ -128,15 +141,22 @@ static void setup_dma_rx_request(struct tegra_dma_req *req,
        req->source_wrap = dmap->wrap;
        req->dest_bus_width = 32;
        req->req_sel = dmap->req_sel;
+       req->use_smmu = false;
+#if TEGRA30_USE_SMMU
+       req->use_smmu = true;
+#endif
 }
 
-static int tegra_pcm_open(struct snd_pcm_substream *substream)
+int tegra_pcm_allocate(struct snd_pcm_substream *substream,
+                                       int dma_mode,
+                                       const struct snd_pcm_hardware *pcm_hardware)
 {
        struct snd_pcm_runtime *runtime = substream->runtime;
        struct tegra_runtime_data *prtd;
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
        struct tegra_pcm_dma_params * dmap;
        int ret = 0;
+       int i = 0;
 
        prtd = kzalloc(sizeof(struct tegra_runtime_data), GFP_KERNEL);
        if (prtd == NULL)
@@ -147,27 +167,30 @@ static int tegra_pcm_open(struct snd_pcm_substream *substream)
 
        spin_lock_init(&prtd->lock);
 
-       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
-               dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
-               setup_dma_tx_request(&prtd->dma_req[0], dmap);
-               setup_dma_tx_request(&prtd->dma_req[1], dmap);
-       } else {
-               dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
-               setup_dma_rx_request(&prtd->dma_req[0], dmap);
-               setup_dma_rx_request(&prtd->dma_req[1], dmap);
-       }
+       dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
+       prtd->dma_req_count = MAX_DMA_REQ_COUNT;
 
-       prtd->dma_req[0].dev = prtd;
-       prtd->dma_req[1].dev = prtd;
+       if (dmap) {
+               for (i = 0; i < prtd->dma_req_count; i++)
+                       prtd->dma_req[i].dev = prtd;
 
-       prtd->dma_chan = tegra_dma_allocate_channel(TEGRA_DMA_MODE_ONESHOT);
-       if (prtd->dma_chan == NULL) {
-               ret = -ENOMEM;
-               goto err;
+               prtd->dma_chan = tegra_dma_allocate_channel(
+                                       dma_mode,
+                                       "pcm");
+               if (prtd->dma_chan == NULL) {
+                       ret = -ENOMEM;
+                       goto err;
+               }
        }
 
        /* Set HW params now that initialization is complete */
-       snd_soc_set_runtime_hwparams(substream, &tegra_pcm_hardware);
+       snd_soc_set_runtime_hwparams(substream, pcm_hardware);
+
+       /* Ensure period size is multiple of 8 */
+       ret = snd_pcm_hw_constraint_step(runtime, 0,
+               SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 0x8);
+       if (ret < 0)
+               goto err;
 
        /* Ensure that buffer size is a multiple of period size */
        ret = snd_pcm_hw_constraint_integer(runtime,
@@ -187,44 +210,76 @@ err:
        return ret;
 }
 
-static int tegra_pcm_close(struct snd_pcm_substream *substream)
+static int tegra_pcm_open(struct snd_pcm_substream *substream)
+{
+       return tegra_pcm_allocate(substream,
+                                       TEGRA_DMA_MODE_CONTINUOUS_SINGLE,
+                                       &tegra_pcm_hardware);
+
+}
+
+int tegra_pcm_close(struct snd_pcm_substream *substream)
 {
        struct snd_pcm_runtime *runtime = substream->runtime;
        struct tegra_runtime_data *prtd = runtime->private_data;
 
-       tegra_dma_free_channel(prtd->dma_chan);
+       if (prtd->dma_chan)
+               tegra_dma_free_channel(prtd->dma_chan);
 
        kfree(prtd);
 
        return 0;
 }
 
-static int tegra_pcm_hw_params(struct snd_pcm_substream *substream,
+int tegra_pcm_hw_params(struct snd_pcm_substream *substream,
                                struct snd_pcm_hw_params *params)
 {
        struct snd_pcm_runtime *runtime = substream->runtime;
        struct tegra_runtime_data *prtd = runtime->private_data;
+       struct snd_soc_pcm_runtime *rtd = substream->private_data;
+       struct tegra_pcm_dma_params * dmap;
+       int i;
 
        snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
 
-       prtd->dma_req[0].size = params_period_bytes(params);
-       prtd->dma_req[1].size = prtd->dma_req[0].size;
+       /* Limit dma_req_count to period count */
+       if (prtd->dma_req_count > params_periods(params))
+               prtd->dma_req_count = params_periods(params);
+       dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
+       if (dmap) {
+               if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+                       for (i = 0; i < prtd->dma_req_count; i++)
+                               setup_dma_tx_request(&prtd->dma_req[i], dmap);
+               } else {
+                       for (i = 0; i < prtd->dma_req_count; i++)
+                               setup_dma_rx_request(&prtd->dma_req[i], dmap);
+               }
+       }
+       for (i = 0; i < prtd->dma_req_count; i++)
+               prtd->dma_req[i].size = params_period_bytes(params);
 
        return 0;
 }
 
-static int tegra_pcm_hw_free(struct snd_pcm_substream *substream)
+int tegra_pcm_hw_free(struct snd_pcm_substream *substream)
 {
        snd_pcm_set_runtime_buffer(substream, NULL);
 
        return 0;
 }
 
-static int tegra_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
+int tegra_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
 {
        struct snd_pcm_runtime *runtime = substream->runtime;
        struct tegra_runtime_data *prtd = runtime->private_data;
+       struct snd_soc_pcm_runtime *rtd = substream->private_data;
+       struct tegra_pcm_dma_params * dmap;
        unsigned long flags;
+       int i;
+
+       dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
+       if (!dmap)
+               return 0;
 
        switch (cmd) {
        case SNDRV_PCM_TRIGGER_START:
@@ -232,14 +287,23 @@ static int tegra_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
                prtd->dma_pos_end = frames_to_bytes(runtime, runtime->periods * runtime->period_size);
                prtd->period_index = 0;
                prtd->dma_req_idx = 0;
+               if (prtd->disable_intr) {
+                       prtd->dma_req_count = 1;
+                       prtd->dma_req[0].complete = NULL;
+               } else if (!prtd->dma_req[0].complete) {
+                       prtd->dma_req[0].complete = dma_complete_callback;
+                       prtd->dma_req_count =
+                               (MAX_DMA_REQ_COUNT <= runtime->periods) ?
+                               MAX_DMA_REQ_COUNT : runtime->periods;
+               }
                /* Fall-through */
        case SNDRV_PCM_TRIGGER_RESUME:
        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
                spin_lock_irqsave(&prtd->lock, flags);
                prtd->running = 1;
                spin_unlock_irqrestore(&prtd->lock, flags);
-               tegra_pcm_queue_dma(prtd);
-               tegra_pcm_queue_dma(prtd);
+               for (i = 0; i < prtd->dma_req_count; i++)
+                       tegra_pcm_queue_dma(prtd);
                break;
        case SNDRV_PCM_TRIGGER_STOP:
        case SNDRV_PCM_TRIGGER_SUSPEND:
@@ -247,8 +311,13 @@ static int tegra_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
                spin_lock_irqsave(&prtd->lock, flags);
                prtd->running = 0;
                spin_unlock_irqrestore(&prtd->lock, flags);
-               tegra_dma_dequeue_req(prtd->dma_chan, &prtd->dma_req[0]);
-               tegra_dma_dequeue_req(prtd->dma_chan, &prtd->dma_req[1]);
+               tegra_dma_cancel(prtd->dma_chan);
+               for (i = 0; i < prtd->dma_req_count; i++) {
+                       if (prtd->dma_req[i].complete &&
+                               (prtd->dma_req[i].status ==
+                                -TEGRA_DMA_REQ_ERROR_ABORTED))
+                               prtd->dma_req[i].complete(&prtd->dma_req[i]);
+               }
                break;
        default:
                return -EINVAL;
@@ -257,16 +326,20 @@ static int tegra_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
        return 0;
 }
 
-static snd_pcm_uframes_t tegra_pcm_pointer(struct snd_pcm_substream *substream)
+snd_pcm_uframes_t tegra_pcm_pointer(struct snd_pcm_substream *substream)
 {
        struct snd_pcm_runtime *runtime = substream->runtime;
        struct tegra_runtime_data *prtd = runtime->private_data;
+       int dma_transfer_count;
 
-       return prtd->period_index * runtime->period_size;
-}
+       dma_transfer_count = tegra_dma_get_transfer_count(prtd->dma_chan,
+                                       &prtd->dma_req[prtd->dma_req_idx]);
 
+       return prtd->period_index * runtime->period_size +
+               bytes_to_frames(runtime, dma_transfer_count);
+}
 
-static int tegra_pcm_mmap(struct snd_pcm_substream *substream,
+int tegra_pcm_mmap(struct snd_pcm_substream *substream,
                                struct vm_area_struct *vma)
 {
        struct snd_pcm_runtime *runtime = substream->runtime;
@@ -288,62 +361,100 @@ static struct snd_pcm_ops tegra_pcm_ops = {
        .mmap           = tegra_pcm_mmap,
 };
 
-static int tegra_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
+static int tegra_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
+                               int stream , size_t size)
 {
        struct snd_pcm_substream *substream = pcm->streams[stream].substream;
        struct snd_dma_buffer *buf = &substream->dma_buffer;
-       size_t size = tegra_pcm_hardware.buffer_bytes_max;
-
+#if TEGRA30_USE_SMMU
+       unsigned char *vaddr;
+       phys_addr_t paddr;
+       struct tegra_smmu_data *ptsd;
+
+       ptsd = kzalloc(sizeof(struct tegra_smmu_data), GFP_KERNEL);
+       ptsd->pcm_nvmap_client = nvmap_create_client(nvmap_dev, "Audio_SMMU");
+       ptsd->pcm_nvmap_handle = nvmap_alloc(ptsd->pcm_nvmap_client,
+                                size,
+                                32,
+                                NVMAP_HANDLE_WRITE_COMBINE,
+                                NVMAP_HEAP_IOVMM);
+
+       vaddr = (unsigned char *) nvmap_mmap(ptsd->pcm_nvmap_handle);
+       paddr = nvmap_pin(ptsd->pcm_nvmap_client, ptsd->pcm_nvmap_handle);
+       buf->area = vaddr;
+       buf->addr = paddr;
+       buf->private_data = ptsd;
+#else
        buf->area = dma_alloc_writecombine(pcm->card->dev, size,
                                                &buf->addr, GFP_KERNEL);
        if (!buf->area)
                return -ENOMEM;
-
+       buf->private_data = NULL;
+#endif
        buf->dev.type = SNDRV_DMA_TYPE_DEV;
        buf->dev.dev = pcm->card->dev;
-       buf->private_data = NULL;
        buf->bytes = size;
 
        return 0;
 }
 
-static void tegra_pcm_deallocate_dma_buffer(struct snd_pcm *pcm, int stream)
+void tegra_pcm_deallocate_dma_buffer(struct snd_pcm *pcm, int stream)
 {
-       struct snd_pcm_substream *substream = pcm->streams[stream].substream;
-       struct snd_dma_buffer *buf = &substream->dma_buffer;
+       struct snd_pcm_substream *substream;
+       struct snd_dma_buffer *buf;
+#if TEGRA30_USE_SMMU
+       struct tegra_smmu_data *ptsd;
+#endif
+
+       substream = pcm->streams[stream].substream;
+       if (!substream)
+               return;
 
+       buf = &substream->dma_buffer;
        if (!buf->area)
                return;
 
+#if TEGRA30_USE_SMMU
+       if (!buf->private_data)
+               return;
+       ptsd = (struct tegra_smmu_data *)buf->private_data;
+       nvmap_unpin(ptsd->pcm_nvmap_client, ptsd->pcm_nvmap_handle);
+       nvmap_munmap(ptsd->pcm_nvmap_handle, buf->area);
+       nvmap_free(ptsd->pcm_nvmap_client, ptsd->pcm_nvmap_handle);
+       kfree(ptsd);
+       buf->private_data = NULL;
+#else
        dma_free_writecombine(pcm->card->dev, buf->bytes,
                                buf->area, buf->addr);
+#endif
        buf->area = NULL;
 }
 
 static u64 tegra_dma_mask = DMA_BIT_MASK(32);
 
-static int tegra_pcm_new(struct snd_soc_pcm_runtime *rtd)
+int tegra_pcm_dma_allocate(struct snd_soc_pcm_runtime *rtd, size_t size)
 {
        struct snd_card *card = rtd->card->snd_card;
-       struct snd_soc_dai *dai = rtd->cpu_dai;
        struct snd_pcm *pcm = rtd->pcm;
        int ret = 0;
 
        if (!card->dev->dma_mask)
                card->dev->dma_mask = &tegra_dma_mask;
        if (!card->dev->coherent_dma_mask)
-               card->dev->coherent_dma_mask = 0xffffffff;
+               card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
 
-       if (dai->driver->playback.channels_min) {
+       if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
                ret = tegra_pcm_preallocate_dma_buffer(pcm,
-                                               SNDRV_PCM_STREAM_PLAYBACK);
+                                               SNDRV_PCM_STREAM_PLAYBACK,
+                                               size);
                if (ret)
                        goto err;
        }
 
-       if (dai->driver->capture.channels_min) {
+       if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
                ret = tegra_pcm_preallocate_dma_buffer(pcm,
-                                               SNDRV_PCM_STREAM_CAPTURE);
+                                               SNDRV_PCM_STREAM_CAPTURE,
+                                               size);
                if (ret)
                        goto err_free_play;
        }
@@ -356,16 +467,29 @@ err:
        return ret;
 }
 
-static void tegra_pcm_free(struct snd_pcm *pcm)
+int tegra_pcm_new(struct snd_soc_pcm_runtime *rtd)
+{
+       return tegra_pcm_dma_allocate(rtd ,
+                                       tegra_pcm_hardware.buffer_bytes_max);
+}
+
+void tegra_pcm_free(struct snd_pcm *pcm)
 {
        tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_CAPTURE);
        tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
 }
 
-struct snd_soc_platform_driver tegra_pcm_platform = {
+static int tegra_pcm_probe(struct snd_soc_platform *platform)
+{
+       platform->dapm.idle_bias_off = 1;
+       return 0;
+}
+
+static struct snd_soc_platform_driver tegra_pcm_platform = {
        .ops            = &tegra_pcm_ops,
        .pcm_new        = tegra_pcm_new,
        .pcm_free       = tegra_pcm_free,
+       .probe          = tegra_pcm_probe,
 };
 
 static int __devinit tegra_pcm_platform_probe(struct platform_device *pdev)
@@ -387,18 +511,7 @@ static struct platform_driver tegra_pcm_driver = {
        .probe = tegra_pcm_platform_probe,
        .remove = __devexit_p(tegra_pcm_platform_remove),
 };
-
-static int __init snd_tegra_pcm_init(void)
-{
-       return platform_driver_register(&tegra_pcm_driver);
-}
-module_init(snd_tegra_pcm_init);
-
-static void __exit snd_tegra_pcm_exit(void)
-{
-       platform_driver_unregister(&tegra_pcm_driver);
-}
-module_exit(snd_tegra_pcm_exit);
+module_platform_driver(tegra_pcm_driver);
 
 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
 MODULE_DESCRIPTION("Tegra PCM ASoC driver");