xref: /linux/sound/soc/renesas/rcar/src.c (revision fab183d632628381b466a41479489541ac0e29a0)
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 use Synchronous Sampling Rate Convert (if no DVC)
10  *
11  *	amixer set "SRC Out Rate" on
12  *	aplay xxx.wav &
13  *	amixer set "SRC Out Rate" 96000 // convert rate to 96000Hz
14  *	amixer set "SRC Out Rate" 22050 // convert rate to 22050Hz
15  */
16 
17 /*
18  * you can enable below define if you don't need
19  * SSI interrupt status debug message when debugging
20  * see rsnd_print_irq_status()
21  *
22  * #define RSND_DEBUG_NO_IRQ_STATUS 1
23  */
24 
25 #include <linux/of_irq.h>
26 #include "rsnd.h"
27 
28 #define SRC_NAME "src"
29 
30 /* SCU_SYSTEM_STATUS0/1 */
31 #define OUF_SRC(id)	((1 << (id + 16)) | (1 << id))
32 
33 struct rsnd_src {
34 	struct rsnd_mod mod;
35 	struct rsnd_mod *dma;
36 	struct rsnd_kctrl_cfg_s sen;  /* sync convert enable */
37 	struct rsnd_kctrl_cfg_s sync; /* sync convert */
38 	u32 current_sync_rate;
39 	int irq;
40 };
41 
42 
43 #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
44 #define rsnd_src_nr(priv) ((priv)->src_nr)
45 #define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val)
46 
47 #define rsnd_mod_to_src(_mod)				\
48 	container_of((_mod), struct rsnd_src, mod)
49 
50 #define for_each_rsnd_src(pos, priv, i)				\
51 	for ((i) = 0;						\
52 	     ((i) < rsnd_src_nr(priv)) &&			\
53 	     ((pos) = (struct rsnd_src *)(priv)->src + i);	\
54 	     i++)
55 
56 struct rsnd_src_ctrl {
57 	struct clk *scu;
58 	struct clk *scu_x2;
59 	struct clk *scu_supply;
60 };
61 
62 #define rsnd_priv_to_src_ctrl(priv) \
63 	((struct rsnd_src_ctrl *)(priv)->src_ctrl)
64 
65 /*
66  *		image of SRC (Sampling Rate Converter)
67  *
68  * 96kHz   <-> +-----+	48kHz	+-----+	 48kHz	+-------+
69  * 48kHz   <-> | SRC | <------>	| SSI |	<----->	| codec |
70  * 44.1kHz <-> +-----+		+-----+		+-------+
71  * ...
72  *
73  */
74 
rsnd_src_activation(struct rsnd_mod * mod)75 static void rsnd_src_activation(struct rsnd_mod *mod)
76 {
77 	rsnd_mod_write(mod, SRC_SWRSR, 0);
78 	rsnd_mod_write(mod, SRC_SWRSR, 1);
79 }
80 
rsnd_src_halt(struct rsnd_mod * mod)81 static void rsnd_src_halt(struct rsnd_mod *mod)
82 {
83 	rsnd_mod_write(mod, SRC_SRCIR, 1);
84 	rsnd_mod_write(mod, SRC_SWRSR, 0);
85 }
86 
rsnd_src_dma_req(struct rsnd_dai_stream * io,struct rsnd_mod * mod)87 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
88 					 struct rsnd_mod *mod)
89 {
90 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
91 	int is_play = rsnd_io_is_play(io);
92 
93 	return rsnd_dma_request_channel(rsnd_src_of_node(priv),
94 					SRC_NAME, mod,
95 					is_play ? "rx" : "tx");
96 }
97 
rsnd_src_convert_rate(struct rsnd_dai_stream * io,struct rsnd_mod * mod)98 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
99 				 struct rsnd_mod *mod)
100 {
101 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
102 	struct rsnd_src *src = rsnd_mod_to_src(mod);
103 	u32 convert_rate;
104 
105 	if (!runtime)
106 		return 0;
107 
108 	if (!rsnd_src_sync_is_enabled(mod))
109 		return rsnd_io_converted_rate(io);
110 
111 	convert_rate = src->current_sync_rate;
112 
113 	if (!convert_rate)
114 		convert_rate = rsnd_io_converted_rate(io);
115 
116 	if (!convert_rate)
117 		convert_rate = runtime->rate;
118 
119 	return convert_rate;
120 }
121 
rsnd_src_get_rate(struct rsnd_priv * priv,struct rsnd_dai_stream * io,int is_in)122 unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
123 			       struct rsnd_dai_stream *io,
124 			       int is_in)
125 {
126 	struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
127 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
128 	unsigned int rate = 0;
129 	int is_play = rsnd_io_is_play(io);
130 
131 	/*
132 	 * Playback
133 	 * runtime_rate -> [SRC] -> convert_rate
134 	 *
135 	 * Capture
136 	 * convert_rate -> [SRC] -> runtime_rate
137 	 */
138 
139 	if (is_play == is_in)
140 		return runtime->rate;
141 
142 	/*
143 	 * return convert rate if SRC is used,
144 	 * otherwise, return runtime->rate as usual
145 	 */
146 	if (src_mod)
147 		rate = rsnd_src_convert_rate(io, src_mod);
148 
149 	if (!rate)
150 		rate = runtime->rate;
151 
152 	return rate;
153 }
154 
155 static const u32 bsdsr_table_pattern1[] = {
156 	0x01800000, /* 6 - 1/6 */
157 	0x01000000, /* 6 - 1/4 */
158 	0x00c00000, /* 6 - 1/3 */
159 	0x00800000, /* 6 - 1/2 */
160 	0x00600000, /* 6 - 2/3 */
161 	0x00400000, /* 6 - 1   */
162 };
163 
164 static const u32 bsdsr_table_pattern2[] = {
165 	0x02400000, /* 6 - 1/6 */
166 	0x01800000, /* 6 - 1/4 */
167 	0x01200000, /* 6 - 1/3 */
168 	0x00c00000, /* 6 - 1/2 */
169 	0x00900000, /* 6 - 2/3 */
170 	0x00600000, /* 6 - 1   */
171 };
172 
173 static const u32 bsisr_table[] = {
174 	0x00100060, /* 6 - 1/6 */
175 	0x00100040, /* 6 - 1/4 */
176 	0x00100030, /* 6 - 1/3 */
177 	0x00100020, /* 6 - 1/2 */
178 	0x00100020, /* 6 - 2/3 */
179 	0x00100020, /* 6 - 1   */
180 };
181 
182 static const u32 chan288888[] = {
183 	0x00000006, /* 1 to 2 */
184 	0x000001fe, /* 1 to 8 */
185 	0x000001fe, /* 1 to 8 */
186 	0x000001fe, /* 1 to 8 */
187 	0x000001fe, /* 1 to 8 */
188 	0x000001fe, /* 1 to 8 */
189 };
190 
191 static const u32 chan244888[] = {
192 	0x00000006, /* 1 to 2 */
193 	0x0000001e, /* 1 to 4 */
194 	0x0000001e, /* 1 to 4 */
195 	0x000001fe, /* 1 to 8 */
196 	0x000001fe, /* 1 to 8 */
197 	0x000001fe, /* 1 to 8 */
198 };
199 
200 static const u32 chan222222[] = {
201 	0x00000006, /* 1 to 2 */
202 	0x00000006, /* 1 to 2 */
203 	0x00000006, /* 1 to 2 */
204 	0x00000006, /* 1 to 2 */
205 	0x00000006, /* 1 to 2 */
206 	0x00000006, /* 1 to 2 */
207 };
208 
rsnd_src_set_convert_rate(struct rsnd_dai_stream * io,struct rsnd_mod * mod)209 static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
210 				      struct rsnd_mod *mod)
211 {
212 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
213 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
214 	struct rsnd_src *src = rsnd_mod_to_src(mod);
215 	u32 fin, fout, new_rate;
216 	int inc, cnt, rate;
217 	u64 base, val;
218 
219 	if (!runtime)
220 		return;
221 
222 	if (!rsnd_src_sync_is_enabled(mod))
223 		return;
224 
225 	fin	= rsnd_src_get_in_rate(priv, io);
226 	fout	= rsnd_src_get_out_rate(priv, io);
227 
228 	new_rate = src->sync.val;
229 
230 	if (!new_rate)
231 		new_rate = fout;
232 
233 	/* Do nothing if no diff */
234 	if (new_rate == src->current_sync_rate)
235 		return;
236 
237 	/*
238 	 * SRCm_IFSVR::INTIFS can change within 1%
239 	 * see
240 	 *	SRCm_IFSVR::INTIFS Note
241 	 */
242 	inc = fout / 100;
243 	cnt = abs(new_rate - fout) / inc;
244 	if (fout > new_rate)
245 		inc *= -1;
246 
247 	/*
248 	 * After start running SRC, we can update only SRC_IFSVR
249 	 * for Synchronous Mode
250 	 */
251 	base = (u64)0x0400000 * fin;
252 	rate  = fout;
253 	for (int i = 0; i < cnt; i++) {
254 		val   = base;
255 		rate += inc;
256 		do_div(val, rate);
257 
258 		rsnd_mod_write(mod, SRC_IFSVR, val);
259 	}
260 	val   = base;
261 	do_div(val, new_rate);
262 
263 	rsnd_mod_write(mod, SRC_IFSVR, val);
264 
265 	/* update current_sync_rate */
266 	src->current_sync_rate = new_rate;
267 }
268 
rsnd_src_init_convert_rate(struct rsnd_dai_stream * io,struct rsnd_mod * mod)269 static void rsnd_src_init_convert_rate(struct rsnd_dai_stream *io,
270 				       struct rsnd_mod *mod)
271 {
272 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
273 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
274 	struct device *dev = rsnd_priv_to_dev(priv);
275 	int is_play = rsnd_io_is_play(io);
276 	int use_src = 0;
277 	u32 fin, fout;
278 	u32 ifscr, adinr;
279 	u32 cr, route;
280 	u32 i_busif, o_busif, tmp;
281 	const u32 *bsdsr_table;
282 	const u32 *chptn;
283 	uint ratio;
284 	int chan;
285 	int idx;
286 
287 	if (!runtime)
288 		return;
289 
290 	fin  = rsnd_src_get_in_rate(priv, io);
291 	fout = rsnd_src_get_out_rate(priv, io);
292 
293 	chan = rsnd_runtime_channel_original(io);
294 
295 	/* 6 - 1/6 are very enough ratio for SRC_BSDSR */
296 	if (fin == fout)
297 		ratio = 0;
298 	else if (fin > fout)
299 		ratio = 100 * fin / fout;
300 	else
301 		ratio = 100 * fout / fin;
302 
303 	if (ratio > 600) {
304 		dev_err(dev, "FSO/FSI ratio error\n");
305 		return;
306 	}
307 
308 	use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
309 
310 	/*
311 	 * SRC_ADINR
312 	 */
313 	adinr = rsnd_get_adinr_bit(mod, io) | chan;
314 
315 	/*
316 	 * SRC_IFSCR
317 	 * SRC_SRCCR / SRC_ROUTE_MODE0
318 	 */
319 	ifscr	= 0;
320 	cr	= 0x00011110;
321 	route	= 0x0;
322 	if (use_src) {
323 		route	= 0x1;
324 		ifscr	= 0x1;
325 
326 		if (rsnd_src_sync_is_enabled(mod)) {
327 			cr |= 0x1;
328 			route |= rsnd_io_is_play(io) ?
329 				(0x1 << 24) : (0x1 << 25);
330 		}
331 	}
332 
333 	/*
334 	 * SRC_BSDSR / SRC_BSISR
335 	 *
336 	 * see
337 	 *	Combination of Register Setting Related to
338 	 *	FSO/FSI Ratio and Channel, Latency
339 	 */
340 	switch (rsnd_mod_id(mod)) {
341 	case 0:
342 		chptn		= chan288888;
343 		bsdsr_table	= bsdsr_table_pattern1;
344 		break;
345 	case 1:
346 	case 3:
347 	case 4:
348 		chptn		= chan244888;
349 		bsdsr_table	= bsdsr_table_pattern1;
350 		break;
351 	case 2:
352 	case 9:
353 		chptn		= chan222222;
354 		bsdsr_table	= bsdsr_table_pattern1;
355 		break;
356 	case 5:
357 	case 6:
358 	case 7:
359 	case 8:
360 		chptn		= chan222222;
361 		bsdsr_table	= bsdsr_table_pattern2;
362 		break;
363 	default:
364 		goto convert_rate_err;
365 	}
366 
367 	/*
368 	 * E3 need to overwrite
369 	 */
370 	if (rsnd_is_gen3_e3(priv))
371 		switch (rsnd_mod_id(mod)) {
372 		case 0:
373 		case 4:
374 			chptn	= chan222222;
375 		}
376 
377 	for (idx = 0; idx < ARRAY_SIZE(chan222222); idx++)
378 		if (chptn[idx] & (1 << chan))
379 			break;
380 
381 	if (chan > 8 ||
382 	    idx >= ARRAY_SIZE(chan222222))
383 		goto convert_rate_err;
384 
385 	/* BUSIF_MODE */
386 	tmp = rsnd_get_busif_shift(io, mod);
387 	i_busif = ( is_play ? tmp : 0) | 1;
388 	o_busif = (!is_play ? tmp : 0) | 1;
389 
390 	rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
391 
392 	rsnd_mod_write(mod, SRC_SRCIR, 1);	/* initialize */
393 	rsnd_mod_write(mod, SRC_ADINR, adinr);
394 	rsnd_mod_write(mod, SRC_IFSCR, ifscr);
395 	rsnd_mod_write(mod, SRC_SRCCR, cr);
396 	rsnd_mod_write(mod, SRC_BSDSR, bsdsr_table[idx]);
397 	rsnd_mod_write(mod, SRC_BSISR, bsisr_table[idx]);
398 	rsnd_mod_write(mod, SRC_SRCIR, 0);	/* cancel initialize */
399 
400 	rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
401 	rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);
402 
403 	rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
404 
405 	rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
406 
407 	/* update SRC_IFSVR */
408 	rsnd_src_set_convert_rate(io, mod);
409 
410 	return;
411 
412 convert_rate_err:
413 	dev_err(dev, "unknown BSDSR/BSDIR settings\n");
414 }
415 
rsnd_src_irq(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv,int enable)416 static int rsnd_src_irq(struct rsnd_mod *mod,
417 			struct rsnd_dai_stream *io,
418 			struct rsnd_priv *priv,
419 			int enable)
420 {
421 	struct rsnd_src *src = rsnd_mod_to_src(mod);
422 	u32 sys_int_val, int_val, sys_int_mask;
423 	int irq = src->irq;
424 	int id = rsnd_mod_id(mod);
425 
426 	sys_int_val =
427 	sys_int_mask = OUF_SRC(id);
428 	int_val = 0x3300;
429 
430 	/*
431 	 * IRQ is not supported on non-DT
432 	 * see
433 	 *	rsnd_src_probe_()
434 	 */
435 	if ((irq <= 0) || !enable) {
436 		sys_int_val = 0;
437 		int_val = 0;
438 	}
439 
440 	/*
441 	 * WORKAROUND
442 	 *
443 	 * ignore over flow error when rsnd_src_sync_is_enabled()
444 	 */
445 	if (rsnd_src_sync_is_enabled(mod))
446 		sys_int_val = sys_int_val & 0xffff;
447 
448 	rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
449 	rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
450 	rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
451 
452 	return 0;
453 }
454 
rsnd_src_status_clear(struct rsnd_mod * mod)455 static void rsnd_src_status_clear(struct rsnd_mod *mod)
456 {
457 	u32 val = OUF_SRC(rsnd_mod_id(mod));
458 
459 	rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
460 	rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
461 }
462 
rsnd_src_error_occurred(struct rsnd_mod * mod)463 static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
464 {
465 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
466 	struct device *dev = rsnd_priv_to_dev(priv);
467 	u32 val0, val1;
468 	u32 status0, status1;
469 	bool ret = false;
470 
471 	val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
472 
473 	/*
474 	 * WORKAROUND
475 	 *
476 	 * ignore over flow error when rsnd_src_sync_is_enabled()
477 	 */
478 	if (rsnd_src_sync_is_enabled(mod))
479 		val0 = val0 & 0xffff;
480 
481 	status0 = rsnd_mod_read(mod, SCU_SYS_STATUS0);
482 	status1 = rsnd_mod_read(mod, SCU_SYS_STATUS1);
483 	if ((status0 & val0) || (status1 & val1)) {
484 		rsnd_print_irq_status(dev, "%s err status : 0x%08x, 0x%08x\n",
485 				      rsnd_mod_name(mod), status0, status1);
486 
487 		ret = true;
488 	}
489 
490 	return ret;
491 }
492 
rsnd_src_start(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)493 static int rsnd_src_start(struct rsnd_mod *mod,
494 			  struct rsnd_dai_stream *io,
495 			  struct rsnd_priv *priv)
496 {
497 	u32 val;
498 
499 	/*
500 	 * WORKAROUND
501 	 *
502 	 * Enable SRC output if you want to use sync convert together with DVC
503 	 */
504 	val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
505 		0x01 : 0x11;
506 
507 	rsnd_mod_write(mod, SRC_CTRL, val);
508 
509 	return 0;
510 }
511 
rsnd_src_stop(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)512 static int rsnd_src_stop(struct rsnd_mod *mod,
513 			 struct rsnd_dai_stream *io,
514 			 struct rsnd_priv *priv)
515 {
516 	rsnd_mod_write(mod, SRC_CTRL, 0);
517 
518 	return 0;
519 }
520 
rsnd_src_init(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)521 static int rsnd_src_init(struct rsnd_mod *mod,
522 			 struct rsnd_dai_stream *io,
523 			 struct rsnd_priv *priv)
524 {
525 	struct rsnd_src *src = rsnd_mod_to_src(mod);
526 	int ret;
527 
528 	/* reset sync convert_rate */
529 	src->sync.val		=
530 	src->current_sync_rate	= 0;
531 
532 	ret = rsnd_mod_power_on(mod);
533 	if (ret < 0)
534 		return ret;
535 
536 	rsnd_src_activation(mod);
537 
538 	rsnd_src_init_convert_rate(io, mod);
539 
540 	rsnd_src_status_clear(mod);
541 
542 	return 0;
543 }
544 
rsnd_src_quit(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)545 static int rsnd_src_quit(struct rsnd_mod *mod,
546 			 struct rsnd_dai_stream *io,
547 			 struct rsnd_priv *priv)
548 {
549 	struct rsnd_src *src = rsnd_mod_to_src(mod);
550 
551 	rsnd_src_halt(mod);
552 
553 	rsnd_mod_power_off(mod);
554 
555 	/* reset sync convert_rate */
556 	src->sync.val		=
557 	src->current_sync_rate	= 0;
558 
559 	return 0;
560 }
561 
__rsnd_src_interrupt(struct rsnd_mod * mod,struct rsnd_dai_stream * io)562 static void __rsnd_src_interrupt(struct rsnd_mod *mod,
563 				 struct rsnd_dai_stream *io)
564 {
565 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
566 	bool stop = false;
567 
568 	scoped_guard(spinlock, &priv->lock) {
569 		/* ignore all cases if not working */
570 		if (!rsnd_io_is_working(io))
571 			break;
572 
573 		if (rsnd_src_error_occurred(mod))
574 			stop = true;
575 
576 		rsnd_src_status_clear(mod);
577 	}
578 
579 	if (stop)
580 		snd_pcm_stop_xrun(io->substream);
581 }
582 
rsnd_src_interrupt(int irq,void * data)583 static irqreturn_t rsnd_src_interrupt(int irq, void *data)
584 {
585 	struct rsnd_mod *mod = data;
586 
587 	rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
588 
589 	return IRQ_HANDLED;
590 }
591 
rsnd_src_kctrl_accept_runtime(struct rsnd_dai_stream * io)592 static int rsnd_src_kctrl_accept_runtime(struct rsnd_dai_stream *io)
593 {
594 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
595 
596 	if (!runtime) {
597 		struct rsnd_priv *priv = rsnd_io_to_priv(io);
598 		struct device *dev = rsnd_priv_to_dev(priv);
599 
600 		dev_warn(dev, "\"SRC Out Rate\" can use during running\n");
601 
602 		return 0;
603 	}
604 
605 	return 1;
606 }
607 
rsnd_src_probe_(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)608 static int rsnd_src_probe_(struct rsnd_mod *mod,
609 			   struct rsnd_dai_stream *io,
610 			   struct rsnd_priv *priv)
611 {
612 	struct rsnd_src *src = rsnd_mod_to_src(mod);
613 	struct device *dev = rsnd_priv_to_dev(priv);
614 	int irq = src->irq;
615 	int ret;
616 
617 	if (irq > 0) {
618 		/*
619 		 * IRQ is not supported on non-DT
620 		 * see
621 		 *	rsnd_src_irq()
622 		 */
623 		ret = devm_request_irq(dev, irq,
624 				       rsnd_src_interrupt,
625 				       IRQF_SHARED,
626 				       dev_name(dev), mod);
627 		if (ret)
628 			return ret;
629 	}
630 
631 	ret = rsnd_dma_attach(io, mod, &src->dma);
632 
633 	return ret;
634 }
635 
rsnd_src_pcm_new(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct snd_soc_pcm_runtime * rtd)636 static int rsnd_src_pcm_new(struct rsnd_mod *mod,
637 			    struct rsnd_dai_stream *io,
638 			    struct snd_soc_pcm_runtime *rtd)
639 {
640 	struct rsnd_src *src = rsnd_mod_to_src(mod);
641 	int ret;
642 
643 	/*
644 	 * enable SRC sync convert if possible
645 	 */
646 
647 	/*
648 	 * It can't use SRC Synchronous convert
649 	 * when Capture if it uses CMD
650 	 */
651 	if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
652 		return 0;
653 
654 	/*
655 	 * enable sync convert
656 	 */
657 	ret = rsnd_kctrl_new_s(mod, io, rtd,
658 			       rsnd_io_is_play(io) ?
659 			       "SRC Out Rate Switch" :
660 			       "SRC In Rate Switch",
661 			       rsnd_kctrl_accept_anytime,
662 			       rsnd_src_init_convert_rate,
663 			       &src->sen, 1);
664 	if (ret < 0)
665 		return ret;
666 
667 	ret = rsnd_kctrl_new_s(mod, io, rtd,
668 			       rsnd_io_is_play(io) ?
669 			       "SRC Out Rate" :
670 			       "SRC In Rate",
671 			       rsnd_src_kctrl_accept_runtime,
672 			       rsnd_src_set_convert_rate,
673 			       &src->sync, 192000);
674 
675 	return ret;
676 }
677 
678 #ifdef CONFIG_DEBUG_FS
rsnd_src_debug_info(struct seq_file * m,struct rsnd_dai_stream * io,struct rsnd_mod * mod)679 static void rsnd_src_debug_info(struct seq_file *m,
680 				struct rsnd_dai_stream *io,
681 				struct rsnd_mod *mod)
682 {
683 	rsnd_debugfs_mod_reg_show(m, mod, RSND_BASE_SCU,
684 				  rsnd_mod_id(mod) * 0x20, 0x20);
685 	seq_puts(m, "\n");
686 	rsnd_debugfs_mod_reg_show(m, mod, RSND_BASE_SCU,
687 				  0x1c0, 0x20);
688 	seq_puts(m, "\n");
689 	rsnd_debugfs_mod_reg_show(m, mod, RSND_BASE_SCU,
690 				  0x200 + rsnd_mod_id(mod) * 0x40, 0x40);
691 }
692 #define DEBUG_INFO .debug_info = rsnd_src_debug_info
693 #else
694 #define DEBUG_INFO
695 #endif
696 
697 static struct rsnd_mod_ops rsnd_src_ops = {
698 	.name		= SRC_NAME,
699 	.dma_req	= rsnd_src_dma_req,
700 	.probe		= rsnd_src_probe_,
701 	.init		= rsnd_src_init,
702 	.quit		= rsnd_src_quit,
703 	.start		= rsnd_src_start,
704 	.stop		= rsnd_src_stop,
705 	.irq		= rsnd_src_irq,
706 	.pcm_new	= rsnd_src_pcm_new,
707 	.get_status	= rsnd_mod_get_status,
708 	DEBUG_INFO
709 };
710 
rsnd_src_mod_get(struct rsnd_priv * priv,int id)711 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
712 {
713 	if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
714 		id = 0;
715 
716 	return rsnd_mod_get(rsnd_src_get(priv, id));
717 }
718 
rsnd_src_probe(struct rsnd_priv * priv)719 int rsnd_src_probe(struct rsnd_priv *priv)
720 {
721 	struct device_node *node;
722 	struct device *dev = rsnd_priv_to_dev(priv);
723 	struct reset_control *rstc;
724 	struct rsnd_src_ctrl *src_ctrl;
725 	struct rsnd_src *src;
726 	struct clk *clk;
727 	int i, nr, ret;
728 
729 	node = rsnd_src_of_node(priv);
730 	if (!node)
731 		return 0; /* not used is not error */
732 
733 	nr = rsnd_node_count(priv, node, SRC_NAME);
734 	if (!nr) {
735 		ret = -EINVAL;
736 		goto rsnd_src_probe_done;
737 	}
738 
739 	src_ctrl = devm_kzalloc(dev, sizeof(*src_ctrl), GFP_KERNEL);
740 	if (!src_ctrl) {
741 		ret = -ENOMEM;
742 		goto rsnd_src_probe_done;
743 	}
744 
745 	src	= devm_kcalloc(dev, nr, sizeof(*src), GFP_KERNEL);
746 	if (!src) {
747 		ret = -ENOMEM;
748 		goto rsnd_src_probe_done;
749 	}
750 
751 	priv->src_nr	= nr;
752 	priv->src	= src;
753 	priv->src_ctrl	= src_ctrl;
754 
755 	src_ctrl->scu = devm_clk_get_optional_enabled(dev, "scu");
756 	if (IS_ERR(src_ctrl->scu)) {
757 		ret = dev_err_probe(dev, PTR_ERR(src_ctrl->scu),
758 				    "failed to get scu clock\n");
759 		goto rsnd_src_probe_done;
760 	}
761 
762 	src_ctrl->scu_x2 = devm_clk_get_optional_enabled(dev, "scu_x2");
763 	if (IS_ERR(src_ctrl->scu_x2)) {
764 		ret = dev_err_probe(dev, PTR_ERR(src_ctrl->scu_x2),
765 				    "failed to get scu_x2 clock\n");
766 		goto rsnd_src_probe_done;
767 	}
768 
769 	src_ctrl->scu_supply = devm_clk_get_optional_enabled(dev, "scu_supply");
770 	if (IS_ERR(src_ctrl->scu_supply)) {
771 		ret = dev_err_probe(dev, PTR_ERR(src_ctrl->scu_supply),
772 				    "failed to get scu_supply clock\n");
773 		goto rsnd_src_probe_done;
774 	}
775 
776 	/*
777 	 * Shared SCU reset for every SRC module; acquire once.
778 	 * R-Car platforms typically don't have SRC reset controls.
779 	 */
780 	rstc = devm_reset_control_get_optional_shared(dev, "scu");
781 	if (IS_ERR(rstc)) {
782 		ret = PTR_ERR(rstc);
783 		goto rsnd_src_probe_done;
784 	}
785 
786 	i = 0;
787 	for_each_child_of_node_scoped(node, np) {
788 		if (!of_device_is_available(np))
789 			goto skip;
790 
791 		i = rsnd_node_fixed_index(dev, np, SRC_NAME, i);
792 		if (i < 0) {
793 			ret = -EINVAL;
794 			goto rsnd_src_probe_done;
795 		}
796 
797 		src = rsnd_src_get(priv, i);
798 
799 		src->irq = irq_of_parse_and_map(np, 0);
800 		if (!src->irq) {
801 			ret = -EINVAL;
802 			goto rsnd_src_probe_done;
803 		}
804 
805 		clk = rsnd_devm_clk_get_indexed(dev, SRC_NAME, i);
806 		if (IS_ERR(clk)) {
807 			ret = PTR_ERR(clk);
808 			goto rsnd_src_probe_done;
809 		}
810 
811 		ret = rsnd_mod_init(priv, rsnd_mod_get(src),
812 				    &rsnd_src_ops, clk, rstc, RSND_MOD_SRC, i);
813 		if (ret)
814 			goto rsnd_src_probe_done;
815 
816 skip:
817 		i++;
818 	}
819 
820 	ret = 0;
821 
822 rsnd_src_probe_done:
823 	of_node_put(node);
824 
825 	return ret;
826 }
827 
rsnd_src_remove(struct rsnd_priv * priv)828 void rsnd_src_remove(struct rsnd_priv *priv)
829 {
830 	struct rsnd_src *src;
831 	int i;
832 
833 	for_each_rsnd_src(src, priv, i) {
834 		rsnd_mod_quit(rsnd_mod_get(src));
835 	}
836 }
837 
rsnd_src_suspend(struct rsnd_priv * priv)838 void rsnd_src_suspend(struct rsnd_priv *priv)
839 {
840 	struct rsnd_src_ctrl *src_ctrl = rsnd_priv_to_src_ctrl(priv);
841 	struct rsnd_src *src;
842 	int i;
843 
844 	if (!src_ctrl)
845 		return;
846 
847 	for_each_rsnd_src(src, priv, i)
848 		rsnd_suspend_clk_reset(rsnd_mod_get(src)->clk,
849 				       rsnd_mod_get(src)->rstc);
850 
851 	clk_disable_unprepare(src_ctrl->scu_x2);
852 	clk_disable_unprepare(src_ctrl->scu);
853 	clk_disable_unprepare(src_ctrl->scu_supply);
854 }
855 
rsnd_src_resume(struct rsnd_priv * priv)856 void rsnd_src_resume(struct rsnd_priv *priv)
857 {
858 	struct rsnd_src_ctrl *src_ctrl = rsnd_priv_to_src_ctrl(priv);
859 	struct rsnd_src *src;
860 	int i;
861 
862 	if (!src_ctrl)
863 		return;
864 
865 	clk_prepare_enable(src_ctrl->scu_supply);
866 	clk_prepare_enable(src_ctrl->scu);
867 	clk_prepare_enable(src_ctrl->scu_x2);
868 
869 	for_each_rsnd_src(src, priv, i)
870 		rsnd_resume_clk_reset(rsnd_mod_get(src)->clk,
871 				      rsnd_mod_get(src)->rstc);
872 }
873