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