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