ASoC: fsi: use dmaengine_prep_dma_cyclic() for DMA transfer
[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 * |
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>
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
90e8e50f
KM
103static struct rsnd_of_data rsnd_of_data_gen1 = {
104 .flags = RSND_GEN1,
105};
106
107static struct rsnd_of_data rsnd_of_data_gen2 = {
108 .flags = RSND_GEN2,
109};
110
111static struct of_device_id rsnd_of_match[] = {
112 { .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 },
113 { .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 },
114 {},
115};
116MODULE_DEVICE_TABLE(of, rsnd_of_match);
117
1536a968
KM
118/*
119 * rsnd_platform functions
120 */
121#define rsnd_platform_call(priv, dai, func, param...) \
740ad6c3 122 (!(priv->info->func) ? 0 : \
1536a968
KM
123 priv->info->func(param))
124
389933d9
KM
125#define rsnd_is_enable_path(io, name) \
126 ((io)->info ? (io)->info->name : NULL)
127#define rsnd_info_id(priv, io, name) \
128 ((io)->info->name - priv->info->name##_info)
129
cdaa3cdf
KM
130/*
131 * rsnd_mod functions
132 */
133char *rsnd_mod_name(struct rsnd_mod *mod)
134{
135 if (!mod || !mod->ops)
136 return "unknown";
137
138 return mod->ops->name;
139}
140
141void rsnd_mod_init(struct rsnd_priv *priv,
142 struct rsnd_mod *mod,
143 struct rsnd_mod_ops *ops,
a126021d 144 enum rsnd_mod_type type,
cdaa3cdf
KM
145 int id)
146{
147 mod->priv = priv;
148 mod->id = id;
149 mod->ops = ops;
a126021d 150 mod->type = type;
cdaa3cdf
KM
151}
152
0a4d94c0
KM
153/*
154 * rsnd_dma functions
155 */
f5cab3b8 156static void __rsnd_dma_start(struct rsnd_dma *dma);
0a4d94c0
KM
157static void rsnd_dma_continue(struct rsnd_dma *dma)
158{
159 /* push next A or B plane */
160 dma->submit_loop = 1;
161 schedule_work(&dma->work);
162}
163
164void rsnd_dma_start(struct rsnd_dma *dma)
165{
166 /* push both A and B plane*/
4686a0ad 167 dma->offset = 0;
0a4d94c0 168 dma->submit_loop = 2;
f5cab3b8 169 __rsnd_dma_start(dma);
0a4d94c0
KM
170}
171
172void rsnd_dma_stop(struct rsnd_dma *dma)
173{
174 dma->submit_loop = 0;
175 cancel_work_sync(&dma->work);
176 dmaengine_terminate_all(dma->chan);
177}
178
179static void rsnd_dma_complete(void *data)
180{
181 struct rsnd_dma *dma = (struct rsnd_dma *)data;
4686a0ad 182 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
9b5ab573 183 struct rsnd_priv *priv = rsnd_mod_to_priv(rsnd_dma_to_mod(dma));
4686a0ad 184 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
0a4d94c0
KM
185 unsigned long flags;
186
187 rsnd_lock(priv, flags);
188
4686a0ad
KM
189 /*
190 * Renesas sound Gen1 needs 1 DMAC,
191 * Gen2 needs 2 DMAC.
192 * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
193 * But, Audio-DMAC-peri-peri doesn't have interrupt,
194 * and this driver is assuming that here.
195 *
196 * If Audio-DMAC-peri-peri has interrpt,
197 * rsnd_dai_pointer_update() will be called twice,
198 * ant it will breaks io->byte_pos
199 */
0a4d94c0
KM
200 if (dma->submit_loop)
201 rsnd_dma_continue(dma);
202
203 rsnd_unlock(priv, flags);
836b31fe
KM
204
205 rsnd_dai_pointer_update(io, io->byte_per_period);
0a4d94c0
KM
206}
207
f5cab3b8 208static void __rsnd_dma_start(struct rsnd_dma *dma)
0a4d94c0 209{
4686a0ad
KM
210 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
211 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
212 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
213 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
0a4d94c0
KM
214 struct device *dev = rsnd_priv_to_dev(priv);
215 struct dma_async_tx_descriptor *desc;
216 dma_addr_t buf;
4686a0ad 217 size_t len = io->byte_per_period;
0a4d94c0
KM
218 int i;
219
220 for (i = 0; i < dma->submit_loop; i++) {
221
4686a0ad
KM
222 buf = runtime->dma_addr +
223 rsnd_dai_pointer_offset(io, dma->offset + len);
224 dma->offset = len;
0a4d94c0
KM
225
226 desc = dmaengine_prep_slave_single(
227 dma->chan, buf, len, dma->dir,
228 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
229 if (!desc) {
230 dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
231 return;
232 }
233
234 desc->callback = rsnd_dma_complete;
235 desc->callback_param = dma;
236
237 if (dmaengine_submit(desc) < 0) {
238 dev_err(dev, "dmaengine_submit() fail\n");
239 return;
240 }
241
a0d32bca 242 dma_async_issue_pending(dma->chan);
0a4d94c0 243 }
0a4d94c0
KM
244}
245
f5cab3b8
KM
246static void rsnd_dma_do_work(struct work_struct *work)
247{
248 struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work);
249
250 __rsnd_dma_start(dma);
251}
252
0a4d94c0
KM
253int rsnd_dma_available(struct rsnd_dma *dma)
254{
255 return !!dma->chan;
256}
257
199e7688
KM
258#define DMA_NAME_SIZE 16
259#define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
260static int _rsnd_dma_of_name(char *dma_name, struct rsnd_mod *mod)
261{
262 if (mod)
263 return snprintf(dma_name, DMA_NAME_SIZE / 2, "%s%d",
264 rsnd_mod_name(mod), rsnd_mod_id(mod));
265 else
266 return snprintf(dma_name, DMA_NAME_SIZE / 2, "mem");
267
268}
269
270static void rsnd_dma_of_name(struct rsnd_dma *dma,
271 int is_play, char *dma_name)
272{
273 struct rsnd_mod *this = rsnd_dma_to_mod(dma);
274 struct rsnd_dai_stream *io = rsnd_mod_to_io(this);
275 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
276 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
277 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
278 struct rsnd_mod *mod[MOD_MAX];
279 struct rsnd_mod *src_mod, *dst_mod;
280 int i, index;
281
282
283 for (i = 0; i < MOD_MAX; i++)
284 mod[i] = NULL;
285
286 /*
287 * in play case...
288 *
289 * src -> dst
290 *
291 * mem -> SSI
292 * mem -> SRC -> SSI
293 * mem -> SRC -> DVC -> SSI
294 */
295 mod[0] = NULL; /* for "mem" */
296 index = 1;
297 for (i = 1; i < MOD_MAX; i++) {
298 if (!src) {
299 mod[i] = ssi;
199e7688
KM
300 } else if (!dvc) {
301 mod[i] = src;
302 src = NULL;
303 } else {
304 mod[i] = dvc;
305 dvc = NULL;
306 }
307
308 if (mod[i] == this)
309 index = i;
c08c3b08
KM
310
311 if (mod[i] == ssi)
312 break;
199e7688
KM
313 }
314
315 if (is_play) {
316 src_mod = mod[index - 1];
317 dst_mod = mod[index];
318 } else {
319 src_mod = mod[index];
4cf61278 320 dst_mod = mod[index - 1];
199e7688
KM
321 }
322
323 index = 0;
324 index = _rsnd_dma_of_name(dma_name + index, src_mod);
325 *(dma_name + index++) = '_';
326 index = _rsnd_dma_of_name(dma_name + index, dst_mod);
327}
328
0a4d94c0 329int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
4686a0ad 330 int is_play, int id)
0a4d94c0
KM
331{
332 struct device *dev = rsnd_priv_to_dev(priv);
9ade09d6 333 struct dma_slave_config cfg;
199e7688 334 char dma_name[DMA_NAME_SIZE];
0a4d94c0 335 dma_cap_mask_t mask;
9ade09d6 336 int ret;
0a4d94c0
KM
337
338 if (dma->chan) {
339 dev_err(dev, "it already has dma channel\n");
340 return -EIO;
341 }
342
343 dma_cap_zero(mask);
344 dma_cap_set(DMA_SLAVE, mask);
345
199e7688
KM
346 if (dev->of_node)
347 rsnd_dma_of_name(dma, is_play, dma_name);
348 else
349 snprintf(dma_name, DMA_NAME_SIZE,
350 is_play ? "tx" : "rx");
351
352 dev_dbg(dev, "dma name : %s\n", dma_name);
353
9ade09d6
KM
354 dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
355 (void *)id, dev,
199e7688 356 dma_name);
0a4d94c0
KM
357 if (!dma->chan) {
358 dev_err(dev, "can't get dma channel\n");
359 return -EIO;
360 }
361
ad32d0c7 362 rsnd_gen_dma_addr(priv, dma, &cfg, is_play, id);
9ade09d6
KM
363
364 ret = dmaengine_slave_config(dma->chan, &cfg);
365 if (ret < 0)
366 goto rsnd_dma_init_err;
367
cd7bcc60 368 dma->dir = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
0a4d94c0
KM
369 INIT_WORK(&dma->work, rsnd_dma_do_work);
370
371 return 0;
9ade09d6
KM
372
373rsnd_dma_init_err:
374 rsnd_dma_quit(priv, dma);
375
376 return ret;
0a4d94c0
KM
377}
378
379void rsnd_dma_quit(struct rsnd_priv *priv,
380 struct rsnd_dma *dma)
381{
382 if (dma->chan)
383 dma_release_channel(dma->chan);
384
385 dma->chan = NULL;
386}
387
d7bdbc5d
KM
388/*
389 * settting function
390 */
391u32 rsnd_get_adinr(struct rsnd_mod *mod)
392{
393 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
394 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
395 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
396 struct device *dev = rsnd_priv_to_dev(priv);
397 u32 adinr = runtime->channels;
398
399 switch (runtime->sample_bits) {
400 case 16:
401 adinr |= (8 << 16);
402 break;
403 case 32:
404 adinr |= (0 << 16);
405 break;
406 default:
407 dev_warn(dev, "not supported sample bits\n");
408 return 0;
409 }
410
411 return adinr;
412}
413
1536a968
KM
414/*
415 * rsnd_dai functions
416 */
68b6af36 417#define __rsnd_mod_call(mod, func, rdai...) \
d870a91e
KM
418({ \
419 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
420 struct device *dev = rsnd_priv_to_dev(priv); \
421 dev_dbg(dev, "%s [%d] %s\n", \
422 rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \
b42fccf6 423 (mod)->ops->func(mod, rdai); \
d870a91e
KM
424})
425
68b6af36 426#define rsnd_mod_call(mod, func, rdai...) \
d870a91e
KM
427 (!(mod) ? -ENODEV : \
428 !((mod)->ops->func) ? 0 : \
68b6af36 429 __rsnd_mod_call(mod, func, rdai))
d870a91e 430
68b6af36 431#define rsnd_dai_call(fn, io, rdai...) \
d870a91e 432({ \
a126021d
KM
433 struct rsnd_mod *mod; \
434 int ret = 0, i; \
435 for (i = 0; i < RSND_MOD_MAX; i++) { \
436 mod = (io)->mod[i]; \
437 if (!mod) \
438 continue; \
68b6af36 439 ret = rsnd_mod_call(mod, fn, rdai); \
d870a91e
KM
440 if (ret < 0) \
441 break; \
442 } \
443 ret; \
cdaa3cdf
KM
444})
445
a126021d 446static int rsnd_dai_connect(struct rsnd_mod *mod,
9bfed6cf 447 struct rsnd_dai_stream *io)
cdaa3cdf 448{
6020779b 449 if (!mod)
cdaa3cdf 450 return -EIO;
cdaa3cdf 451
a126021d 452 if (io->mod[mod->type]) {
6020779b
KM
453 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
454 struct device *dev = rsnd_priv_to_dev(priv);
455
cdaa3cdf
KM
456 dev_err(dev, "%s%d is not empty\n",
457 rsnd_mod_name(mod),
458 rsnd_mod_id(mod));
459 return -EIO;
460 }
461
a126021d 462 io->mod[mod->type] = mod;
4686a0ad 463 mod->io = io;
cdaa3cdf
KM
464
465 return 0;
466}
467
4b4dab82
KM
468int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
469{
470 int id = rdai - priv->rdai;
471
ecba9e72 472 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
4b4dab82
KM
473 return -EINVAL;
474
475 return id;
476}
477
1536a968
KM
478struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
479{
ecba9e72 480 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
2192f81c
KM
481 return NULL;
482
1536a968
KM
483 return priv->rdai + id;
484}
485
486static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
487{
488 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
489
490 return rsnd_dai_get(priv, dai->id);
491}
492
493int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
494{
495 return &rdai->playback == io;
496}
497
498/*
499 * rsnd_soc_dai functions
500 */
501int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
502{
503 struct snd_pcm_substream *substream = io->substream;
504 struct snd_pcm_runtime *runtime = substream->runtime;
505 int pos = io->byte_pos + additional;
506
507 pos %= (runtime->periods * io->byte_per_period);
508
509 return pos;
510}
511
512void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
513{
514 io->byte_pos += byte;
515
516 if (io->byte_pos >= io->next_period_byte) {
517 struct snd_pcm_substream *substream = io->substream;
518 struct snd_pcm_runtime *runtime = substream->runtime;
519
520 io->period_pos++;
521 io->next_period_byte += io->byte_per_period;
522
523 if (io->period_pos >= runtime->periods) {
524 io->byte_pos = 0;
525 io->period_pos = 0;
526 io->next_period_byte = io->byte_per_period;
527 }
528
529 snd_pcm_period_elapsed(substream);
530 }
531}
532
533static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
534 struct snd_pcm_substream *substream)
535{
536 struct snd_pcm_runtime *runtime = substream->runtime;
537
1536a968
KM
538 io->substream = substream;
539 io->byte_pos = 0;
540 io->period_pos = 0;
541 io->byte_per_period = runtime->period_size *
542 runtime->channels *
543 samples_to_bytes(runtime, 1);
544 io->next_period_byte = io->byte_per_period;
545
546 return 0;
547}
548
549static
550struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
551{
552 struct snd_soc_pcm_runtime *rtd = substream->private_data;
553
554 return rtd->cpu_dai;
555}
556
557static
558struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
559 struct snd_pcm_substream *substream)
560{
561 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
562 return &rdai->playback;
563 else
564 return &rdai->capture;
565}
566
567static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
568 struct snd_soc_dai *dai)
569{
570 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
571 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
572 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
29e69fd2 573 int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
1536a968
KM
574 int ret;
575 unsigned long flags;
576
577 rsnd_lock(priv, flags);
578
579 switch (cmd) {
580 case SNDRV_PCM_TRIGGER_START:
581 ret = rsnd_dai_stream_init(io, substream);
582 if (ret < 0)
583 goto dai_trigger_end;
584
585 ret = rsnd_platform_call(priv, dai, start, ssi_id);
586 if (ret < 0)
587 goto dai_trigger_end;
588
68b6af36 589 ret = rsnd_dai_call(init, io, rdai);
cdaa3cdf
KM
590 if (ret < 0)
591 goto dai_trigger_end;
592
68b6af36 593 ret = rsnd_dai_call(start, io, rdai);
cdaa3cdf
KM
594 if (ret < 0)
595 goto dai_trigger_end;
1536a968
KM
596 break;
597 case SNDRV_PCM_TRIGGER_STOP:
68b6af36 598 ret = rsnd_dai_call(stop, io, rdai);
cdaa3cdf
KM
599 if (ret < 0)
600 goto dai_trigger_end;
601
68b6af36 602 ret = rsnd_dai_call(quit, io, rdai);
cdaa3cdf
KM
603 if (ret < 0)
604 goto dai_trigger_end;
605
3337744a
KM
606 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
607 if (ret < 0)
608 goto dai_trigger_end;
1536a968
KM
609 break;
610 default:
611 ret = -EINVAL;
612 }
613
614dai_trigger_end:
615 rsnd_unlock(priv, flags);
616
617 return ret;
618}
619
620static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
621{
622 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
623
624 /* set master/slave audio interface */
625 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
626 case SND_SOC_DAIFMT_CBM_CFM:
e1508289 627 rdai->clk_master = 0;
1536a968
KM
628 break;
629 case SND_SOC_DAIFMT_CBS_CFS:
e1508289 630 rdai->clk_master = 1; /* codec is slave, cpu is master */
1536a968
KM
631 break;
632 default:
633 return -EINVAL;
634 }
635
636 /* set clock inversion */
637 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
638 case SND_SOC_DAIFMT_NB_IF:
639 rdai->bit_clk_inv = 0;
640 rdai->frm_clk_inv = 1;
641 break;
642 case SND_SOC_DAIFMT_IB_NF:
643 rdai->bit_clk_inv = 1;
644 rdai->frm_clk_inv = 0;
645 break;
646 case SND_SOC_DAIFMT_IB_IF:
647 rdai->bit_clk_inv = 1;
648 rdai->frm_clk_inv = 1;
649 break;
650 case SND_SOC_DAIFMT_NB_NF:
651 default:
652 rdai->bit_clk_inv = 0;
653 rdai->frm_clk_inv = 0;
654 break;
655 }
656
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;
662 break;
663 case SND_SOC_DAIFMT_LEFT_J:
664 rdai->sys_delay = 1;
665 rdai->data_alignment = 0;
666 break;
667 case SND_SOC_DAIFMT_RIGHT_J:
668 rdai->sys_delay = 1;
669 rdai->data_alignment = 1;
670 break;
671 }
672
673 return 0;
674}
675
676static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
677 .trigger = rsnd_soc_dai_trigger,
678 .set_fmt = rsnd_soc_dai_set_fmt,
679};
680
739f9502
KM
681#define rsnd_path_parse(priv, io, type) \
682({ \
683 struct rsnd_mod *mod; \
684 int ret = 0; \
685 int id = -1; \
686 \
687 if (rsnd_is_enable_path(io, type)) { \
688 id = rsnd_info_id(priv, io, type); \
689 if (id >= 0) { \
690 mod = rsnd_##type##_mod_get(priv, id); \
691 ret = rsnd_dai_connect(mod, io); \
692 } \
693 } \
694 ret; \
695})
696
9bfed6cf
KM
697static int rsnd_path_init(struct rsnd_priv *priv,
698 struct rsnd_dai *rdai,
699 struct rsnd_dai_stream *io)
700{
9bfed6cf 701 int ret;
9bfed6cf
KM
702
703 /*
704 * Gen1 is created by SRU/SSI, and this SRU is base module of
705 * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
706 *
707 * Easy image is..
708 * Gen1 SRU = Gen2 SCU + SSIU + etc
709 *
710 * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
711 * using fixed path.
9bfed6cf 712 */
9bfed6cf 713
ba9c949f 714 /* SRC */
739f9502
KM
715 ret = rsnd_path_parse(priv, io, src);
716 if (ret < 0)
717 return ret;
9bfed6cf
KM
718
719 /* SSI */
739f9502
KM
720 ret = rsnd_path_parse(priv, io, ssi);
721 if (ret < 0)
722 return ret;
9bfed6cf 723
bff58ea4
KM
724 /* DVC */
725 ret = rsnd_path_parse(priv, io, dvc);
726 if (ret < 0)
727 return ret;
9bfed6cf
KM
728
729 return ret;
730}
731
90e8e50f
KM
732static void rsnd_of_parse_dai(struct platform_device *pdev,
733 const struct rsnd_of_data *of_data,
734 struct rsnd_priv *priv)
735{
736 struct device_node *dai_node, *dai_np;
737 struct device_node *ssi_node, *ssi_np;
738 struct device_node *src_node, *src_np;
739 struct device_node *playback, *capture;
740 struct rsnd_dai_platform_info *dai_info;
741 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
742 struct device *dev = &pdev->dev;
743 int nr, i;
744 int dai_i, ssi_i, src_i;
745
746 if (!of_data)
747 return;
748
749 dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
750 if (!dai_node)
751 return;
752
753 nr = of_get_child_count(dai_node);
754 if (!nr)
755 return;
756
757 dai_info = devm_kzalloc(dev,
758 sizeof(struct rsnd_dai_platform_info) * nr,
759 GFP_KERNEL);
760 if (!dai_info) {
761 dev_err(dev, "dai info allocation error\n");
762 return;
763 }
764
765 info->dai_info_nr = nr;
766 info->dai_info = dai_info;
767
768 ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
769 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
770
771#define mod_parse(name) \
772if (name##_node) { \
773 struct rsnd_##name##_platform_info *name##_info; \
774 \
775 name##_i = 0; \
776 for_each_child_of_node(name##_node, name##_np) { \
777 name##_info = info->name##_info + name##_i; \
778 \
779 if (name##_np == playback) \
780 dai_info->playback.name = name##_info; \
781 if (name##_np == capture) \
782 dai_info->capture.name = name##_info; \
783 \
784 name##_i++; \
785 } \
786}
787
788 /*
789 * parse all dai
790 */
791 dai_i = 0;
792 for_each_child_of_node(dai_node, dai_np) {
793 dai_info = info->dai_info + dai_i;
794
795 for (i = 0;; i++) {
796
797 playback = of_parse_phandle(dai_np, "playback", i);
798 capture = of_parse_phandle(dai_np, "capture", i);
799
800 if (!playback && !capture)
801 break;
802
803 mod_parse(ssi);
804 mod_parse(src);
805
806 if (playback)
807 of_node_put(playback);
808 if (capture)
809 of_node_put(capture);
810 }
811
812 dai_i++;
813 }
814}
815
1536a968 816static int rsnd_dai_probe(struct platform_device *pdev,
90e8e50f 817 const struct rsnd_of_data *of_data,
1536a968
KM
818 struct rsnd_priv *priv)
819{
820 struct snd_soc_dai_driver *drv;
78f13d0c 821 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
1536a968 822 struct rsnd_dai *rdai;
29e69fd2 823 struct rsnd_ssi_platform_info *pmod, *cmod;
1536a968 824 struct device *dev = rsnd_priv_to_dev(priv);
90e8e50f 825 int dai_nr;
4b4dab82
KM
826 int i;
827
90e8e50f
KM
828 rsnd_of_parse_dai(pdev, of_data, priv);
829
90e8e50f 830 dai_nr = info->dai_info_nr;
4b4dab82
KM
831 if (!dai_nr) {
832 dev_err(dev, "no dai\n");
833 return -EIO;
834 }
1536a968
KM
835
836 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
837 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
838 if (!drv || !rdai) {
839 dev_err(dev, "dai allocate failed\n");
840 return -ENOMEM;
841 }
842
ecba9e72 843 priv->rdai_nr = dai_nr;
49848073
KM
844 priv->daidrv = drv;
845 priv->rdai = rdai;
846
1536a968 847 for (i = 0; i < dai_nr; i++) {
29e69fd2 848 rdai[i].info = &info->dai_info[i];
1536a968 849
29e69fd2
KM
850 pmod = rdai[i].info->playback.ssi;
851 cmod = rdai[i].info->capture.ssi;
1536a968
KM
852
853 /*
854 * init rsnd_dai
855 */
1536a968
KM
856 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
857
858 /*
859 * init snd_soc_dai_driver
860 */
861 drv[i].name = rdai[i].name;
862 drv[i].ops = &rsnd_soc_dai_ops;
4b4dab82 863 if (pmod) {
1536a968
KM
864 drv[i].playback.rates = RSND_RATES;
865 drv[i].playback.formats = RSND_FMTS;
866 drv[i].playback.channels_min = 2;
867 drv[i].playback.channels_max = 2;
389933d9 868
29e69fd2 869 rdai[i].playback.info = &info->dai_info[i].playback;
9bfed6cf 870 rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
1536a968 871 }
4b4dab82 872 if (cmod) {
1536a968
KM
873 drv[i].capture.rates = RSND_RATES;
874 drv[i].capture.formats = RSND_FMTS;
875 drv[i].capture.channels_min = 2;
876 drv[i].capture.channels_max = 2;
389933d9 877
29e69fd2 878 rdai[i].capture.info = &info->dai_info[i].capture;
9bfed6cf 879 rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
1536a968
KM
880 }
881
4b4dab82
KM
882 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
883 pmod ? "play" : " -- ",
884 cmod ? "capture" : " -- ");
1536a968
KM
885 }
886
1536a968
KM
887 return 0;
888}
889
1536a968
KM
890/*
891 * pcm ops
892 */
893static struct snd_pcm_hardware rsnd_pcm_hardware = {
894 .info = SNDRV_PCM_INFO_INTERLEAVED |
895 SNDRV_PCM_INFO_MMAP |
896 SNDRV_PCM_INFO_MMAP_VALID |
897 SNDRV_PCM_INFO_PAUSE,
1536a968
KM
898 .buffer_bytes_max = 64 * 1024,
899 .period_bytes_min = 32,
900 .period_bytes_max = 8192,
901 .periods_min = 1,
902 .periods_max = 32,
903 .fifo_size = 256,
904};
905
906static int rsnd_pcm_open(struct snd_pcm_substream *substream)
907{
908 struct snd_pcm_runtime *runtime = substream->runtime;
909 int ret = 0;
910
911 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
912
913 ret = snd_pcm_hw_constraint_integer(runtime,
914 SNDRV_PCM_HW_PARAM_PERIODS);
915
916 return ret;
917}
918
919static int rsnd_hw_params(struct snd_pcm_substream *substream,
920 struct snd_pcm_hw_params *hw_params)
921{
922 return snd_pcm_lib_malloc_pages(substream,
923 params_buffer_bytes(hw_params));
924}
925
926static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
927{
928 struct snd_pcm_runtime *runtime = substream->runtime;
929 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
930 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
931 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
932
933 return bytes_to_frames(runtime, io->byte_pos);
934}
935
936static struct snd_pcm_ops rsnd_pcm_ops = {
937 .open = rsnd_pcm_open,
938 .ioctl = snd_pcm_lib_ioctl,
939 .hw_params = rsnd_hw_params,
940 .hw_free = snd_pcm_lib_free_pages,
941 .pointer = rsnd_pointer,
942};
943
944/*
945 * snd_soc_platform
946 */
947
948#define PREALLOC_BUFFER (32 * 1024)
949#define PREALLOC_BUFFER_MAX (32 * 1024)
950
951static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
952{
bff58ea4
KM
953 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
954 struct rsnd_dai *rdai;
955 int i, ret;
956
957 for_each_rsnd_dai(rdai, priv, i) {
958 ret = rsnd_dai_call(pcm_new, &rdai->playback, rdai, rtd);
959 if (ret)
960 return ret;
961
962 ret = rsnd_dai_call(pcm_new, &rdai->capture, rdai, rtd);
963 if (ret)
964 return ret;
965 }
966
1536a968
KM
967 return snd_pcm_lib_preallocate_pages_for_all(
968 rtd->pcm,
969 SNDRV_DMA_TYPE_DEV,
970 rtd->card->snd_card->dev,
971 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
972}
973
974static void rsnd_pcm_free(struct snd_pcm *pcm)
975{
976 snd_pcm_lib_preallocate_free_for_all(pcm);
977}
978
979static struct snd_soc_platform_driver rsnd_soc_platform = {
980 .ops = &rsnd_pcm_ops,
981 .pcm_new = rsnd_pcm_new,
982 .pcm_free = rsnd_pcm_free,
983};
984
985static const struct snd_soc_component_driver rsnd_soc_component = {
986 .name = "rsnd",
987};
988
989/*
990 * rsnd probe
991 */
992static int rsnd_probe(struct platform_device *pdev)
993{
994 struct rcar_snd_info *info;
995 struct rsnd_priv *priv;
996 struct device *dev = &pdev->dev;
7681f6ac 997 struct rsnd_dai *rdai;
90e8e50f
KM
998 const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
999 const struct rsnd_of_data *of_data;
d1ac970f 1000 int (*probe_func[])(struct platform_device *pdev,
90e8e50f 1001 const struct rsnd_of_data *of_data,
d1ac970f
KM
1002 struct rsnd_priv *priv) = {
1003 rsnd_gen_probe,
1004 rsnd_ssi_probe,
ba9c949f 1005 rsnd_src_probe,
bff58ea4 1006 rsnd_dvc_probe,
d1ac970f
KM
1007 rsnd_adg_probe,
1008 rsnd_dai_probe,
1009 };
1010 int ret, i;
1536a968 1011
90e8e50f
KM
1012 info = NULL;
1013 of_data = NULL;
1014 if (of_id) {
1015 info = devm_kzalloc(&pdev->dev,
1016 sizeof(struct rcar_snd_info), GFP_KERNEL);
1017 of_data = of_id->data;
1018 } else {
1019 info = pdev->dev.platform_data;
1020 }
1021
1536a968
KM
1022 if (!info) {
1023 dev_err(dev, "driver needs R-Car sound information\n");
1024 return -ENODEV;
1025 }
1026
1027 /*
1028 * init priv data
1029 */
1030 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1031 if (!priv) {
1032 dev_err(dev, "priv allocate failed\n");
1033 return -ENODEV;
1034 }
1035
9f464f8e 1036 priv->pdev = pdev;
1536a968
KM
1037 priv->info = info;
1038 spin_lock_init(&priv->lock);
1039
1040 /*
1041 * init each module
1042 */
d1ac970f 1043 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
90e8e50f 1044 ret = probe_func[i](pdev, of_data, priv);
d1ac970f
KM
1045 if (ret)
1046 return ret;
1047 }
07539c1d 1048
7681f6ac 1049 for_each_rsnd_dai(rdai, priv, i) {
68b6af36 1050 ret = rsnd_dai_call(probe, &rdai->playback, rdai);
7681f6ac
KM
1051 if (ret)
1052 return ret;
dfc9403b 1053
68b6af36 1054 ret = rsnd_dai_call(probe, &rdai->capture, rdai);
7681f6ac
KM
1055 if (ret)
1056 return ret;
1057 }
4b4dab82 1058
1536a968
KM
1059 /*
1060 * asoc register
1061 */
1062 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1063 if (ret < 0) {
1064 dev_err(dev, "cannot snd soc register\n");
1065 return ret;
1066 }
1067
1068 ret = snd_soc_register_component(dev, &rsnd_soc_component,
ecba9e72 1069 priv->daidrv, rsnd_rdai_nr(priv));
1536a968
KM
1070 if (ret < 0) {
1071 dev_err(dev, "cannot snd dai register\n");
1072 goto exit_snd_soc;
1073 }
1074
1075 dev_set_drvdata(dev, priv);
1076
1077 pm_runtime_enable(dev);
1078
1079 dev_info(dev, "probed\n");
1080 return ret;
1081
1082exit_snd_soc:
1083 snd_soc_unregister_platform(dev);
1084
1085 return ret;
1086}
1087
1088static int rsnd_remove(struct platform_device *pdev)
1089{
1090 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
7681f6ac
KM
1091 struct rsnd_dai *rdai;
1092 int ret, i;
1536a968
KM
1093
1094 pm_runtime_disable(&pdev->dev);
1095
7681f6ac 1096 for_each_rsnd_dai(rdai, priv, i) {
68b6af36 1097 ret = rsnd_dai_call(remove, &rdai->playback, rdai);
7681f6ac
KM
1098 if (ret)
1099 return ret;
1100
68b6af36 1101 ret = rsnd_dai_call(remove, &rdai->capture, rdai);
7681f6ac
KM
1102 if (ret)
1103 return ret;
1104 }
1536a968
KM
1105
1106 return 0;
1107}
1108
1109static struct platform_driver rsnd_driver = {
1110 .driver = {
1111 .name = "rcar_sound",
90e8e50f 1112 .of_match_table = rsnd_of_match,
1536a968
KM
1113 },
1114 .probe = rsnd_probe,
1115 .remove = rsnd_remove,
1116};
1117module_platform_driver(rsnd_driver);
1118
1119MODULE_LICENSE("GPL");
1120MODULE_DESCRIPTION("Renesas R-Car audio driver");
1121MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1122MODULE_ALIAS("platform:rcar-pcm-audio");
This page took 0.119177 seconds and 5 git commands to generate.