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
);
667 if (dir
== SND_SOC_CLOCK_OUT
) {
668 mcasp_set_bits(mcasp
, DAVINCI_MCASP_AHCLKXCTL_REG
, AHCLKXE
);
669 mcasp_set_bits(mcasp
, DAVINCI_MCASP_AHCLKRCTL_REG
, AHCLKRE
);
670 set_bit(PIN_BIT_AHCLKX
, &mcasp
->pdir
);
672 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_AHCLKXCTL_REG
, AHCLKXE
);
673 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_AHCLKRCTL_REG
, AHCLKRE
);
674 clear_bit(PIN_BIT_AHCLKX
, &mcasp
->pdir
);
677 mcasp
->sysclk_freq
= freq
;
679 pm_runtime_put(mcasp
->dev
);
683 /* All serializers must have equal number of channels */
684 static int davinci_mcasp_ch_constraint(struct davinci_mcasp
*mcasp
, int stream
,
687 struct snd_pcm_hw_constraint_list
*cl
= &mcasp
->chconstr
[stream
];
688 unsigned int *list
= (unsigned int *) cl
->list
;
689 int slots
= mcasp
->tdm_slots
;
692 if (mcasp
->tdm_mask
[stream
])
693 slots
= hweight32(mcasp
->tdm_mask
[stream
]);
695 for (i
= 1; i
<= slots
; i
++)
698 for (i
= 2; i
<= serializers
; i
++)
699 list
[count
++] = i
*slots
;
706 static int davinci_mcasp_set_ch_constraints(struct davinci_mcasp
*mcasp
)
708 int rx_serializers
= 0, tx_serializers
= 0, ret
, i
;
710 for (i
= 0; i
< mcasp
->num_serializer
; i
++)
711 if (mcasp
->serial_dir
[i
] == TX_MODE
)
713 else if (mcasp
->serial_dir
[i
] == RX_MODE
)
716 ret
= davinci_mcasp_ch_constraint(mcasp
, SNDRV_PCM_STREAM_PLAYBACK
,
721 ret
= davinci_mcasp_ch_constraint(mcasp
, SNDRV_PCM_STREAM_CAPTURE
,
728 static int davinci_mcasp_set_tdm_slot(struct snd_soc_dai
*dai
,
729 unsigned int tx_mask
,
730 unsigned int rx_mask
,
731 int slots
, int slot_width
)
733 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(dai
);
736 "%s() tx_mask 0x%08x rx_mask 0x%08x slots %d width %d\n",
737 __func__
, tx_mask
, rx_mask
, slots
, slot_width
);
739 if (tx_mask
>= (1<<slots
) || rx_mask
>= (1<<slots
)) {
741 "Bad tdm mask tx: 0x%08x rx: 0x%08x slots %d\n",
742 tx_mask
, rx_mask
, slots
);
747 (slot_width
< 8 || slot_width
> 32 || slot_width
% 4 != 0)) {
748 dev_err(mcasp
->dev
, "%s: Unsupported slot_width %d\n",
749 __func__
, slot_width
);
753 mcasp
->tdm_slots
= slots
;
754 mcasp
->tdm_mask
[SNDRV_PCM_STREAM_PLAYBACK
] = tx_mask
;
755 mcasp
->tdm_mask
[SNDRV_PCM_STREAM_CAPTURE
] = rx_mask
;
756 mcasp
->slot_width
= slot_width
;
758 return davinci_mcasp_set_ch_constraints(mcasp
);
761 static int davinci_config_channel_size(struct davinci_mcasp
*mcasp
,
765 u32 tx_rotate
, rx_rotate
, slot_width
;
766 u32 mask
= (1ULL << sample_width
) - 1;
768 if (mcasp
->slot_width
)
769 slot_width
= mcasp
->slot_width
;
770 else if (mcasp
->max_format_width
)
771 slot_width
= mcasp
->max_format_width
;
773 slot_width
= sample_width
;
776 * right aligned formats: rotate w/ slot_width
777 * left aligned formats: rotate w/ sample_width
780 * right aligned formats: no rotation needed
781 * left aligned formats: rotate w/ (slot_width - sample_width)
783 if ((mcasp
->dai_fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) ==
784 SND_SOC_DAIFMT_RIGHT_J
) {
785 tx_rotate
= (slot_width
/ 4) & 0x7;
788 tx_rotate
= (sample_width
/ 4) & 0x7;
789 rx_rotate
= (slot_width
- sample_width
) / 4;
792 /* mapping of the XSSZ bit-field as described in the datasheet */
793 fmt
= (slot_width
>> 1) - 1;
795 if (mcasp
->op_mode
!= DAVINCI_MCASP_DIT_MODE
) {
796 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_RXFMT_REG
, RXSSZ(fmt
),
798 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_TXFMT_REG
, TXSSZ(fmt
),
800 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_TXFMT_REG
, TXROT(tx_rotate
),
802 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_RXFMT_REG
, RXROT(rx_rotate
),
804 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXMASK_REG
, mask
);
807 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXMASK_REG
, mask
);
812 static int mcasp_common_hw_param(struct davinci_mcasp
*mcasp
, int stream
,
813 int period_words
, int channels
)
815 struct snd_dmaengine_dai_dma_data
*dma_data
= &mcasp
->dma_data
[stream
];
819 u8 slots
= mcasp
->tdm_slots
;
820 u8 max_active_serializers
= (channels
+ slots
- 1) / slots
;
821 u8 max_rx_serializers
, max_tx_serializers
;
822 int active_serializers
, numevt
;
824 /* Default configuration */
825 if (mcasp
->version
< MCASP_VERSION_3
)
826 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PWREMUMGT_REG
, MCASP_SOFT
);
828 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
829 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXSTAT_REG
, 0xFFFFFFFF);
830 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_XEVTCTL_REG
, TXDATADMADIS
);
831 max_tx_serializers
= max_active_serializers
;
833 mcasp
->active_serializers
[SNDRV_PCM_STREAM_CAPTURE
];
835 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXSTAT_REG
, 0xFFFFFFFF);
836 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_REVTCTL_REG
, RXDATADMADIS
);
838 mcasp
->active_serializers
[SNDRV_PCM_STREAM_PLAYBACK
];
839 max_rx_serializers
= max_active_serializers
;
842 for (i
= 0; i
< mcasp
->num_serializer
; i
++) {
843 mcasp_set_bits(mcasp
, DAVINCI_MCASP_XRSRCTL_REG(i
),
844 mcasp
->serial_dir
[i
]);
845 if (mcasp
->serial_dir
[i
] == TX_MODE
&&
846 tx_ser
< max_tx_serializers
) {
847 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_XRSRCTL_REG(i
),
848 mcasp
->dismod
, DISMOD_MASK
);
849 set_bit(PIN_BIT_AXR(i
), &mcasp
->pdir
);
851 } else if (mcasp
->serial_dir
[i
] == RX_MODE
&&
852 rx_ser
< max_rx_serializers
) {
853 clear_bit(PIN_BIT_AXR(i
), &mcasp
->pdir
);
856 /* Inactive or unused pin, set it to inactive */
857 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_XRSRCTL_REG(i
),
858 SRMOD_INACTIVE
, SRMOD_MASK
);
859 /* If unused, set DISMOD for the pin */
860 if (mcasp
->serial_dir
[i
] != INACTIVE_MODE
)
861 mcasp_mod_bits(mcasp
,
862 DAVINCI_MCASP_XRSRCTL_REG(i
),
863 mcasp
->dismod
, DISMOD_MASK
);
864 clear_bit(PIN_BIT_AXR(i
), &mcasp
->pdir
);
868 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
869 active_serializers
= tx_ser
;
870 numevt
= mcasp
->txnumevt
;
871 reg
= mcasp
->fifo_base
+ MCASP_WFIFOCTL_OFFSET
;
873 active_serializers
= rx_ser
;
874 numevt
= mcasp
->rxnumevt
;
875 reg
= mcasp
->fifo_base
+ MCASP_RFIFOCTL_OFFSET
;
878 if (active_serializers
< max_active_serializers
) {
879 dev_warn(mcasp
->dev
, "stream has more channels (%d) than are "
880 "enabled in mcasp (%d)\n", channels
,
881 active_serializers
* slots
);
885 /* AFIFO is not in use */
887 /* Configure the burst size for platform drivers */
888 if (active_serializers
> 1) {
890 * If more than one serializers are in use we have one
891 * DMA request to provide data for all serializers.
892 * For example if three serializers are enabled the DMA
893 * need to transfer three words per DMA request.
895 dma_data
->maxburst
= active_serializers
;
897 dma_data
->maxburst
= 0;
903 if (period_words
% active_serializers
) {
904 dev_err(mcasp
->dev
, "Invalid combination of period words and "
905 "active serializers: %d, %d\n", period_words
,
911 * Calculate the optimal AFIFO depth for platform side:
912 * The number of words for numevt need to be in steps of active
915 numevt
= (numevt
/ active_serializers
) * active_serializers
;
917 while (period_words
% numevt
&& numevt
> 0)
918 numevt
-= active_serializers
;
920 numevt
= active_serializers
;
922 mcasp_mod_bits(mcasp
, reg
, active_serializers
, NUMDMA_MASK
);
923 mcasp_mod_bits(mcasp
, reg
, NUMEVT(numevt
), NUMEVT_MASK
);
925 /* Configure the burst size for platform drivers */
928 dma_data
->maxburst
= numevt
;
931 mcasp
->active_serializers
[stream
] = active_serializers
;
936 static int mcasp_i2s_hw_param(struct davinci_mcasp
*mcasp
, int stream
,
941 int active_serializers
;
945 total_slots
= mcasp
->tdm_slots
;
948 * If more than one serializer is needed, then use them with
949 * all the specified tdm_slots. Otherwise, one serializer can
950 * cope with the transaction using just as many slots as there
951 * are channels in the stream.
953 if (mcasp
->tdm_mask
[stream
]) {
954 active_slots
= hweight32(mcasp
->tdm_mask
[stream
]);
955 active_serializers
= (channels
+ active_slots
- 1) /
957 if (active_serializers
== 1)
958 active_slots
= channels
;
959 for (i
= 0; i
< total_slots
; i
++) {
960 if ((1 << i
) & mcasp
->tdm_mask
[stream
]) {
962 if (--active_slots
<= 0)
967 active_serializers
= (channels
+ total_slots
- 1) / total_slots
;
968 if (active_serializers
== 1)
969 active_slots
= channels
;
971 active_slots
= total_slots
;
973 for (i
= 0; i
< active_slots
; i
++)
977 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, TX_ASYNC
);
979 if (!mcasp
->dat_port
)
982 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
983 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXTDM_REG
, mask
);
984 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMT_REG
, busel
| TXORD
);
985 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
,
986 FSXMOD(total_slots
), FSXMOD(0x1FF));
987 } else if (stream
== SNDRV_PCM_STREAM_CAPTURE
) {
988 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXTDM_REG
, mask
);
989 mcasp_set_bits(mcasp
, DAVINCI_MCASP_RXFMT_REG
, busel
| RXORD
);
990 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
,
991 FSRMOD(total_slots
), FSRMOD(0x1FF));
993 * If McASP is set to be TX/RX synchronous and the playback is
994 * not running already we need to configure the TX slots in
995 * order to have correct FSX on the bus
997 if (mcasp_is_synchronous(mcasp
) && !mcasp
->channels
)
998 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
,
999 FSXMOD(total_slots
), FSXMOD(0x1FF));
1006 static int mcasp_dit_hw_param(struct davinci_mcasp
*mcasp
,
1010 u8
*cs_bytes
= (u8
*) &cs_value
;
1012 /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
1014 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMT_REG
, TXROT(6) | TXSSZ(15));
1016 /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
1017 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, AFSXE
| FSXMOD(0x180));
1019 /* Set the TX tdm : for all the slots */
1020 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXTDM_REG
, 0xFFFFFFFF);
1022 /* Set the TX clock controls : div = 1 and internal */
1023 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXE
| TX_ASYNC
);
1025 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_XEVTCTL_REG
, TXDATADMADIS
);
1027 /* Only 44100 and 48000 are valid, both have the same setting */
1028 mcasp_set_bits(mcasp
, DAVINCI_MCASP_AHCLKXCTL_REG
, AHCLKXDIV(3));
1030 /* Enable the DIT */
1031 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXDITCTL_REG
, DITEN
);
1033 /* Set S/PDIF channel status bits */
1034 cs_bytes
[0] = IEC958_AES0_CON_NOT_COPYRIGHT
;
1035 cs_bytes
[1] = IEC958_AES1_CON_PCM_CODER
;
1039 cs_bytes
[3] |= IEC958_AES3_CON_FS_22050
;
1042 cs_bytes
[3] |= IEC958_AES3_CON_FS_24000
;
1045 cs_bytes
[3] |= IEC958_AES3_CON_FS_32000
;
1048 cs_bytes
[3] |= IEC958_AES3_CON_FS_44100
;
1051 cs_bytes
[3] |= IEC958_AES3_CON_FS_48000
;
1054 cs_bytes
[3] |= IEC958_AES3_CON_FS_88200
;
1057 cs_bytes
[3] |= IEC958_AES3_CON_FS_96000
;
1060 cs_bytes
[3] |= IEC958_AES3_CON_FS_176400
;
1063 cs_bytes
[3] |= IEC958_AES3_CON_FS_192000
;
1066 printk(KERN_WARNING
"unsupported sampling rate: %d\n", rate
);
1070 mcasp_set_reg(mcasp
, DAVINCI_MCASP_DITCSRA_REG
, cs_value
);
1071 mcasp_set_reg(mcasp
, DAVINCI_MCASP_DITCSRB_REG
, cs_value
);
1076 static int davinci_mcasp_calc_clk_div(struct davinci_mcasp
*mcasp
,
1077 unsigned int sysclk_freq
,
1078 unsigned int bclk_freq
, bool set
)
1080 u32 reg
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_AHCLKXCTL_REG
);
1081 int div
= sysclk_freq
/ bclk_freq
;
1082 int rem
= sysclk_freq
% bclk_freq
;
1086 if (div
> (ACLKXDIV_MASK
+ 1)) {
1087 if (reg
& AHCLKXE
) {
1088 aux_div
= div
/ (ACLKXDIV_MASK
+ 1);
1089 if (div
% (ACLKXDIV_MASK
+ 1))
1092 sysclk_freq
/= aux_div
;
1093 div
= sysclk_freq
/ bclk_freq
;
1094 rem
= sysclk_freq
% bclk_freq
;
1096 dev_warn(mcasp
->dev
, "Too fast reference clock (%u)\n",
1103 ((sysclk_freq
/ div
) - bclk_freq
) >
1104 (bclk_freq
- (sysclk_freq
/ (div
+1)))) {
1106 rem
= rem
- bclk_freq
;
1109 error_ppm
= (div
*1000000 + (int)div64_long(1000000LL*rem
,
1110 (int)bclk_freq
)) / div
- 1000000;
1114 dev_info(mcasp
->dev
, "Sample-rate is off by %d PPM\n",
1117 __davinci_mcasp_set_clkdiv(mcasp
, MCASP_CLKDIV_BCLK
, div
, 0);
1119 __davinci_mcasp_set_clkdiv(mcasp
, MCASP_CLKDIV_AUXCLK
,
1126 static inline u32
davinci_mcasp_tx_delay(struct davinci_mcasp
*mcasp
)
1128 if (!mcasp
->txnumevt
)
1131 return mcasp_get_reg(mcasp
, mcasp
->fifo_base
+ MCASP_WFIFOSTS_OFFSET
);
1134 static inline u32
davinci_mcasp_rx_delay(struct davinci_mcasp
*mcasp
)
1136 if (!mcasp
->rxnumevt
)
1139 return mcasp_get_reg(mcasp
, mcasp
->fifo_base
+ MCASP_RFIFOSTS_OFFSET
);
1142 static snd_pcm_sframes_t
davinci_mcasp_delay(
1143 struct snd_pcm_substream
*substream
,
1144 struct snd_soc_dai
*cpu_dai
)
1146 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(cpu_dai
);
1149 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1150 fifo_use
= davinci_mcasp_tx_delay(mcasp
);
1152 fifo_use
= davinci_mcasp_rx_delay(mcasp
);
1155 * Divide the used locations with the channel count to get the
1156 * FIFO usage in samples (don't care about partial samples in the
1159 return fifo_use
/ substream
->runtime
->channels
;
1162 static int davinci_mcasp_hw_params(struct snd_pcm_substream
*substream
,
1163 struct snd_pcm_hw_params
*params
,
1164 struct snd_soc_dai
*cpu_dai
)
1166 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(cpu_dai
);
1168 int channels
= params_channels(params
);
1169 int period_size
= params_period_size(params
);
1172 switch (params_format(params
)) {
1173 case SNDRV_PCM_FORMAT_U8
:
1174 case SNDRV_PCM_FORMAT_S8
:
1178 case SNDRV_PCM_FORMAT_U16_LE
:
1179 case SNDRV_PCM_FORMAT_S16_LE
:
1183 case SNDRV_PCM_FORMAT_U24_3LE
:
1184 case SNDRV_PCM_FORMAT_S24_3LE
:
1188 case SNDRV_PCM_FORMAT_U24_LE
:
1189 case SNDRV_PCM_FORMAT_S24_LE
:
1193 case SNDRV_PCM_FORMAT_U32_LE
:
1194 case SNDRV_PCM_FORMAT_S32_LE
:
1199 printk(KERN_WARNING
"davinci-mcasp: unsupported PCM format");
1203 ret
= davinci_mcasp_set_dai_fmt(cpu_dai
, mcasp
->dai_fmt
);
1208 * If mcasp is BCLK master, and a BCLK divider was not provided by
1209 * the machine driver, we need to calculate the ratio.
1211 if (mcasp
->bclk_master
&& mcasp
->bclk_div
== 0 && mcasp
->sysclk_freq
) {
1212 int slots
= mcasp
->tdm_slots
;
1213 int rate
= params_rate(params
);
1214 int sbits
= params_width(params
);
1216 if (mcasp
->slot_width
)
1217 sbits
= mcasp
->slot_width
;
1219 davinci_mcasp_calc_clk_div(mcasp
, mcasp
->sysclk_freq
,
1220 rate
* sbits
* slots
, true);
1223 ret
= mcasp_common_hw_param(mcasp
, substream
->stream
,
1224 period_size
* channels
, channels
);
1228 if (mcasp
->op_mode
== DAVINCI_MCASP_DIT_MODE
)
1229 ret
= mcasp_dit_hw_param(mcasp
, params_rate(params
));
1231 ret
= mcasp_i2s_hw_param(mcasp
, substream
->stream
,
1237 davinci_config_channel_size(mcasp
, word_length
);
1239 if (mcasp
->op_mode
== DAVINCI_MCASP_IIS_MODE
) {
1240 mcasp
->channels
= channels
;
1241 if (!mcasp
->max_format_width
)
1242 mcasp
->max_format_width
= word_length
;
1248 static int davinci_mcasp_trigger(struct snd_pcm_substream
*substream
,
1249 int cmd
, struct snd_soc_dai
*cpu_dai
)
1251 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(cpu_dai
);
1255 case SNDRV_PCM_TRIGGER_RESUME
:
1256 case SNDRV_PCM_TRIGGER_START
:
1257 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1258 davinci_mcasp_start(mcasp
, substream
->stream
);
1260 case SNDRV_PCM_TRIGGER_SUSPEND
:
1261 case SNDRV_PCM_TRIGGER_STOP
:
1262 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1263 davinci_mcasp_stop(mcasp
, substream
->stream
);
1273 static int davinci_mcasp_hw_rule_slot_width(struct snd_pcm_hw_params
*params
,
1274 struct snd_pcm_hw_rule
*rule
)
1276 struct davinci_mcasp_ruledata
*rd
= rule
->private;
1277 struct snd_mask
*fmt
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1278 struct snd_mask nfmt
;
1281 snd_mask_none(&nfmt
);
1282 slot_width
= rd
->mcasp
->slot_width
;
1284 for (i
= 0; i
<= SNDRV_PCM_FORMAT_LAST
; i
++) {
1285 if (snd_mask_test(fmt
, i
)) {
1286 if (snd_pcm_format_width(i
) <= slot_width
) {
1287 snd_mask_set(&nfmt
, i
);
1292 return snd_mask_refine(fmt
, &nfmt
);
1295 static int davinci_mcasp_hw_rule_format_width(struct snd_pcm_hw_params
*params
,
1296 struct snd_pcm_hw_rule
*rule
)
1298 struct davinci_mcasp_ruledata
*rd
= rule
->private;
1299 struct snd_mask
*fmt
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1300 struct snd_mask nfmt
;
1301 int i
, format_width
;
1303 snd_mask_none(&nfmt
);
1304 format_width
= rd
->mcasp
->max_format_width
;
1306 for (i
= 0; i
<= SNDRV_PCM_FORMAT_LAST
; i
++) {
1307 if (snd_mask_test(fmt
, i
)) {
1308 if (snd_pcm_format_width(i
) == format_width
) {
1309 snd_mask_set(&nfmt
, i
);
1314 return snd_mask_refine(fmt
, &nfmt
);
1317 static const unsigned int davinci_mcasp_dai_rates
[] = {
1318 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
1319 88200, 96000, 176400, 192000,
1322 #define DAVINCI_MAX_RATE_ERROR_PPM 1000
1324 static int davinci_mcasp_hw_rule_rate(struct snd_pcm_hw_params
*params
,
1325 struct snd_pcm_hw_rule
*rule
)
1327 struct davinci_mcasp_ruledata
*rd
= rule
->private;
1328 struct snd_interval
*ri
=
1329 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
1330 int sbits
= params_width(params
);
1331 int slots
= rd
->mcasp
->tdm_slots
;
1332 struct snd_interval range
;
1335 if (rd
->mcasp
->slot_width
)
1336 sbits
= rd
->mcasp
->slot_width
;
1338 snd_interval_any(&range
);
1341 for (i
= 0; i
< ARRAY_SIZE(davinci_mcasp_dai_rates
); i
++) {
1342 if (snd_interval_test(ri
, davinci_mcasp_dai_rates
[i
])) {
1343 uint bclk_freq
= sbits
* slots
*
1344 davinci_mcasp_dai_rates
[i
];
1345 unsigned int sysclk_freq
;
1348 if (rd
->mcasp
->auxclk_fs_ratio
)
1349 sysclk_freq
= davinci_mcasp_dai_rates
[i
] *
1350 rd
->mcasp
->auxclk_fs_ratio
;
1352 sysclk_freq
= rd
->mcasp
->sysclk_freq
;
1354 ppm
= davinci_mcasp_calc_clk_div(rd
->mcasp
, sysclk_freq
,
1356 if (abs(ppm
) < DAVINCI_MAX_RATE_ERROR_PPM
) {
1358 range
.min
= davinci_mcasp_dai_rates
[i
];
1361 range
.max
= davinci_mcasp_dai_rates
[i
];
1366 dev_dbg(rd
->mcasp
->dev
,
1367 "Frequencies %d-%d -> %d-%d for %d sbits and %d tdm slots\n",
1368 ri
->min
, ri
->max
, range
.min
, range
.max
, sbits
, slots
);
1370 return snd_interval_refine(hw_param_interval(params
, rule
->var
),
1374 static int davinci_mcasp_hw_rule_format(struct snd_pcm_hw_params
*params
,
1375 struct snd_pcm_hw_rule
*rule
)
1377 struct davinci_mcasp_ruledata
*rd
= rule
->private;
1378 struct snd_mask
*fmt
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1379 struct snd_mask nfmt
;
1380 int rate
= params_rate(params
);
1381 int slots
= rd
->mcasp
->tdm_slots
;
1384 snd_mask_none(&nfmt
);
1386 for (i
= 0; i
<= SNDRV_PCM_FORMAT_LAST
; i
++) {
1387 if (snd_mask_test(fmt
, i
)) {
1388 uint sbits
= snd_pcm_format_width(i
);
1389 unsigned int sysclk_freq
;
1392 if (rd
->mcasp
->auxclk_fs_ratio
)
1393 sysclk_freq
= rate
*
1394 rd
->mcasp
->auxclk_fs_ratio
;
1396 sysclk_freq
= rd
->mcasp
->sysclk_freq
;
1398 if (rd
->mcasp
->slot_width
)
1399 sbits
= rd
->mcasp
->slot_width
;
1401 ppm
= davinci_mcasp_calc_clk_div(rd
->mcasp
, sysclk_freq
,
1402 sbits
* slots
* rate
,
1404 if (abs(ppm
) < DAVINCI_MAX_RATE_ERROR_PPM
) {
1405 snd_mask_set(&nfmt
, i
);
1410 dev_dbg(rd
->mcasp
->dev
,
1411 "%d possible sample format for %d Hz and %d tdm slots\n",
1412 count
, rate
, slots
);
1414 return snd_mask_refine(fmt
, &nfmt
);
1417 static int davinci_mcasp_hw_rule_min_periodsize(
1418 struct snd_pcm_hw_params
*params
, struct snd_pcm_hw_rule
*rule
)
1420 struct snd_interval
*period_size
= hw_param_interval(params
,
1421 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
1422 struct snd_interval frames
;
1424 snd_interval_any(&frames
);
1428 return snd_interval_refine(period_size
, &frames
);
1431 static int davinci_mcasp_startup(struct snd_pcm_substream
*substream
,
1432 struct snd_soc_dai
*cpu_dai
)
1434 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(cpu_dai
);
1435 struct davinci_mcasp_ruledata
*ruledata
=
1436 &mcasp
->ruledata
[substream
->stream
];
1437 u32 max_channels
= 0;
1439 int tdm_slots
= mcasp
->tdm_slots
;
1441 /* Do not allow more then one stream per direction */
1442 if (mcasp
->substreams
[substream
->stream
])
1445 mcasp
->substreams
[substream
->stream
] = substream
;
1447 if (mcasp
->tdm_mask
[substream
->stream
])
1448 tdm_slots
= hweight32(mcasp
->tdm_mask
[substream
->stream
]);
1450 if (mcasp
->op_mode
== DAVINCI_MCASP_DIT_MODE
)
1454 * Limit the maximum allowed channels for the first stream:
1455 * number of serializers for the direction * tdm slots per serializer
1457 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1462 for (i
= 0; i
< mcasp
->num_serializer
; i
++) {
1463 if (mcasp
->serial_dir
[i
] == dir
)
1466 ruledata
->serializers
= max_channels
;
1467 ruledata
->mcasp
= mcasp
;
1468 max_channels
*= tdm_slots
;
1470 * If the already active stream has less channels than the calculated
1471 * limit based on the seirializers * tdm_slots, and only one serializer
1472 * is in use we need to use that as a constraint for the second stream.
1473 * Otherwise (first stream or less allowed channels or more than one
1474 * serializer in use) we use the calculated constraint.
1476 if (mcasp
->channels
&& mcasp
->channels
< max_channels
&&
1477 ruledata
->serializers
== 1)
1478 max_channels
= mcasp
->channels
;
1480 * But we can always allow channels upto the amount of
1481 * the available tdm_slots.
1483 if (max_channels
< tdm_slots
)
1484 max_channels
= tdm_slots
;
1486 snd_pcm_hw_constraint_minmax(substream
->runtime
,
1487 SNDRV_PCM_HW_PARAM_CHANNELS
,
1490 snd_pcm_hw_constraint_list(substream
->runtime
,
1491 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1492 &mcasp
->chconstr
[substream
->stream
]);
1494 if (mcasp
->max_format_width
) {
1496 * Only allow formats which require same amount of bits on the
1497 * bus as the currently running stream
1499 ret
= snd_pcm_hw_rule_add(substream
->runtime
, 0,
1500 SNDRV_PCM_HW_PARAM_FORMAT
,
1501 davinci_mcasp_hw_rule_format_width
,
1503 SNDRV_PCM_HW_PARAM_FORMAT
, -1);
1507 else if (mcasp
->slot_width
) {
1508 /* Only allow formats require <= slot_width bits on the bus */
1509 ret
= snd_pcm_hw_rule_add(substream
->runtime
, 0,
1510 SNDRV_PCM_HW_PARAM_FORMAT
,
1511 davinci_mcasp_hw_rule_slot_width
,
1513 SNDRV_PCM_HW_PARAM_FORMAT
, -1);
1519 * If we rely on implicit BCLK divider setting we should
1520 * set constraints based on what we can provide.
1522 if (mcasp
->bclk_master
&& mcasp
->bclk_div
== 0 && mcasp
->sysclk_freq
) {
1523 ret
= snd_pcm_hw_rule_add(substream
->runtime
, 0,
1524 SNDRV_PCM_HW_PARAM_RATE
,
1525 davinci_mcasp_hw_rule_rate
,
1527 SNDRV_PCM_HW_PARAM_FORMAT
, -1);
1530 ret
= snd_pcm_hw_rule_add(substream
->runtime
, 0,
1531 SNDRV_PCM_HW_PARAM_FORMAT
,
1532 davinci_mcasp_hw_rule_format
,
1534 SNDRV_PCM_HW_PARAM_RATE
, -1);
1539 snd_pcm_hw_rule_add(substream
->runtime
, 0,
1540 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1541 davinci_mcasp_hw_rule_min_periodsize
, NULL
,
1542 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1547 static void davinci_mcasp_shutdown(struct snd_pcm_substream
*substream
,
1548 struct snd_soc_dai
*cpu_dai
)
1550 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(cpu_dai
);
1552 mcasp
->substreams
[substream
->stream
] = NULL
;
1553 mcasp
->active_serializers
[substream
->stream
] = 0;
1555 if (mcasp
->op_mode
== DAVINCI_MCASP_DIT_MODE
)
1558 if (!cpu_dai
->active
) {
1559 mcasp
->channels
= 0;
1560 mcasp
->max_format_width
= 0;
1564 static const struct snd_soc_dai_ops davinci_mcasp_dai_ops
= {
1565 .startup
= davinci_mcasp_startup
,
1566 .shutdown
= davinci_mcasp_shutdown
,
1567 .trigger
= davinci_mcasp_trigger
,
1568 .delay
= davinci_mcasp_delay
,
1569 .hw_params
= davinci_mcasp_hw_params
,
1570 .set_fmt
= davinci_mcasp_set_dai_fmt
,
1571 .set_clkdiv
= davinci_mcasp_set_clkdiv
,
1572 .set_sysclk
= davinci_mcasp_set_sysclk
,
1573 .set_tdm_slot
= davinci_mcasp_set_tdm_slot
,
1576 static int davinci_mcasp_dai_probe(struct snd_soc_dai
*dai
)
1578 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(dai
);
1580 dai
->playback_dma_data
= &mcasp
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
];
1581 dai
->capture_dma_data
= &mcasp
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
];
1586 #define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
1588 #define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1589 SNDRV_PCM_FMTBIT_U8 | \
1590 SNDRV_PCM_FMTBIT_S16_LE | \
1591 SNDRV_PCM_FMTBIT_U16_LE | \
1592 SNDRV_PCM_FMTBIT_S24_LE | \
1593 SNDRV_PCM_FMTBIT_U24_LE | \
1594 SNDRV_PCM_FMTBIT_S24_3LE | \
1595 SNDRV_PCM_FMTBIT_U24_3LE | \
1596 SNDRV_PCM_FMTBIT_S32_LE | \
1597 SNDRV_PCM_FMTBIT_U32_LE)
1599 static struct snd_soc_dai_driver davinci_mcasp_dai
[] = {
1601 .name
= "davinci-mcasp.0",
1602 .probe
= davinci_mcasp_dai_probe
,
1605 .channels_max
= 32 * 16,
1606 .rates
= DAVINCI_MCASP_RATES
,
1607 .formats
= DAVINCI_MCASP_PCM_FMTS
,
1611 .channels_max
= 32 * 16,
1612 .rates
= DAVINCI_MCASP_RATES
,
1613 .formats
= DAVINCI_MCASP_PCM_FMTS
,
1615 .ops
= &davinci_mcasp_dai_ops
,
1617 .symmetric_rates
= 1,
1620 .name
= "davinci-mcasp.1",
1621 .probe
= davinci_mcasp_dai_probe
,
1624 .channels_max
= 384,
1625 .rates
= DAVINCI_MCASP_RATES
,
1626 .formats
= DAVINCI_MCASP_PCM_FMTS
,
1628 .ops
= &davinci_mcasp_dai_ops
,
1633 static const struct snd_soc_component_driver davinci_mcasp_component
= {
1634 .name
= "davinci-mcasp",
1637 /* Some HW specific values and defaults. The rest is filled in from DT. */
1638 static struct davinci_mcasp_pdata dm646x_mcasp_pdata
= {
1639 .tx_dma_offset
= 0x400,
1640 .rx_dma_offset
= 0x400,
1641 .version
= MCASP_VERSION_1
,
1644 static struct davinci_mcasp_pdata da830_mcasp_pdata
= {
1645 .tx_dma_offset
= 0x2000,
1646 .rx_dma_offset
= 0x2000,
1647 .version
= MCASP_VERSION_2
,
1650 static struct davinci_mcasp_pdata am33xx_mcasp_pdata
= {
1653 .version
= MCASP_VERSION_3
,
1656 static struct davinci_mcasp_pdata dra7_mcasp_pdata
= {
1657 /* The CFG port offset will be calculated if it is needed */
1660 .version
= MCASP_VERSION_4
,
1663 static const struct of_device_id mcasp_dt_ids
[] = {
1665 .compatible
= "ti,dm646x-mcasp-audio",
1666 .data
= &dm646x_mcasp_pdata
,
1669 .compatible
= "ti,da830-mcasp-audio",
1670 .data
= &da830_mcasp_pdata
,
1673 .compatible
= "ti,am33xx-mcasp-audio",
1674 .data
= &am33xx_mcasp_pdata
,
1677 .compatible
= "ti,dra7-mcasp-audio",
1678 .data
= &dra7_mcasp_pdata
,
1682 MODULE_DEVICE_TABLE(of
, mcasp_dt_ids
);
1684 static int mcasp_reparent_fck(struct platform_device
*pdev
)
1686 struct device_node
*node
= pdev
->dev
.of_node
;
1687 struct clk
*gfclk
, *parent_clk
;
1688 const char *parent_name
;
1694 parent_name
= of_get_property(node
, "fck_parent", NULL
);
1698 dev_warn(&pdev
->dev
, "Update the bindings to use assigned-clocks!\n");
1700 gfclk
= clk_get(&pdev
->dev
, "fck");
1701 if (IS_ERR(gfclk
)) {
1702 dev_err(&pdev
->dev
, "failed to get fck\n");
1703 return PTR_ERR(gfclk
);
1706 parent_clk
= clk_get(NULL
, parent_name
);
1707 if (IS_ERR(parent_clk
)) {
1708 dev_err(&pdev
->dev
, "failed to get parent clock\n");
1709 ret
= PTR_ERR(parent_clk
);
1713 ret
= clk_set_parent(gfclk
, parent_clk
);
1715 dev_err(&pdev
->dev
, "failed to reparent fck\n");
1720 clk_put(parent_clk
);
1726 static struct davinci_mcasp_pdata
*davinci_mcasp_set_pdata_from_of(
1727 struct platform_device
*pdev
)
1729 struct device_node
*np
= pdev
->dev
.of_node
;
1730 struct davinci_mcasp_pdata
*pdata
= NULL
;
1731 const struct of_device_id
*match
=
1732 of_match_device(mcasp_dt_ids
, &pdev
->dev
);
1733 struct of_phandle_args dma_spec
;
1735 const u32
*of_serial_dir32
;
1739 if (pdev
->dev
.platform_data
) {
1740 pdata
= pdev
->dev
.platform_data
;
1741 pdata
->dismod
= DISMOD_LOW
;
1744 pdata
= devm_kmemdup(&pdev
->dev
, match
->data
, sizeof(*pdata
),
1751 /* control shouldn't reach here. something is wrong */
1756 ret
= of_property_read_u32(np
, "op-mode", &val
);
1758 pdata
->op_mode
= val
;
1760 ret
= of_property_read_u32(np
, "tdm-slots", &val
);
1762 if (val
< 2 || val
> 32) {
1764 "tdm-slots must be in rage [2-32]\n");
1769 pdata
->tdm_slots
= val
;
1772 of_serial_dir32
= of_get_property(np
, "serial-dir", &val
);
1774 if (of_serial_dir32
) {
1775 u8
*of_serial_dir
= devm_kzalloc(&pdev
->dev
,
1776 (sizeof(*of_serial_dir
) * val
),
1778 if (!of_serial_dir
) {
1783 for (i
= 0; i
< val
; i
++)
1784 of_serial_dir
[i
] = be32_to_cpup(&of_serial_dir32
[i
]);
1786 pdata
->num_serializer
= val
;
1787 pdata
->serial_dir
= of_serial_dir
;
1790 ret
= of_property_match_string(np
, "dma-names", "tx");
1794 ret
= of_parse_phandle_with_args(np
, "dmas", "#dma-cells", ret
,
1799 pdata
->tx_dma_channel
= dma_spec
.args
[0];
1801 /* RX is not valid in DIT mode */
1802 if (pdata
->op_mode
!= DAVINCI_MCASP_DIT_MODE
) {
1803 ret
= of_property_match_string(np
, "dma-names", "rx");
1807 ret
= of_parse_phandle_with_args(np
, "dmas", "#dma-cells", ret
,
1812 pdata
->rx_dma_channel
= dma_spec
.args
[0];
1815 ret
= of_property_read_u32(np
, "tx-num-evt", &val
);
1817 pdata
->txnumevt
= val
;
1819 ret
= of_property_read_u32(np
, "rx-num-evt", &val
);
1821 pdata
->rxnumevt
= val
;
1823 ret
= of_property_read_u32(np
, "sram-size-playback", &val
);
1825 pdata
->sram_size_playback
= val
;
1827 ret
= of_property_read_u32(np
, "sram-size-capture", &val
);
1829 pdata
->sram_size_capture
= val
;
1831 ret
= of_property_read_u32(np
, "dismod", &val
);
1833 if (val
== 0 || val
== 2 || val
== 3) {
1834 pdata
->dismod
= DISMOD_VAL(val
);
1836 dev_warn(&pdev
->dev
, "Invalid dismod value: %u\n", val
);
1837 pdata
->dismod
= DISMOD_LOW
;
1840 pdata
->dismod
= DISMOD_LOW
;
1847 dev_err(&pdev
->dev
, "Error populating platform data, err %d\n",
1858 static const char *sdma_prefix
= "ti,omap";
1860 static int davinci_mcasp_get_dma_type(struct davinci_mcasp
*mcasp
)
1862 struct dma_chan
*chan
;
1866 if (!mcasp
->dev
->of_node
)
1869 tmp
= mcasp
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
].filter_data
;
1870 chan
= dma_request_chan(mcasp
->dev
, tmp
);
1872 if (PTR_ERR(chan
) != -EPROBE_DEFER
)
1874 "Can't verify DMA configuration (%ld)\n",
1876 return PTR_ERR(chan
);
1878 if (WARN_ON(!chan
->device
|| !chan
->device
->dev
))
1881 if (chan
->device
->dev
->of_node
)
1882 ret
= of_property_read_string(chan
->device
->dev
->of_node
,
1883 "compatible", &tmp
);
1885 dev_dbg(mcasp
->dev
, "DMA controller has no of-node\n");
1887 dma_release_channel(chan
);
1891 dev_dbg(mcasp
->dev
, "DMA controller compatible = \"%s\"\n", tmp
);
1892 if (!strncmp(tmp
, sdma_prefix
, strlen(sdma_prefix
)))
1898 static u32
davinci_mcasp_txdma_offset(struct davinci_mcasp_pdata
*pdata
)
1903 if (pdata
->version
!= MCASP_VERSION_4
)
1904 return pdata
->tx_dma_offset
;
1906 for (i
= 0; i
< pdata
->num_serializer
; i
++) {
1907 if (pdata
->serial_dir
[i
] == TX_MODE
) {
1909 offset
= DAVINCI_MCASP_TXBUF_REG(i
);
1911 pr_err("%s: Only one serializer allowed!\n",
1921 static u32
davinci_mcasp_rxdma_offset(struct davinci_mcasp_pdata
*pdata
)
1926 if (pdata
->version
!= MCASP_VERSION_4
)
1927 return pdata
->rx_dma_offset
;
1929 for (i
= 0; i
< pdata
->num_serializer
; i
++) {
1930 if (pdata
->serial_dir
[i
] == RX_MODE
) {
1932 offset
= DAVINCI_MCASP_RXBUF_REG(i
);
1934 pr_err("%s: Only one serializer allowed!\n",
1944 #ifdef CONFIG_GPIOLIB
1945 static int davinci_mcasp_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
1947 struct davinci_mcasp
*mcasp
= gpiochip_get_data(chip
);
1949 if (mcasp
->num_serializer
&& offset
< mcasp
->num_serializer
&&
1950 mcasp
->serial_dir
[offset
] != INACTIVE_MODE
) {
1951 dev_err(mcasp
->dev
, "AXR%u pin is used for audio\n", offset
);
1955 /* Do not change the PIN yet */
1957 return pm_runtime_get_sync(mcasp
->dev
);
1960 static void davinci_mcasp_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
1962 struct davinci_mcasp
*mcasp
= gpiochip_get_data(chip
);
1964 /* Set the direction to input */
1965 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, BIT(offset
));
1967 /* Set the pin as McASP pin */
1968 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PFUNC_REG
, BIT(offset
));
1970 pm_runtime_put_sync(mcasp
->dev
);
1973 static int davinci_mcasp_gpio_direction_out(struct gpio_chip
*chip
,
1974 unsigned offset
, int value
)
1976 struct davinci_mcasp
*mcasp
= gpiochip_get_data(chip
);
1980 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PDOUT_REG
, BIT(offset
));
1982 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PDOUT_REG
, BIT(offset
));
1984 val
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_PFUNC_REG
);
1985 if (!(val
& BIT(offset
))) {
1986 /* Set the pin as GPIO pin */
1987 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PFUNC_REG
, BIT(offset
));
1989 /* Set the direction to output */
1990 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, BIT(offset
));
1996 static void davinci_mcasp_gpio_set(struct gpio_chip
*chip
, unsigned offset
,
1999 struct davinci_mcasp
*mcasp
= gpiochip_get_data(chip
);
2002 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PDOUT_REG
, BIT(offset
));
2004 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PDOUT_REG
, BIT(offset
));
2007 static int davinci_mcasp_gpio_direction_in(struct gpio_chip
*chip
,
2010 struct davinci_mcasp
*mcasp
= gpiochip_get_data(chip
);
2013 val
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_PFUNC_REG
);
2014 if (!(val
& BIT(offset
))) {
2015 /* Set the direction to input */
2016 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, BIT(offset
));
2018 /* Set the pin as GPIO pin */
2019 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PFUNC_REG
, BIT(offset
));
2025 static int davinci_mcasp_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
2027 struct davinci_mcasp
*mcasp
= gpiochip_get_data(chip
);
2030 val
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_PDSET_REG
);
2031 if (val
& BIT(offset
))
2037 static int davinci_mcasp_gpio_get_direction(struct gpio_chip
*chip
,
2040 struct davinci_mcasp
*mcasp
= gpiochip_get_data(chip
);
2043 val
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_PDIR_REG
);
2044 if (val
& BIT(offset
))
2050 static const struct gpio_chip davinci_mcasp_template_chip
= {
2051 .owner
= THIS_MODULE
,
2052 .request
= davinci_mcasp_gpio_request
,
2053 .free
= davinci_mcasp_gpio_free
,
2054 .direction_output
= davinci_mcasp_gpio_direction_out
,
2055 .set
= davinci_mcasp_gpio_set
,
2056 .direction_input
= davinci_mcasp_gpio_direction_in
,
2057 .get
= davinci_mcasp_gpio_get
,
2058 .get_direction
= davinci_mcasp_gpio_get_direction
,
2063 static int davinci_mcasp_init_gpiochip(struct davinci_mcasp
*mcasp
)
2065 if (!of_property_read_bool(mcasp
->dev
->of_node
, "gpio-controller"))
2068 mcasp
->gpio_chip
= davinci_mcasp_template_chip
;
2069 mcasp
->gpio_chip
.label
= dev_name(mcasp
->dev
);
2070 mcasp
->gpio_chip
.parent
= mcasp
->dev
;
2071 #ifdef CONFIG_OF_GPIO
2072 mcasp
->gpio_chip
.of_node
= mcasp
->dev
->of_node
;
2075 return devm_gpiochip_add_data(mcasp
->dev
, &mcasp
->gpio_chip
, mcasp
);
2078 #else /* CONFIG_GPIOLIB */
2079 static inline int davinci_mcasp_init_gpiochip(struct davinci_mcasp
*mcasp
)
2083 #endif /* CONFIG_GPIOLIB */
2085 static int davinci_mcasp_get_dt_params(struct davinci_mcasp
*mcasp
)
2087 struct device_node
*np
= mcasp
->dev
->of_node
;
2094 ret
= of_property_read_u32(np
, "auxclk-fs-ratio", &val
);
2096 mcasp
->auxclk_fs_ratio
= val
;
2101 static int davinci_mcasp_probe(struct platform_device
*pdev
)
2103 struct snd_dmaengine_dai_dma_data
*dma_data
;
2104 struct resource
*mem
, *res
, *dat
;
2105 struct davinci_mcasp_pdata
*pdata
;
2106 struct davinci_mcasp
*mcasp
;
2112 if (!pdev
->dev
.platform_data
&& !pdev
->dev
.of_node
) {
2113 dev_err(&pdev
->dev
, "No platform data supplied\n");
2117 mcasp
= devm_kzalloc(&pdev
->dev
, sizeof(struct davinci_mcasp
),
2122 pdata
= davinci_mcasp_set_pdata_from_of(pdev
);
2124 dev_err(&pdev
->dev
, "no platform data\n");
2128 mem
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mpu");
2130 dev_warn(mcasp
->dev
,
2131 "\"mpu\" mem resource not found, using index 0\n");
2132 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2134 dev_err(&pdev
->dev
, "no mem resource?\n");
2139 mcasp
->base
= devm_ioremap_resource(&pdev
->dev
, mem
);
2140 if (IS_ERR(mcasp
->base
))
2141 return PTR_ERR(mcasp
->base
);
2143 pm_runtime_enable(&pdev
->dev
);
2145 mcasp
->op_mode
= pdata
->op_mode
;
2146 /* sanity check for tdm slots parameter */
2147 if (mcasp
->op_mode
== DAVINCI_MCASP_IIS_MODE
) {
2148 if (pdata
->tdm_slots
< 2) {
2149 dev_err(&pdev
->dev
, "invalid tdm slots: %d\n",
2151 mcasp
->tdm_slots
= 2;
2152 } else if (pdata
->tdm_slots
> 32) {
2153 dev_err(&pdev
->dev
, "invalid tdm slots: %d\n",
2155 mcasp
->tdm_slots
= 32;
2157 mcasp
->tdm_slots
= pdata
->tdm_slots
;
2161 mcasp
->num_serializer
= pdata
->num_serializer
;
2163 mcasp
->context
.xrsr_regs
= devm_kcalloc(&pdev
->dev
,
2164 mcasp
->num_serializer
, sizeof(u32
),
2166 if (!mcasp
->context
.xrsr_regs
) {
2171 mcasp
->serial_dir
= pdata
->serial_dir
;
2172 mcasp
->version
= pdata
->version
;
2173 mcasp
->txnumevt
= pdata
->txnumevt
;
2174 mcasp
->rxnumevt
= pdata
->rxnumevt
;
2175 mcasp
->dismod
= pdata
->dismod
;
2177 mcasp
->dev
= &pdev
->dev
;
2179 irq
= platform_get_irq_byname(pdev
, "common");
2181 irq_name
= devm_kasprintf(&pdev
->dev
, GFP_KERNEL
, "%s_common",
2182 dev_name(&pdev
->dev
));
2187 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
2188 davinci_mcasp_common_irq_handler
,
2189 IRQF_ONESHOT
| IRQF_SHARED
,
2192 dev_err(&pdev
->dev
, "common IRQ request failed\n");
2196 mcasp
->irq_request
[SNDRV_PCM_STREAM_PLAYBACK
] = XUNDRN
;
2197 mcasp
->irq_request
[SNDRV_PCM_STREAM_CAPTURE
] = ROVRN
;
2200 irq
= platform_get_irq_byname(pdev
, "rx");
2202 irq_name
= devm_kasprintf(&pdev
->dev
, GFP_KERNEL
, "%s_rx",
2203 dev_name(&pdev
->dev
));
2208 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
2209 davinci_mcasp_rx_irq_handler
,
2210 IRQF_ONESHOT
, irq_name
, mcasp
);
2212 dev_err(&pdev
->dev
, "RX IRQ request failed\n");
2216 mcasp
->irq_request
[SNDRV_PCM_STREAM_CAPTURE
] = ROVRN
;
2219 irq
= platform_get_irq_byname(pdev
, "tx");
2221 irq_name
= devm_kasprintf(&pdev
->dev
, GFP_KERNEL
, "%s_tx",
2222 dev_name(&pdev
->dev
));
2227 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
2228 davinci_mcasp_tx_irq_handler
,
2229 IRQF_ONESHOT
, irq_name
, mcasp
);
2231 dev_err(&pdev
->dev
, "TX IRQ request failed\n");
2235 mcasp
->irq_request
[SNDRV_PCM_STREAM_PLAYBACK
] = XUNDRN
;
2238 dat
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dat");
2240 mcasp
->dat_port
= true;
2242 dma_data
= &mcasp
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
];
2244 dma_data
->addr
= dat
->start
;
2246 dma_data
->addr
= mem
->start
+ davinci_mcasp_txdma_offset(pdata
);
2248 dma
= &mcasp
->dma_request
[SNDRV_PCM_STREAM_PLAYBACK
];
2249 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
2253 *dma
= pdata
->tx_dma_channel
;
2255 /* dmaengine filter data for DT and non-DT boot */
2256 if (pdev
->dev
.of_node
)
2257 dma_data
->filter_data
= "tx";
2259 dma_data
->filter_data
= dma
;
2261 /* RX is not valid in DIT mode */
2262 if (mcasp
->op_mode
!= DAVINCI_MCASP_DIT_MODE
) {
2263 dma_data
= &mcasp
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
];
2265 dma_data
->addr
= dat
->start
;
2268 mem
->start
+ davinci_mcasp_rxdma_offset(pdata
);
2270 dma
= &mcasp
->dma_request
[SNDRV_PCM_STREAM_CAPTURE
];
2271 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
2275 *dma
= pdata
->rx_dma_channel
;
2277 /* dmaengine filter data for DT and non-DT boot */
2278 if (pdev
->dev
.of_node
)
2279 dma_data
->filter_data
= "rx";
2281 dma_data
->filter_data
= dma
;
2284 if (mcasp
->version
< MCASP_VERSION_3
) {
2285 mcasp
->fifo_base
= DAVINCI_MCASP_V2_AFIFO_BASE
;
2286 /* dma_params->dma_addr is pointing to the data port address */
2287 mcasp
->dat_port
= true;
2289 mcasp
->fifo_base
= DAVINCI_MCASP_V3_AFIFO_BASE
;
2292 /* Allocate memory for long enough list for all possible
2293 * scenarios. Maximum number tdm slots is 32 and there cannot
2294 * be more serializers than given in the configuration. The
2295 * serializer directions could be taken into account, but it
2296 * would make code much more complex and save only couple of
2299 mcasp
->chconstr
[SNDRV_PCM_STREAM_PLAYBACK
].list
=
2300 devm_kcalloc(mcasp
->dev
,
2301 32 + mcasp
->num_serializer
- 1,
2302 sizeof(unsigned int),
2305 mcasp
->chconstr
[SNDRV_PCM_STREAM_CAPTURE
].list
=
2306 devm_kcalloc(mcasp
->dev
,
2307 32 + mcasp
->num_serializer
- 1,
2308 sizeof(unsigned int),
2311 if (!mcasp
->chconstr
[SNDRV_PCM_STREAM_PLAYBACK
].list
||
2312 !mcasp
->chconstr
[SNDRV_PCM_STREAM_CAPTURE
].list
) {
2317 ret
= davinci_mcasp_set_ch_constraints(mcasp
);
2321 dev_set_drvdata(&pdev
->dev
, mcasp
);
2323 mcasp_reparent_fck(pdev
);
2325 /* All PINS as McASP */
2326 pm_runtime_get_sync(mcasp
->dev
);
2327 mcasp_set_reg(mcasp
, DAVINCI_MCASP_PFUNC_REG
, 0x00000000);
2328 pm_runtime_put(mcasp
->dev
);
2330 ret
= davinci_mcasp_init_gpiochip(mcasp
);
2334 ret
= davinci_mcasp_get_dt_params(mcasp
);
2338 ret
= devm_snd_soc_register_component(&pdev
->dev
,
2339 &davinci_mcasp_component
,
2340 &davinci_mcasp_dai
[pdata
->op_mode
], 1);
2345 ret
= davinci_mcasp_get_dma_type(mcasp
);
2348 ret
= edma_pcm_platform_register(&pdev
->dev
);
2351 ret
= sdma_pcm_platform_register(&pdev
->dev
, "tx", "rx");
2354 dev_err(&pdev
->dev
, "No DMA controller found (%d)\n", ret
);
2361 dev_err(&pdev
->dev
, "register PCM failed: %d\n", ret
);
2368 pm_runtime_disable(&pdev
->dev
);
2372 static int davinci_mcasp_remove(struct platform_device
*pdev
)
2374 pm_runtime_disable(&pdev
->dev
);
2380 static int davinci_mcasp_runtime_suspend(struct device
*dev
)
2382 struct davinci_mcasp
*mcasp
= dev_get_drvdata(dev
);
2383 struct davinci_mcasp_context
*context
= &mcasp
->context
;
2387 for (i
= 0; i
< ARRAY_SIZE(context_regs
); i
++)
2388 context
->config_regs
[i
] = mcasp_get_reg(mcasp
, context_regs
[i
]);
2390 if (mcasp
->txnumevt
) {
2391 reg
= mcasp
->fifo_base
+ MCASP_WFIFOCTL_OFFSET
;
2392 context
->afifo_regs
[0] = mcasp_get_reg(mcasp
, reg
);
2394 if (mcasp
->rxnumevt
) {
2395 reg
= mcasp
->fifo_base
+ MCASP_RFIFOCTL_OFFSET
;
2396 context
->afifo_regs
[1] = mcasp_get_reg(mcasp
, reg
);
2399 for (i
= 0; i
< mcasp
->num_serializer
; i
++)
2400 context
->xrsr_regs
[i
] = mcasp_get_reg(mcasp
,
2401 DAVINCI_MCASP_XRSRCTL_REG(i
));
2406 static int davinci_mcasp_runtime_resume(struct device
*dev
)
2408 struct davinci_mcasp
*mcasp
= dev_get_drvdata(dev
);
2409 struct davinci_mcasp_context
*context
= &mcasp
->context
;
2413 for (i
= 0; i
< ARRAY_SIZE(context_regs
); i
++)
2414 mcasp_set_reg(mcasp
, context_regs
[i
], context
->config_regs
[i
]);
2416 if (mcasp
->txnumevt
) {
2417 reg
= mcasp
->fifo_base
+ MCASP_WFIFOCTL_OFFSET
;
2418 mcasp_set_reg(mcasp
, reg
, context
->afifo_regs
[0]);
2420 if (mcasp
->rxnumevt
) {
2421 reg
= mcasp
->fifo_base
+ MCASP_RFIFOCTL_OFFSET
;
2422 mcasp_set_reg(mcasp
, reg
, context
->afifo_regs
[1]);
2425 for (i
= 0; i
< mcasp
->num_serializer
; i
++)
2426 mcasp_set_reg(mcasp
, DAVINCI_MCASP_XRSRCTL_REG(i
),
2427 context
->xrsr_regs
[i
]);
2434 static const struct dev_pm_ops davinci_mcasp_pm_ops
= {
2435 SET_RUNTIME_PM_OPS(davinci_mcasp_runtime_suspend
,
2436 davinci_mcasp_runtime_resume
,
2440 static struct platform_driver davinci_mcasp_driver
= {
2441 .probe
= davinci_mcasp_probe
,
2442 .remove
= davinci_mcasp_remove
,
2444 .name
= "davinci-mcasp",
2445 .pm
= &davinci_mcasp_pm_ops
,
2446 .of_match_table
= mcasp_dt_ids
,
2450 module_platform_driver(davinci_mcasp_driver
);
2452 MODULE_AUTHOR("Steve Chen");
2453 MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
2454 MODULE_LICENSE("GPL");