1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2014-2015 Broadcom Corporation
3 #include <linux/debugfs.h>
4 #include <linux/dma-mapping.h>
5 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <linux/timer.h>
10 #include <sound/core.h>
11 #include <sound/pcm.h>
12 #include <sound/pcm_params.h>
13 #include <sound/soc.h>
14 #include <sound/soc-dai.h>
16 #include "cygnus-ssp.h"
18 /* Register offset needed for ASoC PCM module */
20 #define INTH_R5F_STATUS_OFFSET 0x040
21 #define INTH_R5F_CLEAR_OFFSET 0x048
22 #define INTH_R5F_MASK_SET_OFFSET 0x050
23 #define INTH_R5F_MASK_CLEAR_OFFSET 0x054
25 #define BF_REARM_FREE_MARK_OFFSET 0x344
26 #define BF_REARM_FULL_MARK_OFFSET 0x348
28 /* Ring Buffer Ctrl Regs --- Start */
29 /* AUD_FMM_BF_CTRL_SOURCECH_RINGBUF_X_RDADDR_REG_BASE */
30 #define SRC_RBUF_0_RDADDR_OFFSET 0x500
31 #define SRC_RBUF_1_RDADDR_OFFSET 0x518
32 #define SRC_RBUF_2_RDADDR_OFFSET 0x530
33 #define SRC_RBUF_3_RDADDR_OFFSET 0x548
34 #define SRC_RBUF_4_RDADDR_OFFSET 0x560
35 #define SRC_RBUF_5_RDADDR_OFFSET 0x578
36 #define SRC_RBUF_6_RDADDR_OFFSET 0x590
38 /* AUD_FMM_BF_CTRL_SOURCECH_RINGBUF_X_WRADDR_REG_BASE */
39 #define SRC_RBUF_0_WRADDR_OFFSET 0x504
40 #define SRC_RBUF_1_WRADDR_OFFSET 0x51c
41 #define SRC_RBUF_2_WRADDR_OFFSET 0x534
42 #define SRC_RBUF_3_WRADDR_OFFSET 0x54c
43 #define SRC_RBUF_4_WRADDR_OFFSET 0x564
44 #define SRC_RBUF_5_WRADDR_OFFSET 0x57c
45 #define SRC_RBUF_6_WRADDR_OFFSET 0x594
47 /* AUD_FMM_BF_CTRL_SOURCECH_RINGBUF_X_BASEADDR_REG_BASE */
48 #define SRC_RBUF_0_BASEADDR_OFFSET 0x508
49 #define SRC_RBUF_1_BASEADDR_OFFSET 0x520
50 #define SRC_RBUF_2_BASEADDR_OFFSET 0x538
51 #define SRC_RBUF_3_BASEADDR_OFFSET 0x550
52 #define SRC_RBUF_4_BASEADDR_OFFSET 0x568
53 #define SRC_RBUF_5_BASEADDR_OFFSET 0x580
54 #define SRC_RBUF_6_BASEADDR_OFFSET 0x598
56 /* AUD_FMM_BF_CTRL_SOURCECH_RINGBUF_X_ENDADDR_REG_BASE */
57 #define SRC_RBUF_0_ENDADDR_OFFSET 0x50c
58 #define SRC_RBUF_1_ENDADDR_OFFSET 0x524
59 #define SRC_RBUF_2_ENDADDR_OFFSET 0x53c
60 #define SRC_RBUF_3_ENDADDR_OFFSET 0x554
61 #define SRC_RBUF_4_ENDADDR_OFFSET 0x56c
62 #define SRC_RBUF_5_ENDADDR_OFFSET 0x584
63 #define SRC_RBUF_6_ENDADDR_OFFSET 0x59c
65 /* AUD_FMM_BF_CTRL_SOURCECH_RINGBUF_X_FREE_MARK_REG_BASE */
66 #define SRC_RBUF_0_FREE_MARK_OFFSET 0x510
67 #define SRC_RBUF_1_FREE_MARK_OFFSET 0x528
68 #define SRC_RBUF_2_FREE_MARK_OFFSET 0x540
69 #define SRC_RBUF_3_FREE_MARK_OFFSET 0x558
70 #define SRC_RBUF_4_FREE_MARK_OFFSET 0x570
71 #define SRC_RBUF_5_FREE_MARK_OFFSET 0x588
72 #define SRC_RBUF_6_FREE_MARK_OFFSET 0x5a0
74 /* AUD_FMM_BF_CTRL_DESTCH_RINGBUF_X_RDADDR_REG_BASE */
75 #define DST_RBUF_0_RDADDR_OFFSET 0x5c0
76 #define DST_RBUF_1_RDADDR_OFFSET 0x5d8
77 #define DST_RBUF_2_RDADDR_OFFSET 0x5f0
78 #define DST_RBUF_3_RDADDR_OFFSET 0x608
79 #define DST_RBUF_4_RDADDR_OFFSET 0x620
80 #define DST_RBUF_5_RDADDR_OFFSET 0x638
82 /* AUD_FMM_BF_CTRL_DESTCH_RINGBUF_X_WRADDR_REG_BASE */
83 #define DST_RBUF_0_WRADDR_OFFSET 0x5c4
84 #define DST_RBUF_1_WRADDR_OFFSET 0x5dc
85 #define DST_RBUF_2_WRADDR_OFFSET 0x5f4
86 #define DST_RBUF_3_WRADDR_OFFSET 0x60c
87 #define DST_RBUF_4_WRADDR_OFFSET 0x624
88 #define DST_RBUF_5_WRADDR_OFFSET 0x63c
90 /* AUD_FMM_BF_CTRL_DESTCH_RINGBUF_X_BASEADDR_REG_BASE */
91 #define DST_RBUF_0_BASEADDR_OFFSET 0x5c8
92 #define DST_RBUF_1_BASEADDR_OFFSET 0x5e0
93 #define DST_RBUF_2_BASEADDR_OFFSET 0x5f8
94 #define DST_RBUF_3_BASEADDR_OFFSET 0x610
95 #define DST_RBUF_4_BASEADDR_OFFSET 0x628
96 #define DST_RBUF_5_BASEADDR_OFFSET 0x640
98 /* AUD_FMM_BF_CTRL_DESTCH_RINGBUF_X_ENDADDR_REG_BASE */
99 #define DST_RBUF_0_ENDADDR_OFFSET 0x5cc
100 #define DST_RBUF_1_ENDADDR_OFFSET 0x5e4
101 #define DST_RBUF_2_ENDADDR_OFFSET 0x5fc
102 #define DST_RBUF_3_ENDADDR_OFFSET 0x614
103 #define DST_RBUF_4_ENDADDR_OFFSET 0x62c
104 #define DST_RBUF_5_ENDADDR_OFFSET 0x644
106 /* AUD_FMM_BF_CTRL_DESTCH_RINGBUF_X_FULL_MARK_REG_BASE */
107 #define DST_RBUF_0_FULL_MARK_OFFSET 0x5d0
108 #define DST_RBUF_1_FULL_MARK_OFFSET 0x5e8
109 #define DST_RBUF_2_FULL_MARK_OFFSET 0x600
110 #define DST_RBUF_3_FULL_MARK_OFFSET 0x618
111 #define DST_RBUF_4_FULL_MARK_OFFSET 0x630
112 #define DST_RBUF_5_FULL_MARK_OFFSET 0x648
113 /* Ring Buffer Ctrl Regs --- End */
115 /* Error Status Regs --- Start */
116 /* AUD_FMM_BF_ESR_ESRX_STATUS_REG_BASE */
117 #define ESR0_STATUS_OFFSET 0x900
118 #define ESR1_STATUS_OFFSET 0x918
119 #define ESR2_STATUS_OFFSET 0x930
120 #define ESR3_STATUS_OFFSET 0x948
121 #define ESR4_STATUS_OFFSET 0x960
123 /* AUD_FMM_BF_ESR_ESRX_STATUS_CLEAR_REG_BASE */
124 #define ESR0_STATUS_CLR_OFFSET 0x908
125 #define ESR1_STATUS_CLR_OFFSET 0x920
126 #define ESR2_STATUS_CLR_OFFSET 0x938
127 #define ESR3_STATUS_CLR_OFFSET 0x950
128 #define ESR4_STATUS_CLR_OFFSET 0x968
130 /* AUD_FMM_BF_ESR_ESRX_MASK_REG_BASE */
131 #define ESR0_MASK_STATUS_OFFSET 0x90c
132 #define ESR1_MASK_STATUS_OFFSET 0x924
133 #define ESR2_MASK_STATUS_OFFSET 0x93c
134 #define ESR3_MASK_STATUS_OFFSET 0x954
135 #define ESR4_MASK_STATUS_OFFSET 0x96c
137 /* AUD_FMM_BF_ESR_ESRX_MASK_SET_REG_BASE */
138 #define ESR0_MASK_SET_OFFSET 0x910
139 #define ESR1_MASK_SET_OFFSET 0x928
140 #define ESR2_MASK_SET_OFFSET 0x940
141 #define ESR3_MASK_SET_OFFSET 0x958
142 #define ESR4_MASK_SET_OFFSET 0x970
144 /* AUD_FMM_BF_ESR_ESRX_MASK_CLEAR_REG_BASE */
145 #define ESR0_MASK_CLR_OFFSET 0x914
146 #define ESR1_MASK_CLR_OFFSET 0x92c
147 #define ESR2_MASK_CLR_OFFSET 0x944
148 #define ESR3_MASK_CLR_OFFSET 0x95c
149 #define ESR4_MASK_CLR_OFFSET 0x974
150 /* Error Status Regs --- End */
152 #define R5F_ESR0_SHIFT 0 /* esr0 = fifo underflow */
153 #define R5F_ESR1_SHIFT 1 /* esr1 = ringbuf underflow */
154 #define R5F_ESR2_SHIFT 2 /* esr2 = ringbuf overflow */
155 #define R5F_ESR3_SHIFT 3 /* esr3 = freemark */
156 #define R5F_ESR4_SHIFT 4 /* esr4 = fullmark */
159 /* Mask for R5F register. Set all relevant interrupt for playback handler */
160 #define ANY_PLAYBACK_IRQ (BIT(R5F_ESR0_SHIFT) | \
161 BIT(R5F_ESR1_SHIFT) | \
164 /* Mask for R5F register. Set all relevant interrupt for capture handler */
165 #define ANY_CAPTURE_IRQ (BIT(R5F_ESR2_SHIFT) | BIT(R5F_ESR4_SHIFT))
168 * PERIOD_BYTES_MIN is the number of bytes to at which the interrupt will tick.
169 * This number should be a multiple of 256. Minimum value is 256
171 #define PERIOD_BYTES_MIN 0x100
173 static const struct snd_pcm_hardware cygnus_pcm_hw
= {
174 .info
= SNDRV_PCM_INFO_MMAP
|
175 SNDRV_PCM_INFO_MMAP_VALID
|
176 SNDRV_PCM_INFO_INTERLEAVED
,
177 .formats
= SNDRV_PCM_FMTBIT_S16_LE
|
178 SNDRV_PCM_FMTBIT_S32_LE
,
180 /* A period is basically an interrupt */
181 .period_bytes_min
= PERIOD_BYTES_MIN
,
182 .period_bytes_max
= 0x10000,
184 /* period_min/max gives range of approx interrupts per buffer */
189 * maximum buffer size in bytes = period_bytes_max * periods_max
190 * We allocate this amount of data for each enabled channel
192 .buffer_bytes_max
= 4 * 0x8000,
195 static u64 cygnus_dma_dmamask
= DMA_BIT_MASK(32);
197 static struct cygnus_aio_port
*cygnus_dai_get_dma_data(
198 struct snd_pcm_substream
*substream
)
200 struct snd_soc_pcm_runtime
*soc_runtime
= snd_soc_substream_to_rtd(substream
);
202 return snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(soc_runtime
, 0), substream
);
205 static void ringbuf_set_initial(void __iomem
*audio_io
,
206 struct ringbuf_regs
*p_rbuf
,
215 u32 fmark_val
; /* free or full mark */
217 p_rbuf
->period_bytes
= periodsize
;
218 p_rbuf
->buf_size
= bufsize
;
221 /* Set the pointers to indicate full (flip uppermost bit) */
223 initial_wr
= initial_rd
^ BIT(31);
225 /* Set the pointers to indicate empty */
227 initial_rd
= initial_wr
;
230 end
= start
+ bufsize
- 1;
233 * The interrupt will fire when free/full mark is *exceeded*
234 * The fmark value must be multiple of PERIOD_BYTES_MIN so set fmark
235 * to be PERIOD_BYTES_MIN less than the period size.
237 fmark_val
= periodsize
- PERIOD_BYTES_MIN
;
239 writel(start
, audio_io
+ p_rbuf
->baseaddr
);
240 writel(end
, audio_io
+ p_rbuf
->endaddr
);
241 writel(fmark_val
, audio_io
+ p_rbuf
->fmark
);
242 writel(initial_rd
, audio_io
+ p_rbuf
->rdaddr
);
243 writel(initial_wr
, audio_io
+ p_rbuf
->wraddr
);
246 static int configure_ringbuf_regs(struct snd_pcm_substream
*substream
)
248 struct cygnus_aio_port
*aio
;
249 struct ringbuf_regs
*p_rbuf
;
252 aio
= cygnus_dai_get_dma_data(substream
);
254 /* Map the ssp portnum to a set of ring buffers. */
255 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
256 p_rbuf
= &aio
->play_rb_regs
;
258 switch (aio
->portnum
) {
260 *p_rbuf
= RINGBUF_REG_PLAYBACK(0);
263 *p_rbuf
= RINGBUF_REG_PLAYBACK(2);
266 *p_rbuf
= RINGBUF_REG_PLAYBACK(4);
269 *p_rbuf
= RINGBUF_REG_PLAYBACK(6);
275 p_rbuf
= &aio
->capture_rb_regs
;
277 switch (aio
->portnum
) {
279 *p_rbuf
= RINGBUF_REG_CAPTURE(0);
282 *p_rbuf
= RINGBUF_REG_CAPTURE(2);
285 *p_rbuf
= RINGBUF_REG_CAPTURE(4);
295 static struct ringbuf_regs
*get_ringbuf(struct snd_pcm_substream
*substream
)
297 struct cygnus_aio_port
*aio
;
298 struct ringbuf_regs
*p_rbuf
= NULL
;
300 aio
= cygnus_dai_get_dma_data(substream
);
302 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
303 p_rbuf
= &aio
->play_rb_regs
;
305 p_rbuf
= &aio
->capture_rb_regs
;
310 static void enable_intr(struct snd_pcm_substream
*substream
)
312 struct cygnus_aio_port
*aio
;
315 aio
= cygnus_dai_get_dma_data(substream
);
317 /* The port number maps to the bit position to be cleared */
318 clear_mask
= BIT(aio
->portnum
);
320 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
321 /* Clear interrupt status before enabling them */
322 writel(clear_mask
, aio
->cygaud
->audio
+ ESR0_STATUS_CLR_OFFSET
);
323 writel(clear_mask
, aio
->cygaud
->audio
+ ESR1_STATUS_CLR_OFFSET
);
324 writel(clear_mask
, aio
->cygaud
->audio
+ ESR3_STATUS_CLR_OFFSET
);
325 /* Unmask the interrupts of the given port*/
326 writel(clear_mask
, aio
->cygaud
->audio
+ ESR0_MASK_CLR_OFFSET
);
327 writel(clear_mask
, aio
->cygaud
->audio
+ ESR1_MASK_CLR_OFFSET
);
328 writel(clear_mask
, aio
->cygaud
->audio
+ ESR3_MASK_CLR_OFFSET
);
330 writel(ANY_PLAYBACK_IRQ
,
331 aio
->cygaud
->audio
+ INTH_R5F_MASK_CLEAR_OFFSET
);
333 writel(clear_mask
, aio
->cygaud
->audio
+ ESR2_STATUS_CLR_OFFSET
);
334 writel(clear_mask
, aio
->cygaud
->audio
+ ESR4_STATUS_CLR_OFFSET
);
335 writel(clear_mask
, aio
->cygaud
->audio
+ ESR2_MASK_CLR_OFFSET
);
336 writel(clear_mask
, aio
->cygaud
->audio
+ ESR4_MASK_CLR_OFFSET
);
338 writel(ANY_CAPTURE_IRQ
,
339 aio
->cygaud
->audio
+ INTH_R5F_MASK_CLEAR_OFFSET
);
344 static void disable_intr(struct snd_pcm_substream
*substream
)
346 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
347 struct cygnus_aio_port
*aio
;
350 aio
= cygnus_dai_get_dma_data(substream
);
352 dev_dbg(snd_soc_rtd_to_cpu(rtd
, 0)->dev
, "%s on port %d\n", __func__
, aio
->portnum
);
354 /* The port number maps to the bit position to be set */
355 set_mask
= BIT(aio
->portnum
);
357 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
358 /* Mask the interrupts of the given port*/
359 writel(set_mask
, aio
->cygaud
->audio
+ ESR0_MASK_SET_OFFSET
);
360 writel(set_mask
, aio
->cygaud
->audio
+ ESR1_MASK_SET_OFFSET
);
361 writel(set_mask
, aio
->cygaud
->audio
+ ESR3_MASK_SET_OFFSET
);
363 writel(set_mask
, aio
->cygaud
->audio
+ ESR2_MASK_SET_OFFSET
);
364 writel(set_mask
, aio
->cygaud
->audio
+ ESR4_MASK_SET_OFFSET
);
369 static int cygnus_pcm_trigger(struct snd_soc_component
*component
,
370 struct snd_pcm_substream
*substream
, int cmd
)
375 case SNDRV_PCM_TRIGGER_START
:
376 case SNDRV_PCM_TRIGGER_RESUME
:
377 enable_intr(substream
);
380 case SNDRV_PCM_TRIGGER_STOP
:
381 case SNDRV_PCM_TRIGGER_SUSPEND
:
382 disable_intr(substream
);
391 static void cygnus_pcm_period_elapsed(struct snd_pcm_substream
*substream
)
393 struct cygnus_aio_port
*aio
;
394 struct ringbuf_regs
*p_rbuf
= NULL
;
397 aio
= cygnus_dai_get_dma_data(substream
);
399 p_rbuf
= get_ringbuf(substream
);
402 * If free/full mark interrupt occurs, provide timestamp
403 * to ALSA and update appropriate idx by period_bytes
405 snd_pcm_period_elapsed(substream
);
407 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
408 /* Set the ring buffer to full */
409 regval
= readl(aio
->cygaud
->audio
+ p_rbuf
->rdaddr
);
410 regval
= regval
^ BIT(31);
411 writel(regval
, aio
->cygaud
->audio
+ p_rbuf
->wraddr
);
413 /* Set the ring buffer to empty */
414 regval
= readl(aio
->cygaud
->audio
+ p_rbuf
->wraddr
);
415 writel(regval
, aio
->cygaud
->audio
+ p_rbuf
->rdaddr
);
420 * ESR0/1/3 status Description
421 * 0x1 I2S0_out port caused interrupt
422 * 0x2 I2S1_out port caused interrupt
423 * 0x4 I2S2_out port caused interrupt
424 * 0x8 SPDIF_out port caused interrupt
426 static void handle_playback_irq(struct cygnus_audio
*cygaud
)
428 void __iomem
*audio_io
;
430 u32 esr_status0
, esr_status1
, esr_status3
;
432 audio_io
= cygaud
->audio
;
435 * ESR status gets updates with/without interrupts enabled.
436 * So, check the ESR mask, which provides interrupt enable/
437 * disable status and use it to determine which ESR status
438 * should be serviced.
440 esr_status0
= readl(audio_io
+ ESR0_STATUS_OFFSET
);
441 esr_status0
&= ~readl(audio_io
+ ESR0_MASK_STATUS_OFFSET
);
442 esr_status1
= readl(audio_io
+ ESR1_STATUS_OFFSET
);
443 esr_status1
&= ~readl(audio_io
+ ESR1_MASK_STATUS_OFFSET
);
444 esr_status3
= readl(audio_io
+ ESR3_STATUS_OFFSET
);
445 esr_status3
&= ~readl(audio_io
+ ESR3_MASK_STATUS_OFFSET
);
447 for (port
= 0; port
< CYGNUS_MAX_PLAYBACK_PORTS
; port
++) {
448 u32 esrmask
= BIT(port
);
451 * Ringbuffer or FIFO underflow
452 * If we get this interrupt then, it is also true that we have
453 * not yet responded to the freemark interrupt.
454 * Log a debug message. The freemark handler below will
455 * handle getting everything going again.
457 if ((esrmask
& esr_status1
) || (esrmask
& esr_status0
)) {
459 "Underrun: esr0=0x%x, esr1=0x%x esr3=0x%x\n",
460 esr_status0
, esr_status1
, esr_status3
);
464 * Freemark is hit. This is the normal interrupt.
465 * In typical operation the read and write regs will be equal
467 if (esrmask
& esr_status3
) {
468 struct snd_pcm_substream
*playstr
;
470 playstr
= cygaud
->portinfo
[port
].play_stream
;
471 cygnus_pcm_period_elapsed(playstr
);
475 /* Clear ESR interrupt */
476 writel(esr_status0
, audio_io
+ ESR0_STATUS_CLR_OFFSET
);
477 writel(esr_status1
, audio_io
+ ESR1_STATUS_CLR_OFFSET
);
478 writel(esr_status3
, audio_io
+ ESR3_STATUS_CLR_OFFSET
);
479 /* Rearm freemark logic by writing 1 to the correct bit */
480 writel(esr_status3
, audio_io
+ BF_REARM_FREE_MARK_OFFSET
);
484 * ESR2/4 status Description
485 * 0x1 I2S0_in port caused interrupt
486 * 0x2 I2S1_in port caused interrupt
487 * 0x4 I2S2_in port caused interrupt
489 static void handle_capture_irq(struct cygnus_audio
*cygaud
)
491 void __iomem
*audio_io
;
493 u32 esr_status2
, esr_status4
;
495 audio_io
= cygaud
->audio
;
498 * ESR status gets updates with/without interrupts enabled.
499 * So, check the ESR mask, which provides interrupt enable/
500 * disable status and use it to determine which ESR status
501 * should be serviced.
503 esr_status2
= readl(audio_io
+ ESR2_STATUS_OFFSET
);
504 esr_status2
&= ~readl(audio_io
+ ESR2_MASK_STATUS_OFFSET
);
505 esr_status4
= readl(audio_io
+ ESR4_STATUS_OFFSET
);
506 esr_status4
&= ~readl(audio_io
+ ESR4_MASK_STATUS_OFFSET
);
508 for (port
= 0; port
< CYGNUS_MAX_CAPTURE_PORTS
; port
++) {
509 u32 esrmask
= BIT(port
);
512 * Ringbuffer or FIFO overflow
513 * If we get this interrupt then, it is also true that we have
514 * not yet responded to the fullmark interrupt.
515 * Log a debug message. The fullmark handler below will
516 * handle getting everything going again.
518 if (esrmask
& esr_status2
)
520 "Overflow: esr2=0x%x\n", esr_status2
);
522 if (esrmask
& esr_status4
) {
523 struct snd_pcm_substream
*capstr
;
525 capstr
= cygaud
->portinfo
[port
].capture_stream
;
526 cygnus_pcm_period_elapsed(capstr
);
530 writel(esr_status2
, audio_io
+ ESR2_STATUS_CLR_OFFSET
);
531 writel(esr_status4
, audio_io
+ ESR4_STATUS_CLR_OFFSET
);
532 /* Rearm fullmark logic by writing 1 to the correct bit */
533 writel(esr_status4
, audio_io
+ BF_REARM_FULL_MARK_OFFSET
);
536 static irqreturn_t
cygnus_dma_irq(int irq
, void *data
)
539 struct cygnus_audio
*cygaud
= data
;
542 * R5 status bits Description
543 * 0 ESR0 (playback FIFO interrupt)
544 * 1 ESR1 (playback rbuf interrupt)
545 * 2 ESR2 (capture rbuf interrupt)
546 * 3 ESR3 (Freemark play. interrupt)
547 * 4 ESR4 (Fullmark capt. interrupt)
549 r5_status
= readl(cygaud
->audio
+ INTH_R5F_STATUS_OFFSET
);
551 if (!(r5_status
& (ANY_PLAYBACK_IRQ
| ANY_CAPTURE_IRQ
)))
554 /* If playback interrupt happened */
555 if (ANY_PLAYBACK_IRQ
& r5_status
) {
556 handle_playback_irq(cygaud
);
557 writel(ANY_PLAYBACK_IRQ
& r5_status
,
558 cygaud
->audio
+ INTH_R5F_CLEAR_OFFSET
);
561 /* If capture interrupt happened */
562 if (ANY_CAPTURE_IRQ
& r5_status
) {
563 handle_capture_irq(cygaud
);
564 writel(ANY_CAPTURE_IRQ
& r5_status
,
565 cygaud
->audio
+ INTH_R5F_CLEAR_OFFSET
);
571 static int cygnus_pcm_open(struct snd_soc_component
*component
,
572 struct snd_pcm_substream
*substream
)
574 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
575 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
576 struct cygnus_aio_port
*aio
;
579 aio
= cygnus_dai_get_dma_data(substream
);
583 dev_dbg(snd_soc_rtd_to_cpu(rtd
, 0)->dev
, "%s port %d\n", __func__
, aio
->portnum
);
585 snd_soc_set_runtime_hwparams(substream
, &cygnus_pcm_hw
);
587 ret
= snd_pcm_hw_constraint_step(runtime
, 0,
588 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, PERIOD_BYTES_MIN
);
592 ret
= snd_pcm_hw_constraint_step(runtime
, 0,
593 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, PERIOD_BYTES_MIN
);
597 * Keep track of which substream belongs to which port.
598 * This info is needed by snd_pcm_period_elapsed() in irq_handler
600 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
601 aio
->play_stream
= substream
;
603 aio
->capture_stream
= substream
;
608 static int cygnus_pcm_close(struct snd_soc_component
*component
,
609 struct snd_pcm_substream
*substream
)
611 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
612 struct cygnus_aio_port
*aio
;
614 aio
= cygnus_dai_get_dma_data(substream
);
616 dev_dbg(snd_soc_rtd_to_cpu(rtd
, 0)->dev
, "%s port %d\n", __func__
, aio
->portnum
);
618 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
619 aio
->play_stream
= NULL
;
621 aio
->capture_stream
= NULL
;
623 if (!aio
->play_stream
&& !aio
->capture_stream
)
624 dev_dbg(snd_soc_rtd_to_cpu(rtd
, 0)->dev
, "freed port %d\n", aio
->portnum
);
629 static int cygnus_pcm_prepare(struct snd_soc_component
*component
,
630 struct snd_pcm_substream
*substream
)
632 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
633 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
634 struct cygnus_aio_port
*aio
;
635 unsigned long bufsize
, periodsize
;
638 struct ringbuf_regs
*p_rbuf
= NULL
;
640 aio
= cygnus_dai_get_dma_data(substream
);
641 dev_dbg(snd_soc_rtd_to_cpu(rtd
, 0)->dev
, "%s port %d\n", __func__
, aio
->portnum
);
643 bufsize
= snd_pcm_lib_buffer_bytes(substream
);
644 periodsize
= snd_pcm_lib_period_bytes(substream
);
646 dev_dbg(snd_soc_rtd_to_cpu(rtd
, 0)->dev
, "%s (buf_size %lu) (period_size %lu)\n",
647 __func__
, bufsize
, periodsize
);
649 configure_ringbuf_regs(substream
);
651 p_rbuf
= get_ringbuf(substream
);
653 start
= runtime
->dma_addr
;
655 is_play
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) ? 1 : 0;
657 ringbuf_set_initial(aio
->cygaud
->audio
, p_rbuf
, is_play
, start
,
658 periodsize
, bufsize
);
663 static snd_pcm_uframes_t
cygnus_pcm_pointer(struct snd_soc_component
*component
,
664 struct snd_pcm_substream
*substream
)
666 struct cygnus_aio_port
*aio
;
667 unsigned int res
= 0, cur
= 0, base
= 0;
668 struct ringbuf_regs
*p_rbuf
= NULL
;
670 aio
= cygnus_dai_get_dma_data(substream
);
673 * Get the offset of the current read (for playack) or write
674 * index (for capture). Report this value back to the asoc framework.
676 p_rbuf
= get_ringbuf(substream
);
677 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
678 cur
= readl(aio
->cygaud
->audio
+ p_rbuf
->rdaddr
);
680 cur
= readl(aio
->cygaud
->audio
+ p_rbuf
->wraddr
);
682 base
= readl(aio
->cygaud
->audio
+ p_rbuf
->baseaddr
);
685 * Mask off the MSB of the rdaddr,wraddr and baseaddr
686 * since MSB is not part of the address
688 res
= (cur
& 0x7fffffff) - (base
& 0x7fffffff);
690 return bytes_to_frames(substream
->runtime
, res
);
693 static int cygnus_dma_new(struct snd_soc_component
*component
,
694 struct snd_soc_pcm_runtime
*rtd
)
696 size_t size
= cygnus_pcm_hw
.buffer_bytes_max
;
697 struct snd_card
*card
= rtd
->card
->snd_card
;
699 if (!card
->dev
->dma_mask
)
700 card
->dev
->dma_mask
= &cygnus_dma_dmamask
;
701 if (!card
->dev
->coherent_dma_mask
)
702 card
->dev
->coherent_dma_mask
= DMA_BIT_MASK(32);
704 snd_pcm_set_managed_buffer_all(rtd
->pcm
, SNDRV_DMA_TYPE_DEV
,
705 card
->dev
, size
, size
);
710 static const struct snd_soc_component_driver cygnus_soc_platform
= {
711 .open
= cygnus_pcm_open
,
712 .close
= cygnus_pcm_close
,
713 .prepare
= cygnus_pcm_prepare
,
714 .trigger
= cygnus_pcm_trigger
,
715 .pointer
= cygnus_pcm_pointer
,
716 .pcm_construct
= cygnus_dma_new
,
719 int cygnus_soc_platform_register(struct device
*dev
,
720 struct cygnus_audio
*cygaud
)
724 dev_dbg(dev
, "%s Enter\n", __func__
);
726 rc
= devm_request_irq(dev
, cygaud
->irq_num
, cygnus_dma_irq
,
727 IRQF_SHARED
, "cygnus-audio", cygaud
);
729 dev_err(dev
, "%s request_irq error %d\n", __func__
, rc
);
733 rc
= devm_snd_soc_register_component(dev
, &cygnus_soc_platform
,
736 dev_err(dev
, "%s failed\n", __func__
);
743 int cygnus_soc_platform_unregister(struct device
*dev
)
748 MODULE_LICENSE("GPL v2");
749 MODULE_AUTHOR("Broadcom");
750 MODULE_DESCRIPTION("Cygnus ASoC PCM module");