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