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