blob: 0f3d73d4ef48ea72d5d9b3c63d72f90e919ba7bc [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
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020036static int kirkwood_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
37 unsigned int fmt)
38{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000039 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(cpu_dai);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020040 unsigned long mask;
41 unsigned long value;
42
43 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
44 case SND_SOC_DAIFMT_RIGHT_J:
45 mask = KIRKWOOD_I2S_CTL_RJ;
46 break;
47 case SND_SOC_DAIFMT_LEFT_J:
48 mask = KIRKWOOD_I2S_CTL_LJ;
49 break;
50 case SND_SOC_DAIFMT_I2S:
51 mask = KIRKWOOD_I2S_CTL_I2S;
52 break;
53 default:
54 return -EINVAL;
55 }
56
57 /*
58 * Set same format for playback and record
59 * This avoids some troubles.
60 */
61 value = readl(priv->io+KIRKWOOD_I2S_PLAYCTL);
62 value &= ~KIRKWOOD_I2S_CTL_JUST_MASK;
63 value |= mask;
64 writel(value, priv->io+KIRKWOOD_I2S_PLAYCTL);
65
66 value = readl(priv->io+KIRKWOOD_I2S_RECCTL);
67 value &= ~KIRKWOOD_I2S_CTL_JUST_MASK;
68 value |= mask;
69 writel(value, priv->io+KIRKWOOD_I2S_RECCTL);
70
71 return 0;
72}
73
74static inline void kirkwood_set_dco(void __iomem *io, unsigned long rate)
75{
76 unsigned long value;
77
78 value = KIRKWOOD_DCO_CTL_OFFSET_0;
79 switch (rate) {
80 default:
81 case 44100:
82 value |= KIRKWOOD_DCO_CTL_FREQ_11;
83 break;
84 case 48000:
85 value |= KIRKWOOD_DCO_CTL_FREQ_12;
86 break;
87 case 96000:
88 value |= KIRKWOOD_DCO_CTL_FREQ_24;
89 break;
90 }
91 writel(value, io + KIRKWOOD_DCO_CTL);
92
93 /* wait for dco locked */
94 do {
95 cpu_relax();
96 value = readl(io + KIRKWOOD_DCO_SPCR_STATUS);
Russell King2424d452012-11-20 12:18:32 +000097 value &= KIRKWOOD_DCO_SPCR_STATUS_DCO_LOCK;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020098 } while (value == 0);
99}
100
Russell King363589b2012-11-20 12:20:34 +0000101static void kirkwood_set_rate(struct snd_soc_dai *dai,
102 struct kirkwood_dma_data *priv, unsigned long rate)
103{
104 uint32_t clks_ctrl;
105
106 if (rate == 44100 || rate == 48000 || rate == 96000) {
Jean-Francois Moine8a537f82013-07-16 08:47:47 +0200107 /* use internal dco for the supported rates
108 * defined in kirkwood_i2s_dai */
Russell King363589b2012-11-20 12:20:34 +0000109 dev_dbg(dai->dev, "%s: dco set rate = %lu\n",
110 __func__, rate);
111 kirkwood_set_dco(priv->io, rate);
112
113 clks_ctrl = KIRKWOOD_MCLK_SOURCE_DCO;
Jean-Francois Moine8a537f82013-07-16 08:47:47 +0200114 } else {
115 /* use the external clock for the other rates
116 * defined in kirkwood_i2s_dai_extclk */
Russell King363589b2012-11-20 12:20:34 +0000117 dev_dbg(dai->dev, "%s: extclk set rate = %lu -> %lu\n",
118 __func__, rate, 256 * rate);
119 clk_set_rate(priv->extclk, 256 * rate);
120
121 clks_ctrl = KIRKWOOD_MCLK_SOURCE_EXTCLK;
122 }
123 writel(clks_ctrl, priv->io + KIRKWOOD_CLOCKS_CTRL);
124}
125
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000126static int kirkwood_i2s_startup(struct snd_pcm_substream *substream,
127 struct snd_soc_dai *dai)
128{
129 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
130
131 snd_soc_dai_set_dma_data(dai, substream, priv);
132 return 0;
133}
134
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200135static int kirkwood_i2s_hw_params(struct snd_pcm_substream *substream,
136 struct snd_pcm_hw_params *params,
137 struct snd_soc_dai *dai)
138{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000139 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
Russell Kingd8d11ba2012-11-20 12:19:53 +0000140 uint32_t ctl_play, ctl_rec;
141 unsigned int i2s_reg;
142 unsigned long i2s_value;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200143
144 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
145 i2s_reg = KIRKWOOD_I2S_PLAYCTL;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200146 } else {
147 i2s_reg = KIRKWOOD_I2S_RECCTL;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200148 }
149
Russell King363589b2012-11-20 12:20:34 +0000150 kirkwood_set_rate(dai, priv, params_rate(params));
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200151
152 i2s_value = readl(priv->io+i2s_reg);
153 i2s_value &= ~KIRKWOOD_I2S_CTL_SIZE_MASK;
154
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200155 /*
156 * Size settings in play/rec i2s control regs and play/rec control
157 * regs must be the same.
158 */
159 switch (params_format(params)) {
160 case SNDRV_PCM_FORMAT_S16_LE:
161 i2s_value |= KIRKWOOD_I2S_CTL_SIZE_16;
Russell Kingd8d11ba2012-11-20 12:19:53 +0000162 ctl_play = KIRKWOOD_PLAYCTL_SIZE_16_C |
163 KIRKWOOD_PLAYCTL_I2S_EN;
164 ctl_rec = KIRKWOOD_RECCTL_SIZE_16_C |
165 KIRKWOOD_RECCTL_I2S_EN;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200166 break;
167 /*
168 * doesn't work... S20_3LE != kirkwood 20bit format ?
169 *
170 case SNDRV_PCM_FORMAT_S20_3LE:
171 i2s_value |= KIRKWOOD_I2S_CTL_SIZE_20;
Russell Kingd8d11ba2012-11-20 12:19:53 +0000172 ctl_play = KIRKWOOD_PLAYCTL_SIZE_20 |
173 KIRKWOOD_PLAYCTL_I2S_EN;
174 ctl_rec = KIRKWOOD_RECCTL_SIZE_20 |
175 KIRKWOOD_RECCTL_I2S_EN;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200176 break;
177 */
178 case SNDRV_PCM_FORMAT_S24_LE:
179 i2s_value |= KIRKWOOD_I2S_CTL_SIZE_24;
Russell Kingd8d11ba2012-11-20 12:19:53 +0000180 ctl_play = KIRKWOOD_PLAYCTL_SIZE_24 |
181 KIRKWOOD_PLAYCTL_I2S_EN;
182 ctl_rec = KIRKWOOD_RECCTL_SIZE_24 |
183 KIRKWOOD_RECCTL_I2S_EN;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200184 break;
185 case SNDRV_PCM_FORMAT_S32_LE:
186 i2s_value |= KIRKWOOD_I2S_CTL_SIZE_32;
Russell Kingd8d11ba2012-11-20 12:19:53 +0000187 ctl_play = KIRKWOOD_PLAYCTL_SIZE_32 |
188 KIRKWOOD_PLAYCTL_I2S_EN;
189 ctl_rec = KIRKWOOD_RECCTL_SIZE_32 |
190 KIRKWOOD_RECCTL_I2S_EN;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200191 break;
192 default:
193 return -EINVAL;
194 }
arnaud.patard@rtp-net.orgdfe4c932010-07-11 23:28:31 +0200195
196 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
arnaud.patard@rtp-net.orgdfe4c932010-07-11 23:28:31 +0200197 if (params_channels(params) == 1)
Russell Kingd8d11ba2012-11-20 12:19:53 +0000198 ctl_play |= KIRKWOOD_PLAYCTL_MONO_BOTH;
arnaud.patard@rtp-net.orgdfe4c932010-07-11 23:28:31 +0200199 else
Russell Kingd8d11ba2012-11-20 12:19:53 +0000200 ctl_play |= KIRKWOOD_PLAYCTL_MONO_OFF;
201
202 priv->ctl_play &= ~(KIRKWOOD_PLAYCTL_MONO_MASK |
Russell Kingdb43b162013-08-04 20:26:03 +0100203 KIRKWOOD_PLAYCTL_ENABLE_MASK |
Russell Kingd8d11ba2012-11-20 12:19:53 +0000204 KIRKWOOD_PLAYCTL_SIZE_MASK);
205 priv->ctl_play |= ctl_play;
206 } else {
207 priv->ctl_rec &= ~KIRKWOOD_RECCTL_SIZE_MASK;
208 priv->ctl_rec |= ctl_rec;
arnaud.patard@rtp-net.orgdfe4c932010-07-11 23:28:31 +0200209 }
210
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200211 writel(i2s_value, priv->io+i2s_reg);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200212
213 return 0;
214}
215
216static int kirkwood_i2s_play_trigger(struct snd_pcm_substream *substream,
217 int cmd, struct snd_soc_dai *dai)
218{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000219 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
Russell King982b6042012-11-20 12:18:52 +0000220 uint32_t ctl, value;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200221
Russell King982b6042012-11-20 12:18:52 +0000222 ctl = readl(priv->io + KIRKWOOD_PLAYCTL);
223 if (ctl & KIRKWOOD_PLAYCTL_PAUSE) {
224 unsigned timeout = 5000;
225 /*
226 * The Armada510 spec says that if we enter pause mode, the
227 * busy bit must be read back as clear _twice_. Make sure
228 * we respect that otherwise we get DMA underruns.
229 */
230 do {
231 value = ctl;
232 ctl = readl(priv->io + KIRKWOOD_PLAYCTL);
233 if (!((ctl | value) & KIRKWOOD_PLAYCTL_PLAY_BUSY))
234 break;
235 udelay(1);
236 } while (timeout--);
237
238 if ((ctl | value) & KIRKWOOD_PLAYCTL_PLAY_BUSY)
239 dev_notice(dai->dev, "timed out waiting for busy to deassert: %08x\n",
240 ctl);
241 }
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200242
243 switch (cmd) {
244 case SNDRV_PCM_TRIGGER_START:
Russell Kingd8d11ba2012-11-20 12:19:53 +0000245 /* configure */
246 ctl = priv->ctl_play;
Russell Kingdb43b162013-08-04 20:26:03 +0100247 value = ctl & ~KIRKWOOD_PLAYCTL_ENABLE_MASK;
Russell Kingd8d11ba2012-11-20 12:19:53 +0000248 writel(value, priv->io + KIRKWOOD_PLAYCTL);
249
250 /* enable interrupts */
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200251 value = readl(priv->io + KIRKWOOD_INT_MASK);
252 value |= KIRKWOOD_INT_CAUSE_PLAY_BYTES;
253 writel(value, priv->io + KIRKWOOD_INT_MASK);
254
Russell Kingd8d11ba2012-11-20 12:19:53 +0000255 /* enable playback */
Russell King982b6042012-11-20 12:18:52 +0000256 writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200257 break;
258
259 case SNDRV_PCM_TRIGGER_STOP:
260 /* stop audio, disable interrupts */
Russell King982b6042012-11-20 12:18:52 +0000261 ctl |= KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE;
262 writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200263
264 value = readl(priv->io + KIRKWOOD_INT_MASK);
265 value &= ~KIRKWOOD_INT_CAUSE_PLAY_BYTES;
266 writel(value, priv->io + KIRKWOOD_INT_MASK);
267
268 /* disable all playbacks */
Russell Kingdb43b162013-08-04 20:26:03 +0100269 ctl &= ~KIRKWOOD_PLAYCTL_ENABLE_MASK;
Russell King982b6042012-11-20 12:18:52 +0000270 writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200271 break;
272
273 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
274 case SNDRV_PCM_TRIGGER_SUSPEND:
Russell King982b6042012-11-20 12:18:52 +0000275 ctl |= KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE;
276 writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200277 break;
278
279 case SNDRV_PCM_TRIGGER_RESUME:
280 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Russell King982b6042012-11-20 12:18:52 +0000281 ctl &= ~(KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE);
282 writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200283 break;
284
285 default:
286 return -EINVAL;
287 }
288
289 return 0;
290}
291
292static int kirkwood_i2s_rec_trigger(struct snd_pcm_substream *substream,
293 int cmd, struct snd_soc_dai *dai)
294{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000295 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
Russell Kingd8d11ba2012-11-20 12:19:53 +0000296 uint32_t ctl, value;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200297
298 value = readl(priv->io + KIRKWOOD_RECCTL);
299
300 switch (cmd) {
301 case SNDRV_PCM_TRIGGER_START:
Russell Kingd8d11ba2012-11-20 12:19:53 +0000302 /* configure */
303 ctl = priv->ctl_rec;
304 value = ctl & ~KIRKWOOD_RECCTL_I2S_EN;
305 writel(value, priv->io + KIRKWOOD_RECCTL);
306
307 /* enable interrupts */
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200308 value = readl(priv->io + KIRKWOOD_INT_MASK);
309 value |= KIRKWOOD_INT_CAUSE_REC_BYTES;
310 writel(value, priv->io + KIRKWOOD_INT_MASK);
311
Russell Kingd8d11ba2012-11-20 12:19:53 +0000312 /* enable record */
313 writel(ctl, priv->io + KIRKWOOD_RECCTL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200314 break;
315
316 case SNDRV_PCM_TRIGGER_STOP:
317 /* stop audio, disable interrupts */
318 value = readl(priv->io + KIRKWOOD_RECCTL);
arnaud.patard@rtp-net.orgb424ec92010-07-11 23:28:32 +0200319 value |= KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200320 writel(value, priv->io + KIRKWOOD_RECCTL);
321
322 value = readl(priv->io + KIRKWOOD_INT_MASK);
323 value &= ~KIRKWOOD_INT_CAUSE_REC_BYTES;
324 writel(value, priv->io + KIRKWOOD_INT_MASK);
325
326 /* disable all records */
327 value = readl(priv->io + KIRKWOOD_RECCTL);
328 value &= ~(KIRKWOOD_RECCTL_I2S_EN | KIRKWOOD_RECCTL_SPDIF_EN);
329 writel(value, priv->io + KIRKWOOD_RECCTL);
330 break;
331
332 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
333 case SNDRV_PCM_TRIGGER_SUSPEND:
334 value = readl(priv->io + KIRKWOOD_RECCTL);
arnaud.patard@rtp-net.orgb424ec92010-07-11 23:28:32 +0200335 value |= KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200336 writel(value, priv->io + KIRKWOOD_RECCTL);
337 break;
338
339 case SNDRV_PCM_TRIGGER_RESUME:
340 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
341 value = readl(priv->io + KIRKWOOD_RECCTL);
arnaud.patard@rtp-net.orgb424ec92010-07-11 23:28:32 +0200342 value &= ~(KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200343 writel(value, priv->io + KIRKWOOD_RECCTL);
344 break;
345
346 default:
347 return -EINVAL;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200348 }
349
350 return 0;
351}
352
353static int kirkwood_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
354 struct snd_soc_dai *dai)
355{
356 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
357 return kirkwood_i2s_play_trigger(substream, cmd, dai);
358 else
359 return kirkwood_i2s_rec_trigger(substream, cmd, dai);
360
361 return 0;
362}
363
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000364static int kirkwood_i2s_probe(struct snd_soc_dai *dai)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200365{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000366 struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200367 unsigned long value;
368 unsigned int reg_data;
369
370 /* put system in a "safe" state : */
371 /* disable audio interrupts */
372 writel(0xffffffff, priv->io + KIRKWOOD_INT_CAUSE);
373 writel(0, priv->io + KIRKWOOD_INT_MASK);
374
375 reg_data = readl(priv->io + 0x1200);
376 reg_data &= (~(0x333FF8));
377 reg_data |= 0x111D18;
378 writel(reg_data, priv->io + 0x1200);
379
380 msleep(500);
381
382 reg_data = readl(priv->io + 0x1200);
383 reg_data &= (~(0x333FF8));
384 reg_data |= 0x111D18;
385 writel(reg_data, priv->io + 0x1200);
386
387 /* disable playback/record */
388 value = readl(priv->io + KIRKWOOD_PLAYCTL);
Russell Kingdb43b162013-08-04 20:26:03 +0100389 value &= ~KIRKWOOD_PLAYCTL_ENABLE_MASK;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200390 writel(value, priv->io + KIRKWOOD_PLAYCTL);
391
392 value = readl(priv->io + KIRKWOOD_RECCTL);
393 value &= ~(KIRKWOOD_RECCTL_I2S_EN | KIRKWOOD_RECCTL_SPDIF_EN);
394 writel(value, priv->io + KIRKWOOD_RECCTL);
395
396 return 0;
397
398}
399
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100400static const struct snd_soc_dai_ops kirkwood_i2s_dai_ops = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000401 .startup = kirkwood_i2s_startup,
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200402 .trigger = kirkwood_i2s_trigger,
403 .hw_params = kirkwood_i2s_hw_params,
404 .set_fmt = kirkwood_i2s_set_fmt,
405};
406
407
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000408static struct snd_soc_dai_driver kirkwood_i2s_dai = {
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200409 .probe = kirkwood_i2s_probe,
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200410 .playback = {
411 .channels_min = 1,
412 .channels_max = 2,
Mark Brown9e12cbd2013-07-16 10:47:10 +0100413 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
414 SNDRV_PCM_RATE_96000,
Russell King363589b2012-11-20 12:20:34 +0000415 .formats = KIRKWOOD_I2S_FORMATS,
416 },
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200417 .capture = {
418 .channels_min = 1,
419 .channels_max = 2,
Mark Brown9e12cbd2013-07-16 10:47:10 +0100420 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
421 SNDRV_PCM_RATE_96000,
Russell King363589b2012-11-20 12:20:34 +0000422 .formats = KIRKWOOD_I2S_FORMATS,
423 },
424 .ops = &kirkwood_i2s_dai_ops,
425};
426
427static struct snd_soc_dai_driver kirkwood_i2s_dai_extclk = {
428 .probe = kirkwood_i2s_probe,
Russell King363589b2012-11-20 12:20:34 +0000429 .playback = {
430 .channels_min = 1,
431 .channels_max = 2,
432 .rates = SNDRV_PCM_RATE_8000_192000 |
433 SNDRV_PCM_RATE_CONTINUOUS |
434 SNDRV_PCM_RATE_KNOT,
435 .formats = KIRKWOOD_I2S_FORMATS,
436 },
437 .capture = {
438 .channels_min = 1,
439 .channels_max = 2,
440 .rates = SNDRV_PCM_RATE_8000_192000 |
441 SNDRV_PCM_RATE_CONTINUOUS |
442 SNDRV_PCM_RATE_KNOT,
443 .formats = KIRKWOOD_I2S_FORMATS,
444 },
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200445 .ops = &kirkwood_i2s_dai_ops,
446};
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200447
Kuninori Morimoto83d85f52013-03-21 03:32:39 -0700448static const struct snd_soc_component_driver kirkwood_i2s_component = {
449 .name = DRV_NAME,
450};
451
Bill Pemberton34e15fb2012-12-07 09:26:26 -0500452static int kirkwood_i2s_dev_probe(struct platform_device *pdev)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200453{
Russell King363589b2012-11-20 12:20:34 +0000454 struct kirkwood_asoc_platform_data *data = pdev->dev.platform_data;
455 struct snd_soc_dai_driver *soc_dai = &kirkwood_i2s_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000456 struct kirkwood_dma_data *priv;
Russell King363589b2012-11-20 12:20:34 +0000457 struct resource *mem;
Jean-Francois Moineeb632312013-08-14 12:27:33 +0200458 struct device_node *np = pdev->dev.of_node;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200459 int err;
460
Russell Kingdbc517b2012-11-20 12:19:33 +0000461 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200462 if (!priv) {
463 dev_err(&pdev->dev, "allocation failed\n");
Russell Kingdbc517b2012-11-20 12:19:33 +0000464 return -ENOMEM;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200465 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000466 dev_set_drvdata(&pdev->dev, priv);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200467
468 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingb25b5aa2013-01-21 11:09:26 +0100469 priv->io = devm_ioremap_resource(&pdev->dev, mem);
470 if (IS_ERR(priv->io))
471 return PTR_ERR(priv->io);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200472
473 priv->irq = platform_get_irq(pdev, 0);
474 if (priv->irq <= 0) {
475 dev_err(&pdev->dev, "platform_get_irq failed\n");
Russell Kingdbc517b2012-11-20 12:19:33 +0000476 return -ENXIO;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200477 }
478
Jean-Francois Moineeb632312013-08-14 12:27:33 +0200479 if (np) {
480 priv->burst = 128; /* might be 32 or 128 */
481 } else if (data) {
482 priv->burst = data->burst;
483 } else {
484 dev_err(&pdev->dev, "no DT nor platform data ?!\n");
Russell Kingdbc517b2012-11-20 12:19:33 +0000485 return -EINVAL;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200486 }
487
Jean-Francois Moineeb632312013-08-14 12:27:33 +0200488 priv->clk = devm_clk_get(&pdev->dev, np ? "internal" : NULL);
Andrew Lunne919c712012-03-09 09:56:41 +0100489 if (IS_ERR(priv->clk)) {
490 dev_err(&pdev->dev, "no clock\n");
Russell Kingdbc517b2012-11-20 12:19:33 +0000491 return PTR_ERR(priv->clk);
Andrew Lunne919c712012-03-09 09:56:41 +0100492 }
Russell Kingdbc517b2012-11-20 12:19:33 +0000493
494 err = clk_prepare_enable(priv->clk);
495 if (err < 0)
496 return err;
Andrew Lunne919c712012-03-09 09:56:41 +0100497
Mark Brown4734dc92013-07-15 16:41:14 +0100498 priv->extclk = devm_clk_get(&pdev->dev, "extclk");
Russell Kingd8d11ba2012-11-20 12:19:53 +0000499 if (!IS_ERR(priv->extclk)) {
500 if (priv->extclk == priv->clk) {
Russell Kingaf64d732013-08-04 20:23:03 +0100501 devm_clk_put(&pdev->dev, priv->extclk);
Russell Kingd8d11ba2012-11-20 12:19:53 +0000502 priv->extclk = ERR_PTR(-EINVAL);
503 } else {
504 dev_info(&pdev->dev, "found external clock\n");
505 clk_prepare_enable(priv->extclk);
506 soc_dai = &kirkwood_i2s_dai_extclk;
507 }
508 }
509
510 /* Some sensible defaults - this reflects the powerup values */
511 priv->ctl_play = KIRKWOOD_PLAYCTL_SIZE_24;
512 priv->ctl_rec = KIRKWOOD_RECCTL_SIZE_24;
513
514 /* Select the burst size */
Jean-Francois Moineeb632312013-08-14 12:27:33 +0200515 if (priv->burst == 32) {
Russell Kingd8d11ba2012-11-20 12:19:53 +0000516 priv->ctl_play |= KIRKWOOD_PLAYCTL_BURST_32;
517 priv->ctl_rec |= KIRKWOOD_RECCTL_BURST_32;
518 } else {
519 priv->ctl_play |= KIRKWOOD_PLAYCTL_BURST_128;
520 priv->ctl_rec |= KIRKWOOD_RECCTL_BURST_128;
521 }
522
Kuninori Morimoto83d85f52013-03-21 03:32:39 -0700523 err = snd_soc_register_component(&pdev->dev, &kirkwood_i2s_component,
524 soc_dai, 1);
Russell King64ddf1f2013-08-04 20:27:03 +0100525 if (err) {
526 dev_err(&pdev->dev, "snd_soc_register_component failed\n");
527 goto err_component;
528 }
Simon Baatzbaffab22012-07-19 00:04:09 +0200529
Russell King64ddf1f2013-08-04 20:27:03 +0100530 err = snd_soc_register_platform(&pdev->dev, &kirkwood_soc_platform);
531 if (err) {
532 dev_err(&pdev->dev, "snd_soc_register_platform failed\n");
533 goto err_platform;
534 }
535 return 0;
536 err_platform:
537 snd_soc_unregister_component(&pdev->dev);
538 err_component:
Mark Brown4734dc92013-07-15 16:41:14 +0100539 if (!IS_ERR(priv->extclk))
Russell King363589b2012-11-20 12:20:34 +0000540 clk_disable_unprepare(priv->extclk);
Simon Baatzbaffab22012-07-19 00:04:09 +0200541 clk_disable_unprepare(priv->clk);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200542
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200543 return err;
544}
545
Bill Pemberton34e15fb2012-12-07 09:26:26 -0500546static int kirkwood_i2s_dev_remove(struct platform_device *pdev)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200547{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548 struct kirkwood_dma_data *priv = dev_get_drvdata(&pdev->dev);
549
Russell King64ddf1f2013-08-04 20:27:03 +0100550 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimoto83d85f52013-03-21 03:32:39 -0700551 snd_soc_unregister_component(&pdev->dev);
Andrew Lunne919c712012-03-09 09:56:41 +0100552
Mark Brown4734dc92013-07-15 16:41:14 +0100553 if (!IS_ERR(priv->extclk))
Russell King363589b2012-11-20 12:20:34 +0000554 clk_disable_unprepare(priv->extclk);
Andrew Lunne919c712012-03-09 09:56:41 +0100555 clk_disable_unprepare(priv->clk);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000556
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200557 return 0;
558}
559
Jean-Francois Moineeb632312013-08-14 12:27:33 +0200560#ifdef CONFIG_OF
561static struct of_device_id mvebu_audio_of_match[] = {
Thomas Petazzonid098b2f2013-09-05 14:56:31 +0200562 { .compatible = "marvell,kirkwood-audio" },
563 { .compatible = "marvell,dove-audio" },
Jean-Francois Moineeb632312013-08-14 12:27:33 +0200564 { }
565};
566MODULE_DEVICE_TABLE(of, mvebu_audio_of_match);
567#endif
568
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200569static struct platform_driver kirkwood_i2s_driver = {
570 .probe = kirkwood_i2s_dev_probe,
Bill Pemberton34e15fb2012-12-07 09:26:26 -0500571 .remove = kirkwood_i2s_dev_remove,
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200572 .driver = {
573 .name = DRV_NAME,
574 .owner = THIS_MODULE,
Jean-Francois Moineeb632312013-08-14 12:27:33 +0200575 .of_match_table = of_match_ptr(mvebu_audio_of_match),
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200576 },
577};
578
Axel Lin41b10222011-11-24 11:43:09 +0800579module_platform_driver(kirkwood_i2s_driver);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200580
581/* Module information */
Arnaud Patard (Rtp)69737892010-09-09 14:10:33 +0200582MODULE_AUTHOR("Arnaud Patard, <arnaud.patard@rtp-net.org>");
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200583MODULE_DESCRIPTION("Kirkwood I2S SoC Interface");
584MODULE_LICENSE("GPL");
Russell King64ddf1f2013-08-04 20:27:03 +0100585MODULE_ALIAS("platform:mvebu-audio");