blob: 4ae707048021886c45533ec6c5f6ca5cadf3999c [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
Vladimir Barinov310355c2008-02-18 11:40:22 +0100100struct davinci_mcbsp_dev {
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700101 /*
102 * dma_params must be first because rtd->dai->cpu_dai->private_data
103 * is cast to a pointer of an array of struct davinci_pcm_dma_params in
104 * davinci_pcm_open.
105 */
106 struct davinci_pcm_dma_params dma_params[2];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100107 void __iomem *base;
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700108#define MOD_DSP_A 0
109#define MOD_DSP_B 1
110 int mode;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700111 u32 pcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100112 struct clk *clk;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100113};
114
115static inline void davinci_mcbsp_write_reg(struct davinci_mcbsp_dev *dev,
116 int reg, u32 val)
117{
118 __raw_writel(val, dev->base + reg);
119}
120
121static inline u32 davinci_mcbsp_read_reg(struct davinci_mcbsp_dev *dev, int reg)
122{
123 return __raw_readl(dev->base + reg);
124}
125
Troy Kiskyc392bec2009-07-04 19:29:52 -0700126static void toggle_clock(struct davinci_mcbsp_dev *dev, int playback)
127{
128 u32 m = playback ? DAVINCI_MCBSP_PCR_CLKXP : DAVINCI_MCBSP_PCR_CLKRP;
129 /* The clock needs to toggle to complete reset.
130 * So, fake it by toggling the clk polarity.
131 */
132 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr ^ m);
133 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr);
134}
135
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700136static void davinci_mcbsp_start(struct davinci_mcbsp_dev *dev,
137 struct snd_pcm_substream *substream)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100138{
139 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530140 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000141 struct snd_soc_platform *platform = socdev->card->platform;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700142 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Troy Kisky35cf6352009-07-04 19:29:51 -0700143 u32 spcr;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700144 u32 mask = playback ? DAVINCI_MCBSP_SPCR_XRST : DAVINCI_MCBSP_SPCR_RRST;
Troy Kisky35cf6352009-07-04 19:29:51 -0700145 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700146 if (spcr & mask) {
147 /* start off disabled */
148 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG,
149 spcr & ~mask);
150 toggle_clock(dev, playback);
151 }
Troy Kisky1bef4492009-07-04 19:29:55 -0700152 if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM |
153 DAVINCI_MCBSP_PCR_CLKXM | DAVINCI_MCBSP_PCR_CLKRM)) {
154 /* Start the sample generator */
155 spcr |= DAVINCI_MCBSP_SPCR_GRST;
156 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
157 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100158
Troy Kisky1bef4492009-07-04 19:29:55 -0700159 if (playback) {
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530160 /* Stop the DMA to avoid data loss */
161 /* while the transmitter is out of reset to handle XSYNCERR */
162 if (platform->pcm_ops->trigger) {
Troy Kiskyeba575c2009-07-04 19:29:54 -0700163 int ret = platform->pcm_ops->trigger(substream,
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530164 SNDRV_PCM_TRIGGER_STOP);
165 if (ret < 0)
166 printk(KERN_DEBUG "Playback DMA stop failed\n");
167 }
168
169 /* Enable the transmitter */
Troy Kisky35cf6352009-07-04 19:29:51 -0700170 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
171 spcr |= DAVINCI_MCBSP_SPCR_XRST;
172 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530173
174 /* wait for any unexpected frame sync error to occur */
175 udelay(100);
176
177 /* Disable the transmitter to clear any outstanding XSYNCERR */
Troy Kisky35cf6352009-07-04 19:29:51 -0700178 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
179 spcr &= ~DAVINCI_MCBSP_SPCR_XRST;
180 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700181 toggle_clock(dev, playback);
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530182
183 /* Restart the DMA */
184 if (platform->pcm_ops->trigger) {
Troy Kiskyeba575c2009-07-04 19:29:54 -0700185 int ret = platform->pcm_ops->trigger(substream,
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530186 SNDRV_PCM_TRIGGER_START);
187 if (ret < 0)
188 printk(KERN_DEBUG "Playback DMA start failed\n");
189 }
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530190 }
191
Troy Kisky1bef4492009-07-04 19:29:55 -0700192 /* Enable transmitter or receiver */
Troy Kisky35cf6352009-07-04 19:29:51 -0700193 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Troy Kisky1bef4492009-07-04 19:29:55 -0700194 spcr |= mask;
195
196 if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM)) {
197 /* Start frame sync */
198 spcr |= DAVINCI_MCBSP_SPCR_FRST;
199 }
Troy Kisky35cf6352009-07-04 19:29:51 -0700200 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100201}
202
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700203static void davinci_mcbsp_stop(struct davinci_mcbsp_dev *dev, int playback)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100204{
Troy Kisky35cf6352009-07-04 19:29:51 -0700205 u32 spcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100206
207 /* Reset transmitter/receiver and sample rate/frame sync generators */
Troy Kisky35cf6352009-07-04 19:29:51 -0700208 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
209 spcr &= ~(DAVINCI_MCBSP_SPCR_GRST | DAVINCI_MCBSP_SPCR_FRST);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700210 spcr &= playback ? ~DAVINCI_MCBSP_SPCR_XRST : ~DAVINCI_MCBSP_SPCR_RRST;
Troy Kisky35cf6352009-07-04 19:29:51 -0700211 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700212 toggle_clock(dev, playback);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100213}
214
Troy Kisky21903c12008-12-18 12:36:43 -0700215#define DEFAULT_BITPERSAMPLE 16
216
Liam Girdwood9cb132d2008-07-07 16:07:42 +0100217static int davinci_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
Vladimir Barinov310355c2008-02-18 11:40:22 +0100218 unsigned int fmt)
219{
220 struct davinci_mcbsp_dev *dev = cpu_dai->private_data;
Troy Kisky21903c12008-12-18 12:36:43 -0700221 unsigned int pcr;
222 unsigned int srgr;
Troy Kisky21903c12008-12-18 12:36:43 -0700223 srgr = DAVINCI_MCBSP_SRGR_FSGM |
224 DAVINCI_MCBSP_SRGR_FPER(DEFAULT_BITPERSAMPLE * 2 - 1) |
225 DAVINCI_MCBSP_SRGR_FWID(DEFAULT_BITPERSAMPLE - 1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100226
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700227 /* set master/slave audio interface */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100228 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
229 case SND_SOC_DAIFMT_CBS_CFS:
Troy Kisky21903c12008-12-18 12:36:43 -0700230 /* cpu is master */
231 pcr = DAVINCI_MCBSP_PCR_FSXM |
232 DAVINCI_MCBSP_PCR_FSRM |
233 DAVINCI_MCBSP_PCR_CLKXM |
234 DAVINCI_MCBSP_PCR_CLKRM;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100235 break;
Hugo Villeneuveb402dff2008-11-08 13:26:09 -0500236 case SND_SOC_DAIFMT_CBM_CFS:
237 /* McBSP CLKR pin is the input for the Sample Rate Generator.
238 * McBSP FSR and FSX are driven by the Sample Rate Generator. */
Troy Kisky21903c12008-12-18 12:36:43 -0700239 pcr = DAVINCI_MCBSP_PCR_SCLKME |
240 DAVINCI_MCBSP_PCR_FSXM |
241 DAVINCI_MCBSP_PCR_FSRM;
Hugo Villeneuveb402dff2008-11-08 13:26:09 -0500242 break;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100243 case SND_SOC_DAIFMT_CBM_CFM:
Troy Kisky21903c12008-12-18 12:36:43 -0700244 /* codec is master */
245 pcr = 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100246 break;
247 default:
Troy Kisky21903c12008-12-18 12:36:43 -0700248 printk(KERN_ERR "%s:bad master\n", __func__);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100249 return -EINVAL;
250 }
251
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700252 /* interface format */
Troy Kisky69ab8202008-12-18 12:36:44 -0700253 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Troy Kisky69ab8202008-12-18 12:36:44 -0700254 case SND_SOC_DAIFMT_I2S:
Troy Kisky07d8d9d2008-12-19 13:05:24 -0700255 /* Davinci doesn't support TRUE I2S, but some codecs will have
256 * the left and right channels contiguous. This allows
257 * dsp_a mode to be used with an inverted normal frame clk.
258 * If your codec is master and does not have contiguous
259 * channels, then you will have sound on only one channel.
260 * Try using a different mode, or codec as slave.
261 *
262 * The TLV320AIC33 is an example of a codec where this works.
263 * It has a variable bit clock frequency allowing it to have
264 * valid data on every bit clock.
265 *
266 * The TLV320AIC23 is an example of a codec where this does not
267 * work. It has a fixed bit clock frequency with progressively
268 * more empty bit clock slots between channels as the sample
269 * rate is lowered.
270 */
271 fmt ^= SND_SOC_DAIFMT_NB_IF;
272 case SND_SOC_DAIFMT_DSP_A:
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700273 dev->mode = MOD_DSP_A;
274 break;
275 case SND_SOC_DAIFMT_DSP_B:
276 dev->mode = MOD_DSP_B;
Troy Kisky69ab8202008-12-18 12:36:44 -0700277 break;
278 default:
279 printk(KERN_ERR "%s:bad format\n", __func__);
280 return -EINVAL;
281 }
282
Vladimir Barinov310355c2008-02-18 11:40:22 +0100283 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
Troy Kisky9e031622008-12-19 13:05:23 -0700284 case SND_SOC_DAIFMT_NB_NF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700285 /* CLKRP Receive clock polarity,
286 * 1 - sampled on rising edge of CLKR
287 * valid on rising edge
288 * CLKXP Transmit clock polarity,
289 * 1 - clocked on falling edge of CLKX
290 * valid on rising edge
291 * FSRP Receive frame sync pol, 0 - active high
292 * FSXP Transmit frame sync pol, 0 - active high
293 */
Troy Kisky21903c12008-12-18 12:36:43 -0700294 pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100295 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700296 case SND_SOC_DAIFMT_IB_IF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700297 /* CLKRP Receive clock polarity,
298 * 0 - sampled on falling edge of CLKR
299 * valid on falling edge
300 * CLKXP Transmit clock polarity,
301 * 0 - clocked on rising edge of CLKX
302 * valid on falling edge
303 * FSRP Receive frame sync pol, 1 - active low
304 * FSXP Transmit frame sync pol, 1 - active low
305 */
Troy Kisky21903c12008-12-18 12:36:43 -0700306 pcr |= (DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100307 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700308 case SND_SOC_DAIFMT_NB_IF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700309 /* CLKRP Receive clock polarity,
310 * 1 - sampled on rising edge of CLKR
311 * valid on rising edge
312 * CLKXP Transmit clock polarity,
313 * 1 - clocked on falling edge of CLKX
314 * valid on rising edge
315 * FSRP Receive frame sync pol, 1 - active low
316 * FSXP Transmit frame sync pol, 1 - active low
317 */
Troy Kisky21903c12008-12-18 12:36:43 -0700318 pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP |
319 DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100320 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700321 case SND_SOC_DAIFMT_IB_NF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700322 /* CLKRP Receive clock polarity,
323 * 0 - sampled on falling edge of CLKR
324 * valid on falling edge
325 * CLKXP Transmit clock polarity,
326 * 0 - clocked on rising edge of CLKX
327 * valid on falling edge
328 * FSRP Receive frame sync pol, 0 - active high
329 * FSXP Transmit frame sync pol, 0 - active high
330 */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100331 break;
332 default:
333 return -EINVAL;
334 }
Troy Kisky21903c12008-12-18 12:36:43 -0700335 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700336 dev->pcr = pcr;
Troy Kisky21903c12008-12-18 12:36:43 -0700337 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, pcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100338 return 0;
339}
340
341static int davinci_i2s_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000342 struct snd_pcm_hw_params *params,
343 struct snd_soc_dai *dai)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100344{
Troy Kisky9bb74152009-08-06 16:55:31 -0700345 struct davinci_mcbsp_dev *dev = dai->private_data;
Troy Kisky81ac55a2009-09-11 14:29:02 -0700346 struct davinci_pcm_dma_params *dma_params =
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700347 &dev->dma_params[substream->stream];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100348 struct snd_interval *i = NULL;
349 int mcbsp_word_length;
Troy Kisky35cf6352009-07-04 19:29:51 -0700350 unsigned int rcr, xcr, srgr;
351 u32 spcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100352
353 /* general line settings */
Troy Kisky35cf6352009-07-04 19:29:51 -0700354 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530355 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Troy Kisky35cf6352009-07-04 19:29:51 -0700356 spcr |= DAVINCI_MCBSP_SPCR_RINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
357 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530358 } else {
Troy Kisky35cf6352009-07-04 19:29:51 -0700359 spcr |= DAVINCI_MCBSP_SPCR_XINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
360 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530361 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100362
363 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Troy Kisky35cf6352009-07-04 19:29:51 -0700364 srgr = DAVINCI_MCBSP_SRGR_FSGM;
365 srgr |= DAVINCI_MCBSP_SRGR_FWID(snd_interval_value(i) - 1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100366
367 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_FRAME_BITS);
Troy Kisky35cf6352009-07-04 19:29:51 -0700368 srgr |= DAVINCI_MCBSP_SRGR_FPER(snd_interval_value(i) - 1);
369 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100370
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700371 rcr = DAVINCI_MCBSP_RCR_RFIG;
372 xcr = DAVINCI_MCBSP_XCR_XFIG;
373 if (dev->mode == MOD_DSP_B) {
374 rcr |= DAVINCI_MCBSP_RCR_RDATDLY(0);
375 xcr |= DAVINCI_MCBSP_XCR_XDATDLY(0);
376 } else {
377 rcr |= DAVINCI_MCBSP_RCR_RDATDLY(1);
378 xcr |= DAVINCI_MCBSP_XCR_XDATDLY(1);
379 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100380 /* Determine xfer data type */
381 switch (params_format(params)) {
382 case SNDRV_PCM_FORMAT_S8:
383 dma_params->data_type = 1;
384 mcbsp_word_length = DAVINCI_MCBSP_WORD_8;
385 break;
386 case SNDRV_PCM_FORMAT_S16_LE:
387 dma_params->data_type = 2;
388 mcbsp_word_length = DAVINCI_MCBSP_WORD_16;
389 break;
390 case SNDRV_PCM_FORMAT_S32_LE:
391 dma_params->data_type = 4;
392 mcbsp_word_length = DAVINCI_MCBSP_WORD_32;
393 break;
394 default:
Jean Delvare9b6e12e2008-08-26 15:47:55 +0200395 printk(KERN_WARNING "davinci-i2s: unsupported PCM format\n");
Vladimir Barinov310355c2008-02-18 11:40:22 +0100396 return -EINVAL;
397 }
398
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400399 dma_params->acnt = dma_params->data_type;
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700400 rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(1);
401 xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100402
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700403 rcr |= DAVINCI_MCBSP_RCR_RWDLEN1(mcbsp_word_length) |
404 DAVINCI_MCBSP_RCR_RWDLEN2(mcbsp_word_length);
405 xcr |= DAVINCI_MCBSP_XCR_XWDLEN1(mcbsp_word_length) |
406 DAVINCI_MCBSP_XCR_XWDLEN2(mcbsp_word_length);
407
408 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Troy Kisky35cf6352009-07-04 19:29:51 -0700409 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_XCR_REG, xcr);
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700410 else
411 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_RCR_REG, rcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100412 return 0;
413}
414
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700415static int davinci_i2s_prepare(struct snd_pcm_substream *substream,
416 struct snd_soc_dai *dai)
417{
Troy Kisky9bb74152009-08-06 16:55:31 -0700418 struct davinci_mcbsp_dev *dev = dai->private_data;
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700419 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
420 davinci_mcbsp_stop(dev, playback);
421 if ((dev->pcr & DAVINCI_MCBSP_PCR_FSXM) == 0) {
422 /* codec is master */
423 davinci_mcbsp_start(dev, substream);
424 }
425 return 0;
426}
427
Mark Browndee89c42008-11-18 22:11:38 +0000428static int davinci_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
429 struct snd_soc_dai *dai)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100430{
Troy Kisky9bb74152009-08-06 16:55:31 -0700431 struct davinci_mcbsp_dev *dev = dai->private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100432 int ret = 0;
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700433 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700434 if ((dev->pcr & DAVINCI_MCBSP_PCR_FSXM) == 0)
435 return 0; /* return if codec is master */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100436
437 switch (cmd) {
438 case SNDRV_PCM_TRIGGER_START:
439 case SNDRV_PCM_TRIGGER_RESUME:
440 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700441 davinci_mcbsp_start(dev, substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100442 break;
443 case SNDRV_PCM_TRIGGER_STOP:
444 case SNDRV_PCM_TRIGGER_SUSPEND:
445 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700446 davinci_mcbsp_stop(dev, playback);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100447 break;
448 default:
449 ret = -EINVAL;
450 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100451 return ret;
452}
453
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700454static void davinci_i2s_shutdown(struct snd_pcm_substream *substream,
455 struct snd_soc_dai *dai)
456{
Troy Kisky9bb74152009-08-06 16:55:31 -0700457 struct davinci_mcbsp_dev *dev = dai->private_data;
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700458 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
459 davinci_mcbsp_stop(dev, playback);
460}
461
Chaithrika U S5204d492009-06-05 06:28:23 -0400462#define DAVINCI_I2S_RATES SNDRV_PCM_RATE_8000_96000
463
464static struct snd_soc_dai_ops davinci_i2s_dai_ops = {
Mark Brown3f405b42009-07-07 19:18:46 +0100465 .shutdown = davinci_i2s_shutdown,
466 .prepare = davinci_i2s_prepare,
Chaithrika U S5204d492009-06-05 06:28:23 -0400467 .trigger = davinci_i2s_trigger,
468 .hw_params = davinci_i2s_hw_params,
469 .set_fmt = davinci_i2s_set_dai_fmt,
470
471};
472
473struct snd_soc_dai davinci_i2s_dai = {
474 .name = "davinci-i2s",
475 .id = 0,
476 .playback = {
477 .channels_min = 2,
478 .channels_max = 2,
479 .rates = DAVINCI_I2S_RATES,
480 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
481 .capture = {
482 .channels_min = 2,
483 .channels_max = 2,
484 .rates = DAVINCI_I2S_RATES,
485 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
486 .ops = &davinci_i2s_dai_ops,
487
488};
489EXPORT_SYMBOL_GPL(davinci_i2s_dai);
490
491static int davinci_i2s_probe(struct platform_device *pdev)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100492{
Chaithrika U S5204d492009-06-05 06:28:23 -0400493 struct snd_platform_data *pdata = pdev->dev.platform_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100494 struct davinci_mcbsp_dev *dev;
Chaithrika U S5204d492009-06-05 06:28:23 -0400495 struct resource *mem, *ioarea, *res;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100496 int ret;
497
498 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
499 if (!mem) {
500 dev_err(&pdev->dev, "no mem resource?\n");
501 return -ENODEV;
502 }
503
504 ioarea = request_mem_region(mem->start, (mem->end - mem->start) + 1,
505 pdev->name);
506 if (!ioarea) {
507 dev_err(&pdev->dev, "McBSP region already claimed\n");
508 return -EBUSY;
509 }
510
511 dev = kzalloc(sizeof(struct davinci_mcbsp_dev), GFP_KERNEL);
512 if (!dev) {
513 ret = -ENOMEM;
514 goto err_release_region;
515 }
516
Kevin Hilman3e46a442009-07-15 10:42:09 -0700517 dev->clk = clk_get(&pdev->dev, NULL);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100518 if (IS_ERR(dev->clk)) {
519 ret = -ENODEV;
520 goto err_free_mem;
521 }
522 clk_enable(dev->clk);
523
524 dev->base = (void __iomem *)IO_ADDRESS(mem->start);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100525
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700526 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].dma_addr =
Vladimir Barinov310355c2008-02-18 11:40:22 +0100527 (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DXR_REG);
528
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700529 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].dma_addr =
Vladimir Barinov310355c2008-02-18 11:40:22 +0100530 (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DRR_REG);
531
Chaithrika U S5204d492009-06-05 06:28:23 -0400532 /* first TX, then RX */
533 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
534 if (!res) {
535 dev_err(&pdev->dev, "no DMA resource\n");
Chaithrika U Sefd13be2009-06-08 06:49:41 -0400536 ret = -ENXIO;
Chaithrika U S5204d492009-06-05 06:28:23 -0400537 goto err_free_mem;
538 }
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700539 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].channel = res->start;
Chaithrika U S5204d492009-06-05 06:28:23 -0400540
541 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
542 if (!res) {
543 dev_err(&pdev->dev, "no DMA resource\n");
Chaithrika U Sefd13be2009-06-08 06:49:41 -0400544 ret = -ENXIO;
Chaithrika U S5204d492009-06-05 06:28:23 -0400545 goto err_free_mem;
546 }
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700547 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].channel = res->start;
Chaithrika U S5204d492009-06-05 06:28:23 -0400548
549 davinci_i2s_dai.private_data = dev;
550 ret = snd_soc_register_dai(&davinci_i2s_dai);
551 if (ret != 0)
552 goto err_free_mem;
553
Vladimir Barinov310355c2008-02-18 11:40:22 +0100554 return 0;
555
556err_free_mem:
557 kfree(dev);
558err_release_region:
559 release_mem_region(mem->start, (mem->end - mem->start) + 1);
560
561 return ret;
562}
563
Chaithrika U S5204d492009-06-05 06:28:23 -0400564static int davinci_i2s_remove(struct platform_device *pdev)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100565{
Chaithrika U S5204d492009-06-05 06:28:23 -0400566 struct davinci_mcbsp_dev *dev = davinci_i2s_dai.private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100567 struct resource *mem;
568
Chaithrika U S5204d492009-06-05 06:28:23 -0400569 snd_soc_unregister_dai(&davinci_i2s_dai);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100570 clk_disable(dev->clk);
571 clk_put(dev->clk);
572 dev->clk = NULL;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100573 kfree(dev);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100574 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
575 release_mem_region(mem->start, (mem->end - mem->start) + 1);
Chaithrika U S5204d492009-06-05 06:28:23 -0400576
577 return 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100578}
579
Chaithrika U S5204d492009-06-05 06:28:23 -0400580static struct platform_driver davinci_mcbsp_driver = {
581 .probe = davinci_i2s_probe,
582 .remove = davinci_i2s_remove,
583 .driver = {
584 .name = "davinci-asp",
585 .owner = THIS_MODULE,
586 },
Eric Miao6335d052009-03-03 09:41:00 +0800587};
588
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100589static int __init davinci_i2s_init(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000590{
Chaithrika U S5204d492009-06-05 06:28:23 -0400591 return platform_driver_register(&davinci_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000592}
593module_init(davinci_i2s_init);
594
595static void __exit davinci_i2s_exit(void)
596{
Chaithrika U S5204d492009-06-05 06:28:23 -0400597 platform_driver_unregister(&davinci_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000598}
599module_exit(davinci_i2s_exit);
600
Vladimir Barinov310355c2008-02-18 11:40:22 +0100601MODULE_AUTHOR("Vladimir Barinov");
602MODULE_DESCRIPTION("TI DAVINCI I2S (McBSP) SoC Interface");
603MODULE_LICENSE("GPL");