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> |
| 22 | |
| 23 | #include <sound/core.h> |
| 24 | #include <sound/initval.h> |
| 25 | #include <sound/pcm.h> |
| 26 | #include <sound/pcm_params.h> |
| 27 | #include <sound/soc.h> |
| 28 | |
| 29 | #include <mach/dma-mx1-mx2.h> |
| 30 | |
| 31 | #include "imx-ssi.h" |
| 32 | |
| 33 | struct imx_pcm_runtime_data { |
| 34 | int sg_count; |
| 35 | struct scatterlist *sg_list; |
| 36 | int period; |
| 37 | int periods; |
| 38 | unsigned long dma_addr; |
| 39 | int dma; |
| 40 | struct snd_pcm_substream *substream; |
| 41 | unsigned long offset; |
| 42 | unsigned long size; |
| 43 | unsigned long period_cnt; |
| 44 | void *buf; |
| 45 | int period_time; |
| 46 | }; |
| 47 | |
| 48 | /* Called by the DMA framework when a period has elapsed */ |
| 49 | static void imx_ssi_dma_progression(int channel, void *data, |
| 50 | struct scatterlist *sg) |
| 51 | { |
| 52 | struct snd_pcm_substream *substream = data; |
| 53 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 54 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 55 | |
| 56 | if (!sg) |
| 57 | return; |
| 58 | |
| 59 | runtime = iprtd->substream->runtime; |
| 60 | |
| 61 | iprtd->offset = sg->dma_address - runtime->dma_addr; |
| 62 | |
| 63 | snd_pcm_period_elapsed(iprtd->substream); |
| 64 | } |
| 65 | |
| 66 | static void imx_ssi_dma_callback(int channel, void *data) |
| 67 | { |
| 68 | pr_err("%s shouldn't be called\n", __func__); |
| 69 | } |
| 70 | |
| 71 | static void snd_imx_dma_err_callback(int channel, void *data, int err) |
| 72 | { |
| 73 | pr_err("DMA error callback called\n"); |
| 74 | |
| 75 | pr_err("DMA timeout on channel %d -%s%s%s%s\n", |
| 76 | channel, |
| 77 | err & IMX_DMA_ERR_BURST ? " burst" : "", |
| 78 | err & IMX_DMA_ERR_REQUEST ? " request" : "", |
| 79 | err & IMX_DMA_ERR_TRANSFER ? " transfer" : "", |
| 80 | err & IMX_DMA_ERR_BUFFER ? " buffer" : ""); |
| 81 | } |
| 82 | |
| 83 | static int imx_ssi_dma_alloc(struct snd_pcm_substream *substream) |
| 84 | { |
| 85 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 86 | struct imx_pcm_dma_params *dma_params = rtd->dai->cpu_dai->dma_data; |
| 87 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 88 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 89 | int ret; |
| 90 | |
| 91 | iprtd->dma = imx_dma_request_by_prio(DRV_NAME, DMA_PRIO_HIGH); |
| 92 | if (iprtd->dma < 0) { |
| 93 | pr_err("Failed to claim the audio DMA\n"); |
| 94 | return -ENODEV; |
| 95 | } |
| 96 | |
| 97 | ret = imx_dma_setup_handlers(iprtd->dma, |
| 98 | imx_ssi_dma_callback, |
| 99 | snd_imx_dma_err_callback, substream); |
| 100 | if (ret) |
| 101 | goto out; |
| 102 | |
| 103 | ret = imx_dma_setup_progression_handler(iprtd->dma, |
| 104 | imx_ssi_dma_progression); |
| 105 | if (ret) { |
| 106 | pr_err("Failed to setup the DMA handler\n"); |
| 107 | goto out; |
| 108 | } |
| 109 | |
| 110 | ret = imx_dma_config_channel(iprtd->dma, |
| 111 | IMX_DMA_MEMSIZE_16 | IMX_DMA_TYPE_FIFO, |
| 112 | IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR, |
| 113 | dma_params->dma, 1); |
| 114 | if (ret < 0) { |
| 115 | pr_err("Cannot configure DMA channel: %d\n", ret); |
| 116 | goto out; |
| 117 | } |
| 118 | |
| 119 | imx_dma_config_burstlen(iprtd->dma, dma_params->burstsize * 2); |
| 120 | |
| 121 | return 0; |
| 122 | out: |
| 123 | imx_dma_free(iprtd->dma); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream, |
| 128 | struct snd_pcm_hw_params *params) |
| 129 | { |
| 130 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 131 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 132 | int i; |
| 133 | unsigned long dma_addr; |
| 134 | |
| 135 | imx_ssi_dma_alloc(substream); |
| 136 | |
| 137 | iprtd->size = params_buffer_bytes(params); |
| 138 | iprtd->periods = params_periods(params); |
| 139 | iprtd->period = params_period_bytes(params); |
| 140 | iprtd->offset = 0; |
| 141 | iprtd->period_time = HZ / (params_rate(params) / |
| 142 | params_period_size(params)); |
| 143 | |
| 144 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 145 | |
| 146 | if (iprtd->sg_count != iprtd->periods) { |
| 147 | kfree(iprtd->sg_list); |
| 148 | |
| 149 | iprtd->sg_list = kcalloc(iprtd->periods + 1, |
| 150 | sizeof(struct scatterlist), GFP_KERNEL); |
| 151 | if (!iprtd->sg_list) |
| 152 | return -ENOMEM; |
| 153 | iprtd->sg_count = iprtd->periods + 1; |
| 154 | } |
| 155 | |
| 156 | sg_init_table(iprtd->sg_list, iprtd->sg_count); |
| 157 | dma_addr = runtime->dma_addr; |
| 158 | |
| 159 | for (i = 0; i < iprtd->periods; i++) { |
| 160 | iprtd->sg_list[i].page_link = 0; |
| 161 | iprtd->sg_list[i].offset = 0; |
| 162 | iprtd->sg_list[i].dma_address = dma_addr; |
| 163 | iprtd->sg_list[i].length = iprtd->period; |
| 164 | dma_addr += iprtd->period; |
| 165 | } |
| 166 | |
| 167 | /* close the loop */ |
| 168 | iprtd->sg_list[iprtd->sg_count - 1].offset = 0; |
| 169 | iprtd->sg_list[iprtd->sg_count - 1].length = 0; |
| 170 | iprtd->sg_list[iprtd->sg_count - 1].page_link = |
| 171 | ((unsigned long) iprtd->sg_list | 0x01) & ~0x02; |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int snd_imx_pcm_hw_free(struct snd_pcm_substream *substream) |
| 176 | { |
| 177 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 178 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 179 | |
| 180 | if (iprtd->dma >= 0) { |
| 181 | imx_dma_free(iprtd->dma); |
| 182 | iprtd->dma = -EINVAL; |
| 183 | } |
| 184 | |
| 185 | kfree(iprtd->sg_list); |
| 186 | iprtd->sg_list = NULL; |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream) |
| 192 | { |
| 193 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 194 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 195 | struct imx_pcm_dma_params *dma_params = rtd->dai->cpu_dai->dma_data; |
| 196 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 197 | int err; |
| 198 | |
| 199 | iprtd->substream = substream; |
| 200 | iprtd->buf = (unsigned int *)substream->dma_buffer.area; |
| 201 | iprtd->period_cnt = 0; |
| 202 | |
| 203 | pr_debug("%s: buf: %p period: %d periods: %d\n", |
| 204 | __func__, iprtd->buf, iprtd->period, iprtd->periods); |
| 205 | |
| 206 | err = imx_dma_setup_sg(iprtd->dma, iprtd->sg_list, iprtd->sg_count, |
| 207 | IMX_DMA_LENGTH_LOOP, dma_params->dma_addr, |
| 208 | substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? |
| 209 | DMA_MODE_WRITE : DMA_MODE_READ); |
| 210 | if (err) |
| 211 | return err; |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 217 | { |
| 218 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 219 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 220 | |
| 221 | switch (cmd) { |
| 222 | case SNDRV_PCM_TRIGGER_START: |
| 223 | case SNDRV_PCM_TRIGGER_RESUME: |
| 224 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 225 | imx_dma_enable(iprtd->dma); |
| 226 | |
| 227 | break; |
| 228 | |
| 229 | case SNDRV_PCM_TRIGGER_STOP: |
| 230 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 231 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 232 | imx_dma_disable(iprtd->dma); |
| 233 | |
| 234 | break; |
| 235 | default: |
| 236 | return -EINVAL; |
| 237 | } |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream) |
| 243 | { |
| 244 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 245 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 246 | |
| 247 | return bytes_to_frames(substream->runtime, iprtd->offset); |
| 248 | } |
| 249 | |
| 250 | static struct snd_pcm_hardware snd_imx_hardware = { |
| 251 | .info = SNDRV_PCM_INFO_INTERLEAVED | |
| 252 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 253 | SNDRV_PCM_INFO_MMAP | |
| 254 | SNDRV_PCM_INFO_MMAP_VALID | |
| 255 | SNDRV_PCM_INFO_PAUSE | |
| 256 | SNDRV_PCM_INFO_RESUME, |
| 257 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 258 | .rate_min = 8000, |
| 259 | .channels_min = 2, |
| 260 | .channels_max = 2, |
| 261 | .buffer_bytes_max = IMX_SSI_DMABUF_SIZE, |
| 262 | .period_bytes_min = 128, |
| 263 | .period_bytes_max = 16 * 1024, |
| 264 | .periods_min = 2, |
| 265 | .periods_max = 255, |
| 266 | .fifo_size = 0, |
| 267 | }; |
| 268 | |
| 269 | static int snd_imx_open(struct snd_pcm_substream *substream) |
| 270 | { |
| 271 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 272 | struct imx_pcm_runtime_data *iprtd; |
| 273 | int ret; |
| 274 | |
| 275 | iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL); |
| 276 | runtime->private_data = iprtd; |
| 277 | |
| 278 | ret = snd_pcm_hw_constraint_integer(substream->runtime, |
| 279 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 280 | if (ret < 0) |
| 281 | return ret; |
| 282 | |
| 283 | snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware); |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static struct snd_pcm_ops imx_pcm_ops = { |
| 288 | .open = snd_imx_open, |
| 289 | .ioctl = snd_pcm_lib_ioctl, |
| 290 | .hw_params = snd_imx_pcm_hw_params, |
| 291 | .hw_free = snd_imx_pcm_hw_free, |
| 292 | .prepare = snd_imx_pcm_prepare, |
| 293 | .trigger = snd_imx_pcm_trigger, |
| 294 | .pointer = snd_imx_pcm_pointer, |
| 295 | .mmap = snd_imx_pcm_mmap, |
| 296 | }; |
| 297 | |
| 298 | static struct snd_soc_platform imx_soc_platform_dma = { |
| 299 | .name = "imx-audio", |
| 300 | .pcm_ops = &imx_pcm_ops, |
| 301 | .pcm_new = imx_pcm_new, |
| 302 | .pcm_free = imx_pcm_free, |
| 303 | }; |
| 304 | |
| 305 | struct snd_soc_platform *imx_ssi_dma_mx2_init(struct platform_device *pdev, |
| 306 | struct imx_ssi *ssi) |
| 307 | { |
| 308 | ssi->dma_params_tx.burstsize = DMA_TXFIFO_BURST; |
| 309 | ssi->dma_params_rx.burstsize = DMA_RXFIFO_BURST; |
| 310 | |
| 311 | return &imx_soc_platform_dma; |
| 312 | } |
| 313 | |