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