xref: /linux/sound/soc/spear/spdif_out.c (revision 1a2ac6d7ecdcde74a4e16f31de64124160fc7237)
1 /*
2  * ALSA SoC SPDIF Out Audio Layer for spear processors
3  *
4  * Copyright (C) 2012 ST Microelectronics
5  * Vipin Kumar <vipin.kumar@st.com>
6  *
7  * This file is licensed under the terms of the GNU General Public
8  * License version 2. This program is licensed "as is" without any
9  * warranty of any kind, whether express or implied.
10  */
11 
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/io.h>
18 #include <linux/ioport.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <sound/dmaengine_pcm.h>
22 #include <sound/soc.h>
23 #include <sound/spear_dma.h>
24 #include <sound/spear_spdif.h>
25 #include "spdif_out_regs.h"
26 #include "spear_pcm.h"
27 
28 struct spdif_out_params {
29 	u32 rate;
30 	u32 core_freq;
31 	u32 mute;
32 };
33 
34 struct spdif_out_dev {
35 	struct clk *clk;
36 	struct spear_dma_data dma_params;
37 	struct spdif_out_params saved_params;
38 	u32 running;
39 	void __iomem *io_base;
40 	struct snd_dmaengine_dai_dma_data dma_params_tx;
41 	struct snd_dmaengine_pcm_config config;
42 };
43 
44 static void spdif_out_configure(struct spdif_out_dev *host)
45 {
46 	writel(SPDIF_OUT_RESET, host->io_base + SPDIF_OUT_SOFT_RST);
47 	mdelay(1);
48 	writel(readl(host->io_base + SPDIF_OUT_SOFT_RST) & ~SPDIF_OUT_RESET,
49 			host->io_base + SPDIF_OUT_SOFT_RST);
50 
51 	writel(SPDIF_OUT_FDMA_TRIG_16 | SPDIF_OUT_MEMFMT_16_16 |
52 			SPDIF_OUT_VALID_HW | SPDIF_OUT_USER_HW |
53 			SPDIF_OUT_CHNLSTA_HW | SPDIF_OUT_PARITY_HW,
54 			host->io_base + SPDIF_OUT_CFG);
55 
56 	writel(0x7F, host->io_base + SPDIF_OUT_INT_STA_CLR);
57 	writel(0x7F, host->io_base + SPDIF_OUT_INT_EN_CLR);
58 }
59 
60 static int spdif_out_startup(struct snd_pcm_substream *substream,
61 		struct snd_soc_dai *cpu_dai)
62 {
63 	struct spdif_out_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
64 	int ret;
65 
66 	if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
67 		return -EINVAL;
68 
69 	ret = clk_enable(host->clk);
70 	if (ret)
71 		return ret;
72 
73 	host->running = true;
74 	spdif_out_configure(host);
75 
76 	return 0;
77 }
78 
79 static void spdif_out_shutdown(struct snd_pcm_substream *substream,
80 		struct snd_soc_dai *dai)
81 {
82 	struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
83 
84 	if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
85 		return;
86 
87 	clk_disable(host->clk);
88 	host->running = false;
89 }
90 
91 static void spdif_out_clock(struct spdif_out_dev *host, u32 core_freq,
92 		u32 rate)
93 {
94 	u32 divider, ctrl;
95 
96 	clk_set_rate(host->clk, core_freq);
97 	divider = DIV_ROUND_CLOSEST(clk_get_rate(host->clk), (rate * 128));
98 
99 	ctrl = readl(host->io_base + SPDIF_OUT_CTRL);
100 	ctrl &= ~SPDIF_DIVIDER_MASK;
101 	ctrl |= (divider << SPDIF_DIVIDER_SHIFT) & SPDIF_DIVIDER_MASK;
102 	writel(ctrl, host->io_base + SPDIF_OUT_CTRL);
103 }
104 
105 static int spdif_out_hw_params(struct snd_pcm_substream *substream,
106 		struct snd_pcm_hw_params *params,
107 		struct snd_soc_dai *dai)
108 {
109 	struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
110 	u32 rate, core_freq;
111 
112 	if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
113 		return -EINVAL;
114 
115 	rate = params_rate(params);
116 
117 	switch (rate) {
118 	case 8000:
119 	case 16000:
120 	case 32000:
121 	case 64000:
122 		/*
123 		 * The clock is multiplied by 10 to bring it to feasible range
124 		 * of frequencies for sscg
125 		 */
126 		core_freq = 64000 * 128 * 10;	/* 81.92 MHz */
127 		break;
128 	case 5512:
129 	case 11025:
130 	case 22050:
131 	case 44100:
132 	case 88200:
133 	case 176400:
134 		core_freq = 176400 * 128;	/* 22.5792 MHz */
135 		break;
136 	case 48000:
137 	case 96000:
138 	case 192000:
139 	default:
140 		core_freq = 192000 * 128;	/* 24.576 MHz */
141 		break;
142 	}
143 
144 	spdif_out_clock(host, core_freq, rate);
145 	host->saved_params.core_freq = core_freq;
146 	host->saved_params.rate = rate;
147 
148 	return 0;
149 }
150 
151 static int spdif_out_trigger(struct snd_pcm_substream *substream, int cmd,
152 		struct snd_soc_dai *dai)
153 {
154 	struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
155 	u32 ctrl;
156 	int ret = 0;
157 
158 	if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
159 		return -EINVAL;
160 
161 	switch (cmd) {
162 	case SNDRV_PCM_TRIGGER_START:
163 	case SNDRV_PCM_TRIGGER_RESUME:
164 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
165 			ctrl = readl(host->io_base + SPDIF_OUT_CTRL);
166 			ctrl &= ~SPDIF_OPMODE_MASK;
167 			if (!host->saved_params.mute)
168 				ctrl |= SPDIF_OPMODE_AUD_DATA |
169 					SPDIF_STATE_NORMAL;
170 			else
171 				ctrl |= SPDIF_OPMODE_MUTE_PCM;
172 			writel(ctrl, host->io_base + SPDIF_OUT_CTRL);
173 		break;
174 
175 	case SNDRV_PCM_TRIGGER_STOP:
176 	case SNDRV_PCM_TRIGGER_SUSPEND:
177 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
178 		ctrl = readl(host->io_base + SPDIF_OUT_CTRL);
179 		ctrl &= ~SPDIF_OPMODE_MASK;
180 		ctrl |= SPDIF_OPMODE_OFF;
181 		writel(ctrl, host->io_base + SPDIF_OUT_CTRL);
182 		break;
183 
184 	default:
185 		ret = -EINVAL;
186 		break;
187 	}
188 	return ret;
189 }
190 
191 static int spdif_mute(struct snd_soc_dai *dai, int mute, int direction)
192 {
193 	struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
194 	u32 val;
195 
196 	host->saved_params.mute = mute;
197 	val = readl(host->io_base + SPDIF_OUT_CTRL);
198 	val &= ~SPDIF_OPMODE_MASK;
199 
200 	if (mute)
201 		val |= SPDIF_OPMODE_MUTE_PCM;
202 	else {
203 		if (host->running)
204 			val |= SPDIF_OPMODE_AUD_DATA | SPDIF_STATE_NORMAL;
205 		else
206 			val |= SPDIF_OPMODE_OFF;
207 	}
208 
209 	writel(val, host->io_base + SPDIF_OUT_CTRL);
210 	return 0;
211 }
212 
213 static int spdif_mute_get(struct snd_kcontrol *kcontrol,
214 		struct snd_ctl_elem_value *ucontrol)
215 {
216 	struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
217 	struct spdif_out_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
218 
219 	ucontrol->value.integer.value[0] = host->saved_params.mute;
220 	return 0;
221 }
222 
223 static int spdif_mute_put(struct snd_kcontrol *kcontrol,
224 		struct snd_ctl_elem_value *ucontrol)
225 {
226 	struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
227 	struct spdif_out_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
228 
229 	if (host->saved_params.mute == ucontrol->value.integer.value[0])
230 		return 0;
231 
232 	spdif_mute(cpu_dai, ucontrol->value.integer.value[0],
233 		   SNDRV_PCM_STREAM_PLAYBACK);
234 
235 	return 1;
236 }
237 static const struct snd_kcontrol_new spdif_out_controls[] = {
238 	SOC_SINGLE_BOOL_EXT("IEC958 Playback Switch", 0,
239 			spdif_mute_get, spdif_mute_put),
240 };
241 
242 static int spdif_soc_dai_probe(struct snd_soc_dai *dai)
243 {
244 	struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
245 
246 	host->dma_params_tx.filter_data = &host->dma_params;
247 
248 	snd_soc_dai_dma_data_set_playback(dai, &host->dma_params_tx);
249 
250 	return snd_soc_add_dai_controls(dai, spdif_out_controls,
251 				ARRAY_SIZE(spdif_out_controls));
252 }
253 
254 static const struct snd_soc_dai_ops spdif_out_dai_ops = {
255 	.mute_stream	= spdif_mute,
256 	.startup	= spdif_out_startup,
257 	.shutdown	= spdif_out_shutdown,
258 	.trigger	= spdif_out_trigger,
259 	.hw_params	= spdif_out_hw_params,
260 	.no_capture_mute = 1,
261 };
262 
263 static struct snd_soc_dai_driver spdif_out_dai = {
264 	.playback = {
265 		.channels_min = 2,
266 		.channels_max = 2,
267 		.rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
268 				 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
269 				 SNDRV_PCM_RATE_192000),
270 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
271 	},
272 	.probe = spdif_soc_dai_probe,
273 	.ops = &spdif_out_dai_ops,
274 };
275 
276 static const struct snd_soc_component_driver spdif_out_component = {
277 	.name			= "spdif-out",
278 	.legacy_dai_naming	= 1,
279 };
280 
281 static int spdif_out_probe(struct platform_device *pdev)
282 {
283 	struct spdif_out_dev *host;
284 	struct spear_spdif_platform_data *pdata;
285 	struct resource *res;
286 	int ret;
287 
288 	host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
289 	if (!host)
290 		return -ENOMEM;
291 
292 	host->io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
293 	if (IS_ERR(host->io_base))
294 		return PTR_ERR(host->io_base);
295 
296 	host->clk = devm_clk_get(&pdev->dev, NULL);
297 	if (IS_ERR(host->clk))
298 		return PTR_ERR(host->clk);
299 
300 	pdata = dev_get_platdata(&pdev->dev);
301 
302 	host->dma_params.data = pdata->dma_params;
303 	host->dma_params.addr = res->start + SPDIF_OUT_FIFO_DATA;
304 	host->dma_params.max_burst = 16;
305 	host->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
306 
307 	dev_set_drvdata(&pdev->dev, host);
308 
309 	ret = devm_snd_soc_register_component(&pdev->dev, &spdif_out_component,
310 					      &spdif_out_dai, 1);
311 	if (ret)
312 		return ret;
313 
314 	return devm_spear_pcm_platform_register(&pdev->dev, &host->config,
315 						pdata->filter);
316 }
317 
318 #ifdef CONFIG_PM
319 static int spdif_out_suspend(struct device *dev)
320 {
321 	struct platform_device *pdev = to_platform_device(dev);
322 	struct spdif_out_dev *host = dev_get_drvdata(&pdev->dev);
323 
324 	if (host->running)
325 		clk_disable(host->clk);
326 
327 	return 0;
328 }
329 
330 static int spdif_out_resume(struct device *dev)
331 {
332 	struct platform_device *pdev = to_platform_device(dev);
333 	struct spdif_out_dev *host = dev_get_drvdata(&pdev->dev);
334 
335 	if (host->running) {
336 		clk_enable(host->clk);
337 		spdif_out_configure(host);
338 		spdif_out_clock(host, host->saved_params.core_freq,
339 				host->saved_params.rate);
340 	}
341 	return 0;
342 }
343 
344 static SIMPLE_DEV_PM_OPS(spdif_out_dev_pm_ops, spdif_out_suspend, \
345 		spdif_out_resume);
346 
347 #define SPDIF_OUT_DEV_PM_OPS (&spdif_out_dev_pm_ops)
348 
349 #else
350 #define SPDIF_OUT_DEV_PM_OPS NULL
351 
352 #endif
353 
354 static struct platform_driver spdif_out_driver = {
355 	.probe		= spdif_out_probe,
356 	.driver		= {
357 		.name	= "spdif-out",
358 		.pm	= SPDIF_OUT_DEV_PM_OPS,
359 	},
360 };
361 
362 module_platform_driver(spdif_out_driver);
363 
364 MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>");
365 MODULE_DESCRIPTION("SPEAr SPDIF OUT SoC Interface");
366 MODULE_LICENSE("GPL");
367 MODULE_ALIAS("platform:spdif_out");
368