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