xref: /linux/sound/soc/samsung/spdif.c (revision 8c749ce93ee69e789e46b3be98de9e0cbfcf8ed8)
1 /* sound/soc/samsung/spdif.c
2  *
3  * ALSA SoC Audio Layer - Samsung S/PDIF Controller driver
4  *
5  * Copyright (c) 2010 Samsung Electronics Co. Ltd
6  *		http://www.samsung.com/
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #include <linux/clk.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 
17 #include <sound/soc.h>
18 #include <sound/pcm_params.h>
19 
20 #include <linux/platform_data/asoc-s3c.h>
21 
22 #include "dma.h"
23 #include "spdif.h"
24 
25 /* Registers */
26 #define CLKCON				0x00
27 #define CON				0x04
28 #define BSTAS				0x08
29 #define CSTAS				0x0C
30 #define DATA_OUTBUF			0x10
31 #define DCNT				0x14
32 #define BSTAS_S				0x18
33 #define DCNT_S				0x1C
34 
35 #define CLKCTL_MASK			0x7
36 #define CLKCTL_MCLK_EXT			(0x1 << 2)
37 #define CLKCTL_PWR_ON			(0x1 << 0)
38 
39 #define CON_MASK			0x3ffffff
40 #define CON_FIFO_TH_SHIFT		19
41 #define CON_FIFO_TH_MASK		(0x7 << 19)
42 #define CON_USERDATA_23RDBIT		(0x1 << 12)
43 
44 #define CON_SW_RESET			(0x1 << 5)
45 
46 #define CON_MCLKDIV_MASK		(0x3 << 3)
47 #define CON_MCLKDIV_256FS		(0x0 << 3)
48 #define CON_MCLKDIV_384FS		(0x1 << 3)
49 #define CON_MCLKDIV_512FS		(0x2 << 3)
50 
51 #define CON_PCM_MASK			(0x3 << 1)
52 #define CON_PCM_16BIT			(0x0 << 1)
53 #define CON_PCM_20BIT			(0x1 << 1)
54 #define CON_PCM_24BIT			(0x2 << 1)
55 
56 #define CON_PCM_DATA			(0x1 << 0)
57 
58 #define CSTAS_MASK			0x3fffffff
59 #define CSTAS_SAMP_FREQ_MASK		(0xF << 24)
60 #define CSTAS_SAMP_FREQ_44		(0x0 << 24)
61 #define CSTAS_SAMP_FREQ_48		(0x2 << 24)
62 #define CSTAS_SAMP_FREQ_32		(0x3 << 24)
63 #define CSTAS_SAMP_FREQ_96		(0xA << 24)
64 
65 #define CSTAS_CATEGORY_MASK		(0xFF << 8)
66 #define CSTAS_CATEGORY_CODE_CDP		(0x01 << 8)
67 
68 #define CSTAS_NO_COPYRIGHT		(0x1 << 2)
69 
70 /**
71  * struct samsung_spdif_info - Samsung S/PDIF Controller information
72  * @lock: Spin lock for S/PDIF.
73  * @dev: The parent device passed to use from the probe.
74  * @regs: The pointer to the device register block.
75  * @clk_rate: Current clock rate for calcurate ratio.
76  * @pclk: The peri-clock pointer for spdif master operation.
77  * @sclk: The source clock pointer for making sync signals.
78  * @save_clkcon: Backup clkcon reg. in suspend.
79  * @save_con: Backup con reg. in suspend.
80  * @save_cstas: Backup cstas reg. in suspend.
81  * @dma_playback: DMA information for playback channel.
82  */
83 struct samsung_spdif_info {
84 	spinlock_t	lock;
85 	struct device	*dev;
86 	void __iomem	*regs;
87 	unsigned long	clk_rate;
88 	struct clk	*pclk;
89 	struct clk	*sclk;
90 	u32		saved_clkcon;
91 	u32		saved_con;
92 	u32		saved_cstas;
93 	struct s3c_dma_params	*dma_playback;
94 };
95 
96 static struct s3c_dma_params spdif_stereo_out;
97 static struct samsung_spdif_info spdif_info;
98 
99 static inline struct samsung_spdif_info *to_info(struct snd_soc_dai *cpu_dai)
100 {
101 	return snd_soc_dai_get_drvdata(cpu_dai);
102 }
103 
104 static void spdif_snd_txctrl(struct samsung_spdif_info *spdif, int on)
105 {
106 	void __iomem *regs = spdif->regs;
107 	u32 clkcon;
108 
109 	dev_dbg(spdif->dev, "Entered %s\n", __func__);
110 
111 	clkcon = readl(regs + CLKCON) & CLKCTL_MASK;
112 	if (on)
113 		writel(clkcon | CLKCTL_PWR_ON, regs + CLKCON);
114 	else
115 		writel(clkcon & ~CLKCTL_PWR_ON, regs + CLKCON);
116 }
117 
118 static int spdif_set_sysclk(struct snd_soc_dai *cpu_dai,
119 				int clk_id, unsigned int freq, int dir)
120 {
121 	struct samsung_spdif_info *spdif = to_info(cpu_dai);
122 	u32 clkcon;
123 
124 	dev_dbg(spdif->dev, "Entered %s\n", __func__);
125 
126 	clkcon = readl(spdif->regs + CLKCON);
127 
128 	if (clk_id == SND_SOC_SPDIF_INT_MCLK)
129 		clkcon &= ~CLKCTL_MCLK_EXT;
130 	else
131 		clkcon |= CLKCTL_MCLK_EXT;
132 
133 	writel(clkcon, spdif->regs + CLKCON);
134 
135 	spdif->clk_rate = freq;
136 
137 	return 0;
138 }
139 
140 static int spdif_trigger(struct snd_pcm_substream *substream, int cmd,
141 				struct snd_soc_dai *dai)
142 {
143 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
144 	struct samsung_spdif_info *spdif = to_info(rtd->cpu_dai);
145 	unsigned long flags;
146 
147 	dev_dbg(spdif->dev, "Entered %s\n", __func__);
148 
149 	switch (cmd) {
150 	case SNDRV_PCM_TRIGGER_START:
151 	case SNDRV_PCM_TRIGGER_RESUME:
152 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
153 		spin_lock_irqsave(&spdif->lock, flags);
154 		spdif_snd_txctrl(spdif, 1);
155 		spin_unlock_irqrestore(&spdif->lock, flags);
156 		break;
157 	case SNDRV_PCM_TRIGGER_STOP:
158 	case SNDRV_PCM_TRIGGER_SUSPEND:
159 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
160 		spin_lock_irqsave(&spdif->lock, flags);
161 		spdif_snd_txctrl(spdif, 0);
162 		spin_unlock_irqrestore(&spdif->lock, flags);
163 		break;
164 	default:
165 		return -EINVAL;
166 	}
167 
168 	return 0;
169 }
170 
171 static int spdif_sysclk_ratios[] = {
172 	512, 384, 256,
173 };
174 
175 static int spdif_hw_params(struct snd_pcm_substream *substream,
176 				struct snd_pcm_hw_params *params,
177 				struct snd_soc_dai *socdai)
178 {
179 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
180 	struct samsung_spdif_info *spdif = to_info(rtd->cpu_dai);
181 	void __iomem *regs = spdif->regs;
182 	struct s3c_dma_params *dma_data;
183 	u32 con, clkcon, cstas;
184 	unsigned long flags;
185 	int i, ratio;
186 
187 	dev_dbg(spdif->dev, "Entered %s\n", __func__);
188 
189 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
190 		dma_data = spdif->dma_playback;
191 	else {
192 		dev_err(spdif->dev, "Capture is not supported\n");
193 		return -EINVAL;
194 	}
195 
196 	snd_soc_dai_set_dma_data(rtd->cpu_dai, substream, dma_data);
197 
198 	spin_lock_irqsave(&spdif->lock, flags);
199 
200 	con = readl(regs + CON) & CON_MASK;
201 	cstas = readl(regs + CSTAS) & CSTAS_MASK;
202 	clkcon = readl(regs + CLKCON) & CLKCTL_MASK;
203 
204 	con &= ~CON_FIFO_TH_MASK;
205 	con |= (0x7 << CON_FIFO_TH_SHIFT);
206 	con |= CON_USERDATA_23RDBIT;
207 	con |= CON_PCM_DATA;
208 
209 	con &= ~CON_PCM_MASK;
210 	switch (params_width(params)) {
211 	case 16:
212 		con |= CON_PCM_16BIT;
213 		break;
214 	default:
215 		dev_err(spdif->dev, "Unsupported data size.\n");
216 		goto err;
217 	}
218 
219 	ratio = spdif->clk_rate / params_rate(params);
220 	for (i = 0; i < ARRAY_SIZE(spdif_sysclk_ratios); i++)
221 		if (ratio == spdif_sysclk_ratios[i])
222 			break;
223 	if (i == ARRAY_SIZE(spdif_sysclk_ratios)) {
224 		dev_err(spdif->dev, "Invalid clock ratio %ld/%d\n",
225 				spdif->clk_rate, params_rate(params));
226 		goto err;
227 	}
228 
229 	con &= ~CON_MCLKDIV_MASK;
230 	switch (ratio) {
231 	case 256:
232 		con |= CON_MCLKDIV_256FS;
233 		break;
234 	case 384:
235 		con |= CON_MCLKDIV_384FS;
236 		break;
237 	case 512:
238 		con |= CON_MCLKDIV_512FS;
239 		break;
240 	}
241 
242 	cstas &= ~CSTAS_SAMP_FREQ_MASK;
243 	switch (params_rate(params)) {
244 	case 44100:
245 		cstas |= CSTAS_SAMP_FREQ_44;
246 		break;
247 	case 48000:
248 		cstas |= CSTAS_SAMP_FREQ_48;
249 		break;
250 	case 32000:
251 		cstas |= CSTAS_SAMP_FREQ_32;
252 		break;
253 	case 96000:
254 		cstas |= CSTAS_SAMP_FREQ_96;
255 		break;
256 	default:
257 		dev_err(spdif->dev, "Invalid sampling rate %d\n",
258 				params_rate(params));
259 		goto err;
260 	}
261 
262 	cstas &= ~CSTAS_CATEGORY_MASK;
263 	cstas |= CSTAS_CATEGORY_CODE_CDP;
264 	cstas |= CSTAS_NO_COPYRIGHT;
265 
266 	writel(con, regs + CON);
267 	writel(cstas, regs + CSTAS);
268 	writel(clkcon, regs + CLKCON);
269 
270 	spin_unlock_irqrestore(&spdif->lock, flags);
271 
272 	return 0;
273 err:
274 	spin_unlock_irqrestore(&spdif->lock, flags);
275 	return -EINVAL;
276 }
277 
278 static void spdif_shutdown(struct snd_pcm_substream *substream,
279 				struct snd_soc_dai *dai)
280 {
281 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
282 	struct samsung_spdif_info *spdif = to_info(rtd->cpu_dai);
283 	void __iomem *regs = spdif->regs;
284 	u32 con, clkcon;
285 
286 	dev_dbg(spdif->dev, "Entered %s\n", __func__);
287 
288 	con = readl(regs + CON) & CON_MASK;
289 	clkcon = readl(regs + CLKCON) & CLKCTL_MASK;
290 
291 	writel(con | CON_SW_RESET, regs + CON);
292 	cpu_relax();
293 
294 	writel(clkcon & ~CLKCTL_PWR_ON, regs + CLKCON);
295 }
296 
297 #ifdef CONFIG_PM
298 static int spdif_suspend(struct snd_soc_dai *cpu_dai)
299 {
300 	struct samsung_spdif_info *spdif = to_info(cpu_dai);
301 	u32 con = spdif->saved_con;
302 
303 	dev_dbg(spdif->dev, "Entered %s\n", __func__);
304 
305 	spdif->saved_clkcon = readl(spdif->regs	+ CLKCON) & CLKCTL_MASK;
306 	spdif->saved_con = readl(spdif->regs + CON) & CON_MASK;
307 	spdif->saved_cstas = readl(spdif->regs + CSTAS) & CSTAS_MASK;
308 
309 	writel(con | CON_SW_RESET, spdif->regs + CON);
310 	cpu_relax();
311 
312 	return 0;
313 }
314 
315 static int spdif_resume(struct snd_soc_dai *cpu_dai)
316 {
317 	struct samsung_spdif_info *spdif = to_info(cpu_dai);
318 
319 	dev_dbg(spdif->dev, "Entered %s\n", __func__);
320 
321 	writel(spdif->saved_clkcon, spdif->regs	+ CLKCON);
322 	writel(spdif->saved_con, spdif->regs + CON);
323 	writel(spdif->saved_cstas, spdif->regs + CSTAS);
324 
325 	return 0;
326 }
327 #else
328 #define spdif_suspend NULL
329 #define spdif_resume NULL
330 #endif
331 
332 static const struct snd_soc_dai_ops spdif_dai_ops = {
333 	.set_sysclk	= spdif_set_sysclk,
334 	.trigger	= spdif_trigger,
335 	.hw_params	= spdif_hw_params,
336 	.shutdown	= spdif_shutdown,
337 };
338 
339 static struct snd_soc_dai_driver samsung_spdif_dai = {
340 	.name = "samsung-spdif",
341 	.playback = {
342 		.stream_name = "S/PDIF Playback",
343 		.channels_min = 2,
344 		.channels_max = 2,
345 		.rates = (SNDRV_PCM_RATE_32000 |
346 				SNDRV_PCM_RATE_44100 |
347 				SNDRV_PCM_RATE_48000 |
348 				SNDRV_PCM_RATE_96000),
349 		.formats = SNDRV_PCM_FMTBIT_S16_LE, },
350 	.ops = &spdif_dai_ops,
351 	.suspend = spdif_suspend,
352 	.resume = spdif_resume,
353 };
354 
355 static const struct snd_soc_component_driver samsung_spdif_component = {
356 	.name		= "samsung-spdif",
357 };
358 
359 static int spdif_probe(struct platform_device *pdev)
360 {
361 	struct s3c_audio_pdata *spdif_pdata;
362 	struct resource *mem_res;
363 	struct samsung_spdif_info *spdif;
364 	dma_filter_fn filter;
365 	int ret;
366 
367 	spdif_pdata = pdev->dev.platform_data;
368 
369 	dev_dbg(&pdev->dev, "Entered %s\n", __func__);
370 
371 	mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
372 	if (!mem_res) {
373 		dev_err(&pdev->dev, "Unable to get register resource.\n");
374 		return -ENXIO;
375 	}
376 
377 	if (spdif_pdata && spdif_pdata->cfg_gpio
378 			&& spdif_pdata->cfg_gpio(pdev)) {
379 		dev_err(&pdev->dev, "Unable to configure GPIO pins\n");
380 		return -EINVAL;
381 	}
382 
383 	spdif = &spdif_info;
384 	spdif->dev = &pdev->dev;
385 
386 	spin_lock_init(&spdif->lock);
387 
388 	spdif->pclk = devm_clk_get(&pdev->dev, "spdif");
389 	if (IS_ERR(spdif->pclk)) {
390 		dev_err(&pdev->dev, "failed to get peri-clock\n");
391 		ret = -ENOENT;
392 		goto err0;
393 	}
394 	clk_prepare_enable(spdif->pclk);
395 
396 	spdif->sclk = devm_clk_get(&pdev->dev, "sclk_spdif");
397 	if (IS_ERR(spdif->sclk)) {
398 		dev_err(&pdev->dev, "failed to get internal source clock\n");
399 		ret = -ENOENT;
400 		goto err1;
401 	}
402 	clk_prepare_enable(spdif->sclk);
403 
404 	/* Request S/PDIF Register's memory region */
405 	if (!request_mem_region(mem_res->start,
406 				resource_size(mem_res), "samsung-spdif")) {
407 		dev_err(&pdev->dev, "Unable to request register region\n");
408 		ret = -EBUSY;
409 		goto err2;
410 	}
411 
412 	spdif->regs = ioremap(mem_res->start, 0x100);
413 	if (spdif->regs == NULL) {
414 		dev_err(&pdev->dev, "Cannot ioremap registers\n");
415 		ret = -ENXIO;
416 		goto err3;
417 	}
418 
419 	dev_set_drvdata(&pdev->dev, spdif);
420 
421 	ret = devm_snd_soc_register_component(&pdev->dev,
422 			&samsung_spdif_component, &samsung_spdif_dai, 1);
423 	if (ret != 0) {
424 		dev_err(&pdev->dev, "fail to register dai\n");
425 		goto err4;
426 	}
427 
428 	spdif_stereo_out.dma_size = 2;
429 	spdif_stereo_out.dma_addr = mem_res->start + DATA_OUTBUF;
430 	filter = NULL;
431 	if (spdif_pdata) {
432 		spdif_stereo_out.slave = spdif_pdata->dma_playback;
433 		filter = spdif_pdata->dma_filter;
434 	}
435 
436 	spdif->dma_playback = &spdif_stereo_out;
437 
438 	ret = samsung_asoc_dma_platform_register(&pdev->dev, filter);
439 	if (ret) {
440 		dev_err(&pdev->dev, "failed to register DMA: %d\n", ret);
441 		goto err4;
442 	}
443 
444 	return 0;
445 err4:
446 	iounmap(spdif->regs);
447 err3:
448 	release_mem_region(mem_res->start, resource_size(mem_res));
449 err2:
450 	clk_disable_unprepare(spdif->sclk);
451 err1:
452 	clk_disable_unprepare(spdif->pclk);
453 err0:
454 	return ret;
455 }
456 
457 static int spdif_remove(struct platform_device *pdev)
458 {
459 	struct samsung_spdif_info *spdif = &spdif_info;
460 	struct resource *mem_res;
461 
462 	iounmap(spdif->regs);
463 
464 	mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
465 	if (mem_res)
466 		release_mem_region(mem_res->start, resource_size(mem_res));
467 
468 	clk_disable_unprepare(spdif->sclk);
469 	clk_disable_unprepare(spdif->pclk);
470 
471 	return 0;
472 }
473 
474 static struct platform_driver samsung_spdif_driver = {
475 	.probe	= spdif_probe,
476 	.remove	= spdif_remove,
477 	.driver	= {
478 		.name	= "samsung-spdif",
479 	},
480 };
481 
482 module_platform_driver(samsung_spdif_driver);
483 
484 MODULE_AUTHOR("Seungwhan Youn, <sw.youn@samsung.com>");
485 MODULE_DESCRIPTION("Samsung S/PDIF Controller Driver");
486 MODULE_LICENSE("GPL");
487 MODULE_ALIAS("platform:samsung-spdif");
488