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