xref: /openbmc/linux/sound/soc/spear/spdif_out.c (revision 8a10bc9d)
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_digital_mute(struct snd_soc_dai *dai, int mute)
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_codec *codec = snd_kcontrol_chip(kcontrol);
217 	struct snd_soc_card *card = codec->card;
218 	struct snd_soc_pcm_runtime *rtd = card->rtd;
219 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
220 	struct spdif_out_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
221 
222 	ucontrol->value.integer.value[0] = host->saved_params.mute;
223 	return 0;
224 }
225 
226 static int spdif_mute_put(struct snd_kcontrol *kcontrol,
227 		struct snd_ctl_elem_value *ucontrol)
228 {
229 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
230 	struct snd_soc_card *card = codec->card;
231 	struct snd_soc_pcm_runtime *rtd = card->rtd;
232 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
233 	struct spdif_out_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
234 
235 	if (host->saved_params.mute == ucontrol->value.integer.value[0])
236 		return 0;
237 
238 	spdif_digital_mute(cpu_dai, ucontrol->value.integer.value[0]);
239 
240 	return 1;
241 }
242 static const struct snd_kcontrol_new spdif_out_controls[] = {
243 	SOC_SINGLE_BOOL_EXT("IEC958 Playback Switch", 0,
244 			spdif_mute_get, spdif_mute_put),
245 };
246 
247 static int spdif_soc_dai_probe(struct snd_soc_dai *dai)
248 {
249 	struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
250 
251 	host->dma_params_tx.filter_data = &host->dma_params;
252 	dai->playback_dma_data = &host->dma_params_tx;
253 
254 	return snd_soc_add_dai_controls(dai, spdif_out_controls,
255 				ARRAY_SIZE(spdif_out_controls));
256 }
257 
258 static const struct snd_soc_dai_ops spdif_out_dai_ops = {
259 	.digital_mute	= spdif_digital_mute,
260 	.startup	= spdif_out_startup,
261 	.shutdown	= spdif_out_shutdown,
262 	.trigger	= spdif_out_trigger,
263 	.hw_params	= spdif_out_hw_params,
264 };
265 
266 static struct snd_soc_dai_driver spdif_out_dai = {
267 	.playback = {
268 		.channels_min = 2,
269 		.channels_max = 2,
270 		.rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
271 				 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
272 				 SNDRV_PCM_RATE_192000),
273 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
274 	},
275 	.probe = spdif_soc_dai_probe,
276 	.ops = &spdif_out_dai_ops,
277 };
278 
279 static const struct snd_soc_component_driver spdif_out_component = {
280 	.name		= "spdif-out",
281 };
282 
283 static int spdif_out_probe(struct platform_device *pdev)
284 {
285 	struct spdif_out_dev *host;
286 	struct spear_spdif_platform_data *pdata;
287 	struct resource *res;
288 	int ret;
289 
290 	host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
291 	if (!host) {
292 		dev_warn(&pdev->dev, "kzalloc fail\n");
293 		return -ENOMEM;
294 	}
295 
296 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
297 	host->io_base = devm_ioremap_resource(&pdev->dev, res);
298 	if (IS_ERR(host->io_base))
299 		return PTR_ERR(host->io_base);
300 
301 	host->clk = devm_clk_get(&pdev->dev, NULL);
302 	if (IS_ERR(host->clk))
303 		return PTR_ERR(host->clk);
304 
305 	pdata = dev_get_platdata(&pdev->dev);
306 
307 	host->dma_params.data = pdata->dma_params;
308 	host->dma_params.addr = res->start + SPDIF_OUT_FIFO_DATA;
309 	host->dma_params.max_burst = 16;
310 	host->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
311 
312 	dev_set_drvdata(&pdev->dev, host);
313 
314 	ret = devm_snd_soc_register_component(&pdev->dev, &spdif_out_component,
315 					      &spdif_out_dai, 1);
316 	if (ret)
317 		return ret;
318 
319 	return devm_spear_pcm_platform_register(&pdev->dev, &host->config,
320 						pdata->filter);
321 }
322 
323 #ifdef CONFIG_PM
324 static int spdif_out_suspend(struct device *dev)
325 {
326 	struct platform_device *pdev = to_platform_device(dev);
327 	struct spdif_out_dev *host = dev_get_drvdata(&pdev->dev);
328 
329 	if (host->running)
330 		clk_disable(host->clk);
331 
332 	return 0;
333 }
334 
335 static int spdif_out_resume(struct device *dev)
336 {
337 	struct platform_device *pdev = to_platform_device(dev);
338 	struct spdif_out_dev *host = dev_get_drvdata(&pdev->dev);
339 
340 	if (host->running) {
341 		clk_enable(host->clk);
342 		spdif_out_configure(host);
343 		spdif_out_clock(host, host->saved_params.core_freq,
344 				host->saved_params.rate);
345 	}
346 	return 0;
347 }
348 
349 static SIMPLE_DEV_PM_OPS(spdif_out_dev_pm_ops, spdif_out_suspend, \
350 		spdif_out_resume);
351 
352 #define SPDIF_OUT_DEV_PM_OPS (&spdif_out_dev_pm_ops)
353 
354 #else
355 #define SPDIF_OUT_DEV_PM_OPS NULL
356 
357 #endif
358 
359 static struct platform_driver spdif_out_driver = {
360 	.probe		= spdif_out_probe,
361 	.driver		= {
362 		.name	= "spdif-out",
363 		.owner	= THIS_MODULE,
364 		.pm	= SPDIF_OUT_DEV_PM_OPS,
365 	},
366 };
367 
368 module_platform_driver(spdif_out_driver);
369 
370 MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>");
371 MODULE_DESCRIPTION("SPEAr SPDIF OUT SoC Interface");
372 MODULE_LICENSE("GPL");
373 MODULE_ALIAS("platform:spdif_out");
374