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