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