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>
41 #include "davinci-mcasp.h"
43 #define MCASP_MAX_AFIFO_DEPTH 64
46 static u32 context_regs
[] = {
47 DAVINCI_MCASP_TXFMCTL_REG
,
48 DAVINCI_MCASP_RXFMCTL_REG
,
49 DAVINCI_MCASP_TXFMT_REG
,
50 DAVINCI_MCASP_RXFMT_REG
,
51 DAVINCI_MCASP_ACLKXCTL_REG
,
52 DAVINCI_MCASP_ACLKRCTL_REG
,
53 DAVINCI_MCASP_AHCLKXCTL_REG
,
54 DAVINCI_MCASP_AHCLKRCTL_REG
,
55 DAVINCI_MCASP_PDIR_REG
,
56 DAVINCI_MCASP_PFUNC_REG
,
57 DAVINCI_MCASP_RXMASK_REG
,
58 DAVINCI_MCASP_TXMASK_REG
,
59 DAVINCI_MCASP_RXTDM_REG
,
60 DAVINCI_MCASP_TXTDM_REG
,
63 struct davinci_mcasp_context
{
64 u32 config_regs
[ARRAY_SIZE(context_regs
)];
65 u32 afifo_regs
[2]; /* for read/write fifo control registers */
66 u32
*xrsr_regs
; /* for serializer configuration */
71 struct davinci_mcasp_ruledata
{
72 struct davinci_mcasp
*mcasp
;
76 struct davinci_mcasp
{
77 struct snd_dmaengine_dai_dma_data dma_data
[2];
81 struct snd_pcm_substream
*substreams
[2];
84 /* McASP specific data */
102 unsigned long pdir
; /* Pin direction bitfield */
104 /* McASP FIFO related */
110 /* Used for comstraint setting on the second stream */
112 int max_format_width
;
113 u8 active_serializers
[2];
115 #ifdef CONFIG_GPIOLIB
116 struct gpio_chip gpio_chip
;
120 struct davinci_mcasp_context context
;
123 struct davinci_mcasp_ruledata ruledata
[2];
124 struct snd_pcm_hw_constraint_list chconstr
[2];
127 static inline void mcasp_set_bits(struct davinci_mcasp
*mcasp
, u32 offset
,
130 void __iomem
*reg
= mcasp
->base
+ offset
;
131 __raw_writel(__raw_readl(reg
) | val
, reg
);
134 static inline void mcasp_clr_bits(struct davinci_mcasp
*mcasp
, u32 offset
,
137 void __iomem
*reg
= mcasp
->base
+ offset
;
138 __raw_writel((__raw_readl(reg
) & ~(val
)), reg
);
141 static inline void mcasp_mod_bits(struct davinci_mcasp
*mcasp
, u32 offset
,
144 void __iomem
*reg
= mcasp
->base
+ offset
;
145 __raw_writel((__raw_readl(reg
) & ~mask
) | val
, reg
);
148 static inline void mcasp_set_reg(struct davinci_mcasp
*mcasp
, u32 offset
,
151 __raw_writel(val
, mcasp
->base
+ offset
);
154 static inline u32
mcasp_get_reg(struct davinci_mcasp
*mcasp
, u32 offset
)
156 return (u32
)__raw_readl(mcasp
->base
+ offset
);
159 static void mcasp_set_ctl_reg(struct davinci_mcasp
*mcasp
, u32 ctl_reg
, u32 val
)
163 mcasp_set_bits(mcasp
, ctl_reg
, val
);
165 /* programming GBLCTL needs to read back from GBLCTL and verfiy */
166 /* loop count is to avoid the lock-up */
167 for (i
= 0; i
< 1000; i
++) {
168 if ((mcasp_get_reg(mcasp
, ctl_reg
) & val
) == val
)
172 if (i
== 1000 && ((mcasp_get_reg(mcasp
, ctl_reg
) & val
) != val
))
173 printk(KERN_ERR
"GBLCTL write error\n");
176 static bool mcasp_is_synchronous(struct davinci_mcasp
*mcasp
)
178 u32 rxfmctl
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
);
179 u32 aclkxctl
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
);
181 return !(aclkxctl
& TX_ASYNC
) && rxfmctl
& AFSRE
;
184 static inline void mcasp_set_clk_pdir(struct davinci_mcasp
*mcasp
, bool enable
)
186 u32 bit
= PIN_BIT_AMUTE
;
188 for_each_set_bit_from(bit
, &mcasp
->pdir
, PIN_BIT_AFSR
+ 1) {
190 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, BIT(bit
));
192 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, BIT(bit
));
196 static inline void mcasp_set_axr_pdir(struct davinci_mcasp
*mcasp
, bool enable
)
200 for_each_set_bit(bit
, &mcasp
->pdir
, PIN_BIT_AMUTE
) {
202 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, BIT(bit
));
204 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, BIT(bit
));
208 static void mcasp_start_rx(struct davinci_mcasp
*mcasp
)
210 if (mcasp
->rxnumevt
) { /* enable FIFO */
211 u32 reg
= mcasp
->fifo_base
+ MCASP_RFIFOCTL_OFFSET
;
213 mcasp_clr_bits(mcasp
, reg
, FIFO_ENABLE
);
214 mcasp_set_bits(mcasp
, reg
, FIFO_ENABLE
);
218 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLR_REG
, RXHCLKRST
);
219 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLR_REG
, RXCLKRST
);
221 * When ASYNC == 0 the transmit and receive sections operate
222 * synchronously from the transmit clock and frame sync. We need to make
223 * sure that the TX signlas are enabled when starting reception.
225 if (mcasp_is_synchronous(mcasp
)) {
226 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXHCLKRST
);
227 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXCLKRST
);
228 mcasp_set_clk_pdir(mcasp
, true);
231 /* Activate serializer(s) */
232 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXSTAT_REG
, 0xFFFFFFFF);
233 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLR_REG
, RXSERCLR
);
234 /* Release RX state machine */
235 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLR_REG
, RXSMRST
);
236 /* Release Frame Sync generator */
237 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLR_REG
, RXFSRST
);
238 if (mcasp_is_synchronous(mcasp
))
239 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXFSRST
);
241 /* enable receive IRQs */
242 mcasp_set_bits(mcasp
, DAVINCI_MCASP_EVTCTLR_REG
,
243 mcasp
->irq_request
[SNDRV_PCM_STREAM_CAPTURE
]);
246 static void mcasp_start_tx(struct davinci_mcasp
*mcasp
)
250 if (mcasp
->txnumevt
) { /* enable FIFO */
251 u32 reg
= mcasp
->fifo_base
+ MCASP_WFIFOCTL_OFFSET
;
253 mcasp_clr_bits(mcasp
, reg
, FIFO_ENABLE
);
254 mcasp_set_bits(mcasp
, reg
, FIFO_ENABLE
);
258 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXHCLKRST
);
259 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXCLKRST
);
260 mcasp_set_clk_pdir(mcasp
, true);
262 /* Activate serializer(s) */
263 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXSTAT_REG
, 0xFFFFFFFF);
264 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXSERCLR
);
266 /* wait for XDATA to be cleared */
268 while ((mcasp_get_reg(mcasp
, DAVINCI_MCASP_TXSTAT_REG
) & XRDATA
) &&
272 mcasp_set_axr_pdir(mcasp
, true);
274 /* Release TX state machine */
275 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXSMRST
);
276 /* Release Frame Sync generator */
277 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXFSRST
);
279 /* enable transmit IRQs */
280 mcasp_set_bits(mcasp
, DAVINCI_MCASP_EVTCTLX_REG
,
281 mcasp
->irq_request
[SNDRV_PCM_STREAM_PLAYBACK
]);
284 static void davinci_mcasp_start(struct davinci_mcasp
*mcasp
, int stream
)
288 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
289 mcasp_start_tx(mcasp
);
291 mcasp_start_rx(mcasp
);
294 static void mcasp_stop_rx(struct davinci_mcasp
*mcasp
)
296 /* disable IRQ sources */
297 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_EVTCTLR_REG
,
298 mcasp
->irq_request
[SNDRV_PCM_STREAM_CAPTURE
]);
301 * In synchronous mode stop the TX clocks if no other stream is
304 if (mcasp_is_synchronous(mcasp
) && !mcasp
->streams
) {
305 mcasp_set_clk_pdir(mcasp
, false);
306 mcasp_set_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, 0);
309 mcasp_set_reg(mcasp
, DAVINCI_MCASP_GBLCTLR_REG
, 0);
310 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXSTAT_REG
, 0xFFFFFFFF);
312 if (mcasp
->rxnumevt
) { /* disable FIFO */
313 u32 reg
= mcasp
->fifo_base
+ MCASP_RFIFOCTL_OFFSET
;
315 mcasp_clr_bits(mcasp
, reg
, FIFO_ENABLE
);
319 static void mcasp_stop_tx(struct davinci_mcasp
*mcasp
)
323 /* disable IRQ sources */
324 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_EVTCTLX_REG
,
325 mcasp
->irq_request
[SNDRV_PCM_STREAM_PLAYBACK
]);
328 * In synchronous mode keep TX clocks running if the capture stream is
331 if (mcasp_is_synchronous(mcasp
) && mcasp
->streams
)
332 val
= TXHCLKRST
| TXCLKRST
| TXFSRST
;
334 mcasp_set_clk_pdir(mcasp
, false);
337 mcasp_set_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, val
);
338 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXSTAT_REG
, 0xFFFFFFFF);
340 if (mcasp
->txnumevt
) { /* disable FIFO */
341 u32 reg
= mcasp
->fifo_base
+ MCASP_WFIFOCTL_OFFSET
;
343 mcasp_clr_bits(mcasp
, reg
, FIFO_ENABLE
);
346 mcasp_set_axr_pdir(mcasp
, false);
349 static void davinci_mcasp_stop(struct davinci_mcasp
*mcasp
, int stream
)
353 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
354 mcasp_stop_tx(mcasp
);
356 mcasp_stop_rx(mcasp
);
359 static irqreturn_t
davinci_mcasp_tx_irq_handler(int irq
, void *data
)
361 struct davinci_mcasp
*mcasp
= (struct davinci_mcasp
*)data
;
362 struct snd_pcm_substream
*substream
;
363 u32 irq_mask
= mcasp
->irq_request
[SNDRV_PCM_STREAM_PLAYBACK
];
364 u32 handled_mask
= 0;
367 stat
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_TXSTAT_REG
);
368 if (stat
& XUNDRN
& irq_mask
) {
369 dev_warn(mcasp
->dev
, "Transmit buffer underflow\n");
370 handled_mask
|= XUNDRN
;
372 substream
= mcasp
->substreams
[SNDRV_PCM_STREAM_PLAYBACK
];
374 snd_pcm_stop_xrun(substream
);
378 dev_warn(mcasp
->dev
, "unhandled tx event. txstat: 0x%08x\n",
382 handled_mask
|= XRERR
;
384 /* Ack the handled event only */
385 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXSTAT_REG
, handled_mask
);
387 return IRQ_RETVAL(handled_mask
);
390 static irqreturn_t
davinci_mcasp_rx_irq_handler(int irq
, void *data
)
392 struct davinci_mcasp
*mcasp
= (struct davinci_mcasp
*)data
;
393 struct snd_pcm_substream
*substream
;
394 u32 irq_mask
= mcasp
->irq_request
[SNDRV_PCM_STREAM_CAPTURE
];
395 u32 handled_mask
= 0;
398 stat
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_RXSTAT_REG
);
399 if (stat
& ROVRN
& irq_mask
) {
400 dev_warn(mcasp
->dev
, "Receive buffer overflow\n");
401 handled_mask
|= ROVRN
;
403 substream
= mcasp
->substreams
[SNDRV_PCM_STREAM_CAPTURE
];
405 snd_pcm_stop_xrun(substream
);
409 dev_warn(mcasp
->dev
, "unhandled rx event. rxstat: 0x%08x\n",
413 handled_mask
|= XRERR
;
415 /* Ack the handled event only */
416 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXSTAT_REG
, handled_mask
);
418 return IRQ_RETVAL(handled_mask
);
421 static irqreturn_t
davinci_mcasp_common_irq_handler(int irq
, void *data
)
423 struct davinci_mcasp
*mcasp
= (struct davinci_mcasp
*)data
;
424 irqreturn_t ret
= IRQ_NONE
;
426 if (mcasp
->substreams
[SNDRV_PCM_STREAM_PLAYBACK
])
427 ret
= davinci_mcasp_tx_irq_handler(irq
, data
);
429 if (mcasp
->substreams
[SNDRV_PCM_STREAM_CAPTURE
])
430 ret
|= davinci_mcasp_rx_irq_handler(irq
, data
);
435 static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai
*cpu_dai
,
438 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(cpu_dai
);
447 pm_runtime_get_sync(mcasp
->dev
);
448 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
449 case SND_SOC_DAIFMT_DSP_A
:
450 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, FSXDUR
);
451 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, FSRDUR
);
452 /* 1st data bit occur one ACLK cycle after the frame sync */
455 case SND_SOC_DAIFMT_DSP_B
:
456 case SND_SOC_DAIFMT_AC97
:
457 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, FSXDUR
);
458 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, FSRDUR
);
459 /* No delay after FS */
462 case SND_SOC_DAIFMT_I2S
:
463 /* configure a full-word SYNC pulse (LRCLK) */
464 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, FSXDUR
);
465 mcasp_set_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, FSRDUR
);
466 /* 1st data bit occur one ACLK cycle after the frame sync */
468 /* FS need to be inverted */
471 case SND_SOC_DAIFMT_RIGHT_J
:
472 case SND_SOC_DAIFMT_LEFT_J
:
473 /* configure a full-word SYNC pulse (LRCLK) */
474 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, FSXDUR
);
475 mcasp_set_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, FSRDUR
);
476 /* No delay after FS */
484 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_TXFMT_REG
, FSXDLY(data_delay
),
486 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_RXFMT_REG
, FSRDLY(data_delay
),
489 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
490 case SND_SOC_DAIFMT_CBS_CFS
:
491 /* codec is clock and frame slave */
492 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXE
);
493 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, AFSXE
);
495 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRE
);
496 mcasp_set_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, AFSRE
);
499 set_bit(PIN_BIT_ACLKX
, &mcasp
->pdir
);
500 set_bit(PIN_BIT_ACLKR
, &mcasp
->pdir
);
502 set_bit(PIN_BIT_AFSX
, &mcasp
->pdir
);
503 set_bit(PIN_BIT_AFSR
, &mcasp
->pdir
);
505 mcasp
->bclk_master
= 1;
507 case SND_SOC_DAIFMT_CBS_CFM
:
508 /* codec is clock slave and frame master */
509 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXE
);
510 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, AFSXE
);
512 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRE
);
513 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, AFSRE
);
516 set_bit(PIN_BIT_ACLKX
, &mcasp
->pdir
);
517 set_bit(PIN_BIT_ACLKR
, &mcasp
->pdir
);
519 clear_bit(PIN_BIT_AFSX
, &mcasp
->pdir
);
520 clear_bit(PIN_BIT_AFSR
, &mcasp
->pdir
);
522 mcasp
->bclk_master
= 1;
524 case SND_SOC_DAIFMT_CBM_CFS
:
525 /* codec is clock master and frame slave */
526 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXE
);
527 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, AFSXE
);
529 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRE
);
530 mcasp_set_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, AFSRE
);
533 clear_bit(PIN_BIT_ACLKX
, &mcasp
->pdir
);
534 clear_bit(PIN_BIT_ACLKR
, &mcasp
->pdir
);
536 set_bit(PIN_BIT_AFSX
, &mcasp
->pdir
);
537 set_bit(PIN_BIT_AFSR
, &mcasp
->pdir
);
539 mcasp
->bclk_master
= 0;
541 case SND_SOC_DAIFMT_CBM_CFM
:
542 /* codec is clock and frame master */
543 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXE
);
544 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, AFSXE
);
546 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRE
);
547 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, AFSRE
);
550 clear_bit(PIN_BIT_ACLKX
, &mcasp
->pdir
);
551 clear_bit(PIN_BIT_ACLKR
, &mcasp
->pdir
);
553 clear_bit(PIN_BIT_AFSX
, &mcasp
->pdir
);
554 clear_bit(PIN_BIT_AFSR
, &mcasp
->pdir
);
556 mcasp
->bclk_master
= 0;
563 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
564 case SND_SOC_DAIFMT_IB_NF
:
565 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXPOL
);
566 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRPOL
);
567 fs_pol_rising
= true;
569 case SND_SOC_DAIFMT_NB_IF
:
570 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXPOL
);
571 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRPOL
);
572 fs_pol_rising
= false;
574 case SND_SOC_DAIFMT_IB_IF
:
575 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXPOL
);
576 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRPOL
);
577 fs_pol_rising
= false;
579 case SND_SOC_DAIFMT_NB_NF
:
580 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXPOL
);
581 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRPOL
);
582 fs_pol_rising
= true;
590 fs_pol_rising
= !fs_pol_rising
;
593 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, FSXPOL
);
594 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, FSRPOL
);
596 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, FSXPOL
);
597 mcasp_set_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, FSRPOL
);
600 mcasp
->dai_fmt
= fmt
;
602 pm_runtime_put(mcasp
->dev
);
606 static int __davinci_mcasp_set_clkdiv(struct davinci_mcasp
*mcasp
, int div_id
,
607 int div
, bool explicit)
609 pm_runtime_get_sync(mcasp
->dev
);
611 case MCASP_CLKDIV_AUXCLK
: /* MCLK divider */
612 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_AHCLKXCTL_REG
,
613 AHCLKXDIV(div
- 1), AHCLKXDIV_MASK
);
614 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_AHCLKRCTL_REG
,
615 AHCLKRDIV(div
- 1), AHCLKRDIV_MASK
);
618 case MCASP_CLKDIV_BCLK
: /* BCLK divider */
619 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
,
620 ACLKXDIV(div
- 1), ACLKXDIV_MASK
);
621 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
,
622 ACLKRDIV(div
- 1), ACLKRDIV_MASK
);
624 mcasp
->bclk_div
= div
;
627 case MCASP_CLKDIV_BCLK_FS_RATIO
:
629 * BCLK/LRCLK ratio descries how many bit-clock cycles
630 * fit into one frame. The clock ratio is given for a
631 * full period of data (for I2S format both left and
632 * right channels), so it has to be divided by number
633 * of tdm-slots (for I2S - divided by 2).
634 * Instead of storing this ratio, we calculate a new
635 * tdm_slot width by dividing the the ratio by the
636 * number of configured tdm slots.
638 mcasp
->slot_width
= div
/ mcasp
->tdm_slots
;
639 if (div
% mcasp
->tdm_slots
)
641 "%s(): BCLK/LRCLK %d is not divisible by %d tdm slots",
642 __func__
, div
, mcasp
->tdm_slots
);
649 pm_runtime_put(mcasp
->dev
);
653 static int davinci_mcasp_set_clkdiv(struct snd_soc_dai
*dai
, int div_id
,
656 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(dai
);
658 return __davinci_mcasp_set_clkdiv(mcasp
, div_id
, div
, 1);
661 static int davinci_mcasp_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
662 unsigned int freq
, int dir
)
664 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(dai
);
666 pm_runtime_get_sync(mcasp
->dev
);
668 if (dir
== SND_SOC_CLOCK_IN
) {
670 case MCASP_CLK_HCLK_AHCLK
:
671 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_AHCLKXCTL_REG
,
673 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_AHCLKRCTL_REG
,
675 clear_bit(PIN_BIT_AHCLKX
, &mcasp
->pdir
);
677 case MCASP_CLK_HCLK_AUXCLK
:
678 mcasp_set_bits(mcasp
, DAVINCI_MCASP_AHCLKXCTL_REG
,
680 mcasp_set_bits(mcasp
, DAVINCI_MCASP_AHCLKRCTL_REG
,
682 set_bit(PIN_BIT_AHCLKX
, &mcasp
->pdir
);
685 dev_err(mcasp
->dev
, "Invalid clk id: %d\n", clk_id
);
689 /* Select AUXCLK as HCLK */
690 mcasp_set_bits(mcasp
, DAVINCI_MCASP_AHCLKXCTL_REG
, AHCLKXE
);
691 mcasp_set_bits(mcasp
, DAVINCI_MCASP_AHCLKRCTL_REG
, AHCLKRE
);
692 set_bit(PIN_BIT_AHCLKX
, &mcasp
->pdir
);
695 * When AHCLK X/R is selected to be output it means that the HCLK is
696 * the same clock - coming via AUXCLK.
698 mcasp
->sysclk_freq
= freq
;
700 pm_runtime_put(mcasp
->dev
);
704 /* All serializers must have equal number of channels */
705 static int davinci_mcasp_ch_constraint(struct davinci_mcasp
*mcasp
, int stream
,
708 struct snd_pcm_hw_constraint_list
*cl
= &mcasp
->chconstr
[stream
];
709 unsigned int *list
= (unsigned int *) cl
->list
;
710 int slots
= mcasp
->tdm_slots
;
713 if (mcasp
->tdm_mask
[stream
])
714 slots
= hweight32(mcasp
->tdm_mask
[stream
]);
716 for (i
= 1; i
<= slots
; i
++)
719 for (i
= 2; i
<= serializers
; i
++)
720 list
[count
++] = i
*slots
;
727 static int davinci_mcasp_set_ch_constraints(struct davinci_mcasp
*mcasp
)
729 int rx_serializers
= 0, tx_serializers
= 0, ret
, i
;
731 for (i
= 0; i
< mcasp
->num_serializer
; i
++)
732 if (mcasp
->serial_dir
[i
] == TX_MODE
)
734 else if (mcasp
->serial_dir
[i
] == RX_MODE
)
737 ret
= davinci_mcasp_ch_constraint(mcasp
, SNDRV_PCM_STREAM_PLAYBACK
,
742 ret
= davinci_mcasp_ch_constraint(mcasp
, SNDRV_PCM_STREAM_CAPTURE
,
749 static int davinci_mcasp_set_tdm_slot(struct snd_soc_dai
*dai
,
750 unsigned int tx_mask
,
751 unsigned int rx_mask
,
752 int slots
, int slot_width
)
754 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(dai
);
757 "%s() tx_mask 0x%08x rx_mask 0x%08x slots %d width %d\n",
758 __func__
, tx_mask
, rx_mask
, slots
, slot_width
);
760 if (tx_mask
>= (1<<slots
) || rx_mask
>= (1<<slots
)) {
762 "Bad tdm mask tx: 0x%08x rx: 0x%08x slots %d\n",
763 tx_mask
, rx_mask
, slots
);
768 (slot_width
< 8 || slot_width
> 32 || slot_width
% 4 != 0)) {
769 dev_err(mcasp
->dev
, "%s: Unsupported slot_width %d\n",
770 __func__
, slot_width
);
774 mcasp
->tdm_slots
= slots
;
775 mcasp
->tdm_mask
[SNDRV_PCM_STREAM_PLAYBACK
] = tx_mask
;
776 mcasp
->tdm_mask
[SNDRV_PCM_STREAM_CAPTURE
] = rx_mask
;
777 mcasp
->slot_width
= slot_width
;
779 return davinci_mcasp_set_ch_constraints(mcasp
);
782 static int davinci_config_channel_size(struct davinci_mcasp
*mcasp
,
786 u32 tx_rotate
, rx_rotate
, slot_width
;
787 u32 mask
= (1ULL << sample_width
) - 1;
789 if (mcasp
->slot_width
)
790 slot_width
= mcasp
->slot_width
;
791 else if (mcasp
->max_format_width
)
792 slot_width
= mcasp
->max_format_width
;
794 slot_width
= sample_width
;
797 * right aligned formats: rotate w/ slot_width
798 * left aligned formats: rotate w/ sample_width
801 * right aligned formats: no rotation needed
802 * left aligned formats: rotate w/ (slot_width - sample_width)
804 if ((mcasp
->dai_fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) ==
805 SND_SOC_DAIFMT_RIGHT_J
) {
806 tx_rotate
= (slot_width
/ 4) & 0x7;
809 tx_rotate
= (sample_width
/ 4) & 0x7;
810 rx_rotate
= (slot_width
- sample_width
) / 4;
813 /* mapping of the XSSZ bit-field as described in the datasheet */
814 fmt
= (slot_width
>> 1) - 1;
816 if (mcasp
->op_mode
!= DAVINCI_MCASP_DIT_MODE
) {
817 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_RXFMT_REG
, RXSSZ(fmt
),
819 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_TXFMT_REG
, TXSSZ(fmt
),
821 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_TXFMT_REG
, TXROT(tx_rotate
),
823 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_RXFMT_REG
, RXROT(rx_rotate
),
825 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXMASK_REG
, mask
);
828 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXMASK_REG
, mask
);
833 static int mcasp_common_hw_param(struct davinci_mcasp
*mcasp
, int stream
,
834 int period_words
, int channels
)
836 struct snd_dmaengine_dai_dma_data
*dma_data
= &mcasp
->dma_data
[stream
];
840 u8 slots
= mcasp
->tdm_slots
;
841 u8 max_active_serializers
= (channels
+ slots
- 1) / slots
;
842 u8 max_rx_serializers
, max_tx_serializers
;
843 int active_serializers
, numevt
;
845 /* Default configuration */
846 if (mcasp
->version
< MCASP_VERSION_3
)
847 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PWREMUMGT_REG
, MCASP_SOFT
);
849 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
850 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXSTAT_REG
, 0xFFFFFFFF);
851 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_XEVTCTL_REG
, TXDATADMADIS
);
852 max_tx_serializers
= max_active_serializers
;
854 mcasp
->active_serializers
[SNDRV_PCM_STREAM_CAPTURE
];
856 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXSTAT_REG
, 0xFFFFFFFF);
857 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_REVTCTL_REG
, RXDATADMADIS
);
859 mcasp
->active_serializers
[SNDRV_PCM_STREAM_PLAYBACK
];
860 max_rx_serializers
= max_active_serializers
;
863 for (i
= 0; i
< mcasp
->num_serializer
; i
++) {
864 mcasp_set_bits(mcasp
, DAVINCI_MCASP_XRSRCTL_REG(i
),
865 mcasp
->serial_dir
[i
]);
866 if (mcasp
->serial_dir
[i
] == TX_MODE
&&
867 tx_ser
< max_tx_serializers
) {
868 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_XRSRCTL_REG(i
),
869 mcasp
->dismod
, DISMOD_MASK
);
870 set_bit(PIN_BIT_AXR(i
), &mcasp
->pdir
);
872 } else if (mcasp
->serial_dir
[i
] == RX_MODE
&&
873 rx_ser
< max_rx_serializers
) {
874 clear_bit(PIN_BIT_AXR(i
), &mcasp
->pdir
);
877 /* Inactive or unused pin, set it to inactive */
878 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_XRSRCTL_REG(i
),
879 SRMOD_INACTIVE
, SRMOD_MASK
);
880 /* If unused, set DISMOD for the pin */
881 if (mcasp
->serial_dir
[i
] != INACTIVE_MODE
)
882 mcasp_mod_bits(mcasp
,
883 DAVINCI_MCASP_XRSRCTL_REG(i
),
884 mcasp
->dismod
, DISMOD_MASK
);
885 clear_bit(PIN_BIT_AXR(i
), &mcasp
->pdir
);
889 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
890 active_serializers
= tx_ser
;
891 numevt
= mcasp
->txnumevt
;
892 reg
= mcasp
->fifo_base
+ MCASP_WFIFOCTL_OFFSET
;
894 active_serializers
= rx_ser
;
895 numevt
= mcasp
->rxnumevt
;
896 reg
= mcasp
->fifo_base
+ MCASP_RFIFOCTL_OFFSET
;
899 if (active_serializers
< max_active_serializers
) {
900 dev_warn(mcasp
->dev
, "stream has more channels (%d) than are "
901 "enabled in mcasp (%d)\n", channels
,
902 active_serializers
* slots
);
906 /* AFIFO is not in use */
908 /* Configure the burst size for platform drivers */
909 if (active_serializers
> 1) {
911 * If more than one serializers are in use we have one
912 * DMA request to provide data for all serializers.
913 * For example if three serializers are enabled the DMA
914 * need to transfer three words per DMA request.
916 dma_data
->maxburst
= active_serializers
;
918 dma_data
->maxburst
= 0;
924 if (period_words
% active_serializers
) {
925 dev_err(mcasp
->dev
, "Invalid combination of period words and "
926 "active serializers: %d, %d\n", period_words
,
932 * Calculate the optimal AFIFO depth for platform side:
933 * The number of words for numevt need to be in steps of active
936 numevt
= (numevt
/ active_serializers
) * active_serializers
;
938 while (period_words
% numevt
&& numevt
> 0)
939 numevt
-= active_serializers
;
941 numevt
= active_serializers
;
943 mcasp_mod_bits(mcasp
, reg
, active_serializers
, NUMDMA_MASK
);
944 mcasp_mod_bits(mcasp
, reg
, NUMEVT(numevt
), NUMEVT_MASK
);
946 /* Configure the burst size for platform drivers */
949 dma_data
->maxburst
= numevt
;
952 mcasp
->active_serializers
[stream
] = active_serializers
;
957 static int mcasp_i2s_hw_param(struct davinci_mcasp
*mcasp
, int stream
,
962 int active_serializers
;
966 total_slots
= mcasp
->tdm_slots
;
969 * If more than one serializer is needed, then use them with
970 * all the specified tdm_slots. Otherwise, one serializer can
971 * cope with the transaction using just as many slots as there
972 * are channels in the stream.
974 if (mcasp
->tdm_mask
[stream
]) {
975 active_slots
= hweight32(mcasp
->tdm_mask
[stream
]);
976 active_serializers
= (channels
+ active_slots
- 1) /
978 if (active_serializers
== 1)
979 active_slots
= channels
;
980 for (i
= 0; i
< total_slots
; i
++) {
981 if ((1 << i
) & mcasp
->tdm_mask
[stream
]) {
983 if (--active_slots
<= 0)
988 active_serializers
= (channels
+ total_slots
- 1) / total_slots
;
989 if (active_serializers
== 1)
990 active_slots
= channels
;
992 active_slots
= total_slots
;
994 for (i
= 0; i
< active_slots
; i
++)
998 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, TX_ASYNC
);
1000 if (!mcasp
->dat_port
)
1003 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1004 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXTDM_REG
, mask
);
1005 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMT_REG
, busel
| TXORD
);
1006 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
,
1007 FSXMOD(total_slots
), FSXMOD(0x1FF));
1008 } else if (stream
== SNDRV_PCM_STREAM_CAPTURE
) {
1009 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXTDM_REG
, mask
);
1010 mcasp_set_bits(mcasp
, DAVINCI_MCASP_RXFMT_REG
, busel
| RXORD
);
1011 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
,
1012 FSRMOD(total_slots
), FSRMOD(0x1FF));
1014 * If McASP is set to be TX/RX synchronous and the playback is
1015 * not running already we need to configure the TX slots in
1016 * order to have correct FSX on the bus
1018 if (mcasp_is_synchronous(mcasp
) && !mcasp
->channels
)
1019 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
,
1020 FSXMOD(total_slots
), FSXMOD(0x1FF));
1027 static int mcasp_dit_hw_param(struct davinci_mcasp
*mcasp
,
1031 u8
*cs_bytes
= (u8
*) &cs_value
;
1033 /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
1035 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMT_REG
, TXROT(6) | TXSSZ(15));
1037 /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
1038 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, AFSXE
| FSXMOD(0x180));
1040 /* Set the TX tdm : for all the slots */
1041 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXTDM_REG
, 0xFFFFFFFF);
1043 /* Set the TX clock controls : div = 1 and internal */
1044 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXE
| TX_ASYNC
);
1046 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_XEVTCTL_REG
, TXDATADMADIS
);
1048 /* Only 44100 and 48000 are valid, both have the same setting */
1049 mcasp_set_bits(mcasp
, DAVINCI_MCASP_AHCLKXCTL_REG
, AHCLKXDIV(3));
1051 /* Enable the DIT */
1052 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXDITCTL_REG
, DITEN
);
1054 /* Set S/PDIF channel status bits */
1055 cs_bytes
[0] = IEC958_AES0_CON_NOT_COPYRIGHT
;
1056 cs_bytes
[1] = IEC958_AES1_CON_PCM_CODER
;
1060 cs_bytes
[3] |= IEC958_AES3_CON_FS_22050
;
1063 cs_bytes
[3] |= IEC958_AES3_CON_FS_24000
;
1066 cs_bytes
[3] |= IEC958_AES3_CON_FS_32000
;
1069 cs_bytes
[3] |= IEC958_AES3_CON_FS_44100
;
1072 cs_bytes
[3] |= IEC958_AES3_CON_FS_48000
;
1075 cs_bytes
[3] |= IEC958_AES3_CON_FS_88200
;
1078 cs_bytes
[3] |= IEC958_AES3_CON_FS_96000
;
1081 cs_bytes
[3] |= IEC958_AES3_CON_FS_176400
;
1084 cs_bytes
[3] |= IEC958_AES3_CON_FS_192000
;
1087 printk(KERN_WARNING
"unsupported sampling rate: %d\n", rate
);
1091 mcasp_set_reg(mcasp
, DAVINCI_MCASP_DITCSRA_REG
, cs_value
);
1092 mcasp_set_reg(mcasp
, DAVINCI_MCASP_DITCSRB_REG
, cs_value
);
1097 static int davinci_mcasp_calc_clk_div(struct davinci_mcasp
*mcasp
,
1098 unsigned int sysclk_freq
,
1099 unsigned int bclk_freq
, bool set
)
1101 u32 reg
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_AHCLKXCTL_REG
);
1102 int div
= sysclk_freq
/ bclk_freq
;
1103 int rem
= sysclk_freq
% bclk_freq
;
1107 if (div
> (ACLKXDIV_MASK
+ 1)) {
1108 if (reg
& AHCLKXE
) {
1109 aux_div
= div
/ (ACLKXDIV_MASK
+ 1);
1110 if (div
% (ACLKXDIV_MASK
+ 1))
1113 sysclk_freq
/= aux_div
;
1114 div
= sysclk_freq
/ bclk_freq
;
1115 rem
= sysclk_freq
% bclk_freq
;
1117 dev_warn(mcasp
->dev
, "Too fast reference clock (%u)\n",
1124 ((sysclk_freq
/ div
) - bclk_freq
) >
1125 (bclk_freq
- (sysclk_freq
/ (div
+1)))) {
1127 rem
= rem
- bclk_freq
;
1130 error_ppm
= (div
*1000000 + (int)div64_long(1000000LL*rem
,
1131 (int)bclk_freq
)) / div
- 1000000;
1135 dev_info(mcasp
->dev
, "Sample-rate is off by %d PPM\n",
1138 __davinci_mcasp_set_clkdiv(mcasp
, MCASP_CLKDIV_BCLK
, div
, 0);
1140 __davinci_mcasp_set_clkdiv(mcasp
, MCASP_CLKDIV_AUXCLK
,
1147 static inline u32
davinci_mcasp_tx_delay(struct davinci_mcasp
*mcasp
)
1149 if (!mcasp
->txnumevt
)
1152 return mcasp_get_reg(mcasp
, mcasp
->fifo_base
+ MCASP_WFIFOSTS_OFFSET
);
1155 static inline u32
davinci_mcasp_rx_delay(struct davinci_mcasp
*mcasp
)
1157 if (!mcasp
->rxnumevt
)
1160 return mcasp_get_reg(mcasp
, mcasp
->fifo_base
+ MCASP_RFIFOSTS_OFFSET
);
1163 static snd_pcm_sframes_t
davinci_mcasp_delay(
1164 struct snd_pcm_substream
*substream
,
1165 struct snd_soc_dai
*cpu_dai
)
1167 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(cpu_dai
);
1170 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1171 fifo_use
= davinci_mcasp_tx_delay(mcasp
);
1173 fifo_use
= davinci_mcasp_rx_delay(mcasp
);
1176 * Divide the used locations with the channel count to get the
1177 * FIFO usage in samples (don't care about partial samples in the
1180 return fifo_use
/ substream
->runtime
->channels
;
1183 static int davinci_mcasp_hw_params(struct snd_pcm_substream
*substream
,
1184 struct snd_pcm_hw_params
*params
,
1185 struct snd_soc_dai
*cpu_dai
)
1187 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(cpu_dai
);
1189 int channels
= params_channels(params
);
1190 int period_size
= params_period_size(params
);
1193 switch (params_format(params
)) {
1194 case SNDRV_PCM_FORMAT_U8
:
1195 case SNDRV_PCM_FORMAT_S8
:
1199 case SNDRV_PCM_FORMAT_U16_LE
:
1200 case SNDRV_PCM_FORMAT_S16_LE
:
1204 case SNDRV_PCM_FORMAT_U24_3LE
:
1205 case SNDRV_PCM_FORMAT_S24_3LE
:
1209 case SNDRV_PCM_FORMAT_U24_LE
:
1210 case SNDRV_PCM_FORMAT_S24_LE
:
1214 case SNDRV_PCM_FORMAT_U32_LE
:
1215 case SNDRV_PCM_FORMAT_S32_LE
:
1220 printk(KERN_WARNING
"davinci-mcasp: unsupported PCM format");
1224 ret
= davinci_mcasp_set_dai_fmt(cpu_dai
, mcasp
->dai_fmt
);
1229 * If mcasp is BCLK master, and a BCLK divider was not provided by
1230 * the machine driver, we need to calculate the ratio.
1232 if (mcasp
->bclk_master
&& mcasp
->bclk_div
== 0 && mcasp
->sysclk_freq
) {
1233 int slots
= mcasp
->tdm_slots
;
1234 int rate
= params_rate(params
);
1235 int sbits
= params_width(params
);
1237 if (mcasp
->slot_width
)
1238 sbits
= mcasp
->slot_width
;
1240 davinci_mcasp_calc_clk_div(mcasp
, mcasp
->sysclk_freq
,
1241 rate
* sbits
* slots
, true);
1244 ret
= mcasp_common_hw_param(mcasp
, substream
->stream
,
1245 period_size
* channels
, channels
);
1249 if (mcasp
->op_mode
== DAVINCI_MCASP_DIT_MODE
)
1250 ret
= mcasp_dit_hw_param(mcasp
, params_rate(params
));
1252 ret
= mcasp_i2s_hw_param(mcasp
, substream
->stream
,
1258 davinci_config_channel_size(mcasp
, word_length
);
1260 if (mcasp
->op_mode
== DAVINCI_MCASP_IIS_MODE
) {
1261 mcasp
->channels
= channels
;
1262 if (!mcasp
->max_format_width
)
1263 mcasp
->max_format_width
= word_length
;
1269 static int davinci_mcasp_trigger(struct snd_pcm_substream
*substream
,
1270 int cmd
, struct snd_soc_dai
*cpu_dai
)
1272 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(cpu_dai
);
1276 case SNDRV_PCM_TRIGGER_RESUME
:
1277 case SNDRV_PCM_TRIGGER_START
:
1278 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1279 davinci_mcasp_start(mcasp
, substream
->stream
);
1281 case SNDRV_PCM_TRIGGER_SUSPEND
:
1282 case SNDRV_PCM_TRIGGER_STOP
:
1283 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1284 davinci_mcasp_stop(mcasp
, substream
->stream
);
1294 static int davinci_mcasp_hw_rule_slot_width(struct snd_pcm_hw_params
*params
,
1295 struct snd_pcm_hw_rule
*rule
)
1297 struct davinci_mcasp_ruledata
*rd
= rule
->private;
1298 struct snd_mask
*fmt
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1299 struct snd_mask nfmt
;
1302 snd_mask_none(&nfmt
);
1303 slot_width
= rd
->mcasp
->slot_width
;
1305 for (i
= 0; i
<= SNDRV_PCM_FORMAT_LAST
; i
++) {
1306 if (snd_mask_test(fmt
, i
)) {
1307 if (snd_pcm_format_width(i
) <= slot_width
) {
1308 snd_mask_set(&nfmt
, i
);
1313 return snd_mask_refine(fmt
, &nfmt
);
1316 static int davinci_mcasp_hw_rule_format_width(struct snd_pcm_hw_params
*params
,
1317 struct snd_pcm_hw_rule
*rule
)
1319 struct davinci_mcasp_ruledata
*rd
= rule
->private;
1320 struct snd_mask
*fmt
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1321 struct snd_mask nfmt
;
1322 int i
, format_width
;
1324 snd_mask_none(&nfmt
);
1325 format_width
= rd
->mcasp
->max_format_width
;
1327 for (i
= 0; i
<= SNDRV_PCM_FORMAT_LAST
; i
++) {
1328 if (snd_mask_test(fmt
, i
)) {
1329 if (snd_pcm_format_width(i
) == format_width
) {
1330 snd_mask_set(&nfmt
, i
);
1335 return snd_mask_refine(fmt
, &nfmt
);
1338 static const unsigned int davinci_mcasp_dai_rates
[] = {
1339 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
1340 88200, 96000, 176400, 192000,
1343 #define DAVINCI_MAX_RATE_ERROR_PPM 1000
1345 static int davinci_mcasp_hw_rule_rate(struct snd_pcm_hw_params
*params
,
1346 struct snd_pcm_hw_rule
*rule
)
1348 struct davinci_mcasp_ruledata
*rd
= rule
->private;
1349 struct snd_interval
*ri
=
1350 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
1351 int sbits
= params_width(params
);
1352 int slots
= rd
->mcasp
->tdm_slots
;
1353 struct snd_interval range
;
1356 if (rd
->mcasp
->slot_width
)
1357 sbits
= rd
->mcasp
->slot_width
;
1359 snd_interval_any(&range
);
1362 for (i
= 0; i
< ARRAY_SIZE(davinci_mcasp_dai_rates
); i
++) {
1363 if (snd_interval_test(ri
, davinci_mcasp_dai_rates
[i
])) {
1364 uint bclk_freq
= sbits
* slots
*
1365 davinci_mcasp_dai_rates
[i
];
1366 unsigned int sysclk_freq
;
1369 if (rd
->mcasp
->auxclk_fs_ratio
)
1370 sysclk_freq
= davinci_mcasp_dai_rates
[i
] *
1371 rd
->mcasp
->auxclk_fs_ratio
;
1373 sysclk_freq
= rd
->mcasp
->sysclk_freq
;
1375 ppm
= davinci_mcasp_calc_clk_div(rd
->mcasp
, sysclk_freq
,
1377 if (abs(ppm
) < DAVINCI_MAX_RATE_ERROR_PPM
) {
1379 range
.min
= davinci_mcasp_dai_rates
[i
];
1382 range
.max
= davinci_mcasp_dai_rates
[i
];
1387 dev_dbg(rd
->mcasp
->dev
,
1388 "Frequencies %d-%d -> %d-%d for %d sbits and %d tdm slots\n",
1389 ri
->min
, ri
->max
, range
.min
, range
.max
, sbits
, slots
);
1391 return snd_interval_refine(hw_param_interval(params
, rule
->var
),
1395 static int davinci_mcasp_hw_rule_format(struct snd_pcm_hw_params
*params
,
1396 struct snd_pcm_hw_rule
*rule
)
1398 struct davinci_mcasp_ruledata
*rd
= rule
->private;
1399 struct snd_mask
*fmt
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1400 struct snd_mask nfmt
;
1401 int rate
= params_rate(params
);
1402 int slots
= rd
->mcasp
->tdm_slots
;
1405 snd_mask_none(&nfmt
);
1407 for (i
= 0; i
<= SNDRV_PCM_FORMAT_LAST
; i
++) {
1408 if (snd_mask_test(fmt
, i
)) {
1409 uint sbits
= snd_pcm_format_width(i
);
1410 unsigned int sysclk_freq
;
1413 if (rd
->mcasp
->auxclk_fs_ratio
)
1414 sysclk_freq
= rate
*
1415 rd
->mcasp
->auxclk_fs_ratio
;
1417 sysclk_freq
= rd
->mcasp
->sysclk_freq
;
1419 if (rd
->mcasp
->slot_width
)
1420 sbits
= rd
->mcasp
->slot_width
;
1422 ppm
= davinci_mcasp_calc_clk_div(rd
->mcasp
, sysclk_freq
,
1423 sbits
* slots
* rate
,
1425 if (abs(ppm
) < DAVINCI_MAX_RATE_ERROR_PPM
) {
1426 snd_mask_set(&nfmt
, i
);
1431 dev_dbg(rd
->mcasp
->dev
,
1432 "%d possible sample format for %d Hz and %d tdm slots\n",
1433 count
, rate
, slots
);
1435 return snd_mask_refine(fmt
, &nfmt
);
1438 static int davinci_mcasp_hw_rule_min_periodsize(
1439 struct snd_pcm_hw_params
*params
, struct snd_pcm_hw_rule
*rule
)
1441 struct snd_interval
*period_size
= hw_param_interval(params
,
1442 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
1443 struct snd_interval frames
;
1445 snd_interval_any(&frames
);
1449 return snd_interval_refine(period_size
, &frames
);
1452 static int davinci_mcasp_startup(struct snd_pcm_substream
*substream
,
1453 struct snd_soc_dai
*cpu_dai
)
1455 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(cpu_dai
);
1456 struct davinci_mcasp_ruledata
*ruledata
=
1457 &mcasp
->ruledata
[substream
->stream
];
1458 u32 max_channels
= 0;
1460 int tdm_slots
= mcasp
->tdm_slots
;
1462 /* Do not allow more then one stream per direction */
1463 if (mcasp
->substreams
[substream
->stream
])
1466 mcasp
->substreams
[substream
->stream
] = substream
;
1468 if (mcasp
->tdm_mask
[substream
->stream
])
1469 tdm_slots
= hweight32(mcasp
->tdm_mask
[substream
->stream
]);
1471 if (mcasp
->op_mode
== DAVINCI_MCASP_DIT_MODE
)
1475 * Limit the maximum allowed channels for the first stream:
1476 * number of serializers for the direction * tdm slots per serializer
1478 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1483 for (i
= 0; i
< mcasp
->num_serializer
; i
++) {
1484 if (mcasp
->serial_dir
[i
] == dir
)
1487 ruledata
->serializers
= max_channels
;
1488 ruledata
->mcasp
= mcasp
;
1489 max_channels
*= tdm_slots
;
1491 * If the already active stream has less channels than the calculated
1492 * limit based on the seirializers * tdm_slots, and only one serializer
1493 * is in use we need to use that as a constraint for the second stream.
1494 * Otherwise (first stream or less allowed channels or more than one
1495 * serializer in use) we use the calculated constraint.
1497 if (mcasp
->channels
&& mcasp
->channels
< max_channels
&&
1498 ruledata
->serializers
== 1)
1499 max_channels
= mcasp
->channels
;
1501 * But we can always allow channels upto the amount of
1502 * the available tdm_slots.
1504 if (max_channels
< tdm_slots
)
1505 max_channels
= tdm_slots
;
1507 snd_pcm_hw_constraint_minmax(substream
->runtime
,
1508 SNDRV_PCM_HW_PARAM_CHANNELS
,
1511 snd_pcm_hw_constraint_list(substream
->runtime
,
1512 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1513 &mcasp
->chconstr
[substream
->stream
]);
1515 if (mcasp
->max_format_width
) {
1517 * Only allow formats which require same amount of bits on the
1518 * bus as the currently running stream
1520 ret
= snd_pcm_hw_rule_add(substream
->runtime
, 0,
1521 SNDRV_PCM_HW_PARAM_FORMAT
,
1522 davinci_mcasp_hw_rule_format_width
,
1524 SNDRV_PCM_HW_PARAM_FORMAT
, -1);
1528 else if (mcasp
->slot_width
) {
1529 /* Only allow formats require <= slot_width bits on the bus */
1530 ret
= snd_pcm_hw_rule_add(substream
->runtime
, 0,
1531 SNDRV_PCM_HW_PARAM_FORMAT
,
1532 davinci_mcasp_hw_rule_slot_width
,
1534 SNDRV_PCM_HW_PARAM_FORMAT
, -1);
1540 * If we rely on implicit BCLK divider setting we should
1541 * set constraints based on what we can provide.
1543 if (mcasp
->bclk_master
&& mcasp
->bclk_div
== 0 && mcasp
->sysclk_freq
) {
1544 ret
= snd_pcm_hw_rule_add(substream
->runtime
, 0,
1545 SNDRV_PCM_HW_PARAM_RATE
,
1546 davinci_mcasp_hw_rule_rate
,
1548 SNDRV_PCM_HW_PARAM_FORMAT
, -1);
1551 ret
= snd_pcm_hw_rule_add(substream
->runtime
, 0,
1552 SNDRV_PCM_HW_PARAM_FORMAT
,
1553 davinci_mcasp_hw_rule_format
,
1555 SNDRV_PCM_HW_PARAM_RATE
, -1);
1560 snd_pcm_hw_rule_add(substream
->runtime
, 0,
1561 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1562 davinci_mcasp_hw_rule_min_periodsize
, NULL
,
1563 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1568 static void davinci_mcasp_shutdown(struct snd_pcm_substream
*substream
,
1569 struct snd_soc_dai
*cpu_dai
)
1571 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(cpu_dai
);
1573 mcasp
->substreams
[substream
->stream
] = NULL
;
1574 mcasp
->active_serializers
[substream
->stream
] = 0;
1576 if (mcasp
->op_mode
== DAVINCI_MCASP_DIT_MODE
)
1579 if (!cpu_dai
->active
) {
1580 mcasp
->channels
= 0;
1581 mcasp
->max_format_width
= 0;
1585 static const struct snd_soc_dai_ops davinci_mcasp_dai_ops
= {
1586 .startup
= davinci_mcasp_startup
,
1587 .shutdown
= davinci_mcasp_shutdown
,
1588 .trigger
= davinci_mcasp_trigger
,
1589 .delay
= davinci_mcasp_delay
,
1590 .hw_params
= davinci_mcasp_hw_params
,
1591 .set_fmt
= davinci_mcasp_set_dai_fmt
,
1592 .set_clkdiv
= davinci_mcasp_set_clkdiv
,
1593 .set_sysclk
= davinci_mcasp_set_sysclk
,
1594 .set_tdm_slot
= davinci_mcasp_set_tdm_slot
,
1597 static int davinci_mcasp_dai_probe(struct snd_soc_dai
*dai
)
1599 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(dai
);
1601 dai
->playback_dma_data
= &mcasp
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
];
1602 dai
->capture_dma_data
= &mcasp
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
];
1607 #define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
1609 #define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1610 SNDRV_PCM_FMTBIT_U8 | \
1611 SNDRV_PCM_FMTBIT_S16_LE | \
1612 SNDRV_PCM_FMTBIT_U16_LE | \
1613 SNDRV_PCM_FMTBIT_S24_LE | \
1614 SNDRV_PCM_FMTBIT_U24_LE | \
1615 SNDRV_PCM_FMTBIT_S24_3LE | \
1616 SNDRV_PCM_FMTBIT_U24_3LE | \
1617 SNDRV_PCM_FMTBIT_S32_LE | \
1618 SNDRV_PCM_FMTBIT_U32_LE)
1620 static struct snd_soc_dai_driver davinci_mcasp_dai
[] = {
1622 .name
= "davinci-mcasp.0",
1623 .probe
= davinci_mcasp_dai_probe
,
1626 .channels_max
= 32 * 16,
1627 .rates
= DAVINCI_MCASP_RATES
,
1628 .formats
= DAVINCI_MCASP_PCM_FMTS
,
1632 .channels_max
= 32 * 16,
1633 .rates
= DAVINCI_MCASP_RATES
,
1634 .formats
= DAVINCI_MCASP_PCM_FMTS
,
1636 .ops
= &davinci_mcasp_dai_ops
,
1638 .symmetric_rates
= 1,
1641 .name
= "davinci-mcasp.1",
1642 .probe
= davinci_mcasp_dai_probe
,
1645 .channels_max
= 384,
1646 .rates
= DAVINCI_MCASP_RATES
,
1647 .formats
= DAVINCI_MCASP_PCM_FMTS
,
1649 .ops
= &davinci_mcasp_dai_ops
,
1654 static const struct snd_soc_component_driver davinci_mcasp_component
= {
1655 .name
= "davinci-mcasp",
1658 /* Some HW specific values and defaults. The rest is filled in from DT. */
1659 static struct davinci_mcasp_pdata dm646x_mcasp_pdata
= {
1660 .tx_dma_offset
= 0x400,
1661 .rx_dma_offset
= 0x400,
1662 .version
= MCASP_VERSION_1
,
1665 static struct davinci_mcasp_pdata da830_mcasp_pdata
= {
1666 .tx_dma_offset
= 0x2000,
1667 .rx_dma_offset
= 0x2000,
1668 .version
= MCASP_VERSION_2
,
1671 static struct davinci_mcasp_pdata am33xx_mcasp_pdata
= {
1674 .version
= MCASP_VERSION_3
,
1677 static struct davinci_mcasp_pdata dra7_mcasp_pdata
= {
1678 /* The CFG port offset will be calculated if it is needed */
1681 .version
= MCASP_VERSION_4
,
1684 static const struct of_device_id mcasp_dt_ids
[] = {
1686 .compatible
= "ti,dm646x-mcasp-audio",
1687 .data
= &dm646x_mcasp_pdata
,
1690 .compatible
= "ti,da830-mcasp-audio",
1691 .data
= &da830_mcasp_pdata
,
1694 .compatible
= "ti,am33xx-mcasp-audio",
1695 .data
= &am33xx_mcasp_pdata
,
1698 .compatible
= "ti,dra7-mcasp-audio",
1699 .data
= &dra7_mcasp_pdata
,
1703 MODULE_DEVICE_TABLE(of
, mcasp_dt_ids
);
1705 static int mcasp_reparent_fck(struct platform_device
*pdev
)
1707 struct device_node
*node
= pdev
->dev
.of_node
;
1708 struct clk
*gfclk
, *parent_clk
;
1709 const char *parent_name
;
1715 parent_name
= of_get_property(node
, "fck_parent", NULL
);
1719 dev_warn(&pdev
->dev
, "Update the bindings to use assigned-clocks!\n");
1721 gfclk
= clk_get(&pdev
->dev
, "fck");
1722 if (IS_ERR(gfclk
)) {
1723 dev_err(&pdev
->dev
, "failed to get fck\n");
1724 return PTR_ERR(gfclk
);
1727 parent_clk
= clk_get(NULL
, parent_name
);
1728 if (IS_ERR(parent_clk
)) {
1729 dev_err(&pdev
->dev
, "failed to get parent clock\n");
1730 ret
= PTR_ERR(parent_clk
);
1734 ret
= clk_set_parent(gfclk
, parent_clk
);
1736 dev_err(&pdev
->dev
, "failed to reparent fck\n");
1741 clk_put(parent_clk
);
1747 static struct davinci_mcasp_pdata
*davinci_mcasp_set_pdata_from_of(
1748 struct platform_device
*pdev
)
1750 struct device_node
*np
= pdev
->dev
.of_node
;
1751 struct davinci_mcasp_pdata
*pdata
= NULL
;
1752 const struct of_device_id
*match
=
1753 of_match_device(mcasp_dt_ids
, &pdev
->dev
);
1754 struct of_phandle_args dma_spec
;
1756 const u32
*of_serial_dir32
;
1760 if (pdev
->dev
.platform_data
) {
1761 pdata
= pdev
->dev
.platform_data
;
1762 pdata
->dismod
= DISMOD_LOW
;
1765 pdata
= devm_kmemdup(&pdev
->dev
, match
->data
, sizeof(*pdata
),
1772 /* control shouldn't reach here. something is wrong */
1777 ret
= of_property_read_u32(np
, "op-mode", &val
);
1779 pdata
->op_mode
= val
;
1781 ret
= of_property_read_u32(np
, "tdm-slots", &val
);
1783 if (val
< 2 || val
> 32) {
1785 "tdm-slots must be in rage [2-32]\n");
1790 pdata
->tdm_slots
= val
;
1793 of_serial_dir32
= of_get_property(np
, "serial-dir", &val
);
1795 if (of_serial_dir32
) {
1796 u8
*of_serial_dir
= devm_kzalloc(&pdev
->dev
,
1797 (sizeof(*of_serial_dir
) * val
),
1799 if (!of_serial_dir
) {
1804 for (i
= 0; i
< val
; i
++)
1805 of_serial_dir
[i
] = be32_to_cpup(&of_serial_dir32
[i
]);
1807 pdata
->num_serializer
= val
;
1808 pdata
->serial_dir
= of_serial_dir
;
1811 ret
= of_property_match_string(np
, "dma-names", "tx");
1815 ret
= of_parse_phandle_with_args(np
, "dmas", "#dma-cells", ret
,
1820 pdata
->tx_dma_channel
= dma_spec
.args
[0];
1822 /* RX is not valid in DIT mode */
1823 if (pdata
->op_mode
!= DAVINCI_MCASP_DIT_MODE
) {
1824 ret
= of_property_match_string(np
, "dma-names", "rx");
1828 ret
= of_parse_phandle_with_args(np
, "dmas", "#dma-cells", ret
,
1833 pdata
->rx_dma_channel
= dma_spec
.args
[0];
1836 ret
= of_property_read_u32(np
, "tx-num-evt", &val
);
1838 pdata
->txnumevt
= val
;
1840 ret
= of_property_read_u32(np
, "rx-num-evt", &val
);
1842 pdata
->rxnumevt
= val
;
1844 ret
= of_property_read_u32(np
, "sram-size-playback", &val
);
1846 pdata
->sram_size_playback
= val
;
1848 ret
= of_property_read_u32(np
, "sram-size-capture", &val
);
1850 pdata
->sram_size_capture
= val
;
1852 ret
= of_property_read_u32(np
, "dismod", &val
);
1854 if (val
== 0 || val
== 2 || val
== 3) {
1855 pdata
->dismod
= DISMOD_VAL(val
);
1857 dev_warn(&pdev
->dev
, "Invalid dismod value: %u\n", val
);
1858 pdata
->dismod
= DISMOD_LOW
;
1861 pdata
->dismod
= DISMOD_LOW
;
1868 dev_err(&pdev
->dev
, "Error populating platform data, err %d\n",
1879 static const char *sdma_prefix
= "ti,omap";
1881 static int davinci_mcasp_get_dma_type(struct davinci_mcasp
*mcasp
)
1883 struct dma_chan
*chan
;
1887 if (!mcasp
->dev
->of_node
)
1890 tmp
= mcasp
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
].filter_data
;
1891 chan
= dma_request_chan(mcasp
->dev
, tmp
);
1893 if (PTR_ERR(chan
) != -EPROBE_DEFER
)
1895 "Can't verify DMA configuration (%ld)\n",
1897 return PTR_ERR(chan
);
1899 if (WARN_ON(!chan
->device
|| !chan
->device
->dev
))
1902 if (chan
->device
->dev
->of_node
)
1903 ret
= of_property_read_string(chan
->device
->dev
->of_node
,
1904 "compatible", &tmp
);
1906 dev_dbg(mcasp
->dev
, "DMA controller has no of-node\n");
1908 dma_release_channel(chan
);
1912 dev_dbg(mcasp
->dev
, "DMA controller compatible = \"%s\"\n", tmp
);
1913 if (!strncmp(tmp
, sdma_prefix
, strlen(sdma_prefix
)))
1919 static u32
davinci_mcasp_txdma_offset(struct davinci_mcasp_pdata
*pdata
)
1924 if (pdata
->version
!= MCASP_VERSION_4
)
1925 return pdata
->tx_dma_offset
;
1927 for (i
= 0; i
< pdata
->num_serializer
; i
++) {
1928 if (pdata
->serial_dir
[i
] == TX_MODE
) {
1930 offset
= DAVINCI_MCASP_TXBUF_REG(i
);
1932 pr_err("%s: Only one serializer allowed!\n",
1942 static u32
davinci_mcasp_rxdma_offset(struct davinci_mcasp_pdata
*pdata
)
1947 if (pdata
->version
!= MCASP_VERSION_4
)
1948 return pdata
->rx_dma_offset
;
1950 for (i
= 0; i
< pdata
->num_serializer
; i
++) {
1951 if (pdata
->serial_dir
[i
] == RX_MODE
) {
1953 offset
= DAVINCI_MCASP_RXBUF_REG(i
);
1955 pr_err("%s: Only one serializer allowed!\n",
1965 #ifdef CONFIG_GPIOLIB
1966 static int davinci_mcasp_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
1968 struct davinci_mcasp
*mcasp
= gpiochip_get_data(chip
);
1970 if (mcasp
->num_serializer
&& offset
< mcasp
->num_serializer
&&
1971 mcasp
->serial_dir
[offset
] != INACTIVE_MODE
) {
1972 dev_err(mcasp
->dev
, "AXR%u pin is used for audio\n", offset
);
1976 /* Do not change the PIN yet */
1978 return pm_runtime_get_sync(mcasp
->dev
);
1981 static void davinci_mcasp_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
1983 struct davinci_mcasp
*mcasp
= gpiochip_get_data(chip
);
1985 /* Set the direction to input */
1986 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, BIT(offset
));
1988 /* Set the pin as McASP pin */
1989 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PFUNC_REG
, BIT(offset
));
1991 pm_runtime_put_sync(mcasp
->dev
);
1994 static int davinci_mcasp_gpio_direction_out(struct gpio_chip
*chip
,
1995 unsigned offset
, int value
)
1997 struct davinci_mcasp
*mcasp
= gpiochip_get_data(chip
);
2001 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PDOUT_REG
, BIT(offset
));
2003 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PDOUT_REG
, BIT(offset
));
2005 val
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_PFUNC_REG
);
2006 if (!(val
& BIT(offset
))) {
2007 /* Set the pin as GPIO pin */
2008 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PFUNC_REG
, BIT(offset
));
2010 /* Set the direction to output */
2011 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, BIT(offset
));
2017 static void davinci_mcasp_gpio_set(struct gpio_chip
*chip
, unsigned offset
,
2020 struct davinci_mcasp
*mcasp
= gpiochip_get_data(chip
);
2023 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PDOUT_REG
, BIT(offset
));
2025 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PDOUT_REG
, BIT(offset
));
2028 static int davinci_mcasp_gpio_direction_in(struct gpio_chip
*chip
,
2031 struct davinci_mcasp
*mcasp
= gpiochip_get_data(chip
);
2034 val
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_PFUNC_REG
);
2035 if (!(val
& BIT(offset
))) {
2036 /* Set the direction to input */
2037 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, BIT(offset
));
2039 /* Set the pin as GPIO pin */
2040 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PFUNC_REG
, BIT(offset
));
2046 static int davinci_mcasp_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
2048 struct davinci_mcasp
*mcasp
= gpiochip_get_data(chip
);
2051 val
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_PDSET_REG
);
2052 if (val
& BIT(offset
))
2058 static int davinci_mcasp_gpio_get_direction(struct gpio_chip
*chip
,
2061 struct davinci_mcasp
*mcasp
= gpiochip_get_data(chip
);
2064 val
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_PDIR_REG
);
2065 if (val
& BIT(offset
))
2071 static const struct gpio_chip davinci_mcasp_template_chip
= {
2072 .owner
= THIS_MODULE
,
2073 .request
= davinci_mcasp_gpio_request
,
2074 .free
= davinci_mcasp_gpio_free
,
2075 .direction_output
= davinci_mcasp_gpio_direction_out
,
2076 .set
= davinci_mcasp_gpio_set
,
2077 .direction_input
= davinci_mcasp_gpio_direction_in
,
2078 .get
= davinci_mcasp_gpio_get
,
2079 .get_direction
= davinci_mcasp_gpio_get_direction
,
2084 static int davinci_mcasp_init_gpiochip(struct davinci_mcasp
*mcasp
)
2086 if (!of_property_read_bool(mcasp
->dev
->of_node
, "gpio-controller"))
2089 mcasp
->gpio_chip
= davinci_mcasp_template_chip
;
2090 mcasp
->gpio_chip
.label
= dev_name(mcasp
->dev
);
2091 mcasp
->gpio_chip
.parent
= mcasp
->dev
;
2092 #ifdef CONFIG_OF_GPIO
2093 mcasp
->gpio_chip
.of_node
= mcasp
->dev
->of_node
;
2096 return devm_gpiochip_add_data(mcasp
->dev
, &mcasp
->gpio_chip
, mcasp
);
2099 #else /* CONFIG_GPIOLIB */
2100 static inline int davinci_mcasp_init_gpiochip(struct davinci_mcasp
*mcasp
)
2104 #endif /* CONFIG_GPIOLIB */
2106 static int davinci_mcasp_get_dt_params(struct davinci_mcasp
*mcasp
)
2108 struct device_node
*np
= mcasp
->dev
->of_node
;
2115 ret
= of_property_read_u32(np
, "auxclk-fs-ratio", &val
);
2117 mcasp
->auxclk_fs_ratio
= val
;
2122 static int davinci_mcasp_probe(struct platform_device
*pdev
)
2124 struct snd_dmaengine_dai_dma_data
*dma_data
;
2125 struct resource
*mem
, *res
, *dat
;
2126 struct davinci_mcasp_pdata
*pdata
;
2127 struct davinci_mcasp
*mcasp
;
2133 if (!pdev
->dev
.platform_data
&& !pdev
->dev
.of_node
) {
2134 dev_err(&pdev
->dev
, "No platform data supplied\n");
2138 mcasp
= devm_kzalloc(&pdev
->dev
, sizeof(struct davinci_mcasp
),
2143 pdata
= davinci_mcasp_set_pdata_from_of(pdev
);
2145 dev_err(&pdev
->dev
, "no platform data\n");
2149 mem
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mpu");
2151 dev_warn(mcasp
->dev
,
2152 "\"mpu\" mem resource not found, using index 0\n");
2153 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2155 dev_err(&pdev
->dev
, "no mem resource?\n");
2160 mcasp
->base
= devm_ioremap_resource(&pdev
->dev
, mem
);
2161 if (IS_ERR(mcasp
->base
))
2162 return PTR_ERR(mcasp
->base
);
2164 pm_runtime_enable(&pdev
->dev
);
2166 mcasp
->op_mode
= pdata
->op_mode
;
2167 /* sanity check for tdm slots parameter */
2168 if (mcasp
->op_mode
== DAVINCI_MCASP_IIS_MODE
) {
2169 if (pdata
->tdm_slots
< 2) {
2170 dev_err(&pdev
->dev
, "invalid tdm slots: %d\n",
2172 mcasp
->tdm_slots
= 2;
2173 } else if (pdata
->tdm_slots
> 32) {
2174 dev_err(&pdev
->dev
, "invalid tdm slots: %d\n",
2176 mcasp
->tdm_slots
= 32;
2178 mcasp
->tdm_slots
= pdata
->tdm_slots
;
2182 mcasp
->num_serializer
= pdata
->num_serializer
;
2184 mcasp
->context
.xrsr_regs
= devm_kcalloc(&pdev
->dev
,
2185 mcasp
->num_serializer
, sizeof(u32
),
2187 if (!mcasp
->context
.xrsr_regs
) {
2192 mcasp
->serial_dir
= pdata
->serial_dir
;
2193 mcasp
->version
= pdata
->version
;
2194 mcasp
->txnumevt
= pdata
->txnumevt
;
2195 mcasp
->rxnumevt
= pdata
->rxnumevt
;
2196 mcasp
->dismod
= pdata
->dismod
;
2198 mcasp
->dev
= &pdev
->dev
;
2200 irq
= platform_get_irq_byname(pdev
, "common");
2202 irq_name
= devm_kasprintf(&pdev
->dev
, GFP_KERNEL
, "%s_common",
2203 dev_name(&pdev
->dev
));
2208 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
2209 davinci_mcasp_common_irq_handler
,
2210 IRQF_ONESHOT
| IRQF_SHARED
,
2213 dev_err(&pdev
->dev
, "common IRQ request failed\n");
2217 mcasp
->irq_request
[SNDRV_PCM_STREAM_PLAYBACK
] = XUNDRN
;
2218 mcasp
->irq_request
[SNDRV_PCM_STREAM_CAPTURE
] = ROVRN
;
2221 irq
= platform_get_irq_byname(pdev
, "rx");
2223 irq_name
= devm_kasprintf(&pdev
->dev
, GFP_KERNEL
, "%s_rx",
2224 dev_name(&pdev
->dev
));
2229 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
2230 davinci_mcasp_rx_irq_handler
,
2231 IRQF_ONESHOT
, irq_name
, mcasp
);
2233 dev_err(&pdev
->dev
, "RX IRQ request failed\n");
2237 mcasp
->irq_request
[SNDRV_PCM_STREAM_CAPTURE
] = ROVRN
;
2240 irq
= platform_get_irq_byname(pdev
, "tx");
2242 irq_name
= devm_kasprintf(&pdev
->dev
, GFP_KERNEL
, "%s_tx",
2243 dev_name(&pdev
->dev
));
2248 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
2249 davinci_mcasp_tx_irq_handler
,
2250 IRQF_ONESHOT
, irq_name
, mcasp
);
2252 dev_err(&pdev
->dev
, "TX IRQ request failed\n");
2256 mcasp
->irq_request
[SNDRV_PCM_STREAM_PLAYBACK
] = XUNDRN
;
2259 dat
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dat");
2261 mcasp
->dat_port
= true;
2263 dma_data
= &mcasp
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
];
2265 dma_data
->addr
= dat
->start
;
2267 dma_data
->addr
= mem
->start
+ davinci_mcasp_txdma_offset(pdata
);
2269 dma
= &mcasp
->dma_request
[SNDRV_PCM_STREAM_PLAYBACK
];
2270 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
2274 *dma
= pdata
->tx_dma_channel
;
2276 /* dmaengine filter data for DT and non-DT boot */
2277 if (pdev
->dev
.of_node
)
2278 dma_data
->filter_data
= "tx";
2280 dma_data
->filter_data
= dma
;
2282 /* RX is not valid in DIT mode */
2283 if (mcasp
->op_mode
!= DAVINCI_MCASP_DIT_MODE
) {
2284 dma_data
= &mcasp
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
];
2286 dma_data
->addr
= dat
->start
;
2289 mem
->start
+ davinci_mcasp_rxdma_offset(pdata
);
2291 dma
= &mcasp
->dma_request
[SNDRV_PCM_STREAM_CAPTURE
];
2292 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
2296 *dma
= pdata
->rx_dma_channel
;
2298 /* dmaengine filter data for DT and non-DT boot */
2299 if (pdev
->dev
.of_node
)
2300 dma_data
->filter_data
= "rx";
2302 dma_data
->filter_data
= dma
;
2305 if (mcasp
->version
< MCASP_VERSION_3
) {
2306 mcasp
->fifo_base
= DAVINCI_MCASP_V2_AFIFO_BASE
;
2307 /* dma_params->dma_addr is pointing to the data port address */
2308 mcasp
->dat_port
= true;
2310 mcasp
->fifo_base
= DAVINCI_MCASP_V3_AFIFO_BASE
;
2313 /* Allocate memory for long enough list for all possible
2314 * scenarios. Maximum number tdm slots is 32 and there cannot
2315 * be more serializers than given in the configuration. The
2316 * serializer directions could be taken into account, but it
2317 * would make code much more complex and save only couple of
2320 mcasp
->chconstr
[SNDRV_PCM_STREAM_PLAYBACK
].list
=
2321 devm_kcalloc(mcasp
->dev
,
2322 32 + mcasp
->num_serializer
- 1,
2323 sizeof(unsigned int),
2326 mcasp
->chconstr
[SNDRV_PCM_STREAM_CAPTURE
].list
=
2327 devm_kcalloc(mcasp
->dev
,
2328 32 + mcasp
->num_serializer
- 1,
2329 sizeof(unsigned int),
2332 if (!mcasp
->chconstr
[SNDRV_PCM_STREAM_PLAYBACK
].list
||
2333 !mcasp
->chconstr
[SNDRV_PCM_STREAM_CAPTURE
].list
) {
2338 ret
= davinci_mcasp_set_ch_constraints(mcasp
);
2342 dev_set_drvdata(&pdev
->dev
, mcasp
);
2344 mcasp_reparent_fck(pdev
);
2346 /* All PINS as McASP */
2347 pm_runtime_get_sync(mcasp
->dev
);
2348 mcasp_set_reg(mcasp
, DAVINCI_MCASP_PFUNC_REG
, 0x00000000);
2349 pm_runtime_put(mcasp
->dev
);
2351 ret
= davinci_mcasp_init_gpiochip(mcasp
);
2355 ret
= davinci_mcasp_get_dt_params(mcasp
);
2359 ret
= devm_snd_soc_register_component(&pdev
->dev
,
2360 &davinci_mcasp_component
,
2361 &davinci_mcasp_dai
[pdata
->op_mode
], 1);
2366 ret
= davinci_mcasp_get_dma_type(mcasp
);
2369 ret
= edma_pcm_platform_register(&pdev
->dev
);
2372 ret
= sdma_pcm_platform_register(&pdev
->dev
, "tx", "rx");
2375 dev_err(&pdev
->dev
, "No DMA controller found (%d)\n", ret
);
2382 dev_err(&pdev
->dev
, "register PCM failed: %d\n", ret
);
2389 pm_runtime_disable(&pdev
->dev
);
2393 static int davinci_mcasp_remove(struct platform_device
*pdev
)
2395 pm_runtime_disable(&pdev
->dev
);
2401 static int davinci_mcasp_runtime_suspend(struct device
*dev
)
2403 struct davinci_mcasp
*mcasp
= dev_get_drvdata(dev
);
2404 struct davinci_mcasp_context
*context
= &mcasp
->context
;
2408 for (i
= 0; i
< ARRAY_SIZE(context_regs
); i
++)
2409 context
->config_regs
[i
] = mcasp_get_reg(mcasp
, context_regs
[i
]);
2411 if (mcasp
->txnumevt
) {
2412 reg
= mcasp
->fifo_base
+ MCASP_WFIFOCTL_OFFSET
;
2413 context
->afifo_regs
[0] = mcasp_get_reg(mcasp
, reg
);
2415 if (mcasp
->rxnumevt
) {
2416 reg
= mcasp
->fifo_base
+ MCASP_RFIFOCTL_OFFSET
;
2417 context
->afifo_regs
[1] = mcasp_get_reg(mcasp
, reg
);
2420 for (i
= 0; i
< mcasp
->num_serializer
; i
++)
2421 context
->xrsr_regs
[i
] = mcasp_get_reg(mcasp
,
2422 DAVINCI_MCASP_XRSRCTL_REG(i
));
2427 static int davinci_mcasp_runtime_resume(struct device
*dev
)
2429 struct davinci_mcasp
*mcasp
= dev_get_drvdata(dev
);
2430 struct davinci_mcasp_context
*context
= &mcasp
->context
;
2434 for (i
= 0; i
< ARRAY_SIZE(context_regs
); i
++)
2435 mcasp_set_reg(mcasp
, context_regs
[i
], context
->config_regs
[i
]);
2437 if (mcasp
->txnumevt
) {
2438 reg
= mcasp
->fifo_base
+ MCASP_WFIFOCTL_OFFSET
;
2439 mcasp_set_reg(mcasp
, reg
, context
->afifo_regs
[0]);
2441 if (mcasp
->rxnumevt
) {
2442 reg
= mcasp
->fifo_base
+ MCASP_RFIFOCTL_OFFSET
;
2443 mcasp_set_reg(mcasp
, reg
, context
->afifo_regs
[1]);
2446 for (i
= 0; i
< mcasp
->num_serializer
; i
++)
2447 mcasp_set_reg(mcasp
, DAVINCI_MCASP_XRSRCTL_REG(i
),
2448 context
->xrsr_regs
[i
]);
2455 static const struct dev_pm_ops davinci_mcasp_pm_ops
= {
2456 SET_RUNTIME_PM_OPS(davinci_mcasp_runtime_suspend
,
2457 davinci_mcasp_runtime_resume
,
2461 static struct platform_driver davinci_mcasp_driver
= {
2462 .probe
= davinci_mcasp_probe
,
2463 .remove
= davinci_mcasp_remove
,
2465 .name
= "davinci-mcasp",
2466 .pm
= &davinci_mcasp_pm_ops
,
2467 .of_match_table
= mcasp_dt_ids
,
2471 module_platform_driver(davinci_mcasp_driver
);
2473 MODULE_AUTHOR("Steve Chen");
2474 MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
2475 MODULE_LICENSE("GPL");