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