2 * ALSA SoC I2S Audio Layer for Broadcom BCM2835 SoC
4 * Author: Florian Meier <florian.meier@koalo.de>
8 * Raspberry Pi PCM I2S ALSA Driver
9 * Copyright (c) by Phil Poole 2013
11 * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
12 * Vladimir Barinov, <vbarinov@embeddedalley.com>
13 * Copyright (C) 2007 MontaVista Software, Inc., <source@mvista.com>
15 * OMAP ALSA SoC DAI driver using McBSP port
16 * Copyright (C) 2008 Nokia Corporation
17 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
18 * Peter Ujfalusi <peter.ujfalusi@ti.com>
20 * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
21 * Author: Timur Tabi <timur@freescale.com>
22 * Copyright 2007-2010 Freescale Semiconductor, Inc.
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * version 2 as published by the Free Software Foundation.
28 * This program is distributed in the hope that it will be useful, but
29 * WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * General Public License for more details.
34 #include <linux/bitops.h>
35 #include <linux/clk.h>
36 #include <linux/delay.h>
37 #include <linux/device.h>
38 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/of_address.h>
42 #include <linux/slab.h>
44 #include <sound/core.h>
45 #include <sound/dmaengine_pcm.h>
46 #include <sound/initval.h>
47 #include <sound/pcm.h>
48 #include <sound/pcm_params.h>
49 #include <sound/soc.h>
52 #define BCM2835_I2S_CS_A_REG 0x00
53 #define BCM2835_I2S_FIFO_A_REG 0x04
54 #define BCM2835_I2S_MODE_A_REG 0x08
55 #define BCM2835_I2S_RXC_A_REG 0x0c
56 #define BCM2835_I2S_TXC_A_REG 0x10
57 #define BCM2835_I2S_DREQ_A_REG 0x14
58 #define BCM2835_I2S_INTEN_A_REG 0x18
59 #define BCM2835_I2S_INTSTC_A_REG 0x1c
60 #define BCM2835_I2S_GRAY_REG 0x20
62 /* I2S register settings */
63 #define BCM2835_I2S_STBY BIT(25)
64 #define BCM2835_I2S_SYNC BIT(24)
65 #define BCM2835_I2S_RXSEX BIT(23)
66 #define BCM2835_I2S_RXF BIT(22)
67 #define BCM2835_I2S_TXE BIT(21)
68 #define BCM2835_I2S_RXD BIT(20)
69 #define BCM2835_I2S_TXD BIT(19)
70 #define BCM2835_I2S_RXR BIT(18)
71 #define BCM2835_I2S_TXW BIT(17)
72 #define BCM2835_I2S_CS_RXERR BIT(16)
73 #define BCM2835_I2S_CS_TXERR BIT(15)
74 #define BCM2835_I2S_RXSYNC BIT(14)
75 #define BCM2835_I2S_TXSYNC BIT(13)
76 #define BCM2835_I2S_DMAEN BIT(9)
77 #define BCM2835_I2S_RXTHR(v) ((v) << 7)
78 #define BCM2835_I2S_TXTHR(v) ((v) << 5)
79 #define BCM2835_I2S_RXCLR BIT(4)
80 #define BCM2835_I2S_TXCLR BIT(3)
81 #define BCM2835_I2S_TXON BIT(2)
82 #define BCM2835_I2S_RXON BIT(1)
83 #define BCM2835_I2S_EN (1)
85 #define BCM2835_I2S_CLKDIS BIT(28)
86 #define BCM2835_I2S_PDMN BIT(27)
87 #define BCM2835_I2S_PDME BIT(26)
88 #define BCM2835_I2S_FRXP BIT(25)
89 #define BCM2835_I2S_FTXP BIT(24)
90 #define BCM2835_I2S_CLKM BIT(23)
91 #define BCM2835_I2S_CLKI BIT(22)
92 #define BCM2835_I2S_FSM BIT(21)
93 #define BCM2835_I2S_FSI BIT(20)
94 #define BCM2835_I2S_FLEN(v) ((v) << 10)
95 #define BCM2835_I2S_FSLEN(v) (v)
97 #define BCM2835_I2S_CHWEX BIT(15)
98 #define BCM2835_I2S_CHEN BIT(14)
99 #define BCM2835_I2S_CHPOS(v) ((v) << 4)
100 #define BCM2835_I2S_CHWID(v) (v)
101 #define BCM2835_I2S_CH1(v) ((v) << 16)
102 #define BCM2835_I2S_CH2(v) (v)
103 #define BCM2835_I2S_CH1_POS(v) BCM2835_I2S_CH1(BCM2835_I2S_CHPOS(v))
104 #define BCM2835_I2S_CH2_POS(v) BCM2835_I2S_CH2(BCM2835_I2S_CHPOS(v))
106 #define BCM2835_I2S_TX_PANIC(v) ((v) << 24)
107 #define BCM2835_I2S_RX_PANIC(v) ((v) << 16)
108 #define BCM2835_I2S_TX(v) ((v) << 8)
109 #define BCM2835_I2S_RX(v) (v)
111 #define BCM2835_I2S_INT_RXERR BIT(3)
112 #define BCM2835_I2S_INT_TXERR BIT(2)
113 #define BCM2835_I2S_INT_RXR BIT(1)
114 #define BCM2835_I2S_INT_TXW BIT(0)
116 /* Frame length register is 10 bit, maximum length 1024 */
117 #define BCM2835_I2S_MAX_FRAME_LENGTH 1024
119 /* General device struct */
120 struct bcm2835_i2s_dev
{
122 struct snd_dmaengine_dai_dma_data dma_data
[2];
124 unsigned int tdm_slots
;
125 unsigned int rx_mask
;
126 unsigned int tx_mask
;
127 unsigned int slot_width
;
128 unsigned int frame_length
;
130 struct regmap
*i2s_regmap
;
136 static void bcm2835_i2s_start_clock(struct bcm2835_i2s_dev
*dev
)
138 unsigned int master
= dev
->fmt
& SND_SOC_DAIFMT_MASTER_MASK
;
140 if (dev
->clk_prepared
)
144 case SND_SOC_DAIFMT_CBS_CFS
:
145 case SND_SOC_DAIFMT_CBS_CFM
:
146 clk_prepare_enable(dev
->clk
);
147 dev
->clk_prepared
= true;
154 static void bcm2835_i2s_stop_clock(struct bcm2835_i2s_dev
*dev
)
156 if (dev
->clk_prepared
)
157 clk_disable_unprepare(dev
->clk
);
158 dev
->clk_prepared
= false;
161 static void bcm2835_i2s_clear_fifos(struct bcm2835_i2s_dev
*dev
,
167 uint32_t i2s_active_state
;
168 bool clk_was_prepared
;
172 off
= tx
? BCM2835_I2S_TXON
: 0;
173 off
|= rx
? BCM2835_I2S_RXON
: 0;
175 clr
= tx
? BCM2835_I2S_TXCLR
: 0;
176 clr
|= rx
? BCM2835_I2S_RXCLR
: 0;
178 /* Backup the current state */
179 regmap_read(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
, &csreg
);
180 i2s_active_state
= csreg
& (BCM2835_I2S_RXON
| BCM2835_I2S_TXON
);
182 /* Start clock if not running */
183 clk_was_prepared
= dev
->clk_prepared
;
184 if (!clk_was_prepared
)
185 bcm2835_i2s_start_clock(dev
);
187 /* Stop I2S module */
188 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
, off
, 0);
192 * Requires at least 2 PCM clock cycles to take effect
194 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
, clr
, clr
);
196 /* Wait for 2 PCM clock cycles */
199 * Toggle the SYNC flag. After 2 PCM clock cycles it can be read back
200 * FIXME: This does not seem to work for slave mode!
202 regmap_read(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
, &syncval
);
203 syncval
&= BCM2835_I2S_SYNC
;
205 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
,
206 BCM2835_I2S_SYNC
, ~syncval
);
208 /* Wait for the SYNC flag changing it's state */
210 regmap_read(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
, &csreg
);
211 if ((csreg
& BCM2835_I2S_SYNC
) != syncval
)
216 dev_err(dev
->dev
, "I2S SYNC error!\n");
218 /* Stop clock if it was not running before */
219 if (!clk_was_prepared
)
220 bcm2835_i2s_stop_clock(dev
);
222 /* Restore I2S state */
223 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
,
224 BCM2835_I2S_RXON
| BCM2835_I2S_TXON
, i2s_active_state
);
227 static int bcm2835_i2s_set_dai_fmt(struct snd_soc_dai
*dai
,
230 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
235 static int bcm2835_i2s_set_dai_bclk_ratio(struct snd_soc_dai
*dai
,
238 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
245 if (ratio
> BCM2835_I2S_MAX_FRAME_LENGTH
)
251 dev
->slot_width
= ratio
/ 2;
252 dev
->frame_length
= ratio
;
257 static int bcm2835_i2s_set_dai_tdm_slot(struct snd_soc_dai
*dai
,
258 unsigned int tx_mask
, unsigned int rx_mask
,
259 int slots
, int width
)
261 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
264 if (slots
< 0 || width
< 0)
267 /* Limit masks to available slots */
268 rx_mask
&= GENMASK(slots
- 1, 0);
269 tx_mask
&= GENMASK(slots
- 1, 0);
272 * The driver is limited to 2-channel setups.
273 * Check that exactly 2 bits are set in the masks.
275 if (hweight_long((unsigned long) rx_mask
) != 2
276 || hweight_long((unsigned long) tx_mask
) != 2)
279 if (slots
* width
> BCM2835_I2S_MAX_FRAME_LENGTH
)
283 dev
->tdm_slots
= slots
;
285 dev
->rx_mask
= rx_mask
;
286 dev
->tx_mask
= tx_mask
;
287 dev
->slot_width
= width
;
288 dev
->frame_length
= slots
* width
;
294 * Convert logical slot number into physical slot number.
296 * If odd_offset is 0 sequential number is identical to logical number.
297 * This is used for DSP modes with slot numbering 0 1 2 3 ...
299 * Otherwise odd_offset defines the physical offset for odd numbered
300 * slots. This is used for I2S and left/right justified modes to
301 * translate from logical slot numbers 0 1 2 3 ... into physical slot
302 * numbers 0 2 ... 3 4 ...
304 static int bcm2835_i2s_convert_slot(unsigned int slot
, unsigned int odd_offset
)
310 return (slot
>> 1) + odd_offset
;
316 * Calculate channel position from mask and slot width.
318 * Mask must contain exactly 2 set bits.
319 * Lowest set bit is channel 1 position, highest set bit channel 2.
320 * The constant offset is added to both channel positions.
322 * If odd_offset is > 0 slot positions are translated to
323 * I2S-style TDM slot numbering ( 0 2 ... 3 4 ...) with odd
324 * logical slot numbers starting at physical slot odd_offset.
326 static void bcm2835_i2s_calc_channel_pos(
327 unsigned int *ch1_pos
, unsigned int *ch2_pos
,
328 unsigned int mask
, unsigned int width
,
329 unsigned int bit_offset
, unsigned int odd_offset
)
331 *ch1_pos
= bcm2835_i2s_convert_slot((ffs(mask
) - 1), odd_offset
)
332 * width
+ bit_offset
;
333 *ch2_pos
= bcm2835_i2s_convert_slot((fls(mask
) - 1), odd_offset
)
334 * width
+ bit_offset
;
337 static int bcm2835_i2s_hw_params(struct snd_pcm_substream
*substream
,
338 struct snd_pcm_hw_params
*params
,
339 struct snd_soc_dai
*dai
)
341 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
342 unsigned int data_length
, data_delay
, framesync_length
;
343 unsigned int slots
, slot_width
, odd_slot_offset
;
344 int frame_length
, bclk_rate
;
345 unsigned int rx_mask
, tx_mask
;
346 unsigned int rx_ch1_pos
, rx_ch2_pos
, tx_ch1_pos
, tx_ch2_pos
;
347 unsigned int mode
, format
;
348 bool bit_clock_master
= false;
349 bool frame_sync_master
= false;
350 bool frame_start_falling_edge
= false;
355 * If a stream is already enabled,
356 * the registers are already set properly.
358 regmap_read(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
, &csreg
);
360 if (csreg
& (BCM2835_I2S_TXON
| BCM2835_I2S_RXON
))
363 data_length
= params_width(params
);
368 if (dev
->tdm_slots
) {
369 slots
= dev
->tdm_slots
;
370 slot_width
= dev
->slot_width
;
371 frame_length
= dev
->frame_length
;
372 rx_mask
= dev
->rx_mask
;
373 tx_mask
= dev
->tx_mask
;
374 bclk_rate
= dev
->frame_length
* params_rate(params
);
377 slot_width
= params_width(params
);
381 frame_length
= snd_soc_params_to_frame_size(params
);
382 if (frame_length
< 0)
385 bclk_rate
= snd_soc_params_to_bclk(params
);
390 /* Check if data fits into slots */
391 if (data_length
> slot_width
)
394 /* Check if CPU is bit clock master */
395 switch (dev
->fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
396 case SND_SOC_DAIFMT_CBS_CFS
:
397 case SND_SOC_DAIFMT_CBS_CFM
:
398 bit_clock_master
= true;
400 case SND_SOC_DAIFMT_CBM_CFS
:
401 case SND_SOC_DAIFMT_CBM_CFM
:
402 bit_clock_master
= false;
408 /* Check if CPU is frame sync master */
409 switch (dev
->fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
410 case SND_SOC_DAIFMT_CBS_CFS
:
411 case SND_SOC_DAIFMT_CBM_CFS
:
412 frame_sync_master
= true;
414 case SND_SOC_DAIFMT_CBS_CFM
:
415 case SND_SOC_DAIFMT_CBM_CFM
:
416 frame_sync_master
= false;
422 /* Clock should only be set up here if CPU is clock master */
423 if (bit_clock_master
&&
424 (!dev
->clk_prepared
|| dev
->clk_rate
!= bclk_rate
)) {
425 if (dev
->clk_prepared
)
426 bcm2835_i2s_stop_clock(dev
);
428 if (dev
->clk_rate
!= bclk_rate
) {
429 ret
= clk_set_rate(dev
->clk
, bclk_rate
);
432 dev
->clk_rate
= bclk_rate
;
435 bcm2835_i2s_start_clock(dev
);
438 /* Setup the frame format */
439 format
= BCM2835_I2S_CHEN
;
441 if (data_length
>= 24)
442 format
|= BCM2835_I2S_CHWEX
;
444 format
|= BCM2835_I2S_CHWID((data_length
-8)&0xf);
446 /* CH2 format is the same as for CH1 */
447 format
= BCM2835_I2S_CH1(format
) | BCM2835_I2S_CH2(format
);
449 switch (dev
->fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
450 case SND_SOC_DAIFMT_I2S
:
451 /* I2S mode needs an even number of slots */
456 * Use I2S-style logical slot numbering: even slots
457 * are in first half of frame, odd slots in second half.
459 odd_slot_offset
= slots
>> 1;
461 /* MSB starts one cycle after frame start */
464 /* Setup frame sync signal for 50% duty cycle */
465 framesync_length
= frame_length
/ 2;
466 frame_start_falling_edge
= true;
468 case SND_SOC_DAIFMT_LEFT_J
:
472 odd_slot_offset
= slots
>> 1;
474 framesync_length
= frame_length
/ 2;
475 frame_start_falling_edge
= false;
477 case SND_SOC_DAIFMT_RIGHT_J
:
481 /* Odd frame lengths aren't supported */
482 if (frame_length
& 1)
485 odd_slot_offset
= slots
>> 1;
486 data_delay
= slot_width
- data_length
;
487 framesync_length
= frame_length
/ 2;
488 frame_start_falling_edge
= false;
490 case SND_SOC_DAIFMT_DSP_A
:
492 framesync_length
= 1;
493 frame_start_falling_edge
= false;
495 case SND_SOC_DAIFMT_DSP_B
:
497 framesync_length
= 1;
498 frame_start_falling_edge
= false;
504 bcm2835_i2s_calc_channel_pos(&rx_ch1_pos
, &rx_ch2_pos
,
505 rx_mask
, slot_width
, data_delay
, odd_slot_offset
);
506 bcm2835_i2s_calc_channel_pos(&tx_ch1_pos
, &tx_ch2_pos
,
507 tx_mask
, slot_width
, data_delay
, odd_slot_offset
);
510 * Transmitting data immediately after frame start, eg
511 * in left-justified or DSP mode A, only works stable
512 * if bcm2835 is the frame clock master.
514 if ((!rx_ch1_pos
|| !tx_ch1_pos
) && !frame_sync_master
)
516 "Unstable slave config detected, L/R may be swapped");
519 * Set format for both streams.
520 * We cannot set another frame length
521 * (and therefore word length) anyway,
522 * so the format will be the same.
524 regmap_write(dev
->i2s_regmap
, BCM2835_I2S_RXC_A_REG
,
526 | BCM2835_I2S_CH1_POS(rx_ch1_pos
)
527 | BCM2835_I2S_CH2_POS(rx_ch2_pos
));
528 regmap_write(dev
->i2s_regmap
, BCM2835_I2S_TXC_A_REG
,
530 | BCM2835_I2S_CH1_POS(tx_ch1_pos
)
531 | BCM2835_I2S_CH2_POS(tx_ch2_pos
));
533 /* Setup the I2S mode */
535 if (data_length
<= 16) {
537 * Use frame packed mode (2 channels per 32 bit word)
538 * We cannot set another frame length in the second stream
539 * (and therefore word length) anyway,
540 * so the format will be the same.
542 mode
|= BCM2835_I2S_FTXP
| BCM2835_I2S_FRXP
;
545 mode
|= BCM2835_I2S_FLEN(frame_length
- 1);
546 mode
|= BCM2835_I2S_FSLEN(framesync_length
);
548 /* CLKM selects bcm2835 clock slave mode */
549 if (!bit_clock_master
)
550 mode
|= BCM2835_I2S_CLKM
;
552 /* FSM selects bcm2835 frame sync slave mode */
553 if (!frame_sync_master
)
554 mode
|= BCM2835_I2S_FSM
;
556 /* CLKI selects normal clocking mode, sampling on rising edge */
557 switch (dev
->fmt
& SND_SOC_DAIFMT_INV_MASK
) {
558 case SND_SOC_DAIFMT_NB_NF
:
559 case SND_SOC_DAIFMT_NB_IF
:
560 mode
|= BCM2835_I2S_CLKI
;
562 case SND_SOC_DAIFMT_IB_NF
:
563 case SND_SOC_DAIFMT_IB_IF
:
569 /* FSI selects frame start on falling edge */
570 switch (dev
->fmt
& SND_SOC_DAIFMT_INV_MASK
) {
571 case SND_SOC_DAIFMT_NB_NF
:
572 case SND_SOC_DAIFMT_IB_NF
:
573 if (frame_start_falling_edge
)
574 mode
|= BCM2835_I2S_FSI
;
576 case SND_SOC_DAIFMT_NB_IF
:
577 case SND_SOC_DAIFMT_IB_IF
:
578 if (!frame_start_falling_edge
)
579 mode
|= BCM2835_I2S_FSI
;
585 regmap_write(dev
->i2s_regmap
, BCM2835_I2S_MODE_A_REG
, mode
);
587 /* Setup the DMA parameters */
588 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
,
590 | BCM2835_I2S_TXTHR(1)
591 | BCM2835_I2S_DMAEN
, 0xffffffff);
593 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_DREQ_A_REG
,
594 BCM2835_I2S_TX_PANIC(0x10)
595 | BCM2835_I2S_RX_PANIC(0x30)
596 | BCM2835_I2S_TX(0x30)
597 | BCM2835_I2S_RX(0x20), 0xffffffff);
600 bcm2835_i2s_clear_fifos(dev
, true, true);
603 "slots: %d width: %d rx mask: 0x%02x tx_mask: 0x%02x\n",
604 slots
, slot_width
, rx_mask
, tx_mask
);
606 dev_dbg(dev
->dev
, "frame len: %d sync len: %d data len: %d\n",
607 frame_length
, framesync_length
, data_length
);
609 dev_dbg(dev
->dev
, "rx pos: %d,%d tx pos: %d,%d\n",
610 rx_ch1_pos
, rx_ch2_pos
, tx_ch1_pos
, tx_ch2_pos
);
612 dev_dbg(dev
->dev
, "sampling rate: %d bclk rate: %d\n",
613 params_rate(params
), bclk_rate
);
615 dev_dbg(dev
->dev
, "CLKM: %d CLKI: %d FSM: %d FSI: %d frame start: %s edge\n",
616 !!(mode
& BCM2835_I2S_CLKM
),
617 !!(mode
& BCM2835_I2S_CLKI
),
618 !!(mode
& BCM2835_I2S_FSM
),
619 !!(mode
& BCM2835_I2S_FSI
),
620 (mode
& BCM2835_I2S_FSI
) ? "falling" : "rising");
625 static int bcm2835_i2s_prepare(struct snd_pcm_substream
*substream
,
626 struct snd_soc_dai
*dai
)
628 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
632 * Clear both FIFOs if the one that should be started
633 * is not empty at the moment. This should only happen
634 * after overrun. Otherwise, hw_params would have cleared
637 regmap_read(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
, &cs_reg
);
639 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
640 && !(cs_reg
& BCM2835_I2S_TXE
))
641 bcm2835_i2s_clear_fifos(dev
, true, false);
642 else if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
643 && (cs_reg
& BCM2835_I2S_RXD
))
644 bcm2835_i2s_clear_fifos(dev
, false, true);
649 static void bcm2835_i2s_stop(struct bcm2835_i2s_dev
*dev
,
650 struct snd_pcm_substream
*substream
,
651 struct snd_soc_dai
*dai
)
655 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
656 mask
= BCM2835_I2S_RXON
;
658 mask
= BCM2835_I2S_TXON
;
660 regmap_update_bits(dev
->i2s_regmap
,
661 BCM2835_I2S_CS_A_REG
, mask
, 0);
663 /* Stop also the clock when not SND_SOC_DAIFMT_CONT */
664 if (!dai
->active
&& !(dev
->fmt
& SND_SOC_DAIFMT_CONT
))
665 bcm2835_i2s_stop_clock(dev
);
668 static int bcm2835_i2s_trigger(struct snd_pcm_substream
*substream
, int cmd
,
669 struct snd_soc_dai
*dai
)
671 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
675 case SNDRV_PCM_TRIGGER_START
:
676 case SNDRV_PCM_TRIGGER_RESUME
:
677 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
678 bcm2835_i2s_start_clock(dev
);
680 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
681 mask
= BCM2835_I2S_RXON
;
683 mask
= BCM2835_I2S_TXON
;
685 regmap_update_bits(dev
->i2s_regmap
,
686 BCM2835_I2S_CS_A_REG
, mask
, mask
);
689 case SNDRV_PCM_TRIGGER_STOP
:
690 case SNDRV_PCM_TRIGGER_SUSPEND
:
691 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
692 bcm2835_i2s_stop(dev
, substream
, dai
);
701 static int bcm2835_i2s_startup(struct snd_pcm_substream
*substream
,
702 struct snd_soc_dai
*dai
)
704 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
709 /* Should this still be running stop it */
710 bcm2835_i2s_stop_clock(dev
);
712 /* Enable PCM block */
713 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
,
714 BCM2835_I2S_EN
, BCM2835_I2S_EN
);
718 * Requires at least 4 PCM clock cycles to take effect.
720 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
,
721 BCM2835_I2S_STBY
, BCM2835_I2S_STBY
);
726 static void bcm2835_i2s_shutdown(struct snd_pcm_substream
*substream
,
727 struct snd_soc_dai
*dai
)
729 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
731 bcm2835_i2s_stop(dev
, substream
, dai
);
733 /* If both streams are stopped, disable module and clock */
737 /* Disable the module */
738 regmap_update_bits(dev
->i2s_regmap
, BCM2835_I2S_CS_A_REG
,
742 * Stopping clock is necessary, because stop does
743 * not stop the clock when SND_SOC_DAIFMT_CONT
745 bcm2835_i2s_stop_clock(dev
);
748 static const struct snd_soc_dai_ops bcm2835_i2s_dai_ops
= {
749 .startup
= bcm2835_i2s_startup
,
750 .shutdown
= bcm2835_i2s_shutdown
,
751 .prepare
= bcm2835_i2s_prepare
,
752 .trigger
= bcm2835_i2s_trigger
,
753 .hw_params
= bcm2835_i2s_hw_params
,
754 .set_fmt
= bcm2835_i2s_set_dai_fmt
,
755 .set_bclk_ratio
= bcm2835_i2s_set_dai_bclk_ratio
,
756 .set_tdm_slot
= bcm2835_i2s_set_dai_tdm_slot
,
759 static int bcm2835_i2s_dai_probe(struct snd_soc_dai
*dai
)
761 struct bcm2835_i2s_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
763 snd_soc_dai_init_dma_data(dai
,
764 &dev
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
],
765 &dev
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
]);
770 static struct snd_soc_dai_driver bcm2835_i2s_dai
= {
771 .name
= "bcm2835-i2s",
772 .probe
= bcm2835_i2s_dai_probe
,
776 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
779 .formats
= SNDRV_PCM_FMTBIT_S16_LE
780 | SNDRV_PCM_FMTBIT_S24_LE
781 | SNDRV_PCM_FMTBIT_S32_LE
786 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
789 .formats
= SNDRV_PCM_FMTBIT_S16_LE
790 | SNDRV_PCM_FMTBIT_S24_LE
791 | SNDRV_PCM_FMTBIT_S32_LE
793 .ops
= &bcm2835_i2s_dai_ops
,
794 .symmetric_rates
= 1,
795 .symmetric_samplebits
= 1,
798 static bool bcm2835_i2s_volatile_reg(struct device
*dev
, unsigned int reg
)
801 case BCM2835_I2S_CS_A_REG
:
802 case BCM2835_I2S_FIFO_A_REG
:
803 case BCM2835_I2S_INTSTC_A_REG
:
804 case BCM2835_I2S_GRAY_REG
:
811 static bool bcm2835_i2s_precious_reg(struct device
*dev
, unsigned int reg
)
814 case BCM2835_I2S_FIFO_A_REG
:
821 static const struct regmap_config bcm2835_regmap_config
= {
825 .max_register
= BCM2835_I2S_GRAY_REG
,
826 .precious_reg
= bcm2835_i2s_precious_reg
,
827 .volatile_reg
= bcm2835_i2s_volatile_reg
,
828 .cache_type
= REGCACHE_RBTREE
,
831 static const struct snd_soc_component_driver bcm2835_i2s_component
= {
832 .name
= "bcm2835-i2s-comp",
835 static int bcm2835_i2s_probe(struct platform_device
*pdev
)
837 struct bcm2835_i2s_dev
*dev
;
839 struct resource
*mem
;
844 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
),
850 dev
->clk_prepared
= false;
851 dev
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
852 if (IS_ERR(dev
->clk
)) {
853 dev_err(&pdev
->dev
, "could not get clk: %ld\n",
855 return PTR_ERR(dev
->clk
);
859 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
860 base
= devm_ioremap_resource(&pdev
->dev
, mem
);
862 return PTR_ERR(base
);
864 dev
->i2s_regmap
= devm_regmap_init_mmio(&pdev
->dev
, base
,
865 &bcm2835_regmap_config
);
866 if (IS_ERR(dev
->i2s_regmap
))
867 return PTR_ERR(dev
->i2s_regmap
);
869 /* Set the DMA address - we have to parse DT ourselves */
870 addr
= of_get_address(pdev
->dev
.of_node
, 0, NULL
, NULL
);
872 dev_err(&pdev
->dev
, "could not get DMA-register address\n");
875 dma_base
= be32_to_cpup(addr
);
877 dev
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
].addr
=
878 dma_base
+ BCM2835_I2S_FIFO_A_REG
;
880 dev
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
].addr
=
881 dma_base
+ BCM2835_I2S_FIFO_A_REG
;
883 /* Set the bus width */
884 dev
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
].addr_width
=
885 DMA_SLAVE_BUSWIDTH_4_BYTES
;
886 dev
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
].addr_width
=
887 DMA_SLAVE_BUSWIDTH_4_BYTES
;
890 dev
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
].maxburst
= 2;
891 dev
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
].maxburst
= 2;
894 * Set the PACK flag to enable S16_LE support (2 S16_LE values
895 * packed into 32-bit transfers).
897 dev
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
].flags
=
898 SND_DMAENGINE_PCM_DAI_FLAG_PACK
;
899 dev
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
].flags
=
900 SND_DMAENGINE_PCM_DAI_FLAG_PACK
;
903 dev
->dev
= &pdev
->dev
;
904 dev_set_drvdata(&pdev
->dev
, dev
);
906 ret
= devm_snd_soc_register_component(&pdev
->dev
,
907 &bcm2835_i2s_component
, &bcm2835_i2s_dai
, 1);
909 dev_err(&pdev
->dev
, "Could not register DAI: %d\n", ret
);
913 ret
= devm_snd_dmaengine_pcm_register(&pdev
->dev
, NULL
, 0);
915 dev_err(&pdev
->dev
, "Could not register PCM: %d\n", ret
);
922 static const struct of_device_id bcm2835_i2s_of_match
[] = {
923 { .compatible
= "brcm,bcm2835-i2s", },
927 MODULE_DEVICE_TABLE(of
, bcm2835_i2s_of_match
);
929 static struct platform_driver bcm2835_i2s_driver
= {
930 .probe
= bcm2835_i2s_probe
,
932 .name
= "bcm2835-i2s",
933 .of_match_table
= bcm2835_i2s_of_match
,
937 module_platform_driver(bcm2835_i2s_driver
);
939 MODULE_ALIAS("platform:bcm2835-i2s");
940 MODULE_DESCRIPTION("BCM2835 I2S interface");
941 MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
942 MODULE_LICENSE("GPL v2");