blob: 3a36d60e178504637963a3193f2a60514e0d272f [file] [log] [blame]
apatard@mandriva.comf9b95982010-05-31 13:49:14 +02001/*
2 * kirkwood-i2s.c
3 *
4 * (c) 2010 Arnaud Patard <apatard@mandriva.com>
Arnaud Patard (Rtp)69737892010-09-09 14:10:33 +02005 * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
apatard@mandriva.comf9b95982010-05-31 13:49:14 +02006 *
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.
11 */
12
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>
Andrew Lunne919c712012-03-09 09:56:41 +010020#include <linux/clk.h>
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020021#include <sound/pcm.h>
22#include <sound/pcm_params.h>
23#include <sound/soc.h>
Arnd Bergmannc02cecb2012-08-24 15:21:54 +020024#include <linux/platform_data/asoc-kirkwood.h>
Jean-Francois Moineeb632312013-08-14 12:27:33 +020025#include <linux/of.h>
26
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020027#include "kirkwood.h"
28
Russell King64ddf1f2013-08-04 20:27:03 +010029#define DRV_NAME "mvebu-audio"
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020030
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020031#define KIRKWOOD_I2S_FORMATS \
32 (SNDRV_PCM_FMTBIT_S16_LE | \
33 SNDRV_PCM_FMTBIT_S24_LE | \
34 SNDRV_PCM_FMTBIT_S32_LE)
35
Jean-Francois Moine1c195dd2013-11-26 10:41:40 +010036#define KIRKWOOD_SPDIF_FORMATS \
37 (SNDRV_PCM_FMTBIT_S16_LE | \
38 SNDRV_PCM_FMTBIT_S24_LE)
39
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020040static int kirkwood_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
41 unsigned int fmt)
42{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000043 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(cpu_dai);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020044 unsigned long mask;
45 unsigned long value;
46
47 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
48 case SND_SOC_DAIFMT_RIGHT_J:
49 mask = KIRKWOOD_I2S_CTL_RJ;
50 break;
51 case SND_SOC_DAIFMT_LEFT_J:
52 mask = KIRKWOOD_I2S_CTL_LJ;
53 break;
54 case SND_SOC_DAIFMT_I2S:
55 mask = KIRKWOOD_I2S_CTL_I2S;
56 break;
57 default:
58 return -EINVAL;
59 }
60
61 /*
62 * Set same format for playback and record
63 * This avoids some troubles.
64 */
65 value = readl(priv->io+KIRKWOOD_I2S_PLAYCTL);
66 value &= ~KIRKWOOD_I2S_CTL_JUST_MASK;
67 value |= mask;
68 writel(value, priv->io+KIRKWOOD_I2S_PLAYCTL);
69
70 value = readl(priv->io+KIRKWOOD_I2S_RECCTL);
71 value &= ~KIRKWOOD_I2S_CTL_JUST_MASK;
72 value |= mask;
73 writel(value, priv->io+KIRKWOOD_I2S_RECCTL);
74
75 return 0;
76}
77
78static inline void kirkwood_set_dco(void __iomem *io, unsigned long rate)
79{
80 unsigned long value;
81
82 value = KIRKWOOD_DCO_CTL_OFFSET_0;
83 switch (rate) {
84 default:
85 case 44100:
86 value |= KIRKWOOD_DCO_CTL_FREQ_11;
87 break;
88 case 48000:
89 value |= KIRKWOOD_DCO_CTL_FREQ_12;
90 break;
91 case 96000:
92 value |= KIRKWOOD_DCO_CTL_FREQ_24;
93 break;
94 }
95 writel(value, io + KIRKWOOD_DCO_CTL);
96
97 /* wait for dco locked */
98 do {
99 cpu_relax();
100 value = readl(io + KIRKWOOD_DCO_SPCR_STATUS);
Russell King2424d452012-11-20 12:18:32 +0000101 value &= KIRKWOOD_DCO_SPCR_STATUS_DCO_LOCK;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200102 } while (value == 0);
103}
104
Russell King363589b2012-11-20 12:20:34 +0000105static void kirkwood_set_rate(struct snd_soc_dai *dai,
106 struct kirkwood_dma_data *priv, unsigned long rate)
107{
108 uint32_t clks_ctrl;
109
Jean-Francois Moine1f1b6572013-10-18 20:34:52 +0200110 if (IS_ERR(priv->extclk)) {
Jean-Francois Moine8a537f82013-07-16 08:47:47 +0200111 /* use internal dco for the supported rates
112 * defined in kirkwood_i2s_dai */
Russell King363589b2012-11-20 12:20:34 +0000113 dev_dbg(dai->dev, "%s: dco set rate = %lu\n",
114 __func__, rate);
115 kirkwood_set_dco(priv->io, rate);
116
117 clks_ctrl = KIRKWOOD_MCLK_SOURCE_DCO;
Jean-Francois Moine8a537f82013-07-16 08:47:47 +0200118 } else {
119 /* use the external clock for the other rates
120 * defined in kirkwood_i2s_dai_extclk */
Russell King363589b2012-11-20 12:20:34 +0000121 dev_dbg(dai->dev, "%s: extclk set rate = %lu -> %lu\n",
122 __func__, rate, 256 * rate);
123 clk_set_rate(priv->extclk, 256 * rate);
124
125 clks_ctrl = KIRKWOOD_MCLK_SOURCE_EXTCLK;
126 }
127 writel(clks_ctrl, priv->io + KIRKWOOD_CLOCKS_CTRL);
128}
129
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000130static int kirkwood_i2s_startup(struct snd_pcm_substream *substream,
131 struct snd_soc_dai *dai)
132{
133 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
134
135 snd_soc_dai_set_dma_data(dai, substream, priv);
136 return 0;
137}
138
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200139static int kirkwood_i2s_hw_params(struct snd_pcm_substream *substream,
140 struct snd_pcm_hw_params *params,
141 struct snd_soc_dai *dai)
142{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000143 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
Russell Kingd8d11ba2012-11-20 12:19:53 +0000144 uint32_t ctl_play, ctl_rec;
145 unsigned int i2s_reg;
146 unsigned long i2s_value;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200147
148 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
149 i2s_reg = KIRKWOOD_I2S_PLAYCTL;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200150 } else {
151 i2s_reg = KIRKWOOD_I2S_RECCTL;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200152 }
153
Russell King363589b2012-11-20 12:20:34 +0000154 kirkwood_set_rate(dai, priv, params_rate(params));
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200155
156 i2s_value = readl(priv->io+i2s_reg);
157 i2s_value &= ~KIRKWOOD_I2S_CTL_SIZE_MASK;
158
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200159 /*
160 * Size settings in play/rec i2s control regs and play/rec control
161 * regs must be the same.
162 */
163 switch (params_format(params)) {
164 case SNDRV_PCM_FORMAT_S16_LE:
165 i2s_value |= KIRKWOOD_I2S_CTL_SIZE_16;
Russell Kingd8d11ba2012-11-20 12:19:53 +0000166 ctl_play = KIRKWOOD_PLAYCTL_SIZE_16_C |
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200167 KIRKWOOD_PLAYCTL_I2S_EN |
168 KIRKWOOD_PLAYCTL_SPDIF_EN;
Russell Kingd8d11ba2012-11-20 12:19:53 +0000169 ctl_rec = KIRKWOOD_RECCTL_SIZE_16_C |
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200170 KIRKWOOD_RECCTL_I2S_EN |
171 KIRKWOOD_RECCTL_SPDIF_EN;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200172 break;
173 /*
174 * doesn't work... S20_3LE != kirkwood 20bit format ?
175 *
176 case SNDRV_PCM_FORMAT_S20_3LE:
177 i2s_value |= KIRKWOOD_I2S_CTL_SIZE_20;
Russell Kingd8d11ba2012-11-20 12:19:53 +0000178 ctl_play = KIRKWOOD_PLAYCTL_SIZE_20 |
179 KIRKWOOD_PLAYCTL_I2S_EN;
180 ctl_rec = KIRKWOOD_RECCTL_SIZE_20 |
181 KIRKWOOD_RECCTL_I2S_EN;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200182 break;
183 */
184 case SNDRV_PCM_FORMAT_S24_LE:
185 i2s_value |= KIRKWOOD_I2S_CTL_SIZE_24;
Russell Kingd8d11ba2012-11-20 12:19:53 +0000186 ctl_play = KIRKWOOD_PLAYCTL_SIZE_24 |
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200187 KIRKWOOD_PLAYCTL_I2S_EN |
188 KIRKWOOD_PLAYCTL_SPDIF_EN;
Russell Kingd8d11ba2012-11-20 12:19:53 +0000189 ctl_rec = KIRKWOOD_RECCTL_SIZE_24 |
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200190 KIRKWOOD_RECCTL_I2S_EN |
191 KIRKWOOD_RECCTL_SPDIF_EN;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200192 break;
193 case SNDRV_PCM_FORMAT_S32_LE:
194 i2s_value |= KIRKWOOD_I2S_CTL_SIZE_32;
Russell Kingd8d11ba2012-11-20 12:19:53 +0000195 ctl_play = KIRKWOOD_PLAYCTL_SIZE_32 |
196 KIRKWOOD_PLAYCTL_I2S_EN;
197 ctl_rec = KIRKWOOD_RECCTL_SIZE_32 |
198 KIRKWOOD_RECCTL_I2S_EN;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200199 break;
200 default:
201 return -EINVAL;
202 }
arnaud.patard@rtp-net.orgdfe4c932010-07-11 23:28:31 +0200203
204 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
arnaud.patard@rtp-net.orgdfe4c932010-07-11 23:28:31 +0200205 if (params_channels(params) == 1)
Russell Kingd8d11ba2012-11-20 12:19:53 +0000206 ctl_play |= KIRKWOOD_PLAYCTL_MONO_BOTH;
arnaud.patard@rtp-net.orgdfe4c932010-07-11 23:28:31 +0200207 else
Russell Kingd8d11ba2012-11-20 12:19:53 +0000208 ctl_play |= KIRKWOOD_PLAYCTL_MONO_OFF;
209
210 priv->ctl_play &= ~(KIRKWOOD_PLAYCTL_MONO_MASK |
Russell Kingdb43b162013-08-04 20:26:03 +0100211 KIRKWOOD_PLAYCTL_ENABLE_MASK |
Russell Kingd8d11ba2012-11-20 12:19:53 +0000212 KIRKWOOD_PLAYCTL_SIZE_MASK);
213 priv->ctl_play |= ctl_play;
214 } else {
Russell King67721902014-06-26 15:23:00 +0100215 priv->ctl_rec &= ~(KIRKWOOD_RECCTL_ENABLE_MASK |
216 KIRKWOOD_RECCTL_SIZE_MASK);
Russell Kingd8d11ba2012-11-20 12:19:53 +0000217 priv->ctl_rec |= ctl_rec;
arnaud.patard@rtp-net.orgdfe4c932010-07-11 23:28:31 +0200218 }
219
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200220 writel(i2s_value, priv->io+i2s_reg);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200221
222 return 0;
223}
224
Russell King2fbc3822014-06-26 15:23:05 +0100225static unsigned kirkwood_i2s_play_mute(unsigned ctl)
226{
227 if (!(ctl & KIRKWOOD_PLAYCTL_I2S_EN))
228 ctl |= KIRKWOOD_PLAYCTL_I2S_MUTE;
229 if (!(ctl & KIRKWOOD_PLAYCTL_SPDIF_EN))
230 ctl |= KIRKWOOD_PLAYCTL_SPDIF_MUTE;
231 return ctl;
232}
233
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200234static int kirkwood_i2s_play_trigger(struct snd_pcm_substream *substream,
235 int cmd, struct snd_soc_dai *dai)
236{
Russell King920ec4e2014-06-26 15:23:20 +0100237 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000238 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
Russell King982b6042012-11-20 12:18:52 +0000239 uint32_t ctl, value;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200240
Russell King982b6042012-11-20 12:18:52 +0000241 ctl = readl(priv->io + KIRKWOOD_PLAYCTL);
Russell King4d2097e2014-06-26 15:23:10 +0100242 if ((ctl & KIRKWOOD_PLAYCTL_ENABLE_MASK) == 0) {
Russell King982b6042012-11-20 12:18:52 +0000243 unsigned timeout = 5000;
244 /*
245 * The Armada510 spec says that if we enter pause mode, the
246 * busy bit must be read back as clear _twice_. Make sure
247 * we respect that otherwise we get DMA underruns.
248 */
249 do {
250 value = ctl;
251 ctl = readl(priv->io + KIRKWOOD_PLAYCTL);
252 if (!((ctl | value) & KIRKWOOD_PLAYCTL_PLAY_BUSY))
253 break;
254 udelay(1);
255 } while (timeout--);
256
257 if ((ctl | value) & KIRKWOOD_PLAYCTL_PLAY_BUSY)
258 dev_notice(dai->dev, "timed out waiting for busy to deassert: %08x\n",
259 ctl);
260 }
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200261
262 switch (cmd) {
263 case SNDRV_PCM_TRIGGER_START:
Russell Kingd8d11ba2012-11-20 12:19:53 +0000264 /* configure */
265 ctl = priv->ctl_play;
Jean-Francois Moine4f6f1472013-11-25 20:19:05 +0100266 if (dai->id == 0)
267 ctl &= ~KIRKWOOD_PLAYCTL_SPDIF_EN; /* i2s */
268 else
269 ctl &= ~KIRKWOOD_PLAYCTL_I2S_EN; /* spdif */
Russell King2fbc3822014-06-26 15:23:05 +0100270 ctl = kirkwood_i2s_play_mute(ctl);
Russell Kingdb43b162013-08-04 20:26:03 +0100271 value = ctl & ~KIRKWOOD_PLAYCTL_ENABLE_MASK;
Russell Kingd8d11ba2012-11-20 12:19:53 +0000272 writel(value, priv->io + KIRKWOOD_PLAYCTL);
273
274 /* enable interrupts */
Russell King920ec4e2014-06-26 15:23:20 +0100275 if (!runtime->no_period_wakeup) {
276 value = readl(priv->io + KIRKWOOD_INT_MASK);
277 value |= KIRKWOOD_INT_CAUSE_PLAY_BYTES;
278 writel(value, priv->io + KIRKWOOD_INT_MASK);
279 }
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200280
Russell Kingd8d11ba2012-11-20 12:19:53 +0000281 /* enable playback */
Russell King982b6042012-11-20 12:18:52 +0000282 writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200283 break;
284
285 case SNDRV_PCM_TRIGGER_STOP:
286 /* stop audio, disable interrupts */
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200287 ctl |= KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE |
288 KIRKWOOD_PLAYCTL_SPDIF_MUTE;
Russell King982b6042012-11-20 12:18:52 +0000289 writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200290
291 value = readl(priv->io + KIRKWOOD_INT_MASK);
292 value &= ~KIRKWOOD_INT_CAUSE_PLAY_BYTES;
293 writel(value, priv->io + KIRKWOOD_INT_MASK);
294
295 /* disable all playbacks */
Russell Kingdb43b162013-08-04 20:26:03 +0100296 ctl &= ~KIRKWOOD_PLAYCTL_ENABLE_MASK;
Russell King982b6042012-11-20 12:18:52 +0000297 writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200298 break;
299
300 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
301 case SNDRV_PCM_TRIGGER_SUSPEND:
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200302 ctl |= KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE |
303 KIRKWOOD_PLAYCTL_SPDIF_MUTE;
Russell King982b6042012-11-20 12:18:52 +0000304 writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200305 break;
306
307 case SNDRV_PCM_TRIGGER_RESUME:
308 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200309 ctl &= ~(KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE |
310 KIRKWOOD_PLAYCTL_SPDIF_MUTE);
Russell King2fbc3822014-06-26 15:23:05 +0100311 ctl = kirkwood_i2s_play_mute(ctl);
Russell King982b6042012-11-20 12:18:52 +0000312 writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200313 break;
314
315 default:
316 return -EINVAL;
317 }
318
319 return 0;
320}
321
322static int kirkwood_i2s_rec_trigger(struct snd_pcm_substream *substream,
323 int cmd, struct snd_soc_dai *dai)
324{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000325 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
Russell Kingd8d11ba2012-11-20 12:19:53 +0000326 uint32_t ctl, value;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200327
328 value = readl(priv->io + KIRKWOOD_RECCTL);
329
330 switch (cmd) {
331 case SNDRV_PCM_TRIGGER_START:
Russell Kingd8d11ba2012-11-20 12:19:53 +0000332 /* configure */
333 ctl = priv->ctl_rec;
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200334 if (dai->id == 0)
335 ctl &= ~KIRKWOOD_RECCTL_SPDIF_EN; /* i2s */
336 else
337 ctl &= ~KIRKWOOD_RECCTL_I2S_EN; /* spdif */
338
Russell King52b896c2014-06-26 15:22:55 +0100339 value = ctl & ~KIRKWOOD_RECCTL_ENABLE_MASK;
Russell Kingd8d11ba2012-11-20 12:19:53 +0000340 writel(value, priv->io + KIRKWOOD_RECCTL);
341
342 /* enable interrupts */
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200343 value = readl(priv->io + KIRKWOOD_INT_MASK);
344 value |= KIRKWOOD_INT_CAUSE_REC_BYTES;
345 writel(value, priv->io + KIRKWOOD_INT_MASK);
346
Russell Kingd8d11ba2012-11-20 12:19:53 +0000347 /* enable record */
348 writel(ctl, priv->io + KIRKWOOD_RECCTL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200349 break;
350
351 case SNDRV_PCM_TRIGGER_STOP:
352 /* stop audio, disable interrupts */
353 value = readl(priv->io + KIRKWOOD_RECCTL);
arnaud.patard@rtp-net.orgb424ec92010-07-11 23:28:32 +0200354 value |= KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200355 writel(value, priv->io + KIRKWOOD_RECCTL);
356
357 value = readl(priv->io + KIRKWOOD_INT_MASK);
358 value &= ~KIRKWOOD_INT_CAUSE_REC_BYTES;
359 writel(value, priv->io + KIRKWOOD_INT_MASK);
360
361 /* disable all records */
362 value = readl(priv->io + KIRKWOOD_RECCTL);
Russell King52b896c2014-06-26 15:22:55 +0100363 value &= ~KIRKWOOD_RECCTL_ENABLE_MASK;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200364 writel(value, priv->io + KIRKWOOD_RECCTL);
365 break;
366
367 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
368 case SNDRV_PCM_TRIGGER_SUSPEND:
369 value = readl(priv->io + KIRKWOOD_RECCTL);
arnaud.patard@rtp-net.orgb424ec92010-07-11 23:28:32 +0200370 value |= KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200371 writel(value, priv->io + KIRKWOOD_RECCTL);
372 break;
373
374 case SNDRV_PCM_TRIGGER_RESUME:
375 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
376 value = readl(priv->io + KIRKWOOD_RECCTL);
arnaud.patard@rtp-net.orgb424ec92010-07-11 23:28:32 +0200377 value &= ~(KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200378 writel(value, priv->io + KIRKWOOD_RECCTL);
379 break;
380
381 default:
382 return -EINVAL;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200383 }
384
385 return 0;
386}
387
388static int kirkwood_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
389 struct snd_soc_dai *dai)
390{
391 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
392 return kirkwood_i2s_play_trigger(substream, cmd, dai);
393 else
394 return kirkwood_i2s_rec_trigger(substream, cmd, dai);
395
396 return 0;
397}
398
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200399static int kirkwood_i2s_init(struct kirkwood_dma_data *priv)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200400{
401 unsigned long value;
402 unsigned int reg_data;
403
404 /* put system in a "safe" state : */
405 /* disable audio interrupts */
406 writel(0xffffffff, priv->io + KIRKWOOD_INT_CAUSE);
407 writel(0, priv->io + KIRKWOOD_INT_MASK);
408
409 reg_data = readl(priv->io + 0x1200);
410 reg_data &= (~(0x333FF8));
411 reg_data |= 0x111D18;
412 writel(reg_data, priv->io + 0x1200);
413
414 msleep(500);
415
416 reg_data = readl(priv->io + 0x1200);
417 reg_data &= (~(0x333FF8));
418 reg_data |= 0x111D18;
419 writel(reg_data, priv->io + 0x1200);
420
421 /* disable playback/record */
422 value = readl(priv->io + KIRKWOOD_PLAYCTL);
Russell Kingdb43b162013-08-04 20:26:03 +0100423 value &= ~KIRKWOOD_PLAYCTL_ENABLE_MASK;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200424 writel(value, priv->io + KIRKWOOD_PLAYCTL);
425
426 value = readl(priv->io + KIRKWOOD_RECCTL);
Russell King52b896c2014-06-26 15:22:55 +0100427 value &= ~KIRKWOOD_RECCTL_ENABLE_MASK;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200428 writel(value, priv->io + KIRKWOOD_RECCTL);
429
430 return 0;
431
432}
433
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100434static const struct snd_soc_dai_ops kirkwood_i2s_dai_ops = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000435 .startup = kirkwood_i2s_startup,
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200436 .trigger = kirkwood_i2s_trigger,
437 .hw_params = kirkwood_i2s_hw_params,
438 .set_fmt = kirkwood_i2s_set_fmt,
439};
440
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200441static struct snd_soc_dai_driver kirkwood_i2s_dai[2] = {
442 {
443 .name = "i2s",
444 .id = 0,
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200445 .playback = {
446 .channels_min = 1,
447 .channels_max = 2,
Mark Brown9e12cbd2013-07-16 10:47:10 +0100448 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
449 SNDRV_PCM_RATE_96000,
Russell King363589b2012-11-20 12:20:34 +0000450 .formats = KIRKWOOD_I2S_FORMATS,
451 },
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200452 .capture = {
453 .channels_min = 1,
454 .channels_max = 2,
Mark Brown9e12cbd2013-07-16 10:47:10 +0100455 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
456 SNDRV_PCM_RATE_96000,
Russell King363589b2012-11-20 12:20:34 +0000457 .formats = KIRKWOOD_I2S_FORMATS,
458 },
459 .ops = &kirkwood_i2s_dai_ops,
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200460 },
461 {
462 .name = "spdif",
463 .id = 1,
464 .playback = {
465 .channels_min = 1,
466 .channels_max = 2,
467 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
468 SNDRV_PCM_RATE_96000,
Jean-Francois Moine1c195dd2013-11-26 10:41:40 +0100469 .formats = KIRKWOOD_SPDIF_FORMATS,
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200470 },
471 .capture = {
472 .channels_min = 1,
473 .channels_max = 2,
474 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
475 SNDRV_PCM_RATE_96000,
Jean-Francois Moine1c195dd2013-11-26 10:41:40 +0100476 .formats = KIRKWOOD_SPDIF_FORMATS,
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200477 },
478 .ops = &kirkwood_i2s_dai_ops,
479 },
Russell King363589b2012-11-20 12:20:34 +0000480};
481
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200482static struct snd_soc_dai_driver kirkwood_i2s_dai_extclk[2] = {
483 {
484 .name = "i2s",
485 .id = 0,
Russell King363589b2012-11-20 12:20:34 +0000486 .playback = {
487 .channels_min = 1,
488 .channels_max = 2,
Jean-Francois Moine02fc17c2013-11-27 21:10:24 +0100489 .rates = SNDRV_PCM_RATE_CONTINUOUS,
490 .rate_min = 5512,
491 .rate_max = 192000,
Russell King363589b2012-11-20 12:20:34 +0000492 .formats = KIRKWOOD_I2S_FORMATS,
493 },
494 .capture = {
495 .channels_min = 1,
496 .channels_max = 2,
Jean-Francois Moine02fc17c2013-11-27 21:10:24 +0100497 .rates = SNDRV_PCM_RATE_CONTINUOUS,
498 .rate_min = 5512,
499 .rate_max = 192000,
Russell King363589b2012-11-20 12:20:34 +0000500 .formats = KIRKWOOD_I2S_FORMATS,
501 },
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200502 .ops = &kirkwood_i2s_dai_ops,
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200503 },
504 {
505 .name = "spdif",
506 .id = 1,
507 .playback = {
508 .channels_min = 1,
509 .channels_max = 2,
Jean-Francois Moine02fc17c2013-11-27 21:10:24 +0100510 .rates = SNDRV_PCM_RATE_CONTINUOUS,
511 .rate_min = 5512,
512 .rate_max = 192000,
Jean-Francois Moine1c195dd2013-11-26 10:41:40 +0100513 .formats = KIRKWOOD_SPDIF_FORMATS,
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200514 },
515 .capture = {
516 .channels_min = 1,
517 .channels_max = 2,
Jean-Francois Moine02fc17c2013-11-27 21:10:24 +0100518 .rates = SNDRV_PCM_RATE_CONTINUOUS,
519 .rate_min = 5512,
520 .rate_max = 192000,
Jean-Francois Moine1c195dd2013-11-26 10:41:40 +0100521 .formats = KIRKWOOD_SPDIF_FORMATS,
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200522 },
523 .ops = &kirkwood_i2s_dai_ops,
524 },
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200525};
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200526
Kuninori Morimoto83d85f52013-03-21 03:32:39 -0700527static const struct snd_soc_component_driver kirkwood_i2s_component = {
528 .name = DRV_NAME,
529};
530
Bill Pemberton34e15fb2012-12-07 09:26:26 -0500531static int kirkwood_i2s_dev_probe(struct platform_device *pdev)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200532{
Russell King363589b2012-11-20 12:20:34 +0000533 struct kirkwood_asoc_platform_data *data = pdev->dev.platform_data;
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200534 struct snd_soc_dai_driver *soc_dai = kirkwood_i2s_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000535 struct kirkwood_dma_data *priv;
Russell King363589b2012-11-20 12:20:34 +0000536 struct resource *mem;
Jean-Francois Moineeb632312013-08-14 12:27:33 +0200537 struct device_node *np = pdev->dev.of_node;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200538 int err;
539
Russell Kingdbc517b2012-11-20 12:19:33 +0000540 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200541 if (!priv) {
542 dev_err(&pdev->dev, "allocation failed\n");
Russell Kingdbc517b2012-11-20 12:19:33 +0000543 return -ENOMEM;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200544 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000545 dev_set_drvdata(&pdev->dev, priv);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200546
547 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingb25b5aa2013-01-21 11:09:26 +0100548 priv->io = devm_ioremap_resource(&pdev->dev, mem);
549 if (IS_ERR(priv->io))
550 return PTR_ERR(priv->io);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200551
552 priv->irq = platform_get_irq(pdev, 0);
553 if (priv->irq <= 0) {
554 dev_err(&pdev->dev, "platform_get_irq failed\n");
Russell Kingdbc517b2012-11-20 12:19:33 +0000555 return -ENXIO;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200556 }
557
Jean-Francois Moineeb632312013-08-14 12:27:33 +0200558 if (np) {
559 priv->burst = 128; /* might be 32 or 128 */
560 } else if (data) {
561 priv->burst = data->burst;
562 } else {
563 dev_err(&pdev->dev, "no DT nor platform data ?!\n");
Russell Kingdbc517b2012-11-20 12:19:33 +0000564 return -EINVAL;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200565 }
566
Jean-Francois Moineeb632312013-08-14 12:27:33 +0200567 priv->clk = devm_clk_get(&pdev->dev, np ? "internal" : NULL);
Andrew Lunne919c712012-03-09 09:56:41 +0100568 if (IS_ERR(priv->clk)) {
569 dev_err(&pdev->dev, "no clock\n");
Russell Kingdbc517b2012-11-20 12:19:33 +0000570 return PTR_ERR(priv->clk);
Andrew Lunne919c712012-03-09 09:56:41 +0100571 }
Russell Kingdbc517b2012-11-20 12:19:33 +0000572
573 err = clk_prepare_enable(priv->clk);
574 if (err < 0)
575 return err;
Andrew Lunne919c712012-03-09 09:56:41 +0100576
Mark Brown4734dc92013-07-15 16:41:14 +0100577 priv->extclk = devm_clk_get(&pdev->dev, "extclk");
Jean-Francois Moine84aac6c2013-09-21 12:00:36 +0200578 if (IS_ERR(priv->extclk)) {
579 if (PTR_ERR(priv->extclk) == -EPROBE_DEFER)
580 return -EPROBE_DEFER;
581 } else {
Shawn Guoaaa6d062015-02-25 22:53:38 +0800582 if (clk_is_match(priv->extclk, priv->clk)) {
Russell Kingaf64d732013-08-04 20:23:03 +0100583 devm_clk_put(&pdev->dev, priv->extclk);
Russell Kingd8d11ba2012-11-20 12:19:53 +0000584 priv->extclk = ERR_PTR(-EINVAL);
585 } else {
586 dev_info(&pdev->dev, "found external clock\n");
587 clk_prepare_enable(priv->extclk);
Jean-Francois Moine99d8d3b2013-10-24 17:55:18 +0200588 soc_dai = kirkwood_i2s_dai_extclk;
Russell Kingd8d11ba2012-11-20 12:19:53 +0000589 }
590 }
591
592 /* Some sensible defaults - this reflects the powerup values */
593 priv->ctl_play = KIRKWOOD_PLAYCTL_SIZE_24;
594 priv->ctl_rec = KIRKWOOD_RECCTL_SIZE_24;
595
596 /* Select the burst size */
Jean-Francois Moineeb632312013-08-14 12:27:33 +0200597 if (priv->burst == 32) {
Russell Kingd8d11ba2012-11-20 12:19:53 +0000598 priv->ctl_play |= KIRKWOOD_PLAYCTL_BURST_32;
599 priv->ctl_rec |= KIRKWOOD_RECCTL_BURST_32;
600 } else {
601 priv->ctl_play |= KIRKWOOD_PLAYCTL_BURST_128;
602 priv->ctl_rec |= KIRKWOOD_RECCTL_BURST_128;
603 }
604
Kuninori Morimoto83d85f52013-03-21 03:32:39 -0700605 err = snd_soc_register_component(&pdev->dev, &kirkwood_i2s_component,
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200606 soc_dai, 2);
Russell King64ddf1f2013-08-04 20:27:03 +0100607 if (err) {
608 dev_err(&pdev->dev, "snd_soc_register_component failed\n");
609 goto err_component;
610 }
Simon Baatzbaffab22012-07-19 00:04:09 +0200611
Russell King64ddf1f2013-08-04 20:27:03 +0100612 err = snd_soc_register_platform(&pdev->dev, &kirkwood_soc_platform);
613 if (err) {
614 dev_err(&pdev->dev, "snd_soc_register_platform failed\n");
615 goto err_platform;
616 }
Jean-Francois Moine75b9b652013-10-21 10:50:49 +0200617
618 kirkwood_i2s_init(priv);
619
Russell King64ddf1f2013-08-04 20:27:03 +0100620 return 0;
621 err_platform:
622 snd_soc_unregister_component(&pdev->dev);
623 err_component:
Mark Brown4734dc92013-07-15 16:41:14 +0100624 if (!IS_ERR(priv->extclk))
Russell King363589b2012-11-20 12:20:34 +0000625 clk_disable_unprepare(priv->extclk);
Simon Baatzbaffab22012-07-19 00:04:09 +0200626 clk_disable_unprepare(priv->clk);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200627
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200628 return err;
629}
630
Bill Pemberton34e15fb2012-12-07 09:26:26 -0500631static int kirkwood_i2s_dev_remove(struct platform_device *pdev)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200632{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633 struct kirkwood_dma_data *priv = dev_get_drvdata(&pdev->dev);
634
Russell King64ddf1f2013-08-04 20:27:03 +0100635 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimoto83d85f52013-03-21 03:32:39 -0700636 snd_soc_unregister_component(&pdev->dev);
Andrew Lunne919c712012-03-09 09:56:41 +0100637
Mark Brown4734dc92013-07-15 16:41:14 +0100638 if (!IS_ERR(priv->extclk))
Russell King363589b2012-11-20 12:20:34 +0000639 clk_disable_unprepare(priv->extclk);
Andrew Lunne919c712012-03-09 09:56:41 +0100640 clk_disable_unprepare(priv->clk);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000641
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200642 return 0;
643}
644
Jean-Francois Moineeb632312013-08-14 12:27:33 +0200645#ifdef CONFIG_OF
Fabian Frederick7f2c52a2015-03-18 17:48:58 +0100646static const struct of_device_id mvebu_audio_of_match[] = {
Thomas Petazzonid098b2f2013-09-05 14:56:31 +0200647 { .compatible = "marvell,kirkwood-audio" },
648 { .compatible = "marvell,dove-audio" },
Thomas Petazzoni9a0d5112014-01-30 18:14:06 +0100649 { .compatible = "marvell,armada370-audio" },
Jean-Francois Moineeb632312013-08-14 12:27:33 +0200650 { }
651};
652MODULE_DEVICE_TABLE(of, mvebu_audio_of_match);
653#endif
654
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200655static struct platform_driver kirkwood_i2s_driver = {
656 .probe = kirkwood_i2s_dev_probe,
Bill Pemberton34e15fb2012-12-07 09:26:26 -0500657 .remove = kirkwood_i2s_dev_remove,
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200658 .driver = {
659 .name = DRV_NAME,
Jean-Francois Moineeb632312013-08-14 12:27:33 +0200660 .of_match_table = of_match_ptr(mvebu_audio_of_match),
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200661 },
662};
663
Axel Lin41b10222011-11-24 11:43:09 +0800664module_platform_driver(kirkwood_i2s_driver);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200665
666/* Module information */
Arnaud Patard (Rtp)69737892010-09-09 14:10:33 +0200667MODULE_AUTHOR("Arnaud Patard, <arnaud.patard@rtp-net.org>");
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200668MODULE_DESCRIPTION("Kirkwood I2S SoC Interface");
669MODULE_LICENSE("GPL");
Russell King64ddf1f2013-08-04 20:27:03 +0100670MODULE_ALIAS("platform:mvebu-audio");