blob: 5c1102e9e1598823b8eac35bfe72b3b203dcb5f7 [file] [log] [blame]
Ryan Mallondb5bf412010-06-04 17:11:24 +12001/*
2 * linux/sound/soc/ep93xx-i2s.c
3 * EP93xx I2S driver
4 *
Ryan Mallon1c5454e2011-06-15 14:45:36 +10005 * Copyright (C) 2010 Ryan Mallon
Ryan Mallondb5bf412010-06-04 17:11:24 +12006 *
7 * Based on the original driver by:
8 * Copyright (C) 2007 Chase Douglas <chasedouglas@gmail>
9 * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/clk.h>
21#include <linux/io.h>
22
23#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/initval.h>
27#include <sound/soc.h>
28
29#include <mach/hardware.h>
30#include <mach/ep93xx-regs.h>
Arnd Bergmanna3b29242012-08-24 15:12:11 +020031#include <linux/platform_data/dma-ep93xx.h>
Ryan Mallondb5bf412010-06-04 17:11:24 +120032
Ryan Mallondb5bf412010-06-04 17:11:24 +120033#define EP93XX_I2S_TXCLKCFG 0x00
34#define EP93XX_I2S_RXCLKCFG 0x04
35#define EP93XX_I2S_GLCTRL 0x0C
36
37#define EP93XX_I2S_TXLINCTRLDATA 0x28
38#define EP93XX_I2S_TXCTRL 0x2C
39#define EP93XX_I2S_TXWRDLEN 0x30
40#define EP93XX_I2S_TX0EN 0x34
41
42#define EP93XX_I2S_RXLINCTRLDATA 0x58
43#define EP93XX_I2S_RXCTRL 0x5C
44#define EP93XX_I2S_RXWRDLEN 0x60
45#define EP93XX_I2S_RX0EN 0x64
46
47#define EP93XX_I2S_WRDLEN_16 (0 << 0)
48#define EP93XX_I2S_WRDLEN_24 (1 << 0)
49#define EP93XX_I2S_WRDLEN_32 (2 << 0)
50
51#define EP93XX_I2S_LINCTRLDATA_R_JUST (1 << 2) /* Right justify */
52
53#define EP93XX_I2S_CLKCFG_LRS (1 << 0) /* lrclk polarity */
54#define EP93XX_I2S_CLKCFG_CKP (1 << 1) /* Bit clock polarity */
55#define EP93XX_I2S_CLKCFG_REL (1 << 2) /* First bit transition */
56#define EP93XX_I2S_CLKCFG_MASTER (1 << 3) /* Master mode */
57#define EP93XX_I2S_CLKCFG_NBCG (1 << 4) /* Not bit clock gating */
58
59struct ep93xx_i2s_info {
60 struct clk *mclk;
61 struct clk *sclk;
62 struct clk *lrclk;
Lars-Peter Clausen453807f2013-03-22 14:12:10 +010063 struct ep93xx_dma_data *dma_data;
Ryan Mallondb5bf412010-06-04 17:11:24 +120064 void __iomem *regs;
65};
66
Lars-Peter Clausen453807f2013-03-22 14:12:10 +010067struct ep93xx_dma_data ep93xx_i2s_dma_data[] = {
Ryan Mallondb5bf412010-06-04 17:11:24 +120068 [SNDRV_PCM_STREAM_PLAYBACK] = {
69 .name = "i2s-pcm-out",
Lars-Peter Clausene6451c32013-04-03 11:00:00 +020070 .port = EP93XX_DMA_I2S1,
Lars-Peter Clausen453807f2013-03-22 14:12:10 +010071 .direction = DMA_MEM_TO_DEV,
Ryan Mallondb5bf412010-06-04 17:11:24 +120072 },
73 [SNDRV_PCM_STREAM_CAPTURE] = {
74 .name = "i2s-pcm-in",
Lars-Peter Clausene6451c32013-04-03 11:00:00 +020075 .port = EP93XX_DMA_I2S1,
Lars-Peter Clausen453807f2013-03-22 14:12:10 +010076 .direction = DMA_DEV_TO_MEM,
Ryan Mallondb5bf412010-06-04 17:11:24 +120077 },
78};
79
80static inline void ep93xx_i2s_write_reg(struct ep93xx_i2s_info *info,
81 unsigned reg, unsigned val)
82{
83 __raw_writel(val, info->regs + reg);
84}
85
86static inline unsigned ep93xx_i2s_read_reg(struct ep93xx_i2s_info *info,
87 unsigned reg)
88{
89 return __raw_readl(info->regs + reg);
90}
91
92static void ep93xx_i2s_enable(struct ep93xx_i2s_info *info, int stream)
93{
94 unsigned base_reg;
95 int i;
96
97 if ((ep93xx_i2s_read_reg(info, EP93XX_I2S_TX0EN) & 0x1) == 0 &&
98 (ep93xx_i2s_read_reg(info, EP93XX_I2S_RX0EN) & 0x1) == 0) {
99 /* Enable clocks */
100 clk_enable(info->mclk);
101 clk_enable(info->sclk);
102 clk_enable(info->lrclk);
103
104 /* Enable i2s */
105 ep93xx_i2s_write_reg(info, EP93XX_I2S_GLCTRL, 1);
106 }
107
108 /* Enable fifos */
109 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
110 base_reg = EP93XX_I2S_TX0EN;
111 else
112 base_reg = EP93XX_I2S_RX0EN;
113 for (i = 0; i < 3; i++)
114 ep93xx_i2s_write_reg(info, base_reg + (i * 4), 1);
115}
116
117static void ep93xx_i2s_disable(struct ep93xx_i2s_info *info, int stream)
118{
119 unsigned base_reg;
120 int i;
121
122 /* Disable fifos */
123 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
124 base_reg = EP93XX_I2S_TX0EN;
125 else
126 base_reg = EP93XX_I2S_RX0EN;
127 for (i = 0; i < 3; i++)
128 ep93xx_i2s_write_reg(info, base_reg + (i * 4), 0);
129
130 if ((ep93xx_i2s_read_reg(info, EP93XX_I2S_TX0EN) & 0x1) == 0 &&
131 (ep93xx_i2s_read_reg(info, EP93XX_I2S_RX0EN) & 0x1) == 0) {
132 /* Disable i2s */
133 ep93xx_i2s_write_reg(info, EP93XX_I2S_GLCTRL, 0);
134
135 /* Disable clocks */
136 clk_disable(info->lrclk);
137 clk_disable(info->sclk);
138 clk_disable(info->mclk);
139 }
140}
141
142static int ep93xx_i2s_startup(struct snd_pcm_substream *substream,
143 struct snd_soc_dai *dai)
144{
145 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000146 struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
147 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Ryan Mallondb5bf412010-06-04 17:11:24 +1200148
149 snd_soc_dai_set_dma_data(cpu_dai, substream,
Lars-Peter Clausen453807f2013-03-22 14:12:10 +0100150 &info->dma_data[substream->stream]);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200151 return 0;
152}
153
154static void ep93xx_i2s_shutdown(struct snd_pcm_substream *substream,
155 struct snd_soc_dai *dai)
156{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000157 struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200158
159 ep93xx_i2s_disable(info, substream->stream);
160}
161
162static int ep93xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
163 unsigned int fmt)
164{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000165 struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(cpu_dai);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200166 unsigned int clk_cfg, lin_ctrl;
167
168 clk_cfg = ep93xx_i2s_read_reg(info, EP93XX_I2S_RXCLKCFG);
169 lin_ctrl = ep93xx_i2s_read_reg(info, EP93XX_I2S_RXLINCTRLDATA);
170
171 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
172 case SND_SOC_DAIFMT_I2S:
173 clk_cfg |= EP93XX_I2S_CLKCFG_REL;
174 lin_ctrl &= ~EP93XX_I2S_LINCTRLDATA_R_JUST;
175 break;
176
177 case SND_SOC_DAIFMT_LEFT_J:
178 clk_cfg &= ~EP93XX_I2S_CLKCFG_REL;
179 lin_ctrl &= ~EP93XX_I2S_LINCTRLDATA_R_JUST;
180 break;
181
182 case SND_SOC_DAIFMT_RIGHT_J:
183 clk_cfg &= ~EP93XX_I2S_CLKCFG_REL;
184 lin_ctrl |= EP93XX_I2S_LINCTRLDATA_R_JUST;
185 break;
186
187 default:
188 return -EINVAL;
189 }
190
191 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
192 case SND_SOC_DAIFMT_CBS_CFS:
193 /* CPU is master */
194 clk_cfg |= EP93XX_I2S_CLKCFG_MASTER;
195 break;
196
197 case SND_SOC_DAIFMT_CBM_CFM:
198 /* Codec is master */
199 clk_cfg &= ~EP93XX_I2S_CLKCFG_MASTER;
200 break;
201
202 default:
203 return -EINVAL;
204 }
205
206 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
207 case SND_SOC_DAIFMT_NB_NF:
208 /* Negative bit clock, lrclk low on left word */
209 clk_cfg &= ~(EP93XX_I2S_CLKCFG_CKP | EP93XX_I2S_CLKCFG_REL);
210 break;
211
212 case SND_SOC_DAIFMT_NB_IF:
213 /* Negative bit clock, lrclk low on right word */
214 clk_cfg &= ~EP93XX_I2S_CLKCFG_CKP;
215 clk_cfg |= EP93XX_I2S_CLKCFG_REL;
216 break;
217
218 case SND_SOC_DAIFMT_IB_NF:
219 /* Positive bit clock, lrclk low on left word */
220 clk_cfg |= EP93XX_I2S_CLKCFG_CKP;
221 clk_cfg &= ~EP93XX_I2S_CLKCFG_REL;
222 break;
223
224 case SND_SOC_DAIFMT_IB_IF:
225 /* Positive bit clock, lrclk low on right word */
226 clk_cfg |= EP93XX_I2S_CLKCFG_CKP | EP93XX_I2S_CLKCFG_REL;
227 break;
228 }
229
230 /* Write new register values */
231 ep93xx_i2s_write_reg(info, EP93XX_I2S_RXCLKCFG, clk_cfg);
232 ep93xx_i2s_write_reg(info, EP93XX_I2S_TXCLKCFG, clk_cfg);
233 ep93xx_i2s_write_reg(info, EP93XX_I2S_RXLINCTRLDATA, lin_ctrl);
234 ep93xx_i2s_write_reg(info, EP93XX_I2S_TXLINCTRLDATA, lin_ctrl);
235 return 0;
236}
237
238static int ep93xx_i2s_hw_params(struct snd_pcm_substream *substream,
239 struct snd_pcm_hw_params *params,
240 struct snd_soc_dai *dai)
241{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000242 struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200243 unsigned word_len, div, sdiv, lrdiv;
Alexander Sverdlin5cbf7e42011-03-07 20:30:36 +0300244 int err;
Ryan Mallondb5bf412010-06-04 17:11:24 +1200245
246 switch (params_format(params)) {
247 case SNDRV_PCM_FORMAT_S16_LE:
248 word_len = EP93XX_I2S_WRDLEN_16;
249 break;
250
251 case SNDRV_PCM_FORMAT_S24_LE:
252 word_len = EP93XX_I2S_WRDLEN_24;
253 break;
254
255 case SNDRV_PCM_FORMAT_S32_LE:
256 word_len = EP93XX_I2S_WRDLEN_32;
257 break;
258
259 default:
260 return -EINVAL;
261 }
262
263 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
264 ep93xx_i2s_write_reg(info, EP93XX_I2S_TXWRDLEN, word_len);
265 else
266 ep93xx_i2s_write_reg(info, EP93XX_I2S_RXWRDLEN, word_len);
267
268 /*
Alexander Sverdlin7322ce22011-01-16 15:48:05 +0300269 * EP93xx I2S module can be setup so SCLK / LRCLK value can be
270 * 32, 64, 128. MCLK / SCLK value can be 2 and 4.
271 * We set LRCLK equal to `rate' and minimum SCLK / LRCLK
272 * value is 64, because our sample size is 32 bit * 2 channels.
273 * I2S standard permits us to transmit more bits than
274 * the codec uses.
Ryan Mallondb5bf412010-06-04 17:11:24 +1200275 */
Alexander Sverdlin7322ce22011-01-16 15:48:05 +0300276 div = clk_get_rate(info->mclk) / params_rate(params);
Alexander Sverdlin5cbf7e42011-03-07 20:30:36 +0300277 sdiv = 4;
278 if (div > (256 + 512) / 2) {
279 lrdiv = 128;
280 } else {
281 lrdiv = 64;
282 if (div < (128 + 256) / 2)
283 sdiv = 2;
284 }
Ryan Mallondb5bf412010-06-04 17:11:24 +1200285
286 err = clk_set_rate(info->sclk, clk_get_rate(info->mclk) / sdiv);
287 if (err)
288 return err;
289
290 err = clk_set_rate(info->lrclk, clk_get_rate(info->sclk) / lrdiv);
291 if (err)
292 return err;
293
294 ep93xx_i2s_enable(info, substream->stream);
295 return 0;
296}
297
298static int ep93xx_i2s_set_sysclk(struct snd_soc_dai *cpu_dai, int clk_id,
299 unsigned int freq, int dir)
300{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000301 struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(cpu_dai);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200302
303 if (dir == SND_SOC_CLOCK_IN || clk_id != 0)
304 return -EINVAL;
305
306 return clk_set_rate(info->mclk, freq);
307}
308
309#ifdef CONFIG_PM
310static int ep93xx_i2s_suspend(struct snd_soc_dai *dai)
311{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000312 struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200313
314 if (!dai->active)
Alexander Sverdlinf9c54042011-03-07 20:29:58 +0300315 return 0;
Ryan Mallondb5bf412010-06-04 17:11:24 +1200316
317 ep93xx_i2s_disable(info, SNDRV_PCM_STREAM_PLAYBACK);
318 ep93xx_i2s_disable(info, SNDRV_PCM_STREAM_CAPTURE);
Alexander Sverdlinf9c54042011-03-07 20:29:58 +0300319
320 return 0;
Ryan Mallondb5bf412010-06-04 17:11:24 +1200321}
322
323static int ep93xx_i2s_resume(struct snd_soc_dai *dai)
324{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000325 struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200326
327 if (!dai->active)
Alexander Sverdlinf9c54042011-03-07 20:29:58 +0300328 return 0;
Ryan Mallondb5bf412010-06-04 17:11:24 +1200329
330 ep93xx_i2s_enable(info, SNDRV_PCM_STREAM_PLAYBACK);
331 ep93xx_i2s_enable(info, SNDRV_PCM_STREAM_CAPTURE);
Alexander Sverdlinf9c54042011-03-07 20:29:58 +0300332
333 return 0;
Ryan Mallondb5bf412010-06-04 17:11:24 +1200334}
335#else
336#define ep93xx_i2s_suspend NULL
337#define ep93xx_i2s_resume NULL
338#endif
339
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100340static const struct snd_soc_dai_ops ep93xx_i2s_dai_ops = {
Ryan Mallondb5bf412010-06-04 17:11:24 +1200341 .startup = ep93xx_i2s_startup,
342 .shutdown = ep93xx_i2s_shutdown,
343 .hw_params = ep93xx_i2s_hw_params,
344 .set_sysclk = ep93xx_i2s_set_sysclk,
345 .set_fmt = ep93xx_i2s_set_dai_fmt,
346};
347
Alexander Sverdlin7322ce22011-01-16 15:48:05 +0300348#define EP93XX_I2S_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
Ryan Mallondb5bf412010-06-04 17:11:24 +1200349
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000350static struct snd_soc_dai_driver ep93xx_i2s_dai = {
Ryan Mallondb5bf412010-06-04 17:11:24 +1200351 .symmetric_rates= 1,
352 .suspend = ep93xx_i2s_suspend,
353 .resume = ep93xx_i2s_resume,
354 .playback = {
355 .channels_min = 2,
356 .channels_max = 2,
Alexander Sverdlin4cfeb692011-03-07 20:30:12 +0300357 .rates = SNDRV_PCM_RATE_8000_192000,
Ryan Mallondb5bf412010-06-04 17:11:24 +1200358 .formats = EP93XX_I2S_FORMATS,
359 },
360 .capture = {
361 .channels_min = 2,
362 .channels_max = 2,
Alexander Sverdlin4cfeb692011-03-07 20:30:12 +0300363 .rates = SNDRV_PCM_RATE_8000_192000,
Ryan Mallondb5bf412010-06-04 17:11:24 +1200364 .formats = EP93XX_I2S_FORMATS,
365 },
366 .ops = &ep93xx_i2s_dai_ops,
367};
Ryan Mallondb5bf412010-06-04 17:11:24 +1200368
Kuninori Morimotoec050852013-03-21 03:30:43 -0700369static const struct snd_soc_component_driver ep93xx_i2s_component = {
370 .name = "ep93xx-i2s",
371};
372
Ryan Mallondb5bf412010-06-04 17:11:24 +1200373static int ep93xx_i2s_probe(struct platform_device *pdev)
374{
375 struct ep93xx_i2s_info *info;
376 struct resource *res;
377 int err;
378
H Hartley Sweeten01651ba2012-03-26 16:00:17 -0700379 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
380 if (!info)
381 return -ENOMEM;
Ryan Mallondb5bf412010-06-04 17:11:24 +1200382
383 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
H Hartley Sweeten01651ba2012-03-26 16:00:17 -0700384 if (!res)
385 return -ENODEV;
Ryan Mallondb5bf412010-06-04 17:11:24 +1200386
Thierry Redingb25b5aa2013-01-21 11:09:26 +0100387 info->regs = devm_ioremap_resource(&pdev->dev, res);
388 if (IS_ERR(info->regs))
389 return PTR_ERR(info->regs);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200390
391 info->mclk = clk_get(&pdev->dev, "mclk");
392 if (IS_ERR(info->mclk)) {
393 err = PTR_ERR(info->mclk);
H Hartley Sweeten01651ba2012-03-26 16:00:17 -0700394 goto fail;
Ryan Mallondb5bf412010-06-04 17:11:24 +1200395 }
396
397 info->sclk = clk_get(&pdev->dev, "sclk");
398 if (IS_ERR(info->sclk)) {
399 err = PTR_ERR(info->sclk);
400 goto fail_put_mclk;
401 }
402
403 info->lrclk = clk_get(&pdev->dev, "lrclk");
404 if (IS_ERR(info->lrclk)) {
405 err = PTR_ERR(info->lrclk);
406 goto fail_put_sclk;
407 }
408
H Hartley Sweeten01651ba2012-03-26 16:00:17 -0700409 dev_set_drvdata(&pdev->dev, info);
Lars-Peter Clausen453807f2013-03-22 14:12:10 +0100410 info->dma_data = ep93xx_i2s_dma_data;
H Hartley Sweeten01651ba2012-03-26 16:00:17 -0700411
Kuninori Morimotoec050852013-03-21 03:30:43 -0700412 err = snd_soc_register_component(&pdev->dev, &ep93xx_i2s_component,
413 &ep93xx_i2s_dai, 1);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200414 if (err)
415 goto fail_put_lrclk;
416
417 return 0;
418
419fail_put_lrclk:
H Hartley Sweeten01651ba2012-03-26 16:00:17 -0700420 dev_set_drvdata(&pdev->dev, NULL);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200421 clk_put(info->lrclk);
422fail_put_sclk:
423 clk_put(info->sclk);
424fail_put_mclk:
425 clk_put(info->mclk);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200426fail:
427 return err;
428}
429
Bill Pemberton145e2872012-12-07 09:26:23 -0500430static int ep93xx_i2s_remove(struct platform_device *pdev)
Ryan Mallondb5bf412010-06-04 17:11:24 +1200431{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000432 struct ep93xx_i2s_info *info = dev_get_drvdata(&pdev->dev);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200433
Kuninori Morimotoec050852013-03-21 03:30:43 -0700434 snd_soc_unregister_component(&pdev->dev);
H Hartley Sweeten01651ba2012-03-26 16:00:17 -0700435 dev_set_drvdata(&pdev->dev, NULL);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200436 clk_put(info->lrclk);
437 clk_put(info->sclk);
438 clk_put(info->mclk);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200439 return 0;
440}
441
442static struct platform_driver ep93xx_i2s_driver = {
443 .probe = ep93xx_i2s_probe,
Bill Pemberton145e2872012-12-07 09:26:23 -0500444 .remove = ep93xx_i2s_remove,
Ryan Mallondb5bf412010-06-04 17:11:24 +1200445 .driver = {
446 .name = "ep93xx-i2s",
447 .owner = THIS_MODULE,
448 },
449};
450
Axel Linee18f632011-11-24 12:07:55 +0800451module_platform_driver(ep93xx_i2s_driver);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200452
453MODULE_ALIAS("platform:ep93xx-i2s");
Ryan Mallon1c5454e2011-06-15 14:45:36 +1000454MODULE_AUTHOR("Ryan Mallon");
Ryan Mallondb5bf412010-06-04 17:11:24 +1200455MODULE_DESCRIPTION("EP93XX I2S driver");
456MODULE_LICENSE("GPL");