xref: /openbmc/linux/sound/soc/sh/rcar/core.c (revision 8fdf9062)
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_connect_simple(struct rsnd_priv *priv,
1035 				      struct device_node *dai_np,
1036 				      int dai_i, int is_play)
1037 {
1038 	struct device *dev = rsnd_priv_to_dev(priv);
1039 	struct rsnd_dai *rdai = rsnd_rdai_get(priv, dai_i);
1040 	struct rsnd_dai_stream *io = is_play ?
1041 		&rdai->playback :
1042 		&rdai->capture;
1043 	struct device_node *ssiu_np = rsnd_ssiu_of_node(priv);
1044 	struct device_node *np;
1045 	int i, j;
1046 
1047 	if (!ssiu_np)
1048 		return;
1049 
1050 	if (!rsnd_io_to_mod_ssi(io))
1051 		return;
1052 
1053 	/*
1054 	 * This driver assumes that it is TDM Split mode
1055 	 * if it includes ssiu node
1056 	 */
1057 	for (i = 0;; i++) {
1058 		struct device_node *node = is_play ?
1059 			of_parse_phandle(dai_np, "playback", i) :
1060 			of_parse_phandle(dai_np, "capture",  i);
1061 
1062 		if (!node)
1063 			break;
1064 
1065 		j = 0;
1066 		for_each_child_of_node(ssiu_np, np) {
1067 			if (np == node) {
1068 				rsnd_flags_set(io, RSND_STREAM_TDM_SPLIT);
1069 				dev_dbg(dev, "%s is part of TDM Split\n", io->name);
1070 			}
1071 			j++;
1072 		}
1073 
1074 	}
1075 }
1076 
1077 static void rsnd_parse_connect_graph(struct rsnd_priv *priv,
1078 				     struct rsnd_dai_stream *io,
1079 				     struct device_node *endpoint)
1080 {
1081 	struct device *dev = rsnd_priv_to_dev(priv);
1082 	struct device_node *remote_port = of_graph_get_remote_port(endpoint);
1083 	struct device_node *remote_node = of_graph_get_remote_port_parent(endpoint);
1084 
1085 	if (!rsnd_io_to_mod_ssi(io))
1086 		return;
1087 
1088 	/* HDMI0 */
1089 	if (strstr(remote_node->full_name, "hdmi@fead0000")) {
1090 		rsnd_flags_set(io, RSND_STREAM_HDMI0);
1091 		dev_dbg(dev, "%s connected to HDMI0\n", io->name);
1092 	}
1093 
1094 	/* HDMI1 */
1095 	if (strstr(remote_node->full_name, "hdmi@feae0000")) {
1096 		rsnd_flags_set(io, RSND_STREAM_HDMI1);
1097 		dev_dbg(dev, "%s connected to HDMI1\n", io->name);
1098 	}
1099 
1100 	/*
1101 	 * This driver assumes that it is TDM Split mode
1102 	 * if remote node has multi endpoint
1103 	 */
1104 	if (of_get_child_count(remote_port) > 1) {
1105 		rsnd_flags_set(io, RSND_STREAM_TDM_SPLIT);
1106 		dev_dbg(dev, "%s is part of TDM Split\n", io->name);
1107 	}
1108 }
1109 
1110 void rsnd_parse_connect_common(struct rsnd_dai *rdai,
1111 		struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
1112 		struct device_node *node,
1113 		struct device_node *playback,
1114 		struct device_node *capture)
1115 {
1116 	struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1117 	struct device_node *np;
1118 	struct rsnd_mod *mod;
1119 	int i;
1120 
1121 	if (!node)
1122 		return;
1123 
1124 	i = 0;
1125 	for_each_child_of_node(node, np) {
1126 		mod = mod_get(priv, i);
1127 		if (np == playback)
1128 			rsnd_dai_connect(mod, &rdai->playback, mod->type);
1129 		if (np == capture)
1130 			rsnd_dai_connect(mod, &rdai->capture, mod->type);
1131 		i++;
1132 	}
1133 
1134 	of_node_put(node);
1135 }
1136 
1137 static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
1138 					    int *is_graph)
1139 {
1140 	struct device *dev = rsnd_priv_to_dev(priv);
1141 	struct device_node *np = dev->of_node;
1142 	struct device_node *dai_node;
1143 	struct device_node *ret;
1144 
1145 	*is_graph = 0;
1146 
1147 	/*
1148 	 * parse both previous dai (= rcar_sound,dai), and
1149 	 * graph dai (= ports/port)
1150 	 */
1151 	dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
1152 	if (dai_node) {
1153 		ret = dai_node;
1154 		goto of_node_compatible;
1155 	}
1156 
1157 	ret = np;
1158 
1159 	dai_node = of_graph_get_next_endpoint(np, NULL);
1160 	if (dai_node)
1161 		goto of_node_graph;
1162 
1163 	return NULL;
1164 
1165 of_node_graph:
1166 	*is_graph = 1;
1167 of_node_compatible:
1168 	of_node_put(dai_node);
1169 
1170 	return ret;
1171 }
1172 
1173 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1174 			     struct device_node *dai_np,
1175 			     int dai_i)
1176 {
1177 	struct device_node *playback, *capture;
1178 	struct rsnd_dai_stream *io_playback;
1179 	struct rsnd_dai_stream *io_capture;
1180 	struct snd_soc_dai_driver *drv;
1181 	struct rsnd_dai *rdai;
1182 	struct device *dev = rsnd_priv_to_dev(priv);
1183 	int io_i;
1184 
1185 	rdai		= rsnd_rdai_get(priv, dai_i);
1186 	drv		= rsnd_daidrv_get(priv, dai_i);
1187 	io_playback	= &rdai->playback;
1188 	io_capture	= &rdai->capture;
1189 
1190 	snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1191 
1192 	rdai->priv	= priv;
1193 	drv->name	= rdai->name;
1194 	drv->ops	= &rsnd_soc_dai_ops;
1195 
1196 	snprintf(io_playback->name, RSND_DAI_NAME_SIZE,
1197 		 "DAI%d Playback", dai_i);
1198 	drv->playback.rates		= RSND_RATES;
1199 	drv->playback.formats		= RSND_FMTS;
1200 	drv->playback.channels_min	= 2;
1201 	drv->playback.channels_max	= 8;
1202 	drv->playback.stream_name	= io_playback->name;
1203 
1204 	snprintf(io_capture->name, RSND_DAI_NAME_SIZE,
1205 		 "DAI%d Capture", dai_i);
1206 	drv->capture.rates		= RSND_RATES;
1207 	drv->capture.formats		= RSND_FMTS;
1208 	drv->capture.channels_min	= 2;
1209 	drv->capture.channels_max	= 8;
1210 	drv->capture.stream_name	= io_capture->name;
1211 
1212 	io_playback->rdai		= rdai;
1213 	io_capture->rdai		= rdai;
1214 	rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1215 	rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1216 	rsnd_rdai_width_set(rdai, 32);   /* default 32bit width */
1217 
1218 	for (io_i = 0;; io_i++) {
1219 		playback = of_parse_phandle(dai_np, "playback", io_i);
1220 		capture  = of_parse_phandle(dai_np, "capture", io_i);
1221 
1222 		if (!playback && !capture)
1223 			break;
1224 
1225 		rsnd_parse_connect_ssi(rdai, playback, capture);
1226 		rsnd_parse_connect_ssiu(rdai, playback, capture);
1227 		rsnd_parse_connect_src(rdai, playback, capture);
1228 		rsnd_parse_connect_ctu(rdai, playback, capture);
1229 		rsnd_parse_connect_mix(rdai, playback, capture);
1230 		rsnd_parse_connect_dvc(rdai, playback, capture);
1231 
1232 		of_node_put(playback);
1233 		of_node_put(capture);
1234 	}
1235 
1236 	if (rsnd_ssi_is_pin_sharing(io_capture) ||
1237 	    rsnd_ssi_is_pin_sharing(io_playback)) {
1238 		/* should have symmetric_rates if pin sharing */
1239 		drv->symmetric_rates = 1;
1240 	}
1241 
1242 	dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1243 		rsnd_io_to_mod_ssi(io_playback) ? "play"    : " -- ",
1244 		rsnd_io_to_mod_ssi(io_capture) ? "capture" : "  --   ");
1245 }
1246 
1247 static int rsnd_dai_probe(struct rsnd_priv *priv)
1248 {
1249 	struct device_node *dai_node;
1250 	struct device_node *dai_np;
1251 	struct snd_soc_dai_driver *rdrv;
1252 	struct device *dev = rsnd_priv_to_dev(priv);
1253 	struct rsnd_dai *rdai;
1254 	int nr;
1255 	int is_graph;
1256 	int dai_i;
1257 
1258 	dai_node = rsnd_dai_of_node(priv, &is_graph);
1259 	if (is_graph)
1260 		nr = of_graph_get_endpoint_count(dai_node);
1261 	else
1262 		nr = of_get_child_count(dai_node);
1263 
1264 	if (!nr)
1265 		return -EINVAL;
1266 
1267 	rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1268 	rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1269 	if (!rdrv || !rdai)
1270 		return -ENOMEM;
1271 
1272 	priv->rdai_nr	= nr;
1273 	priv->daidrv	= rdrv;
1274 	priv->rdai	= rdai;
1275 
1276 	/*
1277 	 * parse all dai
1278 	 */
1279 	dai_i = 0;
1280 	if (is_graph) {
1281 		for_each_endpoint_of_node(dai_node, dai_np) {
1282 			__rsnd_dai_probe(priv, dai_np, dai_i);
1283 			if (rsnd_is_gen3(priv)) {
1284 				struct rsnd_dai *rdai = rsnd_rdai_get(priv, dai_i);
1285 
1286 				rsnd_parse_connect_graph(priv, &rdai->playback, dai_np);
1287 				rsnd_parse_connect_graph(priv, &rdai->capture,  dai_np);
1288 			}
1289 			dai_i++;
1290 		}
1291 	} else {
1292 		for_each_child_of_node(dai_node, dai_np) {
1293 			__rsnd_dai_probe(priv, dai_np, dai_i);
1294 			if (rsnd_is_gen3(priv)) {
1295 				rsnd_parse_connect_simple(priv, dai_np, dai_i, 1);
1296 				rsnd_parse_connect_simple(priv, dai_np, dai_i, 0);
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 (ex. MIXer case)
1530 	 * 2) re-register if card was rebinded
1531 	 */
1532 	list_for_each_entry(kctrl, &card->controls, list) {
1533 		struct rsnd_kctrl_cfg *c = kctrl->private_data;
1534 
1535 		if (strcmp(kctrl->id.name, name) == 0 &&
1536 		    c->mod == mod)
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 	int err;
1579 
1580 	/*
1581 	 * use Audio-DMAC dev if we can use IPMMU
1582 	 * see
1583 	 *	rsnd_dmaen_attach()
1584 	 */
1585 	if (io->dmac_dev)
1586 		dev = io->dmac_dev;
1587 
1588 	for (substream = rtd->pcm->streams[stream].substream;
1589 	     substream;
1590 	     substream = substream->next) {
1591 		err = snd_pcm_lib_preallocate_pages(substream,
1592 					SNDRV_DMA_TYPE_DEV,
1593 					dev,
1594 					PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1595 		if (err < 0)
1596 			return err;
1597 	}
1598 
1599 	return 0;
1600 }
1601 
1602 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1603 {
1604 	struct snd_soc_dai *dai = rtd->cpu_dai;
1605 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1606 	int ret;
1607 
1608 	ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1609 	if (ret)
1610 		return ret;
1611 
1612 	ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1613 	if (ret)
1614 		return ret;
1615 
1616 	ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1617 				     SNDRV_PCM_STREAM_PLAYBACK);
1618 	if (ret)
1619 		return ret;
1620 
1621 	ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1622 				     SNDRV_PCM_STREAM_CAPTURE);
1623 	if (ret)
1624 		return ret;
1625 
1626 	return 0;
1627 }
1628 
1629 static const struct snd_soc_component_driver rsnd_soc_component = {
1630 	.ops		= &rsnd_pcm_ops,
1631 	.pcm_new	= rsnd_pcm_new,
1632 	.name		= "rsnd",
1633 };
1634 
1635 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1636 				       struct rsnd_dai_stream *io)
1637 {
1638 	int ret;
1639 
1640 	ret = rsnd_dai_call(probe, io, priv);
1641 	if (ret == -EAGAIN) {
1642 		struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1643 		struct rsnd_mod *mod;
1644 		int i;
1645 
1646 		/*
1647 		 * Fallback to PIO mode
1648 		 */
1649 
1650 		/*
1651 		 * call "remove" for SSI/SRC/DVC
1652 		 * SSI will be switch to PIO mode if it was DMA mode
1653 		 * see
1654 		 *	rsnd_dma_init()
1655 		 *	rsnd_ssi_fallback()
1656 		 */
1657 		rsnd_dai_call(remove, io, priv);
1658 
1659 		/*
1660 		 * remove all mod from io
1661 		 * and, re connect ssi
1662 		 */
1663 		for_each_rsnd_mod(i, mod, io)
1664 			rsnd_dai_disconnect(mod, io, i);
1665 		rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1666 
1667 		/*
1668 		 * fallback
1669 		 */
1670 		rsnd_dai_call(fallback, io, priv);
1671 
1672 		/*
1673 		 * retry to "probe".
1674 		 * DAI has SSI which is PIO mode only now.
1675 		 */
1676 		ret = rsnd_dai_call(probe, io, priv);
1677 	}
1678 
1679 	return ret;
1680 }
1681 
1682 /*
1683  *	rsnd probe
1684  */
1685 static int rsnd_probe(struct platform_device *pdev)
1686 {
1687 	struct rsnd_priv *priv;
1688 	struct device *dev = &pdev->dev;
1689 	struct rsnd_dai *rdai;
1690 	int (*probe_func[])(struct rsnd_priv *priv) = {
1691 		rsnd_gen_probe,
1692 		rsnd_dma_probe,
1693 		rsnd_ssi_probe,
1694 		rsnd_ssiu_probe,
1695 		rsnd_src_probe,
1696 		rsnd_ctu_probe,
1697 		rsnd_mix_probe,
1698 		rsnd_dvc_probe,
1699 		rsnd_cmd_probe,
1700 		rsnd_adg_probe,
1701 		rsnd_dai_probe,
1702 	};
1703 	int ret, i;
1704 
1705 	/*
1706 	 *	init priv data
1707 	 */
1708 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1709 	if (!priv)
1710 		return -ENODEV;
1711 
1712 	priv->pdev	= pdev;
1713 	priv->flags	= (unsigned long)of_device_get_match_data(dev);
1714 	spin_lock_init(&priv->lock);
1715 
1716 	/*
1717 	 *	init each module
1718 	 */
1719 	for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1720 		ret = probe_func[i](priv);
1721 		if (ret)
1722 			return ret;
1723 	}
1724 
1725 	for_each_rsnd_dai(rdai, priv, i) {
1726 		ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1727 		if (ret)
1728 			goto exit_snd_probe;
1729 
1730 		ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1731 		if (ret)
1732 			goto exit_snd_probe;
1733 	}
1734 
1735 	dev_set_drvdata(dev, priv);
1736 
1737 	/*
1738 	 *	asoc register
1739 	 */
1740 	ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
1741 					 priv->daidrv, rsnd_rdai_nr(priv));
1742 	if (ret < 0) {
1743 		dev_err(dev, "cannot snd dai register\n");
1744 		goto exit_snd_probe;
1745 	}
1746 
1747 	pm_runtime_enable(dev);
1748 
1749 	dev_info(dev, "probed\n");
1750 	return ret;
1751 
1752 exit_snd_probe:
1753 	for_each_rsnd_dai(rdai, priv, i) {
1754 		rsnd_dai_call(remove, &rdai->playback, priv);
1755 		rsnd_dai_call(remove, &rdai->capture, priv);
1756 	}
1757 
1758 	/*
1759 	 * adg is very special mod which can't use rsnd_dai_call(remove),
1760 	 * and it registers ADG clock on probe.
1761 	 * It should be unregister if probe failed.
1762 	 * Mainly it is assuming -EPROBE_DEFER case
1763 	 */
1764 	rsnd_adg_remove(priv);
1765 
1766 	return ret;
1767 }
1768 
1769 static int rsnd_remove(struct platform_device *pdev)
1770 {
1771 	struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1772 	struct rsnd_dai *rdai;
1773 	void (*remove_func[])(struct rsnd_priv *priv) = {
1774 		rsnd_ssi_remove,
1775 		rsnd_ssiu_remove,
1776 		rsnd_src_remove,
1777 		rsnd_ctu_remove,
1778 		rsnd_mix_remove,
1779 		rsnd_dvc_remove,
1780 		rsnd_cmd_remove,
1781 		rsnd_adg_remove,
1782 	};
1783 	int ret = 0, i;
1784 
1785 	snd_soc_disconnect_sync(&pdev->dev);
1786 
1787 	pm_runtime_disable(&pdev->dev);
1788 
1789 	for_each_rsnd_dai(rdai, priv, i) {
1790 		ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1791 		ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1792 	}
1793 
1794 	for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1795 		remove_func[i](priv);
1796 
1797 	return ret;
1798 }
1799 
1800 static int __maybe_unused rsnd_suspend(struct device *dev)
1801 {
1802 	struct rsnd_priv *priv = dev_get_drvdata(dev);
1803 
1804 	rsnd_adg_clk_disable(priv);
1805 
1806 	return 0;
1807 }
1808 
1809 static int __maybe_unused rsnd_resume(struct device *dev)
1810 {
1811 	struct rsnd_priv *priv = dev_get_drvdata(dev);
1812 
1813 	rsnd_adg_clk_enable(priv);
1814 
1815 	return 0;
1816 }
1817 
1818 static const struct dev_pm_ops rsnd_pm_ops = {
1819 	SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
1820 };
1821 
1822 static struct platform_driver rsnd_driver = {
1823 	.driver	= {
1824 		.name	= "rcar_sound",
1825 		.pm	= &rsnd_pm_ops,
1826 		.of_match_table = rsnd_of_match,
1827 	},
1828 	.probe		= rsnd_probe,
1829 	.remove		= rsnd_remove,
1830 };
1831 module_platform_driver(rsnd_driver);
1832 
1833 MODULE_LICENSE("GPL v2");
1834 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1835 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1836 MODULE_ALIAS("platform:rcar-pcm-audio");
1837