ASoC: rsnd: use devm_clk_get() instead of clk_get()
[deliverable/linux.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 * |
76 * | ** these control scu
77 * |
78 * +- scu
79 * |
80 * +- scu[0]
81 * +- scu[1]
82 * +- scu[2]
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>
9ade09d6 97#include <linux/shdma-base.h>
1536a968
KM
98#include "rsnd.h"
99
100#define RSND_RATES SNDRV_PCM_RATE_8000_96000
101#define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
102
103/*
104 * rsnd_platform functions
105 */
106#define rsnd_platform_call(priv, dai, func, param...) \
740ad6c3 107 (!(priv->info->func) ? 0 : \
1536a968
KM
108 priv->info->func(param))
109
cdaa3cdf
KM
110/*
111 * rsnd_mod functions
112 */
113char *rsnd_mod_name(struct rsnd_mod *mod)
114{
115 if (!mod || !mod->ops)
116 return "unknown";
117
118 return mod->ops->name;
119}
120
121void rsnd_mod_init(struct rsnd_priv *priv,
122 struct rsnd_mod *mod,
123 struct rsnd_mod_ops *ops,
a126021d 124 enum rsnd_mod_type type,
cdaa3cdf
KM
125 int id)
126{
127 mod->priv = priv;
128 mod->id = id;
129 mod->ops = ops;
a126021d 130 mod->type = type;
cdaa3cdf
KM
131}
132
0a4d94c0
KM
133/*
134 * rsnd_dma functions
135 */
f5cab3b8 136static void __rsnd_dma_start(struct rsnd_dma *dma);
0a4d94c0
KM
137static void rsnd_dma_continue(struct rsnd_dma *dma)
138{
139 /* push next A or B plane */
140 dma->submit_loop = 1;
141 schedule_work(&dma->work);
142}
143
144void rsnd_dma_start(struct rsnd_dma *dma)
145{
146 /* push both A and B plane*/
4686a0ad 147 dma->offset = 0;
0a4d94c0 148 dma->submit_loop = 2;
f5cab3b8 149 __rsnd_dma_start(dma);
0a4d94c0
KM
150}
151
152void rsnd_dma_stop(struct rsnd_dma *dma)
153{
154 dma->submit_loop = 0;
155 cancel_work_sync(&dma->work);
156 dmaengine_terminate_all(dma->chan);
157}
158
159static void rsnd_dma_complete(void *data)
160{
161 struct rsnd_dma *dma = (struct rsnd_dma *)data;
4686a0ad 162 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
9b5ab573 163 struct rsnd_priv *priv = rsnd_mod_to_priv(rsnd_dma_to_mod(dma));
4686a0ad 164 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
0a4d94c0
KM
165 unsigned long flags;
166
167 rsnd_lock(priv, flags);
168
4686a0ad
KM
169 /*
170 * Renesas sound Gen1 needs 1 DMAC,
171 * Gen2 needs 2 DMAC.
172 * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
173 * But, Audio-DMAC-peri-peri doesn't have interrupt,
174 * and this driver is assuming that here.
175 *
176 * If Audio-DMAC-peri-peri has interrpt,
177 * rsnd_dai_pointer_update() will be called twice,
178 * ant it will breaks io->byte_pos
179 */
180
181 rsnd_dai_pointer_update(io, io->byte_per_period);
0a4d94c0
KM
182
183 if (dma->submit_loop)
184 rsnd_dma_continue(dma);
185
186 rsnd_unlock(priv, flags);
187}
188
f5cab3b8 189static void __rsnd_dma_start(struct rsnd_dma *dma)
0a4d94c0 190{
4686a0ad
KM
191 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
192 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
193 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
194 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
0a4d94c0
KM
195 struct device *dev = rsnd_priv_to_dev(priv);
196 struct dma_async_tx_descriptor *desc;
197 dma_addr_t buf;
4686a0ad 198 size_t len = io->byte_per_period;
0a4d94c0
KM
199 int i;
200
201 for (i = 0; i < dma->submit_loop; i++) {
202
4686a0ad
KM
203 buf = runtime->dma_addr +
204 rsnd_dai_pointer_offset(io, dma->offset + len);
205 dma->offset = len;
0a4d94c0
KM
206
207 desc = dmaengine_prep_slave_single(
208 dma->chan, buf, len, dma->dir,
209 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
210 if (!desc) {
211 dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
212 return;
213 }
214
215 desc->callback = rsnd_dma_complete;
216 desc->callback_param = dma;
217
218 if (dmaengine_submit(desc) < 0) {
219 dev_err(dev, "dmaengine_submit() fail\n");
220 return;
221 }
222
a0d32bca 223 dma_async_issue_pending(dma->chan);
0a4d94c0 224 }
0a4d94c0
KM
225}
226
f5cab3b8
KM
227static void rsnd_dma_do_work(struct work_struct *work)
228{
229 struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work);
230
231 __rsnd_dma_start(dma);
232}
233
0a4d94c0
KM
234int rsnd_dma_available(struct rsnd_dma *dma)
235{
236 return !!dma->chan;
237}
238
0a4d94c0 239int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
4686a0ad 240 int is_play, int id)
0a4d94c0
KM
241{
242 struct device *dev = rsnd_priv_to_dev(priv);
9ade09d6 243 struct dma_slave_config cfg;
0a4d94c0 244 dma_cap_mask_t mask;
9ade09d6 245 int ret;
0a4d94c0
KM
246
247 if (dma->chan) {
248 dev_err(dev, "it already has dma channel\n");
249 return -EIO;
250 }
251
252 dma_cap_zero(mask);
253 dma_cap_set(DMA_SLAVE, mask);
254
9ade09d6
KM
255 dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
256 (void *)id, dev,
257 is_play ? "tx" : "rx");
0a4d94c0
KM
258 if (!dma->chan) {
259 dev_err(dev, "can't get dma channel\n");
260 return -EIO;
261 }
262
9ade09d6
KM
263 cfg.slave_id = id;
264 cfg.dst_addr = 0; /* use default addr when playback */
265 cfg.src_addr = 0; /* use default addr when capture */
266 cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
267
268 ret = dmaengine_slave_config(dma->chan, &cfg);
269 if (ret < 0)
270 goto rsnd_dma_init_err;
271
0a4d94c0 272 dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
0a4d94c0
KM
273 INIT_WORK(&dma->work, rsnd_dma_do_work);
274
275 return 0;
9ade09d6
KM
276
277rsnd_dma_init_err:
278 rsnd_dma_quit(priv, dma);
279
280 return ret;
0a4d94c0
KM
281}
282
283void rsnd_dma_quit(struct rsnd_priv *priv,
284 struct rsnd_dma *dma)
285{
286 if (dma->chan)
287 dma_release_channel(dma->chan);
288
289 dma->chan = NULL;
290}
291
1536a968
KM
292/*
293 * rsnd_dai functions
294 */
d870a91e
KM
295#define __rsnd_mod_call(mod, func, rdai, io) \
296({ \
297 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
298 struct device *dev = rsnd_priv_to_dev(priv); \
299 dev_dbg(dev, "%s [%d] %s\n", \
300 rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \
301 (mod)->ops->func(mod, rdai, io); \
302})
303
304#define rsnd_mod_call(mod, func, rdai, io) \
305 (!(mod) ? -ENODEV : \
306 !((mod)->ops->func) ? 0 : \
307 __rsnd_mod_call(mod, func, (rdai), (io)))
308
309#define rsnd_dai_call(rdai, io, fn) \
310({ \
a126021d
KM
311 struct rsnd_mod *mod; \
312 int ret = 0, i; \
313 for (i = 0; i < RSND_MOD_MAX; i++) { \
314 mod = (io)->mod[i]; \
315 if (!mod) \
316 continue; \
d870a91e
KM
317 ret = rsnd_mod_call(mod, fn, (rdai), (io)); \
318 if (ret < 0) \
319 break; \
320 } \
321 ret; \
cdaa3cdf
KM
322})
323
a126021d 324static int rsnd_dai_connect(struct rsnd_mod *mod,
9bfed6cf 325 struct rsnd_dai_stream *io)
cdaa3cdf 326{
6020779b 327 if (!mod)
cdaa3cdf 328 return -EIO;
cdaa3cdf 329
a126021d 330 if (io->mod[mod->type]) {
6020779b
KM
331 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
332 struct device *dev = rsnd_priv_to_dev(priv);
333
cdaa3cdf
KM
334 dev_err(dev, "%s%d is not empty\n",
335 rsnd_mod_name(mod),
336 rsnd_mod_id(mod));
337 return -EIO;
338 }
339
a126021d 340 io->mod[mod->type] = mod;
4686a0ad 341 mod->io = io;
cdaa3cdf
KM
342
343 return 0;
344}
345
a126021d 346static int rsnd_dai_disconnect(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
cdaa3cdf 347{
a126021d 348 io->mod[mod->type] = NULL;
4686a0ad 349 mod->io = NULL;
cdaa3cdf
KM
350
351 return 0;
352}
353
4b4dab82
KM
354int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
355{
356 int id = rdai - priv->rdai;
357
ecba9e72 358 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
4b4dab82
KM
359 return -EINVAL;
360
361 return id;
362}
363
1536a968
KM
364struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
365{
ecba9e72 366 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
2192f81c
KM
367 return NULL;
368
1536a968
KM
369 return priv->rdai + id;
370}
371
372static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
373{
374 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
375
376 return rsnd_dai_get(priv, dai->id);
377}
378
379int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
380{
381 return &rdai->playback == io;
382}
383
384/*
385 * rsnd_soc_dai functions
386 */
387int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
388{
389 struct snd_pcm_substream *substream = io->substream;
390 struct snd_pcm_runtime *runtime = substream->runtime;
391 int pos = io->byte_pos + additional;
392
393 pos %= (runtime->periods * io->byte_per_period);
394
395 return pos;
396}
397
398void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
399{
400 io->byte_pos += byte;
401
402 if (io->byte_pos >= io->next_period_byte) {
403 struct snd_pcm_substream *substream = io->substream;
404 struct snd_pcm_runtime *runtime = substream->runtime;
405
406 io->period_pos++;
407 io->next_period_byte += io->byte_per_period;
408
409 if (io->period_pos >= runtime->periods) {
410 io->byte_pos = 0;
411 io->period_pos = 0;
412 io->next_period_byte = io->byte_per_period;
413 }
414
415 snd_pcm_period_elapsed(substream);
416 }
417}
418
419static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
420 struct snd_pcm_substream *substream)
421{
422 struct snd_pcm_runtime *runtime = substream->runtime;
423
1536a968
KM
424 io->substream = substream;
425 io->byte_pos = 0;
426 io->period_pos = 0;
427 io->byte_per_period = runtime->period_size *
428 runtime->channels *
429 samples_to_bytes(runtime, 1);
430 io->next_period_byte = io->byte_per_period;
431
432 return 0;
433}
434
435static
436struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
437{
438 struct snd_soc_pcm_runtime *rtd = substream->private_data;
439
440 return rtd->cpu_dai;
441}
442
443static
444struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
445 struct snd_pcm_substream *substream)
446{
447 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
448 return &rdai->playback;
449 else
450 return &rdai->capture;
451}
452
453static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
454 struct snd_soc_dai *dai)
455{
456 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
457 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
458 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
4b4dab82
KM
459 struct rsnd_mod *mod = rsnd_ssi_mod_get_frm_dai(priv,
460 rsnd_dai_id(priv, rdai),
461 rsnd_dai_is_play(rdai, io));
462 int ssi_id = rsnd_mod_id(mod);
1536a968
KM
463 int ret;
464 unsigned long flags;
465
466 rsnd_lock(priv, flags);
467
468 switch (cmd) {
469 case SNDRV_PCM_TRIGGER_START:
470 ret = rsnd_dai_stream_init(io, substream);
471 if (ret < 0)
472 goto dai_trigger_end;
473
474 ret = rsnd_platform_call(priv, dai, start, ssi_id);
475 if (ret < 0)
476 goto dai_trigger_end;
477
cdaa3cdf
KM
478 ret = rsnd_dai_call(rdai, io, init);
479 if (ret < 0)
480 goto dai_trigger_end;
481
482 ret = rsnd_dai_call(rdai, io, start);
483 if (ret < 0)
484 goto dai_trigger_end;
1536a968
KM
485 break;
486 case SNDRV_PCM_TRIGGER_STOP:
cdaa3cdf
KM
487 ret = rsnd_dai_call(rdai, io, stop);
488 if (ret < 0)
489 goto dai_trigger_end;
490
491 ret = rsnd_dai_call(rdai, io, quit);
492 if (ret < 0)
493 goto dai_trigger_end;
494
3337744a
KM
495 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
496 if (ret < 0)
497 goto dai_trigger_end;
1536a968
KM
498 break;
499 default:
500 ret = -EINVAL;
501 }
502
503dai_trigger_end:
504 rsnd_unlock(priv, flags);
505
506 return ret;
507}
508
509static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
510{
511 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
512
513 /* set master/slave audio interface */
514 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
515 case SND_SOC_DAIFMT_CBM_CFM:
516 rdai->clk_master = 1;
517 break;
518 case SND_SOC_DAIFMT_CBS_CFS:
519 rdai->clk_master = 0;
520 break;
521 default:
522 return -EINVAL;
523 }
524
525 /* set clock inversion */
526 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
527 case SND_SOC_DAIFMT_NB_IF:
528 rdai->bit_clk_inv = 0;
529 rdai->frm_clk_inv = 1;
530 break;
531 case SND_SOC_DAIFMT_IB_NF:
532 rdai->bit_clk_inv = 1;
533 rdai->frm_clk_inv = 0;
534 break;
535 case SND_SOC_DAIFMT_IB_IF:
536 rdai->bit_clk_inv = 1;
537 rdai->frm_clk_inv = 1;
538 break;
539 case SND_SOC_DAIFMT_NB_NF:
540 default:
541 rdai->bit_clk_inv = 0;
542 rdai->frm_clk_inv = 0;
543 break;
544 }
545
546 /* set format */
547 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
548 case SND_SOC_DAIFMT_I2S:
549 rdai->sys_delay = 0;
550 rdai->data_alignment = 0;
551 break;
552 case SND_SOC_DAIFMT_LEFT_J:
553 rdai->sys_delay = 1;
554 rdai->data_alignment = 0;
555 break;
556 case SND_SOC_DAIFMT_RIGHT_J:
557 rdai->sys_delay = 1;
558 rdai->data_alignment = 1;
559 break;
560 }
561
562 return 0;
563}
564
565static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
566 .trigger = rsnd_soc_dai_trigger,
567 .set_fmt = rsnd_soc_dai_set_fmt,
568};
569
9bfed6cf
KM
570static int rsnd_path_init(struct rsnd_priv *priv,
571 struct rsnd_dai *rdai,
572 struct rsnd_dai_stream *io)
573{
574 struct rsnd_mod *mod;
575 int ret;
576 int id;
577
578 /*
579 * Gen1 is created by SRU/SSI, and this SRU is base module of
580 * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
581 *
582 * Easy image is..
583 * Gen1 SRU = Gen2 SCU + SSIU + etc
584 *
585 * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
586 * using fixed path.
587 *
588 * Then, SSI id = SCU id here
589 */
590 /* get SSI's ID */
591 mod = rsnd_ssi_mod_get_frm_dai(priv,
592 rsnd_dai_id(priv, rdai),
593 rsnd_dai_is_play(rdai, io));
594 if (!mod)
595 return 0;
596 id = rsnd_mod_id(mod);
597 ret = 0;
598
599 /* SCU */
600 mod = rsnd_scu_mod_get(priv, id);
601 if (mod) {
a126021d 602 ret = rsnd_dai_connect(mod, io);
9bfed6cf
KM
603 if (ret < 0)
604 return ret;
605 }
606
607 /* SSI */
608 mod = rsnd_ssi_mod_get(priv, id);
609 if (mod) {
a126021d 610 ret = rsnd_dai_connect(mod, io);
9bfed6cf
KM
611 if (ret < 0)
612 return ret;
613 }
614
615 return ret;
616}
617
618static int rsnd_path_exit(struct rsnd_priv *priv,
619 struct rsnd_dai *rdai,
620 struct rsnd_dai_stream *io)
621{
a126021d
KM
622 struct rsnd_mod *mod;
623 int ret = 0, i;
9bfed6cf
KM
624
625 /*
626 * remove all mod from rdai
627 */
a126021d
KM
628 for (i = 0; i < RSND_MOD_MAX; i++) {
629 mod = io->mod[i];
630 if (!mod)
631 continue;
632 ret |= rsnd_dai_disconnect(mod, io);
633 }
9bfed6cf
KM
634
635 return ret;
636}
637
1536a968 638static int rsnd_dai_probe(struct platform_device *pdev,
1536a968
KM
639 struct rsnd_priv *priv)
640{
641 struct snd_soc_dai_driver *drv;
642 struct rsnd_dai *rdai;
4b4dab82 643 struct rsnd_mod *pmod, *cmod;
1536a968 644 struct device *dev = rsnd_priv_to_dev(priv);
4b4dab82
KM
645 int dai_nr;
646 int i;
647
648 /* get max dai nr */
649 for (dai_nr = 0; dai_nr < 32; dai_nr++) {
650 pmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 1);
651 cmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 0);
652
653 if (!pmod && !cmod)
654 break;
655 }
656
657 if (!dai_nr) {
658 dev_err(dev, "no dai\n");
659 return -EIO;
660 }
1536a968
KM
661
662 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
663 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
664 if (!drv || !rdai) {
665 dev_err(dev, "dai allocate failed\n");
666 return -ENOMEM;
667 }
668
ecba9e72 669 priv->rdai_nr = dai_nr;
49848073
KM
670 priv->daidrv = drv;
671 priv->rdai = rdai;
672
1536a968 673 for (i = 0; i < dai_nr; i++) {
1536a968 674
4b4dab82
KM
675 pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1);
676 cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0);
1536a968
KM
677
678 /*
679 * init rsnd_dai
680 */
1536a968
KM
681 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
682
683 /*
684 * init snd_soc_dai_driver
685 */
686 drv[i].name = rdai[i].name;
687 drv[i].ops = &rsnd_soc_dai_ops;
4b4dab82 688 if (pmod) {
1536a968
KM
689 drv[i].playback.rates = RSND_RATES;
690 drv[i].playback.formats = RSND_FMTS;
691 drv[i].playback.channels_min = 2;
692 drv[i].playback.channels_max = 2;
9bfed6cf 693 rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
1536a968 694 }
4b4dab82 695 if (cmod) {
1536a968
KM
696 drv[i].capture.rates = RSND_RATES;
697 drv[i].capture.formats = RSND_FMTS;
698 drv[i].capture.channels_min = 2;
699 drv[i].capture.channels_max = 2;
9bfed6cf 700 rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
1536a968
KM
701 }
702
4b4dab82
KM
703 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
704 pmod ? "play" : " -- ",
705 cmod ? "capture" : " -- ");
1536a968
KM
706 }
707
1536a968
KM
708 return 0;
709}
710
711static void rsnd_dai_remove(struct platform_device *pdev,
712 struct rsnd_priv *priv)
713{
9bfed6cf
KM
714 struct rsnd_dai *rdai;
715 int i;
716
717 for (i = 0; i < rsnd_rdai_nr(priv); i++) {
718 rdai = rsnd_dai_get(priv, i);
719 rsnd_path_exit(priv, rdai, &rdai->playback);
720 rsnd_path_exit(priv, rdai, &rdai->capture);
721 }
1536a968
KM
722}
723
724/*
725 * pcm ops
726 */
727static struct snd_pcm_hardware rsnd_pcm_hardware = {
728 .info = SNDRV_PCM_INFO_INTERLEAVED |
729 SNDRV_PCM_INFO_MMAP |
730 SNDRV_PCM_INFO_MMAP_VALID |
731 SNDRV_PCM_INFO_PAUSE,
1536a968
KM
732 .buffer_bytes_max = 64 * 1024,
733 .period_bytes_min = 32,
734 .period_bytes_max = 8192,
735 .periods_min = 1,
736 .periods_max = 32,
737 .fifo_size = 256,
738};
739
740static int rsnd_pcm_open(struct snd_pcm_substream *substream)
741{
742 struct snd_pcm_runtime *runtime = substream->runtime;
743 int ret = 0;
744
745 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
746
747 ret = snd_pcm_hw_constraint_integer(runtime,
748 SNDRV_PCM_HW_PARAM_PERIODS);
749
750 return ret;
751}
752
753static int rsnd_hw_params(struct snd_pcm_substream *substream,
754 struct snd_pcm_hw_params *hw_params)
755{
756 return snd_pcm_lib_malloc_pages(substream,
757 params_buffer_bytes(hw_params));
758}
759
760static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
761{
762 struct snd_pcm_runtime *runtime = substream->runtime;
763 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
764 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
765 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
766
767 return bytes_to_frames(runtime, io->byte_pos);
768}
769
770static struct snd_pcm_ops rsnd_pcm_ops = {
771 .open = rsnd_pcm_open,
772 .ioctl = snd_pcm_lib_ioctl,
773 .hw_params = rsnd_hw_params,
774 .hw_free = snd_pcm_lib_free_pages,
775 .pointer = rsnd_pointer,
776};
777
778/*
779 * snd_soc_platform
780 */
781
782#define PREALLOC_BUFFER (32 * 1024)
783#define PREALLOC_BUFFER_MAX (32 * 1024)
784
785static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
786{
787 return snd_pcm_lib_preallocate_pages_for_all(
788 rtd->pcm,
789 SNDRV_DMA_TYPE_DEV,
790 rtd->card->snd_card->dev,
791 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
792}
793
794static void rsnd_pcm_free(struct snd_pcm *pcm)
795{
796 snd_pcm_lib_preallocate_free_for_all(pcm);
797}
798
799static struct snd_soc_platform_driver rsnd_soc_platform = {
800 .ops = &rsnd_pcm_ops,
801 .pcm_new = rsnd_pcm_new,
802 .pcm_free = rsnd_pcm_free,
803};
804
805static const struct snd_soc_component_driver rsnd_soc_component = {
806 .name = "rsnd",
807};
808
809/*
810 * rsnd probe
811 */
812static int rsnd_probe(struct platform_device *pdev)
813{
814 struct rcar_snd_info *info;
815 struct rsnd_priv *priv;
816 struct device *dev = &pdev->dev;
817 int ret;
818
819 info = pdev->dev.platform_data;
820 if (!info) {
821 dev_err(dev, "driver needs R-Car sound information\n");
822 return -ENODEV;
823 }
824
825 /*
826 * init priv data
827 */
828 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
829 if (!priv) {
830 dev_err(dev, "priv allocate failed\n");
831 return -ENODEV;
832 }
833
834 priv->dev = dev;
835 priv->info = info;
836 spin_lock_init(&priv->lock);
837
838 /*
839 * init each module
840 */
5da39cf3 841 ret = rsnd_gen_probe(pdev, priv);
106d2eff 842 if (ret)
3337744a
KM
843 return ret;
844
5da39cf3 845 ret = rsnd_ssi_probe(pdev, priv);
106d2eff 846 if (ret)
07539c1d
KM
847 return ret;
848
5da39cf3 849 ret = rsnd_scu_probe(pdev, priv);
106d2eff 850 if (ret)
dfc9403b
KM
851 return ret;
852
5da39cf3 853 ret = rsnd_adg_probe(pdev, priv);
106d2eff 854 if (ret)
ae5c3223
KM
855 return ret;
856
5da39cf3 857 ret = rsnd_dai_probe(pdev, priv);
106d2eff 858 if (ret)
4b4dab82
KM
859 return ret;
860
1536a968
KM
861 /*
862 * asoc register
863 */
864 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
865 if (ret < 0) {
866 dev_err(dev, "cannot snd soc register\n");
867 return ret;
868 }
869
870 ret = snd_soc_register_component(dev, &rsnd_soc_component,
ecba9e72 871 priv->daidrv, rsnd_rdai_nr(priv));
1536a968
KM
872 if (ret < 0) {
873 dev_err(dev, "cannot snd dai register\n");
874 goto exit_snd_soc;
875 }
876
877 dev_set_drvdata(dev, priv);
878
879 pm_runtime_enable(dev);
880
881 dev_info(dev, "probed\n");
882 return ret;
883
884exit_snd_soc:
885 snd_soc_unregister_platform(dev);
886
887 return ret;
888}
889
890static int rsnd_remove(struct platform_device *pdev)
891{
892 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
893
894 pm_runtime_disable(&pdev->dev);
895
896 /*
897 * remove each module
898 */
ae5c3223 899 rsnd_ssi_remove(pdev, priv);
dfc9403b 900 rsnd_adg_remove(pdev, priv);
07539c1d 901 rsnd_scu_remove(pdev, priv);
1536a968 902 rsnd_dai_remove(pdev, priv);
3337744a 903 rsnd_gen_remove(pdev, priv);
1536a968
KM
904
905 return 0;
906}
907
908static struct platform_driver rsnd_driver = {
909 .driver = {
910 .name = "rcar_sound",
911 },
912 .probe = rsnd_probe,
913 .remove = rsnd_remove,
914};
915module_platform_driver(rsnd_driver);
916
917MODULE_LICENSE("GPL");
918MODULE_DESCRIPTION("Renesas R-Car audio driver");
919MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
920MODULE_ALIAS("platform:rcar-pcm-audio");
This page took 0.080409 seconds and 5 git commands to generate.