blob: a3206e65e5e54d3abc84eea1f66fd02cd512e0bb [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 *
Petr Kulhavy5f9a50c2016-04-18 14:32:41 +02007 * DT support (c) 2016 Petr Kulhavy, Barix AG <petr@barix.com>
8 * based on davinci-mcasp.c DT support
9 *
Vladimir Barinov310355c2008-02-18 11:40:22 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
Petr Kulhavy5f9a50c2016-04-18 14:32:41 +020013 *
14 * TODO:
15 * on DA850 implement HW FIFOs instead of DMA into DXR and DRR registers
Vladimir Barinov310355c2008-02-18 11:40:22 +010016 */
17
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Vladimir Barinov310355c2008-02-18 11:40:22 +010022#include <linux/delay.h>
23#include <linux/io.h>
24#include <linux/clk.h>
Hebbar, Gururaja896f66b2012-08-27 18:56:41 +053025#include <linux/platform_data/davinci_asp.h>
Vladimir Barinov310355c2008-02-18 11:40:22 +010026
27#include <sound/core.h>
28#include <sound/pcm.h>
29#include <sound/pcm_params.h>
30#include <sound/initval.h>
31#include <sound/soc.h>
Peter Ujfalusi257ade72015-03-03 16:45:18 +020032#include <sound/dmaengine_pcm.h>
Vladimir Barinov310355c2008-02-18 11:40:22 +010033
Peter Ujfalusi257ade72015-03-03 16:45:18 +020034#include "edma-pcm.h"
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +020035#include "davinci-i2s.h"
Vladimir Barinov310355c2008-02-18 11:40:22 +010036
Kuninori Morimotoa49cb312018-01-29 02:46:04 +000037#define DRV_NAME "davinci-i2s"
David Brownella62114c2009-05-14 12:47:42 -070038
39/*
40 * NOTE: terminology here is confusing.
41 *
42 * - This driver supports the "Audio Serial Port" (ASP),
43 * found on dm6446, dm355, and other DaVinci chips.
44 *
45 * - But it labels it a "Multi-channel Buffered Serial Port"
46 * (McBSP) as on older chips like the dm642 ... which was
47 * backward-compatible, possibly explaining that confusion.
48 *
49 * - OMAP chips have a controller called McBSP, which is
50 * incompatible with the DaVinci flavor of McBSP.
51 *
52 * - Newer DaVinci chips have a controller called McASP,
53 * incompatible with ASP and with either McBSP.
54 *
55 * In short: this uses ASP to implement I2S, not McBSP.
56 * And it won't be the only DaVinci implemention of I2S.
57 */
Vladimir Barinov310355c2008-02-18 11:40:22 +010058#define DAVINCI_MCBSP_DRR_REG 0x00
59#define DAVINCI_MCBSP_DXR_REG 0x04
60#define DAVINCI_MCBSP_SPCR_REG 0x08
61#define DAVINCI_MCBSP_RCR_REG 0x0c
62#define DAVINCI_MCBSP_XCR_REG 0x10
63#define DAVINCI_MCBSP_SRGR_REG 0x14
64#define DAVINCI_MCBSP_PCR_REG 0x24
65
66#define DAVINCI_MCBSP_SPCR_RRST (1 << 0)
67#define DAVINCI_MCBSP_SPCR_RINTM(v) ((v) << 4)
68#define DAVINCI_MCBSP_SPCR_XRST (1 << 16)
69#define DAVINCI_MCBSP_SPCR_XINTM(v) ((v) << 20)
70#define DAVINCI_MCBSP_SPCR_GRST (1 << 22)
71#define DAVINCI_MCBSP_SPCR_FRST (1 << 23)
72#define DAVINCI_MCBSP_SPCR_FREE (1 << 25)
73
74#define DAVINCI_MCBSP_RCR_RWDLEN1(v) ((v) << 5)
75#define DAVINCI_MCBSP_RCR_RFRLEN1(v) ((v) << 8)
76#define DAVINCI_MCBSP_RCR_RDATDLY(v) ((v) << 16)
Troy Kiskyf5cfa952009-07-04 19:29:57 -070077#define DAVINCI_MCBSP_RCR_RFIG (1 << 18)
Vladimir Barinov310355c2008-02-18 11:40:22 +010078#define DAVINCI_MCBSP_RCR_RWDLEN2(v) ((v) << 21)
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +020079#define DAVINCI_MCBSP_RCR_RFRLEN2(v) ((v) << 24)
80#define DAVINCI_MCBSP_RCR_RPHASE BIT(31)
Vladimir Barinov310355c2008-02-18 11:40:22 +010081
82#define DAVINCI_MCBSP_XCR_XWDLEN1(v) ((v) << 5)
83#define DAVINCI_MCBSP_XCR_XFRLEN1(v) ((v) << 8)
84#define DAVINCI_MCBSP_XCR_XDATDLY(v) ((v) << 16)
85#define DAVINCI_MCBSP_XCR_XFIG (1 << 18)
86#define DAVINCI_MCBSP_XCR_XWDLEN2(v) ((v) << 21)
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +020087#define DAVINCI_MCBSP_XCR_XFRLEN2(v) ((v) << 24)
88#define DAVINCI_MCBSP_XCR_XPHASE BIT(31)
Vladimir Barinov310355c2008-02-18 11:40:22 +010089
90#define DAVINCI_MCBSP_SRGR_FWID(v) ((v) << 8)
91#define DAVINCI_MCBSP_SRGR_FPER(v) ((v) << 16)
92#define DAVINCI_MCBSP_SRGR_FSGM (1 << 28)
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +020093#define DAVINCI_MCBSP_SRGR_CLKSM BIT(29)
Vladimir Barinov310355c2008-02-18 11:40:22 +010094
95#define DAVINCI_MCBSP_PCR_CLKRP (1 << 0)
96#define DAVINCI_MCBSP_PCR_CLKXP (1 << 1)
97#define DAVINCI_MCBSP_PCR_FSRP (1 << 2)
98#define DAVINCI_MCBSP_PCR_FSXP (1 << 3)
Hugo Villeneuveb402dff2008-11-08 13:26:09 -050099#define DAVINCI_MCBSP_PCR_SCLKME (1 << 7)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100100#define DAVINCI_MCBSP_PCR_CLKRM (1 << 8)
101#define DAVINCI_MCBSP_PCR_CLKXM (1 << 9)
102#define DAVINCI_MCBSP_PCR_FSRM (1 << 10)
103#define DAVINCI_MCBSP_PCR_FSXM (1 << 11)
104
Vladimir Barinov310355c2008-02-18 11:40:22 +0100105enum {
106 DAVINCI_MCBSP_WORD_8 = 0,
107 DAVINCI_MCBSP_WORD_12,
108 DAVINCI_MCBSP_WORD_16,
109 DAVINCI_MCBSP_WORD_20,
110 DAVINCI_MCBSP_WORD_24,
111 DAVINCI_MCBSP_WORD_32,
112};
113
Troy Kisky0d6c9772009-11-18 17:49:51 -0700114static const unsigned char data_type[SNDRV_PCM_FORMAT_S32_LE + 1] = {
115 [SNDRV_PCM_FORMAT_S8] = 1,
116 [SNDRV_PCM_FORMAT_S16_LE] = 2,
117 [SNDRV_PCM_FORMAT_S32_LE] = 4,
118};
119
120static const unsigned char asp_word_length[SNDRV_PCM_FORMAT_S32_LE + 1] = {
121 [SNDRV_PCM_FORMAT_S8] = DAVINCI_MCBSP_WORD_8,
122 [SNDRV_PCM_FORMAT_S16_LE] = DAVINCI_MCBSP_WORD_16,
123 [SNDRV_PCM_FORMAT_S32_LE] = DAVINCI_MCBSP_WORD_32,
124};
125
126static const unsigned char double_fmt[SNDRV_PCM_FORMAT_S32_LE + 1] = {
127 [SNDRV_PCM_FORMAT_S8] = SNDRV_PCM_FORMAT_S16_LE,
128 [SNDRV_PCM_FORMAT_S16_LE] = SNDRV_PCM_FORMAT_S32_LE,
129};
130
Vladimir Barinov310355c2008-02-18 11:40:22 +0100131struct davinci_mcbsp_dev {
Raffaele Recalcatiec637552010-07-06 10:39:03 +0200132 struct device *dev;
Peter Ujfalusi257ade72015-03-03 16:45:18 +0200133 struct snd_dmaengine_dai_dma_data dma_data[2];
134 int dma_request[2];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100135 void __iomem *base;
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700136#define MOD_DSP_A 0
137#define MOD_DSP_B 1
138 int mode;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700139 u32 pcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100140 struct clk *clk;
Troy Kisky0d6c9772009-11-18 17:49:51 -0700141 /*
142 * Combining both channels into 1 element will at least double the
143 * amount of time between servicing the dma channel, increase
144 * effiency, and reduce the chance of overrun/underrun. But,
145 * it will result in the left & right channels being swapped.
146 *
147 * If relabeling the left and right channels is not possible,
148 * you may want to let the codec know to swap them back.
149 *
150 * It may allow x10 the amount of time to service dma requests,
151 * if the codec is master and is using an unnecessarily fast bit clock
152 * (ie. tlvaic23b), independent of the sample rate. So, having an
153 * entire frame at once means it can be serviced at the sample rate
154 * instead of the bit clock rate.
155 *
156 * In the now unlikely case that an underrun still
157 * occurs, both the left and right samples will be repeated
158 * so that no pops are heard, and the left and right channels
159 * won't end up being swapped because of the underrun.
160 */
161 unsigned enable_channel_combine:1;
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200162
163 unsigned int fmt;
164 int clk_div;
Raffaele Recalcatiec637552010-07-06 10:39:03 +0200165 int clk_input_pin;
Raffaele Recalcatid9823ed92010-07-06 10:39:04 +0200166 bool i2s_accurate_sck;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100167};
168
169static inline void davinci_mcbsp_write_reg(struct davinci_mcbsp_dev *dev,
170 int reg, u32 val)
171{
172 __raw_writel(val, dev->base + reg);
173}
174
175static inline u32 davinci_mcbsp_read_reg(struct davinci_mcbsp_dev *dev, int reg)
176{
177 return __raw_readl(dev->base + reg);
178}
179
Troy Kiskyc392bec2009-07-04 19:29:52 -0700180static void toggle_clock(struct davinci_mcbsp_dev *dev, int playback)
181{
182 u32 m = playback ? DAVINCI_MCBSP_PCR_CLKXP : DAVINCI_MCBSP_PCR_CLKRP;
183 /* The clock needs to toggle to complete reset.
184 * So, fake it by toggling the clk polarity.
185 */
186 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr ^ m);
187 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr);
188}
189
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700190static void davinci_mcbsp_start(struct davinci_mcbsp_dev *dev,
191 struct snd_pcm_substream *substream)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100192{
193 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotoa49cb312018-01-29 02:46:04 +0000194 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700195 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Troy Kisky35cf6352009-07-04 19:29:51 -0700196 u32 spcr;
Troy Kiskyc392bec2009-07-04 19:29:52 -0700197 u32 mask = playback ? DAVINCI_MCBSP_SPCR_XRST : DAVINCI_MCBSP_SPCR_RRST;
Troy Kisky35cf6352009-07-04 19:29:51 -0700198 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700199 if (spcr & mask) {
200 /* start off disabled */
201 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG,
202 spcr & ~mask);
203 toggle_clock(dev, playback);
204 }
Troy Kisky1bef4492009-07-04 19:29:55 -0700205 if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM |
206 DAVINCI_MCBSP_PCR_CLKXM | DAVINCI_MCBSP_PCR_CLKRM)) {
207 /* Start the sample generator */
208 spcr |= DAVINCI_MCBSP_SPCR_GRST;
209 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
210 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100211
Troy Kisky1bef4492009-07-04 19:29:55 -0700212 if (playback) {
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530213 /* Stop the DMA to avoid data loss */
214 /* while the transmitter is out of reset to handle XSYNCERR */
Kuninori Morimotoa49cb312018-01-29 02:46:04 +0000215 if (component->driver->ops->trigger) {
216 int ret = component->driver->ops->trigger(substream,
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530217 SNDRV_PCM_TRIGGER_STOP);
218 if (ret < 0)
219 printk(KERN_DEBUG "Playback DMA stop failed\n");
220 }
221
222 /* Enable the transmitter */
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);
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530226
227 /* wait for any unexpected frame sync error to occur */
228 udelay(100);
229
230 /* Disable the transmitter to clear any outstanding XSYNCERR */
Troy Kisky35cf6352009-07-04 19:29:51 -0700231 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
232 spcr &= ~DAVINCI_MCBSP_SPCR_XRST;
233 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700234 toggle_clock(dev, playback);
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530235
236 /* Restart the DMA */
Kuninori Morimotoa49cb312018-01-29 02:46:04 +0000237 if (component->driver->ops->trigger) {
238 int ret = component->driver->ops->trigger(substream,
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530239 SNDRV_PCM_TRIGGER_START);
240 if (ret < 0)
241 printk(KERN_DEBUG "Playback DMA start failed\n");
242 }
Naresh Medisettyfb0ef642008-11-12 10:26:31 +0530243 }
244
Troy Kisky1bef4492009-07-04 19:29:55 -0700245 /* Enable transmitter or receiver */
Troy Kisky35cf6352009-07-04 19:29:51 -0700246 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Troy Kisky1bef4492009-07-04 19:29:55 -0700247 spcr |= mask;
248
249 if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM)) {
250 /* Start frame sync */
251 spcr |= DAVINCI_MCBSP_SPCR_FRST;
252 }
Troy Kisky35cf6352009-07-04 19:29:51 -0700253 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100254}
255
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700256static void davinci_mcbsp_stop(struct davinci_mcbsp_dev *dev, int playback)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100257{
Troy Kisky35cf6352009-07-04 19:29:51 -0700258 u32 spcr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100259
260 /* Reset transmitter/receiver and sample rate/frame sync generators */
Troy Kisky35cf6352009-07-04 19:29:51 -0700261 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
262 spcr &= ~(DAVINCI_MCBSP_SPCR_GRST | DAVINCI_MCBSP_SPCR_FRST);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700263 spcr &= playback ? ~DAVINCI_MCBSP_SPCR_XRST : ~DAVINCI_MCBSP_SPCR_RRST;
Troy Kisky35cf6352009-07-04 19:29:51 -0700264 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700265 toggle_clock(dev, playback);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100266}
267
Troy Kisky21903c12008-12-18 12:36:43 -0700268#define DEFAULT_BITPERSAMPLE 16
269
Liam Girdwood9cb132d2008-07-07 16:07:42 +0100270static int davinci_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
Vladimir Barinov310355c2008-02-18 11:40:22 +0100271 unsigned int fmt)
272{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000273 struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
Troy Kisky21903c12008-12-18 12:36:43 -0700274 unsigned int pcr;
275 unsigned int srgr;
Jarkko Nikulaad51f762011-09-30 10:55:33 +0300276 bool inv_fs = false;
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200277 /* Attention srgr is updated by hw_params! */
Troy Kisky21903c12008-12-18 12:36:43 -0700278 srgr = DAVINCI_MCBSP_SRGR_FSGM |
279 DAVINCI_MCBSP_SRGR_FPER(DEFAULT_BITPERSAMPLE * 2 - 1) |
280 DAVINCI_MCBSP_SRGR_FWID(DEFAULT_BITPERSAMPLE - 1);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100281
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200282 dev->fmt = fmt;
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700283 /* set master/slave audio interface */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100284 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
285 case SND_SOC_DAIFMT_CBS_CFS:
Troy Kisky21903c12008-12-18 12:36:43 -0700286 /* cpu is master */
287 pcr = DAVINCI_MCBSP_PCR_FSXM |
288 DAVINCI_MCBSP_PCR_FSRM |
289 DAVINCI_MCBSP_PCR_CLKXM |
290 DAVINCI_MCBSP_PCR_CLKRM;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100291 break;
Hugo Villeneuveb402dff2008-11-08 13:26:09 -0500292 case SND_SOC_DAIFMT_CBM_CFS:
Raffaele Recalcatiec637552010-07-06 10:39:03 +0200293 pcr = DAVINCI_MCBSP_PCR_FSRM | DAVINCI_MCBSP_PCR_FSXM;
294 /*
295 * Selection of the clock input pin that is the
296 * input for the Sample Rate Generator.
297 * McBSP FSR and FSX are driven by the Sample Rate
298 * Generator.
299 */
300 switch (dev->clk_input_pin) {
301 case MCBSP_CLKS:
302 pcr |= DAVINCI_MCBSP_PCR_CLKXM |
303 DAVINCI_MCBSP_PCR_CLKRM;
304 break;
305 case MCBSP_CLKR:
306 pcr |= DAVINCI_MCBSP_PCR_SCLKME;
307 break;
308 default:
309 dev_err(dev->dev, "bad clk_input_pin\n");
310 return -EINVAL;
311 }
312
Hugo Villeneuveb402dff2008-11-08 13:26:09 -0500313 break;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100314 case SND_SOC_DAIFMT_CBM_CFM:
Troy Kisky21903c12008-12-18 12:36:43 -0700315 /* codec is master */
316 pcr = 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100317 break;
318 default:
Troy Kisky21903c12008-12-18 12:36:43 -0700319 printk(KERN_ERR "%s:bad master\n", __func__);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100320 return -EINVAL;
321 }
322
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700323 /* interface format */
Troy Kisky69ab8202008-12-18 12:36:44 -0700324 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Troy Kisky69ab8202008-12-18 12:36:44 -0700325 case SND_SOC_DAIFMT_I2S:
Troy Kisky07d8d9d2008-12-19 13:05:24 -0700326 /* Davinci doesn't support TRUE I2S, but some codecs will have
327 * the left and right channels contiguous. This allows
328 * dsp_a mode to be used with an inverted normal frame clk.
329 * If your codec is master and does not have contiguous
330 * channels, then you will have sound on only one channel.
331 * Try using a different mode, or codec as slave.
332 *
333 * The TLV320AIC33 is an example of a codec where this works.
334 * It has a variable bit clock frequency allowing it to have
335 * valid data on every bit clock.
336 *
337 * The TLV320AIC23 is an example of a codec where this does not
338 * work. It has a fixed bit clock frequency with progressively
339 * more empty bit clock slots between channels as the sample
340 * rate is lowered.
341 */
Jarkko Nikulaad51f762011-09-30 10:55:33 +0300342 inv_fs = true;
Gustavo A. R. Silva3b7c88f2018-08-03 11:28:24 -0500343 /* fall through */
Troy Kisky07d8d9d2008-12-19 13:05:24 -0700344 case SND_SOC_DAIFMT_DSP_A:
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700345 dev->mode = MOD_DSP_A;
346 break;
347 case SND_SOC_DAIFMT_DSP_B:
348 dev->mode = MOD_DSP_B;
Troy Kisky69ab8202008-12-18 12:36:44 -0700349 break;
350 default:
351 printk(KERN_ERR "%s:bad format\n", __func__);
352 return -EINVAL;
353 }
354
Vladimir Barinov310355c2008-02-18 11:40:22 +0100355 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
Troy Kisky9e031622008-12-19 13:05:23 -0700356 case SND_SOC_DAIFMT_NB_NF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700357 /* CLKRP Receive clock polarity,
358 * 1 - sampled on rising edge of CLKR
359 * valid on rising edge
360 * CLKXP Transmit clock polarity,
361 * 1 - clocked on falling edge of CLKX
362 * valid on rising edge
363 * FSRP Receive frame sync pol, 0 - active high
364 * FSXP Transmit frame sync pol, 0 - active high
365 */
Troy Kisky21903c12008-12-18 12:36:43 -0700366 pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100367 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700368 case SND_SOC_DAIFMT_IB_IF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700369 /* CLKRP Receive clock polarity,
370 * 0 - sampled on falling edge of CLKR
371 * valid on falling edge
372 * CLKXP Transmit clock polarity,
373 * 0 - clocked on rising edge of CLKX
374 * valid on falling edge
375 * FSRP Receive frame sync pol, 1 - active low
376 * FSXP Transmit frame sync pol, 1 - active low
377 */
Troy Kisky21903c12008-12-18 12:36:43 -0700378 pcr |= (DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100379 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700380 case SND_SOC_DAIFMT_NB_IF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700381 /* CLKRP Receive clock polarity,
382 * 1 - sampled on rising edge of CLKR
383 * valid on rising edge
384 * CLKXP Transmit clock polarity,
385 * 1 - clocked on falling edge of CLKX
386 * valid on rising edge
387 * FSRP Receive frame sync pol, 1 - active low
388 * FSXP Transmit frame sync pol, 1 - active low
389 */
Troy Kisky21903c12008-12-18 12:36:43 -0700390 pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP |
391 DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100392 break;
Troy Kisky9e031622008-12-19 13:05:23 -0700393 case SND_SOC_DAIFMT_IB_NF:
Troy Kisky664b4af2008-12-18 12:36:41 -0700394 /* CLKRP Receive clock polarity,
395 * 0 - sampled on falling edge of CLKR
396 * valid on falling edge
397 * CLKXP Transmit clock polarity,
398 * 0 - clocked on rising edge of CLKX
399 * valid on falling edge
400 * FSRP Receive frame sync pol, 0 - active high
401 * FSXP Transmit frame sync pol, 0 - active high
402 */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100403 break;
404 default:
405 return -EINVAL;
406 }
Jarkko Nikulaad51f762011-09-30 10:55:33 +0300407 if (inv_fs == true)
408 pcr ^= (DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
Troy Kisky21903c12008-12-18 12:36:43 -0700409 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
Troy Kiskyc392bec2009-07-04 19:29:52 -0700410 dev->pcr = pcr;
Troy Kisky21903c12008-12-18 12:36:43 -0700411 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, pcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100412 return 0;
413}
414
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200415static int davinci_i2s_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
416 int div_id, int div)
417{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000418 struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200419
420 if (div_id != DAVINCI_MCBSP_CLKGDV)
421 return -ENODEV;
422
423 dev->clk_div = div;
424 return 0;
425}
426
Vladimir Barinov310355c2008-02-18 11:40:22 +0100427static int davinci_i2s_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000428 struct snd_pcm_hw_params *params,
429 struct snd_soc_dai *dai)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100430{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000431 struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100432 struct snd_interval *i = NULL;
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200433 int mcbsp_word_length, master;
434 unsigned int rcr, xcr, srgr, clk_div, freq, framesize;
Troy Kisky35cf6352009-07-04 19:29:51 -0700435 u32 spcr;
Troy Kisky0d6c9772009-11-18 17:49:51 -0700436 snd_pcm_format_t fmt;
437 unsigned element_cnt = 1;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100438
439 /* general line settings */
Troy Kisky35cf6352009-07-04 19:29:51 -0700440 spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530441 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Troy Kisky35cf6352009-07-04 19:29:51 -0700442 spcr |= DAVINCI_MCBSP_SPCR_RINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
443 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530444 } else {
Troy Kisky35cf6352009-07-04 19:29:51 -0700445 spcr |= DAVINCI_MCBSP_SPCR_XINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
446 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
Naresh Medisettycb6e2062008-11-18 11:01:03 +0530447 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100448
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200449 master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
450 fmt = params_format(params);
451 mcbsp_word_length = asp_word_length[fmt];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100452
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200453 switch (master) {
454 case SND_SOC_DAIFMT_CBS_CFS:
455 freq = clk_get_rate(dev->clk);
456 srgr = DAVINCI_MCBSP_SRGR_FSGM |
457 DAVINCI_MCBSP_SRGR_CLKSM;
458 srgr |= DAVINCI_MCBSP_SRGR_FWID(mcbsp_word_length *
459 8 - 1);
Raffaele Recalcatid9823ed92010-07-06 10:39:04 +0200460 if (dev->i2s_accurate_sck) {
461 clk_div = 256;
462 do {
463 framesize = (freq / (--clk_div)) /
464 params->rate_num *
465 params->rate_den;
466 } while (((framesize < 33) || (framesize > 4095)) &&
467 (clk_div));
468 clk_div--;
469 srgr |= DAVINCI_MCBSP_SRGR_FPER(framesize - 1);
470 } else {
471 /* symmetric waveforms */
472 clk_div = freq / (mcbsp_word_length * 16) /
473 params->rate_num * params->rate_den;
474 srgr |= DAVINCI_MCBSP_SRGR_FPER(mcbsp_word_length *
475 16 - 1);
476 }
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200477 clk_div &= 0xFF;
478 srgr |= clk_div;
479 break;
480 case SND_SOC_DAIFMT_CBM_CFS:
481 srgr = DAVINCI_MCBSP_SRGR_FSGM;
482 clk_div = dev->clk_div - 1;
483 srgr |= DAVINCI_MCBSP_SRGR_FWID(mcbsp_word_length * 8 - 1);
484 srgr |= DAVINCI_MCBSP_SRGR_FPER(mcbsp_word_length * 16 - 1);
485 clk_div &= 0xFF;
486 srgr |= clk_div;
487 break;
488 case SND_SOC_DAIFMT_CBM_CFM:
489 /* Clock and frame sync given from external sources */
490 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
491 srgr = DAVINCI_MCBSP_SRGR_FSGM;
492 srgr |= DAVINCI_MCBSP_SRGR_FWID(snd_interval_value(i) - 1);
493 pr_debug("%s - %d FWID set: re-read srgr = %X\n",
494 __func__, __LINE__, snd_interval_value(i) - 1);
495
496 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_FRAME_BITS);
497 srgr |= DAVINCI_MCBSP_SRGR_FPER(snd_interval_value(i) - 1);
498 break;
499 default:
500 return -EINVAL;
501 }
Troy Kisky35cf6352009-07-04 19:29:51 -0700502 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100503
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700504 rcr = DAVINCI_MCBSP_RCR_RFIG;
505 xcr = DAVINCI_MCBSP_XCR_XFIG;
506 if (dev->mode == MOD_DSP_B) {
507 rcr |= DAVINCI_MCBSP_RCR_RDATDLY(0);
508 xcr |= DAVINCI_MCBSP_XCR_XDATDLY(0);
509 } else {
510 rcr |= DAVINCI_MCBSP_RCR_RDATDLY(1);
511 xcr |= DAVINCI_MCBSP_XCR_XDATDLY(1);
512 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100513 /* Determine xfer data type */
Troy Kisky0d6c9772009-11-18 17:49:51 -0700514 fmt = params_format(params);
515 if ((fmt > SNDRV_PCM_FORMAT_S32_LE) || !data_type[fmt]) {
Jean Delvare9b6e12e2008-08-26 15:47:55 +0200516 printk(KERN_WARNING "davinci-i2s: unsupported PCM format\n");
Vladimir Barinov310355c2008-02-18 11:40:22 +0100517 return -EINVAL;
518 }
519
Troy Kisky0d6c9772009-11-18 17:49:51 -0700520 if (params_channels(params) == 2) {
521 element_cnt = 2;
522 if (double_fmt[fmt] && dev->enable_channel_combine) {
523 element_cnt = 1;
524 fmt = double_fmt[fmt];
525 }
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200526 switch (master) {
527 case SND_SOC_DAIFMT_CBS_CFS:
528 case SND_SOC_DAIFMT_CBS_CFM:
529 rcr |= DAVINCI_MCBSP_RCR_RFRLEN2(0);
530 xcr |= DAVINCI_MCBSP_XCR_XFRLEN2(0);
531 rcr |= DAVINCI_MCBSP_RCR_RPHASE;
532 xcr |= DAVINCI_MCBSP_XCR_XPHASE;
533 break;
534 case SND_SOC_DAIFMT_CBM_CFM:
535 case SND_SOC_DAIFMT_CBM_CFS:
536 rcr |= DAVINCI_MCBSP_RCR_RFRLEN2(element_cnt - 1);
537 xcr |= DAVINCI_MCBSP_XCR_XFRLEN2(element_cnt - 1);
538 break;
539 default:
540 return -EINVAL;
541 }
Troy Kisky0d6c9772009-11-18 17:49:51 -0700542 }
Troy Kisky0d6c9772009-11-18 17:49:51 -0700543 mcbsp_word_length = asp_word_length[fmt];
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200544
545 switch (master) {
546 case SND_SOC_DAIFMT_CBS_CFS:
547 case SND_SOC_DAIFMT_CBS_CFM:
548 rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(0);
549 xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(0);
550 break;
551 case SND_SOC_DAIFMT_CBM_CFM:
552 case SND_SOC_DAIFMT_CBM_CFS:
553 rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(element_cnt - 1);
554 xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(element_cnt - 1);
555 break;
556 default:
557 return -EINVAL;
558 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100559
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700560 rcr |= DAVINCI_MCBSP_RCR_RWDLEN1(mcbsp_word_length) |
561 DAVINCI_MCBSP_RCR_RWDLEN2(mcbsp_word_length);
562 xcr |= DAVINCI_MCBSP_XCR_XWDLEN1(mcbsp_word_length) |
563 DAVINCI_MCBSP_XCR_XWDLEN2(mcbsp_word_length);
564
565 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Troy Kisky35cf6352009-07-04 19:29:51 -0700566 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_XCR_REG, xcr);
Troy Kiskyf5cfa952009-07-04 19:29:57 -0700567 else
568 davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_RCR_REG, rcr);
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200569
570 pr_debug("%s - %d srgr=%X\n", __func__, __LINE__, srgr);
571 pr_debug("%s - %d xcr=%X\n", __func__, __LINE__, xcr);
572 pr_debug("%s - %d rcr=%X\n", __func__, __LINE__, rcr);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100573 return 0;
574}
575
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700576static int davinci_i2s_prepare(struct snd_pcm_substream *substream,
577 struct snd_soc_dai *dai)
578{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000579 struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700580 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
581 davinci_mcbsp_stop(dev, playback);
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700582 return 0;
583}
584
Mark Browndee89c42008-11-18 22:11:38 +0000585static int davinci_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
586 struct snd_soc_dai *dai)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100587{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588 struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100589 int ret = 0;
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700590 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100591
592 switch (cmd) {
593 case SNDRV_PCM_TRIGGER_START:
594 case SNDRV_PCM_TRIGGER_RESUME:
595 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700596 davinci_mcbsp_start(dev, substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100597 break;
598 case SNDRV_PCM_TRIGGER_STOP:
599 case SNDRV_PCM_TRIGGER_SUSPEND:
600 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Troy Kiskyf9af37c2009-07-04 19:29:53 -0700601 davinci_mcbsp_stop(dev, playback);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100602 break;
603 default:
604 ret = -EINVAL;
605 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100606 return ret;
607}
608
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700609static void davinci_i2s_shutdown(struct snd_pcm_substream *substream,
610 struct snd_soc_dai *dai)
611{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612 struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
Troy Kiskyaf0adf32009-07-04 19:29:59 -0700613 int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
614 davinci_mcbsp_stop(dev, playback);
615}
616
Chaithrika U S5204d492009-06-05 06:28:23 -0400617#define DAVINCI_I2S_RATES SNDRV_PCM_RATE_8000_96000
618
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100619static const struct snd_soc_dai_ops davinci_i2s_dai_ops = {
Mark Brown3f405b42009-07-07 19:18:46 +0100620 .shutdown = davinci_i2s_shutdown,
621 .prepare = davinci_i2s_prepare,
Chaithrika U S5204d492009-06-05 06:28:23 -0400622 .trigger = davinci_i2s_trigger,
623 .hw_params = davinci_i2s_hw_params,
624 .set_fmt = davinci_i2s_set_dai_fmt,
Raffaele Recalcatia4c8ea22010-07-06 10:39:02 +0200625 .set_clkdiv = davinci_i2s_dai_set_clkdiv,
Chaithrika U S5204d492009-06-05 06:28:23 -0400626
627};
628
Peter Ujfalusi257ade72015-03-03 16:45:18 +0200629static int davinci_i2s_dai_probe(struct snd_soc_dai *dai)
630{
631 struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
632
633 dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
634 dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
635
636 return 0;
637}
638
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639static struct snd_soc_dai_driver davinci_i2s_dai = {
Peter Ujfalusi257ade72015-03-03 16:45:18 +0200640 .probe = davinci_i2s_dai_probe,
Chaithrika U S5204d492009-06-05 06:28:23 -0400641 .playback = {
642 .channels_min = 2,
643 .channels_max = 2,
644 .rates = DAVINCI_I2S_RATES,
645 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
646 .capture = {
647 .channels_min = 2,
648 .channels_max = 2,
649 .rates = DAVINCI_I2S_RATES,
650 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
651 .ops = &davinci_i2s_dai_ops,
652
653};
Chaithrika U S5204d492009-06-05 06:28:23 -0400654
Kuninori Morimotobfcb9212013-03-21 03:30:54 -0700655static const struct snd_soc_component_driver davinci_i2s_component = {
Kuninori Morimotoa49cb312018-01-29 02:46:04 +0000656 .name = DRV_NAME,
Kuninori Morimotobfcb9212013-03-21 03:30:54 -0700657};
658
Chaithrika U S5204d492009-06-05 06:28:23 -0400659static int davinci_i2s_probe(struct platform_device *pdev)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100660{
Petr Kulhavy5f9a50c2016-04-18 14:32:41 +0200661 struct snd_dmaengine_dai_dma_data *dma_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100662 struct davinci_mcbsp_dev *dev;
Axel Lin508a43f2015-08-24 16:47:36 +0800663 struct resource *mem, *res;
664 void __iomem *io_base;
Peter Ujfalusi257ade72015-03-03 16:45:18 +0200665 int *dma;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100666 int ret;
667
Petr Kulhavy5f9a50c2016-04-18 14:32:41 +0200668 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
669 if (!mem) {
670 dev_warn(&pdev->dev,
671 "\"mpu\" mem resource not found, using index 0\n");
672 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
673 if (!mem) {
674 dev_err(&pdev->dev, "no mem resource?\n");
675 return -ENODEV;
676 }
677 }
678
Axel Lin508a43f2015-08-24 16:47:36 +0800679 io_base = devm_ioremap_resource(&pdev->dev, mem);
680 if (IS_ERR(io_base))
681 return PTR_ERR(io_base);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100682
Julia Lawallcd0ff7e2011-12-29 17:51:22 +0100683 dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcbsp_dev),
684 GFP_KERNEL);
685 if (!dev)
686 return -ENOMEM;
Sekhar Nori48519f02010-07-19 12:31:16 +0530687
Petr Kulhavy5f9a50c2016-04-18 14:32:41 +0200688 dev->base = io_base;
689
690 /* setup DMA, first TX, then RX */
691 dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
692 dma_data->addr = (dma_addr_t)(mem->start + DAVINCI_MCBSP_DXR_REG);
693
694 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
695 if (res) {
696 dma = &dev->dma_request[SNDRV_PCM_STREAM_PLAYBACK];
697 *dma = res->start;
698 dma_data->filter_data = dma;
699 } else if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
700 dma_data->filter_data = "tx";
701 } else {
702 dev_err(&pdev->dev, "Missing DMA tx resource\n");
703 return -ENODEV;
704 }
705
706 dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
707 dma_data->addr = (dma_addr_t)(mem->start + DAVINCI_MCBSP_DRR_REG);
708
709 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
710 if (res) {
711 dma = &dev->dma_request[SNDRV_PCM_STREAM_CAPTURE];
712 *dma = res->start;
713 dma_data->filter_data = dma;
714 } else if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
715 dma_data->filter_data = "rx";
716 } else {
717 dev_err(&pdev->dev, "Missing DMA rx resource\n");
718 return -ENODEV;
719 }
720
Kevin Hilman3e46a442009-07-15 10:42:09 -0700721 dev->clk = clk_get(&pdev->dev, NULL);
Julia Lawallcd0ff7e2011-12-29 17:51:22 +0100722 if (IS_ERR(dev->clk))
723 return -ENODEV;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100724 clk_enable(dev->clk);
725
Peter Ujfalusi257ade72015-03-03 16:45:18 +0200726 dev->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000727 dev_set_drvdata(&pdev->dev, dev);
728
Kuninori Morimotobfcb9212013-03-21 03:30:54 -0700729 ret = snd_soc_register_component(&pdev->dev, &davinci_i2s_component,
730 &davinci_i2s_dai, 1);
Chaithrika U S5204d492009-06-05 06:28:23 -0400731 if (ret != 0)
Julia Lawallcd0ff7e2011-12-29 17:51:22 +0100732 goto err_release_clk;
Chaithrika U S5204d492009-06-05 06:28:23 -0400733
Peter Ujfalusi257ade72015-03-03 16:45:18 +0200734 ret = edma_pcm_platform_register(&pdev->dev);
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +0530735 if (ret) {
736 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
Kuninori Morimotobfcb9212013-03-21 03:30:54 -0700737 goto err_unregister_component;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +0530738 }
739
Vladimir Barinov310355c2008-02-18 11:40:22 +0100740 return 0;
741
Kuninori Morimotobfcb9212013-03-21 03:30:54 -0700742err_unregister_component:
743 snd_soc_unregister_component(&pdev->dev);
Vaibhav Bediaeef6d7b2011-02-09 18:39:53 +0530744err_release_clk:
745 clk_disable(dev->clk);
746 clk_put(dev->clk);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100747 return ret;
748}
749
Chaithrika U S5204d492009-06-05 06:28:23 -0400750static int davinci_i2s_remove(struct platform_device *pdev)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100751{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000752 struct davinci_mcbsp_dev *dev = dev_get_drvdata(&pdev->dev);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100753
Kuninori Morimotobfcb9212013-03-21 03:30:54 -0700754 snd_soc_unregister_component(&pdev->dev);
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +0530755
Vladimir Barinov310355c2008-02-18 11:40:22 +0100756 clk_disable(dev->clk);
757 clk_put(dev->clk);
758 dev->clk = NULL;
Chaithrika U S5204d492009-06-05 06:28:23 -0400759
760 return 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100761}
762
Petr Kulhavy5f9a50c2016-04-18 14:32:41 +0200763static const struct of_device_id davinci_i2s_match[] = {
764 { .compatible = "ti,da850-mcbsp" },
765 {},
766};
767MODULE_DEVICE_TABLE(of, davinci_i2s_match);
768
Chaithrika U S5204d492009-06-05 06:28:23 -0400769static struct platform_driver davinci_mcbsp_driver = {
770 .probe = davinci_i2s_probe,
771 .remove = davinci_i2s_remove,
772 .driver = {
Chris Paulson-Ellisbedad0c2010-11-16 12:27:09 +0000773 .name = "davinci-mcbsp",
Petr Kulhavy5f9a50c2016-04-18 14:32:41 +0200774 .of_match_table = of_match_ptr(davinci_i2s_match),
Chaithrika U S5204d492009-06-05 06:28:23 -0400775 },
Eric Miao6335d052009-03-03 09:41:00 +0800776};
777
Axel Linf9b8a512011-11-25 10:09:27 +0800778module_platform_driver(davinci_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000779
Vladimir Barinov310355c2008-02-18 11:40:22 +0100780MODULE_AUTHOR("Vladimir Barinov");
781MODULE_DESCRIPTION("TI DAVINCI I2S (McBSP) SoC Interface");
782MODULE_LICENSE("GPL");