blob: 9a406144b18ff1c0c697b5327ef8453f5d016795 [file] [log] [blame]
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +00001// SPDX-License-Identifier: GPL-2.0
2//
3// Driver for Microchip I2S Multi-channel controller
4//
5// Copyright (C) 2018 Microchip Technology Inc. and its subsidiaries
6//
7// Author: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
8
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/device.h>
12#include <linux/slab.h>
13
14#include <linux/delay.h>
15#include <linux/io.h>
16#include <linux/clk.h>
17#include <linux/mfd/syscon.h>
18#include <linux/lcm.h>
19
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>
26
27/*
28 * ---- I2S Controller Register map ----
29 */
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 */
38
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 */
43
44#define MCHP_I2SMCC_RHR 0x0030 /* Receiver Holding Register */
45#define MCHP_I2SMCC_THR 0x0034 /* Transmitter Holding Register */
46
47#define MCHP_I2SMCC_RHL0R 0x0040 /* Receiver Holding Left 0 Register */
48#define MCHP_I2SMCC_RHR0R 0x0044 /* Receiver Holding Right 0 Register */
49
50#define MCHP_I2SMCC_RHL1R 0x0048 /* Receiver Holding Left 1 Register */
51#define MCHP_I2SMCC_RHR1R 0x004C /* Receiver Holding Right 1 Register */
52
53#define MCHP_I2SMCC_RHL2R 0x0050 /* Receiver Holding Left 2 Register */
54#define MCHP_I2SMCC_RHR2R 0x0054 /* Receiver Holding Right 2 Register */
55
56#define MCHP_I2SMCC_RHL3R 0x0058 /* Receiver Holding Left 3 Register */
57#define MCHP_I2SMCC_RHR3R 0x005C /* Receiver Holding Right 3 Register */
58
59#define MCHP_I2SMCC_THL0R 0x0060 /* Transmitter Holding Left 0 Register */
60#define MCHP_I2SMCC_THR0R 0x0064 /* Transmitter Holding Right 0 Register */
61
62#define MCHP_I2SMCC_THL1R 0x0068 /* Transmitter Holding Left 1 Register */
63#define MCHP_I2SMCC_THR1R 0x006C /* Transmitter Holding Right 1 Register */
64
65#define MCHP_I2SMCC_THL2R 0x0070 /* Transmitter Holding Left 2 Register */
66#define MCHP_I2SMCC_THR2R 0x0074 /* Transmitter Holding Right 2 Register */
67
68#define MCHP_I2SMCC_THL3R 0x0078 /* Transmitter Holding Left 3 Register */
69#define MCHP_I2SMCC_THR3R 0x007C /* Transmitter Holding Right 3 Register */
70
71#define MCHP_I2SMCC_VERSION 0x00FC /* Version Register */
72
73/*
74 * ---- Control Register (Write-only) ----
75 */
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 */
83
84/*
85 * ---- Mode Register A (Read/Write) ----
86 */
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)
90
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)
100
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)
106
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)
112
113/* Transmitter uses one DMA channel ... */
114/* Left audio samples duplicated to right audio channel */
115#define MCHP_I2SMCC_MRA_RXMONO BIT(8)
116
117/* I2SDO output of I2SC is internally connected to I2SDI input */
118#define MCHP_I2SMCC_MRA_RXLOOP BIT(9)
119
120/* Receiver uses one DMA channel ... */
121/* Left audio samples duplicated to right audio channel */
122#define MCHP_I2SMCC_MRA_TXMONO BIT(10)
123
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 */
127
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)
131
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)
136
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)
141
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)
147
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)
152
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)
159
160/* Slot Width */
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)
164
165/*
166 * ---- Mode Register B (Read/Write) ----
167 */
168/* all enabled I2S left channels are filled first, then I2S right channels */
169#define MCHP_I2SMCC_MRB_CRAMODE_LEFT_FIRST (0 << 0)
170/*
171 * an enabled I2S left channel is filled, then the corresponding right
172 * channel, until all channels are filled
173 */
174#define MCHP_I2SMCC_MRB_CRAMODE_REGULAR (1 << 0)
175
176#define MCHP_I2SMCC_MRB_FIFOEN BIT(1)
177
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)
181
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)
185
186/*
187 * ---- Status Registers (Read-only) ----
188 */
189#define MCHP_I2SMCC_SR_RXEN BIT(0) /* Receiver Enabled */
190#define MCHP_I2SMCC_SR_TXEN BIT(4) /* Transmitter Enabled */
191
192/*
193 * ---- Interrupt Enable/Disable/Mask/Status Registers A ----
194 */
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)
203
204/*
205 * ---- Interrupt Enable/Disable/Mask/Status Registers B ----
206 */
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)
212
213/*
214 * ---- Version Register (Read-only) ----
215 */
216#define MCHP_I2SMCC_VERSION_MASK GENMASK(11, 0)
217
218#define MCHP_I2SMCC_MAX_CHANNELS 8
219#define MCHP_I2MCC_TDM_SLOT_WIDTH 32
220
221static const struct regmap_config mchp_i2s_mcc_regmap_config = {
222 .reg_bits = 32,
223 .reg_stride = 4,
224 .val_bits = 32,
225 .max_register = MCHP_I2SMCC_VERSION,
226};
227
228struct mchp_i2s_mcc_dev {
229 struct wait_queue_head wq_txrdy;
230 struct wait_queue_head wq_rxrdy;
231 struct device *dev;
232 struct regmap *regmap;
233 struct clk *pclk;
234 struct clk *gclk;
235 struct snd_dmaengine_dai_dma_data playback;
236 struct snd_dmaengine_dai_dma_data capture;
237 unsigned int fmt;
238 unsigned int sysclk;
239 unsigned int frame_length;
240 int tdm_slots;
241 int channels;
242 int gclk_use:1;
243 int gclk_running:1;
244 int tx_rdy:1;
245 int rx_rdy:1;
246};
247
248static irqreturn_t mchp_i2s_mcc_interrupt(int irq, void *dev_id)
249{
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;
253
254 regmap_read(dev->regmap, MCHP_I2SMCC_IMRA, &imra);
255 regmap_read(dev->regmap, MCHP_I2SMCC_ISRA, &sra);
256 pendinga = imra & sra;
257
258 regmap_read(dev->regmap, MCHP_I2SMCC_IMRB, &imrb);
259 regmap_read(dev->regmap, MCHP_I2SMCC_ISRB, &srb);
260 pendingb = imrb & srb;
261
262 if (!pendinga && !pendingb)
263 return IRQ_NONE;
264
265 /*
266 * Tx/Rx ready interrupts are enabled when stopping only, to assure
267 * availability and to disable clocks if necessary
268 */
269 idra |= pendinga & (MCHP_I2SMCC_INT_TXRDY_MASK(dev->channels) |
270 MCHP_I2SMCC_INT_RXRDY_MASK(dev->channels));
271 if (idra)
272 ret = IRQ_HANDLED;
273
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))) {
277 dev->tx_rdy = 1;
278 wake_up_interruptible(&dev->wq_txrdy);
279 }
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))) {
283 dev->rx_rdy = 1;
284 wake_up_interruptible(&dev->wq_rxrdy);
285 }
286 regmap_write(dev->regmap, MCHP_I2SMCC_IDRA, idra);
287
288 return ret;
289}
290
291static int mchp_i2s_mcc_set_sysclk(struct snd_soc_dai *dai,
292 int clk_id, unsigned int freq, int dir)
293{
294 struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai);
295
296 dev_dbg(dev->dev, "%s() clk_id=%d freq=%u dir=%d\n",
297 __func__, clk_id, freq, dir);
298
299 /* We do not need SYSCLK */
300 if (dir == SND_SOC_CLOCK_IN)
301 return 0;
302
303 dev->sysclk = freq;
304
305 return 0;
306}
307
308static int mchp_i2s_mcc_set_bclk_ratio(struct snd_soc_dai *dai,
309 unsigned int ratio)
310{
311 struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai);
312
313 dev_dbg(dev->dev, "%s() ratio=%u\n", __func__, ratio);
314
315 dev->frame_length = ratio;
316
317 return 0;
318}
319
320static int mchp_i2s_mcc_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
321{
322 struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai);
323
324 dev_dbg(dev->dev, "%s() fmt=%#x\n", __func__, fmt);
325
326 /* We don't support any kind of clock inversion */
327 if ((fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF)
328 return -EINVAL;
329
330 /* We can't generate only FSYNC */
331 if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBM_CFS)
332 return -EINVAL;
333
334 /* We can only reconfigure the IP when it's stopped */
335 if (fmt & SND_SOC_DAIFMT_CONT)
336 return -EINVAL;
337
338 dev->fmt = fmt;
339
340 return 0;
341}
342
343static 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)
347{
348 struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai);
349
350 dev_dbg(dev->dev,
351 "%s() tx_mask=0x%08x rx_mask=0x%08x slots=%d width=%d\n",
352 __func__, tx_mask, rx_mask, slots, slot_width);
353
354 if (slots < 0 || slots > MCHP_I2SMCC_MAX_CHANNELS ||
355 slot_width != MCHP_I2MCC_TDM_SLOT_WIDTH)
356 return -EINVAL;
357
358 if (slots) {
359 /* We do not support daisy chain */
360 if (rx_mask != GENMASK(slots - 1, 0) ||
361 rx_mask != tx_mask)
362 return -EINVAL;
363 }
364
365 dev->tdm_slots = slots;
366 dev->frame_length = slots * MCHP_I2MCC_TDM_SLOT_WIDTH;
367
368 return 0;
369}
370
371static int mchp_i2s_mcc_clk_get_rate_diff(struct clk *clk,
372 unsigned long rate,
373 struct clk **best_clk,
374 unsigned long *best_rate,
375 unsigned long *best_diff_rate)
376{
377 long round_rate;
378 unsigned int diff_rate;
379
380 round_rate = clk_round_rate(clk, rate);
381 if (round_rate < 0)
382 return (int)round_rate;
383
384 diff_rate = abs(rate - round_rate);
385 if (diff_rate < *best_diff_rate) {
386 *best_clk = clk;
387 *best_diff_rate = diff_rate;
388 *best_rate = rate;
389 }
390
391 return 0;
392}
393
394static int mchp_i2s_mcc_config_divs(struct mchp_i2s_mcc_dev *dev,
Codrin Ciubotariuc9cff332019-08-20 19:24:11 +0300395 unsigned int bclk, unsigned int *mra,
396 unsigned long *best_rate)
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000397{
398 unsigned long clk_rate;
399 unsigned long lcm_rate;
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000400 unsigned long best_diff_rate = ~0;
401 unsigned int sysclk;
402 struct clk *best_clk = NULL;
403 int ret;
404
405 /* For code simplification */
406 if (!dev->sysclk)
407 sysclk = bclk;
408 else
409 sysclk = dev->sysclk;
410
411 /*
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
415 */
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))
419 lcm_rate *= 2;
420
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,
Codrin Ciubotariuc9cff332019-08-20 19:24:11 +0300426 &best_clk, best_rate,
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000427 &best_diff_rate);
428 if (ret) {
429 dev_err(dev->dev, "gclk error for rate %lu: %d",
430 clk_rate, ret);
431 } else {
432 if (!best_diff_rate) {
433 dev_dbg(dev->dev, "found perfect rate on gclk: %lu\n",
434 clk_rate);
435 break;
436 }
437 }
438
439 ret = mchp_i2s_mcc_clk_get_rate_diff(dev->pclk, clk_rate,
Codrin Ciubotariuc9cff332019-08-20 19:24:11 +0300440 &best_clk, best_rate,
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000441 &best_diff_rate);
442 if (ret) {
443 dev_err(dev->dev, "pclk error for rate %lu: %d",
444 clk_rate, ret);
445 } else {
446 if (!best_diff_rate) {
447 dev_dbg(dev->dev, "found perfect rate on pclk: %lu\n",
448 clk_rate);
449 break;
450 }
451 }
452 }
453
454 /* check if clocks returned only errors */
455 if (!best_clk) {
456 dev_err(dev->dev, "unable to change rate to clocks\n");
457 return -EINVAL;
458 }
459
460 dev_dbg(dev->dev, "source CLK is %s with rate %lu, diff %lu\n",
461 best_clk == dev->pclk ? "pclk" : "gclk",
Codrin Ciubotariuc9cff332019-08-20 19:24:11 +0300462 *best_rate, best_diff_rate);
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000463
464 /* Configure divisors */
465 if (dev->sysclk)
Codrin Ciubotariuc9cff332019-08-20 19:24:11 +0300466 *mra |= MCHP_I2SMCC_MRA_IMCKDIV(*best_rate / (2 * sysclk));
467 *mra |= MCHP_I2SMCC_MRA_ISCKDIV(*best_rate / (2 * bclk));
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000468
Codrin Ciubotariuc9cff332019-08-20 19:24:11 +0300469 if (best_clk == dev->gclk)
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000470 *mra |= MCHP_I2SMCC_MRA_SRCCLK_GCLK;
Codrin Ciubotariuc9cff332019-08-20 19:24:11 +0300471 else
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000472 *mra |= MCHP_I2SMCC_MRA_SRCCLK_PCLK;
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000473
474 return 0;
475}
476
477static int mchp_i2s_mcc_is_running(struct mchp_i2s_mcc_dev *dev)
478{
479 u32 sr;
480
481 regmap_read(dev->regmap, MCHP_I2SMCC_SR, &sr);
482 return !!(sr & (MCHP_I2SMCC_SR_TXEN | MCHP_I2SMCC_SR_RXEN));
483}
484
485static int mchp_i2s_mcc_hw_params(struct snd_pcm_substream *substream,
486 struct snd_pcm_hw_params *params,
487 struct snd_soc_dai *dai)
488{
Codrin Ciubotariuc9cff332019-08-20 19:24:11 +0300489 unsigned long rate = 0;
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000490 struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai);
491 u32 mra = 0;
492 u32 mrb = 0;
493 unsigned int channels = params_channels(params);
494 unsigned int frame_length = dev->frame_length;
495 unsigned int bclk_rate;
496 int set_divs = 0;
497 int ret;
498 bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
499
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));
503
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");
508 return -EINVAL;
509 }
510 mra |= MCHP_I2SMCC_MRA_FORMAT_I2S;
511 break;
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");
515 return -EINVAL;
516 }
517 mra |= MCHP_I2SMCC_MRA_FORMAT_LJ;
518 break;
519 case SND_SOC_DAIFMT_DSP_A:
520 mra |= MCHP_I2SMCC_MRA_FORMAT_TDM;
521 break;
522 default:
523 dev_err(dev->dev, "unsupported bus format\n");
524 return -EINVAL;
525 }
526
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;
531 if (dev->sysclk)
532 mra |= MCHP_I2SMCC_MRA_IMCKMODE_GEN;
533 set_divs = 1;
534 break;
535 case SND_SOC_DAIFMT_CBS_CFM:
536 /* cpu is BCLK master */
537 mrb |= MCHP_I2SMCC_MRB_CLKSEL_INT;
538 set_divs = 1;
539 /* fall through */
540 case SND_SOC_DAIFMT_CBM_CFM:
541 /* cpu is slave */
542 mra |= MCHP_I2SMCC_MRA_MODE_SLAVE;
543 if (dev->sysclk)
544 dev_warn(dev->dev, "Unable to generate MCLK in Slave mode\n");
545 break;
546 default:
547 dev_err(dev->dev, "unsupported master/slave mode\n");
548 return -EINVAL;
549 }
550
551 if (dev->fmt & (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_LEFT_J)) {
552 switch (channels) {
553 case 1:
554 if (is_playback)
555 mra |= MCHP_I2SMCC_MRA_TXMONO;
556 else
557 mra |= MCHP_I2SMCC_MRA_RXMONO;
558 break;
559 case 2:
560 break;
561 default:
562 dev_err(dev->dev, "unsupported number of audio channels\n");
563 return -EINVAL;
564 }
565
566 if (!frame_length)
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) {
571 /*
572 * Duplicate data for even-numbered channels
573 * to odd-numbered channels
574 */
575 if (is_playback)
576 mra |= MCHP_I2SMCC_MRA_TXMONO;
577 else
578 mra |= MCHP_I2SMCC_MRA_RXMONO;
579 }
580 channels = dev->tdm_slots;
581 }
582
583 mra |= MCHP_I2SMCC_MRA_NBCHAN(channels);
584 if (!frame_length)
585 frame_length = channels * MCHP_I2MCC_TDM_SLOT_WIDTH;
586 }
587
588 /*
589 * We must have the same burst size configured
590 * in the DMA transfer and in out IP
591 */
592 mrb |= MCHP_I2SMCC_MRB_DMACHUNK(channels);
593 if (is_playback)
594 dev->playback.maxburst = 1 << (fls(channels) - 1);
595 else
596 dev->capture.maxburst = 1 << (fls(channels) - 1);
597
598 switch (params_format(params)) {
599 case SNDRV_PCM_FORMAT_S8:
600 mra |= MCHP_I2SMCC_MRA_DATALENGTH_8_BITS;
601 break;
602 case SNDRV_PCM_FORMAT_S16_LE:
603 mra |= MCHP_I2SMCC_MRA_DATALENGTH_16_BITS;
604 break;
605 case SNDRV_PCM_FORMAT_S18_3LE:
606 mra |= MCHP_I2SMCC_MRA_DATALENGTH_18_BITS |
607 MCHP_I2SMCC_MRA_IWS;
608 break;
609 case SNDRV_PCM_FORMAT_S20_3LE:
610 mra |= MCHP_I2SMCC_MRA_DATALENGTH_20_BITS |
611 MCHP_I2SMCC_MRA_IWS;
612 break;
613 case SNDRV_PCM_FORMAT_S24_3LE:
614 mra |= MCHP_I2SMCC_MRA_DATALENGTH_24_BITS |
615 MCHP_I2SMCC_MRA_IWS;
616 break;
617 case SNDRV_PCM_FORMAT_S24_LE:
618 mra |= MCHP_I2SMCC_MRA_DATALENGTH_24_BITS;
619 break;
620 case SNDRV_PCM_FORMAT_S32_LE:
621 mra |= MCHP_I2SMCC_MRA_DATALENGTH_32_BITS;
622 break;
623 default:
624 dev_err(dev->dev, "unsupported size/endianness for audio samples\n");
625 return -EINVAL;
626 }
627
Codrin Ciubotariuc9cff332019-08-20 19:24:11 +0300628 if (set_divs) {
629 bclk_rate = frame_length * params_rate(params);
630 ret = mchp_i2s_mcc_config_divs(dev, bclk_rate, &mra,
631 &rate);
632 if (ret) {
633 dev_err(dev->dev,
634 "unable to configure the divisors: %d\n", ret);
635 return ret;
636 }
637 }
638
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000639 /*
640 * If we are already running, the wanted setup must be
641 * the same with the one that's currently ongoing
642 */
643 if (mchp_i2s_mcc_is_running(dev)) {
644 u32 mra_cur;
645 u32 mrb_cur;
646
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)
650 return -EINVAL;
651
652 return 0;
653 }
654
Codrin Ciubotariuc9cff332019-08-20 19:24:11 +0300655 if (mra & MCHP_I2SMCC_MRA_SRCCLK_GCLK && !dev->gclk_use) {
656 /* set the rate */
657 ret = clk_set_rate(dev->gclk, rate);
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000658 if (ret) {
Codrin Ciubotariuc9cff332019-08-20 19:24:11 +0300659 dev_err(dev->dev,
660 "unable to set rate %lu to GCLK: %d\n",
661 rate, ret);
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000662 return ret;
663 }
Codrin Ciubotariuc9cff332019-08-20 19:24:11 +0300664
665 ret = clk_prepare(dev->gclk);
666 if (ret < 0) {
667 dev_err(dev->dev, "unable to prepare GCLK: %d\n", ret);
668 return ret;
669 }
670 dev->gclk_use = 1;
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000671 }
672
Codrin Ciubotariuc9cff332019-08-20 19:24:11 +0300673 /* Save the number of channels to know what interrupts to enable */
674 dev->channels = channels;
675
Codrin Ciubotariub87d37d2019-03-05 11:26:45 +0000676 ret = regmap_write(dev->regmap, MCHP_I2SMCC_MRA, mra);
677 if (ret < 0)
678 return ret;
679 return regmap_write(dev->regmap, MCHP_I2SMCC_MRB, mrb);
680}
681
682static int mchp_i2s_mcc_hw_free(struct snd_pcm_substream *substream,
683 struct snd_soc_dai *dai)
684{
685 struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai);
686 bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
687 long err;
688
689 if (is_playback) {
690 err = wait_event_interruptible_timeout(dev->wq_txrdy,
691 dev->tx_rdy,
692 msecs_to_jiffies(500));
693 } else {
694 err = wait_event_interruptible_timeout(dev->wq_rxrdy,
695 dev->rx_rdy,
696 msecs_to_jiffies(500));
697 }
698
699 if (err == 0) {
700 u32 idra;
701
702 dev_warn_once(dev->dev, "Timeout waiting for %s\n",
703 is_playback ? "Tx ready" : "Rx ready");
704 if (is_playback)
705 idra = MCHP_I2SMCC_INT_TXRDY_MASK(dev->channels);
706 else
707 idra = MCHP_I2SMCC_INT_RXRDY_MASK(dev->channels);
708 regmap_write(dev->regmap, MCHP_I2SMCC_IDRA, idra);
709 }
710
711 if (!mchp_i2s_mcc_is_running(dev)) {
712 regmap_write(dev->regmap, MCHP_I2SMCC_CR, MCHP_I2SMCC_CR_CKDIS);
713
714 if (dev->gclk_running) {
715 clk_disable_unprepare(dev->gclk);
716 dev->gclk_running = 0;
717 }
718 }
719
720 return 0;
721}
722
723static int mchp_i2s_mcc_trigger(struct snd_pcm_substream *substream, int cmd,
724 struct snd_soc_dai *dai)
725{
726 struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai);
727 bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
728 u32 cr = 0;
729 u32 iera = 0;
730 u32 sr;
731 int err;
732
733 switch (cmd) {
734 case SNDRV_PCM_TRIGGER_START:
735 case SNDRV_PCM_TRIGGER_RESUME:
736 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
737 if (is_playback)
738 cr = MCHP_I2SMCC_CR_TXEN | MCHP_I2SMCC_CR_CKEN;
739 else
740 cr = MCHP_I2SMCC_CR_RXEN | MCHP_I2SMCC_CR_CKEN;
741 break;
742 case SNDRV_PCM_TRIGGER_STOP:
743 case SNDRV_PCM_TRIGGER_SUSPEND:
744 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
745 regmap_read(dev->regmap, MCHP_I2SMCC_SR, &sr);
746 if (is_playback && (sr & MCHP_I2SMCC_SR_TXEN)) {
747 cr = MCHP_I2SMCC_CR_TXDIS;
748 dev->tx_rdy = 0;
749 /*
750 * Enable Tx Ready interrupts on all channels
751 * to assure all data is sent
752 */
753 iera = MCHP_I2SMCC_INT_TXRDY_MASK(dev->channels);
754 } else if (!is_playback && (sr & MCHP_I2SMCC_SR_RXEN)) {
755 cr = MCHP_I2SMCC_CR_RXDIS;
756 dev->rx_rdy = 0;
757 /*
758 * Enable Rx Ready interrupts on all channels
759 * to assure all data is received
760 */
761 iera = MCHP_I2SMCC_INT_RXRDY_MASK(dev->channels);
762 }
763 break;
764 default:
765 return -EINVAL;
766 }
767
768 if ((cr & MCHP_I2SMCC_CR_CKEN) && dev->gclk_use &&
769 !dev->gclk_running) {
770 err = clk_enable(dev->gclk);
771 if (err) {
772 dev_err_once(dev->dev, "failed to enable GCLK: %d\n",
773 err);
774 } else {
775 dev->gclk_running = 1;
776 }
777 }
778
779 regmap_write(dev->regmap, MCHP_I2SMCC_IERA, iera);
780 regmap_write(dev->regmap, MCHP_I2SMCC_CR, cr);
781
782 return 0;
783}
784
785static int mchp_i2s_mcc_startup(struct snd_pcm_substream *substream,
786 struct snd_soc_dai *dai)
787{
788 struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai);
789
790 /* Software reset the IP if it's not running */
791 if (!mchp_i2s_mcc_is_running(dev)) {
792 return regmap_write(dev->regmap, MCHP_I2SMCC_CR,
793 MCHP_I2SMCC_CR_SWRST);
794 }
795
796 return 0;
797}
798
799static const struct snd_soc_dai_ops mchp_i2s_mcc_dai_ops = {
800 .set_sysclk = mchp_i2s_mcc_set_sysclk,
801 .set_bclk_ratio = mchp_i2s_mcc_set_bclk_ratio,
802 .startup = mchp_i2s_mcc_startup,
803 .trigger = mchp_i2s_mcc_trigger,
804 .hw_params = mchp_i2s_mcc_hw_params,
805 .hw_free = mchp_i2s_mcc_hw_free,
806 .set_fmt = mchp_i2s_mcc_set_dai_fmt,
807 .set_tdm_slot = mchp_i2s_mcc_set_dai_tdm_slot,
808};
809
810static int mchp_i2s_mcc_dai_probe(struct snd_soc_dai *dai)
811{
812 struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai);
813
814 init_waitqueue_head(&dev->wq_txrdy);
815 init_waitqueue_head(&dev->wq_rxrdy);
816
817 snd_soc_dai_init_dma_data(dai, &dev->playback, &dev->capture);
818
819 return 0;
820}
821
822#define MCHP_I2SMCC_RATES SNDRV_PCM_RATE_8000_192000
823
824#define MCHP_I2SMCC_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
825 SNDRV_PCM_FMTBIT_S16_LE | \
826 SNDRV_PCM_FMTBIT_S18_3LE | \
827 SNDRV_PCM_FMTBIT_S20_3LE | \
828 SNDRV_PCM_FMTBIT_S24_3LE | \
829 SNDRV_PCM_FMTBIT_S24_LE | \
830 SNDRV_PCM_FMTBIT_S32_LE)
831
832static struct snd_soc_dai_driver mchp_i2s_mcc_dai = {
833 .probe = mchp_i2s_mcc_dai_probe,
834 .playback = {
835 .stream_name = "I2SMCC-Playback",
836 .channels_min = 1,
837 .channels_max = 8,
838 .rates = MCHP_I2SMCC_RATES,
839 .formats = MCHP_I2SMCC_FORMATS,
840 },
841 .capture = {
842 .stream_name = "I2SMCC-Capture",
843 .channels_min = 1,
844 .channels_max = 8,
845 .rates = MCHP_I2SMCC_RATES,
846 .formats = MCHP_I2SMCC_FORMATS,
847 },
848 .ops = &mchp_i2s_mcc_dai_ops,
849 .symmetric_rates = 1,
850 .symmetric_samplebits = 1,
851 .symmetric_channels = 1,
852};
853
854static const struct snd_soc_component_driver mchp_i2s_mcc_component = {
855 .name = "mchp-i2s-mcc",
856};
857
858#ifdef CONFIG_OF
859static const struct of_device_id mchp_i2s_mcc_dt_ids[] = {
860 {
861 .compatible = "microchip,sam9x60-i2smcc",
862 },
863 { /* sentinel */ }
864};
865MODULE_DEVICE_TABLE(of, mchp_i2s_mcc_dt_ids);
866#endif
867
868static int mchp_i2s_mcc_probe(struct platform_device *pdev)
869{
870 struct mchp_i2s_mcc_dev *dev;
871 struct resource *mem;
872 struct regmap *regmap;
873 void __iomem *base;
874 u32 version;
875 int irq;
876 int err;
877
878 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
879 if (!dev)
880 return -ENOMEM;
881
882 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
883 base = devm_ioremap_resource(&pdev->dev, mem);
884 if (IS_ERR(base))
885 return PTR_ERR(base);
886
887 regmap = devm_regmap_init_mmio(&pdev->dev, base,
888 &mchp_i2s_mcc_regmap_config);
889 if (IS_ERR(regmap))
890 return PTR_ERR(regmap);
891
892 irq = platform_get_irq(pdev, 0);
893 if (irq < 0)
894 return irq;
895
896 err = devm_request_irq(&pdev->dev, irq, mchp_i2s_mcc_interrupt, 0,
897 dev_name(&pdev->dev), dev);
898 if (err)
899 return err;
900
901 dev->pclk = devm_clk_get(&pdev->dev, "pclk");
902 if (IS_ERR(dev->pclk)) {
903 err = PTR_ERR(dev->pclk);
904 dev_err(&pdev->dev,
905 "failed to get the peripheral clock: %d\n", err);
906 return err;
907 }
908
909 /* Get the optional generated clock */
910 dev->gclk = devm_clk_get(&pdev->dev, "gclk");
911 if (IS_ERR(dev->gclk)) {
912 if (PTR_ERR(dev->gclk) == -EPROBE_DEFER)
913 return -EPROBE_DEFER;
914 dev_warn(&pdev->dev,
915 "generated clock not found: %d\n", err);
916 dev->gclk = NULL;
917 }
918
919 dev->dev = &pdev->dev;
920 dev->regmap = regmap;
921 platform_set_drvdata(pdev, dev);
922
923 err = clk_prepare_enable(dev->pclk);
924 if (err) {
925 dev_err(&pdev->dev,
926 "failed to enable the peripheral clock: %d\n", err);
927 return err;
928 }
929
930 err = devm_snd_soc_register_component(&pdev->dev,
931 &mchp_i2s_mcc_component,
932 &mchp_i2s_mcc_dai, 1);
933 if (err) {
934 dev_err(&pdev->dev, "failed to register DAI: %d\n", err);
935 clk_disable_unprepare(dev->pclk);
936 return err;
937 }
938
939 dev->playback.addr = (dma_addr_t)mem->start + MCHP_I2SMCC_THR;
940 dev->capture.addr = (dma_addr_t)mem->start + MCHP_I2SMCC_RHR;
941
942 err = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
943 if (err) {
944 dev_err(&pdev->dev, "failed to register PCM: %d\n", err);
945 clk_disable_unprepare(dev->pclk);
946 return err;
947 }
948
949 /* Get IP version. */
950 regmap_read(dev->regmap, MCHP_I2SMCC_VERSION, &version);
951 dev_info(&pdev->dev, "hw version: %#lx\n",
952 version & MCHP_I2SMCC_VERSION_MASK);
953
954 return 0;
955}
956
957static int mchp_i2s_mcc_remove(struct platform_device *pdev)
958{
959 struct mchp_i2s_mcc_dev *dev = platform_get_drvdata(pdev);
960
961 clk_disable_unprepare(dev->pclk);
962
963 return 0;
964}
965
966static struct platform_driver mchp_i2s_mcc_driver = {
967 .driver = {
968 .name = "mchp_i2s_mcc",
969 .of_match_table = of_match_ptr(mchp_i2s_mcc_dt_ids),
970 },
971 .probe = mchp_i2s_mcc_probe,
972 .remove = mchp_i2s_mcc_remove,
973};
974module_platform_driver(mchp_i2s_mcc_driver);
975
976MODULE_DESCRIPTION("Microchip I2S Multi-Channel Controller driver");
977MODULE_AUTHOR("Codrin Ciubotariu <codrin.ciubotariu@microchip.com>");
978MODULE_LICENSE("GPL v2");