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