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