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