Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * imx-pcm-dma-mx2.c -- ALSA Soc Audio Layer |
| 3 | * |
| 4 | * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de> |
| 5 | * |
| 6 | * This code is based on code copyrighted by Freescale, |
| 7 | * Liam Girdwood, Javier Martin and probably others. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
| 13 | */ |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/dma-mapping.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 23 | #include <linux/dmaengine.h> |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 24 | |
| 25 | #include <sound/core.h> |
| 26 | #include <sound/initval.h> |
| 27 | #include <sound/pcm.h> |
| 28 | #include <sound/pcm_params.h> |
| 29 | #include <sound/soc.h> |
| 30 | |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 31 | #include <mach/dma.h> |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 32 | |
| 33 | #include "imx-ssi.h" |
| 34 | |
| 35 | struct imx_pcm_runtime_data { |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 36 | int period_bytes; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 37 | int periods; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 38 | unsigned long offset; |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 39 | struct dma_async_tx_descriptor *desc; |
| 40 | struct dma_chan *dma_chan; |
| 41 | struct imx_dma_data dma_data; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 44 | static void audio_dma_irq(void *data) |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 45 | { |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 46 | struct snd_pcm_substream *substream = (struct snd_pcm_substream *)data; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 47 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 48 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 49 | |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 50 | iprtd->offset += iprtd->period_bytes; |
| 51 | iprtd->offset %= iprtd->period_bytes * iprtd->periods; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 52 | |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 53 | snd_pcm_period_elapsed(substream); |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 56 | static bool filter(struct dma_chan *chan, void *param) |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 57 | { |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 58 | struct imx_pcm_runtime_data *iprtd = param; |
| 59 | |
| 60 | if (!imx_dma_is_general_purpose(chan)) |
| 61 | return false; |
| 62 | |
| 63 | chan->private = &iprtd->dma_data; |
| 64 | |
| 65 | return true; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Lars-Peter Clausen | 4564d10 | 2012-02-22 10:49:06 +0100 | [diff] [blame^] | 68 | static int imx_ssi_dma_alloc(struct snd_pcm_substream *substream) |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 69 | { |
| 70 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 71 | struct imx_pcm_dma_params *dma_params; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 72 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 73 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 74 | dma_cap_mask_t mask; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 75 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 76 | dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 77 | |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 78 | iprtd->dma_data.peripheral_type = IMX_DMATYPE_SSI; |
| 79 | iprtd->dma_data.priority = DMA_PRIO_HIGH; |
| 80 | iprtd->dma_data.dma_request = dma_params->dma; |
| 81 | |
| 82 | /* Try to grab a DMA channel */ |
Lars-Peter Clausen | 4564d10 | 2012-02-22 10:49:06 +0100 | [diff] [blame^] | 83 | dma_cap_zero(mask); |
| 84 | dma_cap_set(DMA_SLAVE, mask); |
| 85 | iprtd->dma_chan = dma_request_channel(mask, filter, iprtd); |
| 86 | if (!iprtd->dma_chan) |
| 87 | return -EINVAL; |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream, |
| 93 | struct snd_pcm_hw_params *params) |
| 94 | { |
| 95 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 96 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 97 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 98 | struct dma_chan *chan = iprtd->dma_chan; |
| 99 | struct imx_pcm_dma_params *dma_params; |
| 100 | struct dma_slave_config slave_config; |
| 101 | enum dma_slave_buswidth buswidth; |
| 102 | unsigned long dma_addr; |
| 103 | int ret; |
| 104 | |
| 105 | dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 106 | |
| 107 | switch (params_format(params)) { |
| 108 | case SNDRV_PCM_FORMAT_S16_LE: |
| 109 | buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 110 | break; |
| 111 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 112 | case SNDRV_PCM_FORMAT_S24_LE: |
| 113 | buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 114 | break; |
| 115 | default: |
| 116 | return 0; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 119 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Vinod Koul | 35e1658 | 2011-10-14 10:49:30 +0530 | [diff] [blame] | 120 | slave_config.direction = DMA_MEM_TO_DEV; |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 121 | slave_config.dst_addr = dma_params->dma_addr; |
| 122 | slave_config.dst_addr_width = buswidth; |
Sascha Hauer | 6584cb8 | 2011-07-06 11:18:33 +0200 | [diff] [blame] | 123 | slave_config.dst_maxburst = dma_params->burstsize; |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 124 | } else { |
Vinod Koul | 35e1658 | 2011-10-14 10:49:30 +0530 | [diff] [blame] | 125 | slave_config.direction = DMA_DEV_TO_MEM; |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 126 | slave_config.src_addr = dma_params->dma_addr; |
| 127 | slave_config.src_addr_width = buswidth; |
Sascha Hauer | 6584cb8 | 2011-07-06 11:18:33 +0200 | [diff] [blame] | 128 | slave_config.src_maxburst = dma_params->burstsize; |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Lars-Peter Clausen | 4564d10 | 2012-02-22 10:49:06 +0100 | [diff] [blame^] | 131 | ret = dmaengine_slave_config(chan, &slave_config); |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 132 | if (ret) |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 133 | return ret; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 134 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 135 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 136 | iprtd->periods = params_periods(params); |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 137 | iprtd->period_bytes = params_period_bytes(params); |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 138 | iprtd->offset = 0; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 139 | |
| 140 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 141 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 142 | dma_addr = runtime->dma_addr; |
| 143 | |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 144 | iprtd->desc = chan->device->device_prep_dma_cyclic(chan, dma_addr, |
| 145 | iprtd->period_bytes * iprtd->periods, |
| 146 | iprtd->period_bytes, |
| 147 | substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? |
Vinod Koul | 35e1658 | 2011-10-14 10:49:30 +0530 | [diff] [blame] | 148 | DMA_MEM_TO_DEV : DMA_DEV_TO_MEM); |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 149 | if (!iprtd->desc) { |
| 150 | dev_err(&chan->dev->device, "cannot prepare slave dma\n"); |
| 151 | return -EINVAL; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 154 | iprtd->desc->callback = audio_dma_irq; |
| 155 | iprtd->desc->callback_param = substream; |
| 156 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 160 | static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 161 | { |
| 162 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 163 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 164 | |
| 165 | switch (cmd) { |
| 166 | case SNDRV_PCM_TRIGGER_START: |
| 167 | case SNDRV_PCM_TRIGGER_RESUME: |
| 168 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 169 | dmaengine_submit(iprtd->desc); |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 170 | |
| 171 | break; |
| 172 | |
| 173 | case SNDRV_PCM_TRIGGER_STOP: |
| 174 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 175 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 176 | dmaengine_terminate_all(iprtd->dma_chan); |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 177 | |
| 178 | break; |
| 179 | default: |
| 180 | return -EINVAL; |
| 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream) |
| 187 | { |
| 188 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 189 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 190 | |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 191 | pr_debug("%s: %ld %ld\n", __func__, iprtd->offset, |
| 192 | bytes_to_frames(substream->runtime, iprtd->offset)); |
| 193 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 194 | return bytes_to_frames(substream->runtime, iprtd->offset); |
| 195 | } |
| 196 | |
| 197 | static struct snd_pcm_hardware snd_imx_hardware = { |
| 198 | .info = SNDRV_PCM_INFO_INTERLEAVED | |
| 199 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 200 | SNDRV_PCM_INFO_MMAP | |
| 201 | SNDRV_PCM_INFO_MMAP_VALID | |
| 202 | SNDRV_PCM_INFO_PAUSE | |
| 203 | SNDRV_PCM_INFO_RESUME, |
| 204 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 205 | .rate_min = 8000, |
| 206 | .channels_min = 2, |
| 207 | .channels_max = 2, |
| 208 | .buffer_bytes_max = IMX_SSI_DMABUF_SIZE, |
| 209 | .period_bytes_min = 128, |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 210 | .period_bytes_max = 65535, /* Limited by SDMA engine */ |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 211 | .periods_min = 2, |
| 212 | .periods_max = 255, |
| 213 | .fifo_size = 0, |
| 214 | }; |
| 215 | |
| 216 | static int snd_imx_open(struct snd_pcm_substream *substream) |
| 217 | { |
| 218 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 219 | struct imx_pcm_runtime_data *iprtd; |
| 220 | int ret; |
| 221 | |
| 222 | iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL); |
Kulikov Vasiliy | 51b6dfb | 2010-07-16 20:16:34 +0400 | [diff] [blame] | 223 | if (iprtd == NULL) |
| 224 | return -ENOMEM; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 225 | runtime->private_data = iprtd; |
| 226 | |
| 227 | ret = snd_pcm_hw_constraint_integer(substream->runtime, |
| 228 | SNDRV_PCM_HW_PARAM_PERIODS); |
Kulikov Vasiliy | 51b6dfb | 2010-07-16 20:16:34 +0400 | [diff] [blame] | 229 | if (ret < 0) { |
| 230 | kfree(iprtd); |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 231 | return ret; |
Kulikov Vasiliy | 51b6dfb | 2010-07-16 20:16:34 +0400 | [diff] [blame] | 232 | } |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 233 | |
Lars-Peter Clausen | 4564d10 | 2012-02-22 10:49:06 +0100 | [diff] [blame^] | 234 | ret = imx_ssi_dma_alloc(substream); |
| 235 | if (ret < 0) { |
| 236 | kfree(iprtd); |
| 237 | return ret; |
| 238 | } |
| 239 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 240 | snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware); |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int snd_imx_close(struct snd_pcm_substream *substream) |
| 246 | { |
| 247 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 248 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 249 | |
Lars-Peter Clausen | 4564d10 | 2012-02-22 10:49:06 +0100 | [diff] [blame^] | 250 | dma_release_channel(iprtd->dma_chan); |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 251 | kfree(iprtd); |
| 252 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static struct snd_pcm_ops imx_pcm_ops = { |
| 257 | .open = snd_imx_open, |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 258 | .close = snd_imx_close, |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 259 | .ioctl = snd_pcm_lib_ioctl, |
| 260 | .hw_params = snd_imx_pcm_hw_params, |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 261 | .trigger = snd_imx_pcm_trigger, |
| 262 | .pointer = snd_imx_pcm_pointer, |
| 263 | .mmap = snd_imx_pcm_mmap, |
| 264 | }; |
| 265 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 266 | static struct snd_soc_platform_driver imx_soc_platform_mx2 = { |
| 267 | .ops = &imx_pcm_ops, |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 268 | .pcm_new = imx_pcm_new, |
| 269 | .pcm_free = imx_pcm_free, |
| 270 | }; |
| 271 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 272 | static int __devinit imx_soc_platform_probe(struct platform_device *pdev) |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 273 | { |
Wolfram Sang | 2c4cf17 | 2011-03-25 16:51:44 +0100 | [diff] [blame] | 274 | struct imx_ssi *ssi = platform_get_drvdata(pdev); |
| 275 | |
| 276 | ssi->dma_params_tx.burstsize = 6; |
| 277 | ssi->dma_params_rx.burstsize = 4; |
| 278 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 279 | return snd_soc_register_platform(&pdev->dev, &imx_soc_platform_mx2); |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 282 | static int __devexit imx_soc_platform_remove(struct platform_device *pdev) |
| 283 | { |
| 284 | snd_soc_unregister_platform(&pdev->dev); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static struct platform_driver imx_pcm_driver = { |
| 289 | .driver = { |
| 290 | .name = "imx-pcm-audio", |
| 291 | .owner = THIS_MODULE, |
| 292 | }, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 293 | .probe = imx_soc_platform_probe, |
| 294 | .remove = __devexit_p(imx_soc_platform_remove), |
| 295 | }; |
| 296 | |
Axel Lin | 7a24b2b | 2011-11-24 15:03:50 +0800 | [diff] [blame] | 297 | module_platform_driver(imx_pcm_driver); |
Arnaud Patard (Rtp) | 96dcabb | 2011-06-22 22:21:49 +0200 | [diff] [blame] | 298 | MODULE_LICENSE("GPL"); |
| 299 | MODULE_ALIAS("platform:imx-pcm-audio"); |