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