1 // SPDX-License-Identifier: GPL-2.0-only
3 * ALSA SoC I2S Audio Layer for Broadcom BCM2835 SoC
5 * Author: Florian Meier <florian.meier@koalo.de>
9 * Raspberry Pi PCM I2S ALSA Driver
10 * Copyright (c) by Phil Poole 2013
12 * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
13 * Vladimir Barinov, <vbarinov@embeddedalley.com>
14 * Copyright (C) 2007 MontaVista Software, Inc., <source@mvista.com>
16 * OMAP ALSA SoC DAI driver using McBSP port
17 * Copyright (C) 2008 Nokia Corporation
18 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
19 * Peter Ujfalusi <peter.ujfalusi@ti.com>
21 * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
22 * Author: Timur Tabi <timur@freescale.com>
23 * Copyright 2007-2010 Freescale Semiconductor, Inc.
26 #include <linux/bitops.h>
27 #include <linux/clk.h>
28 #include <linux/delay.h>
29 #include <linux/device.h>
30 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <linux/of_address.h>
34 #include <linux/slab.h>
36 #include <sound/core.h>
37 #include <sound/dmaengine_pcm.h>
38 #include <sound/initval.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/soc.h>
44 #define BCM2835_I2S_CS_A_REG 0x00
45 #define BCM2835_I2S_FIFO_A_REG 0x04
46 #define BCM2835_I2S_MODE_A_REG 0x08
47 #define BCM2835_I2S_RXC_A_REG 0x0c
48 #define BCM2835_I2S_TXC_A_REG 0x10
49 #define BCM2835_I2S_DREQ_A_REG 0x14
50 #define BCM2835_I2S_INTEN_A_REG 0x18
51 #define BCM2835_I2S_INTSTC_A_REG 0x1c
52 #define BCM2835_I2S_GRAY_REG 0x20
54 /* I2S register settings */
55 #define BCM2835_I2S_STBY BIT(25)
56 #define BCM2835_I2S_SYNC BIT(24)
57 #define BCM2835_I2S_RXSEX BIT(23)
58 #define BCM2835_I2S_RXF BIT(22)
59 #define BCM2835_I2S_TXE BIT(21)
60 #define BCM2835_I2S_RXD BIT(20)
61 #define BCM2835_I2S_TXD BIT(19)
62 #define BCM2835_I2S_RXR BIT(18)
63 #define BCM2835_I2S_TXW BIT(17)
64 #define BCM2835_I2S_CS_RXERR BIT(16)
65 #define BCM2835_I2S_CS_TXERR BIT(15)
66 #define BCM2835_I2S_RXSYNC BIT(14)
67 #define BCM2835_I2S_TXSYNC BIT(13)
68 #define BCM2835_I2S_DMAEN BIT(9)
69 #define BCM2835_I2S_RXTHR(v) ((v) << 7)
70 #define BCM2835_I2S_TXTHR(v) ((v) << 5)
71 #define BCM2835_I2S_RXCLR BIT(4)
72 #define BCM2835_I2S_TXCLR BIT(3)
73 #define BCM2835_I2S_TXON BIT(2)
74 #define BCM2835_I2S_RXON BIT(1)
75 #define BCM2835_I2S_EN (1)
77 #define BCM2835_I2S_CLKDIS BIT(28)
78 #define BCM2835_I2S_PDMN BIT(27)
79 #define BCM2835_I2S_PDME BIT(26)
80 #define BCM2835_I2S_FRXP BIT(25)
81 #define BCM2835_I2S_FTXP BIT(24)
82 #define BCM2835_I2S_CLKM BIT(23)
83 #define BCM2835_I2S_CLKI BIT(22)
84 #define BCM2835_I2S_FSM BIT(21)
85 #define BCM2835_I2S_FSI BIT(20)
86 #define BCM2835_I2S_FLEN(v) ((v) << 10)
87 #define BCM2835_I2S_FSLEN(v) (v)
89 #define BCM2835_I2S_CHWEX BIT(15)
90 #define BCM2835_I2S_CHEN BIT(14)
91 #define BCM2835_I2S_CHPOS(v) ((v) << 4)
92 #define BCM2835_I2S_CHWID(v) (v)
93 #define BCM2835_I2S_CH1(v) ((v) << 16)
94 #define BCM2835_I2S_CH2(v) (v)
95 #define BCM2835_I2S_CH1_POS(v) BCM2835_I2S_CH1(BCM2835_I2S_CHPOS(v))
96 #define BCM2835_I2S_CH2_POS(v) BCM2835_I2S_CH2(BCM2835_I2S_CHPOS(v))
98 #define BCM2835_I2S_TX_PANIC(v) ((v) << 24)
99 #define BCM2835_I2S_RX_PANIC(v) ((v) << 16)
100 #define BCM2835_I2S_TX(v) ((v) << 8)
101 #define BCM2835_I2S_RX(v) (v)
103 #define BCM2835_I2S_INT_RXERR BIT(3)
104 #define BCM2835_I2S_INT_TXERR BIT(2)
105 #define BCM2835_I2S_INT_RXR BIT(1)
106 #define BCM2835_I2S_INT_TXW BIT(0)
108 /* Frame length register is 10 bit, maximum length 1024 */
109 #define BCM2835_I2S_MAX_FRAME_LENGTH 1024
111 /* General device struct */
112 struct bcm2835_i2s_dev
{
114 struct snd_dmaengine_dai_dma_data dma_data
[2];
116 unsigned int tdm_slots
;
117 unsigned int rx_mask
;
118 unsigned int tx_mask
;
119 unsigned int slot_width
;
120 unsigned int frame_length
;
122 struct regmap
*i2s_regmap
;
128 static void bcm2835_i2s_start_clock(struct bcm2835_i2s_dev
*dev
)
130 unsigned int master
= dev
->fmt
& SND_SOC_DAIFMT_MASTER_MASK
;
132 if (dev
->clk_prepared
)
136 case SND_SOC_DAIFMT_CBS_CFS
:
137 case SND_SOC_DAIFMT_CBS_CFM
:
138 clk_prepare_enable(dev
->clk
);
139 dev
->clk_prepared
= true;
146 static void bcm2835_i2s_stop_clock(struct bcm2835_i2s_dev
*dev
)
148 if (dev
->clk_prepared
)
149 clk_disable_unprepare(dev
->clk
);
150 dev
->clk_prepared
= false;
153 static void bcm2835_i2s_clear_fifos(struct bcm2835_i2s_dev
*dev
,
159 uint32_t i2s_active_state
;
160 bool clk_was_prepared
;
164 off
= tx
? BCM2835_I2S_TXON
: 0;
165 off
|= rx
? BCM2835_I2S_RXON
: 0;
167 clr
= tx
? BCM2835_I2S_TXCLR
: 0;
168 clr
|= rx
? BCM2835_I2S_RXCLR
: 0;
170 /* Backup the current state */
171 regmap_read(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
, &csreg
);
172 i2s_active_state
= csreg
& (BCM2835_I2S_RXON
| BCM2835_I2S_TXON
);
174 /* Start clock if not running */
175 clk_was_prepared
= dev
->clk_prepared
;
176 if (!clk_was_prepared
)
177 bcm2835_i2s_start_clock(dev
);
179 /* Stop I2S module */
180 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
, off
, 0);
184 * Requires at least 2 PCM clock cycles to take effect
186 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
, clr
, clr
);
188 /* Wait for 2 PCM clock cycles */
191 * Toggle the SYNC flag. After 2 PCM clock cycles it can be read back
192 * FIXME: This does not seem to work for slave mode!
194 regmap_read(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
, &syncval
);
195 syncval
&= BCM2835_I2S_SYNC
;
197 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
,
198 BCM2835_I2S_SYNC
, ~syncval
);
200 /* Wait for the SYNC flag changing it's state */
202 regmap_read(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
, &csreg
);
203 if ((csreg
& BCM2835_I2S_SYNC
) != syncval
)
208 dev_err(dev
->dev
, "I2S SYNC error!\n");
210 /* Stop clock if it was not running before */
211 if (!clk_was_prepared
)
212 bcm2835_i2s_stop_clock(dev
);
214 /* Restore I2S state */
215 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
,
216 BCM2835_I2S_RXON
| BCM2835_I2S_TXON
, i2s_active_state
);
219 static int bcm2835_i2s_set_dai_fmt(struct snd_soc_dai
*dai
,
222 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
227 static int bcm2835_i2s_set_dai_bclk_ratio(struct snd_soc_dai
*dai
,
230 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
237 if (ratio
> BCM2835_I2S_MAX_FRAME_LENGTH
)
243 dev
->slot_width
= ratio
/ 2;
244 dev
->frame_length
= ratio
;
249 static int bcm2835_i2s_set_dai_tdm_slot(struct snd_soc_dai
*dai
,
250 unsigned int tx_mask
, unsigned int rx_mask
,
251 int slots
, int width
)
253 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
256 if (slots
< 0 || width
< 0)
259 /* Limit masks to available slots */
260 rx_mask
&= GENMASK(slots
- 1, 0);
261 tx_mask
&= GENMASK(slots
- 1, 0);
264 * The driver is limited to 2-channel setups.
265 * Check that exactly 2 bits are set in the masks.
267 if (hweight_long((unsigned long) rx_mask
) != 2
268 || hweight_long((unsigned long) tx_mask
) != 2)
271 if (slots
* width
> BCM2835_I2S_MAX_FRAME_LENGTH
)
275 dev
->tdm_slots
= slots
;
277 dev
->rx_mask
= rx_mask
;
278 dev
->tx_mask
= tx_mask
;
279 dev
->slot_width
= width
;
280 dev
->frame_length
= slots
* width
;
286 * Convert logical slot number into physical slot number.
288 * If odd_offset is 0 sequential number is identical to logical number.
289 * This is used for DSP modes with slot numbering 0 1 2 3 ...
291 * Otherwise odd_offset defines the physical offset for odd numbered
292 * slots. This is used for I2S and left/right justified modes to
293 * translate from logical slot numbers 0 1 2 3 ... into physical slot
294 * numbers 0 2 ... 3 4 ...
296 static int bcm2835_i2s_convert_slot(unsigned int slot
, unsigned int odd_offset
)
302 return (slot
>> 1) + odd_offset
;
308 * Calculate channel position from mask and slot width.
310 * Mask must contain exactly 2 set bits.
311 * Lowest set bit is channel 1 position, highest set bit channel 2.
312 * The constant offset is added to both channel positions.
314 * If odd_offset is > 0 slot positions are translated to
315 * I2S-style TDM slot numbering ( 0 2 ... 3 4 ...) with odd
316 * logical slot numbers starting at physical slot odd_offset.
318 static void bcm2835_i2s_calc_channel_pos(
319 unsigned int *ch1_pos
, unsigned int *ch2_pos
,
320 unsigned int mask
, unsigned int width
,
321 unsigned int bit_offset
, unsigned int odd_offset
)
323 *ch1_pos
= bcm2835_i2s_convert_slot((ffs(mask
) - 1), odd_offset
)
324 * width
+ bit_offset
;
325 *ch2_pos
= bcm2835_i2s_convert_slot((fls(mask
) - 1), odd_offset
)
326 * width
+ bit_offset
;
329 static int bcm2835_i2s_hw_params(struct snd_pcm_substream
*substream
,
330 struct snd_pcm_hw_params
*params
,
331 struct snd_soc_dai
*dai
)
333 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
334 unsigned int data_length
, data_delay
, framesync_length
;
335 unsigned int slots
, slot_width
, odd_slot_offset
;
336 int frame_length
, bclk_rate
;
337 unsigned int rx_mask
, tx_mask
;
338 unsigned int rx_ch1_pos
, rx_ch2_pos
, tx_ch1_pos
, tx_ch2_pos
;
339 unsigned int mode
, format
;
340 bool bit_clock_master
= false;
341 bool frame_sync_master
= false;
342 bool frame_start_falling_edge
= false;
347 * If a stream is already enabled,
348 * the registers are already set properly.
350 regmap_read(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
, &csreg
);
352 if (csreg
& (BCM2835_I2S_TXON
| BCM2835_I2S_RXON
))
355 data_length
= params_width(params
);
360 if (dev
->tdm_slots
) {
361 slots
= dev
->tdm_slots
;
362 slot_width
= dev
->slot_width
;
363 frame_length
= dev
->frame_length
;
364 rx_mask
= dev
->rx_mask
;
365 tx_mask
= dev
->tx_mask
;
366 bclk_rate
= dev
->frame_length
* params_rate(params
);
369 slot_width
= params_width(params
);
373 frame_length
= snd_soc_params_to_frame_size(params
);
374 if (frame_length
< 0)
377 bclk_rate
= snd_soc_params_to_bclk(params
);
382 /* Check if data fits into slots */
383 if (data_length
> slot_width
)
386 /* Check if CPU is bit clock master */
387 switch (dev
->fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
388 case SND_SOC_DAIFMT_CBS_CFS
:
389 case SND_SOC_DAIFMT_CBS_CFM
:
390 bit_clock_master
= true;
392 case SND_SOC_DAIFMT_CBM_CFS
:
393 case SND_SOC_DAIFMT_CBM_CFM
:
394 bit_clock_master
= false;
400 /* Check if CPU is frame sync master */
401 switch (dev
->fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
402 case SND_SOC_DAIFMT_CBS_CFS
:
403 case SND_SOC_DAIFMT_CBM_CFS
:
404 frame_sync_master
= true;
406 case SND_SOC_DAIFMT_CBS_CFM
:
407 case SND_SOC_DAIFMT_CBM_CFM
:
408 frame_sync_master
= false;
414 /* Clock should only be set up here if CPU is clock master */
415 if (bit_clock_master
&&
416 (!dev
->clk_prepared
|| dev
->clk_rate
!= bclk_rate
)) {
417 if (dev
->clk_prepared
)
418 bcm2835_i2s_stop_clock(dev
);
420 if (dev
->clk_rate
!= bclk_rate
) {
421 ret
= clk_set_rate(dev
->clk
, bclk_rate
);
424 dev
->clk_rate
= bclk_rate
;
427 bcm2835_i2s_start_clock(dev
);
430 /* Setup the frame format */
431 format
= BCM2835_I2S_CHEN
;
433 if (data_length
>= 24)
434 format
|= BCM2835_I2S_CHWEX
;
436 format
|= BCM2835_I2S_CHWID((data_length
-8)&0xf);
438 /* CH2 format is the same as for CH1 */
439 format
= BCM2835_I2S_CH1(format
) | BCM2835_I2S_CH2(format
);
441 switch (dev
->fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
442 case SND_SOC_DAIFMT_I2S
:
443 /* I2S mode needs an even number of slots */
448 * Use I2S-style logical slot numbering: even slots
449 * are in first half of frame, odd slots in second half.
451 odd_slot_offset
= slots
>> 1;
453 /* MSB starts one cycle after frame start */
456 /* Setup frame sync signal for 50% duty cycle */
457 framesync_length
= frame_length
/ 2;
458 frame_start_falling_edge
= true;
460 case SND_SOC_DAIFMT_LEFT_J
:
464 odd_slot_offset
= slots
>> 1;
466 framesync_length
= frame_length
/ 2;
467 frame_start_falling_edge
= false;
469 case SND_SOC_DAIFMT_RIGHT_J
:
473 /* Odd frame lengths aren't supported */
474 if (frame_length
& 1)
477 odd_slot_offset
= slots
>> 1;
478 data_delay
= slot_width
- data_length
;
479 framesync_length
= frame_length
/ 2;
480 frame_start_falling_edge
= false;
482 case SND_SOC_DAIFMT_DSP_A
:
484 framesync_length
= 1;
485 frame_start_falling_edge
= false;
487 case SND_SOC_DAIFMT_DSP_B
:
489 framesync_length
= 1;
490 frame_start_falling_edge
= false;
496 bcm2835_i2s_calc_channel_pos(&rx_ch1_pos
, &rx_ch2_pos
,
497 rx_mask
, slot_width
, data_delay
, odd_slot_offset
);
498 bcm2835_i2s_calc_channel_pos(&tx_ch1_pos
, &tx_ch2_pos
,
499 tx_mask
, slot_width
, data_delay
, odd_slot_offset
);
502 * Transmitting data immediately after frame start, eg
503 * in left-justified or DSP mode A, only works stable
504 * if bcm2835 is the frame clock master.
506 if ((!rx_ch1_pos
|| !tx_ch1_pos
) && !frame_sync_master
)
508 "Unstable slave config detected, L/R may be swapped");
511 * Set format for both streams.
512 * We cannot set another frame length
513 * (and therefore word length) anyway,
514 * so the format will be the same.
516 regmap_write(dev
->i2s_regmap
, BCM2835_I2S_RXC_A_REG
,
518 | BCM2835_I2S_CH1_POS(rx_ch1_pos
)
519 | BCM2835_I2S_CH2_POS(rx_ch2_pos
));
520 regmap_write(dev
->i2s_regmap
, BCM2835_I2S_TXC_A_REG
,
522 | BCM2835_I2S_CH1_POS(tx_ch1_pos
)
523 | BCM2835_I2S_CH2_POS(tx_ch2_pos
));
525 /* Setup the I2S mode */
527 if (data_length
<= 16) {
529 * Use frame packed mode (2 channels per 32 bit word)
530 * We cannot set another frame length in the second stream
531 * (and therefore word length) anyway,
532 * so the format will be the same.
534 mode
|= BCM2835_I2S_FTXP
| BCM2835_I2S_FRXP
;
537 mode
|= BCM2835_I2S_FLEN(frame_length
- 1);
538 mode
|= BCM2835_I2S_FSLEN(framesync_length
);
540 /* CLKM selects bcm2835 clock slave mode */
541 if (!bit_clock_master
)
542 mode
|= BCM2835_I2S_CLKM
;
544 /* FSM selects bcm2835 frame sync slave mode */
545 if (!frame_sync_master
)
546 mode
|= BCM2835_I2S_FSM
;
548 /* CLKI selects normal clocking mode, sampling on rising edge */
549 switch (dev
->fmt
& SND_SOC_DAIFMT_INV_MASK
) {
550 case SND_SOC_DAIFMT_NB_NF
:
551 case SND_SOC_DAIFMT_NB_IF
:
552 mode
|= BCM2835_I2S_CLKI
;
554 case SND_SOC_DAIFMT_IB_NF
:
555 case SND_SOC_DAIFMT_IB_IF
:
561 /* FSI selects frame start on falling edge */
562 switch (dev
->fmt
& SND_SOC_DAIFMT_INV_MASK
) {
563 case SND_SOC_DAIFMT_NB_NF
:
564 case SND_SOC_DAIFMT_IB_NF
:
565 if (frame_start_falling_edge
)
566 mode
|= BCM2835_I2S_FSI
;
568 case SND_SOC_DAIFMT_NB_IF
:
569 case SND_SOC_DAIFMT_IB_IF
:
570 if (!frame_start_falling_edge
)
571 mode
|= BCM2835_I2S_FSI
;
577 regmap_write(dev
->i2s_regmap
, BCM2835_I2S_MODE_A_REG
, mode
);
579 /* Setup the DMA parameters */
580 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
,
582 | BCM2835_I2S_TXTHR(1)
583 | BCM2835_I2S_DMAEN
, 0xffffffff);
585 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_DREQ_A_REG
,
586 BCM2835_I2S_TX_PANIC(0x10)
587 | BCM2835_I2S_RX_PANIC(0x30)
588 | BCM2835_I2S_TX(0x30)
589 | BCM2835_I2S_RX(0x20), 0xffffffff);
592 bcm2835_i2s_clear_fifos(dev
, true, true);
595 "slots: %d width: %d rx mask: 0x%02x tx_mask: 0x%02x\n",
596 slots
, slot_width
, rx_mask
, tx_mask
);
598 dev_dbg(dev
->dev
, "frame len: %d sync len: %d data len: %d\n",
599 frame_length
, framesync_length
, data_length
);
601 dev_dbg(dev
->dev
, "rx pos: %d,%d tx pos: %d,%d\n",
602 rx_ch1_pos
, rx_ch2_pos
, tx_ch1_pos
, tx_ch2_pos
);
604 dev_dbg(dev
->dev
, "sampling rate: %d bclk rate: %d\n",
605 params_rate(params
), bclk_rate
);
607 dev_dbg(dev
->dev
, "CLKM: %d CLKI: %d FSM: %d FSI: %d frame start: %s edge\n",
608 !!(mode
& BCM2835_I2S_CLKM
),
609 !!(mode
& BCM2835_I2S_CLKI
),
610 !!(mode
& BCM2835_I2S_FSM
),
611 !!(mode
& BCM2835_I2S_FSI
),
612 (mode
& BCM2835_I2S_FSI
) ? "falling" : "rising");
617 static int bcm2835_i2s_prepare(struct snd_pcm_substream
*substream
,
618 struct snd_soc_dai
*dai
)
620 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
624 * Clear both FIFOs if the one that should be started
625 * is not empty at the moment. This should only happen
626 * after overrun. Otherwise, hw_params would have cleared
629 regmap_read(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
, &cs_reg
);
631 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
632 && !(cs_reg
& BCM2835_I2S_TXE
))
633 bcm2835_i2s_clear_fifos(dev
, true, false);
634 else if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
635 && (cs_reg
& BCM2835_I2S_RXD
))
636 bcm2835_i2s_clear_fifos(dev
, false, true);
641 static void bcm2835_i2s_stop(struct bcm2835_i2s_dev
*dev
,
642 struct snd_pcm_substream
*substream
,
643 struct snd_soc_dai
*dai
)
647 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
648 mask
= BCM2835_I2S_RXON
;
650 mask
= BCM2835_I2S_TXON
;
652 regmap_update_bits(dev
->i2s_regmap
,
653 BCM2835_I2S_CS_A_REG
, mask
, 0);
655 /* Stop also the clock when not SND_SOC_DAIFMT_CONT */
656 if (!snd_soc_dai_active(dai
) && !(dev
->fmt
& SND_SOC_DAIFMT_CONT
))
657 bcm2835_i2s_stop_clock(dev
);
660 static int bcm2835_i2s_trigger(struct snd_pcm_substream
*substream
, int cmd
,
661 struct snd_soc_dai
*dai
)
663 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
667 case SNDRV_PCM_TRIGGER_START
:
668 case SNDRV_PCM_TRIGGER_RESUME
:
669 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
670 bcm2835_i2s_start_clock(dev
);
672 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
673 mask
= BCM2835_I2S_RXON
;
675 mask
= BCM2835_I2S_TXON
;
677 regmap_update_bits(dev
->i2s_regmap
,
678 BCM2835_I2S_CS_A_REG
, mask
, mask
);
681 case SNDRV_PCM_TRIGGER_STOP
:
682 case SNDRV_PCM_TRIGGER_SUSPEND
:
683 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
684 bcm2835_i2s_stop(dev
, substream
, dai
);
693 static int bcm2835_i2s_startup(struct snd_pcm_substream
*substream
,
694 struct snd_soc_dai
*dai
)
696 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
698 if (snd_soc_dai_active(dai
))
701 /* Should this still be running stop it */
702 bcm2835_i2s_stop_clock(dev
);
704 /* Enable PCM block */
705 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
,
706 BCM2835_I2S_EN
, BCM2835_I2S_EN
);
710 * Requires at least 4 PCM clock cycles to take effect.
712 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
,
713 BCM2835_I2S_STBY
, BCM2835_I2S_STBY
);
718 static void bcm2835_i2s_shutdown(struct snd_pcm_substream
*substream
,
719 struct snd_soc_dai
*dai
)
721 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
723 bcm2835_i2s_stop(dev
, substream
, dai
);
725 /* If both streams are stopped, disable module and clock */
726 if (snd_soc_dai_active(dai
))
729 /* Disable the module */
730 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
,
734 * Stopping clock is necessary, because stop does
735 * not stop the clock when SND_SOC_DAIFMT_CONT
737 bcm2835_i2s_stop_clock(dev
);
740 static const struct snd_soc_dai_ops bcm2835_i2s_dai_ops
= {
741 .startup
= bcm2835_i2s_startup
,
742 .shutdown
= bcm2835_i2s_shutdown
,
743 .prepare
= bcm2835_i2s_prepare
,
744 .trigger
= bcm2835_i2s_trigger
,
745 .hw_params
= bcm2835_i2s_hw_params
,
746 .set_fmt
= bcm2835_i2s_set_dai_fmt
,
747 .set_bclk_ratio
= bcm2835_i2s_set_dai_bclk_ratio
,
748 .set_tdm_slot
= bcm2835_i2s_set_dai_tdm_slot
,
751 static int bcm2835_i2s_dai_probe(struct snd_soc_dai
*dai
)
753 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
755 snd_soc_dai_init_dma_data(dai
,
756 &dev
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
],
757 &dev
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
]);
762 static struct snd_soc_dai_driver bcm2835_i2s_dai
= {
763 .name
= "bcm2835-i2s",
764 .probe
= bcm2835_i2s_dai_probe
,
768 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
771 .formats
= SNDRV_PCM_FMTBIT_S16_LE
772 | SNDRV_PCM_FMTBIT_S24_LE
773 | SNDRV_PCM_FMTBIT_S32_LE
778 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
781 .formats
= SNDRV_PCM_FMTBIT_S16_LE
782 | SNDRV_PCM_FMTBIT_S24_LE
783 | SNDRV_PCM_FMTBIT_S32_LE
785 .ops
= &bcm2835_i2s_dai_ops
,
786 .symmetric_rates
= 1,
787 .symmetric_samplebits
= 1,
790 static bool bcm2835_i2s_volatile_reg(struct device
*dev
, unsigned int reg
)
793 case BCM2835_I2S_CS_A_REG
:
794 case BCM2835_I2S_FIFO_A_REG
:
795 case BCM2835_I2S_INTSTC_A_REG
:
796 case BCM2835_I2S_GRAY_REG
:
803 static bool bcm2835_i2s_precious_reg(struct device
*dev
, unsigned int reg
)
806 case BCM2835_I2S_FIFO_A_REG
:
813 static const struct regmap_config bcm2835_regmap_config
= {
817 .max_register
= BCM2835_I2S_GRAY_REG
,
818 .precious_reg
= bcm2835_i2s_precious_reg
,
819 .volatile_reg
= bcm2835_i2s_volatile_reg
,
820 .cache_type
= REGCACHE_RBTREE
,
823 static const struct snd_soc_component_driver bcm2835_i2s_component
= {
824 .name
= "bcm2835-i2s-comp",
827 static int bcm2835_i2s_probe(struct platform_device
*pdev
)
829 struct bcm2835_i2s_dev
*dev
;
835 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
),
841 dev
->clk_prepared
= false;
842 dev
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
843 if (IS_ERR(dev
->clk
)) {
844 ret
= PTR_ERR(dev
->clk
);
845 if (ret
== -EPROBE_DEFER
)
846 dev_dbg(&pdev
->dev
, "could not get clk: %d\n", ret
);
848 dev_err(&pdev
->dev
, "could not get clk: %d\n", ret
);
853 base
= devm_platform_ioremap_resource(pdev
, 0);
855 return PTR_ERR(base
);
857 dev
->i2s_regmap
= devm_regmap_init_mmio(&pdev
->dev
, base
,
858 &bcm2835_regmap_config
);
859 if (IS_ERR(dev
->i2s_regmap
))
860 return PTR_ERR(dev
->i2s_regmap
);
862 /* Set the DMA address - we have to parse DT ourselves */
863 addr
= of_get_address(pdev
->dev
.of_node
, 0, NULL
, NULL
);
865 dev_err(&pdev
->dev
, "could not get DMA-register address\n");
868 dma_base
= be32_to_cpup(addr
);
870 dev
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
].addr
=
871 dma_base
+ BCM2835_I2S_FIFO_A_REG
;
873 dev
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
].addr
=
874 dma_base
+ BCM2835_I2S_FIFO_A_REG
;
876 /* Set the bus width */
877 dev
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
].addr_width
=
878 DMA_SLAVE_BUSWIDTH_4_BYTES
;
879 dev
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
].addr_width
=
880 DMA_SLAVE_BUSWIDTH_4_BYTES
;
883 dev
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
].maxburst
= 2;
884 dev
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
].maxburst
= 2;
887 * Set the PACK flag to enable S16_LE support (2 S16_LE values
888 * packed into 32-bit transfers).
890 dev
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
].flags
=
891 SND_DMAENGINE_PCM_DAI_FLAG_PACK
;
892 dev
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
].flags
=
893 SND_DMAENGINE_PCM_DAI_FLAG_PACK
;
896 dev
->dev
= &pdev
->dev
;
897 dev_set_drvdata(&pdev
->dev
, dev
);
899 ret
= devm_snd_soc_register_component(&pdev
->dev
,
900 &bcm2835_i2s_component
, &bcm2835_i2s_dai
, 1);
902 dev_err(&pdev
->dev
, "Could not register DAI: %d\n", ret
);
906 ret
= devm_snd_dmaengine_pcm_register(&pdev
->dev
, NULL
, 0);
908 dev_err(&pdev
->dev
, "Could not register PCM: %d\n", ret
);
915 static const struct of_device_id bcm2835_i2s_of_match
[] = {
916 { .compatible
= "brcm,bcm2835-i2s", },
920 MODULE_DEVICE_TABLE(of
, bcm2835_i2s_of_match
);
922 static struct platform_driver bcm2835_i2s_driver
= {
923 .probe
= bcm2835_i2s_probe
,
925 .name
= "bcm2835-i2s",
926 .of_match_table
= bcm2835_i2s_of_match
,
930 module_platform_driver(bcm2835_i2s_driver
);
932 MODULE_ALIAS("platform:bcm2835-i2s");
933 MODULE_DESCRIPTION("BCM2835 I2S interface");
934 MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
935 MODULE_LICENSE("GPL v2");