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