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