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