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