blob: 2ab809359c08a487a8bb4c75a1a631b228de697d [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;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400400 dma_params->fifo_level = 0;
401
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700402 rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(1);
403 xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100404
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700405 rcr |= DAVINCI_MCBSP_RCR_RWDLEN1(mcbsp_word_length) |
406 DAVINCI_MCBSP_RCR_RWDLEN2(mcbsp_word_length);
407 xcr |= DAVINCI_MCBSP_XCR_XWDLEN1(mcbsp_word_length) |
408 DAVINCI_MCBSP_XCR_XWDLEN2(mcbsp_word_length);
409
410 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Troy Kisky35cf6352009-07-04 19:29:51 -0700411 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_XCR_REG, xcr);
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700412 else
413 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_RCR_REG, rcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100414 return 0;
415}
416
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700417static int davinci_i2s_prepare(struct snd_pcm_substream *substream,
418 struct snd_soc_dai *dai)
419{
Troy Kisky9bb74152009-08-06 16:55:31 -0700420 struct davinci_mcbsp_dev *dev = dai->private_data;
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700421 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
422 davinci_mcbsp_stop(dev, playback);
423 if ((dev->pcr & DAVINCI_MCBSP_PCR_FSXM) == 0) {
424 /* codec is master */
425 davinci_mcbsp_start(dev, substream);
426 }
427 return 0;
428}
429
Mark Browndee89c42008-11-18 22:11:38 +0000430static int davinci_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
431 struct snd_soc_dai *dai)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100432{
Troy Kisky9bb74152009-08-06 16:55:31 -0700433 struct davinci_mcbsp_dev *dev = dai->private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100434 int ret = 0;
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700435 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700436 if ((dev->pcr & DAVINCI_MCBSP_PCR_FSXM) == 0)
437 return 0; /* return if codec is master */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100438
439 switch (cmd) {
440 case SNDRV_PCM_TRIGGER_START:
441 case SNDRV_PCM_TRIGGER_RESUME:
442 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700443 davinci_mcbsp_start(dev, substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100444 break;
445 case SNDRV_PCM_TRIGGER_STOP:
446 case SNDRV_PCM_TRIGGER_SUSPEND:
447 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700448 davinci_mcbsp_stop(dev, playback);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100449 break;
450 default:
451 ret = -EINVAL;
452 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100453 return ret;
454}
455
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700456static void davinci_i2s_shutdown(struct snd_pcm_substream *substream,
457 struct snd_soc_dai *dai)
458{
Troy Kisky9bb74152009-08-06 16:55:31 -0700459 struct davinci_mcbsp_dev *dev = dai->private_data;
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700460 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
461 davinci_mcbsp_stop(dev, playback);
462}
463
Chaithrika U S5204d492009-06-05 06:28:23 -0400464#define DAVINCI_I2S_RATES SNDRV_PCM_RATE_8000_96000
465
466static struct snd_soc_dai_ops davinci_i2s_dai_ops = {
Mark Brown3f405b42009-07-07 19:18:46 +0100467 .shutdown = davinci_i2s_shutdown,
468 .prepare = davinci_i2s_prepare,
Chaithrika U S5204d492009-06-05 06:28:23 -0400469 .trigger = davinci_i2s_trigger,
470 .hw_params = davinci_i2s_hw_params,
471 .set_fmt = davinci_i2s_set_dai_fmt,
472
473};
474
475struct snd_soc_dai davinci_i2s_dai = {
476 .name = "davinci-i2s",
477 .id = 0,
478 .playback = {
479 .channels_min = 2,
480 .channels_max = 2,
481 .rates = DAVINCI_I2S_RATES,
482 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
483 .capture = {
484 .channels_min = 2,
485 .channels_max = 2,
486 .rates = DAVINCI_I2S_RATES,
487 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
488 .ops = &davinci_i2s_dai_ops,
489
490};
491EXPORT_SYMBOL_GPL(davinci_i2s_dai);
492
493static int davinci_i2s_probe(struct platform_device *pdev)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100494{
Chaithrika U S5204d492009-06-05 06:28:23 -0400495 struct snd_platform_data *pdata = pdev->dev.platform_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100496 struct davinci_mcbsp_dev *dev;
Chaithrika U S5204d492009-06-05 06:28:23 -0400497 struct resource *mem, *ioarea, *res;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100498 int ret;
499
500 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
501 if (!mem) {
502 dev_err(&pdev->dev, "no mem resource?\n");
503 return -ENODEV;
504 }
505
506 ioarea = request_mem_region(mem->start, (mem->end - mem->start) + 1,
507 pdev->name);
508 if (!ioarea) {
509 dev_err(&pdev->dev, "McBSP region already claimed\n");
510 return -EBUSY;
511 }
512
513 dev = kzalloc(sizeof(struct davinci_mcbsp_dev), GFP_KERNEL);
514 if (!dev) {
515 ret = -ENOMEM;
516 goto err_release_region;
517 }
518
Kevin Hilman3e46a442009-07-15 10:42:09 -0700519 dev->clk = clk_get(&pdev->dev, NULL);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100520 if (IS_ERR(dev->clk)) {
521 ret = -ENODEV;
522 goto err_free_mem;
523 }
524 clk_enable(dev->clk);
525
526 dev->base = (void __iomem *)IO_ADDRESS(mem->start);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100527
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700528 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].dma_addr =
Vladimir Barinov310355c2008-02-18 11:40:22 +0100529 (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DXR_REG);
530
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700531 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].dma_addr =
Vladimir Barinov310355c2008-02-18 11:40:22 +0100532 (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DRR_REG);
533
Chaithrika U S5204d492009-06-05 06:28:23 -0400534 /* first TX, then RX */
535 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
536 if (!res) {
537 dev_err(&pdev->dev, "no DMA resource\n");
Chaithrika U Sefd13be2009-06-08 06:49:41 -0400538 ret = -ENXIO;
Chaithrika U S5204d492009-06-05 06:28:23 -0400539 goto err_free_mem;
540 }
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700541 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].channel = res->start;
Chaithrika U S5204d492009-06-05 06:28:23 -0400542
543 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
544 if (!res) {
545 dev_err(&pdev->dev, "no DMA resource\n");
Chaithrika U Sefd13be2009-06-08 06:49:41 -0400546 ret = -ENXIO;
Chaithrika U S5204d492009-06-05 06:28:23 -0400547 goto err_free_mem;
548 }
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700549 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].channel = res->start;
Chaithrika U S5204d492009-06-05 06:28:23 -0400550
551 davinci_i2s_dai.private_data = dev;
552 ret = snd_soc_register_dai(&davinci_i2s_dai);
553 if (ret != 0)
554 goto err_free_mem;
555
Vladimir Barinov310355c2008-02-18 11:40:22 +0100556 return 0;
557
558err_free_mem:
559 kfree(dev);
560err_release_region:
561 release_mem_region(mem->start, (mem->end - mem->start) + 1);
562
563 return ret;
564}
565
Chaithrika U S5204d492009-06-05 06:28:23 -0400566static int davinci_i2s_remove(struct platform_device *pdev)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100567{
Chaithrika U S5204d492009-06-05 06:28:23 -0400568 struct davinci_mcbsp_dev *dev = davinci_i2s_dai.private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100569 struct resource *mem;
570
Chaithrika U S5204d492009-06-05 06:28:23 -0400571 snd_soc_unregister_dai(&davinci_i2s_dai);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100572 clk_disable(dev->clk);
573 clk_put(dev->clk);
574 dev->clk = NULL;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100575 kfree(dev);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100576 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
577 release_mem_region(mem->start, (mem->end - mem->start) + 1);
Chaithrika U S5204d492009-06-05 06:28:23 -0400578
579 return 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100580}
581
Chaithrika U S5204d492009-06-05 06:28:23 -0400582static struct platform_driver davinci_mcbsp_driver = {
583 .probe = davinci_i2s_probe,
584 .remove = davinci_i2s_remove,
585 .driver = {
586 .name = "davinci-asp",
587 .owner = THIS_MODULE,
588 },
Eric Miao6335d052009-03-03 09:41:00 +0800589};
590
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100591static int __init davinci_i2s_init(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000592{
Chaithrika U S5204d492009-06-05 06:28:23 -0400593 return platform_driver_register(&davinci_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000594}
595module_init(davinci_i2s_init);
596
597static void __exit davinci_i2s_exit(void)
598{
Chaithrika U S5204d492009-06-05 06:28:23 -0400599 platform_driver_unregister(&davinci_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000600}
601module_exit(davinci_i2s_exit);
602
Vladimir Barinov310355c2008-02-18 11:40:22 +0100603MODULE_AUTHOR("Vladimir Barinov");
604MODULE_DESCRIPTION("TI DAVINCI I2S (McBSP) SoC Interface");
605MODULE_LICENSE("GPL");