1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Renesas R-Car SRU/SCU/SSIU/SSI support
4 //
5 // Copyright (C) 2013 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 //
8 // Based on fsi.c
9 // Kuninori Morimoto <morimoto.kuninori@renesas.com>
10
11 /*
12 * Renesas R-Car sound device structure
13 *
14 * Gen1
15 *
16 * SRU : Sound Routing Unit
17 * - SRC : Sampling Rate Converter
18 * - CMD
19 * - CTU : Channel Count Conversion Unit
20 * - MIX : Mixer
21 * - DVC : Digital Volume and Mute Function
22 * - SSI : Serial Sound Interface
23 *
24 * Gen2
25 *
26 * SCU : Sampling Rate Converter Unit
27 * - SRC : Sampling Rate Converter
28 * - CMD
29 * - CTU : Channel Count Conversion Unit
30 * - MIX : Mixer
31 * - DVC : Digital Volume and Mute Function
32 * SSIU : Serial Sound Interface Unit
33 * - SSI : Serial Sound Interface
34 */
35
36 /*
37 * driver data Image
38 *
39 * rsnd_priv
40 * |
41 * | ** this depends on Gen1/Gen2
42 * |
43 * +- gen
44 * |
45 * | ** these depend on data path
46 * | ** gen and platform data control it
47 * |
48 * +- rdai[0]
49 * | | sru ssiu ssi
50 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
51 * | |
52 * | | sru ssiu ssi
53 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
54 * |
55 * +- rdai[1]
56 * | | sru ssiu ssi
57 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
58 * | |
59 * | | sru ssiu ssi
60 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
61 * ...
62 * |
63 * | ** these control ssi
64 * |
65 * +- ssi
66 * | |
67 * | +- ssi[0]
68 * | +- ssi[1]
69 * | +- ssi[2]
70 * | ...
71 * |
72 * | ** these control src
73 * |
74 * +- src
75 * |
76 * +- src[0]
77 * +- src[1]
78 * +- src[2]
79 * ...
80 *
81 *
82 * for_each_rsnd_dai(xx, priv, xx)
83 * rdai[0] => rdai[1] => rdai[2] => ...
84 *
85 * for_each_rsnd_mod(xx, rdai, xx)
86 * [mod] => [mod] => [mod] => ...
87 *
88 * rsnd_dai_call(xxx, fn )
89 * [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
90 *
91 */
92
93 #include <linux/pm_runtime.h>
94 #include <linux/of_graph.h>
95 #include "rsnd.h"
96
97 #define RSND_RATES SNDRV_PCM_RATE_8000_192000
98 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S8 |\
99 SNDRV_PCM_FMTBIT_S16_LE |\
100 SNDRV_PCM_FMTBIT_S24_LE)
101
102 static 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_GEN3 },
106 { .compatible = "renesas,rcar_sound-gen4", .data = (void *)RSND_GEN4 },
107 /* Special Handling */
108 { .compatible = "renesas,rcar_sound-r8a77990", .data = (void *)(RSND_GEN3 | RSND_SOC_E) },
109 {},
110 };
111 MODULE_DEVICE_TABLE(of, rsnd_of_match);
112
113 /*
114 * rsnd_mod functions
115 */
rsnd_mod_make_sure(struct rsnd_mod * mod,enum rsnd_mod_type type)116 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
117 {
118 if (mod->type != type) {
119 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
120 struct device *dev = rsnd_priv_to_dev(priv);
121
122 dev_warn(dev, "%s is not your expected module\n",
123 rsnd_mod_name(mod));
124 }
125 }
126
rsnd_mod_dma_req(struct rsnd_dai_stream * io,struct rsnd_mod * mod)127 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
128 struct rsnd_mod *mod)
129 {
130 if (!mod || !mod->ops || !mod->ops->dma_req)
131 return NULL;
132
133 return mod->ops->dma_req(io, mod);
134 }
135
136 #define MOD_NAME_NUM 5
137 #define MOD_NAME_SIZE 16
rsnd_mod_name(struct rsnd_mod * mod)138 char *rsnd_mod_name(struct rsnd_mod *mod)
139 {
140 static char names[MOD_NAME_NUM][MOD_NAME_SIZE];
141 static int num;
142 char *name = names[num];
143
144 num++;
145 if (num >= MOD_NAME_NUM)
146 num = 0;
147
148 /*
149 * Let's use same char to avoid pointlessness memory
150 * Thus, rsnd_mod_name() should be used immediately
151 * Don't keep pointer
152 */
153 if ((mod)->ops->id_sub) {
154 snprintf(name, MOD_NAME_SIZE, "%s[%d%d]",
155 mod->ops->name,
156 rsnd_mod_id(mod),
157 rsnd_mod_id_sub(mod));
158 } else {
159 snprintf(name, MOD_NAME_SIZE, "%s[%d]",
160 mod->ops->name,
161 rsnd_mod_id(mod));
162 }
163
164 return name;
165 }
166
rsnd_mod_get_status(struct rsnd_mod * mod,struct rsnd_dai_stream * io,enum rsnd_mod_type type)167 u32 *rsnd_mod_get_status(struct rsnd_mod *mod,
168 struct rsnd_dai_stream *io,
169 enum rsnd_mod_type type)
170 {
171 return &mod->status;
172 }
173
rsnd_mod_id_raw(struct rsnd_mod * mod)174 int rsnd_mod_id_raw(struct rsnd_mod *mod)
175 {
176 return mod->id;
177 }
178
rsnd_mod_id(struct rsnd_mod * mod)179 int rsnd_mod_id(struct rsnd_mod *mod)
180 {
181 if ((mod)->ops->id)
182 return (mod)->ops->id(mod);
183
184 return rsnd_mod_id_raw(mod);
185 }
186
rsnd_mod_id_sub(struct rsnd_mod * mod)187 int rsnd_mod_id_sub(struct rsnd_mod *mod)
188 {
189 if ((mod)->ops->id_sub)
190 return (mod)->ops->id_sub(mod);
191
192 return 0;
193 }
194
rsnd_mod_init(struct rsnd_priv * priv,struct rsnd_mod * mod,struct rsnd_mod_ops * ops,struct clk * clk,enum rsnd_mod_type type,int id)195 int rsnd_mod_init(struct rsnd_priv *priv,
196 struct rsnd_mod *mod,
197 struct rsnd_mod_ops *ops,
198 struct clk *clk,
199 enum rsnd_mod_type type,
200 int id)
201 {
202 int ret = clk_prepare(clk);
203
204 if (ret)
205 return ret;
206
207 mod->id = id;
208 mod->ops = ops;
209 mod->type = type;
210 mod->clk = clk;
211 mod->priv = priv;
212
213 return 0;
214 }
215
rsnd_mod_quit(struct rsnd_mod * mod)216 void rsnd_mod_quit(struct rsnd_mod *mod)
217 {
218 clk_unprepare(mod->clk);
219 mod->clk = NULL;
220 }
221
rsnd_mod_interrupt(struct rsnd_mod * mod,void (* callback)(struct rsnd_mod * mod,struct rsnd_dai_stream * io))222 void rsnd_mod_interrupt(struct rsnd_mod *mod,
223 void (*callback)(struct rsnd_mod *mod,
224 struct rsnd_dai_stream *io))
225 {
226 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
227 struct rsnd_dai *rdai;
228 int i;
229
230 for_each_rsnd_dai(rdai, priv, i) {
231 struct rsnd_dai_stream *io = &rdai->playback;
232
233 if (mod == io->mod[mod->type])
234 callback(mod, io);
235
236 io = &rdai->capture;
237 if (mod == io->mod[mod->type])
238 callback(mod, io);
239 }
240 }
241
rsnd_io_is_working(struct rsnd_dai_stream * io)242 int rsnd_io_is_working(struct rsnd_dai_stream *io)
243 {
244 /* see rsnd_dai_stream_init/quit() */
245 if (io->substream)
246 return snd_pcm_running(io->substream);
247
248 return 0;
249 }
250
rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream * io,struct snd_pcm_hw_params * params)251 int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
252 struct snd_pcm_hw_params *params)
253 {
254 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
255
256 /*
257 * params will be added when refine
258 * see
259 * __rsnd_soc_hw_rule_rate()
260 * __rsnd_soc_hw_rule_channels()
261 */
262 if (params)
263 return params_channels(params);
264 else if (runtime)
265 return runtime->channels;
266 return 0;
267 }
268
rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream * io,struct snd_pcm_hw_params * params)269 int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
270 struct snd_pcm_hw_params *params)
271 {
272 int chan = rsnd_runtime_channel_original_with_params(io, params);
273 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
274
275 if (ctu_mod) {
276 u32 converted_chan = rsnd_io_converted_chan(io);
277
278 /*
279 * !! Note !!
280 *
281 * converted_chan will be used for CTU,
282 * or TDM Split mode.
283 * User shouldn't use CTU with TDM Split mode.
284 */
285 if (rsnd_runtime_is_tdm_split(io)) {
286 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));
287
288 dev_err(dev, "CTU and TDM Split should be used\n");
289 }
290
291 if (converted_chan)
292 return converted_chan;
293 }
294
295 return chan;
296 }
297
rsnd_channel_normalization(int chan)298 int rsnd_channel_normalization(int chan)
299 {
300 if (WARN_ON((chan > 8) || (chan < 0)))
301 return 0;
302
303 /* TDM Extend Mode needs 8ch */
304 if (chan == 6)
305 chan = 8;
306
307 return chan;
308 }
309
rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream * io,struct snd_pcm_hw_params * params)310 int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
311 struct snd_pcm_hw_params *params)
312 {
313 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
314 int chan = rsnd_io_is_play(io) ?
315 rsnd_runtime_channel_after_ctu_with_params(io, params) :
316 rsnd_runtime_channel_original_with_params(io, params);
317
318 /* Use Multi SSI */
319 if (rsnd_runtime_is_multi_ssi(io))
320 chan /= rsnd_rdai_ssi_lane_get(rdai);
321
322 return rsnd_channel_normalization(chan);
323 }
324
rsnd_runtime_is_multi_ssi(struct rsnd_dai_stream * io)325 int rsnd_runtime_is_multi_ssi(struct rsnd_dai_stream *io)
326 {
327 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
328 int lane = rsnd_rdai_ssi_lane_get(rdai);
329 int chan = rsnd_io_is_play(io) ?
330 rsnd_runtime_channel_after_ctu(io) :
331 rsnd_runtime_channel_original(io);
332
333 return (chan > 2) && (lane > 1);
334 }
335
rsnd_runtime_is_tdm(struct rsnd_dai_stream * io)336 int rsnd_runtime_is_tdm(struct rsnd_dai_stream *io)
337 {
338 return rsnd_runtime_channel_for_ssi(io) >= 6;
339 }
340
rsnd_runtime_is_tdm_split(struct rsnd_dai_stream * io)341 int rsnd_runtime_is_tdm_split(struct rsnd_dai_stream *io)
342 {
343 return !!rsnd_flags_has(io, RSND_STREAM_TDM_SPLIT);
344 }
345
346 /*
347 * ADINR function
348 */
rsnd_get_adinr_bit(struct rsnd_mod * mod,struct rsnd_dai_stream * io)349 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
350 {
351 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
352 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
353 struct device *dev = rsnd_priv_to_dev(priv);
354
355 switch (snd_pcm_format_width(runtime->format)) {
356 case 8:
357 return 16 << 16;
358 case 16:
359 return 8 << 16;
360 case 24:
361 return 0 << 16;
362 }
363
364 dev_warn(dev, "not supported sample bits\n");
365
366 return 0;
367 }
368
369 /*
370 * DALIGN function
371 */
rsnd_get_dalign(struct rsnd_mod * mod,struct rsnd_dai_stream * io)372 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
373 {
374 static const u32 dalign_values[8] = {
375 0x76543210, 0x00000032, 0x00007654, 0x00000076,
376 0xfedcba98, 0x000000ba, 0x0000fedc, 0x000000fe,
377 };
378 int id = 0;
379 struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
380 struct rsnd_mod *target;
381 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
382 u32 dalign;
383
384 /*
385 * *Hardware* L/R and *Software* L/R are inverted for 16bit data.
386 * 31..16 15...0
387 * HW: [L ch] [R ch]
388 * SW: [R ch] [L ch]
389 * We need to care about inversion timing to control
390 * Playback/Capture correctly.
391 * The point is [DVC] needs *Hardware* L/R, [MEM] needs *Software* L/R
392 *
393 * sL/R : software L/R
394 * hL/R : hardware L/R
395 * (*) : conversion timing
396 *
397 * Playback
398 * sL/R (*) hL/R hL/R hL/R hL/R hL/R
399 * [MEM] -> [SRC] -> [DVC] -> [CMD] -> [SSIU] -> [SSI] -> codec
400 *
401 * Capture
402 * hL/R hL/R hL/R hL/R hL/R (*) sL/R
403 * codec -> [SSI] -> [SSIU] -> [SRC] -> [DVC] -> [CMD] -> [MEM]
404 */
405 if (rsnd_io_is_play(io)) {
406 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
407
408 target = src ? src : ssiu;
409 } else {
410 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
411
412 target = cmd ? cmd : ssiu;
413 }
414
415 if (mod == ssiu)
416 id = rsnd_mod_id_sub(mod);
417
418 dalign = dalign_values[id];
419
420 if (mod == target && snd_pcm_format_width(runtime->format) == 16) {
421 /* Target mod needs inverted DALIGN when 16bit */
422 dalign = (dalign & 0xf0f0f0f0) >> 4 |
423 (dalign & 0x0f0f0f0f) << 4;
424 }
425
426 return dalign;
427 }
428
rsnd_get_busif_shift(struct rsnd_dai_stream * io,struct rsnd_mod * mod)429 u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
430 {
431 static const enum rsnd_mod_type playback_mods[] = {
432 RSND_MOD_SRC,
433 RSND_MOD_CMD,
434 RSND_MOD_SSIU,
435 };
436 static const enum rsnd_mod_type capture_mods[] = {
437 RSND_MOD_CMD,
438 RSND_MOD_SRC,
439 RSND_MOD_SSIU,
440 };
441 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
442 struct rsnd_mod *tmod = NULL;
443 const enum rsnd_mod_type *mods =
444 rsnd_io_is_play(io) ?
445 playback_mods : capture_mods;
446 int i;
447
448 /*
449 * This is needed for 24bit data
450 * We need to shift 8bit
451 *
452 * Linux 24bit data is located as 0x00******
453 * HW 24bit data is located as 0x******00
454 *
455 */
456 if (snd_pcm_format_width(runtime->format) != 24)
457 return 0;
458
459 for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
460 tmod = rsnd_io_to_mod(io, mods[i]);
461 if (tmod)
462 break;
463 }
464
465 if (tmod != mod)
466 return 0;
467
468 if (rsnd_io_is_play(io))
469 return (0 << 20) | /* shift to Left */
470 (8 << 16); /* 8bit */
471 else
472 return (1 << 20) | /* shift to Right */
473 (8 << 16); /* 8bit */
474 }
475
476 /*
477 * rsnd_dai functions
478 */
rsnd_mod_next(int * iterator,struct rsnd_dai_stream * io,enum rsnd_mod_type * array,int array_size)479 struct rsnd_mod *rsnd_mod_next(int *iterator,
480 struct rsnd_dai_stream *io,
481 enum rsnd_mod_type *array,
482 int array_size)
483 {
484 int max = array ? array_size : RSND_MOD_MAX;
485
486 for (; *iterator < max; (*iterator)++) {
487 enum rsnd_mod_type type = (array) ? array[*iterator] : *iterator;
488 struct rsnd_mod *mod = rsnd_io_to_mod(io, type);
489
490 if (mod)
491 return mod;
492 }
493
494 return NULL;
495 }
496
497 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
498 {
499 /* CAPTURE */
500 RSND_MOD_AUDMAPP,
501 RSND_MOD_AUDMA,
502 RSND_MOD_DVC,
503 RSND_MOD_MIX,
504 RSND_MOD_CTU,
505 RSND_MOD_CMD,
506 RSND_MOD_SRC,
507 RSND_MOD_SSIU,
508 RSND_MOD_SSIM3,
509 RSND_MOD_SSIM2,
510 RSND_MOD_SSIM1,
511 RSND_MOD_SSIP,
512 RSND_MOD_SSI,
513 }, {
514 /* PLAYBACK */
515 RSND_MOD_AUDMAPP,
516 RSND_MOD_AUDMA,
517 RSND_MOD_SSIM3,
518 RSND_MOD_SSIM2,
519 RSND_MOD_SSIM1,
520 RSND_MOD_SSIP,
521 RSND_MOD_SSI,
522 RSND_MOD_SSIU,
523 RSND_MOD_DVC,
524 RSND_MOD_MIX,
525 RSND_MOD_CTU,
526 RSND_MOD_CMD,
527 RSND_MOD_SRC,
528 },
529 };
530
rsnd_status_update(struct rsnd_dai_stream * io,struct rsnd_mod * mod,enum rsnd_mod_type type,int shift,int add,int timing)531 static int rsnd_status_update(struct rsnd_dai_stream *io,
532 struct rsnd_mod *mod, enum rsnd_mod_type type,
533 int shift, int add, int timing)
534 {
535 u32 *status = mod->ops->get_status(mod, io, type);
536 u32 mask = 0xF << shift;
537 u8 val = (*status >> shift) & 0xF;
538 u8 next_val = (val + add) & 0xF;
539 int func_call = (val == timing);
540
541 /* no status update */
542 if (add == 0 || shift == 28)
543 return 1;
544
545 if (next_val == 0xF) /* underflow case */
546 func_call = -1;
547 else
548 *status = (*status & ~mask) + (next_val << shift);
549
550 return func_call;
551 }
552
553 #define rsnd_dai_call(fn, io, param...) \
554 ({ \
555 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io)); \
556 struct rsnd_mod *mod; \
557 int is_play = rsnd_io_is_play(io); \
558 int ret = 0, i; \
559 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
560 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
561 int tmp = 0; \
562 int func_call = rsnd_status_update(io, mod, types[i], \
563 __rsnd_mod_shift_##fn, \
564 __rsnd_mod_add_##fn, \
565 __rsnd_mod_call_##fn); \
566 if (func_call > 0 && (mod)->ops->fn) \
567 tmp = (mod)->ops->fn(mod, io, param); \
568 if (unlikely(func_call < 0) || \
569 unlikely(tmp && (tmp != -EPROBE_DEFER))) \
570 dev_err(dev, "%s : %s error (%d, %d)\n", \
571 rsnd_mod_name(mod), #fn, tmp, func_call);\
572 ret |= tmp; \
573 } \
574 ret; \
575 })
576
rsnd_dai_connect(struct rsnd_mod * mod,struct rsnd_dai_stream * io,enum rsnd_mod_type type)577 int rsnd_dai_connect(struct rsnd_mod *mod,
578 struct rsnd_dai_stream *io,
579 enum rsnd_mod_type type)
580 {
581 struct rsnd_priv *priv;
582 struct device *dev;
583
584 if (!mod)
585 return -EIO;
586
587 if (io->mod[type] == mod)
588 return 0;
589
590 if (io->mod[type])
591 return -EINVAL;
592
593 priv = rsnd_mod_to_priv(mod);
594 dev = rsnd_priv_to_dev(priv);
595
596 io->mod[type] = mod;
597
598 dev_dbg(dev, "%s is connected to io (%s)\n",
599 rsnd_mod_name(mod),
600 rsnd_io_is_play(io) ? "Playback" : "Capture");
601
602 return 0;
603 }
604
rsnd_dai_disconnect(struct rsnd_mod * mod,struct rsnd_dai_stream * io,enum rsnd_mod_type type)605 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
606 struct rsnd_dai_stream *io,
607 enum rsnd_mod_type type)
608 {
609 io->mod[type] = NULL;
610 }
611
rsnd_rdai_channels_ctrl(struct rsnd_dai * rdai,int max_channels)612 int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
613 int max_channels)
614 {
615 if (max_channels > 0)
616 rdai->max_channels = max_channels;
617
618 return rdai->max_channels;
619 }
620
rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai * rdai,int ssi_lane)621 int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
622 int ssi_lane)
623 {
624 if (ssi_lane > 0)
625 rdai->ssi_lane = ssi_lane;
626
627 return rdai->ssi_lane;
628 }
629
rsnd_rdai_width_ctrl(struct rsnd_dai * rdai,int width)630 int rsnd_rdai_width_ctrl(struct rsnd_dai *rdai, int width)
631 {
632 if (width > 0)
633 rdai->chan_width = width;
634
635 return rdai->chan_width;
636 }
637
rsnd_rdai_get(struct rsnd_priv * priv,int id)638 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
639 {
640 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
641 return NULL;
642
643 return priv->rdai + id;
644 }
645
646 static struct snd_soc_dai_driver
rsnd_daidrv_get(struct rsnd_priv * priv,int id)647 *rsnd_daidrv_get(struct rsnd_priv *priv, int id)
648 {
649 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
650 return NULL;
651
652 return priv->daidrv + id;
653 }
654
655 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
rsnd_dai_to_rdai(struct snd_soc_dai * dai)656 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
657 {
658 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
659
660 return rsnd_rdai_get(priv, dai->id);
661 }
662
663 /*
664 * rsnd_soc_dai functions
665 */
rsnd_dai_period_elapsed(struct rsnd_dai_stream * io)666 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
667 {
668 struct snd_pcm_substream *substream = io->substream;
669
670 /*
671 * this function should be called...
672 *
673 * - if rsnd_dai_pointer_update() returns true
674 * - without spin lock
675 */
676
677 snd_pcm_period_elapsed(substream);
678 }
679
rsnd_dai_stream_init(struct rsnd_dai_stream * io,struct snd_pcm_substream * substream)680 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
681 struct snd_pcm_substream *substream)
682 {
683 io->substream = substream;
684 }
685
rsnd_dai_stream_quit(struct rsnd_dai_stream * io)686 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
687 {
688 io->substream = NULL;
689 }
690
691 static
rsnd_substream_to_dai(struct snd_pcm_substream * substream)692 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
693 {
694 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
695
696 return snd_soc_rtd_to_cpu(rtd, 0);
697 }
698
699 static
rsnd_rdai_to_io(struct rsnd_dai * rdai,struct snd_pcm_substream * substream)700 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
701 struct snd_pcm_substream *substream)
702 {
703 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
704 return &rdai->playback;
705 else
706 return &rdai->capture;
707 }
708
rsnd_soc_dai_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)709 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
710 struct snd_soc_dai *dai)
711 {
712 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
713 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
714 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
715 int ret;
716 unsigned long flags;
717
718 spin_lock_irqsave(&priv->lock, flags);
719
720 switch (cmd) {
721 case SNDRV_PCM_TRIGGER_START:
722 case SNDRV_PCM_TRIGGER_RESUME:
723 ret = rsnd_dai_call(init, io, priv);
724 if (ret < 0)
725 goto dai_trigger_end;
726
727 ret = rsnd_dai_call(start, io, priv);
728 if (ret < 0)
729 goto dai_trigger_end;
730
731 ret = rsnd_dai_call(irq, io, priv, 1);
732 if (ret < 0)
733 goto dai_trigger_end;
734
735 break;
736 case SNDRV_PCM_TRIGGER_STOP:
737 case SNDRV_PCM_TRIGGER_SUSPEND:
738 ret = rsnd_dai_call(irq, io, priv, 0);
739
740 ret |= rsnd_dai_call(stop, io, priv);
741
742 ret |= rsnd_dai_call(quit, io, priv);
743
744 break;
745 default:
746 ret = -EINVAL;
747 }
748
749 dai_trigger_end:
750 spin_unlock_irqrestore(&priv->lock, flags);
751
752 return ret;
753 }
754
rsnd_soc_dai_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)755 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
756 {
757 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
758
759 /* set clock master for audio interface */
760 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
761 case SND_SOC_DAIFMT_BC_FC:
762 rdai->clk_master = 0;
763 break;
764 case SND_SOC_DAIFMT_BP_FP:
765 rdai->clk_master = 1; /* cpu is master */
766 break;
767 default:
768 return -EINVAL;
769 }
770
771 /* set format */
772 rdai->bit_clk_inv = 0;
773 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
774 case SND_SOC_DAIFMT_I2S:
775 rdai->sys_delay = 0;
776 rdai->data_alignment = 0;
777 rdai->frm_clk_inv = 0;
778 break;
779 case SND_SOC_DAIFMT_LEFT_J:
780 case SND_SOC_DAIFMT_DSP_B:
781 rdai->sys_delay = 1;
782 rdai->data_alignment = 0;
783 rdai->frm_clk_inv = 1;
784 break;
785 case SND_SOC_DAIFMT_RIGHT_J:
786 rdai->sys_delay = 1;
787 rdai->data_alignment = 1;
788 rdai->frm_clk_inv = 1;
789 break;
790 case SND_SOC_DAIFMT_DSP_A:
791 rdai->sys_delay = 0;
792 rdai->data_alignment = 0;
793 rdai->frm_clk_inv = 1;
794 break;
795 }
796
797 /* set clock inversion */
798 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
799 case SND_SOC_DAIFMT_NB_IF:
800 rdai->frm_clk_inv = !rdai->frm_clk_inv;
801 break;
802 case SND_SOC_DAIFMT_IB_NF:
803 rdai->bit_clk_inv = !rdai->bit_clk_inv;
804 break;
805 case SND_SOC_DAIFMT_IB_IF:
806 rdai->bit_clk_inv = !rdai->bit_clk_inv;
807 rdai->frm_clk_inv = !rdai->frm_clk_inv;
808 break;
809 case SND_SOC_DAIFMT_NB_NF:
810 default:
811 break;
812 }
813
814 return 0;
815 }
816
rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai * dai,u32 tx_mask,u32 rx_mask,int slots,int slot_width)817 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
818 u32 tx_mask, u32 rx_mask,
819 int slots, int slot_width)
820 {
821 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
822 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
823 struct device *dev = rsnd_priv_to_dev(priv);
824
825 switch (slot_width) {
826 case 16:
827 case 24:
828 case 32:
829 break;
830 default:
831 /* use default */
832 /*
833 * Indicate warning if DT has "dai-tdm-slot-width"
834 * but the value was not expected.
835 */
836 if (slot_width)
837 dev_warn(dev, "unsupported TDM slot width (%d), force to use default 32\n",
838 slot_width);
839 slot_width = 32;
840 }
841
842 switch (slots) {
843 case 2:
844 /* TDM Split Mode */
845 case 6:
846 case 8:
847 /* TDM Extend Mode */
848 rsnd_rdai_channels_set(rdai, slots);
849 rsnd_rdai_ssi_lane_set(rdai, 1);
850 rsnd_rdai_width_set(rdai, slot_width);
851 break;
852 default:
853 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
854 return -EINVAL;
855 }
856
857 return 0;
858 }
859
860 static unsigned int rsnd_soc_hw_channels_list[] = {
861 2, 6, 8,
862 };
863
864 static unsigned int rsnd_soc_hw_rate_list[] = {
865 8000,
866 11025,
867 16000,
868 22050,
869 32000,
870 44100,
871 48000,
872 64000,
873 88200,
874 96000,
875 176400,
876 192000,
877 };
878
rsnd_soc_hw_rule(struct rsnd_dai * rdai,unsigned int * list,int list_num,struct snd_interval * baseline,struct snd_interval * iv,struct rsnd_dai_stream * io,char * unit)879 static int rsnd_soc_hw_rule(struct rsnd_dai *rdai,
880 unsigned int *list, int list_num,
881 struct snd_interval *baseline, struct snd_interval *iv,
882 struct rsnd_dai_stream *io, char *unit)
883 {
884 struct snd_interval p;
885 unsigned int rate;
886 int i;
887
888 snd_interval_any(&p);
889 p.min = UINT_MAX;
890 p.max = 0;
891
892 for (i = 0; i < list_num; i++) {
893
894 if (!snd_interval_test(iv, list[i]))
895 continue;
896
897 rate = rsnd_ssi_clk_query(rdai,
898 baseline->min, list[i], NULL);
899 if (rate > 0) {
900 p.min = min(p.min, list[i]);
901 p.max = max(p.max, list[i]);
902 }
903
904 rate = rsnd_ssi_clk_query(rdai,
905 baseline->max, list[i], NULL);
906 if (rate > 0) {
907 p.min = min(p.min, list[i]);
908 p.max = max(p.max, list[i]);
909 }
910 }
911
912 /* Indicate error once if it can't handle */
913 if (!rsnd_flags_has(io, RSND_HW_RULE_ERR) && (p.min > p.max)) {
914 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
915 struct device *dev = rsnd_priv_to_dev(priv);
916
917 dev_warn(dev, "It can't handle %d %s <-> %d %s\n",
918 baseline->min, unit, baseline->max, unit);
919 rsnd_flags_set(io, RSND_HW_RULE_ERR);
920 }
921
922 return snd_interval_refine(iv, &p);
923 }
924
rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)925 static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
926 struct snd_pcm_hw_rule *rule)
927 {
928 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
929 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
930 struct snd_interval ic;
931 struct rsnd_dai_stream *io = rule->private;
932 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
933
934 /*
935 * possible sampling rate limitation is same as
936 * 2ch if it supports multi ssi
937 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
938 */
939 ic = *ic_;
940 ic.min =
941 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
942
943 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_rate_list,
944 ARRAY_SIZE(rsnd_soc_hw_rate_list),
945 &ic, ir, io, "ch");
946 }
947
rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)948 static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
949 struct snd_pcm_hw_rule *rule)
950 {
951 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
952 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
953 struct snd_interval ic;
954 struct rsnd_dai_stream *io = rule->private;
955 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
956
957 /*
958 * possible sampling rate limitation is same as
959 * 2ch if it supports multi ssi
960 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
961 */
962 ic = *ic_;
963 ic.min =
964 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
965
966 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_channels_list,
967 ARRAY_SIZE(rsnd_soc_hw_channels_list),
968 ir, &ic, io, "Hz");
969 }
970
971 static const struct snd_pcm_hardware rsnd_pcm_hardware = {
972 .info = SNDRV_PCM_INFO_INTERLEAVED |
973 SNDRV_PCM_INFO_MMAP |
974 SNDRV_PCM_INFO_MMAP_VALID,
975 .buffer_bytes_max = 64 * 1024,
976 .period_bytes_min = 32,
977 .period_bytes_max = 8192,
978 .periods_min = 1,
979 .periods_max = 32,
980 .fifo_size = 256,
981 };
982
rsnd_soc_dai_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)983 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
984 struct snd_soc_dai *dai)
985 {
986 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
987 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
988 struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
989 struct snd_pcm_runtime *runtime = substream->runtime;
990 unsigned int max_channels = rsnd_rdai_channels_get(rdai);
991 int i;
992
993 rsnd_flags_del(io, RSND_HW_RULE_ERR);
994
995 rsnd_dai_stream_init(io, substream);
996
997 /*
998 * Channel Limitation
999 * It depends on Platform design
1000 */
1001 constraint->list = rsnd_soc_hw_channels_list;
1002 constraint->count = 0;
1003 constraint->mask = 0;
1004
1005 for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
1006 if (rsnd_soc_hw_channels_list[i] > max_channels)
1007 break;
1008 constraint->count = i + 1;
1009 }
1010
1011 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
1012
1013 snd_pcm_hw_constraint_list(runtime, 0,
1014 SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
1015
1016 snd_pcm_hw_constraint_integer(runtime,
1017 SNDRV_PCM_HW_PARAM_PERIODS);
1018
1019 /*
1020 * Sampling Rate / Channel Limitation
1021 * It depends on Clock Master Mode
1022 */
1023 if (rsnd_rdai_is_clk_master(rdai)) {
1024 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1025
1026 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1027 rsnd_soc_hw_rule_rate,
1028 is_play ? &rdai->playback : &rdai->capture,
1029 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1030 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1031 rsnd_soc_hw_rule_channels,
1032 is_play ? &rdai->playback : &rdai->capture,
1033 SNDRV_PCM_HW_PARAM_RATE, -1);
1034 }
1035
1036 return 0;
1037 }
1038
rsnd_soc_dai_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1039 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
1040 struct snd_soc_dai *dai)
1041 {
1042 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1043 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1044 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1045
1046 /*
1047 * call rsnd_dai_call without spinlock
1048 */
1049 rsnd_dai_call(cleanup, io, priv);
1050
1051 rsnd_dai_stream_quit(io);
1052 }
1053
rsnd_soc_dai_prepare(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1054 static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream,
1055 struct snd_soc_dai *dai)
1056 {
1057 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
1058 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1059 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1060
1061 return rsnd_dai_call(prepare, io, priv);
1062 }
1063
1064 static u64 rsnd_soc_dai_formats[] = {
1065 /*
1066 * 1st Priority
1067 *
1068 * Well tested formats.
1069 * Select below from Sound Card, not auto
1070 * SND_SOC_DAIFMT_CBC_CFC
1071 * SND_SOC_DAIFMT_CBP_CFP
1072 */
1073 SND_SOC_POSSIBLE_DAIFMT_I2S |
1074 SND_SOC_POSSIBLE_DAIFMT_RIGHT_J |
1075 SND_SOC_POSSIBLE_DAIFMT_LEFT_J |
1076 SND_SOC_POSSIBLE_DAIFMT_NB_NF |
1077 SND_SOC_POSSIBLE_DAIFMT_NB_IF |
1078 SND_SOC_POSSIBLE_DAIFMT_IB_NF |
1079 SND_SOC_POSSIBLE_DAIFMT_IB_IF,
1080 /*
1081 * 2nd Priority
1082 *
1083 * Supported, but not well tested
1084 */
1085 SND_SOC_POSSIBLE_DAIFMT_DSP_A |
1086 SND_SOC_POSSIBLE_DAIFMT_DSP_B,
1087 };
1088
rsnd_parse_tdm_split_mode(struct rsnd_priv * priv,struct rsnd_dai_stream * io,struct device_node * dai_np)1089 static void rsnd_parse_tdm_split_mode(struct rsnd_priv *priv,
1090 struct rsnd_dai_stream *io,
1091 struct device_node *dai_np)
1092 {
1093 struct device *dev = rsnd_priv_to_dev(priv);
1094 struct device_node *ssiu_np = rsnd_ssiu_of_node(priv);
1095 struct device_node *np;
1096 int is_play = rsnd_io_is_play(io);
1097 int i;
1098
1099 if (!ssiu_np)
1100 return;
1101
1102 /*
1103 * This driver assumes that it is TDM Split mode
1104 * if it includes ssiu node
1105 */
1106 for (i = 0;; i++) {
1107 struct device_node *node = is_play ?
1108 of_parse_phandle(dai_np, "playback", i) :
1109 of_parse_phandle(dai_np, "capture", i);
1110
1111 if (!node)
1112 break;
1113
1114 for_each_child_of_node(ssiu_np, np) {
1115 if (np == node) {
1116 rsnd_flags_set(io, RSND_STREAM_TDM_SPLIT);
1117 dev_dbg(dev, "%s is part of TDM Split\n", io->name);
1118 }
1119 }
1120
1121 of_node_put(node);
1122 }
1123
1124 of_node_put(ssiu_np);
1125 }
1126
rsnd_parse_connect_simple(struct rsnd_priv * priv,struct rsnd_dai_stream * io,struct device_node * dai_np)1127 static void rsnd_parse_connect_simple(struct rsnd_priv *priv,
1128 struct rsnd_dai_stream *io,
1129 struct device_node *dai_np)
1130 {
1131 if (!rsnd_io_to_mod_ssi(io))
1132 return;
1133
1134 rsnd_parse_tdm_split_mode(priv, io, dai_np);
1135 }
1136
rsnd_parse_connect_graph(struct rsnd_priv * priv,struct rsnd_dai_stream * io,struct device_node * endpoint)1137 static void rsnd_parse_connect_graph(struct rsnd_priv *priv,
1138 struct rsnd_dai_stream *io,
1139 struct device_node *endpoint)
1140 {
1141 struct device *dev = rsnd_priv_to_dev(priv);
1142 struct device_node *remote_node;
1143
1144 if (!rsnd_io_to_mod_ssi(io))
1145 return;
1146
1147 remote_node = of_graph_get_remote_port_parent(endpoint);
1148
1149 /* HDMI0 */
1150 if (strstr(remote_node->full_name, "hdmi@fead0000")) {
1151 rsnd_flags_set(io, RSND_STREAM_HDMI0);
1152 dev_dbg(dev, "%s connected to HDMI0\n", io->name);
1153 }
1154
1155 /* HDMI1 */
1156 if (strstr(remote_node->full_name, "hdmi@feae0000")) {
1157 rsnd_flags_set(io, RSND_STREAM_HDMI1);
1158 dev_dbg(dev, "%s connected to HDMI1\n", io->name);
1159 }
1160
1161 rsnd_parse_tdm_split_mode(priv, io, endpoint);
1162
1163 of_node_put(remote_node);
1164 }
1165
rsnd_parse_connect_common(struct rsnd_dai * rdai,char * name,struct rsnd_mod * (* mod_get)(struct rsnd_priv * priv,int id),struct device_node * node,struct device_node * playback,struct device_node * capture)1166 void rsnd_parse_connect_common(struct rsnd_dai *rdai, char *name,
1167 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
1168 struct device_node *node,
1169 struct device_node *playback,
1170 struct device_node *capture)
1171 {
1172 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1173 struct device *dev = rsnd_priv_to_dev(priv);
1174 struct device_node *np;
1175 int i;
1176
1177 if (!node)
1178 return;
1179
1180 i = 0;
1181 for_each_child_of_node(node, np) {
1182 struct rsnd_mod *mod;
1183
1184 i = rsnd_node_fixed_index(dev, np, name, i);
1185 if (i < 0) {
1186 of_node_put(np);
1187 break;
1188 }
1189
1190 mod = mod_get(priv, i);
1191
1192 if (np == playback)
1193 rsnd_dai_connect(mod, &rdai->playback, mod->type);
1194 if (np == capture)
1195 rsnd_dai_connect(mod, &rdai->capture, mod->type);
1196 i++;
1197 }
1198
1199 of_node_put(node);
1200 }
1201
rsnd_node_fixed_index(struct device * dev,struct device_node * node,char * name,int idx)1202 int rsnd_node_fixed_index(struct device *dev, struct device_node *node, char *name, int idx)
1203 {
1204 char node_name[16];
1205
1206 /*
1207 * rsnd is assuming each device nodes are sequential numbering,
1208 * but some of them are not.
1209 * This function adjusts index for it.
1210 *
1211 * ex)
1212 * Normal case, special case
1213 * ssi-0
1214 * ssi-1
1215 * ssi-2
1216 * ssi-3 ssi-3
1217 * ssi-4 ssi-4
1218 * ...
1219 *
1220 * assume Max 64 node
1221 */
1222 for (; idx < 64; idx++) {
1223 snprintf(node_name, sizeof(node_name), "%s-%d", name, idx);
1224
1225 if (strncmp(node_name, of_node_full_name(node), sizeof(node_name)) == 0)
1226 return idx;
1227 }
1228
1229 dev_err(dev, "strange node numbering (%s)",
1230 of_node_full_name(node));
1231 return -EINVAL;
1232 }
1233
rsnd_node_count(struct rsnd_priv * priv,struct device_node * node,char * name)1234 int rsnd_node_count(struct rsnd_priv *priv, struct device_node *node, char *name)
1235 {
1236 struct device *dev = rsnd_priv_to_dev(priv);
1237 struct device_node *np;
1238 int i;
1239
1240 i = 0;
1241 for_each_child_of_node(node, np) {
1242 i = rsnd_node_fixed_index(dev, np, name, i);
1243 if (i < 0) {
1244 of_node_put(np);
1245 return 0;
1246 }
1247 i++;
1248 }
1249
1250 return i;
1251 }
1252
rsnd_dai_of_node(struct rsnd_priv * priv,int * is_graph)1253 static int rsnd_dai_of_node(struct rsnd_priv *priv, int *is_graph)
1254 {
1255 struct device *dev = rsnd_priv_to_dev(priv);
1256 struct device_node *np = dev->of_node;
1257 struct device_node *ports, *node;
1258 int nr = 0;
1259 int i = 0;
1260
1261 *is_graph = 0;
1262
1263 /*
1264 * parse both previous dai (= rcar_sound,dai), and
1265 * graph dai (= ports/port)
1266 */
1267
1268 /*
1269 * Simple-Card
1270 */
1271 node = of_get_child_by_name(np, RSND_NODE_DAI);
1272 if (!node)
1273 goto audio_graph;
1274
1275 of_node_put(node);
1276
1277 for_each_child_of_node(np, node) {
1278 if (!of_node_name_eq(node, RSND_NODE_DAI))
1279 continue;
1280
1281 priv->component_dais[i] = of_get_child_count(node);
1282 nr += priv->component_dais[i];
1283 i++;
1284 if (i >= RSND_MAX_COMPONENT) {
1285 dev_info(dev, "reach to max component\n");
1286 of_node_put(node);
1287 break;
1288 }
1289 }
1290
1291 return nr;
1292
1293 audio_graph:
1294 /*
1295 * Audio-Graph-Card
1296 */
1297 for_each_child_of_node(np, ports) {
1298 if (!of_node_name_eq(ports, "ports") &&
1299 !of_node_name_eq(ports, "port"))
1300 continue;
1301 priv->component_dais[i] = of_graph_get_endpoint_count(ports);
1302 nr += priv->component_dais[i];
1303 i++;
1304 if (i >= RSND_MAX_COMPONENT) {
1305 dev_info(dev, "reach to max component\n");
1306 of_node_put(ports);
1307 break;
1308 }
1309 }
1310
1311 *is_graph = 1;
1312
1313 return nr;
1314 }
1315
1316
1317 #define PREALLOC_BUFFER (32 * 1024)
1318 #define PREALLOC_BUFFER_MAX (32 * 1024)
1319
rsnd_preallocate_pages(struct snd_soc_pcm_runtime * rtd,struct rsnd_dai_stream * io,int stream)1320 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1321 struct rsnd_dai_stream *io,
1322 int stream)
1323 {
1324 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1325 struct device *dev = rsnd_priv_to_dev(priv);
1326 struct snd_pcm_substream *substream;
1327
1328 /*
1329 * use Audio-DMAC dev if we can use IPMMU
1330 * see
1331 * rsnd_dmaen_attach()
1332 */
1333 if (io->dmac_dev)
1334 dev = io->dmac_dev;
1335
1336 for (substream = rtd->pcm->streams[stream].substream;
1337 substream;
1338 substream = substream->next) {
1339 snd_pcm_set_managed_buffer(substream,
1340 SNDRV_DMA_TYPE_DEV,
1341 dev,
1342 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1343 }
1344
1345 return 0;
1346 }
1347
rsnd_soc_dai_pcm_new(struct snd_soc_pcm_runtime * rtd,struct snd_soc_dai * dai)1348 static int rsnd_soc_dai_pcm_new(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai)
1349 {
1350 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1351 int ret;
1352
1353 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1354 if (ret)
1355 return ret;
1356
1357 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1358 if (ret)
1359 return ret;
1360
1361 ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1362 SNDRV_PCM_STREAM_PLAYBACK);
1363 if (ret)
1364 return ret;
1365
1366 ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1367 SNDRV_PCM_STREAM_CAPTURE);
1368 if (ret)
1369 return ret;
1370
1371 return 0;
1372 }
1373
1374 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
1375 .pcm_new = rsnd_soc_dai_pcm_new,
1376 .startup = rsnd_soc_dai_startup,
1377 .shutdown = rsnd_soc_dai_shutdown,
1378 .trigger = rsnd_soc_dai_trigger,
1379 .set_fmt = rsnd_soc_dai_set_fmt,
1380 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
1381 .prepare = rsnd_soc_dai_prepare,
1382 .auto_selectable_formats = rsnd_soc_dai_formats,
1383 .num_auto_selectable_formats = ARRAY_SIZE(rsnd_soc_dai_formats),
1384 };
1385
__rsnd_dai_probe(struct rsnd_priv * priv,struct device_node * dai_np,struct device_node * node_np,uint32_t node_arg,int dai_i)1386 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1387 struct device_node *dai_np,
1388 struct device_node *node_np,
1389 uint32_t node_arg,
1390 int dai_i)
1391 {
1392 struct rsnd_dai_stream *io_playback;
1393 struct rsnd_dai_stream *io_capture;
1394 struct snd_soc_dai_driver *drv;
1395 struct rsnd_dai *rdai;
1396 struct device *dev = rsnd_priv_to_dev(priv);
1397 int playback_exist = 0, capture_exist = 0;
1398 int io_i;
1399
1400 rdai = rsnd_rdai_get(priv, dai_i);
1401 drv = rsnd_daidrv_get(priv, dai_i);
1402 io_playback = &rdai->playback;
1403 io_capture = &rdai->capture;
1404
1405 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1406
1407 /* for multi Component */
1408 rdai->dai_args.np = node_np;
1409 rdai->dai_args.args_count = 1;
1410 rdai->dai_args.args[0] = node_arg;
1411
1412 rdai->priv = priv;
1413 drv->name = rdai->name;
1414 drv->ops = &rsnd_soc_dai_ops;
1415 drv->id = dai_i;
1416 drv->dai_args = &rdai->dai_args;
1417
1418 io_playback->rdai = rdai;
1419 io_capture->rdai = rdai;
1420 rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1421 rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1422 rsnd_rdai_width_set(rdai, 32); /* default 32bit width */
1423
1424 for (io_i = 0;; io_i++) {
1425 struct device_node *playback = of_parse_phandle(dai_np, "playback", io_i);
1426 struct device_node *capture = of_parse_phandle(dai_np, "capture", io_i);
1427
1428 if (!playback && !capture)
1429 break;
1430
1431 if (io_i == 0) {
1432 /* check whether playback/capture property exists */
1433 if (playback)
1434 playback_exist = 1;
1435 if (capture)
1436 capture_exist = 1;
1437 }
1438
1439 rsnd_parse_connect_ssi(rdai, playback, capture);
1440 rsnd_parse_connect_ssiu(rdai, playback, capture);
1441 rsnd_parse_connect_src(rdai, playback, capture);
1442 rsnd_parse_connect_ctu(rdai, playback, capture);
1443 rsnd_parse_connect_mix(rdai, playback, capture);
1444 rsnd_parse_connect_dvc(rdai, playback, capture);
1445
1446 of_node_put(playback);
1447 of_node_put(capture);
1448 }
1449
1450 if (playback_exist) {
1451 snprintf(io_playback->name, RSND_DAI_NAME_SIZE, "DAI%d Playback", dai_i);
1452 drv->playback.rates = RSND_RATES;
1453 drv->playback.formats = RSND_FMTS;
1454 drv->playback.channels_min = 2;
1455 drv->playback.channels_max = 8;
1456 drv->playback.stream_name = io_playback->name;
1457 }
1458 if (capture_exist) {
1459 snprintf(io_capture->name, RSND_DAI_NAME_SIZE, "DAI%d Capture", dai_i);
1460 drv->capture.rates = RSND_RATES;
1461 drv->capture.formats = RSND_FMTS;
1462 drv->capture.channels_min = 2;
1463 drv->capture.channels_max = 8;
1464 drv->capture.stream_name = io_capture->name;
1465 }
1466
1467 if (rsnd_ssi_is_pin_sharing(io_capture) ||
1468 rsnd_ssi_is_pin_sharing(io_playback)) {
1469 /* should have symmetric_rate if pin sharing */
1470 drv->symmetric_rate = 1;
1471 }
1472
1473 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1474 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
1475 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
1476 }
1477
rsnd_dai_probe(struct rsnd_priv * priv)1478 static int rsnd_dai_probe(struct rsnd_priv *priv)
1479 {
1480 struct snd_soc_dai_driver *rdrv;
1481 struct device *dev = rsnd_priv_to_dev(priv);
1482 struct device_node *np = dev->of_node;
1483 struct rsnd_dai *rdai;
1484 int nr = 0;
1485 int is_graph;
1486 int dai_i;
1487
1488 nr = rsnd_dai_of_node(priv, &is_graph);
1489 if (!nr)
1490 return -EINVAL;
1491
1492 rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1493 rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1494 if (!rdrv || !rdai)
1495 return -ENOMEM;
1496
1497 priv->rdai_nr = nr;
1498 priv->daidrv = rdrv;
1499 priv->rdai = rdai;
1500
1501 /*
1502 * parse all dai
1503 */
1504 dai_i = 0;
1505 if (is_graph) {
1506 struct device_node *ports;
1507 struct device_node *dai_np;
1508
1509 for_each_child_of_node(np, ports) {
1510 if (!of_node_name_eq(ports, "ports") &&
1511 !of_node_name_eq(ports, "port"))
1512 continue;
1513 for_each_endpoint_of_node(ports, dai_np) {
1514 __rsnd_dai_probe(priv, dai_np, dai_np, 0, dai_i);
1515 if (rsnd_is_gen3(priv) || rsnd_is_gen4(priv)) {
1516 rdai = rsnd_rdai_get(priv, dai_i);
1517
1518 rsnd_parse_connect_graph(priv, &rdai->playback, dai_np);
1519 rsnd_parse_connect_graph(priv, &rdai->capture, dai_np);
1520 }
1521 dai_i++;
1522 }
1523 }
1524 } else {
1525 struct device_node *node;
1526 struct device_node *dai_np;
1527
1528 for_each_child_of_node(np, node) {
1529 if (!of_node_name_eq(node, RSND_NODE_DAI))
1530 continue;
1531
1532 for_each_child_of_node(node, dai_np) {
1533 __rsnd_dai_probe(priv, dai_np, np, dai_i, dai_i);
1534 if (rsnd_is_gen3(priv) || rsnd_is_gen4(priv)) {
1535 rdai = rsnd_rdai_get(priv, dai_i);
1536
1537 rsnd_parse_connect_simple(priv, &rdai->playback, dai_np);
1538 rsnd_parse_connect_simple(priv, &rdai->capture, dai_np);
1539 }
1540 dai_i++;
1541 }
1542 }
1543 }
1544
1545 return 0;
1546 }
1547
1548 /*
1549 * pcm ops
1550 */
rsnd_hw_update(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)1551 static int rsnd_hw_update(struct snd_pcm_substream *substream,
1552 struct snd_pcm_hw_params *hw_params)
1553 {
1554 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1555 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1556 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1557 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1558 unsigned long flags;
1559 int ret;
1560
1561 spin_lock_irqsave(&priv->lock, flags);
1562 if (hw_params)
1563 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1564 else
1565 ret = rsnd_dai_call(hw_free, io, substream);
1566 spin_unlock_irqrestore(&priv->lock, flags);
1567
1568 return ret;
1569 }
1570
rsnd_hw_params(struct snd_soc_component * component,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)1571 static int rsnd_hw_params(struct snd_soc_component *component,
1572 struct snd_pcm_substream *substream,
1573 struct snd_pcm_hw_params *hw_params)
1574 {
1575 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1576 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1577 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1578 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1579
1580 /*
1581 * rsnd assumes that it might be used under DPCM if user want to use
1582 * channel / rate convert. Then, rsnd should be FE.
1583 * And then, this function will be called *after* BE settings.
1584 * this means, each BE already has fixuped hw_params.
1585 * see
1586 * dpcm_fe_dai_hw_params()
1587 * dpcm_be_dai_hw_params()
1588 */
1589 io->converted_rate = 0;
1590 io->converted_chan = 0;
1591 if (fe->dai_link->dynamic) {
1592 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1593 struct device *dev = rsnd_priv_to_dev(priv);
1594 struct snd_soc_dpcm *dpcm;
1595 int stream = substream->stream;
1596
1597 for_each_dpcm_be(fe, stream, dpcm) {
1598 struct snd_soc_pcm_runtime *be = dpcm->be;
1599 struct snd_pcm_hw_params *be_params = &be->dpcm[stream].hw_params;
1600
1601 if (params_channels(hw_params) != params_channels(be_params))
1602 io->converted_chan = params_channels(be_params);
1603 if (params_rate(hw_params) != params_rate(be_params))
1604 io->converted_rate = params_rate(be_params);
1605 }
1606 if (io->converted_chan)
1607 dev_dbg(dev, "convert channels = %d\n", io->converted_chan);
1608 if (io->converted_rate) {
1609 /*
1610 * SRC supports convert rates from params_rate(hw_params)/k_down
1611 * to params_rate(hw_params)*k_up, where k_up is always 6, and
1612 * k_down depends on number of channels and SRC unit.
1613 * So all SRC units can upsample audio up to 6 times regardless
1614 * its number of channels. And all SRC units can downsample
1615 * 2 channel audio up to 6 times too.
1616 */
1617 int k_up = 6;
1618 int k_down = 6;
1619 int channel;
1620 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
1621
1622 dev_dbg(dev, "convert rate = %d\n", io->converted_rate);
1623
1624 channel = io->converted_chan ? io->converted_chan :
1625 params_channels(hw_params);
1626
1627 switch (rsnd_mod_id(src_mod)) {
1628 /*
1629 * SRC0 can downsample 4, 6 and 8 channel audio up to 4 times.
1630 * SRC1, SRC3 and SRC4 can downsample 4 channel audio
1631 * up to 4 times.
1632 * SRC1, SRC3 and SRC4 can downsample 6 and 8 channel audio
1633 * no more than twice.
1634 */
1635 case 1:
1636 case 3:
1637 case 4:
1638 if (channel > 4) {
1639 k_down = 2;
1640 break;
1641 }
1642 fallthrough;
1643 case 0:
1644 if (channel > 2)
1645 k_down = 4;
1646 break;
1647
1648 /* Other SRC units do not support more than 2 channels */
1649 default:
1650 if (channel > 2)
1651 return -EINVAL;
1652 }
1653
1654 if (params_rate(hw_params) > io->converted_rate * k_down) {
1655 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1656 io->converted_rate * k_down;
1657 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1658 io->converted_rate * k_down;
1659 hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1660 } else if (params_rate(hw_params) * k_up < io->converted_rate) {
1661 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1662 DIV_ROUND_UP(io->converted_rate, k_up);
1663 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1664 DIV_ROUND_UP(io->converted_rate, k_up);
1665 hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1666 }
1667
1668 /*
1669 * TBD: Max SRC input and output rates also depend on number
1670 * of channels and SRC unit:
1671 * SRC1, SRC3 and SRC4 do not support more than 128kHz
1672 * for 6 channel and 96kHz for 8 channel audio.
1673 * Perhaps this function should return EINVAL if the input or
1674 * the output rate exceeds the limitation.
1675 */
1676 }
1677 }
1678
1679 return rsnd_hw_update(substream, hw_params);
1680 }
1681
rsnd_hw_free(struct snd_soc_component * component,struct snd_pcm_substream * substream)1682 static int rsnd_hw_free(struct snd_soc_component *component,
1683 struct snd_pcm_substream *substream)
1684 {
1685 return rsnd_hw_update(substream, NULL);
1686 }
1687
rsnd_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)1688 static snd_pcm_uframes_t rsnd_pointer(struct snd_soc_component *component,
1689 struct snd_pcm_substream *substream)
1690 {
1691 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1692 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1693 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1694 snd_pcm_uframes_t pointer = 0;
1695
1696 rsnd_dai_call(pointer, io, &pointer);
1697
1698 return pointer;
1699 }
1700
1701 /*
1702 * snd_kcontrol
1703 */
rsnd_kctrl_info(struct snd_kcontrol * kctrl,struct snd_ctl_elem_info * uinfo)1704 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1705 struct snd_ctl_elem_info *uinfo)
1706 {
1707 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1708
1709 if (cfg->texts) {
1710 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1711 uinfo->count = cfg->size;
1712 uinfo->value.enumerated.items = cfg->max;
1713 if (uinfo->value.enumerated.item >= cfg->max)
1714 uinfo->value.enumerated.item = cfg->max - 1;
1715 strscpy(uinfo->value.enumerated.name,
1716 cfg->texts[uinfo->value.enumerated.item],
1717 sizeof(uinfo->value.enumerated.name));
1718 } else {
1719 uinfo->count = cfg->size;
1720 uinfo->value.integer.min = 0;
1721 uinfo->value.integer.max = cfg->max;
1722 uinfo->type = (cfg->max == 1) ?
1723 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1724 SNDRV_CTL_ELEM_TYPE_INTEGER;
1725 }
1726
1727 return 0;
1728 }
1729
rsnd_kctrl_get(struct snd_kcontrol * kctrl,struct snd_ctl_elem_value * uc)1730 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1731 struct snd_ctl_elem_value *uc)
1732 {
1733 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1734 int i;
1735
1736 for (i = 0; i < cfg->size; i++)
1737 if (cfg->texts)
1738 uc->value.enumerated.item[i] = cfg->val[i];
1739 else
1740 uc->value.integer.value[i] = cfg->val[i];
1741
1742 return 0;
1743 }
1744
rsnd_kctrl_put(struct snd_kcontrol * kctrl,struct snd_ctl_elem_value * uc)1745 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1746 struct snd_ctl_elem_value *uc)
1747 {
1748 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1749 int i, change = 0;
1750
1751 if (!cfg->accept(cfg->io))
1752 return 0;
1753
1754 for (i = 0; i < cfg->size; i++) {
1755 if (cfg->texts) {
1756 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1757 cfg->val[i] = uc->value.enumerated.item[i];
1758 } else {
1759 change |= (uc->value.integer.value[i] != cfg->val[i]);
1760 cfg->val[i] = uc->value.integer.value[i];
1761 }
1762 }
1763
1764 if (change && cfg->update)
1765 cfg->update(cfg->io, cfg->mod);
1766
1767 return change;
1768 }
1769
rsnd_kctrl_accept_anytime(struct rsnd_dai_stream * io)1770 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1771 {
1772 return 1;
1773 }
1774
rsnd_kctrl_accept_runtime(struct rsnd_dai_stream * io)1775 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1776 {
1777 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1778 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1779 struct device *dev = rsnd_priv_to_dev(priv);
1780
1781 if (!runtime) {
1782 dev_warn(dev, "Can't update kctrl when idle\n");
1783 return 0;
1784 }
1785
1786 return 1;
1787 }
1788
rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m * cfg)1789 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1790 {
1791 cfg->cfg.val = cfg->val;
1792
1793 return &cfg->cfg;
1794 }
1795
rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s * cfg)1796 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1797 {
1798 cfg->cfg.val = &cfg->val;
1799
1800 return &cfg->cfg;
1801 }
1802
1803 const char * const volume_ramp_rate[] = {
1804 "128 dB/1 step", /* 00000 */
1805 "64 dB/1 step", /* 00001 */
1806 "32 dB/1 step", /* 00010 */
1807 "16 dB/1 step", /* 00011 */
1808 "8 dB/1 step", /* 00100 */
1809 "4 dB/1 step", /* 00101 */
1810 "2 dB/1 step", /* 00110 */
1811 "1 dB/1 step", /* 00111 */
1812 "0.5 dB/1 step", /* 01000 */
1813 "0.25 dB/1 step", /* 01001 */
1814 "0.125 dB/1 step", /* 01010 = VOLUME_RAMP_MAX_MIX */
1815 "0.125 dB/2 steps", /* 01011 */
1816 "0.125 dB/4 steps", /* 01100 */
1817 "0.125 dB/8 steps", /* 01101 */
1818 "0.125 dB/16 steps", /* 01110 */
1819 "0.125 dB/32 steps", /* 01111 */
1820 "0.125 dB/64 steps", /* 10000 */
1821 "0.125 dB/128 steps", /* 10001 */
1822 "0.125 dB/256 steps", /* 10010 */
1823 "0.125 dB/512 steps", /* 10011 */
1824 "0.125 dB/1024 steps", /* 10100 */
1825 "0.125 dB/2048 steps", /* 10101 */
1826 "0.125 dB/4096 steps", /* 10110 */
1827 "0.125 dB/8192 steps", /* 10111 = VOLUME_RAMP_MAX_DVC */
1828 };
1829
rsnd_kctrl_new(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct snd_soc_pcm_runtime * rtd,const unsigned char * name,int (* accept)(struct rsnd_dai_stream * io),void (* update)(struct rsnd_dai_stream * io,struct rsnd_mod * mod),struct rsnd_kctrl_cfg * cfg,const char * const * texts,int size,u32 max)1830 int rsnd_kctrl_new(struct rsnd_mod *mod,
1831 struct rsnd_dai_stream *io,
1832 struct snd_soc_pcm_runtime *rtd,
1833 const unsigned char *name,
1834 int (*accept)(struct rsnd_dai_stream *io),
1835 void (*update)(struct rsnd_dai_stream *io,
1836 struct rsnd_mod *mod),
1837 struct rsnd_kctrl_cfg *cfg,
1838 const char * const *texts,
1839 int size,
1840 u32 max)
1841 {
1842 struct snd_card *card = rtd->card->snd_card;
1843 struct snd_kcontrol *kctrl;
1844 struct snd_kcontrol_new knew = {
1845 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1846 .name = name,
1847 .info = rsnd_kctrl_info,
1848 .index = rtd->num,
1849 .get = rsnd_kctrl_get,
1850 .put = rsnd_kctrl_put,
1851 };
1852 int ret;
1853
1854 /*
1855 * 1) Avoid duplicate register for DVC with MIX case
1856 * 2) Allow duplicate register for MIX
1857 * 3) re-register if card was rebinded
1858 */
1859 list_for_each_entry(kctrl, &card->controls, list) {
1860 struct rsnd_kctrl_cfg *c = kctrl->private_data;
1861
1862 if (c == cfg)
1863 return 0;
1864 }
1865
1866 if (size > RSND_MAX_CHANNELS)
1867 return -EINVAL;
1868
1869 kctrl = snd_ctl_new1(&knew, cfg);
1870 if (!kctrl)
1871 return -ENOMEM;
1872
1873 ret = snd_ctl_add(card, kctrl);
1874 if (ret < 0)
1875 return ret;
1876
1877 cfg->texts = texts;
1878 cfg->max = max;
1879 cfg->size = size;
1880 cfg->accept = accept;
1881 cfg->update = update;
1882 cfg->card = card;
1883 cfg->kctrl = kctrl;
1884 cfg->io = io;
1885 cfg->mod = mod;
1886
1887 return 0;
1888 }
1889
1890 /*
1891 * snd_soc_component
1892 */
1893 static const struct snd_soc_component_driver rsnd_soc_component = {
1894 .name = "rsnd",
1895 .probe = rsnd_debugfs_probe,
1896 .hw_params = rsnd_hw_params,
1897 .hw_free = rsnd_hw_free,
1898 .pointer = rsnd_pointer,
1899 .legacy_dai_naming = 1,
1900 };
1901
rsnd_rdai_continuance_probe(struct rsnd_priv * priv,struct rsnd_dai_stream * io)1902 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1903 struct rsnd_dai_stream *io)
1904 {
1905 int ret;
1906
1907 ret = rsnd_dai_call(probe, io, priv);
1908 if (ret == -EAGAIN) {
1909 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1910 struct rsnd_mod *mod;
1911 int i;
1912
1913 /*
1914 * Fallback to PIO mode
1915 */
1916
1917 /*
1918 * call "remove" for SSI/SRC/DVC
1919 * SSI will be switch to PIO mode if it was DMA mode
1920 * see
1921 * rsnd_dma_init()
1922 * rsnd_ssi_fallback()
1923 */
1924 rsnd_dai_call(remove, io, priv);
1925
1926 /*
1927 * remove all mod from io
1928 * and, re connect ssi
1929 */
1930 for_each_rsnd_mod(i, mod, io)
1931 rsnd_dai_disconnect(mod, io, i);
1932 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1933
1934 /*
1935 * fallback
1936 */
1937 rsnd_dai_call(fallback, io, priv);
1938
1939 /*
1940 * retry to "probe".
1941 * DAI has SSI which is PIO mode only now.
1942 */
1943 ret = rsnd_dai_call(probe, io, priv);
1944 }
1945
1946 return ret;
1947 }
1948
1949 /*
1950 * rsnd probe
1951 */
rsnd_probe(struct platform_device * pdev)1952 static int rsnd_probe(struct platform_device *pdev)
1953 {
1954 struct rsnd_priv *priv;
1955 struct device *dev = &pdev->dev;
1956 struct rsnd_dai *rdai;
1957 int (*probe_func[])(struct rsnd_priv *priv) = {
1958 rsnd_gen_probe,
1959 rsnd_dma_probe,
1960 rsnd_ssi_probe,
1961 rsnd_ssiu_probe,
1962 rsnd_src_probe,
1963 rsnd_ctu_probe,
1964 rsnd_mix_probe,
1965 rsnd_dvc_probe,
1966 rsnd_cmd_probe,
1967 rsnd_adg_probe,
1968 rsnd_dai_probe,
1969 };
1970 int ret, i;
1971 int ci;
1972
1973 /*
1974 * init priv data
1975 */
1976 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1977 if (!priv)
1978 return -ENODEV;
1979
1980 priv->pdev = pdev;
1981 priv->flags = (unsigned long)of_device_get_match_data(dev);
1982 spin_lock_init(&priv->lock);
1983
1984 /*
1985 * init each module
1986 */
1987 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1988 ret = probe_func[i](priv);
1989 if (ret)
1990 return ret;
1991 }
1992
1993 for_each_rsnd_dai(rdai, priv, i) {
1994 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1995 if (ret)
1996 goto exit_snd_probe;
1997
1998 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1999 if (ret)
2000 goto exit_snd_probe;
2001 }
2002
2003 dev_set_drvdata(dev, priv);
2004
2005 /*
2006 * asoc register
2007 */
2008 ci = 0;
2009 for (i = 0; priv->component_dais[i] > 0; i++) {
2010 int nr = priv->component_dais[i];
2011
2012 ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
2013 priv->daidrv + ci, nr);
2014 if (ret < 0) {
2015 dev_err(dev, "cannot snd component register\n");
2016 goto exit_snd_probe;
2017 }
2018
2019 ci += nr;
2020 }
2021
2022 pm_runtime_enable(dev);
2023
2024 dev_info(dev, "probed\n");
2025 return ret;
2026
2027 exit_snd_probe:
2028 for_each_rsnd_dai(rdai, priv, i) {
2029 rsnd_dai_call(remove, &rdai->playback, priv);
2030 rsnd_dai_call(remove, &rdai->capture, priv);
2031 }
2032
2033 /*
2034 * adg is very special mod which can't use rsnd_dai_call(remove),
2035 * and it registers ADG clock on probe.
2036 * It should be unregister if probe failed.
2037 * Mainly it is assuming -EPROBE_DEFER case
2038 */
2039 rsnd_adg_remove(priv);
2040
2041 return ret;
2042 }
2043
rsnd_remove(struct platform_device * pdev)2044 static void rsnd_remove(struct platform_device *pdev)
2045 {
2046 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
2047 struct rsnd_dai *rdai;
2048 void (*remove_func[])(struct rsnd_priv *priv) = {
2049 rsnd_ssi_remove,
2050 rsnd_ssiu_remove,
2051 rsnd_src_remove,
2052 rsnd_ctu_remove,
2053 rsnd_mix_remove,
2054 rsnd_dvc_remove,
2055 rsnd_cmd_remove,
2056 rsnd_adg_remove,
2057 };
2058 int i;
2059
2060 pm_runtime_disable(&pdev->dev);
2061
2062 for_each_rsnd_dai(rdai, priv, i) {
2063 int ret;
2064
2065 ret = rsnd_dai_call(remove, &rdai->playback, priv);
2066 if (ret)
2067 dev_warn(&pdev->dev, "Failed to remove playback dai #%d\n", i);
2068
2069 ret = rsnd_dai_call(remove, &rdai->capture, priv);
2070 if (ret)
2071 dev_warn(&pdev->dev, "Failed to remove capture dai #%d\n", i);
2072 }
2073
2074 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
2075 remove_func[i](priv);
2076 }
2077
rsnd_suspend(struct device * dev)2078 static int __maybe_unused rsnd_suspend(struct device *dev)
2079 {
2080 struct rsnd_priv *priv = dev_get_drvdata(dev);
2081
2082 rsnd_adg_clk_disable(priv);
2083
2084 return 0;
2085 }
2086
rsnd_resume(struct device * dev)2087 static int __maybe_unused rsnd_resume(struct device *dev)
2088 {
2089 struct rsnd_priv *priv = dev_get_drvdata(dev);
2090
2091 rsnd_adg_clk_enable(priv);
2092
2093 return 0;
2094 }
2095
2096 static const struct dev_pm_ops rsnd_pm_ops = {
2097 SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
2098 };
2099
2100 static struct platform_driver rsnd_driver = {
2101 .driver = {
2102 .name = "rcar_sound",
2103 .pm = &rsnd_pm_ops,
2104 .of_match_table = rsnd_of_match,
2105 },
2106 .probe = rsnd_probe,
2107 .remove_new = rsnd_remove,
2108 };
2109 module_platform_driver(rsnd_driver);
2110
2111 MODULE_LICENSE("GPL v2");
2112 MODULE_DESCRIPTION("Renesas R-Car audio driver");
2113 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
2114 MODULE_ALIAS("platform:rcar-pcm-audio");
2115