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