1 /* sound/soc/rockchip/rockchip_i2s.c
2  *
3  * ALSA SoC Audio Layer - Rockchip I2S Controller driver
4  *
5  * Copyright (c) 2014 Rockchip Electronics Co. Ltd.
6  * Author: Jianqun <jay.xu@rock-chips.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/module.h>
14 #include <linux/delay.h>
15 #include <linux/of_gpio.h>
16 #include <linux/clk.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regmap.h>
19 #include <sound/pcm_params.h>
20 #include <sound/dmaengine_pcm.h>
21 
22 #include "rockchip_i2s.h"
23 
24 #define DRV_NAME "rockchip-i2s"
25 
26 struct rk_i2s_dev {
27 	struct device *dev;
28 
29 	struct clk *hclk;
30 	struct clk *mclk;
31 
32 	struct snd_dmaengine_dai_dma_data capture_dma_data;
33 	struct snd_dmaengine_dai_dma_data playback_dma_data;
34 
35 	struct regmap *regmap;
36 
37 /*
38  * Used to indicate the tx/rx status.
39  * I2S controller hopes to start the tx and rx together,
40  * also to stop them when they are both try to stop.
41 */
42 	bool tx_start;
43 	bool rx_start;
44 };
45 
46 static int i2s_runtime_suspend(struct device *dev)
47 {
48 	struct rk_i2s_dev *i2s = dev_get_drvdata(dev);
49 
50 	clk_disable_unprepare(i2s->mclk);
51 
52 	return 0;
53 }
54 
55 static int i2s_runtime_resume(struct device *dev)
56 {
57 	struct rk_i2s_dev *i2s = dev_get_drvdata(dev);
58 	int ret;
59 
60 	ret = clk_prepare_enable(i2s->mclk);
61 	if (ret) {
62 		dev_err(i2s->dev, "clock enable failed %d\n", ret);
63 		return ret;
64 	}
65 
66 	return 0;
67 }
68 
69 static inline struct rk_i2s_dev *to_info(struct snd_soc_dai *dai)
70 {
71 	return snd_soc_dai_get_drvdata(dai);
72 }
73 
74 static void rockchip_snd_txctrl(struct rk_i2s_dev *i2s, int on)
75 {
76 	unsigned int val = 0;
77 	int retry = 10;
78 
79 	if (on) {
80 		regmap_update_bits(i2s->regmap, I2S_DMACR,
81 				   I2S_DMACR_TDE_ENABLE, I2S_DMACR_TDE_ENABLE);
82 
83 		regmap_update_bits(i2s->regmap, I2S_XFER,
84 				   I2S_XFER_TXS_START | I2S_XFER_RXS_START,
85 				   I2S_XFER_TXS_START | I2S_XFER_RXS_START);
86 
87 		i2s->tx_start = true;
88 	} else {
89 		i2s->tx_start = false;
90 
91 		regmap_update_bits(i2s->regmap, I2S_DMACR,
92 				   I2S_DMACR_TDE_ENABLE, I2S_DMACR_TDE_DISABLE);
93 
94 		if (!i2s->rx_start) {
95 			regmap_update_bits(i2s->regmap, I2S_XFER,
96 					   I2S_XFER_TXS_START |
97 					   I2S_XFER_RXS_START,
98 					   I2S_XFER_TXS_STOP |
99 					   I2S_XFER_RXS_STOP);
100 
101 			regmap_update_bits(i2s->regmap, I2S_CLR,
102 					   I2S_CLR_TXC | I2S_CLR_RXC,
103 					   I2S_CLR_TXC | I2S_CLR_RXC);
104 
105 			regmap_read(i2s->regmap, I2S_CLR, &val);
106 
107 			/* Should wait for clear operation to finish */
108 			while (val) {
109 				regmap_read(i2s->regmap, I2S_CLR, &val);
110 				retry--;
111 				if (!retry) {
112 					dev_warn(i2s->dev, "fail to clear\n");
113 					break;
114 				}
115 			}
116 		}
117 	}
118 }
119 
120 static void rockchip_snd_rxctrl(struct rk_i2s_dev *i2s, int on)
121 {
122 	unsigned int val = 0;
123 	int retry = 10;
124 
125 	if (on) {
126 		regmap_update_bits(i2s->regmap, I2S_DMACR,
127 				   I2S_DMACR_RDE_ENABLE, I2S_DMACR_RDE_ENABLE);
128 
129 		regmap_update_bits(i2s->regmap, I2S_XFER,
130 				   I2S_XFER_TXS_START | I2S_XFER_RXS_START,
131 				   I2S_XFER_TXS_START | I2S_XFER_RXS_START);
132 
133 		i2s->rx_start = true;
134 	} else {
135 		i2s->rx_start = false;
136 
137 		regmap_update_bits(i2s->regmap, I2S_DMACR,
138 				   I2S_DMACR_RDE_ENABLE, I2S_DMACR_RDE_DISABLE);
139 
140 		if (!i2s->tx_start) {
141 			regmap_update_bits(i2s->regmap, I2S_XFER,
142 					   I2S_XFER_TXS_START |
143 					   I2S_XFER_RXS_START,
144 					   I2S_XFER_TXS_STOP |
145 					   I2S_XFER_RXS_STOP);
146 
147 			regmap_update_bits(i2s->regmap, I2S_CLR,
148 					   I2S_CLR_TXC | I2S_CLR_RXC,
149 					   I2S_CLR_TXC | I2S_CLR_RXC);
150 
151 			regmap_read(i2s->regmap, I2S_CLR, &val);
152 
153 			/* Should wait for clear operation to finish */
154 			while (val) {
155 				regmap_read(i2s->regmap, I2S_CLR, &val);
156 				retry--;
157 				if (!retry) {
158 					dev_warn(i2s->dev, "fail to clear\n");
159 					break;
160 				}
161 			}
162 		}
163 	}
164 }
165 
166 static int rockchip_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
167 				unsigned int fmt)
168 {
169 	struct rk_i2s_dev *i2s = to_info(cpu_dai);
170 	unsigned int mask = 0, val = 0;
171 
172 	mask = I2S_CKR_MSS_MASK;
173 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
174 	case SND_SOC_DAIFMT_CBS_CFS:
175 		/* Set source clock in Master mode */
176 		val = I2S_CKR_MSS_MASTER;
177 		break;
178 	case SND_SOC_DAIFMT_CBM_CFM:
179 		val = I2S_CKR_MSS_SLAVE;
180 		break;
181 	default:
182 		return -EINVAL;
183 	}
184 
185 	regmap_update_bits(i2s->regmap, I2S_CKR, mask, val);
186 
187 	mask = I2S_TXCR_IBM_MASK;
188 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
189 	case SND_SOC_DAIFMT_RIGHT_J:
190 		val = I2S_TXCR_IBM_RSJM;
191 		break;
192 	case SND_SOC_DAIFMT_LEFT_J:
193 		val = I2S_TXCR_IBM_LSJM;
194 		break;
195 	case SND_SOC_DAIFMT_I2S:
196 		val = I2S_TXCR_IBM_NORMAL;
197 		break;
198 	default:
199 		return -EINVAL;
200 	}
201 
202 	regmap_update_bits(i2s->regmap, I2S_TXCR, mask, val);
203 
204 	mask = I2S_RXCR_IBM_MASK;
205 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
206 	case SND_SOC_DAIFMT_RIGHT_J:
207 		val = I2S_RXCR_IBM_RSJM;
208 		break;
209 	case SND_SOC_DAIFMT_LEFT_J:
210 		val = I2S_RXCR_IBM_LSJM;
211 		break;
212 	case SND_SOC_DAIFMT_I2S:
213 		val = I2S_RXCR_IBM_NORMAL;
214 		break;
215 	default:
216 		return -EINVAL;
217 	}
218 
219 	regmap_update_bits(i2s->regmap, I2S_RXCR, mask, val);
220 
221 	return 0;
222 }
223 
224 static int rockchip_i2s_hw_params(struct snd_pcm_substream *substream,
225 				  struct snd_pcm_hw_params *params,
226 				  struct snd_soc_dai *dai)
227 {
228 	struct rk_i2s_dev *i2s = to_info(dai);
229 	unsigned int val = 0;
230 
231 	switch (params_format(params)) {
232 	case SNDRV_PCM_FORMAT_S8:
233 		val |= I2S_TXCR_VDW(8);
234 		break;
235 	case SNDRV_PCM_FORMAT_S16_LE:
236 		val |= I2S_TXCR_VDW(16);
237 		break;
238 	case SNDRV_PCM_FORMAT_S20_3LE:
239 		val |= I2S_TXCR_VDW(20);
240 		break;
241 	case SNDRV_PCM_FORMAT_S24_LE:
242 		val |= I2S_TXCR_VDW(24);
243 		break;
244 	default:
245 		return -EINVAL;
246 	}
247 
248 	regmap_update_bits(i2s->regmap, I2S_TXCR, I2S_TXCR_VDW_MASK, val);
249 	regmap_update_bits(i2s->regmap, I2S_RXCR, I2S_RXCR_VDW_MASK, val);
250 
251 	return 0;
252 }
253 
254 static int rockchip_i2s_trigger(struct snd_pcm_substream *substream,
255 				int cmd, struct snd_soc_dai *dai)
256 {
257 	struct rk_i2s_dev *i2s = to_info(dai);
258 	int ret = 0;
259 
260 	switch (cmd) {
261 	case SNDRV_PCM_TRIGGER_START:
262 	case SNDRV_PCM_TRIGGER_RESUME:
263 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
264 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
265 			rockchip_snd_rxctrl(i2s, 1);
266 		else
267 			rockchip_snd_txctrl(i2s, 1);
268 		break;
269 	case SNDRV_PCM_TRIGGER_SUSPEND:
270 	case SNDRV_PCM_TRIGGER_STOP:
271 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
272 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
273 			rockchip_snd_rxctrl(i2s, 0);
274 		else
275 			rockchip_snd_txctrl(i2s, 0);
276 		break;
277 	default:
278 		ret = -EINVAL;
279 		break;
280 	}
281 
282 	return ret;
283 }
284 
285 static int rockchip_i2s_set_sysclk(struct snd_soc_dai *cpu_dai, int clk_id,
286 				   unsigned int freq, int dir)
287 {
288 	struct rk_i2s_dev *i2s = to_info(cpu_dai);
289 	int ret;
290 
291 	ret = clk_set_rate(i2s->mclk, freq);
292 	if (ret)
293 		dev_err(i2s->dev, "Fail to set mclk %d\n", ret);
294 
295 	return ret;
296 }
297 
298 static int rockchip_i2s_dai_probe(struct snd_soc_dai *dai)
299 {
300 	struct rk_i2s_dev *i2s = snd_soc_dai_get_drvdata(dai);
301 
302 	dai->capture_dma_data = &i2s->capture_dma_data;
303 	dai->playback_dma_data = &i2s->playback_dma_data;
304 
305 	return 0;
306 }
307 
308 static const struct snd_soc_dai_ops rockchip_i2s_dai_ops = {
309 	.hw_params = rockchip_i2s_hw_params,
310 	.set_sysclk = rockchip_i2s_set_sysclk,
311 	.set_fmt = rockchip_i2s_set_fmt,
312 	.trigger = rockchip_i2s_trigger,
313 };
314 
315 static struct snd_soc_dai_driver rockchip_i2s_dai = {
316 	.probe = rockchip_i2s_dai_probe,
317 	.playback = {
318 		.stream_name = "Playback",
319 		.channels_min = 2,
320 		.channels_max = 8,
321 		.rates = SNDRV_PCM_RATE_8000_192000,
322 		.formats = (SNDRV_PCM_FMTBIT_S8 |
323 			    SNDRV_PCM_FMTBIT_S16_LE |
324 			    SNDRV_PCM_FMTBIT_S20_3LE |
325 			    SNDRV_PCM_FMTBIT_S24_LE),
326 	},
327 	.capture = {
328 		.stream_name = "Capture",
329 		.channels_min = 2,
330 		.channels_max = 2,
331 		.rates = SNDRV_PCM_RATE_8000_192000,
332 		.formats = (SNDRV_PCM_FMTBIT_S8 |
333 			    SNDRV_PCM_FMTBIT_S16_LE |
334 			    SNDRV_PCM_FMTBIT_S20_3LE |
335 			    SNDRV_PCM_FMTBIT_S24_LE),
336 	},
337 	.ops = &rockchip_i2s_dai_ops,
338 };
339 
340 static const struct snd_soc_component_driver rockchip_i2s_component = {
341 	.name = DRV_NAME,
342 };
343 
344 static bool rockchip_i2s_wr_reg(struct device *dev, unsigned int reg)
345 {
346 	switch (reg) {
347 	case I2S_TXCR:
348 	case I2S_RXCR:
349 	case I2S_CKR:
350 	case I2S_DMACR:
351 	case I2S_INTCR:
352 	case I2S_XFER:
353 	case I2S_CLR:
354 	case I2S_TXDR:
355 		return true;
356 	default:
357 		return false;
358 	}
359 }
360 
361 static bool rockchip_i2s_rd_reg(struct device *dev, unsigned int reg)
362 {
363 	switch (reg) {
364 	case I2S_TXCR:
365 	case I2S_RXCR:
366 	case I2S_CKR:
367 	case I2S_DMACR:
368 	case I2S_INTCR:
369 	case I2S_XFER:
370 	case I2S_CLR:
371 	case I2S_RXDR:
372 	case I2S_FIFOLR:
373 	case I2S_INTSR:
374 		return true;
375 	default:
376 		return false;
377 	}
378 }
379 
380 static bool rockchip_i2s_volatile_reg(struct device *dev, unsigned int reg)
381 {
382 	switch (reg) {
383 	case I2S_INTSR:
384 	case I2S_CLR:
385 		return true;
386 	default:
387 		return false;
388 	}
389 }
390 
391 static bool rockchip_i2s_precious_reg(struct device *dev, unsigned int reg)
392 {
393 	switch (reg) {
394 	default:
395 		return false;
396 	}
397 }
398 
399 static const struct regmap_config rockchip_i2s_regmap_config = {
400 	.reg_bits = 32,
401 	.reg_stride = 4,
402 	.val_bits = 32,
403 	.max_register = I2S_RXDR,
404 	.writeable_reg = rockchip_i2s_wr_reg,
405 	.readable_reg = rockchip_i2s_rd_reg,
406 	.volatile_reg = rockchip_i2s_volatile_reg,
407 	.precious_reg = rockchip_i2s_precious_reg,
408 	.cache_type = REGCACHE_FLAT,
409 };
410 
411 static int rockchip_i2s_probe(struct platform_device *pdev)
412 {
413 	struct rk_i2s_dev *i2s;
414 	struct resource *res;
415 	void __iomem *regs;
416 	int ret;
417 
418 	i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL);
419 	if (!i2s) {
420 		dev_err(&pdev->dev, "Can't allocate rk_i2s_dev\n");
421 		return -ENOMEM;
422 	}
423 
424 	/* try to prepare related clocks */
425 	i2s->hclk = devm_clk_get(&pdev->dev, "i2s_hclk");
426 	if (IS_ERR(i2s->hclk)) {
427 		dev_err(&pdev->dev, "Can't retrieve i2s bus clock\n");
428 		return PTR_ERR(i2s->hclk);
429 	}
430 	ret = clk_prepare_enable(i2s->hclk);
431 	if (ret) {
432 		dev_err(i2s->dev, "hclock enable failed %d\n", ret);
433 		return ret;
434 	}
435 
436 	i2s->mclk = devm_clk_get(&pdev->dev, "i2s_clk");
437 	if (IS_ERR(i2s->mclk)) {
438 		dev_err(&pdev->dev, "Can't retrieve i2s master clock\n");
439 		return PTR_ERR(i2s->mclk);
440 	}
441 
442 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
443 	regs = devm_ioremap_resource(&pdev->dev, res);
444 	if (IS_ERR(regs))
445 		return PTR_ERR(regs);
446 
447 	i2s->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
448 					    &rockchip_i2s_regmap_config);
449 	if (IS_ERR(i2s->regmap)) {
450 		dev_err(&pdev->dev,
451 			"Failed to initialise managed register map\n");
452 		return PTR_ERR(i2s->regmap);
453 	}
454 
455 	i2s->playback_dma_data.addr = res->start + I2S_TXDR;
456 	i2s->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
457 	i2s->playback_dma_data.maxburst = 4;
458 
459 	i2s->capture_dma_data.addr = res->start + I2S_RXDR;
460 	i2s->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
461 	i2s->capture_dma_data.maxburst = 4;
462 
463 	i2s->dev = &pdev->dev;
464 	dev_set_drvdata(&pdev->dev, i2s);
465 
466 	pm_runtime_enable(&pdev->dev);
467 	if (!pm_runtime_enabled(&pdev->dev)) {
468 		ret = i2s_runtime_resume(&pdev->dev);
469 		if (ret)
470 			goto err_pm_disable;
471 	}
472 
473 	ret = devm_snd_soc_register_component(&pdev->dev,
474 					      &rockchip_i2s_component,
475 					      &rockchip_i2s_dai, 1);
476 	if (ret) {
477 		dev_err(&pdev->dev, "Could not register DAI\n");
478 		goto err_suspend;
479 	}
480 
481 	ret = snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
482 	if (ret) {
483 		dev_err(&pdev->dev, "Could not register PCM\n");
484 		goto err_pcm_register;
485 	}
486 
487 	return 0;
488 
489 err_pcm_register:
490 	snd_dmaengine_pcm_unregister(&pdev->dev);
491 err_suspend:
492 	if (!pm_runtime_status_suspended(&pdev->dev))
493 		i2s_runtime_suspend(&pdev->dev);
494 err_pm_disable:
495 	pm_runtime_disable(&pdev->dev);
496 
497 	return ret;
498 }
499 
500 static int rockchip_i2s_remove(struct platform_device *pdev)
501 {
502 	struct rk_i2s_dev *i2s = dev_get_drvdata(&pdev->dev);
503 
504 	pm_runtime_disable(&pdev->dev);
505 	if (!pm_runtime_status_suspended(&pdev->dev))
506 		i2s_runtime_suspend(&pdev->dev);
507 
508 	clk_disable_unprepare(i2s->mclk);
509 	clk_disable_unprepare(i2s->hclk);
510 	snd_dmaengine_pcm_unregister(&pdev->dev);
511 	snd_soc_unregister_component(&pdev->dev);
512 
513 	return 0;
514 }
515 
516 static const struct of_device_id rockchip_i2s_match[] = {
517 	{ .compatible = "rockchip,rk3066-i2s", },
518 	{},
519 };
520 
521 static const struct dev_pm_ops rockchip_i2s_pm_ops = {
522 	SET_RUNTIME_PM_OPS(i2s_runtime_suspend, i2s_runtime_resume,
523 			   NULL)
524 };
525 
526 static struct platform_driver rockchip_i2s_driver = {
527 	.probe = rockchip_i2s_probe,
528 	.remove = rockchip_i2s_remove,
529 	.driver = {
530 		.name = DRV_NAME,
531 		.of_match_table = of_match_ptr(rockchip_i2s_match),
532 		.pm = &rockchip_i2s_pm_ops,
533 	},
534 };
535 module_platform_driver(rockchip_i2s_driver);
536 
537 MODULE_DESCRIPTION("ROCKCHIP IIS ASoC Interface");
538 MODULE_AUTHOR("jianqun <jay.xu@rock-chips.com>");
539 MODULE_LICENSE("GPL v2");
540 MODULE_ALIAS("platform:" DRV_NAME);
541 MODULE_DEVICE_TABLE(of, rockchip_i2s_match);
542