1 // SPDX-License-Identifier: GPL-2.0-only
3 * ALSA SoC McASP Audio Layer for TI DAVINCI processor
5 * Multi-channel Audio Serial Port Driver
7 * Author: Nirmal Pandey <n-pandey@ti.com>,
8 * Suresh Rajashekara <suresh.r@ti.com>
9 * Steve Chen <schen@.mvista.com>
11 * Copyright: (C) 2009 MontaVista Software, Inc., <source@mvista.com>
12 * Copyright: (C) 2009 Texas Instruments, India
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
21 #include <linux/clk.h>
22 #include <linux/pm_runtime.h>
24 #include <linux/of_platform.h>
25 #include <linux/of_device.h>
26 #include <linux/platform_data/davinci_asp.h>
27 #include <linux/math64.h>
28 #include <linux/bitmap.h>
29 #include <linux/gpio/driver.h>
31 #include <sound/asoundef.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/initval.h>
36 #include <sound/soc.h>
37 #include <sound/dmaengine_pcm.h>
42 #include "davinci-mcasp.h"
44 #define MCASP_MAX_AFIFO_DEPTH 64
47 static u32 context_regs[] = {
48 DAVINCI_MCASP_TXFMCTL_REG,
49 DAVINCI_MCASP_RXFMCTL_REG,
50 DAVINCI_MCASP_TXFMT_REG,
51 DAVINCI_MCASP_RXFMT_REG,
52 DAVINCI_MCASP_ACLKXCTL_REG,
53 DAVINCI_MCASP_ACLKRCTL_REG,
54 DAVINCI_MCASP_AHCLKXCTL_REG,
55 DAVINCI_MCASP_AHCLKRCTL_REG,
56 DAVINCI_MCASP_PDIR_REG,
57 DAVINCI_MCASP_PFUNC_REG,
58 DAVINCI_MCASP_RXMASK_REG,
59 DAVINCI_MCASP_TXMASK_REG,
60 DAVINCI_MCASP_RXTDM_REG,
61 DAVINCI_MCASP_TXTDM_REG,
64 struct davinci_mcasp_context {
65 u32 config_regs[ARRAY_SIZE(context_regs)];
66 u32 afifo_regs[2]; /* for read/write fifo control registers */
67 u32 *xrsr_regs; /* for serializer configuration */
72 struct davinci_mcasp_ruledata {
73 struct davinci_mcasp *mcasp;
77 struct davinci_mcasp {
78 struct snd_dmaengine_dai_dma_data dma_data[2];
79 struct davinci_mcasp_pdata *pdata;
83 struct snd_pcm_substream *substreams[2];
86 /* Audio can not be enabled due to missing parameter(s) */
87 bool missing_audio_param;
89 /* McASP specific data */
106 unsigned long pdir; /* Pin direction bitfield */
108 /* McASP FIFO related */
114 /* Used for comstraint setting on the second stream */
116 int max_format_width;
117 u8 active_serializers[2];
119 #ifdef CONFIG_GPIOLIB
120 struct gpio_chip gpio_chip;
124 struct davinci_mcasp_context context;
127 struct davinci_mcasp_ruledata ruledata[2];
128 struct snd_pcm_hw_constraint_list chconstr[2];
131 static inline void mcasp_set_bits(struct davinci_mcasp *mcasp, u32 offset,
134 void __iomem *reg = mcasp->base + offset;
135 __raw_writel(__raw_readl(reg) | val, reg);
138 static inline void mcasp_clr_bits(struct davinci_mcasp *mcasp, u32 offset,
141 void __iomem *reg = mcasp->base + offset;
142 __raw_writel((__raw_readl(reg) & ~(val)), reg);
145 static inline void mcasp_mod_bits(struct davinci_mcasp *mcasp, u32 offset,
148 void __iomem *reg = mcasp->base + offset;
149 __raw_writel((__raw_readl(reg) & ~mask) | val, reg);
152 static inline void mcasp_set_reg(struct davinci_mcasp *mcasp, u32 offset,
155 __raw_writel(val, mcasp->base + offset);
158 static inline u32 mcasp_get_reg(struct davinci_mcasp *mcasp, u32 offset)
160 return (u32)__raw_readl(mcasp->base + offset);
163 static void mcasp_set_ctl_reg(struct davinci_mcasp *mcasp, u32 ctl_reg, u32 val)
167 mcasp_set_bits(mcasp, ctl_reg, val);
169 /* programming GBLCTL needs to read back from GBLCTL and verfiy */
170 /* loop count is to avoid the lock-up */
171 for (i = 0; i < 1000; i++) {
172 if ((mcasp_get_reg(mcasp, ctl_reg) & val) == val)
176 if (i == 1000 && ((mcasp_get_reg(mcasp, ctl_reg) & val) != val))
177 printk(KERN_ERR "GBLCTL write error\n");
180 static bool mcasp_is_synchronous(struct davinci_mcasp *mcasp)
182 u32 rxfmctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXFMCTL_REG);
183 u32 aclkxctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_ACLKXCTL_REG);
185 return !(aclkxctl & TX_ASYNC) && rxfmctl & AFSRE;
188 static inline void mcasp_set_clk_pdir(struct davinci_mcasp *mcasp, bool enable)
190 u32 bit = PIN_BIT_AMUTE;
192 for_each_set_bit_from(bit, &mcasp->pdir, PIN_BIT_AFSR + 1) {
194 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
196 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
200 static inline void mcasp_set_axr_pdir(struct davinci_mcasp *mcasp, bool enable)
204 for_each_set_bit(bit, &mcasp->pdir, PIN_BIT_AMUTE) {
206 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
208 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
212 static void mcasp_start_rx(struct davinci_mcasp *mcasp)
214 if (mcasp->rxnumevt) { /* enable FIFO */
215 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
217 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
218 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
222 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST);
223 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST);
225 * When ASYNC == 0 the transmit and receive sections operate
226 * synchronously from the transmit clock and frame sync. We need to make
227 * sure that the TX signlas are enabled when starting reception.
229 if (mcasp_is_synchronous(mcasp)) {
230 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
231 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
232 mcasp_set_clk_pdir(mcasp, true);
235 /* Activate serializer(s) */
236 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
237 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR);
238 /* Release RX state machine */
239 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
240 /* Release Frame Sync generator */
241 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
242 if (mcasp_is_synchronous(mcasp))
243 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
245 /* enable receive IRQs */
246 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
247 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
250 static void mcasp_start_tx(struct davinci_mcasp *mcasp)
254 if (mcasp->txnumevt) { /* enable FIFO */
255 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
257 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
258 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
262 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
263 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
264 mcasp_set_clk_pdir(mcasp, true);
266 /* Activate serializer(s) */
267 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
268 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR);
270 /* wait for XDATA to be cleared */
272 while ((mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG) & XRDATA) &&
276 mcasp_set_axr_pdir(mcasp, true);
278 /* Release TX state machine */
279 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSMRST);
280 /* Release Frame Sync generator */
281 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
283 /* enable transmit IRQs */
284 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
285 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
288 static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream)
292 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
293 mcasp_start_tx(mcasp);
295 mcasp_start_rx(mcasp);
298 static void mcasp_stop_rx(struct davinci_mcasp *mcasp)
300 /* disable IRQ sources */
301 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
302 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
305 * In synchronous mode stop the TX clocks if no other stream is
308 if (mcasp_is_synchronous(mcasp) && !mcasp->streams) {
309 mcasp_set_clk_pdir(mcasp, false);
310 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, 0);
313 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, 0);
314 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
316 if (mcasp->rxnumevt) { /* disable FIFO */
317 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
319 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
323 static void mcasp_stop_tx(struct davinci_mcasp *mcasp)
327 /* disable IRQ sources */
328 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
329 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
332 * In synchronous mode keep TX clocks running if the capture stream is
335 if (mcasp_is_synchronous(mcasp) && mcasp->streams)
336 val = TXHCLKRST | TXCLKRST | TXFSRST;
338 mcasp_set_clk_pdir(mcasp, false);
341 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, val);
342 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
344 if (mcasp->txnumevt) { /* disable FIFO */
345 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
347 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
350 mcasp_set_axr_pdir(mcasp, false);
353 static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream)
357 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
358 mcasp_stop_tx(mcasp);
360 mcasp_stop_rx(mcasp);
363 static irqreturn_t davinci_mcasp_tx_irq_handler(int irq, void *data)
365 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
366 struct snd_pcm_substream *substream;
367 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK];
368 u32 handled_mask = 0;
371 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG);
372 if (stat & XUNDRN & irq_mask) {
373 dev_warn(mcasp->dev, "Transmit buffer underflow\n");
374 handled_mask |= XUNDRN;
376 substream = mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK];
378 snd_pcm_stop_xrun(substream);
382 dev_warn(mcasp->dev, "unhandled tx event. txstat: 0x%08x\n",
386 handled_mask |= XRERR;
388 /* Ack the handled event only */
389 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, handled_mask);
391 return IRQ_RETVAL(handled_mask);
394 static irqreturn_t davinci_mcasp_rx_irq_handler(int irq, void *data)
396 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
397 struct snd_pcm_substream *substream;
398 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE];
399 u32 handled_mask = 0;
402 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG);
403 if (stat & ROVRN & irq_mask) {
404 dev_warn(mcasp->dev, "Receive buffer overflow\n");
405 handled_mask |= ROVRN;
407 substream = mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE];
409 snd_pcm_stop_xrun(substream);
413 dev_warn(mcasp->dev, "unhandled rx event. rxstat: 0x%08x\n",
417 handled_mask |= XRERR;
419 /* Ack the handled event only */
420 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, handled_mask);
422 return IRQ_RETVAL(handled_mask);
425 static irqreturn_t davinci_mcasp_common_irq_handler(int irq, void *data)
427 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
428 irqreturn_t ret = IRQ_NONE;
430 if (mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK])
431 ret = davinci_mcasp_tx_irq_handler(irq, data);
433 if (mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE])
434 ret |= davinci_mcasp_rx_irq_handler(irq, data);
439 static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
442 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
451 pm_runtime_get_sync(mcasp->dev);
452 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
453 case SND_SOC_DAIFMT_DSP_A:
454 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
455 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
456 /* 1st data bit occur one ACLK cycle after the frame sync */
459 case SND_SOC_DAIFMT_DSP_B:
460 case SND_SOC_DAIFMT_AC97:
461 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
462 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
463 /* No delay after FS */
466 case SND_SOC_DAIFMT_I2S:
467 /* configure a full-word SYNC pulse (LRCLK) */
468 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
469 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
470 /* 1st data bit occur one ACLK cycle after the frame sync */
472 /* FS need to be inverted */
475 case SND_SOC_DAIFMT_RIGHT_J:
476 case SND_SOC_DAIFMT_LEFT_J:
477 /* configure a full-word SYNC pulse (LRCLK) */
478 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
479 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
480 /* No delay after FS */
488 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, FSXDLY(data_delay),
490 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, FSRDLY(data_delay),
493 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
494 case SND_SOC_DAIFMT_CBS_CFS:
495 /* codec is clock and frame slave */
496 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
497 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
499 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
500 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
503 set_bit(PIN_BIT_ACLKX, &mcasp->pdir);
504 set_bit(PIN_BIT_ACLKR, &mcasp->pdir);
506 set_bit(PIN_BIT_AFSX, &mcasp->pdir);
507 set_bit(PIN_BIT_AFSR, &mcasp->pdir);
509 mcasp->bclk_master = 1;
511 case SND_SOC_DAIFMT_CBS_CFM:
512 /* codec is clock slave and frame master */
513 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
514 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
516 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
517 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
520 set_bit(PIN_BIT_ACLKX, &mcasp->pdir);
521 set_bit(PIN_BIT_ACLKR, &mcasp->pdir);
523 clear_bit(PIN_BIT_AFSX, &mcasp->pdir);
524 clear_bit(PIN_BIT_AFSR, &mcasp->pdir);
526 mcasp->bclk_master = 1;
528 case SND_SOC_DAIFMT_CBM_CFS:
529 /* codec is clock master and frame slave */
530 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
531 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
533 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
534 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
537 clear_bit(PIN_BIT_ACLKX, &mcasp->pdir);
538 clear_bit(PIN_BIT_ACLKR, &mcasp->pdir);
540 set_bit(PIN_BIT_AFSX, &mcasp->pdir);
541 set_bit(PIN_BIT_AFSR, &mcasp->pdir);
543 mcasp->bclk_master = 0;
545 case SND_SOC_DAIFMT_CBM_CFM:
546 /* codec is clock and frame master */
547 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
548 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
550 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
551 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
554 clear_bit(PIN_BIT_ACLKX, &mcasp->pdir);
555 clear_bit(PIN_BIT_ACLKR, &mcasp->pdir);
557 clear_bit(PIN_BIT_AFSX, &mcasp->pdir);
558 clear_bit(PIN_BIT_AFSR, &mcasp->pdir);
560 mcasp->bclk_master = 0;
567 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
568 case SND_SOC_DAIFMT_IB_NF:
569 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
570 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
571 fs_pol_rising = true;
573 case SND_SOC_DAIFMT_NB_IF:
574 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
575 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
576 fs_pol_rising = false;
578 case SND_SOC_DAIFMT_IB_IF:
579 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
580 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
581 fs_pol_rising = false;
583 case SND_SOC_DAIFMT_NB_NF:
584 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
585 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
586 fs_pol_rising = true;
594 fs_pol_rising = !fs_pol_rising;
597 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
598 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
600 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
601 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
604 mcasp->dai_fmt = fmt;
606 pm_runtime_put(mcasp->dev);
610 static int __davinci_mcasp_set_clkdiv(struct davinci_mcasp *mcasp, int div_id,
611 int div, bool explicit)
613 pm_runtime_get_sync(mcasp->dev);
615 case MCASP_CLKDIV_AUXCLK: /* MCLK divider */
616 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
617 AHCLKXDIV(div - 1), AHCLKXDIV_MASK);
618 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
619 AHCLKRDIV(div - 1), AHCLKRDIV_MASK);
622 case MCASP_CLKDIV_BCLK: /* BCLK divider */
623 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG,
624 ACLKXDIV(div - 1), ACLKXDIV_MASK);
625 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG,
626 ACLKRDIV(div - 1), ACLKRDIV_MASK);
628 mcasp->bclk_div = div;
631 case MCASP_CLKDIV_BCLK_FS_RATIO:
633 * BCLK/LRCLK ratio descries how many bit-clock cycles
634 * fit into one frame. The clock ratio is given for a
635 * full period of data (for I2S format both left and
636 * right channels), so it has to be divided by number
637 * of tdm-slots (for I2S - divided by 2).
638 * Instead of storing this ratio, we calculate a new
639 * tdm_slot width by dividing the ratio by the
640 * number of configured tdm slots.
642 mcasp->slot_width = div / mcasp->tdm_slots;
643 if (div % mcasp->tdm_slots)
645 "%s(): BCLK/LRCLK %d is not divisible by %d tdm slots",
646 __func__, div, mcasp->tdm_slots);
653 pm_runtime_put(mcasp->dev);
657 static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
660 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
662 return __davinci_mcasp_set_clkdiv(mcasp, div_id, div, 1);
665 static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id,
666 unsigned int freq, int dir)
668 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
670 pm_runtime_get_sync(mcasp->dev);
672 if (dir == SND_SOC_CLOCK_IN) {
674 case MCASP_CLK_HCLK_AHCLK:
675 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
677 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
679 clear_bit(PIN_BIT_AHCLKX, &mcasp->pdir);
681 case MCASP_CLK_HCLK_AUXCLK:
682 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
684 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
686 set_bit(PIN_BIT_AHCLKX, &mcasp->pdir);
689 dev_err(mcasp->dev, "Invalid clk id: %d\n", clk_id);
693 /* Select AUXCLK as HCLK */
694 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
695 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
696 set_bit(PIN_BIT_AHCLKX, &mcasp->pdir);
699 * When AHCLK X/R is selected to be output it means that the HCLK is
700 * the same clock - coming via AUXCLK.
702 mcasp->sysclk_freq = freq;
704 pm_runtime_put(mcasp->dev);
708 /* All serializers must have equal number of channels */
709 static int davinci_mcasp_ch_constraint(struct davinci_mcasp *mcasp, int stream,
712 struct snd_pcm_hw_constraint_list *cl = &mcasp->chconstr[stream];
713 unsigned int *list = (unsigned int *) cl->list;
714 int slots = mcasp->tdm_slots;
717 if (mcasp->tdm_mask[stream])
718 slots = hweight32(mcasp->tdm_mask[stream]);
720 for (i = 1; i <= slots; i++)
723 for (i = 2; i <= serializers; i++)
724 list[count++] = i*slots;
731 static int davinci_mcasp_set_ch_constraints(struct davinci_mcasp *mcasp)
733 int rx_serializers = 0, tx_serializers = 0, ret, i;
735 for (i = 0; i < mcasp->num_serializer; i++)
736 if (mcasp->serial_dir[i] == TX_MODE)
738 else if (mcasp->serial_dir[i] == RX_MODE)
741 ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_PLAYBACK,
746 ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_CAPTURE,
753 static int davinci_mcasp_set_tdm_slot(struct snd_soc_dai *dai,
754 unsigned int tx_mask,
755 unsigned int rx_mask,
756 int slots, int slot_width)
758 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
761 "%s() tx_mask 0x%08x rx_mask 0x%08x slots %d width %d\n",
762 __func__, tx_mask, rx_mask, slots, slot_width);
764 if (tx_mask >= (1<<slots) || rx_mask >= (1<<slots)) {
766 "Bad tdm mask tx: 0x%08x rx: 0x%08x slots %d\n",
767 tx_mask, rx_mask, slots);
772 (slot_width < 8 || slot_width > 32 || slot_width % 4 != 0)) {
773 dev_err(mcasp->dev, "%s: Unsupported slot_width %d\n",
774 __func__, slot_width);
778 mcasp->tdm_slots = slots;
779 mcasp->tdm_mask[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask;
780 mcasp->tdm_mask[SNDRV_PCM_STREAM_CAPTURE] = rx_mask;
781 mcasp->slot_width = slot_width;
783 return davinci_mcasp_set_ch_constraints(mcasp);
786 static int davinci_config_channel_size(struct davinci_mcasp *mcasp,
790 u32 tx_rotate, rx_rotate, slot_width;
791 u32 mask = (1ULL << sample_width) - 1;
793 if (mcasp->slot_width)
794 slot_width = mcasp->slot_width;
795 else if (mcasp->max_format_width)
796 slot_width = mcasp->max_format_width;
798 slot_width = sample_width;
801 * right aligned formats: rotate w/ slot_width
802 * left aligned formats: rotate w/ sample_width
805 * right aligned formats: no rotation needed
806 * left aligned formats: rotate w/ (slot_width - sample_width)
808 if ((mcasp->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
809 SND_SOC_DAIFMT_RIGHT_J) {
810 tx_rotate = (slot_width / 4) & 0x7;
813 tx_rotate = (sample_width / 4) & 0x7;
814 rx_rotate = (slot_width - sample_width) / 4;
817 /* mapping of the XSSZ bit-field as described in the datasheet */
818 fmt = (slot_width >> 1) - 1;
820 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
821 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXSSZ(fmt),
823 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXSSZ(fmt),
825 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(tx_rotate),
827 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXROT(rx_rotate),
829 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXMASK_REG, mask);
832 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXMASK_REG, mask);
837 static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream,
838 int period_words, int channels)
840 struct snd_dmaengine_dai_dma_data *dma_data = &mcasp->dma_data[stream];
844 u8 slots = mcasp->tdm_slots;
845 u8 max_active_serializers = (channels + slots - 1) / slots;
846 u8 max_rx_serializers, max_tx_serializers;
847 int active_serializers, numevt;
849 /* Default configuration */
850 if (mcasp->version < MCASP_VERSION_3)
851 mcasp_set_bits(mcasp, DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT);
853 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
854 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
855 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
856 max_tx_serializers = max_active_serializers;
858 mcasp->active_serializers[SNDRV_PCM_STREAM_CAPTURE];
860 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
861 mcasp_clr_bits(mcasp, DAVINCI_MCASP_REVTCTL_REG, RXDATADMADIS);
863 mcasp->active_serializers[SNDRV_PCM_STREAM_PLAYBACK];
864 max_rx_serializers = max_active_serializers;
867 for (i = 0; i < mcasp->num_serializer; i++) {
868 mcasp_set_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
869 mcasp->serial_dir[i]);
870 if (mcasp->serial_dir[i] == TX_MODE &&
871 tx_ser < max_tx_serializers) {
872 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
873 mcasp->dismod, DISMOD_MASK);
874 set_bit(PIN_BIT_AXR(i), &mcasp->pdir);
876 } else if (mcasp->serial_dir[i] == RX_MODE &&
877 rx_ser < max_rx_serializers) {
878 clear_bit(PIN_BIT_AXR(i), &mcasp->pdir);
881 /* Inactive or unused pin, set it to inactive */
882 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
883 SRMOD_INACTIVE, SRMOD_MASK);
884 /* If unused, set DISMOD for the pin */
885 if (mcasp->serial_dir[i] != INACTIVE_MODE)
886 mcasp_mod_bits(mcasp,
887 DAVINCI_MCASP_XRSRCTL_REG(i),
888 mcasp->dismod, DISMOD_MASK);
889 clear_bit(PIN_BIT_AXR(i), &mcasp->pdir);
893 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
894 active_serializers = tx_ser;
895 numevt = mcasp->txnumevt;
896 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
898 active_serializers = rx_ser;
899 numevt = mcasp->rxnumevt;
900 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
903 if (active_serializers < max_active_serializers) {
904 dev_warn(mcasp->dev, "stream has more channels (%d) than are "
905 "enabled in mcasp (%d)\n", channels,
906 active_serializers * slots);
910 /* AFIFO is not in use */
912 /* Configure the burst size for platform drivers */
913 if (active_serializers > 1) {
915 * If more than one serializers are in use we have one
916 * DMA request to provide data for all serializers.
917 * For example if three serializers are enabled the DMA
918 * need to transfer three words per DMA request.
920 dma_data->maxburst = active_serializers;
922 dma_data->maxburst = 0;
928 if (period_words % active_serializers) {
929 dev_err(mcasp->dev, "Invalid combination of period words and "
930 "active serializers: %d, %d\n", period_words,
936 * Calculate the optimal AFIFO depth for platform side:
937 * The number of words for numevt need to be in steps of active
940 numevt = (numevt / active_serializers) * active_serializers;
942 while (period_words % numevt && numevt > 0)
943 numevt -= active_serializers;
945 numevt = active_serializers;
947 mcasp_mod_bits(mcasp, reg, active_serializers, NUMDMA_MASK);
948 mcasp_mod_bits(mcasp, reg, NUMEVT(numevt), NUMEVT_MASK);
950 /* Configure the burst size for platform drivers */
953 dma_data->maxburst = numevt;
956 mcasp->active_serializers[stream] = active_serializers;
961 static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream,
966 int active_serializers;
970 total_slots = mcasp->tdm_slots;
973 * If more than one serializer is needed, then use them with
974 * all the specified tdm_slots. Otherwise, one serializer can
975 * cope with the transaction using just as many slots as there
976 * are channels in the stream.
978 if (mcasp->tdm_mask[stream]) {
979 active_slots = hweight32(mcasp->tdm_mask[stream]);
980 active_serializers = (channels + active_slots - 1) /
982 if (active_serializers == 1)
983 active_slots = channels;
984 for (i = 0; i < total_slots; i++) {
985 if ((1 << i) & mcasp->tdm_mask[stream]) {
987 if (--active_slots <= 0)
992 active_serializers = (channels + total_slots - 1) / total_slots;
993 if (active_serializers == 1)
994 active_slots = channels;
996 active_slots = total_slots;
998 for (i = 0; i < active_slots; i++)
1002 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC);
1004 if (!mcasp->dat_port)
1007 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1008 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask);
1009 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD);
1010 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
1011 FSXMOD(total_slots), FSXMOD(0x1FF));
1012 } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
1013 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask);
1014 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD);
1015 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG,
1016 FSRMOD(total_slots), FSRMOD(0x1FF));
1018 * If McASP is set to be TX/RX synchronous and the playback is
1019 * not running already we need to configure the TX slots in
1020 * order to have correct FSX on the bus
1022 if (mcasp_is_synchronous(mcasp) && !mcasp->channels)
1023 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
1024 FSXMOD(total_slots), FSXMOD(0x1FF));
1031 static int mcasp_dit_hw_param(struct davinci_mcasp *mcasp,
1035 u8 *cs_bytes = (u8*) &cs_value;
1037 /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
1039 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(6) | TXSSZ(15));
1041 /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
1042 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE | FSXMOD(0x180));
1044 /* Set the TX tdm : for all the slots */
1045 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF);
1047 /* Set the TX clock controls : div = 1 and internal */
1048 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE | TX_ASYNC);
1050 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
1052 /* Only 44100 and 48000 are valid, both have the same setting */
1053 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3));
1055 /* Enable the DIT */
1056 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXDITCTL_REG, DITEN);
1058 /* Set S/PDIF channel status bits */
1059 cs_bytes[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
1060 cs_bytes[1] = IEC958_AES1_CON_PCM_CODER;
1064 cs_bytes[3] |= IEC958_AES3_CON_FS_22050;
1067 cs_bytes[3] |= IEC958_AES3_CON_FS_24000;
1070 cs_bytes[3] |= IEC958_AES3_CON_FS_32000;
1073 cs_bytes[3] |= IEC958_AES3_CON_FS_44100;
1076 cs_bytes[3] |= IEC958_AES3_CON_FS_48000;
1079 cs_bytes[3] |= IEC958_AES3_CON_FS_88200;
1082 cs_bytes[3] |= IEC958_AES3_CON_FS_96000;
1085 cs_bytes[3] |= IEC958_AES3_CON_FS_176400;
1088 cs_bytes[3] |= IEC958_AES3_CON_FS_192000;
1091 printk(KERN_WARNING "unsupported sampling rate: %d\n", rate);
1095 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRA_REG, cs_value);
1096 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRB_REG, cs_value);
1101 static int davinci_mcasp_calc_clk_div(struct davinci_mcasp *mcasp,
1102 unsigned int sysclk_freq,
1103 unsigned int bclk_freq, bool set)
1105 u32 reg = mcasp_get_reg(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG);
1106 int div = sysclk_freq / bclk_freq;
1107 int rem = sysclk_freq % bclk_freq;
1111 if (div > (ACLKXDIV_MASK + 1)) {
1112 if (reg & AHCLKXE) {
1113 aux_div = div / (ACLKXDIV_MASK + 1);
1114 if (div % (ACLKXDIV_MASK + 1))
1117 sysclk_freq /= aux_div;
1118 div = sysclk_freq / bclk_freq;
1119 rem = sysclk_freq % bclk_freq;
1121 dev_warn(mcasp->dev, "Too fast reference clock (%u)\n",
1128 ((sysclk_freq / div) - bclk_freq) >
1129 (bclk_freq - (sysclk_freq / (div+1)))) {
1131 rem = rem - bclk_freq;
1134 error_ppm = (div*1000000 + (int)div64_long(1000000LL*rem,
1135 (int)bclk_freq)) / div - 1000000;
1139 dev_info(mcasp->dev, "Sample-rate is off by %d PPM\n",
1142 __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_BCLK, div, 0);
1144 __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_AUXCLK,
1151 static inline u32 davinci_mcasp_tx_delay(struct davinci_mcasp *mcasp)
1153 if (!mcasp->txnumevt)
1156 return mcasp_get_reg(mcasp, mcasp->fifo_base + MCASP_WFIFOSTS_OFFSET);
1159 static inline u32 davinci_mcasp_rx_delay(struct davinci_mcasp *mcasp)
1161 if (!mcasp->rxnumevt)
1164 return mcasp_get_reg(mcasp, mcasp->fifo_base + MCASP_RFIFOSTS_OFFSET);
1167 static snd_pcm_sframes_t davinci_mcasp_delay(
1168 struct snd_pcm_substream *substream,
1169 struct snd_soc_dai *cpu_dai)
1171 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1174 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1175 fifo_use = davinci_mcasp_tx_delay(mcasp);
1177 fifo_use = davinci_mcasp_rx_delay(mcasp);
1180 * Divide the used locations with the channel count to get the
1181 * FIFO usage in samples (don't care about partial samples in the
1184 return fifo_use / substream->runtime->channels;
1187 static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
1188 struct snd_pcm_hw_params *params,
1189 struct snd_soc_dai *cpu_dai)
1191 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1193 int channels = params_channels(params);
1194 int period_size = params_period_size(params);
1197 switch (params_format(params)) {
1198 case SNDRV_PCM_FORMAT_U8:
1199 case SNDRV_PCM_FORMAT_S8:
1203 case SNDRV_PCM_FORMAT_U16_LE:
1204 case SNDRV_PCM_FORMAT_S16_LE:
1208 case SNDRV_PCM_FORMAT_U24_3LE:
1209 case SNDRV_PCM_FORMAT_S24_3LE:
1213 case SNDRV_PCM_FORMAT_U24_LE:
1214 case SNDRV_PCM_FORMAT_S24_LE:
1218 case SNDRV_PCM_FORMAT_U32_LE:
1219 case SNDRV_PCM_FORMAT_S32_LE:
1224 printk(KERN_WARNING "davinci-mcasp: unsupported PCM format");
1228 ret = davinci_mcasp_set_dai_fmt(cpu_dai, mcasp->dai_fmt);
1233 * If mcasp is BCLK master, and a BCLK divider was not provided by
1234 * the machine driver, we need to calculate the ratio.
1236 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
1237 int slots = mcasp->tdm_slots;
1238 int rate = params_rate(params);
1239 int sbits = params_width(params);
1241 if (mcasp->slot_width)
1242 sbits = mcasp->slot_width;
1244 davinci_mcasp_calc_clk_div(mcasp, mcasp->sysclk_freq,
1245 rate * sbits * slots, true);
1248 ret = mcasp_common_hw_param(mcasp, substream->stream,
1249 period_size * channels, channels);
1253 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1254 ret = mcasp_dit_hw_param(mcasp, params_rate(params));
1256 ret = mcasp_i2s_hw_param(mcasp, substream->stream,
1262 davinci_config_channel_size(mcasp, word_length);
1264 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
1265 mcasp->channels = channels;
1266 if (!mcasp->max_format_width)
1267 mcasp->max_format_width = word_length;
1273 static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
1274 int cmd, struct snd_soc_dai *cpu_dai)
1276 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1280 case SNDRV_PCM_TRIGGER_RESUME:
1281 case SNDRV_PCM_TRIGGER_START:
1282 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1283 davinci_mcasp_start(mcasp, substream->stream);
1285 case SNDRV_PCM_TRIGGER_SUSPEND:
1286 case SNDRV_PCM_TRIGGER_STOP:
1287 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1288 davinci_mcasp_stop(mcasp, substream->stream);
1298 static int davinci_mcasp_hw_rule_slot_width(struct snd_pcm_hw_params *params,
1299 struct snd_pcm_hw_rule *rule)
1301 struct davinci_mcasp_ruledata *rd = rule->private;
1302 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1303 struct snd_mask nfmt;
1306 snd_mask_none(&nfmt);
1307 slot_width = rd->mcasp->slot_width;
1309 for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
1310 if (snd_mask_test(fmt, i)) {
1311 if (snd_pcm_format_width(i) <= slot_width) {
1312 snd_mask_set(&nfmt, i);
1317 return snd_mask_refine(fmt, &nfmt);
1320 static int davinci_mcasp_hw_rule_format_width(struct snd_pcm_hw_params *params,
1321 struct snd_pcm_hw_rule *rule)
1323 struct davinci_mcasp_ruledata *rd = rule->private;
1324 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1325 struct snd_mask nfmt;
1326 int i, format_width;
1328 snd_mask_none(&nfmt);
1329 format_width = rd->mcasp->max_format_width;
1331 for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
1332 if (snd_mask_test(fmt, i)) {
1333 if (snd_pcm_format_width(i) == format_width) {
1334 snd_mask_set(&nfmt, i);
1339 return snd_mask_refine(fmt, &nfmt);
1342 static const unsigned int davinci_mcasp_dai_rates[] = {
1343 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
1344 88200, 96000, 176400, 192000,
1347 #define DAVINCI_MAX_RATE_ERROR_PPM 1000
1349 static int davinci_mcasp_hw_rule_rate(struct snd_pcm_hw_params *params,
1350 struct snd_pcm_hw_rule *rule)
1352 struct davinci_mcasp_ruledata *rd = rule->private;
1353 struct snd_interval *ri =
1354 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1355 int sbits = params_width(params);
1356 int slots = rd->mcasp->tdm_slots;
1357 struct snd_interval range;
1360 if (rd->mcasp->slot_width)
1361 sbits = rd->mcasp->slot_width;
1363 snd_interval_any(&range);
1366 for (i = 0; i < ARRAY_SIZE(davinci_mcasp_dai_rates); i++) {
1367 if (snd_interval_test(ri, davinci_mcasp_dai_rates[i])) {
1368 uint bclk_freq = sbits * slots *
1369 davinci_mcasp_dai_rates[i];
1370 unsigned int sysclk_freq;
1373 if (rd->mcasp->auxclk_fs_ratio)
1374 sysclk_freq = davinci_mcasp_dai_rates[i] *
1375 rd->mcasp->auxclk_fs_ratio;
1377 sysclk_freq = rd->mcasp->sysclk_freq;
1379 ppm = davinci_mcasp_calc_clk_div(rd->mcasp, sysclk_freq,
1381 if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
1383 range.min = davinci_mcasp_dai_rates[i];
1386 range.max = davinci_mcasp_dai_rates[i];
1391 dev_dbg(rd->mcasp->dev,
1392 "Frequencies %d-%d -> %d-%d for %d sbits and %d tdm slots\n",
1393 ri->min, ri->max, range.min, range.max, sbits, slots);
1395 return snd_interval_refine(hw_param_interval(params, rule->var),
1399 static int davinci_mcasp_hw_rule_format(struct snd_pcm_hw_params *params,
1400 struct snd_pcm_hw_rule *rule)
1402 struct davinci_mcasp_ruledata *rd = rule->private;
1403 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1404 struct snd_mask nfmt;
1405 int rate = params_rate(params);
1406 int slots = rd->mcasp->tdm_slots;
1409 snd_mask_none(&nfmt);
1411 for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
1412 if (snd_mask_test(fmt, i)) {
1413 uint sbits = snd_pcm_format_width(i);
1414 unsigned int sysclk_freq;
1417 if (rd->mcasp->auxclk_fs_ratio)
1418 sysclk_freq = rate *
1419 rd->mcasp->auxclk_fs_ratio;
1421 sysclk_freq = rd->mcasp->sysclk_freq;
1423 if (rd->mcasp->slot_width)
1424 sbits = rd->mcasp->slot_width;
1426 ppm = davinci_mcasp_calc_clk_div(rd->mcasp, sysclk_freq,
1427 sbits * slots * rate,
1429 if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
1430 snd_mask_set(&nfmt, i);
1435 dev_dbg(rd->mcasp->dev,
1436 "%d possible sample format for %d Hz and %d tdm slots\n",
1437 count, rate, slots);
1439 return snd_mask_refine(fmt, &nfmt);
1442 static int davinci_mcasp_hw_rule_min_periodsize(
1443 struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1445 struct snd_interval *period_size = hw_param_interval(params,
1446 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1447 struct snd_interval frames;
1449 snd_interval_any(&frames);
1453 return snd_interval_refine(period_size, &frames);
1456 static int davinci_mcasp_startup(struct snd_pcm_substream *substream,
1457 struct snd_soc_dai *cpu_dai)
1459 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1460 struct davinci_mcasp_ruledata *ruledata =
1461 &mcasp->ruledata[substream->stream];
1462 u32 max_channels = 0;
1464 int tdm_slots = mcasp->tdm_slots;
1466 /* Do not allow more then one stream per direction */
1467 if (mcasp->substreams[substream->stream])
1470 mcasp->substreams[substream->stream] = substream;
1472 if (mcasp->tdm_mask[substream->stream])
1473 tdm_slots = hweight32(mcasp->tdm_mask[substream->stream]);
1475 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1479 * Limit the maximum allowed channels for the first stream:
1480 * number of serializers for the direction * tdm slots per serializer
1482 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1487 for (i = 0; i < mcasp->num_serializer; i++) {
1488 if (mcasp->serial_dir[i] == dir)
1491 ruledata->serializers = max_channels;
1492 ruledata->mcasp = mcasp;
1493 max_channels *= tdm_slots;
1495 * If the already active stream has less channels than the calculated
1496 * limit based on the seirializers * tdm_slots, and only one serializer
1497 * is in use we need to use that as a constraint for the second stream.
1498 * Otherwise (first stream or less allowed channels or more than one
1499 * serializer in use) we use the calculated constraint.
1501 if (mcasp->channels && mcasp->channels < max_channels &&
1502 ruledata->serializers == 1)
1503 max_channels = mcasp->channels;
1505 * But we can always allow channels upto the amount of
1506 * the available tdm_slots.
1508 if (max_channels < tdm_slots)
1509 max_channels = tdm_slots;
1511 snd_pcm_hw_constraint_minmax(substream->runtime,
1512 SNDRV_PCM_HW_PARAM_CHANNELS,
1515 snd_pcm_hw_constraint_list(substream->runtime,
1516 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1517 &mcasp->chconstr[substream->stream]);
1519 if (mcasp->max_format_width) {
1521 * Only allow formats which require same amount of bits on the
1522 * bus as the currently running stream
1524 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1525 SNDRV_PCM_HW_PARAM_FORMAT,
1526 davinci_mcasp_hw_rule_format_width,
1528 SNDRV_PCM_HW_PARAM_FORMAT, -1);
1532 else if (mcasp->slot_width) {
1533 /* Only allow formats require <= slot_width bits on the bus */
1534 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1535 SNDRV_PCM_HW_PARAM_FORMAT,
1536 davinci_mcasp_hw_rule_slot_width,
1538 SNDRV_PCM_HW_PARAM_FORMAT, -1);
1544 * If we rely on implicit BCLK divider setting we should
1545 * set constraints based on what we can provide.
1547 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
1548 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1549 SNDRV_PCM_HW_PARAM_RATE,
1550 davinci_mcasp_hw_rule_rate,
1552 SNDRV_PCM_HW_PARAM_FORMAT, -1);
1555 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1556 SNDRV_PCM_HW_PARAM_FORMAT,
1557 davinci_mcasp_hw_rule_format,
1559 SNDRV_PCM_HW_PARAM_RATE, -1);
1564 snd_pcm_hw_rule_add(substream->runtime, 0,
1565 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1566 davinci_mcasp_hw_rule_min_periodsize, NULL,
1567 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1572 static void davinci_mcasp_shutdown(struct snd_pcm_substream *substream,
1573 struct snd_soc_dai *cpu_dai)
1575 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1577 mcasp->substreams[substream->stream] = NULL;
1578 mcasp->active_serializers[substream->stream] = 0;
1580 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1583 if (!snd_soc_dai_active(cpu_dai)) {
1584 mcasp->channels = 0;
1585 mcasp->max_format_width = 0;
1589 static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
1590 .startup = davinci_mcasp_startup,
1591 .shutdown = davinci_mcasp_shutdown,
1592 .trigger = davinci_mcasp_trigger,
1593 .delay = davinci_mcasp_delay,
1594 .hw_params = davinci_mcasp_hw_params,
1595 .set_fmt = davinci_mcasp_set_dai_fmt,
1596 .set_clkdiv = davinci_mcasp_set_clkdiv,
1597 .set_sysclk = davinci_mcasp_set_sysclk,
1598 .set_tdm_slot = davinci_mcasp_set_tdm_slot,
1601 static int davinci_mcasp_dai_probe(struct snd_soc_dai *dai)
1603 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
1605 dai->playback_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
1606 dai->capture_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
1611 #define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
1613 #define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1614 SNDRV_PCM_FMTBIT_U8 | \
1615 SNDRV_PCM_FMTBIT_S16_LE | \
1616 SNDRV_PCM_FMTBIT_U16_LE | \
1617 SNDRV_PCM_FMTBIT_S24_LE | \
1618 SNDRV_PCM_FMTBIT_U24_LE | \
1619 SNDRV_PCM_FMTBIT_S24_3LE | \
1620 SNDRV_PCM_FMTBIT_U24_3LE | \
1621 SNDRV_PCM_FMTBIT_S32_LE | \
1622 SNDRV_PCM_FMTBIT_U32_LE)
1624 static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
1626 .name = "davinci-mcasp.0",
1627 .probe = davinci_mcasp_dai_probe,
1629 .stream_name = "IIS Playback",
1631 .channels_max = 32 * 16,
1632 .rates = DAVINCI_MCASP_RATES,
1633 .formats = DAVINCI_MCASP_PCM_FMTS,
1636 .stream_name = "IIS Capture",
1638 .channels_max = 32 * 16,
1639 .rates = DAVINCI_MCASP_RATES,
1640 .formats = DAVINCI_MCASP_PCM_FMTS,
1642 .ops = &davinci_mcasp_dai_ops,
1644 .symmetric_rate = 1,
1647 .name = "davinci-mcasp.1",
1648 .probe = davinci_mcasp_dai_probe,
1650 .stream_name = "DIT Playback",
1652 .channels_max = 384,
1653 .rates = DAVINCI_MCASP_RATES,
1654 .formats = DAVINCI_MCASP_PCM_FMTS,
1656 .ops = &davinci_mcasp_dai_ops,
1661 static const struct snd_soc_component_driver davinci_mcasp_component = {
1662 .name = "davinci-mcasp",
1665 /* Some HW specific values and defaults. The rest is filled in from DT. */
1666 static struct davinci_mcasp_pdata dm646x_mcasp_pdata = {
1667 .tx_dma_offset = 0x400,
1668 .rx_dma_offset = 0x400,
1669 .version = MCASP_VERSION_1,
1672 static struct davinci_mcasp_pdata da830_mcasp_pdata = {
1673 .tx_dma_offset = 0x2000,
1674 .rx_dma_offset = 0x2000,
1675 .version = MCASP_VERSION_2,
1678 static struct davinci_mcasp_pdata am33xx_mcasp_pdata = {
1681 .version = MCASP_VERSION_3,
1684 static struct davinci_mcasp_pdata dra7_mcasp_pdata = {
1685 /* The CFG port offset will be calculated if it is needed */
1688 .version = MCASP_VERSION_4,
1691 static const struct of_device_id mcasp_dt_ids[] = {
1693 .compatible = "ti,dm646x-mcasp-audio",
1694 .data = &dm646x_mcasp_pdata,
1697 .compatible = "ti,da830-mcasp-audio",
1698 .data = &da830_mcasp_pdata,
1701 .compatible = "ti,am33xx-mcasp-audio",
1702 .data = &am33xx_mcasp_pdata,
1705 .compatible = "ti,dra7-mcasp-audio",
1706 .data = &dra7_mcasp_pdata,
1710 MODULE_DEVICE_TABLE(of, mcasp_dt_ids);
1712 static int mcasp_reparent_fck(struct platform_device *pdev)
1714 struct device_node *node = pdev->dev.of_node;
1715 struct clk *gfclk, *parent_clk;
1716 const char *parent_name;
1722 parent_name = of_get_property(node, "fck_parent", NULL);
1726 dev_warn(&pdev->dev, "Update the bindings to use assigned-clocks!\n");
1728 gfclk = clk_get(&pdev->dev, "fck");
1729 if (IS_ERR(gfclk)) {
1730 dev_err(&pdev->dev, "failed to get fck\n");
1731 return PTR_ERR(gfclk);
1734 parent_clk = clk_get(NULL, parent_name);
1735 if (IS_ERR(parent_clk)) {
1736 dev_err(&pdev->dev, "failed to get parent clock\n");
1737 ret = PTR_ERR(parent_clk);
1741 ret = clk_set_parent(gfclk, parent_clk);
1743 dev_err(&pdev->dev, "failed to reparent fck\n");
1748 clk_put(parent_clk);
1754 static bool davinci_mcasp_have_gpiochip(struct davinci_mcasp *mcasp)
1756 #ifdef CONFIG_OF_GPIO
1757 if (mcasp->dev->of_node &&
1758 of_property_read_bool(mcasp->dev->of_node, "gpio-controller"))
1765 static int davinci_mcasp_get_config(struct davinci_mcasp *mcasp,
1766 struct platform_device *pdev)
1768 const struct of_device_id *match = of_match_device(mcasp_dt_ids, &pdev->dev);
1769 struct device_node *np = pdev->dev.of_node;
1770 struct davinci_mcasp_pdata *pdata = NULL;
1771 const u32 *of_serial_dir32;
1775 if (pdev->dev.platform_data) {
1776 pdata = pdev->dev.platform_data;
1777 pdata->dismod = DISMOD_LOW;
1780 pdata = devm_kmemdup(&pdev->dev, match->data, sizeof(*pdata),
1785 dev_err(&pdev->dev, "No compatible match found\n");
1789 if (of_property_read_u32(np, "op-mode", &val) == 0) {
1790 pdata->op_mode = val;
1792 mcasp->missing_audio_param = true;
1796 if (of_property_read_u32(np, "tdm-slots", &val) == 0) {
1797 if (val < 2 || val > 32) {
1798 dev_err(&pdev->dev, "tdm-slots must be in rage [2-32]\n");
1802 pdata->tdm_slots = val;
1803 } else if (pdata->op_mode == DAVINCI_MCASP_IIS_MODE) {
1804 mcasp->missing_audio_param = true;
1808 of_serial_dir32 = of_get_property(np, "serial-dir", &val);
1810 if (of_serial_dir32) {
1811 u8 *of_serial_dir = devm_kzalloc(&pdev->dev,
1812 (sizeof(*of_serial_dir) * val),
1817 for (i = 0; i < val; i++)
1818 of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]);
1820 pdata->num_serializer = val;
1821 pdata->serial_dir = of_serial_dir;
1823 mcasp->missing_audio_param = true;
1827 if (of_property_read_u32(np, "tx-num-evt", &val) == 0)
1828 pdata->txnumevt = val;
1830 if (of_property_read_u32(np, "rx-num-evt", &val) == 0)
1831 pdata->rxnumevt = val;
1833 if (of_property_read_u32(np, "auxclk-fs-ratio", &val) == 0)
1834 mcasp->auxclk_fs_ratio = val;
1836 if (of_property_read_u32(np, "dismod", &val) == 0) {
1837 if (val == 0 || val == 2 || val == 3) {
1838 pdata->dismod = DISMOD_VAL(val);
1840 dev_warn(&pdev->dev, "Invalid dismod value: %u\n", val);
1841 pdata->dismod = DISMOD_LOW;
1844 pdata->dismod = DISMOD_LOW;
1848 mcasp->pdata = pdata;
1850 if (mcasp->missing_audio_param) {
1851 if (davinci_mcasp_have_gpiochip(mcasp)) {
1852 dev_dbg(&pdev->dev, "Missing DT parameter(s) for audio\n");
1856 dev_err(&pdev->dev, "Insufficient DT parameter(s)\n");
1860 mcasp->op_mode = pdata->op_mode;
1861 /* sanity check for tdm slots parameter */
1862 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
1863 if (pdata->tdm_slots < 2) {
1864 dev_warn(&pdev->dev, "invalid tdm slots: %d\n",
1866 mcasp->tdm_slots = 2;
1867 } else if (pdata->tdm_slots > 32) {
1868 dev_warn(&pdev->dev, "invalid tdm slots: %d\n",
1870 mcasp->tdm_slots = 32;
1872 mcasp->tdm_slots = pdata->tdm_slots;
1876 mcasp->num_serializer = pdata->num_serializer;
1878 mcasp->context.xrsr_regs = devm_kcalloc(&pdev->dev,
1879 mcasp->num_serializer, sizeof(u32),
1881 if (!mcasp->context.xrsr_regs)
1884 mcasp->serial_dir = pdata->serial_dir;
1885 mcasp->version = pdata->version;
1886 mcasp->txnumevt = pdata->txnumevt;
1887 mcasp->rxnumevt = pdata->rxnumevt;
1888 mcasp->dismod = pdata->dismod;
1898 static const char *sdma_prefix = "ti,omap";
1900 static int davinci_mcasp_get_dma_type(struct davinci_mcasp *mcasp)
1902 struct dma_chan *chan;
1906 if (!mcasp->dev->of_node)
1909 tmp = mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK].filter_data;
1910 chan = dma_request_chan(mcasp->dev, tmp);
1912 if (PTR_ERR(chan) != -EPROBE_DEFER)
1914 "Can't verify DMA configuration (%ld)\n",
1916 return PTR_ERR(chan);
1918 if (WARN_ON(!chan->device || !chan->device->dev)) {
1919 dma_release_channel(chan);
1923 if (chan->device->dev->of_node)
1924 ret = of_property_read_string(chan->device->dev->of_node,
1925 "compatible", &tmp);
1927 dev_dbg(mcasp->dev, "DMA controller has no of-node\n");
1929 dma_release_channel(chan);
1933 dev_dbg(mcasp->dev, "DMA controller compatible = \"%s\"\n", tmp);
1934 if (!strncmp(tmp, sdma_prefix, strlen(sdma_prefix)))
1936 else if (strstr(tmp, "udmap"))
1942 static u32 davinci_mcasp_txdma_offset(struct davinci_mcasp_pdata *pdata)
1947 if (pdata->version != MCASP_VERSION_4)
1948 return pdata->tx_dma_offset;
1950 for (i = 0; i < pdata->num_serializer; i++) {
1951 if (pdata->serial_dir[i] == TX_MODE) {
1953 offset = DAVINCI_MCASP_TXBUF_REG(i);
1955 pr_err("%s: Only one serializer allowed!\n",
1965 static u32 davinci_mcasp_rxdma_offset(struct davinci_mcasp_pdata *pdata)
1970 if (pdata->version != MCASP_VERSION_4)
1971 return pdata->rx_dma_offset;
1973 for (i = 0; i < pdata->num_serializer; i++) {
1974 if (pdata->serial_dir[i] == RX_MODE) {
1976 offset = DAVINCI_MCASP_RXBUF_REG(i);
1978 pr_err("%s: Only one serializer allowed!\n",
1988 #ifdef CONFIG_GPIOLIB
1989 static int davinci_mcasp_gpio_request(struct gpio_chip *chip, unsigned offset)
1991 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
1993 if (mcasp->num_serializer && offset < mcasp->num_serializer &&
1994 mcasp->serial_dir[offset] != INACTIVE_MODE) {
1995 dev_err(mcasp->dev, "AXR%u pin is used for audio\n", offset);
1999 /* Do not change the PIN yet */
2001 return pm_runtime_get_sync(mcasp->dev);
2004 static void davinci_mcasp_gpio_free(struct gpio_chip *chip, unsigned offset)
2006 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2008 /* Set the direction to input */
2009 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
2011 /* Set the pin as McASP pin */
2012 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
2014 pm_runtime_put_sync(mcasp->dev);
2017 static int davinci_mcasp_gpio_direction_out(struct gpio_chip *chip,
2018 unsigned offset, int value)
2020 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2024 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2026 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2028 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PFUNC_REG);
2029 if (!(val & BIT(offset))) {
2030 /* Set the pin as GPIO pin */
2031 mcasp_set_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
2033 /* Set the direction to output */
2034 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
2040 static void davinci_mcasp_gpio_set(struct gpio_chip *chip, unsigned offset,
2043 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2046 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2048 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2051 static int davinci_mcasp_gpio_direction_in(struct gpio_chip *chip,
2054 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2057 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PFUNC_REG);
2058 if (!(val & BIT(offset))) {
2059 /* Set the direction to input */
2060 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
2062 /* Set the pin as GPIO pin */
2063 mcasp_set_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
2069 static int davinci_mcasp_gpio_get(struct gpio_chip *chip, unsigned offset)
2071 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2074 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PDSET_REG);
2075 if (val & BIT(offset))
2081 static int davinci_mcasp_gpio_get_direction(struct gpio_chip *chip,
2084 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2087 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PDIR_REG);
2088 if (val & BIT(offset))
2094 static const struct gpio_chip davinci_mcasp_template_chip = {
2095 .owner = THIS_MODULE,
2096 .request = davinci_mcasp_gpio_request,
2097 .free = davinci_mcasp_gpio_free,
2098 .direction_output = davinci_mcasp_gpio_direction_out,
2099 .set = davinci_mcasp_gpio_set,
2100 .direction_input = davinci_mcasp_gpio_direction_in,
2101 .get = davinci_mcasp_gpio_get,
2102 .get_direction = davinci_mcasp_gpio_get_direction,
2107 static int davinci_mcasp_init_gpiochip(struct davinci_mcasp *mcasp)
2109 if (!davinci_mcasp_have_gpiochip(mcasp))
2112 mcasp->gpio_chip = davinci_mcasp_template_chip;
2113 mcasp->gpio_chip.label = dev_name(mcasp->dev);
2114 mcasp->gpio_chip.parent = mcasp->dev;
2115 #ifdef CONFIG_OF_GPIO
2116 mcasp->gpio_chip.of_node = mcasp->dev->of_node;
2119 return devm_gpiochip_add_data(mcasp->dev, &mcasp->gpio_chip, mcasp);
2122 #else /* CONFIG_GPIOLIB */
2123 static inline int davinci_mcasp_init_gpiochip(struct davinci_mcasp *mcasp)
2127 #endif /* CONFIG_GPIOLIB */
2129 static int davinci_mcasp_probe(struct platform_device *pdev)
2131 struct snd_dmaengine_dai_dma_data *dma_data;
2132 struct resource *mem, *dat;
2133 struct davinci_mcasp *mcasp;
2138 if (!pdev->dev.platform_data && !pdev->dev.of_node) {
2139 dev_err(&pdev->dev, "No platform data supplied\n");
2143 mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp),
2148 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
2150 dev_warn(&pdev->dev,
2151 "\"mpu\" mem resource not found, using index 0\n");
2152 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2154 dev_err(&pdev->dev, "no mem resource?\n");
2159 mcasp->base = devm_ioremap_resource(&pdev->dev, mem);
2160 if (IS_ERR(mcasp->base))
2161 return PTR_ERR(mcasp->base);
2163 dev_set_drvdata(&pdev->dev, mcasp);
2164 pm_runtime_enable(&pdev->dev);
2166 mcasp->dev = &pdev->dev;
2167 ret = davinci_mcasp_get_config(mcasp, pdev);
2171 /* All PINS as McASP */
2172 pm_runtime_get_sync(mcasp->dev);
2173 mcasp_set_reg(mcasp, DAVINCI_MCASP_PFUNC_REG, 0x00000000);
2174 pm_runtime_put(mcasp->dev);
2176 /* Skip audio related setup code if the configuration is not adequat */
2177 if (mcasp->missing_audio_param)
2180 irq = platform_get_irq_byname_optional(pdev, "common");
2182 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_common",
2183 dev_name(&pdev->dev));
2188 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2189 davinci_mcasp_common_irq_handler,
2190 IRQF_ONESHOT | IRQF_SHARED,
2193 dev_err(&pdev->dev, "common IRQ request failed\n");
2197 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
2198 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
2201 irq = platform_get_irq_byname_optional(pdev, "rx");
2203 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_rx",
2204 dev_name(&pdev->dev));
2209 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2210 davinci_mcasp_rx_irq_handler,
2211 IRQF_ONESHOT, irq_name, mcasp);
2213 dev_err(&pdev->dev, "RX IRQ request failed\n");
2217 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
2220 irq = platform_get_irq_byname_optional(pdev, "tx");
2222 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_tx",
2223 dev_name(&pdev->dev));
2228 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2229 davinci_mcasp_tx_irq_handler,
2230 IRQF_ONESHOT, irq_name, mcasp);
2232 dev_err(&pdev->dev, "TX IRQ request failed\n");
2236 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
2239 dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
2241 mcasp->dat_port = true;
2243 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
2244 dma_data->filter_data = "tx";
2246 dma_data->addr = dat->start;
2248 dma_data->addr = mem->start + davinci_mcasp_txdma_offset(mcasp->pdata);
2251 /* RX is not valid in DIT mode */
2252 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
2253 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
2254 dma_data->filter_data = "rx";
2256 dma_data->addr = dat->start;
2259 mem->start + davinci_mcasp_rxdma_offset(mcasp->pdata);
2262 if (mcasp->version < MCASP_VERSION_3) {
2263 mcasp->fifo_base = DAVINCI_MCASP_V2_AFIFO_BASE;
2264 /* dma_params->dma_addr is pointing to the data port address */
2265 mcasp->dat_port = true;
2267 mcasp->fifo_base = DAVINCI_MCASP_V3_AFIFO_BASE;
2270 /* Allocate memory for long enough list for all possible
2271 * scenarios. Maximum number tdm slots is 32 and there cannot
2272 * be more serializers than given in the configuration. The
2273 * serializer directions could be taken into account, but it
2274 * would make code much more complex and save only couple of
2277 mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list =
2278 devm_kcalloc(mcasp->dev,
2279 32 + mcasp->num_serializer - 1,
2280 sizeof(unsigned int),
2283 mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list =
2284 devm_kcalloc(mcasp->dev,
2285 32 + mcasp->num_serializer - 1,
2286 sizeof(unsigned int),
2289 if (!mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list ||
2290 !mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list) {
2295 ret = davinci_mcasp_set_ch_constraints(mcasp);
2299 mcasp_reparent_fck(pdev);
2301 ret = devm_snd_soc_register_component(&pdev->dev, &davinci_mcasp_component,
2302 &davinci_mcasp_dai[mcasp->op_mode], 1);
2307 ret = davinci_mcasp_get_dma_type(mcasp);
2310 ret = edma_pcm_platform_register(&pdev->dev);
2313 ret = sdma_pcm_platform_register(&pdev->dev, "tx", "rx");
2316 ret = udma_pcm_platform_register(&pdev->dev);
2319 dev_err(&pdev->dev, "No DMA controller found (%d)\n", ret);
2325 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
2330 ret = davinci_mcasp_init_gpiochip(mcasp);
2332 dev_err(&pdev->dev, "gpiochip registration failed: %d\n", ret);
2338 pm_runtime_disable(&pdev->dev);
2342 static int davinci_mcasp_remove(struct platform_device *pdev)
2344 pm_runtime_disable(&pdev->dev);
2350 static int davinci_mcasp_runtime_suspend(struct device *dev)
2352 struct davinci_mcasp *mcasp = dev_get_drvdata(dev);
2353 struct davinci_mcasp_context *context = &mcasp->context;
2357 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
2358 context->config_regs[i] = mcasp_get_reg(mcasp, context_regs[i]);
2360 if (mcasp->txnumevt) {
2361 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
2362 context->afifo_regs[0] = mcasp_get_reg(mcasp, reg);
2364 if (mcasp->rxnumevt) {
2365 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
2366 context->afifo_regs[1] = mcasp_get_reg(mcasp, reg);
2369 for (i = 0; i < mcasp->num_serializer; i++)
2370 context->xrsr_regs[i] = mcasp_get_reg(mcasp,
2371 DAVINCI_MCASP_XRSRCTL_REG(i));
2376 static int davinci_mcasp_runtime_resume(struct device *dev)
2378 struct davinci_mcasp *mcasp = dev_get_drvdata(dev);
2379 struct davinci_mcasp_context *context = &mcasp->context;
2383 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
2384 mcasp_set_reg(mcasp, context_regs[i], context->config_regs[i]);
2386 if (mcasp->txnumevt) {
2387 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
2388 mcasp_set_reg(mcasp, reg, context->afifo_regs[0]);
2390 if (mcasp->rxnumevt) {
2391 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
2392 mcasp_set_reg(mcasp, reg, context->afifo_regs[1]);
2395 for (i = 0; i < mcasp->num_serializer; i++)
2396 mcasp_set_reg(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
2397 context->xrsr_regs[i]);
2404 static const struct dev_pm_ops davinci_mcasp_pm_ops = {
2405 SET_RUNTIME_PM_OPS(davinci_mcasp_runtime_suspend,
2406 davinci_mcasp_runtime_resume,
2410 static struct platform_driver davinci_mcasp_driver = {
2411 .probe = davinci_mcasp_probe,
2412 .remove = davinci_mcasp_remove,
2414 .name = "davinci-mcasp",
2415 .pm = &davinci_mcasp_pm_ops,
2416 .of_match_table = mcasp_dt_ids,
2420 module_platform_driver(davinci_mcasp_driver);
2422 MODULE_AUTHOR("Steve Chen");
2423 MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
2424 MODULE_LICENSE("GPL");