xref: /openbmc/linux/sound/soc/sh/rcar/core.c (revision e2afb7de)
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 <linux/shdma-base.h>
98 #include "rsnd.h"
99 
100 #define RSND_RATES SNDRV_PCM_RATE_8000_96000
101 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
102 
103 static struct rsnd_of_data rsnd_of_data_gen1 = {
104 	.flags = RSND_GEN1,
105 };
106 
107 static struct rsnd_of_data rsnd_of_data_gen2 = {
108 	.flags = RSND_GEN2,
109 };
110 
111 static struct of_device_id rsnd_of_match[] = {
112 	{ .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 },
113 	{ .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 },
114 	{},
115 };
116 MODULE_DEVICE_TABLE(of, rsnd_of_match);
117 
118 /*
119  *	rsnd_platform functions
120  */
121 #define rsnd_platform_call(priv, dai, func, param...)	\
122 	(!(priv->info->func) ? 0 :		\
123 	 priv->info->func(param))
124 
125 #define rsnd_is_enable_path(io, name) \
126 	((io)->info ? (io)->info->name : NULL)
127 #define rsnd_info_id(priv, io, name) \
128 	((io)->info->name - priv->info->name##_info)
129 
130 /*
131  *	rsnd_mod functions
132  */
133 char *rsnd_mod_name(struct rsnd_mod *mod)
134 {
135 	if (!mod || !mod->ops)
136 		return "unknown";
137 
138 	return mod->ops->name;
139 }
140 
141 char *rsnd_mod_dma_name(struct rsnd_mod *mod)
142 {
143 	if (!mod || !mod->ops)
144 		return "unknown";
145 
146 	if (!mod->ops->dma_name)
147 		return mod->ops->name;
148 
149 	return mod->ops->dma_name(mod);
150 }
151 
152 void rsnd_mod_init(struct rsnd_priv *priv,
153 		   struct rsnd_mod *mod,
154 		   struct rsnd_mod_ops *ops,
155 		   enum rsnd_mod_type type,
156 		   int id)
157 {
158 	mod->priv	= priv;
159 	mod->id		= id;
160 	mod->ops	= ops;
161 	mod->type	= type;
162 }
163 
164 /*
165  *	rsnd_dma functions
166  */
167 void rsnd_dma_stop(struct rsnd_dma *dma)
168 {
169 	dmaengine_terminate_all(dma->chan);
170 }
171 
172 static void rsnd_dma_complete(void *data)
173 {
174 	struct rsnd_dma *dma = (struct rsnd_dma *)data;
175 	struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
176 	struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
177 
178 	/*
179 	 * Renesas sound Gen1 needs 1 DMAC,
180 	 * Gen2 needs 2 DMAC.
181 	 * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
182 	 * But, Audio-DMAC-peri-peri doesn't have interrupt,
183 	 * and this driver is assuming that here.
184 	 *
185 	 * If Audio-DMAC-peri-peri has interrpt,
186 	 * rsnd_dai_pointer_update() will be called twice,
187 	 * ant it will breaks io->byte_pos
188 	 */
189 
190 	rsnd_dai_pointer_update(io, io->byte_per_period);
191 }
192 
193 void rsnd_dma_start(struct rsnd_dma *dma)
194 {
195 	struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
196 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
197 	struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
198 	struct snd_pcm_substream *substream = io->substream;
199 	struct device *dev = rsnd_priv_to_dev(priv);
200 	struct dma_async_tx_descriptor *desc;
201 
202 	desc = dmaengine_prep_dma_cyclic(dma->chan,
203 					 (dma->addr) ? dma->addr :
204 					 substream->runtime->dma_addr,
205 					 snd_pcm_lib_buffer_bytes(substream),
206 					 snd_pcm_lib_period_bytes(substream),
207 					 dma->dir,
208 					 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
209 
210 	if (!desc) {
211 		dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
212 		return;
213 	}
214 
215 	desc->callback		= rsnd_dma_complete;
216 	desc->callback_param	= dma;
217 
218 	if (dmaengine_submit(desc) < 0) {
219 		dev_err(dev, "dmaengine_submit() fail\n");
220 		return;
221 	}
222 
223 	dma_async_issue_pending(dma->chan);
224 }
225 
226 int rsnd_dma_available(struct rsnd_dma *dma)
227 {
228 	return !!dma->chan;
229 }
230 
231 #define DMA_NAME_SIZE 16
232 #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
233 static int _rsnd_dma_of_name(char *dma_name, struct rsnd_mod *mod)
234 {
235 	if (mod)
236 		return snprintf(dma_name, DMA_NAME_SIZE / 2, "%s%d",
237 			 rsnd_mod_dma_name(mod), rsnd_mod_id(mod));
238 	else
239 		return snprintf(dma_name, DMA_NAME_SIZE / 2, "mem");
240 
241 }
242 
243 static void rsnd_dma_of_name(struct rsnd_mod *mod_from,
244 			     struct rsnd_mod *mod_to,
245 			     char *dma_name)
246 {
247 	int index = 0;
248 
249 	index = _rsnd_dma_of_name(dma_name + index, mod_from);
250 	*(dma_name + index++) = '_';
251 	index = _rsnd_dma_of_name(dma_name + index, mod_to);
252 }
253 
254 static void rsnd_dma_of_path(struct rsnd_dma *dma,
255 			     int is_play,
256 			     struct rsnd_mod **mod_from,
257 			     struct rsnd_mod **mod_to)
258 {
259 	struct rsnd_mod *this = rsnd_dma_to_mod(dma);
260 	struct rsnd_dai_stream *io = rsnd_mod_to_io(this);
261 	struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
262 	struct rsnd_mod *src = rsnd_io_to_mod_src(io);
263 	struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
264 	struct rsnd_mod *mod[MOD_MAX];
265 	int i, index;
266 
267 
268 	for (i = 0; i < MOD_MAX; i++)
269 		mod[i] = NULL;
270 
271 	/*
272 	 * in play case...
273 	 *
274 	 * src -> dst
275 	 *
276 	 * mem -> SSI
277 	 * mem -> SRC -> SSI
278 	 * mem -> SRC -> DVC -> SSI
279 	 */
280 	mod[0] = NULL; /* for "mem" */
281 	index = 1;
282 	for (i = 1; i < MOD_MAX; i++) {
283 		if (!src) {
284 			mod[i] = ssi;
285 		} else if (!dvc) {
286 			mod[i] = src;
287 			src = NULL;
288 		} else {
289 			if ((!is_play) && (this == src))
290 				this = dvc;
291 
292 			mod[i] = (is_play) ? src : dvc;
293 			i++;
294 			mod[i] = (is_play) ? dvc : src;
295 			src = NULL;
296 			dvc = NULL;
297 		}
298 
299 		if (mod[i] == this)
300 			index = i;
301 
302 		if (mod[i] == ssi)
303 			break;
304 	}
305 
306 	if (is_play) {
307 		*mod_from = mod[index - 1];
308 		*mod_to   = mod[index];
309 	} else {
310 		*mod_from = mod[index];
311 		*mod_to   = mod[index - 1];
312 	}
313 }
314 
315 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
316 		  int is_play, int id)
317 {
318 	struct device *dev = rsnd_priv_to_dev(priv);
319 	struct dma_slave_config cfg;
320 	struct rsnd_mod *mod_from;
321 	struct rsnd_mod *mod_to;
322 	char dma_name[DMA_NAME_SIZE];
323 	dma_cap_mask_t mask;
324 	int ret;
325 
326 	if (dma->chan) {
327 		dev_err(dev, "it already has dma channel\n");
328 		return -EIO;
329 	}
330 
331 	dma_cap_zero(mask);
332 	dma_cap_set(DMA_SLAVE, mask);
333 
334 	rsnd_dma_of_path(dma, is_play, &mod_from, &mod_to);
335 	rsnd_dma_of_name(mod_from, mod_to, dma_name);
336 
337 	cfg.slave_id	= id;
338 	cfg.direction	= is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
339 	cfg.src_addr	= rsnd_gen_dma_addr(priv, mod_from, is_play, 1);
340 	cfg.dst_addr	= rsnd_gen_dma_addr(priv, mod_to,   is_play, 0);
341 	cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
342 	cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
343 
344 	dev_dbg(dev, "dma : %s %pad -> %pad\n",
345 		dma_name, &cfg.src_addr, &cfg.dst_addr);
346 
347 	dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
348 						     (void *)id, dev,
349 						     dma_name);
350 	if (!dma->chan) {
351 		dev_err(dev, "can't get dma channel\n");
352 		return -EIO;
353 	}
354 
355 	ret = dmaengine_slave_config(dma->chan, &cfg);
356 	if (ret < 0)
357 		goto rsnd_dma_init_err;
358 
359 	dma->addr = is_play ? cfg.src_addr : cfg.dst_addr;
360 	dma->dir = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
361 
362 	return 0;
363 
364 rsnd_dma_init_err:
365 	rsnd_dma_quit(priv, dma);
366 
367 	return ret;
368 }
369 
370 void  rsnd_dma_quit(struct rsnd_priv *priv,
371 		    struct rsnd_dma *dma)
372 {
373 	if (dma->chan)
374 		dma_release_channel(dma->chan);
375 
376 	dma->chan = NULL;
377 }
378 
379 /*
380  *	settting function
381  */
382 u32 rsnd_get_adinr(struct rsnd_mod *mod)
383 {
384 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
385 	struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
386 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
387 	struct device *dev = rsnd_priv_to_dev(priv);
388 	u32 adinr = runtime->channels;
389 
390 	switch (runtime->sample_bits) {
391 	case 16:
392 		adinr |= (8 << 16);
393 		break;
394 	case 32:
395 		adinr |= (0 << 16);
396 		break;
397 	default:
398 		dev_warn(dev, "not supported sample bits\n");
399 		return 0;
400 	}
401 
402 	return adinr;
403 }
404 
405 /*
406  *	rsnd_dai functions
407  */
408 #define __rsnd_mod_call(mod, func, rdai...)			\
409 ({								\
410 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);		\
411 	struct device *dev = rsnd_priv_to_dev(priv);		\
412 	dev_dbg(dev, "%s [%d] %s\n",				\
413 		rsnd_mod_name(mod), rsnd_mod_id(mod), #func);	\
414 	(mod)->ops->func(mod, rdai);				\
415 })
416 
417 #define rsnd_mod_call(mod, func, rdai...)	\
418 	(!(mod) ? -ENODEV :			\
419 	 !((mod)->ops->func) ? 0 :		\
420 	 __rsnd_mod_call(mod, func, rdai))
421 
422 #define rsnd_dai_call(fn, io, rdai...)				\
423 ({								\
424 	struct rsnd_mod *mod;					\
425 	int ret = 0, i;						\
426 	for (i = 0; i < RSND_MOD_MAX; i++) {			\
427 		mod = (io)->mod[i];				\
428 		if (!mod)					\
429 			continue;				\
430 		ret = rsnd_mod_call(mod, fn, rdai);		\
431 		if (ret < 0)					\
432 			break;					\
433 	}							\
434 	ret;							\
435 })
436 
437 static int rsnd_dai_connect(struct rsnd_mod *mod,
438 			    struct rsnd_dai_stream *io)
439 {
440 	if (!mod)
441 		return -EIO;
442 
443 	if (io->mod[mod->type]) {
444 		struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
445 		struct device *dev = rsnd_priv_to_dev(priv);
446 
447 		dev_err(dev, "%s%d is not empty\n",
448 			rsnd_mod_name(mod),
449 			rsnd_mod_id(mod));
450 		return -EIO;
451 	}
452 
453 	io->mod[mod->type] = mod;
454 	mod->io = io;
455 
456 	return 0;
457 }
458 
459 int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
460 {
461 	int id = rdai - priv->rdai;
462 
463 	if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
464 		return -EINVAL;
465 
466 	return id;
467 }
468 
469 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
470 {
471 	if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
472 		return NULL;
473 
474 	return priv->rdai + id;
475 }
476 
477 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
478 {
479 	struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
480 
481 	return rsnd_dai_get(priv, dai->id);
482 }
483 
484 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
485 {
486 	return &rdai->playback == io;
487 }
488 
489 /*
490  *	rsnd_soc_dai functions
491  */
492 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
493 {
494 	struct snd_pcm_substream *substream = io->substream;
495 	struct snd_pcm_runtime *runtime = substream->runtime;
496 	int pos = io->byte_pos + additional;
497 
498 	pos %= (runtime->periods * io->byte_per_period);
499 
500 	return pos;
501 }
502 
503 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
504 {
505 	io->byte_pos += byte;
506 
507 	if (io->byte_pos >= io->next_period_byte) {
508 		struct snd_pcm_substream *substream = io->substream;
509 		struct snd_pcm_runtime *runtime = substream->runtime;
510 
511 		io->period_pos++;
512 		io->next_period_byte += io->byte_per_period;
513 
514 		if (io->period_pos >= runtime->periods) {
515 			io->byte_pos = 0;
516 			io->period_pos = 0;
517 			io->next_period_byte = io->byte_per_period;
518 		}
519 
520 		snd_pcm_period_elapsed(substream);
521 	}
522 }
523 
524 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
525 				struct snd_pcm_substream *substream)
526 {
527 	struct snd_pcm_runtime *runtime = substream->runtime;
528 
529 	io->substream		= substream;
530 	io->byte_pos		= 0;
531 	io->period_pos		= 0;
532 	io->byte_per_period	= runtime->period_size *
533 				  runtime->channels *
534 				  samples_to_bytes(runtime, 1);
535 	io->next_period_byte	= io->byte_per_period;
536 
537 	return 0;
538 }
539 
540 static
541 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
542 {
543 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
544 
545 	return  rtd->cpu_dai;
546 }
547 
548 static
549 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
550 					struct snd_pcm_substream *substream)
551 {
552 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
553 		return &rdai->playback;
554 	else
555 		return &rdai->capture;
556 }
557 
558 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
559 			    struct snd_soc_dai *dai)
560 {
561 	struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
562 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
563 	struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
564 	int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
565 	int ret;
566 	unsigned long flags;
567 
568 	rsnd_lock(priv, flags);
569 
570 	switch (cmd) {
571 	case SNDRV_PCM_TRIGGER_START:
572 		ret = rsnd_dai_stream_init(io, substream);
573 		if (ret < 0)
574 			goto dai_trigger_end;
575 
576 		ret = rsnd_platform_call(priv, dai, start, ssi_id);
577 		if (ret < 0)
578 			goto dai_trigger_end;
579 
580 		ret = rsnd_dai_call(init, io, rdai);
581 		if (ret < 0)
582 			goto dai_trigger_end;
583 
584 		ret = rsnd_dai_call(start, io, rdai);
585 		if (ret < 0)
586 			goto dai_trigger_end;
587 		break;
588 	case SNDRV_PCM_TRIGGER_STOP:
589 		ret = rsnd_dai_call(stop, io, rdai);
590 		if (ret < 0)
591 			goto dai_trigger_end;
592 
593 		ret = rsnd_dai_call(quit, io, rdai);
594 		if (ret < 0)
595 			goto dai_trigger_end;
596 
597 		ret = rsnd_platform_call(priv, dai, stop, ssi_id);
598 		if (ret < 0)
599 			goto dai_trigger_end;
600 		break;
601 	default:
602 		ret = -EINVAL;
603 	}
604 
605 dai_trigger_end:
606 	rsnd_unlock(priv, flags);
607 
608 	return ret;
609 }
610 
611 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
612 {
613 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
614 
615 	/* set master/slave audio interface */
616 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
617 	case SND_SOC_DAIFMT_CBM_CFM:
618 		rdai->clk_master = 0;
619 		break;
620 	case SND_SOC_DAIFMT_CBS_CFS:
621 		rdai->clk_master = 1; /* codec is slave, cpu is master */
622 		break;
623 	default:
624 		return -EINVAL;
625 	}
626 
627 	/* set format */
628 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
629 	case SND_SOC_DAIFMT_I2S:
630 		rdai->sys_delay = 0;
631 		rdai->data_alignment = 0;
632 		rdai->frm_clk_inv = 0;
633 		break;
634 	case SND_SOC_DAIFMT_LEFT_J:
635 		rdai->sys_delay = 1;
636 		rdai->data_alignment = 0;
637 		rdai->frm_clk_inv = 1;
638 		break;
639 	case SND_SOC_DAIFMT_RIGHT_J:
640 		rdai->sys_delay = 1;
641 		rdai->data_alignment = 1;
642 		rdai->frm_clk_inv = 1;
643 		break;
644 	}
645 
646 	/* set clock inversion */
647 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
648 	case SND_SOC_DAIFMT_NB_IF:
649 		rdai->bit_clk_inv =  rdai->bit_clk_inv;
650 		rdai->frm_clk_inv = !rdai->frm_clk_inv;
651 		break;
652 	case SND_SOC_DAIFMT_IB_NF:
653 		rdai->bit_clk_inv = !rdai->bit_clk_inv;
654 		rdai->frm_clk_inv =  rdai->frm_clk_inv;
655 		break;
656 	case SND_SOC_DAIFMT_IB_IF:
657 		rdai->bit_clk_inv = !rdai->bit_clk_inv;
658 		rdai->frm_clk_inv = !rdai->frm_clk_inv;
659 		break;
660 	case SND_SOC_DAIFMT_NB_NF:
661 	default:
662 		break;
663 	}
664 
665 	return 0;
666 }
667 
668 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
669 	.trigger	= rsnd_soc_dai_trigger,
670 	.set_fmt	= rsnd_soc_dai_set_fmt,
671 };
672 
673 #define rsnd_path_parse(priv, io, type)				\
674 ({								\
675 	struct rsnd_mod *mod;					\
676 	int ret = 0;						\
677 	int id = -1;						\
678 								\
679 	if (rsnd_is_enable_path(io, type)) {			\
680 		id = rsnd_info_id(priv, io, type);		\
681 		if (id >= 0) {					\
682 			mod = rsnd_##type##_mod_get(priv, id);	\
683 			ret = rsnd_dai_connect(mod, io);	\
684 		}						\
685 	}							\
686 	ret;							\
687 })
688 
689 static int rsnd_path_init(struct rsnd_priv *priv,
690 			  struct rsnd_dai *rdai,
691 			  struct rsnd_dai_stream *io)
692 {
693 	int ret;
694 
695 	/*
696 	 * Gen1 is created by SRU/SSI, and this SRU is base module of
697 	 * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
698 	 *
699 	 * Easy image is..
700 	 *	Gen1 SRU = Gen2 SCU + SSIU + etc
701 	 *
702 	 * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
703 	 * using fixed path.
704 	 */
705 
706 	/* SRC */
707 	ret = rsnd_path_parse(priv, io, src);
708 	if (ret < 0)
709 		return ret;
710 
711 	/* SSI */
712 	ret = rsnd_path_parse(priv, io, ssi);
713 	if (ret < 0)
714 		return ret;
715 
716 	/* DVC */
717 	ret = rsnd_path_parse(priv, io, dvc);
718 	if (ret < 0)
719 		return ret;
720 
721 	return ret;
722 }
723 
724 static void rsnd_of_parse_dai(struct platform_device *pdev,
725 			      const struct rsnd_of_data *of_data,
726 			      struct rsnd_priv *priv)
727 {
728 	struct device_node *dai_node,	*dai_np;
729 	struct device_node *ssi_node,	*ssi_np;
730 	struct device_node *src_node,	*src_np;
731 	struct device_node *dvc_node,	*dvc_np;
732 	struct device_node *playback, *capture;
733 	struct rsnd_dai_platform_info *dai_info;
734 	struct rcar_snd_info *info = rsnd_priv_to_info(priv);
735 	struct device *dev = &pdev->dev;
736 	int nr, i;
737 	int dai_i, ssi_i, src_i, dvc_i;
738 
739 	if (!of_data)
740 		return;
741 
742 	dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
743 	if (!dai_node)
744 		return;
745 
746 	nr = of_get_child_count(dai_node);
747 	if (!nr)
748 		return;
749 
750 	dai_info = devm_kzalloc(dev,
751 				sizeof(struct rsnd_dai_platform_info) * nr,
752 				GFP_KERNEL);
753 	if (!dai_info) {
754 		dev_err(dev, "dai info allocation error\n");
755 		return;
756 	}
757 
758 	info->dai_info_nr	= nr;
759 	info->dai_info		= dai_info;
760 
761 	ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
762 	src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
763 	dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
764 
765 #define mod_parse(name)							\
766 if (name##_node) {							\
767 	struct rsnd_##name##_platform_info *name##_info;		\
768 									\
769 	name##_i = 0;							\
770 	for_each_child_of_node(name##_node, name##_np) {		\
771 		name##_info = info->name##_info + name##_i;		\
772 									\
773 		if (name##_np == playback)				\
774 			dai_info->playback.name = name##_info;		\
775 		if (name##_np == capture)				\
776 			dai_info->capture.name = name##_info;		\
777 									\
778 		name##_i++;						\
779 	}								\
780 }
781 
782 	/*
783 	 * parse all dai
784 	 */
785 	dai_i = 0;
786 	for_each_child_of_node(dai_node, dai_np) {
787 		dai_info = info->dai_info + dai_i;
788 
789 		for (i = 0;; i++) {
790 
791 			playback = of_parse_phandle(dai_np, "playback", i);
792 			capture  = of_parse_phandle(dai_np, "capture", i);
793 
794 			if (!playback && !capture)
795 				break;
796 
797 			mod_parse(ssi);
798 			mod_parse(src);
799 			mod_parse(dvc);
800 
801 			of_node_put(playback);
802 			of_node_put(capture);
803 		}
804 
805 		dai_i++;
806 	}
807 }
808 
809 static int rsnd_dai_probe(struct platform_device *pdev,
810 			  const struct rsnd_of_data *of_data,
811 			  struct rsnd_priv *priv)
812 {
813 	struct snd_soc_dai_driver *drv;
814 	struct rcar_snd_info *info = rsnd_priv_to_info(priv);
815 	struct rsnd_dai *rdai;
816 	struct rsnd_ssi_platform_info *pmod, *cmod;
817 	struct device *dev = rsnd_priv_to_dev(priv);
818 	int dai_nr;
819 	int i;
820 
821 	rsnd_of_parse_dai(pdev, of_data, priv);
822 
823 	dai_nr = info->dai_info_nr;
824 	if (!dai_nr) {
825 		dev_err(dev, "no dai\n");
826 		return -EIO;
827 	}
828 
829 	drv  = devm_kzalloc(dev, sizeof(*drv)  * dai_nr, GFP_KERNEL);
830 	rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
831 	if (!drv || !rdai) {
832 		dev_err(dev, "dai allocate failed\n");
833 		return -ENOMEM;
834 	}
835 
836 	priv->rdai_nr	= dai_nr;
837 	priv->daidrv	= drv;
838 	priv->rdai	= rdai;
839 
840 	for (i = 0; i < dai_nr; i++) {
841 		rdai[i].info = &info->dai_info[i];
842 
843 		pmod = rdai[i].info->playback.ssi;
844 		cmod = rdai[i].info->capture.ssi;
845 
846 		/*
847 		 *	init rsnd_dai
848 		 */
849 		snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
850 
851 		/*
852 		 *	init snd_soc_dai_driver
853 		 */
854 		drv[i].name	= rdai[i].name;
855 		drv[i].ops	= &rsnd_soc_dai_ops;
856 		if (pmod) {
857 			drv[i].playback.rates		= RSND_RATES;
858 			drv[i].playback.formats		= RSND_FMTS;
859 			drv[i].playback.channels_min	= 2;
860 			drv[i].playback.channels_max	= 2;
861 
862 			rdai[i].playback.info = &info->dai_info[i].playback;
863 			rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
864 		}
865 		if (cmod) {
866 			drv[i].capture.rates		= RSND_RATES;
867 			drv[i].capture.formats		= RSND_FMTS;
868 			drv[i].capture.channels_min	= 2;
869 			drv[i].capture.channels_max	= 2;
870 
871 			rdai[i].capture.info = &info->dai_info[i].capture;
872 			rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
873 		}
874 
875 		dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
876 			pmod ? "play"    : " -- ",
877 			cmod ? "capture" : "  --   ");
878 	}
879 
880 	return 0;
881 }
882 
883 /*
884  *		pcm ops
885  */
886 static struct snd_pcm_hardware rsnd_pcm_hardware = {
887 	.info =		SNDRV_PCM_INFO_INTERLEAVED	|
888 			SNDRV_PCM_INFO_MMAP		|
889 			SNDRV_PCM_INFO_MMAP_VALID,
890 	.buffer_bytes_max	= 64 * 1024,
891 	.period_bytes_min	= 32,
892 	.period_bytes_max	= 8192,
893 	.periods_min		= 1,
894 	.periods_max		= 32,
895 	.fifo_size		= 256,
896 };
897 
898 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
899 {
900 	struct snd_pcm_runtime *runtime = substream->runtime;
901 	int ret = 0;
902 
903 	snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
904 
905 	ret = snd_pcm_hw_constraint_integer(runtime,
906 					    SNDRV_PCM_HW_PARAM_PERIODS);
907 
908 	return ret;
909 }
910 
911 static int rsnd_hw_params(struct snd_pcm_substream *substream,
912 			 struct snd_pcm_hw_params *hw_params)
913 {
914 	return snd_pcm_lib_malloc_pages(substream,
915 					params_buffer_bytes(hw_params));
916 }
917 
918 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
919 {
920 	struct snd_pcm_runtime *runtime = substream->runtime;
921 	struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
922 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
923 	struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
924 
925 	return bytes_to_frames(runtime, io->byte_pos);
926 }
927 
928 static struct snd_pcm_ops rsnd_pcm_ops = {
929 	.open		= rsnd_pcm_open,
930 	.ioctl		= snd_pcm_lib_ioctl,
931 	.hw_params	= rsnd_hw_params,
932 	.hw_free	= snd_pcm_lib_free_pages,
933 	.pointer	= rsnd_pointer,
934 };
935 
936 /*
937  *		snd_soc_platform
938  */
939 
940 #define PREALLOC_BUFFER		(32 * 1024)
941 #define PREALLOC_BUFFER_MAX	(32 * 1024)
942 
943 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
944 {
945 	struct snd_soc_dai *dai = rtd->cpu_dai;
946 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
947 	int ret;
948 
949 	ret = rsnd_dai_call(pcm_new, &rdai->playback, rdai, rtd);
950 	if (ret)
951 		return ret;
952 
953 	ret = rsnd_dai_call(pcm_new, &rdai->capture, rdai, rtd);
954 	if (ret)
955 		return ret;
956 
957 	return snd_pcm_lib_preallocate_pages_for_all(
958 		rtd->pcm,
959 		SNDRV_DMA_TYPE_DEV,
960 		rtd->card->snd_card->dev,
961 		PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
962 }
963 
964 static void rsnd_pcm_free(struct snd_pcm *pcm)
965 {
966 	snd_pcm_lib_preallocate_free_for_all(pcm);
967 }
968 
969 static struct snd_soc_platform_driver rsnd_soc_platform = {
970 	.ops		= &rsnd_pcm_ops,
971 	.pcm_new	= rsnd_pcm_new,
972 	.pcm_free	= rsnd_pcm_free,
973 };
974 
975 static const struct snd_soc_component_driver rsnd_soc_component = {
976 	.name		= "rsnd",
977 };
978 
979 /*
980  *	rsnd probe
981  */
982 static int rsnd_probe(struct platform_device *pdev)
983 {
984 	struct rcar_snd_info *info;
985 	struct rsnd_priv *priv;
986 	struct device *dev = &pdev->dev;
987 	struct rsnd_dai *rdai;
988 	const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
989 	const struct rsnd_of_data *of_data;
990 	int (*probe_func[])(struct platform_device *pdev,
991 			    const struct rsnd_of_data *of_data,
992 			    struct rsnd_priv *priv) = {
993 		rsnd_gen_probe,
994 		rsnd_ssi_probe,
995 		rsnd_src_probe,
996 		rsnd_dvc_probe,
997 		rsnd_adg_probe,
998 		rsnd_dai_probe,
999 	};
1000 	int ret, i;
1001 
1002 	info = NULL;
1003 	of_data = NULL;
1004 	if (of_id) {
1005 		info = devm_kzalloc(&pdev->dev,
1006 				    sizeof(struct rcar_snd_info), GFP_KERNEL);
1007 		of_data = of_id->data;
1008 	} else {
1009 		info = pdev->dev.platform_data;
1010 	}
1011 
1012 	if (!info) {
1013 		dev_err(dev, "driver needs R-Car sound information\n");
1014 		return -ENODEV;
1015 	}
1016 
1017 	/*
1018 	 *	init priv data
1019 	 */
1020 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1021 	if (!priv) {
1022 		dev_err(dev, "priv allocate failed\n");
1023 		return -ENODEV;
1024 	}
1025 
1026 	priv->pdev	= pdev;
1027 	priv->info	= info;
1028 	spin_lock_init(&priv->lock);
1029 
1030 	/*
1031 	 *	init each module
1032 	 */
1033 	for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1034 		ret = probe_func[i](pdev, of_data, priv);
1035 		if (ret)
1036 			return ret;
1037 	}
1038 
1039 	for_each_rsnd_dai(rdai, priv, i) {
1040 		ret = rsnd_dai_call(probe, &rdai->playback, rdai);
1041 		if (ret)
1042 			goto exit_snd_probe;
1043 
1044 		ret = rsnd_dai_call(probe, &rdai->capture, rdai);
1045 		if (ret)
1046 			goto exit_snd_probe;
1047 	}
1048 
1049 	/*
1050 	 *	asoc register
1051 	 */
1052 	ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1053 	if (ret < 0) {
1054 		dev_err(dev, "cannot snd soc register\n");
1055 		return ret;
1056 	}
1057 
1058 	ret = snd_soc_register_component(dev, &rsnd_soc_component,
1059 					 priv->daidrv, rsnd_rdai_nr(priv));
1060 	if (ret < 0) {
1061 		dev_err(dev, "cannot snd dai register\n");
1062 		goto exit_snd_soc;
1063 	}
1064 
1065 	dev_set_drvdata(dev, priv);
1066 
1067 	pm_runtime_enable(dev);
1068 
1069 	dev_info(dev, "probed\n");
1070 	return ret;
1071 
1072 exit_snd_soc:
1073 	snd_soc_unregister_platform(dev);
1074 exit_snd_probe:
1075 	for_each_rsnd_dai(rdai, priv, i) {
1076 		rsnd_dai_call(remove, &rdai->playback, rdai);
1077 		rsnd_dai_call(remove, &rdai->capture, rdai);
1078 	}
1079 
1080 	return ret;
1081 }
1082 
1083 static int rsnd_remove(struct platform_device *pdev)
1084 {
1085 	struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1086 	struct rsnd_dai *rdai;
1087 	int ret = 0, i;
1088 
1089 	pm_runtime_disable(&pdev->dev);
1090 
1091 	for_each_rsnd_dai(rdai, priv, i) {
1092 		ret |= rsnd_dai_call(remove, &rdai->playback, rdai);
1093 		ret |= rsnd_dai_call(remove, &rdai->capture, rdai);
1094 	}
1095 
1096 	return ret;
1097 }
1098 
1099 static struct platform_driver rsnd_driver = {
1100 	.driver	= {
1101 		.name	= "rcar_sound",
1102 		.of_match_table = rsnd_of_match,
1103 	},
1104 	.probe		= rsnd_probe,
1105 	.remove		= rsnd_remove,
1106 };
1107 module_platform_driver(rsnd_driver);
1108 
1109 MODULE_LICENSE("GPL");
1110 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1111 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1112 MODULE_ALIAS("platform:rcar-pcm-audio");
1113