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