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
rsnd_dai_stream_init(struct rsnd_dai_stream * io,struct snd_pcm_substream * substream)663 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
664 struct snd_pcm_substream *substream)
665 {
666 io->substream = substream;
667 }
668
rsnd_dai_stream_quit(struct rsnd_dai_stream * io)669 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
670 {
671 io->substream = NULL;
672 }
673
674 static
rsnd_substream_to_dai(struct snd_pcm_substream * substream)675 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
676 {
677 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
678
679 return snd_soc_rtd_to_cpu(rtd, 0);
680 }
681
682 static
rsnd_rdai_to_io(struct rsnd_dai * rdai,struct snd_pcm_substream * substream)683 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
684 struct snd_pcm_substream *substream)
685 {
686 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
687 return &rdai->playback;
688 else
689 return &rdai->capture;
690 }
691
rsnd_soc_dai_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)692 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
693 struct snd_soc_dai *dai)
694 {
695 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
696 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
697 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
698 int ret;
699 unsigned long flags;
700
701 spin_lock_irqsave(&priv->lock, flags);
702
703 switch (cmd) {
704 case SNDRV_PCM_TRIGGER_START:
705 case SNDRV_PCM_TRIGGER_RESUME:
706 ret = rsnd_dai_call(init, io, priv);
707 if (ret < 0)
708 goto dai_trigger_end;
709
710 ret = rsnd_dai_call(start, io, priv);
711 if (ret < 0)
712 goto dai_trigger_end;
713
714 ret = rsnd_dai_call(irq, io, priv, 1);
715 if (ret < 0)
716 goto dai_trigger_end;
717
718 break;
719 case SNDRV_PCM_TRIGGER_STOP:
720 case SNDRV_PCM_TRIGGER_SUSPEND:
721 ret = rsnd_dai_call(irq, io, priv, 0);
722
723 ret |= rsnd_dai_call(stop, io, priv);
724
725 ret |= rsnd_dai_call(quit, io, priv);
726
727 break;
728 default:
729 ret = -EINVAL;
730 }
731
732 dai_trigger_end:
733 spin_unlock_irqrestore(&priv->lock, flags);
734
735 return ret;
736 }
737
rsnd_soc_dai_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)738 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
739 {
740 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
741
742 /* set clock master for audio interface */
743 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
744 case SND_SOC_DAIFMT_BC_FC:
745 rdai->clk_master = 0;
746 break;
747 case SND_SOC_DAIFMT_BP_FP:
748 rdai->clk_master = 1; /* cpu is master */
749 break;
750 default:
751 return -EINVAL;
752 }
753
754 /* set format */
755 rdai->bit_clk_inv = 0;
756 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
757 case SND_SOC_DAIFMT_I2S:
758 rdai->sys_delay = 0;
759 rdai->data_alignment = 0;
760 rdai->frm_clk_inv = 0;
761 break;
762 case SND_SOC_DAIFMT_LEFT_J:
763 case SND_SOC_DAIFMT_DSP_B:
764 rdai->sys_delay = 1;
765 rdai->data_alignment = 0;
766 rdai->frm_clk_inv = 1;
767 break;
768 case SND_SOC_DAIFMT_RIGHT_J:
769 rdai->sys_delay = 1;
770 rdai->data_alignment = 1;
771 rdai->frm_clk_inv = 1;
772 break;
773 case SND_SOC_DAIFMT_DSP_A:
774 rdai->sys_delay = 0;
775 rdai->data_alignment = 0;
776 rdai->frm_clk_inv = 1;
777 break;
778 }
779
780 /* set clock inversion */
781 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
782 case SND_SOC_DAIFMT_NB_IF:
783 rdai->frm_clk_inv = !rdai->frm_clk_inv;
784 break;
785 case SND_SOC_DAIFMT_IB_NF:
786 rdai->bit_clk_inv = !rdai->bit_clk_inv;
787 break;
788 case SND_SOC_DAIFMT_IB_IF:
789 rdai->bit_clk_inv = !rdai->bit_clk_inv;
790 rdai->frm_clk_inv = !rdai->frm_clk_inv;
791 break;
792 case SND_SOC_DAIFMT_NB_NF:
793 default:
794 break;
795 }
796
797 return 0;
798 }
799
rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai * dai,u32 tx_mask,u32 rx_mask,int slots,int slot_width)800 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
801 u32 tx_mask, u32 rx_mask,
802 int slots, int slot_width)
803 {
804 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
805 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
806 struct device *dev = rsnd_priv_to_dev(priv);
807
808 switch (slot_width) {
809 case 16:
810 case 24:
811 case 32:
812 break;
813 default:
814 /* use default */
815 /*
816 * Indicate warning if DT has "dai-tdm-slot-width"
817 * but the value was not expected.
818 */
819 if (slot_width)
820 dev_warn(dev, "unsupported TDM slot width (%d), force to use default 32\n",
821 slot_width);
822 slot_width = 32;
823 }
824
825 switch (slots) {
826 case 2:
827 /* TDM Split Mode */
828 case 6:
829 case 8:
830 /* TDM Extend Mode */
831 rsnd_rdai_channels_set(rdai, slots);
832 rsnd_rdai_ssi_lane_set(rdai, 1);
833 rsnd_rdai_width_set(rdai, slot_width);
834 break;
835 default:
836 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
837 return -EINVAL;
838 }
839
840 return 0;
841 }
842
843 static unsigned int rsnd_soc_hw_channels_list[] = {
844 2, 6, 8,
845 };
846
847 static unsigned int rsnd_soc_hw_rate_list[] = {
848 8000,
849 11025,
850 16000,
851 22050,
852 32000,
853 44100,
854 48000,
855 64000,
856 88200,
857 96000,
858 176400,
859 192000,
860 };
861
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)862 static int rsnd_soc_hw_rule(struct rsnd_dai *rdai,
863 unsigned int *list, int list_num,
864 struct snd_interval *baseline, struct snd_interval *iv,
865 struct rsnd_dai_stream *io, char *unit)
866 {
867 struct snd_interval p;
868 unsigned int rate;
869 int i;
870
871 snd_interval_any(&p);
872 p.min = UINT_MAX;
873 p.max = 0;
874
875 for (i = 0; i < list_num; i++) {
876
877 if (!snd_interval_test(iv, list[i]))
878 continue;
879
880 rate = rsnd_ssi_clk_query(rdai,
881 baseline->min, list[i], NULL);
882 if (rate > 0) {
883 p.min = min(p.min, list[i]);
884 p.max = max(p.max, list[i]);
885 }
886
887 rate = rsnd_ssi_clk_query(rdai,
888 baseline->max, list[i], NULL);
889 if (rate > 0) {
890 p.min = min(p.min, list[i]);
891 p.max = max(p.max, list[i]);
892 }
893 }
894
895 /* Indicate error once if it can't handle */
896 if (!rsnd_flags_has(io, RSND_HW_RULE_ERR) && (p.min > p.max)) {
897 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
898 struct device *dev = rsnd_priv_to_dev(priv);
899
900 dev_warn(dev, "It can't handle %d %s <-> %d %s\n",
901 baseline->min, unit, baseline->max, unit);
902 rsnd_flags_set(io, RSND_HW_RULE_ERR);
903 }
904
905 return snd_interval_refine(iv, &p);
906 }
907
rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)908 static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
909 struct snd_pcm_hw_rule *rule)
910 {
911 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
912 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
913 struct snd_interval ic;
914 struct rsnd_dai_stream *io = rule->private;
915 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
916
917 /*
918 * possible sampling rate limitation is same as
919 * 2ch if it supports multi ssi
920 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
921 */
922 ic = *ic_;
923 ic.min =
924 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
925
926 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_rate_list,
927 ARRAY_SIZE(rsnd_soc_hw_rate_list),
928 &ic, ir, io, "ch");
929 }
930
rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)931 static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
932 struct snd_pcm_hw_rule *rule)
933 {
934 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
935 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
936 struct snd_interval ic;
937 struct rsnd_dai_stream *io = rule->private;
938 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
939
940 /*
941 * possible sampling rate limitation is same as
942 * 2ch if it supports multi ssi
943 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
944 */
945 ic = *ic_;
946 ic.min =
947 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
948
949 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_channels_list,
950 ARRAY_SIZE(rsnd_soc_hw_channels_list),
951 ir, &ic, io, "Hz");
952 }
953
954 static const struct snd_pcm_hardware rsnd_pcm_hardware = {
955 .info = SNDRV_PCM_INFO_INTERLEAVED |
956 SNDRV_PCM_INFO_MMAP |
957 SNDRV_PCM_INFO_MMAP_VALID,
958 .buffer_bytes_max = 64 * 1024,
959 .period_bytes_min = 32,
960 .period_bytes_max = 8192,
961 .periods_min = 1,
962 .periods_max = 32,
963 .fifo_size = 256,
964 };
965
rsnd_soc_dai_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)966 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
967 struct snd_soc_dai *dai)
968 {
969 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
970 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
971 struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
972 struct snd_pcm_runtime *runtime = substream->runtime;
973 unsigned int max_channels = rsnd_rdai_channels_get(rdai);
974 int i;
975
976 rsnd_flags_del(io, RSND_HW_RULE_ERR);
977
978 rsnd_dai_stream_init(io, substream);
979
980 /*
981 * Channel Limitation
982 * It depends on Platform design
983 */
984 constraint->list = rsnd_soc_hw_channels_list;
985 constraint->count = 0;
986 constraint->mask = 0;
987
988 for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
989 if (rsnd_soc_hw_channels_list[i] > max_channels)
990 break;
991 constraint->count = i + 1;
992 }
993
994 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
995
996 snd_pcm_hw_constraint_list(runtime, 0,
997 SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
998
999 snd_pcm_hw_constraint_integer(runtime,
1000 SNDRV_PCM_HW_PARAM_PERIODS);
1001
1002 /*
1003 * Sampling Rate / Channel Limitation
1004 * It depends on Clock Master Mode
1005 */
1006 if (rsnd_rdai_is_clk_master(rdai)) {
1007 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1008
1009 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1010 rsnd_soc_hw_rule_rate,
1011 is_play ? &rdai->playback : &rdai->capture,
1012 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1013 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1014 rsnd_soc_hw_rule_channels,
1015 is_play ? &rdai->playback : &rdai->capture,
1016 SNDRV_PCM_HW_PARAM_RATE, -1);
1017 }
1018
1019 return 0;
1020 }
1021
rsnd_soc_dai_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1022 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
1023 struct snd_soc_dai *dai)
1024 {
1025 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1026 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1027 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1028
1029 /*
1030 * call rsnd_dai_call without spinlock
1031 */
1032 rsnd_dai_call(cleanup, io, priv);
1033
1034 rsnd_dai_stream_quit(io);
1035 }
1036
rsnd_soc_dai_prepare(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1037 static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream,
1038 struct snd_soc_dai *dai)
1039 {
1040 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
1041 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1042 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1043
1044 return rsnd_dai_call(prepare, io, priv);
1045 }
1046
1047 static const u64 rsnd_soc_dai_formats[] = {
1048 /*
1049 * 1st Priority
1050 *
1051 * Well tested formats.
1052 * Select below from Sound Card, not auto
1053 * SND_SOC_DAIFMT_CBC_CFC
1054 * SND_SOC_DAIFMT_CBP_CFP
1055 */
1056 SND_SOC_POSSIBLE_DAIFMT_I2S |
1057 SND_SOC_POSSIBLE_DAIFMT_RIGHT_J |
1058 SND_SOC_POSSIBLE_DAIFMT_LEFT_J |
1059 SND_SOC_POSSIBLE_DAIFMT_NB_NF |
1060 SND_SOC_POSSIBLE_DAIFMT_NB_IF |
1061 SND_SOC_POSSIBLE_DAIFMT_IB_NF |
1062 SND_SOC_POSSIBLE_DAIFMT_IB_IF,
1063 /*
1064 * 2nd Priority
1065 *
1066 * Supported, but not well tested
1067 */
1068 SND_SOC_POSSIBLE_DAIFMT_DSP_A |
1069 SND_SOC_POSSIBLE_DAIFMT_DSP_B,
1070 };
1071
rsnd_parse_tdm_split_mode(struct rsnd_priv * priv,struct rsnd_dai_stream * io,struct device_node * dai_np)1072 static void rsnd_parse_tdm_split_mode(struct rsnd_priv *priv,
1073 struct rsnd_dai_stream *io,
1074 struct device_node *dai_np)
1075 {
1076 struct device *dev = rsnd_priv_to_dev(priv);
1077 struct device_node *ssiu_np = rsnd_ssiu_of_node(priv);
1078 struct device_node *np;
1079 int is_play = rsnd_io_is_play(io);
1080 int i;
1081
1082 if (!ssiu_np)
1083 return;
1084
1085 /*
1086 * This driver assumes that it is TDM Split mode
1087 * if it includes ssiu node
1088 */
1089 for (i = 0;; i++) {
1090 struct device_node *node = is_play ?
1091 of_parse_phandle(dai_np, "playback", i) :
1092 of_parse_phandle(dai_np, "capture", i);
1093
1094 if (!node)
1095 break;
1096
1097 for_each_child_of_node(ssiu_np, np) {
1098 if (np == node) {
1099 rsnd_flags_set(io, RSND_STREAM_TDM_SPLIT);
1100 dev_dbg(dev, "%s is part of TDM Split\n", io->name);
1101 }
1102 }
1103
1104 of_node_put(node);
1105 }
1106
1107 of_node_put(ssiu_np);
1108 }
1109
rsnd_parse_connect_simple(struct rsnd_priv * priv,struct rsnd_dai_stream * io,struct device_node * dai_np)1110 static void rsnd_parse_connect_simple(struct rsnd_priv *priv,
1111 struct rsnd_dai_stream *io,
1112 struct device_node *dai_np)
1113 {
1114 if (!rsnd_io_to_mod_ssi(io))
1115 return;
1116
1117 rsnd_parse_tdm_split_mode(priv, io, dai_np);
1118 }
1119
rsnd_parse_connect_graph(struct rsnd_priv * priv,struct rsnd_dai_stream * io,struct device_node * endpoint)1120 static void rsnd_parse_connect_graph(struct rsnd_priv *priv,
1121 struct rsnd_dai_stream *io,
1122 struct device_node *endpoint)
1123 {
1124 struct device *dev = rsnd_priv_to_dev(priv);
1125 struct device_node *remote_node;
1126
1127 if (!rsnd_io_to_mod_ssi(io))
1128 return;
1129
1130 remote_node = of_graph_get_remote_port_parent(endpoint);
1131
1132 /* HDMI0 */
1133 if (strstr(remote_node->full_name, "hdmi@fead0000")) {
1134 rsnd_flags_set(io, RSND_STREAM_HDMI0);
1135 dev_dbg(dev, "%s connected to HDMI0\n", io->name);
1136 }
1137
1138 /* HDMI1 */
1139 if (strstr(remote_node->full_name, "hdmi@feae0000")) {
1140 rsnd_flags_set(io, RSND_STREAM_HDMI1);
1141 dev_dbg(dev, "%s connected to HDMI1\n", io->name);
1142 }
1143
1144 rsnd_parse_tdm_split_mode(priv, io, endpoint);
1145
1146 of_node_put(remote_node);
1147 }
1148
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)1149 void rsnd_parse_connect_common(struct rsnd_dai *rdai, char *name,
1150 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
1151 struct device_node *node,
1152 struct device_node *playback,
1153 struct device_node *capture)
1154 {
1155 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1156 struct device *dev = rsnd_priv_to_dev(priv);
1157 struct device_node *np;
1158 int i;
1159
1160 if (!node)
1161 return;
1162
1163 i = 0;
1164 for_each_child_of_node(node, np) {
1165 struct rsnd_mod *mod;
1166
1167 i = rsnd_node_fixed_index(dev, np, name, i);
1168 if (i < 0) {
1169 of_node_put(np);
1170 break;
1171 }
1172
1173 mod = mod_get(priv, i);
1174
1175 if (np == playback)
1176 rsnd_dai_connect(mod, &rdai->playback, mod->type);
1177 if (np == capture)
1178 rsnd_dai_connect(mod, &rdai->capture, mod->type);
1179 i++;
1180 }
1181
1182 of_node_put(node);
1183 }
1184
rsnd_node_fixed_index(struct device * dev,struct device_node * node,char * name,int idx)1185 int rsnd_node_fixed_index(struct device *dev, struct device_node *node, char *name, int idx)
1186 {
1187 char node_name[16];
1188
1189 /*
1190 * rsnd is assuming each device nodes are sequential numbering,
1191 * but some of them are not.
1192 * This function adjusts index for it.
1193 *
1194 * ex)
1195 * Normal case, special case
1196 * ssi-0
1197 * ssi-1
1198 * ssi-2
1199 * ssi-3 ssi-3
1200 * ssi-4 ssi-4
1201 * ...
1202 *
1203 * assume Max 64 node
1204 */
1205 for (; idx < 64; idx++) {
1206 snprintf(node_name, sizeof(node_name), "%s-%d", name, idx);
1207
1208 if (strncmp(node_name, of_node_full_name(node), sizeof(node_name)) == 0)
1209 return idx;
1210 }
1211
1212 dev_err(dev, "strange node numbering (%s)",
1213 of_node_full_name(node));
1214 return -EINVAL;
1215 }
1216
rsnd_node_count(struct rsnd_priv * priv,struct device_node * node,char * name)1217 int rsnd_node_count(struct rsnd_priv *priv, struct device_node *node, char *name)
1218 {
1219 struct device *dev = rsnd_priv_to_dev(priv);
1220 struct device_node *np;
1221 int i;
1222
1223 i = 0;
1224 for_each_child_of_node(node, np) {
1225 i = rsnd_node_fixed_index(dev, np, name, i);
1226 if (i < 0) {
1227 of_node_put(np);
1228 return 0;
1229 }
1230 i++;
1231 }
1232
1233 return i;
1234 }
1235
rsnd_dai_of_node(struct rsnd_priv * priv,int * is_graph)1236 static int rsnd_dai_of_node(struct rsnd_priv *priv, int *is_graph)
1237 {
1238 struct device *dev = rsnd_priv_to_dev(priv);
1239 struct device_node *np = dev->of_node;
1240 struct device_node *ports, *node;
1241 int nr = 0;
1242 int i = 0;
1243
1244 *is_graph = 0;
1245
1246 /*
1247 * parse both previous dai (= rcar_sound,dai), and
1248 * graph dai (= ports/port)
1249 */
1250
1251 /*
1252 * Simple-Card
1253 */
1254 node = of_get_child_by_name(np, RSND_NODE_DAI);
1255 if (!node)
1256 goto audio_graph;
1257
1258 of_node_put(node);
1259
1260 for_each_child_of_node(np, node) {
1261 if (!of_node_name_eq(node, RSND_NODE_DAI))
1262 continue;
1263
1264 priv->component_dais[i] = of_get_child_count(node);
1265 nr += priv->component_dais[i];
1266 i++;
1267 if (i >= RSND_MAX_COMPONENT) {
1268 dev_info(dev, "reach to max component\n");
1269 of_node_put(node);
1270 break;
1271 }
1272 }
1273
1274 return nr;
1275
1276 audio_graph:
1277 /*
1278 * Audio-Graph-Card
1279 */
1280 for_each_child_of_node(np, ports) {
1281 if (!of_node_name_eq(ports, "ports") &&
1282 !of_node_name_eq(ports, "port"))
1283 continue;
1284 priv->component_dais[i] = of_graph_get_endpoint_count(ports);
1285 nr += priv->component_dais[i];
1286 i++;
1287 if (i >= RSND_MAX_COMPONENT) {
1288 dev_info(dev, "reach to max component\n");
1289 of_node_put(ports);
1290 break;
1291 }
1292 }
1293
1294 *is_graph = 1;
1295
1296 return nr;
1297 }
1298
1299
1300 #define PREALLOC_BUFFER (32 * 1024)
1301 #define PREALLOC_BUFFER_MAX (32 * 1024)
1302
rsnd_preallocate_pages(struct snd_soc_pcm_runtime * rtd,struct rsnd_dai_stream * io,int stream)1303 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1304 struct rsnd_dai_stream *io,
1305 int stream)
1306 {
1307 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1308 struct device *dev = rsnd_priv_to_dev(priv);
1309 struct snd_pcm_substream *substream;
1310
1311 /*
1312 * use Audio-DMAC dev if we can use IPMMU
1313 * see
1314 * rsnd_dmaen_attach()
1315 */
1316 if (io->dmac_dev)
1317 dev = io->dmac_dev;
1318
1319 for (substream = rtd->pcm->streams[stream].substream;
1320 substream;
1321 substream = substream->next) {
1322 snd_pcm_set_managed_buffer(substream,
1323 SNDRV_DMA_TYPE_DEV,
1324 dev,
1325 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1326 }
1327
1328 return 0;
1329 }
1330
rsnd_soc_dai_pcm_new(struct snd_soc_pcm_runtime * rtd,struct snd_soc_dai * dai)1331 static int rsnd_soc_dai_pcm_new(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai)
1332 {
1333 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1334 int ret;
1335
1336 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1337 if (ret)
1338 return ret;
1339
1340 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1341 if (ret)
1342 return ret;
1343
1344 ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1345 SNDRV_PCM_STREAM_PLAYBACK);
1346 if (ret)
1347 return ret;
1348
1349 ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1350 SNDRV_PCM_STREAM_CAPTURE);
1351 if (ret)
1352 return ret;
1353
1354 return 0;
1355 }
1356
1357 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
1358 .pcm_new = rsnd_soc_dai_pcm_new,
1359 .startup = rsnd_soc_dai_startup,
1360 .shutdown = rsnd_soc_dai_shutdown,
1361 .trigger = rsnd_soc_dai_trigger,
1362 .set_fmt = rsnd_soc_dai_set_fmt,
1363 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
1364 .prepare = rsnd_soc_dai_prepare,
1365 .auto_selectable_formats = rsnd_soc_dai_formats,
1366 .num_auto_selectable_formats = ARRAY_SIZE(rsnd_soc_dai_formats),
1367 };
1368
__rsnd_dai_probe(struct rsnd_priv * priv,struct device_node * dai_np,struct device_node * node_np,uint32_t node_arg,int dai_i)1369 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1370 struct device_node *dai_np,
1371 struct device_node *node_np,
1372 uint32_t node_arg,
1373 int dai_i)
1374 {
1375 struct rsnd_dai_stream *io_playback;
1376 struct rsnd_dai_stream *io_capture;
1377 struct snd_soc_dai_driver *drv;
1378 struct rsnd_dai *rdai;
1379 struct device *dev = rsnd_priv_to_dev(priv);
1380 int playback_exist = 0, capture_exist = 0;
1381 int io_i;
1382
1383 rdai = rsnd_rdai_get(priv, dai_i);
1384 drv = rsnd_daidrv_get(priv, dai_i);
1385 io_playback = &rdai->playback;
1386 io_capture = &rdai->capture;
1387
1388 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1389
1390 /* for multi Component */
1391 rdai->dai_args.np = node_np;
1392 rdai->dai_args.args_count = 1;
1393 rdai->dai_args.args[0] = node_arg;
1394
1395 rdai->priv = priv;
1396 drv->name = rdai->name;
1397 drv->ops = &rsnd_soc_dai_ops;
1398 drv->id = dai_i;
1399 drv->dai_args = &rdai->dai_args;
1400
1401 io_playback->rdai = rdai;
1402 io_capture->rdai = rdai;
1403 rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1404 rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1405 rsnd_rdai_width_set(rdai, 32); /* default 32bit width */
1406
1407 for (io_i = 0;; io_i++) {
1408 struct device_node *playback = of_parse_phandle(dai_np, "playback", io_i);
1409 struct device_node *capture = of_parse_phandle(dai_np, "capture", io_i);
1410
1411 if (!playback && !capture)
1412 break;
1413
1414 if (io_i == 0) {
1415 /* check whether playback/capture property exists */
1416 if (playback)
1417 playback_exist = 1;
1418 if (capture)
1419 capture_exist = 1;
1420 }
1421
1422 rsnd_parse_connect_ssi(rdai, playback, capture);
1423 rsnd_parse_connect_ssiu(rdai, playback, capture);
1424 rsnd_parse_connect_src(rdai, playback, capture);
1425 rsnd_parse_connect_ctu(rdai, playback, capture);
1426 rsnd_parse_connect_mix(rdai, playback, capture);
1427 rsnd_parse_connect_dvc(rdai, playback, capture);
1428
1429 of_node_put(playback);
1430 of_node_put(capture);
1431 }
1432
1433 if (playback_exist) {
1434 snprintf(io_playback->name, RSND_DAI_NAME_SIZE, "DAI%d Playback", dai_i);
1435 drv->playback.rates = RSND_RATES;
1436 drv->playback.formats = RSND_FMTS;
1437 drv->playback.channels_min = 2;
1438 drv->playback.channels_max = 8;
1439 drv->playback.stream_name = io_playback->name;
1440 }
1441 if (capture_exist) {
1442 snprintf(io_capture->name, RSND_DAI_NAME_SIZE, "DAI%d Capture", dai_i);
1443 drv->capture.rates = RSND_RATES;
1444 drv->capture.formats = RSND_FMTS;
1445 drv->capture.channels_min = 2;
1446 drv->capture.channels_max = 8;
1447 drv->capture.stream_name = io_capture->name;
1448 }
1449
1450 if (rsnd_ssi_is_pin_sharing(io_capture) ||
1451 rsnd_ssi_is_pin_sharing(io_playback)) {
1452 /* should have symmetric_rate if pin sharing */
1453 drv->symmetric_rate = 1;
1454 }
1455
1456 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1457 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
1458 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
1459 }
1460
rsnd_dai_probe(struct rsnd_priv * priv)1461 static int rsnd_dai_probe(struct rsnd_priv *priv)
1462 {
1463 struct snd_soc_dai_driver *rdrv;
1464 struct device *dev = rsnd_priv_to_dev(priv);
1465 struct device_node *np = dev->of_node;
1466 struct rsnd_dai *rdai;
1467 int nr = 0;
1468 int is_graph;
1469 int dai_i;
1470
1471 nr = rsnd_dai_of_node(priv, &is_graph);
1472 if (!nr)
1473 return -EINVAL;
1474
1475 rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1476 rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1477 if (!rdrv || !rdai)
1478 return -ENOMEM;
1479
1480 priv->rdai_nr = nr;
1481 priv->daidrv = rdrv;
1482 priv->rdai = rdai;
1483
1484 /*
1485 * parse all dai
1486 */
1487 dai_i = 0;
1488 if (is_graph) {
1489 struct device_node *ports;
1490 struct device_node *dai_np;
1491
1492 for_each_child_of_node(np, ports) {
1493 if (!of_node_name_eq(ports, "ports") &&
1494 !of_node_name_eq(ports, "port"))
1495 continue;
1496 for_each_endpoint_of_node(ports, dai_np) {
1497 __rsnd_dai_probe(priv, dai_np, dai_np, 0, dai_i);
1498 if (!rsnd_is_gen1(priv) && !rsnd_is_gen2(priv)) {
1499 rdai = rsnd_rdai_get(priv, dai_i);
1500
1501 rsnd_parse_connect_graph(priv, &rdai->playback, dai_np);
1502 rsnd_parse_connect_graph(priv, &rdai->capture, dai_np);
1503 }
1504 dai_i++;
1505 }
1506 }
1507 } else {
1508 struct device_node *node;
1509 struct device_node *dai_np;
1510
1511 for_each_child_of_node(np, node) {
1512 if (!of_node_name_eq(node, RSND_NODE_DAI))
1513 continue;
1514
1515 for_each_child_of_node(node, dai_np) {
1516 __rsnd_dai_probe(priv, dai_np, np, dai_i, dai_i);
1517 if (!rsnd_is_gen1(priv) && !rsnd_is_gen2(priv)) {
1518 rdai = rsnd_rdai_get(priv, dai_i);
1519
1520 rsnd_parse_connect_simple(priv, &rdai->playback, dai_np);
1521 rsnd_parse_connect_simple(priv, &rdai->capture, dai_np);
1522 }
1523 dai_i++;
1524 }
1525 }
1526 }
1527
1528 return 0;
1529 }
1530
1531 /*
1532 * pcm ops
1533 */
rsnd_hw_update(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)1534 static int rsnd_hw_update(struct snd_pcm_substream *substream,
1535 struct snd_pcm_hw_params *hw_params)
1536 {
1537 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1538 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1539 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1540 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1541 unsigned long flags;
1542 int ret;
1543
1544 spin_lock_irqsave(&priv->lock, flags);
1545 if (hw_params)
1546 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1547 else
1548 ret = rsnd_dai_call(hw_free, io, substream);
1549 spin_unlock_irqrestore(&priv->lock, flags);
1550
1551 return ret;
1552 }
1553
rsnd_hw_params(struct snd_soc_component * component,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)1554 static int rsnd_hw_params(struct snd_soc_component *component,
1555 struct snd_pcm_substream *substream,
1556 struct snd_pcm_hw_params *hw_params)
1557 {
1558 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1559 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1560 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1561 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1562
1563 /*
1564 * rsnd assumes that it might be used under DPCM if user want to use
1565 * channel / rate convert. Then, rsnd should be FE.
1566 * And then, this function will be called *after* BE settings.
1567 * this means, each BE already has fixuped hw_params.
1568 * see
1569 * dpcm_fe_dai_hw_params()
1570 * dpcm_be_dai_hw_params()
1571 */
1572 io->converted_rate = 0;
1573 io->converted_chan = 0;
1574 if (fe->dai_link->dynamic) {
1575 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1576 struct device *dev = rsnd_priv_to_dev(priv);
1577 struct snd_soc_dpcm *dpcm;
1578 int stream = substream->stream;
1579
1580 for_each_dpcm_be(fe, stream, dpcm) {
1581 struct snd_soc_pcm_runtime *be = dpcm->be;
1582 struct snd_pcm_hw_params *be_params = &be->dpcm[stream].hw_params;
1583
1584 if (params_channels(hw_params) != params_channels(be_params))
1585 io->converted_chan = params_channels(be_params);
1586 if (params_rate(hw_params) != params_rate(be_params))
1587 io->converted_rate = params_rate(be_params);
1588 }
1589 if (io->converted_chan)
1590 dev_dbg(dev, "convert channels = %d\n", io->converted_chan);
1591 if (io->converted_rate) {
1592 /*
1593 * SRC supports convert rates from params_rate(hw_params)/k_down
1594 * to params_rate(hw_params)*k_up, where k_up is always 6, and
1595 * k_down depends on number of channels and SRC unit.
1596 * So all SRC units can upsample audio up to 6 times regardless
1597 * its number of channels. And all SRC units can downsample
1598 * 2 channel audio up to 6 times too.
1599 */
1600 int k_up = 6;
1601 int k_down = 6;
1602 int channel;
1603 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
1604
1605 dev_dbg(dev, "convert rate = %d\n", io->converted_rate);
1606
1607 channel = io->converted_chan ? io->converted_chan :
1608 params_channels(hw_params);
1609
1610 switch (rsnd_mod_id(src_mod)) {
1611 /*
1612 * SRC0 can downsample 4, 6 and 8 channel audio up to 4 times.
1613 * SRC1, SRC3 and SRC4 can downsample 4 channel audio
1614 * up to 4 times.
1615 * SRC1, SRC3 and SRC4 can downsample 6 and 8 channel audio
1616 * no more than twice.
1617 */
1618 case 1:
1619 case 3:
1620 case 4:
1621 if (channel > 4) {
1622 k_down = 2;
1623 break;
1624 }
1625 fallthrough;
1626 case 0:
1627 if (channel > 2)
1628 k_down = 4;
1629 break;
1630
1631 /* Other SRC units do not support more than 2 channels */
1632 default:
1633 if (channel > 2)
1634 return -EINVAL;
1635 }
1636
1637 if (params_rate(hw_params) > io->converted_rate * k_down) {
1638 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1639 io->converted_rate * k_down;
1640 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1641 io->converted_rate * k_down;
1642 hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1643 } else if (params_rate(hw_params) * k_up < io->converted_rate) {
1644 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1645 DIV_ROUND_UP(io->converted_rate, k_up);
1646 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1647 DIV_ROUND_UP(io->converted_rate, k_up);
1648 hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1649 }
1650
1651 /*
1652 * TBD: Max SRC input and output rates also depend on number
1653 * of channels and SRC unit:
1654 * SRC1, SRC3 and SRC4 do not support more than 128kHz
1655 * for 6 channel and 96kHz for 8 channel audio.
1656 * Perhaps this function should return EINVAL if the input or
1657 * the output rate exceeds the limitation.
1658 */
1659 }
1660 }
1661
1662 return rsnd_hw_update(substream, hw_params);
1663 }
1664
rsnd_hw_free(struct snd_soc_component * component,struct snd_pcm_substream * substream)1665 static int rsnd_hw_free(struct snd_soc_component *component,
1666 struct snd_pcm_substream *substream)
1667 {
1668 return rsnd_hw_update(substream, NULL);
1669 }
1670
rsnd_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)1671 static snd_pcm_uframes_t rsnd_pointer(struct snd_soc_component *component,
1672 struct snd_pcm_substream *substream)
1673 {
1674 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1675 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1676 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1677 snd_pcm_uframes_t pointer = 0;
1678
1679 rsnd_dai_call(pointer, io, &pointer);
1680
1681 return pointer;
1682 }
1683
1684 /*
1685 * snd_kcontrol
1686 */
rsnd_kctrl_info(struct snd_kcontrol * kctrl,struct snd_ctl_elem_info * uinfo)1687 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1688 struct snd_ctl_elem_info *uinfo)
1689 {
1690 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1691
1692 if (cfg->texts) {
1693 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1694 uinfo->count = cfg->size;
1695 uinfo->value.enumerated.items = cfg->max;
1696 if (uinfo->value.enumerated.item >= cfg->max)
1697 uinfo->value.enumerated.item = cfg->max - 1;
1698 strscpy(uinfo->value.enumerated.name,
1699 cfg->texts[uinfo->value.enumerated.item],
1700 sizeof(uinfo->value.enumerated.name));
1701 } else {
1702 uinfo->count = cfg->size;
1703 uinfo->value.integer.min = 0;
1704 uinfo->value.integer.max = cfg->max;
1705 uinfo->type = (cfg->max == 1) ?
1706 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1707 SNDRV_CTL_ELEM_TYPE_INTEGER;
1708 }
1709
1710 return 0;
1711 }
1712
rsnd_kctrl_get(struct snd_kcontrol * kctrl,struct snd_ctl_elem_value * uc)1713 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1714 struct snd_ctl_elem_value *uc)
1715 {
1716 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1717 int i;
1718
1719 for (i = 0; i < cfg->size; i++)
1720 if (cfg->texts)
1721 uc->value.enumerated.item[i] = cfg->val[i];
1722 else
1723 uc->value.integer.value[i] = cfg->val[i];
1724
1725 return 0;
1726 }
1727
rsnd_kctrl_put(struct snd_kcontrol * kctrl,struct snd_ctl_elem_value * uc)1728 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1729 struct snd_ctl_elem_value *uc)
1730 {
1731 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1732 int i, change = 0;
1733
1734 if (!cfg->accept(cfg->io))
1735 return 0;
1736
1737 for (i = 0; i < cfg->size; i++) {
1738 if (cfg->texts) {
1739 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1740 cfg->val[i] = uc->value.enumerated.item[i];
1741 } else {
1742 change |= (uc->value.integer.value[i] != cfg->val[i]);
1743 cfg->val[i] = uc->value.integer.value[i];
1744 }
1745 }
1746
1747 if (change && cfg->update)
1748 cfg->update(cfg->io, cfg->mod);
1749
1750 return change;
1751 }
1752
rsnd_kctrl_accept_anytime(struct rsnd_dai_stream * io)1753 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1754 {
1755 return 1;
1756 }
1757
rsnd_kctrl_accept_runtime(struct rsnd_dai_stream * io)1758 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1759 {
1760 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1761 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1762 struct device *dev = rsnd_priv_to_dev(priv);
1763
1764 if (!runtime) {
1765 dev_warn(dev, "Can't update kctrl when idle\n");
1766 return 0;
1767 }
1768
1769 return 1;
1770 }
1771
rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m * cfg)1772 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1773 {
1774 cfg->cfg.val = cfg->val;
1775
1776 return &cfg->cfg;
1777 }
1778
rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s * cfg)1779 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1780 {
1781 cfg->cfg.val = &cfg->val;
1782
1783 return &cfg->cfg;
1784 }
1785
1786 const char * const volume_ramp_rate[] = {
1787 "128 dB/1 step", /* 00000 */
1788 "64 dB/1 step", /* 00001 */
1789 "32 dB/1 step", /* 00010 */
1790 "16 dB/1 step", /* 00011 */
1791 "8 dB/1 step", /* 00100 */
1792 "4 dB/1 step", /* 00101 */
1793 "2 dB/1 step", /* 00110 */
1794 "1 dB/1 step", /* 00111 */
1795 "0.5 dB/1 step", /* 01000 */
1796 "0.25 dB/1 step", /* 01001 */
1797 "0.125 dB/1 step", /* 01010 = VOLUME_RAMP_MAX_MIX */
1798 "0.125 dB/2 steps", /* 01011 */
1799 "0.125 dB/4 steps", /* 01100 */
1800 "0.125 dB/8 steps", /* 01101 */
1801 "0.125 dB/16 steps", /* 01110 */
1802 "0.125 dB/32 steps", /* 01111 */
1803 "0.125 dB/64 steps", /* 10000 */
1804 "0.125 dB/128 steps", /* 10001 */
1805 "0.125 dB/256 steps", /* 10010 */
1806 "0.125 dB/512 steps", /* 10011 */
1807 "0.125 dB/1024 steps", /* 10100 */
1808 "0.125 dB/2048 steps", /* 10101 */
1809 "0.125 dB/4096 steps", /* 10110 */
1810 "0.125 dB/8192 steps", /* 10111 = VOLUME_RAMP_MAX_DVC */
1811 };
1812
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)1813 int rsnd_kctrl_new(struct rsnd_mod *mod,
1814 struct rsnd_dai_stream *io,
1815 struct snd_soc_pcm_runtime *rtd,
1816 const unsigned char *name,
1817 int (*accept)(struct rsnd_dai_stream *io),
1818 void (*update)(struct rsnd_dai_stream *io,
1819 struct rsnd_mod *mod),
1820 struct rsnd_kctrl_cfg *cfg,
1821 const char * const *texts,
1822 int size,
1823 u32 max)
1824 {
1825 struct snd_card *card = rtd->card->snd_card;
1826 struct snd_kcontrol *kctrl;
1827 struct snd_kcontrol_new knew = {
1828 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1829 .name = name,
1830 .info = rsnd_kctrl_info,
1831 .index = rtd->num,
1832 .get = rsnd_kctrl_get,
1833 .put = rsnd_kctrl_put,
1834 };
1835 int ret;
1836
1837 /*
1838 * 1) Avoid duplicate register for DVC with MIX case
1839 * 2) Allow duplicate register for MIX
1840 * 3) re-register if card was rebinded
1841 */
1842 list_for_each_entry(kctrl, &card->controls, list) {
1843 struct rsnd_kctrl_cfg *c = kctrl->private_data;
1844
1845 if (c == cfg)
1846 return 0;
1847 }
1848
1849 if (size > RSND_MAX_CHANNELS)
1850 return -EINVAL;
1851
1852 kctrl = snd_ctl_new1(&knew, cfg);
1853 if (!kctrl)
1854 return -ENOMEM;
1855
1856 ret = snd_ctl_add(card, kctrl);
1857 if (ret < 0)
1858 return ret;
1859
1860 cfg->texts = texts;
1861 cfg->max = max;
1862 cfg->size = size;
1863 cfg->accept = accept;
1864 cfg->update = update;
1865 cfg->card = card;
1866 cfg->kctrl = kctrl;
1867 cfg->io = io;
1868 cfg->mod = mod;
1869
1870 return 0;
1871 }
1872
1873 /*
1874 * snd_soc_component
1875 */
1876 static const struct snd_soc_component_driver rsnd_soc_component = {
1877 .name = "rsnd",
1878 .probe = rsnd_debugfs_probe,
1879 .hw_params = rsnd_hw_params,
1880 .hw_free = rsnd_hw_free,
1881 .pointer = rsnd_pointer,
1882 .legacy_dai_naming = 1,
1883 };
1884
rsnd_rdai_continuance_probe(struct rsnd_priv * priv,struct rsnd_dai_stream * io)1885 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1886 struct rsnd_dai_stream *io)
1887 {
1888 int ret;
1889
1890 ret = rsnd_dai_call(probe, io, priv);
1891 if (ret == -EAGAIN) {
1892 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1893 struct rsnd_mod *mod;
1894 int i;
1895
1896 /*
1897 * Fallback to PIO mode
1898 */
1899
1900 /*
1901 * call "remove" for SSI/SRC/DVC
1902 * SSI will be switch to PIO mode if it was DMA mode
1903 * see
1904 * rsnd_dma_init()
1905 * rsnd_ssi_fallback()
1906 */
1907 rsnd_dai_call(remove, io, priv);
1908
1909 /*
1910 * remove all mod from io
1911 * and, re connect ssi
1912 */
1913 for_each_rsnd_mod(i, mod, io)
1914 rsnd_dai_disconnect(mod, io, i);
1915 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1916
1917 /*
1918 * fallback
1919 */
1920 rsnd_dai_call(fallback, io, priv);
1921
1922 /*
1923 * retry to "probe".
1924 * DAI has SSI which is PIO mode only now.
1925 */
1926 ret = rsnd_dai_call(probe, io, priv);
1927 }
1928
1929 return ret;
1930 }
1931
1932 /*
1933 * rsnd probe
1934 */
rsnd_probe(struct platform_device * pdev)1935 static int rsnd_probe(struct platform_device *pdev)
1936 {
1937 struct rsnd_priv *priv;
1938 struct device *dev = &pdev->dev;
1939 struct rsnd_dai *rdai;
1940 int (*probe_func[])(struct rsnd_priv *priv) = {
1941 rsnd_gen_probe,
1942 rsnd_dma_probe,
1943 rsnd_ssi_probe,
1944 rsnd_ssiu_probe,
1945 rsnd_src_probe,
1946 rsnd_ctu_probe,
1947 rsnd_mix_probe,
1948 rsnd_dvc_probe,
1949 rsnd_cmd_probe,
1950 rsnd_adg_probe,
1951 rsnd_dai_probe,
1952 };
1953 int ret, i;
1954 int ci;
1955
1956 /*
1957 * init priv data
1958 */
1959 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1960 if (!priv)
1961 return -ENODEV;
1962
1963 priv->pdev = pdev;
1964 priv->flags = (unsigned long)of_device_get_match_data(dev);
1965 spin_lock_init(&priv->lock);
1966
1967 /*
1968 * init each module
1969 */
1970 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1971 ret = probe_func[i](priv);
1972 if (ret)
1973 return ret;
1974 }
1975
1976 for_each_rsnd_dai(rdai, priv, i) {
1977 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1978 if (ret)
1979 goto exit_snd_probe;
1980
1981 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1982 if (ret)
1983 goto exit_snd_probe;
1984 }
1985
1986 dev_set_drvdata(dev, priv);
1987
1988 /*
1989 * asoc register
1990 */
1991 ci = 0;
1992 for (i = 0; priv->component_dais[i] > 0; i++) {
1993 int nr = priv->component_dais[i];
1994
1995 ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
1996 priv->daidrv + ci, nr);
1997 if (ret < 0) {
1998 dev_err(dev, "cannot snd component register\n");
1999 goto exit_snd_probe;
2000 }
2001
2002 ci += nr;
2003 }
2004
2005 pm_runtime_enable(dev);
2006
2007 dev_info(dev, "probed\n");
2008 return ret;
2009
2010 exit_snd_probe:
2011 for_each_rsnd_dai(rdai, priv, i) {
2012 rsnd_dai_call(remove, &rdai->playback, priv);
2013 rsnd_dai_call(remove, &rdai->capture, priv);
2014 }
2015
2016 /*
2017 * adg is very special mod which can't use rsnd_dai_call(remove),
2018 * and it registers ADG clock on probe.
2019 * It should be unregister if probe failed.
2020 * Mainly it is assuming -EPROBE_DEFER case
2021 */
2022 rsnd_adg_remove(priv);
2023
2024 return ret;
2025 }
2026
rsnd_remove(struct platform_device * pdev)2027 static void rsnd_remove(struct platform_device *pdev)
2028 {
2029 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
2030 struct rsnd_dai *rdai;
2031 void (*remove_func[])(struct rsnd_priv *priv) = {
2032 rsnd_ssi_remove,
2033 rsnd_ssiu_remove,
2034 rsnd_src_remove,
2035 rsnd_ctu_remove,
2036 rsnd_mix_remove,
2037 rsnd_dvc_remove,
2038 rsnd_cmd_remove,
2039 rsnd_adg_remove,
2040 };
2041 int i;
2042
2043 pm_runtime_disable(&pdev->dev);
2044
2045 for_each_rsnd_dai(rdai, priv, i) {
2046 int ret;
2047
2048 ret = rsnd_dai_call(remove, &rdai->playback, priv);
2049 if (ret)
2050 dev_warn(&pdev->dev, "Failed to remove playback dai #%d\n", i);
2051
2052 ret = rsnd_dai_call(remove, &rdai->capture, priv);
2053 if (ret)
2054 dev_warn(&pdev->dev, "Failed to remove capture dai #%d\n", i);
2055 }
2056
2057 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
2058 remove_func[i](priv);
2059 }
2060
rsnd_suspend(struct device * dev)2061 static int __maybe_unused rsnd_suspend(struct device *dev)
2062 {
2063 struct rsnd_priv *priv = dev_get_drvdata(dev);
2064
2065 rsnd_adg_clk_disable(priv);
2066
2067 return 0;
2068 }
2069
rsnd_resume(struct device * dev)2070 static int __maybe_unused rsnd_resume(struct device *dev)
2071 {
2072 struct rsnd_priv *priv = dev_get_drvdata(dev);
2073
2074 rsnd_adg_clk_enable(priv);
2075
2076 return 0;
2077 }
2078
2079 static const struct dev_pm_ops rsnd_pm_ops = {
2080 SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
2081 };
2082
2083 static struct platform_driver rsnd_driver = {
2084 .driver = {
2085 .name = "rcar_sound",
2086 .pm = &rsnd_pm_ops,
2087 .of_match_table = rsnd_of_match,
2088 },
2089 .probe = rsnd_probe,
2090 .remove = rsnd_remove,
2091 };
2092 module_platform_driver(rsnd_driver);
2093
2094 MODULE_LICENSE("GPL v2");
2095 MODULE_DESCRIPTION("Renesas R-Car audio driver");
2096 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
2097 MODULE_ALIAS("platform:rcar-pcm-audio");
2098