Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * omap-pcm.c -- ALSA PCM interface for the OMAP SoC |
| 3 | * |
| 4 | * Copyright (C) 2008 Nokia Corporation |
| 5 | * |
Jarkko Nikula | 7ec41ee | 2011-08-11 15:44:57 +0300 | [diff] [blame] | 6 | * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com> |
Peter Ujfalusi | 1c7687b | 2011-05-03 18:14:43 +0300 | [diff] [blame] | 7 | * Peter Ujfalusi <peter.ujfalusi@ti.com> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * version 2 as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 21 | * 02110-1301 USA |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <linux/dma-mapping.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 28 | #include <sound/core.h> |
| 29 | #include <sound/pcm.h> |
| 30 | #include <sound/pcm_params.h> |
| 31 | #include <sound/soc.h> |
| 32 | |
Tony Lindgren | 7d7e1eb | 2012-08-27 17:43:01 -0700 | [diff] [blame] | 33 | #include <plat/cpu.h> |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 34 | #include <plat/dma.h> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 35 | #include "omap-pcm.h" |
| 36 | |
| 37 | static const struct snd_pcm_hardware omap_pcm_hardware = { |
| 38 | .info = SNDRV_PCM_INFO_MMAP | |
| 39 | SNDRV_PCM_INFO_MMAP_VALID | |
| 40 | SNDRV_PCM_INFO_INTERLEAVED | |
| 41 | SNDRV_PCM_INFO_PAUSE | |
Peter Ujfalusi | b417382 | 2011-05-12 13:04:55 +0300 | [diff] [blame] | 42 | SNDRV_PCM_INFO_RESUME | |
| 43 | SNDRV_PCM_INFO_NO_PERIOD_WAKEUP, |
Misael Lopez Cruz | e17dd32 | 2010-02-22 15:09:19 -0600 | [diff] [blame] | 44 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
| 45 | SNDRV_PCM_FMTBIT_S32_LE, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 46 | .period_bytes_min = 32, |
| 47 | .period_bytes_max = 64 * 1024, |
| 48 | .periods_min = 2, |
| 49 | .periods_max = 255, |
| 50 | .buffer_bytes_max = 128 * 1024, |
| 51 | }; |
| 52 | |
| 53 | struct omap_runtime_data { |
| 54 | spinlock_t lock; |
| 55 | struct omap_pcm_dma_data *dma_data; |
| 56 | int dma_ch; |
| 57 | int period_index; |
| 58 | }; |
| 59 | |
| 60 | static void omap_pcm_dma_irq(int ch, u16 stat, void *data) |
| 61 | { |
| 62 | struct snd_pcm_substream *substream = data; |
| 63 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 64 | struct omap_runtime_data *prtd = runtime->private_data; |
| 65 | unsigned long flags; |
| 66 | |
Janusz Krzysztofik | b5442a7 | 2010-03-28 22:29:29 +0200 | [diff] [blame] | 67 | if ((cpu_is_omap1510())) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 68 | /* |
Janusz Krzysztofik | 64844a6 | 2009-08-10 10:50:04 +0200 | [diff] [blame] | 69 | * OMAP1510 doesn't fully support DMA progress counter |
| 70 | * and there is no software emulation implemented yet, |
Janusz Krzysztofik | b5442a7 | 2010-03-28 22:29:29 +0200 | [diff] [blame] | 71 | * so have to maintain our own progress counters |
Janusz Krzysztofik | 64844a6 | 2009-08-10 10:50:04 +0200 | [diff] [blame] | 72 | * that can be used by omap_pcm_pointer() instead. |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 73 | */ |
| 74 | spin_lock_irqsave(&prtd->lock, flags); |
Janusz Krzysztofik | 471e3de | 2009-08-11 21:44:29 +0200 | [diff] [blame] | 75 | if ((stat == OMAP_DMA_LAST_IRQ) && |
| 76 | (prtd->period_index == runtime->periods - 1)) { |
| 77 | /* we are in sync, do nothing */ |
| 78 | spin_unlock_irqrestore(&prtd->lock, flags); |
| 79 | return; |
| 80 | } |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 81 | if (prtd->period_index >= 0) { |
Janusz Krzysztofik | 471e3de | 2009-08-11 21:44:29 +0200 | [diff] [blame] | 82 | if (stat & OMAP_DMA_BLOCK_IRQ) { |
| 83 | /* end of buffer reached, loop back */ |
| 84 | prtd->period_index = 0; |
| 85 | } else if (stat & OMAP_DMA_LAST_IRQ) { |
| 86 | /* update the counter for the last period */ |
| 87 | prtd->period_index = runtime->periods - 1; |
| 88 | } else if (++prtd->period_index >= runtime->periods) { |
| 89 | /* end of buffer missed? loop back */ |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 90 | prtd->period_index = 0; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | spin_unlock_irqrestore(&prtd->lock, flags); |
| 94 | } |
| 95 | |
| 96 | snd_pcm_period_elapsed(substream); |
| 97 | } |
| 98 | |
| 99 | /* this may get called several times by oss emulation */ |
| 100 | static int omap_pcm_hw_params(struct snd_pcm_substream *substream, |
| 101 | struct snd_pcm_hw_params *params) |
| 102 | { |
| 103 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 104 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 105 | struct omap_runtime_data *prtd = runtime->private_data; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 106 | struct omap_pcm_dma_data *dma_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 107 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 108 | int err = 0; |
| 109 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 110 | dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 111 | |
Joonyoung Shim | 1b4246a | 2009-04-22 10:56:50 +0900 | [diff] [blame] | 112 | /* return if this is a bufferless transfer e.g. |
| 113 | * codec <--> BT codec or GSM modem -- lg FIXME */ |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 114 | if (!dma_data) |
Joonyoung Shim | 1b4246a | 2009-04-22 10:56:50 +0900 | [diff] [blame] | 115 | return 0; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 116 | |
| 117 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 118 | runtime->dma_bytes = params_buffer_bytes(params); |
| 119 | |
| 120 | if (prtd->dma_data) |
| 121 | return 0; |
| 122 | prtd->dma_data = dma_data; |
| 123 | err = omap_request_dma(dma_data->dma_req, dma_data->name, |
| 124 | omap_pcm_dma_irq, substream, &prtd->dma_ch); |
Janusz Krzysztofik | 64844a6 | 2009-08-10 10:50:04 +0200 | [diff] [blame] | 125 | if (!err) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 126 | /* |
| 127 | * Link channel with itself so DMA doesn't need any |
| 128 | * reprogramming while looping the buffer |
| 129 | */ |
| 130 | omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch); |
| 131 | } |
| 132 | |
| 133 | return err; |
| 134 | } |
| 135 | |
| 136 | static int omap_pcm_hw_free(struct snd_pcm_substream *substream) |
| 137 | { |
| 138 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 139 | struct omap_runtime_data *prtd = runtime->private_data; |
| 140 | |
| 141 | if (prtd->dma_data == NULL) |
| 142 | return 0; |
| 143 | |
Janusz Krzysztofik | 64844a6 | 2009-08-10 10:50:04 +0200 | [diff] [blame] | 144 | omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 145 | omap_free_dma(prtd->dma_ch); |
| 146 | prtd->dma_data = NULL; |
| 147 | |
| 148 | snd_pcm_set_runtime_buffer(substream, NULL); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int omap_pcm_prepare(struct snd_pcm_substream *substream) |
| 154 | { |
| 155 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 156 | struct omap_runtime_data *prtd = runtime->private_data; |
| 157 | struct omap_pcm_dma_data *dma_data = prtd->dma_data; |
| 158 | struct omap_dma_channel_params dma_params; |
Misael Lopez Cruz | e17dd32 | 2010-02-22 15:09:19 -0600 | [diff] [blame] | 159 | int bytes; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 160 | |
Joonyoung Shim | 1b4246a | 2009-04-22 10:56:50 +0900 | [diff] [blame] | 161 | /* return if this is a bufferless transfer e.g. |
| 162 | * codec <--> BT codec or GSM modem -- lg FIXME */ |
| 163 | if (!prtd->dma_data) |
| 164 | return 0; |
| 165 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 166 | memset(&dma_params, 0, sizeof(dma_params)); |
Misael Lopez Cruz | e17dd32 | 2010-02-22 15:09:19 -0600 | [diff] [blame] | 167 | dma_params.data_type = dma_data->data_type; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 168 | dma_params.trigger = dma_data->dma_req; |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 169 | dma_params.sync_mode = dma_data->sync_mode; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 170 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 171 | dma_params.src_amode = OMAP_DMA_AMODE_POST_INC; |
| 172 | dma_params.dst_amode = OMAP_DMA_AMODE_CONSTANT; |
| 173 | dma_params.src_or_dst_synch = OMAP_DMA_DST_SYNC; |
| 174 | dma_params.src_start = runtime->dma_addr; |
| 175 | dma_params.dst_start = dma_data->port_addr; |
Arun KS | 9d37484 | 2008-09-30 15:35:16 +0530 | [diff] [blame] | 176 | dma_params.dst_port = OMAP_DMA_PORT_MPUI; |
Misael Lopez Cruz | e17dd32 | 2010-02-22 15:09:19 -0600 | [diff] [blame] | 177 | dma_params.dst_fi = dma_data->packet_size; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 178 | } else { |
| 179 | dma_params.src_amode = OMAP_DMA_AMODE_CONSTANT; |
| 180 | dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC; |
| 181 | dma_params.src_or_dst_synch = OMAP_DMA_SRC_SYNC; |
| 182 | dma_params.src_start = dma_data->port_addr; |
| 183 | dma_params.dst_start = runtime->dma_addr; |
Arun KS | 9d37484 | 2008-09-30 15:35:16 +0530 | [diff] [blame] | 184 | dma_params.src_port = OMAP_DMA_PORT_MPUI; |
Misael Lopez Cruz | e17dd32 | 2010-02-22 15:09:19 -0600 | [diff] [blame] | 185 | dma_params.src_fi = dma_data->packet_size; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 186 | } |
| 187 | /* |
| 188 | * Set DMA transfer frame size equal to ALSA period size and frame |
| 189 | * count as no. of ALSA periods. Then with DMA frame interrupt enabled, |
| 190 | * we can transfer the whole ALSA buffer with single DMA transfer but |
| 191 | * still can get an interrupt at each period bounary |
| 192 | */ |
Misael Lopez Cruz | e17dd32 | 2010-02-22 15:09:19 -0600 | [diff] [blame] | 193 | bytes = snd_pcm_lib_period_bytes(substream); |
| 194 | dma_params.elem_count = bytes >> dma_data->data_type; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 195 | dma_params.frame_count = runtime->periods; |
| 196 | omap_set_dma_params(prtd->dma_ch, &dma_params); |
| 197 | |
Janusz Krzysztofik | b5442a7 | 2010-03-28 22:29:29 +0200 | [diff] [blame] | 198 | if ((cpu_is_omap1510())) |
Janusz Krzysztofik | 471e3de | 2009-08-11 21:44:29 +0200 | [diff] [blame] | 199 | omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ | |
| 200 | OMAP_DMA_LAST_IRQ | OMAP_DMA_BLOCK_IRQ); |
Peter Ujfalusi | b417382 | 2011-05-12 13:04:55 +0300 | [diff] [blame] | 201 | else if (!substream->runtime->no_period_wakeup) |
Janusz Krzysztofik | 471e3de | 2009-08-11 21:44:29 +0200 | [diff] [blame] | 202 | omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ); |
Peter Ujfalusi | b1b6cff | 2011-10-04 11:09:52 +0300 | [diff] [blame] | 203 | else { |
| 204 | /* |
| 205 | * No period wakeup: |
| 206 | * we need to disable BLOCK_IRQ, which is enabled by the omap |
| 207 | * dma core at request dma time. |
| 208 | */ |
| 209 | omap_disable_dma_irq(prtd->dma_ch, OMAP_DMA_BLOCK_IRQ); |
| 210 | } |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 211 | |
Janusz Krzysztofik | 4d187fb | 2009-10-21 23:10:03 +0200 | [diff] [blame] | 212 | if (!(cpu_class_is_omap1())) { |
| 213 | omap_set_dma_src_burst_mode(prtd->dma_ch, |
| 214 | OMAP_DMA_DATA_BURST_16); |
| 215 | omap_set_dma_dest_burst_mode(prtd->dma_ch, |
| 216 | OMAP_DMA_DATA_BURST_16); |
| 217 | } |
Eduardo Valentin | 9599d48 | 2009-08-20 16:18:21 +0300 | [diff] [blame] | 218 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 223 | { |
| 224 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 225 | struct omap_runtime_data *prtd = runtime->private_data; |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 226 | struct omap_pcm_dma_data *dma_data = prtd->dma_data; |
Eero Nurkkala | 21dff43 | 2009-02-02 14:20:46 +0200 | [diff] [blame] | 227 | unsigned long flags; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 228 | int ret = 0; |
| 229 | |
Eero Nurkkala | 21dff43 | 2009-02-02 14:20:46 +0200 | [diff] [blame] | 230 | spin_lock_irqsave(&prtd->lock, flags); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 231 | switch (cmd) { |
| 232 | case SNDRV_PCM_TRIGGER_START: |
| 233 | case SNDRV_PCM_TRIGGER_RESUME: |
| 234 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 235 | prtd->period_index = 0; |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 236 | /* Configure McBSP internal buffer usage */ |
| 237 | if (dma_data->set_threshold) |
| 238 | dma_data->set_threshold(substream); |
| 239 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 240 | omap_start_dma(prtd->dma_ch); |
| 241 | break; |
| 242 | |
| 243 | case SNDRV_PCM_TRIGGER_STOP: |
| 244 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 245 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 246 | prtd->period_index = -1; |
| 247 | omap_stop_dma(prtd->dma_ch); |
| 248 | break; |
| 249 | default: |
| 250 | ret = -EINVAL; |
| 251 | } |
Eero Nurkkala | 21dff43 | 2009-02-02 14:20:46 +0200 | [diff] [blame] | 252 | spin_unlock_irqrestore(&prtd->lock, flags); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream) |
| 258 | { |
| 259 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 260 | struct omap_runtime_data *prtd = runtime->private_data; |
| 261 | dma_addr_t ptr; |
| 262 | snd_pcm_uframes_t offset; |
| 263 | |
Janusz Krzysztofik | b5442a7 | 2010-03-28 22:29:29 +0200 | [diff] [blame] | 264 | if (cpu_is_omap1510()) { |
| 265 | offset = prtd->period_index * runtime->period_size; |
| 266 | } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 267 | ptr = omap_get_dma_dst_pos(prtd->dma_ch); |
Janusz Krzysztofik | 1bdd741 | 2009-06-28 00:21:05 +0200 | [diff] [blame] | 268 | offset = bytes_to_frames(runtime, ptr - runtime->dma_addr); |
Janusz Krzysztofik | b5442a7 | 2010-03-28 22:29:29 +0200 | [diff] [blame] | 269 | } else { |
Janusz Krzysztofik | 1bdd741 | 2009-06-28 00:21:05 +0200 | [diff] [blame] | 270 | ptr = omap_get_dma_src_pos(prtd->dma_ch); |
| 271 | offset = bytes_to_frames(runtime, ptr - runtime->dma_addr); |
Janusz Krzysztofik | b5442a7 | 2010-03-28 22:29:29 +0200 | [diff] [blame] | 272 | } |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 273 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 274 | if (offset >= runtime->buffer_size) |
| 275 | offset = 0; |
| 276 | |
| 277 | return offset; |
| 278 | } |
| 279 | |
| 280 | static int omap_pcm_open(struct snd_pcm_substream *substream) |
| 281 | { |
| 282 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 283 | struct omap_runtime_data *prtd; |
| 284 | int ret; |
| 285 | |
| 286 | snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware); |
| 287 | |
| 288 | /* Ensure that buffer size is a multiple of period size */ |
| 289 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 290 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 291 | if (ret < 0) |
| 292 | goto out; |
| 293 | |
Stanley Miao | 19b3f31 | 2008-12-19 22:08:22 +0800 | [diff] [blame] | 294 | prtd = kzalloc(sizeof(*prtd), GFP_KERNEL); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 295 | if (prtd == NULL) { |
| 296 | ret = -ENOMEM; |
| 297 | goto out; |
| 298 | } |
| 299 | spin_lock_init(&prtd->lock); |
| 300 | runtime->private_data = prtd; |
| 301 | |
| 302 | out: |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | static int omap_pcm_close(struct snd_pcm_substream *substream) |
| 307 | { |
| 308 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 309 | |
| 310 | kfree(runtime->private_data); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static int omap_pcm_mmap(struct snd_pcm_substream *substream, |
| 315 | struct vm_area_struct *vma) |
| 316 | { |
| 317 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 318 | |
| 319 | return dma_mmap_writecombine(substream->pcm->card->dev, vma, |
| 320 | runtime->dma_area, |
| 321 | runtime->dma_addr, |
| 322 | runtime->dma_bytes); |
| 323 | } |
| 324 | |
Mark Brown | b2a19d0 | 2009-01-17 19:14:26 +0000 | [diff] [blame] | 325 | static struct snd_pcm_ops omap_pcm_ops = { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 326 | .open = omap_pcm_open, |
| 327 | .close = omap_pcm_close, |
| 328 | .ioctl = snd_pcm_lib_ioctl, |
| 329 | .hw_params = omap_pcm_hw_params, |
| 330 | .hw_free = omap_pcm_hw_free, |
| 331 | .prepare = omap_pcm_prepare, |
| 332 | .trigger = omap_pcm_trigger, |
| 333 | .pointer = omap_pcm_pointer, |
| 334 | .mmap = omap_pcm_mmap, |
| 335 | }; |
| 336 | |
Eduardo Valentin | a152ff2 | 2009-08-20 16:18:22 +0300 | [diff] [blame] | 337 | static u64 omap_pcm_dmamask = DMA_BIT_MASK(64); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 338 | |
| 339 | static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, |
| 340 | int stream) |
| 341 | { |
| 342 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 343 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
| 344 | size_t size = omap_pcm_hardware.buffer_bytes_max; |
| 345 | |
| 346 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 347 | buf->dev.dev = pcm->card->dev; |
| 348 | buf->private_data = NULL; |
| 349 | buf->area = dma_alloc_writecombine(pcm->card->dev, size, |
| 350 | &buf->addr, GFP_KERNEL); |
| 351 | if (!buf->area) |
| 352 | return -ENOMEM; |
| 353 | |
| 354 | buf->bytes = size; |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm) |
| 359 | { |
| 360 | struct snd_pcm_substream *substream; |
| 361 | struct snd_dma_buffer *buf; |
| 362 | int stream; |
| 363 | |
| 364 | for (stream = 0; stream < 2; stream++) { |
| 365 | substream = pcm->streams[stream].substream; |
| 366 | if (!substream) |
| 367 | continue; |
| 368 | |
| 369 | buf = &substream->dma_buffer; |
| 370 | if (!buf->area) |
| 371 | continue; |
| 372 | |
| 373 | dma_free_writecombine(pcm->card->dev, buf->bytes, |
| 374 | buf->area, buf->addr); |
| 375 | buf->area = NULL; |
| 376 | } |
| 377 | } |
| 378 | |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 379 | static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 380 | { |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 381 | struct snd_card *card = rtd->card->snd_card; |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 382 | struct snd_pcm *pcm = rtd->pcm; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 383 | int ret = 0; |
| 384 | |
| 385 | if (!card->dev->dma_mask) |
| 386 | card->dev->dma_mask = &omap_pcm_dmamask; |
| 387 | if (!card->dev->coherent_dma_mask) |
Eduardo Valentin | a152ff2 | 2009-08-20 16:18:22 +0300 | [diff] [blame] | 388 | card->dev->coherent_dma_mask = DMA_BIT_MASK(64); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 389 | |
Joachim Eastwood | 25e9e75 | 2012-01-01 01:58:44 +0100 | [diff] [blame] | 390 | if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 391 | ret = omap_pcm_preallocate_dma_buffer(pcm, |
| 392 | SNDRV_PCM_STREAM_PLAYBACK); |
| 393 | if (ret) |
| 394 | goto out; |
| 395 | } |
| 396 | |
Joachim Eastwood | 25e9e75 | 2012-01-01 01:58:44 +0100 | [diff] [blame] | 397 | if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 398 | ret = omap_pcm_preallocate_dma_buffer(pcm, |
| 399 | SNDRV_PCM_STREAM_CAPTURE); |
| 400 | if (ret) |
| 401 | goto out; |
| 402 | } |
| 403 | |
| 404 | out: |
Oleg Matcovschi | fad9365 | 2012-04-24 19:02:02 -0700 | [diff] [blame] | 405 | /* free preallocated buffers in case of error */ |
| 406 | if (ret) |
| 407 | omap_pcm_free_dma_buffers(pcm); |
| 408 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 409 | return ret; |
| 410 | } |
| 411 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 412 | static struct snd_soc_platform_driver omap_soc_platform = { |
| 413 | .ops = &omap_pcm_ops, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 414 | .pcm_new = omap_pcm_new, |
| 415 | .pcm_free = omap_pcm_free_dma_buffers, |
| 416 | }; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 417 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 418 | static __devinit int omap_pcm_probe(struct platform_device *pdev) |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 419 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 420 | return snd_soc_register_platform(&pdev->dev, |
| 421 | &omap_soc_platform); |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 422 | } |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 423 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 424 | static int __devexit omap_pcm_remove(struct platform_device *pdev) |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 425 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 426 | snd_soc_unregister_platform(&pdev->dev); |
| 427 | return 0; |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 428 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 429 | |
| 430 | static struct platform_driver omap_pcm_driver = { |
| 431 | .driver = { |
| 432 | .name = "omap-pcm-audio", |
| 433 | .owner = THIS_MODULE, |
| 434 | }, |
| 435 | |
| 436 | .probe = omap_pcm_probe, |
| 437 | .remove = __devexit_p(omap_pcm_remove), |
| 438 | }; |
| 439 | |
Axel Lin | beda5bf5 | 2011-11-25 10:12:16 +0800 | [diff] [blame] | 440 | module_platform_driver(omap_pcm_driver); |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 441 | |
Jarkko Nikula | 7ec41ee | 2011-08-11 15:44:57 +0300 | [diff] [blame] | 442 | MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>"); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 443 | MODULE_DESCRIPTION("OMAP PCM DMA module"); |
| 444 | MODULE_LICENSE("GPL"); |
Guillaume Gardet | 5e70b7fc | 2012-07-12 15:08:16 +0200 | [diff] [blame] | 445 | MODULE_ALIAS("platform:omap-pcm-audio"); |