ASoC: rsnd: Use generic names for device nodes
[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);
e9c390df 734 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
3b7843ff 735 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
e9c390df 736 unsigned long flags;
3b7843ff
KM
737 int ret;
738
e9c390df 739 rsnd_lock(priv, flags);
3b7843ff 740 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
e9c390df
KM
741 rsnd_unlock(priv, flags);
742
3b7843ff
KM
743 if (ret)
744 return ret;
745
1536a968
KM
746 return snd_pcm_lib_malloc_pages(substream,
747 params_buffer_bytes(hw_params));
748}
749
750static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
751{
752 struct snd_pcm_runtime *runtime = substream->runtime;
753 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
754 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
755 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
756
757 return bytes_to_frames(runtime, io->byte_pos);
758}
759
760static struct snd_pcm_ops rsnd_pcm_ops = {
761 .open = rsnd_pcm_open,
762 .ioctl = snd_pcm_lib_ioctl,
763 .hw_params = rsnd_hw_params,
764 .hw_free = snd_pcm_lib_free_pages,
765 .pointer = rsnd_pointer,
766};
767
170a2497
KM
768/*
769 * snd_kcontrol
770 */
771#define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
772static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
773 struct snd_ctl_elem_info *uinfo)
774{
775 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
776
777 if (cfg->texts) {
778 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
779 uinfo->count = cfg->size;
780 uinfo->value.enumerated.items = cfg->max;
781 if (uinfo->value.enumerated.item >= cfg->max)
782 uinfo->value.enumerated.item = cfg->max - 1;
783 strlcpy(uinfo->value.enumerated.name,
784 cfg->texts[uinfo->value.enumerated.item],
785 sizeof(uinfo->value.enumerated.name));
786 } else {
787 uinfo->count = cfg->size;
788 uinfo->value.integer.min = 0;
789 uinfo->value.integer.max = cfg->max;
790 uinfo->type = (cfg->max == 1) ?
791 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
792 SNDRV_CTL_ELEM_TYPE_INTEGER;
793 }
794
795 return 0;
796}
797
798static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
799 struct snd_ctl_elem_value *uc)
800{
801 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
802 int i;
803
804 for (i = 0; i < cfg->size; i++)
805 if (cfg->texts)
806 uc->value.enumerated.item[i] = cfg->val[i];
807 else
808 uc->value.integer.value[i] = cfg->val[i];
809
810 return 0;
811}
812
813static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
814 struct snd_ctl_elem_value *uc)
815{
816 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
817 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
818 int i, change = 0;
819
820 for (i = 0; i < cfg->size; i++) {
821 if (cfg->texts) {
822 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
823 cfg->val[i] = uc->value.enumerated.item[i];
824 } else {
825 change |= (uc->value.integer.value[i] != cfg->val[i]);
826 cfg->val[i] = uc->value.integer.value[i];
827 }
828 }
829
830 if (change)
831 cfg->update(mod);
832
833 return change;
834}
835
836static int __rsnd_kctrl_new(struct rsnd_mod *mod,
170a2497
KM
837 struct snd_soc_pcm_runtime *rtd,
838 const unsigned char *name,
839 struct rsnd_kctrl_cfg *cfg,
840 void (*update)(struct rsnd_mod *mod))
841{
da620d72 842 struct snd_soc_card *soc_card = rtd->card;
170a2497
KM
843 struct snd_card *card = rtd->card->snd_card;
844 struct snd_kcontrol *kctrl;
845 struct snd_kcontrol_new knew = {
846 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
847 .name = name,
848 .info = rsnd_kctrl_info,
da620d72 849 .index = rtd - soc_card->rtd,
170a2497
KM
850 .get = rsnd_kctrl_get,
851 .put = rsnd_kctrl_put,
852 .private_value = (unsigned long)cfg,
853 };
854 int ret;
855
856 kctrl = snd_ctl_new1(&knew, mod);
857 if (!kctrl)
858 return -ENOMEM;
859
860 ret = snd_ctl_add(card, kctrl);
d1f83d6e
KM
861 if (ret < 0) {
862 snd_ctl_free_one(kctrl);
170a2497 863 return ret;
d1f83d6e 864 }
170a2497
KM
865
866 cfg->update = update;
d1f83d6e
KM
867 cfg->card = card;
868 cfg->kctrl = kctrl;
170a2497
KM
869
870 return 0;
871}
872
d1f83d6e
KM
873void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
874{
875 snd_ctl_remove(cfg->card, cfg->kctrl);
876}
877
170a2497 878int rsnd_kctrl_new_m(struct rsnd_mod *mod,
170a2497
KM
879 struct snd_soc_pcm_runtime *rtd,
880 const unsigned char *name,
881 void (*update)(struct rsnd_mod *mod),
882 struct rsnd_kctrl_cfg_m *_cfg,
883 u32 max)
884{
885 _cfg->cfg.max = max;
886 _cfg->cfg.size = RSND_DVC_CHANNELS;
887 _cfg->cfg.val = _cfg->val;
f708d944 888 return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
170a2497
KM
889}
890
891int rsnd_kctrl_new_s(struct rsnd_mod *mod,
170a2497
KM
892 struct snd_soc_pcm_runtime *rtd,
893 const unsigned char *name,
894 void (*update)(struct rsnd_mod *mod),
895 struct rsnd_kctrl_cfg_s *_cfg,
896 u32 max)
897{
898 _cfg->cfg.max = max;
899 _cfg->cfg.size = 1;
900 _cfg->cfg.val = &_cfg->val;
f708d944 901 return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
170a2497
KM
902}
903
904int rsnd_kctrl_new_e(struct rsnd_mod *mod,
170a2497
KM
905 struct snd_soc_pcm_runtime *rtd,
906 const unsigned char *name,
907 struct rsnd_kctrl_cfg_s *_cfg,
908 void (*update)(struct rsnd_mod *mod),
909 const char * const *texts,
910 u32 max)
911{
912 _cfg->cfg.max = max;
913 _cfg->cfg.size = 1;
914 _cfg->cfg.val = &_cfg->val;
915 _cfg->cfg.texts = texts;
f708d944 916 return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
170a2497
KM
917}
918
1536a968
KM
919/*
920 * snd_soc_platform
921 */
922
923#define PREALLOC_BUFFER (32 * 1024)
924#define PREALLOC_BUFFER_MAX (32 * 1024)
925
926static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
927{
7c63f3c0 928 struct snd_soc_dai *dai = rtd->cpu_dai;
e9c390df 929 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
7c63f3c0 930 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
e9c390df
KM
931 unsigned long flags;
932 int ret = 0;
bff58ea4 933
e9c390df
KM
934 rsnd_lock(priv, flags);
935 ret |= rsnd_dai_call(pcm_new, &rdai->playback, rtd);
936 ret |= rsnd_dai_call(pcm_new, &rdai->capture, rtd);
937 rsnd_unlock(priv, flags);
bff58ea4 938
7c63f3c0
KM
939 if (ret)
940 return ret;
bff58ea4 941
1536a968
KM
942 return snd_pcm_lib_preallocate_pages_for_all(
943 rtd->pcm,
944 SNDRV_DMA_TYPE_DEV,
945 rtd->card->snd_card->dev,
946 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
947}
948
1536a968
KM
949static struct snd_soc_platform_driver rsnd_soc_platform = {
950 .ops = &rsnd_pcm_ops,
951 .pcm_new = rsnd_pcm_new,
1536a968
KM
952};
953
954static const struct snd_soc_component_driver rsnd_soc_component = {
955 .name = "rsnd",
956};
957
d3a76823 958static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
f708d944 959 struct rsnd_dai_stream *io)
d3a76823 960{
e9c390df 961 unsigned long flags;
d3a76823
KM
962 int ret;
963
e9c390df
KM
964 rsnd_lock(priv, flags);
965
690602fc 966 ret = rsnd_dai_call(probe, io, priv);
d3a76823
KM
967 if (ret == -EAGAIN) {
968 /*
969 * Fallback to PIO mode
970 */
971
972 /*
973 * call "remove" for SSI/SRC/DVC
974 * SSI will be switch to PIO mode if it was DMA mode
975 * see
976 * rsnd_dma_init()
97463e19 977 * rsnd_ssi_fallback()
d3a76823 978 */
690602fc 979 rsnd_dai_call(remove, io, priv);
d3a76823
KM
980
981 /*
982 * remove SRC/DVC from DAI,
983 */
984 rsnd_path_break(priv, io, src);
985 rsnd_path_break(priv, io, dvc);
986
97463e19
KM
987 /*
988 * fallback
989 */
690602fc 990 rsnd_dai_call(fallback, io, priv);
97463e19 991
d3a76823
KM
992 /*
993 * retry to "probe".
994 * DAI has SSI which is PIO mode only now.
995 */
690602fc 996 ret = rsnd_dai_call(probe, io, priv);
d3a76823 997 }
e9c390df 998 rsnd_unlock(priv, flags);
d3a76823
KM
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;
e9c390df 1014 unsigned long flags;
d1ac970f 1015 int (*probe_func[])(struct platform_device *pdev,
90e8e50f 1016 const struct rsnd_of_data *of_data,
d1ac970f
KM
1017 struct rsnd_priv *priv) = {
1018 rsnd_gen_probe,
288f392e 1019 rsnd_dma_probe,
d1ac970f 1020 rsnd_ssi_probe,
ba9c949f 1021 rsnd_src_probe,
bff58ea4 1022 rsnd_dvc_probe,
d1ac970f
KM
1023 rsnd_adg_probe,
1024 rsnd_dai_probe,
1025 };
1026 int ret, i;
1536a968 1027
90e8e50f
KM
1028 info = NULL;
1029 of_data = NULL;
1030 if (of_id) {
1031 info = devm_kzalloc(&pdev->dev,
1032 sizeof(struct rcar_snd_info), GFP_KERNEL);
1033 of_data = of_id->data;
1034 } else {
1035 info = pdev->dev.platform_data;
1036 }
1037
1536a968
KM
1038 if (!info) {
1039 dev_err(dev, "driver needs R-Car sound information\n");
1040 return -ENODEV;
1041 }
1042
1043 /*
1044 * init priv data
1045 */
1046 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1047 if (!priv) {
1048 dev_err(dev, "priv allocate failed\n");
1049 return -ENODEV;
1050 }
1051
9f464f8e 1052 priv->pdev = pdev;
1536a968
KM
1053 priv->info = info;
1054 spin_lock_init(&priv->lock);
1055
1056 /*
1057 * init each module
1058 */
d1ac970f 1059 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
90e8e50f 1060 ret = probe_func[i](pdev, of_data, priv);
d1ac970f
KM
1061 if (ret)
1062 return ret;
1063 }
07539c1d 1064
7681f6ac 1065 for_each_rsnd_dai(rdai, priv, i) {
f708d944 1066 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
7681f6ac 1067 if (ret)
d62a3dcd 1068 goto exit_snd_probe;
dfc9403b 1069
f708d944 1070 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
7681f6ac 1071 if (ret)
d62a3dcd 1072 goto exit_snd_probe;
7681f6ac 1073 }
4b4dab82 1074
0b1f6ec7
KM
1075 dev_set_drvdata(dev, priv);
1076
1536a968
KM
1077 /*
1078 * asoc register
1079 */
1080 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1081 if (ret < 0) {
1082 dev_err(dev, "cannot snd soc register\n");
1083 return ret;
1084 }
1085
1086 ret = snd_soc_register_component(dev, &rsnd_soc_component,
ecba9e72 1087 priv->daidrv, rsnd_rdai_nr(priv));
1536a968
KM
1088 if (ret < 0) {
1089 dev_err(dev, "cannot snd dai register\n");
1090 goto exit_snd_soc;
1091 }
1092
1536a968
KM
1093 pm_runtime_enable(dev);
1094
1095 dev_info(dev, "probed\n");
1096 return ret;
1097
1098exit_snd_soc:
1099 snd_soc_unregister_platform(dev);
d62a3dcd 1100exit_snd_probe:
e9c390df 1101 rsnd_lock(priv, flags);
d62a3dcd 1102 for_each_rsnd_dai(rdai, priv, i) {
690602fc
KM
1103 rsnd_dai_call(remove, &rdai->playback, priv);
1104 rsnd_dai_call(remove, &rdai->capture, priv);
d62a3dcd 1105 }
e9c390df 1106 rsnd_unlock(priv, flags);
1536a968
KM
1107
1108 return ret;
1109}
1110
1111static int rsnd_remove(struct platform_device *pdev)
1112{
1113 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
7681f6ac 1114 struct rsnd_dai *rdai;
e9c390df 1115 unsigned long flags;
2f78dd7f
KM
1116 void (*remove_func[])(struct platform_device *pdev,
1117 struct rsnd_priv *priv) = {
1118 rsnd_ssi_remove,
1119 rsnd_src_remove,
1120 rsnd_dvc_remove,
1121 };
d62a3dcd 1122 int ret = 0, i;
1536a968
KM
1123
1124 pm_runtime_disable(&pdev->dev);
1125
e9c390df 1126 rsnd_lock(priv, flags);
7681f6ac 1127 for_each_rsnd_dai(rdai, priv, i) {
690602fc
KM
1128 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1129 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
7681f6ac 1130 }
e9c390df 1131 rsnd_unlock(priv, flags);
1536a968 1132
2f78dd7f
KM
1133 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1134 remove_func[i](pdev, priv);
1135
d7c42ff8
KM
1136 snd_soc_unregister_component(&pdev->dev);
1137 snd_soc_unregister_platform(&pdev->dev);
1138
d62a3dcd 1139 return ret;
1536a968
KM
1140}
1141
1142static struct platform_driver rsnd_driver = {
1143 .driver = {
1144 .name = "rcar_sound",
90e8e50f 1145 .of_match_table = rsnd_of_match,
1536a968
KM
1146 },
1147 .probe = rsnd_probe,
1148 .remove = rsnd_remove,
1149};
1150module_platform_driver(rsnd_driver);
1151
1152MODULE_LICENSE("GPL");
1153MODULE_DESCRIPTION("Renesas R-Car audio driver");
1154MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1155MODULE_ALIAS("platform:rcar-pcm-audio");
This page took 0.211216 seconds and 5 git commands to generate.