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