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