ASoC: rsnd: set SSIWSR setting on rsnd_ssi_config_init()
[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
2ea6b074 549static int rsnd_dai_probe(struct rsnd_priv *priv)
90e8e50f 550{
94e2710c
KM
551 struct device_node *dai_node;
552 struct device_node *dai_np, *np, *node;
90e8e50f 553 struct device_node *playback, *capture;
94e2710c
KM
554 struct rsnd_dai_stream *io_playback;
555 struct rsnd_dai_stream *io_capture;
556 struct snd_soc_dai_driver *drv;
557 struct rsnd_dai *rdai;
2ea6b074 558 struct device *dev = rsnd_priv_to_dev(priv);
94e2710c
KM
559 int nr, dai_i, io_i, np_i;
560 int ret;
90e8e50f 561
94e2710c 562 dai_node = rsnd_dai_of_node(priv);
90e8e50f 563 nr = of_get_child_count(dai_node);
94e2710c
KM
564 if (!nr) {
565 ret = -EINVAL;
566 goto rsnd_dai_probe_done;
90e8e50f
KM
567 }
568
94e2710c
KM
569 drv = devm_kzalloc(dev, sizeof(*drv) * nr, GFP_KERNEL);
570 rdai = devm_kzalloc(dev, sizeof(*rdai) * nr, GFP_KERNEL);
571 if (!drv || !rdai) {
572 ret = -ENOMEM;
573 goto rsnd_dai_probe_done;
574 }
90e8e50f 575
94e2710c
KM
576 priv->rdai_nr = nr;
577 priv->daidrv = drv;
578 priv->rdai = rdai;
90e8e50f
KM
579
580 /*
581 * parse all dai
582 */
583 dai_i = 0;
584 for_each_child_of_node(dai_node, dai_np) {
94e2710c
KM
585 rdai = rsnd_rdai_get(priv, dai_i);
586 drv = drv + dai_i;
587 io_playback = &rdai->playback;
588 io_capture = &rdai->capture;
589
590 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
591
592 rdai->priv = priv;
593 drv->name = rdai->name;
594 drv->ops = &rsnd_soc_dai_ops;
595
596 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
597 "DAI%d Playback", dai_i);
598 drv->playback.rates = RSND_RATES;
599 drv->playback.formats = RSND_FMTS;
600 drv->playback.channels_min = 2;
601 drv->playback.channels_max = 2;
602 drv->playback.stream_name = rdai->playback.name;
603
604 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
605 "DAI%d Capture", dai_i);
606 drv->capture.rates = RSND_RATES;
607 drv->capture.formats = RSND_FMTS;
608 drv->capture.channels_min = 2;
609 drv->capture.channels_max = 2;
610 drv->capture.stream_name = rdai->capture.name;
611
612 rdai->playback.rdai = rdai;
613 rdai->capture.rdai = rdai;
90e8e50f 614
94e2710c
KM
615#define mod_parse(name) \
616node = rsnd_##name##_of_node(priv); \
617if (node) { \
618 struct rsnd_mod *mod; \
619 np_i = 0; \
620 for_each_child_of_node(node, np) { \
621 mod = rsnd_##name##_mod_get(priv, np_i); \
622 if (np == playback) \
623 rsnd_dai_connect(mod, io_playback, mod->type); \
624 if (np == capture) \
625 rsnd_dai_connect(mod, io_capture, mod->type); \
626 np_i++; \
627 } \
628 of_node_put(node); \
629}
90e8e50f 630
94e2710c
KM
631 for (io_i = 0;; io_i++) {
632 playback = of_parse_phandle(dai_np, "playback", io_i);
633 capture = of_parse_phandle(dai_np, "capture", io_i);
90e8e50f
KM
634
635 if (!playback && !capture)
636 break;
637
638 mod_parse(ssi);
639 mod_parse(src);
9269e3c3 640 mod_parse(ctu);
70fb1052 641 mod_parse(mix);
34cb6123 642 mod_parse(dvc);
90e8e50f 643
a493b6a6
JL
644 of_node_put(playback);
645 of_node_put(capture);
90e8e50f
KM
646 }
647
648 dai_i++;
1536a968 649
94e2710c
KM
650 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
651 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
652 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
1536a968
KM
653 }
654
94e2710c 655 ret = 0;
49848073 656
94e2710c
KM
657rsnd_dai_probe_done:
658 of_node_put(dai_node);
1536a968 659
94e2710c 660 return ret;
1536a968
KM
661}
662
1536a968
KM
663/*
664 * pcm ops
665 */
666static struct snd_pcm_hardware rsnd_pcm_hardware = {
667 .info = SNDRV_PCM_INFO_INTERLEAVED |
668 SNDRV_PCM_INFO_MMAP |
706c6621 669 SNDRV_PCM_INFO_MMAP_VALID,
1536a968
KM
670 .buffer_bytes_max = 64 * 1024,
671 .period_bytes_min = 32,
672 .period_bytes_max = 8192,
673 .periods_min = 1,
674 .periods_max = 32,
675 .fifo_size = 256,
676};
677
678static int rsnd_pcm_open(struct snd_pcm_substream *substream)
679{
680 struct snd_pcm_runtime *runtime = substream->runtime;
681 int ret = 0;
682
683 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
684
685 ret = snd_pcm_hw_constraint_integer(runtime,
686 SNDRV_PCM_HW_PARAM_PERIODS);
687
688 return ret;
689}
690
691static int rsnd_hw_params(struct snd_pcm_substream *substream,
692 struct snd_pcm_hw_params *hw_params)
693{
3b7843ff
KM
694 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
695 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
696 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
697 int ret;
698
699 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
700 if (ret)
701 return ret;
702
1536a968
KM
703 return snd_pcm_lib_malloc_pages(substream,
704 params_buffer_bytes(hw_params));
705}
706
707static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
708{
709 struct snd_pcm_runtime *runtime = substream->runtime;
710 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
711 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
712 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
713
714 return bytes_to_frames(runtime, io->byte_pos);
715}
716
717static struct snd_pcm_ops rsnd_pcm_ops = {
718 .open = rsnd_pcm_open,
719 .ioctl = snd_pcm_lib_ioctl,
720 .hw_params = rsnd_hw_params,
721 .hw_free = snd_pcm_lib_free_pages,
722 .pointer = rsnd_pointer,
723};
724
170a2497
KM
725/*
726 * snd_kcontrol
727 */
728#define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
729static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
730 struct snd_ctl_elem_info *uinfo)
731{
732 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
733
734 if (cfg->texts) {
735 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
736 uinfo->count = cfg->size;
737 uinfo->value.enumerated.items = cfg->max;
738 if (uinfo->value.enumerated.item >= cfg->max)
739 uinfo->value.enumerated.item = cfg->max - 1;
740 strlcpy(uinfo->value.enumerated.name,
741 cfg->texts[uinfo->value.enumerated.item],
742 sizeof(uinfo->value.enumerated.name));
743 } else {
744 uinfo->count = cfg->size;
745 uinfo->value.integer.min = 0;
746 uinfo->value.integer.max = cfg->max;
747 uinfo->type = (cfg->max == 1) ?
748 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
749 SNDRV_CTL_ELEM_TYPE_INTEGER;
750 }
751
752 return 0;
753}
754
755static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
756 struct snd_ctl_elem_value *uc)
757{
758 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
759 int i;
760
761 for (i = 0; i < cfg->size; i++)
762 if (cfg->texts)
763 uc->value.enumerated.item[i] = cfg->val[i];
764 else
765 uc->value.integer.value[i] = cfg->val[i];
766
767 return 0;
768}
769
770static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
771 struct snd_ctl_elem_value *uc)
772{
773 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
774 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
775 int i, change = 0;
776
777 for (i = 0; i < cfg->size; i++) {
778 if (cfg->texts) {
779 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
780 cfg->val[i] = uc->value.enumerated.item[i];
781 } else {
782 change |= (uc->value.integer.value[i] != cfg->val[i]);
783 cfg->val[i] = uc->value.integer.value[i];
784 }
785 }
786
787 if (change)
b65a7ccc 788 cfg->update(cfg->io, mod);
170a2497
KM
789
790 return change;
791}
792
793static int __rsnd_kctrl_new(struct rsnd_mod *mod,
b65a7ccc 794 struct rsnd_dai_stream *io,
170a2497
KM
795 struct snd_soc_pcm_runtime *rtd,
796 const unsigned char *name,
797 struct rsnd_kctrl_cfg *cfg,
b65a7ccc
KM
798 void (*update)(struct rsnd_dai_stream *io,
799 struct rsnd_mod *mod))
170a2497 800{
da620d72 801 struct snd_soc_card *soc_card = rtd->card;
170a2497
KM
802 struct snd_card *card = rtd->card->snd_card;
803 struct snd_kcontrol *kctrl;
804 struct snd_kcontrol_new knew = {
805 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
806 .name = name,
807 .info = rsnd_kctrl_info,
da620d72 808 .index = rtd - soc_card->rtd,
170a2497
KM
809 .get = rsnd_kctrl_get,
810 .put = rsnd_kctrl_put,
811 .private_value = (unsigned long)cfg,
812 };
813 int ret;
814
815 kctrl = snd_ctl_new1(&knew, mod);
816 if (!kctrl)
817 return -ENOMEM;
818
819 ret = snd_ctl_add(card, kctrl);
d1f83d6e
KM
820 if (ret < 0) {
821 snd_ctl_free_one(kctrl);
170a2497 822 return ret;
d1f83d6e 823 }
170a2497
KM
824
825 cfg->update = update;
d1f83d6e
KM
826 cfg->card = card;
827 cfg->kctrl = kctrl;
b65a7ccc 828 cfg->io = io;
170a2497
KM
829
830 return 0;
831}
832
d1f83d6e
KM
833void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
834{
835 snd_ctl_remove(cfg->card, cfg->kctrl);
836}
837
170a2497 838int rsnd_kctrl_new_m(struct rsnd_mod *mod,
b65a7ccc 839 struct rsnd_dai_stream *io,
170a2497
KM
840 struct snd_soc_pcm_runtime *rtd,
841 const unsigned char *name,
b65a7ccc
KM
842 void (*update)(struct rsnd_dai_stream *io,
843 struct rsnd_mod *mod),
170a2497
KM
844 struct rsnd_kctrl_cfg_m *_cfg,
845 u32 max)
846{
847 _cfg->cfg.max = max;
848 _cfg->cfg.size = RSND_DVC_CHANNELS;
849 _cfg->cfg.val = _cfg->val;
b65a7ccc 850 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
170a2497
KM
851}
852
853int rsnd_kctrl_new_s(struct rsnd_mod *mod,
b65a7ccc 854 struct rsnd_dai_stream *io,
170a2497
KM
855 struct snd_soc_pcm_runtime *rtd,
856 const unsigned char *name,
b65a7ccc
KM
857 void (*update)(struct rsnd_dai_stream *io,
858 struct rsnd_mod *mod),
170a2497
KM
859 struct rsnd_kctrl_cfg_s *_cfg,
860 u32 max)
861{
862 _cfg->cfg.max = max;
863 _cfg->cfg.size = 1;
864 _cfg->cfg.val = &_cfg->val;
b65a7ccc 865 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
170a2497
KM
866}
867
868int rsnd_kctrl_new_e(struct rsnd_mod *mod,
b65a7ccc 869 struct rsnd_dai_stream *io,
170a2497
KM
870 struct snd_soc_pcm_runtime *rtd,
871 const unsigned char *name,
872 struct rsnd_kctrl_cfg_s *_cfg,
b65a7ccc
KM
873 void (*update)(struct rsnd_dai_stream *io,
874 struct rsnd_mod *mod),
170a2497
KM
875 const char * const *texts,
876 u32 max)
877{
878 _cfg->cfg.max = max;
879 _cfg->cfg.size = 1;
880 _cfg->cfg.val = &_cfg->val;
881 _cfg->cfg.texts = texts;
b65a7ccc 882 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
170a2497
KM
883}
884
1536a968
KM
885/*
886 * snd_soc_platform
887 */
888
889#define PREALLOC_BUFFER (32 * 1024)
890#define PREALLOC_BUFFER_MAX (32 * 1024)
891
892static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
893{
7c63f3c0
KM
894 struct snd_soc_dai *dai = rtd->cpu_dai;
895 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
ae11a9be 896 int ret;
bff58ea4 897
ae11a9be
KM
898 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
899 if (ret)
900 return ret;
bff58ea4 901
ae11a9be 902 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
7c63f3c0
KM
903 if (ret)
904 return ret;
bff58ea4 905
1536a968
KM
906 return snd_pcm_lib_preallocate_pages_for_all(
907 rtd->pcm,
908 SNDRV_DMA_TYPE_DEV,
909 rtd->card->snd_card->dev,
910 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
911}
912
1536a968
KM
913static struct snd_soc_platform_driver rsnd_soc_platform = {
914 .ops = &rsnd_pcm_ops,
915 .pcm_new = rsnd_pcm_new,
1536a968
KM
916};
917
918static const struct snd_soc_component_driver rsnd_soc_component = {
919 .name = "rsnd",
920};
921
d3a76823 922static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
f708d944 923 struct rsnd_dai_stream *io)
d3a76823 924{
d3a76823
KM
925 int ret;
926
690602fc 927 ret = rsnd_dai_call(probe, io, priv);
d3a76823 928 if (ret == -EAGAIN) {
48d58281
KM
929 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
930 int i;
931
d3a76823
KM
932 /*
933 * Fallback to PIO mode
934 */
935
936 /*
937 * call "remove" for SSI/SRC/DVC
938 * SSI will be switch to PIO mode if it was DMA mode
939 * see
940 * rsnd_dma_init()
97463e19 941 * rsnd_ssi_fallback()
d3a76823 942 */
690602fc 943 rsnd_dai_call(remove, io, priv);
d3a76823
KM
944
945 /*
48d58281
KM
946 * remove all mod from io
947 * and, re connect ssi
d3a76823 948 */
48d58281
KM
949 for (i = 0; i < RSND_MOD_MAX; i++)
950 rsnd_dai_disconnect((io)->mod[i], io, i);
951 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
d3a76823 952
97463e19
KM
953 /*
954 * fallback
955 */
690602fc 956 rsnd_dai_call(fallback, io, priv);
97463e19 957
d3a76823
KM
958 /*
959 * retry to "probe".
960 * DAI has SSI which is PIO mode only now.
961 */
690602fc 962 ret = rsnd_dai_call(probe, io, priv);
d3a76823
KM
963 }
964
965 return ret;
966}
967
1536a968
KM
968/*
969 * rsnd probe
970 */
971static int rsnd_probe(struct platform_device *pdev)
972{
1536a968
KM
973 struct rsnd_priv *priv;
974 struct device *dev = &pdev->dev;
7681f6ac 975 struct rsnd_dai *rdai;
90e8e50f 976 const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
2ea6b074 977 int (*probe_func[])(struct rsnd_priv *priv) = {
d1ac970f 978 rsnd_gen_probe,
288f392e 979 rsnd_dma_probe,
d1ac970f 980 rsnd_ssi_probe,
c7f69ab5 981 rsnd_ssiu_probe,
ba9c949f 982 rsnd_src_probe,
9269e3c3 983 rsnd_ctu_probe,
70fb1052 984 rsnd_mix_probe,
bff58ea4 985 rsnd_dvc_probe,
1b2ca0ad 986 rsnd_cmd_probe,
d1ac970f
KM
987 rsnd_adg_probe,
988 rsnd_dai_probe,
989 };
990 int ret, i;
1536a968 991
1536a968
KM
992 /*
993 * init priv data
994 */
995 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
996 if (!priv) {
997 dev_err(dev, "priv allocate failed\n");
998 return -ENODEV;
999 }
1000
9f464f8e 1001 priv->pdev = pdev;
c51eb1c6 1002 priv->flags = (unsigned long)of_id->data;
1536a968
KM
1003 spin_lock_init(&priv->lock);
1004
1005 /*
1006 * init each module
1007 */
d1ac970f 1008 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
2ea6b074 1009 ret = probe_func[i](priv);
d1ac970f
KM
1010 if (ret)
1011 return ret;
1012 }
07539c1d 1013
7681f6ac 1014 for_each_rsnd_dai(rdai, priv, i) {
f708d944 1015 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
7681f6ac 1016 if (ret)
d62a3dcd 1017 goto exit_snd_probe;
dfc9403b 1018
f708d944 1019 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
7681f6ac 1020 if (ret)
d62a3dcd 1021 goto exit_snd_probe;
7681f6ac 1022 }
4b4dab82 1023
0b1f6ec7
KM
1024 dev_set_drvdata(dev, priv);
1025
1536a968
KM
1026 /*
1027 * asoc register
1028 */
1029 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1030 if (ret < 0) {
1031 dev_err(dev, "cannot snd soc register\n");
1032 return ret;
1033 }
1034
1035 ret = snd_soc_register_component(dev, &rsnd_soc_component,
ecba9e72 1036 priv->daidrv, rsnd_rdai_nr(priv));
1536a968
KM
1037 if (ret < 0) {
1038 dev_err(dev, "cannot snd dai register\n");
1039 goto exit_snd_soc;
1040 }
1041
1536a968
KM
1042 pm_runtime_enable(dev);
1043
1044 dev_info(dev, "probed\n");
1045 return ret;
1046
1047exit_snd_soc:
1048 snd_soc_unregister_platform(dev);
d62a3dcd
KM
1049exit_snd_probe:
1050 for_each_rsnd_dai(rdai, priv, i) {
690602fc
KM
1051 rsnd_dai_call(remove, &rdai->playback, priv);
1052 rsnd_dai_call(remove, &rdai->capture, priv);
d62a3dcd 1053 }
1536a968
KM
1054
1055 return ret;
1056}
1057
1058static int rsnd_remove(struct platform_device *pdev)
1059{
1060 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
7681f6ac 1061 struct rsnd_dai *rdai;
2ea6b074 1062 void (*remove_func[])(struct rsnd_priv *priv) = {
2f78dd7f 1063 rsnd_ssi_remove,
c7f69ab5 1064 rsnd_ssiu_remove,
2f78dd7f 1065 rsnd_src_remove,
9269e3c3 1066 rsnd_ctu_remove,
70fb1052 1067 rsnd_mix_remove,
2f78dd7f 1068 rsnd_dvc_remove,
1b2ca0ad 1069 rsnd_cmd_remove,
68a55024 1070 rsnd_adg_remove,
2f78dd7f 1071 };
d62a3dcd 1072 int ret = 0, i;
1536a968
KM
1073
1074 pm_runtime_disable(&pdev->dev);
1075
7681f6ac 1076 for_each_rsnd_dai(rdai, priv, i) {
690602fc
KM
1077 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1078 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
7681f6ac 1079 }
1536a968 1080
2f78dd7f 1081 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
2ea6b074 1082 remove_func[i](priv);
2f78dd7f 1083
d7c42ff8
KM
1084 snd_soc_unregister_component(&pdev->dev);
1085 snd_soc_unregister_platform(&pdev->dev);
1086
d62a3dcd 1087 return ret;
1536a968
KM
1088}
1089
1090static struct platform_driver rsnd_driver = {
1091 .driver = {
1092 .name = "rcar_sound",
90e8e50f 1093 .of_match_table = rsnd_of_match,
1536a968
KM
1094 },
1095 .probe = rsnd_probe,
1096 .remove = rsnd_remove,
1097};
1098module_platform_driver(rsnd_driver);
1099
1100MODULE_LICENSE("GPL");
1101MODULE_DESCRIPTION("Renesas R-Car audio driver");
1102MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1103MODULE_ALIAS("platform:rcar-pcm-audio");
This page took 0.198596 seconds and 5 git commands to generate.