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