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