ASoC: add Renesas R-Car module feature
[deliverable/linux.git] / sound / soc / sh / rcar / core.c
CommitLineData
1536a968
KM
1/*
2 * Renesas R-Car SRU/SCU/SSIU/SSI support
3 *
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * Based on fsi.c
8 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15/*
16 * Renesas R-Car sound device structure
17 *
18 * Gen1
19 *
20 * SRU : Sound Routing Unit
21 * - SRC : Sampling Rate Converter
22 * - CMD
23 * - CTU : Channel Count Conversion Unit
24 * - MIX : Mixer
25 * - DVC : Digital Volume and Mute Function
26 * - SSI : Serial Sound Interface
27 *
28 * Gen2
29 *
30 * SCU : Sampling Rate Converter Unit
31 * - SRC : Sampling Rate Converter
32 * - CMD
33 * - CTU : Channel Count Conversion Unit
34 * - MIX : Mixer
35 * - DVC : Digital Volume and Mute Function
36 * SSIU : Serial Sound Interface Unit
37 * - SSI : Serial Sound Interface
38 */
39
40/*
41 * driver data Image
42 *
43 * rsnd_priv
44 * |
45 * | ** this depends on Gen1/Gen2
46 * |
47 * +- gen
48 * |
49 * | ** these depend on data path
50 * | ** gen and platform data control it
51 * |
52 * +- rdai[0]
53 * | | sru ssiu ssi
54 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
55 * | |
56 * | | sru ssiu ssi
57 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
58 * |
59 * +- rdai[1]
60 * | | sru ssiu ssi
61 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
62 * | |
63 * | | sru ssiu ssi
64 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
65 * ...
66 * |
67 * | ** these control ssi
68 * |
69 * +- ssi
70 * | |
71 * | +- ssi[0]
72 * | +- ssi[1]
73 * | +- ssi[2]
74 * | ...
75 * |
76 * | ** these control scu
77 * |
78 * +- scu
79 * |
80 * +- scu[0]
81 * +- scu[1]
82 * +- scu[2]
83 * ...
84 *
85 *
86 * for_each_rsnd_dai(xx, priv, xx)
87 * rdai[0] => rdai[1] => rdai[2] => ...
88 *
89 * for_each_rsnd_mod(xx, rdai, xx)
90 * [mod] => [mod] => [mod] => ...
91 *
92 * rsnd_dai_call(xxx, fn )
93 * [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
94 *
95 */
96#include <linux/pm_runtime.h>
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
102/*
103 * rsnd_platform functions
104 */
105#define rsnd_platform_call(priv, dai, func, param...) \
106 (!(priv->info->func) ? -ENODEV : \
107 priv->info->func(param))
108
109
cdaa3cdf
KM
110/*
111 * rsnd_mod functions
112 */
113char *rsnd_mod_name(struct rsnd_mod *mod)
114{
115 if (!mod || !mod->ops)
116 return "unknown";
117
118 return mod->ops->name;
119}
120
121void rsnd_mod_init(struct rsnd_priv *priv,
122 struct rsnd_mod *mod,
123 struct rsnd_mod_ops *ops,
124 int id)
125{
126 mod->priv = priv;
127 mod->id = id;
128 mod->ops = ops;
129 INIT_LIST_HEAD(&mod->list);
130}
131
1536a968
KM
132/*
133 * rsnd_dai functions
134 */
cdaa3cdf
KM
135#define rsnd_dai_call(rdai, io, fn) \
136({ \
137 struct rsnd_mod *mod, *n; \
138 int ret = 0; \
139 for_each_rsnd_mod(mod, n, io) { \
140 ret = rsnd_mod_call(mod, fn, rdai, io); \
141 if (ret < 0) \
142 break; \
143 } \
144 ret; \
145})
146
147int rsnd_dai_connect(struct rsnd_dai *rdai,
148 struct rsnd_mod *mod,
149 struct rsnd_dai_stream *io)
150{
151 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
152 struct device *dev = rsnd_priv_to_dev(priv);
153
154 if (!mod) {
155 dev_err(dev, "NULL mod\n");
156 return -EIO;
157 }
158
159 if (!list_empty(&mod->list)) {
160 dev_err(dev, "%s%d is not empty\n",
161 rsnd_mod_name(mod),
162 rsnd_mod_id(mod));
163 return -EIO;
164 }
165
166 list_add_tail(&mod->list, &io->head);
167
168 return 0;
169}
170
171int rsnd_dai_disconnect(struct rsnd_mod *mod)
172{
173 list_del_init(&mod->list);
174
175 return 0;
176}
177
1536a968
KM
178struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
179{
180 return priv->rdai + id;
181}
182
183static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
184{
185 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
186
187 return rsnd_dai_get(priv, dai->id);
188}
189
190int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
191{
192 return &rdai->playback == io;
193}
194
195/*
196 * rsnd_soc_dai functions
197 */
198int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
199{
200 struct snd_pcm_substream *substream = io->substream;
201 struct snd_pcm_runtime *runtime = substream->runtime;
202 int pos = io->byte_pos + additional;
203
204 pos %= (runtime->periods * io->byte_per_period);
205
206 return pos;
207}
208
209void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
210{
211 io->byte_pos += byte;
212
213 if (io->byte_pos >= io->next_period_byte) {
214 struct snd_pcm_substream *substream = io->substream;
215 struct snd_pcm_runtime *runtime = substream->runtime;
216
217 io->period_pos++;
218 io->next_period_byte += io->byte_per_period;
219
220 if (io->period_pos >= runtime->periods) {
221 io->byte_pos = 0;
222 io->period_pos = 0;
223 io->next_period_byte = io->byte_per_period;
224 }
225
226 snd_pcm_period_elapsed(substream);
227 }
228}
229
230static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
231 struct snd_pcm_substream *substream)
232{
233 struct snd_pcm_runtime *runtime = substream->runtime;
234
235 if (!list_empty(&io->head))
236 return -EIO;
237
238 INIT_LIST_HEAD(&io->head);
239 io->substream = substream;
240 io->byte_pos = 0;
241 io->period_pos = 0;
242 io->byte_per_period = runtime->period_size *
243 runtime->channels *
244 samples_to_bytes(runtime, 1);
245 io->next_period_byte = io->byte_per_period;
246
247 return 0;
248}
249
250static
251struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
252{
253 struct snd_soc_pcm_runtime *rtd = substream->private_data;
254
255 return rtd->cpu_dai;
256}
257
258static
259struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
260 struct snd_pcm_substream *substream)
261{
262 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
263 return &rdai->playback;
264 else
265 return &rdai->capture;
266}
267
268static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
269 struct snd_soc_dai *dai)
270{
271 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
272 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
273 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
274 struct rsnd_dai_platform_info *info = rsnd_dai_get_platform_info(rdai);
275 int ssi_id = rsnd_dai_is_play(rdai, io) ? info->ssi_id_playback :
276 info->ssi_id_capture;
277 int ret;
278 unsigned long flags;
279
280 rsnd_lock(priv, flags);
281
282 switch (cmd) {
283 case SNDRV_PCM_TRIGGER_START:
284 ret = rsnd_dai_stream_init(io, substream);
285 if (ret < 0)
286 goto dai_trigger_end;
287
288 ret = rsnd_platform_call(priv, dai, start, ssi_id);
289 if (ret < 0)
290 goto dai_trigger_end;
291
cdaa3cdf
KM
292 ret = rsnd_dai_call(rdai, io, init);
293 if (ret < 0)
294 goto dai_trigger_end;
295
296 ret = rsnd_dai_call(rdai, io, start);
297 if (ret < 0)
298 goto dai_trigger_end;
1536a968
KM
299 break;
300 case SNDRV_PCM_TRIGGER_STOP:
cdaa3cdf
KM
301 ret = rsnd_dai_call(rdai, io, stop);
302 if (ret < 0)
303 goto dai_trigger_end;
304
305 ret = rsnd_dai_call(rdai, io, quit);
306 if (ret < 0)
307 goto dai_trigger_end;
308
1536a968
KM
309 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
310 if (ret < 0)
311 goto dai_trigger_end;
312
313 break;
314 default:
315 ret = -EINVAL;
316 }
317
318dai_trigger_end:
319 rsnd_unlock(priv, flags);
320
321 return ret;
322}
323
324static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
325{
326 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
327
328 /* set master/slave audio interface */
329 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
330 case SND_SOC_DAIFMT_CBM_CFM:
331 rdai->clk_master = 1;
332 break;
333 case SND_SOC_DAIFMT_CBS_CFS:
334 rdai->clk_master = 0;
335 break;
336 default:
337 return -EINVAL;
338 }
339
340 /* set clock inversion */
341 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
342 case SND_SOC_DAIFMT_NB_IF:
343 rdai->bit_clk_inv = 0;
344 rdai->frm_clk_inv = 1;
345 break;
346 case SND_SOC_DAIFMT_IB_NF:
347 rdai->bit_clk_inv = 1;
348 rdai->frm_clk_inv = 0;
349 break;
350 case SND_SOC_DAIFMT_IB_IF:
351 rdai->bit_clk_inv = 1;
352 rdai->frm_clk_inv = 1;
353 break;
354 case SND_SOC_DAIFMT_NB_NF:
355 default:
356 rdai->bit_clk_inv = 0;
357 rdai->frm_clk_inv = 0;
358 break;
359 }
360
361 /* set format */
362 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
363 case SND_SOC_DAIFMT_I2S:
364 rdai->sys_delay = 0;
365 rdai->data_alignment = 0;
366 break;
367 case SND_SOC_DAIFMT_LEFT_J:
368 rdai->sys_delay = 1;
369 rdai->data_alignment = 0;
370 break;
371 case SND_SOC_DAIFMT_RIGHT_J:
372 rdai->sys_delay = 1;
373 rdai->data_alignment = 1;
374 break;
375 }
376
377 return 0;
378}
379
380static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
381 .trigger = rsnd_soc_dai_trigger,
382 .set_fmt = rsnd_soc_dai_set_fmt,
383};
384
385static int rsnd_dai_probe(struct platform_device *pdev,
386 struct rcar_snd_info *info,
387 struct rsnd_priv *priv)
388{
389 struct snd_soc_dai_driver *drv;
390 struct rsnd_dai *rdai;
391 struct device *dev = rsnd_priv_to_dev(priv);
392 struct rsnd_dai_platform_info *dai_info;
393 int dai_nr = info->dai_info_nr;
394 int i, pid, cid;
395
396 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
397 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
398 if (!drv || !rdai) {
399 dev_err(dev, "dai allocate failed\n");
400 return -ENOMEM;
401 }
402
403 for (i = 0; i < dai_nr; i++) {
404 dai_info = &info->dai_info[i];
405
406 pid = dai_info->ssi_id_playback;
407 cid = dai_info->ssi_id_capture;
408
409 /*
410 * init rsnd_dai
411 */
412 INIT_LIST_HEAD(&rdai[i].playback.head);
413 INIT_LIST_HEAD(&rdai[i].capture.head);
414
415 rdai[i].info = dai_info;
416
417 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
418
419 /*
420 * init snd_soc_dai_driver
421 */
422 drv[i].name = rdai[i].name;
423 drv[i].ops = &rsnd_soc_dai_ops;
424 if (pid >= 0) {
425 drv[i].playback.rates = RSND_RATES;
426 drv[i].playback.formats = RSND_FMTS;
427 drv[i].playback.channels_min = 2;
428 drv[i].playback.channels_max = 2;
429 }
430 if (cid >= 0) {
431 drv[i].capture.rates = RSND_RATES;
432 drv[i].capture.formats = RSND_FMTS;
433 drv[i].capture.channels_min = 2;
434 drv[i].capture.channels_max = 2;
435 }
436
437 dev_dbg(dev, "%s (%d, %d) probed", rdai[i].name, pid, cid);
438 }
439
440 priv->dai_nr = dai_nr;
441 priv->daidrv = drv;
442 priv->rdai = rdai;
443
444 return 0;
445}
446
447static void rsnd_dai_remove(struct platform_device *pdev,
448 struct rsnd_priv *priv)
449{
450}
451
452/*
453 * pcm ops
454 */
455static struct snd_pcm_hardware rsnd_pcm_hardware = {
456 .info = SNDRV_PCM_INFO_INTERLEAVED |
457 SNDRV_PCM_INFO_MMAP |
458 SNDRV_PCM_INFO_MMAP_VALID |
459 SNDRV_PCM_INFO_PAUSE,
460 .formats = RSND_FMTS,
461 .rates = RSND_RATES,
462 .rate_min = 8000,
463 .rate_max = 192000,
464 .channels_min = 2,
465 .channels_max = 2,
466 .buffer_bytes_max = 64 * 1024,
467 .period_bytes_min = 32,
468 .period_bytes_max = 8192,
469 .periods_min = 1,
470 .periods_max = 32,
471 .fifo_size = 256,
472};
473
474static int rsnd_pcm_open(struct snd_pcm_substream *substream)
475{
476 struct snd_pcm_runtime *runtime = substream->runtime;
477 int ret = 0;
478
479 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
480
481 ret = snd_pcm_hw_constraint_integer(runtime,
482 SNDRV_PCM_HW_PARAM_PERIODS);
483
484 return ret;
485}
486
487static int rsnd_hw_params(struct snd_pcm_substream *substream,
488 struct snd_pcm_hw_params *hw_params)
489{
490 return snd_pcm_lib_malloc_pages(substream,
491 params_buffer_bytes(hw_params));
492}
493
494static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
495{
496 struct snd_pcm_runtime *runtime = substream->runtime;
497 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
498 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
499 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
500
501 return bytes_to_frames(runtime, io->byte_pos);
502}
503
504static struct snd_pcm_ops rsnd_pcm_ops = {
505 .open = rsnd_pcm_open,
506 .ioctl = snd_pcm_lib_ioctl,
507 .hw_params = rsnd_hw_params,
508 .hw_free = snd_pcm_lib_free_pages,
509 .pointer = rsnd_pointer,
510};
511
512/*
513 * snd_soc_platform
514 */
515
516#define PREALLOC_BUFFER (32 * 1024)
517#define PREALLOC_BUFFER_MAX (32 * 1024)
518
519static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
520{
521 return snd_pcm_lib_preallocate_pages_for_all(
522 rtd->pcm,
523 SNDRV_DMA_TYPE_DEV,
524 rtd->card->snd_card->dev,
525 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
526}
527
528static void rsnd_pcm_free(struct snd_pcm *pcm)
529{
530 snd_pcm_lib_preallocate_free_for_all(pcm);
531}
532
533static struct snd_soc_platform_driver rsnd_soc_platform = {
534 .ops = &rsnd_pcm_ops,
535 .pcm_new = rsnd_pcm_new,
536 .pcm_free = rsnd_pcm_free,
537};
538
539static const struct snd_soc_component_driver rsnd_soc_component = {
540 .name = "rsnd",
541};
542
543/*
544 * rsnd probe
545 */
546static int rsnd_probe(struct platform_device *pdev)
547{
548 struct rcar_snd_info *info;
549 struct rsnd_priv *priv;
550 struct device *dev = &pdev->dev;
551 int ret;
552
553 info = pdev->dev.platform_data;
554 if (!info) {
555 dev_err(dev, "driver needs R-Car sound information\n");
556 return -ENODEV;
557 }
558
559 /*
560 * init priv data
561 */
562 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
563 if (!priv) {
564 dev_err(dev, "priv allocate failed\n");
565 return -ENODEV;
566 }
567
568 priv->dev = dev;
569 priv->info = info;
570 spin_lock_init(&priv->lock);
571
572 /*
573 * init each module
574 */
575 ret = rsnd_dai_probe(pdev, info, priv);
576 if (ret < 0)
577 return ret;
578
579 /*
580 * asoc register
581 */
582 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
583 if (ret < 0) {
584 dev_err(dev, "cannot snd soc register\n");
585 return ret;
586 }
587
588 ret = snd_soc_register_component(dev, &rsnd_soc_component,
589 priv->daidrv, rsnd_dai_nr(priv));
590 if (ret < 0) {
591 dev_err(dev, "cannot snd dai register\n");
592 goto exit_snd_soc;
593 }
594
595 dev_set_drvdata(dev, priv);
596
597 pm_runtime_enable(dev);
598
599 dev_info(dev, "probed\n");
600 return ret;
601
602exit_snd_soc:
603 snd_soc_unregister_platform(dev);
604
605 return ret;
606}
607
608static int rsnd_remove(struct platform_device *pdev)
609{
610 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
611
612 pm_runtime_disable(&pdev->dev);
613
614 /*
615 * remove each module
616 */
617 rsnd_dai_remove(pdev, priv);
618
619 return 0;
620}
621
622static struct platform_driver rsnd_driver = {
623 .driver = {
624 .name = "rcar_sound",
625 },
626 .probe = rsnd_probe,
627 .remove = rsnd_remove,
628};
629module_platform_driver(rsnd_driver);
630
631MODULE_LICENSE("GPL");
632MODULE_DESCRIPTION("Renesas R-Car audio driver");
633MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
634MODULE_ALIAS("platform:rcar-pcm-audio");
This page took 0.092529 seconds and 5 git commands to generate.