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