ASoC: rsnd: don't update status if rsnd_mod_call() doesn't match
[deliverable/linux.git] / sound / soc / sh / rcar / core.c
... / ...
CommitLineData
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 * |
76 * | ** these control src
77 * |
78 * +- src
79 * |
80 * +- src[0]
81 * +- src[1]
82 * +- src[2]
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
102static const struct of_device_id rsnd_of_match[] = {
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 */
106 {},
107};
108MODULE_DEVICE_TABLE(of, rsnd_of_match);
109
110/*
111 * rsnd_mod functions
112 */
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
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
132struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
133 struct rsnd_mod *mod)
134{
135 if (!mod || !mod->ops || !mod->ops->dma_req)
136 return NULL;
137
138 return mod->ops->dma_req(io, mod);
139}
140
141int rsnd_mod_init(struct rsnd_priv *priv,
142 struct rsnd_mod *mod,
143 struct rsnd_mod_ops *ops,
144 struct clk *clk,
145 enum rsnd_mod_type type,
146 int id)
147{
148 int ret = clk_prepare(clk);
149
150 if (ret)
151 return ret;
152
153 mod->id = id;
154 mod->ops = ops;
155 mod->type = type;
156 mod->clk = clk;
157 mod->priv = priv;
158
159 return ret;
160}
161
162void rsnd_mod_quit(struct rsnd_mod *mod)
163{
164 if (mod->clk)
165 clk_unprepare(mod->clk);
166}
167
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;
175 int i;
176
177 for_each_rsnd_dai(rdai, priv, i) {
178 io = &rdai->playback;
179 if (mod == io->mod[mod->type])
180 callback(mod, io);
181
182 io = &rdai->capture;
183 if (mod == io->mod[mod->type])
184 callback(mod, io);
185 }
186}
187
188int rsnd_io_is_working(struct rsnd_dai_stream *io)
189{
190 /* see rsnd_dai_stream_init/quit() */
191 return !!io->substream;
192}
193
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
201int rsnd_get_slot(struct rsnd_dai_stream *io)
202{
203 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
204
205 return rdai->slots;
206}
207
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
215int rsnd_get_slot_width(struct rsnd_dai_stream *io)
216{
217 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
218 int chan = runtime->channels;
219
220 /* Multi channel Mode */
221 if (rsnd_ssi_multi_slaves(io))
222 chan /= rsnd_get_slot_num(io);
223
224 /* TDM Extend Mode needs 8ch */
225 if (chan == 6)
226 chan = 8;
227
228 return chan;
229}
230
231/*
232 * ADINR function
233 */
234u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
235{
236 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
237 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
238 struct device *dev = rsnd_priv_to_dev(priv);
239
240 switch (runtime->sample_bits) {
241 case 16:
242 return 8 << 16;
243 case 32:
244 return 0 << 16;
245 }
246
247 dev_warn(dev, "not supported sample bits\n");
248
249 return 0;
250}
251
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);
255 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
256 struct device *dev = rsnd_priv_to_dev(priv);
257 u32 chan = runtime->channels;
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}
274
275/*
276 * DALIGN function
277 */
278u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
279{
280 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
281 struct rsnd_mod *target;
282 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
283 u32 val = 0x76543210;
284 u32 mask = ~0;
285
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
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
319/*
320 * rsnd_dai functions
321 */
322#define rsnd_mod_call(idx, io, func, param...) \
323({ \
324 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
325 struct rsnd_mod *mod = (io)->mod[idx]; \
326 struct device *dev = rsnd_priv_to_dev(priv); \
327 u32 *status = (io)->mod_status + idx; \
328 u32 mask = 0xF << __rsnd_mod_shift_##func; \
329 u8 val = (*status >> __rsnd_mod_shift_##func) & 0xF; \
330 u8 add = ((val + __rsnd_mod_add_##func) & 0xF); \
331 int ret = 0; \
332 int call = (val == __rsnd_mod_call_##func) && (mod)->ops->func; \
333 if (add == 0xF) \
334 call = 0; \
335 else \
336 *status = (*status & ~mask) + \
337 (add << __rsnd_mod_shift_##func); \
338 dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \
339 rsnd_mod_name(mod), rsnd_mod_id(mod), \
340 *status, call ? #func : ""); \
341 if (call) \
342 ret = (mod)->ops->func(mod, io, param); \
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); \
346 ret; \
347})
348
349#define rsnd_dai_call(fn, io, param...) \
350({ \
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; \
357 ret |= rsnd_mod_call(i, io, fn, param); \
358 } \
359 ret; \
360})
361
362int rsnd_dai_connect(struct rsnd_mod *mod,
363 struct rsnd_dai_stream *io,
364 enum rsnd_mod_type type)
365{
366 struct rsnd_priv *priv;
367 struct device *dev;
368
369 if (!mod)
370 return -EIO;
371
372 if (io->mod[type])
373 return -EINVAL;
374
375 priv = rsnd_mod_to_priv(mod);
376 dev = rsnd_priv_to_dev(priv);
377
378 io->mod[type] = mod;
379
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
384 return 0;
385}
386
387static void rsnd_dai_disconnect(struct rsnd_mod *mod,
388 struct rsnd_dai_stream *io,
389 enum rsnd_mod_type type)
390{
391 io->mod[type] = NULL;
392}
393
394struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
395{
396 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
397 return NULL;
398
399 return priv->rdai + id;
400}
401
402#define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
403static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
404{
405 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
406
407 return rsnd_rdai_get(priv, dai->id);
408}
409
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
424bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
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
441 return true;
442 }
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);
459}
460
461static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
462 struct snd_pcm_substream *substream)
463{
464 struct snd_pcm_runtime *runtime = substream->runtime;
465
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;
473}
474
475static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
476{
477 io->substream = NULL;
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{
501 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
502 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
503 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
504 int ret;
505 unsigned long flags;
506
507 spin_lock_irqsave(&priv->lock, flags);
508
509 switch (cmd) {
510 case SNDRV_PCM_TRIGGER_START:
511 rsnd_dai_stream_init(io, substream);
512
513 ret = rsnd_dai_call(init, io, priv);
514 if (ret < 0)
515 goto dai_trigger_end;
516
517 ret = rsnd_dai_call(start, io, priv);
518 if (ret < 0)
519 goto dai_trigger_end;
520 break;
521 case SNDRV_PCM_TRIGGER_STOP:
522 ret = rsnd_dai_call(stop, io, priv);
523
524 ret |= rsnd_dai_call(quit, io, priv);
525
526 rsnd_dai_stream_quit(io);
527 break;
528 default:
529 ret = -EINVAL;
530 }
531
532dai_trigger_end:
533 spin_unlock_irqrestore(&priv->lock, flags);
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:
545 rdai->clk_master = 0;
546 break;
547 case SND_SOC_DAIFMT_CBS_CFS:
548 rdai->clk_master = 1; /* codec is slave, cpu is master */
549 break;
550 default:
551 return -EINVAL;
552 }
553
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;
559 rdai->frm_clk_inv = 0;
560 break;
561 case SND_SOC_DAIFMT_LEFT_J:
562 rdai->sys_delay = 1;
563 rdai->data_alignment = 0;
564 rdai->frm_clk_inv = 1;
565 break;
566 case SND_SOC_DAIFMT_RIGHT_J:
567 rdai->sys_delay = 1;
568 rdai->data_alignment = 1;
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:
589 break;
590 }
591
592 return 0;
593}
594
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 */
606 rsnd_set_slot(rdai, slots, 1);
607 break;
608 default:
609 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
610 return -EINVAL;
611 }
612
613 return 0;
614}
615
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,
619 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
620};
621
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
649static int rsnd_dai_probe(struct rsnd_priv *priv)
650{
651 struct device_node *dai_node;
652 struct device_node *dai_np;
653 struct device_node *playback, *capture;
654 struct rsnd_dai_stream *io_playback;
655 struct rsnd_dai_stream *io_capture;
656 struct snd_soc_dai_driver *rdrv, *drv;
657 struct rsnd_dai *rdai;
658 struct device *dev = rsnd_priv_to_dev(priv);
659 int nr, dai_i, io_i;
660 int ret;
661
662 dai_node = rsnd_dai_of_node(priv);
663 nr = of_get_child_count(dai_node);
664 if (!nr) {
665 ret = -EINVAL;
666 goto rsnd_dai_probe_done;
667 }
668
669 rdrv = devm_kzalloc(dev, sizeof(*rdrv) * nr, GFP_KERNEL);
670 rdai = devm_kzalloc(dev, sizeof(*rdai) * nr, GFP_KERNEL);
671 if (!rdrv || !rdai) {
672 ret = -ENOMEM;
673 goto rsnd_dai_probe_done;
674 }
675
676 priv->rdai_nr = nr;
677 priv->daidrv = rdrv;
678 priv->rdai = rdai;
679
680 /*
681 * parse all dai
682 */
683 dai_i = 0;
684 for_each_child_of_node(dai_node, dai_np) {
685 rdai = rsnd_rdai_get(priv, dai_i);
686 drv = rdrv + dai_i;
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;
701 drv->playback.channels_max = 6;
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;
709 drv->capture.channels_max = 6;
710 drv->capture.stream_name = rdai->capture.name;
711
712 rdai->playback.rdai = rdai;
713 rdai->capture.rdai = rdai;
714 rsnd_set_slot(rdai, 2, 1); /* default */
715
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);
719
720 if (!playback && !capture)
721 break;
722
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);
728
729 of_node_put(playback);
730 of_node_put(capture);
731 }
732
733 dai_i++;
734
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" : " -- ");
738 }
739
740 ret = 0;
741
742rsnd_dai_probe_done:
743 of_node_put(dai_node);
744
745 return ret;
746}
747
748/*
749 * pcm ops
750 */
751static struct snd_pcm_hardware rsnd_pcm_hardware = {
752 .info = SNDRV_PCM_INFO_INTERLEAVED |
753 SNDRV_PCM_INFO_MMAP |
754 SNDRV_PCM_INFO_MMAP_VALID,
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{
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
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
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)
873 cfg->update(cfg->io, mod);
874
875 return change;
876}
877
878static int __rsnd_kctrl_new(struct rsnd_mod *mod,
879 struct rsnd_dai_stream *io,
880 struct snd_soc_pcm_runtime *rtd,
881 const unsigned char *name,
882 struct rsnd_kctrl_cfg *cfg,
883 void (*update)(struct rsnd_dai_stream *io,
884 struct rsnd_mod *mod))
885{
886 struct snd_soc_card *soc_card = rtd->card;
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,
893 .index = rtd - soc_card->rtd,
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);
905 if (ret < 0) {
906 snd_ctl_free_one(kctrl);
907 return ret;
908 }
909
910 cfg->update = update;
911 cfg->card = card;
912 cfg->kctrl = kctrl;
913 cfg->io = io;
914
915 return 0;
916}
917
918void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
919{
920 snd_ctl_remove(cfg->card, cfg->kctrl);
921}
922
923int rsnd_kctrl_new_m(struct rsnd_mod *mod,
924 struct rsnd_dai_stream *io,
925 struct snd_soc_pcm_runtime *rtd,
926 const unsigned char *name,
927 void (*update)(struct rsnd_dai_stream *io,
928 struct rsnd_mod *mod),
929 struct rsnd_kctrl_cfg_m *_cfg,
930 int ch_size,
931 u32 max)
932{
933 if (ch_size > RSND_DVC_CHANNELS)
934 return -EINVAL;
935
936 _cfg->cfg.max = max;
937 _cfg->cfg.size = ch_size;
938 _cfg->cfg.val = _cfg->val;
939 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
940}
941
942int rsnd_kctrl_new_s(struct rsnd_mod *mod,
943 struct rsnd_dai_stream *io,
944 struct snd_soc_pcm_runtime *rtd,
945 const unsigned char *name,
946 void (*update)(struct rsnd_dai_stream *io,
947 struct rsnd_mod *mod),
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;
954 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
955}
956
957int rsnd_kctrl_new_e(struct rsnd_mod *mod,
958 struct rsnd_dai_stream *io,
959 struct snd_soc_pcm_runtime *rtd,
960 const unsigned char *name,
961 struct rsnd_kctrl_cfg_s *_cfg,
962 void (*update)(struct rsnd_dai_stream *io,
963 struct rsnd_mod *mod),
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;
971 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
972}
973
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{
983 struct snd_soc_dai *dai = rtd->cpu_dai;
984 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
985 int ret;
986
987 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
988 if (ret)
989 return ret;
990
991 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
992 if (ret)
993 return ret;
994
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
1002static struct snd_soc_platform_driver rsnd_soc_platform = {
1003 .ops = &rsnd_pcm_ops,
1004 .pcm_new = rsnd_pcm_new,
1005};
1006
1007static const struct snd_soc_component_driver rsnd_soc_component = {
1008 .name = "rsnd",
1009};
1010
1011static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1012 struct rsnd_dai_stream *io)
1013{
1014 int ret;
1015
1016 ret = rsnd_dai_call(probe, io, priv);
1017 if (ret == -EAGAIN) {
1018 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1019 int i;
1020
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()
1030 * rsnd_ssi_fallback()
1031 */
1032 rsnd_dai_call(remove, io, priv);
1033
1034 /*
1035 * remove all mod from io
1036 * and, re connect ssi
1037 */
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);
1041
1042 /*
1043 * fallback
1044 */
1045 rsnd_dai_call(fallback, io, priv);
1046
1047 /*
1048 * retry to "probe".
1049 * DAI has SSI which is PIO mode only now.
1050 */
1051 ret = rsnd_dai_call(probe, io, priv);
1052 }
1053
1054 return ret;
1055}
1056
1057/*
1058 * rsnd probe
1059 */
1060static int rsnd_probe(struct platform_device *pdev)
1061{
1062 struct rsnd_priv *priv;
1063 struct device *dev = &pdev->dev;
1064 struct rsnd_dai *rdai;
1065 const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
1066 int (*probe_func[])(struct rsnd_priv *priv) = {
1067 rsnd_gen_probe,
1068 rsnd_dma_probe,
1069 rsnd_ssi_probe,
1070 rsnd_ssiu_probe,
1071 rsnd_src_probe,
1072 rsnd_ctu_probe,
1073 rsnd_mix_probe,
1074 rsnd_dvc_probe,
1075 rsnd_cmd_probe,
1076 rsnd_adg_probe,
1077 rsnd_dai_probe,
1078 };
1079 int ret, i;
1080
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
1090 priv->pdev = pdev;
1091 priv->flags = (unsigned long)of_id->data;
1092 spin_lock_init(&priv->lock);
1093
1094 /*
1095 * init each module
1096 */
1097 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1098 ret = probe_func[i](priv);
1099 if (ret)
1100 return ret;
1101 }
1102
1103 for_each_rsnd_dai(rdai, priv, i) {
1104 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1105 if (ret)
1106 goto exit_snd_probe;
1107
1108 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1109 if (ret)
1110 goto exit_snd_probe;
1111 }
1112
1113 dev_set_drvdata(dev, priv);
1114
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,
1125 priv->daidrv, rsnd_rdai_nr(priv));
1126 if (ret < 0) {
1127 dev_err(dev, "cannot snd dai register\n");
1128 goto exit_snd_soc;
1129 }
1130
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);
1138exit_snd_probe:
1139 for_each_rsnd_dai(rdai, priv, i) {
1140 rsnd_dai_call(remove, &rdai->playback, priv);
1141 rsnd_dai_call(remove, &rdai->capture, priv);
1142 }
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);
1150 struct rsnd_dai *rdai;
1151 void (*remove_func[])(struct rsnd_priv *priv) = {
1152 rsnd_ssi_remove,
1153 rsnd_ssiu_remove,
1154 rsnd_src_remove,
1155 rsnd_ctu_remove,
1156 rsnd_mix_remove,
1157 rsnd_dvc_remove,
1158 rsnd_cmd_remove,
1159 rsnd_adg_remove,
1160 };
1161 int ret = 0, i;
1162
1163 pm_runtime_disable(&pdev->dev);
1164
1165 for_each_rsnd_dai(rdai, priv, i) {
1166 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1167 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1168 }
1169
1170 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1171 remove_func[i](priv);
1172
1173 snd_soc_unregister_component(&pdev->dev);
1174 snd_soc_unregister_platform(&pdev->dev);
1175
1176 return ret;
1177}
1178
1179static struct platform_driver rsnd_driver = {
1180 .driver = {
1181 .name = "rcar_sound",
1182 .of_match_table = rsnd_of_match,
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.066782 seconds and 5 git commands to generate.