Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ALSA PCM interface for the Stetch s6000 family |
| 3 | * |
| 4 | * Author: Daniel Gloeckner, <dg@emlix.com> |
| 5 | * Copyright: (C) 2009 emlix GmbH <info@emlix.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/dma-mapping.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | |
| 19 | #include <sound/core.h> |
| 20 | #include <sound/pcm.h> |
| 21 | #include <sound/pcm_params.h> |
| 22 | #include <sound/soc.h> |
| 23 | |
| 24 | #include <asm/dma.h> |
| 25 | #include <variant/dmac.h> |
| 26 | |
| 27 | #include "s6000-pcm.h" |
| 28 | |
| 29 | #define S6_PCM_PREALLOCATE_SIZE (96 * 1024) |
| 30 | #define S6_PCM_PREALLOCATE_MAX (2048 * 1024) |
| 31 | |
| 32 | static struct snd_pcm_hardware s6000_pcm_hardware = { |
| 33 | .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 34 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | |
| 35 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_JOINT_DUPLEX), |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 36 | .buffer_bytes_max = 0x7ffffff0, |
| 37 | .period_bytes_min = 16, |
| 38 | .period_bytes_max = 0xfffff0, |
| 39 | .periods_min = 2, |
| 40 | .periods_max = 1024, /* no limit */ |
| 41 | .fifo_size = 0, |
| 42 | }; |
| 43 | |
| 44 | struct s6000_runtime_data { |
| 45 | spinlock_t lock; |
| 46 | int period; /* current DMA period */ |
| 47 | }; |
| 48 | |
| 49 | static void s6000_pcm_enqueue_dma(struct snd_pcm_substream *substream) |
| 50 | { |
| 51 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 52 | struct s6000_runtime_data *prtd = runtime->private_data; |
| 53 | struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 54 | struct s6000_pcm_dma_params *par; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 55 | int channel; |
| 56 | unsigned int period_size; |
| 57 | unsigned int dma_offset; |
| 58 | dma_addr_t dma_pos; |
| 59 | dma_addr_t src, dst; |
| 60 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 61 | par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream); |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 62 | |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 63 | period_size = snd_pcm_lib_period_bytes(substream); |
| 64 | dma_offset = prtd->period * period_size; |
| 65 | dma_pos = runtime->dma_addr + dma_offset; |
| 66 | |
| 67 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 68 | src = dma_pos; |
| 69 | dst = par->sif_out; |
| 70 | channel = par->dma_out; |
| 71 | } else { |
| 72 | src = par->sif_in; |
| 73 | dst = dma_pos; |
| 74 | channel = par->dma_in; |
| 75 | } |
| 76 | |
| 77 | if (!s6dmac_channel_enabled(DMA_MASK_DMAC(channel), |
| 78 | DMA_INDEX_CHNL(channel))) |
| 79 | return; |
| 80 | |
| 81 | if (s6dmac_fifo_full(DMA_MASK_DMAC(channel), DMA_INDEX_CHNL(channel))) { |
| 82 | printk(KERN_ERR "s6000-pcm: fifo full\n"); |
| 83 | return; |
| 84 | } |
| 85 | |
Takashi Iwai | 4a318f1 | 2013-11-05 18:40:04 +0100 | [diff] [blame] | 86 | if (WARN_ON(period_size & 15)) |
| 87 | return; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 88 | s6dmac_put_fifo(DMA_MASK_DMAC(channel), DMA_INDEX_CHNL(channel), |
| 89 | src, dst, period_size); |
| 90 | |
| 91 | prtd->period++; |
| 92 | if (unlikely(prtd->period >= runtime->periods)) |
| 93 | prtd->period = 0; |
| 94 | } |
| 95 | |
| 96 | static irqreturn_t s6000_pcm_irq(int irq, void *data) |
| 97 | { |
| 98 | struct snd_pcm *pcm = data; |
| 99 | struct snd_soc_pcm_runtime *runtime = pcm->private_data; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 100 | struct s6000_runtime_data *prtd; |
| 101 | unsigned int has_xrun; |
| 102 | int i, ret = IRQ_NONE; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 103 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 104 | for (i = 0; i < 2; ++i) { |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 105 | struct snd_pcm_substream *substream = pcm->streams[i].substream; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 106 | struct s6000_pcm_dma_params *params = |
| 107 | snd_soc_dai_get_dma_data(runtime->cpu_dai, substream); |
| 108 | u32 channel; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 109 | unsigned int pending; |
| 110 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 111 | if (substream == SNDRV_PCM_STREAM_PLAYBACK) |
| 112 | channel = params->dma_out; |
| 113 | else |
| 114 | channel = params->dma_in; |
| 115 | |
| 116 | has_xrun = params->check_xrun(runtime->cpu_dai); |
| 117 | |
| 118 | if (!channel) |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 119 | continue; |
| 120 | |
| 121 | if (unlikely(has_xrun & (1 << i)) && |
| 122 | substream->runtime && |
| 123 | snd_pcm_running(substream)) { |
| 124 | dev_dbg(pcm->dev, "xrun\n"); |
Takashi Iwai | 61be2b9 | 2013-07-11 18:00:25 +0200 | [diff] [blame] | 125 | snd_pcm_stream_lock(substream); |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 126 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
Takashi Iwai | 61be2b9 | 2013-07-11 18:00:25 +0200 | [diff] [blame] | 127 | snd_pcm_stream_unlock(substream); |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 128 | ret = IRQ_HANDLED; |
| 129 | } |
| 130 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 131 | pending = s6dmac_int_sources(DMA_MASK_DMAC(channel), |
| 132 | DMA_INDEX_CHNL(channel)); |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 133 | |
| 134 | if (pending & 1) { |
| 135 | ret = IRQ_HANDLED; |
| 136 | if (likely(substream->runtime && |
| 137 | snd_pcm_running(substream))) { |
| 138 | snd_pcm_period_elapsed(substream); |
| 139 | dev_dbg(pcm->dev, "period elapsed %x %x\n", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 140 | s6dmac_cur_src(DMA_MASK_DMAC(channel), |
| 141 | DMA_INDEX_CHNL(channel)), |
| 142 | s6dmac_cur_dst(DMA_MASK_DMAC(channel), |
| 143 | DMA_INDEX_CHNL(channel))); |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 144 | prtd = substream->runtime->private_data; |
| 145 | spin_lock(&prtd->lock); |
| 146 | s6000_pcm_enqueue_dma(substream); |
| 147 | spin_unlock(&prtd->lock); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (unlikely(pending & ~7)) { |
| 152 | if (pending & (1 << 3)) |
| 153 | printk(KERN_WARNING |
| 154 | "s6000-pcm: DMA %x Underflow\n", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 155 | channel); |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 156 | if (pending & (1 << 4)) |
| 157 | printk(KERN_WARNING |
| 158 | "s6000-pcm: DMA %x Overflow\n", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 159 | channel); |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 160 | if (pending & 0x1e0) |
| 161 | printk(KERN_WARNING |
| 162 | "s6000-pcm: DMA %x Master Error " |
| 163 | "(mask %x)\n", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 164 | channel, pending >> 5); |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 165 | |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | static int s6000_pcm_start(struct snd_pcm_substream *substream) |
| 173 | { |
| 174 | struct s6000_runtime_data *prtd = substream->runtime->private_data; |
| 175 | struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 176 | struct s6000_pcm_dma_params *par; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 177 | unsigned long flags; |
| 178 | int srcinc; |
| 179 | u32 dma; |
| 180 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 181 | par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream); |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 182 | |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 183 | spin_lock_irqsave(&prtd->lock, flags); |
| 184 | |
| 185 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 186 | srcinc = 1; |
| 187 | dma = par->dma_out; |
| 188 | } else { |
| 189 | srcinc = 0; |
| 190 | dma = par->dma_in; |
| 191 | } |
| 192 | s6dmac_enable_chan(DMA_MASK_DMAC(dma), DMA_INDEX_CHNL(dma), |
| 193 | 1 /* priority 1 (0 is max) */, |
| 194 | 0 /* peripheral requests w/o xfer length mode */, |
| 195 | srcinc /* source address increment */, |
| 196 | srcinc^1 /* destination address increment */, |
| 197 | 0 /* chunksize 0 (skip impossible on this dma) */, |
| 198 | 0 /* source skip after chunk (impossible) */, |
| 199 | 0 /* destination skip after chunk (impossible) */, |
| 200 | 4 /* 16 byte burst size */, |
| 201 | -1 /* don't conserve bandwidth */, |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 202 | 0 /* low watermark irq descriptor threshold */, |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 203 | 0 /* disable hardware timestamps */, |
| 204 | 1 /* enable channel */); |
| 205 | |
| 206 | s6000_pcm_enqueue_dma(substream); |
| 207 | s6000_pcm_enqueue_dma(substream); |
| 208 | |
| 209 | spin_unlock_irqrestore(&prtd->lock, flags); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static int s6000_pcm_stop(struct snd_pcm_substream *substream) |
| 215 | { |
| 216 | struct s6000_runtime_data *prtd = substream->runtime->private_data; |
| 217 | struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 218 | struct s6000_pcm_dma_params *par; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 219 | unsigned long flags; |
| 220 | u32 channel; |
| 221 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 222 | par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream); |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 223 | |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 224 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 225 | channel = par->dma_out; |
| 226 | else |
| 227 | channel = par->dma_in; |
| 228 | |
| 229 | s6dmac_set_terminal_count(DMA_MASK_DMAC(channel), |
| 230 | DMA_INDEX_CHNL(channel), 0); |
| 231 | |
| 232 | spin_lock_irqsave(&prtd->lock, flags); |
| 233 | |
| 234 | s6dmac_disable_chan(DMA_MASK_DMAC(channel), DMA_INDEX_CHNL(channel)); |
| 235 | |
| 236 | spin_unlock_irqrestore(&prtd->lock, flags); |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static int s6000_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 242 | { |
| 243 | struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 244 | struct s6000_pcm_dma_params *par; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 245 | int ret; |
| 246 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 247 | par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream); |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 248 | |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 249 | ret = par->trigger(substream, cmd, 0); |
| 250 | if (ret < 0) |
| 251 | return ret; |
| 252 | |
| 253 | switch (cmd) { |
| 254 | case SNDRV_PCM_TRIGGER_START: |
| 255 | case SNDRV_PCM_TRIGGER_RESUME: |
| 256 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 257 | ret = s6000_pcm_start(substream); |
| 258 | break; |
| 259 | case SNDRV_PCM_TRIGGER_STOP: |
| 260 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 261 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 262 | ret = s6000_pcm_stop(substream); |
| 263 | break; |
| 264 | default: |
| 265 | ret = -EINVAL; |
| 266 | } |
| 267 | if (ret < 0) |
| 268 | return ret; |
| 269 | |
| 270 | return par->trigger(substream, cmd, 1); |
| 271 | } |
| 272 | |
| 273 | static int s6000_pcm_prepare(struct snd_pcm_substream *substream) |
| 274 | { |
| 275 | struct s6000_runtime_data *prtd = substream->runtime->private_data; |
| 276 | |
| 277 | prtd->period = 0; |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static snd_pcm_uframes_t s6000_pcm_pointer(struct snd_pcm_substream *substream) |
| 283 | { |
| 284 | struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 285 | struct s6000_pcm_dma_params *par; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 286 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 287 | struct s6000_runtime_data *prtd = runtime->private_data; |
| 288 | unsigned long flags; |
| 289 | unsigned int offset; |
| 290 | dma_addr_t count; |
| 291 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 292 | par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream); |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 293 | |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 294 | spin_lock_irqsave(&prtd->lock, flags); |
| 295 | |
| 296 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 297 | count = s6dmac_cur_src(DMA_MASK_DMAC(par->dma_out), |
| 298 | DMA_INDEX_CHNL(par->dma_out)); |
| 299 | else |
| 300 | count = s6dmac_cur_dst(DMA_MASK_DMAC(par->dma_in), |
| 301 | DMA_INDEX_CHNL(par->dma_in)); |
| 302 | |
| 303 | count -= runtime->dma_addr; |
| 304 | |
| 305 | spin_unlock_irqrestore(&prtd->lock, flags); |
| 306 | |
| 307 | offset = bytes_to_frames(runtime, count); |
| 308 | if (unlikely(offset >= runtime->buffer_size)) |
| 309 | offset = 0; |
| 310 | |
| 311 | return offset; |
| 312 | } |
| 313 | |
| 314 | static int s6000_pcm_open(struct snd_pcm_substream *substream) |
| 315 | { |
| 316 | struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 317 | struct s6000_pcm_dma_params *par; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 318 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 319 | struct s6000_runtime_data *prtd; |
| 320 | int ret; |
| 321 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 322 | par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream); |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 323 | snd_soc_set_runtime_hwparams(substream, &s6000_pcm_hardware); |
| 324 | |
| 325 | ret = snd_pcm_hw_constraint_step(runtime, 0, |
| 326 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 16); |
| 327 | if (ret < 0) |
| 328 | return ret; |
| 329 | ret = snd_pcm_hw_constraint_step(runtime, 0, |
| 330 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 16); |
| 331 | if (ret < 0) |
| 332 | return ret; |
| 333 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 334 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 335 | if (ret < 0) |
| 336 | return ret; |
| 337 | |
| 338 | if (par->same_rate) { |
| 339 | int rate; |
| 340 | spin_lock(&par->lock); /* needed? */ |
| 341 | rate = par->rate; |
| 342 | spin_unlock(&par->lock); |
| 343 | if (rate != -1) { |
| 344 | ret = snd_pcm_hw_constraint_minmax(runtime, |
| 345 | SNDRV_PCM_HW_PARAM_RATE, |
| 346 | rate, rate); |
| 347 | if (ret < 0) |
| 348 | return ret; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | prtd = kzalloc(sizeof(struct s6000_runtime_data), GFP_KERNEL); |
| 353 | if (prtd == NULL) |
| 354 | return -ENOMEM; |
| 355 | |
| 356 | spin_lock_init(&prtd->lock); |
| 357 | |
| 358 | runtime->private_data = prtd; |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static int s6000_pcm_close(struct snd_pcm_substream *substream) |
| 364 | { |
| 365 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 366 | struct s6000_runtime_data *prtd = runtime->private_data; |
| 367 | |
| 368 | kfree(prtd); |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static int s6000_pcm_hw_params(struct snd_pcm_substream *substream, |
| 374 | struct snd_pcm_hw_params *hw_params) |
| 375 | { |
| 376 | struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 377 | struct s6000_pcm_dma_params *par; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 378 | int ret; |
| 379 | ret = snd_pcm_lib_malloc_pages(substream, |
| 380 | params_buffer_bytes(hw_params)); |
| 381 | if (ret < 0) { |
| 382 | printk(KERN_WARNING "s6000-pcm: allocation of memory failed\n"); |
| 383 | return ret; |
| 384 | } |
| 385 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 386 | par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream); |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 387 | |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 388 | if (par->same_rate) { |
| 389 | spin_lock(&par->lock); |
| 390 | if (par->rate == -1 || |
| 391 | !(par->in_use & ~(1 << substream->stream))) { |
| 392 | par->rate = params_rate(hw_params); |
| 393 | par->in_use |= 1 << substream->stream; |
| 394 | } else if (params_rate(hw_params) != par->rate) { |
| 395 | snd_pcm_lib_free_pages(substream); |
| 396 | par->in_use &= ~(1 << substream->stream); |
| 397 | ret = -EBUSY; |
| 398 | } |
| 399 | spin_unlock(&par->lock); |
| 400 | } |
| 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | static int s6000_pcm_hw_free(struct snd_pcm_substream *substream) |
| 405 | { |
| 406 | struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 407 | struct s6000_pcm_dma_params *par = |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 408 | snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream); |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 409 | |
| 410 | spin_lock(&par->lock); |
| 411 | par->in_use &= ~(1 << substream->stream); |
| 412 | if (!par->in_use) |
| 413 | par->rate = -1; |
| 414 | spin_unlock(&par->lock); |
| 415 | |
| 416 | return snd_pcm_lib_free_pages(substream); |
| 417 | } |
| 418 | |
| 419 | static struct snd_pcm_ops s6000_pcm_ops = { |
| 420 | .open = s6000_pcm_open, |
| 421 | .close = s6000_pcm_close, |
| 422 | .ioctl = snd_pcm_lib_ioctl, |
| 423 | .hw_params = s6000_pcm_hw_params, |
| 424 | .hw_free = s6000_pcm_hw_free, |
| 425 | .trigger = s6000_pcm_trigger, |
| 426 | .prepare = s6000_pcm_prepare, |
| 427 | .pointer = s6000_pcm_pointer, |
| 428 | }; |
| 429 | |
| 430 | static void s6000_pcm_free(struct snd_pcm *pcm) |
| 431 | { |
| 432 | struct snd_soc_pcm_runtime *runtime = pcm->private_data; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 433 | struct s6000_pcm_dma_params *params = |
Joachim Eastwood | 6296914 | 2012-01-01 02:14:24 +0100 | [diff] [blame] | 434 | snd_soc_dai_get_dma_data(runtime->cpu_dai, |
| 435 | pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream); |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 436 | |
| 437 | free_irq(params->irq, pcm); |
| 438 | snd_pcm_lib_preallocate_free_for_all(pcm); |
| 439 | } |
| 440 | |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 441 | static int s6000_pcm_new(struct snd_soc_pcm_runtime *runtime) |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 442 | { |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 443 | struct snd_card *card = runtime->card->snd_card; |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 444 | struct snd_pcm *pcm = runtime->pcm; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 445 | struct s6000_pcm_dma_params *params; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 446 | int res; |
| 447 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 448 | params = snd_soc_dai_get_dma_data(runtime->cpu_dai, |
Joachim Eastwood | 6296914 | 2012-01-01 02:14:24 +0100 | [diff] [blame] | 449 | pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream); |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 450 | |
Russell King | c9bd5e6 | 2013-06-27 12:53:37 +0100 | [diff] [blame] | 451 | res = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32)); |
| 452 | if (res) |
| 453 | return res; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 454 | |
| 455 | if (params->dma_in) { |
| 456 | s6dmac_disable_chan(DMA_MASK_DMAC(params->dma_in), |
| 457 | DMA_INDEX_CHNL(params->dma_in)); |
| 458 | s6dmac_int_sources(DMA_MASK_DMAC(params->dma_in), |
| 459 | DMA_INDEX_CHNL(params->dma_in)); |
| 460 | } |
| 461 | |
| 462 | if (params->dma_out) { |
| 463 | s6dmac_disable_chan(DMA_MASK_DMAC(params->dma_out), |
| 464 | DMA_INDEX_CHNL(params->dma_out)); |
| 465 | s6dmac_int_sources(DMA_MASK_DMAC(params->dma_out), |
| 466 | DMA_INDEX_CHNL(params->dma_out)); |
| 467 | } |
| 468 | |
| 469 | res = request_irq(params->irq, s6000_pcm_irq, IRQF_SHARED, |
Daniel Glöckner | 9e4ea71 | 2010-11-30 01:00:17 +0100 | [diff] [blame] | 470 | "s6000-audio", pcm); |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 471 | if (res) { |
| 472 | printk(KERN_ERR "s6000-pcm couldn't get IRQ\n"); |
| 473 | return res; |
| 474 | } |
| 475 | |
| 476 | res = snd_pcm_lib_preallocate_pages_for_all(pcm, |
| 477 | SNDRV_DMA_TYPE_DEV, |
| 478 | card->dev, |
| 479 | S6_PCM_PREALLOCATE_SIZE, |
| 480 | S6_PCM_PREALLOCATE_MAX); |
| 481 | if (res) |
| 482 | printk(KERN_WARNING "s6000-pcm: preallocation failed\n"); |
| 483 | |
| 484 | spin_lock_init(¶ms->lock); |
| 485 | params->in_use = 0; |
| 486 | params->rate = -1; |
| 487 | return 0; |
| 488 | } |
| 489 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 490 | static struct snd_soc_platform_driver s6000_soc_platform = { |
| 491 | .ops = &s6000_pcm_ops, |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 492 | .pcm_new = s6000_pcm_new, |
| 493 | .pcm_free = s6000_pcm_free, |
| 494 | }; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 495 | |
Bill Pemberton | 9ac8a71 | 2012-12-07 09:26:30 -0500 | [diff] [blame] | 496 | static int s6000_soc_platform_probe(struct platform_device *pdev) |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 497 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 498 | return snd_soc_register_platform(&pdev->dev, &s6000_soc_platform); |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 499 | } |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 500 | |
Bill Pemberton | 9ac8a71 | 2012-12-07 09:26:30 -0500 | [diff] [blame] | 501 | static int s6000_soc_platform_remove(struct platform_device *pdev) |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 502 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 503 | snd_soc_unregister_platform(&pdev->dev); |
| 504 | return 0; |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 505 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 506 | |
| 507 | static struct platform_driver s6000_pcm_driver = { |
| 508 | .driver = { |
| 509 | .name = "s6000-pcm-audio", |
| 510 | .owner = THIS_MODULE, |
| 511 | }, |
| 512 | |
| 513 | .probe = s6000_soc_platform_probe, |
Bill Pemberton | 9ac8a71 | 2012-12-07 09:26:30 -0500 | [diff] [blame] | 514 | .remove = s6000_soc_platform_remove, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 515 | }; |
| 516 | |
Axel Lin | 880dd72 | 2011-11-24 12:14:56 +0800 | [diff] [blame] | 517 | module_platform_driver(s6000_pcm_driver); |
Daniel Glöckner | 4b166da | 2009-03-28 19:47:01 +0100 | [diff] [blame] | 518 | |
| 519 | MODULE_AUTHOR("Daniel Gloeckner"); |
| 520 | MODULE_DESCRIPTION("Stretch s6000 family PCM DMA module"); |
| 521 | MODULE_LICENSE("GPL"); |