xref: /openbmc/linux/sound/soc/sh/rcar/src.c (revision 92a2c6b2)
1 /*
2  * Renesas R-Car SRC support
3  *
4  * Copyright (C) 2013 Renesas Solutions Corp.
5  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include "rsnd.h"
12 
13 #define SRC_NAME "src"
14 
15 /* SRCx_STATUS */
16 #define OUF_SRCO	((1 << 12) | (1 << 13))
17 #define OUF_SRCI	((1 <<  9) | (1 <<  8))
18 
19 /* SCU_SYSTEM_STATUS0/1 */
20 #define OUF_SRC(id)	((1 << (id + 16)) | (1 << id))
21 
22 struct rsnd_src {
23 	struct rsnd_src_platform_info *info; /* rcar_snd.h */
24 	struct rsnd_mod mod;
25 	int err;
26 };
27 
28 #define RSND_SRC_NAME_SIZE 16
29 
30 #define rsnd_src_convert_rate(p) ((p)->info->convert_rate)
31 #define rsnd_mod_to_src(_mod)				\
32 	container_of((_mod), struct rsnd_src, mod)
33 #define rsnd_src_dma_available(src) \
34 	rsnd_dma_available(rsnd_mod_to_dma(&(src)->mod))
35 
36 #define for_each_rsnd_src(pos, priv, i)				\
37 	for ((i) = 0;						\
38 	     ((i) < rsnd_src_nr(priv)) &&			\
39 	     ((pos) = (struct rsnd_src *)(priv)->src + i);	\
40 	     i++)
41 
42 
43 /*
44  *		image of SRC (Sampling Rate Converter)
45  *
46  * 96kHz   <-> +-----+	48kHz	+-----+	 48kHz	+-------+
47  * 48kHz   <-> | SRC | <------>	| SSI |	<----->	| codec |
48  * 44.1kHz <-> +-----+		+-----+		+-------+
49  * ...
50  *
51  */
52 
53 /*
54  * src.c is caring...
55  *
56  * Gen1
57  *
58  * [mem] -> [SRU] -> [SSI]
59  *        |--------|
60  *
61  * Gen2
62  *
63  * [mem] -> [SRC] -> [SSIU] -> [SSI]
64  *        |-----------------|
65  */
66 
67 /*
68  *	How to use SRC bypass mode for debugging
69  *
70  * SRC has bypass mode, and it is useful for debugging.
71  * In Gen2 case,
72  * SRCm_MODE controls whether SRC is used or not
73  * SSI_MODE0 controls whether SSIU which receives SRC data
74  * is used or not.
75  * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
76  * but SRC bypass mode needs SSI_MODE0 only.
77  *
78  * This driver request
79  * struct rsnd_src_platform_info {
80  *	u32 convert_rate;
81  *	int dma_id;
82  * }
83  *
84  * rsnd_src_convert_rate() indicates
85  * above convert_rate, and it controls
86  * whether SRC is used or not.
87  *
88  * ex) doesn't use SRC
89  * static struct rsnd_dai_platform_info rsnd_dai = {
90  *	.playback = { .ssi = &rsnd_ssi[0], },
91  * };
92  *
93  * ex) uses SRC
94  * static struct rsnd_src_platform_info rsnd_src[] = {
95  *	RSND_SCU(48000, 0),
96  *	...
97  * };
98  * static struct rsnd_dai_platform_info rsnd_dai = {
99  *	.playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
100  * };
101  *
102  * ex) uses SRC bypass mode
103  * static struct rsnd_src_platform_info rsnd_src[] = {
104  *	RSND_SCU(0, 0),
105  *	...
106  * };
107  * static struct rsnd_dai_platform_info rsnd_dai = {
108  *	.playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
109  * };
110  *
111  */
112 
113 /*
114  *		Gen1/Gen2 common functions
115  */
116 int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
117 			int use_busif)
118 {
119 	struct rsnd_dai_stream *io = rsnd_mod_to_io(ssi_mod);
120 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
121 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
122 	int ssi_id = rsnd_mod_id(ssi_mod);
123 
124 	/*
125 	 * SSI_MODE0
126 	 */
127 	rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
128 		      !use_busif << ssi_id);
129 
130 	/*
131 	 * SSI_MODE1
132 	 */
133 	if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
134 		int shift = -1;
135 		switch (ssi_id) {
136 		case 1:
137 			shift = 0;
138 			break;
139 		case 2:
140 			shift = 2;
141 			break;
142 		case 4:
143 			shift = 16;
144 			break;
145 		}
146 
147 		if (shift >= 0)
148 			rsnd_mod_bset(ssi_mod, SSI_MODE1,
149 				      0x3 << shift,
150 				      rsnd_rdai_is_clk_master(rdai) ?
151 				      0x2 << shift : 0x1 << shift);
152 	}
153 
154 	/*
155 	 * DMA settings for SSIU
156 	 */
157 	if (use_busif) {
158 		u32 val = 0x76543210;
159 		u32 mask = ~0;
160 
161 		rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR,
162 			       rsnd_get_adinr(ssi_mod));
163 		rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE,  1);
164 		rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1);
165 
166 		mask <<= runtime->channels * 4;
167 		val = val & mask;
168 
169 		switch (runtime->sample_bits) {
170 		case 16:
171 			val |= 0x67452301 & ~mask;
172 			break;
173 		case 32:
174 			val |= 0x76543210 & ~mask;
175 			break;
176 		}
177 		rsnd_mod_write(ssi_mod, BUSIF_DALIGN, val);
178 
179 	}
180 
181 	return 0;
182 }
183 
184 int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod)
185 {
186 	/*
187 	 * DMA settings for SSIU
188 	 */
189 	rsnd_mod_write(ssi_mod, SSI_CTRL, 0);
190 
191 	return 0;
192 }
193 
194 int rsnd_src_ssi_irq_enable(struct rsnd_mod *ssi_mod)
195 {
196 	struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
197 
198 	if (rsnd_is_gen1(priv))
199 		return 0;
200 
201 	/* enable SSI interrupt if Gen2 */
202 	if (rsnd_ssi_is_dma_mode(ssi_mod))
203 		rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0e000000);
204 	else
205 		rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
206 
207 	return 0;
208 }
209 
210 int rsnd_src_ssi_irq_disable(struct rsnd_mod *ssi_mod)
211 {
212 	struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
213 
214 	if (rsnd_is_gen1(priv))
215 		return 0;
216 
217 	/* disable SSI interrupt if Gen2 */
218 	rsnd_mod_write(ssi_mod, INT_ENABLE, 0x00000000);
219 
220 	return 0;
221 }
222 
223 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
224 				   struct rsnd_dai_stream *io,
225 				   struct snd_pcm_runtime *runtime)
226 {
227 	struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
228 	struct rsnd_src *src;
229 	unsigned int rate = 0;
230 
231 	if (src_mod) {
232 		src = rsnd_mod_to_src(src_mod);
233 
234 		/*
235 		 * return convert rate if SRC is used,
236 		 * otherwise, return runtime->rate as usual
237 		 */
238 		rate = rsnd_src_convert_rate(src);
239 	}
240 
241 	if (!rate)
242 		rate = runtime->rate;
243 
244 	return rate;
245 }
246 
247 static int rsnd_src_set_convert_rate(struct rsnd_mod *mod)
248 {
249 	struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
250 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
251 	struct rsnd_src *src = rsnd_mod_to_src(mod);
252 	u32 convert_rate = rsnd_src_convert_rate(src);
253 	u32 fsrate = 0;
254 
255 	if (convert_rate)
256 		fsrate = 0x0400000 / convert_rate * runtime->rate;
257 
258 	/* set/clear soft reset */
259 	rsnd_mod_write(mod, SRC_SWRSR, 0);
260 	rsnd_mod_write(mod, SRC_SWRSR, 1);
261 
262 	/* Set channel number and output bit length */
263 	rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod));
264 
265 	/* Enable the initial value of IFS */
266 	if (fsrate) {
267 		rsnd_mod_write(mod, SRC_IFSCR, 1);
268 
269 		/* Set initial value of IFS */
270 		rsnd_mod_write(mod, SRC_IFSVR, fsrate);
271 	}
272 
273 	/* use DMA transfer */
274 	rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
275 
276 	return 0;
277 }
278 
279 static int rsnd_src_init(struct rsnd_mod *mod)
280 {
281 	struct rsnd_src *src = rsnd_mod_to_src(mod);
282 
283 	rsnd_mod_hw_start(mod);
284 
285 	src->err = 0;
286 
287 	/*
288 	 * Initialize the operation of the SRC internal circuits
289 	 * see rsnd_src_start()
290 	 */
291 	rsnd_mod_write(mod, SRC_SRCIR, 1);
292 
293 	return 0;
294 }
295 
296 static int rsnd_src_quit(struct rsnd_mod *mod,
297 			 struct rsnd_priv *priv)
298 {
299 	struct rsnd_src *src = rsnd_mod_to_src(mod);
300 	struct device *dev = rsnd_priv_to_dev(priv);
301 
302 	rsnd_mod_hw_stop(mod);
303 
304 	if (src->err)
305 		dev_warn(dev, "%s[%d] under/over flow err = %d\n",
306 			 rsnd_mod_name(mod), rsnd_mod_id(mod), src->err);
307 
308 	return 0;
309 }
310 
311 static int rsnd_src_start(struct rsnd_mod *mod)
312 {
313 	/*
314 	 * Cancel the initialization and operate the SRC function
315 	 * see rsnd_src_init()
316 	 */
317 	rsnd_mod_write(mod, SRC_SRCIR, 0);
318 
319 	return 0;
320 }
321 
322 static int rsnd_src_stop(struct rsnd_mod *mod)
323 {
324 	/* nothing to do */
325 	return 0;
326 }
327 
328 /*
329  *		Gen1 functions
330  */
331 static int rsnd_src_set_route_gen1(struct rsnd_mod *mod)
332 {
333 	struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
334 	struct src_route_config {
335 		u32 mask;
336 		int shift;
337 	} routes[] = {
338 		{ 0xF,  0, }, /* 0 */
339 		{ 0xF,  4, }, /* 1 */
340 		{ 0xF,  8, }, /* 2 */
341 		{ 0x7, 12, }, /* 3 */
342 		{ 0x7, 16, }, /* 4 */
343 		{ 0x7, 20, }, /* 5 */
344 		{ 0x7, 24, }, /* 6 */
345 		{ 0x3, 28, }, /* 7 */
346 		{ 0x3, 30, }, /* 8 */
347 	};
348 	u32 mask;
349 	u32 val;
350 	int id;
351 
352 	id = rsnd_mod_id(mod);
353 	if (id < 0 || id >= ARRAY_SIZE(routes))
354 		return -EIO;
355 
356 	/*
357 	 * SRC_ROUTE_SELECT
358 	 */
359 	val = rsnd_io_is_play(io) ? 0x1 : 0x2;
360 	val = val		<< routes[id].shift;
361 	mask = routes[id].mask	<< routes[id].shift;
362 
363 	rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
364 
365 	return 0;
366 }
367 
368 static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod)
369 {
370 	struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
371 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
372 	struct rsnd_src *src = rsnd_mod_to_src(mod);
373 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
374 	u32 convert_rate = rsnd_src_convert_rate(src);
375 	u32 mask;
376 	u32 val;
377 	int shift;
378 	int id = rsnd_mod_id(mod);
379 	int ret;
380 
381 	/*
382 	 * SRC_TIMING_SELECT
383 	 */
384 	shift	= (id % 4) * 8;
385 	mask	= 0x1F << shift;
386 
387 	/*
388 	 * ADG is used as source clock if SRC was used,
389 	 * then, SSI WS is used as destination clock.
390 	 * SSI WS is used as source clock if SRC is not used
391 	 * (when playback, source/destination become reverse when capture)
392 	 */
393 	ret = 0;
394 	if (convert_rate) {
395 		/* use ADG */
396 		val = 0;
397 		ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
398 						    runtime->rate,
399 						    convert_rate);
400 	} else if (8 == id) {
401 		/* use SSI WS, but SRU8 is special */
402 		val = id << shift;
403 	} else {
404 		/* use SSI WS */
405 		val = (id + 1) << shift;
406 	}
407 
408 	if (ret < 0)
409 		return ret;
410 
411 	switch (id / 4) {
412 	case 0:
413 		rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
414 		break;
415 	case 1:
416 		rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
417 		break;
418 	case 2:
419 		rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
420 		break;
421 	}
422 
423 	return 0;
424 }
425 
426 static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod)
427 {
428 	struct rsnd_src *src = rsnd_mod_to_src(mod);
429 	int ret;
430 
431 	ret = rsnd_src_set_convert_rate(mod);
432 	if (ret < 0)
433 		return ret;
434 
435 	/* Select SRC mode (fixed value) */
436 	rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
437 
438 	/* Set the restriction value of the FS ratio (98%) */
439 	rsnd_mod_write(mod, SRC_MNFSR,
440 		       rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
441 
442 	/* Gen1/Gen2 are not compatible */
443 	if (rsnd_src_convert_rate(src))
444 		rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
445 
446 	/* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
447 
448 	return 0;
449 }
450 
451 static int rsnd_src_probe_gen1(struct rsnd_mod *mod,
452 			       struct rsnd_priv *priv)
453 {
454 	struct device *dev = rsnd_priv_to_dev(priv);
455 
456 	dev_dbg(dev, "%s[%d] (Gen1) is probed\n",
457 		rsnd_mod_name(mod), rsnd_mod_id(mod));
458 
459 	return 0;
460 }
461 
462 static int rsnd_src_init_gen1(struct rsnd_mod *mod,
463 			      struct rsnd_priv *priv)
464 {
465 	int ret;
466 
467 	ret = rsnd_src_init(mod);
468 	if (ret < 0)
469 		return ret;
470 
471 	ret = rsnd_src_set_route_gen1(mod);
472 	if (ret < 0)
473 		return ret;
474 
475 	ret = rsnd_src_set_convert_rate_gen1(mod);
476 	if (ret < 0)
477 		return ret;
478 
479 	ret = rsnd_src_set_convert_timing_gen1(mod);
480 	if (ret < 0)
481 		return ret;
482 
483 	return 0;
484 }
485 
486 static int rsnd_src_start_gen1(struct rsnd_mod *mod,
487 			       struct rsnd_priv *priv)
488 {
489 	int id = rsnd_mod_id(mod);
490 
491 	rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
492 
493 	return rsnd_src_start(mod);
494 }
495 
496 static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
497 			      struct rsnd_priv *priv)
498 {
499 	int id = rsnd_mod_id(mod);
500 
501 	rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
502 
503 	return rsnd_src_stop(mod);
504 }
505 
506 static struct rsnd_mod_ops rsnd_src_gen1_ops = {
507 	.name	= SRC_NAME,
508 	.probe	= rsnd_src_probe_gen1,
509 	.init	= rsnd_src_init_gen1,
510 	.quit	= rsnd_src_quit,
511 	.start	= rsnd_src_start_gen1,
512 	.stop	= rsnd_src_stop_gen1,
513 };
514 
515 /*
516  *		Gen2 functions
517  */
518 #define rsnd_src_irq_enable_gen2(mod)  rsnd_src_irq_ctrol_gen2(mod, 1)
519 #define rsnd_src_irq_disable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 0)
520 static void rsnd_src_irq_ctrol_gen2(struct rsnd_mod *mod, int enable)
521 {
522 	struct rsnd_src *src = rsnd_mod_to_src(mod);
523 	u32 sys_int_val, int_val, sys_int_mask;
524 	int irq = src->info->irq;
525 	int id = rsnd_mod_id(mod);
526 
527 	sys_int_val =
528 	sys_int_mask = OUF_SRC(id);
529 	int_val = 0x3300;
530 
531 	/*
532 	 * IRQ is not supported on non-DT
533 	 * see
534 	 *	rsnd_src_probe_gen2()
535 	 */
536 	if ((irq <= 0) || !enable) {
537 		sys_int_val = 0;
538 		int_val = 0;
539 	}
540 
541 	rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
542 	rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
543 	rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
544 }
545 
546 static void rsnd_src_error_clear_gen2(struct rsnd_mod *mod)
547 {
548 	u32 val = OUF_SRC(rsnd_mod_id(mod));
549 
550 	rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
551 	rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
552 }
553 
554 static bool rsnd_src_error_record_gen2(struct rsnd_mod *mod)
555 {
556 	u32 val = OUF_SRC(rsnd_mod_id(mod));
557 	bool ret = false;
558 
559 	if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val) ||
560 	    (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val)) {
561 		struct rsnd_src *src = rsnd_mod_to_src(mod);
562 
563 		src->err++;
564 		ret = true;
565 	}
566 
567 	/* clear error static */
568 	rsnd_src_error_clear_gen2(mod);
569 
570 	return ret;
571 }
572 
573 static int _rsnd_src_start_gen2(struct rsnd_mod *mod)
574 {
575 	struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
576 	u32 val = rsnd_io_to_mod_dvc(io) ? 0x01 : 0x11;
577 
578 	rsnd_mod_write(mod, SRC_CTRL, val);
579 
580 	rsnd_src_error_clear_gen2(mod);
581 
582 	rsnd_src_start(mod);
583 
584 	rsnd_src_irq_enable_gen2(mod);
585 
586 	return 0;
587 }
588 
589 static int _rsnd_src_stop_gen2(struct rsnd_mod *mod)
590 {
591 	rsnd_src_irq_disable_gen2(mod);
592 
593 	rsnd_mod_write(mod, SRC_CTRL, 0);
594 
595 	rsnd_src_error_record_gen2(mod);
596 
597 	return rsnd_src_stop(mod);
598 }
599 
600 static irqreturn_t rsnd_src_interrupt_gen2(int irq, void *data)
601 {
602 	struct rsnd_mod *mod = data;
603 	struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
604 
605 	if (!io)
606 		return IRQ_NONE;
607 
608 	if (rsnd_src_error_record_gen2(mod)) {
609 		struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
610 		struct device *dev = rsnd_priv_to_dev(priv);
611 
612 		_rsnd_src_stop_gen2(mod);
613 		_rsnd_src_start_gen2(mod);
614 
615 		dev_dbg(dev, "%s[%d] restart\n",
616 			rsnd_mod_name(mod), rsnd_mod_id(mod));
617 	}
618 
619 	return IRQ_HANDLED;
620 }
621 
622 static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod)
623 {
624 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
625 	struct device *dev = rsnd_priv_to_dev(priv);
626 	struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
627 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
628 	struct rsnd_src *src = rsnd_mod_to_src(mod);
629 	u32 convert_rate = rsnd_src_convert_rate(src);
630 	uint ratio;
631 	int ret;
632 
633 	/* 6 - 1/6 are very enough ratio for SRC_BSDSR */
634 	if (!convert_rate)
635 		ratio = 0;
636 	else if (convert_rate > runtime->rate)
637 		ratio = 100 * convert_rate / runtime->rate;
638 	else
639 		ratio = 100 * runtime->rate / convert_rate;
640 
641 	if (ratio > 600) {
642 		dev_err(dev, "FSO/FSI ratio error\n");
643 		return -EINVAL;
644 	}
645 
646 	ret = rsnd_src_set_convert_rate(mod);
647 	if (ret < 0)
648 		return ret;
649 
650 	rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
651 
652 	if (convert_rate) {
653 		/* Gen1/Gen2 are not compatible */
654 		rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
655 	}
656 
657 	switch (rsnd_mod_id(mod)) {
658 	case 5:
659 	case 6:
660 	case 7:
661 	case 8:
662 		rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
663 		break;
664 	default:
665 		rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
666 		break;
667 	}
668 
669 	rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
670 
671 	return 0;
672 }
673 
674 static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod)
675 {
676 	struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
677 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
678 	struct rsnd_src *src = rsnd_mod_to_src(mod);
679 	u32 convert_rate = rsnd_src_convert_rate(src);
680 	int ret;
681 
682 	if (convert_rate)
683 		ret = rsnd_adg_set_convert_clk_gen2(mod, io,
684 						    runtime->rate,
685 						    convert_rate);
686 	else
687 		ret = rsnd_adg_set_convert_timing_gen2(mod, io);
688 
689 	return ret;
690 }
691 
692 static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
693 			       struct rsnd_priv *priv)
694 {
695 	struct rsnd_src *src = rsnd_mod_to_src(mod);
696 	struct device *dev = rsnd_priv_to_dev(priv);
697 	int irq = src->info->irq;
698 	int ret;
699 
700 	if (irq > 0) {
701 		/*
702 		 * IRQ is not supported on non-DT
703 		 * see
704 		 *	rsnd_src_irq_enable_gen2()
705 		 */
706 		ret = devm_request_irq(dev, irq,
707 				       rsnd_src_interrupt_gen2,
708 				       IRQF_SHARED,
709 				       dev_name(dev), mod);
710 		if (ret)
711 			goto rsnd_src_probe_gen2_fail;
712 	}
713 
714 	ret = rsnd_dma_init(priv,
715 			    rsnd_mod_to_dma(mod),
716 			    rsnd_info_is_playback(priv, src),
717 			    src->info->dma_id);
718 	if (ret)
719 		goto rsnd_src_probe_gen2_fail;
720 
721 	dev_dbg(dev, "%s[%d] (Gen2) is probed\n",
722 		rsnd_mod_name(mod), rsnd_mod_id(mod));
723 
724 	return ret;
725 
726 rsnd_src_probe_gen2_fail:
727 	dev_err(dev, "%s[%d] (Gen2) failed\n",
728 		rsnd_mod_name(mod), rsnd_mod_id(mod));
729 
730 	return ret;
731 }
732 
733 static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
734 				struct rsnd_priv *priv)
735 {
736 	rsnd_dma_quit(priv, rsnd_mod_to_dma(mod));
737 
738 	return 0;
739 }
740 
741 static int rsnd_src_init_gen2(struct rsnd_mod *mod,
742 			      struct rsnd_priv *priv)
743 {
744 	int ret;
745 
746 	ret = rsnd_src_init(mod);
747 	if (ret < 0)
748 		return ret;
749 
750 	ret = rsnd_src_set_convert_rate_gen2(mod);
751 	if (ret < 0)
752 		return ret;
753 
754 	ret = rsnd_src_set_convert_timing_gen2(mod);
755 	if (ret < 0)
756 		return ret;
757 
758 	return 0;
759 }
760 
761 static int rsnd_src_start_gen2(struct rsnd_mod *mod,
762 			       struct rsnd_priv *priv)
763 {
764 	rsnd_dma_start(rsnd_mod_to_dma(mod));
765 
766 	return _rsnd_src_start_gen2(mod);
767 }
768 
769 static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
770 			      struct rsnd_priv *priv)
771 {
772 	int ret;
773 
774 	ret = _rsnd_src_stop_gen2(mod);
775 
776 	rsnd_dma_stop(rsnd_mod_to_dma(mod));
777 
778 	return ret;
779 }
780 
781 static struct rsnd_mod_ops rsnd_src_gen2_ops = {
782 	.name	= SRC_NAME,
783 	.probe	= rsnd_src_probe_gen2,
784 	.remove	= rsnd_src_remove_gen2,
785 	.init	= rsnd_src_init_gen2,
786 	.quit	= rsnd_src_quit,
787 	.start	= rsnd_src_start_gen2,
788 	.stop	= rsnd_src_stop_gen2,
789 };
790 
791 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
792 {
793 	if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
794 		id = 0;
795 
796 	return &((struct rsnd_src *)(priv->src) + id)->mod;
797 }
798 
799 static void rsnd_of_parse_src(struct platform_device *pdev,
800 			      const struct rsnd_of_data *of_data,
801 			      struct rsnd_priv *priv)
802 {
803 	struct device_node *src_node;
804 	struct device_node *np;
805 	struct rcar_snd_info *info = rsnd_priv_to_info(priv);
806 	struct rsnd_src_platform_info *src_info;
807 	struct device *dev = &pdev->dev;
808 	int nr, i;
809 
810 	if (!of_data)
811 		return;
812 
813 	src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
814 	if (!src_node)
815 		return;
816 
817 	nr = of_get_child_count(src_node);
818 	if (!nr)
819 		goto rsnd_of_parse_src_end;
820 
821 	src_info = devm_kzalloc(dev,
822 				sizeof(struct rsnd_src_platform_info) * nr,
823 				GFP_KERNEL);
824 	if (!src_info) {
825 		dev_err(dev, "src info allocation error\n");
826 		goto rsnd_of_parse_src_end;
827 	}
828 
829 	info->src_info		= src_info;
830 	info->src_info_nr	= nr;
831 
832 	i = 0;
833 	for_each_child_of_node(src_node, np) {
834 		src_info[i].irq = irq_of_parse_and_map(np, 0);
835 
836 		i++;
837 	}
838 
839 rsnd_of_parse_src_end:
840 	of_node_put(src_node);
841 }
842 
843 int rsnd_src_probe(struct platform_device *pdev,
844 		   const struct rsnd_of_data *of_data,
845 		   struct rsnd_priv *priv)
846 {
847 	struct rcar_snd_info *info = rsnd_priv_to_info(priv);
848 	struct device *dev = rsnd_priv_to_dev(priv);
849 	struct rsnd_src *src;
850 	struct rsnd_mod_ops *ops;
851 	struct clk *clk;
852 	char name[RSND_SRC_NAME_SIZE];
853 	int i, nr;
854 
855 	ops = NULL;
856 	if (rsnd_is_gen1(priv))
857 		ops = &rsnd_src_gen1_ops;
858 	if (rsnd_is_gen2(priv))
859 		ops = &rsnd_src_gen2_ops;
860 	if (!ops) {
861 		dev_err(dev, "unknown Generation\n");
862 		return -EIO;
863 	}
864 
865 	rsnd_of_parse_src(pdev, of_data, priv);
866 
867 	/*
868 	 * init SRC
869 	 */
870 	nr	= info->src_info_nr;
871 	if (!nr)
872 		return 0;
873 
874 	src	= devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
875 	if (!src) {
876 		dev_err(dev, "SRC allocate failed\n");
877 		return -ENOMEM;
878 	}
879 
880 	priv->src_nr	= nr;
881 	priv->src	= src;
882 
883 	for_each_rsnd_src(src, priv, i) {
884 		snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
885 			 SRC_NAME, i);
886 
887 		clk = devm_clk_get(dev, name);
888 		if (IS_ERR(clk))
889 			return PTR_ERR(clk);
890 
891 		src->info = &info->src_info[i];
892 
893 		rsnd_mod_init(&src->mod, ops, clk, RSND_MOD_SRC, i);
894 
895 		dev_dbg(dev, "SRC%d probed\n", i);
896 	}
897 
898 	return 0;
899 }
900