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