xref: /openbmc/linux/sound/soc/sh/rcar/core.c (revision 089a49b6)
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 scu
77  *   |
78  *   +- scu
79  *      |
80  *      +- scu[0]
81  *      +- scu[1]
82  *      +- scu[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_96000
100 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
101 
102 /*
103  *	rsnd_platform functions
104  */
105 #define rsnd_platform_call(priv, dai, func, param...)	\
106 	(!(priv->info->func) ? -ENODEV :		\
107 	 priv->info->func(param))
108 
109 
110 /*
111  *	basic function
112  */
113 u32 rsnd_read(struct rsnd_priv *priv,
114 	      struct rsnd_mod *mod, enum rsnd_reg reg)
115 {
116 	void __iomem *base = rsnd_gen_reg_get(priv, mod, reg);
117 
118 	BUG_ON(!base);
119 
120 	return ioread32(base);
121 }
122 
123 void rsnd_write(struct rsnd_priv *priv,
124 		struct rsnd_mod *mod,
125 		enum rsnd_reg reg, u32 data)
126 {
127 	void __iomem *base = rsnd_gen_reg_get(priv, mod, reg);
128 	struct device *dev = rsnd_priv_to_dev(priv);
129 
130 	BUG_ON(!base);
131 
132 	dev_dbg(dev, "w %p : %08x\n", base, data);
133 
134 	iowrite32(data, base);
135 }
136 
137 void rsnd_bset(struct rsnd_priv *priv, struct rsnd_mod *mod,
138 	       enum rsnd_reg reg, u32 mask, u32 data)
139 {
140 	void __iomem *base = rsnd_gen_reg_get(priv, mod, reg);
141 	struct device *dev = rsnd_priv_to_dev(priv);
142 	u32 val;
143 
144 	BUG_ON(!base);
145 
146 	val = ioread32(base);
147 	val &= ~mask;
148 	val |= data & mask;
149 	iowrite32(val, base);
150 
151 	dev_dbg(dev, "s %p : %08x\n", base, val);
152 }
153 
154 /*
155  *	rsnd_mod functions
156  */
157 char *rsnd_mod_name(struct rsnd_mod *mod)
158 {
159 	if (!mod || !mod->ops)
160 		return "unknown";
161 
162 	return mod->ops->name;
163 }
164 
165 void rsnd_mod_init(struct rsnd_priv *priv,
166 		   struct rsnd_mod *mod,
167 		   struct rsnd_mod_ops *ops,
168 		   int id)
169 {
170 	mod->priv	= priv;
171 	mod->id		= id;
172 	mod->ops	= ops;
173 	INIT_LIST_HEAD(&mod->list);
174 }
175 
176 /*
177  *	rsnd_dma functions
178  */
179 static void rsnd_dma_continue(struct rsnd_dma *dma)
180 {
181 	/* push next A or B plane */
182 	dma->submit_loop = 1;
183 	schedule_work(&dma->work);
184 }
185 
186 void rsnd_dma_start(struct rsnd_dma *dma)
187 {
188 	/* push both A and B plane*/
189 	dma->submit_loop = 2;
190 	schedule_work(&dma->work);
191 }
192 
193 void rsnd_dma_stop(struct rsnd_dma *dma)
194 {
195 	dma->submit_loop = 0;
196 	cancel_work_sync(&dma->work);
197 	dmaengine_terminate_all(dma->chan);
198 }
199 
200 static void rsnd_dma_complete(void *data)
201 {
202 	struct rsnd_dma *dma = (struct rsnd_dma *)data;
203 	struct rsnd_priv *priv = dma->priv;
204 	unsigned long flags;
205 
206 	rsnd_lock(priv, flags);
207 
208 	dma->complete(dma);
209 
210 	if (dma->submit_loop)
211 		rsnd_dma_continue(dma);
212 
213 	rsnd_unlock(priv, flags);
214 }
215 
216 static void rsnd_dma_do_work(struct work_struct *work)
217 {
218 	struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work);
219 	struct rsnd_priv *priv = dma->priv;
220 	struct device *dev = rsnd_priv_to_dev(priv);
221 	struct dma_async_tx_descriptor *desc;
222 	dma_addr_t buf;
223 	size_t len;
224 	int i;
225 
226 	for (i = 0; i < dma->submit_loop; i++) {
227 
228 		if (dma->inquiry(dma, &buf, &len) < 0)
229 			return;
230 
231 		desc = dmaengine_prep_slave_single(
232 			dma->chan, buf, len, dma->dir,
233 			DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
234 		if (!desc) {
235 			dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
236 			return;
237 		}
238 
239 		desc->callback		= rsnd_dma_complete;
240 		desc->callback_param	= dma;
241 
242 		if (dmaengine_submit(desc) < 0) {
243 			dev_err(dev, "dmaengine_submit() fail\n");
244 			return;
245 		}
246 
247 	}
248 
249 	dma_async_issue_pending(dma->chan);
250 }
251 
252 int rsnd_dma_available(struct rsnd_dma *dma)
253 {
254 	return !!dma->chan;
255 }
256 
257 static bool rsnd_dma_filter(struct dma_chan *chan, void *param)
258 {
259 	chan->private = param;
260 
261 	return true;
262 }
263 
264 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
265 		  int is_play, int id,
266 		  int (*inquiry)(struct rsnd_dma *dma,
267 				  dma_addr_t *buf, int *len),
268 		  int (*complete)(struct rsnd_dma *dma))
269 {
270 	struct device *dev = rsnd_priv_to_dev(priv);
271 	dma_cap_mask_t mask;
272 
273 	if (dma->chan) {
274 		dev_err(dev, "it already has dma channel\n");
275 		return -EIO;
276 	}
277 
278 	dma_cap_zero(mask);
279 	dma_cap_set(DMA_SLAVE, mask);
280 
281 	dma->slave.shdma_slave.slave_id = id;
282 
283 	dma->chan = dma_request_channel(mask, rsnd_dma_filter,
284 					&dma->slave.shdma_slave);
285 	if (!dma->chan) {
286 		dev_err(dev, "can't get dma channel\n");
287 		return -EIO;
288 	}
289 
290 	dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
291 	dma->priv = priv;
292 	dma->inquiry = inquiry;
293 	dma->complete = complete;
294 	INIT_WORK(&dma->work, rsnd_dma_do_work);
295 
296 	return 0;
297 }
298 
299 void  rsnd_dma_quit(struct rsnd_priv *priv,
300 		    struct rsnd_dma *dma)
301 {
302 	if (dma->chan)
303 		dma_release_channel(dma->chan);
304 
305 	dma->chan = NULL;
306 }
307 
308 /*
309  *	rsnd_dai functions
310  */
311 #define rsnd_dai_call(rdai, io, fn)			\
312 ({							\
313 	struct rsnd_mod *mod, *n;			\
314 	int ret = 0;					\
315 	for_each_rsnd_mod(mod, n, io) {			\
316 		ret = rsnd_mod_call(mod, fn, rdai, io);	\
317 		if (ret < 0)				\
318 			break;				\
319 	}						\
320 	ret;						\
321 })
322 
323 int rsnd_dai_connect(struct rsnd_dai *rdai,
324 		     struct rsnd_mod *mod,
325 		     struct rsnd_dai_stream *io)
326 {
327 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
328 	struct device *dev = rsnd_priv_to_dev(priv);
329 
330 	if (!mod) {
331 		dev_err(dev, "NULL mod\n");
332 		return -EIO;
333 	}
334 
335 	if (!list_empty(&mod->list)) {
336 		dev_err(dev, "%s%d is not empty\n",
337 			rsnd_mod_name(mod),
338 			rsnd_mod_id(mod));
339 		return -EIO;
340 	}
341 
342 	list_add_tail(&mod->list, &io->head);
343 
344 	return 0;
345 }
346 
347 int rsnd_dai_disconnect(struct rsnd_mod *mod)
348 {
349 	list_del_init(&mod->list);
350 
351 	return 0;
352 }
353 
354 int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
355 {
356 	int id = rdai - priv->rdai;
357 
358 	if ((id < 0) || (id >= rsnd_dai_nr(priv)))
359 		return -EINVAL;
360 
361 	return id;
362 }
363 
364 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
365 {
366 	return priv->rdai + id;
367 }
368 
369 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
370 {
371 	struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
372 
373 	return rsnd_dai_get(priv, dai->id);
374 }
375 
376 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
377 {
378 	return &rdai->playback == io;
379 }
380 
381 /*
382  *	rsnd_soc_dai functions
383  */
384 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
385 {
386 	struct snd_pcm_substream *substream = io->substream;
387 	struct snd_pcm_runtime *runtime = substream->runtime;
388 	int pos = io->byte_pos + additional;
389 
390 	pos %= (runtime->periods * io->byte_per_period);
391 
392 	return pos;
393 }
394 
395 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
396 {
397 	io->byte_pos += byte;
398 
399 	if (io->byte_pos >= io->next_period_byte) {
400 		struct snd_pcm_substream *substream = io->substream;
401 		struct snd_pcm_runtime *runtime = substream->runtime;
402 
403 		io->period_pos++;
404 		io->next_period_byte += io->byte_per_period;
405 
406 		if (io->period_pos >= runtime->periods) {
407 			io->byte_pos = 0;
408 			io->period_pos = 0;
409 			io->next_period_byte = io->byte_per_period;
410 		}
411 
412 		snd_pcm_period_elapsed(substream);
413 	}
414 }
415 
416 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
417 				struct snd_pcm_substream *substream)
418 {
419 	struct snd_pcm_runtime *runtime = substream->runtime;
420 
421 	if (!list_empty(&io->head))
422 		return -EIO;
423 
424 	INIT_LIST_HEAD(&io->head);
425 	io->substream		= substream;
426 	io->byte_pos		= 0;
427 	io->period_pos		= 0;
428 	io->byte_per_period	= runtime->period_size *
429 				  runtime->channels *
430 				  samples_to_bytes(runtime, 1);
431 	io->next_period_byte	= io->byte_per_period;
432 
433 	return 0;
434 }
435 
436 static
437 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
438 {
439 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
440 
441 	return  rtd->cpu_dai;
442 }
443 
444 static
445 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
446 					struct snd_pcm_substream *substream)
447 {
448 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
449 		return &rdai->playback;
450 	else
451 		return &rdai->capture;
452 }
453 
454 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
455 			    struct snd_soc_dai *dai)
456 {
457 	struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
458 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
459 	struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
460 	struct rsnd_mod *mod = rsnd_ssi_mod_get_frm_dai(priv,
461 						rsnd_dai_id(priv, rdai),
462 						rsnd_dai_is_play(rdai, io));
463 	int ssi_id = rsnd_mod_id(mod);
464 	int ret;
465 	unsigned long flags;
466 
467 	rsnd_lock(priv, flags);
468 
469 	switch (cmd) {
470 	case SNDRV_PCM_TRIGGER_START:
471 		ret = rsnd_dai_stream_init(io, substream);
472 		if (ret < 0)
473 			goto dai_trigger_end;
474 
475 		ret = rsnd_platform_call(priv, dai, start, ssi_id);
476 		if (ret < 0)
477 			goto dai_trigger_end;
478 
479 		ret = rsnd_gen_path_init(priv, rdai, io);
480 		if (ret < 0)
481 			goto dai_trigger_end;
482 
483 		ret = rsnd_dai_call(rdai, io, init);
484 		if (ret < 0)
485 			goto dai_trigger_end;
486 
487 		ret = rsnd_dai_call(rdai, io, start);
488 		if (ret < 0)
489 			goto dai_trigger_end;
490 		break;
491 	case SNDRV_PCM_TRIGGER_STOP:
492 		ret = rsnd_dai_call(rdai, io, stop);
493 		if (ret < 0)
494 			goto dai_trigger_end;
495 
496 		ret = rsnd_dai_call(rdai, io, quit);
497 		if (ret < 0)
498 			goto dai_trigger_end;
499 
500 		ret = rsnd_gen_path_exit(priv, rdai, io);
501 		if (ret < 0)
502 			goto dai_trigger_end;
503 
504 		ret = rsnd_platform_call(priv, dai, stop, ssi_id);
505 		if (ret < 0)
506 			goto dai_trigger_end;
507 		break;
508 	default:
509 		ret = -EINVAL;
510 	}
511 
512 dai_trigger_end:
513 	rsnd_unlock(priv, flags);
514 
515 	return ret;
516 }
517 
518 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
519 {
520 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
521 
522 	/* set master/slave audio interface */
523 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
524 	case SND_SOC_DAIFMT_CBM_CFM:
525 		rdai->clk_master = 1;
526 		break;
527 	case SND_SOC_DAIFMT_CBS_CFS:
528 		rdai->clk_master = 0;
529 		break;
530 	default:
531 		return -EINVAL;
532 	}
533 
534 	/* set clock inversion */
535 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
536 	case SND_SOC_DAIFMT_NB_IF:
537 		rdai->bit_clk_inv = 0;
538 		rdai->frm_clk_inv = 1;
539 		break;
540 	case SND_SOC_DAIFMT_IB_NF:
541 		rdai->bit_clk_inv = 1;
542 		rdai->frm_clk_inv = 0;
543 		break;
544 	case SND_SOC_DAIFMT_IB_IF:
545 		rdai->bit_clk_inv = 1;
546 		rdai->frm_clk_inv = 1;
547 		break;
548 	case SND_SOC_DAIFMT_NB_NF:
549 	default:
550 		rdai->bit_clk_inv = 0;
551 		rdai->frm_clk_inv = 0;
552 		break;
553 	}
554 
555 	/* set format */
556 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
557 	case SND_SOC_DAIFMT_I2S:
558 		rdai->sys_delay = 0;
559 		rdai->data_alignment = 0;
560 		break;
561 	case SND_SOC_DAIFMT_LEFT_J:
562 		rdai->sys_delay = 1;
563 		rdai->data_alignment = 0;
564 		break;
565 	case SND_SOC_DAIFMT_RIGHT_J:
566 		rdai->sys_delay = 1;
567 		rdai->data_alignment = 1;
568 		break;
569 	}
570 
571 	return 0;
572 }
573 
574 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
575 	.trigger	= rsnd_soc_dai_trigger,
576 	.set_fmt	= rsnd_soc_dai_set_fmt,
577 };
578 
579 static int rsnd_dai_probe(struct platform_device *pdev,
580 			  struct rcar_snd_info *info,
581 			  struct rsnd_priv *priv)
582 {
583 	struct snd_soc_dai_driver *drv;
584 	struct rsnd_dai *rdai;
585 	struct rsnd_mod *pmod, *cmod;
586 	struct device *dev = rsnd_priv_to_dev(priv);
587 	int dai_nr;
588 	int i;
589 
590 	/* get max dai nr */
591 	for (dai_nr = 0; dai_nr < 32; dai_nr++) {
592 		pmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 1);
593 		cmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 0);
594 
595 		if (!pmod && !cmod)
596 			break;
597 	}
598 
599 	if (!dai_nr) {
600 		dev_err(dev, "no dai\n");
601 		return -EIO;
602 	}
603 
604 	drv  = devm_kzalloc(dev, sizeof(*drv)  * dai_nr, GFP_KERNEL);
605 	rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
606 	if (!drv || !rdai) {
607 		dev_err(dev, "dai allocate failed\n");
608 		return -ENOMEM;
609 	}
610 
611 	for (i = 0; i < dai_nr; i++) {
612 
613 		pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1);
614 		cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0);
615 
616 		/*
617 		 *	init rsnd_dai
618 		 */
619 		INIT_LIST_HEAD(&rdai[i].playback.head);
620 		INIT_LIST_HEAD(&rdai[i].capture.head);
621 
622 		snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
623 
624 		/*
625 		 *	init snd_soc_dai_driver
626 		 */
627 		drv[i].name	= rdai[i].name;
628 		drv[i].ops	= &rsnd_soc_dai_ops;
629 		if (pmod) {
630 			drv[i].playback.rates		= RSND_RATES;
631 			drv[i].playback.formats		= RSND_FMTS;
632 			drv[i].playback.channels_min	= 2;
633 			drv[i].playback.channels_max	= 2;
634 		}
635 		if (cmod) {
636 			drv[i].capture.rates		= RSND_RATES;
637 			drv[i].capture.formats		= RSND_FMTS;
638 			drv[i].capture.channels_min	= 2;
639 			drv[i].capture.channels_max	= 2;
640 		}
641 
642 		dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
643 			pmod ? "play"    : " -- ",
644 			cmod ? "capture" : "  --   ");
645 	}
646 
647 	priv->dai_nr	= dai_nr;
648 	priv->daidrv	= drv;
649 	priv->rdai	= rdai;
650 
651 	return 0;
652 }
653 
654 static void rsnd_dai_remove(struct platform_device *pdev,
655 			  struct rsnd_priv *priv)
656 {
657 }
658 
659 /*
660  *		pcm ops
661  */
662 static struct snd_pcm_hardware rsnd_pcm_hardware = {
663 	.info =		SNDRV_PCM_INFO_INTERLEAVED	|
664 			SNDRV_PCM_INFO_MMAP		|
665 			SNDRV_PCM_INFO_MMAP_VALID	|
666 			SNDRV_PCM_INFO_PAUSE,
667 	.formats		= RSND_FMTS,
668 	.rates			= RSND_RATES,
669 	.rate_min		= 8000,
670 	.rate_max		= 192000,
671 	.channels_min		= 2,
672 	.channels_max		= 2,
673 	.buffer_bytes_max	= 64 * 1024,
674 	.period_bytes_min	= 32,
675 	.period_bytes_max	= 8192,
676 	.periods_min		= 1,
677 	.periods_max		= 32,
678 	.fifo_size		= 256,
679 };
680 
681 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
682 {
683 	struct snd_pcm_runtime *runtime = substream->runtime;
684 	int ret = 0;
685 
686 	snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
687 
688 	ret = snd_pcm_hw_constraint_integer(runtime,
689 					    SNDRV_PCM_HW_PARAM_PERIODS);
690 
691 	return ret;
692 }
693 
694 static int rsnd_hw_params(struct snd_pcm_substream *substream,
695 			 struct snd_pcm_hw_params *hw_params)
696 {
697 	return snd_pcm_lib_malloc_pages(substream,
698 					params_buffer_bytes(hw_params));
699 }
700 
701 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
702 {
703 	struct snd_pcm_runtime *runtime = substream->runtime;
704 	struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
705 	struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
706 	struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
707 
708 	return bytes_to_frames(runtime, io->byte_pos);
709 }
710 
711 static struct snd_pcm_ops rsnd_pcm_ops = {
712 	.open		= rsnd_pcm_open,
713 	.ioctl		= snd_pcm_lib_ioctl,
714 	.hw_params	= rsnd_hw_params,
715 	.hw_free	= snd_pcm_lib_free_pages,
716 	.pointer	= rsnd_pointer,
717 };
718 
719 /*
720  *		snd_soc_platform
721  */
722 
723 #define PREALLOC_BUFFER		(32 * 1024)
724 #define PREALLOC_BUFFER_MAX	(32 * 1024)
725 
726 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
727 {
728 	return snd_pcm_lib_preallocate_pages_for_all(
729 		rtd->pcm,
730 		SNDRV_DMA_TYPE_DEV,
731 		rtd->card->snd_card->dev,
732 		PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
733 }
734 
735 static void rsnd_pcm_free(struct snd_pcm *pcm)
736 {
737 	snd_pcm_lib_preallocate_free_for_all(pcm);
738 }
739 
740 static struct snd_soc_platform_driver rsnd_soc_platform = {
741 	.ops		= &rsnd_pcm_ops,
742 	.pcm_new	= rsnd_pcm_new,
743 	.pcm_free	= rsnd_pcm_free,
744 };
745 
746 static const struct snd_soc_component_driver rsnd_soc_component = {
747 	.name		= "rsnd",
748 };
749 
750 /*
751  *	rsnd probe
752  */
753 static int rsnd_probe(struct platform_device *pdev)
754 {
755 	struct rcar_snd_info *info;
756 	struct rsnd_priv *priv;
757 	struct device *dev = &pdev->dev;
758 	int ret;
759 
760 	info = pdev->dev.platform_data;
761 	if (!info) {
762 		dev_err(dev, "driver needs R-Car sound information\n");
763 		return -ENODEV;
764 	}
765 
766 	/*
767 	 *	init priv data
768 	 */
769 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
770 	if (!priv) {
771 		dev_err(dev, "priv allocate failed\n");
772 		return -ENODEV;
773 	}
774 
775 	priv->dev	= dev;
776 	priv->info	= info;
777 	spin_lock_init(&priv->lock);
778 
779 	/*
780 	 *	init each module
781 	 */
782 	ret = rsnd_gen_probe(pdev, info, priv);
783 	if (ret < 0)
784 		return ret;
785 
786 	ret = rsnd_scu_probe(pdev, info, priv);
787 	if (ret < 0)
788 		return ret;
789 
790 	ret = rsnd_adg_probe(pdev, info, priv);
791 	if (ret < 0)
792 		return ret;
793 
794 	ret = rsnd_ssi_probe(pdev, info, priv);
795 	if (ret < 0)
796 		return ret;
797 
798 	ret = rsnd_dai_probe(pdev, info, priv);
799 	if (ret < 0)
800 		return ret;
801 
802 	/*
803 	 *	asoc register
804 	 */
805 	ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
806 	if (ret < 0) {
807 		dev_err(dev, "cannot snd soc register\n");
808 		return ret;
809 	}
810 
811 	ret = snd_soc_register_component(dev, &rsnd_soc_component,
812 					 priv->daidrv, rsnd_dai_nr(priv));
813 	if (ret < 0) {
814 		dev_err(dev, "cannot snd dai register\n");
815 		goto exit_snd_soc;
816 	}
817 
818 	dev_set_drvdata(dev, priv);
819 
820 	pm_runtime_enable(dev);
821 
822 	dev_info(dev, "probed\n");
823 	return ret;
824 
825 exit_snd_soc:
826 	snd_soc_unregister_platform(dev);
827 
828 	return ret;
829 }
830 
831 static int rsnd_remove(struct platform_device *pdev)
832 {
833 	struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
834 
835 	pm_runtime_disable(&pdev->dev);
836 
837 	/*
838 	 *	remove each module
839 	 */
840 	rsnd_ssi_remove(pdev, priv);
841 	rsnd_adg_remove(pdev, priv);
842 	rsnd_scu_remove(pdev, priv);
843 	rsnd_dai_remove(pdev, priv);
844 	rsnd_gen_remove(pdev, priv);
845 
846 	return 0;
847 }
848 
849 static struct platform_driver rsnd_driver = {
850 	.driver	= {
851 		.name	= "rcar_sound",
852 	},
853 	.probe		= rsnd_probe,
854 	.remove		= rsnd_remove,
855 };
856 module_platform_driver(rsnd_driver);
857 
858 MODULE_LICENSE("GPL");
859 MODULE_DESCRIPTION("Renesas R-Car audio driver");
860 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
861 MODULE_ALIAS("platform:rcar-pcm-audio");
862