xref: /openbmc/linux/sound/soc/sh/rcar/src.c (revision 8440bb9b)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Renesas R-Car SRC support
4 //
5 // Copyright (C) 2013 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 
8 /*
9  * you can enable below define if you don't need
10  * SSI interrupt status debug message when debugging
11  * see rsnd_dbg_irq_status()
12  *
13  * #define RSND_DEBUG_NO_IRQ_STATUS 1
14  */
15 
16 #include "rsnd.h"
17 #include <linux/sys_soc.h>
18 
19 #define SRC_NAME "src"
20 
21 /* SCU_SYSTEM_STATUS0/1 */
22 #define OUF_SRC(id)	((1 << (id + 16)) | (1 << id))
23 
24 struct rsnd_src {
25 	struct rsnd_mod mod;
26 	struct rsnd_mod *dma;
27 	struct rsnd_kctrl_cfg_s sen;  /* sync convert enable */
28 	struct rsnd_kctrl_cfg_s sync; /* sync convert */
29 	int irq;
30 };
31 
32 #define RSND_SRC_NAME_SIZE 16
33 
34 #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
35 #define rsnd_src_nr(priv) ((priv)->src_nr)
36 #define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val)
37 
38 #define rsnd_mod_to_src(_mod)				\
39 	container_of((_mod), struct rsnd_src, mod)
40 
41 #define for_each_rsnd_src(pos, priv, i)				\
42 	for ((i) = 0;						\
43 	     ((i) < rsnd_src_nr(priv)) &&			\
44 	     ((pos) = (struct rsnd_src *)(priv)->src + i);	\
45 	     i++)
46 
47 
48 /*
49  *		image of SRC (Sampling Rate Converter)
50  *
51  * 96kHz   <-> +-----+	48kHz	+-----+	 48kHz	+-------+
52  * 48kHz   <-> | SRC | <------>	| SSI |	<----->	| codec |
53  * 44.1kHz <-> +-----+		+-----+		+-------+
54  * ...
55  *
56  */
57 
58 static void rsnd_src_activation(struct rsnd_mod *mod)
59 {
60 	rsnd_mod_write(mod, SRC_SWRSR, 0);
61 	rsnd_mod_write(mod, SRC_SWRSR, 1);
62 }
63 
64 static void rsnd_src_halt(struct rsnd_mod *mod)
65 {
66 	rsnd_mod_write(mod, SRC_SRCIR, 1);
67 	rsnd_mod_write(mod, SRC_SWRSR, 0);
68 }
69 
70 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
71 					 struct rsnd_mod *mod)
72 {
73 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
74 	int is_play = rsnd_io_is_play(io);
75 
76 	return rsnd_dma_request_channel(rsnd_src_of_node(priv),
77 					mod,
78 					is_play ? "rx" : "tx");
79 }
80 
81 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
82 				 struct rsnd_mod *mod)
83 {
84 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
85 	struct rsnd_src *src = rsnd_mod_to_src(mod);
86 	u32 convert_rate;
87 
88 	if (!runtime)
89 		return 0;
90 
91 	if (!rsnd_src_sync_is_enabled(mod))
92 		return rsnd_io_converted_rate(io);
93 
94 	convert_rate = src->sync.val;
95 
96 	if (!convert_rate)
97 		convert_rate = rsnd_io_converted_rate(io);
98 
99 	if (!convert_rate)
100 		convert_rate = runtime->rate;
101 
102 	return convert_rate;
103 }
104 
105 unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
106 			       struct rsnd_dai_stream *io,
107 			       int is_in)
108 {
109 	struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
110 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
111 	unsigned int rate = 0;
112 	int is_play = rsnd_io_is_play(io);
113 
114 	/*
115 	 * Playback
116 	 * runtime_rate -> [SRC] -> convert_rate
117 	 *
118 	 * Capture
119 	 * convert_rate -> [SRC] -> runtime_rate
120 	 */
121 
122 	if (is_play == is_in)
123 		return runtime->rate;
124 
125 	/*
126 	 * return convert rate if SRC is used,
127 	 * otherwise, return runtime->rate as usual
128 	 */
129 	if (src_mod)
130 		rate = rsnd_src_convert_rate(io, src_mod);
131 
132 	if (!rate)
133 		rate = runtime->rate;
134 
135 	return rate;
136 }
137 
138 const static u32 bsdsr_table_pattern1[] = {
139 	0x01800000, /* 6 - 1/6 */
140 	0x01000000, /* 6 - 1/4 */
141 	0x00c00000, /* 6 - 1/3 */
142 	0x00800000, /* 6 - 1/2 */
143 	0x00600000, /* 6 - 2/3 */
144 	0x00400000, /* 6 - 1   */
145 };
146 
147 const static u32 bsdsr_table_pattern2[] = {
148 	0x02400000, /* 6 - 1/6 */
149 	0x01800000, /* 6 - 1/4 */
150 	0x01200000, /* 6 - 1/3 */
151 	0x00c00000, /* 6 - 1/2 */
152 	0x00900000, /* 6 - 2/3 */
153 	0x00600000, /* 6 - 1   */
154 };
155 
156 const static u32 bsisr_table[] = {
157 	0x00100060, /* 6 - 1/6 */
158 	0x00100040, /* 6 - 1/4 */
159 	0x00100030, /* 6 - 1/3 */
160 	0x00100020, /* 6 - 1/2 */
161 	0x00100020, /* 6 - 2/3 */
162 	0x00100020, /* 6 - 1   */
163 };
164 
165 const static u32 chan288888[] = {
166 	0x00000006, /* 1 to 2 */
167 	0x000001fe, /* 1 to 8 */
168 	0x000001fe, /* 1 to 8 */
169 	0x000001fe, /* 1 to 8 */
170 	0x000001fe, /* 1 to 8 */
171 	0x000001fe, /* 1 to 8 */
172 };
173 
174 const static u32 chan244888[] = {
175 	0x00000006, /* 1 to 2 */
176 	0x0000001e, /* 1 to 4 */
177 	0x0000001e, /* 1 to 4 */
178 	0x000001fe, /* 1 to 8 */
179 	0x000001fe, /* 1 to 8 */
180 	0x000001fe, /* 1 to 8 */
181 };
182 
183 const static u32 chan222222[] = {
184 	0x00000006, /* 1 to 2 */
185 	0x00000006, /* 1 to 2 */
186 	0x00000006, /* 1 to 2 */
187 	0x00000006, /* 1 to 2 */
188 	0x00000006, /* 1 to 2 */
189 	0x00000006, /* 1 to 2 */
190 };
191 
192 static const struct soc_device_attribute ov_soc[] = {
193 	{ .soc_id = "r8a77990" }, /* E3 */
194 	{ /* sentinel */ }
195 };
196 
197 static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
198 				      struct rsnd_mod *mod)
199 {
200 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
201 	struct device *dev = rsnd_priv_to_dev(priv);
202 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
203 	const struct soc_device_attribute *soc = soc_device_match(ov_soc);
204 	int is_play = rsnd_io_is_play(io);
205 	int use_src = 0;
206 	u32 fin, fout;
207 	u32 ifscr, fsrate, adinr;
208 	u32 cr, route;
209 	u32 i_busif, o_busif, tmp;
210 	const u32 *bsdsr_table;
211 	const u32 *chptn;
212 	uint ratio;
213 	int chan;
214 	int idx;
215 
216 	if (!runtime)
217 		return;
218 
219 	fin  = rsnd_src_get_in_rate(priv, io);
220 	fout = rsnd_src_get_out_rate(priv, io);
221 
222 	chan = rsnd_runtime_channel_original(io);
223 
224 	/* 6 - 1/6 are very enough ratio for SRC_BSDSR */
225 	if (fin == fout)
226 		ratio = 0;
227 	else if (fin > fout)
228 		ratio = 100 * fin / fout;
229 	else
230 		ratio = 100 * fout / fin;
231 
232 	if (ratio > 600) {
233 		dev_err(dev, "FSO/FSI ratio error\n");
234 		return;
235 	}
236 
237 	use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
238 
239 	/*
240 	 * SRC_ADINR
241 	 */
242 	adinr = rsnd_get_adinr_bit(mod, io) | chan;
243 
244 	/*
245 	 * SRC_IFSCR / SRC_IFSVR
246 	 */
247 	ifscr = 0;
248 	fsrate = 0;
249 	if (use_src) {
250 		u64 n;
251 
252 		ifscr = 1;
253 		n = (u64)0x0400000 * fin;
254 		do_div(n, fout);
255 		fsrate = n;
256 	}
257 
258 	/*
259 	 * SRC_SRCCR / SRC_ROUTE_MODE0
260 	 */
261 	cr	= 0x00011110;
262 	route	= 0x0;
263 	if (use_src) {
264 		route	= 0x1;
265 
266 		if (rsnd_src_sync_is_enabled(mod)) {
267 			cr |= 0x1;
268 			route |= rsnd_io_is_play(io) ?
269 				(0x1 << 24) : (0x1 << 25);
270 		}
271 	}
272 
273 	/*
274 	 * SRC_BSDSR / SRC_BSISR
275 	 *
276 	 * see
277 	 *	Combination of Register Setting Related to
278 	 *	FSO/FSI Ratio and Channel, Latency
279 	 */
280 	switch (rsnd_mod_id(mod)) {
281 	case 0:
282 		chptn		= chan288888;
283 		bsdsr_table	= bsdsr_table_pattern1;
284 		break;
285 	case 1:
286 	case 3:
287 	case 4:
288 		chptn		= chan244888;
289 		bsdsr_table	= bsdsr_table_pattern1;
290 		break;
291 	case 2:
292 	case 9:
293 		chptn		= chan222222;
294 		bsdsr_table	= bsdsr_table_pattern1;
295 		break;
296 	case 5:
297 	case 6:
298 	case 7:
299 	case 8:
300 		chptn		= chan222222;
301 		bsdsr_table	= bsdsr_table_pattern2;
302 		break;
303 	default:
304 		goto convert_rate_err;
305 	}
306 
307 	/*
308 	 * E3 need to overwrite
309 	 */
310 	if (soc)
311 		switch (rsnd_mod_id(mod)) {
312 		case 0:
313 		case 4:
314 			chptn	= chan222222;
315 		}
316 
317 	for (idx = 0; idx < ARRAY_SIZE(chan222222); idx++)
318 		if (chptn[idx] & (1 << chan))
319 			break;
320 
321 	if (chan > 8 ||
322 	    idx >= ARRAY_SIZE(chan222222))
323 		goto convert_rate_err;
324 
325 	/* BUSIF_MODE */
326 	tmp = rsnd_get_busif_shift(io, mod);
327 	i_busif = ( is_play ? tmp : 0) | 1;
328 	o_busif = (!is_play ? tmp : 0) | 1;
329 
330 	rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
331 
332 	rsnd_mod_write(mod, SRC_SRCIR, 1);	/* initialize */
333 	rsnd_mod_write(mod, SRC_ADINR, adinr);
334 	rsnd_mod_write(mod, SRC_IFSCR, ifscr);
335 	rsnd_mod_write(mod, SRC_IFSVR, fsrate);
336 	rsnd_mod_write(mod, SRC_SRCCR, cr);
337 	rsnd_mod_write(mod, SRC_BSDSR, bsdsr_table[idx]);
338 	rsnd_mod_write(mod, SRC_BSISR, bsisr_table[idx]);
339 	rsnd_mod_write(mod, SRC_SRCIR, 0);	/* cancel initialize */
340 
341 	rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
342 	rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);
343 
344 	rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
345 
346 	rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
347 
348 	return;
349 
350 convert_rate_err:
351 	dev_err(dev, "unknown BSDSR/BSDIR settings\n");
352 }
353 
354 static int rsnd_src_irq(struct rsnd_mod *mod,
355 			struct rsnd_dai_stream *io,
356 			struct rsnd_priv *priv,
357 			int enable)
358 {
359 	struct rsnd_src *src = rsnd_mod_to_src(mod);
360 	u32 sys_int_val, int_val, sys_int_mask;
361 	int irq = src->irq;
362 	int id = rsnd_mod_id(mod);
363 
364 	sys_int_val =
365 	sys_int_mask = OUF_SRC(id);
366 	int_val = 0x3300;
367 
368 	/*
369 	 * IRQ is not supported on non-DT
370 	 * see
371 	 *	rsnd_src_probe_()
372 	 */
373 	if ((irq <= 0) || !enable) {
374 		sys_int_val = 0;
375 		int_val = 0;
376 	}
377 
378 	/*
379 	 * WORKAROUND
380 	 *
381 	 * ignore over flow error when rsnd_src_sync_is_enabled()
382 	 */
383 	if (rsnd_src_sync_is_enabled(mod))
384 		sys_int_val = sys_int_val & 0xffff;
385 
386 	rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
387 	rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
388 	rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
389 
390 	return 0;
391 }
392 
393 static void rsnd_src_status_clear(struct rsnd_mod *mod)
394 {
395 	u32 val = OUF_SRC(rsnd_mod_id(mod));
396 
397 	rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
398 	rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
399 }
400 
401 static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
402 {
403 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
404 	struct device *dev = rsnd_priv_to_dev(priv);
405 	u32 val0, val1;
406 	u32 status0, status1;
407 	bool ret = false;
408 
409 	val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
410 
411 	/*
412 	 * WORKAROUND
413 	 *
414 	 * ignore over flow error when rsnd_src_sync_is_enabled()
415 	 */
416 	if (rsnd_src_sync_is_enabled(mod))
417 		val0 = val0 & 0xffff;
418 
419 	status0 = rsnd_mod_read(mod, SCU_SYS_STATUS0);
420 	status1 = rsnd_mod_read(mod, SCU_SYS_STATUS1);
421 	if ((status0 & val0) || (status1 & val1)) {
422 		rsnd_dbg_irq_status(dev, "%s err status : 0x%08x, 0x%08x\n",
423 			rsnd_mod_name(mod), status0, status1);
424 
425 		ret = true;
426 	}
427 
428 	return ret;
429 }
430 
431 static int rsnd_src_start(struct rsnd_mod *mod,
432 			  struct rsnd_dai_stream *io,
433 			  struct rsnd_priv *priv)
434 {
435 	u32 val;
436 
437 	/*
438 	 * WORKAROUND
439 	 *
440 	 * Enable SRC output if you want to use sync convert together with DVC
441 	 */
442 	val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
443 		0x01 : 0x11;
444 
445 	rsnd_mod_write(mod, SRC_CTRL, val);
446 
447 	return 0;
448 }
449 
450 static int rsnd_src_stop(struct rsnd_mod *mod,
451 			 struct rsnd_dai_stream *io,
452 			 struct rsnd_priv *priv)
453 {
454 	rsnd_mod_write(mod, SRC_CTRL, 0);
455 
456 	return 0;
457 }
458 
459 static int rsnd_src_init(struct rsnd_mod *mod,
460 			 struct rsnd_dai_stream *io,
461 			 struct rsnd_priv *priv)
462 {
463 	struct rsnd_src *src = rsnd_mod_to_src(mod);
464 
465 	/* reset sync convert_rate */
466 	src->sync.val = 0;
467 
468 	rsnd_mod_power_on(mod);
469 
470 	rsnd_src_activation(mod);
471 
472 	rsnd_src_set_convert_rate(io, mod);
473 
474 	rsnd_src_status_clear(mod);
475 
476 	return 0;
477 }
478 
479 static int rsnd_src_quit(struct rsnd_mod *mod,
480 			 struct rsnd_dai_stream *io,
481 			 struct rsnd_priv *priv)
482 {
483 	struct rsnd_src *src = rsnd_mod_to_src(mod);
484 
485 	rsnd_src_halt(mod);
486 
487 	rsnd_mod_power_off(mod);
488 
489 	/* reset sync convert_rate */
490 	src->sync.val = 0;
491 
492 	return 0;
493 }
494 
495 static void __rsnd_src_interrupt(struct rsnd_mod *mod,
496 				 struct rsnd_dai_stream *io)
497 {
498 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
499 	bool stop = false;
500 
501 	spin_lock(&priv->lock);
502 
503 	/* ignore all cases if not working */
504 	if (!rsnd_io_is_working(io))
505 		goto rsnd_src_interrupt_out;
506 
507 	if (rsnd_src_error_occurred(mod))
508 		stop = true;
509 
510 	rsnd_src_status_clear(mod);
511 rsnd_src_interrupt_out:
512 
513 	spin_unlock(&priv->lock);
514 
515 	if (stop)
516 		snd_pcm_stop_xrun(io->substream);
517 }
518 
519 static irqreturn_t rsnd_src_interrupt(int irq, void *data)
520 {
521 	struct rsnd_mod *mod = data;
522 
523 	rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
524 
525 	return IRQ_HANDLED;
526 }
527 
528 static int rsnd_src_probe_(struct rsnd_mod *mod,
529 			   struct rsnd_dai_stream *io,
530 			   struct rsnd_priv *priv)
531 {
532 	struct rsnd_src *src = rsnd_mod_to_src(mod);
533 	struct device *dev = rsnd_priv_to_dev(priv);
534 	int irq = src->irq;
535 	int ret;
536 
537 	if (irq > 0) {
538 		/*
539 		 * IRQ is not supported on non-DT
540 		 * see
541 		 *	rsnd_src_irq()
542 		 */
543 		ret = devm_request_irq(dev, irq,
544 				       rsnd_src_interrupt,
545 				       IRQF_SHARED,
546 				       dev_name(dev), mod);
547 		if (ret)
548 			return ret;
549 	}
550 
551 	ret = rsnd_dma_attach(io, mod, &src->dma);
552 
553 	return ret;
554 }
555 
556 static int rsnd_src_pcm_new(struct rsnd_mod *mod,
557 			    struct rsnd_dai_stream *io,
558 			    struct snd_soc_pcm_runtime *rtd)
559 {
560 	struct rsnd_src *src = rsnd_mod_to_src(mod);
561 	int ret;
562 
563 	/*
564 	 * enable SRC sync convert if possible
565 	 */
566 
567 	/*
568 	 * It can't use SRC Synchronous convert
569 	 * when Capture if it uses CMD
570 	 */
571 	if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
572 		return 0;
573 
574 	/*
575 	 * enable sync convert
576 	 */
577 	ret = rsnd_kctrl_new_s(mod, io, rtd,
578 			       rsnd_io_is_play(io) ?
579 			       "SRC Out Rate Switch" :
580 			       "SRC In Rate Switch",
581 			       rsnd_kctrl_accept_anytime,
582 			       rsnd_src_set_convert_rate,
583 			       &src->sen, 1);
584 	if (ret < 0)
585 		return ret;
586 
587 	ret = rsnd_kctrl_new_s(mod, io, rtd,
588 			       rsnd_io_is_play(io) ?
589 			       "SRC Out Rate" :
590 			       "SRC In Rate",
591 			       rsnd_kctrl_accept_runtime,
592 			       rsnd_src_set_convert_rate,
593 			       &src->sync, 192000);
594 
595 	return ret;
596 }
597 
598 static struct rsnd_mod_ops rsnd_src_ops = {
599 	.name		= SRC_NAME,
600 	.dma_req	= rsnd_src_dma_req,
601 	.probe		= rsnd_src_probe_,
602 	.init		= rsnd_src_init,
603 	.quit		= rsnd_src_quit,
604 	.start		= rsnd_src_start,
605 	.stop		= rsnd_src_stop,
606 	.irq		= rsnd_src_irq,
607 	.pcm_new	= rsnd_src_pcm_new,
608 	.get_status	= rsnd_mod_get_status,
609 };
610 
611 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
612 {
613 	if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
614 		id = 0;
615 
616 	return rsnd_mod_get(rsnd_src_get(priv, id));
617 }
618 
619 int rsnd_src_probe(struct rsnd_priv *priv)
620 {
621 	struct device_node *node;
622 	struct device_node *np;
623 	struct device *dev = rsnd_priv_to_dev(priv);
624 	struct rsnd_src *src;
625 	struct clk *clk;
626 	char name[RSND_SRC_NAME_SIZE];
627 	int i, nr, ret;
628 
629 	/* This driver doesn't support Gen1 at this point */
630 	if (rsnd_is_gen1(priv))
631 		return 0;
632 
633 	node = rsnd_src_of_node(priv);
634 	if (!node)
635 		return 0; /* not used is not error */
636 
637 	nr = of_get_child_count(node);
638 	if (!nr) {
639 		ret = -EINVAL;
640 		goto rsnd_src_probe_done;
641 	}
642 
643 	src	= devm_kcalloc(dev, nr, sizeof(*src), GFP_KERNEL);
644 	if (!src) {
645 		ret = -ENOMEM;
646 		goto rsnd_src_probe_done;
647 	}
648 
649 	priv->src_nr	= nr;
650 	priv->src	= src;
651 
652 	i = 0;
653 	for_each_child_of_node(node, np) {
654 		if (!of_device_is_available(np))
655 			goto skip;
656 
657 		src = rsnd_src_get(priv, i);
658 
659 		snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
660 			 SRC_NAME, i);
661 
662 		src->irq = irq_of_parse_and_map(np, 0);
663 		if (!src->irq) {
664 			ret = -EINVAL;
665 			of_node_put(np);
666 			goto rsnd_src_probe_done;
667 		}
668 
669 		clk = devm_clk_get(dev, name);
670 		if (IS_ERR(clk)) {
671 			ret = PTR_ERR(clk);
672 			of_node_put(np);
673 			goto rsnd_src_probe_done;
674 		}
675 
676 		ret = rsnd_mod_init(priv, rsnd_mod_get(src),
677 				    &rsnd_src_ops, clk, RSND_MOD_SRC, i);
678 		if (ret) {
679 			of_node_put(np);
680 			goto rsnd_src_probe_done;
681 		}
682 
683 skip:
684 		i++;
685 	}
686 
687 	ret = 0;
688 
689 rsnd_src_probe_done:
690 	of_node_put(node);
691 
692 	return ret;
693 }
694 
695 void rsnd_src_remove(struct rsnd_priv *priv)
696 {
697 	struct rsnd_src *src;
698 	int i;
699 
700 	for_each_rsnd_src(src, priv, i) {
701 		rsnd_mod_quit(rsnd_mod_get(src));
702 	}
703 }
704