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