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