]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - sound/soc/sh/rcar/core.c
ASoC: rsnd: move .get_status under rsnd_mod_ops
[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
KM
92
93/*
94 * you can enable below define if you don't need
95 * DAI status debug message when debugging
96 * see rsnd_dbg_dai_call()
97 *
98 * #define RSND_DEBUG_NO_DAI_CALL 1
99 */
100
1536a968
KM
101#include <linux/pm_runtime.h>
102#include "rsnd.h"
103
dc272156 104#define RSND_RATES SNDRV_PCM_RATE_8000_192000
ba5d553b
DT
105#define RSND_FMTS (SNDRV_PCM_FMTBIT_S8 |\
106 SNDRV_PCM_FMTBIT_S16_LE |\
107 SNDRV_PCM_FMTBIT_S24_LE)
1536a968 108
33187fb4 109static const struct of_device_id rsnd_of_match[] = {
e797f58e
KM
110 { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
111 { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
d188e140 112 { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN3 },
90e8e50f
KM
113 {},
114};
115MODULE_DEVICE_TABLE(of, rsnd_of_match);
116
81ad174d
KM
117/*
118 * rsnd_mod functions
119 */
f1df1229
KM
120void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
121{
122 if (mod->type != type) {
123 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
124 struct device *dev = rsnd_priv_to_dev(priv);
125
126 dev_warn(dev, "%s[%d] is not your expected module\n",
127 rsnd_mod_name(mod), rsnd_mod_id(mod));
128 }
129}
130
9b99e9a7
KM
131struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
132 struct rsnd_mod *mod)
d9288d0b 133{
72adc61f
KM
134 if (!mod || !mod->ops || !mod->ops->dma_req)
135 return NULL;
d9288d0b 136
9b99e9a7 137 return mod->ops->dma_req(io, mod);
d9288d0b
KM
138}
139
7e7fe06d
KM
140u32 *rsnd_mod_get_status(struct rsnd_mod *mod,
141 struct rsnd_dai_stream *io,
5ba17b42
KM
142 enum rsnd_mod_type type)
143{
144 return &mod->status;
145}
146
2099bc8e
KM
147int rsnd_mod_init(struct rsnd_priv *priv,
148 struct rsnd_mod *mod,
5ba17b42
KM
149 struct rsnd_mod_ops *ops,
150 struct clk *clk,
5ba17b42
KM
151 enum rsnd_mod_type type,
152 int id)
cdaa3cdf 153{
2f78dd7f
KM
154 int ret = clk_prepare(clk);
155
156 if (ret)
157 return ret;
158
cdaa3cdf
KM
159 mod->id = id;
160 mod->ops = ops;
a126021d 161 mod->type = type;
85642952 162 mod->clk = clk;
2099bc8e 163 mod->priv = priv;
2f78dd7f
KM
164
165 return ret;
166}
167
168void rsnd_mod_quit(struct rsnd_mod *mod)
169{
ed3ac14c 170 clk_unprepare(mod->clk);
ea96380b 171 mod->clk = NULL;
cdaa3cdf
KM
172}
173
f501b7a4
KM
174void rsnd_mod_interrupt(struct rsnd_mod *mod,
175 void (*callback)(struct rsnd_mod *mod,
176 struct rsnd_dai_stream *io))
177{
178 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
179 struct rsnd_dai_stream *io;
180 struct rsnd_dai *rdai;
2daf71ad 181 int i;
f501b7a4 182
2daf71ad
KM
183 for_each_rsnd_dai(rdai, priv, i) {
184 io = &rdai->playback;
185 if (mod == io->mod[mod->type])
186 callback(mod, io);
f501b7a4 187
2daf71ad
KM
188 io = &rdai->capture;
189 if (mod == io->mod[mod->type])
190 callback(mod, io);
f501b7a4
KM
191 }
192}
193
d5bbe7de 194int rsnd_io_is_working(struct rsnd_dai_stream *io)
02299d98 195{
02299d98 196 /* see rsnd_dai_stream_init/quit() */
8fce974b
KM
197 if (io->substream)
198 return snd_pcm_running(io->substream);
199
200 return 0;
02299d98
KM
201}
202
b2fb31bb
KM
203int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
204 struct snd_pcm_hw_params *params)
8ec85e7f 205{
5858a7d1 206 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
b4c83b17 207
b2fb31bb
KM
208 /*
209 * params will be added when refine
210 * see
211 * __rsnd_soc_hw_rule_rate()
212 * __rsnd_soc_hw_rule_channels()
213 */
214 if (params)
215 return params_channels(params);
216 else
217 return runtime->channels;
eed76bb8
KM
218}
219
b2fb31bb
KM
220int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
221 struct snd_pcm_hw_params *params)
eed76bb8 222{
b2fb31bb 223 int chan = rsnd_runtime_channel_original_with_params(io, params);
eed76bb8
KM
224 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
225
226 if (ctu_mod) {
227 u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod);
228
229 if (converted_chan)
230 return converted_chan;
231 }
232
233 return chan;
234}
235
b2fb31bb
KM
236int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
237 struct snd_pcm_hw_params *params)
eed76bb8 238{
1ff9593d 239 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
eed76bb8 240 int chan = rsnd_io_is_play(io) ?
b2fb31bb
KM
241 rsnd_runtime_channel_after_ctu_with_params(io, params) :
242 rsnd_runtime_channel_original_with_params(io, params);
eed76bb8
KM
243
244 /* Use Multi SSI */
245 if (rsnd_runtime_is_ssi_multi(io))
1ff9593d 246 chan /= rsnd_rdai_ssi_lane_get(rdai);
8ec85e7f
KM
247
248 /* TDM Extend Mode needs 8ch */
249 if (chan == 6)
250 chan = 8;
251
252 return chan;
253}
254
eed76bb8
KM
255int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io)
256{
1ff9593d
KM
257 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
258 int lane = rsnd_rdai_ssi_lane_get(rdai);
eed76bb8
KM
259 int chan = rsnd_io_is_play(io) ?
260 rsnd_runtime_channel_after_ctu(io) :
261 rsnd_runtime_channel_original(io);
262
1ff9593d 263 return (chan > 2) && (lane > 1);
eed76bb8
KM
264}
265
266int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io)
267{
268 return rsnd_runtime_channel_for_ssi(io) >= 6;
269}
270
d7bdbc5d 271/*
3023b384 272 * ADINR function
d7bdbc5d 273 */
3023b384 274u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
d7bdbc5d
KM
275{
276 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
d7bdbc5d
KM
277 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
278 struct device *dev = rsnd_priv_to_dev(priv);
d7bdbc5d 279
41acc8ec 280 switch (snd_pcm_format_width(runtime->format)) {
ba5d553b
DT
281 case 8:
282 return 16 << 16;
d7bdbc5d 283 case 16:
5e7b9edd 284 return 8 << 16;
41acc8ec 285 case 24:
5e7b9edd 286 return 0 << 16;
d7bdbc5d
KM
287 }
288
5e7b9edd
KM
289 dev_warn(dev, "not supported sample bits\n");
290
291 return 0;
d7bdbc5d
KM
292}
293
4689032b
KM
294/*
295 * DALIGN function
296 */
297u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
298{
3ce2959d 299 struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
a504b1ee 300 struct rsnd_mod *target;
4689032b 301 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
4689032b 302
8cce431a 303 /*
a914e446
KM
304 * *Hardware* L/R and *Software* L/R are inverted for 16bit data.
305 * 31..16 15...0
306 * HW: [L ch] [R ch]
307 * SW: [R ch] [L ch]
8cce431a
KM
308 * We need to care about inversion timing to control
309 * Playback/Capture correctly.
310 * The point is [DVC] needs *Hardware* L/R, [MEM] needs *Software* L/R
311 *
312 * sL/R : software L/R
313 * hL/R : hardware L/R
314 * (*) : conversion timing
315 *
316 * Playback
317 * sL/R (*) hL/R hL/R hL/R hL/R hL/R
318 * [MEM] -> [SRC] -> [DVC] -> [CMD] -> [SSIU] -> [SSI] -> codec
319 *
320 * Capture
321 * hL/R hL/R hL/R hL/R hL/R (*) sL/R
322 * codec -> [SSI] -> [SSIU] -> [SRC] -> [DVC] -> [CMD] -> [MEM]
323 */
a504b1ee
KM
324 if (rsnd_io_is_play(io)) {
325 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
326
3ce2959d 327 target = src ? src : ssiu;
a504b1ee
KM
328 } else {
329 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
330
3ce2959d 331 target = cmd ? cmd : ssiu;
a504b1ee
KM
332 }
333
ba5d553b 334 /* Non target mod or non 16bit needs normal DALIGN */
41acc8ec 335 if ((snd_pcm_format_width(runtime->format) != 16) ||
a914e446 336 (mod != target))
4689032b 337 return 0x76543210;
a914e446
KM
338 /* Target mod needs inverted DALIGN when 16bit */
339 else
340 return 0x67452301;
4689032b
KM
341}
342
90431eb4
KM
343u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
344{
345 enum rsnd_mod_type playback_mods[] = {
346 RSND_MOD_SRC,
347 RSND_MOD_CMD,
348 RSND_MOD_SSIU,
349 };
350 enum rsnd_mod_type capture_mods[] = {
351 RSND_MOD_CMD,
352 RSND_MOD_SRC,
353 RSND_MOD_SSIU,
354 };
355 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
356 struct rsnd_mod *tmod = NULL;
357 enum rsnd_mod_type *mods =
358 rsnd_io_is_play(io) ?
359 playback_mods : capture_mods;
360 int i;
361
362 /*
363 * This is needed for 24bit data
364 * We need to shift 8bit
365 *
366 * Linux 24bit data is located as 0x00******
367 * HW 24bit data is located as 0x******00
368 *
369 */
ba5d553b 370 if (snd_pcm_format_width(runtime->format) != 24)
90431eb4 371 return 0;
90431eb4
KM
372
373 for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
374 tmod = rsnd_io_to_mod(io, mods[i]);
375 if (tmod)
376 break;
377 }
378
379 if (tmod != mod)
380 return 0;
381
382 if (rsnd_io_is_play(io))
383 return (0 << 20) | /* shift to Left */
384 (8 << 16); /* 8bit */
385 else
386 return (1 << 20) | /* shift to Right */
387 (8 << 16); /* 8bit */
388}
389
1536a968
KM
390/*
391 * rsnd_dai functions
392 */
b3ca3fbe
KM
393struct rsnd_mod *rsnd_mod_next(int *iterator,
394 struct rsnd_dai_stream *io,
395 enum rsnd_mod_type *array,
396 int array_size)
397{
398 struct rsnd_mod *mod;
399 enum rsnd_mod_type type;
400 int max = array ? array_size : RSND_MOD_MAX;
401
402 for (; *iterator < max; (*iterator)++) {
403 type = (array) ? array[*iterator] : *iterator;
138f8786 404 mod = rsnd_io_to_mod(io, type);
b12f1e3a
KM
405 if (mod)
406 return mod;
b3ca3fbe
KM
407 }
408
409 return NULL;
410}
411
38587f4c
KM
412static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
413 {
414 /* CAPTURE */
415 RSND_MOD_AUDMAPP,
416 RSND_MOD_AUDMA,
417 RSND_MOD_DVC,
418 RSND_MOD_MIX,
419 RSND_MOD_CTU,
420 RSND_MOD_CMD,
421 RSND_MOD_SRC,
422 RSND_MOD_SSIU,
423 RSND_MOD_SSIM3,
424 RSND_MOD_SSIM2,
425 RSND_MOD_SSIM1,
426 RSND_MOD_SSIP,
427 RSND_MOD_SSI,
428 }, {
429 /* PLAYBACK */
430 RSND_MOD_AUDMAPP,
431 RSND_MOD_AUDMA,
432 RSND_MOD_SSIM3,
433 RSND_MOD_SSIM2,
434 RSND_MOD_SSIM1,
435 RSND_MOD_SSIP,
436 RSND_MOD_SSI,
437 RSND_MOD_SSIU,
438 RSND_MOD_DVC,
439 RSND_MOD_MIX,
440 RSND_MOD_CTU,
441 RSND_MOD_CMD,
442 RSND_MOD_SRC,
443 },
444};
445
5f222a29
KM
446static int rsnd_status_update(u32 *status,
447 int shift, int add, int timing)
448{
449 u32 mask = 0xF << shift;
450 u8 val = (*status >> shift) & 0xF;
451 u8 next_val = (val + add) & 0xF;
452 int func_call = (val == timing);
453
454 if (next_val == 0xF) /* underflow case */
455 func_call = 0;
456 else
457 *status = (*status & ~mask) + (next_val << shift);
458
459 return func_call;
460}
461
462#define rsnd_dai_call(fn, io, param...) \
463({ \
f30b4ca4 464 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io)); \
5f222a29
KM
465 struct rsnd_mod *mod; \
466 int is_play = rsnd_io_is_play(io); \
467 int ret = 0, i; \
468 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
469 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
470 int tmp = 0; \
7e7fe06d 471 u32 *status = mod->ops->get_status(mod, io, types[i]); \
5f222a29
KM
472 int func_call = rsnd_status_update(status, \
473 __rsnd_mod_shift_##fn, \
474 __rsnd_mod_add_##fn, \
475 __rsnd_mod_call_##fn); \
1f6e920f 476 rsnd_dbg_dai_call(dev, "%s[%d]\t0x%08x %s\n", \
5f222a29
KM
477 rsnd_mod_name(mod), rsnd_mod_id(mod), *status, \
478 (func_call && (mod)->ops->fn) ? #fn : ""); \
479 if (func_call && (mod)->ops->fn) \
480 tmp = (mod)->ops->fn(mod, io, param); \
6c92d5a2 481 if (tmp && (tmp != -EPROBE_DEFER)) \
5f222a29
KM
482 dev_err(dev, "%s[%d] : %s error %d\n", \
483 rsnd_mod_name(mod), rsnd_mod_id(mod), \
484 #fn, tmp); \
485 ret |= tmp; \
486 } \
487 ret; \
cdaa3cdf
KM
488})
489
27924f32
KM
490int rsnd_dai_connect(struct rsnd_mod *mod,
491 struct rsnd_dai_stream *io,
492 enum rsnd_mod_type type)
cdaa3cdf 493{
48725e9c
KM
494 struct rsnd_priv *priv;
495 struct device *dev;
84e95355 496
6020779b 497 if (!mod)
cdaa3cdf 498 return -EIO;
cdaa3cdf 499
bfa3119c
KM
500 if (io->mod[type] == mod)
501 return 0;
502
52dc6852
KM
503 if (io->mod[type])
504 return -EINVAL;
505
48725e9c
KM
506 priv = rsnd_mod_to_priv(mod);
507 dev = rsnd_priv_to_dev(priv);
508
27924f32 509 io->mod[type] = mod;
cdaa3cdf 510
84e95355
KM
511 dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
512 rsnd_mod_name(mod), rsnd_mod_id(mod),
513 rsnd_io_is_play(io) ? "Playback" : "Capture");
514
cdaa3cdf
KM
515 return 0;
516}
517
d3a76823 518static void rsnd_dai_disconnect(struct rsnd_mod *mod,
27924f32
KM
519 struct rsnd_dai_stream *io,
520 enum rsnd_mod_type type)
d3a76823 521{
27924f32 522 io->mod[type] = NULL;
d3a76823
KM
523}
524
1ff9593d
KM
525int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
526 int max_channels)
527{
528 if (max_channels > 0)
529 rdai->max_channels = max_channels;
530
531 return rdai->max_channels;
532}
533
534int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
535 int ssi_lane)
536{
537 if (ssi_lane > 0)
538 rdai->ssi_lane = ssi_lane;
539
540 return rdai->ssi_lane;
541}
542
fb2815f4
DT
543int rsnd_rdai_width_ctrl(struct rsnd_dai *rdai, int width)
544{
545 if (width > 0)
546 rdai->chan_width = width;
547
548 return rdai->chan_width;
549}
550
710d0889 551struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
1536a968 552{
ecba9e72 553 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
2192f81c
KM
554 return NULL;
555
1536a968
KM
556 return priv->rdai + id;
557}
558
a0d847c3
KM
559static struct snd_soc_dai_driver
560*rsnd_daidrv_get(struct rsnd_priv *priv, int id)
561{
562 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
563 return NULL;
564
565 return priv->daidrv + id;
566}
567
eb2535f5 568#define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
1536a968
KM
569static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
570{
eb2535f5 571 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
1536a968 572
710d0889 573 return rsnd_rdai_get(priv, dai->id);
1536a968
KM
574}
575
1536a968
KM
576/*
577 * rsnd_soc_dai functions
578 */
75defee0
KM
579void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
580{
581 struct snd_pcm_substream *substream = io->substream;
582
583 /*
584 * this function should be called...
585 *
586 * - if rsnd_dai_pointer_update() returns true
587 * - without spin lock
588 */
589
590 snd_pcm_period_elapsed(substream);
1536a968
KM
591}
592
5626ad08 593static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
1536a968
KM
594 struct snd_pcm_substream *substream)
595{
1536a968 596 io->substream = substream;
5626ad08 597}
1536a968 598
5626ad08
KM
599static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
600{
601 io->substream = NULL;
1536a968
KM
602}
603
604static
605struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
606{
607 struct snd_soc_pcm_runtime *rtd = substream->private_data;
608
609 return rtd->cpu_dai;
610}
611
612static
613struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
614 struct snd_pcm_substream *substream)
615{
616 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
617 return &rdai->playback;
618 else
619 return &rdai->capture;
620}
621
622static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
623 struct snd_soc_dai *dai)
624{
eb2535f5 625 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
1536a968
KM
626 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
627 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1536a968
KM
628 int ret;
629 unsigned long flags;
630
02299d98 631 spin_lock_irqsave(&priv->lock, flags);
1536a968
KM
632
633 switch (cmd) {
634 case SNDRV_PCM_TRIGGER_START:
4b9c75ea 635 case SNDRV_PCM_TRIGGER_RESUME:
690602fc 636 ret = rsnd_dai_call(init, io, priv);
cdaa3cdf
KM
637 if (ret < 0)
638 goto dai_trigger_end;
639
690602fc 640 ret = rsnd_dai_call(start, io, priv);
cdaa3cdf
KM
641 if (ret < 0)
642 goto dai_trigger_end;
b5b442ab
KM
643
644 ret = rsnd_dai_call(irq, io, priv, 1);
645 if (ret < 0)
646 goto dai_trigger_end;
647
1536a968
KM
648 break;
649 case SNDRV_PCM_TRIGGER_STOP:
4b9c75ea 650 case SNDRV_PCM_TRIGGER_SUSPEND:
b5b442ab
KM
651 ret = rsnd_dai_call(irq, io, priv, 0);
652
653 ret |= rsnd_dai_call(stop, io, priv);
cdaa3cdf 654
89e3e2c3 655 ret |= rsnd_dai_call(quit, io, priv);
cdaa3cdf 656
1536a968
KM
657 break;
658 default:
659 ret = -EINVAL;
660 }
661
662dai_trigger_end:
02299d98 663 spin_unlock_irqrestore(&priv->lock, flags);
1536a968
KM
664
665 return ret;
666}
667
668static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
669{
670 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
671
672 /* set master/slave audio interface */
673 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
674 case SND_SOC_DAIFMT_CBM_CFM:
e1508289 675 rdai->clk_master = 0;
1536a968
KM
676 break;
677 case SND_SOC_DAIFMT_CBS_CFS:
e1508289 678 rdai->clk_master = 1; /* codec is slave, cpu is master */
1536a968
KM
679 break;
680 default:
681 return -EINVAL;
682 }
683
1536a968
KM
684 /* set format */
685 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
686 case SND_SOC_DAIFMT_I2S:
687 rdai->sys_delay = 0;
688 rdai->data_alignment = 0;
1a7889ca 689 rdai->frm_clk_inv = 0;
1536a968
KM
690 break;
691 case SND_SOC_DAIFMT_LEFT_J:
3791b3ee 692 case SND_SOC_DAIFMT_DSP_B:
1536a968
KM
693 rdai->sys_delay = 1;
694 rdai->data_alignment = 0;
1a7889ca 695 rdai->frm_clk_inv = 1;
1536a968
KM
696 break;
697 case SND_SOC_DAIFMT_RIGHT_J:
698 rdai->sys_delay = 1;
699 rdai->data_alignment = 1;
1a7889ca
KM
700 rdai->frm_clk_inv = 1;
701 break;
3791b3ee
DT
702 case SND_SOC_DAIFMT_DSP_A:
703 rdai->sys_delay = 0;
704 rdai->data_alignment = 0;
705 rdai->frm_clk_inv = 1;
706 break;
1a7889ca
KM
707 }
708
709 /* set clock inversion */
710 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
711 case SND_SOC_DAIFMT_NB_IF:
1a7889ca
KM
712 rdai->frm_clk_inv = !rdai->frm_clk_inv;
713 break;
714 case SND_SOC_DAIFMT_IB_NF:
715 rdai->bit_clk_inv = !rdai->bit_clk_inv;
1a7889ca
KM
716 break;
717 case SND_SOC_DAIFMT_IB_IF:
718 rdai->bit_clk_inv = !rdai->bit_clk_inv;
719 rdai->frm_clk_inv = !rdai->frm_clk_inv;
720 break;
721 case SND_SOC_DAIFMT_NB_NF:
722 default:
1536a968
KM
723 break;
724 }
725
726 return 0;
727}
728
186fadc1
KM
729static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
730 u32 tx_mask, u32 rx_mask,
731 int slots, int slot_width)
732{
733 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
734 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
735 struct device *dev = rsnd_priv_to_dev(priv);
736
fb2815f4
DT
737 switch (slot_width) {
738 case 16:
739 case 24:
740 case 32:
741 break;
742 default:
82ab7e9a
KM
743 /* use default */
744 slot_width = 32;
fb2815f4
DT
745 }
746
186fadc1 747 switch (slots) {
8cc03722 748 case 2:
186fadc1 749 case 6:
8cc03722 750 case 8:
186fadc1 751 /* TDM Extend Mode */
1ff9593d
KM
752 rsnd_rdai_channels_set(rdai, slots);
753 rsnd_rdai_ssi_lane_set(rdai, 1);
fb2815f4 754 rsnd_rdai_width_set(rdai, slot_width);
186fadc1
KM
755 break;
756 default:
757 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
758 return -EINVAL;
759 }
760
761 return 0;
762}
763
8cc03722 764static unsigned int rsnd_soc_hw_channels_list[] = {
29d03ff5 765 2, 6, 8,
8cc03722
KM
766};
767
768static unsigned int rsnd_soc_hw_rate_list[] = {
769 8000,
770 11025,
771 16000,
772 22050,
773 32000,
774 44100,
775 48000,
776 64000,
777 88200,
778 96000,
779 176400,
780 192000,
781};
782
fb2815f4 783static int rsnd_soc_hw_rule(struct rsnd_dai *rdai,
8cc03722
KM
784 unsigned int *list, int list_num,
785 struct snd_interval *baseline, struct snd_interval *iv)
786{
787 struct snd_interval p;
947f4eb5 788 unsigned int rate;
8cc03722
KM
789 int i;
790
791 snd_interval_any(&p);
792 p.min = UINT_MAX;
793 p.max = 0;
794
795 for (i = 0; i < list_num; i++) {
796
797 if (!snd_interval_test(iv, list[i]))
798 continue;
799
fb2815f4 800 rate = rsnd_ssi_clk_query(rdai,
8cc03722
KM
801 baseline->min, list[i], NULL);
802 if (rate > 0) {
803 p.min = min(p.min, list[i]);
804 p.max = max(p.max, list[i]);
805 }
806
fb2815f4 807 rate = rsnd_ssi_clk_query(rdai,
8cc03722
KM
808 baseline->max, list[i], NULL);
809 if (rate > 0) {
810 p.min = min(p.min, list[i]);
811 p.max = max(p.max, list[i]);
812 }
813 }
814
815 return snd_interval_refine(iv, &p);
816}
817
b735662f
JW
818static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
819 struct snd_pcm_hw_rule *rule)
8cc03722
KM
820{
821 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
822 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
823 struct snd_interval ic;
b735662f
JW
824 struct rsnd_dai_stream *io = rule->private;
825 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
8cc03722
KM
826
827 /*
828 * possible sampling rate limitation is same as
829 * 2ch if it supports multi ssi
b2fb31bb 830 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
8cc03722
KM
831 */
832 ic = *ic_;
b2fb31bb
KM
833 ic.min =
834 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
8cc03722 835
fb2815f4 836 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_rate_list,
8cc03722
KM
837 ARRAY_SIZE(rsnd_soc_hw_rate_list),
838 &ic, ir);
839}
840
b735662f
JW
841static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
842 struct snd_pcm_hw_rule *rule)
8cc03722
KM
843{
844 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
845 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
846 struct snd_interval ic;
b735662f
JW
847 struct rsnd_dai_stream *io = rule->private;
848 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
8cc03722
KM
849
850 /*
851 * possible sampling rate limitation is same as
852 * 2ch if it supports multi ssi
b2fb31bb 853 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
8cc03722
KM
854 */
855 ic = *ic_;
b2fb31bb
KM
856 ic.min =
857 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
8cc03722 858
fb2815f4 859 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_channels_list,
8cc03722
KM
860 ARRAY_SIZE(rsnd_soc_hw_channels_list),
861 ir, &ic);
862}
863
5c2e035e 864static const struct snd_pcm_hardware rsnd_pcm_hardware = {
3c9736aa
KM
865 .info = SNDRV_PCM_INFO_INTERLEAVED |
866 SNDRV_PCM_INFO_MMAP |
867 SNDRV_PCM_INFO_MMAP_VALID,
868 .buffer_bytes_max = 64 * 1024,
869 .period_bytes_min = 32,
870 .period_bytes_max = 8192,
871 .periods_min = 1,
872 .periods_max = 32,
873 .fifo_size = 256,
874};
875
876static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
877 struct snd_soc_dai *dai)
8cc03722
KM
878{
879 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
3c9736aa 880 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
8cc03722 881 struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
3c9736aa 882 struct snd_pcm_runtime *runtime = substream->runtime;
8cc03722
KM
883 unsigned int max_channels = rsnd_rdai_channels_get(rdai);
884 int i;
885
b2fb31bb
KM
886 rsnd_dai_stream_init(io, substream);
887
8cc03722
KM
888 /*
889 * Channel Limitation
890 * It depends on Platform design
891 */
892 constraint->list = rsnd_soc_hw_channels_list;
893 constraint->count = 0;
894 constraint->mask = 0;
895
896 for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
897 if (rsnd_soc_hw_channels_list[i] > max_channels)
898 break;
899 constraint->count = i + 1;
900 }
901
3c9736aa
KM
902 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
903
8cc03722
KM
904 snd_pcm_hw_constraint_list(runtime, 0,
905 SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
906
3c9736aa
KM
907 snd_pcm_hw_constraint_integer(runtime,
908 SNDRV_PCM_HW_PARAM_PERIODS);
909
8cc03722
KM
910 /*
911 * Sampling Rate / Channel Limitation
912 * It depends on Clock Master Mode
913 */
3c9736aa 914 if (rsnd_rdai_is_clk_master(rdai)) {
b2fb31bb
KM
915 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
916
3c9736aa 917 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
b735662f
JW
918 rsnd_soc_hw_rule_rate,
919 is_play ? &rdai->playback : &rdai->capture,
3c9736aa
KM
920 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
921 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
b735662f
JW
922 rsnd_soc_hw_rule_channels,
923 is_play ? &rdai->playback : &rdai->capture,
3c9736aa
KM
924 SNDRV_PCM_HW_PARAM_RATE, -1);
925 }
8cc03722 926
6ab6a247 927 return 0;
10a9cca1
KM
928}
929
930static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
931 struct snd_soc_dai *dai)
932{
933 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
f30b4ca4 934 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
10a9cca1
KM
935 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
936
937 /*
938 * call rsnd_dai_call without spinlock
939 */
6ab6a247 940 rsnd_dai_call(cleanup, io, priv);
b2fb31bb
KM
941
942 rsnd_dai_stream_quit(io);
10a9cca1
KM
943}
944
4d230d12
JW
945static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream,
946 struct snd_soc_dai *dai)
947{
948 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
949 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
950 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
951
952 return rsnd_dai_call(prepare, io, priv);
953}
954
1536a968 955static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
10a9cca1
KM
956 .startup = rsnd_soc_dai_startup,
957 .shutdown = rsnd_soc_dai_shutdown,
1536a968
KM
958 .trigger = rsnd_soc_dai_trigger,
959 .set_fmt = rsnd_soc_dai_set_fmt,
186fadc1 960 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
4d230d12 961 .prepare = rsnd_soc_dai_prepare,
1536a968
KM
962};
963
89b66174
KM
964void rsnd_parse_connect_common(struct rsnd_dai *rdai,
965 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
966 struct device_node *node,
967 struct device_node *playback,
968 struct device_node *capture)
969{
970 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
971 struct device_node *np;
972 struct rsnd_mod *mod;
973 int i;
974
975 if (!node)
976 return;
977
978 i = 0;
979 for_each_child_of_node(node, np) {
980 mod = mod_get(priv, i);
981 if (np == playback)
982 rsnd_dai_connect(mod, &rdai->playback, mod->type);
983 if (np == capture)
984 rsnd_dai_connect(mod, &rdai->capture, mod->type);
985 i++;
986 }
987
988 of_node_put(node);
989}
990
11d0f8ed
KM
991static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
992 int *is_graph)
993{
994 struct device *dev = rsnd_priv_to_dev(priv);
995 struct device_node *np = dev->of_node;
996 struct device_node *dai_node;
997 struct device_node *ret;
998
999 *is_graph = 0;
1000
1001 /*
1002 * parse both previous dai (= rcar_sound,dai), and
1003 * graph dai (= ports/port)
1004 */
1005 dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
1006 if (dai_node) {
1007 ret = dai_node;
1008 goto of_node_compatible;
1009 }
1010
1011 ret = np;
1012
1013 dai_node = of_graph_get_next_endpoint(np, NULL);
1014 if (dai_node)
1015 goto of_node_graph;
1016
1017 return NULL;
1018
1019of_node_graph:
1020 *is_graph = 1;
1021of_node_compatible:
1022 of_node_put(dai_node);
1023
1024 return ret;
1025}
1026
4d4b334b
KM
1027static void __rsnd_dai_probe(struct rsnd_priv *priv,
1028 struct device_node *dai_np,
9f761183 1029 int dai_i)
90e8e50f 1030{
90e8e50f 1031 struct device_node *playback, *capture;
94e2710c
KM
1032 struct rsnd_dai_stream *io_playback;
1033 struct rsnd_dai_stream *io_capture;
4d4b334b 1034 struct snd_soc_dai_driver *drv;
94e2710c 1035 struct rsnd_dai *rdai;
2ea6b074 1036 struct device *dev = rsnd_priv_to_dev(priv);
4d4b334b
KM
1037 int io_i;
1038
1039 rdai = rsnd_rdai_get(priv, dai_i);
a0d847c3 1040 drv = rsnd_daidrv_get(priv, dai_i);
4d4b334b
KM
1041 io_playback = &rdai->playback;
1042 io_capture = &rdai->capture;
1043
1044 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1045
1046 rdai->priv = priv;
1047 drv->name = rdai->name;
1048 drv->ops = &rsnd_soc_dai_ops;
1049
1050 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
1051 "DAI%d Playback", dai_i);
1052 drv->playback.rates = RSND_RATES;
1053 drv->playback.formats = RSND_FMTS;
1054 drv->playback.channels_min = 2;
29d03ff5 1055 drv->playback.channels_max = 8;
4d4b334b
KM
1056 drv->playback.stream_name = rdai->playback.name;
1057
1058 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
1059 "DAI%d Capture", dai_i);
1060 drv->capture.rates = RSND_RATES;
1061 drv->capture.formats = RSND_FMTS;
1062 drv->capture.channels_min = 2;
29d03ff5 1063 drv->capture.channels_max = 8;
4d4b334b
KM
1064 drv->capture.stream_name = rdai->capture.name;
1065
1066 rdai->playback.rdai = rdai;
1067 rdai->capture.rdai = rdai;
1ff9593d
KM
1068 rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1069 rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
fb2815f4 1070 rsnd_rdai_width_set(rdai, 32); /* default 32bit width */
4d4b334b
KM
1071
1072 for (io_i = 0;; io_i++) {
1073 playback = of_parse_phandle(dai_np, "playback", io_i);
1074 capture = of_parse_phandle(dai_np, "capture", io_i);
1075
1076 if (!playback && !capture)
1077 break;
1078
1079 rsnd_parse_connect_ssi(rdai, playback, capture);
1080 rsnd_parse_connect_src(rdai, playback, capture);
1081 rsnd_parse_connect_ctu(rdai, playback, capture);
1082 rsnd_parse_connect_mix(rdai, playback, capture);
1083 rsnd_parse_connect_dvc(rdai, playback, capture);
1084
1085 of_node_put(playback);
1086 of_node_put(capture);
1087 }
1088
7cc90a5c
KM
1089 if (rsnd_ssi_is_pin_sharing(io_capture) ||
1090 rsnd_ssi_is_pin_sharing(io_playback)) {
1091 /* should have symmetric_rates if pin sharing */
1092 drv->symmetric_rates = 1;
1093 }
1094
4d4b334b
KM
1095 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1096 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
1097 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
1098}
1099
1100static int rsnd_dai_probe(struct rsnd_priv *priv)
1101{
1102 struct device_node *dai_node;
1103 struct device_node *dai_np;
1104 struct snd_soc_dai_driver *rdrv;
1105 struct device *dev = rsnd_priv_to_dev(priv);
1106 struct rsnd_dai *rdai;
1107 int nr;
11d0f8ed 1108 int is_graph;
4d4b334b 1109 int dai_i;
90e8e50f 1110
11d0f8ed 1111 dai_node = rsnd_dai_of_node(priv, &is_graph);
4d4b334b
KM
1112 if (is_graph)
1113 nr = of_graph_get_endpoint_count(dai_node);
1114 else
1115 nr = of_get_child_count(dai_node);
1116
1117 if (!nr)
1118 return -EINVAL;
90e8e50f 1119
a86854d0
KC
1120 rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1121 rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
4d4b334b
KM
1122 if (!rdrv || !rdai)
1123 return -ENOMEM;
90e8e50f 1124
94e2710c 1125 priv->rdai_nr = nr;
2ff2ecca 1126 priv->daidrv = rdrv;
94e2710c 1127 priv->rdai = rdai;
90e8e50f
KM
1128
1129 /*
1130 * parse all dai
1131 */
1132 dai_i = 0;
4d4b334b 1133 if (is_graph) {
7fa72cca 1134 for_each_endpoint_of_node(dai_node, dai_np) {
9f761183 1135 __rsnd_dai_probe(priv, dai_np, dai_i);
7fa72cca
KM
1136 rsnd_ssi_parse_hdmi_connection(priv, dai_np, dai_i);
1137 dai_i++;
1138 }
4d4b334b
KM
1139 } else {
1140 for_each_child_of_node(dai_node, dai_np)
9f761183 1141 __rsnd_dai_probe(priv, dai_np, dai_i++);
1536a968
KM
1142 }
1143
4d4b334b 1144 return 0;
1536a968
KM
1145}
1146
1536a968
KM
1147/*
1148 * pcm ops
1149 */
1536a968
KM
1150static int rsnd_hw_params(struct snd_pcm_substream *substream,
1151 struct snd_pcm_hw_params *hw_params)
1152{
3b7843ff
KM
1153 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1154 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1155 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1156 int ret;
1157
1158 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1159 if (ret)
1160 return ret;
1161
1536a968
KM
1162 return snd_pcm_lib_malloc_pages(substream,
1163 params_buffer_bytes(hw_params));
1164}
1165
1166static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
1167{
1536a968
KM
1168 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1169 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1170 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
07b7acb5
KM
1171 snd_pcm_uframes_t pointer = 0;
1172
1173 rsnd_dai_call(pointer, io, &pointer);
1536a968 1174
07b7acb5 1175 return pointer;
1536a968
KM
1176}
1177
b23bd34c 1178static const struct snd_pcm_ops rsnd_pcm_ops = {
1536a968
KM
1179 .ioctl = snd_pcm_lib_ioctl,
1180 .hw_params = rsnd_hw_params,
1181 .hw_free = snd_pcm_lib_free_pages,
1182 .pointer = rsnd_pointer,
1183};
1184
170a2497
KM
1185/*
1186 * snd_kcontrol
1187 */
170a2497
KM
1188static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1189 struct snd_ctl_elem_info *uinfo)
1190{
16d44989 1191 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
170a2497
KM
1192
1193 if (cfg->texts) {
1194 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1195 uinfo->count = cfg->size;
1196 uinfo->value.enumerated.items = cfg->max;
1197 if (uinfo->value.enumerated.item >= cfg->max)
1198 uinfo->value.enumerated.item = cfg->max - 1;
1199 strlcpy(uinfo->value.enumerated.name,
1200 cfg->texts[uinfo->value.enumerated.item],
1201 sizeof(uinfo->value.enumerated.name));
1202 } else {
1203 uinfo->count = cfg->size;
1204 uinfo->value.integer.min = 0;
1205 uinfo->value.integer.max = cfg->max;
1206 uinfo->type = (cfg->max == 1) ?
1207 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1208 SNDRV_CTL_ELEM_TYPE_INTEGER;
1209 }
1210
1211 return 0;
1212}
1213
1214static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1215 struct snd_ctl_elem_value *uc)
1216{
16d44989 1217 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
170a2497
KM
1218 int i;
1219
1220 for (i = 0; i < cfg->size; i++)
1221 if (cfg->texts)
1222 uc->value.enumerated.item[i] = cfg->val[i];
1223 else
1224 uc->value.integer.value[i] = cfg->val[i];
1225
1226 return 0;
1227}
1228
1229static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1230 struct snd_ctl_elem_value *uc)
1231{
16d44989 1232 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
170a2497
KM
1233 int i, change = 0;
1234
f0b04d8b
KM
1235 if (!cfg->accept(cfg->io))
1236 return 0;
1237
170a2497
KM
1238 for (i = 0; i < cfg->size; i++) {
1239 if (cfg->texts) {
1240 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1241 cfg->val[i] = uc->value.enumerated.item[i];
1242 } else {
1243 change |= (uc->value.integer.value[i] != cfg->val[i]);
1244 cfg->val[i] = uc->value.integer.value[i];
1245 }
1246 }
1247
d7289565 1248 if (change && cfg->update)
16d44989 1249 cfg->update(cfg->io, cfg->mod);
170a2497
KM
1250
1251 return change;
1252}
1253
f0b04d8b
KM
1254int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1255{
1256 return 1;
1257}
1258
1259int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1260{
1261 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
b5c08868
JW
1262 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1263 struct device *dev = rsnd_priv_to_dev(priv);
f0b04d8b 1264
b5c08868
JW
1265 if (!runtime) {
1266 dev_warn(dev, "Can't update kctrl when idle\n");
1267 return 0;
1268 }
1269
1270 return 1;
f0b04d8b
KM
1271}
1272
32973dcf
KM
1273struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1274{
1275 cfg->cfg.val = cfg->val;
1276
1277 return &cfg->cfg;
1278}
1279
1280struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1281{
1282 cfg->cfg.val = &cfg->val;
1283
1284 return &cfg->cfg;
1285}
1286
f3c26ac6
KM
1287const char * const volume_ramp_rate[] = {
1288 "128 dB/1 step", /* 00000 */
1289 "64 dB/1 step", /* 00001 */
1290 "32 dB/1 step", /* 00010 */
1291 "16 dB/1 step", /* 00011 */
1292 "8 dB/1 step", /* 00100 */
1293 "4 dB/1 step", /* 00101 */
1294 "2 dB/1 step", /* 00110 */
1295 "1 dB/1 step", /* 00111 */
1296 "0.5 dB/1 step", /* 01000 */
1297 "0.25 dB/1 step", /* 01001 */
3e3c9ee1 1298 "0.125 dB/1 step", /* 01010 = VOLUME_RAMP_MAX_MIX */
f3c26ac6
KM
1299 "0.125 dB/2 steps", /* 01011 */
1300 "0.125 dB/4 steps", /* 01100 */
1301 "0.125 dB/8 steps", /* 01101 */
1302 "0.125 dB/16 steps", /* 01110 */
1303 "0.125 dB/32 steps", /* 01111 */
1304 "0.125 dB/64 steps", /* 10000 */
1305 "0.125 dB/128 steps", /* 10001 */
1306 "0.125 dB/256 steps", /* 10010 */
1307 "0.125 dB/512 steps", /* 10011 */
1308 "0.125 dB/1024 steps", /* 10100 */
1309 "0.125 dB/2048 steps", /* 10101 */
1310 "0.125 dB/4096 steps", /* 10110 */
1311 "0.125 dB/8192 steps", /* 10111 = VOLUME_RAMP_MAX_DVC */
1312};
1313
32973dcf
KM
1314int rsnd_kctrl_new(struct rsnd_mod *mod,
1315 struct rsnd_dai_stream *io,
1316 struct snd_soc_pcm_runtime *rtd,
1317 const unsigned char *name,
f0b04d8b 1318 int (*accept)(struct rsnd_dai_stream *io),
32973dcf
KM
1319 void (*update)(struct rsnd_dai_stream *io,
1320 struct rsnd_mod *mod),
1321 struct rsnd_kctrl_cfg *cfg,
1322 const char * const *texts,
1323 int size,
1324 u32 max)
170a2497
KM
1325{
1326 struct snd_card *card = rtd->card->snd_card;
1327 struct snd_kcontrol *kctrl;
1328 struct snd_kcontrol_new knew = {
1329 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1330 .name = name,
1331 .info = rsnd_kctrl_info,
1a497983 1332 .index = rtd->num,
170a2497
KM
1333 .get = rsnd_kctrl_get,
1334 .put = rsnd_kctrl_put,
170a2497
KM
1335 };
1336 int ret;
1337
32973dcf
KM
1338 if (size > RSND_MAX_CHANNELS)
1339 return -EINVAL;
1340
16d44989 1341 kctrl = snd_ctl_new1(&knew, cfg);
170a2497
KM
1342 if (!kctrl)
1343 return -ENOMEM;
1344
1345 ret = snd_ctl_add(card, kctrl);
0ea617a2 1346 if (ret < 0)
170a2497
KM
1347 return ret;
1348
32973dcf
KM
1349 cfg->texts = texts;
1350 cfg->max = max;
1351 cfg->size = size;
f0b04d8b 1352 cfg->accept = accept;
32973dcf
KM
1353 cfg->update = update;
1354 cfg->card = card;
1355 cfg->kctrl = kctrl;
1356 cfg->io = io;
16d44989 1357 cfg->mod = mod;
170a2497
KM
1358
1359 return 0;
1360}
1361
1536a968 1362/*
019ea01b 1363 * snd_soc_component
1536a968
KM
1364 */
1365
1366#define PREALLOC_BUFFER (32 * 1024)
1367#define PREALLOC_BUFFER_MAX (32 * 1024)
1368
5423d772
KM
1369static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1370 struct rsnd_dai_stream *io,
1371 int stream)
1372{
1373 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1374 struct device *dev = rsnd_priv_to_dev(priv);
1375 struct snd_pcm_substream *substream;
1376 int err;
1377
1378 /*
1379 * use Audio-DMAC dev if we can use IPMMU
1380 * see
1381 * rsnd_dmaen_attach()
1382 */
1383 if (io->dmac_dev)
1384 dev = io->dmac_dev;
1385
1386 for (substream = rtd->pcm->streams[stream].substream;
1387 substream;
1388 substream = substream->next) {
1389 err = snd_pcm_lib_preallocate_pages(substream,
1390 SNDRV_DMA_TYPE_DEV,
1391 dev,
1392 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1393 if (err < 0)
1394 return err;
1395 }
1396
1397 return 0;
1398}
1399
1536a968
KM
1400static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1401{
7c63f3c0
KM
1402 struct snd_soc_dai *dai = rtd->cpu_dai;
1403 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
ae11a9be 1404 int ret;
bff58ea4 1405
ae11a9be
KM
1406 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1407 if (ret)
1408 return ret;
bff58ea4 1409
ae11a9be 1410 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
7c63f3c0
KM
1411 if (ret)
1412 return ret;
bff58ea4 1413
5423d772
KM
1414 ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1415 SNDRV_PCM_STREAM_PLAYBACK);
1416 if (ret)
1417 return ret;
1418
1419 ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1420 SNDRV_PCM_STREAM_CAPTURE);
1421 if (ret)
1422 return ret;
1423
1424 return 0;
1536a968
KM
1425}
1426
019ea01b 1427static const struct snd_soc_component_driver rsnd_soc_component = {
1536a968
KM
1428 .ops = &rsnd_pcm_ops,
1429 .pcm_new = rsnd_pcm_new,
1536a968
KM
1430 .name = "rsnd",
1431};
1432
d3a76823 1433static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
f708d944 1434 struct rsnd_dai_stream *io)
d3a76823 1435{
d3a76823
KM
1436 int ret;
1437
690602fc 1438 ret = rsnd_dai_call(probe, io, priv);
d3a76823 1439 if (ret == -EAGAIN) {
48d58281 1440 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
9b87bfb2 1441 struct rsnd_mod *mod;
48d58281
KM
1442 int i;
1443
d3a76823
KM
1444 /*
1445 * Fallback to PIO mode
1446 */
1447
1448 /*
1449 * call "remove" for SSI/SRC/DVC
1450 * SSI will be switch to PIO mode if it was DMA mode
1451 * see
1452 * rsnd_dma_init()
97463e19 1453 * rsnd_ssi_fallback()
d3a76823 1454 */
690602fc 1455 rsnd_dai_call(remove, io, priv);
d3a76823
KM
1456
1457 /*
48d58281
KM
1458 * remove all mod from io
1459 * and, re connect ssi
d3a76823 1460 */
9b87bfb2
KM
1461 for_each_rsnd_mod(i, mod, io)
1462 rsnd_dai_disconnect(mod, io, i);
48d58281 1463 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
d3a76823 1464
97463e19
KM
1465 /*
1466 * fallback
1467 */
690602fc 1468 rsnd_dai_call(fallback, io, priv);
97463e19 1469
d3a76823
KM
1470 /*
1471 * retry to "probe".
1472 * DAI has SSI which is PIO mode only now.
1473 */
690602fc 1474 ret = rsnd_dai_call(probe, io, priv);
d3a76823
KM
1475 }
1476
1477 return ret;
1478}
1479
1536a968
KM
1480/*
1481 * rsnd probe
1482 */
1483static int rsnd_probe(struct platform_device *pdev)
1484{
1536a968
KM
1485 struct rsnd_priv *priv;
1486 struct device *dev = &pdev->dev;
7681f6ac 1487 struct rsnd_dai *rdai;
2ea6b074 1488 int (*probe_func[])(struct rsnd_priv *priv) = {
d1ac970f 1489 rsnd_gen_probe,
288f392e 1490 rsnd_dma_probe,
d1ac970f 1491 rsnd_ssi_probe,
c7f69ab5 1492 rsnd_ssiu_probe,
ba9c949f 1493 rsnd_src_probe,
9269e3c3 1494 rsnd_ctu_probe,
70fb1052 1495 rsnd_mix_probe,
bff58ea4 1496 rsnd_dvc_probe,
1b2ca0ad 1497 rsnd_cmd_probe,
d1ac970f
KM
1498 rsnd_adg_probe,
1499 rsnd_dai_probe,
1500 };
1501 int ret, i;
1536a968 1502
1536a968
KM
1503 /*
1504 * init priv data
1505 */
1506 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0d7820d0 1507 if (!priv)
1536a968 1508 return -ENODEV;
1536a968 1509
9f464f8e 1510 priv->pdev = pdev;
6d8044b4 1511 priv->flags = (unsigned long)of_device_get_match_data(dev);
1536a968
KM
1512 spin_lock_init(&priv->lock);
1513
1514 /*
1515 * init each module
1516 */
d1ac970f 1517 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
2ea6b074 1518 ret = probe_func[i](priv);
d1ac970f
KM
1519 if (ret)
1520 return ret;
1521 }
07539c1d 1522
7681f6ac 1523 for_each_rsnd_dai(rdai, priv, i) {
f708d944 1524 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
7681f6ac 1525 if (ret)
d62a3dcd 1526 goto exit_snd_probe;
dfc9403b 1527
f708d944 1528 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
7681f6ac 1529 if (ret)
d62a3dcd 1530 goto exit_snd_probe;
7681f6ac 1531 }
4b4dab82 1532
0b1f6ec7
KM
1533 dev_set_drvdata(dev, priv);
1534
1536a968
KM
1535 /*
1536 * asoc register
1537 */
019ea01b 1538 ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
ecba9e72 1539 priv->daidrv, rsnd_rdai_nr(priv));
1536a968
KM
1540 if (ret < 0) {
1541 dev_err(dev, "cannot snd dai register\n");
019ea01b 1542 goto exit_snd_probe;
1536a968
KM
1543 }
1544
1536a968
KM
1545 pm_runtime_enable(dev);
1546
1547 dev_info(dev, "probed\n");
1548 return ret;
1549
d62a3dcd
KM
1550exit_snd_probe:
1551 for_each_rsnd_dai(rdai, priv, i) {
690602fc
KM
1552 rsnd_dai_call(remove, &rdai->playback, priv);
1553 rsnd_dai_call(remove, &rdai->capture, priv);
d62a3dcd 1554 }
1536a968 1555
6c92d5a2
KM
1556 /*
1557 * adg is very special mod which can't use rsnd_dai_call(remove),
1558 * and it registers ADG clock on probe.
1559 * It should be unregister if probe failed.
1560 * Mainly it is assuming -EPROBE_DEFER case
1561 */
1562 rsnd_adg_remove(priv);
1563
1536a968
KM
1564 return ret;
1565}
1566
1567static int rsnd_remove(struct platform_device *pdev)
1568{
1569 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
7681f6ac 1570 struct rsnd_dai *rdai;
2ea6b074 1571 void (*remove_func[])(struct rsnd_priv *priv) = {
2f78dd7f 1572 rsnd_ssi_remove,
c7f69ab5 1573 rsnd_ssiu_remove,
2f78dd7f 1574 rsnd_src_remove,
9269e3c3 1575 rsnd_ctu_remove,
70fb1052 1576 rsnd_mix_remove,
2f78dd7f 1577 rsnd_dvc_remove,
1b2ca0ad 1578 rsnd_cmd_remove,
68a55024 1579 rsnd_adg_remove,
2f78dd7f 1580 };
d62a3dcd 1581 int ret = 0, i;
1536a968 1582
180d9ef5
KM
1583 snd_soc_disconnect_sync(&pdev->dev);
1584
1536a968
KM
1585 pm_runtime_disable(&pdev->dev);
1586
7681f6ac 1587 for_each_rsnd_dai(rdai, priv, i) {
690602fc
KM
1588 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1589 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
7681f6ac 1590 }
1536a968 1591
2f78dd7f 1592 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
2ea6b074 1593 remove_func[i](priv);
2f78dd7f 1594
d62a3dcd 1595 return ret;
1536a968
KM
1596}
1597
6f542703 1598static int __maybe_unused rsnd_suspend(struct device *dev)
c2d31718
KM
1599{
1600 struct rsnd_priv *priv = dev_get_drvdata(dev);
1601
1602 rsnd_adg_clk_disable(priv);
1603
1604 return 0;
1605}
1606
6f542703 1607static int __maybe_unused rsnd_resume(struct device *dev)
c2d31718
KM
1608{
1609 struct rsnd_priv *priv = dev_get_drvdata(dev);
1610
1611 rsnd_adg_clk_enable(priv);
1612
1613 return 0;
1614}
1615
49ebf13b 1616static const struct dev_pm_ops rsnd_pm_ops = {
f8a9a29c 1617 SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
c2d31718
KM
1618};
1619
1536a968
KM
1620static struct platform_driver rsnd_driver = {
1621 .driver = {
1622 .name = "rcar_sound",
c2d31718 1623 .pm = &rsnd_pm_ops,
90e8e50f 1624 .of_match_table = rsnd_of_match,
1536a968
KM
1625 },
1626 .probe = rsnd_probe,
1627 .remove = rsnd_remove,
1628};
1629module_platform_driver(rsnd_driver);
1630
1e0edd4d 1631MODULE_LICENSE("GPL v2");
1536a968
KM
1632MODULE_DESCRIPTION("Renesas R-Car audio driver");
1633MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1634MODULE_ALIAS("platform:rcar-pcm-audio");