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