]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - sound/soc/sh/rcar/core.c
Merge tag 'kvm-x86-docs-6.7' of https://github.com/kvm-x86/linux into HEAD
[thirdparty/kernel/stable.git] / sound / soc / sh / rcar / core.c
CommitLineData
1e0edd4d
KM
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>
1536a968
KM
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 * |
ba9c949f 72 * | ** these control src
1536a968 73 * |
ba9c949f 74 * +- src
1536a968 75 * |
ba9c949f
KM
76 * +- src[0]
77 * +- src[1]
78 * +- src[2]
1536a968
KM
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 */
1f6e920f 92
1536a968
KM
93#include <linux/pm_runtime.h>
94#include "rsnd.h"
95
dc272156 96#define RSND_RATES SNDRV_PCM_RATE_8000_192000
ba5d553b
DT
97#define RSND_FMTS (SNDRV_PCM_FMTBIT_S8 |\
98 SNDRV_PCM_FMTBIT_S16_LE |\
99 SNDRV_PCM_FMTBIT_S24_LE)
1536a968 100
33187fb4 101static const struct of_device_id rsnd_of_match[] = {
e797f58e
KM
102 { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
103 { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
d188e140 104 { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN3 },
c2bc6527 105 { .compatible = "renesas,rcar_sound-gen4", .data = (void *)RSND_GEN4 },
ba164a49
JW
106 /* Special Handling */
107 { .compatible = "renesas,rcar_sound-r8a77990", .data = (void *)(RSND_GEN3 | RSND_SOC_E) },
90e8e50f
KM
108 {},
109};
110MODULE_DEVICE_TABLE(of, rsnd_of_match);
111
81ad174d
KM
112/*
113 * rsnd_mod functions
114 */
f1df1229
KM
115void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
116{
117 if (mod->type != type) {
118 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
119 struct device *dev = rsnd_priv_to_dev(priv);
120
c0ea089d
KM
121 dev_warn(dev, "%s is not your expected module\n",
122 rsnd_mod_name(mod));
f1df1229
KM
123 }
124}
125
9b99e9a7
KM
126struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
127 struct rsnd_mod *mod)
d9288d0b 128{
72adc61f
KM
129 if (!mod || !mod->ops || !mod->ops->dma_req)
130 return NULL;
d9288d0b 131
9b99e9a7 132 return mod->ops->dma_req(io, mod);
d9288d0b
KM
133}
134
0246c661 135#define MOD_NAME_NUM 5
c0ea089d
KM
136#define MOD_NAME_SIZE 16
137char *rsnd_mod_name(struct rsnd_mod *mod)
138{
0246c661
KM
139 static char names[MOD_NAME_NUM][MOD_NAME_SIZE];
140 static int num;
141 char *name = names[num];
142
143 num++;
144 if (num >= MOD_NAME_NUM)
145 num = 0;
c0ea089d
KM
146
147 /*
148 * Let's use same char to avoid pointlessness memory
149 * Thus, rsnd_mod_name() should be used immediately
150 * Don't keep pointer
151 */
152 if ((mod)->ops->id_sub) {
153 snprintf(name, MOD_NAME_SIZE, "%s[%d%d]",
154 mod->ops->name,
155 rsnd_mod_id(mod),
156 rsnd_mod_id_sub(mod));
157 } else {
158 snprintf(name, MOD_NAME_SIZE, "%s[%d]",
159 mod->ops->name,
160 rsnd_mod_id(mod));
161 }
162
163 return name;
164}
165
7e7fe06d
KM
166u32 *rsnd_mod_get_status(struct rsnd_mod *mod,
167 struct rsnd_dai_stream *io,
5ba17b42
KM
168 enum rsnd_mod_type type)
169{
170 return &mod->status;
171}
172
c16015f3
KM
173int rsnd_mod_id_raw(struct rsnd_mod *mod)
174{
175 return mod->id;
176}
177
178int rsnd_mod_id(struct rsnd_mod *mod)
179{
180 if ((mod)->ops->id)
181 return (mod)->ops->id(mod);
182
183 return rsnd_mod_id_raw(mod);
184}
185
186int rsnd_mod_id_sub(struct rsnd_mod *mod)
187{
188 if ((mod)->ops->id_sub)
189 return (mod)->ops->id_sub(mod);
190
191 return 0;
192}
193
2099bc8e
KM
194int rsnd_mod_init(struct rsnd_priv *priv,
195 struct rsnd_mod *mod,
5ba17b42
KM
196 struct rsnd_mod_ops *ops,
197 struct clk *clk,
5ba17b42
KM
198 enum rsnd_mod_type type,
199 int id)
cdaa3cdf 200{
2f78dd7f
KM
201 int ret = clk_prepare(clk);
202
203 if (ret)
204 return ret;
205
cdaa3cdf
KM
206 mod->id = id;
207 mod->ops = ops;
a126021d 208 mod->type = type;
85642952 209 mod->clk = clk;
2099bc8e 210 mod->priv = priv;
2f78dd7f 211
652fc90f 212 return 0;
2f78dd7f
KM
213}
214
215void rsnd_mod_quit(struct rsnd_mod *mod)
216{
ed3ac14c 217 clk_unprepare(mod->clk);
ea96380b 218 mod->clk = NULL;
cdaa3cdf
KM
219}
220
f501b7a4
KM
221void rsnd_mod_interrupt(struct rsnd_mod *mod,
222 void (*callback)(struct rsnd_mod *mod,
223 struct rsnd_dai_stream *io))
224{
225 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
f501b7a4 226 struct rsnd_dai *rdai;
2daf71ad 227 int i;
f501b7a4 228
2daf71ad 229 for_each_rsnd_dai(rdai, priv, i) {
e539943c
KM
230 struct rsnd_dai_stream *io = &rdai->playback;
231
2daf71ad
KM
232 if (mod == io->mod[mod->type])
233 callback(mod, io);
f501b7a4 234
2daf71ad
KM
235 io = &rdai->capture;
236 if (mod == io->mod[mod->type])
237 callback(mod, io);
f501b7a4
KM
238 }
239}
240
d5bbe7de 241int rsnd_io_is_working(struct rsnd_dai_stream *io)
02299d98 242{
02299d98 243 /* see rsnd_dai_stream_init/quit() */
8fce974b
KM
244 if (io->substream)
245 return snd_pcm_running(io->substream);
246
247 return 0;
02299d98
KM
248}
249
b2fb31bb
KM
250int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
251 struct snd_pcm_hw_params *params)
8ec85e7f 252{
5858a7d1 253 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
b4c83b17 254
b2fb31bb
KM
255 /*
256 * params will be added when refine
257 * see
258 * __rsnd_soc_hw_rule_rate()
259 * __rsnd_soc_hw_rule_channels()
260 */
261 if (params)
262 return params_channels(params);
6da8f00e 263 else if (runtime)
b2fb31bb 264 return runtime->channels;
6da8f00e 265 return 0;
eed76bb8
KM
266}
267
b2fb31bb
KM
268int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
269 struct snd_pcm_hw_params *params)
eed76bb8 270{
b2fb31bb 271 int chan = rsnd_runtime_channel_original_with_params(io, params);
eed76bb8
KM
272 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
273
274 if (ctu_mod) {
c2aaaa57 275 u32 converted_chan = rsnd_io_converted_chan(io);
eed76bb8 276
f69f4522
KM
277 /*
278 * !! Note !!
279 *
280 * converted_chan will be used for CTU,
281 * or TDM Split mode.
282 * User shouldn't use CTU with TDM Split mode.
283 */
284 if (rsnd_runtime_is_tdm_split(io)) {
285 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));
286
287 dev_err(dev, "CTU and TDM Split should be used\n");
288 }
289
eed76bb8
KM
290 if (converted_chan)
291 return converted_chan;
292 }
293
294 return chan;
295}
296
66287def
KM
297int rsnd_channel_normalization(int chan)
298{
5db8617b 299 if (WARN_ON((chan > 8) || (chan < 0)))
66287def
KM
300 return 0;
301
302 /* TDM Extend Mode needs 8ch */
303 if (chan == 6)
304 chan = 8;
305
306 return chan;
307}
308
b2fb31bb
KM
309int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
310 struct snd_pcm_hw_params *params)
eed76bb8 311{
1ff9593d 312 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
eed76bb8 313 int chan = rsnd_io_is_play(io) ?
b2fb31bb
KM
314 rsnd_runtime_channel_after_ctu_with_params(io, params) :
315 rsnd_runtime_channel_original_with_params(io, params);
eed76bb8
KM
316
317 /* Use Multi SSI */
a6072802 318 if (rsnd_runtime_is_multi_ssi(io))
1ff9593d 319 chan /= rsnd_rdai_ssi_lane_get(rdai);
8ec85e7f 320
66287def 321 return rsnd_channel_normalization(chan);
8ec85e7f
KM
322}
323
a6072802 324int rsnd_runtime_is_multi_ssi(struct rsnd_dai_stream *io)
eed76bb8 325{
1ff9593d
KM
326 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
327 int lane = rsnd_rdai_ssi_lane_get(rdai);
eed76bb8
KM
328 int chan = rsnd_io_is_play(io) ?
329 rsnd_runtime_channel_after_ctu(io) :
330 rsnd_runtime_channel_original(io);
331
1ff9593d 332 return (chan > 2) && (lane > 1);
eed76bb8
KM
333}
334
a6072802 335int rsnd_runtime_is_tdm(struct rsnd_dai_stream *io)
eed76bb8
KM
336{
337 return rsnd_runtime_channel_for_ssi(io) >= 6;
338}
339
f69f4522
KM
340int rsnd_runtime_is_tdm_split(struct rsnd_dai_stream *io)
341{
342 return !!rsnd_flags_has(io, RSND_STREAM_TDM_SPLIT);
343}
344
d7bdbc5d 345/*
3023b384 346 * ADINR function
d7bdbc5d 347 */
3023b384 348u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
d7bdbc5d
KM
349{
350 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
d7bdbc5d
KM
351 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
352 struct device *dev = rsnd_priv_to_dev(priv);
d7bdbc5d 353
41acc8ec 354 switch (snd_pcm_format_width(runtime->format)) {
ba5d553b
DT
355 case 8:
356 return 16 << 16;
d7bdbc5d 357 case 16:
5e7b9edd 358 return 8 << 16;
41acc8ec 359 case 24:
5e7b9edd 360 return 0 << 16;
d7bdbc5d
KM
361 }
362
5e7b9edd
KM
363 dev_warn(dev, "not supported sample bits\n");
364
365 return 0;
d7bdbc5d
KM
366}
367
4689032b
KM
368/*
369 * DALIGN function
370 */
371u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
372{
49df1e39
GU
373 static const u32 dalign_values[8] = {
374 0x76543210, 0x00000032, 0x00007654, 0x00000076,
375 0xfedcba98, 0x000000ba, 0x0000fedc, 0x000000fe,
ef8e1479 376 };
49df1e39 377 int id = 0;
3ce2959d 378 struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
a504b1ee 379 struct rsnd_mod *target;
4689032b 380 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
49df1e39 381 u32 dalign;
4689032b 382
8cce431a 383 /*
a914e446
KM
384 * *Hardware* L/R and *Software* L/R are inverted for 16bit data.
385 * 31..16 15...0
386 * HW: [L ch] [R ch]
387 * SW: [R ch] [L ch]
8cce431a
KM
388 * We need to care about inversion timing to control
389 * Playback/Capture correctly.
390 * The point is [DVC] needs *Hardware* L/R, [MEM] needs *Software* L/R
391 *
392 * sL/R : software L/R
393 * hL/R : hardware L/R
394 * (*) : conversion timing
395 *
396 * Playback
397 * sL/R (*) hL/R hL/R hL/R hL/R hL/R
398 * [MEM] -> [SRC] -> [DVC] -> [CMD] -> [SSIU] -> [SSI] -> codec
399 *
400 * Capture
401 * hL/R hL/R hL/R hL/R hL/R (*) sL/R
402 * codec -> [SSI] -> [SSIU] -> [SRC] -> [DVC] -> [CMD] -> [MEM]
403 */
a504b1ee
KM
404 if (rsnd_io_is_play(io)) {
405 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
406
3ce2959d 407 target = src ? src : ssiu;
a504b1ee
KM
408 } else {
409 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
410
3ce2959d 411 target = cmd ? cmd : ssiu;
a504b1ee
KM
412 }
413
ef8e1479
NA
414 if (mod == ssiu)
415 id = rsnd_mod_id_sub(mod);
416
49df1e39
GU
417 dalign = dalign_values[id];
418
419 if (mod == target && snd_pcm_format_width(runtime->format) == 16) {
420 /* Target mod needs inverted DALIGN when 16bit */
421 dalign = (dalign & 0xf0f0f0f0) >> 4 |
422 (dalign & 0x0f0f0f0f) << 4;
423 }
ef8e1479 424
49df1e39 425 return dalign;
4689032b
KM
426}
427
90431eb4
KM
428u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
429{
28889de6 430 static const enum rsnd_mod_type playback_mods[] = {
90431eb4
KM
431 RSND_MOD_SRC,
432 RSND_MOD_CMD,
433 RSND_MOD_SSIU,
434 };
28889de6 435 static const enum rsnd_mod_type capture_mods[] = {
90431eb4
KM
436 RSND_MOD_CMD,
437 RSND_MOD_SRC,
438 RSND_MOD_SSIU,
439 };
440 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
441 struct rsnd_mod *tmod = NULL;
28889de6 442 const enum rsnd_mod_type *mods =
90431eb4
KM
443 rsnd_io_is_play(io) ?
444 playback_mods : capture_mods;
445 int i;
446
447 /*
448 * This is needed for 24bit data
449 * We need to shift 8bit
450 *
451 * Linux 24bit data is located as 0x00******
452 * HW 24bit data is located as 0x******00
453 *
454 */
ba5d553b 455 if (snd_pcm_format_width(runtime->format) != 24)
90431eb4 456 return 0;
90431eb4
KM
457
458 for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
459 tmod = rsnd_io_to_mod(io, mods[i]);
460 if (tmod)
461 break;
462 }
463
464 if (tmod != mod)
465 return 0;
466
467 if (rsnd_io_is_play(io))
468 return (0 << 20) | /* shift to Left */
469 (8 << 16); /* 8bit */
470 else
471 return (1 << 20) | /* shift to Right */
472 (8 << 16); /* 8bit */
473}
474
1536a968
KM
475/*
476 * rsnd_dai functions
477 */
b3ca3fbe
KM
478struct rsnd_mod *rsnd_mod_next(int *iterator,
479 struct rsnd_dai_stream *io,
480 enum rsnd_mod_type *array,
481 int array_size)
482{
b3ca3fbe
KM
483 int max = array ? array_size : RSND_MOD_MAX;
484
485 for (; *iterator < max; (*iterator)++) {
e539943c
KM
486 enum rsnd_mod_type type = (array) ? array[*iterator] : *iterator;
487 struct rsnd_mod *mod = rsnd_io_to_mod(io, type);
488
b12f1e3a
KM
489 if (mod)
490 return mod;
b3ca3fbe
KM
491 }
492
493 return NULL;
494}
495
38587f4c
KM
496static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
497 {
498 /* CAPTURE */
499 RSND_MOD_AUDMAPP,
500 RSND_MOD_AUDMA,
501 RSND_MOD_DVC,
502 RSND_MOD_MIX,
503 RSND_MOD_CTU,
504 RSND_MOD_CMD,
505 RSND_MOD_SRC,
506 RSND_MOD_SSIU,
507 RSND_MOD_SSIM3,
508 RSND_MOD_SSIM2,
509 RSND_MOD_SSIM1,
510 RSND_MOD_SSIP,
511 RSND_MOD_SSI,
512 }, {
513 /* PLAYBACK */
514 RSND_MOD_AUDMAPP,
515 RSND_MOD_AUDMA,
516 RSND_MOD_SSIM3,
517 RSND_MOD_SSIM2,
518 RSND_MOD_SSIM1,
519 RSND_MOD_SSIP,
520 RSND_MOD_SSI,
521 RSND_MOD_SSIU,
522 RSND_MOD_DVC,
523 RSND_MOD_MIX,
524 RSND_MOD_CTU,
525 RSND_MOD_CMD,
526 RSND_MOD_SRC,
527 },
528};
529
b43b8ae8
KM
530static int rsnd_status_update(struct rsnd_dai_stream *io,
531 struct rsnd_mod *mod, enum rsnd_mod_type type,
5f222a29
KM
532 int shift, int add, int timing)
533{
b43b8ae8 534 u32 *status = mod->ops->get_status(mod, io, type);
5f222a29
KM
535 u32 mask = 0xF << shift;
536 u8 val = (*status >> shift) & 0xF;
537 u8 next_val = (val + add) & 0xF;
538 int func_call = (val == timing);
539
b43b8ae8
KM
540 /* no status update */
541 if (add == 0 || shift == 28)
542 return 1;
543
5f222a29 544 if (next_val == 0xF) /* underflow case */
9ff07d19 545 func_call = -1;
5f222a29
KM
546 else
547 *status = (*status & ~mask) + (next_val << shift);
548
549 return func_call;
550}
551
552#define rsnd_dai_call(fn, io, param...) \
553({ \
f30b4ca4 554 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io)); \
5f222a29
KM
555 struct rsnd_mod *mod; \
556 int is_play = rsnd_io_is_play(io); \
557 int ret = 0, i; \
558 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
559 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
560 int tmp = 0; \
b43b8ae8 561 int func_call = rsnd_status_update(io, mod, types[i], \
5f222a29
KM
562 __rsnd_mod_shift_##fn, \
563 __rsnd_mod_add_##fn, \
564 __rsnd_mod_call_##fn); \
9ff07d19 565 if (func_call > 0 && (mod)->ops->fn) \
5f222a29 566 tmp = (mod)->ops->fn(mod, io, param); \
9ff07d19
KM
567 if (unlikely(func_call < 0) || \
568 unlikely(tmp && (tmp != -EPROBE_DEFER))) \
569 dev_err(dev, "%s : %s error (%d, %d)\n", \
570 rsnd_mod_name(mod), #fn, tmp, func_call);\
5f222a29
KM
571 ret |= tmp; \
572 } \
573 ret; \
cdaa3cdf
KM
574})
575
27924f32
KM
576int rsnd_dai_connect(struct rsnd_mod *mod,
577 struct rsnd_dai_stream *io,
578 enum rsnd_mod_type type)
cdaa3cdf 579{
48725e9c
KM
580 struct rsnd_priv *priv;
581 struct device *dev;
84e95355 582
6020779b 583 if (!mod)
cdaa3cdf 584 return -EIO;
cdaa3cdf 585
bfa3119c
KM
586 if (io->mod[type] == mod)
587 return 0;
588
52dc6852
KM
589 if (io->mod[type])
590 return -EINVAL;
591
48725e9c
KM
592 priv = rsnd_mod_to_priv(mod);
593 dev = rsnd_priv_to_dev(priv);
594
27924f32 595 io->mod[type] = mod;
cdaa3cdf 596
c0ea089d
KM
597 dev_dbg(dev, "%s is connected to io (%s)\n",
598 rsnd_mod_name(mod),
84e95355
KM
599 rsnd_io_is_play(io) ? "Playback" : "Capture");
600
cdaa3cdf
KM
601 return 0;
602}
603
d3a76823 604static void rsnd_dai_disconnect(struct rsnd_mod *mod,
27924f32
KM
605 struct rsnd_dai_stream *io,
606 enum rsnd_mod_type type)
d3a76823 607{
27924f32 608 io->mod[type] = NULL;
d3a76823
KM
609}
610
1ff9593d
KM
611int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
612 int max_channels)
613{
614 if (max_channels > 0)
615 rdai->max_channels = max_channels;
616
617 return rdai->max_channels;
618}
619
620int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
621 int ssi_lane)
622{
623 if (ssi_lane > 0)
624 rdai->ssi_lane = ssi_lane;
625
626 return rdai->ssi_lane;
627}
628
fb2815f4
DT
629int rsnd_rdai_width_ctrl(struct rsnd_dai *rdai, int width)
630{
631 if (width > 0)
632 rdai->chan_width = width;
633
634 return rdai->chan_width;
635}
636
710d0889 637struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
1536a968 638{
ecba9e72 639 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
2192f81c
KM
640 return NULL;
641
1536a968
KM
642 return priv->rdai + id;
643}
644
a0d847c3
KM
645static struct snd_soc_dai_driver
646*rsnd_daidrv_get(struct rsnd_priv *priv, int id)
647{
648 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
649 return NULL;
650
651 return priv->daidrv + id;
652}
653
eb2535f5 654#define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
1536a968
KM
655static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
656{
eb2535f5 657 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
1536a968 658
710d0889 659 return rsnd_rdai_get(priv, dai->id);
1536a968
KM
660}
661
1536a968
KM
662/*
663 * rsnd_soc_dai functions
664 */
75defee0
KM
665void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
666{
667 struct snd_pcm_substream *substream = io->substream;
668
669 /*
670 * this function should be called...
671 *
672 * - if rsnd_dai_pointer_update() returns true
673 * - without spin lock
674 */
675
676 snd_pcm_period_elapsed(substream);
1536a968
KM
677}
678
5626ad08 679static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
1536a968
KM
680 struct snd_pcm_substream *substream)
681{
1536a968 682 io->substream = substream;
5626ad08 683}
1536a968 684
5626ad08
KM
685static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
686{
687 io->substream = NULL;
1536a968
KM
688}
689
690static
691struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
692{
28ec78b0 693 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1536a968 694
34a43780 695 return asoc_rtd_to_cpu(rtd, 0);
1536a968
KM
696}
697
698static
699struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
700 struct snd_pcm_substream *substream)
701{
702 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
703 return &rdai->playback;
704 else
705 return &rdai->capture;
706}
707
708static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
709 struct snd_soc_dai *dai)
710{
eb2535f5 711 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
1536a968
KM
712 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
713 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1536a968
KM
714 int ret;
715 unsigned long flags;
716
02299d98 717 spin_lock_irqsave(&priv->lock, flags);
1536a968
KM
718
719 switch (cmd) {
720 case SNDRV_PCM_TRIGGER_START:
4b9c75ea 721 case SNDRV_PCM_TRIGGER_RESUME:
690602fc 722 ret = rsnd_dai_call(init, io, priv);
cdaa3cdf
KM
723 if (ret < 0)
724 goto dai_trigger_end;
725
690602fc 726 ret = rsnd_dai_call(start, io, priv);
cdaa3cdf
KM
727 if (ret < 0)
728 goto dai_trigger_end;
b5b442ab
KM
729
730 ret = rsnd_dai_call(irq, io, priv, 1);
731 if (ret < 0)
732 goto dai_trigger_end;
733
1536a968
KM
734 break;
735 case SNDRV_PCM_TRIGGER_STOP:
4b9c75ea 736 case SNDRV_PCM_TRIGGER_SUSPEND:
b5b442ab
KM
737 ret = rsnd_dai_call(irq, io, priv, 0);
738
739 ret |= rsnd_dai_call(stop, io, priv);
cdaa3cdf 740
89e3e2c3 741 ret |= rsnd_dai_call(quit, io, priv);
cdaa3cdf 742
1536a968
KM
743 break;
744 default:
745 ret = -EINVAL;
746 }
747
748dai_trigger_end:
02299d98 749 spin_unlock_irqrestore(&priv->lock, flags);
1536a968
KM
750
751 return ret;
752}
753
754static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
755{
756 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
757
f7c7a24b 758 /* set clock master for audio interface */
7704a82e 759 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
2d4dd776 760 case SND_SOC_DAIFMT_BC_FC:
e1508289 761 rdai->clk_master = 0;
1536a968 762 break;
2d4dd776 763 case SND_SOC_DAIFMT_BP_FP:
f7c7a24b 764 rdai->clk_master = 1; /* cpu is master */
1536a968
KM
765 break;
766 default:
767 return -EINVAL;
768 }
769
1536a968 770 /* set format */
22e58665 771 rdai->bit_clk_inv = 0;
1536a968
KM
772 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
773 case SND_SOC_DAIFMT_I2S:
774 rdai->sys_delay = 0;
775 rdai->data_alignment = 0;
1a7889ca 776 rdai->frm_clk_inv = 0;
1536a968
KM
777 break;
778 case SND_SOC_DAIFMT_LEFT_J:
3791b3ee 779 case SND_SOC_DAIFMT_DSP_B:
1536a968
KM
780 rdai->sys_delay = 1;
781 rdai->data_alignment = 0;
1a7889ca 782 rdai->frm_clk_inv = 1;
1536a968
KM
783 break;
784 case SND_SOC_DAIFMT_RIGHT_J:
785 rdai->sys_delay = 1;
786 rdai->data_alignment = 1;
1a7889ca
KM
787 rdai->frm_clk_inv = 1;
788 break;
3791b3ee
DT
789 case SND_SOC_DAIFMT_DSP_A:
790 rdai->sys_delay = 0;
791 rdai->data_alignment = 0;
792 rdai->frm_clk_inv = 1;
793 break;
1a7889ca
KM
794 }
795
796 /* set clock inversion */
797 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
798 case SND_SOC_DAIFMT_NB_IF:
1a7889ca
KM
799 rdai->frm_clk_inv = !rdai->frm_clk_inv;
800 break;
801 case SND_SOC_DAIFMT_IB_NF:
802 rdai->bit_clk_inv = !rdai->bit_clk_inv;
1a7889ca
KM
803 break;
804 case SND_SOC_DAIFMT_IB_IF:
805 rdai->bit_clk_inv = !rdai->bit_clk_inv;
806 rdai->frm_clk_inv = !rdai->frm_clk_inv;
807 break;
808 case SND_SOC_DAIFMT_NB_NF:
809 default:
1536a968
KM
810 break;
811 }
812
813 return 0;
814}
815
186fadc1
KM
816static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
817 u32 tx_mask, u32 rx_mask,
818 int slots, int slot_width)
819{
820 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
821 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
822 struct device *dev = rsnd_priv_to_dev(priv);
823
fb2815f4
DT
824 switch (slot_width) {
825 case 16:
826 case 24:
827 case 32:
828 break;
829 default:
82ab7e9a 830 /* use default */
1ad059a1
KM
831 /*
832 * Indicate warning if DT has "dai-tdm-slot-width"
833 * but the value was not expected.
834 */
835 if (slot_width)
836 dev_warn(dev, "unsupported TDM slot width (%d), force to use default 32\n",
837 slot_width);
82ab7e9a 838 slot_width = 32;
fb2815f4
DT
839 }
840
186fadc1 841 switch (slots) {
8cc03722 842 case 2:
f69f4522 843 /* TDM Split Mode */
186fadc1 844 case 6:
8cc03722 845 case 8:
186fadc1 846 /* TDM Extend Mode */
1ff9593d
KM
847 rsnd_rdai_channels_set(rdai, slots);
848 rsnd_rdai_ssi_lane_set(rdai, 1);
fb2815f4 849 rsnd_rdai_width_set(rdai, slot_width);
186fadc1
KM
850 break;
851 default:
852 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
853 return -EINVAL;
854 }
855
856 return 0;
857}
858
8cc03722 859static unsigned int rsnd_soc_hw_channels_list[] = {
29d03ff5 860 2, 6, 8,
8cc03722
KM
861};
862
863static unsigned int rsnd_soc_hw_rate_list[] = {
864 8000,
865 11025,
866 16000,
867 22050,
868 32000,
869 44100,
870 48000,
871 64000,
872 88200,
873 96000,
874 176400,
875 192000,
876};
877
fb2815f4 878static int rsnd_soc_hw_rule(struct rsnd_dai *rdai,
8cc03722 879 unsigned int *list, int list_num,
6d612f67
KM
880 struct snd_interval *baseline, struct snd_interval *iv,
881 struct rsnd_dai_stream *io, char *unit)
8cc03722
KM
882{
883 struct snd_interval p;
947f4eb5 884 unsigned int rate;
8cc03722
KM
885 int i;
886
887 snd_interval_any(&p);
888 p.min = UINT_MAX;
889 p.max = 0;
890
891 for (i = 0; i < list_num; i++) {
892
893 if (!snd_interval_test(iv, list[i]))
894 continue;
895
fb2815f4 896 rate = rsnd_ssi_clk_query(rdai,
8cc03722
KM
897 baseline->min, list[i], NULL);
898 if (rate > 0) {
899 p.min = min(p.min, list[i]);
900 p.max = max(p.max, list[i]);
901 }
902
fb2815f4 903 rate = rsnd_ssi_clk_query(rdai,
8cc03722
KM
904 baseline->max, list[i], NULL);
905 if (rate > 0) {
906 p.min = min(p.min, list[i]);
907 p.max = max(p.max, list[i]);
908 }
909 }
910
6d612f67
KM
911 /* Indicate error once if it can't handle */
912 if (!rsnd_flags_has(io, RSND_HW_RULE_ERR) && (p.min > p.max)) {
913 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
914 struct device *dev = rsnd_priv_to_dev(priv);
915
916 dev_warn(dev, "It can't handle %d %s <-> %d %s\n",
917 baseline->min, unit, baseline->max, unit);
918 rsnd_flags_set(io, RSND_HW_RULE_ERR);
919 }
920
8cc03722
KM
921 return snd_interval_refine(iv, &p);
922}
923
b735662f
JW
924static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
925 struct snd_pcm_hw_rule *rule)
8cc03722
KM
926{
927 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
928 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
929 struct snd_interval ic;
b735662f
JW
930 struct rsnd_dai_stream *io = rule->private;
931 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
8cc03722
KM
932
933 /*
934 * possible sampling rate limitation is same as
935 * 2ch if it supports multi ssi
b2fb31bb 936 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
8cc03722
KM
937 */
938 ic = *ic_;
b2fb31bb
KM
939 ic.min =
940 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
8cc03722 941
fb2815f4 942 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_rate_list,
8cc03722 943 ARRAY_SIZE(rsnd_soc_hw_rate_list),
6d612f67 944 &ic, ir, io, "ch");
8cc03722
KM
945}
946
b735662f
JW
947static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
948 struct snd_pcm_hw_rule *rule)
8cc03722
KM
949{
950 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
951 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
952 struct snd_interval ic;
b735662f
JW
953 struct rsnd_dai_stream *io = rule->private;
954 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
8cc03722
KM
955
956 /*
957 * possible sampling rate limitation is same as
958 * 2ch if it supports multi ssi
b2fb31bb 959 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
8cc03722
KM
960 */
961 ic = *ic_;
b2fb31bb
KM
962 ic.min =
963 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
8cc03722 964
fb2815f4 965 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_channels_list,
8cc03722 966 ARRAY_SIZE(rsnd_soc_hw_channels_list),
6d612f67 967 ir, &ic, io, "Hz");
8cc03722
KM
968}
969
5c2e035e 970static const struct snd_pcm_hardware rsnd_pcm_hardware = {
3c9736aa
KM
971 .info = SNDRV_PCM_INFO_INTERLEAVED |
972 SNDRV_PCM_INFO_MMAP |
973 SNDRV_PCM_INFO_MMAP_VALID,
974 .buffer_bytes_max = 64 * 1024,
975 .period_bytes_min = 32,
976 .period_bytes_max = 8192,
977 .periods_min = 1,
978 .periods_max = 32,
979 .fifo_size = 256,
980};
981
982static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
983 struct snd_soc_dai *dai)
8cc03722
KM
984{
985 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
3c9736aa 986 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
8cc03722 987 struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
3c9736aa 988 struct snd_pcm_runtime *runtime = substream->runtime;
8cc03722
KM
989 unsigned int max_channels = rsnd_rdai_channels_get(rdai);
990 int i;
991
6d612f67
KM
992 rsnd_flags_del(io, RSND_HW_RULE_ERR);
993
b2fb31bb
KM
994 rsnd_dai_stream_init(io, substream);
995
8cc03722
KM
996 /*
997 * Channel Limitation
998 * It depends on Platform design
999 */
1000 constraint->list = rsnd_soc_hw_channels_list;
1001 constraint->count = 0;
1002 constraint->mask = 0;
1003
1004 for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
1005 if (rsnd_soc_hw_channels_list[i] > max_channels)
1006 break;
1007 constraint->count = i + 1;
1008 }
1009
3c9736aa
KM
1010 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
1011
8cc03722
KM
1012 snd_pcm_hw_constraint_list(runtime, 0,
1013 SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
1014
3c9736aa
KM
1015 snd_pcm_hw_constraint_integer(runtime,
1016 SNDRV_PCM_HW_PARAM_PERIODS);
1017
8cc03722
KM
1018 /*
1019 * Sampling Rate / Channel Limitation
1020 * It depends on Clock Master Mode
1021 */
3c9736aa 1022 if (rsnd_rdai_is_clk_master(rdai)) {
b2fb31bb
KM
1023 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1024
3c9736aa 1025 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
b735662f
JW
1026 rsnd_soc_hw_rule_rate,
1027 is_play ? &rdai->playback : &rdai->capture,
3c9736aa
KM
1028 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1029 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
b735662f
JW
1030 rsnd_soc_hw_rule_channels,
1031 is_play ? &rdai->playback : &rdai->capture,
3c9736aa
KM
1032 SNDRV_PCM_HW_PARAM_RATE, -1);
1033 }
8cc03722 1034
6ab6a247 1035 return 0;
10a9cca1
KM
1036}
1037
1038static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
1039 struct snd_soc_dai *dai)
1040{
1041 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
f30b4ca4 1042 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
10a9cca1
KM
1043 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1044
1045 /*
1046 * call rsnd_dai_call without spinlock
1047 */
6ab6a247 1048 rsnd_dai_call(cleanup, io, priv);
b2fb31bb
KM
1049
1050 rsnd_dai_stream_quit(io);
10a9cca1
KM
1051}
1052
4d230d12
JW
1053static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream,
1054 struct snd_soc_dai *dai)
1055{
1056 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
1057 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1058 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1059
1060 return rsnd_dai_call(prepare, io, priv);
1061}
1062
02921765
KM
1063static u64 rsnd_soc_dai_formats[] = {
1064 /*
1065 * 1st Priority
1066 *
1067 * Well tested formats.
1068 * Select below from Sound Card, not auto
1069 * SND_SOC_DAIFMT_CBC_CFC
1070 * SND_SOC_DAIFMT_CBP_CFP
1071 */
1072 SND_SOC_POSSIBLE_DAIFMT_I2S |
1073 SND_SOC_POSSIBLE_DAIFMT_RIGHT_J |
1074 SND_SOC_POSSIBLE_DAIFMT_LEFT_J |
1075 SND_SOC_POSSIBLE_DAIFMT_NB_NF |
1076 SND_SOC_POSSIBLE_DAIFMT_NB_IF |
1077 SND_SOC_POSSIBLE_DAIFMT_IB_NF |
1078 SND_SOC_POSSIBLE_DAIFMT_IB_IF,
1079 /*
1080 * 2nd Priority
1081 *
1082 * Supported, but not well tested
1083 */
1084 SND_SOC_POSSIBLE_DAIFMT_DSP_A |
1085 SND_SOC_POSSIBLE_DAIFMT_DSP_B,
1086};
1087
2264cf2e 1088static void rsnd_parse_tdm_split_mode(struct rsnd_priv *priv,
f497c88b
KM
1089 struct rsnd_dai_stream *io,
1090 struct device_node *dai_np)
538a4ffe
KM
1091{
1092 struct device *dev = rsnd_priv_to_dev(priv);
538a4ffe
KM
1093 struct device_node *ssiu_np = rsnd_ssiu_of_node(priv);
1094 struct device_node *np;
f497c88b 1095 int is_play = rsnd_io_is_play(io);
e539943c 1096 int i;
538a4ffe
KM
1097
1098 if (!ssiu_np)
1099 return;
1100
538a4ffe
KM
1101 /*
1102 * This driver assumes that it is TDM Split mode
1103 * if it includes ssiu node
1104 */
1105 for (i = 0;; i++) {
1106 struct device_node *node = is_play ?
1107 of_parse_phandle(dai_np, "playback", i) :
1108 of_parse_phandle(dai_np, "capture", i);
1109
1110 if (!node)
1111 break;
1112
538a4ffe
KM
1113 for_each_child_of_node(ssiu_np, np) {
1114 if (np == node) {
1115 rsnd_flags_set(io, RSND_STREAM_TDM_SPLIT);
1116 dev_dbg(dev, "%s is part of TDM Split\n", io->name);
1117 }
538a4ffe
KM
1118 }
1119
ef2c6951 1120 of_node_put(node);
538a4ffe 1121 }
ef2c6951
KM
1122
1123 of_node_put(ssiu_np);
538a4ffe
KM
1124}
1125
2264cf2e
KM
1126static void rsnd_parse_connect_simple(struct rsnd_priv *priv,
1127 struct rsnd_dai_stream *io,
1128 struct device_node *dai_np)
1129{
1130 if (!rsnd_io_to_mod_ssi(io))
1131 return;
1132
1133 rsnd_parse_tdm_split_mode(priv, io, dai_np);
1134}
1135
beed78ae
KM
1136static void rsnd_parse_connect_graph(struct rsnd_priv *priv,
1137 struct rsnd_dai_stream *io,
1138 struct device_node *endpoint)
1139{
1140 struct device *dev = rsnd_priv_to_dev(priv);
ef2c6951 1141 struct device_node *remote_node;
beed78ae
KM
1142
1143 if (!rsnd_io_to_mod_ssi(io))
1144 return;
1145
ef2c6951
KM
1146 remote_node = of_graph_get_remote_port_parent(endpoint);
1147
beed78ae
KM
1148 /* HDMI0 */
1149 if (strstr(remote_node->full_name, "hdmi@fead0000")) {
1150 rsnd_flags_set(io, RSND_STREAM_HDMI0);
1151 dev_dbg(dev, "%s connected to HDMI0\n", io->name);
1152 }
1153
1154 /* HDMI1 */
1155 if (strstr(remote_node->full_name, "hdmi@feae0000")) {
1156 rsnd_flags_set(io, RSND_STREAM_HDMI1);
1157 dev_dbg(dev, "%s connected to HDMI1\n", io->name);
1158 }
f69f4522 1159
2264cf2e 1160 rsnd_parse_tdm_split_mode(priv, io, endpoint);
ef2c6951
KM
1161
1162 of_node_put(remote_node);
beed78ae
KM
1163}
1164
ec02b5a1 1165void rsnd_parse_connect_common(struct rsnd_dai *rdai, char *name,
89b66174
KM
1166 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
1167 struct device_node *node,
1168 struct device_node *playback,
1169 struct device_node *capture)
1170{
1171 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
d09a7db4 1172 struct device *dev = rsnd_priv_to_dev(priv);
89b66174 1173 struct device_node *np;
89b66174
KM
1174 int i;
1175
1176 if (!node)
1177 return;
1178
1179 i = 0;
1180 for_each_child_of_node(node, np) {
73919dbe
KM
1181 struct rsnd_mod *mod;
1182
d09a7db4
KM
1183 i = rsnd_node_fixed_index(dev, np, name, i);
1184 if (i < 0) {
1185 of_node_put(np);
1186 break;
1187 }
c413983e 1188
73919dbe 1189 mod = mod_get(priv, i);
e539943c 1190
89b66174
KM
1191 if (np == playback)
1192 rsnd_dai_connect(mod, &rdai->playback, mod->type);
1193 if (np == capture)
1194 rsnd_dai_connect(mod, &rdai->capture, mod->type);
1195 i++;
1196 }
1197
1198 of_node_put(node);
1199}
1200
d09a7db4 1201int rsnd_node_fixed_index(struct device *dev, struct device_node *node, char *name, int idx)
c413983e
KM
1202{
1203 char node_name[16];
1204
1205 /*
1206 * rsnd is assuming each device nodes are sequential numbering,
1207 * but some of them are not.
1208 * This function adjusts index for it.
1209 *
1210 * ex)
1211 * Normal case, special case
1212 * ssi-0
1213 * ssi-1
1214 * ssi-2
1215 * ssi-3 ssi-3
1216 * ssi-4 ssi-4
1217 * ...
1218 *
1219 * assume Max 64 node
1220 */
1221 for (; idx < 64; idx++) {
1222 snprintf(node_name, sizeof(node_name), "%s-%d", name, idx);
1223
1224 if (strncmp(node_name, of_node_full_name(node), sizeof(node_name)) == 0)
1225 return idx;
1226 }
1227
d09a7db4
KM
1228 dev_err(dev, "strange node numbering (%s)",
1229 of_node_full_name(node));
c413983e
KM
1230 return -EINVAL;
1231}
1232
1233int rsnd_node_count(struct rsnd_priv *priv, struct device_node *node, char *name)
1234{
1235 struct device *dev = rsnd_priv_to_dev(priv);
1236 struct device_node *np;
1237 int i;
1238
1239 i = 0;
1240 for_each_child_of_node(node, np) {
d09a7db4 1241 i = rsnd_node_fixed_index(dev, np, name, i);
c413983e 1242 if (i < 0) {
17363235 1243 of_node_put(np);
c413983e
KM
1244 return 0;
1245 }
1246 i++;
1247 }
1248
1249 return i;
1250}
1251
547b02f7 1252static int rsnd_dai_of_node(struct rsnd_priv *priv, int *is_graph)
11d0f8ed
KM
1253{
1254 struct device *dev = rsnd_priv_to_dev(priv);
1255 struct device_node *np = dev->of_node;
547b02f7
KM
1256 struct device_node *ports, *node;
1257 int nr = 0;
1258 int i = 0;
11d0f8ed
KM
1259
1260 *is_graph = 0;
1261
1262 /*
1263 * parse both previous dai (= rcar_sound,dai), and
1264 * graph dai (= ports/port)
1265 */
6328489c
KM
1266
1267 /*
1268 * Simple-Card
1269 */
547b02f7
KM
1270 node = of_get_child_by_name(np, RSND_NODE_DAI);
1271 if (!node)
1272 goto audio_graph;
1273
1274 of_node_put(node);
1275
1276 for_each_child_of_node(np, node) {
1277 if (!of_node_name_eq(node, RSND_NODE_DAI))
1278 continue;
1279
1280 priv->component_dais[i] = of_get_child_count(node);
1281 nr += priv->component_dais[i];
1282 i++;
1283 if (i >= RSND_MAX_COMPONENT) {
1284 dev_info(dev, "reach to max component\n");
1a5ca2aa 1285 of_node_put(node);
547b02f7
KM
1286 break;
1287 }
11d0f8ed
KM
1288 }
1289
547b02f7
KM
1290 return nr;
1291
1292audio_graph:
6328489c
KM
1293 /*
1294 * Audio-Graph-Card
1295 */
547b02f7
KM
1296 for_each_child_of_node(np, ports) {
1297 if (!of_node_name_eq(ports, "ports") &&
1298 !of_node_name_eq(ports, "port"))
1299 continue;
1300 priv->component_dais[i] = of_graph_get_endpoint_count(ports);
1301 nr += priv->component_dais[i];
1302 i++;
1303 if (i >= RSND_MAX_COMPONENT) {
1304 dev_info(dev, "reach to max component\n");
1a5ca2aa 1305 of_node_put(node);
28115b1c 1306 of_node_put(ports);
547b02f7
KM
1307 break;
1308 }
6328489c 1309 }
11d0f8ed 1310
547b02f7 1311 *is_graph = 1;
11d0f8ed 1312
547b02f7 1313 return nr;
11d0f8ed
KM
1314}
1315
e9b5daad
KM
1316
1317#define PREALLOC_BUFFER (32 * 1024)
1318#define PREALLOC_BUFFER_MAX (32 * 1024)
1319
1320static 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) {
e494dbcd
TI
1339 snd_pcm_set_managed_buffer(substream,
1340 SNDRV_DMA_TYPE_DEV,
1341 dev,
1342 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
e9b5daad
KM
1343 }
1344
1345 return 0;
1346}
1347
4b0891a7 1348static int rsnd_soc_dai_pcm_new(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai)
e9b5daad
KM
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
4b0891a7
KM
1374static 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
4d4b334b
KM
1386static void __rsnd_dai_probe(struct rsnd_priv *priv,
1387 struct device_node *dai_np,
547b02f7
KM
1388 struct device_node *node_np,
1389 uint32_t node_arg,
9f761183 1390 int dai_i)
90e8e50f 1391{
94e2710c
KM
1392 struct rsnd_dai_stream *io_playback;
1393 struct rsnd_dai_stream *io_capture;
4d4b334b 1394 struct snd_soc_dai_driver *drv;
94e2710c 1395 struct rsnd_dai *rdai;
2ea6b074 1396 struct device *dev = rsnd_priv_to_dev(priv);
68a410af 1397 int playback_exist = 0, capture_exist = 0;
4d4b334b
KM
1398 int io_i;
1399
1400 rdai = rsnd_rdai_get(priv, dai_i);
a0d847c3 1401 drv = rsnd_daidrv_get(priv, dai_i);
4d4b334b
KM
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
547b02f7
KM
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
4d4b334b
KM
1412 rdai->priv = priv;
1413 drv->name = rdai->name;
1414 drv->ops = &rsnd_soc_dai_ops;
8897a147 1415 drv->id = dai_i;
547b02f7 1416 drv->dai_args = &rdai->dai_args;
4d4b334b 1417
9328882e
KM
1418 io_playback->rdai = rdai;
1419 io_capture->rdai = rdai;
1ff9593d
KM
1420 rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1421 rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
fb2815f4 1422 rsnd_rdai_width_set(rdai, 32); /* default 32bit width */
4d4b334b
KM
1423
1424 for (io_i = 0;; io_i++) {
e539943c
KM
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);
4d4b334b
KM
1427
1428 if (!playback && !capture)
1429 break;
1430
68a410af
KM
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
4d4b334b 1439 rsnd_parse_connect_ssi(rdai, playback, capture);
4e7788fb 1440 rsnd_parse_connect_ssiu(rdai, playback, capture);
4d4b334b
KM
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
68a410af
KM
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
7cc90a5c
KM
1467 if (rsnd_ssi_is_pin_sharing(io_capture) ||
1468 rsnd_ssi_is_pin_sharing(io_playback)) {
f1cd5f3b
KM
1469 /* should have symmetric_rate if pin sharing */
1470 drv->symmetric_rate = 1;
7cc90a5c
KM
1471 }
1472
4d4b334b
KM
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
1478static int rsnd_dai_probe(struct rsnd_priv *priv)
1479{
4d4b334b
KM
1480 struct snd_soc_dai_driver *rdrv;
1481 struct device *dev = rsnd_priv_to_dev(priv);
547b02f7 1482 struct device_node *np = dev->of_node;
4d4b334b 1483 struct rsnd_dai *rdai;
6328489c 1484 int nr = 0;
11d0f8ed 1485 int is_graph;
4d4b334b 1486 int dai_i;
90e8e50f 1487
547b02f7 1488 nr = rsnd_dai_of_node(priv, &is_graph);
4d4b334b
KM
1489 if (!nr)
1490 return -EINVAL;
90e8e50f 1491
a86854d0
KC
1492 rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1493 rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
4d4b334b
KM
1494 if (!rdrv || !rdai)
1495 return -ENOMEM;
90e8e50f 1496
94e2710c 1497 priv->rdai_nr = nr;
2ff2ecca 1498 priv->daidrv = rdrv;
94e2710c 1499 priv->rdai = rdai;
90e8e50f
KM
1500
1501 /*
1502 * parse all dai
1503 */
1504 dai_i = 0;
4d4b334b 1505 if (is_graph) {
547b02f7
KM
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++;
beed78ae 1522 }
7fa72cca 1523 }
4d4b334b 1524 } else {
547b02f7
KM
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;
f497c88b 1531
547b02f7
KM
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++;
538a4ffe 1541 }
538a4ffe 1542 }
1536a968
KM
1543 }
1544
4d4b334b 1545 return 0;
1536a968
KM
1546}
1547
1536a968
KM
1548/*
1549 * pcm ops
1550 */
b43b8ae8
KM
1551static 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
9dcefa72
KM
1571static int rsnd_hw_params(struct snd_soc_component *component,
1572 struct snd_pcm_substream *substream,
1573 struct snd_pcm_hw_params *hw_params)
1536a968 1574{
3b7843ff
KM
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);
28ec78b0 1578 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
3b7843ff 1579
c2aaaa57
KM
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;
c2aaaa57
KM
1595 int stream = substream->stream;
1596
1597 for_each_dpcm_be(fe, stream, dpcm) {
25106550
KM
1598 struct snd_soc_pcm_runtime *be = dpcm->be;
1599 struct snd_pcm_hw_params *be_params = &be->dpcm[stream].hw_params;
e539943c 1600
c2aaaa57
KM
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);
19c6a63c
MD
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
c2aaaa57 1622 dev_dbg(dev, "convert rate = %d\n", io->converted_rate);
19c6a63c
MD
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 =
4aa2b05a 1662 DIV_ROUND_UP(io->converted_rate, k_up);
19c6a63c 1663 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
4aa2b05a 1664 DIV_ROUND_UP(io->converted_rate, k_up);
19c6a63c
MD
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 }
c2aaaa57
KM
1677 }
1678
b43b8ae8 1679 return rsnd_hw_update(substream, hw_params);
1536a968
KM
1680}
1681
9dcefa72
KM
1682static int rsnd_hw_free(struct snd_soc_component *component,
1683 struct snd_pcm_substream *substream)
859fd6cb 1684{
b43b8ae8 1685 return rsnd_hw_update(substream, NULL);
859fd6cb
TW
1686}
1687
9dcefa72
KM
1688static snd_pcm_uframes_t rsnd_pointer(struct snd_soc_component *component,
1689 struct snd_pcm_substream *substream)
1536a968 1690{
1536a968
KM
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);
07b7acb5
KM
1694 snd_pcm_uframes_t pointer = 0;
1695
1696 rsnd_dai_call(pointer, io, &pointer);
1536a968 1697
07b7acb5 1698 return pointer;
1536a968
KM
1699}
1700
170a2497
KM
1701/*
1702 * snd_kcontrol
1703 */
170a2497
KM
1704static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1705 struct snd_ctl_elem_info *uinfo)
1706{
16d44989 1707 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
170a2497
KM
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;
75b1a8f9 1715 strscpy(uinfo->value.enumerated.name,
170a2497
KM
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
1730static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1731 struct snd_ctl_elem_value *uc)
1732{
16d44989 1733 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
170a2497
KM
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
1745static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1746 struct snd_ctl_elem_value *uc)
1747{
16d44989 1748 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
170a2497
KM
1749 int i, change = 0;
1750
f0b04d8b
KM
1751 if (!cfg->accept(cfg->io))
1752 return 0;
1753
170a2497
KM
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
d7289565 1764 if (change && cfg->update)
16d44989 1765 cfg->update(cfg->io, cfg->mod);
170a2497
KM
1766
1767 return change;
1768}
1769
f0b04d8b
KM
1770int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1771{
1772 return 1;
1773}
1774
1775int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1776{
1777 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
b5c08868
JW
1778 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1779 struct device *dev = rsnd_priv_to_dev(priv);
f0b04d8b 1780
b5c08868
JW
1781 if (!runtime) {
1782 dev_warn(dev, "Can't update kctrl when idle\n");
1783 return 0;
1784 }
1785
1786 return 1;
f0b04d8b
KM
1787}
1788
32973dcf
KM
1789struct 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
1796struct 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
f3c26ac6
KM
1803const 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 */
3e3c9ee1 1814 "0.125 dB/1 step", /* 01010 = VOLUME_RAMP_MAX_MIX */
f3c26ac6
KM
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
32973dcf
KM
1830int 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,
f0b04d8b 1834 int (*accept)(struct rsnd_dai_stream *io),
32973dcf
KM
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)
170a2497
KM
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,
1a497983 1848 .index = rtd->num,
170a2497
KM
1849 .get = rsnd_kctrl_get,
1850 .put = rsnd_kctrl_put,
170a2497
KM
1851 };
1852 int ret;
1853
9c698e84 1854 /*
7aea8a9d
KM
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
9c698e84
KM
1858 */
1859 list_for_each_entry(kctrl, &card->controls, list) {
1860 struct rsnd_kctrl_cfg *c = kctrl->private_data;
1861
7aea8a9d 1862 if (c == cfg)
9c698e84
KM
1863 return 0;
1864 }
1865
32973dcf
KM
1866 if (size > RSND_MAX_CHANNELS)
1867 return -EINVAL;
1868
16d44989 1869 kctrl = snd_ctl_new1(&knew, cfg);
170a2497
KM
1870 if (!kctrl)
1871 return -ENOMEM;
1872
1873 ret = snd_ctl_add(card, kctrl);
0ea617a2 1874 if (ret < 0)
170a2497
KM
1875 return ret;
1876
32973dcf
KM
1877 cfg->texts = texts;
1878 cfg->max = max;
1879 cfg->size = size;
f0b04d8b 1880 cfg->accept = accept;
32973dcf
KM
1881 cfg->update = update;
1882 cfg->card = card;
1883 cfg->kctrl = kctrl;
1884 cfg->io = io;
16d44989 1885 cfg->mod = mod;
170a2497
KM
1886
1887 return 0;
1888}
1889
1536a968 1890/*
019ea01b 1891 * snd_soc_component
1536a968 1892 */
019ea01b 1893static const struct snd_soc_component_driver rsnd_soc_component = {
f712ff57
CK
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,
1536a968
KM
1900};
1901
d3a76823 1902static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
f708d944 1903 struct rsnd_dai_stream *io)
d3a76823 1904{
d3a76823
KM
1905 int ret;
1906
690602fc 1907 ret = rsnd_dai_call(probe, io, priv);
d3a76823 1908 if (ret == -EAGAIN) {
48d58281 1909 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
9b87bfb2 1910 struct rsnd_mod *mod;
48d58281
KM
1911 int i;
1912
d3a76823
KM
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()
97463e19 1922 * rsnd_ssi_fallback()
d3a76823 1923 */
690602fc 1924 rsnd_dai_call(remove, io, priv);
d3a76823
KM
1925
1926 /*
48d58281
KM
1927 * remove all mod from io
1928 * and, re connect ssi
d3a76823 1929 */
9b87bfb2
KM
1930 for_each_rsnd_mod(i, mod, io)
1931 rsnd_dai_disconnect(mod, io, i);
48d58281 1932 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
d3a76823 1933
97463e19
KM
1934 /*
1935 * fallback
1936 */
690602fc 1937 rsnd_dai_call(fallback, io, priv);
97463e19 1938
d3a76823
KM
1939 /*
1940 * retry to "probe".
1941 * DAI has SSI which is PIO mode only now.
1942 */
690602fc 1943 ret = rsnd_dai_call(probe, io, priv);
d3a76823
KM
1944 }
1945
1946 return ret;
1947}
1948
1536a968
KM
1949/*
1950 * rsnd probe
1951 */
1952static int rsnd_probe(struct platform_device *pdev)
1953{
1536a968
KM
1954 struct rsnd_priv *priv;
1955 struct device *dev = &pdev->dev;
7681f6ac 1956 struct rsnd_dai *rdai;
2ea6b074 1957 int (*probe_func[])(struct rsnd_priv *priv) = {
d1ac970f 1958 rsnd_gen_probe,
288f392e 1959 rsnd_dma_probe,
d1ac970f 1960 rsnd_ssi_probe,
c7f69ab5 1961 rsnd_ssiu_probe,
ba9c949f 1962 rsnd_src_probe,
9269e3c3 1963 rsnd_ctu_probe,
70fb1052 1964 rsnd_mix_probe,
bff58ea4 1965 rsnd_dvc_probe,
1b2ca0ad 1966 rsnd_cmd_probe,
d1ac970f
KM
1967 rsnd_adg_probe,
1968 rsnd_dai_probe,
1969 };
1970 int ret, i;
547b02f7 1971 int ci;
1536a968 1972
1536a968
KM
1973 /*
1974 * init priv data
1975 */
1976 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0d7820d0 1977 if (!priv)
1536a968 1978 return -ENODEV;
1536a968 1979
9f464f8e 1980 priv->pdev = pdev;
6d8044b4 1981 priv->flags = (unsigned long)of_device_get_match_data(dev);
1536a968
KM
1982 spin_lock_init(&priv->lock);
1983
1984 /*
1985 * init each module
1986 */
d1ac970f 1987 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
2ea6b074 1988 ret = probe_func[i](priv);
d1ac970f
KM
1989 if (ret)
1990 return ret;
1991 }
07539c1d 1992
7681f6ac 1993 for_each_rsnd_dai(rdai, priv, i) {
f708d944 1994 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
7681f6ac 1995 if (ret)
d62a3dcd 1996 goto exit_snd_probe;
dfc9403b 1997
f708d944 1998 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
7681f6ac 1999 if (ret)
d62a3dcd 2000 goto exit_snd_probe;
7681f6ac 2001 }
4b4dab82 2002
0b1f6ec7
KM
2003 dev_set_drvdata(dev, priv);
2004
1536a968
KM
2005 /*
2006 * asoc register
2007 */
547b02f7
KM
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;
1536a968
KM
2020 }
2021
1536a968
KM
2022 pm_runtime_enable(dev);
2023
2024 dev_info(dev, "probed\n");
2025 return ret;
2026
d62a3dcd
KM
2027exit_snd_probe:
2028 for_each_rsnd_dai(rdai, priv, i) {
690602fc
KM
2029 rsnd_dai_call(remove, &rdai->playback, priv);
2030 rsnd_dai_call(remove, &rdai->capture, priv);
d62a3dcd 2031 }
1536a968 2032
6c92d5a2
KM
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
1536a968
KM
2041 return ret;
2042}
2043
5310f0a3 2044static void rsnd_remove(struct platform_device *pdev)
1536a968
KM
2045{
2046 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
7681f6ac 2047 struct rsnd_dai *rdai;
2ea6b074 2048 void (*remove_func[])(struct rsnd_priv *priv) = {
2f78dd7f 2049 rsnd_ssi_remove,
c7f69ab5 2050 rsnd_ssiu_remove,
2f78dd7f 2051 rsnd_src_remove,
9269e3c3 2052 rsnd_ctu_remove,
70fb1052 2053 rsnd_mix_remove,
2f78dd7f 2054 rsnd_dvc_remove,
1b2ca0ad 2055 rsnd_cmd_remove,
68a55024 2056 rsnd_adg_remove,
2f78dd7f 2057 };
e57297fc 2058 int i;
1536a968
KM
2059
2060 pm_runtime_disable(&pdev->dev);
2061
7681f6ac 2062 for_each_rsnd_dai(rdai, priv, i) {
e57297fc
UKK
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);
7681f6ac 2072 }
1536a968 2073
2f78dd7f 2074 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
2ea6b074 2075 remove_func[i](priv);
1536a968
KM
2076}
2077
6f542703 2078static int __maybe_unused rsnd_suspend(struct device *dev)
c2d31718
KM
2079{
2080 struct rsnd_priv *priv = dev_get_drvdata(dev);
2081
2082 rsnd_adg_clk_disable(priv);
2083
2084 return 0;
2085}
2086
6f542703 2087static int __maybe_unused rsnd_resume(struct device *dev)
c2d31718
KM
2088{
2089 struct rsnd_priv *priv = dev_get_drvdata(dev);
2090
2091 rsnd_adg_clk_enable(priv);
2092
2093 return 0;
2094}
2095
49ebf13b 2096static const struct dev_pm_ops rsnd_pm_ops = {
f8a9a29c 2097 SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
c2d31718
KM
2098};
2099
1536a968
KM
2100static struct platform_driver rsnd_driver = {
2101 .driver = {
2102 .name = "rcar_sound",
c2d31718 2103 .pm = &rsnd_pm_ops,
90e8e50f 2104 .of_match_table = rsnd_of_match,
1536a968
KM
2105 },
2106 .probe = rsnd_probe,
5310f0a3 2107 .remove_new = rsnd_remove,
1536a968
KM
2108};
2109module_platform_driver(rsnd_driver);
2110
1e0edd4d 2111MODULE_LICENSE("GPL v2");
1536a968
KM
2112MODULE_DESCRIPTION("Renesas R-Car audio driver");
2113MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
2114MODULE_ALIAS("platform:rcar-pcm-audio");