blob: e763d58e51f810881c5d61ec510a2f8cac10faf5 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Vladimir Barinov310355c2008-02-18 11:40:22 +010016#include <linux/delay.h>
17#include <linux/io.h>
18#include <linux/clk.h>
19
20#include <sound/core.h>
21#include <sound/pcm.h>
22#include <sound/pcm_params.h>
23#include <sound/initval.h>
24#include <sound/soc.h>
25
Mark Brownff7d04b2009-07-08 16:54:51 +010026#include <mach/asp.h>
27
Vladimir Barinov310355c2008-02-18 11:40:22 +010028#include "davinci-pcm.h"
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +020029#include "davinci-i2s.h"
Vladimir Barinov310355c2008-02-18 11:40:22 +010030
David Brownella62114c2009-05-14 12:47:42 -070031
32/*
33 * NOTE: terminology here is confusing.
34 *
35 * - This driver supports the "Audio Serial Port" (ASP),
36 * found on dm6446, dm355, and other DaVinci chips.
37 *
38 * - But it labels it a "Multi-channel Buffered Serial Port"
39 * (McBSP) as on older chips like the dm642 ... which was
40 * backward-compatible, possibly explaining that confusion.
41 *
42 * - OMAP chips have a controller called McBSP, which is
43 * incompatible with the DaVinci flavor of McBSP.
44 *
45 * - Newer DaVinci chips have a controller called McASP,
46 * incompatible with ASP and with either McBSP.
47 *
48 * In short: this uses ASP to implement I2S, not McBSP.
49 * And it won't be the only DaVinci implemention of I2S.
50 */
Vladimir Barinov310355c2008-02-18 11:40:22 +010051#define DAVINCI_MCBSP_DRR_REG 0x00
52#define DAVINCI_MCBSP_DXR_REG 0x04
53#define DAVINCI_MCBSP_SPCR_REG 0x08
54#define DAVINCI_MCBSP_RCR_REG 0x0c
55#define DAVINCI_MCBSP_XCR_REG 0x10
56#define DAVINCI_MCBSP_SRGR_REG 0x14
57#define DAVINCI_MCBSP_PCR_REG 0x24
58
59#define DAVINCI_MCBSP_SPCR_RRST (1 << 0)
60#define DAVINCI_MCBSP_SPCR_RINTM(v) ((v) << 4)
61#define DAVINCI_MCBSP_SPCR_XRST (1 << 16)
62#define DAVINCI_MCBSP_SPCR_XINTM(v) ((v) << 20)
63#define DAVINCI_MCBSP_SPCR_GRST (1 << 22)
64#define DAVINCI_MCBSP_SPCR_FRST (1 << 23)
65#define DAVINCI_MCBSP_SPCR_FREE (1 << 25)
66
67#define DAVINCI_MCBSP_RCR_RWDLEN1(v) ((v) << 5)
68#define DAVINCI_MCBSP_RCR_RFRLEN1(v) ((v) << 8)
69#define DAVINCI_MCBSP_RCR_RDATDLY(v) ((v) << 16)
Troy Kiskyf5cfa952009-07-04 19:29:57 -070070#define DAVINCI_MCBSP_RCR_RFIG (1 << 18)
Vladimir Barinov310355c2008-02-18 11:40:22 +010071#define DAVINCI_MCBSP_RCR_RWDLEN2(v) ((v) << 21)
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +020072#define DAVINCI_MCBSP_RCR_RFRLEN2(v) ((v) << 24)
73#define DAVINCI_MCBSP_RCR_RPHASE BIT(31)
Vladimir Barinov310355c2008-02-18 11:40:22 +010074
75#define DAVINCI_MCBSP_XCR_XWDLEN1(v) ((v) << 5)
76#define DAVINCI_MCBSP_XCR_XFRLEN1(v) ((v) << 8)
77#define DAVINCI_MCBSP_XCR_XDATDLY(v) ((v) << 16)
78#define DAVINCI_MCBSP_XCR_XFIG (1 << 18)
79#define DAVINCI_MCBSP_XCR_XWDLEN2(v) ((v) << 21)
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +020080#define DAVINCI_MCBSP_XCR_XFRLEN2(v) ((v) << 24)
81#define DAVINCI_MCBSP_XCR_XPHASE BIT(31)
Vladimir Barinov310355c2008-02-18 11:40:22 +010082
83#define DAVINCI_MCBSP_SRGR_FWID(v) ((v) << 8)
84#define DAVINCI_MCBSP_SRGR_FPER(v) ((v) << 16)
85#define DAVINCI_MCBSP_SRGR_FSGM (1 << 28)
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +020086#define DAVINCI_MCBSP_SRGR_CLKSM BIT(29)
Vladimir Barinov310355c2008-02-18 11:40:22 +010087
88#define DAVINCI_MCBSP_PCR_CLKRP (1 << 0)
89#define DAVINCI_MCBSP_PCR_CLKXP (1 << 1)
90#define DAVINCI_MCBSP_PCR_FSRP (1 << 2)
91#define DAVINCI_MCBSP_PCR_FSXP (1 << 3)
Hugo Villeneuveb402dff2008-11-08 13:26:09 -050092#define DAVINCI_MCBSP_PCR_SCLKME (1 << 7)
Vladimir Barinov310355c2008-02-18 11:40:22 +010093#define DAVINCI_MCBSP_PCR_CLKRM (1 << 8)
94#define DAVINCI_MCBSP_PCR_CLKXM (1 << 9)
95#define DAVINCI_MCBSP_PCR_FSRM (1 << 10)
96#define DAVINCI_MCBSP_PCR_FSXM (1 << 11)
97
Vladimir Barinov310355c2008-02-18 11:40:22 +010098enum {
99 DAVINCI_MCBSP_WORD_8 = 0,
100 DAVINCI_MCBSP_WORD_12,
101 DAVINCI_MCBSP_WORD_16,
102 DAVINCI_MCBSP_WORD_20,
103 DAVINCI_MCBSP_WORD_24,
104 DAVINCI_MCBSP_WORD_32,
105};
106
Troy Kisky0d6c9772009-11-18 17:49:51 -0700107static const unsigned char data_type[SNDRV_PCM_FORMAT_S32_LE + 1] = {
108 [SNDRV_PCM_FORMAT_S8] = 1,
109 [SNDRV_PCM_FORMAT_S16_LE] = 2,
110 [SNDRV_PCM_FORMAT_S32_LE] = 4,
111};
112
113static const unsigned char asp_word_length[SNDRV_PCM_FORMAT_S32_LE + 1] = {
114 [SNDRV_PCM_FORMAT_S8] = DAVINCI_MCBSP_WORD_8,
115 [SNDRV_PCM_FORMAT_S16_LE] = DAVINCI_MCBSP_WORD_16,
116 [SNDRV_PCM_FORMAT_S32_LE] = DAVINCI_MCBSP_WORD_32,
117};
118
119static const unsigned char double_fmt[SNDRV_PCM_FORMAT_S32_LE + 1] = {
120 [SNDRV_PCM_FORMAT_S8] = SNDRV_PCM_FORMAT_S16_LE,
121 [SNDRV_PCM_FORMAT_S16_LE] = SNDRV_PCM_FORMAT_S32_LE,
122};
123
Vladimir Barinov310355c2008-02-18 11:40:22 +0100124struct davinci_mcbsp_dev {
Raffaele Recalcatiec637552010-07-06 10:39:03 +0200125 struct device *dev;
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700126 struct davinci_pcm_dma_params dma_params[2];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100127 void __iomem *base;
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700128#define MOD_DSP_A 0
129#define MOD_DSP_B 1
130 int mode;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700131 u32 pcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100132 struct clk *clk;
Troy Kisky0d6c9772009-11-18 17:49:51 -0700133 /*
134 * Combining both channels into 1 element will at least double the
135 * amount of time between servicing the dma channel, increase
136 * effiency, and reduce the chance of overrun/underrun. But,
137 * it will result in the left & right channels being swapped.
138 *
139 * If relabeling the left and right channels is not possible,
140 * you may want to let the codec know to swap them back.
141 *
142 * It may allow x10 the amount of time to service dma requests,
143 * if the codec is master and is using an unnecessarily fast bit clock
144 * (ie. tlvaic23b), independent of the sample rate. So, having an
145 * entire frame at once means it can be serviced at the sample rate
146 * instead of the bit clock rate.
147 *
148 * In the now unlikely case that an underrun still
149 * occurs, both the left and right samples will be repeated
150 * so that no pops are heard, and the left and right channels
151 * won't end up being swapped because of the underrun.
152 */
153 unsigned enable_channel_combine:1;
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200154
155 unsigned int fmt;
156 int clk_div;
Raffaele Recalcatiec637552010-07-06 10:39:03 +0200157 int clk_input_pin;
Raffaele Recalcatid9823ed92010-07-06 10:39:04 +0200158 bool i2s_accurate_sck;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100159};
160
161static inline void davinci_mcbsp_write_reg(struct davinci_mcbsp_dev *dev,
162 int reg, u32 val)
163{
164 __raw_writel(val, dev->base + reg);
165}
166
167static inline u32 davinci_mcbsp_read_reg(struct davinci_mcbsp_dev *dev, int reg)
168{
169 return __raw_readl(dev->base + reg);
170}
171
Troy Kiskyc392bec2009-07-04 19:29:52 -0700172static void toggle_clock(struct davinci_mcbsp_dev *dev, int playback)
173{
174 u32 m = playback ? DAVINCI_MCBSP_PCR_CLKXP : DAVINCI_MCBSP_PCR_CLKRP;
175 /* The clock needs to toggle to complete reset.
176 * So, fake it by toggling the clk polarity.
177 */
178 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr ^ m);
179 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr);
180}
181
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700182static void davinci_mcbsp_start(struct davinci_mcbsp_dev *dev,
183 struct snd_pcm_substream *substream)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100184{
185 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000186 struct snd_soc_platform *platform = rtd->platform;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700187 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Troy Kisky35cf6352009-07-04 19:29:51 -0700188 u32 spcr;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700189 u32 mask = playback ? DAVINCI_MCBSP_SPCR_XRST : DAVINCI_MCBSP_SPCR_RRST;
Troy Kisky35cf6352009-07-04 19:29:51 -0700190 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700191 if (spcr & mask) {
192 /* start off disabled */
193 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG,
194 spcr & ~mask);
195 toggle_clock(dev, playback);
196 }
Troy Kisky1bef4492009-07-04 19:29:55 -0700197 if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM |
198 DAVINCI_MCBSP_PCR_CLKXM | DAVINCI_MCBSP_PCR_CLKRM)) {
199 /* Start the sample generator */
200 spcr |= DAVINCI_MCBSP_SPCR_GRST;
201 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
202 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100203
Troy Kisky1bef4492009-07-04 19:29:55 -0700204 if (playback) {
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530205 /* Stop the DMA to avoid data loss */
206 /* while the transmitter is out of reset to handle XSYNCERR */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000207 if (platform->driver->ops->trigger) {
208 int ret = platform->driver->ops->trigger(substream,
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530209 SNDRV_PCM_TRIGGER_STOP);
210 if (ret < 0)
211 printk(KERN_DEBUG "Playback DMA stop failed\n");
212 }
213
214 /* Enable the transmitter */
Troy Kisky35cf6352009-07-04 19:29:51 -0700215 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
216 spcr |= DAVINCI_MCBSP_SPCR_XRST;
217 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530218
219 /* wait for any unexpected frame sync error to occur */
220 udelay(100);
221
222 /* Disable the transmitter to clear any outstanding XSYNCERR */
Troy Kisky35cf6352009-07-04 19:29:51 -0700223 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
224 spcr &= ~DAVINCI_MCBSP_SPCR_XRST;
225 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700226 toggle_clock(dev, playback);
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530227
228 /* Restart the DMA */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000229 if (platform->driver->ops->trigger) {
230 int ret = platform->driver->ops->trigger(substream,
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530231 SNDRV_PCM_TRIGGER_START);
232 if (ret < 0)
233 printk(KERN_DEBUG "Playback DMA start failed\n");
234 }
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530235 }
236
Troy Kisky1bef4492009-07-04 19:29:55 -0700237 /* Enable transmitter or receiver */
Troy Kisky35cf6352009-07-04 19:29:51 -0700238 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Troy Kisky1bef4492009-07-04 19:29:55 -0700239 spcr |= mask;
240
241 if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM)) {
242 /* Start frame sync */
243 spcr |= DAVINCI_MCBSP_SPCR_FRST;
244 }
Troy Kisky35cf6352009-07-04 19:29:51 -0700245 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100246}
247
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700248static void davinci_mcbsp_stop(struct davinci_mcbsp_dev *dev, int playback)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100249{
Troy Kisky35cf6352009-07-04 19:29:51 -0700250 u32 spcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100251
252 /* Reset transmitter/receiver and sample rate/frame sync generators */
Troy Kisky35cf6352009-07-04 19:29:51 -0700253 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
254 spcr &= ~(DAVINCI_MCBSP_SPCR_GRST | DAVINCI_MCBSP_SPCR_FRST);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700255 spcr &= playback ? ~DAVINCI_MCBSP_SPCR_XRST : ~DAVINCI_MCBSP_SPCR_RRST;
Troy Kisky35cf6352009-07-04 19:29:51 -0700256 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700257 toggle_clock(dev, playback);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100258}
259
Troy Kisky21903c12008-12-18 12:36:43 -0700260#define DEFAULT_BITPERSAMPLE 16
261
Liam Girdwood9cb132d2008-07-07 16:07:42 +0100262static int davinci_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
Vladimir Barinov310355c2008-02-18 11:40:22 +0100263 unsigned int fmt)
264{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000265 struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
Troy Kisky21903c12008-12-18 12:36:43 -0700266 unsigned int pcr;
267 unsigned int srgr;
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200268 /* Attention srgr is updated by hw_params! */
Troy Kisky21903c12008-12-18 12:36:43 -0700269 srgr = DAVINCI_MCBSP_SRGR_FSGM |
270 DAVINCI_MCBSP_SRGR_FPER(DEFAULT_BITPERSAMPLE * 2 - 1) |
271 DAVINCI_MCBSP_SRGR_FWID(DEFAULT_BITPERSAMPLE - 1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100272
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200273 dev->fmt = fmt;
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700274 /* set master/slave audio interface */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100275 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
276 case SND_SOC_DAIFMT_CBS_CFS:
Troy Kisky21903c12008-12-18 12:36:43 -0700277 /* cpu is master */
278 pcr = DAVINCI_MCBSP_PCR_FSXM |
279 DAVINCI_MCBSP_PCR_FSRM |
280 DAVINCI_MCBSP_PCR_CLKXM |
281 DAVINCI_MCBSP_PCR_CLKRM;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100282 break;
Hugo Villeneuveb402dff2008-11-08 13:26:09 -0500283 case SND_SOC_DAIFMT_CBM_CFS:
Raffaele Recalcatiec637552010-07-06 10:39:03 +0200284 pcr = DAVINCI_MCBSP_PCR_FSRM | DAVINCI_MCBSP_PCR_FSXM;
285 /*
286 * Selection of the clock input pin that is the
287 * input for the Sample Rate Generator.
288 * McBSP FSR and FSX are driven by the Sample Rate
289 * Generator.
290 */
291 switch (dev->clk_input_pin) {
292 case MCBSP_CLKS:
293 pcr |= DAVINCI_MCBSP_PCR_CLKXM |
294 DAVINCI_MCBSP_PCR_CLKRM;
295 break;
296 case MCBSP_CLKR:
297 pcr |= DAVINCI_MCBSP_PCR_SCLKME;
298 break;
299 default:
300 dev_err(dev->dev, "bad clk_input_pin\n");
301 return -EINVAL;
302 }
303
Hugo Villeneuveb402dff2008-11-08 13:26:09 -0500304 break;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100305 case SND_SOC_DAIFMT_CBM_CFM:
Troy Kisky21903c12008-12-18 12:36:43 -0700306 /* codec is master */
307 pcr = 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100308 break;
309 default:
Troy Kisky21903c12008-12-18 12:36:43 -0700310 printk(KERN_ERR "%s:bad master\n", __func__);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100311 return -EINVAL;
312 }
313
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700314 /* interface format */
Troy Kisky69ab8202008-12-18 12:36:44 -0700315 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Troy Kisky69ab8202008-12-18 12:36:44 -0700316 case SND_SOC_DAIFMT_I2S:
Troy Kisky07d8d9d2008-12-19 13:05:24 -0700317 /* Davinci doesn't support TRUE I2S, but some codecs will have
318 * the left and right channels contiguous. This allows
319 * dsp_a mode to be used with an inverted normal frame clk.
320 * If your codec is master and does not have contiguous
321 * channels, then you will have sound on only one channel.
322 * Try using a different mode, or codec as slave.
323 *
324 * The TLV320AIC33 is an example of a codec where this works.
325 * It has a variable bit clock frequency allowing it to have
326 * valid data on every bit clock.
327 *
328 * The TLV320AIC23 is an example of a codec where this does not
329 * work. It has a fixed bit clock frequency with progressively
330 * more empty bit clock slots between channels as the sample
331 * rate is lowered.
332 */
333 fmt ^= SND_SOC_DAIFMT_NB_IF;
334 case SND_SOC_DAIFMT_DSP_A:
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700335 dev->mode = MOD_DSP_A;
336 break;
337 case SND_SOC_DAIFMT_DSP_B:
338 dev->mode = MOD_DSP_B;
Troy Kisky69ab8202008-12-18 12:36:44 -0700339 break;
340 default:
341 printk(KERN_ERR "%s:bad format\n", __func__);
342 return -EINVAL;
343 }
344
Vladimir Barinov310355c2008-02-18 11:40:22 +0100345 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
Troy Kisky9e031622008-12-19 13:05:23 -0700346 case SND_SOC_DAIFMT_NB_NF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700347 /* CLKRP Receive clock polarity,
348 * 1 - sampled on rising edge of CLKR
349 * valid on rising edge
350 * CLKXP Transmit clock polarity,
351 * 1 - clocked on falling edge of CLKX
352 * valid on rising edge
353 * FSRP Receive frame sync pol, 0 - active high
354 * FSXP Transmit frame sync pol, 0 - active high
355 */
Troy Kisky21903c12008-12-18 12:36:43 -0700356 pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100357 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700358 case SND_SOC_DAIFMT_IB_IF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700359 /* CLKRP Receive clock polarity,
360 * 0 - sampled on falling edge of CLKR
361 * valid on falling edge
362 * CLKXP Transmit clock polarity,
363 * 0 - clocked on rising edge of CLKX
364 * valid on falling edge
365 * FSRP Receive frame sync pol, 1 - active low
366 * FSXP Transmit frame sync pol, 1 - active low
367 */
Troy Kisky21903c12008-12-18 12:36:43 -0700368 pcr |= (DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100369 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700370 case SND_SOC_DAIFMT_NB_IF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700371 /* CLKRP Receive clock polarity,
372 * 1 - sampled on rising edge of CLKR
373 * valid on rising edge
374 * CLKXP Transmit clock polarity,
375 * 1 - clocked on falling edge of CLKX
376 * valid on rising edge
377 * FSRP Receive frame sync pol, 1 - active low
378 * FSXP Transmit frame sync pol, 1 - active low
379 */
Troy Kisky21903c12008-12-18 12:36:43 -0700380 pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP |
381 DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100382 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700383 case SND_SOC_DAIFMT_IB_NF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700384 /* CLKRP Receive clock polarity,
385 * 0 - sampled on falling edge of CLKR
386 * valid on falling edge
387 * CLKXP Transmit clock polarity,
388 * 0 - clocked on rising edge of CLKX
389 * valid on falling edge
390 * FSRP Receive frame sync pol, 0 - active high
391 * FSXP Transmit frame sync pol, 0 - active high
392 */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100393 break;
394 default:
395 return -EINVAL;
396 }
Troy Kisky21903c12008-12-18 12:36:43 -0700397 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700398 dev->pcr = pcr;
Troy Kisky21903c12008-12-18 12:36:43 -0700399 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, pcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100400 return 0;
401}
402
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200403static int davinci_i2s_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
404 int div_id, int div)
405{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000406 struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200407
408 if (div_id != DAVINCI_MCBSP_CLKGDV)
409 return -ENODEV;
410
411 dev->clk_div = div;
412 return 0;
413}
414
Vladimir Barinov310355c2008-02-18 11:40:22 +0100415static int davinci_i2s_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000416 struct snd_pcm_hw_params *params,
417 struct snd_soc_dai *dai)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100418{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000419 struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
Troy Kisky81ac55a2009-09-11 14:29:02 -0700420 struct davinci_pcm_dma_params *dma_params =
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700421 &dev->dma_params[substream->stream];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100422 struct snd_interval *i = NULL;
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200423 int mcbsp_word_length, master;
424 unsigned int rcr, xcr, srgr, clk_div, freq, framesize;
Troy Kisky35cf6352009-07-04 19:29:51 -0700425 u32 spcr;
Troy Kisky0d6c9772009-11-18 17:49:51 -0700426 snd_pcm_format_t fmt;
427 unsigned element_cnt = 1;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100428
429 /* general line settings */
Troy Kisky35cf6352009-07-04 19:29:51 -0700430 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530431 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Troy Kisky35cf6352009-07-04 19:29:51 -0700432 spcr |= DAVINCI_MCBSP_SPCR_RINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
433 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530434 } else {
Troy Kisky35cf6352009-07-04 19:29:51 -0700435 spcr |= DAVINCI_MCBSP_SPCR_XINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
436 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530437 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100438
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200439 master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
440 fmt = params_format(params);
441 mcbsp_word_length = asp_word_length[fmt];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100442
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200443 switch (master) {
444 case SND_SOC_DAIFMT_CBS_CFS:
445 freq = clk_get_rate(dev->clk);
446 srgr = DAVINCI_MCBSP_SRGR_FSGM |
447 DAVINCI_MCBSP_SRGR_CLKSM;
448 srgr |= DAVINCI_MCBSP_SRGR_FWID(mcbsp_word_length *
449 8 - 1);
Raffaele Recalcatid9823ed92010-07-06 10:39:04 +0200450 if (dev->i2s_accurate_sck) {
451 clk_div = 256;
452 do {
453 framesize = (freq / (--clk_div)) /
454 params->rate_num *
455 params->rate_den;
456 } while (((framesize < 33) || (framesize > 4095)) &&
457 (clk_div));
458 clk_div--;
459 srgr |= DAVINCI_MCBSP_SRGR_FPER(framesize - 1);
460 } else {
461 /* symmetric waveforms */
462 clk_div = freq / (mcbsp_word_length * 16) /
463 params->rate_num * params->rate_den;
464 srgr |= DAVINCI_MCBSP_SRGR_FPER(mcbsp_word_length *
465 16 - 1);
466 }
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200467 clk_div &= 0xFF;
468 srgr |= clk_div;
469 break;
470 case SND_SOC_DAIFMT_CBM_CFS:
471 srgr = DAVINCI_MCBSP_SRGR_FSGM;
472 clk_div = dev->clk_div - 1;
473 srgr |= DAVINCI_MCBSP_SRGR_FWID(mcbsp_word_length * 8 - 1);
474 srgr |= DAVINCI_MCBSP_SRGR_FPER(mcbsp_word_length * 16 - 1);
475 clk_div &= 0xFF;
476 srgr |= clk_div;
477 break;
478 case SND_SOC_DAIFMT_CBM_CFM:
479 /* Clock and frame sync given from external sources */
480 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
481 srgr = DAVINCI_MCBSP_SRGR_FSGM;
482 srgr |= DAVINCI_MCBSP_SRGR_FWID(snd_interval_value(i) - 1);
483 pr_debug("%s - %d FWID set: re-read srgr = %X\n",
484 __func__, __LINE__, snd_interval_value(i) - 1);
485
486 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_FRAME_BITS);
487 srgr |= DAVINCI_MCBSP_SRGR_FPER(snd_interval_value(i) - 1);
488 break;
489 default:
490 return -EINVAL;
491 }
Troy Kisky35cf6352009-07-04 19:29:51 -0700492 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100493
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700494 rcr = DAVINCI_MCBSP_RCR_RFIG;
495 xcr = DAVINCI_MCBSP_XCR_XFIG;
496 if (dev->mode == MOD_DSP_B) {
497 rcr |= DAVINCI_MCBSP_RCR_RDATDLY(0);
498 xcr |= DAVINCI_MCBSP_XCR_XDATDLY(0);
499 } else {
500 rcr |= DAVINCI_MCBSP_RCR_RDATDLY(1);
501 xcr |= DAVINCI_MCBSP_XCR_XDATDLY(1);
502 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100503 /* Determine xfer data type */
Troy Kisky0d6c9772009-11-18 17:49:51 -0700504 fmt = params_format(params);
505 if ((fmt > SNDRV_PCM_FORMAT_S32_LE) || !data_type[fmt]) {
Jean Delvare9b6e12e2008-08-26 15:47:55 +0200506 printk(KERN_WARNING "davinci-i2s: unsupported PCM format\n");
Vladimir Barinov310355c2008-02-18 11:40:22 +0100507 return -EINVAL;
508 }
509
Troy Kisky0d6c9772009-11-18 17:49:51 -0700510 if (params_channels(params) == 2) {
511 element_cnt = 2;
512 if (double_fmt[fmt] && dev->enable_channel_combine) {
513 element_cnt = 1;
514 fmt = double_fmt[fmt];
515 }
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200516 switch (master) {
517 case SND_SOC_DAIFMT_CBS_CFS:
518 case SND_SOC_DAIFMT_CBS_CFM:
519 rcr |= DAVINCI_MCBSP_RCR_RFRLEN2(0);
520 xcr |= DAVINCI_MCBSP_XCR_XFRLEN2(0);
521 rcr |= DAVINCI_MCBSP_RCR_RPHASE;
522 xcr |= DAVINCI_MCBSP_XCR_XPHASE;
523 break;
524 case SND_SOC_DAIFMT_CBM_CFM:
525 case SND_SOC_DAIFMT_CBM_CFS:
526 rcr |= DAVINCI_MCBSP_RCR_RFRLEN2(element_cnt - 1);
527 xcr |= DAVINCI_MCBSP_XCR_XFRLEN2(element_cnt - 1);
528 break;
529 default:
530 return -EINVAL;
531 }
Troy Kisky0d6c9772009-11-18 17:49:51 -0700532 }
533 dma_params->acnt = dma_params->data_type = data_type[fmt];
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400534 dma_params->fifo_level = 0;
Troy Kisky0d6c9772009-11-18 17:49:51 -0700535 mcbsp_word_length = asp_word_length[fmt];
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200536
537 switch (master) {
538 case SND_SOC_DAIFMT_CBS_CFS:
539 case SND_SOC_DAIFMT_CBS_CFM:
540 rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(0);
541 xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(0);
542 break;
543 case SND_SOC_DAIFMT_CBM_CFM:
544 case SND_SOC_DAIFMT_CBM_CFS:
545 rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(element_cnt - 1);
546 xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(element_cnt - 1);
547 break;
548 default:
549 return -EINVAL;
550 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100551
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700552 rcr |= DAVINCI_MCBSP_RCR_RWDLEN1(mcbsp_word_length) |
553 DAVINCI_MCBSP_RCR_RWDLEN2(mcbsp_word_length);
554 xcr |= DAVINCI_MCBSP_XCR_XWDLEN1(mcbsp_word_length) |
555 DAVINCI_MCBSP_XCR_XWDLEN2(mcbsp_word_length);
556
557 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Troy Kisky35cf6352009-07-04 19:29:51 -0700558 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_XCR_REG, xcr);
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700559 else
560 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_RCR_REG, rcr);
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200561
562 pr_debug("%s - %d srgr=%X\n", __func__, __LINE__, srgr);
563 pr_debug("%s - %d xcr=%X\n", __func__, __LINE__, xcr);
564 pr_debug("%s - %d rcr=%X\n", __func__, __LINE__, rcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100565 return 0;
566}
567
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700568static int davinci_i2s_prepare(struct snd_pcm_substream *substream,
569 struct snd_soc_dai *dai)
570{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571 struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700572 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
573 davinci_mcbsp_stop(dev, playback);
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700574 return 0;
575}
576
Mark Browndee89c42008-11-18 22:11:38 +0000577static int davinci_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
578 struct snd_soc_dai *dai)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100579{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100581 int ret = 0;
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700582 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100583
584 switch (cmd) {
585 case SNDRV_PCM_TRIGGER_START:
586 case SNDRV_PCM_TRIGGER_RESUME:
587 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700588 davinci_mcbsp_start(dev, substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100589 break;
590 case SNDRV_PCM_TRIGGER_STOP:
591 case SNDRV_PCM_TRIGGER_SUSPEND:
592 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700593 davinci_mcbsp_stop(dev, playback);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100594 break;
595 default:
596 ret = -EINVAL;
597 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100598 return ret;
599}
600
Chris Paulson-Ellisbedad0c2010-11-16 12:27:09 +0000601static int davinci_i2s_startup(struct snd_pcm_substream *substream,
602 struct snd_soc_dai *dai)
603{
604 struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
605
606 snd_soc_dai_set_dma_data(dai, substream, dev->dma_params);
607 return 0;
608}
609
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700610static void davinci_i2s_shutdown(struct snd_pcm_substream *substream,
611 struct snd_soc_dai *dai)
612{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700614 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
615 davinci_mcbsp_stop(dev, playback);
616}
617
Chaithrika U S5204d492009-06-05 06:28:23 -0400618#define DAVINCI_I2S_RATES SNDRV_PCM_RATE_8000_96000
619
620static struct snd_soc_dai_ops davinci_i2s_dai_ops = {
Chris Paulson-Ellisbedad0c2010-11-16 12:27:09 +0000621 .startup = davinci_i2s_startup,
Mark Brown3f405b42009-07-07 19:18:46 +0100622 .shutdown = davinci_i2s_shutdown,
623 .prepare = davinci_i2s_prepare,
Chaithrika U S5204d492009-06-05 06:28:23 -0400624 .trigger = davinci_i2s_trigger,
625 .hw_params = davinci_i2s_hw_params,
626 .set_fmt = davinci_i2s_set_dai_fmt,
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200627 .set_clkdiv = davinci_i2s_dai_set_clkdiv,
Chaithrika U S5204d492009-06-05 06:28:23 -0400628
629};
630
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631static struct snd_soc_dai_driver davinci_i2s_dai = {
Chaithrika U S5204d492009-06-05 06:28:23 -0400632 .playback = {
633 .channels_min = 2,
634 .channels_max = 2,
635 .rates = DAVINCI_I2S_RATES,
636 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
637 .capture = {
638 .channels_min = 2,
639 .channels_max = 2,
640 .rates = DAVINCI_I2S_RATES,
641 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
642 .ops = &davinci_i2s_dai_ops,
643
644};
Chaithrika U S5204d492009-06-05 06:28:23 -0400645
646static int davinci_i2s_probe(struct platform_device *pdev)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100647{
Chaithrika U S5204d492009-06-05 06:28:23 -0400648 struct snd_platform_data *pdata = pdev->dev.platform_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100649 struct davinci_mcbsp_dev *dev;
Chaithrika U S5204d492009-06-05 06:28:23 -0400650 struct resource *mem, *ioarea, *res;
Sekhar Nori48519f02010-07-19 12:31:16 +0530651 enum dma_event_q asp_chan_q = EVENTQ_0;
652 enum dma_event_q ram_chan_q = EVENTQ_1;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100653 int ret;
654
655 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
656 if (!mem) {
657 dev_err(&pdev->dev, "no mem resource?\n");
658 return -ENODEV;
659 }
660
Vaibhav Bediad852f4462011-02-09 18:39:52 +0530661 ioarea = request_mem_region(mem->start, resource_size(mem),
Vladimir Barinov310355c2008-02-18 11:40:22 +0100662 pdev->name);
663 if (!ioarea) {
664 dev_err(&pdev->dev, "McBSP region already claimed\n");
665 return -EBUSY;
666 }
667
668 dev = kzalloc(sizeof(struct davinci_mcbsp_dev), GFP_KERNEL);
669 if (!dev) {
670 ret = -ENOMEM;
671 goto err_release_region;
672 }
Troy Kisky1e224f32009-11-18 17:49:53 -0700673 if (pdata) {
Troy Kisky0d6c9772009-11-18 17:49:51 -0700674 dev->enable_channel_combine = pdata->enable_channel_combine;
Troy Kisky1e224f32009-11-18 17:49:53 -0700675 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].sram_size =
676 pdata->sram_size_playback;
677 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].sram_size =
678 pdata->sram_size_capture;
Raffaele Recalcatiec637552010-07-06 10:39:03 +0200679 dev->clk_input_pin = pdata->clk_input_pin;
Raffaele Recalcatid9823ed92010-07-06 10:39:04 +0200680 dev->i2s_accurate_sck = pdata->i2s_accurate_sck;
Sekhar Nori48519f02010-07-19 12:31:16 +0530681 asp_chan_q = pdata->asp_chan_q;
682 ram_chan_q = pdata->ram_chan_q;
Troy Kisky1e224f32009-11-18 17:49:53 -0700683 }
Sekhar Nori48519f02010-07-19 12:31:16 +0530684
685 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].asp_chan_q = asp_chan_q;
686 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].ram_chan_q = ram_chan_q;
687 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].asp_chan_q = asp_chan_q;
688 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].ram_chan_q = ram_chan_q;
689
Kevin Hilman3e46a442009-07-15 10:42:09 -0700690 dev->clk = clk_get(&pdev->dev, NULL);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100691 if (IS_ERR(dev->clk)) {
692 ret = -ENODEV;
693 goto err_free_mem;
694 }
695 clk_enable(dev->clk);
696
697 dev->base = (void __iomem *)IO_ADDRESS(mem->start);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100698
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700699 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].dma_addr =
Vladimir Barinov310355c2008-02-18 11:40:22 +0100700 (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DXR_REG);
701
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700702 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].dma_addr =
Vladimir Barinov310355c2008-02-18 11:40:22 +0100703 (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DRR_REG);
704
Chaithrika U S5204d492009-06-05 06:28:23 -0400705 /* first TX, then RX */
706 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
707 if (!res) {
708 dev_err(&pdev->dev, "no DMA resource\n");
Chaithrika U Sefd13be2009-06-08 06:49:41 -0400709 ret = -ENXIO;
Vaibhav Bediaeef6d7b2011-02-09 18:39:53 +0530710 goto err_release_clk;
Chaithrika U S5204d492009-06-05 06:28:23 -0400711 }
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700712 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].channel = res->start;
Chaithrika U S5204d492009-06-05 06:28:23 -0400713
714 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
715 if (!res) {
716 dev_err(&pdev->dev, "no DMA resource\n");
Chaithrika U Sefd13be2009-06-08 06:49:41 -0400717 ret = -ENXIO;
Vaibhav Bediaeef6d7b2011-02-09 18:39:53 +0530718 goto err_release_clk;
Chaithrika U S5204d492009-06-05 06:28:23 -0400719 }
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700720 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].channel = res->start;
Raffaele Recalcatiec637552010-07-06 10:39:03 +0200721 dev->dev = &pdev->dev;
Chaithrika U S5204d492009-06-05 06:28:23 -0400722
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000723 dev_set_drvdata(&pdev->dev, dev);
724
725 ret = snd_soc_register_dai(&pdev->dev, &davinci_i2s_dai);
Chaithrika U S5204d492009-06-05 06:28:23 -0400726 if (ret != 0)
Vaibhav Bediaeef6d7b2011-02-09 18:39:53 +0530727 goto err_release_clk;
Chaithrika U S5204d492009-06-05 06:28:23 -0400728
Vladimir Barinov310355c2008-02-18 11:40:22 +0100729 return 0;
730
Vaibhav Bediaeef6d7b2011-02-09 18:39:53 +0530731err_release_clk:
732 clk_disable(dev->clk);
733 clk_put(dev->clk);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100734err_free_mem:
735 kfree(dev);
736err_release_region:
Vaibhav Bediad852f4462011-02-09 18:39:52 +0530737 release_mem_region(mem->start, resource_size(mem));
Vladimir Barinov310355c2008-02-18 11:40:22 +0100738
739 return ret;
740}
741
Chaithrika U S5204d492009-06-05 06:28:23 -0400742static int davinci_i2s_remove(struct platform_device *pdev)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100743{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000744 struct davinci_mcbsp_dev *dev = dev_get_drvdata(&pdev->dev);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100745 struct resource *mem;
746
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000747 snd_soc_unregister_dai(&pdev->dev);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100748 clk_disable(dev->clk);
749 clk_put(dev->clk);
750 dev->clk = NULL;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100751 kfree(dev);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100752 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Vaibhav Bediad852f4462011-02-09 18:39:52 +0530753 release_mem_region(mem->start, resource_size(mem));
Chaithrika U S5204d492009-06-05 06:28:23 -0400754
755 return 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100756}
757
Chaithrika U S5204d492009-06-05 06:28:23 -0400758static struct platform_driver davinci_mcbsp_driver = {
759 .probe = davinci_i2s_probe,
760 .remove = davinci_i2s_remove,
761 .driver = {
Chris Paulson-Ellisbedad0c2010-11-16 12:27:09 +0000762 .name = "davinci-mcbsp",
Chaithrika U S5204d492009-06-05 06:28:23 -0400763 .owner = THIS_MODULE,
764 },
Eric Miao6335d052009-03-03 09:41:00 +0800765};
766
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100767static int __init davinci_i2s_init(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000768{
Chaithrika U S5204d492009-06-05 06:28:23 -0400769 return platform_driver_register(&davinci_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000770}
771module_init(davinci_i2s_init);
772
773static void __exit davinci_i2s_exit(void)
774{
Chaithrika U S5204d492009-06-05 06:28:23 -0400775 platform_driver_unregister(&davinci_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000776}
777module_exit(davinci_i2s_exit);
778
Vladimir Barinov310355c2008-02-18 11:40:22 +0100779MODULE_AUTHOR("Vladimir Barinov");
780MODULE_DESCRIPTION("TI DAVINCI I2S (McBSP) SoC Interface");
781MODULE_LICENSE("GPL");