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