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