Merge tag 'drm-misc-next-2017-01-30' of git://anongit.freedesktop.org/git/drm-misc...
[linux-block.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
99#define RSND_RATES SNDRV_PCM_RATE_8000_96000
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 */
1d9d0c65 113#ifdef DEBUG
f1df1229
KM
114void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
115{
116 if (mod->type != type) {
117 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
118 struct device *dev = rsnd_priv_to_dev(priv);
119
120 dev_warn(dev, "%s[%d] is not your expected module\n",
121 rsnd_mod_name(mod), rsnd_mod_id(mod));
122 }
123}
1d9d0c65 124#endif
f1df1229 125
cdaa3cdf
KM
126char *rsnd_mod_name(struct rsnd_mod *mod)
127{
128 if (!mod || !mod->ops)
129 return "unknown";
130
131 return mod->ops->name;
132}
133
9b99e9a7
KM
134struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
135 struct rsnd_mod *mod)
d9288d0b 136{
72adc61f
KM
137 if (!mod || !mod->ops || !mod->ops->dma_req)
138 return NULL;
d9288d0b 139
9b99e9a7 140 return mod->ops->dma_req(io, mod);
d9288d0b
KM
141}
142
5ba17b42
KM
143u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io,
144 struct rsnd_mod *mod,
145 enum rsnd_mod_type type)
146{
147 return &mod->status;
148}
149
2099bc8e
KM
150int rsnd_mod_init(struct rsnd_priv *priv,
151 struct rsnd_mod *mod,
5ba17b42
KM
152 struct rsnd_mod_ops *ops,
153 struct clk *clk,
154 u32* (*get_status)(struct rsnd_dai_stream *io,
155 struct rsnd_mod *mod,
156 enum rsnd_mod_type type),
157 enum rsnd_mod_type type,
158 int id)
cdaa3cdf 159{
2f78dd7f
KM
160 int ret = clk_prepare(clk);
161
162 if (ret)
163 return ret;
164
cdaa3cdf
KM
165 mod->id = id;
166 mod->ops = ops;
a126021d 167 mod->type = type;
85642952 168 mod->clk = clk;
2099bc8e 169 mod->priv = priv;
5ba17b42 170 mod->get_status = get_status;
2f78dd7f
KM
171
172 return ret;
173}
174
175void rsnd_mod_quit(struct rsnd_mod *mod)
176{
177 if (mod->clk)
178 clk_unprepare(mod->clk);
ea96380b 179 mod->clk = NULL;
cdaa3cdf
KM
180}
181
f501b7a4
KM
182void rsnd_mod_interrupt(struct rsnd_mod *mod,
183 void (*callback)(struct rsnd_mod *mod,
184 struct rsnd_dai_stream *io))
185{
186 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
187 struct rsnd_dai_stream *io;
188 struct rsnd_dai *rdai;
2daf71ad 189 int i;
f501b7a4 190
2daf71ad
KM
191 for_each_rsnd_dai(rdai, priv, i) {
192 io = &rdai->playback;
193 if (mod == io->mod[mod->type])
194 callback(mod, io);
f501b7a4 195
2daf71ad
KM
196 io = &rdai->capture;
197 if (mod == io->mod[mod->type])
198 callback(mod, io);
f501b7a4
KM
199 }
200}
201
d5bbe7de 202int rsnd_io_is_working(struct rsnd_dai_stream *io)
02299d98 203{
02299d98
KM
204 /* see rsnd_dai_stream_init/quit() */
205 return !!io->substream;
206}
207
750fd445
KM
208void rsnd_set_slot(struct rsnd_dai *rdai,
209 int slots, int num)
210{
211 rdai->slots = slots;
212 rdai->slots_num = num;
213}
214
c140284b 215int rsnd_get_slot(struct rsnd_dai_stream *io)
8ec85e7f 216{
c140284b
KM
217 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
218
8ec85e7f
KM
219 return rdai->slots;
220}
221
750fd445
KM
222int rsnd_get_slot_num(struct rsnd_dai_stream *io)
223{
224 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
225
226 return rdai->slots_num;
227}
228
eed76bb8 229int rsnd_runtime_channel_original(struct rsnd_dai_stream *io)
8ec85e7f 230{
5858a7d1 231 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
b4c83b17 232
eed76bb8
KM
233 return runtime->channels;
234}
235
236int rsnd_runtime_channel_after_ctu(struct rsnd_dai_stream *io)
237{
238 int chan = rsnd_runtime_channel_original(io);
239 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
240
241 if (ctu_mod) {
242 u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod);
243
244 if (converted_chan)
245 return converted_chan;
246 }
247
248 return chan;
249}
250
251int rsnd_runtime_channel_for_ssi(struct rsnd_dai_stream *io)
252{
253 int chan = rsnd_io_is_play(io) ?
254 rsnd_runtime_channel_after_ctu(io) :
255 rsnd_runtime_channel_original(io);
256
257 /* Use Multi SSI */
258 if (rsnd_runtime_is_ssi_multi(io))
b4c83b17 259 chan /= rsnd_get_slot_num(io);
8ec85e7f
KM
260
261 /* TDM Extend Mode needs 8ch */
262 if (chan == 6)
263 chan = 8;
264
265 return chan;
266}
267
eed76bb8
KM
268int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io)
269{
270 int slots = rsnd_get_slot_num(io);
271 int chan = rsnd_io_is_play(io) ?
272 rsnd_runtime_channel_after_ctu(io) :
273 rsnd_runtime_channel_original(io);
274
275 return (chan >= 6) && (slots > 1);
276}
277
278int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io)
279{
280 return rsnd_runtime_channel_for_ssi(io) >= 6;
281}
282
d7bdbc5d 283/*
3023b384 284 * ADINR function
d7bdbc5d 285 */
3023b384 286u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
d7bdbc5d
KM
287{
288 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
d7bdbc5d
KM
289 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
290 struct device *dev = rsnd_priv_to_dev(priv);
d7bdbc5d
KM
291
292 switch (runtime->sample_bits) {
293 case 16:
5e7b9edd 294 return 8 << 16;
d7bdbc5d 295 case 32:
5e7b9edd 296 return 0 << 16;
d7bdbc5d
KM
297 }
298
5e7b9edd
KM
299 dev_warn(dev, "not supported sample bits\n");
300
301 return 0;
d7bdbc5d
KM
302}
303
4689032b
KM
304/*
305 * DALIGN function
306 */
307u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
308{
3ce2959d 309 struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
a504b1ee 310 struct rsnd_mod *target;
4689032b
KM
311 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
312 u32 val = 0x76543210;
313 u32 mask = ~0;
314
a504b1ee
KM
315 if (rsnd_io_is_play(io)) {
316 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
317
3ce2959d 318 target = src ? src : ssiu;
a504b1ee
KM
319 } else {
320 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
321
3ce2959d 322 target = cmd ? cmd : ssiu;
a504b1ee
KM
323 }
324
4689032b
KM
325 mask <<= runtime->channels * 4;
326 val = val & mask;
327
328 switch (runtime->sample_bits) {
329 case 16:
330 val |= 0x67452301 & ~mask;
331 break;
332 case 32:
333 val |= 0x76543210 & ~mask;
334 break;
335 }
336
337 /*
338 * exchange channeles on SRC if possible,
339 * otherwise, R/L volume settings on DVC
340 * changes inverted channels
341 */
342 if (mod == target)
343 return val;
344 else
345 return 0x76543210;
346}
347
1536a968
KM
348/*
349 * rsnd_dai functions
350 */
b3ca3fbe
KM
351struct rsnd_mod *rsnd_mod_next(int *iterator,
352 struct rsnd_dai_stream *io,
353 enum rsnd_mod_type *array,
354 int array_size)
355{
356 struct rsnd_mod *mod;
357 enum rsnd_mod_type type;
358 int max = array ? array_size : RSND_MOD_MAX;
359
360 for (; *iterator < max; (*iterator)++) {
361 type = (array) ? array[*iterator] : *iterator;
362 mod = io->mod[type];
363 if (!mod)
364 continue;
365
366 (*iterator)++;
367
368 return mod;
369 }
370
371 return NULL;
372}
373
38587f4c
KM
374static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
375 {
376 /* CAPTURE */
377 RSND_MOD_AUDMAPP,
378 RSND_MOD_AUDMA,
379 RSND_MOD_DVC,
380 RSND_MOD_MIX,
381 RSND_MOD_CTU,
382 RSND_MOD_CMD,
383 RSND_MOD_SRC,
384 RSND_MOD_SSIU,
385 RSND_MOD_SSIM3,
386 RSND_MOD_SSIM2,
387 RSND_MOD_SSIM1,
388 RSND_MOD_SSIP,
389 RSND_MOD_SSI,
390 }, {
391 /* PLAYBACK */
392 RSND_MOD_AUDMAPP,
393 RSND_MOD_AUDMA,
394 RSND_MOD_SSIM3,
395 RSND_MOD_SSIM2,
396 RSND_MOD_SSIM1,
397 RSND_MOD_SSIP,
398 RSND_MOD_SSI,
399 RSND_MOD_SSIU,
400 RSND_MOD_DVC,
401 RSND_MOD_MIX,
402 RSND_MOD_CTU,
403 RSND_MOD_CMD,
404 RSND_MOD_SRC,
405 },
406};
407
5f222a29
KM
408static int rsnd_status_update(u32 *status,
409 int shift, int add, int timing)
410{
411 u32 mask = 0xF << shift;
412 u8 val = (*status >> shift) & 0xF;
413 u8 next_val = (val + add) & 0xF;
414 int func_call = (val == timing);
415
416 if (next_val == 0xF) /* underflow case */
417 func_call = 0;
418 else
419 *status = (*status & ~mask) + (next_val << shift);
420
421 return func_call;
422}
423
424#define rsnd_dai_call(fn, io, param...) \
425({ \
426 struct rsnd_priv *priv = rsnd_io_to_priv(io); \
427 struct device *dev = rsnd_priv_to_dev(priv); \
428 struct rsnd_mod *mod; \
429 int is_play = rsnd_io_is_play(io); \
430 int ret = 0, i; \
431 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
432 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
433 int tmp = 0; \
434 u32 *status = mod->get_status(io, mod, types[i]); \
435 int func_call = rsnd_status_update(status, \
436 __rsnd_mod_shift_##fn, \
437 __rsnd_mod_add_##fn, \
438 __rsnd_mod_call_##fn); \
439 dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \
440 rsnd_mod_name(mod), rsnd_mod_id(mod), *status, \
441 (func_call && (mod)->ops->fn) ? #fn : ""); \
442 if (func_call && (mod)->ops->fn) \
443 tmp = (mod)->ops->fn(mod, io, param); \
444 if (tmp) \
445 dev_err(dev, "%s[%d] : %s error %d\n", \
446 rsnd_mod_name(mod), rsnd_mod_id(mod), \
447 #fn, tmp); \
448 ret |= tmp; \
449 } \
450 ret; \
cdaa3cdf
KM
451})
452
27924f32
KM
453int rsnd_dai_connect(struct rsnd_mod *mod,
454 struct rsnd_dai_stream *io,
455 enum rsnd_mod_type type)
cdaa3cdf 456{
48725e9c
KM
457 struct rsnd_priv *priv;
458 struct device *dev;
84e95355 459
6020779b 460 if (!mod)
cdaa3cdf 461 return -EIO;
cdaa3cdf 462
bfa3119c
KM
463 if (io->mod[type] == mod)
464 return 0;
465
52dc6852
KM
466 if (io->mod[type])
467 return -EINVAL;
468
48725e9c
KM
469 priv = rsnd_mod_to_priv(mod);
470 dev = rsnd_priv_to_dev(priv);
471
27924f32 472 io->mod[type] = mod;
cdaa3cdf 473
84e95355
KM
474 dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
475 rsnd_mod_name(mod), rsnd_mod_id(mod),
476 rsnd_io_is_play(io) ? "Playback" : "Capture");
477
cdaa3cdf
KM
478 return 0;
479}
480
d3a76823 481static void rsnd_dai_disconnect(struct rsnd_mod *mod,
27924f32
KM
482 struct rsnd_dai_stream *io,
483 enum rsnd_mod_type type)
d3a76823 484{
27924f32 485 io->mod[type] = NULL;
d3a76823
KM
486}
487
710d0889 488struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
1536a968 489{
ecba9e72 490 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
2192f81c
KM
491 return NULL;
492
1536a968
KM
493 return priv->rdai + id;
494}
495
eb2535f5 496#define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
1536a968
KM
497static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
498{
eb2535f5 499 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
1536a968 500
710d0889 501 return rsnd_rdai_get(priv, dai->id);
1536a968
KM
502}
503
1536a968
KM
504/*
505 * rsnd_soc_dai functions
506 */
507int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
508{
509 struct snd_pcm_substream *substream = io->substream;
510 struct snd_pcm_runtime *runtime = substream->runtime;
511 int pos = io->byte_pos + additional;
512
513 pos %= (runtime->periods * io->byte_per_period);
514
515 return pos;
516}
517
75defee0 518bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
1536a968
KM
519{
520 io->byte_pos += byte;
521
522 if (io->byte_pos >= io->next_period_byte) {
523 struct snd_pcm_substream *substream = io->substream;
524 struct snd_pcm_runtime *runtime = substream->runtime;
525
526 io->period_pos++;
527 io->next_period_byte += io->byte_per_period;
528
529 if (io->period_pos >= runtime->periods) {
530 io->byte_pos = 0;
531 io->period_pos = 0;
532 io->next_period_byte = io->byte_per_period;
533 }
534
75defee0 535 return true;
1536a968 536 }
75defee0
KM
537
538 return false;
539}
540
541void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
542{
543 struct snd_pcm_substream *substream = io->substream;
544
545 /*
546 * this function should be called...
547 *
548 * - if rsnd_dai_pointer_update() returns true
549 * - without spin lock
550 */
551
552 snd_pcm_period_elapsed(substream);
1536a968
KM
553}
554
5626ad08 555static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
1536a968
KM
556 struct snd_pcm_substream *substream)
557{
558 struct snd_pcm_runtime *runtime = substream->runtime;
559
1536a968
KM
560 io->substream = substream;
561 io->byte_pos = 0;
562 io->period_pos = 0;
563 io->byte_per_period = runtime->period_size *
564 runtime->channels *
565 samples_to_bytes(runtime, 1);
566 io->next_period_byte = io->byte_per_period;
5626ad08 567}
1536a968 568
5626ad08
KM
569static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
570{
571 io->substream = NULL;
1536a968
KM
572}
573
574static
575struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
576{
577 struct snd_soc_pcm_runtime *rtd = substream->private_data;
578
579 return rtd->cpu_dai;
580}
581
582static
583struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
584 struct snd_pcm_substream *substream)
585{
586 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
587 return &rdai->playback;
588 else
589 return &rdai->capture;
590}
591
592static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
593 struct snd_soc_dai *dai)
594{
eb2535f5 595 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
1536a968
KM
596 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
597 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1536a968
KM
598 int ret;
599 unsigned long flags;
600
02299d98 601 spin_lock_irqsave(&priv->lock, flags);
1536a968
KM
602
603 switch (cmd) {
604 case SNDRV_PCM_TRIGGER_START:
4b9c75ea 605 case SNDRV_PCM_TRIGGER_RESUME:
5626ad08 606 rsnd_dai_stream_init(io, substream);
1536a968 607
690602fc 608 ret = rsnd_dai_call(init, io, priv);
cdaa3cdf
KM
609 if (ret < 0)
610 goto dai_trigger_end;
611
690602fc 612 ret = rsnd_dai_call(start, io, priv);
cdaa3cdf
KM
613 if (ret < 0)
614 goto dai_trigger_end;
b5b442ab
KM
615
616 ret = rsnd_dai_call(irq, io, priv, 1);
617 if (ret < 0)
618 goto dai_trigger_end;
619
1536a968
KM
620 break;
621 case SNDRV_PCM_TRIGGER_STOP:
4b9c75ea 622 case SNDRV_PCM_TRIGGER_SUSPEND:
b5b442ab
KM
623 ret = rsnd_dai_call(irq, io, priv, 0);
624
625 ret |= rsnd_dai_call(stop, io, priv);
cdaa3cdf 626
89e3e2c3 627 ret |= rsnd_dai_call(quit, io, priv);
cdaa3cdf 628
5626ad08 629 rsnd_dai_stream_quit(io);
1536a968
KM
630 break;
631 default:
632 ret = -EINVAL;
633 }
634
635dai_trigger_end:
02299d98 636 spin_unlock_irqrestore(&priv->lock, flags);
1536a968
KM
637
638 return ret;
639}
640
641static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
642{
643 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
644
645 /* set master/slave audio interface */
646 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
647 case SND_SOC_DAIFMT_CBM_CFM:
e1508289 648 rdai->clk_master = 0;
1536a968
KM
649 break;
650 case SND_SOC_DAIFMT_CBS_CFS:
e1508289 651 rdai->clk_master = 1; /* codec is slave, cpu is master */
1536a968
KM
652 break;
653 default:
654 return -EINVAL;
655 }
656
1536a968
KM
657 /* set format */
658 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
659 case SND_SOC_DAIFMT_I2S:
660 rdai->sys_delay = 0;
661 rdai->data_alignment = 0;
1a7889ca 662 rdai->frm_clk_inv = 0;
1536a968
KM
663 break;
664 case SND_SOC_DAIFMT_LEFT_J:
665 rdai->sys_delay = 1;
666 rdai->data_alignment = 0;
1a7889ca 667 rdai->frm_clk_inv = 1;
1536a968
KM
668 break;
669 case SND_SOC_DAIFMT_RIGHT_J:
670 rdai->sys_delay = 1;
671 rdai->data_alignment = 1;
1a7889ca
KM
672 rdai->frm_clk_inv = 1;
673 break;
674 }
675
676 /* set clock inversion */
677 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
678 case SND_SOC_DAIFMT_NB_IF:
679 rdai->bit_clk_inv = rdai->bit_clk_inv;
680 rdai->frm_clk_inv = !rdai->frm_clk_inv;
681 break;
682 case SND_SOC_DAIFMT_IB_NF:
683 rdai->bit_clk_inv = !rdai->bit_clk_inv;
684 rdai->frm_clk_inv = rdai->frm_clk_inv;
685 break;
686 case SND_SOC_DAIFMT_IB_IF:
687 rdai->bit_clk_inv = !rdai->bit_clk_inv;
688 rdai->frm_clk_inv = !rdai->frm_clk_inv;
689 break;
690 case SND_SOC_DAIFMT_NB_NF:
691 default:
1536a968
KM
692 break;
693 }
694
695 return 0;
696}
697
186fadc1
KM
698static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
699 u32 tx_mask, u32 rx_mask,
700 int slots, int slot_width)
701{
702 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
703 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
704 struct device *dev = rsnd_priv_to_dev(priv);
705
706 switch (slots) {
707 case 6:
708 /* TDM Extend Mode */
750fd445 709 rsnd_set_slot(rdai, slots, 1);
186fadc1
KM
710 break;
711 default:
712 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
713 return -EINVAL;
714 }
715
716 return 0;
717}
718
10a9cca1
KM
719static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
720 struct snd_soc_dai *dai)
721{
722 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
723 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
724
725 /*
726 * call rsnd_dai_call without spinlock
727 */
728 return rsnd_dai_call(nolock_start, io, priv);
729}
730
731static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
732 struct snd_soc_dai *dai)
733{
734 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
735 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
736
737 /*
738 * call rsnd_dai_call without spinlock
739 */
740 rsnd_dai_call(nolock_stop, io, priv);
741}
742
1536a968 743static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
10a9cca1
KM
744 .startup = rsnd_soc_dai_startup,
745 .shutdown = rsnd_soc_dai_shutdown,
1536a968
KM
746 .trigger = rsnd_soc_dai_trigger,
747 .set_fmt = rsnd_soc_dai_set_fmt,
186fadc1 748 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
1536a968
KM
749};
750
89b66174
KM
751void rsnd_parse_connect_common(struct rsnd_dai *rdai,
752 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
753 struct device_node *node,
754 struct device_node *playback,
755 struct device_node *capture)
756{
757 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
758 struct device_node *np;
759 struct rsnd_mod *mod;
760 int i;
761
762 if (!node)
763 return;
764
765 i = 0;
766 for_each_child_of_node(node, np) {
767 mod = mod_get(priv, i);
768 if (np == playback)
769 rsnd_dai_connect(mod, &rdai->playback, mod->type);
770 if (np == capture)
771 rsnd_dai_connect(mod, &rdai->capture, mod->type);
772 i++;
773 }
774
775 of_node_put(node);
776}
777
2ea6b074 778static int rsnd_dai_probe(struct rsnd_priv *priv)
90e8e50f 779{
94e2710c 780 struct device_node *dai_node;
89b66174 781 struct device_node *dai_np;
90e8e50f 782 struct device_node *playback, *capture;
94e2710c
KM
783 struct rsnd_dai_stream *io_playback;
784 struct rsnd_dai_stream *io_capture;
2ff2ecca 785 struct snd_soc_dai_driver *rdrv, *drv;
94e2710c 786 struct rsnd_dai *rdai;
2ea6b074 787 struct device *dev = rsnd_priv_to_dev(priv);
89b66174 788 int nr, dai_i, io_i;
94e2710c 789 int ret;
90e8e50f 790
94e2710c 791 dai_node = rsnd_dai_of_node(priv);
90e8e50f 792 nr = of_get_child_count(dai_node);
94e2710c
KM
793 if (!nr) {
794 ret = -EINVAL;
795 goto rsnd_dai_probe_done;
90e8e50f
KM
796 }
797
2ff2ecca 798 rdrv = devm_kzalloc(dev, sizeof(*rdrv) * nr, GFP_KERNEL);
94e2710c 799 rdai = devm_kzalloc(dev, sizeof(*rdai) * nr, GFP_KERNEL);
2ff2ecca 800 if (!rdrv || !rdai) {
94e2710c
KM
801 ret = -ENOMEM;
802 goto rsnd_dai_probe_done;
803 }
90e8e50f 804
94e2710c 805 priv->rdai_nr = nr;
2ff2ecca 806 priv->daidrv = rdrv;
94e2710c 807 priv->rdai = rdai;
90e8e50f
KM
808
809 /*
810 * parse all dai
811 */
812 dai_i = 0;
813 for_each_child_of_node(dai_node, dai_np) {
94e2710c 814 rdai = rsnd_rdai_get(priv, dai_i);
2ff2ecca 815 drv = rdrv + dai_i;
94e2710c
KM
816 io_playback = &rdai->playback;
817 io_capture = &rdai->capture;
818
819 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
820
821 rdai->priv = priv;
822 drv->name = rdai->name;
823 drv->ops = &rsnd_soc_dai_ops;
824
825 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
826 "DAI%d Playback", dai_i);
827 drv->playback.rates = RSND_RATES;
828 drv->playback.formats = RSND_FMTS;
829 drv->playback.channels_min = 2;
186fadc1 830 drv->playback.channels_max = 6;
94e2710c
KM
831 drv->playback.stream_name = rdai->playback.name;
832
833 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
834 "DAI%d Capture", dai_i);
835 drv->capture.rates = RSND_RATES;
836 drv->capture.formats = RSND_FMTS;
837 drv->capture.channels_min = 2;
186fadc1 838 drv->capture.channels_max = 6;
94e2710c
KM
839 drv->capture.stream_name = rdai->capture.name;
840
841 rdai->playback.rdai = rdai;
842 rdai->capture.rdai = rdai;
750fd445 843 rsnd_set_slot(rdai, 2, 1); /* default */
90e8e50f 844
94e2710c
KM
845 for (io_i = 0;; io_i++) {
846 playback = of_parse_phandle(dai_np, "playback", io_i);
847 capture = of_parse_phandle(dai_np, "capture", io_i);
90e8e50f
KM
848
849 if (!playback && !capture)
850 break;
851
89b66174
KM
852 rsnd_parse_connect_ssi(rdai, playback, capture);
853 rsnd_parse_connect_src(rdai, playback, capture);
854 rsnd_parse_connect_ctu(rdai, playback, capture);
855 rsnd_parse_connect_mix(rdai, playback, capture);
856 rsnd_parse_connect_dvc(rdai, playback, capture);
90e8e50f 857
a493b6a6
JL
858 of_node_put(playback);
859 of_node_put(capture);
90e8e50f
KM
860 }
861
862 dai_i++;
1536a968 863
94e2710c
KM
864 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
865 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
866 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
1536a968
KM
867 }
868
94e2710c 869 ret = 0;
49848073 870
94e2710c
KM
871rsnd_dai_probe_done:
872 of_node_put(dai_node);
1536a968 873
94e2710c 874 return ret;
1536a968
KM
875}
876
1536a968
KM
877/*
878 * pcm ops
879 */
880static struct snd_pcm_hardware rsnd_pcm_hardware = {
881 .info = SNDRV_PCM_INFO_INTERLEAVED |
882 SNDRV_PCM_INFO_MMAP |
706c6621 883 SNDRV_PCM_INFO_MMAP_VALID,
1536a968
KM
884 .buffer_bytes_max = 64 * 1024,
885 .period_bytes_min = 32,
886 .period_bytes_max = 8192,
887 .periods_min = 1,
888 .periods_max = 32,
889 .fifo_size = 256,
890};
891
892static int rsnd_pcm_open(struct snd_pcm_substream *substream)
893{
894 struct snd_pcm_runtime *runtime = substream->runtime;
895 int ret = 0;
896
897 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
898
899 ret = snd_pcm_hw_constraint_integer(runtime,
900 SNDRV_PCM_HW_PARAM_PERIODS);
901
902 return ret;
903}
904
905static int rsnd_hw_params(struct snd_pcm_substream *substream,
906 struct snd_pcm_hw_params *hw_params)
907{
3b7843ff
KM
908 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
909 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
910 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
911 int ret;
912
913 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
914 if (ret)
915 return ret;
916
1536a968
KM
917 return snd_pcm_lib_malloc_pages(substream,
918 params_buffer_bytes(hw_params));
919}
920
921static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
922{
923 struct snd_pcm_runtime *runtime = substream->runtime;
924 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
925 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
926 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
927
928 return bytes_to_frames(runtime, io->byte_pos);
929}
930
931static struct snd_pcm_ops rsnd_pcm_ops = {
932 .open = rsnd_pcm_open,
933 .ioctl = snd_pcm_lib_ioctl,
934 .hw_params = rsnd_hw_params,
935 .hw_free = snd_pcm_lib_free_pages,
936 .pointer = rsnd_pointer,
937};
938
170a2497
KM
939/*
940 * snd_kcontrol
941 */
942#define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
943static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
944 struct snd_ctl_elem_info *uinfo)
945{
946 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
947
948 if (cfg->texts) {
949 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
950 uinfo->count = cfg->size;
951 uinfo->value.enumerated.items = cfg->max;
952 if (uinfo->value.enumerated.item >= cfg->max)
953 uinfo->value.enumerated.item = cfg->max - 1;
954 strlcpy(uinfo->value.enumerated.name,
955 cfg->texts[uinfo->value.enumerated.item],
956 sizeof(uinfo->value.enumerated.name));
957 } else {
958 uinfo->count = cfg->size;
959 uinfo->value.integer.min = 0;
960 uinfo->value.integer.max = cfg->max;
961 uinfo->type = (cfg->max == 1) ?
962 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
963 SNDRV_CTL_ELEM_TYPE_INTEGER;
964 }
965
966 return 0;
967}
968
969static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
970 struct snd_ctl_elem_value *uc)
971{
972 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
973 int i;
974
975 for (i = 0; i < cfg->size; i++)
976 if (cfg->texts)
977 uc->value.enumerated.item[i] = cfg->val[i];
978 else
979 uc->value.integer.value[i] = cfg->val[i];
980
981 return 0;
982}
983
984static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
985 struct snd_ctl_elem_value *uc)
986{
987 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
988 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
989 int i, change = 0;
990
991 for (i = 0; i < cfg->size; i++) {
992 if (cfg->texts) {
993 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
994 cfg->val[i] = uc->value.enumerated.item[i];
995 } else {
996 change |= (uc->value.integer.value[i] != cfg->val[i]);
997 cfg->val[i] = uc->value.integer.value[i];
998 }
999 }
1000
d7289565 1001 if (change && cfg->update)
b65a7ccc 1002 cfg->update(cfg->io, mod);
170a2497
KM
1003
1004 return change;
1005}
1006
1007static int __rsnd_kctrl_new(struct rsnd_mod *mod,
b65a7ccc 1008 struct rsnd_dai_stream *io,
170a2497
KM
1009 struct snd_soc_pcm_runtime *rtd,
1010 const unsigned char *name,
1011 struct rsnd_kctrl_cfg *cfg,
b65a7ccc
KM
1012 void (*update)(struct rsnd_dai_stream *io,
1013 struct rsnd_mod *mod))
170a2497
KM
1014{
1015 struct snd_card *card = rtd->card->snd_card;
1016 struct snd_kcontrol *kctrl;
1017 struct snd_kcontrol_new knew = {
1018 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1019 .name = name,
1020 .info = rsnd_kctrl_info,
1a497983 1021 .index = rtd->num,
170a2497
KM
1022 .get = rsnd_kctrl_get,
1023 .put = rsnd_kctrl_put,
1024 .private_value = (unsigned long)cfg,
1025 };
1026 int ret;
1027
1028 kctrl = snd_ctl_new1(&knew, mod);
1029 if (!kctrl)
1030 return -ENOMEM;
1031
1032 ret = snd_ctl_add(card, kctrl);
0ea617a2 1033 if (ret < 0)
170a2497
KM
1034 return ret;
1035
1036 cfg->update = update;
d1f83d6e
KM
1037 cfg->card = card;
1038 cfg->kctrl = kctrl;
b65a7ccc 1039 cfg->io = io;
170a2497
KM
1040
1041 return 0;
1042}
1043
d1f83d6e
KM
1044void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
1045{
0af5c01a
KM
1046 if (cfg->card && cfg->kctrl)
1047 snd_ctl_remove(cfg->card, cfg->kctrl);
1048
1049 cfg->card = NULL;
1050 cfg->kctrl = NULL;
d1f83d6e
KM
1051}
1052
170a2497 1053int rsnd_kctrl_new_m(struct rsnd_mod *mod,
b65a7ccc 1054 struct rsnd_dai_stream *io,
170a2497
KM
1055 struct snd_soc_pcm_runtime *rtd,
1056 const unsigned char *name,
b65a7ccc
KM
1057 void (*update)(struct rsnd_dai_stream *io,
1058 struct rsnd_mod *mod),
170a2497 1059 struct rsnd_kctrl_cfg_m *_cfg,
42ab9a79 1060 int ch_size,
170a2497
KM
1061 u32 max)
1062{
d2240f0d 1063 if (ch_size > RSND_MAX_CHANNELS)
42ab9a79
KM
1064 return -EINVAL;
1065
170a2497 1066 _cfg->cfg.max = max;
42ab9a79 1067 _cfg->cfg.size = ch_size;
170a2497 1068 _cfg->cfg.val = _cfg->val;
b65a7ccc 1069 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
170a2497
KM
1070}
1071
1072int rsnd_kctrl_new_s(struct rsnd_mod *mod,
b65a7ccc 1073 struct rsnd_dai_stream *io,
170a2497
KM
1074 struct snd_soc_pcm_runtime *rtd,
1075 const unsigned char *name,
b65a7ccc
KM
1076 void (*update)(struct rsnd_dai_stream *io,
1077 struct rsnd_mod *mod),
170a2497
KM
1078 struct rsnd_kctrl_cfg_s *_cfg,
1079 u32 max)
1080{
1081 _cfg->cfg.max = max;
1082 _cfg->cfg.size = 1;
1083 _cfg->cfg.val = &_cfg->val;
b65a7ccc 1084 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
170a2497
KM
1085}
1086
1087int rsnd_kctrl_new_e(struct rsnd_mod *mod,
b65a7ccc 1088 struct rsnd_dai_stream *io,
170a2497
KM
1089 struct snd_soc_pcm_runtime *rtd,
1090 const unsigned char *name,
1091 struct rsnd_kctrl_cfg_s *_cfg,
b65a7ccc
KM
1092 void (*update)(struct rsnd_dai_stream *io,
1093 struct rsnd_mod *mod),
170a2497
KM
1094 const char * const *texts,
1095 u32 max)
1096{
1097 _cfg->cfg.max = max;
1098 _cfg->cfg.size = 1;
1099 _cfg->cfg.val = &_cfg->val;
1100 _cfg->cfg.texts = texts;
b65a7ccc 1101 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
170a2497
KM
1102}
1103
1536a968
KM
1104/*
1105 * snd_soc_platform
1106 */
1107
1108#define PREALLOC_BUFFER (32 * 1024)
1109#define PREALLOC_BUFFER_MAX (32 * 1024)
1110
1111static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1112{
7c63f3c0
KM
1113 struct snd_soc_dai *dai = rtd->cpu_dai;
1114 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
ae11a9be 1115 int ret;
bff58ea4 1116
ae11a9be
KM
1117 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1118 if (ret)
1119 return ret;
bff58ea4 1120
ae11a9be 1121 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
7c63f3c0
KM
1122 if (ret)
1123 return ret;
bff58ea4 1124
1536a968
KM
1125 return snd_pcm_lib_preallocate_pages_for_all(
1126 rtd->pcm,
4821d914
KM
1127 SNDRV_DMA_TYPE_CONTINUOUS,
1128 snd_dma_continuous_data(GFP_KERNEL),
1536a968
KM
1129 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1130}
1131
1536a968
KM
1132static struct snd_soc_platform_driver rsnd_soc_platform = {
1133 .ops = &rsnd_pcm_ops,
1134 .pcm_new = rsnd_pcm_new,
1536a968
KM
1135};
1136
1137static const struct snd_soc_component_driver rsnd_soc_component = {
1138 .name = "rsnd",
1139};
1140
d3a76823 1141static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
f708d944 1142 struct rsnd_dai_stream *io)
d3a76823 1143{
d3a76823
KM
1144 int ret;
1145
690602fc 1146 ret = rsnd_dai_call(probe, io, priv);
d3a76823 1147 if (ret == -EAGAIN) {
48d58281 1148 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
9b87bfb2 1149 struct rsnd_mod *mod;
48d58281
KM
1150 int i;
1151
d3a76823
KM
1152 /*
1153 * Fallback to PIO mode
1154 */
1155
1156 /*
1157 * call "remove" for SSI/SRC/DVC
1158 * SSI will be switch to PIO mode if it was DMA mode
1159 * see
1160 * rsnd_dma_init()
97463e19 1161 * rsnd_ssi_fallback()
d3a76823 1162 */
690602fc 1163 rsnd_dai_call(remove, io, priv);
d3a76823
KM
1164
1165 /*
48d58281
KM
1166 * remove all mod from io
1167 * and, re connect ssi
d3a76823 1168 */
9b87bfb2
KM
1169 for_each_rsnd_mod(i, mod, io)
1170 rsnd_dai_disconnect(mod, io, i);
48d58281 1171 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
d3a76823 1172
97463e19
KM
1173 /*
1174 * fallback
1175 */
690602fc 1176 rsnd_dai_call(fallback, io, priv);
97463e19 1177
d3a76823
KM
1178 /*
1179 * retry to "probe".
1180 * DAI has SSI which is PIO mode only now.
1181 */
690602fc 1182 ret = rsnd_dai_call(probe, io, priv);
d3a76823
KM
1183 }
1184
1185 return ret;
1186}
1187
1536a968
KM
1188/*
1189 * rsnd probe
1190 */
1191static int rsnd_probe(struct platform_device *pdev)
1192{
1536a968
KM
1193 struct rsnd_priv *priv;
1194 struct device *dev = &pdev->dev;
7681f6ac 1195 struct rsnd_dai *rdai;
2ea6b074 1196 int (*probe_func[])(struct rsnd_priv *priv) = {
d1ac970f 1197 rsnd_gen_probe,
288f392e 1198 rsnd_dma_probe,
d1ac970f 1199 rsnd_ssi_probe,
c7f69ab5 1200 rsnd_ssiu_probe,
ba9c949f 1201 rsnd_src_probe,
9269e3c3 1202 rsnd_ctu_probe,
70fb1052 1203 rsnd_mix_probe,
bff58ea4 1204 rsnd_dvc_probe,
1b2ca0ad 1205 rsnd_cmd_probe,
d1ac970f
KM
1206 rsnd_adg_probe,
1207 rsnd_dai_probe,
1208 };
1209 int ret, i;
1536a968 1210
1536a968
KM
1211 /*
1212 * init priv data
1213 */
1214 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1215 if (!priv) {
1216 dev_err(dev, "priv allocate failed\n");
1217 return -ENODEV;
1218 }
1219
9f464f8e 1220 priv->pdev = pdev;
6d8044b4 1221 priv->flags = (unsigned long)of_device_get_match_data(dev);
1536a968
KM
1222 spin_lock_init(&priv->lock);
1223
1224 /*
1225 * init each module
1226 */
d1ac970f 1227 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
2ea6b074 1228 ret = probe_func[i](priv);
d1ac970f
KM
1229 if (ret)
1230 return ret;
1231 }
07539c1d 1232
7681f6ac 1233 for_each_rsnd_dai(rdai, priv, i) {
f708d944 1234 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
7681f6ac 1235 if (ret)
d62a3dcd 1236 goto exit_snd_probe;
dfc9403b 1237
f708d944 1238 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
7681f6ac 1239 if (ret)
d62a3dcd 1240 goto exit_snd_probe;
7681f6ac 1241 }
4b4dab82 1242
0b1f6ec7
KM
1243 dev_set_drvdata(dev, priv);
1244
1536a968
KM
1245 /*
1246 * asoc register
1247 */
1248 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1249 if (ret < 0) {
1250 dev_err(dev, "cannot snd soc register\n");
1251 return ret;
1252 }
1253
1254 ret = snd_soc_register_component(dev, &rsnd_soc_component,
ecba9e72 1255 priv->daidrv, rsnd_rdai_nr(priv));
1536a968
KM
1256 if (ret < 0) {
1257 dev_err(dev, "cannot snd dai register\n");
1258 goto exit_snd_soc;
1259 }
1260
1536a968
KM
1261 pm_runtime_enable(dev);
1262
1263 dev_info(dev, "probed\n");
1264 return ret;
1265
1266exit_snd_soc:
1267 snd_soc_unregister_platform(dev);
d62a3dcd
KM
1268exit_snd_probe:
1269 for_each_rsnd_dai(rdai, priv, i) {
690602fc
KM
1270 rsnd_dai_call(remove, &rdai->playback, priv);
1271 rsnd_dai_call(remove, &rdai->capture, priv);
d62a3dcd 1272 }
1536a968
KM
1273
1274 return ret;
1275}
1276
1277static int rsnd_remove(struct platform_device *pdev)
1278{
1279 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
7681f6ac 1280 struct rsnd_dai *rdai;
2ea6b074 1281 void (*remove_func[])(struct rsnd_priv *priv) = {
2f78dd7f 1282 rsnd_ssi_remove,
c7f69ab5 1283 rsnd_ssiu_remove,
2f78dd7f 1284 rsnd_src_remove,
9269e3c3 1285 rsnd_ctu_remove,
70fb1052 1286 rsnd_mix_remove,
2f78dd7f 1287 rsnd_dvc_remove,
1b2ca0ad 1288 rsnd_cmd_remove,
68a55024 1289 rsnd_adg_remove,
2f78dd7f 1290 };
d62a3dcd 1291 int ret = 0, i;
1536a968
KM
1292
1293 pm_runtime_disable(&pdev->dev);
1294
7681f6ac 1295 for_each_rsnd_dai(rdai, priv, i) {
690602fc
KM
1296 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1297 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
7681f6ac 1298 }
1536a968 1299
2f78dd7f 1300 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
2ea6b074 1301 remove_func[i](priv);
2f78dd7f 1302
d7c42ff8
KM
1303 snd_soc_unregister_component(&pdev->dev);
1304 snd_soc_unregister_platform(&pdev->dev);
1305
d62a3dcd 1306 return ret;
1536a968
KM
1307}
1308
c2d31718
KM
1309static int rsnd_suspend(struct device *dev)
1310{
1311 struct rsnd_priv *priv = dev_get_drvdata(dev);
1312
1313 rsnd_adg_clk_disable(priv);
1314
1315 return 0;
1316}
1317
1318static int rsnd_resume(struct device *dev)
1319{
1320 struct rsnd_priv *priv = dev_get_drvdata(dev);
1321
1322 rsnd_adg_clk_enable(priv);
1323
1324 return 0;
1325}
1326
1327static struct dev_pm_ops rsnd_pm_ops = {
1328 .suspend = rsnd_suspend,
1329 .resume = rsnd_resume,
1330};
1331
1536a968
KM
1332static struct platform_driver rsnd_driver = {
1333 .driver = {
1334 .name = "rcar_sound",
c2d31718 1335 .pm = &rsnd_pm_ops,
90e8e50f 1336 .of_match_table = rsnd_of_match,
1536a968
KM
1337 },
1338 .probe = rsnd_probe,
1339 .remove = rsnd_remove,
1340};
1341module_platform_driver(rsnd_driver);
1342
1343MODULE_LICENSE("GPL");
1344MODULE_DESCRIPTION("Renesas R-Car audio driver");
1345MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1346MODULE_ALIAS("platform:rcar-pcm-audio");