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