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