Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License version 2 as |
| 4 | * published by the Free Software Foundation. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/dma-mapping.h> |
| 9 | |
| 10 | #include <sound/core.h> |
| 11 | #include <sound/pcm.h> |
| 12 | #include <sound/pcm_params.h> |
| 13 | #include <sound/pxa2xx-lib.h> |
| 14 | |
Eric Miao | 5742964 | 2009-01-19 15:28:07 +0800 | [diff] [blame] | 15 | #include <mach/dma.h> |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 16 | |
| 17 | #include "pxa2xx-pcm.h" |
| 18 | |
| 19 | static const struct snd_pcm_hardware pxa2xx_pcm_hardware = { |
| 20 | .info = SNDRV_PCM_INFO_MMAP | |
| 21 | SNDRV_PCM_INFO_MMAP_VALID | |
| 22 | SNDRV_PCM_INFO_INTERLEAVED | |
| 23 | SNDRV_PCM_INFO_PAUSE | |
| 24 | SNDRV_PCM_INFO_RESUME, |
| 25 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
| 26 | SNDRV_PCM_FMTBIT_S24_LE | |
| 27 | SNDRV_PCM_FMTBIT_S32_LE, |
| 28 | .period_bytes_min = 32, |
| 29 | .period_bytes_max = 8192 - 32, |
| 30 | .periods_min = 1, |
| 31 | .periods_max = PAGE_SIZE/sizeof(pxa_dma_desc), |
| 32 | .buffer_bytes_max = 128 * 1024, |
| 33 | .fifo_size = 32, |
| 34 | }; |
| 35 | |
| 36 | int __pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream, |
| 37 | struct snd_pcm_hw_params *params) |
| 38 | { |
| 39 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 40 | struct pxa2xx_runtime_data *rtd = runtime->private_data; |
| 41 | size_t totsize = params_buffer_bytes(params); |
| 42 | size_t period = params_period_bytes(params); |
| 43 | pxa_dma_desc *dma_desc; |
| 44 | dma_addr_t dma_buff_phys, next_desc_phys; |
| 45 | |
| 46 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 47 | runtime->dma_bytes = totsize; |
| 48 | |
| 49 | dma_desc = rtd->dma_desc_array; |
| 50 | next_desc_phys = rtd->dma_desc_array_phys; |
| 51 | dma_buff_phys = runtime->dma_addr; |
| 52 | do { |
| 53 | next_desc_phys += sizeof(pxa_dma_desc); |
| 54 | dma_desc->ddadr = next_desc_phys; |
| 55 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 56 | dma_desc->dsadr = dma_buff_phys; |
| 57 | dma_desc->dtadr = rtd->params->dev_addr; |
| 58 | } else { |
| 59 | dma_desc->dsadr = rtd->params->dev_addr; |
| 60 | dma_desc->dtadr = dma_buff_phys; |
| 61 | } |
| 62 | if (period > totsize) |
| 63 | period = totsize; |
| 64 | dma_desc->dcmd = rtd->params->dcmd | period | DCMD_ENDIRQEN; |
| 65 | dma_desc++; |
| 66 | dma_buff_phys += period; |
| 67 | } while (totsize -= period); |
| 68 | dma_desc[-1].ddadr = rtd->dma_desc_array_phys; |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | EXPORT_SYMBOL(__pxa2xx_pcm_hw_params); |
| 73 | |
| 74 | int __pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream) |
| 75 | { |
| 76 | struct pxa2xx_runtime_data *rtd = substream->runtime->private_data; |
| 77 | |
Daniel Mack | b7d4de7 | 2009-07-08 19:24:26 +0200 | [diff] [blame] | 78 | if (rtd && rtd->params && rtd->params->drcmr) |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 79 | *rtd->params->drcmr = 0; |
| 80 | |
| 81 | snd_pcm_set_runtime_buffer(substream, NULL); |
| 82 | return 0; |
| 83 | } |
| 84 | EXPORT_SYMBOL(__pxa2xx_pcm_hw_free); |
| 85 | |
| 86 | int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 87 | { |
| 88 | struct pxa2xx_runtime_data *prtd = substream->runtime->private_data; |
| 89 | int ret = 0; |
| 90 | |
| 91 | switch (cmd) { |
| 92 | case SNDRV_PCM_TRIGGER_START: |
| 93 | DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys; |
| 94 | DCSR(prtd->dma_ch) = DCSR_RUN; |
| 95 | break; |
| 96 | |
| 97 | case SNDRV_PCM_TRIGGER_STOP: |
| 98 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 99 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 100 | DCSR(prtd->dma_ch) &= ~DCSR_RUN; |
| 101 | break; |
| 102 | |
| 103 | case SNDRV_PCM_TRIGGER_RESUME: |
| 104 | DCSR(prtd->dma_ch) |= DCSR_RUN; |
| 105 | break; |
| 106 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 107 | DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys; |
| 108 | DCSR(prtd->dma_ch) |= DCSR_RUN; |
| 109 | break; |
| 110 | |
| 111 | default: |
| 112 | ret = -EINVAL; |
| 113 | } |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | EXPORT_SYMBOL(pxa2xx_pcm_trigger); |
| 118 | |
| 119 | snd_pcm_uframes_t |
| 120 | pxa2xx_pcm_pointer(struct snd_pcm_substream *substream) |
| 121 | { |
| 122 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 123 | struct pxa2xx_runtime_data *prtd = runtime->private_data; |
| 124 | |
| 125 | dma_addr_t ptr = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? |
| 126 | DSADR(prtd->dma_ch) : DTADR(prtd->dma_ch); |
| 127 | snd_pcm_uframes_t x = bytes_to_frames(runtime, ptr - runtime->dma_addr); |
| 128 | |
| 129 | if (x == runtime->buffer_size) |
| 130 | x = 0; |
| 131 | return x; |
| 132 | } |
| 133 | EXPORT_SYMBOL(pxa2xx_pcm_pointer); |
| 134 | |
| 135 | int __pxa2xx_pcm_prepare(struct snd_pcm_substream *substream) |
| 136 | { |
| 137 | struct pxa2xx_runtime_data *prtd = substream->runtime->private_data; |
| 138 | |
Mark Brown | f8bae4c | 2009-08-19 19:31:46 +0100 | [diff] [blame] | 139 | if (!prtd || !prtd->params) |
| 140 | return 0; |
| 141 | |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 142 | DCSR(prtd->dma_ch) &= ~DCSR_RUN; |
| 143 | DCSR(prtd->dma_ch) = 0; |
| 144 | DCMD(prtd->dma_ch) = 0; |
| 145 | *prtd->params->drcmr = prtd->dma_ch | DRCMR_MAPVLD; |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | EXPORT_SYMBOL(__pxa2xx_pcm_prepare); |
| 150 | |
| 151 | void pxa2xx_pcm_dma_irq(int dma_ch, void *dev_id) |
| 152 | { |
| 153 | struct snd_pcm_substream *substream = dev_id; |
| 154 | struct pxa2xx_runtime_data *rtd = substream->runtime->private_data; |
| 155 | int dcsr; |
| 156 | |
| 157 | dcsr = DCSR(dma_ch); |
| 158 | DCSR(dma_ch) = dcsr & ~DCSR_STOPIRQEN; |
| 159 | |
| 160 | if (dcsr & DCSR_ENDINTR) { |
| 161 | snd_pcm_period_elapsed(substream); |
| 162 | } else { |
| 163 | printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n", |
| 164 | rtd->params->name, dma_ch, dcsr); |
| 165 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
| 166 | } |
| 167 | } |
| 168 | EXPORT_SYMBOL(pxa2xx_pcm_dma_irq); |
| 169 | |
| 170 | int __pxa2xx_pcm_open(struct snd_pcm_substream *substream) |
| 171 | { |
| 172 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 173 | struct pxa2xx_runtime_data *rtd; |
| 174 | int ret; |
| 175 | |
| 176 | runtime->hw = pxa2xx_pcm_hardware; |
| 177 | |
| 178 | /* |
| 179 | * For mysterious reasons (and despite what the manual says) |
| 180 | * playback samples are lost if the DMA count is not a multiple |
| 181 | * of the DMA burst size. Let's add a rule to enforce that. |
| 182 | */ |
| 183 | ret = snd_pcm_hw_constraint_step(runtime, 0, |
| 184 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32); |
| 185 | if (ret) |
| 186 | goto out; |
| 187 | |
| 188 | ret = snd_pcm_hw_constraint_step(runtime, 0, |
| 189 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32); |
| 190 | if (ret) |
| 191 | goto out; |
| 192 | |
| 193 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 194 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 195 | if (ret < 0) |
| 196 | goto out; |
| 197 | |
| 198 | ret = -ENOMEM; |
Mark Brown | 13095c3 | 2008-10-22 13:27:49 +0100 | [diff] [blame] | 199 | rtd = kzalloc(sizeof(*rtd), GFP_KERNEL); |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 200 | if (!rtd) |
| 201 | goto out; |
| 202 | rtd->dma_desc_array = |
| 203 | dma_alloc_writecombine(substream->pcm->card->dev, PAGE_SIZE, |
| 204 | &rtd->dma_desc_array_phys, GFP_KERNEL); |
| 205 | if (!rtd->dma_desc_array) |
| 206 | goto err1; |
| 207 | |
Daniel Mack | 8727b90 | 2010-02-28 10:42:38 +0800 | [diff] [blame^] | 208 | rtd->dma_ch = -1; |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 209 | runtime->private_data = rtd; |
| 210 | return 0; |
| 211 | |
| 212 | err1: |
| 213 | kfree(rtd); |
| 214 | out: |
| 215 | return ret; |
| 216 | } |
| 217 | EXPORT_SYMBOL(__pxa2xx_pcm_open); |
| 218 | |
| 219 | int __pxa2xx_pcm_close(struct snd_pcm_substream *substream) |
| 220 | { |
| 221 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 222 | struct pxa2xx_runtime_data *rtd = runtime->private_data; |
| 223 | |
| 224 | dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE, |
| 225 | rtd->dma_desc_array, rtd->dma_desc_array_phys); |
| 226 | kfree(rtd); |
| 227 | return 0; |
| 228 | } |
| 229 | EXPORT_SYMBOL(__pxa2xx_pcm_close); |
| 230 | |
| 231 | int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream, |
| 232 | struct vm_area_struct *vma) |
| 233 | { |
| 234 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 235 | return dma_mmap_writecombine(substream->pcm->card->dev, vma, |
| 236 | runtime->dma_area, |
| 237 | runtime->dma_addr, |
| 238 | runtime->dma_bytes); |
| 239 | } |
| 240 | EXPORT_SYMBOL(pxa2xx_pcm_mmap); |
| 241 | |
| 242 | int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream) |
| 243 | { |
| 244 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 245 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
| 246 | size_t size = pxa2xx_pcm_hardware.buffer_bytes_max; |
| 247 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 248 | buf->dev.dev = pcm->card->dev; |
| 249 | buf->private_data = NULL; |
| 250 | buf->area = dma_alloc_writecombine(pcm->card->dev, size, |
| 251 | &buf->addr, GFP_KERNEL); |
| 252 | if (!buf->area) |
| 253 | return -ENOMEM; |
| 254 | buf->bytes = size; |
| 255 | return 0; |
| 256 | } |
| 257 | EXPORT_SYMBOL(pxa2xx_pcm_preallocate_dma_buffer); |
| 258 | |
| 259 | void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm) |
| 260 | { |
| 261 | struct snd_pcm_substream *substream; |
| 262 | struct snd_dma_buffer *buf; |
| 263 | int stream; |
| 264 | |
| 265 | for (stream = 0; stream < 2; stream++) { |
| 266 | substream = pcm->streams[stream].substream; |
| 267 | if (!substream) |
| 268 | continue; |
| 269 | buf = &substream->dma_buffer; |
| 270 | if (!buf->area) |
| 271 | continue; |
| 272 | dma_free_writecombine(pcm->card->dev, buf->bytes, |
| 273 | buf->area, buf->addr); |
| 274 | buf->area = NULL; |
| 275 | } |
| 276 | } |
| 277 | EXPORT_SYMBOL(pxa2xx_pcm_free_dma_buffers); |
| 278 | |
| 279 | MODULE_AUTHOR("Nicolas Pitre"); |
| 280 | MODULE_DESCRIPTION("Intel PXA2xx sound library"); |
| 281 | MODULE_LICENSE("GPL"); |