initial commit with v3.6.7
[linux-3.6.7-moxart.git] / sound / soc / kirkwood / kirkwood-i2s.c
blob7646dd7f30cb084fc83c58b8b802085e45770ef4
1 /*
2 * kirkwood-i2s.c
4 * (c) 2010 Arnaud Patard <apatard@mandriva.com>
5 * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/io.h>
17 #include <linux/slab.h>
18 #include <linux/mbus.h>
19 #include <linux/delay.h>
20 #include <linux/clk.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
24 #include <plat/audio.h>
25 #include "kirkwood.h"
27 #define DRV_NAME "kirkwood-i2s"
29 #define KIRKWOOD_I2S_RATES \
30 (SNDRV_PCM_RATE_44100 | \
31 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000)
32 #define KIRKWOOD_I2S_FORMATS \
33 (SNDRV_PCM_FMTBIT_S16_LE | \
34 SNDRV_PCM_FMTBIT_S24_LE | \
35 SNDRV_PCM_FMTBIT_S32_LE)
37 static int kirkwood_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
38 unsigned int fmt)
40 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(cpu_dai);
41 unsigned long mask;
42 unsigned long value;
44 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
45 case SND_SOC_DAIFMT_RIGHT_J:
46 mask = KIRKWOOD_I2S_CTL_RJ;
47 break;
48 case SND_SOC_DAIFMT_LEFT_J:
49 mask = KIRKWOOD_I2S_CTL_LJ;
50 break;
51 case SND_SOC_DAIFMT_I2S:
52 mask = KIRKWOOD_I2S_CTL_I2S;
53 break;
54 default:
55 return -EINVAL;
59 * Set same format for playback and record
60 * This avoids some troubles.
62 value = readl(priv->io+KIRKWOOD_I2S_PLAYCTL);
63 value &= ~KIRKWOOD_I2S_CTL_JUST_MASK;
64 value |= mask;
65 writel(value, priv->io+KIRKWOOD_I2S_PLAYCTL);
67 value = readl(priv->io+KIRKWOOD_I2S_RECCTL);
68 value &= ~KIRKWOOD_I2S_CTL_JUST_MASK;
69 value |= mask;
70 writel(value, priv->io+KIRKWOOD_I2S_RECCTL);
72 return 0;
75 static inline void kirkwood_set_dco(void __iomem *io, unsigned long rate)
77 unsigned long value;
79 value = KIRKWOOD_DCO_CTL_OFFSET_0;
80 switch (rate) {
81 default:
82 case 44100:
83 value |= KIRKWOOD_DCO_CTL_FREQ_11;
84 break;
85 case 48000:
86 value |= KIRKWOOD_DCO_CTL_FREQ_12;
87 break;
88 case 96000:
89 value |= KIRKWOOD_DCO_CTL_FREQ_24;
90 break;
92 writel(value, io + KIRKWOOD_DCO_CTL);
94 /* wait for dco locked */
95 do {
96 cpu_relax();
97 value = readl(io + KIRKWOOD_DCO_SPCR_STATUS);
98 value &= KIRKWOOD_DCO_SPCR_STATUS;
99 } while (value == 0);
102 static int kirkwood_i2s_startup(struct snd_pcm_substream *substream,
103 struct snd_soc_dai *dai)
105 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
107 snd_soc_dai_set_dma_data(dai, substream, priv);
108 return 0;
111 static int kirkwood_i2s_hw_params(struct snd_pcm_substream *substream,
112 struct snd_pcm_hw_params *params,
113 struct snd_soc_dai *dai)
115 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
116 unsigned int i2s_reg, reg;
117 unsigned long i2s_value, value;
119 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
120 i2s_reg = KIRKWOOD_I2S_PLAYCTL;
121 reg = KIRKWOOD_PLAYCTL;
122 } else {
123 i2s_reg = KIRKWOOD_I2S_RECCTL;
124 reg = KIRKWOOD_RECCTL;
127 /* set dco conf */
128 kirkwood_set_dco(priv->io, params_rate(params));
130 i2s_value = readl(priv->io+i2s_reg);
131 i2s_value &= ~KIRKWOOD_I2S_CTL_SIZE_MASK;
133 value = readl(priv->io+reg);
134 value &= ~KIRKWOOD_PLAYCTL_SIZE_MASK;
137 * Size settings in play/rec i2s control regs and play/rec control
138 * regs must be the same.
140 switch (params_format(params)) {
141 case SNDRV_PCM_FORMAT_S16_LE:
142 i2s_value |= KIRKWOOD_I2S_CTL_SIZE_16;
143 value |= KIRKWOOD_PLAYCTL_SIZE_16_C;
144 break;
146 * doesn't work... S20_3LE != kirkwood 20bit format ?
148 case SNDRV_PCM_FORMAT_S20_3LE:
149 i2s_value |= KIRKWOOD_I2S_CTL_SIZE_20;
150 value |= KIRKWOOD_PLAYCTL_SIZE_20;
151 break;
153 case SNDRV_PCM_FORMAT_S24_LE:
154 i2s_value |= KIRKWOOD_I2S_CTL_SIZE_24;
155 value |= KIRKWOOD_PLAYCTL_SIZE_24;
156 break;
157 case SNDRV_PCM_FORMAT_S32_LE:
158 i2s_value |= KIRKWOOD_I2S_CTL_SIZE_32;
159 value |= KIRKWOOD_PLAYCTL_SIZE_32;
160 break;
161 default:
162 return -EINVAL;
165 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
166 value &= ~KIRKWOOD_PLAYCTL_MONO_MASK;
167 if (params_channels(params) == 1)
168 value |= KIRKWOOD_PLAYCTL_MONO_BOTH;
169 else
170 value |= KIRKWOOD_PLAYCTL_MONO_OFF;
173 writel(i2s_value, priv->io+i2s_reg);
174 writel(value, priv->io+reg);
176 return 0;
179 static int kirkwood_i2s_play_trigger(struct snd_pcm_substream *substream,
180 int cmd, struct snd_soc_dai *dai)
182 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
183 unsigned long value;
186 * specs says KIRKWOOD_PLAYCTL must be read 2 times before
187 * changing it. So read 1 time here and 1 later.
189 value = readl(priv->io + KIRKWOOD_PLAYCTL);
191 switch (cmd) {
192 case SNDRV_PCM_TRIGGER_START:
193 /* stop audio, enable interrupts */
194 value = readl(priv->io + KIRKWOOD_PLAYCTL);
195 value |= KIRKWOOD_PLAYCTL_PAUSE;
196 writel(value, priv->io + KIRKWOOD_PLAYCTL);
198 value = readl(priv->io + KIRKWOOD_INT_MASK);
199 value |= KIRKWOOD_INT_CAUSE_PLAY_BYTES;
200 writel(value, priv->io + KIRKWOOD_INT_MASK);
202 /* configure audio & enable i2s playback */
203 value = readl(priv->io + KIRKWOOD_PLAYCTL);
204 value &= ~KIRKWOOD_PLAYCTL_BURST_MASK;
205 value &= ~(KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE
206 | KIRKWOOD_PLAYCTL_SPDIF_EN);
208 if (priv->burst == 32)
209 value |= KIRKWOOD_PLAYCTL_BURST_32;
210 else
211 value |= KIRKWOOD_PLAYCTL_BURST_128;
212 value |= KIRKWOOD_PLAYCTL_I2S_EN;
213 writel(value, priv->io + KIRKWOOD_PLAYCTL);
214 break;
216 case SNDRV_PCM_TRIGGER_STOP:
217 /* stop audio, disable interrupts */
218 value = readl(priv->io + KIRKWOOD_PLAYCTL);
219 value |= KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE;
220 writel(value, priv->io + KIRKWOOD_PLAYCTL);
222 value = readl(priv->io + KIRKWOOD_INT_MASK);
223 value &= ~KIRKWOOD_INT_CAUSE_PLAY_BYTES;
224 writel(value, priv->io + KIRKWOOD_INT_MASK);
226 /* disable all playbacks */
227 value = readl(priv->io + KIRKWOOD_PLAYCTL);
228 value &= ~(KIRKWOOD_PLAYCTL_I2S_EN | KIRKWOOD_PLAYCTL_SPDIF_EN);
229 writel(value, priv->io + KIRKWOOD_PLAYCTL);
230 break;
232 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
233 case SNDRV_PCM_TRIGGER_SUSPEND:
234 value = readl(priv->io + KIRKWOOD_PLAYCTL);
235 value |= KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE;
236 writel(value, priv->io + KIRKWOOD_PLAYCTL);
237 break;
239 case SNDRV_PCM_TRIGGER_RESUME:
240 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
241 value = readl(priv->io + KIRKWOOD_PLAYCTL);
242 value &= ~(KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE);
243 writel(value, priv->io + KIRKWOOD_PLAYCTL);
244 break;
246 default:
247 return -EINVAL;
250 return 0;
253 static int kirkwood_i2s_rec_trigger(struct snd_pcm_substream *substream,
254 int cmd, struct snd_soc_dai *dai)
256 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
257 unsigned long value;
259 value = readl(priv->io + KIRKWOOD_RECCTL);
261 switch (cmd) {
262 case SNDRV_PCM_TRIGGER_START:
263 /* stop audio, enable interrupts */
264 value = readl(priv->io + KIRKWOOD_RECCTL);
265 value |= KIRKWOOD_RECCTL_PAUSE;
266 writel(value, priv->io + KIRKWOOD_RECCTL);
268 value = readl(priv->io + KIRKWOOD_INT_MASK);
269 value |= KIRKWOOD_INT_CAUSE_REC_BYTES;
270 writel(value, priv->io + KIRKWOOD_INT_MASK);
272 /* configure audio & enable i2s record */
273 value = readl(priv->io + KIRKWOOD_RECCTL);
274 value &= ~KIRKWOOD_RECCTL_BURST_MASK;
275 value &= ~KIRKWOOD_RECCTL_MONO;
276 value &= ~(KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE
277 | KIRKWOOD_RECCTL_SPDIF_EN);
279 if (priv->burst == 32)
280 value |= KIRKWOOD_RECCTL_BURST_32;
281 else
282 value |= KIRKWOOD_RECCTL_BURST_128;
283 value |= KIRKWOOD_RECCTL_I2S_EN;
285 writel(value, priv->io + KIRKWOOD_RECCTL);
286 break;
288 case SNDRV_PCM_TRIGGER_STOP:
289 /* stop audio, disable interrupts */
290 value = readl(priv->io + KIRKWOOD_RECCTL);
291 value |= KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE;
292 writel(value, priv->io + KIRKWOOD_RECCTL);
294 value = readl(priv->io + KIRKWOOD_INT_MASK);
295 value &= ~KIRKWOOD_INT_CAUSE_REC_BYTES;
296 writel(value, priv->io + KIRKWOOD_INT_MASK);
298 /* disable all records */
299 value = readl(priv->io + KIRKWOOD_RECCTL);
300 value &= ~(KIRKWOOD_RECCTL_I2S_EN | KIRKWOOD_RECCTL_SPDIF_EN);
301 writel(value, priv->io + KIRKWOOD_RECCTL);
302 break;
304 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
305 case SNDRV_PCM_TRIGGER_SUSPEND:
306 value = readl(priv->io + KIRKWOOD_RECCTL);
307 value |= KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE;
308 writel(value, priv->io + KIRKWOOD_RECCTL);
309 break;
311 case SNDRV_PCM_TRIGGER_RESUME:
312 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
313 value = readl(priv->io + KIRKWOOD_RECCTL);
314 value &= ~(KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE);
315 writel(value, priv->io + KIRKWOOD_RECCTL);
316 break;
318 default:
319 return -EINVAL;
322 return 0;
325 static int kirkwood_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
326 struct snd_soc_dai *dai)
328 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
329 return kirkwood_i2s_play_trigger(substream, cmd, dai);
330 else
331 return kirkwood_i2s_rec_trigger(substream, cmd, dai);
333 return 0;
336 static int kirkwood_i2s_probe(struct snd_soc_dai *dai)
338 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
339 unsigned long value;
340 unsigned int reg_data;
342 /* put system in a "safe" state : */
343 /* disable audio interrupts */
344 writel(0xffffffff, priv->io + KIRKWOOD_INT_CAUSE);
345 writel(0, priv->io + KIRKWOOD_INT_MASK);
347 reg_data = readl(priv->io + 0x1200);
348 reg_data &= (~(0x333FF8));
349 reg_data |= 0x111D18;
350 writel(reg_data, priv->io + 0x1200);
352 msleep(500);
354 reg_data = readl(priv->io + 0x1200);
355 reg_data &= (~(0x333FF8));
356 reg_data |= 0x111D18;
357 writel(reg_data, priv->io + 0x1200);
359 /* disable playback/record */
360 value = readl(priv->io + KIRKWOOD_PLAYCTL);
361 value &= ~(KIRKWOOD_PLAYCTL_I2S_EN|KIRKWOOD_PLAYCTL_SPDIF_EN);
362 writel(value, priv->io + KIRKWOOD_PLAYCTL);
364 value = readl(priv->io + KIRKWOOD_RECCTL);
365 value &= ~(KIRKWOOD_RECCTL_I2S_EN | KIRKWOOD_RECCTL_SPDIF_EN);
366 writel(value, priv->io + KIRKWOOD_RECCTL);
368 return 0;
372 static int kirkwood_i2s_remove(struct snd_soc_dai *dai)
374 return 0;
377 static const struct snd_soc_dai_ops kirkwood_i2s_dai_ops = {
378 .startup = kirkwood_i2s_startup,
379 .trigger = kirkwood_i2s_trigger,
380 .hw_params = kirkwood_i2s_hw_params,
381 .set_fmt = kirkwood_i2s_set_fmt,
385 static struct snd_soc_dai_driver kirkwood_i2s_dai = {
386 .probe = kirkwood_i2s_probe,
387 .remove = kirkwood_i2s_remove,
388 .playback = {
389 .channels_min = 1,
390 .channels_max = 2,
391 .rates = KIRKWOOD_I2S_RATES,
392 .formats = KIRKWOOD_I2S_FORMATS,},
393 .capture = {
394 .channels_min = 1,
395 .channels_max = 2,
396 .rates = KIRKWOOD_I2S_RATES,
397 .formats = KIRKWOOD_I2S_FORMATS,},
398 .ops = &kirkwood_i2s_dai_ops,
401 static __devinit int kirkwood_i2s_dev_probe(struct platform_device *pdev)
403 struct resource *mem;
404 struct kirkwood_asoc_platform_data *data =
405 pdev->dev.platform_data;
406 struct kirkwood_dma_data *priv;
407 int err;
409 priv = kzalloc(sizeof(struct kirkwood_dma_data), GFP_KERNEL);
410 if (!priv) {
411 dev_err(&pdev->dev, "allocation failed\n");
412 err = -ENOMEM;
413 goto error;
415 dev_set_drvdata(&pdev->dev, priv);
417 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
418 if (!mem) {
419 dev_err(&pdev->dev, "platform_get_resource failed\n");
420 err = -ENXIO;
421 goto err_alloc;
424 priv->mem = request_mem_region(mem->start, SZ_16K, DRV_NAME);
425 if (!priv->mem) {
426 dev_err(&pdev->dev, "request_mem_region failed\n");
427 err = -EBUSY;
428 goto err_alloc;
431 priv->io = ioremap(priv->mem->start, SZ_16K);
432 if (!priv->io) {
433 dev_err(&pdev->dev, "ioremap failed\n");
434 err = -ENOMEM;
435 goto err_iomem;
438 priv->irq = platform_get_irq(pdev, 0);
439 if (priv->irq <= 0) {
440 dev_err(&pdev->dev, "platform_get_irq failed\n");
441 err = -ENXIO;
442 goto err_ioremap;
445 if (!data) {
446 dev_err(&pdev->dev, "no platform data ?!\n");
447 err = -EINVAL;
448 goto err_ioremap;
451 priv->burst = data->burst;
453 priv->clk = clk_get(&pdev->dev, NULL);
454 if (IS_ERR(priv->clk)) {
455 dev_err(&pdev->dev, "no clock\n");
456 err = PTR_ERR(priv->clk);
457 goto err_ioremap;
459 clk_prepare_enable(priv->clk);
461 err = snd_soc_register_dai(&pdev->dev, &kirkwood_i2s_dai);
462 if (!err)
463 return 0;
464 dev_err(&pdev->dev, "snd_soc_register_dai failed\n");
466 clk_disable_unprepare(priv->clk);
467 clk_put(priv->clk);
469 err_ioremap:
470 iounmap(priv->io);
471 err_iomem:
472 release_mem_region(priv->mem->start, SZ_16K);
473 err_alloc:
474 kfree(priv);
475 error:
476 return err;
479 static __devexit int kirkwood_i2s_dev_remove(struct platform_device *pdev)
481 struct kirkwood_dma_data *priv = dev_get_drvdata(&pdev->dev);
483 snd_soc_unregister_dai(&pdev->dev);
485 clk_disable_unprepare(priv->clk);
486 clk_put(priv->clk);
488 iounmap(priv->io);
489 release_mem_region(priv->mem->start, SZ_16K);
490 kfree(priv);
492 return 0;
495 static struct platform_driver kirkwood_i2s_driver = {
496 .probe = kirkwood_i2s_dev_probe,
497 .remove = __devexit_p(kirkwood_i2s_dev_remove),
498 .driver = {
499 .name = DRV_NAME,
500 .owner = THIS_MODULE,
504 module_platform_driver(kirkwood_i2s_driver);
506 /* Module information */
507 MODULE_AUTHOR("Arnaud Patard, <arnaud.patard@rtp-net.org>");
508 MODULE_DESCRIPTION("Kirkwood I2S SoC Interface");
509 MODULE_LICENSE("GPL");
510 MODULE_ALIAS("platform:kirkwood-i2s");