blob: b2a5372ef72cdfb7cc18f79d93c622a301eed5fe [file] [log] [blame]
Vladimir Barinov310355c2008-02-18 11:40:22 +01001/*
2 * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
3 *
Vladimir Barinovd6b52032008-09-29 23:14:11 +04004 * Author: Vladimir Barinov, <vbarinov@embeddedalley.com>
Vladimir Barinov310355c2008-02-18 11:40:22 +01005 * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/device.h>
15#include <linux/delay.h>
16#include <linux/io.h>
17#include <linux/clk.h>
18
19#include <sound/core.h>
20#include <sound/pcm.h>
21#include <sound/pcm_params.h>
22#include <sound/initval.h>
23#include <sound/soc.h>
24
Mark Brownff7d04b2009-07-08 16:54:51 +010025#include <mach/asp.h>
26
Vladimir Barinov310355c2008-02-18 11:40:22 +010027#include "davinci-pcm.h"
28
David Brownella62114c2009-05-14 12:47:42 -070029
30/*
31 * NOTE: terminology here is confusing.
32 *
33 * - This driver supports the "Audio Serial Port" (ASP),
34 * found on dm6446, dm355, and other DaVinci chips.
35 *
36 * - But it labels it a "Multi-channel Buffered Serial Port"
37 * (McBSP) as on older chips like the dm642 ... which was
38 * backward-compatible, possibly explaining that confusion.
39 *
40 * - OMAP chips have a controller called McBSP, which is
41 * incompatible with the DaVinci flavor of McBSP.
42 *
43 * - Newer DaVinci chips have a controller called McASP,
44 * incompatible with ASP and with either McBSP.
45 *
46 * In short: this uses ASP to implement I2S, not McBSP.
47 * And it won't be the only DaVinci implemention of I2S.
48 */
Vladimir Barinov310355c2008-02-18 11:40:22 +010049#define DAVINCI_MCBSP_DRR_REG 0x00
50#define DAVINCI_MCBSP_DXR_REG 0x04
51#define DAVINCI_MCBSP_SPCR_REG 0x08
52#define DAVINCI_MCBSP_RCR_REG 0x0c
53#define DAVINCI_MCBSP_XCR_REG 0x10
54#define DAVINCI_MCBSP_SRGR_REG 0x14
55#define DAVINCI_MCBSP_PCR_REG 0x24
56
57#define DAVINCI_MCBSP_SPCR_RRST (1 << 0)
58#define DAVINCI_MCBSP_SPCR_RINTM(v) ((v) << 4)
59#define DAVINCI_MCBSP_SPCR_XRST (1 << 16)
60#define DAVINCI_MCBSP_SPCR_XINTM(v) ((v) << 20)
61#define DAVINCI_MCBSP_SPCR_GRST (1 << 22)
62#define DAVINCI_MCBSP_SPCR_FRST (1 << 23)
63#define DAVINCI_MCBSP_SPCR_FREE (1 << 25)
64
65#define DAVINCI_MCBSP_RCR_RWDLEN1(v) ((v) << 5)
66#define DAVINCI_MCBSP_RCR_RFRLEN1(v) ((v) << 8)
67#define DAVINCI_MCBSP_RCR_RDATDLY(v) ((v) << 16)
Troy Kiskyf5cfa952009-07-04 19:29:57 -070068#define DAVINCI_MCBSP_RCR_RFIG (1 << 18)
Vladimir Barinov310355c2008-02-18 11:40:22 +010069#define DAVINCI_MCBSP_RCR_RWDLEN2(v) ((v) << 21)
70
71#define DAVINCI_MCBSP_XCR_XWDLEN1(v) ((v) << 5)
72#define DAVINCI_MCBSP_XCR_XFRLEN1(v) ((v) << 8)
73#define DAVINCI_MCBSP_XCR_XDATDLY(v) ((v) << 16)
74#define DAVINCI_MCBSP_XCR_XFIG (1 << 18)
75#define DAVINCI_MCBSP_XCR_XWDLEN2(v) ((v) << 21)
76
77#define DAVINCI_MCBSP_SRGR_FWID(v) ((v) << 8)
78#define DAVINCI_MCBSP_SRGR_FPER(v) ((v) << 16)
79#define DAVINCI_MCBSP_SRGR_FSGM (1 << 28)
80
81#define DAVINCI_MCBSP_PCR_CLKRP (1 << 0)
82#define DAVINCI_MCBSP_PCR_CLKXP (1 << 1)
83#define DAVINCI_MCBSP_PCR_FSRP (1 << 2)
84#define DAVINCI_MCBSP_PCR_FSXP (1 << 3)
Hugo Villeneuveb402dff2008-11-08 13:26:09 -050085#define DAVINCI_MCBSP_PCR_SCLKME (1 << 7)
Vladimir Barinov310355c2008-02-18 11:40:22 +010086#define DAVINCI_MCBSP_PCR_CLKRM (1 << 8)
87#define DAVINCI_MCBSP_PCR_CLKXM (1 << 9)
88#define DAVINCI_MCBSP_PCR_FSRM (1 << 10)
89#define DAVINCI_MCBSP_PCR_FSXM (1 << 11)
90
Vladimir Barinov310355c2008-02-18 11:40:22 +010091enum {
92 DAVINCI_MCBSP_WORD_8 = 0,
93 DAVINCI_MCBSP_WORD_12,
94 DAVINCI_MCBSP_WORD_16,
95 DAVINCI_MCBSP_WORD_20,
96 DAVINCI_MCBSP_WORD_24,
97 DAVINCI_MCBSP_WORD_32,
98};
99
Troy Kisky0d6c9772009-11-18 17:49:51 -0700100static const unsigned char data_type[SNDRV_PCM_FORMAT_S32_LE + 1] = {
101 [SNDRV_PCM_FORMAT_S8] = 1,
102 [SNDRV_PCM_FORMAT_S16_LE] = 2,
103 [SNDRV_PCM_FORMAT_S32_LE] = 4,
104};
105
106static const unsigned char asp_word_length[SNDRV_PCM_FORMAT_S32_LE + 1] = {
107 [SNDRV_PCM_FORMAT_S8] = DAVINCI_MCBSP_WORD_8,
108 [SNDRV_PCM_FORMAT_S16_LE] = DAVINCI_MCBSP_WORD_16,
109 [SNDRV_PCM_FORMAT_S32_LE] = DAVINCI_MCBSP_WORD_32,
110};
111
112static const unsigned char double_fmt[SNDRV_PCM_FORMAT_S32_LE + 1] = {
113 [SNDRV_PCM_FORMAT_S8] = SNDRV_PCM_FORMAT_S16_LE,
114 [SNDRV_PCM_FORMAT_S16_LE] = SNDRV_PCM_FORMAT_S32_LE,
115};
116
Vladimir Barinov310355c2008-02-18 11:40:22 +0100117struct davinci_mcbsp_dev {
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700118 struct davinci_pcm_dma_params dma_params[2];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100119 void __iomem *base;
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700120#define MOD_DSP_A 0
121#define MOD_DSP_B 1
122 int mode;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700123 u32 pcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100124 struct clk *clk;
Troy Kisky0d6c9772009-11-18 17:49:51 -0700125 /*
126 * Combining both channels into 1 element will at least double the
127 * amount of time between servicing the dma channel, increase
128 * effiency, and reduce the chance of overrun/underrun. But,
129 * it will result in the left & right channels being swapped.
130 *
131 * If relabeling the left and right channels is not possible,
132 * you may want to let the codec know to swap them back.
133 *
134 * It may allow x10 the amount of time to service dma requests,
135 * if the codec is master and is using an unnecessarily fast bit clock
136 * (ie. tlvaic23b), independent of the sample rate. So, having an
137 * entire frame at once means it can be serviced at the sample rate
138 * instead of the bit clock rate.
139 *
140 * In the now unlikely case that an underrun still
141 * occurs, both the left and right samples will be repeated
142 * so that no pops are heard, and the left and right channels
143 * won't end up being swapped because of the underrun.
144 */
145 unsigned enable_channel_combine:1;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100146};
147
148static inline void davinci_mcbsp_write_reg(struct davinci_mcbsp_dev *dev,
149 int reg, u32 val)
150{
151 __raw_writel(val, dev->base + reg);
152}
153
154static inline u32 davinci_mcbsp_read_reg(struct davinci_mcbsp_dev *dev, int reg)
155{
156 return __raw_readl(dev->base + reg);
157}
158
Troy Kiskyc392bec2009-07-04 19:29:52 -0700159static void toggle_clock(struct davinci_mcbsp_dev *dev, int playback)
160{
161 u32 m = playback ? DAVINCI_MCBSP_PCR_CLKXP : DAVINCI_MCBSP_PCR_CLKRP;
162 /* The clock needs to toggle to complete reset.
163 * So, fake it by toggling the clk polarity.
164 */
165 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr ^ m);
166 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr);
167}
168
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700169static void davinci_mcbsp_start(struct davinci_mcbsp_dev *dev,
170 struct snd_pcm_substream *substream)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100171{
172 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530173 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000174 struct snd_soc_platform *platform = socdev->card->platform;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700175 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Troy Kisky35cf6352009-07-04 19:29:51 -0700176 u32 spcr;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700177 u32 mask = playback ? DAVINCI_MCBSP_SPCR_XRST : DAVINCI_MCBSP_SPCR_RRST;
Troy Kisky35cf6352009-07-04 19:29:51 -0700178 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700179 if (spcr & mask) {
180 /* start off disabled */
181 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG,
182 spcr & ~mask);
183 toggle_clock(dev, playback);
184 }
Troy Kisky1bef4492009-07-04 19:29:55 -0700185 if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM |
186 DAVINCI_MCBSP_PCR_CLKXM | DAVINCI_MCBSP_PCR_CLKRM)) {
187 /* Start the sample generator */
188 spcr |= DAVINCI_MCBSP_SPCR_GRST;
189 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
190 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100191
Troy Kisky1bef4492009-07-04 19:29:55 -0700192 if (playback) {
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530193 /* Stop the DMA to avoid data loss */
194 /* while the transmitter is out of reset to handle XSYNCERR */
195 if (platform->pcm_ops->trigger) {
Troy Kiskyeba575c2009-07-04 19:29:54 -0700196 int ret = platform->pcm_ops->trigger(substream,
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530197 SNDRV_PCM_TRIGGER_STOP);
198 if (ret < 0)
199 printk(KERN_DEBUG "Playback DMA stop failed\n");
200 }
201
202 /* Enable the transmitter */
Troy Kisky35cf6352009-07-04 19:29:51 -0700203 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
204 spcr |= DAVINCI_MCBSP_SPCR_XRST;
205 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530206
207 /* wait for any unexpected frame sync error to occur */
208 udelay(100);
209
210 /* Disable the transmitter to clear any outstanding XSYNCERR */
Troy Kisky35cf6352009-07-04 19:29:51 -0700211 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
212 spcr &= ~DAVINCI_MCBSP_SPCR_XRST;
213 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700214 toggle_clock(dev, playback);
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530215
216 /* Restart the DMA */
217 if (platform->pcm_ops->trigger) {
Troy Kiskyeba575c2009-07-04 19:29:54 -0700218 int ret = platform->pcm_ops->trigger(substream,
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530219 SNDRV_PCM_TRIGGER_START);
220 if (ret < 0)
221 printk(KERN_DEBUG "Playback DMA start failed\n");
222 }
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530223 }
224
Troy Kisky1bef4492009-07-04 19:29:55 -0700225 /* Enable transmitter or receiver */
Troy Kisky35cf6352009-07-04 19:29:51 -0700226 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Troy Kisky1bef4492009-07-04 19:29:55 -0700227 spcr |= mask;
228
229 if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM)) {
230 /* Start frame sync */
231 spcr |= DAVINCI_MCBSP_SPCR_FRST;
232 }
Troy Kisky35cf6352009-07-04 19:29:51 -0700233 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100234}
235
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700236static void davinci_mcbsp_stop(struct davinci_mcbsp_dev *dev, int playback)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100237{
Troy Kisky35cf6352009-07-04 19:29:51 -0700238 u32 spcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100239
240 /* Reset transmitter/receiver and sample rate/frame sync generators */
Troy Kisky35cf6352009-07-04 19:29:51 -0700241 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
242 spcr &= ~(DAVINCI_MCBSP_SPCR_GRST | DAVINCI_MCBSP_SPCR_FRST);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700243 spcr &= playback ? ~DAVINCI_MCBSP_SPCR_XRST : ~DAVINCI_MCBSP_SPCR_RRST;
Troy Kisky35cf6352009-07-04 19:29:51 -0700244 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700245 toggle_clock(dev, playback);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100246}
247
Troy Kisky21903c12008-12-18 12:36:43 -0700248#define DEFAULT_BITPERSAMPLE 16
249
Liam Girdwood9cb132d2008-07-07 16:07:42 +0100250static int davinci_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
Vladimir Barinov310355c2008-02-18 11:40:22 +0100251 unsigned int fmt)
252{
253 struct davinci_mcbsp_dev *dev = cpu_dai->private_data;
Troy Kisky21903c12008-12-18 12:36:43 -0700254 unsigned int pcr;
255 unsigned int srgr;
Troy Kisky21903c12008-12-18 12:36:43 -0700256 srgr = DAVINCI_MCBSP_SRGR_FSGM |
257 DAVINCI_MCBSP_SRGR_FPER(DEFAULT_BITPERSAMPLE * 2 - 1) |
258 DAVINCI_MCBSP_SRGR_FWID(DEFAULT_BITPERSAMPLE - 1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100259
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700260 /* set master/slave audio interface */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100261 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
262 case SND_SOC_DAIFMT_CBS_CFS:
Troy Kisky21903c12008-12-18 12:36:43 -0700263 /* cpu is master */
264 pcr = DAVINCI_MCBSP_PCR_FSXM |
265 DAVINCI_MCBSP_PCR_FSRM |
266 DAVINCI_MCBSP_PCR_CLKXM |
267 DAVINCI_MCBSP_PCR_CLKRM;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100268 break;
Hugo Villeneuveb402dff2008-11-08 13:26:09 -0500269 case SND_SOC_DAIFMT_CBM_CFS:
270 /* McBSP CLKR pin is the input for the Sample Rate Generator.
271 * McBSP FSR and FSX are driven by the Sample Rate Generator. */
Troy Kisky21903c12008-12-18 12:36:43 -0700272 pcr = DAVINCI_MCBSP_PCR_SCLKME |
273 DAVINCI_MCBSP_PCR_FSXM |
274 DAVINCI_MCBSP_PCR_FSRM;
Hugo Villeneuveb402dff2008-11-08 13:26:09 -0500275 break;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100276 case SND_SOC_DAIFMT_CBM_CFM:
Troy Kisky21903c12008-12-18 12:36:43 -0700277 /* codec is master */
278 pcr = 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100279 break;
280 default:
Troy Kisky21903c12008-12-18 12:36:43 -0700281 printk(KERN_ERR "%s:bad master\n", __func__);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100282 return -EINVAL;
283 }
284
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700285 /* interface format */
Troy Kisky69ab8202008-12-18 12:36:44 -0700286 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Troy Kisky69ab8202008-12-18 12:36:44 -0700287 case SND_SOC_DAIFMT_I2S:
Troy Kisky07d8d9d2008-12-19 13:05:24 -0700288 /* Davinci doesn't support TRUE I2S, but some codecs will have
289 * the left and right channels contiguous. This allows
290 * dsp_a mode to be used with an inverted normal frame clk.
291 * If your codec is master and does not have contiguous
292 * channels, then you will have sound on only one channel.
293 * Try using a different mode, or codec as slave.
294 *
295 * The TLV320AIC33 is an example of a codec where this works.
296 * It has a variable bit clock frequency allowing it to have
297 * valid data on every bit clock.
298 *
299 * The TLV320AIC23 is an example of a codec where this does not
300 * work. It has a fixed bit clock frequency with progressively
301 * more empty bit clock slots between channels as the sample
302 * rate is lowered.
303 */
304 fmt ^= SND_SOC_DAIFMT_NB_IF;
305 case SND_SOC_DAIFMT_DSP_A:
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700306 dev->mode = MOD_DSP_A;
307 break;
308 case SND_SOC_DAIFMT_DSP_B:
309 dev->mode = MOD_DSP_B;
Troy Kisky69ab8202008-12-18 12:36:44 -0700310 break;
311 default:
312 printk(KERN_ERR "%s:bad format\n", __func__);
313 return -EINVAL;
314 }
315
Vladimir Barinov310355c2008-02-18 11:40:22 +0100316 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
Troy Kisky9e031622008-12-19 13:05:23 -0700317 case SND_SOC_DAIFMT_NB_NF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700318 /* CLKRP Receive clock polarity,
319 * 1 - sampled on rising edge of CLKR
320 * valid on rising edge
321 * CLKXP Transmit clock polarity,
322 * 1 - clocked on falling edge of CLKX
323 * valid on rising edge
324 * FSRP Receive frame sync pol, 0 - active high
325 * FSXP Transmit frame sync pol, 0 - active high
326 */
Troy Kisky21903c12008-12-18 12:36:43 -0700327 pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100328 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700329 case SND_SOC_DAIFMT_IB_IF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700330 /* CLKRP Receive clock polarity,
331 * 0 - sampled on falling edge of CLKR
332 * valid on falling edge
333 * CLKXP Transmit clock polarity,
334 * 0 - clocked on rising edge of CLKX
335 * valid on falling edge
336 * FSRP Receive frame sync pol, 1 - active low
337 * FSXP Transmit frame sync pol, 1 - active low
338 */
Troy Kisky21903c12008-12-18 12:36:43 -0700339 pcr |= (DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100340 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700341 case SND_SOC_DAIFMT_NB_IF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700342 /* CLKRP Receive clock polarity,
343 * 1 - sampled on rising edge of CLKR
344 * valid on rising edge
345 * CLKXP Transmit clock polarity,
346 * 1 - clocked on falling edge of CLKX
347 * valid on rising edge
348 * FSRP Receive frame sync pol, 1 - active low
349 * FSXP Transmit frame sync pol, 1 - active low
350 */
Troy Kisky21903c12008-12-18 12:36:43 -0700351 pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP |
352 DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100353 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700354 case SND_SOC_DAIFMT_IB_NF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700355 /* CLKRP Receive clock polarity,
356 * 0 - sampled on falling edge of CLKR
357 * valid on falling edge
358 * CLKXP Transmit clock polarity,
359 * 0 - clocked on rising edge of CLKX
360 * valid on falling edge
361 * FSRP Receive frame sync pol, 0 - active high
362 * FSXP Transmit frame sync pol, 0 - active high
363 */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100364 break;
365 default:
366 return -EINVAL;
367 }
Troy Kisky21903c12008-12-18 12:36:43 -0700368 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700369 dev->pcr = pcr;
Troy Kisky21903c12008-12-18 12:36:43 -0700370 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, pcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100371 return 0;
372}
373
374static int davinci_i2s_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000375 struct snd_pcm_hw_params *params,
376 struct snd_soc_dai *dai)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100377{
Troy Kisky9bb74152009-08-06 16:55:31 -0700378 struct davinci_mcbsp_dev *dev = dai->private_data;
Troy Kisky81ac55a2009-09-11 14:29:02 -0700379 struct davinci_pcm_dma_params *dma_params =
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700380 &dev->dma_params[substream->stream];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100381 struct snd_interval *i = NULL;
382 int mcbsp_word_length;
Troy Kisky35cf6352009-07-04 19:29:51 -0700383 unsigned int rcr, xcr, srgr;
384 u32 spcr;
Troy Kisky0d6c9772009-11-18 17:49:51 -0700385 snd_pcm_format_t fmt;
386 unsigned element_cnt = 1;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100387
388 /* general line settings */
Troy Kisky35cf6352009-07-04 19:29:51 -0700389 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530390 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Troy Kisky35cf6352009-07-04 19:29:51 -0700391 spcr |= DAVINCI_MCBSP_SPCR_RINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
392 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530393 } else {
Troy Kisky35cf6352009-07-04 19:29:51 -0700394 spcr |= DAVINCI_MCBSP_SPCR_XINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
395 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530396 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100397
398 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Troy Kisky35cf6352009-07-04 19:29:51 -0700399 srgr = DAVINCI_MCBSP_SRGR_FSGM;
400 srgr |= DAVINCI_MCBSP_SRGR_FWID(snd_interval_value(i) - 1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100401
402 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_FRAME_BITS);
Troy Kisky35cf6352009-07-04 19:29:51 -0700403 srgr |= DAVINCI_MCBSP_SRGR_FPER(snd_interval_value(i) - 1);
404 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100405
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700406 rcr = DAVINCI_MCBSP_RCR_RFIG;
407 xcr = DAVINCI_MCBSP_XCR_XFIG;
408 if (dev->mode == MOD_DSP_B) {
409 rcr |= DAVINCI_MCBSP_RCR_RDATDLY(0);
410 xcr |= DAVINCI_MCBSP_XCR_XDATDLY(0);
411 } else {
412 rcr |= DAVINCI_MCBSP_RCR_RDATDLY(1);
413 xcr |= DAVINCI_MCBSP_XCR_XDATDLY(1);
414 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100415 /* Determine xfer data type */
Troy Kisky0d6c9772009-11-18 17:49:51 -0700416 fmt = params_format(params);
417 if ((fmt > SNDRV_PCM_FORMAT_S32_LE) || !data_type[fmt]) {
Jean Delvare9b6e12e2008-08-26 15:47:55 +0200418 printk(KERN_WARNING "davinci-i2s: unsupported PCM format\n");
Vladimir Barinov310355c2008-02-18 11:40:22 +0100419 return -EINVAL;
420 }
421
Troy Kisky0d6c9772009-11-18 17:49:51 -0700422 if (params_channels(params) == 2) {
423 element_cnt = 2;
424 if (double_fmt[fmt] && dev->enable_channel_combine) {
425 element_cnt = 1;
426 fmt = double_fmt[fmt];
427 }
428 }
429 dma_params->acnt = dma_params->data_type = data_type[fmt];
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400430 dma_params->fifo_level = 0;
Troy Kisky0d6c9772009-11-18 17:49:51 -0700431 mcbsp_word_length = asp_word_length[fmt];
432 rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(element_cnt - 1);
433 xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(element_cnt - 1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100434
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700435 rcr |= DAVINCI_MCBSP_RCR_RWDLEN1(mcbsp_word_length) |
436 DAVINCI_MCBSP_RCR_RWDLEN2(mcbsp_word_length);
437 xcr |= DAVINCI_MCBSP_XCR_XWDLEN1(mcbsp_word_length) |
438 DAVINCI_MCBSP_XCR_XWDLEN2(mcbsp_word_length);
439
440 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Troy Kisky35cf6352009-07-04 19:29:51 -0700441 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_XCR_REG, xcr);
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700442 else
443 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_RCR_REG, rcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100444 return 0;
445}
446
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700447static int davinci_i2s_prepare(struct snd_pcm_substream *substream,
448 struct snd_soc_dai *dai)
449{
Troy Kisky9bb74152009-08-06 16:55:31 -0700450 struct davinci_mcbsp_dev *dev = dai->private_data;
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700451 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
452 davinci_mcbsp_stop(dev, playback);
453 if ((dev->pcr & DAVINCI_MCBSP_PCR_FSXM) == 0) {
454 /* codec is master */
455 davinci_mcbsp_start(dev, substream);
456 }
457 return 0;
458}
459
Mark Browndee89c42008-11-18 22:11:38 +0000460static int davinci_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
461 struct snd_soc_dai *dai)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100462{
Troy Kisky9bb74152009-08-06 16:55:31 -0700463 struct davinci_mcbsp_dev *dev = dai->private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100464 int ret = 0;
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700465 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700466 if ((dev->pcr & DAVINCI_MCBSP_PCR_FSXM) == 0)
467 return 0; /* return if codec is master */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100468
469 switch (cmd) {
470 case SNDRV_PCM_TRIGGER_START:
471 case SNDRV_PCM_TRIGGER_RESUME:
472 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700473 davinci_mcbsp_start(dev, substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100474 break;
475 case SNDRV_PCM_TRIGGER_STOP:
476 case SNDRV_PCM_TRIGGER_SUSPEND:
477 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700478 davinci_mcbsp_stop(dev, playback);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100479 break;
480 default:
481 ret = -EINVAL;
482 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100483 return ret;
484}
485
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700486static void davinci_i2s_shutdown(struct snd_pcm_substream *substream,
487 struct snd_soc_dai *dai)
488{
Troy Kisky9bb74152009-08-06 16:55:31 -0700489 struct davinci_mcbsp_dev *dev = dai->private_data;
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700490 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
491 davinci_mcbsp_stop(dev, playback);
492}
493
Chaithrika U S5204d492009-06-05 06:28:23 -0400494#define DAVINCI_I2S_RATES SNDRV_PCM_RATE_8000_96000
495
496static struct snd_soc_dai_ops davinci_i2s_dai_ops = {
Mark Brown3f405b42009-07-07 19:18:46 +0100497 .shutdown = davinci_i2s_shutdown,
498 .prepare = davinci_i2s_prepare,
Chaithrika U S5204d492009-06-05 06:28:23 -0400499 .trigger = davinci_i2s_trigger,
500 .hw_params = davinci_i2s_hw_params,
501 .set_fmt = davinci_i2s_set_dai_fmt,
502
503};
504
505struct snd_soc_dai davinci_i2s_dai = {
506 .name = "davinci-i2s",
507 .id = 0,
508 .playback = {
509 .channels_min = 2,
510 .channels_max = 2,
511 .rates = DAVINCI_I2S_RATES,
512 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
513 .capture = {
514 .channels_min = 2,
515 .channels_max = 2,
516 .rates = DAVINCI_I2S_RATES,
517 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
518 .ops = &davinci_i2s_dai_ops,
519
520};
521EXPORT_SYMBOL_GPL(davinci_i2s_dai);
522
523static int davinci_i2s_probe(struct platform_device *pdev)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100524{
Chaithrika U S5204d492009-06-05 06:28:23 -0400525 struct snd_platform_data *pdata = pdev->dev.platform_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100526 struct davinci_mcbsp_dev *dev;
Chaithrika U S5204d492009-06-05 06:28:23 -0400527 struct resource *mem, *ioarea, *res;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100528 int ret;
529
530 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
531 if (!mem) {
532 dev_err(&pdev->dev, "no mem resource?\n");
533 return -ENODEV;
534 }
535
536 ioarea = request_mem_region(mem->start, (mem->end - mem->start) + 1,
537 pdev->name);
538 if (!ioarea) {
539 dev_err(&pdev->dev, "McBSP region already claimed\n");
540 return -EBUSY;
541 }
542
543 dev = kzalloc(sizeof(struct davinci_mcbsp_dev), GFP_KERNEL);
544 if (!dev) {
545 ret = -ENOMEM;
546 goto err_release_region;
547 }
Troy Kisky0d6c9772009-11-18 17:49:51 -0700548 if (pdata)
549 dev->enable_channel_combine = pdata->enable_channel_combine;
Kevin Hilman3e46a442009-07-15 10:42:09 -0700550 dev->clk = clk_get(&pdev->dev, NULL);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100551 if (IS_ERR(dev->clk)) {
552 ret = -ENODEV;
553 goto err_free_mem;
554 }
555 clk_enable(dev->clk);
556
557 dev->base = (void __iomem *)IO_ADDRESS(mem->start);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100558
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700559 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].dma_addr =
Vladimir Barinov310355c2008-02-18 11:40:22 +0100560 (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DXR_REG);
561
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700562 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].dma_addr =
Vladimir Barinov310355c2008-02-18 11:40:22 +0100563 (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DRR_REG);
564
Chaithrika U S5204d492009-06-05 06:28:23 -0400565 /* first TX, then RX */
566 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
567 if (!res) {
568 dev_err(&pdev->dev, "no DMA resource\n");
Chaithrika U Sefd13be2009-06-08 06:49:41 -0400569 ret = -ENXIO;
Chaithrika U S5204d492009-06-05 06:28:23 -0400570 goto err_free_mem;
571 }
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700572 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].channel = res->start;
Chaithrika U S5204d492009-06-05 06:28:23 -0400573
574 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
575 if (!res) {
576 dev_err(&pdev->dev, "no DMA resource\n");
Chaithrika U Sefd13be2009-06-08 06:49:41 -0400577 ret = -ENXIO;
Chaithrika U S5204d492009-06-05 06:28:23 -0400578 goto err_free_mem;
579 }
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700580 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].channel = res->start;
Chaithrika U S5204d492009-06-05 06:28:23 -0400581
582 davinci_i2s_dai.private_data = dev;
Troy Kisky57512c62009-11-16 16:52:31 -0700583 davinci_i2s_dai.dma_data = dev->dma_params;
Chaithrika U S5204d492009-06-05 06:28:23 -0400584 ret = snd_soc_register_dai(&davinci_i2s_dai);
585 if (ret != 0)
586 goto err_free_mem;
587
Vladimir Barinov310355c2008-02-18 11:40:22 +0100588 return 0;
589
590err_free_mem:
591 kfree(dev);
592err_release_region:
593 release_mem_region(mem->start, (mem->end - mem->start) + 1);
594
595 return ret;
596}
597
Chaithrika U S5204d492009-06-05 06:28:23 -0400598static int davinci_i2s_remove(struct platform_device *pdev)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100599{
Chaithrika U S5204d492009-06-05 06:28:23 -0400600 struct davinci_mcbsp_dev *dev = davinci_i2s_dai.private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100601 struct resource *mem;
602
Chaithrika U S5204d492009-06-05 06:28:23 -0400603 snd_soc_unregister_dai(&davinci_i2s_dai);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100604 clk_disable(dev->clk);
605 clk_put(dev->clk);
606 dev->clk = NULL;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100607 kfree(dev);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100608 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
609 release_mem_region(mem->start, (mem->end - mem->start) + 1);
Chaithrika U S5204d492009-06-05 06:28:23 -0400610
611 return 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100612}
613
Chaithrika U S5204d492009-06-05 06:28:23 -0400614static struct platform_driver davinci_mcbsp_driver = {
615 .probe = davinci_i2s_probe,
616 .remove = davinci_i2s_remove,
617 .driver = {
618 .name = "davinci-asp",
619 .owner = THIS_MODULE,
620 },
Eric Miao6335d052009-03-03 09:41:00 +0800621};
622
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100623static int __init davinci_i2s_init(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000624{
Chaithrika U S5204d492009-06-05 06:28:23 -0400625 return platform_driver_register(&davinci_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000626}
627module_init(davinci_i2s_init);
628
629static void __exit davinci_i2s_exit(void)
630{
Chaithrika U S5204d492009-06-05 06:28:23 -0400631 platform_driver_unregister(&davinci_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000632}
633module_exit(davinci_i2s_exit);
634
Vladimir Barinov310355c2008-02-18 11:40:22 +0100635MODULE_AUTHOR("Vladimir Barinov");
636MODULE_DESCRIPTION("TI DAVINCI I2S (McBSP) SoC Interface");
637MODULE_LICENSE("GPL");