xref: /linux/sound/soc/kirkwood/kirkwood-i2s.c (revision 34e15fbdaa7b1839941d42c5fea96329a53be135)
1f9b95980Sapatard@mandriva.com /*
2f9b95980Sapatard@mandriva.com  * kirkwood-i2s.c
3f9b95980Sapatard@mandriva.com  *
4f9b95980Sapatard@mandriva.com  * (c) 2010 Arnaud Patard <apatard@mandriva.com>
569737897SArnaud Patard (Rtp)  * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
6f9b95980Sapatard@mandriva.com  *
7f9b95980Sapatard@mandriva.com  *  This program is free software; you can redistribute  it and/or modify it
8f9b95980Sapatard@mandriva.com  *  under  the terms of  the GNU General  Public License as published by the
9f9b95980Sapatard@mandriva.com  *  Free Software Foundation;  either version 2 of the  License, or (at your
10f9b95980Sapatard@mandriva.com  *  option) any later version.
11f9b95980Sapatard@mandriva.com  */
12f9b95980Sapatard@mandriva.com 
13f9b95980Sapatard@mandriva.com #include <linux/init.h>
14f9b95980Sapatard@mandriva.com #include <linux/module.h>
15f9b95980Sapatard@mandriva.com #include <linux/platform_device.h>
16f9b95980Sapatard@mandriva.com #include <linux/io.h>
17f9b95980Sapatard@mandriva.com #include <linux/slab.h>
18f9b95980Sapatard@mandriva.com #include <linux/mbus.h>
19f9b95980Sapatard@mandriva.com #include <linux/delay.h>
20e919c716SAndrew Lunn #include <linux/clk.h>
21f9b95980Sapatard@mandriva.com #include <sound/pcm.h>
22f9b95980Sapatard@mandriva.com #include <sound/pcm_params.h>
23f9b95980Sapatard@mandriva.com #include <sound/soc.h>
24c02cecb9SArnd Bergmann #include <linux/platform_data/asoc-kirkwood.h>
25f9b95980Sapatard@mandriva.com #include "kirkwood.h"
26f9b95980Sapatard@mandriva.com 
27f9b95980Sapatard@mandriva.com #define DRV_NAME	"kirkwood-i2s"
28f9b95980Sapatard@mandriva.com 
29f9b95980Sapatard@mandriva.com #define KIRKWOOD_I2S_RATES \
30f9b95980Sapatard@mandriva.com 	(SNDRV_PCM_RATE_44100 | \
31f9b95980Sapatard@mandriva.com 	 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000)
32f9b95980Sapatard@mandriva.com #define KIRKWOOD_I2S_FORMATS \
33f9b95980Sapatard@mandriva.com 	(SNDRV_PCM_FMTBIT_S16_LE | \
34f9b95980Sapatard@mandriva.com 	 SNDRV_PCM_FMTBIT_S24_LE | \
35f9b95980Sapatard@mandriva.com 	 SNDRV_PCM_FMTBIT_S32_LE)
36f9b95980Sapatard@mandriva.com 
37f9b95980Sapatard@mandriva.com static int kirkwood_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
38f9b95980Sapatard@mandriva.com 		unsigned int fmt)
39f9b95980Sapatard@mandriva.com {
40f0fba2adSLiam Girdwood 	struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(cpu_dai);
41f9b95980Sapatard@mandriva.com 	unsigned long mask;
42f9b95980Sapatard@mandriva.com 	unsigned long value;
43f9b95980Sapatard@mandriva.com 
44f9b95980Sapatard@mandriva.com 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
45f9b95980Sapatard@mandriva.com 	case SND_SOC_DAIFMT_RIGHT_J:
46f9b95980Sapatard@mandriva.com 		mask = KIRKWOOD_I2S_CTL_RJ;
47f9b95980Sapatard@mandriva.com 		break;
48f9b95980Sapatard@mandriva.com 	case SND_SOC_DAIFMT_LEFT_J:
49f9b95980Sapatard@mandriva.com 		mask = KIRKWOOD_I2S_CTL_LJ;
50f9b95980Sapatard@mandriva.com 		break;
51f9b95980Sapatard@mandriva.com 	case SND_SOC_DAIFMT_I2S:
52f9b95980Sapatard@mandriva.com 		mask = KIRKWOOD_I2S_CTL_I2S;
53f9b95980Sapatard@mandriva.com 		break;
54f9b95980Sapatard@mandriva.com 	default:
55f9b95980Sapatard@mandriva.com 		return -EINVAL;
56f9b95980Sapatard@mandriva.com 	}
57f9b95980Sapatard@mandriva.com 
58f9b95980Sapatard@mandriva.com 	/*
59f9b95980Sapatard@mandriva.com 	 * Set same format for playback and record
60f9b95980Sapatard@mandriva.com 	 * This avoids some troubles.
61f9b95980Sapatard@mandriva.com 	 */
62f9b95980Sapatard@mandriva.com 	value = readl(priv->io+KIRKWOOD_I2S_PLAYCTL);
63f9b95980Sapatard@mandriva.com 	value &= ~KIRKWOOD_I2S_CTL_JUST_MASK;
64f9b95980Sapatard@mandriva.com 	value |= mask;
65f9b95980Sapatard@mandriva.com 	writel(value, priv->io+KIRKWOOD_I2S_PLAYCTL);
66f9b95980Sapatard@mandriva.com 
67f9b95980Sapatard@mandriva.com 	value = readl(priv->io+KIRKWOOD_I2S_RECCTL);
68f9b95980Sapatard@mandriva.com 	value &= ~KIRKWOOD_I2S_CTL_JUST_MASK;
69f9b95980Sapatard@mandriva.com 	value |= mask;
70f9b95980Sapatard@mandriva.com 	writel(value, priv->io+KIRKWOOD_I2S_RECCTL);
71f9b95980Sapatard@mandriva.com 
72f9b95980Sapatard@mandriva.com 	return 0;
73f9b95980Sapatard@mandriva.com }
74f9b95980Sapatard@mandriva.com 
75f9b95980Sapatard@mandriva.com static inline void kirkwood_set_dco(void __iomem *io, unsigned long rate)
76f9b95980Sapatard@mandriva.com {
77f9b95980Sapatard@mandriva.com 	unsigned long value;
78f9b95980Sapatard@mandriva.com 
79f9b95980Sapatard@mandriva.com 	value = KIRKWOOD_DCO_CTL_OFFSET_0;
80f9b95980Sapatard@mandriva.com 	switch (rate) {
81f9b95980Sapatard@mandriva.com 	default:
82f9b95980Sapatard@mandriva.com 	case 44100:
83f9b95980Sapatard@mandriva.com 		value |= KIRKWOOD_DCO_CTL_FREQ_11;
84f9b95980Sapatard@mandriva.com 		break;
85f9b95980Sapatard@mandriva.com 	case 48000:
86f9b95980Sapatard@mandriva.com 		value |= KIRKWOOD_DCO_CTL_FREQ_12;
87f9b95980Sapatard@mandriva.com 		break;
88f9b95980Sapatard@mandriva.com 	case 96000:
89f9b95980Sapatard@mandriva.com 		value |= KIRKWOOD_DCO_CTL_FREQ_24;
90f9b95980Sapatard@mandriva.com 		break;
91f9b95980Sapatard@mandriva.com 	}
92f9b95980Sapatard@mandriva.com 	writel(value, io + KIRKWOOD_DCO_CTL);
93f9b95980Sapatard@mandriva.com 
94f9b95980Sapatard@mandriva.com 	/* wait for dco locked */
95f9b95980Sapatard@mandriva.com 	do {
96f9b95980Sapatard@mandriva.com 		cpu_relax();
97f9b95980Sapatard@mandriva.com 		value = readl(io + KIRKWOOD_DCO_SPCR_STATUS);
982424d458SRussell King 		value &= KIRKWOOD_DCO_SPCR_STATUS_DCO_LOCK;
99f9b95980Sapatard@mandriva.com 	} while (value == 0);
100f9b95980Sapatard@mandriva.com }
101f9b95980Sapatard@mandriva.com 
102363589bfSRussell King static void kirkwood_set_rate(struct snd_soc_dai *dai,
103363589bfSRussell King 	struct kirkwood_dma_data *priv, unsigned long rate)
104363589bfSRussell King {
105363589bfSRussell King 	uint32_t clks_ctrl;
106363589bfSRussell King 
107363589bfSRussell King 	if (rate == 44100 || rate == 48000 || rate == 96000) {
108363589bfSRussell King 		/* use internal dco for supported rates */
109363589bfSRussell King 		dev_dbg(dai->dev, "%s: dco set rate = %lu\n",
110363589bfSRussell King 			__func__, rate);
111363589bfSRussell King 		kirkwood_set_dco(priv->io, rate);
112363589bfSRussell King 
113363589bfSRussell King 		clks_ctrl = KIRKWOOD_MCLK_SOURCE_DCO;
114363589bfSRussell King 	} else if (!IS_ERR(priv->extclk)) {
115363589bfSRussell King 		/* use optional external clk for other rates */
116363589bfSRussell King 		dev_dbg(dai->dev, "%s: extclk set rate = %lu -> %lu\n",
117363589bfSRussell King 			__func__, rate, 256 * rate);
118363589bfSRussell King 		clk_set_rate(priv->extclk, 256 * rate);
119363589bfSRussell King 
120363589bfSRussell King 		clks_ctrl = KIRKWOOD_MCLK_SOURCE_EXTCLK;
121363589bfSRussell King 	}
122363589bfSRussell King 	writel(clks_ctrl, priv->io + KIRKWOOD_CLOCKS_CTRL);
123363589bfSRussell King }
124363589bfSRussell King 
125f0fba2adSLiam Girdwood static int kirkwood_i2s_startup(struct snd_pcm_substream *substream,
126f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai)
127f0fba2adSLiam Girdwood {
128f0fba2adSLiam Girdwood 	struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
129f0fba2adSLiam Girdwood 
130f0fba2adSLiam Girdwood 	snd_soc_dai_set_dma_data(dai, substream, priv);
131f0fba2adSLiam Girdwood 	return 0;
132f0fba2adSLiam Girdwood }
133f0fba2adSLiam Girdwood 
134f9b95980Sapatard@mandriva.com static int kirkwood_i2s_hw_params(struct snd_pcm_substream *substream,
135f9b95980Sapatard@mandriva.com 				 struct snd_pcm_hw_params *params,
136f9b95980Sapatard@mandriva.com 				 struct snd_soc_dai *dai)
137f9b95980Sapatard@mandriva.com {
138f0fba2adSLiam Girdwood 	struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
139d8d11ba5SRussell King 	uint32_t ctl_play, ctl_rec;
140d8d11ba5SRussell King 	unsigned int i2s_reg;
141d8d11ba5SRussell King 	unsigned long i2s_value;
142f9b95980Sapatard@mandriva.com 
143f9b95980Sapatard@mandriva.com 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
144f9b95980Sapatard@mandriva.com 		i2s_reg = KIRKWOOD_I2S_PLAYCTL;
145f9b95980Sapatard@mandriva.com 	} else {
146f9b95980Sapatard@mandriva.com 		i2s_reg = KIRKWOOD_I2S_RECCTL;
147f9b95980Sapatard@mandriva.com 	}
148f9b95980Sapatard@mandriva.com 
149363589bfSRussell King 	kirkwood_set_rate(dai, priv, params_rate(params));
150f9b95980Sapatard@mandriva.com 
151f9b95980Sapatard@mandriva.com 	i2s_value = readl(priv->io+i2s_reg);
152f9b95980Sapatard@mandriva.com 	i2s_value &= ~KIRKWOOD_I2S_CTL_SIZE_MASK;
153f9b95980Sapatard@mandriva.com 
154f9b95980Sapatard@mandriva.com 	/*
155f9b95980Sapatard@mandriva.com 	 * Size settings in play/rec i2s control regs and play/rec control
156f9b95980Sapatard@mandriva.com 	 * regs must be the same.
157f9b95980Sapatard@mandriva.com 	 */
158f9b95980Sapatard@mandriva.com 	switch (params_format(params)) {
159f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_FORMAT_S16_LE:
160f9b95980Sapatard@mandriva.com 		i2s_value |= KIRKWOOD_I2S_CTL_SIZE_16;
161d8d11ba5SRussell King 		ctl_play = KIRKWOOD_PLAYCTL_SIZE_16_C |
162d8d11ba5SRussell King 			   KIRKWOOD_PLAYCTL_I2S_EN;
163d8d11ba5SRussell King 		ctl_rec = KIRKWOOD_RECCTL_SIZE_16_C |
164d8d11ba5SRussell King 			  KIRKWOOD_RECCTL_I2S_EN;
165f9b95980Sapatard@mandriva.com 		break;
166f9b95980Sapatard@mandriva.com 	/*
167f9b95980Sapatard@mandriva.com 	 * doesn't work... S20_3LE != kirkwood 20bit format ?
168f9b95980Sapatard@mandriva.com 	 *
169f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_FORMAT_S20_3LE:
170f9b95980Sapatard@mandriva.com 		i2s_value |= KIRKWOOD_I2S_CTL_SIZE_20;
171d8d11ba5SRussell King 		ctl_play = KIRKWOOD_PLAYCTL_SIZE_20 |
172d8d11ba5SRussell King 			   KIRKWOOD_PLAYCTL_I2S_EN;
173d8d11ba5SRussell King 		ctl_rec = KIRKWOOD_RECCTL_SIZE_20 |
174d8d11ba5SRussell King 			  KIRKWOOD_RECCTL_I2S_EN;
175f9b95980Sapatard@mandriva.com 		break;
176f9b95980Sapatard@mandriva.com 	*/
177f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_FORMAT_S24_LE:
178f9b95980Sapatard@mandriva.com 		i2s_value |= KIRKWOOD_I2S_CTL_SIZE_24;
179d8d11ba5SRussell King 		ctl_play = KIRKWOOD_PLAYCTL_SIZE_24 |
180d8d11ba5SRussell King 			   KIRKWOOD_PLAYCTL_I2S_EN;
181d8d11ba5SRussell King 		ctl_rec = KIRKWOOD_RECCTL_SIZE_24 |
182d8d11ba5SRussell King 			  KIRKWOOD_RECCTL_I2S_EN;
183f9b95980Sapatard@mandriva.com 		break;
184f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_FORMAT_S32_LE:
185f9b95980Sapatard@mandriva.com 		i2s_value |= KIRKWOOD_I2S_CTL_SIZE_32;
186d8d11ba5SRussell King 		ctl_play = KIRKWOOD_PLAYCTL_SIZE_32 |
187d8d11ba5SRussell King 			   KIRKWOOD_PLAYCTL_I2S_EN;
188d8d11ba5SRussell King 		ctl_rec = KIRKWOOD_RECCTL_SIZE_32 |
189d8d11ba5SRussell King 			  KIRKWOOD_RECCTL_I2S_EN;
190f9b95980Sapatard@mandriva.com 		break;
191f9b95980Sapatard@mandriva.com 	default:
192f9b95980Sapatard@mandriva.com 		return -EINVAL;
193f9b95980Sapatard@mandriva.com 	}
194dfe4c936Sarnaud.patard@rtp-net.org 
195dfe4c936Sarnaud.patard@rtp-net.org 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
196dfe4c936Sarnaud.patard@rtp-net.org 		if (params_channels(params) == 1)
197d8d11ba5SRussell King 			ctl_play |= KIRKWOOD_PLAYCTL_MONO_BOTH;
198dfe4c936Sarnaud.patard@rtp-net.org 		else
199d8d11ba5SRussell King 			ctl_play |= KIRKWOOD_PLAYCTL_MONO_OFF;
200d8d11ba5SRussell King 
201d8d11ba5SRussell King 		priv->ctl_play &= ~(KIRKWOOD_PLAYCTL_MONO_MASK |
202d8d11ba5SRussell King 				    KIRKWOOD_PLAYCTL_I2S_EN |
203d8d11ba5SRussell King 				    KIRKWOOD_PLAYCTL_SPDIF_EN |
204d8d11ba5SRussell King 				    KIRKWOOD_PLAYCTL_SIZE_MASK);
205d8d11ba5SRussell King 		priv->ctl_play |= ctl_play;
206d8d11ba5SRussell King 	} else {
207d8d11ba5SRussell King 		priv->ctl_rec &= ~KIRKWOOD_RECCTL_SIZE_MASK;
208d8d11ba5SRussell King 		priv->ctl_rec |= ctl_rec;
209dfe4c936Sarnaud.patard@rtp-net.org 	}
210dfe4c936Sarnaud.patard@rtp-net.org 
211f9b95980Sapatard@mandriva.com 	writel(i2s_value, priv->io+i2s_reg);
212f9b95980Sapatard@mandriva.com 
213f9b95980Sapatard@mandriva.com 	return 0;
214f9b95980Sapatard@mandriva.com }
215f9b95980Sapatard@mandriva.com 
216f9b95980Sapatard@mandriva.com static int kirkwood_i2s_play_trigger(struct snd_pcm_substream *substream,
217f9b95980Sapatard@mandriva.com 				int cmd, struct snd_soc_dai *dai)
218f9b95980Sapatard@mandriva.com {
219f0fba2adSLiam Girdwood 	struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
220982b604bSRussell King 	uint32_t ctl, value;
221f9b95980Sapatard@mandriva.com 
222982b604bSRussell King 	ctl = readl(priv->io + KIRKWOOD_PLAYCTL);
223982b604bSRussell King 	if (ctl & KIRKWOOD_PLAYCTL_PAUSE) {
224982b604bSRussell King 		unsigned timeout = 5000;
225f9b95980Sapatard@mandriva.com 		/*
226982b604bSRussell King 		 * The Armada510 spec says that if we enter pause mode, the
227982b604bSRussell King 		 * busy bit must be read back as clear _twice_.  Make sure
228982b604bSRussell King 		 * we respect that otherwise we get DMA underruns.
229f9b95980Sapatard@mandriva.com 		 */
230982b604bSRussell King 		do {
231982b604bSRussell King 			value = ctl;
232982b604bSRussell King 			ctl = readl(priv->io + KIRKWOOD_PLAYCTL);
233982b604bSRussell King 			if (!((ctl | value) & KIRKWOOD_PLAYCTL_PLAY_BUSY))
234982b604bSRussell King 				break;
235982b604bSRussell King 			udelay(1);
236982b604bSRussell King 		} while (timeout--);
237982b604bSRussell King 
238982b604bSRussell King 		if ((ctl | value) & KIRKWOOD_PLAYCTL_PLAY_BUSY)
239982b604bSRussell King 			dev_notice(dai->dev, "timed out waiting for busy to deassert: %08x\n",
240982b604bSRussell King 				   ctl);
241982b604bSRussell King 	}
242f9b95980Sapatard@mandriva.com 
243f9b95980Sapatard@mandriva.com 	switch (cmd) {
244f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_TRIGGER_START:
245d8d11ba5SRussell King 		/* configure */
246d8d11ba5SRussell King 		ctl = priv->ctl_play;
247d8d11ba5SRussell King 		value = ctl & ~(KIRKWOOD_PLAYCTL_I2S_EN |
248d8d11ba5SRussell King 				KIRKWOOD_PLAYCTL_SPDIF_EN);
249d8d11ba5SRussell King 		writel(value, priv->io + KIRKWOOD_PLAYCTL);
250d8d11ba5SRussell King 
251d8d11ba5SRussell King 		/* enable interrupts */
252f9b95980Sapatard@mandriva.com 		value = readl(priv->io + KIRKWOOD_INT_MASK);
253f9b95980Sapatard@mandriva.com 		value |= KIRKWOOD_INT_CAUSE_PLAY_BYTES;
254f9b95980Sapatard@mandriva.com 		writel(value, priv->io + KIRKWOOD_INT_MASK);
255f9b95980Sapatard@mandriva.com 
256d8d11ba5SRussell King 		/* enable playback */
257982b604bSRussell King 		writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
258f9b95980Sapatard@mandriva.com 		break;
259f9b95980Sapatard@mandriva.com 
260f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_TRIGGER_STOP:
261f9b95980Sapatard@mandriva.com 		/* stop audio, disable interrupts */
262982b604bSRussell King 		ctl |= KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE;
263982b604bSRussell King 		writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
264f9b95980Sapatard@mandriva.com 
265f9b95980Sapatard@mandriva.com 		value = readl(priv->io + KIRKWOOD_INT_MASK);
266f9b95980Sapatard@mandriva.com 		value &= ~KIRKWOOD_INT_CAUSE_PLAY_BYTES;
267f9b95980Sapatard@mandriva.com 		writel(value, priv->io + KIRKWOOD_INT_MASK);
268f9b95980Sapatard@mandriva.com 
269f9b95980Sapatard@mandriva.com 		/* disable all playbacks */
270982b604bSRussell King 		ctl &= ~(KIRKWOOD_PLAYCTL_I2S_EN | KIRKWOOD_PLAYCTL_SPDIF_EN);
271982b604bSRussell King 		writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
272f9b95980Sapatard@mandriva.com 		break;
273f9b95980Sapatard@mandriva.com 
274f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
275f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_TRIGGER_SUSPEND:
276982b604bSRussell King 		ctl |= KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE;
277982b604bSRussell King 		writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
278f9b95980Sapatard@mandriva.com 		break;
279f9b95980Sapatard@mandriva.com 
280f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_TRIGGER_RESUME:
281f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
282982b604bSRussell King 		ctl &= ~(KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE);
283982b604bSRussell King 		writel(ctl, priv->io + KIRKWOOD_PLAYCTL);
284f9b95980Sapatard@mandriva.com 		break;
285f9b95980Sapatard@mandriva.com 
286f9b95980Sapatard@mandriva.com 	default:
287f9b95980Sapatard@mandriva.com 		return -EINVAL;
288f9b95980Sapatard@mandriva.com 	}
289f9b95980Sapatard@mandriva.com 
290f9b95980Sapatard@mandriva.com 	return 0;
291f9b95980Sapatard@mandriva.com }
292f9b95980Sapatard@mandriva.com 
293f9b95980Sapatard@mandriva.com static int kirkwood_i2s_rec_trigger(struct snd_pcm_substream *substream,
294f9b95980Sapatard@mandriva.com 				int cmd, struct snd_soc_dai *dai)
295f9b95980Sapatard@mandriva.com {
296f0fba2adSLiam Girdwood 	struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
297d8d11ba5SRussell King 	uint32_t ctl, value;
298f9b95980Sapatard@mandriva.com 
299f9b95980Sapatard@mandriva.com 	value = readl(priv->io + KIRKWOOD_RECCTL);
300f9b95980Sapatard@mandriva.com 
301f9b95980Sapatard@mandriva.com 	switch (cmd) {
302f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_TRIGGER_START:
303d8d11ba5SRussell King 		/* configure */
304d8d11ba5SRussell King 		ctl = priv->ctl_rec;
305d8d11ba5SRussell King 		value = ctl & ~KIRKWOOD_RECCTL_I2S_EN;
306d8d11ba5SRussell King 		writel(value, priv->io + KIRKWOOD_RECCTL);
307d8d11ba5SRussell King 
308d8d11ba5SRussell King 		/* enable interrupts */
309f9b95980Sapatard@mandriva.com 		value = readl(priv->io + KIRKWOOD_INT_MASK);
310f9b95980Sapatard@mandriva.com 		value |= KIRKWOOD_INT_CAUSE_REC_BYTES;
311f9b95980Sapatard@mandriva.com 		writel(value, priv->io + KIRKWOOD_INT_MASK);
312f9b95980Sapatard@mandriva.com 
313d8d11ba5SRussell King 		/* enable record */
314d8d11ba5SRussell King 		writel(ctl, priv->io + KIRKWOOD_RECCTL);
315f9b95980Sapatard@mandriva.com 		break;
316f9b95980Sapatard@mandriva.com 
317f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_TRIGGER_STOP:
318f9b95980Sapatard@mandriva.com 		/* stop audio, disable interrupts */
319f9b95980Sapatard@mandriva.com 		value = readl(priv->io + KIRKWOOD_RECCTL);
320b424ec95Sarnaud.patard@rtp-net.org 		value |= KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE;
321f9b95980Sapatard@mandriva.com 		writel(value, priv->io + KIRKWOOD_RECCTL);
322f9b95980Sapatard@mandriva.com 
323f9b95980Sapatard@mandriva.com 		value = readl(priv->io + KIRKWOOD_INT_MASK);
324f9b95980Sapatard@mandriva.com 		value &= ~KIRKWOOD_INT_CAUSE_REC_BYTES;
325f9b95980Sapatard@mandriva.com 		writel(value, priv->io + KIRKWOOD_INT_MASK);
326f9b95980Sapatard@mandriva.com 
327f9b95980Sapatard@mandriva.com 		/* disable all records */
328f9b95980Sapatard@mandriva.com 		value = readl(priv->io + KIRKWOOD_RECCTL);
329f9b95980Sapatard@mandriva.com 		value &= ~(KIRKWOOD_RECCTL_I2S_EN | KIRKWOOD_RECCTL_SPDIF_EN);
330f9b95980Sapatard@mandriva.com 		writel(value, priv->io + KIRKWOOD_RECCTL);
331f9b95980Sapatard@mandriva.com 		break;
332f9b95980Sapatard@mandriva.com 
333f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
334f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_TRIGGER_SUSPEND:
335f9b95980Sapatard@mandriva.com 		value = readl(priv->io + KIRKWOOD_RECCTL);
336b424ec95Sarnaud.patard@rtp-net.org 		value |= KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE;
337f9b95980Sapatard@mandriva.com 		writel(value, priv->io + KIRKWOOD_RECCTL);
338f9b95980Sapatard@mandriva.com 		break;
339f9b95980Sapatard@mandriva.com 
340f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_TRIGGER_RESUME:
341f9b95980Sapatard@mandriva.com 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
342f9b95980Sapatard@mandriva.com 		value = readl(priv->io + KIRKWOOD_RECCTL);
343b424ec95Sarnaud.patard@rtp-net.org 		value &= ~(KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE);
344f9b95980Sapatard@mandriva.com 		writel(value, priv->io + KIRKWOOD_RECCTL);
345f9b95980Sapatard@mandriva.com 		break;
346f9b95980Sapatard@mandriva.com 
347f9b95980Sapatard@mandriva.com 	default:
348f9b95980Sapatard@mandriva.com 		return -EINVAL;
349f9b95980Sapatard@mandriva.com 	}
350f9b95980Sapatard@mandriva.com 
351f9b95980Sapatard@mandriva.com 	return 0;
352f9b95980Sapatard@mandriva.com }
353f9b95980Sapatard@mandriva.com 
354f9b95980Sapatard@mandriva.com static int kirkwood_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
355f9b95980Sapatard@mandriva.com 			       struct snd_soc_dai *dai)
356f9b95980Sapatard@mandriva.com {
357f9b95980Sapatard@mandriva.com 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
358f9b95980Sapatard@mandriva.com 		return kirkwood_i2s_play_trigger(substream, cmd, dai);
359f9b95980Sapatard@mandriva.com 	else
360f9b95980Sapatard@mandriva.com 		return kirkwood_i2s_rec_trigger(substream, cmd, dai);
361f9b95980Sapatard@mandriva.com 
362f9b95980Sapatard@mandriva.com 	return 0;
363f9b95980Sapatard@mandriva.com }
364f9b95980Sapatard@mandriva.com 
365f0fba2adSLiam Girdwood static int kirkwood_i2s_probe(struct snd_soc_dai *dai)
366f9b95980Sapatard@mandriva.com {
367f0fba2adSLiam Girdwood 	struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
368f9b95980Sapatard@mandriva.com 	unsigned long value;
369f9b95980Sapatard@mandriva.com 	unsigned int reg_data;
370f9b95980Sapatard@mandriva.com 
371f9b95980Sapatard@mandriva.com 	/* put system in a "safe" state : */
372f9b95980Sapatard@mandriva.com 	/* disable audio interrupts */
373f9b95980Sapatard@mandriva.com 	writel(0xffffffff, priv->io + KIRKWOOD_INT_CAUSE);
374f9b95980Sapatard@mandriva.com 	writel(0, priv->io + KIRKWOOD_INT_MASK);
375f9b95980Sapatard@mandriva.com 
376f9b95980Sapatard@mandriva.com 	reg_data = readl(priv->io + 0x1200);
377f9b95980Sapatard@mandriva.com 	reg_data &= (~(0x333FF8));
378f9b95980Sapatard@mandriva.com 	reg_data |= 0x111D18;
379f9b95980Sapatard@mandriva.com 	writel(reg_data, priv->io + 0x1200);
380f9b95980Sapatard@mandriva.com 
381f9b95980Sapatard@mandriva.com 	msleep(500);
382f9b95980Sapatard@mandriva.com 
383f9b95980Sapatard@mandriva.com 	reg_data = readl(priv->io + 0x1200);
384f9b95980Sapatard@mandriva.com 	reg_data &= (~(0x333FF8));
385f9b95980Sapatard@mandriva.com 	reg_data |= 0x111D18;
386f9b95980Sapatard@mandriva.com 	writel(reg_data, priv->io + 0x1200);
387f9b95980Sapatard@mandriva.com 
388f9b95980Sapatard@mandriva.com 	/* disable playback/record */
389f9b95980Sapatard@mandriva.com 	value = readl(priv->io + KIRKWOOD_PLAYCTL);
390f9b95980Sapatard@mandriva.com 	value &= ~(KIRKWOOD_PLAYCTL_I2S_EN|KIRKWOOD_PLAYCTL_SPDIF_EN);
391f9b95980Sapatard@mandriva.com 	writel(value, priv->io + KIRKWOOD_PLAYCTL);
392f9b95980Sapatard@mandriva.com 
393f9b95980Sapatard@mandriva.com 	value = readl(priv->io + KIRKWOOD_RECCTL);
394f9b95980Sapatard@mandriva.com 	value &= ~(KIRKWOOD_RECCTL_I2S_EN | KIRKWOOD_RECCTL_SPDIF_EN);
395f9b95980Sapatard@mandriva.com 	writel(value, priv->io + KIRKWOOD_RECCTL);
396f9b95980Sapatard@mandriva.com 
397f9b95980Sapatard@mandriva.com 	return 0;
398f9b95980Sapatard@mandriva.com 
399f9b95980Sapatard@mandriva.com }
400f9b95980Sapatard@mandriva.com 
401f0fba2adSLiam Girdwood static int kirkwood_i2s_remove(struct snd_soc_dai *dai)
402f9b95980Sapatard@mandriva.com {
403f0fba2adSLiam Girdwood 	return 0;
404f9b95980Sapatard@mandriva.com }
405f9b95980Sapatard@mandriva.com 
40685e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops kirkwood_i2s_dai_ops = {
407f0fba2adSLiam Girdwood 	.startup	= kirkwood_i2s_startup,
408f9b95980Sapatard@mandriva.com 	.trigger	= kirkwood_i2s_trigger,
409f9b95980Sapatard@mandriva.com 	.hw_params      = kirkwood_i2s_hw_params,
410f9b95980Sapatard@mandriva.com 	.set_fmt        = kirkwood_i2s_set_fmt,
411f9b95980Sapatard@mandriva.com };
412f9b95980Sapatard@mandriva.com 
413f9b95980Sapatard@mandriva.com 
414f0fba2adSLiam Girdwood static struct snd_soc_dai_driver kirkwood_i2s_dai = {
415f9b95980Sapatard@mandriva.com 	.probe = kirkwood_i2s_probe,
416f9b95980Sapatard@mandriva.com 	.remove = kirkwood_i2s_remove,
417f9b95980Sapatard@mandriva.com 	.playback = {
418f9b95980Sapatard@mandriva.com 		.channels_min = 1,
419f9b95980Sapatard@mandriva.com 		.channels_max = 2,
420f9b95980Sapatard@mandriva.com 		.rates = KIRKWOOD_I2S_RATES,
421363589bfSRussell King 		.formats = KIRKWOOD_I2S_FORMATS,
422363589bfSRussell King 	},
423f9b95980Sapatard@mandriva.com 	.capture = {
424f9b95980Sapatard@mandriva.com 		.channels_min = 1,
425f9b95980Sapatard@mandriva.com 		.channels_max = 2,
426f9b95980Sapatard@mandriva.com 		.rates = KIRKWOOD_I2S_RATES,
427363589bfSRussell King 		.formats = KIRKWOOD_I2S_FORMATS,
428363589bfSRussell King 	},
429363589bfSRussell King 	.ops = &kirkwood_i2s_dai_ops,
430363589bfSRussell King };
431363589bfSRussell King 
432363589bfSRussell King static struct snd_soc_dai_driver kirkwood_i2s_dai_extclk = {
433363589bfSRussell King 	.probe = kirkwood_i2s_probe,
434363589bfSRussell King 	.remove = kirkwood_i2s_remove,
435363589bfSRussell King 	.playback = {
436363589bfSRussell King 		.channels_min = 1,
437363589bfSRussell King 		.channels_max = 2,
438363589bfSRussell King 		.rates = SNDRV_PCM_RATE_8000_192000 |
439363589bfSRussell King 			 SNDRV_PCM_RATE_CONTINUOUS |
440363589bfSRussell King 			 SNDRV_PCM_RATE_KNOT,
441363589bfSRussell King 		.formats = KIRKWOOD_I2S_FORMATS,
442363589bfSRussell King 	},
443363589bfSRussell King 	.capture = {
444363589bfSRussell King 		.channels_min = 1,
445363589bfSRussell King 		.channels_max = 2,
446363589bfSRussell King 		.rates = SNDRV_PCM_RATE_8000_192000 |
447363589bfSRussell King 			 SNDRV_PCM_RATE_CONTINUOUS |
448363589bfSRussell King 			 SNDRV_PCM_RATE_KNOT,
449363589bfSRussell King 		.formats = KIRKWOOD_I2S_FORMATS,
450363589bfSRussell King 	},
451f9b95980Sapatard@mandriva.com 	.ops = &kirkwood_i2s_dai_ops,
452f9b95980Sapatard@mandriva.com };
453f9b95980Sapatard@mandriva.com 
454*34e15fbdSBill Pemberton static int kirkwood_i2s_dev_probe(struct platform_device *pdev)
455f9b95980Sapatard@mandriva.com {
456363589bfSRussell King 	struct kirkwood_asoc_platform_data *data = pdev->dev.platform_data;
457363589bfSRussell King 	struct snd_soc_dai_driver *soc_dai = &kirkwood_i2s_dai;
458f0fba2adSLiam Girdwood 	struct kirkwood_dma_data *priv;
459363589bfSRussell King 	struct resource *mem;
460f9b95980Sapatard@mandriva.com 	int err;
461f9b95980Sapatard@mandriva.com 
462dbc517bfSRussell King 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
463f9b95980Sapatard@mandriva.com 	if (!priv) {
464f9b95980Sapatard@mandriva.com 		dev_err(&pdev->dev, "allocation failed\n");
465dbc517bfSRussell King 		return -ENOMEM;
466f9b95980Sapatard@mandriva.com 	}
467f0fba2adSLiam Girdwood 	dev_set_drvdata(&pdev->dev, priv);
468f9b95980Sapatard@mandriva.com 
469f9b95980Sapatard@mandriva.com 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
470f9b95980Sapatard@mandriva.com 	if (!mem) {
471f9b95980Sapatard@mandriva.com 		dev_err(&pdev->dev, "platform_get_resource failed\n");
472dbc517bfSRussell King 		return -ENXIO;
473f9b95980Sapatard@mandriva.com 	}
474f9b95980Sapatard@mandriva.com 
475dbc517bfSRussell King 	priv->io = devm_request_and_ioremap(&pdev->dev, mem);
476f9b95980Sapatard@mandriva.com 	if (!priv->io) {
477dbc517bfSRussell King 		dev_err(&pdev->dev, "devm_request_and_ioremap failed\n");
478dbc517bfSRussell King 		return -ENOMEM;
479f9b95980Sapatard@mandriva.com 	}
480f9b95980Sapatard@mandriva.com 
481f9b95980Sapatard@mandriva.com 	priv->irq = platform_get_irq(pdev, 0);
482f9b95980Sapatard@mandriva.com 	if (priv->irq <= 0) {
483f9b95980Sapatard@mandriva.com 		dev_err(&pdev->dev, "platform_get_irq failed\n");
484dbc517bfSRussell King 		return -ENXIO;
485f9b95980Sapatard@mandriva.com 	}
486f9b95980Sapatard@mandriva.com 
487d4d9820bSAxel Lin 	if (!data) {
488f9b95980Sapatard@mandriva.com 		dev_err(&pdev->dev, "no platform data ?!\n");
489dbc517bfSRussell King 		return -EINVAL;
490f9b95980Sapatard@mandriva.com 	}
491f9b95980Sapatard@mandriva.com 
492f9b95980Sapatard@mandriva.com 	priv->burst = data->burst;
493f9b95980Sapatard@mandriva.com 
494dbc517bfSRussell King 	priv->clk = devm_clk_get(&pdev->dev, NULL);
495e919c716SAndrew Lunn 	if (IS_ERR(priv->clk)) {
496e919c716SAndrew Lunn 		dev_err(&pdev->dev, "no clock\n");
497dbc517bfSRussell King 		return PTR_ERR(priv->clk);
498e919c716SAndrew Lunn 	}
499dbc517bfSRussell King 
500dbc517bfSRussell King 	err = clk_prepare_enable(priv->clk);
501dbc517bfSRussell King 	if (err < 0)
502dbc517bfSRussell King 		return err;
503e919c716SAndrew Lunn 
504d8d11ba5SRussell King 	priv->extclk = clk_get(&pdev->dev, "extclk");
505d8d11ba5SRussell King 	if (!IS_ERR(priv->extclk)) {
506d8d11ba5SRussell King 		if (priv->extclk == priv->clk) {
507d8d11ba5SRussell King 			clk_put(priv->extclk);
508d8d11ba5SRussell King 			priv->extclk = ERR_PTR(-EINVAL);
509d8d11ba5SRussell King 		} else {
510d8d11ba5SRussell King 			dev_info(&pdev->dev, "found external clock\n");
511d8d11ba5SRussell King 			clk_prepare_enable(priv->extclk);
512d8d11ba5SRussell King 			soc_dai = &kirkwood_i2s_dai_extclk;
513d8d11ba5SRussell King 		}
514d8d11ba5SRussell King 	}
515d8d11ba5SRussell King 
516d8d11ba5SRussell King 	/* Some sensible defaults - this reflects the powerup values */
517d8d11ba5SRussell King 	priv->ctl_play = KIRKWOOD_PLAYCTL_SIZE_24;
518d8d11ba5SRussell King 	priv->ctl_rec = KIRKWOOD_RECCTL_SIZE_24;
519d8d11ba5SRussell King 
520d8d11ba5SRussell King 	/* Select the burst size */
521d8d11ba5SRussell King 	if (data->burst == 32) {
522d8d11ba5SRussell King 		priv->ctl_play |= KIRKWOOD_PLAYCTL_BURST_32;
523d8d11ba5SRussell King 		priv->ctl_rec |= KIRKWOOD_RECCTL_BURST_32;
524d8d11ba5SRussell King 	} else {
525d8d11ba5SRussell King 		priv->ctl_play |= KIRKWOOD_PLAYCTL_BURST_128;
526d8d11ba5SRussell King 		priv->ctl_rec |= KIRKWOOD_RECCTL_BURST_128;
527d8d11ba5SRussell King 	}
528d8d11ba5SRussell King 
529363589bfSRussell King 	err = snd_soc_register_dai(&pdev->dev, soc_dai);
530baffab28SSimon Baatz 	if (!err)
531baffab28SSimon Baatz 		return 0;
532baffab28SSimon Baatz 	dev_err(&pdev->dev, "snd_soc_register_dai failed\n");
533baffab28SSimon Baatz 
534363589bfSRussell King 	if (!IS_ERR(priv->extclk)) {
535363589bfSRussell King 		clk_disable_unprepare(priv->extclk);
536363589bfSRussell King 		clk_put(priv->extclk);
537363589bfSRussell King 	}
538baffab28SSimon Baatz 	clk_disable_unprepare(priv->clk);
539f9b95980Sapatard@mandriva.com 
540f9b95980Sapatard@mandriva.com 	return err;
541f9b95980Sapatard@mandriva.com }
542f9b95980Sapatard@mandriva.com 
543*34e15fbdSBill Pemberton static int kirkwood_i2s_dev_remove(struct platform_device *pdev)
544f9b95980Sapatard@mandriva.com {
545f0fba2adSLiam Girdwood 	struct kirkwood_dma_data *priv = dev_get_drvdata(&pdev->dev);
546f0fba2adSLiam Girdwood 
547f0fba2adSLiam Girdwood 	snd_soc_unregister_dai(&pdev->dev);
548e919c716SAndrew Lunn 
549363589bfSRussell King 	if (!IS_ERR(priv->extclk)) {
550363589bfSRussell King 		clk_disable_unprepare(priv->extclk);
551363589bfSRussell King 		clk_put(priv->extclk);
552363589bfSRussell King 	}
553e919c716SAndrew Lunn 	clk_disable_unprepare(priv->clk);
554f0fba2adSLiam Girdwood 
555f9b95980Sapatard@mandriva.com 	return 0;
556f9b95980Sapatard@mandriva.com }
557f9b95980Sapatard@mandriva.com 
558f9b95980Sapatard@mandriva.com static struct platform_driver kirkwood_i2s_driver = {
559f9b95980Sapatard@mandriva.com 	.probe  = kirkwood_i2s_dev_probe,
560*34e15fbdSBill Pemberton 	.remove = kirkwood_i2s_dev_remove,
561f9b95980Sapatard@mandriva.com 	.driver = {
562f9b95980Sapatard@mandriva.com 		.name = DRV_NAME,
563f9b95980Sapatard@mandriva.com 		.owner = THIS_MODULE,
564f9b95980Sapatard@mandriva.com 	},
565f9b95980Sapatard@mandriva.com };
566f9b95980Sapatard@mandriva.com 
56741b10225SAxel Lin module_platform_driver(kirkwood_i2s_driver);
568f9b95980Sapatard@mandriva.com 
569f9b95980Sapatard@mandriva.com /* Module information */
57069737897SArnaud Patard (Rtp) MODULE_AUTHOR("Arnaud Patard, <arnaud.patard@rtp-net.org>");
571f9b95980Sapatard@mandriva.com MODULE_DESCRIPTION("Kirkwood I2S SoC Interface");
572f9b95980Sapatard@mandriva.com MODULE_LICENSE("GPL");
573f9b95980Sapatard@mandriva.com MODULE_ALIAS("platform:kirkwood-i2s");
574