Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Au1000/Au1500/Au1100 Audio DMA support. |
| 4 | * |
| 5 | * (c) 2011 Manuel Lauss <manuel.lauss@googlemail.com> |
| 6 | * |
| 7 | * copied almost verbatim from the old ALSA driver, written by |
| 8 | * Charles Eidsness <charles@cooper-street.com> |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/dma-mapping.h> |
| 16 | #include <sound/core.h> |
| 17 | #include <sound/pcm.h> |
| 18 | #include <sound/pcm_params.h> |
| 19 | #include <sound/soc.h> |
| 20 | #include <asm/mach-au1x00/au1000.h> |
| 21 | #include <asm/mach-au1x00/au1000_dma.h> |
| 22 | |
| 23 | #include "psc.h" |
| 24 | |
Kuninori Morimoto | 781a9fc | 2018-01-29 02:50:54 +0000 | [diff] [blame] | 25 | #define DRV_NAME "au1x_dma" |
| 26 | |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 27 | struct pcm_period { |
| 28 | u32 start; |
| 29 | u32 relative_end; /* relative to start of buffer */ |
| 30 | struct pcm_period *next; |
| 31 | }; |
| 32 | |
| 33 | struct audio_stream { |
| 34 | struct snd_pcm_substream *substream; |
| 35 | int dma; |
| 36 | struct pcm_period *buffer; |
| 37 | unsigned int period_size; |
| 38 | unsigned int periods; |
| 39 | }; |
| 40 | |
| 41 | struct alchemy_pcm_ctx { |
| 42 | struct audio_stream stream[2]; /* playback & capture */ |
| 43 | }; |
| 44 | |
| 45 | static void au1000_release_dma_link(struct audio_stream *stream) |
| 46 | { |
| 47 | struct pcm_period *pointer; |
| 48 | struct pcm_period *pointer_next; |
| 49 | |
| 50 | stream->period_size = 0; |
| 51 | stream->periods = 0; |
| 52 | pointer = stream->buffer; |
| 53 | if (!pointer) |
| 54 | return; |
| 55 | do { |
| 56 | pointer_next = pointer->next; |
| 57 | kfree(pointer); |
| 58 | pointer = pointer_next; |
| 59 | } while (pointer != stream->buffer); |
| 60 | stream->buffer = NULL; |
| 61 | } |
| 62 | |
| 63 | static int au1000_setup_dma_link(struct audio_stream *stream, |
| 64 | unsigned int period_bytes, |
| 65 | unsigned int periods) |
| 66 | { |
| 67 | struct snd_pcm_substream *substream = stream->substream; |
| 68 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 69 | struct pcm_period *pointer; |
| 70 | unsigned long dma_start; |
| 71 | int i; |
| 72 | |
| 73 | dma_start = virt_to_phys(runtime->dma_area); |
| 74 | |
| 75 | if (stream->period_size == period_bytes && |
| 76 | stream->periods == periods) |
| 77 | return 0; /* not changed */ |
| 78 | |
| 79 | au1000_release_dma_link(stream); |
| 80 | |
| 81 | stream->period_size = period_bytes; |
| 82 | stream->periods = periods; |
| 83 | |
| 84 | stream->buffer = kmalloc(sizeof(struct pcm_period), GFP_KERNEL); |
| 85 | if (!stream->buffer) |
| 86 | return -ENOMEM; |
| 87 | pointer = stream->buffer; |
| 88 | for (i = 0; i < periods; i++) { |
| 89 | pointer->start = (u32)(dma_start + (i * period_bytes)); |
| 90 | pointer->relative_end = (u32) (((i+1) * period_bytes) - 0x1); |
| 91 | if (i < periods - 1) { |
| 92 | pointer->next = kmalloc(sizeof(struct pcm_period), |
| 93 | GFP_KERNEL); |
| 94 | if (!pointer->next) { |
| 95 | au1000_release_dma_link(stream); |
| 96 | return -ENOMEM; |
| 97 | } |
| 98 | pointer = pointer->next; |
| 99 | } |
| 100 | } |
| 101 | pointer->next = stream->buffer; |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static void au1000_dma_stop(struct audio_stream *stream) |
| 106 | { |
| 107 | if (stream->buffer) |
| 108 | disable_dma(stream->dma); |
| 109 | } |
| 110 | |
| 111 | static void au1000_dma_start(struct audio_stream *stream) |
| 112 | { |
| 113 | if (!stream->buffer) |
| 114 | return; |
| 115 | |
| 116 | init_dma(stream->dma); |
| 117 | if (get_dma_active_buffer(stream->dma) == 0) { |
| 118 | clear_dma_done0(stream->dma); |
| 119 | set_dma_addr0(stream->dma, stream->buffer->start); |
| 120 | set_dma_count0(stream->dma, stream->period_size >> 1); |
| 121 | set_dma_addr1(stream->dma, stream->buffer->next->start); |
| 122 | set_dma_count1(stream->dma, stream->period_size >> 1); |
| 123 | } else { |
| 124 | clear_dma_done1(stream->dma); |
| 125 | set_dma_addr1(stream->dma, stream->buffer->start); |
| 126 | set_dma_count1(stream->dma, stream->period_size >> 1); |
| 127 | set_dma_addr0(stream->dma, stream->buffer->next->start); |
| 128 | set_dma_count0(stream->dma, stream->period_size >> 1); |
| 129 | } |
| 130 | enable_dma_buffers(stream->dma); |
| 131 | start_dma(stream->dma); |
| 132 | } |
| 133 | |
| 134 | static irqreturn_t au1000_dma_interrupt(int irq, void *ptr) |
| 135 | { |
| 136 | struct audio_stream *stream = (struct audio_stream *)ptr; |
| 137 | struct snd_pcm_substream *substream = stream->substream; |
| 138 | |
| 139 | switch (get_dma_buffer_done(stream->dma)) { |
| 140 | case DMA_D0: |
| 141 | stream->buffer = stream->buffer->next; |
| 142 | clear_dma_done0(stream->dma); |
| 143 | set_dma_addr0(stream->dma, stream->buffer->next->start); |
| 144 | set_dma_count0(stream->dma, stream->period_size >> 1); |
| 145 | enable_dma_buffer0(stream->dma); |
| 146 | break; |
| 147 | case DMA_D1: |
| 148 | stream->buffer = stream->buffer->next; |
| 149 | clear_dma_done1(stream->dma); |
| 150 | set_dma_addr1(stream->dma, stream->buffer->next->start); |
| 151 | set_dma_count1(stream->dma, stream->period_size >> 1); |
| 152 | enable_dma_buffer1(stream->dma); |
| 153 | break; |
| 154 | case (DMA_D0 | DMA_D1): |
| 155 | pr_debug("DMA %d missed interrupt.\n", stream->dma); |
| 156 | au1000_dma_stop(stream); |
| 157 | au1000_dma_start(stream); |
| 158 | break; |
| 159 | case (~DMA_D0 & ~DMA_D1): |
| 160 | pr_debug("DMA %d empty irq.\n", stream->dma); |
| 161 | } |
| 162 | snd_pcm_period_elapsed(substream); |
| 163 | return IRQ_HANDLED; |
| 164 | } |
| 165 | |
| 166 | static const struct snd_pcm_hardware alchemy_pcm_hardware = { |
| 167 | .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | |
| 168 | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BATCH, |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 169 | .period_bytes_min = 1024, |
| 170 | .period_bytes_max = 16 * 1024 - 1, |
| 171 | .periods_min = 4, |
| 172 | .periods_max = 255, |
| 173 | .buffer_bytes_max = 128 * 1024, |
| 174 | .fifo_size = 16, |
| 175 | }; |
| 176 | |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 177 | static inline struct alchemy_pcm_ctx *ss_to_ctx(struct snd_pcm_substream *ss, |
| 178 | struct snd_soc_component *component) |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 179 | { |
Kuninori Morimoto | 781a9fc | 2018-01-29 02:50:54 +0000 | [diff] [blame] | 180 | return snd_soc_component_get_drvdata(component); |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 183 | static inline struct audio_stream *ss_to_as(struct snd_pcm_substream *ss, |
| 184 | struct snd_soc_component *component) |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 185 | { |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 186 | struct alchemy_pcm_ctx *ctx = ss_to_ctx(ss, component); |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 187 | return &(ctx->stream[ss->stream]); |
| 188 | } |
| 189 | |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 190 | static int alchemy_pcm_open(struct snd_soc_component *component, |
| 191 | struct snd_pcm_substream *substream) |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 192 | { |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 193 | struct alchemy_pcm_ctx *ctx = ss_to_ctx(substream, component); |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 194 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 195 | int *dmaids, s = substream->stream; |
| 196 | char *name; |
| 197 | |
| 198 | dmaids = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
| 199 | if (!dmaids) |
| 200 | return -ENODEV; /* whoa, has ordering changed? */ |
| 201 | |
| 202 | /* DMA setup */ |
| 203 | name = (s == SNDRV_PCM_STREAM_PLAYBACK) ? "audio-tx" : "audio-rx"; |
| 204 | ctx->stream[s].dma = request_au1000_dma(dmaids[s], name, |
Yong Zhang | 88e24c3 | 2011-09-22 16:59:20 +0800 | [diff] [blame] | 205 | au1000_dma_interrupt, 0, |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 206 | &ctx->stream[s]); |
| 207 | set_dma_mode(ctx->stream[s].dma, |
| 208 | get_dma_mode(ctx->stream[s].dma) & ~DMA_NC); |
| 209 | |
| 210 | ctx->stream[s].substream = substream; |
| 211 | ctx->stream[s].buffer = NULL; |
| 212 | snd_soc_set_runtime_hwparams(substream, &alchemy_pcm_hardware); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 217 | static int alchemy_pcm_close(struct snd_soc_component *component, |
| 218 | struct snd_pcm_substream *substream) |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 219 | { |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 220 | struct alchemy_pcm_ctx *ctx = ss_to_ctx(substream, component); |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 221 | int stype = substream->stream; |
| 222 | |
| 223 | ctx->stream[stype].substream = NULL; |
| 224 | free_au1000_dma(ctx->stream[stype].dma); |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 229 | static int alchemy_pcm_hw_params(struct snd_soc_component *component, |
| 230 | struct snd_pcm_substream *substream, |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 231 | struct snd_pcm_hw_params *hw_params) |
| 232 | { |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 233 | struct audio_stream *stream = ss_to_as(substream, component); |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 234 | int err; |
| 235 | |
| 236 | err = snd_pcm_lib_malloc_pages(substream, |
| 237 | params_buffer_bytes(hw_params)); |
| 238 | if (err < 0) |
| 239 | return err; |
| 240 | err = au1000_setup_dma_link(stream, |
| 241 | params_period_bytes(hw_params), |
| 242 | params_periods(hw_params)); |
| 243 | if (err) |
| 244 | snd_pcm_lib_free_pages(substream); |
| 245 | |
| 246 | return err; |
| 247 | } |
| 248 | |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 249 | static int alchemy_pcm_hw_free(struct snd_soc_component *component, |
| 250 | struct snd_pcm_substream *substream) |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 251 | { |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 252 | struct audio_stream *stream = ss_to_as(substream, component); |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 253 | au1000_release_dma_link(stream); |
| 254 | return snd_pcm_lib_free_pages(substream); |
| 255 | } |
| 256 | |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 257 | static int alchemy_pcm_trigger(struct snd_soc_component *component, |
| 258 | struct snd_pcm_substream *substream, int cmd) |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 259 | { |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 260 | struct audio_stream *stream = ss_to_as(substream, component); |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 261 | int err = 0; |
| 262 | |
| 263 | switch (cmd) { |
| 264 | case SNDRV_PCM_TRIGGER_START: |
| 265 | au1000_dma_start(stream); |
| 266 | break; |
| 267 | case SNDRV_PCM_TRIGGER_STOP: |
| 268 | au1000_dma_stop(stream); |
| 269 | break; |
| 270 | default: |
| 271 | err = -EINVAL; |
| 272 | break; |
| 273 | } |
| 274 | return err; |
| 275 | } |
| 276 | |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 277 | static snd_pcm_uframes_t alchemy_pcm_pointer(struct snd_soc_component *component, |
| 278 | struct snd_pcm_substream *ss) |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 279 | { |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 280 | struct audio_stream *stream = ss_to_as(ss, component); |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 281 | long location; |
| 282 | |
| 283 | location = get_dma_residue(stream->dma); |
| 284 | location = stream->buffer->relative_end - location; |
| 285 | if (location == -1) |
| 286 | location = 0; |
| 287 | return bytes_to_frames(ss->runtime, location); |
| 288 | } |
| 289 | |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 290 | static int alchemy_pcm_new(struct snd_soc_component *component, |
| 291 | struct snd_soc_pcm_runtime *rtd) |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 292 | { |
| 293 | struct snd_pcm *pcm = rtd->pcm; |
| 294 | |
| 295 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, |
| 296 | snd_dma_continuous_data(GFP_KERNEL), 65536, (4096 * 1024) - 1); |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Kuninori Morimoto | 781a9fc | 2018-01-29 02:50:54 +0000 | [diff] [blame] | 301 | static struct snd_soc_component_driver alchemy_pcm_soc_component = { |
| 302 | .name = DRV_NAME, |
Kuninori Morimoto | 297bdfd | 2019-10-02 14:32:13 +0900 | [diff] [blame^] | 303 | .open = alchemy_pcm_open, |
| 304 | .close = alchemy_pcm_close, |
| 305 | .ioctl = snd_soc_pcm_lib_ioctl, |
| 306 | .hw_params = alchemy_pcm_hw_params, |
| 307 | .hw_free = alchemy_pcm_hw_free, |
| 308 | .trigger = alchemy_pcm_trigger, |
| 309 | .pointer = alchemy_pcm_pointer, |
| 310 | .pcm_construct = alchemy_pcm_new, |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 311 | }; |
| 312 | |
Bill Pemberton | 5c658be | 2012-12-07 09:26:22 -0500 | [diff] [blame] | 313 | static int alchemy_pcm_drvprobe(struct platform_device *pdev) |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 314 | { |
| 315 | struct alchemy_pcm_ctx *ctx; |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 316 | |
Julia Lawall | 46c3a02 | 2011-12-29 17:51:27 +0100 | [diff] [blame] | 317 | ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 318 | if (!ctx) |
| 319 | return -ENOMEM; |
| 320 | |
| 321 | platform_set_drvdata(pdev, ctx); |
| 322 | |
Kuninori Morimoto | 781a9fc | 2018-01-29 02:50:54 +0000 | [diff] [blame] | 323 | return devm_snd_soc_register_component(&pdev->dev, |
| 324 | &alchemy_pcm_soc_component, NULL, 0); |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static struct platform_driver alchemy_pcmdma_driver = { |
| 328 | .driver = { |
| 329 | .name = "alchemy-pcm-dma", |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 330 | }, |
| 331 | .probe = alchemy_pcm_drvprobe, |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 332 | }; |
| 333 | |
Axel Lin | 8a124f9 | 2011-11-25 10:06:59 +0800 | [diff] [blame] | 334 | module_platform_driver(alchemy_pcmdma_driver); |
Manuel Lauss | b3c70c9 | 2011-07-25 13:44:45 +0200 | [diff] [blame] | 335 | |
| 336 | MODULE_LICENSE("GPL"); |
| 337 | MODULE_DESCRIPTION("Au1000/Au1500/Au1100 Audio DMA driver"); |
| 338 | MODULE_AUTHOR("Manuel Lauss"); |