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