blob: d336786683b4e8a86658fb975d919321968ed40f [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 struct davinci_pcm_dma_params dma_params[2];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100102 void __iomem *base;
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700103#define MOD_DSP_A 0
104#define MOD_DSP_B 1
105 int mode;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700106 u32 pcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100107 struct clk *clk;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100108};
109
110static inline void davinci_mcbsp_write_reg(struct davinci_mcbsp_dev *dev,
111 int reg, u32 val)
112{
113 __raw_writel(val, dev->base + reg);
114}
115
116static inline u32 davinci_mcbsp_read_reg(struct davinci_mcbsp_dev *dev, int reg)
117{
118 return __raw_readl(dev->base + reg);
119}
120
Troy Kiskyc392bec2009-07-04 19:29:52 -0700121static void toggle_clock(struct davinci_mcbsp_dev *dev, int playback)
122{
123 u32 m = playback ? DAVINCI_MCBSP_PCR_CLKXP : DAVINCI_MCBSP_PCR_CLKRP;
124 /* The clock needs to toggle to complete reset.
125 * So, fake it by toggling the clk polarity.
126 */
127 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr ^ m);
128 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr);
129}
130
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700131static void davinci_mcbsp_start(struct davinci_mcbsp_dev *dev,
132 struct snd_pcm_substream *substream)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100133{
134 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530135 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000136 struct snd_soc_platform *platform = socdev->card->platform;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700137 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Troy Kisky35cf6352009-07-04 19:29:51 -0700138 u32 spcr;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700139 u32 mask = playback ? DAVINCI_MCBSP_SPCR_XRST : DAVINCI_MCBSP_SPCR_RRST;
Troy Kisky35cf6352009-07-04 19:29:51 -0700140 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700141 if (spcr & mask) {
142 /* start off disabled */
143 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG,
144 spcr & ~mask);
145 toggle_clock(dev, playback);
146 }
Troy Kisky1bef4492009-07-04 19:29:55 -0700147 if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM |
148 DAVINCI_MCBSP_PCR_CLKXM | DAVINCI_MCBSP_PCR_CLKRM)) {
149 /* Start the sample generator */
150 spcr |= DAVINCI_MCBSP_SPCR_GRST;
151 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
152 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100153
Troy Kisky1bef4492009-07-04 19:29:55 -0700154 if (playback) {
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530155 /* Stop the DMA to avoid data loss */
156 /* while the transmitter is out of reset to handle XSYNCERR */
157 if (platform->pcm_ops->trigger) {
Troy Kiskyeba575c2009-07-04 19:29:54 -0700158 int ret = platform->pcm_ops->trigger(substream,
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530159 SNDRV_PCM_TRIGGER_STOP);
160 if (ret < 0)
161 printk(KERN_DEBUG "Playback DMA stop failed\n");
162 }
163
164 /* Enable the transmitter */
Troy Kisky35cf6352009-07-04 19:29:51 -0700165 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
166 spcr |= DAVINCI_MCBSP_SPCR_XRST;
167 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530168
169 /* wait for any unexpected frame sync error to occur */
170 udelay(100);
171
172 /* Disable the transmitter to clear any outstanding XSYNCERR */
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);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700176 toggle_clock(dev, playback);
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530177
178 /* Restart the DMA */
179 if (platform->pcm_ops->trigger) {
Troy Kiskyeba575c2009-07-04 19:29:54 -0700180 int ret = platform->pcm_ops->trigger(substream,
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530181 SNDRV_PCM_TRIGGER_START);
182 if (ret < 0)
183 printk(KERN_DEBUG "Playback DMA start failed\n");
184 }
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530185 }
186
Troy Kisky1bef4492009-07-04 19:29:55 -0700187 /* Enable transmitter or receiver */
Troy Kisky35cf6352009-07-04 19:29:51 -0700188 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Troy Kisky1bef4492009-07-04 19:29:55 -0700189 spcr |= mask;
190
191 if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM)) {
192 /* Start frame sync */
193 spcr |= DAVINCI_MCBSP_SPCR_FRST;
194 }
Troy Kisky35cf6352009-07-04 19:29:51 -0700195 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100196}
197
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700198static void davinci_mcbsp_stop(struct davinci_mcbsp_dev *dev, int playback)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100199{
Troy Kisky35cf6352009-07-04 19:29:51 -0700200 u32 spcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100201
202 /* Reset transmitter/receiver and sample rate/frame sync generators */
Troy Kisky35cf6352009-07-04 19:29:51 -0700203 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
204 spcr &= ~(DAVINCI_MCBSP_SPCR_GRST | DAVINCI_MCBSP_SPCR_FRST);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700205 spcr &= playback ? ~DAVINCI_MCBSP_SPCR_XRST : ~DAVINCI_MCBSP_SPCR_RRST;
Troy Kisky35cf6352009-07-04 19:29:51 -0700206 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700207 toggle_clock(dev, playback);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100208}
209
Troy Kisky21903c12008-12-18 12:36:43 -0700210#define DEFAULT_BITPERSAMPLE 16
211
Liam Girdwood9cb132d2008-07-07 16:07:42 +0100212static int davinci_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
Vladimir Barinov310355c2008-02-18 11:40:22 +0100213 unsigned int fmt)
214{
215 struct davinci_mcbsp_dev *dev = cpu_dai->private_data;
Troy Kisky21903c12008-12-18 12:36:43 -0700216 unsigned int pcr;
217 unsigned int srgr;
Troy Kisky21903c12008-12-18 12:36:43 -0700218 srgr = DAVINCI_MCBSP_SRGR_FSGM |
219 DAVINCI_MCBSP_SRGR_FPER(DEFAULT_BITPERSAMPLE * 2 - 1) |
220 DAVINCI_MCBSP_SRGR_FWID(DEFAULT_BITPERSAMPLE - 1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100221
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700222 /* set master/slave audio interface */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100223 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
224 case SND_SOC_DAIFMT_CBS_CFS:
Troy Kisky21903c12008-12-18 12:36:43 -0700225 /* cpu is master */
226 pcr = DAVINCI_MCBSP_PCR_FSXM |
227 DAVINCI_MCBSP_PCR_FSRM |
228 DAVINCI_MCBSP_PCR_CLKXM |
229 DAVINCI_MCBSP_PCR_CLKRM;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100230 break;
Hugo Villeneuveb402dff2008-11-08 13:26:09 -0500231 case SND_SOC_DAIFMT_CBM_CFS:
232 /* McBSP CLKR pin is the input for the Sample Rate Generator.
233 * McBSP FSR and FSX are driven by the Sample Rate Generator. */
Troy Kisky21903c12008-12-18 12:36:43 -0700234 pcr = DAVINCI_MCBSP_PCR_SCLKME |
235 DAVINCI_MCBSP_PCR_FSXM |
236 DAVINCI_MCBSP_PCR_FSRM;
Hugo Villeneuveb402dff2008-11-08 13:26:09 -0500237 break;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100238 case SND_SOC_DAIFMT_CBM_CFM:
Troy Kisky21903c12008-12-18 12:36:43 -0700239 /* codec is master */
240 pcr = 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100241 break;
242 default:
Troy Kisky21903c12008-12-18 12:36:43 -0700243 printk(KERN_ERR "%s:bad master\n", __func__);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100244 return -EINVAL;
245 }
246
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700247 /* interface format */
Troy Kisky69ab8202008-12-18 12:36:44 -0700248 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Troy Kisky69ab8202008-12-18 12:36:44 -0700249 case SND_SOC_DAIFMT_I2S:
Troy Kisky07d8d9d2008-12-19 13:05:24 -0700250 /* Davinci doesn't support TRUE I2S, but some codecs will have
251 * the left and right channels contiguous. This allows
252 * dsp_a mode to be used with an inverted normal frame clk.
253 * If your codec is master and does not have contiguous
254 * channels, then you will have sound on only one channel.
255 * Try using a different mode, or codec as slave.
256 *
257 * The TLV320AIC33 is an example of a codec where this works.
258 * It has a variable bit clock frequency allowing it to have
259 * valid data on every bit clock.
260 *
261 * The TLV320AIC23 is an example of a codec where this does not
262 * work. It has a fixed bit clock frequency with progressively
263 * more empty bit clock slots between channels as the sample
264 * rate is lowered.
265 */
266 fmt ^= SND_SOC_DAIFMT_NB_IF;
267 case SND_SOC_DAIFMT_DSP_A:
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700268 dev->mode = MOD_DSP_A;
269 break;
270 case SND_SOC_DAIFMT_DSP_B:
271 dev->mode = MOD_DSP_B;
Troy Kisky69ab8202008-12-18 12:36:44 -0700272 break;
273 default:
274 printk(KERN_ERR "%s:bad format\n", __func__);
275 return -EINVAL;
276 }
277
Vladimir Barinov310355c2008-02-18 11:40:22 +0100278 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
Troy Kisky9e031622008-12-19 13:05:23 -0700279 case SND_SOC_DAIFMT_NB_NF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700280 /* CLKRP Receive clock polarity,
281 * 1 - sampled on rising edge of CLKR
282 * valid on rising edge
283 * CLKXP Transmit clock polarity,
284 * 1 - clocked on falling edge of CLKX
285 * valid on rising edge
286 * FSRP Receive frame sync pol, 0 - active high
287 * FSXP Transmit frame sync pol, 0 - active high
288 */
Troy Kisky21903c12008-12-18 12:36:43 -0700289 pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100290 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700291 case SND_SOC_DAIFMT_IB_IF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700292 /* CLKRP Receive clock polarity,
293 * 0 - sampled on falling edge of CLKR
294 * valid on falling edge
295 * CLKXP Transmit clock polarity,
296 * 0 - clocked on rising edge of CLKX
297 * valid on falling edge
298 * FSRP Receive frame sync pol, 1 - active low
299 * FSXP Transmit frame sync pol, 1 - active low
300 */
Troy Kisky21903c12008-12-18 12:36:43 -0700301 pcr |= (DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100302 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700303 case SND_SOC_DAIFMT_NB_IF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700304 /* CLKRP Receive clock polarity,
305 * 1 - sampled on rising edge of CLKR
306 * valid on rising edge
307 * CLKXP Transmit clock polarity,
308 * 1 - clocked on falling edge of CLKX
309 * valid on rising edge
310 * FSRP Receive frame sync pol, 1 - active low
311 * FSXP Transmit frame sync pol, 1 - active low
312 */
Troy Kisky21903c12008-12-18 12:36:43 -0700313 pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP |
314 DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100315 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700316 case SND_SOC_DAIFMT_IB_NF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700317 /* CLKRP Receive clock polarity,
318 * 0 - sampled on falling edge of CLKR
319 * valid on falling edge
320 * CLKXP Transmit clock polarity,
321 * 0 - clocked on rising edge of CLKX
322 * valid on falling edge
323 * FSRP Receive frame sync pol, 0 - active high
324 * FSXP Transmit frame sync pol, 0 - active high
325 */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100326 break;
327 default:
328 return -EINVAL;
329 }
Troy Kisky21903c12008-12-18 12:36:43 -0700330 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700331 dev->pcr = pcr;
Troy Kisky21903c12008-12-18 12:36:43 -0700332 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, pcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100333 return 0;
334}
335
336static int davinci_i2s_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000337 struct snd_pcm_hw_params *params,
338 struct snd_soc_dai *dai)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100339{
Troy Kisky9bb74152009-08-06 16:55:31 -0700340 struct davinci_mcbsp_dev *dev = dai->private_data;
Troy Kisky81ac55a2009-09-11 14:29:02 -0700341 struct davinci_pcm_dma_params *dma_params =
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700342 &dev->dma_params[substream->stream];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100343 struct snd_interval *i = NULL;
344 int mcbsp_word_length;
Troy Kisky35cf6352009-07-04 19:29:51 -0700345 unsigned int rcr, xcr, srgr;
346 u32 spcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100347
348 /* general line settings */
Troy Kisky35cf6352009-07-04 19:29:51 -0700349 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530350 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Troy Kisky35cf6352009-07-04 19:29:51 -0700351 spcr |= DAVINCI_MCBSP_SPCR_RINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
352 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530353 } else {
Troy Kisky35cf6352009-07-04 19:29:51 -0700354 spcr |= DAVINCI_MCBSP_SPCR_XINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
355 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530356 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100357
358 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Troy Kisky35cf6352009-07-04 19:29:51 -0700359 srgr = DAVINCI_MCBSP_SRGR_FSGM;
360 srgr |= DAVINCI_MCBSP_SRGR_FWID(snd_interval_value(i) - 1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100361
362 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_FRAME_BITS);
Troy Kisky35cf6352009-07-04 19:29:51 -0700363 srgr |= DAVINCI_MCBSP_SRGR_FPER(snd_interval_value(i) - 1);
364 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100365
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700366 rcr = DAVINCI_MCBSP_RCR_RFIG;
367 xcr = DAVINCI_MCBSP_XCR_XFIG;
368 if (dev->mode == MOD_DSP_B) {
369 rcr |= DAVINCI_MCBSP_RCR_RDATDLY(0);
370 xcr |= DAVINCI_MCBSP_XCR_XDATDLY(0);
371 } else {
372 rcr |= DAVINCI_MCBSP_RCR_RDATDLY(1);
373 xcr |= DAVINCI_MCBSP_XCR_XDATDLY(1);
374 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100375 /* Determine xfer data type */
376 switch (params_format(params)) {
377 case SNDRV_PCM_FORMAT_S8:
378 dma_params->data_type = 1;
379 mcbsp_word_length = DAVINCI_MCBSP_WORD_8;
380 break;
381 case SNDRV_PCM_FORMAT_S16_LE:
382 dma_params->data_type = 2;
383 mcbsp_word_length = DAVINCI_MCBSP_WORD_16;
384 break;
385 case SNDRV_PCM_FORMAT_S32_LE:
386 dma_params->data_type = 4;
387 mcbsp_word_length = DAVINCI_MCBSP_WORD_32;
388 break;
389 default:
Jean Delvare9b6e12e2008-08-26 15:47:55 +0200390 printk(KERN_WARNING "davinci-i2s: unsupported PCM format\n");
Vladimir Barinov310355c2008-02-18 11:40:22 +0100391 return -EINVAL;
392 }
393
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400394 dma_params->acnt = dma_params->data_type;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400395 dma_params->fifo_level = 0;
396
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700397 rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(1);
398 xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100399
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700400 rcr |= DAVINCI_MCBSP_RCR_RWDLEN1(mcbsp_word_length) |
401 DAVINCI_MCBSP_RCR_RWDLEN2(mcbsp_word_length);
402 xcr |= DAVINCI_MCBSP_XCR_XWDLEN1(mcbsp_word_length) |
403 DAVINCI_MCBSP_XCR_XWDLEN2(mcbsp_word_length);
404
405 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Troy Kisky35cf6352009-07-04 19:29:51 -0700406 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_XCR_REG, xcr);
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700407 else
408 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_RCR_REG, rcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100409 return 0;
410}
411
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700412static int davinci_i2s_prepare(struct snd_pcm_substream *substream,
413 struct snd_soc_dai *dai)
414{
Troy Kisky9bb74152009-08-06 16:55:31 -0700415 struct davinci_mcbsp_dev *dev = dai->private_data;
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700416 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
417 davinci_mcbsp_stop(dev, playback);
418 if ((dev->pcr & DAVINCI_MCBSP_PCR_FSXM) == 0) {
419 /* codec is master */
420 davinci_mcbsp_start(dev, substream);
421 }
422 return 0;
423}
424
Mark Browndee89c42008-11-18 22:11:38 +0000425static int davinci_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
426 struct snd_soc_dai *dai)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100427{
Troy Kisky9bb74152009-08-06 16:55:31 -0700428 struct davinci_mcbsp_dev *dev = dai->private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100429 int ret = 0;
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700430 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700431 if ((dev->pcr & DAVINCI_MCBSP_PCR_FSXM) == 0)
432 return 0; /* return if codec is master */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100433
434 switch (cmd) {
435 case SNDRV_PCM_TRIGGER_START:
436 case SNDRV_PCM_TRIGGER_RESUME:
437 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700438 davinci_mcbsp_start(dev, substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100439 break;
440 case SNDRV_PCM_TRIGGER_STOP:
441 case SNDRV_PCM_TRIGGER_SUSPEND:
442 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700443 davinci_mcbsp_stop(dev, playback);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100444 break;
445 default:
446 ret = -EINVAL;
447 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100448 return ret;
449}
450
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700451static void davinci_i2s_shutdown(struct snd_pcm_substream *substream,
452 struct snd_soc_dai *dai)
453{
Troy Kisky9bb74152009-08-06 16:55:31 -0700454 struct davinci_mcbsp_dev *dev = dai->private_data;
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700455 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
456 davinci_mcbsp_stop(dev, playback);
457}
458
Chaithrika U S5204d492009-06-05 06:28:23 -0400459#define DAVINCI_I2S_RATES SNDRV_PCM_RATE_8000_96000
460
461static struct snd_soc_dai_ops davinci_i2s_dai_ops = {
Mark Brown3f405b42009-07-07 19:18:46 +0100462 .shutdown = davinci_i2s_shutdown,
463 .prepare = davinci_i2s_prepare,
Chaithrika U S5204d492009-06-05 06:28:23 -0400464 .trigger = davinci_i2s_trigger,
465 .hw_params = davinci_i2s_hw_params,
466 .set_fmt = davinci_i2s_set_dai_fmt,
467
468};
469
470struct snd_soc_dai davinci_i2s_dai = {
471 .name = "davinci-i2s",
472 .id = 0,
473 .playback = {
474 .channels_min = 2,
475 .channels_max = 2,
476 .rates = DAVINCI_I2S_RATES,
477 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
478 .capture = {
479 .channels_min = 2,
480 .channels_max = 2,
481 .rates = DAVINCI_I2S_RATES,
482 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
483 .ops = &davinci_i2s_dai_ops,
484
485};
486EXPORT_SYMBOL_GPL(davinci_i2s_dai);
487
488static int davinci_i2s_probe(struct platform_device *pdev)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100489{
Chaithrika U S5204d492009-06-05 06:28:23 -0400490 struct snd_platform_data *pdata = pdev->dev.platform_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100491 struct davinci_mcbsp_dev *dev;
Chaithrika U S5204d492009-06-05 06:28:23 -0400492 struct resource *mem, *ioarea, *res;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100493 int ret;
494
495 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
496 if (!mem) {
497 dev_err(&pdev->dev, "no mem resource?\n");
498 return -ENODEV;
499 }
500
501 ioarea = request_mem_region(mem->start, (mem->end - mem->start) + 1,
502 pdev->name);
503 if (!ioarea) {
504 dev_err(&pdev->dev, "McBSP region already claimed\n");
505 return -EBUSY;
506 }
507
508 dev = kzalloc(sizeof(struct davinci_mcbsp_dev), GFP_KERNEL);
509 if (!dev) {
510 ret = -ENOMEM;
511 goto err_release_region;
512 }
513
Kevin Hilman3e46a442009-07-15 10:42:09 -0700514 dev->clk = clk_get(&pdev->dev, NULL);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100515 if (IS_ERR(dev->clk)) {
516 ret = -ENODEV;
517 goto err_free_mem;
518 }
519 clk_enable(dev->clk);
520
521 dev->base = (void __iomem *)IO_ADDRESS(mem->start);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100522
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700523 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].dma_addr =
Vladimir Barinov310355c2008-02-18 11:40:22 +0100524 (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DXR_REG);
525
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700526 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].dma_addr =
Vladimir Barinov310355c2008-02-18 11:40:22 +0100527 (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DRR_REG);
528
Chaithrika U S5204d492009-06-05 06:28:23 -0400529 /* first TX, then RX */
530 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
531 if (!res) {
532 dev_err(&pdev->dev, "no DMA resource\n");
Chaithrika U Sefd13be2009-06-08 06:49:41 -0400533 ret = -ENXIO;
Chaithrika U S5204d492009-06-05 06:28:23 -0400534 goto err_free_mem;
535 }
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700536 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].channel = res->start;
Chaithrika U S5204d492009-06-05 06:28:23 -0400537
538 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
539 if (!res) {
540 dev_err(&pdev->dev, "no DMA resource\n");
Chaithrika U Sefd13be2009-06-08 06:49:41 -0400541 ret = -ENXIO;
Chaithrika U S5204d492009-06-05 06:28:23 -0400542 goto err_free_mem;
543 }
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700544 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].channel = res->start;
Chaithrika U S5204d492009-06-05 06:28:23 -0400545
546 davinci_i2s_dai.private_data = dev;
Troy Kisky57512c62009-11-16 16:52:31 -0700547 davinci_i2s_dai.dma_data = dev->dma_params;
Chaithrika U S5204d492009-06-05 06:28:23 -0400548 ret = snd_soc_register_dai(&davinci_i2s_dai);
549 if (ret != 0)
550 goto err_free_mem;
551
Vladimir Barinov310355c2008-02-18 11:40:22 +0100552 return 0;
553
554err_free_mem:
555 kfree(dev);
556err_release_region:
557 release_mem_region(mem->start, (mem->end - mem->start) + 1);
558
559 return ret;
560}
561
Chaithrika U S5204d492009-06-05 06:28:23 -0400562static int davinci_i2s_remove(struct platform_device *pdev)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100563{
Chaithrika U S5204d492009-06-05 06:28:23 -0400564 struct davinci_mcbsp_dev *dev = davinci_i2s_dai.private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100565 struct resource *mem;
566
Chaithrika U S5204d492009-06-05 06:28:23 -0400567 snd_soc_unregister_dai(&davinci_i2s_dai);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100568 clk_disable(dev->clk);
569 clk_put(dev->clk);
570 dev->clk = NULL;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100571 kfree(dev);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100572 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
573 release_mem_region(mem->start, (mem->end - mem->start) + 1);
Chaithrika U S5204d492009-06-05 06:28:23 -0400574
575 return 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100576}
577
Chaithrika U S5204d492009-06-05 06:28:23 -0400578static struct platform_driver davinci_mcbsp_driver = {
579 .probe = davinci_i2s_probe,
580 .remove = davinci_i2s_remove,
581 .driver = {
582 .name = "davinci-asp",
583 .owner = THIS_MODULE,
584 },
Eric Miao6335d052009-03-03 09:41:00 +0800585};
586
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100587static int __init davinci_i2s_init(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000588{
Chaithrika U S5204d492009-06-05 06:28:23 -0400589 return platform_driver_register(&davinci_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000590}
591module_init(davinci_i2s_init);
592
593static void __exit davinci_i2s_exit(void)
594{
Chaithrika U S5204d492009-06-05 06:28:23 -0400595 platform_driver_unregister(&davinci_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000596}
597module_exit(davinci_i2s_exit);
598
Vladimir Barinov310355c2008-02-18 11:40:22 +0100599MODULE_AUTHOR("Vladimir Barinov");
600MODULE_DESCRIPTION("TI DAVINCI I2S (McBSP) SoC Interface");
601MODULE_LICENSE("GPL");