xref: /openbmc/linux/sound/soc/sh/rcar/core.c (revision 249592bf)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Renesas R-Car SRU/SCU/SSIU/SSI support
4 //
5 // Copyright (C) 2013 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 //
8 // Based on fsi.c
9 // Kuninori Morimoto <morimoto.kuninori@renesas.com>
10 
11 /*
12  * Renesas R-Car sound device structure
13  *
14  * Gen1
15  *
16  * SRU		: Sound Routing Unit
17  *  - SRC	: Sampling Rate Converter
18  *  - CMD
19  *    - CTU	: Channel Count Conversion Unit
20  *    - MIX	: Mixer
21  *    - DVC	: Digital Volume and Mute Function
22  *  - SSI	: Serial Sound Interface
23  *
24  * Gen2
25  *
26  * SCU		: Sampling Rate Converter Unit
27  *  - SRC	: Sampling Rate Converter
28  *  - CMD
29  *   - CTU	: Channel Count Conversion Unit
30  *   - MIX	: Mixer
31  *   - DVC	: Digital Volume and Mute Function
32  * SSIU		: Serial Sound Interface Unit
33  *  - SSI	: Serial Sound Interface
34  */
35 
36 /*
37  *	driver data Image
38  *
39  * rsnd_priv
40  *   |
41  *   | ** this depends on Gen1/Gen2
42  *   |
43  *   +- gen
44  *   |
45  *   | ** these depend on data path
46  *   | ** gen and platform data control it
47  *   |
48  *   +- rdai[0]
49  *   |   |		 sru     ssiu      ssi
50  *   |   +- playback -> [mod] -> [mod] -> [mod] -> ...
51  *   |   |
52  *   |   |		 sru     ssiu      ssi
53  *   |   +- capture  -> [mod] -> [mod] -> [mod] -> ...
54  *   |
55  *   +- rdai[1]
56  *   |   |		 sru     ssiu      ssi
57  *   |   +- playback -> [mod] -> [mod] -> [mod] -> ...
58  *   |   |
59  *   |   |		 sru     ssiu      ssi
60  *   |   +- capture  -> [mod] -> [mod] -> [mod] -> ...
61  *   ...
62  *   |
63  *   | ** these control ssi
64  *   |
65  *   +- ssi
66  *   |  |
67  *   |  +- ssi[0]
68  *   |  +- ssi[1]
69  *   |  +- ssi[2]
70  *   |  ...
71  *   |
72  *   | ** these control src
73  *   |
74  *   +- src
75  *      |
76  *      +- src[0]
77  *      +- src[1]
78  *      +- src[2]
79  *      ...
80  *
81  *
82  * for_each_rsnd_dai(xx, priv, xx)
83  *  rdai[0] => rdai[1] => rdai[2] => ...
84  *
85  * for_each_rsnd_mod(xx, rdai, xx)
86  *  [mod] => [mod] => [mod] => ...
87  *
88  * rsnd_dai_call(xxx, fn )
89  *  [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
90  *
91  */
92 
93 /*
94  * you can enable below define if you don't need
95  * DAI status debug message when debugging
96  * see rsnd_dbg_dai_call()
97  *
98  * #define RSND_DEBUG_NO_DAI_CALL 1
99  */
100 
101 #include <linux/pm_runtime.h>
102 #include "rsnd.h"
103 
104 #define RSND_RATES SNDRV_PCM_RATE_8000_192000
105 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S8 |\
106 		   SNDRV_PCM_FMTBIT_S16_LE |\
107 		   SNDRV_PCM_FMTBIT_S24_LE)
108 
109 static const struct of_device_id rsnd_of_match[] = {
110 	{ .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
111 	{ .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
112 	{ .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN3 },
113 	/* Special Handling */
114 	{ .compatible = "renesas,rcar_sound-r8a77990", .data = (void *)(RSND_GEN3 | RSND_SOC_E) },
115 	{},
116 };
117 MODULE_DEVICE_TABLE(of, rsnd_of_match);
118 
119 /*
120  *	rsnd_mod functions
121  */
122 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
123 {
124 	if (mod->type != type) {
125 		struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
126 		struct device *dev = rsnd_priv_to_dev(priv);
127 
128 		dev_warn(dev, "%s is not your expected module\n",
129 			 rsnd_mod_name(mod));
130 	}
131 }
132 
133 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
134 				  struct rsnd_mod *mod)
135 {
136 	if (!mod || !mod->ops || !mod->ops->dma_req)
137 		return NULL;
138 
139 	return mod->ops->dma_req(io, mod);
140 }
141 
142 #define MOD_NAME_NUM   5
143 #define MOD_NAME_SIZE 16
144 char *rsnd_mod_name(struct rsnd_mod *mod)
145 {
146 	static char names[MOD_NAME_NUM][MOD_NAME_SIZE];
147 	static int num;
148 	char *name = names[num];
149 
150 	num++;
151 	if (num >= MOD_NAME_NUM)
152 		num = 0;
153 
154 	/*
155 	 * Let's use same char to avoid pointlessness memory
156 	 * Thus, rsnd_mod_name() should be used immediately
157 	 * Don't keep pointer
158 	 */
159 	if ((mod)->ops->id_sub) {
160 		snprintf(name, MOD_NAME_SIZE, "%s[%d%d]",
161 			 mod->ops->name,
162 			 rsnd_mod_id(mod),
163 			 rsnd_mod_id_sub(mod));
164 	} else {
165 		snprintf(name, MOD_NAME_SIZE, "%s[%d]",
166 			 mod->ops->name,
167 			 rsnd_mod_id(mod));
168 	}
169 
170 	return name;
171 }
172 
173 u32 *rsnd_mod_get_status(struct rsnd_mod *mod,
174 			 struct rsnd_dai_stream *io,
175 			 enum rsnd_mod_type type)
176 {
177 	return &mod->status;
178 }
179 
180 int rsnd_mod_id_raw(struct rsnd_mod *mod)
181 {
182 	return mod->id;
183 }
184 
185 int rsnd_mod_id(struct rsnd_mod *mod)
186 {
187 	if ((mod)->ops->id)
188 		return (mod)->ops->id(mod);
189 
190 	return rsnd_mod_id_raw(mod);
191 }
192 
193 int rsnd_mod_id_sub(struct rsnd_mod *mod)
194 {
195 	if ((mod)->ops->id_sub)
196 		return (mod)->ops->id_sub(mod);
197 
198 	return 0;
199 }
200 
201 int rsnd_mod_init(struct rsnd_priv *priv,
202 		  struct rsnd_mod *mod,
203 		  struct rsnd_mod_ops *ops,
204 		  struct clk *clk,
205 		  enum rsnd_mod_type type,
206 		  int id)
207 {
208 	int ret = clk_prepare(clk);
209 
210 	if (ret)
211 		return ret;
212 
213 	mod->id		= id;
214 	mod->ops	= ops;
215 	mod->type	= type;
216 	mod->clk	= clk;
217 	mod->priv	= priv;
218 
219 	return 0;
220 }
221 
222 void rsnd_mod_quit(struct rsnd_mod *mod)
223 {
224 	clk_unprepare(mod->clk);
225 	mod->clk = NULL;
226 }
227 
228 void rsnd_mod_interrupt(struct rsnd_mod *mod,
229 			void (*callback)(struct rsnd_mod *mod,
230 					 struct rsnd_dai_stream *io))
231 {
232 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
233 	struct rsnd_dai *rdai;
234 	int i;
235 
236 	for_each_rsnd_dai(rdai, priv, i) {
237 		struct rsnd_dai_stream *io = &rdai->playback;
238 
239 		if (mod == io->mod[mod->type])
240 			callback(mod, io);
241 
242 		io = &rdai->capture;
243 		if (mod == io->mod[mod->type])
244 			callback(mod, io);
245 	}
246 }
247 
248 int rsnd_io_is_working(struct rsnd_dai_stream *io)
249 {
250 	/* see rsnd_dai_stream_init/quit() */
251 	if (io->substream)
252 		return snd_pcm_running(io->substream);
253 
254 	return 0;
255 }
256 
257 int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
258 					      struct snd_pcm_hw_params *params)
259 {
260 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
261 
262 	/*
263 	 * params will be added when refine
264 	 * see
265 	 *	__rsnd_soc_hw_rule_rate()
266 	 *	__rsnd_soc_hw_rule_channels()
267 	 */
268 	if (params)
269 		return params_channels(params);
270 	else
271 		return runtime->channels;
272 }
273 
274 int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
275 					       struct snd_pcm_hw_params *params)
276 {
277 	int chan = rsnd_runtime_channel_original_with_params(io, params);
278 	struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
279 
280 	if (ctu_mod) {
281 		u32 converted_chan = rsnd_io_converted_chan(io);
282 
283 		/*
284 		 * !! Note !!
285 		 *
286 		 * converted_chan will be used for CTU,
287 		 * or TDM Split mode.
288 		 * User shouldn't use CTU with TDM Split mode.
289 		 */
290 		if (rsnd_runtime_is_tdm_split(io)) {
291 			struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));
292 
293 			dev_err(dev, "CTU and TDM Split should be used\n");
294 		}
295 
296 		if (converted_chan)
297 			return converted_chan;
298 	}
299 
300 	return chan;
301 }
302 
303 int rsnd_channel_normalization(int chan)
304 {
305 	if (WARN_ON((chan > 8) || (chan < 0)))
306 		return 0;
307 
308 	/* TDM Extend Mode needs 8ch */
309 	if (chan == 6)
310 		chan = 8;
311 
312 	return chan;
313 }
314 
315 int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
316 					     struct snd_pcm_hw_params *params)
317 {
318 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
319 	int chan = rsnd_io_is_play(io) ?
320 		rsnd_runtime_channel_after_ctu_with_params(io, params) :
321 		rsnd_runtime_channel_original_with_params(io, params);
322 
323 	/* Use Multi SSI */
324 	if (rsnd_runtime_is_multi_ssi(io))
325 		chan /= rsnd_rdai_ssi_lane_get(rdai);
326 
327 	return rsnd_channel_normalization(chan);
328 }
329 
330 int rsnd_runtime_is_multi_ssi(struct rsnd_dai_stream *io)
331 {
332 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
333 	int lane = rsnd_rdai_ssi_lane_get(rdai);
334 	int chan = rsnd_io_is_play(io) ?
335 		rsnd_runtime_channel_after_ctu(io) :
336 		rsnd_runtime_channel_original(io);
337 
338 	return (chan > 2) && (lane > 1);
339 }
340 
341 int rsnd_runtime_is_tdm(struct rsnd_dai_stream *io)
342 {
343 	return rsnd_runtime_channel_for_ssi(io) >= 6;
344 }
345 
346 int rsnd_runtime_is_tdm_split(struct rsnd_dai_stream *io)
347 {
348 	return !!rsnd_flags_has(io, RSND_STREAM_TDM_SPLIT);
349 }
350 
351 /*
352  *	ADINR function
353  */
354 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
355 {
356 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
357 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
358 	struct device *dev = rsnd_priv_to_dev(priv);
359 
360 	switch (snd_pcm_format_width(runtime->format)) {
361 	case 8:
362 		return 16 << 16;
363 	case 16:
364 		return 8 << 16;
365 	case 24:
366 		return 0 << 16;
367 	}
368 
369 	dev_warn(dev, "not supported sample bits\n");
370 
371 	return 0;
372 }
373 
374 /*
375  *	DALIGN function
376  */
377 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
378 {
379 	static const u32 dalign_values[8] = {
380 		0x76543210, 0x00000032, 0x00007654, 0x00000076,
381 		0xfedcba98, 0x000000ba, 0x0000fedc, 0x000000fe,
382 	};
383 	int id = 0;
384 	struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
385 	struct rsnd_mod *target;
386 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
387 	u32 dalign;
388 
389 	/*
390 	 * *Hardware* L/R and *Software* L/R are inverted for 16bit data.
391 	 *	    31..16 15...0
392 	 *	HW: [L ch] [R ch]
393 	 *	SW: [R ch] [L ch]
394 	 * We need to care about inversion timing to control
395 	 * Playback/Capture correctly.
396 	 * The point is [DVC] needs *Hardware* L/R, [MEM] needs *Software* L/R
397 	 *
398 	 * sL/R : software L/R
399 	 * hL/R : hardware L/R
400 	 * (*)  : conversion timing
401 	 *
402 	 * Playback
403 	 *	     sL/R (*) hL/R     hL/R     hL/R      hL/R     hL/R
404 	 *	[MEM] -> [SRC] -> [DVC] -> [CMD] -> [SSIU] -> [SSI] -> codec
405 	 *
406 	 * Capture
407 	 *	     hL/R     hL/R      hL/R     hL/R     hL/R (*) sL/R
408 	 *	codec -> [SSI] -> [SSIU] -> [SRC] -> [DVC] -> [CMD] -> [MEM]
409 	 */
410 	if (rsnd_io_is_play(io)) {
411 		struct rsnd_mod *src = rsnd_io_to_mod_src(io);
412 
413 		target = src ? src : ssiu;
414 	} else {
415 		struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
416 
417 		target = cmd ? cmd : ssiu;
418 	}
419 
420 	if (mod == ssiu)
421 		id = rsnd_mod_id_sub(mod);
422 
423 	dalign = dalign_values[id];
424 
425 	if (mod == target && snd_pcm_format_width(runtime->format) == 16) {
426 		/* Target mod needs inverted DALIGN when 16bit */
427 		dalign = (dalign & 0xf0f0f0f0) >> 4 |
428 			 (dalign & 0x0f0f0f0f) << 4;
429 	}
430 
431 	return dalign;
432 }
433 
434 u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
435 {
436 	enum rsnd_mod_type playback_mods[] = {
437 		RSND_MOD_SRC,
438 		RSND_MOD_CMD,
439 		RSND_MOD_SSIU,
440 	};
441 	enum rsnd_mod_type capture_mods[] = {
442 		RSND_MOD_CMD,
443 		RSND_MOD_SRC,
444 		RSND_MOD_SSIU,
445 	};
446 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
447 	struct rsnd_mod *tmod = NULL;
448 	enum rsnd_mod_type *mods =
449 		rsnd_io_is_play(io) ?
450 		playback_mods : capture_mods;
451 	int i;
452 
453 	/*
454 	 * This is needed for 24bit data
455 	 * We need to shift 8bit
456 	 *
457 	 * Linux 24bit data is located as 0x00******
458 	 * HW    24bit data is located as 0x******00
459 	 *
460 	 */
461 	if (snd_pcm_format_width(runtime->format) != 24)
462 		return 0;
463 
464 	for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
465 		tmod = rsnd_io_to_mod(io, mods[i]);
466 		if (tmod)
467 			break;
468 	}
469 
470 	if (tmod != mod)
471 		return 0;
472 
473 	if (rsnd_io_is_play(io))
474 		return  (0 << 20) | /* shift to Left */
475 			(8 << 16);  /* 8bit */
476 	else
477 		return  (1 << 20) | /* shift to Right */
478 			(8 << 16);  /* 8bit */
479 }
480 
481 /*
482  *	rsnd_dai functions
483  */
484 struct rsnd_mod *rsnd_mod_next(int *iterator,
485 			       struct rsnd_dai_stream *io,
486 			       enum rsnd_mod_type *array,
487 			       int array_size)
488 {
489 	int max = array ? array_size : RSND_MOD_MAX;
490 
491 	for (; *iterator < max; (*iterator)++) {
492 		enum rsnd_mod_type type = (array) ? array[*iterator] : *iterator;
493 		struct rsnd_mod *mod = rsnd_io_to_mod(io, type);
494 
495 		if (mod)
496 			return mod;
497 	}
498 
499 	return NULL;
500 }
501 
502 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
503 	{
504 		/* CAPTURE */
505 		RSND_MOD_AUDMAPP,
506 		RSND_MOD_AUDMA,
507 		RSND_MOD_DVC,
508 		RSND_MOD_MIX,
509 		RSND_MOD_CTU,
510 		RSND_MOD_CMD,
511 		RSND_MOD_SRC,
512 		RSND_MOD_SSIU,
513 		RSND_MOD_SSIM3,
514 		RSND_MOD_SSIM2,
515 		RSND_MOD_SSIM1,
516 		RSND_MOD_SSIP,
517 		RSND_MOD_SSI,
518 	}, {
519 		/* PLAYBACK */
520 		RSND_MOD_AUDMAPP,
521 		RSND_MOD_AUDMA,
522 		RSND_MOD_SSIM3,
523 		RSND_MOD_SSIM2,
524 		RSND_MOD_SSIM1,
525 		RSND_MOD_SSIP,
526 		RSND_MOD_SSI,
527 		RSND_MOD_SSIU,
528 		RSND_MOD_DVC,
529 		RSND_MOD_MIX,
530 		RSND_MOD_CTU,
531 		RSND_MOD_CMD,
532 		RSND_MOD_SRC,
533 	},
534 };
535 
536 static int rsnd_status_update(u32 *status,
537 			      int shift, int add, int timing)
538 {
539 	u32 mask	= 0xF << shift;
540 	u8 val		= (*status >> shift) & 0xF;
541 	u8 next_val	= (val + add) & 0xF;
542 	int func_call	= (val == timing);
543 
544 	if (next_val == 0xF) /* underflow case */
545 		func_call = 0;
546 	else
547 		*status = (*status & ~mask) + (next_val << shift);
548 
549 	return func_call;
550 }
551 
552 #define rsnd_dai_call(fn, io, param...)					\
553 ({									\
554 	struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));	\
555 	struct rsnd_mod *mod;						\
556 	int is_play = rsnd_io_is_play(io);				\
557 	int ret = 0, i;							\
558 	enum rsnd_mod_type *types = rsnd_mod_sequence[is_play];		\
559 	for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) {	\
560 		int tmp = 0;						\
561 		u32 *status = mod->ops->get_status(mod, io, types[i]);	\
562 		int func_call = rsnd_status_update(status,		\
563 						__rsnd_mod_shift_##fn,	\
564 						__rsnd_mod_add_##fn,	\
565 						__rsnd_mod_call_##fn);	\
566 		rsnd_dbg_dai_call(dev, "%s\t0x%08x %s\n",		\
567 			rsnd_mod_name(mod), *status,	\
568 			(func_call && (mod)->ops->fn) ? #fn : "");	\
569 		if (func_call && (mod)->ops->fn)			\
570 			tmp = (mod)->ops->fn(mod, io, param);		\
571 		if (tmp && (tmp != -EPROBE_DEFER))			\
572 			dev_err(dev, "%s : %s error %d\n",		\
573 				rsnd_mod_name(mod), #fn, tmp);		\
574 		ret |= tmp;						\
575 	}								\
576 	ret;								\
577 })
578 
579 int rsnd_dai_connect(struct rsnd_mod *mod,
580 		     struct rsnd_dai_stream *io,
581 		     enum rsnd_mod_type type)
582 {
583 	struct rsnd_priv *priv;
584 	struct device *dev;
585 
586 	if (!mod)
587 		return -EIO;
588 
589 	if (io->mod[type] == mod)
590 		return 0;
591 
592 	if (io->mod[type])
593 		return -EINVAL;
594 
595 	priv = rsnd_mod_to_priv(mod);
596 	dev = rsnd_priv_to_dev(priv);
597 
598 	io->mod[type] = mod;
599 
600 	dev_dbg(dev, "%s is connected to io (%s)\n",
601 		rsnd_mod_name(mod),
602 		rsnd_io_is_play(io) ? "Playback" : "Capture");
603 
604 	return 0;
605 }
606 
607 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
608 				struct rsnd_dai_stream *io,
609 				enum rsnd_mod_type type)
610 {
611 	io->mod[type] = NULL;
612 }
613 
614 int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
615 			    int max_channels)
616 {
617 	if (max_channels > 0)
618 		rdai->max_channels = max_channels;
619 
620 	return rdai->max_channels;
621 }
622 
623 int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
624 			    int ssi_lane)
625 {
626 	if (ssi_lane > 0)
627 		rdai->ssi_lane = ssi_lane;
628 
629 	return rdai->ssi_lane;
630 }
631 
632 int rsnd_rdai_width_ctrl(struct rsnd_dai *rdai, int width)
633 {
634 	if (width > 0)
635 		rdai->chan_width = width;
636 
637 	return rdai->chan_width;
638 }
639 
640 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
641 {
642 	if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
643 		return NULL;
644 
645 	return priv->rdai + id;
646 }
647 
648 static struct snd_soc_dai_driver
649 *rsnd_daidrv_get(struct rsnd_priv *priv, int id)
650 {
651 	if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
652 		return NULL;
653 
654 	return priv->daidrv + id;
655 }
656 
657 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
658 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
659 {
660 	struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
661 
662 	return rsnd_rdai_get(priv, dai->id);
663 }
664 
665 /*
666  *	rsnd_soc_dai functions
667  */
668 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
669 {
670 	struct snd_pcm_substream *substream = io->substream;
671 
672 	/*
673 	 * this function should be called...
674 	 *
675 	 * - if rsnd_dai_pointer_update() returns true
676 	 * - without spin lock
677 	 */
678 
679 	snd_pcm_period_elapsed(substream);
680 }
681 
682 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
683 				struct snd_pcm_substream *substream)
684 {
685 	io->substream		= substream;
686 }
687 
688 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
689 {
690 	io->substream		= NULL;
691 }
692 
693 static
694 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
695 {
696 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
697 
698 	return  asoc_rtd_to_cpu(rtd, 0);
699 }
700 
701 static
702 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
703 					struct snd_pcm_substream *substream)
704 {
705 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
706 		return &rdai->playback;
707 	else
708 		return &rdai->capture;
709 }
710 
711 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
712 			    struct snd_soc_dai *dai)
713 {
714 	struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
715 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
716 	struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
717 	int ret;
718 	unsigned long flags;
719 
720 	spin_lock_irqsave(&priv->lock, flags);
721 
722 	switch (cmd) {
723 	case SNDRV_PCM_TRIGGER_START:
724 	case SNDRV_PCM_TRIGGER_RESUME:
725 		ret = rsnd_dai_call(init, io, priv);
726 		if (ret < 0)
727 			goto dai_trigger_end;
728 
729 		ret = rsnd_dai_call(start, io, priv);
730 		if (ret < 0)
731 			goto dai_trigger_end;
732 
733 		ret = rsnd_dai_call(irq, io, priv, 1);
734 		if (ret < 0)
735 			goto dai_trigger_end;
736 
737 		break;
738 	case SNDRV_PCM_TRIGGER_STOP:
739 	case SNDRV_PCM_TRIGGER_SUSPEND:
740 		ret = rsnd_dai_call(irq, io, priv, 0);
741 
742 		ret |= rsnd_dai_call(stop, io, priv);
743 
744 		ret |= rsnd_dai_call(quit, io, priv);
745 
746 		break;
747 	default:
748 		ret = -EINVAL;
749 	}
750 
751 dai_trigger_end:
752 	spin_unlock_irqrestore(&priv->lock, flags);
753 
754 	return ret;
755 }
756 
757 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
758 {
759 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
760 
761 	/* set clock master for audio interface */
762 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
763 	case SND_SOC_DAIFMT_CBM_CFM:
764 		rdai->clk_master = 0;
765 		break;
766 	case SND_SOC_DAIFMT_CBS_CFS:
767 		rdai->clk_master = 1; /* cpu is master */
768 		break;
769 	default:
770 		return -EINVAL;
771 	}
772 
773 	/* set format */
774 	rdai->bit_clk_inv = 0;
775 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
776 	case SND_SOC_DAIFMT_I2S:
777 		rdai->sys_delay = 0;
778 		rdai->data_alignment = 0;
779 		rdai->frm_clk_inv = 0;
780 		break;
781 	case SND_SOC_DAIFMT_LEFT_J:
782 	case SND_SOC_DAIFMT_DSP_B:
783 		rdai->sys_delay = 1;
784 		rdai->data_alignment = 0;
785 		rdai->frm_clk_inv = 1;
786 		break;
787 	case SND_SOC_DAIFMT_RIGHT_J:
788 		rdai->sys_delay = 1;
789 		rdai->data_alignment = 1;
790 		rdai->frm_clk_inv = 1;
791 		break;
792 	case SND_SOC_DAIFMT_DSP_A:
793 		rdai->sys_delay = 0;
794 		rdai->data_alignment = 0;
795 		rdai->frm_clk_inv = 1;
796 		break;
797 	}
798 
799 	/* set clock inversion */
800 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
801 	case SND_SOC_DAIFMT_NB_IF:
802 		rdai->frm_clk_inv = !rdai->frm_clk_inv;
803 		break;
804 	case SND_SOC_DAIFMT_IB_NF:
805 		rdai->bit_clk_inv = !rdai->bit_clk_inv;
806 		break;
807 	case SND_SOC_DAIFMT_IB_IF:
808 		rdai->bit_clk_inv = !rdai->bit_clk_inv;
809 		rdai->frm_clk_inv = !rdai->frm_clk_inv;
810 		break;
811 	case SND_SOC_DAIFMT_NB_NF:
812 	default:
813 		break;
814 	}
815 
816 	return 0;
817 }
818 
819 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
820 				     u32 tx_mask, u32 rx_mask,
821 				     int slots, int slot_width)
822 {
823 	struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
824 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
825 	struct device *dev = rsnd_priv_to_dev(priv);
826 
827 	switch (slot_width) {
828 	case 16:
829 	case 24:
830 	case 32:
831 		break;
832 	default:
833 		/* use default */
834 		slot_width = 32;
835 	}
836 
837 	switch (slots) {
838 	case 2:
839 		/* TDM Split Mode */
840 	case 6:
841 	case 8:
842 		/* TDM Extend Mode */
843 		rsnd_rdai_channels_set(rdai, slots);
844 		rsnd_rdai_ssi_lane_set(rdai, 1);
845 		rsnd_rdai_width_set(rdai, slot_width);
846 		break;
847 	default:
848 		dev_err(dev, "unsupported TDM slots (%d)\n", slots);
849 		return -EINVAL;
850 	}
851 
852 	return 0;
853 }
854 
855 static unsigned int rsnd_soc_hw_channels_list[] = {
856 	2, 6, 8,
857 };
858 
859 static unsigned int rsnd_soc_hw_rate_list[] = {
860 	  8000,
861 	 11025,
862 	 16000,
863 	 22050,
864 	 32000,
865 	 44100,
866 	 48000,
867 	 64000,
868 	 88200,
869 	 96000,
870 	176400,
871 	192000,
872 };
873 
874 static int rsnd_soc_hw_rule(struct rsnd_dai *rdai,
875 			    unsigned int *list, int list_num,
876 			    struct snd_interval *baseline, struct snd_interval *iv)
877 {
878 	struct snd_interval p;
879 	unsigned int rate;
880 	int i;
881 
882 	snd_interval_any(&p);
883 	p.min = UINT_MAX;
884 	p.max = 0;
885 
886 	for (i = 0; i < list_num; i++) {
887 
888 		if (!snd_interval_test(iv, list[i]))
889 			continue;
890 
891 		rate = rsnd_ssi_clk_query(rdai,
892 					  baseline->min, list[i], NULL);
893 		if (rate > 0) {
894 			p.min = min(p.min, list[i]);
895 			p.max = max(p.max, list[i]);
896 		}
897 
898 		rate = rsnd_ssi_clk_query(rdai,
899 					  baseline->max, list[i], NULL);
900 		if (rate > 0) {
901 			p.min = min(p.min, list[i]);
902 			p.max = max(p.max, list[i]);
903 		}
904 	}
905 
906 	return snd_interval_refine(iv, &p);
907 }
908 
909 static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
910 				 struct snd_pcm_hw_rule *rule)
911 {
912 	struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
913 	struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
914 	struct snd_interval ic;
915 	struct rsnd_dai_stream *io = rule->private;
916 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
917 
918 	/*
919 	 * possible sampling rate limitation is same as
920 	 * 2ch if it supports multi ssi
921 	 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
922 	 */
923 	ic = *ic_;
924 	ic.min =
925 	ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
926 
927 	return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_rate_list,
928 				ARRAY_SIZE(rsnd_soc_hw_rate_list),
929 				&ic, ir);
930 }
931 
932 static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
933 				     struct snd_pcm_hw_rule *rule)
934 {
935 	struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
936 	struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
937 	struct snd_interval ic;
938 	struct rsnd_dai_stream *io = rule->private;
939 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
940 
941 	/*
942 	 * possible sampling rate limitation is same as
943 	 * 2ch if it supports multi ssi
944 	 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
945 	 */
946 	ic = *ic_;
947 	ic.min =
948 	ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
949 
950 	return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_channels_list,
951 				ARRAY_SIZE(rsnd_soc_hw_channels_list),
952 				ir, &ic);
953 }
954 
955 static const struct snd_pcm_hardware rsnd_pcm_hardware = {
956 	.info =		SNDRV_PCM_INFO_INTERLEAVED	|
957 			SNDRV_PCM_INFO_MMAP		|
958 			SNDRV_PCM_INFO_MMAP_VALID,
959 	.buffer_bytes_max	= 64 * 1024,
960 	.period_bytes_min	= 32,
961 	.period_bytes_max	= 8192,
962 	.periods_min		= 1,
963 	.periods_max		= 32,
964 	.fifo_size		= 256,
965 };
966 
967 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
968 				struct snd_soc_dai *dai)
969 {
970 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
971 	struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
972 	struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
973 	struct snd_pcm_runtime *runtime = substream->runtime;
974 	unsigned int max_channels = rsnd_rdai_channels_get(rdai);
975 	int i;
976 
977 	rsnd_dai_stream_init(io, substream);
978 
979 	/*
980 	 * Channel Limitation
981 	 * It depends on Platform design
982 	 */
983 	constraint->list	= rsnd_soc_hw_channels_list;
984 	constraint->count	= 0;
985 	constraint->mask	= 0;
986 
987 	for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
988 		if (rsnd_soc_hw_channels_list[i] > max_channels)
989 			break;
990 		constraint->count = i + 1;
991 	}
992 
993 	snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
994 
995 	snd_pcm_hw_constraint_list(runtime, 0,
996 				   SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
997 
998 	snd_pcm_hw_constraint_integer(runtime,
999 				      SNDRV_PCM_HW_PARAM_PERIODS);
1000 
1001 	/*
1002 	 * Sampling Rate / Channel Limitation
1003 	 * It depends on Clock Master Mode
1004 	 */
1005 	if (rsnd_rdai_is_clk_master(rdai)) {
1006 		int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1007 
1008 		snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1009 				    rsnd_soc_hw_rule_rate,
1010 				    is_play ? &rdai->playback : &rdai->capture,
1011 				    SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1012 		snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1013 				    rsnd_soc_hw_rule_channels,
1014 				    is_play ? &rdai->playback : &rdai->capture,
1015 				    SNDRV_PCM_HW_PARAM_RATE, -1);
1016 	}
1017 
1018 	return 0;
1019 }
1020 
1021 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
1022 				  struct snd_soc_dai *dai)
1023 {
1024 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1025 	struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1026 	struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1027 
1028 	/*
1029 	 * call rsnd_dai_call without spinlock
1030 	 */
1031 	rsnd_dai_call(cleanup, io, priv);
1032 
1033 	rsnd_dai_stream_quit(io);
1034 }
1035 
1036 static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream,
1037 				struct snd_soc_dai *dai)
1038 {
1039 	struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
1040 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1041 	struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1042 
1043 	return rsnd_dai_call(prepare, io, priv);
1044 }
1045 
1046 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
1047 	.startup	= rsnd_soc_dai_startup,
1048 	.shutdown	= rsnd_soc_dai_shutdown,
1049 	.trigger	= rsnd_soc_dai_trigger,
1050 	.set_fmt	= rsnd_soc_dai_set_fmt,
1051 	.set_tdm_slot	= rsnd_soc_set_dai_tdm_slot,
1052 	.prepare	= rsnd_soc_dai_prepare,
1053 };
1054 
1055 static void rsnd_parse_tdm_split_mode(struct rsnd_priv *priv,
1056 				      struct rsnd_dai_stream *io,
1057 				      struct device_node *dai_np)
1058 {
1059 	struct device *dev = rsnd_priv_to_dev(priv);
1060 	struct device_node *ssiu_np = rsnd_ssiu_of_node(priv);
1061 	struct device_node *np;
1062 	int is_play = rsnd_io_is_play(io);
1063 	int i;
1064 
1065 	if (!ssiu_np)
1066 		return;
1067 
1068 	/*
1069 	 * This driver assumes that it is TDM Split mode
1070 	 * if it includes ssiu node
1071 	 */
1072 	for (i = 0;; i++) {
1073 		struct device_node *node = is_play ?
1074 			of_parse_phandle(dai_np, "playback", i) :
1075 			of_parse_phandle(dai_np, "capture",  i);
1076 
1077 		if (!node)
1078 			break;
1079 
1080 		for_each_child_of_node(ssiu_np, np) {
1081 			if (np == node) {
1082 				rsnd_flags_set(io, RSND_STREAM_TDM_SPLIT);
1083 				dev_dbg(dev, "%s is part of TDM Split\n", io->name);
1084 			}
1085 		}
1086 
1087 		of_node_put(node);
1088 	}
1089 
1090 	of_node_put(ssiu_np);
1091 }
1092 
1093 static void rsnd_parse_connect_simple(struct rsnd_priv *priv,
1094 				      struct rsnd_dai_stream *io,
1095 				      struct device_node *dai_np)
1096 {
1097 	if (!rsnd_io_to_mod_ssi(io))
1098 		return;
1099 
1100 	rsnd_parse_tdm_split_mode(priv, io, dai_np);
1101 }
1102 
1103 static void rsnd_parse_connect_graph(struct rsnd_priv *priv,
1104 				     struct rsnd_dai_stream *io,
1105 				     struct device_node *endpoint)
1106 {
1107 	struct device *dev = rsnd_priv_to_dev(priv);
1108 	struct device_node *remote_node;
1109 
1110 	if (!rsnd_io_to_mod_ssi(io))
1111 		return;
1112 
1113 	remote_node = of_graph_get_remote_port_parent(endpoint);
1114 
1115 	/* HDMI0 */
1116 	if (strstr(remote_node->full_name, "hdmi@fead0000")) {
1117 		rsnd_flags_set(io, RSND_STREAM_HDMI0);
1118 		dev_dbg(dev, "%s connected to HDMI0\n", io->name);
1119 	}
1120 
1121 	/* HDMI1 */
1122 	if (strstr(remote_node->full_name, "hdmi@feae0000")) {
1123 		rsnd_flags_set(io, RSND_STREAM_HDMI1);
1124 		dev_dbg(dev, "%s connected to HDMI1\n", io->name);
1125 	}
1126 
1127 	rsnd_parse_tdm_split_mode(priv, io, endpoint);
1128 
1129 	of_node_put(remote_node);
1130 }
1131 
1132 void rsnd_parse_connect_common(struct rsnd_dai *rdai,
1133 		struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
1134 		struct device_node *node,
1135 		struct device_node *playback,
1136 		struct device_node *capture)
1137 {
1138 	struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1139 	struct device_node *np;
1140 	int i;
1141 
1142 	if (!node)
1143 		return;
1144 
1145 	i = 0;
1146 	for_each_child_of_node(node, np) {
1147 		struct rsnd_mod *mod = mod_get(priv, i);
1148 
1149 		if (np == playback)
1150 			rsnd_dai_connect(mod, &rdai->playback, mod->type);
1151 		if (np == capture)
1152 			rsnd_dai_connect(mod, &rdai->capture, mod->type);
1153 		i++;
1154 	}
1155 
1156 	of_node_put(node);
1157 }
1158 
1159 static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
1160 					    int *is_graph)
1161 {
1162 	struct device *dev = rsnd_priv_to_dev(priv);
1163 	struct device_node *np = dev->of_node;
1164 	struct device_node *dai_node;
1165 	struct device_node *ret;
1166 
1167 	*is_graph = 0;
1168 
1169 	/*
1170 	 * parse both previous dai (= rcar_sound,dai), and
1171 	 * graph dai (= ports/port)
1172 	 */
1173 	dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
1174 	if (dai_node) {
1175 		ret = dai_node;
1176 		goto of_node_compatible;
1177 	}
1178 
1179 	ret = np;
1180 
1181 	dai_node = of_graph_get_next_endpoint(np, NULL);
1182 	if (dai_node)
1183 		goto of_node_graph;
1184 
1185 	return NULL;
1186 
1187 of_node_graph:
1188 	*is_graph = 1;
1189 of_node_compatible:
1190 	of_node_put(dai_node);
1191 
1192 	return ret;
1193 }
1194 
1195 
1196 #define PREALLOC_BUFFER		(32 * 1024)
1197 #define PREALLOC_BUFFER_MAX	(32 * 1024)
1198 
1199 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1200 				  struct rsnd_dai_stream *io,
1201 				  int stream)
1202 {
1203 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
1204 	struct device *dev = rsnd_priv_to_dev(priv);
1205 	struct snd_pcm_substream *substream;
1206 
1207 	/*
1208 	 * use Audio-DMAC dev if we can use IPMMU
1209 	 * see
1210 	 *	rsnd_dmaen_attach()
1211 	 */
1212 	if (io->dmac_dev)
1213 		dev = io->dmac_dev;
1214 
1215 	for (substream = rtd->pcm->streams[stream].substream;
1216 	     substream;
1217 	     substream = substream->next) {
1218 		snd_pcm_set_managed_buffer(substream,
1219 					   SNDRV_DMA_TYPE_DEV,
1220 					   dev,
1221 					   PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1222 	}
1223 
1224 	return 0;
1225 }
1226 
1227 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd,
1228 			struct snd_soc_dai *dai)
1229 {
1230 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1231 	int ret;
1232 
1233 	ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1234 	if (ret)
1235 		return ret;
1236 
1237 	ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1238 	if (ret)
1239 		return ret;
1240 
1241 	ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1242 				     SNDRV_PCM_STREAM_PLAYBACK);
1243 	if (ret)
1244 		return ret;
1245 
1246 	ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1247 				     SNDRV_PCM_STREAM_CAPTURE);
1248 	if (ret)
1249 		return ret;
1250 
1251 	return 0;
1252 }
1253 
1254 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1255 			     struct device_node *dai_np,
1256 			     int dai_i)
1257 {
1258 	struct rsnd_dai_stream *io_playback;
1259 	struct rsnd_dai_stream *io_capture;
1260 	struct snd_soc_dai_driver *drv;
1261 	struct rsnd_dai *rdai;
1262 	struct device *dev = rsnd_priv_to_dev(priv);
1263 	int io_i;
1264 
1265 	rdai		= rsnd_rdai_get(priv, dai_i);
1266 	drv		= rsnd_daidrv_get(priv, dai_i);
1267 	io_playback	= &rdai->playback;
1268 	io_capture	= &rdai->capture;
1269 
1270 	snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1271 
1272 	rdai->priv	= priv;
1273 	drv->name	= rdai->name;
1274 	drv->ops	= &rsnd_soc_dai_ops;
1275 	drv->pcm_new	= rsnd_pcm_new;
1276 
1277 	snprintf(io_playback->name, RSND_DAI_NAME_SIZE,
1278 		 "DAI%d Playback", dai_i);
1279 	drv->playback.rates		= RSND_RATES;
1280 	drv->playback.formats		= RSND_FMTS;
1281 	drv->playback.channels_min	= 2;
1282 	drv->playback.channels_max	= 8;
1283 	drv->playback.stream_name	= io_playback->name;
1284 
1285 	snprintf(io_capture->name, RSND_DAI_NAME_SIZE,
1286 		 "DAI%d Capture", dai_i);
1287 	drv->capture.rates		= RSND_RATES;
1288 	drv->capture.formats		= RSND_FMTS;
1289 	drv->capture.channels_min	= 2;
1290 	drv->capture.channels_max	= 8;
1291 	drv->capture.stream_name	= io_capture->name;
1292 
1293 	io_playback->rdai		= rdai;
1294 	io_capture->rdai		= rdai;
1295 	rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1296 	rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1297 	rsnd_rdai_width_set(rdai, 32);   /* default 32bit width */
1298 
1299 	for (io_i = 0;; io_i++) {
1300 		struct device_node *playback = of_parse_phandle(dai_np, "playback", io_i);
1301 		struct device_node *capture  = of_parse_phandle(dai_np, "capture", io_i);
1302 
1303 		if (!playback && !capture)
1304 			break;
1305 
1306 		rsnd_parse_connect_ssi(rdai, playback, capture);
1307 		rsnd_parse_connect_ssiu(rdai, playback, capture);
1308 		rsnd_parse_connect_src(rdai, playback, capture);
1309 		rsnd_parse_connect_ctu(rdai, playback, capture);
1310 		rsnd_parse_connect_mix(rdai, playback, capture);
1311 		rsnd_parse_connect_dvc(rdai, playback, capture);
1312 
1313 		of_node_put(playback);
1314 		of_node_put(capture);
1315 	}
1316 
1317 	if (rsnd_ssi_is_pin_sharing(io_capture) ||
1318 	    rsnd_ssi_is_pin_sharing(io_playback)) {
1319 		/* should have symmetric_rate if pin sharing */
1320 		drv->symmetric_rate = 1;
1321 	}
1322 
1323 	dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1324 		rsnd_io_to_mod_ssi(io_playback) ? "play"    : " -- ",
1325 		rsnd_io_to_mod_ssi(io_capture) ? "capture" : "  --   ");
1326 }
1327 
1328 static int rsnd_dai_probe(struct rsnd_priv *priv)
1329 {
1330 	struct device_node *dai_node;
1331 	struct device_node *dai_np;
1332 	struct snd_soc_dai_driver *rdrv;
1333 	struct device *dev = rsnd_priv_to_dev(priv);
1334 	struct rsnd_dai *rdai;
1335 	int nr;
1336 	int is_graph;
1337 	int dai_i;
1338 
1339 	dai_node = rsnd_dai_of_node(priv, &is_graph);
1340 	if (is_graph)
1341 		nr = of_graph_get_endpoint_count(dai_node);
1342 	else
1343 		nr = of_get_child_count(dai_node);
1344 
1345 	if (!nr)
1346 		return -EINVAL;
1347 
1348 	rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1349 	rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1350 	if (!rdrv || !rdai)
1351 		return -ENOMEM;
1352 
1353 	priv->rdai_nr	= nr;
1354 	priv->daidrv	= rdrv;
1355 	priv->rdai	= rdai;
1356 
1357 	/*
1358 	 * parse all dai
1359 	 */
1360 	dai_i = 0;
1361 	if (is_graph) {
1362 		for_each_endpoint_of_node(dai_node, dai_np) {
1363 			__rsnd_dai_probe(priv, dai_np, dai_i);
1364 			if (rsnd_is_gen3(priv)) {
1365 				rdai = rsnd_rdai_get(priv, dai_i);
1366 
1367 				rsnd_parse_connect_graph(priv, &rdai->playback, dai_np);
1368 				rsnd_parse_connect_graph(priv, &rdai->capture,  dai_np);
1369 			}
1370 			dai_i++;
1371 		}
1372 	} else {
1373 		for_each_child_of_node(dai_node, dai_np) {
1374 			__rsnd_dai_probe(priv, dai_np, dai_i);
1375 			if (rsnd_is_gen3(priv)) {
1376 				rdai = rsnd_rdai_get(priv, dai_i);
1377 
1378 				rsnd_parse_connect_simple(priv, &rdai->playback, dai_np);
1379 				rsnd_parse_connect_simple(priv, &rdai->capture,  dai_np);
1380 			}
1381 			dai_i++;
1382 		}
1383 	}
1384 
1385 	return 0;
1386 }
1387 
1388 /*
1389  *		pcm ops
1390  */
1391 static int rsnd_hw_params(struct snd_soc_component *component,
1392 			  struct snd_pcm_substream *substream,
1393 			  struct snd_pcm_hw_params *hw_params)
1394 {
1395 	struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1396 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1397 	struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1398 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1399 
1400 	/*
1401 	 * rsnd assumes that it might be used under DPCM if user want to use
1402 	 * channel / rate convert. Then, rsnd should be FE.
1403 	 * And then, this function will be called *after* BE settings.
1404 	 * this means, each BE already has fixuped hw_params.
1405 	 * see
1406 	 *	dpcm_fe_dai_hw_params()
1407 	 *	dpcm_be_dai_hw_params()
1408 	 */
1409 	io->converted_rate = 0;
1410 	io->converted_chan = 0;
1411 	if (fe->dai_link->dynamic) {
1412 		struct rsnd_priv *priv = rsnd_io_to_priv(io);
1413 		struct device *dev = rsnd_priv_to_dev(priv);
1414 		struct snd_soc_dpcm *dpcm;
1415 		int stream = substream->stream;
1416 
1417 		for_each_dpcm_be(fe, stream, dpcm) {
1418 			struct snd_pcm_hw_params *be_params = &dpcm->hw_params;
1419 
1420 			if (params_channels(hw_params) != params_channels(be_params))
1421 				io->converted_chan = params_channels(be_params);
1422 			if (params_rate(hw_params) != params_rate(be_params))
1423 				io->converted_rate = params_rate(be_params);
1424 		}
1425 		if (io->converted_chan)
1426 			dev_dbg(dev, "convert channels = %d\n", io->converted_chan);
1427 		if (io->converted_rate) {
1428 			/*
1429 			 * SRC supports convert rates from params_rate(hw_params)/k_down
1430 			 * to params_rate(hw_params)*k_up, where k_up is always 6, and
1431 			 * k_down depends on number of channels and SRC unit.
1432 			 * So all SRC units can upsample audio up to 6 times regardless
1433 			 * its number of channels. And all SRC units can downsample
1434 			 * 2 channel audio up to 6 times too.
1435 			 */
1436 			int k_up = 6;
1437 			int k_down = 6;
1438 			int channel;
1439 			struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
1440 
1441 			dev_dbg(dev, "convert rate     = %d\n", io->converted_rate);
1442 
1443 			channel = io->converted_chan ? io->converted_chan :
1444 				  params_channels(hw_params);
1445 
1446 			switch (rsnd_mod_id(src_mod)) {
1447 			/*
1448 			 * SRC0 can downsample 4, 6 and 8 channel audio up to 4 times.
1449 			 * SRC1, SRC3 and SRC4 can downsample 4 channel audio
1450 			 * up to 4 times.
1451 			 * SRC1, SRC3 and SRC4 can downsample 6 and 8 channel audio
1452 			 * no more than twice.
1453 			 */
1454 			case 1:
1455 			case 3:
1456 			case 4:
1457 				if (channel > 4) {
1458 					k_down = 2;
1459 					break;
1460 				}
1461 				fallthrough;
1462 			case 0:
1463 				if (channel > 2)
1464 					k_down = 4;
1465 				break;
1466 
1467 			/* Other SRC units do not support more than 2 channels */
1468 			default:
1469 				if (channel > 2)
1470 					return -EINVAL;
1471 			}
1472 
1473 			if (params_rate(hw_params) > io->converted_rate * k_down) {
1474 				hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1475 					io->converted_rate * k_down;
1476 				hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1477 					io->converted_rate * k_down;
1478 				hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1479 			} else if (params_rate(hw_params) * k_up < io->converted_rate) {
1480 				hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1481 					(io->converted_rate + k_up - 1) / k_up;
1482 				hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1483 					(io->converted_rate + k_up - 1) / k_up;
1484 				hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1485 			}
1486 
1487 			/*
1488 			 * TBD: Max SRC input and output rates also depend on number
1489 			 * of channels and SRC unit:
1490 			 * SRC1, SRC3 and SRC4 do not support more than 128kHz
1491 			 * for 6 channel and 96kHz for 8 channel audio.
1492 			 * Perhaps this function should return EINVAL if the input or
1493 			 * the output rate exceeds the limitation.
1494 			 */
1495 		}
1496 	}
1497 
1498 	return rsnd_dai_call(hw_params, io, substream, hw_params);
1499 }
1500 
1501 static int rsnd_hw_free(struct snd_soc_component *component,
1502 			struct snd_pcm_substream *substream)
1503 {
1504 	struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1505 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1506 	struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1507 
1508 	return rsnd_dai_call(hw_free, io, substream);
1509 }
1510 
1511 static snd_pcm_uframes_t rsnd_pointer(struct snd_soc_component *component,
1512 				      struct snd_pcm_substream *substream)
1513 {
1514 	struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1515 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1516 	struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1517 	snd_pcm_uframes_t pointer = 0;
1518 
1519 	rsnd_dai_call(pointer, io, &pointer);
1520 
1521 	return pointer;
1522 }
1523 
1524 /*
1525  *		snd_kcontrol
1526  */
1527 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1528 			   struct snd_ctl_elem_info *uinfo)
1529 {
1530 	struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1531 
1532 	if (cfg->texts) {
1533 		uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1534 		uinfo->count = cfg->size;
1535 		uinfo->value.enumerated.items = cfg->max;
1536 		if (uinfo->value.enumerated.item >= cfg->max)
1537 			uinfo->value.enumerated.item = cfg->max - 1;
1538 		strscpy(uinfo->value.enumerated.name,
1539 			cfg->texts[uinfo->value.enumerated.item],
1540 			sizeof(uinfo->value.enumerated.name));
1541 	} else {
1542 		uinfo->count = cfg->size;
1543 		uinfo->value.integer.min = 0;
1544 		uinfo->value.integer.max = cfg->max;
1545 		uinfo->type = (cfg->max == 1) ?
1546 			SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1547 			SNDRV_CTL_ELEM_TYPE_INTEGER;
1548 	}
1549 
1550 	return 0;
1551 }
1552 
1553 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1554 			  struct snd_ctl_elem_value *uc)
1555 {
1556 	struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1557 	int i;
1558 
1559 	for (i = 0; i < cfg->size; i++)
1560 		if (cfg->texts)
1561 			uc->value.enumerated.item[i] = cfg->val[i];
1562 		else
1563 			uc->value.integer.value[i] = cfg->val[i];
1564 
1565 	return 0;
1566 }
1567 
1568 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1569 			  struct snd_ctl_elem_value *uc)
1570 {
1571 	struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1572 	int i, change = 0;
1573 
1574 	if (!cfg->accept(cfg->io))
1575 		return 0;
1576 
1577 	for (i = 0; i < cfg->size; i++) {
1578 		if (cfg->texts) {
1579 			change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1580 			cfg->val[i] = uc->value.enumerated.item[i];
1581 		} else {
1582 			change |= (uc->value.integer.value[i] != cfg->val[i]);
1583 			cfg->val[i] = uc->value.integer.value[i];
1584 		}
1585 	}
1586 
1587 	if (change && cfg->update)
1588 		cfg->update(cfg->io, cfg->mod);
1589 
1590 	return change;
1591 }
1592 
1593 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1594 {
1595 	return 1;
1596 }
1597 
1598 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1599 {
1600 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1601 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
1602 	struct device *dev = rsnd_priv_to_dev(priv);
1603 
1604 	if (!runtime) {
1605 		dev_warn(dev, "Can't update kctrl when idle\n");
1606 		return 0;
1607 	}
1608 
1609 	return 1;
1610 }
1611 
1612 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1613 {
1614 	cfg->cfg.val = cfg->val;
1615 
1616 	return &cfg->cfg;
1617 }
1618 
1619 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1620 {
1621 	cfg->cfg.val = &cfg->val;
1622 
1623 	return &cfg->cfg;
1624 }
1625 
1626 const char * const volume_ramp_rate[] = {
1627 	"128 dB/1 step",	 /* 00000 */
1628 	"64 dB/1 step",		 /* 00001 */
1629 	"32 dB/1 step",		 /* 00010 */
1630 	"16 dB/1 step",		 /* 00011 */
1631 	"8 dB/1 step",		 /* 00100 */
1632 	"4 dB/1 step",		 /* 00101 */
1633 	"2 dB/1 step",		 /* 00110 */
1634 	"1 dB/1 step",		 /* 00111 */
1635 	"0.5 dB/1 step",	 /* 01000 */
1636 	"0.25 dB/1 step",	 /* 01001 */
1637 	"0.125 dB/1 step",	 /* 01010 = VOLUME_RAMP_MAX_MIX */
1638 	"0.125 dB/2 steps",	 /* 01011 */
1639 	"0.125 dB/4 steps",	 /* 01100 */
1640 	"0.125 dB/8 steps",	 /* 01101 */
1641 	"0.125 dB/16 steps",	 /* 01110 */
1642 	"0.125 dB/32 steps",	 /* 01111 */
1643 	"0.125 dB/64 steps",	 /* 10000 */
1644 	"0.125 dB/128 steps",	 /* 10001 */
1645 	"0.125 dB/256 steps",	 /* 10010 */
1646 	"0.125 dB/512 steps",	 /* 10011 */
1647 	"0.125 dB/1024 steps",	 /* 10100 */
1648 	"0.125 dB/2048 steps",	 /* 10101 */
1649 	"0.125 dB/4096 steps",	 /* 10110 */
1650 	"0.125 dB/8192 steps",	 /* 10111 = VOLUME_RAMP_MAX_DVC */
1651 };
1652 
1653 int rsnd_kctrl_new(struct rsnd_mod *mod,
1654 		   struct rsnd_dai_stream *io,
1655 		   struct snd_soc_pcm_runtime *rtd,
1656 		   const unsigned char *name,
1657 		   int (*accept)(struct rsnd_dai_stream *io),
1658 		   void (*update)(struct rsnd_dai_stream *io,
1659 				  struct rsnd_mod *mod),
1660 		   struct rsnd_kctrl_cfg *cfg,
1661 		   const char * const *texts,
1662 		   int size,
1663 		   u32 max)
1664 {
1665 	struct snd_card *card = rtd->card->snd_card;
1666 	struct snd_kcontrol *kctrl;
1667 	struct snd_kcontrol_new knew = {
1668 		.iface		= SNDRV_CTL_ELEM_IFACE_MIXER,
1669 		.name		= name,
1670 		.info		= rsnd_kctrl_info,
1671 		.index		= rtd->num,
1672 		.get		= rsnd_kctrl_get,
1673 		.put		= rsnd_kctrl_put,
1674 	};
1675 	int ret;
1676 
1677 	/*
1678 	 * 1) Avoid duplicate register for DVC with MIX case
1679 	 * 2) Allow duplicate register for MIX
1680 	 * 3) re-register if card was rebinded
1681 	 */
1682 	list_for_each_entry(kctrl, &card->controls, list) {
1683 		struct rsnd_kctrl_cfg *c = kctrl->private_data;
1684 
1685 		if (c == cfg)
1686 			return 0;
1687 	}
1688 
1689 	if (size > RSND_MAX_CHANNELS)
1690 		return -EINVAL;
1691 
1692 	kctrl = snd_ctl_new1(&knew, cfg);
1693 	if (!kctrl)
1694 		return -ENOMEM;
1695 
1696 	ret = snd_ctl_add(card, kctrl);
1697 	if (ret < 0)
1698 		return ret;
1699 
1700 	cfg->texts	= texts;
1701 	cfg->max	= max;
1702 	cfg->size	= size;
1703 	cfg->accept	= accept;
1704 	cfg->update	= update;
1705 	cfg->card	= card;
1706 	cfg->kctrl	= kctrl;
1707 	cfg->io		= io;
1708 	cfg->mod	= mod;
1709 
1710 	return 0;
1711 }
1712 
1713 /*
1714  *		snd_soc_component
1715  */
1716 static const struct snd_soc_component_driver rsnd_soc_component = {
1717 	.name		= "rsnd",
1718 	.hw_params	= rsnd_hw_params,
1719 	.hw_free	= rsnd_hw_free,
1720 	.pointer	= rsnd_pointer,
1721 };
1722 
1723 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1724 				       struct rsnd_dai_stream *io)
1725 {
1726 	int ret;
1727 
1728 	ret = rsnd_dai_call(probe, io, priv);
1729 	if (ret == -EAGAIN) {
1730 		struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1731 		struct rsnd_mod *mod;
1732 		int i;
1733 
1734 		/*
1735 		 * Fallback to PIO mode
1736 		 */
1737 
1738 		/*
1739 		 * call "remove" for SSI/SRC/DVC
1740 		 * SSI will be switch to PIO mode if it was DMA mode
1741 		 * see
1742 		 *	rsnd_dma_init()
1743 		 *	rsnd_ssi_fallback()
1744 		 */
1745 		rsnd_dai_call(remove, io, priv);
1746 
1747 		/*
1748 		 * remove all mod from io
1749 		 * and, re connect ssi
1750 		 */
1751 		for_each_rsnd_mod(i, mod, io)
1752 			rsnd_dai_disconnect(mod, io, i);
1753 		rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1754 
1755 		/*
1756 		 * fallback
1757 		 */
1758 		rsnd_dai_call(fallback, io, priv);
1759 
1760 		/*
1761 		 * retry to "probe".
1762 		 * DAI has SSI which is PIO mode only now.
1763 		 */
1764 		ret = rsnd_dai_call(probe, io, priv);
1765 	}
1766 
1767 	return ret;
1768 }
1769 
1770 /*
1771  *	rsnd probe
1772  */
1773 static int rsnd_probe(struct platform_device *pdev)
1774 {
1775 	struct rsnd_priv *priv;
1776 	struct device *dev = &pdev->dev;
1777 	struct rsnd_dai *rdai;
1778 	int (*probe_func[])(struct rsnd_priv *priv) = {
1779 		rsnd_gen_probe,
1780 		rsnd_dma_probe,
1781 		rsnd_ssi_probe,
1782 		rsnd_ssiu_probe,
1783 		rsnd_src_probe,
1784 		rsnd_ctu_probe,
1785 		rsnd_mix_probe,
1786 		rsnd_dvc_probe,
1787 		rsnd_cmd_probe,
1788 		rsnd_adg_probe,
1789 		rsnd_dai_probe,
1790 	};
1791 	int ret, i;
1792 
1793 	/*
1794 	 *	init priv data
1795 	 */
1796 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1797 	if (!priv)
1798 		return -ENODEV;
1799 
1800 	priv->pdev	= pdev;
1801 	priv->flags	= (unsigned long)of_device_get_match_data(dev);
1802 	spin_lock_init(&priv->lock);
1803 
1804 	/*
1805 	 *	init each module
1806 	 */
1807 	for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1808 		ret = probe_func[i](priv);
1809 		if (ret)
1810 			return ret;
1811 	}
1812 
1813 	for_each_rsnd_dai(rdai, priv, i) {
1814 		ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1815 		if (ret)
1816 			goto exit_snd_probe;
1817 
1818 		ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1819 		if (ret)
1820 			goto exit_snd_probe;
1821 	}
1822 
1823 	dev_set_drvdata(dev, priv);
1824 
1825 	/*
1826 	 *	asoc register
1827 	 */
1828 	ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
1829 					 priv->daidrv, rsnd_rdai_nr(priv));
1830 	if (ret < 0) {
1831 		dev_err(dev, "cannot snd dai register\n");
1832 		goto exit_snd_probe;
1833 	}
1834 
1835 	pm_runtime_enable(dev);
1836 
1837 	dev_info(dev, "probed\n");
1838 	return ret;
1839 
1840 exit_snd_probe:
1841 	for_each_rsnd_dai(rdai, priv, i) {
1842 		rsnd_dai_call(remove, &rdai->playback, priv);
1843 		rsnd_dai_call(remove, &rdai->capture, priv);
1844 	}
1845 
1846 	/*
1847 	 * adg is very special mod which can't use rsnd_dai_call(remove),
1848 	 * and it registers ADG clock on probe.
1849 	 * It should be unregister if probe failed.
1850 	 * Mainly it is assuming -EPROBE_DEFER case
1851 	 */
1852 	rsnd_adg_remove(priv);
1853 
1854 	return ret;
1855 }
1856 
1857 static int rsnd_remove(struct platform_device *pdev)
1858 {
1859 	struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1860 	struct rsnd_dai *rdai;
1861 	void (*remove_func[])(struct rsnd_priv *priv) = {
1862 		rsnd_ssi_remove,
1863 		rsnd_ssiu_remove,
1864 		rsnd_src_remove,
1865 		rsnd_ctu_remove,
1866 		rsnd_mix_remove,
1867 		rsnd_dvc_remove,
1868 		rsnd_cmd_remove,
1869 		rsnd_adg_remove,
1870 	};
1871 	int ret = 0, i;
1872 
1873 	pm_runtime_disable(&pdev->dev);
1874 
1875 	for_each_rsnd_dai(rdai, priv, i) {
1876 		ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1877 		ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1878 	}
1879 
1880 	for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1881 		remove_func[i](priv);
1882 
1883 	return ret;
1884 }
1885 
1886 static int __maybe_unused rsnd_suspend(struct device *dev)
1887 {
1888 	struct rsnd_priv *priv = dev_get_drvdata(dev);
1889 
1890 	rsnd_adg_clk_disable(priv);
1891 
1892 	return 0;
1893 }
1894 
1895 static int __maybe_unused rsnd_resume(struct device *dev)
1896 {
1897 	struct rsnd_priv *priv = dev_get_drvdata(dev);
1898 
1899 	rsnd_adg_clk_enable(priv);
1900 
1901 	return 0;
1902 }
1903 
1904 static const struct dev_pm_ops rsnd_pm_ops = {
1905 	SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
1906 };
1907 
1908 static struct platform_driver rsnd_driver = {
1909 	.driver	= {
1910 		.name	= "rcar_sound",
1911 		.pm	= &rsnd_pm_ops,
1912 		.of_match_table = rsnd_of_match,
1913 	},
1914 	.probe		= rsnd_probe,
1915 	.remove		= rsnd_remove,
1916 };
1917 module_platform_driver(rsnd_driver);
1918 
1919 MODULE_LICENSE("GPL v2");
1920 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1921 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1922 MODULE_ALIAS("platform:rcar-pcm-audio");
1923