blob: 2a56fb78f67a5a48366a5228a56ddbbb8bfe4628 [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
100static struct davinci_pcm_dma_params davinci_i2s_pcm_out = {
101 .name = "I2S PCM Stereo out",
102};
103
104static struct davinci_pcm_dma_params davinci_i2s_pcm_in = {
105 .name = "I2S PCM Stereo in",
106};
107
108struct davinci_mcbsp_dev {
109 void __iomem *base;
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700110#define MOD_DSP_A 0
111#define MOD_DSP_B 1
112 int mode;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700113 u32 pcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100114 struct clk *clk;
115 struct davinci_pcm_dma_params *dma_params[2];
116};
117
118static inline void davinci_mcbsp_write_reg(struct davinci_mcbsp_dev *dev,
119 int reg, u32 val)
120{
121 __raw_writel(val, dev->base + reg);
122}
123
124static inline u32 davinci_mcbsp_read_reg(struct davinci_mcbsp_dev *dev, int reg)
125{
126 return __raw_readl(dev->base + reg);
127}
128
Troy Kiskyc392bec2009-07-04 19:29:52 -0700129static void toggle_clock(struct davinci_mcbsp_dev *dev, int playback)
130{
131 u32 m = playback ? DAVINCI_MCBSP_PCR_CLKXP : DAVINCI_MCBSP_PCR_CLKRP;
132 /* The clock needs to toggle to complete reset.
133 * So, fake it by toggling the clk polarity.
134 */
135 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr ^ m);
136 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr);
137}
138
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700139static void davinci_mcbsp_start(struct davinci_mcbsp_dev *dev,
140 struct snd_pcm_substream *substream)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100141{
142 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530143 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000144 struct snd_soc_platform *platform = socdev->card->platform;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700145 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Troy Kisky35cf6352009-07-04 19:29:51 -0700146 u32 spcr;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700147 u32 mask = playback ? DAVINCI_MCBSP_SPCR_XRST : DAVINCI_MCBSP_SPCR_RRST;
Troy Kisky35cf6352009-07-04 19:29:51 -0700148 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700149 if (spcr & mask) {
150 /* start off disabled */
151 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG,
152 spcr & ~mask);
153 toggle_clock(dev, playback);
154 }
Troy Kisky1bef4492009-07-04 19:29:55 -0700155 if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM |
156 DAVINCI_MCBSP_PCR_CLKXM | DAVINCI_MCBSP_PCR_CLKRM)) {
157 /* Start the sample generator */
158 spcr |= DAVINCI_MCBSP_SPCR_GRST;
159 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
160 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100161
Troy Kisky1bef4492009-07-04 19:29:55 -0700162 if (playback) {
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530163 /* Stop the DMA to avoid data loss */
164 /* while the transmitter is out of reset to handle XSYNCERR */
165 if (platform->pcm_ops->trigger) {
Troy Kiskyeba575c2009-07-04 19:29:54 -0700166 int ret = platform->pcm_ops->trigger(substream,
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530167 SNDRV_PCM_TRIGGER_STOP);
168 if (ret < 0)
169 printk(KERN_DEBUG "Playback DMA stop failed\n");
170 }
171
172 /* Enable the transmitter */
Troy Kisky35cf6352009-07-04 19:29:51 -0700173 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
174 spcr |= DAVINCI_MCBSP_SPCR_XRST;
175 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530176
177 /* wait for any unexpected frame sync error to occur */
178 udelay(100);
179
180 /* Disable the transmitter to clear any outstanding XSYNCERR */
Troy Kisky35cf6352009-07-04 19:29:51 -0700181 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
182 spcr &= ~DAVINCI_MCBSP_SPCR_XRST;
183 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700184 toggle_clock(dev, playback);
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530185
186 /* Restart the DMA */
187 if (platform->pcm_ops->trigger) {
Troy Kiskyeba575c2009-07-04 19:29:54 -0700188 int ret = platform->pcm_ops->trigger(substream,
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530189 SNDRV_PCM_TRIGGER_START);
190 if (ret < 0)
191 printk(KERN_DEBUG "Playback DMA start failed\n");
192 }
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530193 }
194
Troy Kisky1bef4492009-07-04 19:29:55 -0700195 /* Enable transmitter or receiver */
Troy Kisky35cf6352009-07-04 19:29:51 -0700196 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Troy Kisky1bef4492009-07-04 19:29:55 -0700197 spcr |= mask;
198
199 if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM)) {
200 /* Start frame sync */
201 spcr |= DAVINCI_MCBSP_SPCR_FRST;
202 }
Troy Kisky35cf6352009-07-04 19:29:51 -0700203 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100204}
205
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700206static void davinci_mcbsp_stop(struct davinci_mcbsp_dev *dev, int playback)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100207{
Troy Kisky35cf6352009-07-04 19:29:51 -0700208 u32 spcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100209
210 /* Reset transmitter/receiver and sample rate/frame sync generators */
Troy Kisky35cf6352009-07-04 19:29:51 -0700211 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
212 spcr &= ~(DAVINCI_MCBSP_SPCR_GRST | DAVINCI_MCBSP_SPCR_FRST);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700213 spcr &= playback ? ~DAVINCI_MCBSP_SPCR_XRST : ~DAVINCI_MCBSP_SPCR_RRST;
Troy Kisky35cf6352009-07-04 19:29:51 -0700214 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700215 toggle_clock(dev, playback);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100216}
217
Mark Browndee89c42008-11-18 22:11:38 +0000218static int davinci_i2s_startup(struct snd_pcm_substream *substream,
Troy Kisky9333b592009-07-04 19:29:56 -0700219 struct snd_soc_dai *cpu_dai)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100220{
Troy Kisky9333b592009-07-04 19:29:56 -0700221 struct davinci_mcbsp_dev *dev = cpu_dai->private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100222 cpu_dai->dma_data = dev->dma_params[substream->stream];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100223 return 0;
224}
225
Troy Kisky21903c12008-12-18 12:36:43 -0700226#define DEFAULT_BITPERSAMPLE 16
227
Liam Girdwood9cb132d2008-07-07 16:07:42 +0100228static int davinci_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
Vladimir Barinov310355c2008-02-18 11:40:22 +0100229 unsigned int fmt)
230{
231 struct davinci_mcbsp_dev *dev = cpu_dai->private_data;
Troy Kisky21903c12008-12-18 12:36:43 -0700232 unsigned int pcr;
233 unsigned int srgr;
Troy Kisky21903c12008-12-18 12:36:43 -0700234 srgr = DAVINCI_MCBSP_SRGR_FSGM |
235 DAVINCI_MCBSP_SRGR_FPER(DEFAULT_BITPERSAMPLE * 2 - 1) |
236 DAVINCI_MCBSP_SRGR_FWID(DEFAULT_BITPERSAMPLE - 1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100237
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700238 /* set master/slave audio interface */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100239 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
240 case SND_SOC_DAIFMT_CBS_CFS:
Troy Kisky21903c12008-12-18 12:36:43 -0700241 /* cpu is master */
242 pcr = DAVINCI_MCBSP_PCR_FSXM |
243 DAVINCI_MCBSP_PCR_FSRM |
244 DAVINCI_MCBSP_PCR_CLKXM |
245 DAVINCI_MCBSP_PCR_CLKRM;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100246 break;
Hugo Villeneuveb402dff2008-11-08 13:26:09 -0500247 case SND_SOC_DAIFMT_CBM_CFS:
248 /* McBSP CLKR pin is the input for the Sample Rate Generator.
249 * McBSP FSR and FSX are driven by the Sample Rate Generator. */
Troy Kisky21903c12008-12-18 12:36:43 -0700250 pcr = DAVINCI_MCBSP_PCR_SCLKME |
251 DAVINCI_MCBSP_PCR_FSXM |
252 DAVINCI_MCBSP_PCR_FSRM;
Hugo Villeneuveb402dff2008-11-08 13:26:09 -0500253 break;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100254 case SND_SOC_DAIFMT_CBM_CFM:
Troy Kisky21903c12008-12-18 12:36:43 -0700255 /* codec is master */
256 pcr = 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100257 break;
258 default:
Troy Kisky21903c12008-12-18 12:36:43 -0700259 printk(KERN_ERR "%s:bad master\n", __func__);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100260 return -EINVAL;
261 }
262
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700263 /* interface format */
Troy Kisky69ab8202008-12-18 12:36:44 -0700264 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Troy Kisky69ab8202008-12-18 12:36:44 -0700265 case SND_SOC_DAIFMT_I2S:
Troy Kisky07d8d9d2008-12-19 13:05:24 -0700266 /* Davinci doesn't support TRUE I2S, but some codecs will have
267 * the left and right channels contiguous. This allows
268 * dsp_a mode to be used with an inverted normal frame clk.
269 * If your codec is master and does not have contiguous
270 * channels, then you will have sound on only one channel.
271 * Try using a different mode, or codec as slave.
272 *
273 * The TLV320AIC33 is an example of a codec where this works.
274 * It has a variable bit clock frequency allowing it to have
275 * valid data on every bit clock.
276 *
277 * The TLV320AIC23 is an example of a codec where this does not
278 * work. It has a fixed bit clock frequency with progressively
279 * more empty bit clock slots between channels as the sample
280 * rate is lowered.
281 */
282 fmt ^= SND_SOC_DAIFMT_NB_IF;
283 case SND_SOC_DAIFMT_DSP_A:
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700284 dev->mode = MOD_DSP_A;
285 break;
286 case SND_SOC_DAIFMT_DSP_B:
287 dev->mode = MOD_DSP_B;
Troy Kisky69ab8202008-12-18 12:36:44 -0700288 break;
289 default:
290 printk(KERN_ERR "%s:bad format\n", __func__);
291 return -EINVAL;
292 }
293
Vladimir Barinov310355c2008-02-18 11:40:22 +0100294 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
Troy Kisky9e031622008-12-19 13:05:23 -0700295 case SND_SOC_DAIFMT_NB_NF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700296 /* CLKRP Receive clock polarity,
297 * 1 - sampled on rising edge of CLKR
298 * valid on rising edge
299 * CLKXP Transmit clock polarity,
300 * 1 - clocked on falling edge of CLKX
301 * valid on rising edge
302 * FSRP Receive frame sync pol, 0 - active high
303 * FSXP Transmit frame sync pol, 0 - active high
304 */
Troy Kisky21903c12008-12-18 12:36:43 -0700305 pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100306 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700307 case SND_SOC_DAIFMT_IB_IF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700308 /* CLKRP Receive clock polarity,
309 * 0 - sampled on falling edge of CLKR
310 * valid on falling edge
311 * CLKXP Transmit clock polarity,
312 * 0 - clocked on rising edge of CLKX
313 * valid on falling edge
314 * FSRP Receive frame sync pol, 1 - active low
315 * FSXP Transmit frame sync pol, 1 - active low
316 */
Troy Kisky21903c12008-12-18 12:36:43 -0700317 pcr |= (DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100318 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700319 case SND_SOC_DAIFMT_NB_IF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700320 /* CLKRP Receive clock polarity,
321 * 1 - sampled on rising edge of CLKR
322 * valid on rising edge
323 * CLKXP Transmit clock polarity,
324 * 1 - clocked on falling edge of CLKX
325 * valid on rising edge
326 * FSRP Receive frame sync pol, 1 - active low
327 * FSXP Transmit frame sync pol, 1 - active low
328 */
Troy Kisky21903c12008-12-18 12:36:43 -0700329 pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP |
330 DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100331 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700332 case SND_SOC_DAIFMT_IB_NF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700333 /* CLKRP Receive clock polarity,
334 * 0 - sampled on falling edge of CLKR
335 * valid on falling edge
336 * CLKXP Transmit clock polarity,
337 * 0 - clocked on rising edge of CLKX
338 * valid on falling edge
339 * FSRP Receive frame sync pol, 0 - active high
340 * FSXP Transmit frame sync pol, 0 - active high
341 */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100342 break;
343 default:
344 return -EINVAL;
345 }
Troy Kisky21903c12008-12-18 12:36:43 -0700346 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700347 dev->pcr = pcr;
Troy Kisky21903c12008-12-18 12:36:43 -0700348 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, pcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100349 return 0;
350}
351
352static int davinci_i2s_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000353 struct snd_pcm_hw_params *params,
354 struct snd_soc_dai *dai)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100355{
Troy Kisky9bb74152009-08-06 16:55:31 -0700356 struct davinci_pcm_dma_params *dma_params = dai->dma_data;
357 struct davinci_mcbsp_dev *dev = dai->private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100358 struct snd_interval *i = NULL;
359 int mcbsp_word_length;
Troy Kisky35cf6352009-07-04 19:29:51 -0700360 unsigned int rcr, xcr, srgr;
361 u32 spcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100362
363 /* general line settings */
Troy Kisky35cf6352009-07-04 19:29:51 -0700364 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530365 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Troy Kisky35cf6352009-07-04 19:29:51 -0700366 spcr |= DAVINCI_MCBSP_SPCR_RINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
367 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530368 } else {
Troy Kisky35cf6352009-07-04 19:29:51 -0700369 spcr |= DAVINCI_MCBSP_SPCR_XINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
370 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530371 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100372
373 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Troy Kisky35cf6352009-07-04 19:29:51 -0700374 srgr = DAVINCI_MCBSP_SRGR_FSGM;
375 srgr |= DAVINCI_MCBSP_SRGR_FWID(snd_interval_value(i) - 1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100376
377 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_FRAME_BITS);
Troy Kisky35cf6352009-07-04 19:29:51 -0700378 srgr |= DAVINCI_MCBSP_SRGR_FPER(snd_interval_value(i) - 1);
379 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100380
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700381 rcr = DAVINCI_MCBSP_RCR_RFIG;
382 xcr = DAVINCI_MCBSP_XCR_XFIG;
383 if (dev->mode == MOD_DSP_B) {
384 rcr |= DAVINCI_MCBSP_RCR_RDATDLY(0);
385 xcr |= DAVINCI_MCBSP_XCR_XDATDLY(0);
386 } else {
387 rcr |= DAVINCI_MCBSP_RCR_RDATDLY(1);
388 xcr |= DAVINCI_MCBSP_XCR_XDATDLY(1);
389 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100390 /* Determine xfer data type */
391 switch (params_format(params)) {
392 case SNDRV_PCM_FORMAT_S8:
393 dma_params->data_type = 1;
394 mcbsp_word_length = DAVINCI_MCBSP_WORD_8;
395 break;
396 case SNDRV_PCM_FORMAT_S16_LE:
397 dma_params->data_type = 2;
398 mcbsp_word_length = DAVINCI_MCBSP_WORD_16;
399 break;
400 case SNDRV_PCM_FORMAT_S32_LE:
401 dma_params->data_type = 4;
402 mcbsp_word_length = DAVINCI_MCBSP_WORD_32;
403 break;
404 default:
Jean Delvare9b6e12e2008-08-26 15:47:55 +0200405 printk(KERN_WARNING "davinci-i2s: unsupported PCM format\n");
Vladimir Barinov310355c2008-02-18 11:40:22 +0100406 return -EINVAL;
407 }
408
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700409 rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(1);
410 xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100411
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700412 rcr |= DAVINCI_MCBSP_RCR_RWDLEN1(mcbsp_word_length) |
413 DAVINCI_MCBSP_RCR_RWDLEN2(mcbsp_word_length);
414 xcr |= DAVINCI_MCBSP_XCR_XWDLEN1(mcbsp_word_length) |
415 DAVINCI_MCBSP_XCR_XWDLEN2(mcbsp_word_length);
416
417 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Troy Kisky35cf6352009-07-04 19:29:51 -0700418 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_XCR_REG, xcr);
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700419 else
420 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_RCR_REG, rcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100421 return 0;
422}
423
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700424static int davinci_i2s_prepare(struct snd_pcm_substream *substream,
425 struct snd_soc_dai *dai)
426{
Troy Kisky9bb74152009-08-06 16:55:31 -0700427 struct davinci_mcbsp_dev *dev = dai->private_data;
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700428 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
429 davinci_mcbsp_stop(dev, playback);
430 if ((dev->pcr & DAVINCI_MCBSP_PCR_FSXM) == 0) {
431 /* codec is master */
432 davinci_mcbsp_start(dev, substream);
433 }
434 return 0;
435}
436
Mark Browndee89c42008-11-18 22:11:38 +0000437static int davinci_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
438 struct snd_soc_dai *dai)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100439{
Troy Kisky9bb74152009-08-06 16:55:31 -0700440 struct davinci_mcbsp_dev *dev = dai->private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100441 int ret = 0;
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700442 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700443 if ((dev->pcr & DAVINCI_MCBSP_PCR_FSXM) == 0)
444 return 0; /* return if codec is master */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100445
446 switch (cmd) {
447 case SNDRV_PCM_TRIGGER_START:
448 case SNDRV_PCM_TRIGGER_RESUME:
449 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700450 davinci_mcbsp_start(dev, substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100451 break;
452 case SNDRV_PCM_TRIGGER_STOP:
453 case SNDRV_PCM_TRIGGER_SUSPEND:
454 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700455 davinci_mcbsp_stop(dev, playback);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100456 break;
457 default:
458 ret = -EINVAL;
459 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100460 return ret;
461}
462
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700463static void davinci_i2s_shutdown(struct snd_pcm_substream *substream,
464 struct snd_soc_dai *dai)
465{
Troy Kisky9bb74152009-08-06 16:55:31 -0700466 struct davinci_mcbsp_dev *dev = dai->private_data;
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700467 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
468 davinci_mcbsp_stop(dev, playback);
469}
470
Chaithrika U S5204d492009-06-05 06:28:23 -0400471#define DAVINCI_I2S_RATES SNDRV_PCM_RATE_8000_96000
472
473static struct snd_soc_dai_ops davinci_i2s_dai_ops = {
474 .startup = davinci_i2s_startup,
Mark Brown3f405b42009-07-07 19:18:46 +0100475 .shutdown = davinci_i2s_shutdown,
476 .prepare = davinci_i2s_prepare,
Chaithrika U S5204d492009-06-05 06:28:23 -0400477 .trigger = davinci_i2s_trigger,
478 .hw_params = davinci_i2s_hw_params,
479 .set_fmt = davinci_i2s_set_dai_fmt,
480
481};
482
483struct snd_soc_dai davinci_i2s_dai = {
484 .name = "davinci-i2s",
485 .id = 0,
486 .playback = {
487 .channels_min = 2,
488 .channels_max = 2,
489 .rates = DAVINCI_I2S_RATES,
490 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
491 .capture = {
492 .channels_min = 2,
493 .channels_max = 2,
494 .rates = DAVINCI_I2S_RATES,
495 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
496 .ops = &davinci_i2s_dai_ops,
497
498};
499EXPORT_SYMBOL_GPL(davinci_i2s_dai);
500
501static int davinci_i2s_probe(struct platform_device *pdev)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100502{
Chaithrika U S5204d492009-06-05 06:28:23 -0400503 struct snd_platform_data *pdata = pdev->dev.platform_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100504 struct davinci_mcbsp_dev *dev;
Chaithrika U S5204d492009-06-05 06:28:23 -0400505 struct resource *mem, *ioarea, *res;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100506 int ret;
507
508 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
509 if (!mem) {
510 dev_err(&pdev->dev, "no mem resource?\n");
511 return -ENODEV;
512 }
513
514 ioarea = request_mem_region(mem->start, (mem->end - mem->start) + 1,
515 pdev->name);
516 if (!ioarea) {
517 dev_err(&pdev->dev, "McBSP region already claimed\n");
518 return -EBUSY;
519 }
520
521 dev = kzalloc(sizeof(struct davinci_mcbsp_dev), GFP_KERNEL);
522 if (!dev) {
523 ret = -ENOMEM;
524 goto err_release_region;
525 }
526
Kevin Hilman3e46a442009-07-15 10:42:09 -0700527 dev->clk = clk_get(&pdev->dev, NULL);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100528 if (IS_ERR(dev->clk)) {
529 ret = -ENODEV;
530 goto err_free_mem;
531 }
532 clk_enable(dev->clk);
533
534 dev->base = (void __iomem *)IO_ADDRESS(mem->start);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100535
536 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK] = &davinci_i2s_pcm_out;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100537 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK]->dma_addr =
538 (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DXR_REG);
539
540 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE] = &davinci_i2s_pcm_in;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100541 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE]->dma_addr =
542 (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DRR_REG);
543
Chaithrika U S5204d492009-06-05 06:28:23 -0400544 /* first TX, then RX */
545 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
546 if (!res) {
547 dev_err(&pdev->dev, "no DMA resource\n");
Chaithrika U Sefd13be2009-06-08 06:49:41 -0400548 ret = -ENXIO;
Chaithrika U S5204d492009-06-05 06:28:23 -0400549 goto err_free_mem;
550 }
551 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK]->channel = res->start;
552
553 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
554 if (!res) {
555 dev_err(&pdev->dev, "no DMA resource\n");
Chaithrika U Sefd13be2009-06-08 06:49:41 -0400556 ret = -ENXIO;
Chaithrika U S5204d492009-06-05 06:28:23 -0400557 goto err_free_mem;
558 }
559 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE]->channel = res->start;
560
561 davinci_i2s_dai.private_data = dev;
562 ret = snd_soc_register_dai(&davinci_i2s_dai);
563 if (ret != 0)
564 goto err_free_mem;
565
Vladimir Barinov310355c2008-02-18 11:40:22 +0100566 return 0;
567
568err_free_mem:
569 kfree(dev);
570err_release_region:
571 release_mem_region(mem->start, (mem->end - mem->start) + 1);
572
573 return ret;
574}
575
Chaithrika U S5204d492009-06-05 06:28:23 -0400576static int davinci_i2s_remove(struct platform_device *pdev)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100577{
Chaithrika U S5204d492009-06-05 06:28:23 -0400578 struct davinci_mcbsp_dev *dev = davinci_i2s_dai.private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100579 struct resource *mem;
580
Chaithrika U S5204d492009-06-05 06:28:23 -0400581 snd_soc_unregister_dai(&davinci_i2s_dai);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100582 clk_disable(dev->clk);
583 clk_put(dev->clk);
584 dev->clk = NULL;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100585 kfree(dev);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100586 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
587 release_mem_region(mem->start, (mem->end - mem->start) + 1);
Chaithrika U S5204d492009-06-05 06:28:23 -0400588
589 return 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100590}
591
Chaithrika U S5204d492009-06-05 06:28:23 -0400592static struct platform_driver davinci_mcbsp_driver = {
593 .probe = davinci_i2s_probe,
594 .remove = davinci_i2s_remove,
595 .driver = {
596 .name = "davinci-asp",
597 .owner = THIS_MODULE,
598 },
Eric Miao6335d052009-03-03 09:41:00 +0800599};
600
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100601static int __init davinci_i2s_init(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000602{
Chaithrika U S5204d492009-06-05 06:28:23 -0400603 return platform_driver_register(&davinci_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000604}
605module_init(davinci_i2s_init);
606
607static void __exit davinci_i2s_exit(void)
608{
Chaithrika U S5204d492009-06-05 06:28:23 -0400609 platform_driver_unregister(&davinci_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000610}
611module_exit(davinci_i2s_exit);
612
Vladimir Barinov310355c2008-02-18 11:40:22 +0100613MODULE_AUTHOR("Vladimir Barinov");
614MODULE_DESCRIPTION("TI DAVINCI I2S (McBSP) SoC Interface");
615MODULE_LICENSE("GPL");