2 * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
4 * Author: Vladimir Barinov, <vbarinov@embeddedalley.com>
5 * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com>
7 * DT support (c) 2016 Petr Kulhavy, Barix AG <petr@barix.com>
8 * based on davinci-mcasp.c DT support
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 * on DA850 implement HW FIFOs instead of DMA into DXR and DRR registers
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/platform_data/davinci_asp.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/initval.h>
31 #include <sound/soc.h>
32 #include <sound/dmaengine_pcm.h>
35 #include "davinci-i2s.h"
39 * NOTE: terminology here is confusing.
41 * - This driver supports the "Audio Serial Port" (ASP),
42 * found on dm6446, dm355, and other DaVinci chips.
44 * - But it labels it a "Multi-channel Buffered Serial Port"
45 * (McBSP) as on older chips like the dm642 ... which was
46 * backward-compatible, possibly explaining that confusion.
48 * - OMAP chips have a controller called McBSP, which is
49 * incompatible with the DaVinci flavor of McBSP.
51 * - Newer DaVinci chips have a controller called McASP,
52 * incompatible with ASP and with either McBSP.
54 * In short: this uses ASP to implement I2S, not McBSP.
55 * And it won't be the only DaVinci implemention of I2S.
57 #define DAVINCI_MCBSP_DRR_REG 0x00
58 #define DAVINCI_MCBSP_DXR_REG 0x04
59 #define DAVINCI_MCBSP_SPCR_REG 0x08
60 #define DAVINCI_MCBSP_RCR_REG 0x0c
61 #define DAVINCI_MCBSP_XCR_REG 0x10
62 #define DAVINCI_MCBSP_SRGR_REG 0x14
63 #define DAVINCI_MCBSP_PCR_REG 0x24
65 #define DAVINCI_MCBSP_SPCR_RRST (1 << 0)
66 #define DAVINCI_MCBSP_SPCR_RINTM(v) ((v) << 4)
67 #define DAVINCI_MCBSP_SPCR_XRST (1 << 16)
68 #define DAVINCI_MCBSP_SPCR_XINTM(v) ((v) << 20)
69 #define DAVINCI_MCBSP_SPCR_GRST (1 << 22)
70 #define DAVINCI_MCBSP_SPCR_FRST (1 << 23)
71 #define DAVINCI_MCBSP_SPCR_FREE (1 << 25)
73 #define DAVINCI_MCBSP_RCR_RWDLEN1(v) ((v) << 5)
74 #define DAVINCI_MCBSP_RCR_RFRLEN1(v) ((v) << 8)
75 #define DAVINCI_MCBSP_RCR_RDATDLY(v) ((v) << 16)
76 #define DAVINCI_MCBSP_RCR_RFIG (1 << 18)
77 #define DAVINCI_MCBSP_RCR_RWDLEN2(v) ((v) << 21)
78 #define DAVINCI_MCBSP_RCR_RFRLEN2(v) ((v) << 24)
79 #define DAVINCI_MCBSP_RCR_RPHASE BIT(31)
81 #define DAVINCI_MCBSP_XCR_XWDLEN1(v) ((v) << 5)
82 #define DAVINCI_MCBSP_XCR_XFRLEN1(v) ((v) << 8)
83 #define DAVINCI_MCBSP_XCR_XDATDLY(v) ((v) << 16)
84 #define DAVINCI_MCBSP_XCR_XFIG (1 << 18)
85 #define DAVINCI_MCBSP_XCR_XWDLEN2(v) ((v) << 21)
86 #define DAVINCI_MCBSP_XCR_XFRLEN2(v) ((v) << 24)
87 #define DAVINCI_MCBSP_XCR_XPHASE BIT(31)
89 #define DAVINCI_MCBSP_SRGR_FWID(v) ((v) << 8)
90 #define DAVINCI_MCBSP_SRGR_FPER(v) ((v) << 16)
91 #define DAVINCI_MCBSP_SRGR_FSGM (1 << 28)
92 #define DAVINCI_MCBSP_SRGR_CLKSM BIT(29)
94 #define DAVINCI_MCBSP_PCR_CLKRP (1 << 0)
95 #define DAVINCI_MCBSP_PCR_CLKXP (1 << 1)
96 #define DAVINCI_MCBSP_PCR_FSRP (1 << 2)
97 #define DAVINCI_MCBSP_PCR_FSXP (1 << 3)
98 #define DAVINCI_MCBSP_PCR_SCLKME (1 << 7)
99 #define DAVINCI_MCBSP_PCR_CLKRM (1 << 8)
100 #define DAVINCI_MCBSP_PCR_CLKXM (1 << 9)
101 #define DAVINCI_MCBSP_PCR_FSRM (1 << 10)
102 #define DAVINCI_MCBSP_PCR_FSXM (1 << 11)
105 DAVINCI_MCBSP_WORD_8
= 0,
106 DAVINCI_MCBSP_WORD_12
,
107 DAVINCI_MCBSP_WORD_16
,
108 DAVINCI_MCBSP_WORD_20
,
109 DAVINCI_MCBSP_WORD_24
,
110 DAVINCI_MCBSP_WORD_32
,
113 static const unsigned char data_type
[SNDRV_PCM_FORMAT_S32_LE
+ 1] = {
114 [SNDRV_PCM_FORMAT_S8
] = 1,
115 [SNDRV_PCM_FORMAT_S16_LE
] = 2,
116 [SNDRV_PCM_FORMAT_S32_LE
] = 4,
119 static const unsigned char asp_word_length
[SNDRV_PCM_FORMAT_S32_LE
+ 1] = {
120 [SNDRV_PCM_FORMAT_S8
] = DAVINCI_MCBSP_WORD_8
,
121 [SNDRV_PCM_FORMAT_S16_LE
] = DAVINCI_MCBSP_WORD_16
,
122 [SNDRV_PCM_FORMAT_S32_LE
] = DAVINCI_MCBSP_WORD_32
,
125 static const unsigned char double_fmt
[SNDRV_PCM_FORMAT_S32_LE
+ 1] = {
126 [SNDRV_PCM_FORMAT_S8
] = SNDRV_PCM_FORMAT_S16_LE
,
127 [SNDRV_PCM_FORMAT_S16_LE
] = SNDRV_PCM_FORMAT_S32_LE
,
130 struct davinci_mcbsp_dev
{
132 struct snd_dmaengine_dai_dma_data dma_data
[2];
141 * Combining both channels into 1 element will at least double the
142 * amount of time between servicing the dma channel, increase
143 * effiency, and reduce the chance of overrun/underrun. But,
144 * it will result in the left & right channels being swapped.
146 * If relabeling the left and right channels is not possible,
147 * you may want to let the codec know to swap them back.
149 * It may allow x10 the amount of time to service dma requests,
150 * if the codec is master and is using an unnecessarily fast bit clock
151 * (ie. tlvaic23b), independent of the sample rate. So, having an
152 * entire frame at once means it can be serviced at the sample rate
153 * instead of the bit clock rate.
155 * In the now unlikely case that an underrun still
156 * occurs, both the left and right samples will be repeated
157 * so that no pops are heard, and the left and right channels
158 * won't end up being swapped because of the underrun.
160 unsigned enable_channel_combine
:1;
165 bool i2s_accurate_sck
;
168 static inline void davinci_mcbsp_write_reg(struct davinci_mcbsp_dev
*dev
,
171 __raw_writel(val
, dev
->base
+ reg
);
174 static inline u32
davinci_mcbsp_read_reg(struct davinci_mcbsp_dev
*dev
, int reg
)
176 return __raw_readl(dev
->base
+ reg
);
179 static void toggle_clock(struct davinci_mcbsp_dev
*dev
, int playback
)
181 u32 m
= playback
? DAVINCI_MCBSP_PCR_CLKXP
: DAVINCI_MCBSP_PCR_CLKRP
;
182 /* The clock needs to toggle to complete reset.
183 * So, fake it by toggling the clk polarity.
185 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_PCR_REG
, dev
->pcr
^ m
);
186 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_PCR_REG
, dev
->pcr
);
189 static void davinci_mcbsp_start(struct davinci_mcbsp_dev
*dev
,
190 struct snd_pcm_substream
*substream
)
192 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
193 struct snd_soc_platform
*platform
= rtd
->platform
;
194 int playback
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
196 u32 mask
= playback
? DAVINCI_MCBSP_SPCR_XRST
: DAVINCI_MCBSP_SPCR_RRST
;
197 spcr
= davinci_mcbsp_read_reg(dev
, DAVINCI_MCBSP_SPCR_REG
);
199 /* start off disabled */
200 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
,
202 toggle_clock(dev
, playback
);
204 if (dev
->pcr
& (DAVINCI_MCBSP_PCR_FSXM
| DAVINCI_MCBSP_PCR_FSRM
|
205 DAVINCI_MCBSP_PCR_CLKXM
| DAVINCI_MCBSP_PCR_CLKRM
)) {
206 /* Start the sample generator */
207 spcr
|= DAVINCI_MCBSP_SPCR_GRST
;
208 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
, spcr
);
212 /* Stop the DMA to avoid data loss */
213 /* while the transmitter is out of reset to handle XSYNCERR */
214 if (platform
->driver
->ops
->trigger
) {
215 int ret
= platform
->driver
->ops
->trigger(substream
,
216 SNDRV_PCM_TRIGGER_STOP
);
218 printk(KERN_DEBUG
"Playback DMA stop failed\n");
221 /* Enable the transmitter */
222 spcr
= davinci_mcbsp_read_reg(dev
, DAVINCI_MCBSP_SPCR_REG
);
223 spcr
|= DAVINCI_MCBSP_SPCR_XRST
;
224 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
, spcr
);
226 /* wait for any unexpected frame sync error to occur */
229 /* Disable the transmitter to clear any outstanding XSYNCERR */
230 spcr
= davinci_mcbsp_read_reg(dev
, DAVINCI_MCBSP_SPCR_REG
);
231 spcr
&= ~DAVINCI_MCBSP_SPCR_XRST
;
232 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
, spcr
);
233 toggle_clock(dev
, playback
);
235 /* Restart the DMA */
236 if (platform
->driver
->ops
->trigger
) {
237 int ret
= platform
->driver
->ops
->trigger(substream
,
238 SNDRV_PCM_TRIGGER_START
);
240 printk(KERN_DEBUG
"Playback DMA start failed\n");
244 /* Enable transmitter or receiver */
245 spcr
= davinci_mcbsp_read_reg(dev
, DAVINCI_MCBSP_SPCR_REG
);
248 if (dev
->pcr
& (DAVINCI_MCBSP_PCR_FSXM
| DAVINCI_MCBSP_PCR_FSRM
)) {
249 /* Start frame sync */
250 spcr
|= DAVINCI_MCBSP_SPCR_FRST
;
252 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
, spcr
);
255 static void davinci_mcbsp_stop(struct davinci_mcbsp_dev
*dev
, int playback
)
259 /* Reset transmitter/receiver and sample rate/frame sync generators */
260 spcr
= davinci_mcbsp_read_reg(dev
, DAVINCI_MCBSP_SPCR_REG
);
261 spcr
&= ~(DAVINCI_MCBSP_SPCR_GRST
| DAVINCI_MCBSP_SPCR_FRST
);
262 spcr
&= playback
? ~DAVINCI_MCBSP_SPCR_XRST
: ~DAVINCI_MCBSP_SPCR_RRST
;
263 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
, spcr
);
264 toggle_clock(dev
, playback
);
267 #define DEFAULT_BITPERSAMPLE 16
269 static int davinci_i2s_set_dai_fmt(struct snd_soc_dai
*cpu_dai
,
272 struct davinci_mcbsp_dev
*dev
= snd_soc_dai_get_drvdata(cpu_dai
);
276 /* Attention srgr is updated by hw_params! */
277 srgr
= DAVINCI_MCBSP_SRGR_FSGM
|
278 DAVINCI_MCBSP_SRGR_FPER(DEFAULT_BITPERSAMPLE
* 2 - 1) |
279 DAVINCI_MCBSP_SRGR_FWID(DEFAULT_BITPERSAMPLE
- 1);
282 /* set master/slave audio interface */
283 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
284 case SND_SOC_DAIFMT_CBS_CFS
:
286 pcr
= DAVINCI_MCBSP_PCR_FSXM
|
287 DAVINCI_MCBSP_PCR_FSRM
|
288 DAVINCI_MCBSP_PCR_CLKXM
|
289 DAVINCI_MCBSP_PCR_CLKRM
;
291 case SND_SOC_DAIFMT_CBM_CFS
:
292 pcr
= DAVINCI_MCBSP_PCR_FSRM
| DAVINCI_MCBSP_PCR_FSXM
;
294 * Selection of the clock input pin that is the
295 * input for the Sample Rate Generator.
296 * McBSP FSR and FSX are driven by the Sample Rate
299 switch (dev
->clk_input_pin
) {
301 pcr
|= DAVINCI_MCBSP_PCR_CLKXM
|
302 DAVINCI_MCBSP_PCR_CLKRM
;
305 pcr
|= DAVINCI_MCBSP_PCR_SCLKME
;
308 dev_err(dev
->dev
, "bad clk_input_pin\n");
313 case SND_SOC_DAIFMT_CBM_CFM
:
314 /* codec is master */
318 printk(KERN_ERR
"%s:bad master\n", __func__
);
322 /* interface format */
323 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
324 case SND_SOC_DAIFMT_I2S
:
325 /* Davinci doesn't support TRUE I2S, but some codecs will have
326 * the left and right channels contiguous. This allows
327 * dsp_a mode to be used with an inverted normal frame clk.
328 * If your codec is master and does not have contiguous
329 * channels, then you will have sound on only one channel.
330 * Try using a different mode, or codec as slave.
332 * The TLV320AIC33 is an example of a codec where this works.
333 * It has a variable bit clock frequency allowing it to have
334 * valid data on every bit clock.
336 * The TLV320AIC23 is an example of a codec where this does not
337 * work. It has a fixed bit clock frequency with progressively
338 * more empty bit clock slots between channels as the sample
342 case SND_SOC_DAIFMT_DSP_A
:
343 dev
->mode
= MOD_DSP_A
;
345 case SND_SOC_DAIFMT_DSP_B
:
346 dev
->mode
= MOD_DSP_B
;
349 printk(KERN_ERR
"%s:bad format\n", __func__
);
353 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
354 case SND_SOC_DAIFMT_NB_NF
:
355 /* CLKRP Receive clock polarity,
356 * 1 - sampled on rising edge of CLKR
357 * valid on rising edge
358 * CLKXP Transmit clock polarity,
359 * 1 - clocked on falling edge of CLKX
360 * valid on rising edge
361 * FSRP Receive frame sync pol, 0 - active high
362 * FSXP Transmit frame sync pol, 0 - active high
364 pcr
|= (DAVINCI_MCBSP_PCR_CLKXP
| DAVINCI_MCBSP_PCR_CLKRP
);
366 case SND_SOC_DAIFMT_IB_IF
:
367 /* CLKRP Receive clock polarity,
368 * 0 - sampled on falling edge of CLKR
369 * valid on falling edge
370 * CLKXP Transmit clock polarity,
371 * 0 - clocked on rising edge of CLKX
372 * valid on falling edge
373 * FSRP Receive frame sync pol, 1 - active low
374 * FSXP Transmit frame sync pol, 1 - active low
376 pcr
|= (DAVINCI_MCBSP_PCR_FSXP
| DAVINCI_MCBSP_PCR_FSRP
);
378 case SND_SOC_DAIFMT_NB_IF
:
379 /* CLKRP Receive clock polarity,
380 * 1 - sampled on rising edge of CLKR
381 * valid on rising edge
382 * CLKXP Transmit clock polarity,
383 * 1 - clocked on falling edge of CLKX
384 * valid on rising edge
385 * FSRP Receive frame sync pol, 1 - active low
386 * FSXP Transmit frame sync pol, 1 - active low
388 pcr
|= (DAVINCI_MCBSP_PCR_CLKXP
| DAVINCI_MCBSP_PCR_CLKRP
|
389 DAVINCI_MCBSP_PCR_FSXP
| DAVINCI_MCBSP_PCR_FSRP
);
391 case SND_SOC_DAIFMT_IB_NF
:
392 /* CLKRP Receive clock polarity,
393 * 0 - sampled on falling edge of CLKR
394 * valid on falling edge
395 * CLKXP Transmit clock polarity,
396 * 0 - clocked on rising edge of CLKX
397 * valid on falling edge
398 * FSRP Receive frame sync pol, 0 - active high
399 * FSXP Transmit frame sync pol, 0 - active high
406 pcr
^= (DAVINCI_MCBSP_PCR_FSXP
| DAVINCI_MCBSP_PCR_FSRP
);
407 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SRGR_REG
, srgr
);
409 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_PCR_REG
, pcr
);
413 static int davinci_i2s_dai_set_clkdiv(struct snd_soc_dai
*cpu_dai
,
416 struct davinci_mcbsp_dev
*dev
= snd_soc_dai_get_drvdata(cpu_dai
);
418 if (div_id
!= DAVINCI_MCBSP_CLKGDV
)
425 static int davinci_i2s_hw_params(struct snd_pcm_substream
*substream
,
426 struct snd_pcm_hw_params
*params
,
427 struct snd_soc_dai
*dai
)
429 struct davinci_mcbsp_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
430 struct snd_interval
*i
= NULL
;
431 int mcbsp_word_length
, master
;
432 unsigned int rcr
, xcr
, srgr
, clk_div
, freq
, framesize
;
434 snd_pcm_format_t fmt
;
435 unsigned element_cnt
= 1;
437 /* general line settings */
438 spcr
= davinci_mcbsp_read_reg(dev
, DAVINCI_MCBSP_SPCR_REG
);
439 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
440 spcr
|= DAVINCI_MCBSP_SPCR_RINTM(3) | DAVINCI_MCBSP_SPCR_FREE
;
441 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
, spcr
);
443 spcr
|= DAVINCI_MCBSP_SPCR_XINTM(3) | DAVINCI_MCBSP_SPCR_FREE
;
444 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
, spcr
);
447 master
= dev
->fmt
& SND_SOC_DAIFMT_MASTER_MASK
;
448 fmt
= params_format(params
);
449 mcbsp_word_length
= asp_word_length
[fmt
];
452 case SND_SOC_DAIFMT_CBS_CFS
:
453 freq
= clk_get_rate(dev
->clk
);
454 srgr
= DAVINCI_MCBSP_SRGR_FSGM
|
455 DAVINCI_MCBSP_SRGR_CLKSM
;
456 srgr
|= DAVINCI_MCBSP_SRGR_FWID(mcbsp_word_length
*
458 if (dev
->i2s_accurate_sck
) {
461 framesize
= (freq
/ (--clk_div
)) /
464 } while (((framesize
< 33) || (framesize
> 4095)) &&
467 srgr
|= DAVINCI_MCBSP_SRGR_FPER(framesize
- 1);
469 /* symmetric waveforms */
470 clk_div
= freq
/ (mcbsp_word_length
* 16) /
471 params
->rate_num
* params
->rate_den
;
472 srgr
|= DAVINCI_MCBSP_SRGR_FPER(mcbsp_word_length
*
478 case SND_SOC_DAIFMT_CBM_CFS
:
479 srgr
= DAVINCI_MCBSP_SRGR_FSGM
;
480 clk_div
= dev
->clk_div
- 1;
481 srgr
|= DAVINCI_MCBSP_SRGR_FWID(mcbsp_word_length
* 8 - 1);
482 srgr
|= DAVINCI_MCBSP_SRGR_FPER(mcbsp_word_length
* 16 - 1);
486 case SND_SOC_DAIFMT_CBM_CFM
:
487 /* Clock and frame sync given from external sources */
488 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
489 srgr
= DAVINCI_MCBSP_SRGR_FSGM
;
490 srgr
|= DAVINCI_MCBSP_SRGR_FWID(snd_interval_value(i
) - 1);
491 pr_debug("%s - %d FWID set: re-read srgr = %X\n",
492 __func__
, __LINE__
, snd_interval_value(i
) - 1);
494 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_FRAME_BITS
);
495 srgr
|= DAVINCI_MCBSP_SRGR_FPER(snd_interval_value(i
) - 1);
500 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SRGR_REG
, srgr
);
502 rcr
= DAVINCI_MCBSP_RCR_RFIG
;
503 xcr
= DAVINCI_MCBSP_XCR_XFIG
;
504 if (dev
->mode
== MOD_DSP_B
) {
505 rcr
|= DAVINCI_MCBSP_RCR_RDATDLY(0);
506 xcr
|= DAVINCI_MCBSP_XCR_XDATDLY(0);
508 rcr
|= DAVINCI_MCBSP_RCR_RDATDLY(1);
509 xcr
|= DAVINCI_MCBSP_XCR_XDATDLY(1);
511 /* Determine xfer data type */
512 fmt
= params_format(params
);
513 if ((fmt
> SNDRV_PCM_FORMAT_S32_LE
) || !data_type
[fmt
]) {
514 printk(KERN_WARNING
"davinci-i2s: unsupported PCM format\n");
518 if (params_channels(params
) == 2) {
520 if (double_fmt
[fmt
] && dev
->enable_channel_combine
) {
522 fmt
= double_fmt
[fmt
];
525 case SND_SOC_DAIFMT_CBS_CFS
:
526 case SND_SOC_DAIFMT_CBS_CFM
:
527 rcr
|= DAVINCI_MCBSP_RCR_RFRLEN2(0);
528 xcr
|= DAVINCI_MCBSP_XCR_XFRLEN2(0);
529 rcr
|= DAVINCI_MCBSP_RCR_RPHASE
;
530 xcr
|= DAVINCI_MCBSP_XCR_XPHASE
;
532 case SND_SOC_DAIFMT_CBM_CFM
:
533 case SND_SOC_DAIFMT_CBM_CFS
:
534 rcr
|= DAVINCI_MCBSP_RCR_RFRLEN2(element_cnt
- 1);
535 xcr
|= DAVINCI_MCBSP_XCR_XFRLEN2(element_cnt
- 1);
541 mcbsp_word_length
= asp_word_length
[fmt
];
544 case SND_SOC_DAIFMT_CBS_CFS
:
545 case SND_SOC_DAIFMT_CBS_CFM
:
546 rcr
|= DAVINCI_MCBSP_RCR_RFRLEN1(0);
547 xcr
|= DAVINCI_MCBSP_XCR_XFRLEN1(0);
549 case SND_SOC_DAIFMT_CBM_CFM
:
550 case SND_SOC_DAIFMT_CBM_CFS
:
551 rcr
|= DAVINCI_MCBSP_RCR_RFRLEN1(element_cnt
- 1);
552 xcr
|= DAVINCI_MCBSP_XCR_XFRLEN1(element_cnt
- 1);
558 rcr
|= DAVINCI_MCBSP_RCR_RWDLEN1(mcbsp_word_length
) |
559 DAVINCI_MCBSP_RCR_RWDLEN2(mcbsp_word_length
);
560 xcr
|= DAVINCI_MCBSP_XCR_XWDLEN1(mcbsp_word_length
) |
561 DAVINCI_MCBSP_XCR_XWDLEN2(mcbsp_word_length
);
563 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
564 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_XCR_REG
, xcr
);
566 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_RCR_REG
, rcr
);
568 pr_debug("%s - %d srgr=%X\n", __func__
, __LINE__
, srgr
);
569 pr_debug("%s - %d xcr=%X\n", __func__
, __LINE__
, xcr
);
570 pr_debug("%s - %d rcr=%X\n", __func__
, __LINE__
, rcr
);
574 static int davinci_i2s_prepare(struct snd_pcm_substream
*substream
,
575 struct snd_soc_dai
*dai
)
577 struct davinci_mcbsp_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
578 int playback
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
579 davinci_mcbsp_stop(dev
, playback
);
583 static int davinci_i2s_trigger(struct snd_pcm_substream
*substream
, int cmd
,
584 struct snd_soc_dai
*dai
)
586 struct davinci_mcbsp_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
588 int playback
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
591 case SNDRV_PCM_TRIGGER_START
:
592 case SNDRV_PCM_TRIGGER_RESUME
:
593 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
594 davinci_mcbsp_start(dev
, substream
);
596 case SNDRV_PCM_TRIGGER_STOP
:
597 case SNDRV_PCM_TRIGGER_SUSPEND
:
598 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
599 davinci_mcbsp_stop(dev
, playback
);
607 static void davinci_i2s_shutdown(struct snd_pcm_substream
*substream
,
608 struct snd_soc_dai
*dai
)
610 struct davinci_mcbsp_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
611 int playback
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
612 davinci_mcbsp_stop(dev
, playback
);
615 #define DAVINCI_I2S_RATES SNDRV_PCM_RATE_8000_96000
617 static const struct snd_soc_dai_ops davinci_i2s_dai_ops
= {
618 .shutdown
= davinci_i2s_shutdown
,
619 .prepare
= davinci_i2s_prepare
,
620 .trigger
= davinci_i2s_trigger
,
621 .hw_params
= davinci_i2s_hw_params
,
622 .set_fmt
= davinci_i2s_set_dai_fmt
,
623 .set_clkdiv
= davinci_i2s_dai_set_clkdiv
,
627 static int davinci_i2s_dai_probe(struct snd_soc_dai
*dai
)
629 struct davinci_mcbsp_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
631 dai
->playback_dma_data
= &dev
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
];
632 dai
->capture_dma_data
= &dev
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
];
637 static struct snd_soc_dai_driver davinci_i2s_dai
= {
638 .probe
= davinci_i2s_dai_probe
,
642 .rates
= DAVINCI_I2S_RATES
,
643 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,},
647 .rates
= DAVINCI_I2S_RATES
,
648 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,},
649 .ops
= &davinci_i2s_dai_ops
,
653 static const struct snd_soc_component_driver davinci_i2s_component
= {
654 .name
= "davinci-i2s",
657 static int davinci_i2s_probe(struct platform_device
*pdev
)
659 struct snd_dmaengine_dai_dma_data
*dma_data
;
660 struct davinci_mcbsp_dev
*dev
;
661 struct resource
*mem
, *res
;
662 void __iomem
*io_base
;
666 mem
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mpu");
669 "\"mpu\" mem resource not found, using index 0\n");
670 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
672 dev_err(&pdev
->dev
, "no mem resource?\n");
677 io_base
= devm_ioremap_resource(&pdev
->dev
, mem
);
679 return PTR_ERR(io_base
);
681 dev
= devm_kzalloc(&pdev
->dev
, sizeof(struct davinci_mcbsp_dev
),
688 /* setup DMA, first TX, then RX */
689 dma_data
= &dev
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
];
690 dma_data
->addr
= (dma_addr_t
)(mem
->start
+ DAVINCI_MCBSP_DXR_REG
);
692 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
694 dma
= &dev
->dma_request
[SNDRV_PCM_STREAM_PLAYBACK
];
696 dma_data
->filter_data
= dma
;
697 } else if (IS_ENABLED(CONFIG_OF
) && pdev
->dev
.of_node
) {
698 dma_data
->filter_data
= "tx";
700 dev_err(&pdev
->dev
, "Missing DMA tx resource\n");
704 dma_data
= &dev
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
];
705 dma_data
->addr
= (dma_addr_t
)(mem
->start
+ DAVINCI_MCBSP_DRR_REG
);
707 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
709 dma
= &dev
->dma_request
[SNDRV_PCM_STREAM_CAPTURE
];
711 dma_data
->filter_data
= dma
;
712 } else if (IS_ENABLED(CONFIG_OF
) && pdev
->dev
.of_node
) {
713 dma_data
->filter_data
= "rx";
715 dev_err(&pdev
->dev
, "Missing DMA rx resource\n");
719 dev
->clk
= clk_get(&pdev
->dev
, NULL
);
720 if (IS_ERR(dev
->clk
))
722 clk_enable(dev
->clk
);
724 dev
->dev
= &pdev
->dev
;
725 dev_set_drvdata(&pdev
->dev
, dev
);
727 ret
= snd_soc_register_component(&pdev
->dev
, &davinci_i2s_component
,
728 &davinci_i2s_dai
, 1);
730 goto err_release_clk
;
732 ret
= edma_pcm_platform_register(&pdev
->dev
);
734 dev_err(&pdev
->dev
, "register PCM failed: %d\n", ret
);
735 goto err_unregister_component
;
740 err_unregister_component
:
741 snd_soc_unregister_component(&pdev
->dev
);
743 clk_disable(dev
->clk
);
748 static int davinci_i2s_remove(struct platform_device
*pdev
)
750 struct davinci_mcbsp_dev
*dev
= dev_get_drvdata(&pdev
->dev
);
752 snd_soc_unregister_component(&pdev
->dev
);
754 clk_disable(dev
->clk
);
761 static const struct of_device_id davinci_i2s_match
[] = {
762 { .compatible
= "ti,da850-mcbsp" },
765 MODULE_DEVICE_TABLE(of
, davinci_i2s_match
);
767 static struct platform_driver davinci_mcbsp_driver
= {
768 .probe
= davinci_i2s_probe
,
769 .remove
= davinci_i2s_remove
,
771 .name
= "davinci-mcbsp",
772 .of_match_table
= of_match_ptr(davinci_i2s_match
),
776 module_platform_driver(davinci_mcbsp_driver
);
778 MODULE_AUTHOR("Vladimir Barinov");
779 MODULE_DESCRIPTION("TI DAVINCI I2S (McBSP) SoC Interface");
780 MODULE_LICENSE("GPL");