1 // SPDX-License-Identifier: GPL-2.0
3 // Driver for Microchip I2S Multi-channel controller
5 // Copyright (C) 2018 Microchip Technology Inc. and its subsidiaries
7 // Author: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/slab.h>
14 #include <linux/delay.h>
16 #include <linux/clk.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/lcm.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/initval.h>
24 #include <sound/soc.h>
25 #include <sound/dmaengine_pcm.h>
28 * ---- I2S Controller Register map ----
30 #define MCHP_I2SMCC_CR 0x0000 /* Control Register */
31 #define MCHP_I2SMCC_MRA 0x0004 /* Mode Register A */
32 #define MCHP_I2SMCC_MRB 0x0008 /* Mode Register B */
33 #define MCHP_I2SMCC_SR 0x000C /* Status Register */
34 #define MCHP_I2SMCC_IERA 0x0010 /* Interrupt Enable Register A */
35 #define MCHP_I2SMCC_IDRA 0x0014 /* Interrupt Disable Register A */
36 #define MCHP_I2SMCC_IMRA 0x0018 /* Interrupt Mask Register A */
37 #define MCHP_I2SMCC_ISRA 0X001C /* Interrupt Status Register A */
39 #define MCHP_I2SMCC_IERB 0x0020 /* Interrupt Enable Register B */
40 #define MCHP_I2SMCC_IDRB 0x0024 /* Interrupt Disable Register B */
41 #define MCHP_I2SMCC_IMRB 0x0028 /* Interrupt Mask Register B */
42 #define MCHP_I2SMCC_ISRB 0X002C /* Interrupt Status Register B */
44 #define MCHP_I2SMCC_RHR 0x0030 /* Receiver Holding Register */
45 #define MCHP_I2SMCC_THR 0x0034 /* Transmitter Holding Register */
47 #define MCHP_I2SMCC_RHL0R 0x0040 /* Receiver Holding Left 0 Register */
48 #define MCHP_I2SMCC_RHR0R 0x0044 /* Receiver Holding Right 0 Register */
50 #define MCHP_I2SMCC_RHL1R 0x0048 /* Receiver Holding Left 1 Register */
51 #define MCHP_I2SMCC_RHR1R 0x004C /* Receiver Holding Right 1 Register */
53 #define MCHP_I2SMCC_RHL2R 0x0050 /* Receiver Holding Left 2 Register */
54 #define MCHP_I2SMCC_RHR2R 0x0054 /* Receiver Holding Right 2 Register */
56 #define MCHP_I2SMCC_RHL3R 0x0058 /* Receiver Holding Left 3 Register */
57 #define MCHP_I2SMCC_RHR3R 0x005C /* Receiver Holding Right 3 Register */
59 #define MCHP_I2SMCC_THL0R 0x0060 /* Transmitter Holding Left 0 Register */
60 #define MCHP_I2SMCC_THR0R 0x0064 /* Transmitter Holding Right 0 Register */
62 #define MCHP_I2SMCC_THL1R 0x0068 /* Transmitter Holding Left 1 Register */
63 #define MCHP_I2SMCC_THR1R 0x006C /* Transmitter Holding Right 1 Register */
65 #define MCHP_I2SMCC_THL2R 0x0070 /* Transmitter Holding Left 2 Register */
66 #define MCHP_I2SMCC_THR2R 0x0074 /* Transmitter Holding Right 2 Register */
68 #define MCHP_I2SMCC_THL3R 0x0078 /* Transmitter Holding Left 3 Register */
69 #define MCHP_I2SMCC_THR3R 0x007C /* Transmitter Holding Right 3 Register */
71 #define MCHP_I2SMCC_VERSION 0x00FC /* Version Register */
74 * ---- Control Register (Write-only) ----
76 #define MCHP_I2SMCC_CR_RXEN BIT(0) /* Receiver Enable */
77 #define MCHP_I2SMCC_CR_RXDIS BIT(1) /* Receiver Disable */
78 #define MCHP_I2SMCC_CR_CKEN BIT(2) /* Clock Enable */
79 #define MCHP_I2SMCC_CR_CKDIS BIT(3) /* Clock Disable */
80 #define MCHP_I2SMCC_CR_TXEN BIT(4) /* Transmitter Enable */
81 #define MCHP_I2SMCC_CR_TXDIS BIT(5) /* Transmitter Disable */
82 #define MCHP_I2SMCC_CR_SWRST BIT(7) /* Software Reset */
85 * ---- Mode Register A (Read/Write) ----
87 #define MCHP_I2SMCC_MRA_MODE_MASK GENMASK(0, 0)
88 #define MCHP_I2SMCC_MRA_MODE_SLAVE (0 << 0)
89 #define MCHP_I2SMCC_MRA_MODE_MASTER (1 << 0)
91 #define MCHP_I2SMCC_MRA_DATALENGTH_MASK GENMASK(3, 1)
92 #define MCHP_I2SMCC_MRA_DATALENGTH_32_BITS (0 << 1)
93 #define MCHP_I2SMCC_MRA_DATALENGTH_24_BITS (1 << 1)
94 #define MCHP_I2SMCC_MRA_DATALENGTH_20_BITS (2 << 1)
95 #define MCHP_I2SMCC_MRA_DATALENGTH_18_BITS (3 << 1)
96 #define MCHP_I2SMCC_MRA_DATALENGTH_16_BITS (4 << 1)
97 #define MCHP_I2SMCC_MRA_DATALENGTH_16_BITS_COMPACT (5 << 1)
98 #define MCHP_I2SMCC_MRA_DATALENGTH_8_BITS (6 << 1)
99 #define MCHP_I2SMCC_MRA_DATALENGTH_8_BITS_COMPACT (7 << 1)
101 #define MCHP_I2SMCC_MRA_WIRECFG_MASK GENMASK(5, 4)
102 #define MCHP_I2SMCC_MRA_WIRECFG_I2S_1_TDM_0 (0 << 4)
103 #define MCHP_I2SMCC_MRA_WIRECFG_I2S_2_TDM_1 (1 << 4)
104 #define MCHP_I2SMCC_MRA_WIRECFG_I2S_4_TDM_2 (2 << 4)
105 #define MCHP_I2SMCC_MRA_WIRECFG_TDM_3 (3 << 4)
107 #define MCHP_I2SMCC_MRA_FORMAT_MASK GENMASK(7, 6)
108 #define MCHP_I2SMCC_MRA_FORMAT_I2S (0 << 6)
109 #define MCHP_I2SMCC_MRA_FORMAT_LJ (1 << 6) /* Left Justified */
110 #define MCHP_I2SMCC_MRA_FORMAT_TDM (2 << 6)
111 #define MCHP_I2SMCC_MRA_FORMAT_TDMLJ (3 << 6)
113 /* Transmitter uses one DMA channel ... */
114 /* Left audio samples duplicated to right audio channel */
115 #define MCHP_I2SMCC_MRA_RXMONO BIT(8)
117 /* I2SDO output of I2SC is internally connected to I2SDI input */
118 #define MCHP_I2SMCC_MRA_RXLOOP BIT(9)
120 /* Receiver uses one DMA channel ... */
121 /* Left audio samples duplicated to right audio channel */
122 #define MCHP_I2SMCC_MRA_TXMONO BIT(10)
124 /* x sample transmitted when underrun */
125 #define MCHP_I2SMCC_MRA_TXSAME_ZERO (0 << 11) /* Zero sample */
126 #define MCHP_I2SMCC_MRA_TXSAME_PREVIOUS (1 << 11) /* Previous sample */
128 /* select between peripheral clock and generated clock */
129 #define MCHP_I2SMCC_MRA_SRCCLK_PCLK (0 << 12)
130 #define MCHP_I2SMCC_MRA_SRCCLK_GCLK (1 << 12)
132 /* Number of TDM Channels - 1 */
133 #define MCHP_I2SMCC_MRA_NBCHAN_MASK GENMASK(15, 13)
134 #define MCHP_I2SMCC_MRA_NBCHAN(ch) \
135 ((((ch) - 1) << 13) & MCHP_I2SMCC_MRA_NBCHAN_MASK)
137 /* Selected Clock to I2SMCC Master Clock ratio */
138 #define MCHP_I2SMCC_MRA_IMCKDIV_MASK GENMASK(21, 16)
139 #define MCHP_I2SMCC_MRA_IMCKDIV(div) \
140 (((div) << 16) & MCHP_I2SMCC_MRA_IMCKDIV_MASK)
142 /* TDM Frame Synchronization */
143 #define MCHP_I2SMCC_MRA_TDMFS_MASK GENMASK(23, 22)
144 #define MCHP_I2SMCC_MRA_TDMFS_SLOT (0 << 22)
145 #define MCHP_I2SMCC_MRA_TDMFS_HALF (1 << 22)
146 #define MCHP_I2SMCC_MRA_TDMFS_BIT (2 << 22)
148 /* Selected Clock to I2SMC Serial Clock ratio */
149 #define MCHP_I2SMCC_MRA_ISCKDIV_MASK GENMASK(29, 24)
150 #define MCHP_I2SMCC_MRA_ISCKDIV(div) \
151 (((div) << 24) & MCHP_I2SMCC_MRA_ISCKDIV_MASK)
153 /* Master Clock mode */
154 #define MCHP_I2SMCC_MRA_IMCKMODE_MASK GENMASK(30, 30)
155 /* 0: No master clock generated*/
156 #define MCHP_I2SMCC_MRA_IMCKMODE_NONE (0 << 30)
157 /* 1: master clock generated (internally generated clock drives I2SMCK pin) */
158 #define MCHP_I2SMCC_MRA_IMCKMODE_GEN (1 << 30)
161 /* 0: slot is 32 bits wide for DATALENGTH = 18/20/24 bits. */
162 /* 1: slot is 24 bits wide for DATALENGTH = 18/20/24 bits. */
163 #define MCHP_I2SMCC_MRA_IWS BIT(31)
166 * ---- Mode Register B (Read/Write) ----
168 /* all enabled I2S left channels are filled first, then I2S right channels */
169 #define MCHP_I2SMCC_MRB_CRAMODE_LEFT_FIRST (0 << 0)
171 * an enabled I2S left channel is filled, then the corresponding right
172 * channel, until all channels are filled
174 #define MCHP_I2SMCC_MRB_CRAMODE_REGULAR (1 << 0)
176 #define MCHP_I2SMCC_MRB_FIFOEN BIT(1)
178 #define MCHP_I2SMCC_MRB_DMACHUNK_MASK GENMASK(9, 8)
179 #define MCHP_I2SMCC_MRB_DMACHUNK(no_words) \
180 (((fls(no_words) - 1) << 8) & MCHP_I2SMCC_MRB_DMACHUNK_MASK)
182 #define MCHP_I2SMCC_MRB_CLKSEL_MASK GENMASK(16, 16)
183 #define MCHP_I2SMCC_MRB_CLKSEL_EXT (0 << 16)
184 #define MCHP_I2SMCC_MRB_CLKSEL_INT (1 << 16)
187 * ---- Status Registers (Read-only) ----
189 #define MCHP_I2SMCC_SR_RXEN BIT(0) /* Receiver Enabled */
190 #define MCHP_I2SMCC_SR_TXEN BIT(4) /* Transmitter Enabled */
193 * ---- Interrupt Enable/Disable/Mask/Status Registers A ----
195 #define MCHP_I2SMCC_INT_TXRDY_MASK(ch) GENMASK((ch) - 1, 0)
196 #define MCHP_I2SMCC_INT_TXRDYCH(ch) BIT(ch)
197 #define MCHP_I2SMCC_INT_TXUNF_MASK(ch) GENMASK((ch) + 7, 8)
198 #define MCHP_I2SMCC_INT_TXUNFCH(ch) BIT((ch) + 8)
199 #define MCHP_I2SMCC_INT_RXRDY_MASK(ch) GENMASK((ch) + 15, 16)
200 #define MCHP_I2SMCC_INT_RXRDYCH(ch) BIT((ch) + 16)
201 #define MCHP_I2SMCC_INT_RXOVF_MASK(ch) GENMASK((ch) + 23, 24)
202 #define MCHP_I2SMCC_INT_RXOVFCH(ch) BIT((ch) + 24)
205 * ---- Interrupt Enable/Disable/Mask/Status Registers B ----
207 #define MCHP_I2SMCC_INT_WERR BIT(0)
208 #define MCHP_I2SMCC_INT_TXFFRDY BIT(8)
209 #define MCHP_I2SMCC_INT_TXFFEMP BIT(9)
210 #define MCHP_I2SMCC_INT_RXFFRDY BIT(12)
211 #define MCHP_I2SMCC_INT_RXFFFUL BIT(13)
214 * ---- Version Register (Read-only) ----
216 #define MCHP_I2SMCC_VERSION_MASK GENMASK(11, 0)
218 #define MCHP_I2SMCC_MAX_CHANNELS 8
219 #define MCHP_I2MCC_TDM_SLOT_WIDTH 32
221 static const struct regmap_config mchp_i2s_mcc_regmap_config
= {
225 .max_register
= MCHP_I2SMCC_VERSION
,
228 struct mchp_i2s_mcc_dev
{
229 struct wait_queue_head wq_txrdy
;
230 struct wait_queue_head wq_rxrdy
;
232 struct regmap
*regmap
;
235 struct snd_dmaengine_dai_dma_data playback
;
236 struct snd_dmaengine_dai_dma_data capture
;
239 unsigned int frame_length
;
248 static irqreturn_t
mchp_i2s_mcc_interrupt(int irq
, void *dev_id
)
250 struct mchp_i2s_mcc_dev
*dev
= dev_id
;
251 u32 sra
, imra
, srb
, imrb
, pendinga
, pendingb
, idra
= 0;
252 irqreturn_t ret
= IRQ_NONE
;
254 regmap_read(dev
->regmap
, MCHP_I2SMCC_IMRA
, &imra
);
255 regmap_read(dev
->regmap
, MCHP_I2SMCC_ISRA
, &sra
);
256 pendinga
= imra
& sra
;
258 regmap_read(dev
->regmap
, MCHP_I2SMCC_IMRB
, &imrb
);
259 regmap_read(dev
->regmap
, MCHP_I2SMCC_ISRB
, &srb
);
260 pendingb
= imrb
& srb
;
262 if (!pendinga
&& !pendingb
)
266 * Tx/Rx ready interrupts are enabled when stopping only, to assure
267 * availability and to disable clocks if necessary
269 idra
|= pendinga
& (MCHP_I2SMCC_INT_TXRDY_MASK(dev
->channels
) |
270 MCHP_I2SMCC_INT_RXRDY_MASK(dev
->channels
));
274 if ((imra
& MCHP_I2SMCC_INT_TXRDY_MASK(dev
->channels
)) &&
275 (imra
& MCHP_I2SMCC_INT_TXRDY_MASK(dev
->channels
)) ==
276 (idra
& MCHP_I2SMCC_INT_TXRDY_MASK(dev
->channels
))) {
278 wake_up_interruptible(&dev
->wq_txrdy
);
280 if ((imra
& MCHP_I2SMCC_INT_RXRDY_MASK(dev
->channels
)) &&
281 (imra
& MCHP_I2SMCC_INT_RXRDY_MASK(dev
->channels
)) ==
282 (idra
& MCHP_I2SMCC_INT_RXRDY_MASK(dev
->channels
))) {
284 wake_up_interruptible(&dev
->wq_rxrdy
);
286 regmap_write(dev
->regmap
, MCHP_I2SMCC_IDRA
, idra
);
291 static int mchp_i2s_mcc_set_sysclk(struct snd_soc_dai
*dai
,
292 int clk_id
, unsigned int freq
, int dir
)
294 struct mchp_i2s_mcc_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
296 dev_dbg(dev
->dev
, "%s() clk_id=%d freq=%u dir=%d\n",
297 __func__
, clk_id
, freq
, dir
);
299 /* We do not need SYSCLK */
300 if (dir
== SND_SOC_CLOCK_IN
)
308 static int mchp_i2s_mcc_set_bclk_ratio(struct snd_soc_dai
*dai
,
311 struct mchp_i2s_mcc_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
313 dev_dbg(dev
->dev
, "%s() ratio=%u\n", __func__
, ratio
);
315 dev
->frame_length
= ratio
;
320 static int mchp_i2s_mcc_set_dai_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
322 struct mchp_i2s_mcc_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
324 dev_dbg(dev
->dev
, "%s() fmt=%#x\n", __func__
, fmt
);
326 /* We don't support any kind of clock inversion */
327 if ((fmt
& SND_SOC_DAIFMT_INV_MASK
) != SND_SOC_DAIFMT_NB_NF
)
330 /* We can't generate only FSYNC */
331 if ((fmt
& SND_SOC_DAIFMT_MASTER_MASK
) == SND_SOC_DAIFMT_CBM_CFS
)
334 /* We can only reconfigure the IP when it's stopped */
335 if (fmt
& SND_SOC_DAIFMT_CONT
)
343 static int mchp_i2s_mcc_set_dai_tdm_slot(struct snd_soc_dai
*dai
,
344 unsigned int tx_mask
,
345 unsigned int rx_mask
,
346 int slots
, int slot_width
)
348 struct mchp_i2s_mcc_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
351 "%s() tx_mask=0x%08x rx_mask=0x%08x slots=%d width=%d\n",
352 __func__
, tx_mask
, rx_mask
, slots
, slot_width
);
354 if (slots
< 0 || slots
> MCHP_I2SMCC_MAX_CHANNELS
||
355 slot_width
!= MCHP_I2MCC_TDM_SLOT_WIDTH
)
359 /* We do not support daisy chain */
360 if (rx_mask
!= GENMASK(slots
- 1, 0) ||
365 dev
->tdm_slots
= slots
;
366 dev
->frame_length
= slots
* MCHP_I2MCC_TDM_SLOT_WIDTH
;
371 static int mchp_i2s_mcc_clk_get_rate_diff(struct clk
*clk
,
373 struct clk
**best_clk
,
374 unsigned long *best_rate
,
375 unsigned long *best_diff_rate
)
378 unsigned int diff_rate
;
380 round_rate
= clk_round_rate(clk
, rate
);
382 return (int)round_rate
;
384 diff_rate
= abs(rate
- round_rate
);
385 if (diff_rate
< *best_diff_rate
) {
387 *best_diff_rate
= diff_rate
;
394 static int mchp_i2s_mcc_config_divs(struct mchp_i2s_mcc_dev
*dev
,
395 unsigned int bclk
, unsigned int *mra
,
396 unsigned long *best_rate
)
398 unsigned long clk_rate
;
399 unsigned long lcm_rate
;
400 unsigned long best_diff_rate
= ~0;
402 struct clk
*best_clk
= NULL
;
405 /* For code simplification */
409 sysclk
= dev
->sysclk
;
412 * MCLK is Selected CLK / (2 * IMCKDIV),
413 * BCLK is Selected CLK / (2 * ISCKDIV);
414 * if IMCKDIV or ISCKDIV are 0, MCLK or BCLK = Selected CLK
416 lcm_rate
= lcm(sysclk
, bclk
);
417 if ((lcm_rate
/ sysclk
% 2 == 1 && lcm_rate
/ sysclk
> 2) ||
418 (lcm_rate
/ bclk
% 2 == 1 && lcm_rate
/ bclk
> 2))
421 for (clk_rate
= lcm_rate
;
422 (clk_rate
== sysclk
|| clk_rate
/ (sysclk
* 2) <= GENMASK(5, 0)) &&
423 (clk_rate
== bclk
|| clk_rate
/ (bclk
* 2) <= GENMASK(5, 0));
424 clk_rate
+= lcm_rate
) {
425 ret
= mchp_i2s_mcc_clk_get_rate_diff(dev
->gclk
, clk_rate
,
426 &best_clk
, best_rate
,
429 dev_err(dev
->dev
, "gclk error for rate %lu: %d",
432 if (!best_diff_rate
) {
433 dev_dbg(dev
->dev
, "found perfect rate on gclk: %lu\n",
439 ret
= mchp_i2s_mcc_clk_get_rate_diff(dev
->pclk
, clk_rate
,
440 &best_clk
, best_rate
,
443 dev_err(dev
->dev
, "pclk error for rate %lu: %d",
446 if (!best_diff_rate
) {
447 dev_dbg(dev
->dev
, "found perfect rate on pclk: %lu\n",
454 /* check if clocks returned only errors */
456 dev_err(dev
->dev
, "unable to change rate to clocks\n");
460 dev_dbg(dev
->dev
, "source CLK is %s with rate %lu, diff %lu\n",
461 best_clk
== dev
->pclk
? "pclk" : "gclk",
462 *best_rate
, best_diff_rate
);
464 /* Configure divisors */
466 *mra
|= MCHP_I2SMCC_MRA_IMCKDIV(*best_rate
/ (2 * sysclk
));
467 *mra
|= MCHP_I2SMCC_MRA_ISCKDIV(*best_rate
/ (2 * bclk
));
469 if (best_clk
== dev
->gclk
)
470 *mra
|= MCHP_I2SMCC_MRA_SRCCLK_GCLK
;
472 *mra
|= MCHP_I2SMCC_MRA_SRCCLK_PCLK
;
477 static int mchp_i2s_mcc_is_running(struct mchp_i2s_mcc_dev
*dev
)
481 regmap_read(dev
->regmap
, MCHP_I2SMCC_SR
, &sr
);
482 return !!(sr
& (MCHP_I2SMCC_SR_TXEN
| MCHP_I2SMCC_SR_RXEN
));
485 static int mchp_i2s_mcc_hw_params(struct snd_pcm_substream
*substream
,
486 struct snd_pcm_hw_params
*params
,
487 struct snd_soc_dai
*dai
)
489 unsigned long rate
= 0;
490 struct mchp_i2s_mcc_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
493 unsigned int channels
= params_channels(params
);
494 unsigned int frame_length
= dev
->frame_length
;
495 unsigned int bclk_rate
;
498 bool is_playback
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
500 dev_dbg(dev
->dev
, "%s() rate=%u format=%#x width=%u channels=%u\n",
501 __func__
, params_rate(params
), params_format(params
),
502 params_width(params
), params_channels(params
));
504 switch (dev
->fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
505 case SND_SOC_DAIFMT_I2S
:
506 if (dev
->tdm_slots
) {
507 dev_err(dev
->dev
, "I2S with TDM is not supported\n");
510 mra
|= MCHP_I2SMCC_MRA_FORMAT_I2S
;
512 case SND_SOC_DAIFMT_LEFT_J
:
513 if (dev
->tdm_slots
) {
514 dev_err(dev
->dev
, "Left-Justified with TDM is not supported\n");
517 mra
|= MCHP_I2SMCC_MRA_FORMAT_LJ
;
519 case SND_SOC_DAIFMT_DSP_A
:
520 mra
|= MCHP_I2SMCC_MRA_FORMAT_TDM
;
523 dev_err(dev
->dev
, "unsupported bus format\n");
527 switch (dev
->fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
528 case SND_SOC_DAIFMT_CBS_CFS
:
529 /* cpu is BCLK and LRC master */
530 mra
|= MCHP_I2SMCC_MRA_MODE_MASTER
;
532 mra
|= MCHP_I2SMCC_MRA_IMCKMODE_GEN
;
535 case SND_SOC_DAIFMT_CBS_CFM
:
536 /* cpu is BCLK master */
537 mrb
|= MCHP_I2SMCC_MRB_CLKSEL_INT
;
540 case SND_SOC_DAIFMT_CBM_CFM
:
542 mra
|= MCHP_I2SMCC_MRA_MODE_SLAVE
;
544 dev_warn(dev
->dev
, "Unable to generate MCLK in Slave mode\n");
547 dev_err(dev
->dev
, "unsupported master/slave mode\n");
551 if (dev
->fmt
& (SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_LEFT_J
)) {
555 mra
|= MCHP_I2SMCC_MRA_TXMONO
;
557 mra
|= MCHP_I2SMCC_MRA_RXMONO
;
562 dev_err(dev
->dev
, "unsupported number of audio channels\n");
567 frame_length
= 2 * params_physical_width(params
);
568 } else if (dev
->fmt
& SND_SOC_DAIFMT_DSP_A
) {
569 if (dev
->tdm_slots
) {
570 if (channels
% 2 && channels
* 2 <= dev
->tdm_slots
) {
572 * Duplicate data for even-numbered channels
573 * to odd-numbered channels
576 mra
|= MCHP_I2SMCC_MRA_TXMONO
;
578 mra
|= MCHP_I2SMCC_MRA_RXMONO
;
580 channels
= dev
->tdm_slots
;
583 mra
|= MCHP_I2SMCC_MRA_NBCHAN(channels
);
585 frame_length
= channels
* MCHP_I2MCC_TDM_SLOT_WIDTH
;
589 * We must have the same burst size configured
590 * in the DMA transfer and in out IP
592 mrb
|= MCHP_I2SMCC_MRB_DMACHUNK(channels
);
594 dev
->playback
.maxburst
= 1 << (fls(channels
) - 1);
596 dev
->capture
.maxburst
= 1 << (fls(channels
) - 1);
598 switch (params_format(params
)) {
599 case SNDRV_PCM_FORMAT_S8
:
600 mra
|= MCHP_I2SMCC_MRA_DATALENGTH_8_BITS
;
602 case SNDRV_PCM_FORMAT_S16_LE
:
603 mra
|= MCHP_I2SMCC_MRA_DATALENGTH_16_BITS
;
605 case SNDRV_PCM_FORMAT_S18_3LE
:
606 mra
|= MCHP_I2SMCC_MRA_DATALENGTH_18_BITS
|
609 case SNDRV_PCM_FORMAT_S20_3LE
:
610 mra
|= MCHP_I2SMCC_MRA_DATALENGTH_20_BITS
|
613 case SNDRV_PCM_FORMAT_S24_3LE
:
614 mra
|= MCHP_I2SMCC_MRA_DATALENGTH_24_BITS
|
617 case SNDRV_PCM_FORMAT_S24_LE
:
618 mra
|= MCHP_I2SMCC_MRA_DATALENGTH_24_BITS
;
620 case SNDRV_PCM_FORMAT_S32_LE
:
621 mra
|= MCHP_I2SMCC_MRA_DATALENGTH_32_BITS
;
624 dev_err(dev
->dev
, "unsupported size/endianness for audio samples\n");
629 bclk_rate
= frame_length
* params_rate(params
);
630 ret
= mchp_i2s_mcc_config_divs(dev
, bclk_rate
, &mra
,
634 "unable to configure the divisors: %d\n", ret
);
640 * If we are already running, the wanted setup must be
641 * the same with the one that's currently ongoing
643 if (mchp_i2s_mcc_is_running(dev
)) {
647 regmap_read(dev
->regmap
, MCHP_I2SMCC_MRA
, &mra_cur
);
648 regmap_read(dev
->regmap
, MCHP_I2SMCC_MRB
, &mrb_cur
);
649 if (mra
!= mra_cur
|| mrb
!= mrb_cur
)
655 if (mra
& MCHP_I2SMCC_MRA_SRCCLK_GCLK
&& !dev
->gclk_use
) {
657 ret
= clk_set_rate(dev
->gclk
, rate
);
660 "unable to set rate %lu to GCLK: %d\n",
665 ret
= clk_prepare(dev
->gclk
);
667 dev_err(dev
->dev
, "unable to prepare GCLK: %d\n", ret
);
673 /* Save the number of channels to know what interrupts to enable */
674 dev
->channels
= channels
;
676 ret
= regmap_write(dev
->regmap
, MCHP_I2SMCC_MRA
, mra
);
679 clk_unprepare(dev
->gclk
);
684 return regmap_write(dev
->regmap
, MCHP_I2SMCC_MRB
, mrb
);
687 static int mchp_i2s_mcc_hw_free(struct snd_pcm_substream
*substream
,
688 struct snd_soc_dai
*dai
)
690 struct mchp_i2s_mcc_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
691 bool is_playback
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
695 err
= wait_event_interruptible_timeout(dev
->wq_txrdy
,
697 msecs_to_jiffies(500));
699 dev_warn_once(dev
->dev
,
700 "Timeout waiting for Tx ready\n");
701 regmap_write(dev
->regmap
, MCHP_I2SMCC_IDRA
,
702 MCHP_I2SMCC_INT_TXRDY_MASK(dev
->channels
));
706 err
= wait_event_interruptible_timeout(dev
->wq_rxrdy
,
708 msecs_to_jiffies(500));
710 dev_warn_once(dev
->dev
,
711 "Timeout waiting for Rx ready\n");
712 regmap_write(dev
->regmap
, MCHP_I2SMCC_IDRA
,
713 MCHP_I2SMCC_INT_RXRDY_MASK(dev
->channels
));
718 if (!mchp_i2s_mcc_is_running(dev
)) {
719 regmap_write(dev
->regmap
, MCHP_I2SMCC_CR
, MCHP_I2SMCC_CR_CKDIS
);
721 if (dev
->gclk_running
) {
722 clk_disable(dev
->gclk
);
723 dev
->gclk_running
= 0;
726 clk_unprepare(dev
->gclk
);
734 static int mchp_i2s_mcc_trigger(struct snd_pcm_substream
*substream
, int cmd
,
735 struct snd_soc_dai
*dai
)
737 struct mchp_i2s_mcc_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
738 bool is_playback
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
745 case SNDRV_PCM_TRIGGER_START
:
746 case SNDRV_PCM_TRIGGER_RESUME
:
747 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
749 cr
= MCHP_I2SMCC_CR_TXEN
| MCHP_I2SMCC_CR_CKEN
;
751 cr
= MCHP_I2SMCC_CR_RXEN
| MCHP_I2SMCC_CR_CKEN
;
753 case SNDRV_PCM_TRIGGER_STOP
:
754 case SNDRV_PCM_TRIGGER_SUSPEND
:
755 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
756 regmap_read(dev
->regmap
, MCHP_I2SMCC_SR
, &sr
);
757 if (is_playback
&& (sr
& MCHP_I2SMCC_SR_TXEN
)) {
758 cr
= MCHP_I2SMCC_CR_TXDIS
;
761 * Enable Tx Ready interrupts on all channels
762 * to assure all data is sent
764 iera
= MCHP_I2SMCC_INT_TXRDY_MASK(dev
->channels
);
765 } else if (!is_playback
&& (sr
& MCHP_I2SMCC_SR_RXEN
)) {
766 cr
= MCHP_I2SMCC_CR_RXDIS
;
769 * Enable Rx Ready interrupts on all channels
770 * to assure all data is received
772 iera
= MCHP_I2SMCC_INT_RXRDY_MASK(dev
->channels
);
779 if ((cr
& MCHP_I2SMCC_CR_CKEN
) && dev
->gclk_use
&&
780 !dev
->gclk_running
) {
781 err
= clk_enable(dev
->gclk
);
783 dev_err_once(dev
->dev
, "failed to enable GCLK: %d\n",
786 dev
->gclk_running
= 1;
790 regmap_write(dev
->regmap
, MCHP_I2SMCC_IERA
, iera
);
791 regmap_write(dev
->regmap
, MCHP_I2SMCC_CR
, cr
);
796 static int mchp_i2s_mcc_startup(struct snd_pcm_substream
*substream
,
797 struct snd_soc_dai
*dai
)
799 struct mchp_i2s_mcc_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
801 /* Software reset the IP if it's not running */
802 if (!mchp_i2s_mcc_is_running(dev
)) {
803 return regmap_write(dev
->regmap
, MCHP_I2SMCC_CR
,
804 MCHP_I2SMCC_CR_SWRST
);
810 static const struct snd_soc_dai_ops mchp_i2s_mcc_dai_ops
= {
811 .set_sysclk
= mchp_i2s_mcc_set_sysclk
,
812 .set_bclk_ratio
= mchp_i2s_mcc_set_bclk_ratio
,
813 .startup
= mchp_i2s_mcc_startup
,
814 .trigger
= mchp_i2s_mcc_trigger
,
815 .hw_params
= mchp_i2s_mcc_hw_params
,
816 .hw_free
= mchp_i2s_mcc_hw_free
,
817 .set_fmt
= mchp_i2s_mcc_set_dai_fmt
,
818 .set_tdm_slot
= mchp_i2s_mcc_set_dai_tdm_slot
,
821 static int mchp_i2s_mcc_dai_probe(struct snd_soc_dai
*dai
)
823 struct mchp_i2s_mcc_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
825 init_waitqueue_head(&dev
->wq_txrdy
);
826 init_waitqueue_head(&dev
->wq_rxrdy
);
830 snd_soc_dai_init_dma_data(dai
, &dev
->playback
, &dev
->capture
);
835 #define MCHP_I2SMCC_RATES SNDRV_PCM_RATE_8000_192000
837 #define MCHP_I2SMCC_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
838 SNDRV_PCM_FMTBIT_S16_LE | \
839 SNDRV_PCM_FMTBIT_S18_3LE | \
840 SNDRV_PCM_FMTBIT_S20_3LE | \
841 SNDRV_PCM_FMTBIT_S24_3LE | \
842 SNDRV_PCM_FMTBIT_S24_LE | \
843 SNDRV_PCM_FMTBIT_S32_LE)
845 static struct snd_soc_dai_driver mchp_i2s_mcc_dai
= {
846 .probe
= mchp_i2s_mcc_dai_probe
,
848 .stream_name
= "I2SMCC-Playback",
851 .rates
= MCHP_I2SMCC_RATES
,
852 .formats
= MCHP_I2SMCC_FORMATS
,
855 .stream_name
= "I2SMCC-Capture",
858 .rates
= MCHP_I2SMCC_RATES
,
859 .formats
= MCHP_I2SMCC_FORMATS
,
861 .ops
= &mchp_i2s_mcc_dai_ops
,
862 .symmetric_rates
= 1,
863 .symmetric_samplebits
= 1,
864 .symmetric_channels
= 1,
867 static const struct snd_soc_component_driver mchp_i2s_mcc_component
= {
868 .name
= "mchp-i2s-mcc",
872 static const struct of_device_id mchp_i2s_mcc_dt_ids
[] = {
874 .compatible
= "microchip,sam9x60-i2smcc",
878 MODULE_DEVICE_TABLE(of
, mchp_i2s_mcc_dt_ids
);
881 static int mchp_i2s_mcc_probe(struct platform_device
*pdev
)
883 struct mchp_i2s_mcc_dev
*dev
;
884 struct resource
*mem
;
885 struct regmap
*regmap
;
891 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
895 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
896 base
= devm_ioremap_resource(&pdev
->dev
, mem
);
898 return PTR_ERR(base
);
900 regmap
= devm_regmap_init_mmio(&pdev
->dev
, base
,
901 &mchp_i2s_mcc_regmap_config
);
903 return PTR_ERR(regmap
);
905 irq
= platform_get_irq(pdev
, 0);
909 err
= devm_request_irq(&pdev
->dev
, irq
, mchp_i2s_mcc_interrupt
, 0,
910 dev_name(&pdev
->dev
), dev
);
914 dev
->pclk
= devm_clk_get(&pdev
->dev
, "pclk");
915 if (IS_ERR(dev
->pclk
)) {
916 err
= PTR_ERR(dev
->pclk
);
918 "failed to get the peripheral clock: %d\n", err
);
922 /* Get the optional generated clock */
923 dev
->gclk
= devm_clk_get(&pdev
->dev
, "gclk");
924 if (IS_ERR(dev
->gclk
)) {
925 if (PTR_ERR(dev
->gclk
) == -EPROBE_DEFER
)
926 return -EPROBE_DEFER
;
928 "generated clock not found: %d\n", err
);
932 dev
->dev
= &pdev
->dev
;
933 dev
->regmap
= regmap
;
934 platform_set_drvdata(pdev
, dev
);
936 err
= clk_prepare_enable(dev
->pclk
);
939 "failed to enable the peripheral clock: %d\n", err
);
943 err
= devm_snd_soc_register_component(&pdev
->dev
,
944 &mchp_i2s_mcc_component
,
945 &mchp_i2s_mcc_dai
, 1);
947 dev_err(&pdev
->dev
, "failed to register DAI: %d\n", err
);
948 clk_disable_unprepare(dev
->pclk
);
952 dev
->playback
.addr
= (dma_addr_t
)mem
->start
+ MCHP_I2SMCC_THR
;
953 dev
->capture
.addr
= (dma_addr_t
)mem
->start
+ MCHP_I2SMCC_RHR
;
955 err
= devm_snd_dmaengine_pcm_register(&pdev
->dev
, NULL
, 0);
957 dev_err(&pdev
->dev
, "failed to register PCM: %d\n", err
);
958 clk_disable_unprepare(dev
->pclk
);
962 /* Get IP version. */
963 regmap_read(dev
->regmap
, MCHP_I2SMCC_VERSION
, &version
);
964 dev_info(&pdev
->dev
, "hw version: %#lx\n",
965 version
& MCHP_I2SMCC_VERSION_MASK
);
970 static int mchp_i2s_mcc_remove(struct platform_device
*pdev
)
972 struct mchp_i2s_mcc_dev
*dev
= platform_get_drvdata(pdev
);
974 clk_disable_unprepare(dev
->pclk
);
979 static struct platform_driver mchp_i2s_mcc_driver
= {
981 .name
= "mchp_i2s_mcc",
982 .of_match_table
= of_match_ptr(mchp_i2s_mcc_dt_ids
),
984 .probe
= mchp_i2s_mcc_probe
,
985 .remove
= mchp_i2s_mcc_remove
,
987 module_platform_driver(mchp_i2s_mcc_driver
);
989 MODULE_DESCRIPTION("Microchip I2S Multi-Channel Controller driver");
990 MODULE_AUTHOR("Codrin Ciubotariu <codrin.ciubotariu@microchip.com>");
991 MODULE_LICENSE("GPL v2");