Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ALSA PCM interface for the TI DAVINCI processor |
| 3 | * |
Vladimir Barinov | d6b5203 | 2008-09-29 23:14:11 +0400 | [diff] [blame] | 4 | * Author: Vladimir Barinov, <vbarinov@embeddedalley.com> |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 5 | * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com> |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 6 | * added SRAM ping/pong (C) 2008 Troy Kisky <troy.kisky@boundarydevices.com> |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/dma-mapping.h> |
Alexander Beregalov | 9cd28ab | 2008-12-13 16:25:27 +0300 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 19 | |
| 20 | #include <sound/core.h> |
| 21 | #include <sound/pcm.h> |
| 22 | #include <sound/pcm_params.h> |
| 23 | #include <sound/soc.h> |
| 24 | |
| 25 | #include <asm/dma.h> |
David Brownell | 82075af | 2009-05-14 12:41:22 -0700 | [diff] [blame] | 26 | #include <mach/edma.h> |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 27 | #include <mach/sram.h> |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 28 | |
| 29 | #include "davinci-pcm.h" |
| 30 | |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 31 | #ifdef DEBUG |
| 32 | static void print_buf_info(int slot, char *name) |
| 33 | { |
| 34 | struct edmacc_param p; |
| 35 | if (slot < 0) |
| 36 | return; |
| 37 | edma_read_slot(slot, &p); |
| 38 | printk(KERN_DEBUG "%s: 0x%x, opt=%x, src=%x, a_b_cnt=%x dst=%x\n", |
| 39 | name, slot, p.opt, p.src, p.a_b_cnt, p.dst); |
| 40 | printk(KERN_DEBUG " src_dst_bidx=%x link_bcntrld=%x src_dst_cidx=%x ccnt=%x\n", |
| 41 | p.src_dst_bidx, p.link_bcntrld, p.src_dst_cidx, p.ccnt); |
| 42 | } |
| 43 | #else |
| 44 | static void print_buf_info(int slot, char *name) |
| 45 | { |
| 46 | } |
| 47 | #endif |
| 48 | |
Ben Gardiner | 8e56d5b | 2011-05-24 14:50:16 -0400 | [diff] [blame] | 49 | #define DAVINCI_PCM_FMTBITS (\ |
| 50 | SNDRV_PCM_FMTBIT_S8 |\ |
| 51 | SNDRV_PCM_FMTBIT_U8 |\ |
| 52 | SNDRV_PCM_FMTBIT_S16_LE |\ |
| 53 | SNDRV_PCM_FMTBIT_S16_BE |\ |
| 54 | SNDRV_PCM_FMTBIT_U16_LE |\ |
| 55 | SNDRV_PCM_FMTBIT_U16_BE |\ |
| 56 | SNDRV_PCM_FMTBIT_S24_LE |\ |
| 57 | SNDRV_PCM_FMTBIT_S24_BE |\ |
| 58 | SNDRV_PCM_FMTBIT_U24_LE |\ |
| 59 | SNDRV_PCM_FMTBIT_U24_BE |\ |
| 60 | SNDRV_PCM_FMTBIT_S32_LE |\ |
| 61 | SNDRV_PCM_FMTBIT_S32_BE |\ |
| 62 | SNDRV_PCM_FMTBIT_U32_LE |\ |
| 63 | SNDRV_PCM_FMTBIT_U32_BE) |
| 64 | |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 65 | static struct snd_pcm_hardware pcm_hardware_playback = { |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 66 | .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 67 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | |
Ben Gardiner | 52e2c5d | 2011-05-24 14:50:20 -0400 | [diff] [blame] | 68 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME| |
| 69 | SNDRV_PCM_INFO_BATCH), |
Ben Gardiner | 8e56d5b | 2011-05-24 14:50:16 -0400 | [diff] [blame] | 70 | .formats = DAVINCI_PCM_FMTBITS, |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 71 | .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | |
| 72 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | |
| 73 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | |
| 74 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | |
| 75 | SNDRV_PCM_RATE_KNOT), |
| 76 | .rate_min = 8000, |
| 77 | .rate_max = 96000, |
| 78 | .channels_min = 2, |
Ben Gardiner | acb8e26 | 2011-05-24 14:50:17 -0400 | [diff] [blame] | 79 | .channels_max = 384, |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 80 | .buffer_bytes_max = 128 * 1024, |
| 81 | .period_bytes_min = 32, |
| 82 | .period_bytes_max = 8 * 1024, |
| 83 | .periods_min = 16, |
| 84 | .periods_max = 255, |
| 85 | .fifo_size = 0, |
| 86 | }; |
| 87 | |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 88 | static struct snd_pcm_hardware pcm_hardware_capture = { |
| 89 | .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 90 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | |
Ben Gardiner | 52e2c5d | 2011-05-24 14:50:20 -0400 | [diff] [blame] | 91 | SNDRV_PCM_INFO_PAUSE | |
| 92 | SNDRV_PCM_INFO_BATCH), |
Ben Gardiner | 8e56d5b | 2011-05-24 14:50:16 -0400 | [diff] [blame] | 93 | .formats = DAVINCI_PCM_FMTBITS, |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 94 | .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | |
| 95 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | |
| 96 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | |
| 97 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | |
| 98 | SNDRV_PCM_RATE_KNOT), |
| 99 | .rate_min = 8000, |
| 100 | .rate_max = 96000, |
| 101 | .channels_min = 2, |
Ben Gardiner | acb8e26 | 2011-05-24 14:50:17 -0400 | [diff] [blame] | 102 | .channels_max = 384, |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 103 | .buffer_bytes_max = 128 * 1024, |
| 104 | .period_bytes_min = 32, |
| 105 | .period_bytes_max = 8 * 1024, |
| 106 | .periods_min = 16, |
| 107 | .periods_max = 255, |
| 108 | .fifo_size = 0, |
| 109 | }; |
| 110 | |
| 111 | /* |
| 112 | * How ping/pong works.... |
| 113 | * |
| 114 | * Playback: |
| 115 | * ram_params - copys 2*ping_size from start of SDRAM to iram, |
| 116 | * links to ram_link2 |
| 117 | * ram_link2 - copys rest of SDRAM to iram in ping_size units, |
| 118 | * links to ram_link |
| 119 | * ram_link - copys entire SDRAM to iram in ping_size uints, |
| 120 | * links to self |
| 121 | * |
| 122 | * asp_params - same as asp_link[0] |
| 123 | * asp_link[0] - copys from lower half of iram to asp port |
| 124 | * links to asp_link[1], triggers iram copy event on completion |
| 125 | * asp_link[1] - copys from upper half of iram to asp port |
| 126 | * links to asp_link[0], triggers iram copy event on completion |
| 127 | * triggers interrupt only needed to let upper SOC levels update position |
| 128 | * in stream on completion |
| 129 | * |
| 130 | * When playback is started: |
| 131 | * ram_params started |
| 132 | * asp_params started |
| 133 | * |
| 134 | * Capture: |
| 135 | * ram_params - same as ram_link, |
| 136 | * links to ram_link |
| 137 | * ram_link - same as playback |
| 138 | * links to self |
| 139 | * |
| 140 | * asp_params - same as playback |
| 141 | * asp_link[0] - same as playback |
| 142 | * asp_link[1] - same as playback |
| 143 | * |
| 144 | * When capture is started: |
| 145 | * asp_params started |
| 146 | */ |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 147 | struct davinci_runtime_data { |
| 148 | spinlock_t lock; |
| 149 | int period; /* current DMA period */ |
Troy Kisky | 1587ea31 | 2009-11-18 17:49:52 -0700 | [diff] [blame] | 150 | int asp_channel; /* Master DMA channel */ |
| 151 | int asp_link[2]; /* asp parameter link channel, ping/pong */ |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 152 | struct davinci_pcm_dma_params *params; /* DMA params */ |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 153 | int ram_channel; |
| 154 | int ram_link; |
| 155 | int ram_link2; |
| 156 | struct edmacc_param asp_params; |
| 157 | struct edmacc_param ram_params; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 158 | }; |
| 159 | |
Ben Gardiner | 10ab3bf | 2011-05-24 14:50:19 -0400 | [diff] [blame] | 160 | static void davinci_pcm_period_elapsed(struct snd_pcm_substream *substream) |
| 161 | { |
| 162 | struct davinci_runtime_data *prtd = substream->runtime->private_data; |
| 163 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 164 | |
| 165 | prtd->period++; |
| 166 | if (unlikely(prtd->period >= runtime->periods)) |
| 167 | prtd->period = 0; |
| 168 | } |
| 169 | |
| 170 | static void davinci_pcm_period_reset(struct snd_pcm_substream *substream) |
| 171 | { |
| 172 | struct davinci_runtime_data *prtd = substream->runtime->private_data; |
| 173 | |
| 174 | prtd->period = 0; |
| 175 | } |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 176 | /* |
| 177 | * Not used with ping/pong |
| 178 | */ |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 179 | static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream) |
| 180 | { |
| 181 | struct davinci_runtime_data *prtd = substream->runtime->private_data; |
| 182 | struct snd_pcm_runtime *runtime = substream->runtime; |
Troy Kisky | 1587ea31 | 2009-11-18 17:49:52 -0700 | [diff] [blame] | 183 | int link = prtd->asp_link[0]; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 184 | unsigned int period_size; |
| 185 | unsigned int dma_offset; |
| 186 | dma_addr_t dma_pos; |
| 187 | dma_addr_t src, dst; |
| 188 | unsigned short src_bidx, dst_bidx; |
Chaithrika U S | 4fa9c1a | 2009-09-30 17:32:27 -0400 | [diff] [blame] | 189 | unsigned short src_cidx, dst_cidx; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 190 | unsigned int data_type; |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 191 | unsigned short acnt; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 192 | unsigned int count; |
Chaithrika U S | 4fa9c1a | 2009-09-30 17:32:27 -0400 | [diff] [blame] | 193 | unsigned int fifo_level; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 194 | |
| 195 | period_size = snd_pcm_lib_period_bytes(substream); |
| 196 | dma_offset = prtd->period * period_size; |
| 197 | dma_pos = runtime->dma_addr + dma_offset; |
Chaithrika U S | 4fa9c1a | 2009-09-30 17:32:27 -0400 | [diff] [blame] | 198 | fifo_level = prtd->params->fifo_level; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 199 | |
Alexander Beregalov | 9cd28ab | 2008-12-13 16:25:27 +0300 | [diff] [blame] | 200 | pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d " |
Troy Kisky | 1587ea31 | 2009-11-18 17:49:52 -0700 | [diff] [blame] | 201 | "dma_ptr = %x period_size=%x\n", link, dma_pos, period_size); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 202 | |
| 203 | data_type = prtd->params->data_type; |
| 204 | count = period_size / data_type; |
Chaithrika U S | 4fa9c1a | 2009-09-30 17:32:27 -0400 | [diff] [blame] | 205 | if (fifo_level) |
| 206 | count /= fifo_level; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 207 | |
| 208 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 209 | src = dma_pos; |
| 210 | dst = prtd->params->dma_addr; |
| 211 | src_bidx = data_type; |
| 212 | dst_bidx = 0; |
Chaithrika U S | 4fa9c1a | 2009-09-30 17:32:27 -0400 | [diff] [blame] | 213 | src_cidx = data_type * fifo_level; |
| 214 | dst_cidx = 0; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 215 | } else { |
| 216 | src = prtd->params->dma_addr; |
| 217 | dst = dma_pos; |
| 218 | src_bidx = 0; |
| 219 | dst_bidx = data_type; |
Chaithrika U S | 4fa9c1a | 2009-09-30 17:32:27 -0400 | [diff] [blame] | 220 | src_cidx = 0; |
| 221 | dst_cidx = data_type * fifo_level; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 224 | acnt = prtd->params->acnt; |
Troy Kisky | 1587ea31 | 2009-11-18 17:49:52 -0700 | [diff] [blame] | 225 | edma_set_src(link, src, INCR, W8BIT); |
| 226 | edma_set_dest(link, dst, INCR, W8BIT); |
Chaithrika U S | 4fa9c1a | 2009-09-30 17:32:27 -0400 | [diff] [blame] | 227 | |
Troy Kisky | 1587ea31 | 2009-11-18 17:49:52 -0700 | [diff] [blame] | 228 | edma_set_src_index(link, src_bidx, src_cidx); |
| 229 | edma_set_dest_index(link, dst_bidx, dst_cidx); |
Chaithrika U S | 4fa9c1a | 2009-09-30 17:32:27 -0400 | [diff] [blame] | 230 | |
| 231 | if (!fifo_level) |
Troy Kisky | 1587ea31 | 2009-11-18 17:49:52 -0700 | [diff] [blame] | 232 | edma_set_transfer_params(link, acnt, count, 1, 0, ASYNC); |
Chaithrika U S | 4fa9c1a | 2009-09-30 17:32:27 -0400 | [diff] [blame] | 233 | else |
Troy Kisky | 1587ea31 | 2009-11-18 17:49:52 -0700 | [diff] [blame] | 234 | edma_set_transfer_params(link, acnt, fifo_level, count, |
Chaithrika U S | 4fa9c1a | 2009-09-30 17:32:27 -0400 | [diff] [blame] | 235 | fifo_level, ABSYNC); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Troy Kisky | 1587ea31 | 2009-11-18 17:49:52 -0700 | [diff] [blame] | 238 | static void davinci_pcm_dma_irq(unsigned link, u16 ch_status, void *data) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 239 | { |
| 240 | struct snd_pcm_substream *substream = data; |
| 241 | struct davinci_runtime_data *prtd = substream->runtime->private_data; |
| 242 | |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 243 | print_buf_info(prtd->ram_channel, "i ram_channel"); |
Troy Kisky | 1587ea31 | 2009-11-18 17:49:52 -0700 | [diff] [blame] | 244 | pr_debug("davinci_pcm: link=%d, status=0x%x\n", link, ch_status); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 245 | |
| 246 | if (unlikely(ch_status != DMA_COMPLETE)) |
| 247 | return; |
| 248 | |
| 249 | if (snd_pcm_running(substream)) { |
Ben Gardiner | 52e2c5d | 2011-05-24 14:50:20 -0400 | [diff] [blame] | 250 | spin_lock(&prtd->lock); |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 251 | if (prtd->ram_channel < 0) { |
| 252 | /* No ping/pong must fix up link dma data*/ |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 253 | davinci_pcm_enqueue_dma(substream); |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 254 | } |
Ben Gardiner | 52e2c5d | 2011-05-24 14:50:20 -0400 | [diff] [blame] | 255 | davinci_pcm_period_elapsed(substream); |
| 256 | spin_unlock(&prtd->lock); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 257 | snd_pcm_period_elapsed(substream); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 261 | static int allocate_sram(struct snd_pcm_substream *substream, unsigned size, |
| 262 | struct snd_pcm_hardware *ppcm) |
| 263 | { |
| 264 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
| 265 | struct snd_dma_buffer *iram_dma = NULL; |
| 266 | dma_addr_t iram_phys = 0; |
| 267 | void *iram_virt = NULL; |
| 268 | |
| 269 | if (buf->private_data || !size) |
| 270 | return 0; |
| 271 | |
| 272 | ppcm->period_bytes_max = size; |
| 273 | iram_virt = sram_alloc(size, &iram_phys); |
| 274 | if (!iram_virt) |
| 275 | goto exit1; |
| 276 | iram_dma = kzalloc(sizeof(*iram_dma), GFP_KERNEL); |
| 277 | if (!iram_dma) |
| 278 | goto exit2; |
| 279 | iram_dma->area = iram_virt; |
| 280 | iram_dma->addr = iram_phys; |
| 281 | memset(iram_dma->area, 0, size); |
| 282 | iram_dma->bytes = size; |
| 283 | buf->private_data = iram_dma; |
| 284 | return 0; |
| 285 | exit2: |
| 286 | if (iram_virt) |
| 287 | sram_free(iram_virt, size); |
| 288 | exit1: |
| 289 | return -ENOMEM; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Only used with ping/pong. |
| 294 | * This is called after runtime->dma_addr, period_bytes and data_type are valid |
| 295 | */ |
| 296 | static int ping_pong_dma_setup(struct snd_pcm_substream *substream) |
| 297 | { |
| 298 | unsigned short ram_src_cidx, ram_dst_cidx; |
| 299 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 300 | struct davinci_runtime_data *prtd = runtime->private_data; |
| 301 | struct snd_dma_buffer *iram_dma = |
| 302 | (struct snd_dma_buffer *)substream->dma_buffer.private_data; |
| 303 | struct davinci_pcm_dma_params *params = prtd->params; |
| 304 | unsigned int data_type = params->data_type; |
| 305 | unsigned int acnt = params->acnt; |
| 306 | /* divide by 2 for ping/pong */ |
| 307 | unsigned int ping_size = snd_pcm_lib_period_bytes(substream) >> 1; |
| 308 | int link = prtd->asp_link[1]; |
| 309 | unsigned int fifo_level = prtd->params->fifo_level; |
| 310 | unsigned int count; |
| 311 | if ((data_type == 0) || (data_type > 4)) { |
| 312 | printk(KERN_ERR "%s: data_type=%i\n", __func__, data_type); |
| 313 | return -EINVAL; |
| 314 | } |
| 315 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 316 | dma_addr_t asp_src_pong = iram_dma->addr + ping_size; |
| 317 | ram_src_cidx = ping_size; |
| 318 | ram_dst_cidx = -ping_size; |
| 319 | edma_set_src(link, asp_src_pong, INCR, W8BIT); |
| 320 | |
| 321 | link = prtd->asp_link[0]; |
| 322 | edma_set_src_index(link, data_type, data_type * fifo_level); |
| 323 | link = prtd->asp_link[1]; |
| 324 | edma_set_src_index(link, data_type, data_type * fifo_level); |
| 325 | |
| 326 | link = prtd->ram_link; |
| 327 | edma_set_src(link, runtime->dma_addr, INCR, W32BIT); |
| 328 | } else { |
| 329 | dma_addr_t asp_dst_pong = iram_dma->addr + ping_size; |
| 330 | ram_src_cidx = -ping_size; |
| 331 | ram_dst_cidx = ping_size; |
| 332 | edma_set_dest(link, asp_dst_pong, INCR, W8BIT); |
| 333 | |
| 334 | link = prtd->asp_link[0]; |
| 335 | edma_set_dest_index(link, data_type, data_type * fifo_level); |
| 336 | link = prtd->asp_link[1]; |
| 337 | edma_set_dest_index(link, data_type, data_type * fifo_level); |
| 338 | |
| 339 | link = prtd->ram_link; |
| 340 | edma_set_dest(link, runtime->dma_addr, INCR, W32BIT); |
| 341 | } |
| 342 | |
| 343 | if (!fifo_level) { |
| 344 | count = ping_size / data_type; |
| 345 | edma_set_transfer_params(prtd->asp_link[0], acnt, count, |
| 346 | 1, 0, ASYNC); |
| 347 | edma_set_transfer_params(prtd->asp_link[1], acnt, count, |
| 348 | 1, 0, ASYNC); |
| 349 | } else { |
| 350 | count = ping_size / (data_type * fifo_level); |
| 351 | edma_set_transfer_params(prtd->asp_link[0], acnt, fifo_level, |
| 352 | count, fifo_level, ABSYNC); |
| 353 | edma_set_transfer_params(prtd->asp_link[1], acnt, fifo_level, |
| 354 | count, fifo_level, ABSYNC); |
| 355 | } |
| 356 | |
| 357 | link = prtd->ram_link; |
| 358 | edma_set_src_index(link, ping_size, ram_src_cidx); |
| 359 | edma_set_dest_index(link, ping_size, ram_dst_cidx); |
| 360 | edma_set_transfer_params(link, ping_size, 2, |
| 361 | runtime->periods, 2, ASYNC); |
| 362 | |
| 363 | /* init master params */ |
| 364 | edma_read_slot(prtd->asp_link[0], &prtd->asp_params); |
| 365 | edma_read_slot(prtd->ram_link, &prtd->ram_params); |
| 366 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 367 | struct edmacc_param p_ram; |
| 368 | /* Copy entire iram buffer before playback started */ |
| 369 | prtd->ram_params.a_b_cnt = (1 << 16) | (ping_size << 1); |
| 370 | /* 0 dst_bidx */ |
| 371 | prtd->ram_params.src_dst_bidx = (ping_size << 1); |
| 372 | /* 0 dst_cidx */ |
| 373 | prtd->ram_params.src_dst_cidx = (ping_size << 1); |
| 374 | prtd->ram_params.ccnt = 1; |
| 375 | |
| 376 | /* Skip 1st period */ |
| 377 | edma_read_slot(prtd->ram_link, &p_ram); |
| 378 | p_ram.src += (ping_size << 1); |
| 379 | p_ram.ccnt -= 1; |
| 380 | edma_write_slot(prtd->ram_link2, &p_ram); |
| 381 | /* |
| 382 | * When 1st started, ram -> iram dma channel will fill the |
| 383 | * entire iram. Then, whenever a ping/pong asp buffer finishes, |
| 384 | * 1/2 iram will be filled. |
| 385 | */ |
| 386 | prtd->ram_params.link_bcntrld = |
| 387 | EDMA_CHAN_SLOT(prtd->ram_link2) << 5; |
| 388 | } |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | /* 1 asp tx or rx channel using 2 parameter channels |
| 393 | * 1 ram to/from iram channel using 1 parameter channel |
| 394 | * |
| 395 | * Playback |
| 396 | * ram copy channel kicks off first, |
| 397 | * 1st ram copy of entire iram buffer completion kicks off asp channel |
| 398 | * asp tcc always kicks off ram copy of 1/2 iram buffer |
| 399 | * |
| 400 | * Record |
| 401 | * asp channel starts, tcc kicks off ram copy |
| 402 | */ |
| 403 | static int request_ping_pong(struct snd_pcm_substream *substream, |
| 404 | struct davinci_runtime_data *prtd, |
| 405 | struct snd_dma_buffer *iram_dma) |
| 406 | { |
| 407 | dma_addr_t asp_src_ping; |
| 408 | dma_addr_t asp_dst_ping; |
| 409 | int link; |
| 410 | struct davinci_pcm_dma_params *params = prtd->params; |
| 411 | |
| 412 | /* Request ram master channel */ |
| 413 | link = prtd->ram_channel = edma_alloc_channel(EDMA_CHANNEL_ANY, |
| 414 | davinci_pcm_dma_irq, substream, |
Sekhar Nori | 48519f0 | 2010-07-19 12:31:16 +0530 | [diff] [blame] | 415 | prtd->params->ram_chan_q); |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 416 | if (link < 0) |
| 417 | goto exit1; |
| 418 | |
| 419 | /* Request ram link channel */ |
| 420 | link = prtd->ram_link = edma_alloc_slot( |
| 421 | EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY); |
| 422 | if (link < 0) |
| 423 | goto exit2; |
| 424 | |
| 425 | link = prtd->asp_link[1] = edma_alloc_slot( |
| 426 | EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY); |
| 427 | if (link < 0) |
| 428 | goto exit3; |
| 429 | |
| 430 | prtd->ram_link2 = -1; |
| 431 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 432 | link = prtd->ram_link2 = edma_alloc_slot( |
| 433 | EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY); |
| 434 | if (link < 0) |
| 435 | goto exit4; |
| 436 | } |
| 437 | /* circle ping-pong buffers */ |
| 438 | edma_link(prtd->asp_link[0], prtd->asp_link[1]); |
| 439 | edma_link(prtd->asp_link[1], prtd->asp_link[0]); |
| 440 | /* circle ram buffers */ |
| 441 | edma_link(prtd->ram_link, prtd->ram_link); |
| 442 | |
| 443 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 444 | asp_src_ping = iram_dma->addr; |
| 445 | asp_dst_ping = params->dma_addr; /* fifo */ |
| 446 | } else { |
| 447 | asp_src_ping = params->dma_addr; /* fifo */ |
| 448 | asp_dst_ping = iram_dma->addr; |
| 449 | } |
| 450 | /* ping */ |
| 451 | link = prtd->asp_link[0]; |
| 452 | edma_set_src(link, asp_src_ping, INCR, W16BIT); |
| 453 | edma_set_dest(link, asp_dst_ping, INCR, W16BIT); |
| 454 | edma_set_src_index(link, 0, 0); |
| 455 | edma_set_dest_index(link, 0, 0); |
| 456 | |
| 457 | edma_read_slot(link, &prtd->asp_params); |
| 458 | prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f) | TCINTEN); |
Ben Gardiner | fb1e9703 | 2011-05-24 14:50:15 -0400 | [diff] [blame] | 459 | prtd->asp_params.opt |= TCCHEN | |
| 460 | EDMA_TCC(prtd->ram_channel & 0x3f); |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 461 | edma_write_slot(link, &prtd->asp_params); |
| 462 | |
| 463 | /* pong */ |
| 464 | link = prtd->asp_link[1]; |
| 465 | edma_set_src(link, asp_src_ping, INCR, W16BIT); |
| 466 | edma_set_dest(link, asp_dst_ping, INCR, W16BIT); |
| 467 | edma_set_src_index(link, 0, 0); |
| 468 | edma_set_dest_index(link, 0, 0); |
| 469 | |
| 470 | edma_read_slot(link, &prtd->asp_params); |
| 471 | prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f)); |
| 472 | /* interrupt after every pong completion */ |
| 473 | prtd->asp_params.opt |= TCINTEN | TCCHEN | |
Ben Gardiner | fb1e9703 | 2011-05-24 14:50:15 -0400 | [diff] [blame] | 474 | EDMA_TCC(prtd->ram_channel & 0x3f); |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 475 | edma_write_slot(link, &prtd->asp_params); |
| 476 | |
| 477 | /* ram */ |
| 478 | link = prtd->ram_link; |
| 479 | edma_set_src(link, iram_dma->addr, INCR, W32BIT); |
| 480 | edma_set_dest(link, iram_dma->addr, INCR, W32BIT); |
| 481 | pr_debug("%s: audio dma channels/slots in use for ram:%u %u %u," |
| 482 | "for asp:%u %u %u\n", __func__, |
| 483 | prtd->ram_channel, prtd->ram_link, prtd->ram_link2, |
| 484 | prtd->asp_channel, prtd->asp_link[0], |
| 485 | prtd->asp_link[1]); |
| 486 | return 0; |
| 487 | exit4: |
| 488 | edma_free_channel(prtd->asp_link[1]); |
| 489 | prtd->asp_link[1] = -1; |
| 490 | exit3: |
| 491 | edma_free_channel(prtd->ram_link); |
| 492 | prtd->ram_link = -1; |
| 493 | exit2: |
| 494 | edma_free_channel(prtd->ram_channel); |
| 495 | prtd->ram_channel = -1; |
| 496 | exit1: |
| 497 | return link; |
| 498 | } |
| 499 | |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 500 | static int davinci_pcm_dma_request(struct snd_pcm_substream *substream) |
| 501 | { |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 502 | struct snd_dma_buffer *iram_dma; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 503 | struct davinci_runtime_data *prtd = substream->runtime->private_data; |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 504 | struct davinci_pcm_dma_params *params = prtd->params; |
| 505 | int link; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 506 | |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 507 | if (!params) |
| 508 | return -ENODEV; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 509 | |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 510 | /* Request asp master DMA channel */ |
| 511 | link = prtd->asp_channel = edma_alloc_channel(params->channel, |
Sekhar Nori | 48519f0 | 2010-07-19 12:31:16 +0530 | [diff] [blame] | 512 | davinci_pcm_dma_irq, substream, |
| 513 | prtd->params->asp_chan_q); |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 514 | if (link < 0) |
| 515 | goto exit1; |
| 516 | |
| 517 | /* Request asp link channels */ |
| 518 | link = prtd->asp_link[0] = edma_alloc_slot( |
| 519 | EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY); |
| 520 | if (link < 0) |
| 521 | goto exit2; |
| 522 | |
| 523 | iram_dma = (struct snd_dma_buffer *)substream->dma_buffer.private_data; |
| 524 | if (iram_dma) { |
| 525 | if (request_ping_pong(substream, prtd, iram_dma) == 0) |
| 526 | return 0; |
| 527 | printk(KERN_WARNING "%s: dma channel allocation failed," |
| 528 | "not using sram\n", __func__); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 529 | } |
| 530 | |
David Brownell | 82075af | 2009-05-14 12:41:22 -0700 | [diff] [blame] | 531 | /* Issue transfer completion IRQ when the channel completes a |
| 532 | * transfer, then always reload from the same slot (by a kind |
| 533 | * of loopback link). The completion IRQ handler will update |
| 534 | * the reload slot with a new buffer. |
| 535 | * |
| 536 | * REVISIT save p_ram here after setting up everything except |
| 537 | * the buffer and its length (ccnt) ... use it as a template |
| 538 | * so davinci_pcm_enqueue_dma() takes less time in IRQ. |
| 539 | */ |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 540 | edma_read_slot(link, &prtd->asp_params); |
| 541 | prtd->asp_params.opt |= TCINTEN | |
| 542 | EDMA_TCC(EDMA_CHAN_SLOT(prtd->asp_channel)); |
| 543 | prtd->asp_params.link_bcntrld = EDMA_CHAN_SLOT(link) << 5; |
| 544 | edma_write_slot(link, &prtd->asp_params); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 545 | return 0; |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 546 | exit2: |
| 547 | edma_free_channel(prtd->asp_channel); |
| 548 | prtd->asp_channel = -1; |
| 549 | exit1: |
| 550 | return link; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 554 | { |
| 555 | struct davinci_runtime_data *prtd = substream->runtime->private_data; |
| 556 | int ret = 0; |
| 557 | |
| 558 | spin_lock(&prtd->lock); |
| 559 | |
| 560 | switch (cmd) { |
| 561 | case SNDRV_PCM_TRIGGER_START: |
Ben Gardiner | ef39eb6 | 2011-05-24 14:50:18 -0400 | [diff] [blame] | 562 | edma_start(prtd->asp_channel); |
| 563 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 564 | prtd->ram_channel >= 0) { |
| 565 | /* copy 1st iram buffer */ |
| 566 | edma_start(prtd->ram_channel); |
| 567 | } |
| 568 | break; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 569 | case SNDRV_PCM_TRIGGER_RESUME: |
| 570 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Troy Kisky | 2b7b250 | 2009-11-18 17:49:54 -0700 | [diff] [blame] | 571 | edma_resume(prtd->asp_channel); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 572 | break; |
| 573 | case SNDRV_PCM_TRIGGER_STOP: |
| 574 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 575 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Troy Kisky | 2b7b250 | 2009-11-18 17:49:54 -0700 | [diff] [blame] | 576 | edma_pause(prtd->asp_channel); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 577 | break; |
| 578 | default: |
| 579 | ret = -EINVAL; |
| 580 | break; |
| 581 | } |
| 582 | |
| 583 | spin_unlock(&prtd->lock); |
| 584 | |
| 585 | return ret; |
| 586 | } |
| 587 | |
| 588 | static int davinci_pcm_prepare(struct snd_pcm_substream *substream) |
| 589 | { |
| 590 | struct davinci_runtime_data *prtd = substream->runtime->private_data; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 591 | |
Ben Gardiner | 52e2c5d | 2011-05-24 14:50:20 -0400 | [diff] [blame] | 592 | davinci_pcm_period_reset(substream); |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 593 | if (prtd->ram_channel >= 0) { |
| 594 | int ret = ping_pong_dma_setup(substream); |
| 595 | if (ret < 0) |
| 596 | return ret; |
| 597 | |
| 598 | edma_write_slot(prtd->ram_channel, &prtd->ram_params); |
| 599 | edma_write_slot(prtd->asp_channel, &prtd->asp_params); |
| 600 | |
| 601 | print_buf_info(prtd->ram_channel, "ram_channel"); |
| 602 | print_buf_info(prtd->ram_link, "ram_link"); |
| 603 | print_buf_info(prtd->ram_link2, "ram_link2"); |
| 604 | print_buf_info(prtd->asp_channel, "asp_channel"); |
| 605 | print_buf_info(prtd->asp_link[0], "asp_link[0]"); |
| 606 | print_buf_info(prtd->asp_link[1], "asp_link[1]"); |
| 607 | |
Ben Gardiner | bb5b5fd | 2011-05-25 09:27:22 -0400 | [diff] [blame] | 608 | /* |
| 609 | * There is a phase offset of 2 periods between the position |
| 610 | * used by dma setup and the position reported in the pointer |
| 611 | * function. |
| 612 | * |
| 613 | * The phase offset, when not using ping-pong buffers, is due to |
| 614 | * the two consecutive calls to davinci_pcm_enqueue_dma() below. |
| 615 | * |
| 616 | * Whereas here, with ping-pong buffers, the phase is due to |
| 617 | * there being an entire buffer transfer complete before the |
| 618 | * first dma completion event triggers davinci_pcm_dma_irq(). |
| 619 | */ |
Ben Gardiner | 52e2c5d | 2011-05-24 14:50:20 -0400 | [diff] [blame] | 620 | davinci_pcm_period_elapsed(substream); |
| 621 | davinci_pcm_period_elapsed(substream); |
| 622 | |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 623 | return 0; |
| 624 | } |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 625 | davinci_pcm_enqueue_dma(substream); |
Ben Gardiner | 52e2c5d | 2011-05-24 14:50:20 -0400 | [diff] [blame] | 626 | davinci_pcm_period_elapsed(substream); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 627 | |
David Brownell | 82075af | 2009-05-14 12:41:22 -0700 | [diff] [blame] | 628 | /* Copy self-linked parameter RAM entry into master channel */ |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 629 | edma_read_slot(prtd->asp_link[0], &prtd->asp_params); |
| 630 | edma_write_slot(prtd->asp_channel, &prtd->asp_params); |
Troy Kisky | 6e54147 | 2009-07-07 17:36:06 -0700 | [diff] [blame] | 631 | davinci_pcm_enqueue_dma(substream); |
Ben Gardiner | 52e2c5d | 2011-05-24 14:50:20 -0400 | [diff] [blame] | 632 | davinci_pcm_period_elapsed(substream); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | static snd_pcm_uframes_t |
| 638 | davinci_pcm_pointer(struct snd_pcm_substream *substream) |
| 639 | { |
| 640 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 641 | struct davinci_runtime_data *prtd = runtime->private_data; |
| 642 | unsigned int offset; |
Troy Kisky | 1587ea31 | 2009-11-18 17:49:52 -0700 | [diff] [blame] | 643 | int asp_count; |
Ben Gardiner | 52e2c5d | 2011-05-24 14:50:20 -0400 | [diff] [blame] | 644 | unsigned int period_size = snd_pcm_lib_period_bytes(substream); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 645 | |
Ben Gardiner | bb5b5fd | 2011-05-25 09:27:22 -0400 | [diff] [blame] | 646 | /* |
| 647 | * There is a phase offset of 2 periods between the position used by dma |
| 648 | * setup and the position reported in the pointer function. Either +2 in |
| 649 | * the dma setup or -2 here in the pointer function (with wrapping, |
| 650 | * both) accounts for this offset -- choose the latter since it makes |
| 651 | * the first-time setup clearer. |
| 652 | */ |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 653 | spin_lock(&prtd->lock); |
Ben Gardiner | 52e2c5d | 2011-05-24 14:50:20 -0400 | [diff] [blame] | 654 | asp_count = prtd->period - 2; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 655 | spin_unlock(&prtd->lock); |
| 656 | |
Ben Gardiner | 52e2c5d | 2011-05-24 14:50:20 -0400 | [diff] [blame] | 657 | if (asp_count < 0) |
| 658 | asp_count += runtime->periods; |
| 659 | asp_count *= period_size; |
| 660 | |
Troy Kisky | 1587ea31 | 2009-11-18 17:49:52 -0700 | [diff] [blame] | 661 | offset = bytes_to_frames(runtime, asp_count); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 662 | if (offset >= runtime->buffer_size) |
| 663 | offset = 0; |
| 664 | |
| 665 | return offset; |
| 666 | } |
| 667 | |
| 668 | static int davinci_pcm_open(struct snd_pcm_substream *substream) |
| 669 | { |
| 670 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 671 | struct davinci_runtime_data *prtd; |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 672 | struct snd_pcm_hardware *ppcm; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 673 | int ret = 0; |
Troy Kisky | 81ac55a | 2009-09-11 14:29:02 -0700 | [diff] [blame] | 674 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 675 | struct davinci_pcm_dma_params *pa; |
Troy Kisky | 57512c6 | 2009-11-16 16:52:31 -0700 | [diff] [blame] | 676 | struct davinci_pcm_dma_params *params; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 677 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 678 | pa = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
Troy Kisky | 57512c6 | 2009-11-16 16:52:31 -0700 | [diff] [blame] | 679 | if (!pa) |
Troy Kisky | 81ac55a | 2009-09-11 14:29:02 -0700 | [diff] [blame] | 680 | return -ENODEV; |
Troy Kisky | 57512c6 | 2009-11-16 16:52:31 -0700 | [diff] [blame] | 681 | params = &pa[substream->stream]; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 682 | |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 683 | ppcm = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? |
| 684 | &pcm_hardware_playback : &pcm_hardware_capture; |
| 685 | allocate_sram(substream, params->sram_size, ppcm); |
| 686 | snd_soc_set_runtime_hwparams(substream, ppcm); |
Troy Kisky | 6a90d53 | 2009-08-06 16:55:32 -0700 | [diff] [blame] | 687 | /* ensure that buffer size is a multiple of period size */ |
| 688 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 689 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 690 | if (ret < 0) |
| 691 | return ret; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 692 | |
| 693 | prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL); |
| 694 | if (prtd == NULL) |
| 695 | return -ENOMEM; |
| 696 | |
| 697 | spin_lock_init(&prtd->lock); |
Troy Kisky | 81ac55a | 2009-09-11 14:29:02 -0700 | [diff] [blame] | 698 | prtd->params = params; |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 699 | prtd->asp_channel = -1; |
| 700 | prtd->asp_link[0] = prtd->asp_link[1] = -1; |
| 701 | prtd->ram_channel = -1; |
| 702 | prtd->ram_link = -1; |
| 703 | prtd->ram_link2 = -1; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 704 | |
| 705 | runtime->private_data = prtd; |
| 706 | |
| 707 | ret = davinci_pcm_dma_request(substream); |
| 708 | if (ret) { |
| 709 | printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n"); |
| 710 | kfree(prtd); |
| 711 | } |
| 712 | |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | static int davinci_pcm_close(struct snd_pcm_substream *substream) |
| 717 | { |
| 718 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 719 | struct davinci_runtime_data *prtd = runtime->private_data; |
| 720 | |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 721 | if (prtd->ram_channel >= 0) |
| 722 | edma_stop(prtd->ram_channel); |
| 723 | if (prtd->asp_channel >= 0) |
| 724 | edma_stop(prtd->asp_channel); |
| 725 | if (prtd->asp_link[0] >= 0) |
| 726 | edma_unlink(prtd->asp_link[0]); |
| 727 | if (prtd->asp_link[1] >= 0) |
| 728 | edma_unlink(prtd->asp_link[1]); |
| 729 | if (prtd->ram_link >= 0) |
| 730 | edma_unlink(prtd->ram_link); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 731 | |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 732 | if (prtd->asp_link[0] >= 0) |
| 733 | edma_free_slot(prtd->asp_link[0]); |
| 734 | if (prtd->asp_link[1] >= 0) |
| 735 | edma_free_slot(prtd->asp_link[1]); |
| 736 | if (prtd->asp_channel >= 0) |
| 737 | edma_free_channel(prtd->asp_channel); |
| 738 | if (prtd->ram_link >= 0) |
| 739 | edma_free_slot(prtd->ram_link); |
| 740 | if (prtd->ram_link2 >= 0) |
| 741 | edma_free_slot(prtd->ram_link2); |
| 742 | if (prtd->ram_channel >= 0) |
| 743 | edma_free_channel(prtd->ram_channel); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 744 | |
| 745 | kfree(prtd); |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | static int davinci_pcm_hw_params(struct snd_pcm_substream *substream, |
| 751 | struct snd_pcm_hw_params *hw_params) |
| 752 | { |
| 753 | return snd_pcm_lib_malloc_pages(substream, |
| 754 | params_buffer_bytes(hw_params)); |
| 755 | } |
| 756 | |
| 757 | static int davinci_pcm_hw_free(struct snd_pcm_substream *substream) |
| 758 | { |
| 759 | return snd_pcm_lib_free_pages(substream); |
| 760 | } |
| 761 | |
| 762 | static int davinci_pcm_mmap(struct snd_pcm_substream *substream, |
| 763 | struct vm_area_struct *vma) |
| 764 | { |
| 765 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 766 | |
| 767 | return dma_mmap_writecombine(substream->pcm->card->dev, vma, |
| 768 | runtime->dma_area, |
| 769 | runtime->dma_addr, |
| 770 | runtime->dma_bytes); |
| 771 | } |
| 772 | |
Mark Brown | b2a19d0 | 2009-01-17 19:14:26 +0000 | [diff] [blame] | 773 | static struct snd_pcm_ops davinci_pcm_ops = { |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 774 | .open = davinci_pcm_open, |
| 775 | .close = davinci_pcm_close, |
| 776 | .ioctl = snd_pcm_lib_ioctl, |
| 777 | .hw_params = davinci_pcm_hw_params, |
| 778 | .hw_free = davinci_pcm_hw_free, |
| 779 | .prepare = davinci_pcm_prepare, |
| 780 | .trigger = davinci_pcm_trigger, |
| 781 | .pointer = davinci_pcm_pointer, |
| 782 | .mmap = davinci_pcm_mmap, |
| 783 | }; |
| 784 | |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 785 | static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream, |
| 786 | size_t size) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 787 | { |
| 788 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 789 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 790 | |
| 791 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 792 | buf->dev.dev = pcm->card->dev; |
| 793 | buf->private_data = NULL; |
| 794 | buf->area = dma_alloc_writecombine(pcm->card->dev, size, |
| 795 | &buf->addr, GFP_KERNEL); |
| 796 | |
Alexander Beregalov | 9cd28ab | 2008-12-13 16:25:27 +0300 | [diff] [blame] | 797 | pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, " |
| 798 | "size=%d\n", (void *) buf->area, (void *) buf->addr, size); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 799 | |
| 800 | if (!buf->area) |
| 801 | return -ENOMEM; |
| 802 | |
| 803 | buf->bytes = size; |
| 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | static void davinci_pcm_free(struct snd_pcm *pcm) |
| 808 | { |
| 809 | struct snd_pcm_substream *substream; |
| 810 | struct snd_dma_buffer *buf; |
| 811 | int stream; |
| 812 | |
| 813 | for (stream = 0; stream < 2; stream++) { |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 814 | struct snd_dma_buffer *iram_dma; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 815 | substream = pcm->streams[stream].substream; |
| 816 | if (!substream) |
| 817 | continue; |
| 818 | |
| 819 | buf = &substream->dma_buffer; |
| 820 | if (!buf->area) |
| 821 | continue; |
| 822 | |
| 823 | dma_free_writecombine(pcm->card->dev, buf->bytes, |
| 824 | buf->area, buf->addr); |
| 825 | buf->area = NULL; |
Joe Perches | 4726a57 | 2010-07-12 13:50:27 -0700 | [diff] [blame] | 826 | iram_dma = buf->private_data; |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 827 | if (iram_dma) { |
| 828 | sram_free(iram_dma->area, iram_dma->bytes); |
| 829 | kfree(iram_dma); |
| 830 | } |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
| 834 | static u64 davinci_pcm_dmamask = 0xffffffff; |
| 835 | |
| 836 | static int davinci_pcm_new(struct snd_card *card, |
Liam Girdwood | 9cb132d | 2008-07-07 16:07:42 +0100 | [diff] [blame] | 837 | struct snd_soc_dai *dai, struct snd_pcm *pcm) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 838 | { |
| 839 | int ret; |
| 840 | |
| 841 | if (!card->dev->dma_mask) |
| 842 | card->dev->dma_mask = &davinci_pcm_dmamask; |
| 843 | if (!card->dev->coherent_dma_mask) |
| 844 | card->dev->coherent_dma_mask = 0xffffffff; |
| 845 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 846 | if (dai->driver->playback.channels_min) { |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 847 | ret = davinci_pcm_preallocate_dma_buffer(pcm, |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 848 | SNDRV_PCM_STREAM_PLAYBACK, |
| 849 | pcm_hardware_playback.buffer_bytes_max); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 850 | if (ret) |
| 851 | return ret; |
| 852 | } |
| 853 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 854 | if (dai->driver->capture.channels_min) { |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 855 | ret = davinci_pcm_preallocate_dma_buffer(pcm, |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 856 | SNDRV_PCM_STREAM_CAPTURE, |
| 857 | pcm_hardware_capture.buffer_bytes_max); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 858 | if (ret) |
| 859 | return ret; |
| 860 | } |
| 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 865 | static struct snd_soc_platform_driver davinci_soc_platform = { |
| 866 | .ops = &davinci_pcm_ops, |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 867 | .pcm_new = davinci_pcm_new, |
| 868 | .pcm_free = davinci_pcm_free, |
| 869 | }; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 870 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 871 | static int __devinit davinci_soc_platform_probe(struct platform_device *pdev) |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 872 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 873 | return snd_soc_register_platform(&pdev->dev, &davinci_soc_platform); |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 874 | } |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 875 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 876 | static int __devexit davinci_soc_platform_remove(struct platform_device *pdev) |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 877 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 878 | snd_soc_unregister_platform(&pdev->dev); |
| 879 | return 0; |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 880 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 881 | |
| 882 | static struct platform_driver davinci_pcm_driver = { |
| 883 | .driver = { |
| 884 | .name = "davinci-pcm-audio", |
| 885 | .owner = THIS_MODULE, |
| 886 | }, |
| 887 | |
| 888 | .probe = davinci_soc_platform_probe, |
| 889 | .remove = __devexit_p(davinci_soc_platform_remove), |
| 890 | }; |
| 891 | |
| 892 | static int __init snd_davinci_pcm_init(void) |
| 893 | { |
| 894 | return platform_driver_register(&davinci_pcm_driver); |
| 895 | } |
| 896 | module_init(snd_davinci_pcm_init); |
| 897 | |
| 898 | static void __exit snd_davinci_pcm_exit(void) |
| 899 | { |
| 900 | platform_driver_unregister(&davinci_pcm_driver); |
| 901 | } |
| 902 | module_exit(snd_davinci_pcm_exit); |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 903 | |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 904 | MODULE_AUTHOR("Vladimir Barinov"); |
| 905 | MODULE_DESCRIPTION("TI DAVINCI PCM DMA module"); |
| 906 | MODULE_LICENSE("GPL"); |