2 * ALSA SoC McASP Audio Layer for TI DAVINCI processor
4 * Multi-channel Audio Serial Port Driver
6 * Author: Nirmal Pandey <n-pandey@ti.com>,
7 * Suresh Rajashekara <suresh.r@ti.com>
8 * Steve Chen <schen@.mvista.com>
10 * Copyright: (C) 2009 MontaVista Software, Inc., <source@mvista.com>
11 * Copyright: (C) 2009 Texas Instruments, India
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
24 #include <linux/clk.h>
25 #include <linux/pm_runtime.h>
27 #include <linux/of_platform.h>
28 #include <linux/of_device.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/initval.h>
34 #include <sound/soc.h>
35 #include <sound/dmaengine_pcm.h>
37 #include "davinci-pcm.h"
38 #include "davinci-mcasp.h"
40 struct davinci_mcasp
{
41 struct davinci_pcm_dma_params dma_params
[2];
42 struct snd_dmaengine_dai_dma_data dma_data
[2];
47 /* McASP specific data */
56 /* McASP FIFO related */
62 #ifdef CONFIG_PM_SLEEP
75 static inline void mcasp_set_bits(struct davinci_mcasp
*mcasp
, u32 offset
,
78 void __iomem
*reg
= mcasp
->base
+ offset
;
79 __raw_writel(__raw_readl(reg
) | val
, reg
);
82 static inline void mcasp_clr_bits(struct davinci_mcasp
*mcasp
, u32 offset
,
85 void __iomem
*reg
= mcasp
->base
+ offset
;
86 __raw_writel((__raw_readl(reg
) & ~(val
)), reg
);
89 static inline void mcasp_mod_bits(struct davinci_mcasp
*mcasp
, u32 offset
,
92 void __iomem
*reg
= mcasp
->base
+ offset
;
93 __raw_writel((__raw_readl(reg
) & ~mask
) | val
, reg
);
96 static inline void mcasp_set_reg(struct davinci_mcasp
*mcasp
, u32 offset
,
99 __raw_writel(val
, mcasp
->base
+ offset
);
102 static inline u32
mcasp_get_reg(struct davinci_mcasp
*mcasp
, u32 offset
)
104 return (u32
)__raw_readl(mcasp
->base
+ offset
);
107 static void mcasp_set_ctl_reg(struct davinci_mcasp
*mcasp
, u32 ctl_reg
, u32 val
)
111 mcasp_set_bits(mcasp
, ctl_reg
, val
);
113 /* programming GBLCTL needs to read back from GBLCTL and verfiy */
114 /* loop count is to avoid the lock-up */
115 for (i
= 0; i
< 1000; i
++) {
116 if ((mcasp_get_reg(mcasp
, ctl_reg
) & val
) == val
)
120 if (i
== 1000 && ((mcasp_get_reg(mcasp
, ctl_reg
) & val
) != val
))
121 printk(KERN_ERR
"GBLCTL write error\n");
124 static bool mcasp_is_synchronous(struct davinci_mcasp
*mcasp
)
126 u32 rxfmctl
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
);
127 u32 aclkxctl
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
);
129 return !(aclkxctl
& TX_ASYNC
) && rxfmctl
& AFSRE
;
132 static void mcasp_start_rx(struct davinci_mcasp
*mcasp
)
134 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLR_REG
, RXHCLKRST
);
135 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLR_REG
, RXCLKRST
);
138 * When ASYNC == 0 the transmit and receive sections operate
139 * synchronously from the transmit clock and frame sync. We need to make
140 * sure that the TX signlas are enabled when starting reception.
142 if (mcasp_is_synchronous(mcasp
)) {
143 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXHCLKRST
);
144 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXCLKRST
);
147 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLR_REG
, RXSERCLR
);
148 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXBUF_REG
, 0);
150 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLR_REG
, RXSMRST
);
151 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLR_REG
, RXFSRST
);
152 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXBUF_REG
, 0);
154 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLR_REG
, RXSMRST
);
155 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLR_REG
, RXFSRST
);
157 if (mcasp_is_synchronous(mcasp
))
158 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXFSRST
);
161 static void mcasp_start_tx(struct davinci_mcasp
*mcasp
)
166 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXHCLKRST
);
167 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXCLKRST
);
168 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXSERCLR
);
169 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXBUF_REG
, 0);
171 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXSMRST
);
172 mcasp_set_ctl_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, TXFSRST
);
173 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXBUF_REG
, 0);
174 for (i
= 0; i
< mcasp
->num_serializer
; i
++) {
175 if (mcasp
->serial_dir
[i
] == TX_MODE
) {
181 /* wait for TX ready */
183 while (!(mcasp_get_reg(mcasp
, DAVINCI_MCASP_XRSRCTL_REG(offset
)) &
184 TXSTATE
) && (cnt
< 100000))
187 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXBUF_REG
, 0);
190 static void davinci_mcasp_start(struct davinci_mcasp
*mcasp
, int stream
)
196 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
197 if (mcasp
->txnumevt
) { /* enable FIFO */
198 reg
= mcasp
->fifo_base
+ MCASP_WFIFOCTL_OFFSET
;
199 mcasp_clr_bits(mcasp
, reg
, FIFO_ENABLE
);
200 mcasp_set_bits(mcasp
, reg
, FIFO_ENABLE
);
202 mcasp_start_tx(mcasp
);
204 if (mcasp
->rxnumevt
) { /* enable FIFO */
205 reg
= mcasp
->fifo_base
+ MCASP_RFIFOCTL_OFFSET
;
206 mcasp_clr_bits(mcasp
, reg
, FIFO_ENABLE
);
207 mcasp_set_bits(mcasp
, reg
, FIFO_ENABLE
);
209 mcasp_start_rx(mcasp
);
213 static void mcasp_stop_rx(struct davinci_mcasp
*mcasp
)
216 * In synchronous mode stop the TX clocks if no other stream is
219 if (mcasp_is_synchronous(mcasp
) && !mcasp
->streams
)
220 mcasp_set_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, 0);
222 mcasp_set_reg(mcasp
, DAVINCI_MCASP_GBLCTLR_REG
, 0);
223 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXSTAT_REG
, 0xFFFFFFFF);
226 static void mcasp_stop_tx(struct davinci_mcasp
*mcasp
)
231 * In synchronous mode keep TX clocks running if the capture stream is
234 if (mcasp_is_synchronous(mcasp
) && mcasp
->streams
)
235 val
= TXHCLKRST
| TXCLKRST
| TXFSRST
;
237 mcasp_set_reg(mcasp
, DAVINCI_MCASP_GBLCTLX_REG
, val
);
238 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXSTAT_REG
, 0xFFFFFFFF);
241 static void davinci_mcasp_stop(struct davinci_mcasp
*mcasp
, int stream
)
247 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
248 if (mcasp
->txnumevt
) { /* disable FIFO */
249 reg
= mcasp
->fifo_base
+ MCASP_WFIFOCTL_OFFSET
;
250 mcasp_clr_bits(mcasp
, reg
, FIFO_ENABLE
);
252 mcasp_stop_tx(mcasp
);
254 if (mcasp
->rxnumevt
) { /* disable FIFO */
255 reg
= mcasp
->fifo_base
+ MCASP_RFIFOCTL_OFFSET
;
256 mcasp_clr_bits(mcasp
, reg
, FIFO_ENABLE
);
258 mcasp_stop_rx(mcasp
);
262 static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai
*cpu_dai
,
265 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(cpu_dai
);
267 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
268 case SND_SOC_DAIFMT_DSP_B
:
269 case SND_SOC_DAIFMT_AC97
:
270 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, FSXDUR
);
271 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, FSRDUR
);
274 /* configure a full-word SYNC pulse (LRCLK) */
275 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, FSXDUR
);
276 mcasp_set_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, FSRDUR
);
278 /* make 1st data bit occur one ACLK cycle after the frame sync */
279 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMT_REG
, FSXDLY(1));
280 mcasp_set_bits(mcasp
, DAVINCI_MCASP_RXFMT_REG
, FSRDLY(1));
284 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
285 case SND_SOC_DAIFMT_CBS_CFS
:
286 /* codec is clock and frame slave */
287 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXE
);
288 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, AFSXE
);
290 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRE
);
291 mcasp_set_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, AFSRE
);
293 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, ACLKX
| ACLKR
);
294 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, AFSX
| AFSR
);
296 case SND_SOC_DAIFMT_CBM_CFS
:
297 /* codec is clock master and frame slave */
298 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXE
);
299 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, AFSXE
);
301 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRE
);
302 mcasp_set_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, AFSRE
);
304 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, ACLKX
| ACLKR
);
305 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, AFSX
| AFSR
);
307 case SND_SOC_DAIFMT_CBM_CFM
:
308 /* codec is clock and frame master */
309 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXE
);
310 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, AFSXE
);
312 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRE
);
313 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, AFSRE
);
315 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
,
316 ACLKX
| AHCLKX
| AFSX
| ACLKR
| AHCLKR
| AFSR
);
323 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
324 case SND_SOC_DAIFMT_IB_NF
:
325 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXPOL
);
326 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, FSXPOL
);
328 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRPOL
);
329 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, FSRPOL
);
332 case SND_SOC_DAIFMT_NB_IF
:
333 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXPOL
);
334 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, FSXPOL
);
336 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRPOL
);
337 mcasp_set_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, FSRPOL
);
340 case SND_SOC_DAIFMT_IB_IF
:
341 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXPOL
);
342 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, FSXPOL
);
344 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRPOL
);
345 mcasp_set_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, FSRPOL
);
348 case SND_SOC_DAIFMT_NB_NF
:
349 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXPOL
);
350 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, FSXPOL
);
352 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, ACLKRPOL
);
353 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, FSRPOL
);
363 static int davinci_mcasp_set_clkdiv(struct snd_soc_dai
*dai
, int div_id
, int div
)
365 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(dai
);
368 case 0: /* MCLK divider */
369 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_AHCLKXCTL_REG
,
370 AHCLKXDIV(div
- 1), AHCLKXDIV_MASK
);
371 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_AHCLKRCTL_REG
,
372 AHCLKRDIV(div
- 1), AHCLKRDIV_MASK
);
375 case 1: /* BCLK divider */
376 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
,
377 ACLKXDIV(div
- 1), ACLKXDIV_MASK
);
378 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
,
379 ACLKRDIV(div
- 1), ACLKRDIV_MASK
);
382 case 2: /* BCLK/LRCLK ratio */
383 mcasp
->bclk_lrclk_ratio
= div
;
393 static int davinci_mcasp_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
394 unsigned int freq
, int dir
)
396 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(dai
);
398 if (dir
== SND_SOC_CLOCK_OUT
) {
399 mcasp_set_bits(mcasp
, DAVINCI_MCASP_AHCLKXCTL_REG
, AHCLKXE
);
400 mcasp_set_bits(mcasp
, DAVINCI_MCASP_AHCLKRCTL_REG
, AHCLKRE
);
401 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, AHCLKX
);
403 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_AHCLKXCTL_REG
, AHCLKXE
);
404 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_AHCLKRCTL_REG
, AHCLKRE
);
405 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, AHCLKX
);
411 static int davinci_config_channel_size(struct davinci_mcasp
*mcasp
,
415 u32 tx_rotate
= (word_length
/ 4) & 0x7;
416 u32 rx_rotate
= (32 - word_length
) / 4;
417 u32 mask
= (1ULL << word_length
) - 1;
420 * if s BCLK-to-LRCLK ratio has been configured via the set_clkdiv()
421 * callback, take it into account here. That allows us to for example
422 * send 32 bits per channel to the codec, while only 16 of them carry
424 * The clock ratio is given for a full period of data (for I2S format
425 * both left and right channels), so it has to be divided by number of
426 * tdm-slots (for I2S - divided by 2).
428 if (mcasp
->bclk_lrclk_ratio
)
429 word_length
= mcasp
->bclk_lrclk_ratio
/ mcasp
->tdm_slots
;
431 /* mapping of the XSSZ bit-field as described in the datasheet */
432 fmt
= (word_length
>> 1) - 1;
434 if (mcasp
->op_mode
!= DAVINCI_MCASP_DIT_MODE
) {
435 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_RXFMT_REG
, RXSSZ(fmt
),
437 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_TXFMT_REG
, TXSSZ(fmt
),
439 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_TXFMT_REG
, TXROT(tx_rotate
),
441 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_RXFMT_REG
, RXROT(rx_rotate
),
443 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXMASK_REG
, mask
);
446 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXMASK_REG
, mask
);
451 static int davinci_hw_common_param(struct davinci_mcasp
*mcasp
, int stream
,
458 u8 slots
= mcasp
->tdm_slots
;
459 u8 max_active_serializers
= (channels
+ slots
- 1) / slots
;
461 /* Default configuration */
462 if (mcasp
->version
!= MCASP_VERSION_4
)
463 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PWREMUMGT_REG
, MCASP_SOFT
);
465 /* All PINS as McASP */
466 mcasp_set_reg(mcasp
, DAVINCI_MCASP_PFUNC_REG
, 0x00000000);
468 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
469 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXSTAT_REG
, 0xFFFFFFFF);
470 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_XEVTCTL_REG
, TXDATADMADIS
);
472 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXSTAT_REG
, 0xFFFFFFFF);
473 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_REVTCTL_REG
, RXDATADMADIS
);
476 for (i
= 0; i
< mcasp
->num_serializer
; i
++) {
477 mcasp_set_bits(mcasp
, DAVINCI_MCASP_XRSRCTL_REG(i
),
478 mcasp
->serial_dir
[i
]);
479 if (mcasp
->serial_dir
[i
] == TX_MODE
&&
480 tx_ser
< max_active_serializers
) {
481 mcasp_set_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, AXR(i
));
483 } else if (mcasp
->serial_dir
[i
] == RX_MODE
&&
484 rx_ser
< max_active_serializers
) {
485 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_PDIR_REG
, AXR(i
));
488 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_XRSRCTL_REG(i
),
489 SRMOD_INACTIVE
, SRMOD_MASK
);
493 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
498 if (ser
< max_active_serializers
) {
499 dev_warn(mcasp
->dev
, "stream has more channels (%d) than are "
500 "enabled in mcasp (%d)\n", channels
, ser
* slots
);
504 if (mcasp
->txnumevt
&& stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
505 if (mcasp
->txnumevt
* tx_ser
> 64)
508 reg
= mcasp
->fifo_base
+ MCASP_WFIFOCTL_OFFSET
;
509 mcasp_mod_bits(mcasp
, reg
, tx_ser
, NUMDMA_MASK
);
510 mcasp_mod_bits(mcasp
, reg
, ((mcasp
->txnumevt
* tx_ser
) << 8),
514 if (mcasp
->rxnumevt
&& stream
== SNDRV_PCM_STREAM_CAPTURE
) {
515 if (mcasp
->rxnumevt
* rx_ser
> 64)
518 reg
= mcasp
->fifo_base
+ MCASP_RFIFOCTL_OFFSET
;
519 mcasp_mod_bits(mcasp
, reg
, rx_ser
, NUMDMA_MASK
);
520 mcasp_mod_bits(mcasp
, reg
, ((mcasp
->rxnumevt
* rx_ser
) << 8),
527 static void davinci_hw_param(struct davinci_mcasp
*mcasp
, int stream
)
533 active_slots
= (mcasp
->tdm_slots
> 31) ? 32 : mcasp
->tdm_slots
;
534 for (i
= 0; i
< active_slots
; i
++)
537 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, TX_ASYNC
);
539 if (!mcasp
->dat_port
)
542 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
543 /* bit stream is MSB first with no delay */
545 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXTDM_REG
, mask
);
546 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMT_REG
, busel
| TXORD
);
548 if ((mcasp
->tdm_slots
>= 2) && (mcasp
->tdm_slots
<= 32))
549 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
,
550 FSXMOD(mcasp
->tdm_slots
), FSXMOD(0x1FF));
552 printk(KERN_ERR
"playback tdm slot %d not supported\n",
555 /* bit stream is MSB first with no delay */
557 mcasp_set_bits(mcasp
, DAVINCI_MCASP_RXFMT_REG
, busel
| RXORD
);
558 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXTDM_REG
, mask
);
560 if ((mcasp
->tdm_slots
>= 2) && (mcasp
->tdm_slots
<= 32))
561 mcasp_mod_bits(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
,
562 FSRMOD(mcasp
->tdm_slots
), FSRMOD(0x1FF));
564 printk(KERN_ERR
"capture tdm slot %d not supported\n",
570 static void davinci_hw_dit_param(struct davinci_mcasp
*mcasp
)
572 /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
574 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXFMT_REG
, TXROT(6) | TXSSZ(15));
576 /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
577 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, AFSXE
| FSXMOD(0x180));
579 /* Set the TX tdm : for all the slots */
580 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXTDM_REG
, 0xFFFFFFFF);
582 /* Set the TX clock controls : div = 1 and internal */
583 mcasp_set_bits(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, ACLKXE
| TX_ASYNC
);
585 mcasp_clr_bits(mcasp
, DAVINCI_MCASP_XEVTCTL_REG
, TXDATADMADIS
);
587 /* Only 44100 and 48000 are valid, both have the same setting */
588 mcasp_set_bits(mcasp
, DAVINCI_MCASP_AHCLKXCTL_REG
, AHCLKXDIV(3));
591 mcasp_set_bits(mcasp
, DAVINCI_MCASP_TXDITCTL_REG
, DITEN
);
594 static int davinci_mcasp_hw_params(struct snd_pcm_substream
*substream
,
595 struct snd_pcm_hw_params
*params
,
596 struct snd_soc_dai
*cpu_dai
)
598 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(cpu_dai
);
599 struct davinci_pcm_dma_params
*dma_params
=
600 &mcasp
->dma_params
[substream
->stream
];
601 struct snd_dmaengine_dai_dma_data
*dma_data
=
602 &mcasp
->dma_data
[substream
->stream
];
605 u8 slots
= mcasp
->tdm_slots
;
606 u8 active_serializers
;
608 struct snd_interval
*pcm_channels
= hw_param_interval(params
,
609 SNDRV_PCM_HW_PARAM_CHANNELS
);
610 channels
= pcm_channels
->min
;
612 active_serializers
= (channels
+ slots
- 1) / slots
;
614 if (davinci_hw_common_param(mcasp
, substream
->stream
, channels
) == -EINVAL
)
616 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
617 fifo_level
= mcasp
->txnumevt
* active_serializers
;
619 fifo_level
= mcasp
->rxnumevt
* active_serializers
;
621 if (mcasp
->op_mode
== DAVINCI_MCASP_DIT_MODE
)
622 davinci_hw_dit_param(mcasp
);
624 davinci_hw_param(mcasp
, substream
->stream
);
626 switch (params_format(params
)) {
627 case SNDRV_PCM_FORMAT_U8
:
628 case SNDRV_PCM_FORMAT_S8
:
629 dma_params
->data_type
= 1;
633 case SNDRV_PCM_FORMAT_U16_LE
:
634 case SNDRV_PCM_FORMAT_S16_LE
:
635 dma_params
->data_type
= 2;
639 case SNDRV_PCM_FORMAT_U24_3LE
:
640 case SNDRV_PCM_FORMAT_S24_3LE
:
641 dma_params
->data_type
= 3;
645 case SNDRV_PCM_FORMAT_U24_LE
:
646 case SNDRV_PCM_FORMAT_S24_LE
:
647 case SNDRV_PCM_FORMAT_U32_LE
:
648 case SNDRV_PCM_FORMAT_S32_LE
:
649 dma_params
->data_type
= 4;
654 printk(KERN_WARNING
"davinci-mcasp: unsupported PCM format");
658 if (mcasp
->version
== MCASP_VERSION_2
&& !fifo_level
)
659 dma_params
->acnt
= 4;
661 dma_params
->acnt
= dma_params
->data_type
;
663 dma_params
->fifo_level
= fifo_level
;
664 dma_data
->maxburst
= fifo_level
;
666 davinci_config_channel_size(mcasp
, word_length
);
671 static int davinci_mcasp_trigger(struct snd_pcm_substream
*substream
,
672 int cmd
, struct snd_soc_dai
*cpu_dai
)
674 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(cpu_dai
);
678 case SNDRV_PCM_TRIGGER_RESUME
:
679 case SNDRV_PCM_TRIGGER_START
:
680 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
681 ret
= pm_runtime_get_sync(mcasp
->dev
);
682 if (IS_ERR_VALUE(ret
))
683 dev_err(mcasp
->dev
, "pm_runtime_get_sync() failed\n");
684 davinci_mcasp_start(mcasp
, substream
->stream
);
687 case SNDRV_PCM_TRIGGER_SUSPEND
:
688 davinci_mcasp_stop(mcasp
, substream
->stream
);
689 ret
= pm_runtime_put_sync(mcasp
->dev
);
690 if (IS_ERR_VALUE(ret
))
691 dev_err(mcasp
->dev
, "pm_runtime_put_sync() failed\n");
694 case SNDRV_PCM_TRIGGER_STOP
:
695 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
696 davinci_mcasp_stop(mcasp
, substream
->stream
);
706 static int davinci_mcasp_startup(struct snd_pcm_substream
*substream
,
707 struct snd_soc_dai
*dai
)
709 struct davinci_mcasp
*mcasp
= snd_soc_dai_get_drvdata(dai
);
711 if (mcasp
->version
== MCASP_VERSION_4
)
712 snd_soc_dai_set_dma_data(dai
, substream
,
713 &mcasp
->dma_data
[substream
->stream
]);
715 snd_soc_dai_set_dma_data(dai
, substream
, mcasp
->dma_params
);
720 static const struct snd_soc_dai_ops davinci_mcasp_dai_ops
= {
721 .startup
= davinci_mcasp_startup
,
722 .trigger
= davinci_mcasp_trigger
,
723 .hw_params
= davinci_mcasp_hw_params
,
724 .set_fmt
= davinci_mcasp_set_dai_fmt
,
725 .set_clkdiv
= davinci_mcasp_set_clkdiv
,
726 .set_sysclk
= davinci_mcasp_set_sysclk
,
729 #define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
731 #define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
732 SNDRV_PCM_FMTBIT_U8 | \
733 SNDRV_PCM_FMTBIT_S16_LE | \
734 SNDRV_PCM_FMTBIT_U16_LE | \
735 SNDRV_PCM_FMTBIT_S24_LE | \
736 SNDRV_PCM_FMTBIT_U24_LE | \
737 SNDRV_PCM_FMTBIT_S24_3LE | \
738 SNDRV_PCM_FMTBIT_U24_3LE | \
739 SNDRV_PCM_FMTBIT_S32_LE | \
740 SNDRV_PCM_FMTBIT_U32_LE)
742 static struct snd_soc_dai_driver davinci_mcasp_dai
[] = {
744 .name
= "davinci-mcasp.0",
747 .channels_max
= 32 * 16,
748 .rates
= DAVINCI_MCASP_RATES
,
749 .formats
= DAVINCI_MCASP_PCM_FMTS
,
753 .channels_max
= 32 * 16,
754 .rates
= DAVINCI_MCASP_RATES
,
755 .formats
= DAVINCI_MCASP_PCM_FMTS
,
757 .ops
= &davinci_mcasp_dai_ops
,
761 .name
= "davinci-mcasp.1",
765 .rates
= DAVINCI_MCASP_RATES
,
766 .formats
= DAVINCI_MCASP_PCM_FMTS
,
768 .ops
= &davinci_mcasp_dai_ops
,
773 static const struct snd_soc_component_driver davinci_mcasp_component
= {
774 .name
= "davinci-mcasp",
777 /* Some HW specific values and defaults. The rest is filled in from DT. */
778 static struct snd_platform_data dm646x_mcasp_pdata
= {
779 .tx_dma_offset
= 0x400,
780 .rx_dma_offset
= 0x400,
781 .asp_chan_q
= EVENTQ_0
,
782 .version
= MCASP_VERSION_1
,
785 static struct snd_platform_data da830_mcasp_pdata
= {
786 .tx_dma_offset
= 0x2000,
787 .rx_dma_offset
= 0x2000,
788 .asp_chan_q
= EVENTQ_0
,
789 .version
= MCASP_VERSION_2
,
792 static struct snd_platform_data am33xx_mcasp_pdata
= {
795 .asp_chan_q
= EVENTQ_0
,
796 .version
= MCASP_VERSION_3
,
799 static struct snd_platform_data dra7_mcasp_pdata
= {
800 .tx_dma_offset
= 0x200,
801 .rx_dma_offset
= 0x284,
802 .asp_chan_q
= EVENTQ_0
,
803 .version
= MCASP_VERSION_4
,
806 static const struct of_device_id mcasp_dt_ids
[] = {
808 .compatible
= "ti,dm646x-mcasp-audio",
809 .data
= &dm646x_mcasp_pdata
,
812 .compatible
= "ti,da830-mcasp-audio",
813 .data
= &da830_mcasp_pdata
,
816 .compatible
= "ti,am33xx-mcasp-audio",
817 .data
= &am33xx_mcasp_pdata
,
820 .compatible
= "ti,dra7-mcasp-audio",
821 .data
= &dra7_mcasp_pdata
,
825 MODULE_DEVICE_TABLE(of
, mcasp_dt_ids
);
827 static int mcasp_reparent_fck(struct platform_device
*pdev
)
829 struct device_node
*node
= pdev
->dev
.of_node
;
830 struct clk
*gfclk
, *parent_clk
;
831 const char *parent_name
;
837 parent_name
= of_get_property(node
, "fck_parent", NULL
);
841 gfclk
= clk_get(&pdev
->dev
, "fck");
843 dev_err(&pdev
->dev
, "failed to get fck\n");
844 return PTR_ERR(gfclk
);
847 parent_clk
= clk_get(NULL
, parent_name
);
848 if (IS_ERR(parent_clk
)) {
849 dev_err(&pdev
->dev
, "failed to get parent clock\n");
850 ret
= PTR_ERR(parent_clk
);
854 ret
= clk_set_parent(gfclk
, parent_clk
);
856 dev_err(&pdev
->dev
, "failed to reparent fck\n");
867 static struct snd_platform_data
*davinci_mcasp_set_pdata_from_of(
868 struct platform_device
*pdev
)
870 struct device_node
*np
= pdev
->dev
.of_node
;
871 struct snd_platform_data
*pdata
= NULL
;
872 const struct of_device_id
*match
=
873 of_match_device(mcasp_dt_ids
, &pdev
->dev
);
874 struct of_phandle_args dma_spec
;
876 const u32
*of_serial_dir32
;
880 if (pdev
->dev
.platform_data
) {
881 pdata
= pdev
->dev
.platform_data
;
884 pdata
= (struct snd_platform_data
*) match
->data
;
886 /* control shouldn't reach here. something is wrong */
891 ret
= of_property_read_u32(np
, "op-mode", &val
);
893 pdata
->op_mode
= val
;
895 ret
= of_property_read_u32(np
, "tdm-slots", &val
);
897 if (val
< 2 || val
> 32) {
899 "tdm-slots must be in rage [2-32]\n");
904 pdata
->tdm_slots
= val
;
907 of_serial_dir32
= of_get_property(np
, "serial-dir", &val
);
909 if (of_serial_dir32
) {
910 u8
*of_serial_dir
= devm_kzalloc(&pdev
->dev
,
911 (sizeof(*of_serial_dir
) * val
),
913 if (!of_serial_dir
) {
918 for (i
= 0; i
< val
; i
++)
919 of_serial_dir
[i
] = be32_to_cpup(&of_serial_dir32
[i
]);
921 pdata
->num_serializer
= val
;
922 pdata
->serial_dir
= of_serial_dir
;
925 ret
= of_property_match_string(np
, "dma-names", "tx");
929 ret
= of_parse_phandle_with_args(np
, "dmas", "#dma-cells", ret
,
934 pdata
->tx_dma_channel
= dma_spec
.args
[0];
936 ret
= of_property_match_string(np
, "dma-names", "rx");
940 ret
= of_parse_phandle_with_args(np
, "dmas", "#dma-cells", ret
,
945 pdata
->rx_dma_channel
= dma_spec
.args
[0];
947 ret
= of_property_read_u32(np
, "tx-num-evt", &val
);
949 pdata
->txnumevt
= val
;
951 ret
= of_property_read_u32(np
, "rx-num-evt", &val
);
953 pdata
->rxnumevt
= val
;
955 ret
= of_property_read_u32(np
, "sram-size-playback", &val
);
957 pdata
->sram_size_playback
= val
;
959 ret
= of_property_read_u32(np
, "sram-size-capture", &val
);
961 pdata
->sram_size_capture
= val
;
967 dev_err(&pdev
->dev
, "Error populating platform data, err %d\n",
974 static int davinci_mcasp_probe(struct platform_device
*pdev
)
976 struct davinci_pcm_dma_params
*dma_data
;
977 struct resource
*mem
, *ioarea
, *res
, *dat
;
978 struct snd_platform_data
*pdata
;
979 struct davinci_mcasp
*mcasp
;
982 if (!pdev
->dev
.platform_data
&& !pdev
->dev
.of_node
) {
983 dev_err(&pdev
->dev
, "No platform data supplied\n");
987 mcasp
= devm_kzalloc(&pdev
->dev
, sizeof(struct davinci_mcasp
),
992 pdata
= davinci_mcasp_set_pdata_from_of(pdev
);
994 dev_err(&pdev
->dev
, "no platform data\n");
998 mem
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mpu");
1000 dev_warn(mcasp
->dev
,
1001 "\"mpu\" mem resource not found, using index 0\n");
1002 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1004 dev_err(&pdev
->dev
, "no mem resource?\n");
1009 ioarea
= devm_request_mem_region(&pdev
->dev
, mem
->start
,
1010 resource_size(mem
), pdev
->name
);
1012 dev_err(&pdev
->dev
, "Audio region already claimed\n");
1016 pm_runtime_enable(&pdev
->dev
);
1018 ret
= pm_runtime_get_sync(&pdev
->dev
);
1019 if (IS_ERR_VALUE(ret
)) {
1020 dev_err(&pdev
->dev
, "pm_runtime_get_sync() failed\n");
1024 mcasp
->base
= devm_ioremap(&pdev
->dev
, mem
->start
, resource_size(mem
));
1026 dev_err(&pdev
->dev
, "ioremap failed\n");
1028 goto err_release_clk
;
1031 mcasp
->op_mode
= pdata
->op_mode
;
1032 mcasp
->tdm_slots
= pdata
->tdm_slots
;
1033 mcasp
->num_serializer
= pdata
->num_serializer
;
1034 mcasp
->serial_dir
= pdata
->serial_dir
;
1035 mcasp
->version
= pdata
->version
;
1036 mcasp
->txnumevt
= pdata
->txnumevt
;
1037 mcasp
->rxnumevt
= pdata
->rxnumevt
;
1039 mcasp
->dev
= &pdev
->dev
;
1041 dat
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dat");
1043 mcasp
->dat_port
= true;
1045 dma_data
= &mcasp
->dma_params
[SNDRV_PCM_STREAM_PLAYBACK
];
1046 dma_data
->asp_chan_q
= pdata
->asp_chan_q
;
1047 dma_data
->ram_chan_q
= pdata
->ram_chan_q
;
1048 dma_data
->sram_pool
= pdata
->sram_pool
;
1049 dma_data
->sram_size
= pdata
->sram_size_playback
;
1051 dma_data
->dma_addr
= dat
->start
;
1053 dma_data
->dma_addr
= mem
->start
+ pdata
->tx_dma_offset
;
1055 /* Unconditional dmaengine stuff */
1056 mcasp
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
].addr
= dma_data
->dma_addr
;
1058 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
1060 dma_data
->channel
= res
->start
;
1062 dma_data
->channel
= pdata
->tx_dma_channel
;
1064 dma_data
= &mcasp
->dma_params
[SNDRV_PCM_STREAM_CAPTURE
];
1065 dma_data
->asp_chan_q
= pdata
->asp_chan_q
;
1066 dma_data
->ram_chan_q
= pdata
->ram_chan_q
;
1067 dma_data
->sram_pool
= pdata
->sram_pool
;
1068 dma_data
->sram_size
= pdata
->sram_size_capture
;
1070 dma_data
->dma_addr
= dat
->start
;
1072 dma_data
->dma_addr
= mem
->start
+ pdata
->rx_dma_offset
;
1074 /* Unconditional dmaengine stuff */
1075 mcasp
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
].addr
= dma_data
->dma_addr
;
1077 if (mcasp
->version
< MCASP_VERSION_3
) {
1078 mcasp
->fifo_base
= DAVINCI_MCASP_V2_AFIFO_BASE
;
1079 /* dma_data->dma_addr is pointing to the data port address */
1080 mcasp
->dat_port
= true;
1082 mcasp
->fifo_base
= DAVINCI_MCASP_V3_AFIFO_BASE
;
1085 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
1087 dma_data
->channel
= res
->start
;
1089 dma_data
->channel
= pdata
->rx_dma_channel
;
1091 /* Unconditional dmaengine stuff */
1092 mcasp
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
].filter_data
= "tx";
1093 mcasp
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
].filter_data
= "rx";
1095 dev_set_drvdata(&pdev
->dev
, mcasp
);
1097 mcasp_reparent_fck(pdev
);
1099 ret
= snd_soc_register_component(&pdev
->dev
, &davinci_mcasp_component
,
1100 &davinci_mcasp_dai
[pdata
->op_mode
], 1);
1103 goto err_release_clk
;
1105 if (mcasp
->version
!= MCASP_VERSION_4
) {
1106 ret
= davinci_soc_platform_register(&pdev
->dev
);
1108 dev_err(&pdev
->dev
, "register PCM failed: %d\n", ret
);
1109 goto err_unregister_component
;
1115 err_unregister_component
:
1116 snd_soc_unregister_component(&pdev
->dev
);
1118 pm_runtime_put_sync(&pdev
->dev
);
1119 pm_runtime_disable(&pdev
->dev
);
1123 static int davinci_mcasp_remove(struct platform_device
*pdev
)
1125 struct davinci_mcasp
*mcasp
= dev_get_drvdata(&pdev
->dev
);
1127 snd_soc_unregister_component(&pdev
->dev
);
1128 if (mcasp
->version
!= MCASP_VERSION_4
)
1129 davinci_soc_platform_unregister(&pdev
->dev
);
1131 pm_runtime_put_sync(&pdev
->dev
);
1132 pm_runtime_disable(&pdev
->dev
);
1137 #ifdef CONFIG_PM_SLEEP
1138 static int davinci_mcasp_suspend(struct device
*dev
)
1140 struct davinci_mcasp
*mcasp
= dev_get_drvdata(dev
);
1142 mcasp
->context
.txfmtctl
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
);
1143 mcasp
->context
.rxfmtctl
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
);
1144 mcasp
->context
.txfmt
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_TXFMT_REG
);
1145 mcasp
->context
.rxfmt
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_RXFMT_REG
);
1146 mcasp
->context
.aclkxctl
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
);
1147 mcasp
->context
.aclkrctl
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
);
1148 mcasp
->context
.pdir
= mcasp_get_reg(mcasp
, DAVINCI_MCASP_PDIR_REG
);
1153 static int davinci_mcasp_resume(struct device
*dev
)
1155 struct davinci_mcasp
*mcasp
= dev_get_drvdata(dev
);
1157 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXFMCTL_REG
, mcasp
->context
.txfmtctl
);
1158 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXFMCTL_REG
, mcasp
->context
.rxfmtctl
);
1159 mcasp_set_reg(mcasp
, DAVINCI_MCASP_TXFMT_REG
, mcasp
->context
.txfmt
);
1160 mcasp_set_reg(mcasp
, DAVINCI_MCASP_RXFMT_REG
, mcasp
->context
.rxfmt
);
1161 mcasp_set_reg(mcasp
, DAVINCI_MCASP_ACLKXCTL_REG
, mcasp
->context
.aclkxctl
);
1162 mcasp_set_reg(mcasp
, DAVINCI_MCASP_ACLKRCTL_REG
, mcasp
->context
.aclkrctl
);
1163 mcasp_set_reg(mcasp
, DAVINCI_MCASP_PDIR_REG
, mcasp
->context
.pdir
);
1169 SIMPLE_DEV_PM_OPS(davinci_mcasp_pm_ops
,
1170 davinci_mcasp_suspend
,
1171 davinci_mcasp_resume
);
1173 static struct platform_driver davinci_mcasp_driver
= {
1174 .probe
= davinci_mcasp_probe
,
1175 .remove
= davinci_mcasp_remove
,
1177 .name
= "davinci-mcasp",
1178 .owner
= THIS_MODULE
,
1179 .pm
= &davinci_mcasp_pm_ops
,
1180 .of_match_table
= mcasp_dt_ids
,
1184 module_platform_driver(davinci_mcasp_driver
);
1186 MODULE_AUTHOR("Steve Chen");
1187 MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
1188 MODULE_LICENSE("GPL");