xref: /openbmc/linux/sound/soc/sh/rcar/core.c (revision 36de991e)
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_MASTER_MASK) {
759 	case SND_SOC_DAIFMT_CBP_CFP:
760 		rdai->clk_master = 0;
761 		break;
762 	case SND_SOC_DAIFMT_CBC_CFC:
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_node *np;
1163 	int i;
1164 
1165 	if (!node)
1166 		return;
1167 
1168 	i = 0;
1169 	for_each_child_of_node(node, np) {
1170 		struct rsnd_mod *mod;
1171 
1172 		i = rsnd_node_fixed_index(np, name, i);
1173 
1174 		mod = mod_get(priv, i);
1175 
1176 		if (np == playback)
1177 			rsnd_dai_connect(mod, &rdai->playback, mod->type);
1178 		if (np == capture)
1179 			rsnd_dai_connect(mod, &rdai->capture, mod->type);
1180 		i++;
1181 	}
1182 
1183 	of_node_put(node);
1184 }
1185 
1186 int rsnd_node_fixed_index(struct device_node *node, char *name, int idx)
1187 {
1188 	char node_name[16];
1189 
1190 	/*
1191 	 * rsnd is assuming each device nodes are sequential numbering,
1192 	 * but some of them are not.
1193 	 * This function adjusts index for it.
1194 	 *
1195 	 * ex)
1196 	 * Normal case,		special case
1197 	 *	ssi-0
1198 	 *	ssi-1
1199 	 *	ssi-2
1200 	 *	ssi-3		ssi-3
1201 	 *	ssi-4		ssi-4
1202 	 *	...
1203 	 *
1204 	 * assume Max 64 node
1205 	 */
1206 	for (; idx < 64; idx++) {
1207 		snprintf(node_name, sizeof(node_name), "%s-%d", name, idx);
1208 
1209 		if (strncmp(node_name, of_node_full_name(node), sizeof(node_name)) == 0)
1210 			return idx;
1211 	}
1212 
1213 	return -EINVAL;
1214 }
1215 
1216 int rsnd_node_count(struct rsnd_priv *priv, struct device_node *node, char *name)
1217 {
1218 	struct device *dev = rsnd_priv_to_dev(priv);
1219 	struct device_node *np;
1220 	int i;
1221 
1222 	i = 0;
1223 	for_each_child_of_node(node, np) {
1224 		i = rsnd_node_fixed_index(np, name, i);
1225 		if (i < 0) {
1226 			dev_err(dev, "strange node numbering (%s)",
1227 				of_node_full_name(node));
1228 			of_node_put(np);
1229 			return 0;
1230 		}
1231 		i++;
1232 	}
1233 
1234 	return i;
1235 }
1236 
1237 static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
1238 					    int *is_graph)
1239 {
1240 	struct device *dev = rsnd_priv_to_dev(priv);
1241 	struct device_node *np = dev->of_node;
1242 	struct device_node *dai_node;
1243 	struct device_node *ret;
1244 
1245 	*is_graph = 0;
1246 
1247 	/*
1248 	 * parse both previous dai (= rcar_sound,dai), and
1249 	 * graph dai (= ports/port)
1250 	 */
1251 	dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
1252 	if (dai_node) {
1253 		ret = dai_node;
1254 		goto of_node_compatible;
1255 	}
1256 
1257 	ret = np;
1258 
1259 	dai_node = of_graph_get_next_endpoint(np, NULL);
1260 	if (dai_node)
1261 		goto of_node_graph;
1262 
1263 	return NULL;
1264 
1265 of_node_graph:
1266 	*is_graph = 1;
1267 of_node_compatible:
1268 	of_node_put(dai_node);
1269 
1270 	return ret;
1271 }
1272 
1273 
1274 #define PREALLOC_BUFFER		(32 * 1024)
1275 #define PREALLOC_BUFFER_MAX	(32 * 1024)
1276 
1277 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1278 				  struct rsnd_dai_stream *io,
1279 				  int stream)
1280 {
1281 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
1282 	struct device *dev = rsnd_priv_to_dev(priv);
1283 	struct snd_pcm_substream *substream;
1284 
1285 	/*
1286 	 * use Audio-DMAC dev if we can use IPMMU
1287 	 * see
1288 	 *	rsnd_dmaen_attach()
1289 	 */
1290 	if (io->dmac_dev)
1291 		dev = io->dmac_dev;
1292 
1293 	for (substream = rtd->pcm->streams[stream].substream;
1294 	     substream;
1295 	     substream = substream->next) {
1296 		snd_pcm_set_managed_buffer(substream,
1297 					   SNDRV_DMA_TYPE_DEV,
1298 					   dev,
1299 					   PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1300 	}
1301 
1302 	return 0;
1303 }
1304 
1305 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd,
1306 			struct snd_soc_dai *dai)
1307 {
1308 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1309 	int ret;
1310 
1311 	ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1312 	if (ret)
1313 		return ret;
1314 
1315 	ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1316 	if (ret)
1317 		return ret;
1318 
1319 	ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1320 				     SNDRV_PCM_STREAM_PLAYBACK);
1321 	if (ret)
1322 		return ret;
1323 
1324 	ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1325 				     SNDRV_PCM_STREAM_CAPTURE);
1326 	if (ret)
1327 		return ret;
1328 
1329 	return 0;
1330 }
1331 
1332 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1333 			     struct device_node *dai_np,
1334 			     int dai_i)
1335 {
1336 	struct rsnd_dai_stream *io_playback;
1337 	struct rsnd_dai_stream *io_capture;
1338 	struct snd_soc_dai_driver *drv;
1339 	struct rsnd_dai *rdai;
1340 	struct device *dev = rsnd_priv_to_dev(priv);
1341 	int io_i;
1342 
1343 	rdai		= rsnd_rdai_get(priv, dai_i);
1344 	drv		= rsnd_daidrv_get(priv, dai_i);
1345 	io_playback	= &rdai->playback;
1346 	io_capture	= &rdai->capture;
1347 
1348 	snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1349 
1350 	rdai->priv	= priv;
1351 	drv->name	= rdai->name;
1352 	drv->ops	= &rsnd_soc_dai_ops;
1353 	drv->pcm_new	= rsnd_pcm_new;
1354 
1355 	snprintf(io_playback->name, RSND_DAI_NAME_SIZE,
1356 		 "DAI%d Playback", dai_i);
1357 	drv->playback.rates		= RSND_RATES;
1358 	drv->playback.formats		= RSND_FMTS;
1359 	drv->playback.channels_min	= 2;
1360 	drv->playback.channels_max	= 8;
1361 	drv->playback.stream_name	= io_playback->name;
1362 
1363 	snprintf(io_capture->name, RSND_DAI_NAME_SIZE,
1364 		 "DAI%d Capture", dai_i);
1365 	drv->capture.rates		= RSND_RATES;
1366 	drv->capture.formats		= RSND_FMTS;
1367 	drv->capture.channels_min	= 2;
1368 	drv->capture.channels_max	= 8;
1369 	drv->capture.stream_name	= io_capture->name;
1370 
1371 	io_playback->rdai		= rdai;
1372 	io_capture->rdai		= rdai;
1373 	rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1374 	rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1375 	rsnd_rdai_width_set(rdai, 32);   /* default 32bit width */
1376 
1377 	for (io_i = 0;; io_i++) {
1378 		struct device_node *playback = of_parse_phandle(dai_np, "playback", io_i);
1379 		struct device_node *capture  = of_parse_phandle(dai_np, "capture", io_i);
1380 
1381 		if (!playback && !capture)
1382 			break;
1383 
1384 		rsnd_parse_connect_ssi(rdai, playback, capture);
1385 		rsnd_parse_connect_ssiu(rdai, playback, capture);
1386 		rsnd_parse_connect_src(rdai, playback, capture);
1387 		rsnd_parse_connect_ctu(rdai, playback, capture);
1388 		rsnd_parse_connect_mix(rdai, playback, capture);
1389 		rsnd_parse_connect_dvc(rdai, playback, capture);
1390 
1391 		of_node_put(playback);
1392 		of_node_put(capture);
1393 	}
1394 
1395 	if (rsnd_ssi_is_pin_sharing(io_capture) ||
1396 	    rsnd_ssi_is_pin_sharing(io_playback)) {
1397 		/* should have symmetric_rate if pin sharing */
1398 		drv->symmetric_rate = 1;
1399 	}
1400 
1401 	dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1402 		rsnd_io_to_mod_ssi(io_playback) ? "play"    : " -- ",
1403 		rsnd_io_to_mod_ssi(io_capture) ? "capture" : "  --   ");
1404 }
1405 
1406 static int rsnd_dai_probe(struct rsnd_priv *priv)
1407 {
1408 	struct device_node *dai_node;
1409 	struct device_node *dai_np;
1410 	struct snd_soc_dai_driver *rdrv;
1411 	struct device *dev = rsnd_priv_to_dev(priv);
1412 	struct rsnd_dai *rdai;
1413 	int nr;
1414 	int is_graph;
1415 	int dai_i;
1416 
1417 	dai_node = rsnd_dai_of_node(priv, &is_graph);
1418 	if (is_graph)
1419 		nr = of_graph_get_endpoint_count(dai_node);
1420 	else
1421 		nr = of_get_child_count(dai_node);
1422 
1423 	if (!nr)
1424 		return -EINVAL;
1425 
1426 	rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1427 	rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1428 	if (!rdrv || !rdai)
1429 		return -ENOMEM;
1430 
1431 	priv->rdai_nr	= nr;
1432 	priv->daidrv	= rdrv;
1433 	priv->rdai	= rdai;
1434 
1435 	/*
1436 	 * parse all dai
1437 	 */
1438 	dai_i = 0;
1439 	if (is_graph) {
1440 		for_each_endpoint_of_node(dai_node, dai_np) {
1441 			__rsnd_dai_probe(priv, dai_np, dai_i);
1442 			if (rsnd_is_gen3(priv)) {
1443 				rdai = rsnd_rdai_get(priv, dai_i);
1444 
1445 				rsnd_parse_connect_graph(priv, &rdai->playback, dai_np);
1446 				rsnd_parse_connect_graph(priv, &rdai->capture,  dai_np);
1447 			}
1448 			dai_i++;
1449 		}
1450 	} else {
1451 		for_each_child_of_node(dai_node, dai_np) {
1452 			__rsnd_dai_probe(priv, dai_np, dai_i);
1453 			if (rsnd_is_gen3(priv)) {
1454 				rdai = rsnd_rdai_get(priv, dai_i);
1455 
1456 				rsnd_parse_connect_simple(priv, &rdai->playback, dai_np);
1457 				rsnd_parse_connect_simple(priv, &rdai->capture,  dai_np);
1458 			}
1459 			dai_i++;
1460 		}
1461 	}
1462 
1463 	return 0;
1464 }
1465 
1466 /*
1467  *		pcm ops
1468  */
1469 static int rsnd_hw_update(struct snd_pcm_substream *substream,
1470 			  struct snd_pcm_hw_params *hw_params)
1471 {
1472 	struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1473 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1474 	struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1475 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
1476 	unsigned long flags;
1477 	int ret;
1478 
1479 	spin_lock_irqsave(&priv->lock, flags);
1480 	if (hw_params)
1481 		ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1482 	else
1483 		ret = rsnd_dai_call(hw_free, io, substream);
1484 	spin_unlock_irqrestore(&priv->lock, flags);
1485 
1486 	return ret;
1487 }
1488 
1489 static int rsnd_hw_params(struct snd_soc_component *component,
1490 			  struct snd_pcm_substream *substream,
1491 			  struct snd_pcm_hw_params *hw_params)
1492 {
1493 	struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1494 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1495 	struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1496 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1497 
1498 	/*
1499 	 * rsnd assumes that it might be used under DPCM if user want to use
1500 	 * channel / rate convert. Then, rsnd should be FE.
1501 	 * And then, this function will be called *after* BE settings.
1502 	 * this means, each BE already has fixuped hw_params.
1503 	 * see
1504 	 *	dpcm_fe_dai_hw_params()
1505 	 *	dpcm_be_dai_hw_params()
1506 	 */
1507 	io->converted_rate = 0;
1508 	io->converted_chan = 0;
1509 	if (fe->dai_link->dynamic) {
1510 		struct rsnd_priv *priv = rsnd_io_to_priv(io);
1511 		struct device *dev = rsnd_priv_to_dev(priv);
1512 		struct snd_soc_dpcm *dpcm;
1513 		int stream = substream->stream;
1514 
1515 		for_each_dpcm_be(fe, stream, dpcm) {
1516 			struct snd_pcm_hw_params *be_params = &dpcm->hw_params;
1517 
1518 			if (params_channels(hw_params) != params_channels(be_params))
1519 				io->converted_chan = params_channels(be_params);
1520 			if (params_rate(hw_params) != params_rate(be_params))
1521 				io->converted_rate = params_rate(be_params);
1522 		}
1523 		if (io->converted_chan)
1524 			dev_dbg(dev, "convert channels = %d\n", io->converted_chan);
1525 		if (io->converted_rate) {
1526 			/*
1527 			 * SRC supports convert rates from params_rate(hw_params)/k_down
1528 			 * to params_rate(hw_params)*k_up, where k_up is always 6, and
1529 			 * k_down depends on number of channels and SRC unit.
1530 			 * So all SRC units can upsample audio up to 6 times regardless
1531 			 * its number of channels. And all SRC units can downsample
1532 			 * 2 channel audio up to 6 times too.
1533 			 */
1534 			int k_up = 6;
1535 			int k_down = 6;
1536 			int channel;
1537 			struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
1538 
1539 			dev_dbg(dev, "convert rate     = %d\n", io->converted_rate);
1540 
1541 			channel = io->converted_chan ? io->converted_chan :
1542 				  params_channels(hw_params);
1543 
1544 			switch (rsnd_mod_id(src_mod)) {
1545 			/*
1546 			 * SRC0 can downsample 4, 6 and 8 channel audio up to 4 times.
1547 			 * SRC1, SRC3 and SRC4 can downsample 4 channel audio
1548 			 * up to 4 times.
1549 			 * SRC1, SRC3 and SRC4 can downsample 6 and 8 channel audio
1550 			 * no more than twice.
1551 			 */
1552 			case 1:
1553 			case 3:
1554 			case 4:
1555 				if (channel > 4) {
1556 					k_down = 2;
1557 					break;
1558 				}
1559 				fallthrough;
1560 			case 0:
1561 				if (channel > 2)
1562 					k_down = 4;
1563 				break;
1564 
1565 			/* Other SRC units do not support more than 2 channels */
1566 			default:
1567 				if (channel > 2)
1568 					return -EINVAL;
1569 			}
1570 
1571 			if (params_rate(hw_params) > io->converted_rate * k_down) {
1572 				hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1573 					io->converted_rate * k_down;
1574 				hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1575 					io->converted_rate * k_down;
1576 				hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1577 			} else if (params_rate(hw_params) * k_up < io->converted_rate) {
1578 				hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1579 					(io->converted_rate + k_up - 1) / k_up;
1580 				hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1581 					(io->converted_rate + k_up - 1) / k_up;
1582 				hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1583 			}
1584 
1585 			/*
1586 			 * TBD: Max SRC input and output rates also depend on number
1587 			 * of channels and SRC unit:
1588 			 * SRC1, SRC3 and SRC4 do not support more than 128kHz
1589 			 * for 6 channel and 96kHz for 8 channel audio.
1590 			 * Perhaps this function should return EINVAL if the input or
1591 			 * the output rate exceeds the limitation.
1592 			 */
1593 		}
1594 	}
1595 
1596 	return rsnd_hw_update(substream, hw_params);
1597 }
1598 
1599 static int rsnd_hw_free(struct snd_soc_component *component,
1600 			struct snd_pcm_substream *substream)
1601 {
1602 	return rsnd_hw_update(substream, NULL);
1603 }
1604 
1605 static snd_pcm_uframes_t rsnd_pointer(struct snd_soc_component *component,
1606 				      struct snd_pcm_substream *substream)
1607 {
1608 	struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1609 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1610 	struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1611 	snd_pcm_uframes_t pointer = 0;
1612 
1613 	rsnd_dai_call(pointer, io, &pointer);
1614 
1615 	return pointer;
1616 }
1617 
1618 /*
1619  *		snd_kcontrol
1620  */
1621 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1622 			   struct snd_ctl_elem_info *uinfo)
1623 {
1624 	struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1625 
1626 	if (cfg->texts) {
1627 		uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1628 		uinfo->count = cfg->size;
1629 		uinfo->value.enumerated.items = cfg->max;
1630 		if (uinfo->value.enumerated.item >= cfg->max)
1631 			uinfo->value.enumerated.item = cfg->max - 1;
1632 		strscpy(uinfo->value.enumerated.name,
1633 			cfg->texts[uinfo->value.enumerated.item],
1634 			sizeof(uinfo->value.enumerated.name));
1635 	} else {
1636 		uinfo->count = cfg->size;
1637 		uinfo->value.integer.min = 0;
1638 		uinfo->value.integer.max = cfg->max;
1639 		uinfo->type = (cfg->max == 1) ?
1640 			SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1641 			SNDRV_CTL_ELEM_TYPE_INTEGER;
1642 	}
1643 
1644 	return 0;
1645 }
1646 
1647 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1648 			  struct snd_ctl_elem_value *uc)
1649 {
1650 	struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1651 	int i;
1652 
1653 	for (i = 0; i < cfg->size; i++)
1654 		if (cfg->texts)
1655 			uc->value.enumerated.item[i] = cfg->val[i];
1656 		else
1657 			uc->value.integer.value[i] = cfg->val[i];
1658 
1659 	return 0;
1660 }
1661 
1662 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1663 			  struct snd_ctl_elem_value *uc)
1664 {
1665 	struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1666 	int i, change = 0;
1667 
1668 	if (!cfg->accept(cfg->io))
1669 		return 0;
1670 
1671 	for (i = 0; i < cfg->size; i++) {
1672 		if (cfg->texts) {
1673 			change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1674 			cfg->val[i] = uc->value.enumerated.item[i];
1675 		} else {
1676 			change |= (uc->value.integer.value[i] != cfg->val[i]);
1677 			cfg->val[i] = uc->value.integer.value[i];
1678 		}
1679 	}
1680 
1681 	if (change && cfg->update)
1682 		cfg->update(cfg->io, cfg->mod);
1683 
1684 	return change;
1685 }
1686 
1687 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1688 {
1689 	return 1;
1690 }
1691 
1692 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1693 {
1694 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1695 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
1696 	struct device *dev = rsnd_priv_to_dev(priv);
1697 
1698 	if (!runtime) {
1699 		dev_warn(dev, "Can't update kctrl when idle\n");
1700 		return 0;
1701 	}
1702 
1703 	return 1;
1704 }
1705 
1706 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1707 {
1708 	cfg->cfg.val = cfg->val;
1709 
1710 	return &cfg->cfg;
1711 }
1712 
1713 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1714 {
1715 	cfg->cfg.val = &cfg->val;
1716 
1717 	return &cfg->cfg;
1718 }
1719 
1720 const char * const volume_ramp_rate[] = {
1721 	"128 dB/1 step",	 /* 00000 */
1722 	"64 dB/1 step",		 /* 00001 */
1723 	"32 dB/1 step",		 /* 00010 */
1724 	"16 dB/1 step",		 /* 00011 */
1725 	"8 dB/1 step",		 /* 00100 */
1726 	"4 dB/1 step",		 /* 00101 */
1727 	"2 dB/1 step",		 /* 00110 */
1728 	"1 dB/1 step",		 /* 00111 */
1729 	"0.5 dB/1 step",	 /* 01000 */
1730 	"0.25 dB/1 step",	 /* 01001 */
1731 	"0.125 dB/1 step",	 /* 01010 = VOLUME_RAMP_MAX_MIX */
1732 	"0.125 dB/2 steps",	 /* 01011 */
1733 	"0.125 dB/4 steps",	 /* 01100 */
1734 	"0.125 dB/8 steps",	 /* 01101 */
1735 	"0.125 dB/16 steps",	 /* 01110 */
1736 	"0.125 dB/32 steps",	 /* 01111 */
1737 	"0.125 dB/64 steps",	 /* 10000 */
1738 	"0.125 dB/128 steps",	 /* 10001 */
1739 	"0.125 dB/256 steps",	 /* 10010 */
1740 	"0.125 dB/512 steps",	 /* 10011 */
1741 	"0.125 dB/1024 steps",	 /* 10100 */
1742 	"0.125 dB/2048 steps",	 /* 10101 */
1743 	"0.125 dB/4096 steps",	 /* 10110 */
1744 	"0.125 dB/8192 steps",	 /* 10111 = VOLUME_RAMP_MAX_DVC */
1745 };
1746 
1747 int rsnd_kctrl_new(struct rsnd_mod *mod,
1748 		   struct rsnd_dai_stream *io,
1749 		   struct snd_soc_pcm_runtime *rtd,
1750 		   const unsigned char *name,
1751 		   int (*accept)(struct rsnd_dai_stream *io),
1752 		   void (*update)(struct rsnd_dai_stream *io,
1753 				  struct rsnd_mod *mod),
1754 		   struct rsnd_kctrl_cfg *cfg,
1755 		   const char * const *texts,
1756 		   int size,
1757 		   u32 max)
1758 {
1759 	struct snd_card *card = rtd->card->snd_card;
1760 	struct snd_kcontrol *kctrl;
1761 	struct snd_kcontrol_new knew = {
1762 		.iface		= SNDRV_CTL_ELEM_IFACE_MIXER,
1763 		.name		= name,
1764 		.info		= rsnd_kctrl_info,
1765 		.index		= rtd->num,
1766 		.get		= rsnd_kctrl_get,
1767 		.put		= rsnd_kctrl_put,
1768 	};
1769 	int ret;
1770 
1771 	/*
1772 	 * 1) Avoid duplicate register for DVC with MIX case
1773 	 * 2) Allow duplicate register for MIX
1774 	 * 3) re-register if card was rebinded
1775 	 */
1776 	list_for_each_entry(kctrl, &card->controls, list) {
1777 		struct rsnd_kctrl_cfg *c = kctrl->private_data;
1778 
1779 		if (c == cfg)
1780 			return 0;
1781 	}
1782 
1783 	if (size > RSND_MAX_CHANNELS)
1784 		return -EINVAL;
1785 
1786 	kctrl = snd_ctl_new1(&knew, cfg);
1787 	if (!kctrl)
1788 		return -ENOMEM;
1789 
1790 	ret = snd_ctl_add(card, kctrl);
1791 	if (ret < 0)
1792 		return ret;
1793 
1794 	cfg->texts	= texts;
1795 	cfg->max	= max;
1796 	cfg->size	= size;
1797 	cfg->accept	= accept;
1798 	cfg->update	= update;
1799 	cfg->card	= card;
1800 	cfg->kctrl	= kctrl;
1801 	cfg->io		= io;
1802 	cfg->mod	= mod;
1803 
1804 	return 0;
1805 }
1806 
1807 /*
1808  *		snd_soc_component
1809  */
1810 static const struct snd_soc_component_driver rsnd_soc_component = {
1811 	.name		= "rsnd",
1812 	.probe		= rsnd_debugfs_probe,
1813 	.hw_params	= rsnd_hw_params,
1814 	.hw_free	= rsnd_hw_free,
1815 	.pointer	= rsnd_pointer,
1816 };
1817 
1818 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1819 				       struct rsnd_dai_stream *io)
1820 {
1821 	int ret;
1822 
1823 	ret = rsnd_dai_call(probe, io, priv);
1824 	if (ret == -EAGAIN) {
1825 		struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1826 		struct rsnd_mod *mod;
1827 		int i;
1828 
1829 		/*
1830 		 * Fallback to PIO mode
1831 		 */
1832 
1833 		/*
1834 		 * call "remove" for SSI/SRC/DVC
1835 		 * SSI will be switch to PIO mode if it was DMA mode
1836 		 * see
1837 		 *	rsnd_dma_init()
1838 		 *	rsnd_ssi_fallback()
1839 		 */
1840 		rsnd_dai_call(remove, io, priv);
1841 
1842 		/*
1843 		 * remove all mod from io
1844 		 * and, re connect ssi
1845 		 */
1846 		for_each_rsnd_mod(i, mod, io)
1847 			rsnd_dai_disconnect(mod, io, i);
1848 		rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1849 
1850 		/*
1851 		 * fallback
1852 		 */
1853 		rsnd_dai_call(fallback, io, priv);
1854 
1855 		/*
1856 		 * retry to "probe".
1857 		 * DAI has SSI which is PIO mode only now.
1858 		 */
1859 		ret = rsnd_dai_call(probe, io, priv);
1860 	}
1861 
1862 	return ret;
1863 }
1864 
1865 /*
1866  *	rsnd probe
1867  */
1868 static int rsnd_probe(struct platform_device *pdev)
1869 {
1870 	struct rsnd_priv *priv;
1871 	struct device *dev = &pdev->dev;
1872 	struct rsnd_dai *rdai;
1873 	int (*probe_func[])(struct rsnd_priv *priv) = {
1874 		rsnd_gen_probe,
1875 		rsnd_dma_probe,
1876 		rsnd_ssi_probe,
1877 		rsnd_ssiu_probe,
1878 		rsnd_src_probe,
1879 		rsnd_ctu_probe,
1880 		rsnd_mix_probe,
1881 		rsnd_dvc_probe,
1882 		rsnd_cmd_probe,
1883 		rsnd_adg_probe,
1884 		rsnd_dai_probe,
1885 	};
1886 	int ret, i;
1887 
1888 	/*
1889 	 *	init priv data
1890 	 */
1891 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1892 	if (!priv)
1893 		return -ENODEV;
1894 
1895 	priv->pdev	= pdev;
1896 	priv->flags	= (unsigned long)of_device_get_match_data(dev);
1897 	spin_lock_init(&priv->lock);
1898 
1899 	/*
1900 	 *	init each module
1901 	 */
1902 	for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1903 		ret = probe_func[i](priv);
1904 		if (ret)
1905 			return ret;
1906 	}
1907 
1908 	for_each_rsnd_dai(rdai, priv, i) {
1909 		ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1910 		if (ret)
1911 			goto exit_snd_probe;
1912 
1913 		ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1914 		if (ret)
1915 			goto exit_snd_probe;
1916 	}
1917 
1918 	dev_set_drvdata(dev, priv);
1919 
1920 	/*
1921 	 *	asoc register
1922 	 */
1923 	ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
1924 					 priv->daidrv, rsnd_rdai_nr(priv));
1925 	if (ret < 0) {
1926 		dev_err(dev, "cannot snd dai register\n");
1927 		goto exit_snd_probe;
1928 	}
1929 
1930 	pm_runtime_enable(dev);
1931 
1932 	dev_info(dev, "probed\n");
1933 	return ret;
1934 
1935 exit_snd_probe:
1936 	for_each_rsnd_dai(rdai, priv, i) {
1937 		rsnd_dai_call(remove, &rdai->playback, priv);
1938 		rsnd_dai_call(remove, &rdai->capture, priv);
1939 	}
1940 
1941 	/*
1942 	 * adg is very special mod which can't use rsnd_dai_call(remove),
1943 	 * and it registers ADG clock on probe.
1944 	 * It should be unregister if probe failed.
1945 	 * Mainly it is assuming -EPROBE_DEFER case
1946 	 */
1947 	rsnd_adg_remove(priv);
1948 
1949 	return ret;
1950 }
1951 
1952 static int rsnd_remove(struct platform_device *pdev)
1953 {
1954 	struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1955 	struct rsnd_dai *rdai;
1956 	void (*remove_func[])(struct rsnd_priv *priv) = {
1957 		rsnd_ssi_remove,
1958 		rsnd_ssiu_remove,
1959 		rsnd_src_remove,
1960 		rsnd_ctu_remove,
1961 		rsnd_mix_remove,
1962 		rsnd_dvc_remove,
1963 		rsnd_cmd_remove,
1964 		rsnd_adg_remove,
1965 	};
1966 	int ret = 0, i;
1967 
1968 	pm_runtime_disable(&pdev->dev);
1969 
1970 	for_each_rsnd_dai(rdai, priv, i) {
1971 		ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1972 		ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1973 	}
1974 
1975 	for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1976 		remove_func[i](priv);
1977 
1978 	return ret;
1979 }
1980 
1981 static int __maybe_unused rsnd_suspend(struct device *dev)
1982 {
1983 	struct rsnd_priv *priv = dev_get_drvdata(dev);
1984 
1985 	rsnd_adg_clk_disable(priv);
1986 
1987 	return 0;
1988 }
1989 
1990 static int __maybe_unused rsnd_resume(struct device *dev)
1991 {
1992 	struct rsnd_priv *priv = dev_get_drvdata(dev);
1993 
1994 	rsnd_adg_clk_enable(priv);
1995 
1996 	return 0;
1997 }
1998 
1999 static const struct dev_pm_ops rsnd_pm_ops = {
2000 	SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
2001 };
2002 
2003 static struct platform_driver rsnd_driver = {
2004 	.driver	= {
2005 		.name	= "rcar_sound",
2006 		.pm	= &rsnd_pm_ops,
2007 		.of_match_table = rsnd_of_match,
2008 	},
2009 	.probe		= rsnd_probe,
2010 	.remove		= rsnd_remove,
2011 };
2012 module_platform_driver(rsnd_driver);
2013 
2014 MODULE_LICENSE("GPL v2");
2015 MODULE_DESCRIPTION("Renesas R-Car audio driver");
2016 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
2017 MODULE_ALIAS("platform:rcar-pcm-audio");
2018