1 // SPDX-License-Identifier: GPL-2.0-only
3 * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
5 * Author: Vladimir Barinov, <vbarinov@embeddedalley.com>
6 * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com>
8 * DT support (c) 2016 Petr Kulhavy, Barix AG <petr@barix.com>
9 * based on davinci-mcasp.c DT support
12 * on DA850 implement HW FIFOs instead of DMA into DXR and DRR registers
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/platform_data/davinci_asp.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/initval.h>
28 #include <sound/soc.h>
29 #include <sound/dmaengine_pcm.h>
32 #include "davinci-i2s.h"
34 #define DRV_NAME "davinci-i2s"
37 * NOTE: terminology here is confusing.
39 * - This driver supports the "Audio Serial Port" (ASP),
40 * found on dm6446, dm355, and other DaVinci chips.
42 * - But it labels it a "Multi-channel Buffered Serial Port"
43 * (McBSP) as on older chips like the dm642 ... which was
44 * backward-compatible, possibly explaining that confusion.
46 * - OMAP chips have a controller called McBSP, which is
47 * incompatible with the DaVinci flavor of McBSP.
49 * - Newer DaVinci chips have a controller called McASP,
50 * incompatible with ASP and with either McBSP.
52 * In short: this uses ASP to implement I2S, not McBSP.
53 * And it won't be the only DaVinci implemention of I2S.
55 #define DAVINCI_MCBSP_DRR_REG 0x00
56 #define DAVINCI_MCBSP_DXR_REG 0x04
57 #define DAVINCI_MCBSP_SPCR_REG 0x08
58 #define DAVINCI_MCBSP_RCR_REG 0x0c
59 #define DAVINCI_MCBSP_XCR_REG 0x10
60 #define DAVINCI_MCBSP_SRGR_REG 0x14
61 #define DAVINCI_MCBSP_PCR_REG 0x24
63 #define DAVINCI_MCBSP_SPCR_RRST (1 << 0)
64 #define DAVINCI_MCBSP_SPCR_RINTM(v) ((v) << 4)
65 #define DAVINCI_MCBSP_SPCR_XRST (1 << 16)
66 #define DAVINCI_MCBSP_SPCR_XINTM(v) ((v) << 20)
67 #define DAVINCI_MCBSP_SPCR_GRST (1 << 22)
68 #define DAVINCI_MCBSP_SPCR_FRST (1 << 23)
69 #define DAVINCI_MCBSP_SPCR_FREE (1 << 25)
71 #define DAVINCI_MCBSP_RCR_RWDLEN1(v) ((v) << 5)
72 #define DAVINCI_MCBSP_RCR_RFRLEN1(v) ((v) << 8)
73 #define DAVINCI_MCBSP_RCR_RDATDLY(v) ((v) << 16)
74 #define DAVINCI_MCBSP_RCR_RFIG (1 << 18)
75 #define DAVINCI_MCBSP_RCR_RWDLEN2(v) ((v) << 21)
76 #define DAVINCI_MCBSP_RCR_RFRLEN2(v) ((v) << 24)
77 #define DAVINCI_MCBSP_RCR_RPHASE BIT(31)
79 #define DAVINCI_MCBSP_XCR_XWDLEN1(v) ((v) << 5)
80 #define DAVINCI_MCBSP_XCR_XFRLEN1(v) ((v) << 8)
81 #define DAVINCI_MCBSP_XCR_XDATDLY(v) ((v) << 16)
82 #define DAVINCI_MCBSP_XCR_XFIG (1 << 18)
83 #define DAVINCI_MCBSP_XCR_XWDLEN2(v) ((v) << 21)
84 #define DAVINCI_MCBSP_XCR_XFRLEN2(v) ((v) << 24)
85 #define DAVINCI_MCBSP_XCR_XPHASE BIT(31)
87 #define DAVINCI_MCBSP_SRGR_FWID(v) ((v) << 8)
88 #define DAVINCI_MCBSP_SRGR_FPER(v) ((v) << 16)
89 #define DAVINCI_MCBSP_SRGR_FSGM (1 << 28)
90 #define DAVINCI_MCBSP_SRGR_CLKSM BIT(29)
92 #define DAVINCI_MCBSP_PCR_CLKRP (1 << 0)
93 #define DAVINCI_MCBSP_PCR_CLKXP (1 << 1)
94 #define DAVINCI_MCBSP_PCR_FSRP (1 << 2)
95 #define DAVINCI_MCBSP_PCR_FSXP (1 << 3)
96 #define DAVINCI_MCBSP_PCR_SCLKME (1 << 7)
97 #define DAVINCI_MCBSP_PCR_CLKRM (1 << 8)
98 #define DAVINCI_MCBSP_PCR_CLKXM (1 << 9)
99 #define DAVINCI_MCBSP_PCR_FSRM (1 << 10)
100 #define DAVINCI_MCBSP_PCR_FSXM (1 << 11)
103 DAVINCI_MCBSP_WORD_8
= 0,
104 DAVINCI_MCBSP_WORD_12
,
105 DAVINCI_MCBSP_WORD_16
,
106 DAVINCI_MCBSP_WORD_20
,
107 DAVINCI_MCBSP_WORD_24
,
108 DAVINCI_MCBSP_WORD_32
,
111 static const unsigned char data_type
[SNDRV_PCM_FORMAT_S32_LE
+ 1] = {
112 [SNDRV_PCM_FORMAT_S8
] = 1,
113 [SNDRV_PCM_FORMAT_S16_LE
] = 2,
114 [SNDRV_PCM_FORMAT_S32_LE
] = 4,
117 static const unsigned char asp_word_length
[SNDRV_PCM_FORMAT_S32_LE
+ 1] = {
118 [SNDRV_PCM_FORMAT_S8
] = DAVINCI_MCBSP_WORD_8
,
119 [SNDRV_PCM_FORMAT_S16_LE
] = DAVINCI_MCBSP_WORD_16
,
120 [SNDRV_PCM_FORMAT_S32_LE
] = DAVINCI_MCBSP_WORD_32
,
123 static const unsigned char double_fmt
[SNDRV_PCM_FORMAT_S32_LE
+ 1] = {
124 [SNDRV_PCM_FORMAT_S8
] = SNDRV_PCM_FORMAT_S16_LE
,
125 [SNDRV_PCM_FORMAT_S16_LE
] = SNDRV_PCM_FORMAT_S32_LE
,
128 struct davinci_mcbsp_dev
{
130 struct snd_dmaengine_dai_dma_data dma_data
[2];
139 * Combining both channels into 1 element will at least double the
140 * amount of time between servicing the dma channel, increase
141 * effiency, and reduce the chance of overrun/underrun. But,
142 * it will result in the left & right channels being swapped.
144 * If relabeling the left and right channels is not possible,
145 * you may want to let the codec know to swap them back.
147 * It may allow x10 the amount of time to service dma requests,
148 * if the codec is master and is using an unnecessarily fast bit clock
149 * (ie. tlvaic23b), independent of the sample rate. So, having an
150 * entire frame at once means it can be serviced at the sample rate
151 * instead of the bit clock rate.
153 * In the now unlikely case that an underrun still
154 * occurs, both the left and right samples will be repeated
155 * so that no pops are heard, and the left and right channels
156 * won't end up being swapped because of the underrun.
158 unsigned enable_channel_combine
:1;
163 bool i2s_accurate_sck
;
166 static inline void davinci_mcbsp_write_reg(struct davinci_mcbsp_dev
*dev
,
169 __raw_writel(val
, dev
->base
+ reg
);
172 static inline u32
davinci_mcbsp_read_reg(struct davinci_mcbsp_dev
*dev
, int reg
)
174 return __raw_readl(dev
->base
+ reg
);
177 static void toggle_clock(struct davinci_mcbsp_dev
*dev
, int playback
)
179 u32 m
= playback
? DAVINCI_MCBSP_PCR_CLKXP
: DAVINCI_MCBSP_PCR_CLKRP
;
180 /* The clock needs to toggle to complete reset.
181 * So, fake it by toggling the clk polarity.
183 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_PCR_REG
, dev
->pcr
^ m
);
184 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_PCR_REG
, dev
->pcr
);
187 static void davinci_mcbsp_start(struct davinci_mcbsp_dev
*dev
,
188 struct snd_pcm_substream
*substream
)
190 int playback
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
192 u32 mask
= playback
? DAVINCI_MCBSP_SPCR_XRST
: DAVINCI_MCBSP_SPCR_RRST
;
194 /* Enable transmitter or receiver */
195 spcr
= davinci_mcbsp_read_reg(dev
, DAVINCI_MCBSP_SPCR_REG
);
198 if (dev
->pcr
& (DAVINCI_MCBSP_PCR_FSXM
| DAVINCI_MCBSP_PCR_FSRM
)) {
199 /* Start frame sync */
200 spcr
|= DAVINCI_MCBSP_SPCR_FRST
;
202 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
, spcr
);
205 static void davinci_mcbsp_stop(struct davinci_mcbsp_dev
*dev
, int playback
)
209 /* Reset transmitter/receiver and sample rate/frame sync generators */
210 spcr
= davinci_mcbsp_read_reg(dev
, DAVINCI_MCBSP_SPCR_REG
);
211 spcr
&= ~(DAVINCI_MCBSP_SPCR_GRST
| DAVINCI_MCBSP_SPCR_FRST
);
212 spcr
&= playback
? ~DAVINCI_MCBSP_SPCR_XRST
: ~DAVINCI_MCBSP_SPCR_RRST
;
213 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
, spcr
);
214 toggle_clock(dev
, playback
);
217 #define DEFAULT_BITPERSAMPLE 16
219 static int davinci_i2s_set_dai_fmt(struct snd_soc_dai
*cpu_dai
,
222 struct davinci_mcbsp_dev
*dev
= snd_soc_dai_get_drvdata(cpu_dai
);
226 /* Attention srgr is updated by hw_params! */
227 srgr
= DAVINCI_MCBSP_SRGR_FSGM
|
228 DAVINCI_MCBSP_SRGR_FPER(DEFAULT_BITPERSAMPLE
* 2 - 1) |
229 DAVINCI_MCBSP_SRGR_FWID(DEFAULT_BITPERSAMPLE
- 1);
232 /* set master/slave audio interface */
233 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
234 case SND_SOC_DAIFMT_CBS_CFS
:
236 pcr
= DAVINCI_MCBSP_PCR_FSXM
|
237 DAVINCI_MCBSP_PCR_FSRM
|
238 DAVINCI_MCBSP_PCR_CLKXM
|
239 DAVINCI_MCBSP_PCR_CLKRM
;
241 case SND_SOC_DAIFMT_CBM_CFS
:
242 pcr
= DAVINCI_MCBSP_PCR_FSRM
| DAVINCI_MCBSP_PCR_FSXM
;
244 * Selection of the clock input pin that is the
245 * input for the Sample Rate Generator.
246 * McBSP FSR and FSX are driven by the Sample Rate
249 switch (dev
->clk_input_pin
) {
251 pcr
|= DAVINCI_MCBSP_PCR_CLKXM
|
252 DAVINCI_MCBSP_PCR_CLKRM
;
255 pcr
|= DAVINCI_MCBSP_PCR_SCLKME
;
258 dev_err(dev
->dev
, "bad clk_input_pin\n");
263 case SND_SOC_DAIFMT_CBM_CFM
:
264 /* codec is master */
268 printk(KERN_ERR
"%s:bad master\n", __func__
);
272 /* interface format */
273 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
274 case SND_SOC_DAIFMT_I2S
:
275 /* Davinci doesn't support TRUE I2S, but some codecs will have
276 * the left and right channels contiguous. This allows
277 * dsp_a mode to be used with an inverted normal frame clk.
278 * If your codec is master and does not have contiguous
279 * channels, then you will have sound on only one channel.
280 * Try using a different mode, or codec as slave.
282 * The TLV320AIC33 is an example of a codec where this works.
283 * It has a variable bit clock frequency allowing it to have
284 * valid data on every bit clock.
286 * The TLV320AIC23 is an example of a codec where this does not
287 * work. It has a fixed bit clock frequency with progressively
288 * more empty bit clock slots between channels as the sample
293 case SND_SOC_DAIFMT_DSP_A
:
294 dev
->mode
= MOD_DSP_A
;
296 case SND_SOC_DAIFMT_DSP_B
:
297 dev
->mode
= MOD_DSP_B
;
300 printk(KERN_ERR
"%s:bad format\n", __func__
);
304 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
305 case SND_SOC_DAIFMT_NB_NF
:
306 /* CLKRP Receive clock polarity,
307 * 1 - sampled on rising edge of CLKR
308 * valid on rising edge
309 * CLKXP Transmit clock polarity,
310 * 1 - clocked on falling edge of CLKX
311 * valid on rising edge
312 * FSRP Receive frame sync pol, 0 - active high
313 * FSXP Transmit frame sync pol, 0 - active high
315 pcr
|= (DAVINCI_MCBSP_PCR_CLKXP
| DAVINCI_MCBSP_PCR_CLKRP
);
317 case SND_SOC_DAIFMT_IB_IF
:
318 /* CLKRP Receive clock polarity,
319 * 0 - sampled on falling edge of CLKR
320 * valid on falling edge
321 * CLKXP Transmit clock polarity,
322 * 0 - clocked on rising edge of CLKX
323 * valid on falling edge
324 * FSRP Receive frame sync pol, 1 - active low
325 * FSXP Transmit frame sync pol, 1 - active low
327 pcr
|= (DAVINCI_MCBSP_PCR_FSXP
| DAVINCI_MCBSP_PCR_FSRP
);
329 case SND_SOC_DAIFMT_NB_IF
:
330 /* CLKRP Receive clock polarity,
331 * 1 - sampled on rising edge of CLKR
332 * valid on rising edge
333 * CLKXP Transmit clock polarity,
334 * 1 - clocked on falling edge of CLKX
335 * valid on rising edge
336 * FSRP Receive frame sync pol, 1 - active low
337 * FSXP Transmit frame sync pol, 1 - active low
339 pcr
|= (DAVINCI_MCBSP_PCR_CLKXP
| DAVINCI_MCBSP_PCR_CLKRP
|
340 DAVINCI_MCBSP_PCR_FSXP
| DAVINCI_MCBSP_PCR_FSRP
);
342 case SND_SOC_DAIFMT_IB_NF
:
343 /* CLKRP Receive clock polarity,
344 * 0 - sampled on falling edge of CLKR
345 * valid on falling edge
346 * CLKXP Transmit clock polarity,
347 * 0 - clocked on rising edge of CLKX
348 * valid on falling edge
349 * FSRP Receive frame sync pol, 0 - active high
350 * FSXP Transmit frame sync pol, 0 - active high
357 pcr
^= (DAVINCI_MCBSP_PCR_FSXP
| DAVINCI_MCBSP_PCR_FSRP
);
358 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SRGR_REG
, srgr
);
360 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_PCR_REG
, pcr
);
364 static int davinci_i2s_dai_set_clkdiv(struct snd_soc_dai
*cpu_dai
,
367 struct davinci_mcbsp_dev
*dev
= snd_soc_dai_get_drvdata(cpu_dai
);
369 if (div_id
!= DAVINCI_MCBSP_CLKGDV
)
376 static int davinci_i2s_hw_params(struct snd_pcm_substream
*substream
,
377 struct snd_pcm_hw_params
*params
,
378 struct snd_soc_dai
*dai
)
380 struct davinci_mcbsp_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
381 struct snd_interval
*i
= NULL
;
382 int mcbsp_word_length
, master
;
383 unsigned int rcr
, xcr
, srgr
, clk_div
, freq
, framesize
;
385 snd_pcm_format_t fmt
;
386 unsigned element_cnt
= 1;
388 /* general line settings */
389 spcr
= davinci_mcbsp_read_reg(dev
, DAVINCI_MCBSP_SPCR_REG
);
390 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
391 spcr
|= DAVINCI_MCBSP_SPCR_RINTM(3) | DAVINCI_MCBSP_SPCR_FREE
;
392 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
, spcr
);
394 spcr
|= DAVINCI_MCBSP_SPCR_XINTM(3) | DAVINCI_MCBSP_SPCR_FREE
;
395 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
, spcr
);
398 master
= dev
->fmt
& SND_SOC_DAIFMT_MASTER_MASK
;
399 fmt
= params_format(params
);
400 mcbsp_word_length
= asp_word_length
[fmt
];
403 case SND_SOC_DAIFMT_CBS_CFS
:
404 freq
= clk_get_rate(dev
->clk
);
405 srgr
= DAVINCI_MCBSP_SRGR_FSGM
|
406 DAVINCI_MCBSP_SRGR_CLKSM
;
407 srgr
|= DAVINCI_MCBSP_SRGR_FWID(mcbsp_word_length
*
409 if (dev
->i2s_accurate_sck
) {
412 framesize
= (freq
/ (--clk_div
)) /
415 } while (((framesize
< 33) || (framesize
> 4095)) &&
418 srgr
|= DAVINCI_MCBSP_SRGR_FPER(framesize
- 1);
420 /* symmetric waveforms */
421 clk_div
= freq
/ (mcbsp_word_length
* 16) /
422 params
->rate_num
* params
->rate_den
;
423 srgr
|= DAVINCI_MCBSP_SRGR_FPER(mcbsp_word_length
*
429 case SND_SOC_DAIFMT_CBM_CFS
:
430 srgr
= DAVINCI_MCBSP_SRGR_FSGM
;
431 clk_div
= dev
->clk_div
- 1;
432 srgr
|= DAVINCI_MCBSP_SRGR_FWID(mcbsp_word_length
* 8 - 1);
433 srgr
|= DAVINCI_MCBSP_SRGR_FPER(mcbsp_word_length
* 16 - 1);
437 case SND_SOC_DAIFMT_CBM_CFM
:
438 /* Clock and frame sync given from external sources */
439 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
440 srgr
= DAVINCI_MCBSP_SRGR_FSGM
;
441 srgr
|= DAVINCI_MCBSP_SRGR_FWID(snd_interval_value(i
) - 1);
442 pr_debug("%s - %d FWID set: re-read srgr = %X\n",
443 __func__
, __LINE__
, snd_interval_value(i
) - 1);
445 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_FRAME_BITS
);
446 srgr
|= DAVINCI_MCBSP_SRGR_FPER(snd_interval_value(i
) - 1);
451 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SRGR_REG
, srgr
);
453 rcr
= DAVINCI_MCBSP_RCR_RFIG
;
454 xcr
= DAVINCI_MCBSP_XCR_XFIG
;
455 if (dev
->mode
== MOD_DSP_B
) {
456 rcr
|= DAVINCI_MCBSP_RCR_RDATDLY(0);
457 xcr
|= DAVINCI_MCBSP_XCR_XDATDLY(0);
459 rcr
|= DAVINCI_MCBSP_RCR_RDATDLY(1);
460 xcr
|= DAVINCI_MCBSP_XCR_XDATDLY(1);
462 /* Determine xfer data type */
463 fmt
= params_format(params
);
464 if ((fmt
> SNDRV_PCM_FORMAT_S32_LE
) || !data_type
[fmt
]) {
465 printk(KERN_WARNING
"davinci-i2s: unsupported PCM format\n");
469 if (params_channels(params
) == 2) {
471 if (double_fmt
[fmt
] && dev
->enable_channel_combine
) {
473 fmt
= double_fmt
[fmt
];
476 case SND_SOC_DAIFMT_CBS_CFS
:
477 case SND_SOC_DAIFMT_CBS_CFM
:
478 rcr
|= DAVINCI_MCBSP_RCR_RFRLEN2(0);
479 xcr
|= DAVINCI_MCBSP_XCR_XFRLEN2(0);
480 rcr
|= DAVINCI_MCBSP_RCR_RPHASE
;
481 xcr
|= DAVINCI_MCBSP_XCR_XPHASE
;
483 case SND_SOC_DAIFMT_CBM_CFM
:
484 case SND_SOC_DAIFMT_CBM_CFS
:
485 rcr
|= DAVINCI_MCBSP_RCR_RFRLEN2(element_cnt
- 1);
486 xcr
|= DAVINCI_MCBSP_XCR_XFRLEN2(element_cnt
- 1);
492 mcbsp_word_length
= asp_word_length
[fmt
];
495 case SND_SOC_DAIFMT_CBS_CFS
:
496 case SND_SOC_DAIFMT_CBS_CFM
:
497 rcr
|= DAVINCI_MCBSP_RCR_RFRLEN1(0);
498 xcr
|= DAVINCI_MCBSP_XCR_XFRLEN1(0);
500 case SND_SOC_DAIFMT_CBM_CFM
:
501 case SND_SOC_DAIFMT_CBM_CFS
:
502 rcr
|= DAVINCI_MCBSP_RCR_RFRLEN1(element_cnt
- 1);
503 xcr
|= DAVINCI_MCBSP_XCR_XFRLEN1(element_cnt
- 1);
509 rcr
|= DAVINCI_MCBSP_RCR_RWDLEN1(mcbsp_word_length
) |
510 DAVINCI_MCBSP_RCR_RWDLEN2(mcbsp_word_length
);
511 xcr
|= DAVINCI_MCBSP_XCR_XWDLEN1(mcbsp_word_length
) |
512 DAVINCI_MCBSP_XCR_XWDLEN2(mcbsp_word_length
);
514 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
515 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_XCR_REG
, xcr
);
517 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_RCR_REG
, rcr
);
519 pr_debug("%s - %d srgr=%X\n", __func__
, __LINE__
, srgr
);
520 pr_debug("%s - %d xcr=%X\n", __func__
, __LINE__
, xcr
);
521 pr_debug("%s - %d rcr=%X\n", __func__
, __LINE__
, rcr
);
525 static int davinci_i2s_prepare(struct snd_pcm_substream
*substream
,
526 struct snd_soc_dai
*dai
)
528 struct davinci_mcbsp_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
529 int playback
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
531 u32 mask
= playback
? DAVINCI_MCBSP_SPCR_XRST
: DAVINCI_MCBSP_SPCR_RRST
;
533 davinci_mcbsp_stop(dev
, playback
);
535 spcr
= davinci_mcbsp_read_reg(dev
, DAVINCI_MCBSP_SPCR_REG
);
537 /* start off disabled */
538 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
,
540 toggle_clock(dev
, playback
);
542 if (dev
->pcr
& (DAVINCI_MCBSP_PCR_FSXM
| DAVINCI_MCBSP_PCR_FSRM
|
543 DAVINCI_MCBSP_PCR_CLKXM
| DAVINCI_MCBSP_PCR_CLKRM
)) {
544 /* Start the sample generator */
545 spcr
|= DAVINCI_MCBSP_SPCR_GRST
;
546 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
, spcr
);
550 /* Enable the transmitter */
551 spcr
= davinci_mcbsp_read_reg(dev
, DAVINCI_MCBSP_SPCR_REG
);
552 spcr
|= DAVINCI_MCBSP_SPCR_XRST
;
553 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
, spcr
);
555 /* wait for any unexpected frame sync error to occur */
558 /* Disable the transmitter to clear any outstanding XSYNCERR */
559 spcr
= davinci_mcbsp_read_reg(dev
, DAVINCI_MCBSP_SPCR_REG
);
560 spcr
&= ~DAVINCI_MCBSP_SPCR_XRST
;
561 davinci_mcbsp_write_reg(dev
, DAVINCI_MCBSP_SPCR_REG
, spcr
);
562 toggle_clock(dev
, playback
);
568 static int davinci_i2s_trigger(struct snd_pcm_substream
*substream
, int cmd
,
569 struct snd_soc_dai
*dai
)
571 struct davinci_mcbsp_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
573 int playback
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
576 case SNDRV_PCM_TRIGGER_START
:
577 case SNDRV_PCM_TRIGGER_RESUME
:
578 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
579 davinci_mcbsp_start(dev
, substream
);
581 case SNDRV_PCM_TRIGGER_STOP
:
582 case SNDRV_PCM_TRIGGER_SUSPEND
:
583 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
584 davinci_mcbsp_stop(dev
, playback
);
592 static void davinci_i2s_shutdown(struct snd_pcm_substream
*substream
,
593 struct snd_soc_dai
*dai
)
595 struct davinci_mcbsp_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
596 int playback
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
597 davinci_mcbsp_stop(dev
, playback
);
600 #define DAVINCI_I2S_RATES SNDRV_PCM_RATE_8000_96000
601 #define DAVINCI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
602 SNDRV_PCM_FMTBIT_S32_LE)
604 static const struct snd_soc_dai_ops davinci_i2s_dai_ops
= {
605 .shutdown
= davinci_i2s_shutdown
,
606 .prepare
= davinci_i2s_prepare
,
607 .trigger
= davinci_i2s_trigger
,
608 .hw_params
= davinci_i2s_hw_params
,
609 .set_fmt
= davinci_i2s_set_dai_fmt
,
610 .set_clkdiv
= davinci_i2s_dai_set_clkdiv
,
614 static int davinci_i2s_dai_probe(struct snd_soc_dai
*dai
)
616 struct davinci_mcbsp_dev
*dev
= snd_soc_dai_get_drvdata(dai
);
618 dai
->playback_dma_data
= &dev
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
];
619 dai
->capture_dma_data
= &dev
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
];
624 static struct snd_soc_dai_driver davinci_i2s_dai
= {
625 .probe
= davinci_i2s_dai_probe
,
629 .rates
= DAVINCI_I2S_RATES
,
630 .formats
= DAVINCI_I2S_FORMATS
,
635 .rates
= DAVINCI_I2S_RATES
,
636 .formats
= DAVINCI_I2S_FORMATS
,
638 .ops
= &davinci_i2s_dai_ops
,
642 static const struct snd_soc_component_driver davinci_i2s_component
= {
646 static int davinci_i2s_probe(struct platform_device
*pdev
)
648 struct snd_dmaengine_dai_dma_data
*dma_data
;
649 struct davinci_mcbsp_dev
*dev
;
650 struct resource
*mem
, *res
;
651 void __iomem
*io_base
;
655 mem
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mpu");
658 "\"mpu\" mem resource not found, using index 0\n");
659 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
661 dev_err(&pdev
->dev
, "no mem resource?\n");
666 io_base
= devm_ioremap_resource(&pdev
->dev
, mem
);
668 return PTR_ERR(io_base
);
670 dev
= devm_kzalloc(&pdev
->dev
, sizeof(struct davinci_mcbsp_dev
),
677 /* setup DMA, first TX, then RX */
678 dma_data
= &dev
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
];
679 dma_data
->addr
= (dma_addr_t
)(mem
->start
+ DAVINCI_MCBSP_DXR_REG
);
681 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
683 dma
= &dev
->dma_request
[SNDRV_PCM_STREAM_PLAYBACK
];
685 dma_data
->filter_data
= dma
;
686 } else if (IS_ENABLED(CONFIG_OF
) && pdev
->dev
.of_node
) {
687 dma_data
->filter_data
= "tx";
689 dev_err(&pdev
->dev
, "Missing DMA tx resource\n");
693 dma_data
= &dev
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
];
694 dma_data
->addr
= (dma_addr_t
)(mem
->start
+ DAVINCI_MCBSP_DRR_REG
);
696 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
698 dma
= &dev
->dma_request
[SNDRV_PCM_STREAM_CAPTURE
];
700 dma_data
->filter_data
= dma
;
701 } else if (IS_ENABLED(CONFIG_OF
) && pdev
->dev
.of_node
) {
702 dma_data
->filter_data
= "rx";
704 dev_err(&pdev
->dev
, "Missing DMA rx resource\n");
708 dev
->clk
= clk_get(&pdev
->dev
, NULL
);
709 if (IS_ERR(dev
->clk
))
711 clk_enable(dev
->clk
);
713 dev
->dev
= &pdev
->dev
;
714 dev_set_drvdata(&pdev
->dev
, dev
);
716 ret
= snd_soc_register_component(&pdev
->dev
, &davinci_i2s_component
,
717 &davinci_i2s_dai
, 1);
719 goto err_release_clk
;
721 ret
= edma_pcm_platform_register(&pdev
->dev
);
723 dev_err(&pdev
->dev
, "register PCM failed: %d\n", ret
);
724 goto err_unregister_component
;
729 err_unregister_component
:
730 snd_soc_unregister_component(&pdev
->dev
);
732 clk_disable(dev
->clk
);
737 static int davinci_i2s_remove(struct platform_device
*pdev
)
739 struct davinci_mcbsp_dev
*dev
= dev_get_drvdata(&pdev
->dev
);
741 snd_soc_unregister_component(&pdev
->dev
);
743 clk_disable(dev
->clk
);
750 static const struct of_device_id davinci_i2s_match
[] = {
751 { .compatible
= "ti,da850-mcbsp" },
754 MODULE_DEVICE_TABLE(of
, davinci_i2s_match
);
756 static struct platform_driver davinci_mcbsp_driver
= {
757 .probe
= davinci_i2s_probe
,
758 .remove
= davinci_i2s_remove
,
760 .name
= "davinci-mcbsp",
761 .of_match_table
= of_match_ptr(davinci_i2s_match
),
765 module_platform_driver(davinci_mcbsp_driver
);
767 MODULE_AUTHOR("Vladimir Barinov");
768 MODULE_DESCRIPTION("TI DAVINCI I2S (McBSP) SoC Interface");
769 MODULE_LICENSE("GPL");