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