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