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