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