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