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