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