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