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