xref: /openbmc/linux/sound/soc/sh/rcar/src.c (revision 9b9c2cd4)
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 	struct rsnd_kctrl_cfg_s sen;  /* sync convert enable */
26 	struct rsnd_kctrl_cfg_s sync; /* sync convert */
27 	u32 convert_rate; /* sampling rate convert */
28 	int err;
29 };
30 
31 #define RSND_SRC_NAME_SIZE 16
32 
33 #define rsnd_src_nr(priv) ((priv)->src_nr)
34 #define rsnd_enable_sync_convert(src) ((src)->sen.val)
35 #define rsnd_src_of_node(priv) \
36 	of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,src")
37 
38 #define rsnd_mod_to_src(_mod)				\
39 	container_of((_mod), struct rsnd_src, mod)
40 
41 #define for_each_rsnd_src(pos, priv, i)				\
42 	for ((i) = 0;						\
43 	     ((i) < rsnd_src_nr(priv)) &&			\
44 	     ((pos) = (struct rsnd_src *)(priv)->src + i);	\
45 	     i++)
46 
47 
48 /*
49  *		image of SRC (Sampling Rate Converter)
50  *
51  * 96kHz   <-> +-----+	48kHz	+-----+	 48kHz	+-------+
52  * 48kHz   <-> | SRC | <------>	| SSI |	<----->	| codec |
53  * 44.1kHz <-> +-----+		+-----+		+-------+
54  * ...
55  *
56  */
57 
58 /*
59  * src.c is caring...
60  *
61  * Gen1
62  *
63  * [mem] -> [SRU] -> [SSI]
64  *        |--------|
65  *
66  * Gen2
67  *
68  * [mem] -> [SRC] -> [SSIU] -> [SSI]
69  *        |-----------------|
70  */
71 
72 /*
73  *	How to use SRC bypass mode for debugging
74  *
75  * SRC has bypass mode, and it is useful for debugging.
76  * In Gen2 case,
77  * SRCm_MODE controls whether SRC is used or not
78  * SSI_MODE0 controls whether SSIU which receives SRC data
79  * is used or not.
80  * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
81  * but SRC bypass mode needs SSI_MODE0 only.
82  *
83  * This driver request
84  * struct rsnd_src_platform_info {
85  *	u32 convert_rate;
86  *	int dma_id;
87  * }
88  *
89  * rsnd_src_convert_rate() indicates
90  * above convert_rate, and it controls
91  * whether SRC is used or not.
92  *
93  * ex) doesn't use SRC
94  * static struct rsnd_dai_platform_info rsnd_dai = {
95  *	.playback = { .ssi = &rsnd_ssi[0], },
96  * };
97  *
98  * ex) uses SRC
99  * static struct rsnd_src_platform_info rsnd_src[] = {
100  *	RSND_SCU(48000, 0),
101  *	...
102  * };
103  * static struct rsnd_dai_platform_info rsnd_dai = {
104  *	.playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
105  * };
106  *
107  * ex) uses SRC bypass mode
108  * static struct rsnd_src_platform_info rsnd_src[] = {
109  *	RSND_SCU(0, 0),
110  *	...
111  * };
112  * static struct rsnd_dai_platform_info rsnd_dai = {
113  *	.playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
114  * };
115  *
116  */
117 
118 /*
119  *		Gen1/Gen2 common functions
120  */
121 static void rsnd_src_soft_reset(struct rsnd_mod *mod)
122 {
123 	rsnd_mod_write(mod, SRC_SWRSR, 0);
124 	rsnd_mod_write(mod, SRC_SWRSR, 1);
125 }
126 
127 
128 #define rsnd_src_initialize_lock(mod)	__rsnd_src_initialize_lock(mod, 1)
129 #define rsnd_src_initialize_unlock(mod)	__rsnd_src_initialize_lock(mod, 0)
130 static void __rsnd_src_initialize_lock(struct rsnd_mod *mod, u32 enable)
131 {
132 	rsnd_mod_write(mod, SRC_SRCIR, enable);
133 }
134 
135 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
136 					 struct rsnd_mod *mod)
137 {
138 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
139 	int is_play = rsnd_io_is_play(io);
140 
141 	return rsnd_dma_request_channel(rsnd_src_of_node(priv),
142 					mod,
143 					is_play ? "rx" : "tx");
144 }
145 
146 int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
147 			struct rsnd_dai_stream *io,
148 			int use_busif)
149 {
150 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
151 	int ssi_id = rsnd_mod_id(ssi_mod);
152 
153 	/*
154 	 * SSI_MODE0
155 	 */
156 	rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
157 		      !use_busif << ssi_id);
158 
159 	/*
160 	 * SSI_MODE1
161 	 */
162 	if (rsnd_ssi_is_pin_sharing(io)) {
163 		int shift = -1;
164 		switch (ssi_id) {
165 		case 1:
166 			shift = 0;
167 			break;
168 		case 2:
169 			shift = 2;
170 			break;
171 		case 4:
172 			shift = 16;
173 			break;
174 		}
175 
176 		if (shift >= 0)
177 			rsnd_mod_bset(ssi_mod, SSI_MODE1,
178 				      0x3 << shift,
179 				      rsnd_rdai_is_clk_master(rdai) ?
180 				      0x2 << shift : 0x1 << shift);
181 	}
182 
183 	/*
184 	 * DMA settings for SSIU
185 	 */
186 	if (use_busif) {
187 		u32 val = rsnd_get_dalign(ssi_mod, io);
188 
189 		rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR,
190 			       rsnd_get_adinr_bit(ssi_mod, io));
191 		rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE,  1);
192 		rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1);
193 
194 		rsnd_mod_write(ssi_mod, SSI_BUSIF_DALIGN, val);
195 	}
196 
197 	return 0;
198 }
199 
200 int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod,
201 		       struct rsnd_dai_stream *io)
202 {
203 	/*
204 	 * DMA settings for SSIU
205 	 */
206 	rsnd_mod_write(ssi_mod, SSI_CTRL, 0);
207 
208 	return 0;
209 }
210 
211 int rsnd_src_ssi_irq_enable(struct rsnd_mod *ssi_mod)
212 {
213 	struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
214 
215 	if (rsnd_is_gen1(priv))
216 		return 0;
217 
218 	/* enable SSI interrupt if Gen2 */
219 	rsnd_mod_write(ssi_mod, SSI_INT_ENABLE,
220 		       rsnd_ssi_is_dma_mode(ssi_mod) ?
221 		       0x0e000000 : 0x0f000000);
222 
223 	return 0;
224 }
225 
226 int rsnd_src_ssi_irq_disable(struct rsnd_mod *ssi_mod)
227 {
228 	struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
229 
230 	if (rsnd_is_gen1(priv))
231 		return 0;
232 
233 	/* disable SSI interrupt if Gen2 */
234 	rsnd_mod_write(ssi_mod, SSI_INT_ENABLE, 0x00000000);
235 
236 	return 0;
237 }
238 
239 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
240 				 struct rsnd_src *src)
241 {
242 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
243 	u32 convert_rate;
244 
245 	if (!runtime)
246 		return 0;
247 
248 	if (!rsnd_enable_sync_convert(src))
249 		return src->convert_rate;
250 
251 	convert_rate = src->sync.val;
252 
253 	if (!convert_rate)
254 		convert_rate = src->convert_rate;
255 
256 	if (!convert_rate)
257 		convert_rate = runtime->rate;
258 
259 	return convert_rate;
260 }
261 
262 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
263 				   struct rsnd_dai_stream *io,
264 				   struct snd_pcm_runtime *runtime)
265 {
266 	struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
267 	struct rsnd_src *src;
268 	unsigned int rate = 0;
269 
270 	if (src_mod) {
271 		src = rsnd_mod_to_src(src_mod);
272 
273 		/*
274 		 * return convert rate if SRC is used,
275 		 * otherwise, return runtime->rate as usual
276 		 */
277 		rate = rsnd_src_convert_rate(io, src);
278 	}
279 
280 	if (!rate)
281 		rate = runtime->rate;
282 
283 	return rate;
284 }
285 
286 static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
287 				     struct rsnd_dai_stream *io)
288 {
289 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
290 	struct rsnd_src *src = rsnd_mod_to_src(mod);
291 	u32 convert_rate = rsnd_src_convert_rate(io, src);
292 	u32 fsrate = 0;
293 
294 	if (convert_rate)
295 		fsrate = 0x0400000 / convert_rate * runtime->rate;
296 
297 	/* Set channel number and output bit length */
298 	rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr_bit(mod, io));
299 
300 	/* Enable the initial value of IFS */
301 	if (fsrate) {
302 		rsnd_mod_write(mod, SRC_IFSCR, 1);
303 
304 		/* Set initial value of IFS */
305 		rsnd_mod_write(mod, SRC_IFSVR, fsrate);
306 	}
307 
308 	/* use DMA transfer */
309 	rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
310 
311 	return 0;
312 }
313 
314 static int rsnd_src_hw_params(struct rsnd_mod *mod,
315 			      struct rsnd_dai_stream *io,
316 			      struct snd_pcm_substream *substream,
317 			      struct snd_pcm_hw_params *fe_params)
318 {
319 	struct rsnd_src *src = rsnd_mod_to_src(mod);
320 	struct snd_soc_pcm_runtime *fe = substream->private_data;
321 
322 	/* default value (mainly for non-DT) */
323 	src->convert_rate = src->info->convert_rate;
324 
325 	/*
326 	 * SRC assumes that it is used under DPCM if user want to use
327 	 * sampling rate convert. Then, SRC should be FE.
328 	 * And then, this function will be called *after* BE settings.
329 	 * this means, each BE already has fixuped hw_params.
330 	 * see
331 	 *	dpcm_fe_dai_hw_params()
332 	 *	dpcm_be_dai_hw_params()
333 	 */
334 	if (fe->dai_link->dynamic) {
335 		int stream = substream->stream;
336 		struct snd_soc_dpcm *dpcm;
337 		struct snd_pcm_hw_params *be_params;
338 
339 		list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
340 			be_params = &dpcm->hw_params;
341 
342 			if (params_rate(fe_params) != params_rate(be_params))
343 				src->convert_rate = params_rate(be_params);
344 		}
345 	}
346 
347 	return 0;
348 }
349 
350 static int rsnd_src_init(struct rsnd_mod *mod,
351 			 struct rsnd_priv *priv)
352 {
353 	struct rsnd_src *src = rsnd_mod_to_src(mod);
354 
355 	rsnd_mod_power_on(mod);
356 
357 	rsnd_src_soft_reset(mod);
358 
359 	rsnd_src_initialize_lock(mod);
360 
361 	src->err = 0;
362 
363 	/* reset sync convert_rate */
364 	src->sync.val = 0;
365 
366 	return 0;
367 }
368 
369 static int rsnd_src_quit(struct rsnd_mod *mod,
370 			 struct rsnd_dai_stream *io,
371 			 struct rsnd_priv *priv)
372 {
373 	struct rsnd_src *src = rsnd_mod_to_src(mod);
374 	struct device *dev = rsnd_priv_to_dev(priv);
375 
376 	rsnd_mod_power_off(mod);
377 
378 	if (src->err)
379 		dev_warn(dev, "%s[%d] under/over flow err = %d\n",
380 			 rsnd_mod_name(mod), rsnd_mod_id(mod), src->err);
381 
382 	src->convert_rate = 0;
383 
384 	/* reset sync convert_rate */
385 	src->sync.val = 0;
386 
387 	return 0;
388 }
389 
390 static int rsnd_src_start(struct rsnd_mod *mod)
391 {
392 	rsnd_src_initialize_unlock(mod);
393 
394 	return 0;
395 }
396 
397 static int rsnd_src_stop(struct rsnd_mod *mod)
398 {
399 	/* nothing to do */
400 	return 0;
401 }
402 
403 /*
404  *		Gen1 functions
405  */
406 static int rsnd_src_set_route_gen1(struct rsnd_dai_stream *io,
407 				   struct rsnd_mod *mod)
408 {
409 	struct src_route_config {
410 		u32 mask;
411 		int shift;
412 	} routes[] = {
413 		{ 0xF,  0, }, /* 0 */
414 		{ 0xF,  4, }, /* 1 */
415 		{ 0xF,  8, }, /* 2 */
416 		{ 0x7, 12, }, /* 3 */
417 		{ 0x7, 16, }, /* 4 */
418 		{ 0x7, 20, }, /* 5 */
419 		{ 0x7, 24, }, /* 6 */
420 		{ 0x3, 28, }, /* 7 */
421 		{ 0x3, 30, }, /* 8 */
422 	};
423 	u32 mask;
424 	u32 val;
425 	int id;
426 
427 	id = rsnd_mod_id(mod);
428 	if (id < 0 || id >= ARRAY_SIZE(routes))
429 		return -EIO;
430 
431 	/*
432 	 * SRC_ROUTE_SELECT
433 	 */
434 	val = rsnd_io_is_play(io) ? 0x1 : 0x2;
435 	val = val		<< routes[id].shift;
436 	mask = routes[id].mask	<< routes[id].shift;
437 
438 	rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
439 
440 	return 0;
441 }
442 
443 static int rsnd_src_set_convert_timing_gen1(struct rsnd_dai_stream *io,
444 					    struct rsnd_mod *mod)
445 {
446 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
447 	struct rsnd_src *src = rsnd_mod_to_src(mod);
448 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
449 	u32 convert_rate = rsnd_src_convert_rate(io, src);
450 	u32 mask;
451 	u32 val;
452 	int shift;
453 	int id = rsnd_mod_id(mod);
454 	int ret;
455 
456 	/*
457 	 * SRC_TIMING_SELECT
458 	 */
459 	shift	= (id % 4) * 8;
460 	mask	= 0x1F << shift;
461 
462 	/*
463 	 * ADG is used as source clock if SRC was used,
464 	 * then, SSI WS is used as destination clock.
465 	 * SSI WS is used as source clock if SRC is not used
466 	 * (when playback, source/destination become reverse when capture)
467 	 */
468 	ret = 0;
469 	if (convert_rate) {
470 		/* use ADG */
471 		val = 0;
472 		ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
473 						    runtime->rate,
474 						    convert_rate);
475 	} else if (8 == id) {
476 		/* use SSI WS, but SRU8 is special */
477 		val = id << shift;
478 	} else {
479 		/* use SSI WS */
480 		val = (id + 1) << shift;
481 	}
482 
483 	if (ret < 0)
484 		return ret;
485 
486 	switch (id / 4) {
487 	case 0:
488 		rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
489 		break;
490 	case 1:
491 		rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
492 		break;
493 	case 2:
494 		rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
495 		break;
496 	}
497 
498 	return 0;
499 }
500 
501 static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
502 					  struct rsnd_dai_stream *io)
503 {
504 	struct rsnd_src *src = rsnd_mod_to_src(mod);
505 	int ret;
506 
507 	ret = rsnd_src_set_convert_rate(mod, io);
508 	if (ret < 0)
509 		return ret;
510 
511 	/* Select SRC mode (fixed value) */
512 	rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
513 
514 	/* Set the restriction value of the FS ratio (98%) */
515 	rsnd_mod_write(mod, SRC_MNFSR,
516 		       rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
517 
518 	/* Gen1/Gen2 are not compatible */
519 	if (rsnd_src_convert_rate(io, src))
520 		rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
521 
522 	/* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
523 
524 	return 0;
525 }
526 
527 static int rsnd_src_init_gen1(struct rsnd_mod *mod,
528 			      struct rsnd_dai_stream *io,
529 			      struct rsnd_priv *priv)
530 {
531 	int ret;
532 
533 	ret = rsnd_src_init(mod, priv);
534 	if (ret < 0)
535 		return ret;
536 
537 	ret = rsnd_src_set_route_gen1(io, mod);
538 	if (ret < 0)
539 		return ret;
540 
541 	ret = rsnd_src_set_convert_rate_gen1(mod, io);
542 	if (ret < 0)
543 		return ret;
544 
545 	ret = rsnd_src_set_convert_timing_gen1(io, mod);
546 	if (ret < 0)
547 		return ret;
548 
549 	return 0;
550 }
551 
552 static int rsnd_src_start_gen1(struct rsnd_mod *mod,
553 			       struct rsnd_dai_stream *io,
554 			       struct rsnd_priv *priv)
555 {
556 	int id = rsnd_mod_id(mod);
557 
558 	rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
559 
560 	return rsnd_src_start(mod);
561 }
562 
563 static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
564 			      struct rsnd_dai_stream *io,
565 			      struct rsnd_priv *priv)
566 {
567 	int id = rsnd_mod_id(mod);
568 
569 	rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
570 
571 	return rsnd_src_stop(mod);
572 }
573 
574 static struct rsnd_mod_ops rsnd_src_gen1_ops = {
575 	.name	= SRC_NAME,
576 	.dma_req = rsnd_src_dma_req,
577 	.init	= rsnd_src_init_gen1,
578 	.quit	= rsnd_src_quit,
579 	.start	= rsnd_src_start_gen1,
580 	.stop	= rsnd_src_stop_gen1,
581 	.hw_params = rsnd_src_hw_params,
582 };
583 
584 /*
585  *		Gen2 functions
586  */
587 #define rsnd_src_irq_enable_gen2(mod)  rsnd_src_irq_ctrol_gen2(mod, 1)
588 #define rsnd_src_irq_disable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 0)
589 static void rsnd_src_irq_ctrol_gen2(struct rsnd_mod *mod, int enable)
590 {
591 	struct rsnd_src *src = rsnd_mod_to_src(mod);
592 	u32 sys_int_val, int_val, sys_int_mask;
593 	int irq = src->info->irq;
594 	int id = rsnd_mod_id(mod);
595 
596 	sys_int_val =
597 	sys_int_mask = OUF_SRC(id);
598 	int_val = 0x3300;
599 
600 	/*
601 	 * IRQ is not supported on non-DT
602 	 * see
603 	 *	rsnd_src_probe_gen2()
604 	 */
605 	if ((irq <= 0) || !enable) {
606 		sys_int_val = 0;
607 		int_val = 0;
608 	}
609 
610 	/*
611 	 * WORKAROUND
612 	 *
613 	 * ignore over flow error when rsnd_enable_sync_convert()
614 	 */
615 	if (rsnd_enable_sync_convert(src))
616 		sys_int_val = sys_int_val & 0xffff;
617 
618 	rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
619 	rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
620 	rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
621 }
622 
623 static void rsnd_src_error_clear_gen2(struct rsnd_mod *mod)
624 {
625 	u32 val = OUF_SRC(rsnd_mod_id(mod));
626 
627 	rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
628 	rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
629 }
630 
631 static bool rsnd_src_error_record_gen2(struct rsnd_mod *mod)
632 {
633 	struct rsnd_src *src = rsnd_mod_to_src(mod);
634 	u32 val0, val1;
635 	bool ret = false;
636 
637 	val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
638 
639 	/*
640 	 * WORKAROUND
641 	 *
642 	 * ignore over flow error when rsnd_enable_sync_convert()
643 	 */
644 	if (rsnd_enable_sync_convert(src))
645 		val0 = val0 & 0xffff;
646 
647 	if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
648 	    (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1)) {
649 		struct rsnd_src *src = rsnd_mod_to_src(mod);
650 
651 		src->err++;
652 		ret = true;
653 	}
654 
655 	/* clear error static */
656 	rsnd_src_error_clear_gen2(mod);
657 
658 	return ret;
659 }
660 
661 static int _rsnd_src_start_gen2(struct rsnd_mod *mod,
662 				struct rsnd_dai_stream *io)
663 {
664 	struct rsnd_src *src = rsnd_mod_to_src(mod);
665 	u32 val;
666 
667 	val = rsnd_get_dalign(mod, io);
668 
669 	rsnd_mod_write(mod, SRC_BUSIF_DALIGN, val);
670 
671 	/*
672 	 * WORKAROUND
673 	 *
674 	 * Enable SRC output if you want to use sync convert together with DVC
675 	 */
676 	val = (rsnd_io_to_mod_dvc(io) && !rsnd_enable_sync_convert(src)) ?
677 		0x01 : 0x11;
678 
679 	rsnd_mod_write(mod, SRC_CTRL, val);
680 
681 	rsnd_src_error_clear_gen2(mod);
682 
683 	rsnd_src_start(mod);
684 
685 	rsnd_src_irq_enable_gen2(mod);
686 
687 	return 0;
688 }
689 
690 static int _rsnd_src_stop_gen2(struct rsnd_mod *mod)
691 {
692 	rsnd_src_irq_disable_gen2(mod);
693 
694 	rsnd_mod_write(mod, SRC_CTRL, 0);
695 
696 	rsnd_src_error_record_gen2(mod);
697 
698 	return rsnd_src_stop(mod);
699 }
700 
701 static void __rsnd_src_interrupt_gen2(struct rsnd_mod *mod,
702 				      struct rsnd_dai_stream *io)
703 {
704 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
705 
706 	spin_lock(&priv->lock);
707 
708 	/* ignore all cases if not working */
709 	if (!rsnd_io_is_working(io))
710 		goto rsnd_src_interrupt_gen2_out;
711 
712 	if (rsnd_src_error_record_gen2(mod)) {
713 		struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
714 		struct rsnd_src *src = rsnd_mod_to_src(mod);
715 		struct device *dev = rsnd_priv_to_dev(priv);
716 
717 		dev_dbg(dev, "%s[%d] restart\n",
718 			rsnd_mod_name(mod), rsnd_mod_id(mod));
719 
720 		_rsnd_src_stop_gen2(mod);
721 		if (src->err < 1024)
722 			_rsnd_src_start_gen2(mod, io);
723 		else
724 			dev_warn(dev, "no more SRC restart\n");
725 	}
726 
727 rsnd_src_interrupt_gen2_out:
728 	spin_unlock(&priv->lock);
729 }
730 
731 static irqreturn_t rsnd_src_interrupt_gen2(int irq, void *data)
732 {
733 	struct rsnd_mod *mod = data;
734 
735 	rsnd_mod_interrupt(mod, __rsnd_src_interrupt_gen2);
736 
737 	return IRQ_HANDLED;
738 }
739 
740 static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
741 					  struct rsnd_dai_stream *io)
742 {
743 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
744 	struct device *dev = rsnd_priv_to_dev(priv);
745 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
746 	struct rsnd_src *src = rsnd_mod_to_src(mod);
747 	u32 convert_rate = rsnd_src_convert_rate(io, src);
748 	u32 cr, route;
749 	uint ratio;
750 	int ret;
751 
752 	/* 6 - 1/6 are very enough ratio for SRC_BSDSR */
753 	if (!convert_rate)
754 		ratio = 0;
755 	else if (convert_rate > runtime->rate)
756 		ratio = 100 * convert_rate / runtime->rate;
757 	else
758 		ratio = 100 * runtime->rate / convert_rate;
759 
760 	if (ratio > 600) {
761 		dev_err(dev, "FSO/FSI ratio error\n");
762 		return -EINVAL;
763 	}
764 
765 	ret = rsnd_src_set_convert_rate(mod, io);
766 	if (ret < 0)
767 		return ret;
768 
769 	cr	= 0x00011110;
770 	route	= 0x0;
771 	if (convert_rate) {
772 		route	= 0x1;
773 
774 		if (rsnd_enable_sync_convert(src)) {
775 			cr |= 0x1;
776 			route |= rsnd_io_is_play(io) ?
777 				(0x1 << 24) : (0x1 << 25);
778 		}
779 	}
780 
781 	rsnd_mod_write(mod, SRC_SRCCR, cr);
782 	rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
783 
784 	switch (rsnd_mod_id(mod)) {
785 	case 5:
786 	case 6:
787 	case 7:
788 	case 8:
789 		rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
790 		break;
791 	default:
792 		rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
793 		break;
794 	}
795 
796 	rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
797 
798 	return 0;
799 }
800 
801 static int rsnd_src_set_convert_timing_gen2(struct rsnd_dai_stream *io,
802 					    struct rsnd_mod *mod)
803 {
804 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
805 	struct rsnd_src *src = rsnd_mod_to_src(mod);
806 	u32 convert_rate = rsnd_src_convert_rate(io, src);
807 	int ret;
808 
809 	if (convert_rate)
810 		ret = rsnd_adg_set_convert_clk_gen2(mod, io,
811 						    runtime->rate,
812 						    convert_rate);
813 	else
814 		ret = rsnd_adg_set_convert_timing_gen2(mod, io);
815 
816 	return ret;
817 }
818 
819 static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
820 			       struct rsnd_dai_stream *io,
821 			       struct rsnd_priv *priv)
822 {
823 	struct rsnd_src *src = rsnd_mod_to_src(mod);
824 	struct device *dev = rsnd_priv_to_dev(priv);
825 	int irq = src->info->irq;
826 	int ret;
827 
828 	if (irq > 0) {
829 		/*
830 		 * IRQ is not supported on non-DT
831 		 * see
832 		 *	rsnd_src_irq_enable_gen2()
833 		 */
834 		ret = devm_request_irq(dev, irq,
835 				       rsnd_src_interrupt_gen2,
836 				       IRQF_SHARED,
837 				       dev_name(dev), mod);
838 		if (ret)
839 			return ret;
840 	}
841 
842 	ret = rsnd_dma_init(io,
843 			    rsnd_mod_to_dma(mod),
844 			    src->info->dma_id);
845 
846 	return ret;
847 }
848 
849 static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
850 				struct rsnd_dai_stream *io,
851 				struct rsnd_priv *priv)
852 {
853 	rsnd_dma_quit(io, rsnd_mod_to_dma(mod));
854 
855 	return 0;
856 }
857 
858 static int rsnd_src_init_gen2(struct rsnd_mod *mod,
859 			      struct rsnd_dai_stream *io,
860 			      struct rsnd_priv *priv)
861 {
862 	int ret;
863 
864 	ret = rsnd_src_init(mod, priv);
865 	if (ret < 0)
866 		return ret;
867 
868 	ret = rsnd_src_set_convert_rate_gen2(mod, io);
869 	if (ret < 0)
870 		return ret;
871 
872 	ret = rsnd_src_set_convert_timing_gen2(io, mod);
873 	if (ret < 0)
874 		return ret;
875 
876 	return 0;
877 }
878 
879 static int rsnd_src_start_gen2(struct rsnd_mod *mod,
880 			       struct rsnd_dai_stream *io,
881 			       struct rsnd_priv *priv)
882 {
883 	rsnd_dma_start(io, rsnd_mod_to_dma(mod));
884 
885 	return _rsnd_src_start_gen2(mod, io);
886 }
887 
888 static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
889 			      struct rsnd_dai_stream *io,
890 			      struct rsnd_priv *priv)
891 {
892 	int ret;
893 
894 	ret = _rsnd_src_stop_gen2(mod);
895 
896 	rsnd_dma_stop(io, rsnd_mod_to_dma(mod));
897 
898 	return ret;
899 }
900 
901 static void rsnd_src_reconvert_update(struct rsnd_dai_stream *io,
902 				      struct rsnd_mod *mod)
903 {
904 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
905 	struct rsnd_src *src = rsnd_mod_to_src(mod);
906 	u32 convert_rate = rsnd_src_convert_rate(io, src);
907 	u32 fsrate;
908 
909 	if (!runtime)
910 		return;
911 
912 	if (!convert_rate)
913 		convert_rate = runtime->rate;
914 
915 	fsrate = 0x0400000 / convert_rate * runtime->rate;
916 
917 	/* update IFS */
918 	rsnd_mod_write(mod, SRC_IFSVR, fsrate);
919 }
920 
921 static int rsnd_src_pcm_new_gen2(struct rsnd_mod *mod,
922 			    struct rsnd_dai_stream *io,
923 			    struct snd_soc_pcm_runtime *rtd)
924 {
925 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
926 	struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
927 	struct rsnd_src *src = rsnd_mod_to_src(mod);
928 	int ret;
929 
930 	/*
931 	 * enable SRC sync convert if possible
932 	 */
933 
934 	/*
935 	 * SRC sync convert needs clock master
936 	 */
937 	if (!rsnd_rdai_is_clk_master(rdai))
938 		return 0;
939 
940 	/*
941 	 * SRC In doesn't work if DVC was enabled
942 	 */
943 	if (dvc && !rsnd_io_is_play(io))
944 		return 0;
945 
946 	/*
947 	 * enable sync convert
948 	 */
949 	ret = rsnd_kctrl_new_s(mod, io, rtd,
950 			       rsnd_io_is_play(io) ?
951 			       "SRC Out Rate Switch" :
952 			       "SRC In Rate Switch",
953 			       rsnd_src_reconvert_update,
954 			       &src->sen, 1);
955 	if (ret < 0)
956 		return ret;
957 
958 	ret = rsnd_kctrl_new_s(mod, io, rtd,
959 			       rsnd_io_is_play(io) ?
960 			       "SRC Out Rate" :
961 			       "SRC In Rate",
962 			       rsnd_src_reconvert_update,
963 			       &src->sync, 192000);
964 
965 	return ret;
966 }
967 
968 static struct rsnd_mod_ops rsnd_src_gen2_ops = {
969 	.name	= SRC_NAME,
970 	.dma_req = rsnd_src_dma_req,
971 	.probe	= rsnd_src_probe_gen2,
972 	.remove	= rsnd_src_remove_gen2,
973 	.init	= rsnd_src_init_gen2,
974 	.quit	= rsnd_src_quit,
975 	.start	= rsnd_src_start_gen2,
976 	.stop	= rsnd_src_stop_gen2,
977 	.hw_params = rsnd_src_hw_params,
978 	.pcm_new = rsnd_src_pcm_new_gen2,
979 };
980 
981 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
982 {
983 	if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
984 		id = 0;
985 
986 	return rsnd_mod_get((struct rsnd_src *)(priv->src) + id);
987 }
988 
989 static void rsnd_of_parse_src(struct platform_device *pdev,
990 			      const struct rsnd_of_data *of_data,
991 			      struct rsnd_priv *priv)
992 {
993 	struct device_node *src_node;
994 	struct device_node *np;
995 	struct rcar_snd_info *info = rsnd_priv_to_info(priv);
996 	struct rsnd_src_platform_info *src_info;
997 	struct device *dev = &pdev->dev;
998 	int nr, i;
999 
1000 	if (!of_data)
1001 		return;
1002 
1003 	src_node = rsnd_src_of_node(priv);
1004 	if (!src_node)
1005 		return;
1006 
1007 	nr = of_get_child_count(src_node);
1008 	if (!nr)
1009 		goto rsnd_of_parse_src_end;
1010 
1011 	src_info = devm_kzalloc(dev,
1012 				sizeof(struct rsnd_src_platform_info) * nr,
1013 				GFP_KERNEL);
1014 	if (!src_info) {
1015 		dev_err(dev, "src info allocation error\n");
1016 		goto rsnd_of_parse_src_end;
1017 	}
1018 
1019 	info->src_info		= src_info;
1020 	info->src_info_nr	= nr;
1021 
1022 	i = 0;
1023 	for_each_child_of_node(src_node, np) {
1024 		src_info[i].irq = irq_of_parse_and_map(np, 0);
1025 
1026 		i++;
1027 	}
1028 
1029 rsnd_of_parse_src_end:
1030 	of_node_put(src_node);
1031 }
1032 
1033 int rsnd_src_probe(struct platform_device *pdev,
1034 		   const struct rsnd_of_data *of_data,
1035 		   struct rsnd_priv *priv)
1036 {
1037 	struct rcar_snd_info *info = rsnd_priv_to_info(priv);
1038 	struct device *dev = rsnd_priv_to_dev(priv);
1039 	struct rsnd_src *src;
1040 	struct rsnd_mod_ops *ops;
1041 	struct clk *clk;
1042 	char name[RSND_SRC_NAME_SIZE];
1043 	int i, nr, ret;
1044 
1045 	ops = NULL;
1046 	if (rsnd_is_gen1(priv)) {
1047 		ops = &rsnd_src_gen1_ops;
1048 		dev_warn(dev, "Gen1 support will be removed soon\n");
1049 	}
1050 	if (rsnd_is_gen2(priv))
1051 		ops = &rsnd_src_gen2_ops;
1052 	if (!ops) {
1053 		dev_err(dev, "unknown Generation\n");
1054 		return -EIO;
1055 	}
1056 
1057 	rsnd_of_parse_src(pdev, of_data, priv);
1058 
1059 	/*
1060 	 * init SRC
1061 	 */
1062 	nr	= info->src_info_nr;
1063 	if (!nr)
1064 		return 0;
1065 
1066 	src	= devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
1067 	if (!src)
1068 		return -ENOMEM;
1069 
1070 	priv->src_nr	= nr;
1071 	priv->src	= src;
1072 
1073 	for_each_rsnd_src(src, priv, i) {
1074 		snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
1075 			 SRC_NAME, i);
1076 
1077 		clk = devm_clk_get(dev, name);
1078 		if (IS_ERR(clk))
1079 			return PTR_ERR(clk);
1080 
1081 		src->info = &info->src_info[i];
1082 
1083 		ret = rsnd_mod_init(priv, rsnd_mod_get(src), ops, clk, RSND_MOD_SRC, i);
1084 		if (ret)
1085 			return ret;
1086 	}
1087 
1088 	return 0;
1089 }
1090 
1091 void rsnd_src_remove(struct platform_device *pdev,
1092 		     struct rsnd_priv *priv)
1093 {
1094 	struct rsnd_src *src;
1095 	int i;
1096 
1097 	for_each_rsnd_src(src, priv, i) {
1098 		rsnd_mod_quit(rsnd_mod_get(src));
1099 	}
1100 }
1101