treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / sound / soc / bcm / cygnus-pcm.c
blob3a80c613bc3f9f6911c017d614d79d95ae0d2c10
1 /*
2 * Copyright (C) 2014-2015 Broadcom Corporation
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 #include <linux/debugfs.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/init.h>
16 #include <linux/io.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/timer.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
24 #include <sound/soc-dai.h>
26 #include "cygnus-ssp.h"
28 /* Register offset needed for ASoC PCM module */
30 #define INTH_R5F_STATUS_OFFSET 0x040
31 #define INTH_R5F_CLEAR_OFFSET 0x048
32 #define INTH_R5F_MASK_SET_OFFSET 0x050
33 #define INTH_R5F_MASK_CLEAR_OFFSET 0x054
35 #define BF_REARM_FREE_MARK_OFFSET 0x344
36 #define BF_REARM_FULL_MARK_OFFSET 0x348
38 /* Ring Buffer Ctrl Regs --- Start */
39 /* AUD_FMM_BF_CTRL_SOURCECH_RINGBUF_X_RDADDR_REG_BASE */
40 #define SRC_RBUF_0_RDADDR_OFFSET 0x500
41 #define SRC_RBUF_1_RDADDR_OFFSET 0x518
42 #define SRC_RBUF_2_RDADDR_OFFSET 0x530
43 #define SRC_RBUF_3_RDADDR_OFFSET 0x548
44 #define SRC_RBUF_4_RDADDR_OFFSET 0x560
45 #define SRC_RBUF_5_RDADDR_OFFSET 0x578
46 #define SRC_RBUF_6_RDADDR_OFFSET 0x590
48 /* AUD_FMM_BF_CTRL_SOURCECH_RINGBUF_X_WRADDR_REG_BASE */
49 #define SRC_RBUF_0_WRADDR_OFFSET 0x504
50 #define SRC_RBUF_1_WRADDR_OFFSET 0x51c
51 #define SRC_RBUF_2_WRADDR_OFFSET 0x534
52 #define SRC_RBUF_3_WRADDR_OFFSET 0x54c
53 #define SRC_RBUF_4_WRADDR_OFFSET 0x564
54 #define SRC_RBUF_5_WRADDR_OFFSET 0x57c
55 #define SRC_RBUF_6_WRADDR_OFFSET 0x594
57 /* AUD_FMM_BF_CTRL_SOURCECH_RINGBUF_X_BASEADDR_REG_BASE */
58 #define SRC_RBUF_0_BASEADDR_OFFSET 0x508
59 #define SRC_RBUF_1_BASEADDR_OFFSET 0x520
60 #define SRC_RBUF_2_BASEADDR_OFFSET 0x538
61 #define SRC_RBUF_3_BASEADDR_OFFSET 0x550
62 #define SRC_RBUF_4_BASEADDR_OFFSET 0x568
63 #define SRC_RBUF_5_BASEADDR_OFFSET 0x580
64 #define SRC_RBUF_6_BASEADDR_OFFSET 0x598
66 /* AUD_FMM_BF_CTRL_SOURCECH_RINGBUF_X_ENDADDR_REG_BASE */
67 #define SRC_RBUF_0_ENDADDR_OFFSET 0x50c
68 #define SRC_RBUF_1_ENDADDR_OFFSET 0x524
69 #define SRC_RBUF_2_ENDADDR_OFFSET 0x53c
70 #define SRC_RBUF_3_ENDADDR_OFFSET 0x554
71 #define SRC_RBUF_4_ENDADDR_OFFSET 0x56c
72 #define SRC_RBUF_5_ENDADDR_OFFSET 0x584
73 #define SRC_RBUF_6_ENDADDR_OFFSET 0x59c
75 /* AUD_FMM_BF_CTRL_SOURCECH_RINGBUF_X_FREE_MARK_REG_BASE */
76 #define SRC_RBUF_0_FREE_MARK_OFFSET 0x510
77 #define SRC_RBUF_1_FREE_MARK_OFFSET 0x528
78 #define SRC_RBUF_2_FREE_MARK_OFFSET 0x540
79 #define SRC_RBUF_3_FREE_MARK_OFFSET 0x558
80 #define SRC_RBUF_4_FREE_MARK_OFFSET 0x570
81 #define SRC_RBUF_5_FREE_MARK_OFFSET 0x588
82 #define SRC_RBUF_6_FREE_MARK_OFFSET 0x5a0
84 /* AUD_FMM_BF_CTRL_DESTCH_RINGBUF_X_RDADDR_REG_BASE */
85 #define DST_RBUF_0_RDADDR_OFFSET 0x5c0
86 #define DST_RBUF_1_RDADDR_OFFSET 0x5d8
87 #define DST_RBUF_2_RDADDR_OFFSET 0x5f0
88 #define DST_RBUF_3_RDADDR_OFFSET 0x608
89 #define DST_RBUF_4_RDADDR_OFFSET 0x620
90 #define DST_RBUF_5_RDADDR_OFFSET 0x638
92 /* AUD_FMM_BF_CTRL_DESTCH_RINGBUF_X_WRADDR_REG_BASE */
93 #define DST_RBUF_0_WRADDR_OFFSET 0x5c4
94 #define DST_RBUF_1_WRADDR_OFFSET 0x5dc
95 #define DST_RBUF_2_WRADDR_OFFSET 0x5f4
96 #define DST_RBUF_3_WRADDR_OFFSET 0x60c
97 #define DST_RBUF_4_WRADDR_OFFSET 0x624
98 #define DST_RBUF_5_WRADDR_OFFSET 0x63c
100 /* AUD_FMM_BF_CTRL_DESTCH_RINGBUF_X_BASEADDR_REG_BASE */
101 #define DST_RBUF_0_BASEADDR_OFFSET 0x5c8
102 #define DST_RBUF_1_BASEADDR_OFFSET 0x5e0
103 #define DST_RBUF_2_BASEADDR_OFFSET 0x5f8
104 #define DST_RBUF_3_BASEADDR_OFFSET 0x610
105 #define DST_RBUF_4_BASEADDR_OFFSET 0x628
106 #define DST_RBUF_5_BASEADDR_OFFSET 0x640
108 /* AUD_FMM_BF_CTRL_DESTCH_RINGBUF_X_ENDADDR_REG_BASE */
109 #define DST_RBUF_0_ENDADDR_OFFSET 0x5cc
110 #define DST_RBUF_1_ENDADDR_OFFSET 0x5e4
111 #define DST_RBUF_2_ENDADDR_OFFSET 0x5fc
112 #define DST_RBUF_3_ENDADDR_OFFSET 0x614
113 #define DST_RBUF_4_ENDADDR_OFFSET 0x62c
114 #define DST_RBUF_5_ENDADDR_OFFSET 0x644
116 /* AUD_FMM_BF_CTRL_DESTCH_RINGBUF_X_FULL_MARK_REG_BASE */
117 #define DST_RBUF_0_FULL_MARK_OFFSET 0x5d0
118 #define DST_RBUF_1_FULL_MARK_OFFSET 0x5e8
119 #define DST_RBUF_2_FULL_MARK_OFFSET 0x600
120 #define DST_RBUF_3_FULL_MARK_OFFSET 0x618
121 #define DST_RBUF_4_FULL_MARK_OFFSET 0x630
122 #define DST_RBUF_5_FULL_MARK_OFFSET 0x648
123 /* Ring Buffer Ctrl Regs --- End */
125 /* Error Status Regs --- Start */
126 /* AUD_FMM_BF_ESR_ESRX_STATUS_REG_BASE */
127 #define ESR0_STATUS_OFFSET 0x900
128 #define ESR1_STATUS_OFFSET 0x918
129 #define ESR2_STATUS_OFFSET 0x930
130 #define ESR3_STATUS_OFFSET 0x948
131 #define ESR4_STATUS_OFFSET 0x960
133 /* AUD_FMM_BF_ESR_ESRX_STATUS_CLEAR_REG_BASE */
134 #define ESR0_STATUS_CLR_OFFSET 0x908
135 #define ESR1_STATUS_CLR_OFFSET 0x920
136 #define ESR2_STATUS_CLR_OFFSET 0x938
137 #define ESR3_STATUS_CLR_OFFSET 0x950
138 #define ESR4_STATUS_CLR_OFFSET 0x968
140 /* AUD_FMM_BF_ESR_ESRX_MASK_REG_BASE */
141 #define ESR0_MASK_STATUS_OFFSET 0x90c
142 #define ESR1_MASK_STATUS_OFFSET 0x924
143 #define ESR2_MASK_STATUS_OFFSET 0x93c
144 #define ESR3_MASK_STATUS_OFFSET 0x954
145 #define ESR4_MASK_STATUS_OFFSET 0x96c
147 /* AUD_FMM_BF_ESR_ESRX_MASK_SET_REG_BASE */
148 #define ESR0_MASK_SET_OFFSET 0x910
149 #define ESR1_MASK_SET_OFFSET 0x928
150 #define ESR2_MASK_SET_OFFSET 0x940
151 #define ESR3_MASK_SET_OFFSET 0x958
152 #define ESR4_MASK_SET_OFFSET 0x970
154 /* AUD_FMM_BF_ESR_ESRX_MASK_CLEAR_REG_BASE */
155 #define ESR0_MASK_CLR_OFFSET 0x914
156 #define ESR1_MASK_CLR_OFFSET 0x92c
157 #define ESR2_MASK_CLR_OFFSET 0x944
158 #define ESR3_MASK_CLR_OFFSET 0x95c
159 #define ESR4_MASK_CLR_OFFSET 0x974
160 /* Error Status Regs --- End */
162 #define R5F_ESR0_SHIFT 0 /* esr0 = fifo underflow */
163 #define R5F_ESR1_SHIFT 1 /* esr1 = ringbuf underflow */
164 #define R5F_ESR2_SHIFT 2 /* esr2 = ringbuf overflow */
165 #define R5F_ESR3_SHIFT 3 /* esr3 = freemark */
166 #define R5F_ESR4_SHIFT 4 /* esr4 = fullmark */
169 /* Mask for R5F register. Set all relevant interrupt for playback handler */
170 #define ANY_PLAYBACK_IRQ (BIT(R5F_ESR0_SHIFT) | \
171 BIT(R5F_ESR1_SHIFT) | \
172 BIT(R5F_ESR3_SHIFT))
174 /* Mask for R5F register. Set all relevant interrupt for capture handler */
175 #define ANY_CAPTURE_IRQ (BIT(R5F_ESR2_SHIFT) | BIT(R5F_ESR4_SHIFT))
178 * PERIOD_BYTES_MIN is the number of bytes to at which the interrupt will tick.
179 * This number should be a multiple of 256. Minimum value is 256
181 #define PERIOD_BYTES_MIN 0x100
183 static const struct snd_pcm_hardware cygnus_pcm_hw = {
184 .info = SNDRV_PCM_INFO_MMAP |
185 SNDRV_PCM_INFO_MMAP_VALID |
186 SNDRV_PCM_INFO_INTERLEAVED,
187 .formats = SNDRV_PCM_FMTBIT_S16_LE |
188 SNDRV_PCM_FMTBIT_S32_LE,
190 /* A period is basically an interrupt */
191 .period_bytes_min = PERIOD_BYTES_MIN,
192 .period_bytes_max = 0x10000,
194 /* period_min/max gives range of approx interrupts per buffer */
195 .periods_min = 2,
196 .periods_max = 8,
199 * maximum buffer size in bytes = period_bytes_max * periods_max
200 * We allocate this amount of data for each enabled channel
202 .buffer_bytes_max = 4 * 0x8000,
205 static u64 cygnus_dma_dmamask = DMA_BIT_MASK(32);
207 static struct cygnus_aio_port *cygnus_dai_get_dma_data(
208 struct snd_pcm_substream *substream)
210 struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
212 return snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
215 static void ringbuf_set_initial(void __iomem *audio_io,
216 struct ringbuf_regs *p_rbuf,
217 bool is_playback,
218 u32 start,
219 u32 periodsize,
220 u32 bufsize)
222 u32 initial_rd;
223 u32 initial_wr;
224 u32 end;
225 u32 fmark_val; /* free or full mark */
227 p_rbuf->period_bytes = periodsize;
228 p_rbuf->buf_size = bufsize;
230 if (is_playback) {
231 /* Set the pointers to indicate full (flip uppermost bit) */
232 initial_rd = start;
233 initial_wr = initial_rd ^ BIT(31);
234 } else {
235 /* Set the pointers to indicate empty */
236 initial_wr = start;
237 initial_rd = initial_wr;
240 end = start + bufsize - 1;
243 * The interrupt will fire when free/full mark is *exceeded*
244 * The fmark value must be multiple of PERIOD_BYTES_MIN so set fmark
245 * to be PERIOD_BYTES_MIN less than the period size.
247 fmark_val = periodsize - PERIOD_BYTES_MIN;
249 writel(start, audio_io + p_rbuf->baseaddr);
250 writel(end, audio_io + p_rbuf->endaddr);
251 writel(fmark_val, audio_io + p_rbuf->fmark);
252 writel(initial_rd, audio_io + p_rbuf->rdaddr);
253 writel(initial_wr, audio_io + p_rbuf->wraddr);
256 static int configure_ringbuf_regs(struct snd_pcm_substream *substream)
258 struct cygnus_aio_port *aio;
259 struct ringbuf_regs *p_rbuf;
260 int status = 0;
262 aio = cygnus_dai_get_dma_data(substream);
264 /* Map the ssp portnum to a set of ring buffers. */
265 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
266 p_rbuf = &aio->play_rb_regs;
268 switch (aio->portnum) {
269 case 0:
270 *p_rbuf = RINGBUF_REG_PLAYBACK(0);
271 break;
272 case 1:
273 *p_rbuf = RINGBUF_REG_PLAYBACK(2);
274 break;
275 case 2:
276 *p_rbuf = RINGBUF_REG_PLAYBACK(4);
277 break;
278 case 3: /* SPDIF */
279 *p_rbuf = RINGBUF_REG_PLAYBACK(6);
280 break;
281 default:
282 status = -EINVAL;
284 } else {
285 p_rbuf = &aio->capture_rb_regs;
287 switch (aio->portnum) {
288 case 0:
289 *p_rbuf = RINGBUF_REG_CAPTURE(0);
290 break;
291 case 1:
292 *p_rbuf = RINGBUF_REG_CAPTURE(2);
293 break;
294 case 2:
295 *p_rbuf = RINGBUF_REG_CAPTURE(4);
296 break;
297 default:
298 status = -EINVAL;
302 return status;
305 static struct ringbuf_regs *get_ringbuf(struct snd_pcm_substream *substream)
307 struct cygnus_aio_port *aio;
308 struct ringbuf_regs *p_rbuf = NULL;
310 aio = cygnus_dai_get_dma_data(substream);
312 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
313 p_rbuf = &aio->play_rb_regs;
314 else
315 p_rbuf = &aio->capture_rb_regs;
317 return p_rbuf;
320 static void enable_intr(struct snd_pcm_substream *substream)
322 struct cygnus_aio_port *aio;
323 u32 clear_mask;
325 aio = cygnus_dai_get_dma_data(substream);
327 /* The port number maps to the bit position to be cleared */
328 clear_mask = BIT(aio->portnum);
330 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
331 /* Clear interrupt status before enabling them */
332 writel(clear_mask, aio->cygaud->audio + ESR0_STATUS_CLR_OFFSET);
333 writel(clear_mask, aio->cygaud->audio + ESR1_STATUS_CLR_OFFSET);
334 writel(clear_mask, aio->cygaud->audio + ESR3_STATUS_CLR_OFFSET);
335 /* Unmask the interrupts of the given port*/
336 writel(clear_mask, aio->cygaud->audio + ESR0_MASK_CLR_OFFSET);
337 writel(clear_mask, aio->cygaud->audio + ESR1_MASK_CLR_OFFSET);
338 writel(clear_mask, aio->cygaud->audio + ESR3_MASK_CLR_OFFSET);
340 writel(ANY_PLAYBACK_IRQ,
341 aio->cygaud->audio + INTH_R5F_MASK_CLEAR_OFFSET);
342 } else {
343 writel(clear_mask, aio->cygaud->audio + ESR2_STATUS_CLR_OFFSET);
344 writel(clear_mask, aio->cygaud->audio + ESR4_STATUS_CLR_OFFSET);
345 writel(clear_mask, aio->cygaud->audio + ESR2_MASK_CLR_OFFSET);
346 writel(clear_mask, aio->cygaud->audio + ESR4_MASK_CLR_OFFSET);
348 writel(ANY_CAPTURE_IRQ,
349 aio->cygaud->audio + INTH_R5F_MASK_CLEAR_OFFSET);
354 static void disable_intr(struct snd_pcm_substream *substream)
356 struct snd_soc_pcm_runtime *rtd = substream->private_data;
357 struct cygnus_aio_port *aio;
358 u32 set_mask;
360 aio = cygnus_dai_get_dma_data(substream);
362 dev_dbg(rtd->cpu_dai->dev, "%s on port %d\n", __func__, aio->portnum);
364 /* The port number maps to the bit position to be set */
365 set_mask = BIT(aio->portnum);
367 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
368 /* Mask the interrupts of the given port*/
369 writel(set_mask, aio->cygaud->audio + ESR0_MASK_SET_OFFSET);
370 writel(set_mask, aio->cygaud->audio + ESR1_MASK_SET_OFFSET);
371 writel(set_mask, aio->cygaud->audio + ESR3_MASK_SET_OFFSET);
372 } else {
373 writel(set_mask, aio->cygaud->audio + ESR2_MASK_SET_OFFSET);
374 writel(set_mask, aio->cygaud->audio + ESR4_MASK_SET_OFFSET);
379 static int cygnus_pcm_trigger(struct snd_soc_component *component,
380 struct snd_pcm_substream *substream, int cmd)
382 int ret = 0;
384 switch (cmd) {
385 case SNDRV_PCM_TRIGGER_START:
386 case SNDRV_PCM_TRIGGER_RESUME:
387 enable_intr(substream);
388 break;
390 case SNDRV_PCM_TRIGGER_STOP:
391 case SNDRV_PCM_TRIGGER_SUSPEND:
392 disable_intr(substream);
393 break;
394 default:
395 ret = -EINVAL;
398 return ret;
401 static void cygnus_pcm_period_elapsed(struct snd_pcm_substream *substream)
403 struct cygnus_aio_port *aio;
404 struct ringbuf_regs *p_rbuf = NULL;
405 u32 regval;
407 aio = cygnus_dai_get_dma_data(substream);
409 p_rbuf = get_ringbuf(substream);
412 * If free/full mark interrupt occurs, provide timestamp
413 * to ALSA and update appropriate idx by period_bytes
415 snd_pcm_period_elapsed(substream);
417 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
418 /* Set the ring buffer to full */
419 regval = readl(aio->cygaud->audio + p_rbuf->rdaddr);
420 regval = regval ^ BIT(31);
421 writel(regval, aio->cygaud->audio + p_rbuf->wraddr);
422 } else {
423 /* Set the ring buffer to empty */
424 regval = readl(aio->cygaud->audio + p_rbuf->wraddr);
425 writel(regval, aio->cygaud->audio + p_rbuf->rdaddr);
430 * ESR0/1/3 status Description
431 * 0x1 I2S0_out port caused interrupt
432 * 0x2 I2S1_out port caused interrupt
433 * 0x4 I2S2_out port caused interrupt
434 * 0x8 SPDIF_out port caused interrupt
436 static void handle_playback_irq(struct cygnus_audio *cygaud)
438 void __iomem *audio_io;
439 u32 port;
440 u32 esr_status0, esr_status1, esr_status3;
442 audio_io = cygaud->audio;
445 * ESR status gets updates with/without interrupts enabled.
446 * So, check the ESR mask, which provides interrupt enable/
447 * disable status and use it to determine which ESR status
448 * should be serviced.
450 esr_status0 = readl(audio_io + ESR0_STATUS_OFFSET);
451 esr_status0 &= ~readl(audio_io + ESR0_MASK_STATUS_OFFSET);
452 esr_status1 = readl(audio_io + ESR1_STATUS_OFFSET);
453 esr_status1 &= ~readl(audio_io + ESR1_MASK_STATUS_OFFSET);
454 esr_status3 = readl(audio_io + ESR3_STATUS_OFFSET);
455 esr_status3 &= ~readl(audio_io + ESR3_MASK_STATUS_OFFSET);
457 for (port = 0; port < CYGNUS_MAX_PLAYBACK_PORTS; port++) {
458 u32 esrmask = BIT(port);
461 * Ringbuffer or FIFO underflow
462 * If we get this interrupt then, it is also true that we have
463 * not yet responded to the freemark interrupt.
464 * Log a debug message. The freemark handler below will
465 * handle getting everything going again.
467 if ((esrmask & esr_status1) || (esrmask & esr_status0)) {
468 dev_dbg(cygaud->dev,
469 "Underrun: esr0=0x%x, esr1=0x%x esr3=0x%x\n",
470 esr_status0, esr_status1, esr_status3);
474 * Freemark is hit. This is the normal interrupt.
475 * In typical operation the read and write regs will be equal
477 if (esrmask & esr_status3) {
478 struct snd_pcm_substream *playstr;
480 playstr = cygaud->portinfo[port].play_stream;
481 cygnus_pcm_period_elapsed(playstr);
485 /* Clear ESR interrupt */
486 writel(esr_status0, audio_io + ESR0_STATUS_CLR_OFFSET);
487 writel(esr_status1, audio_io + ESR1_STATUS_CLR_OFFSET);
488 writel(esr_status3, audio_io + ESR3_STATUS_CLR_OFFSET);
489 /* Rearm freemark logic by writing 1 to the correct bit */
490 writel(esr_status3, audio_io + BF_REARM_FREE_MARK_OFFSET);
494 * ESR2/4 status Description
495 * 0x1 I2S0_in port caused interrupt
496 * 0x2 I2S1_in port caused interrupt
497 * 0x4 I2S2_in port caused interrupt
499 static void handle_capture_irq(struct cygnus_audio *cygaud)
501 void __iomem *audio_io;
502 u32 port;
503 u32 esr_status2, esr_status4;
505 audio_io = cygaud->audio;
508 * ESR status gets updates with/without interrupts enabled.
509 * So, check the ESR mask, which provides interrupt enable/
510 * disable status and use it to determine which ESR status
511 * should be serviced.
513 esr_status2 = readl(audio_io + ESR2_STATUS_OFFSET);
514 esr_status2 &= ~readl(audio_io + ESR2_MASK_STATUS_OFFSET);
515 esr_status4 = readl(audio_io + ESR4_STATUS_OFFSET);
516 esr_status4 &= ~readl(audio_io + ESR4_MASK_STATUS_OFFSET);
518 for (port = 0; port < CYGNUS_MAX_CAPTURE_PORTS; port++) {
519 u32 esrmask = BIT(port);
522 * Ringbuffer or FIFO overflow
523 * If we get this interrupt then, it is also true that we have
524 * not yet responded to the fullmark interrupt.
525 * Log a debug message. The fullmark handler below will
526 * handle getting everything going again.
528 if (esrmask & esr_status2)
529 dev_dbg(cygaud->dev,
530 "Overflow: esr2=0x%x\n", esr_status2);
532 if (esrmask & esr_status4) {
533 struct snd_pcm_substream *capstr;
535 capstr = cygaud->portinfo[port].capture_stream;
536 cygnus_pcm_period_elapsed(capstr);
540 writel(esr_status2, audio_io + ESR2_STATUS_CLR_OFFSET);
541 writel(esr_status4, audio_io + ESR4_STATUS_CLR_OFFSET);
542 /* Rearm fullmark logic by writing 1 to the correct bit */
543 writel(esr_status4, audio_io + BF_REARM_FULL_MARK_OFFSET);
546 static irqreturn_t cygnus_dma_irq(int irq, void *data)
548 u32 r5_status;
549 struct cygnus_audio *cygaud = data;
552 * R5 status bits Description
553 * 0 ESR0 (playback FIFO interrupt)
554 * 1 ESR1 (playback rbuf interrupt)
555 * 2 ESR2 (capture rbuf interrupt)
556 * 3 ESR3 (Freemark play. interrupt)
557 * 4 ESR4 (Fullmark capt. interrupt)
559 r5_status = readl(cygaud->audio + INTH_R5F_STATUS_OFFSET);
561 if (!(r5_status & (ANY_PLAYBACK_IRQ | ANY_CAPTURE_IRQ)))
562 return IRQ_NONE;
564 /* If playback interrupt happened */
565 if (ANY_PLAYBACK_IRQ & r5_status) {
566 handle_playback_irq(cygaud);
567 writel(ANY_PLAYBACK_IRQ & r5_status,
568 cygaud->audio + INTH_R5F_CLEAR_OFFSET);
571 /* If capture interrupt happened */
572 if (ANY_CAPTURE_IRQ & r5_status) {
573 handle_capture_irq(cygaud);
574 writel(ANY_CAPTURE_IRQ & r5_status,
575 cygaud->audio + INTH_R5F_CLEAR_OFFSET);
578 return IRQ_HANDLED;
581 static int cygnus_pcm_open(struct snd_soc_component *component,
582 struct snd_pcm_substream *substream)
584 struct snd_soc_pcm_runtime *rtd = substream->private_data;
585 struct snd_pcm_runtime *runtime = substream->runtime;
586 struct cygnus_aio_port *aio;
587 int ret;
589 aio = cygnus_dai_get_dma_data(substream);
590 if (!aio)
591 return -ENODEV;
593 dev_dbg(rtd->cpu_dai->dev, "%s port %d\n", __func__, aio->portnum);
595 snd_soc_set_runtime_hwparams(substream, &cygnus_pcm_hw);
597 ret = snd_pcm_hw_constraint_step(runtime, 0,
598 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, PERIOD_BYTES_MIN);
599 if (ret < 0)
600 return ret;
602 ret = snd_pcm_hw_constraint_step(runtime, 0,
603 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, PERIOD_BYTES_MIN);
604 if (ret < 0)
605 return ret;
607 * Keep track of which substream belongs to which port.
608 * This info is needed by snd_pcm_period_elapsed() in irq_handler
610 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
611 aio->play_stream = substream;
612 else
613 aio->capture_stream = substream;
615 return 0;
618 static int cygnus_pcm_close(struct snd_soc_component *component,
619 struct snd_pcm_substream *substream)
621 struct snd_soc_pcm_runtime *rtd = substream->private_data;
622 struct cygnus_aio_port *aio;
624 aio = cygnus_dai_get_dma_data(substream);
626 dev_dbg(rtd->cpu_dai->dev, "%s port %d\n", __func__, aio->portnum);
628 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
629 aio->play_stream = NULL;
630 else
631 aio->capture_stream = NULL;
633 if (!aio->play_stream && !aio->capture_stream)
634 dev_dbg(rtd->cpu_dai->dev, "freed port %d\n", aio->portnum);
636 return 0;
639 static int cygnus_pcm_hw_params(struct snd_soc_component *component,
640 struct snd_pcm_substream *substream,
641 struct snd_pcm_hw_params *params)
643 struct snd_soc_pcm_runtime *rtd = substream->private_data;
644 struct snd_pcm_runtime *runtime = substream->runtime;
645 struct cygnus_aio_port *aio;
647 aio = cygnus_dai_get_dma_data(substream);
648 dev_dbg(rtd->cpu_dai->dev, "%s port %d\n", __func__, aio->portnum);
650 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
651 runtime->dma_bytes = params_buffer_bytes(params);
653 return 0;
656 static int cygnus_pcm_hw_free(struct snd_soc_component *component,
657 struct snd_pcm_substream *substream)
659 struct snd_soc_pcm_runtime *rtd = substream->private_data;
660 struct cygnus_aio_port *aio;
662 aio = cygnus_dai_get_dma_data(substream);
663 dev_dbg(rtd->cpu_dai->dev, "%s port %d\n", __func__, aio->portnum);
665 snd_pcm_set_runtime_buffer(substream, NULL);
666 return 0;
669 static int cygnus_pcm_prepare(struct snd_soc_component *component,
670 struct snd_pcm_substream *substream)
672 struct snd_soc_pcm_runtime *rtd = substream->private_data;
673 struct snd_pcm_runtime *runtime = substream->runtime;
674 struct cygnus_aio_port *aio;
675 unsigned long bufsize, periodsize;
676 bool is_play;
677 u32 start;
678 struct ringbuf_regs *p_rbuf = NULL;
680 aio = cygnus_dai_get_dma_data(substream);
681 dev_dbg(rtd->cpu_dai->dev, "%s port %d\n", __func__, aio->portnum);
683 bufsize = snd_pcm_lib_buffer_bytes(substream);
684 periodsize = snd_pcm_lib_period_bytes(substream);
686 dev_dbg(rtd->cpu_dai->dev, "%s (buf_size %lu) (period_size %lu)\n",
687 __func__, bufsize, periodsize);
689 configure_ringbuf_regs(substream);
691 p_rbuf = get_ringbuf(substream);
693 start = runtime->dma_addr;
695 is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 1 : 0;
697 ringbuf_set_initial(aio->cygaud->audio, p_rbuf, is_play, start,
698 periodsize, bufsize);
700 return 0;
703 static snd_pcm_uframes_t cygnus_pcm_pointer(struct snd_soc_component *component,
704 struct snd_pcm_substream *substream)
706 struct cygnus_aio_port *aio;
707 unsigned int res = 0, cur = 0, base = 0;
708 struct ringbuf_regs *p_rbuf = NULL;
710 aio = cygnus_dai_get_dma_data(substream);
713 * Get the offset of the current read (for playack) or write
714 * index (for capture). Report this value back to the asoc framework.
716 p_rbuf = get_ringbuf(substream);
717 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
718 cur = readl(aio->cygaud->audio + p_rbuf->rdaddr);
719 else
720 cur = readl(aio->cygaud->audio + p_rbuf->wraddr);
722 base = readl(aio->cygaud->audio + p_rbuf->baseaddr);
725 * Mask off the MSB of the rdaddr,wraddr and baseaddr
726 * since MSB is not part of the address
728 res = (cur & 0x7fffffff) - (base & 0x7fffffff);
730 return bytes_to_frames(substream->runtime, res);
733 static int cygnus_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
735 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
736 struct snd_soc_pcm_runtime *rtd = substream->private_data;
737 struct snd_dma_buffer *buf = &substream->dma_buffer;
738 size_t size;
740 size = cygnus_pcm_hw.buffer_bytes_max;
742 buf->dev.type = SNDRV_DMA_TYPE_DEV;
743 buf->dev.dev = pcm->card->dev;
744 buf->private_data = NULL;
745 buf->area = dma_alloc_coherent(pcm->card->dev, size,
746 &buf->addr, GFP_KERNEL);
748 dev_dbg(rtd->cpu_dai->dev, "%s: size 0x%zx @ %pK\n",
749 __func__, size, buf->area);
751 if (!buf->area) {
752 dev_err(rtd->cpu_dai->dev, "%s: dma_alloc failed\n", __func__);
753 return -ENOMEM;
755 buf->bytes = size;
757 return 0;
760 static void cygnus_dma_free_dma_buffers(struct snd_soc_component *component,
761 struct snd_pcm *pcm)
763 struct snd_pcm_substream *substream;
764 struct snd_dma_buffer *buf;
766 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
767 if (substream) {
768 buf = &substream->dma_buffer;
769 if (buf->area) {
770 dma_free_coherent(pcm->card->dev, buf->bytes,
771 buf->area, buf->addr);
772 buf->area = NULL;
776 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
777 if (substream) {
778 buf = &substream->dma_buffer;
779 if (buf->area) {
780 dma_free_coherent(pcm->card->dev, buf->bytes,
781 buf->area, buf->addr);
782 buf->area = NULL;
787 static int cygnus_dma_new(struct snd_soc_component *component,
788 struct snd_soc_pcm_runtime *rtd)
790 struct snd_card *card = rtd->card->snd_card;
791 struct snd_pcm *pcm = rtd->pcm;
792 int ret;
794 if (!card->dev->dma_mask)
795 card->dev->dma_mask = &cygnus_dma_dmamask;
796 if (!card->dev->coherent_dma_mask)
797 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
799 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
800 ret = cygnus_pcm_preallocate_dma_buffer(pcm,
801 SNDRV_PCM_STREAM_PLAYBACK);
802 if (ret)
803 return ret;
806 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
807 ret = cygnus_pcm_preallocate_dma_buffer(pcm,
808 SNDRV_PCM_STREAM_CAPTURE);
809 if (ret) {
810 cygnus_dma_free_dma_buffers(component, pcm);
811 return ret;
815 return 0;
818 static struct snd_soc_component_driver cygnus_soc_platform = {
819 .open = cygnus_pcm_open,
820 .close = cygnus_pcm_close,
821 .hw_params = cygnus_pcm_hw_params,
822 .hw_free = cygnus_pcm_hw_free,
823 .prepare = cygnus_pcm_prepare,
824 .trigger = cygnus_pcm_trigger,
825 .pointer = cygnus_pcm_pointer,
826 .pcm_construct = cygnus_dma_new,
827 .pcm_destruct = cygnus_dma_free_dma_buffers,
830 int cygnus_soc_platform_register(struct device *dev,
831 struct cygnus_audio *cygaud)
833 int rc = 0;
835 dev_dbg(dev, "%s Enter\n", __func__);
837 rc = devm_request_irq(dev, cygaud->irq_num, cygnus_dma_irq,
838 IRQF_SHARED, "cygnus-audio", cygaud);
839 if (rc) {
840 dev_err(dev, "%s request_irq error %d\n", __func__, rc);
841 return rc;
844 rc = devm_snd_soc_register_component(dev, &cygnus_soc_platform,
845 NULL, 0);
846 if (rc) {
847 dev_err(dev, "%s failed\n", __func__);
848 return rc;
851 return 0;
854 int cygnus_soc_platform_unregister(struct device *dev)
856 return 0;
859 MODULE_LICENSE("GPL v2");
860 MODULE_AUTHOR("Broadcom");
861 MODULE_DESCRIPTION("Cygnus ASoC PCM module");