2 * Renesas R-Car SRU/SCU/SSIU/SSI support
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
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.
16 * Renesas R-Car sound device structure
20 * SRU : Sound Routing Unit
21 * - SRC : Sampling Rate Converter
23 * - CTU : Channel Count Conversion Unit
25 * - DVC : Digital Volume and Mute Function
26 * - SSI : Serial Sound Interface
30 * SCU : Sampling Rate Converter Unit
31 * - SRC : Sampling Rate Converter
33 * - CTU : Channel Count Conversion Unit
35 * - DVC : Digital Volume and Mute Function
36 * SSIU : Serial Sound Interface Unit
37 * - SSI : Serial Sound Interface
45 * | ** this depends on Gen1/Gen2
49 * | ** these depend on data path
50 * | ** gen and platform data control it
54 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
57 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
61 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
64 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
67 * | ** these control ssi
76 * | ** these control scu
86 * for_each_rsnd_dai(xx, priv, xx)
87 * rdai[0] => rdai[1] => rdai[2] => ...
89 * for_each_rsnd_mod(xx, rdai, xx)
90 * [mod] => [mod] => [mod] => ...
92 * rsnd_dai_call(xxx, fn )
93 * [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
96 #include <linux/pm_runtime.h>
99 #define RSND_RATES SNDRV_PCM_RATE_8000_96000
100 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
103 * rsnd_platform functions
105 #define rsnd_platform_call(priv, dai, func, param...) \
106 (!(priv->info->func) ? -ENODEV : \
107 priv->info->func(param))
113 u32 rsnd_read(struct rsnd_priv *priv,
114 struct rsnd_mod *mod, enum rsnd_reg reg)
116 void __iomem *base = rsnd_gen_reg_get(priv, mod, reg);
120 return ioread32(base);
123 void rsnd_write(struct rsnd_priv *priv,
124 struct rsnd_mod *mod,
125 enum rsnd_reg reg, u32 data)
127 void __iomem *base = rsnd_gen_reg_get(priv, mod, reg);
128 struct device *dev = rsnd_priv_to_dev(priv);
132 dev_dbg(dev, "w %p : %08x\n", base, data);
134 iowrite32(data, base);
137 void rsnd_bset(struct rsnd_priv *priv, struct rsnd_mod *mod,
138 enum rsnd_reg reg, u32 mask, u32 data)
140 void __iomem *base = rsnd_gen_reg_get(priv, mod, reg);
141 struct device *dev = rsnd_priv_to_dev(priv);
146 val = ioread32(base);
149 iowrite32(val, base);
151 dev_dbg(dev, "s %p : %08x\n", base, val);
157 char *rsnd_mod_name(struct rsnd_mod *mod)
159 if (!mod || !mod->ops)
162 return mod->ops->name;
165 void rsnd_mod_init(struct rsnd_priv *priv,
166 struct rsnd_mod *mod,
167 struct rsnd_mod_ops *ops,
173 INIT_LIST_HEAD(&mod->list);
179 #define rsnd_dai_call(rdai, io, fn) \
181 struct rsnd_mod *mod, *n; \
183 for_each_rsnd_mod(mod, n, io) { \
184 ret = rsnd_mod_call(mod, fn, rdai, io); \
191 int rsnd_dai_connect(struct rsnd_dai *rdai,
192 struct rsnd_mod *mod,
193 struct rsnd_dai_stream *io)
195 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
196 struct device *dev = rsnd_priv_to_dev(priv);
199 dev_err(dev, "NULL mod\n");
203 if (!list_empty(&mod->list)) {
204 dev_err(dev, "%s%d is not empty\n",
210 list_add_tail(&mod->list, &io->head);
215 int rsnd_dai_disconnect(struct rsnd_mod *mod)
217 list_del_init(&mod->list);
222 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
224 return priv->rdai + id;
227 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
229 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
231 return rsnd_dai_get(priv, dai->id);
234 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
236 return &rdai->playback == io;
240 * rsnd_soc_dai functions
242 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
244 struct snd_pcm_substream *substream = io->substream;
245 struct snd_pcm_runtime *runtime = substream->runtime;
246 int pos = io->byte_pos + additional;
248 pos %= (runtime->periods * io->byte_per_period);
253 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
255 io->byte_pos += byte;
257 if (io->byte_pos >= io->next_period_byte) {
258 struct snd_pcm_substream *substream = io->substream;
259 struct snd_pcm_runtime *runtime = substream->runtime;
262 io->next_period_byte += io->byte_per_period;
264 if (io->period_pos >= runtime->periods) {
267 io->next_period_byte = io->byte_per_period;
270 snd_pcm_period_elapsed(substream);
274 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
275 struct snd_pcm_substream *substream)
277 struct snd_pcm_runtime *runtime = substream->runtime;
279 if (!list_empty(&io->head))
282 INIT_LIST_HEAD(&io->head);
283 io->substream = substream;
286 io->byte_per_period = runtime->period_size *
288 samples_to_bytes(runtime, 1);
289 io->next_period_byte = io->byte_per_period;
295 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
297 struct snd_soc_pcm_runtime *rtd = substream->private_data;
303 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
304 struct snd_pcm_substream *substream)
306 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
307 return &rdai->playback;
309 return &rdai->capture;
312 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
313 struct snd_soc_dai *dai)
315 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
316 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
317 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
318 struct rsnd_dai_platform_info *info = rsnd_dai_get_platform_info(rdai);
319 int ssi_id = rsnd_dai_is_play(rdai, io) ? info->ssi_id_playback :
320 info->ssi_id_capture;
324 rsnd_lock(priv, flags);
327 case SNDRV_PCM_TRIGGER_START:
328 ret = rsnd_dai_stream_init(io, substream);
330 goto dai_trigger_end;
332 ret = rsnd_platform_call(priv, dai, start, ssi_id);
334 goto dai_trigger_end;
336 ret = rsnd_gen_path_init(priv, rdai, io);
338 goto dai_trigger_end;
340 ret = rsnd_dai_call(rdai, io, init);
342 goto dai_trigger_end;
344 ret = rsnd_dai_call(rdai, io, start);
346 goto dai_trigger_end;
348 case SNDRV_PCM_TRIGGER_STOP:
349 ret = rsnd_dai_call(rdai, io, stop);
351 goto dai_trigger_end;
353 ret = rsnd_dai_call(rdai, io, quit);
355 goto dai_trigger_end;
357 ret = rsnd_gen_path_exit(priv, rdai, io);
359 goto dai_trigger_end;
361 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
363 goto dai_trigger_end;
370 rsnd_unlock(priv, flags);
375 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
377 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
379 /* set master/slave audio interface */
380 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
381 case SND_SOC_DAIFMT_CBM_CFM:
382 rdai->clk_master = 1;
384 case SND_SOC_DAIFMT_CBS_CFS:
385 rdai->clk_master = 0;
391 /* set clock inversion */
392 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
393 case SND_SOC_DAIFMT_NB_IF:
394 rdai->bit_clk_inv = 0;
395 rdai->frm_clk_inv = 1;
397 case SND_SOC_DAIFMT_IB_NF:
398 rdai->bit_clk_inv = 1;
399 rdai->frm_clk_inv = 0;
401 case SND_SOC_DAIFMT_IB_IF:
402 rdai->bit_clk_inv = 1;
403 rdai->frm_clk_inv = 1;
405 case SND_SOC_DAIFMT_NB_NF:
407 rdai->bit_clk_inv = 0;
408 rdai->frm_clk_inv = 0;
413 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
414 case SND_SOC_DAIFMT_I2S:
416 rdai->data_alignment = 0;
418 case SND_SOC_DAIFMT_LEFT_J:
420 rdai->data_alignment = 0;
422 case SND_SOC_DAIFMT_RIGHT_J:
424 rdai->data_alignment = 1;
431 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
432 .trigger = rsnd_soc_dai_trigger,
433 .set_fmt = rsnd_soc_dai_set_fmt,
436 static int rsnd_dai_probe(struct platform_device *pdev,
437 struct rcar_snd_info *info,
438 struct rsnd_priv *priv)
440 struct snd_soc_dai_driver *drv;
441 struct rsnd_dai *rdai;
442 struct device *dev = rsnd_priv_to_dev(priv);
443 struct rsnd_dai_platform_info *dai_info;
444 int dai_nr = info->dai_info_nr;
447 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
448 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
450 dev_err(dev, "dai allocate failed\n");
454 for (i = 0; i < dai_nr; i++) {
455 dai_info = &info->dai_info[i];
457 pid = dai_info->ssi_id_playback;
458 cid = dai_info->ssi_id_capture;
463 INIT_LIST_HEAD(&rdai[i].playback.head);
464 INIT_LIST_HEAD(&rdai[i].capture.head);
466 rdai[i].info = dai_info;
468 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
471 * init snd_soc_dai_driver
473 drv[i].name = rdai[i].name;
474 drv[i].ops = &rsnd_soc_dai_ops;
476 drv[i].playback.rates = RSND_RATES;
477 drv[i].playback.formats = RSND_FMTS;
478 drv[i].playback.channels_min = 2;
479 drv[i].playback.channels_max = 2;
482 drv[i].capture.rates = RSND_RATES;
483 drv[i].capture.formats = RSND_FMTS;
484 drv[i].capture.channels_min = 2;
485 drv[i].capture.channels_max = 2;
488 dev_dbg(dev, "%s (%d, %d) probed", rdai[i].name, pid, cid);
491 priv->dai_nr = dai_nr;
498 static void rsnd_dai_remove(struct platform_device *pdev,
499 struct rsnd_priv *priv)
506 static struct snd_pcm_hardware rsnd_pcm_hardware = {
507 .info = SNDRV_PCM_INFO_INTERLEAVED |
508 SNDRV_PCM_INFO_MMAP |
509 SNDRV_PCM_INFO_MMAP_VALID |
510 SNDRV_PCM_INFO_PAUSE,
511 .formats = RSND_FMTS,
517 .buffer_bytes_max = 64 * 1024,
518 .period_bytes_min = 32,
519 .period_bytes_max = 8192,
525 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
527 struct snd_pcm_runtime *runtime = substream->runtime;
530 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
532 ret = snd_pcm_hw_constraint_integer(runtime,
533 SNDRV_PCM_HW_PARAM_PERIODS);
538 static int rsnd_hw_params(struct snd_pcm_substream *substream,
539 struct snd_pcm_hw_params *hw_params)
541 return snd_pcm_lib_malloc_pages(substream,
542 params_buffer_bytes(hw_params));
545 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
547 struct snd_pcm_runtime *runtime = substream->runtime;
548 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
549 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
550 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
552 return bytes_to_frames(runtime, io->byte_pos);
555 static struct snd_pcm_ops rsnd_pcm_ops = {
556 .open = rsnd_pcm_open,
557 .ioctl = snd_pcm_lib_ioctl,
558 .hw_params = rsnd_hw_params,
559 .hw_free = snd_pcm_lib_free_pages,
560 .pointer = rsnd_pointer,
567 #define PREALLOC_BUFFER (32 * 1024)
568 #define PREALLOC_BUFFER_MAX (32 * 1024)
570 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
572 return snd_pcm_lib_preallocate_pages_for_all(
575 rtd->card->snd_card->dev,
576 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
579 static void rsnd_pcm_free(struct snd_pcm *pcm)
581 snd_pcm_lib_preallocate_free_for_all(pcm);
584 static struct snd_soc_platform_driver rsnd_soc_platform = {
585 .ops = &rsnd_pcm_ops,
586 .pcm_new = rsnd_pcm_new,
587 .pcm_free = rsnd_pcm_free,
590 static const struct snd_soc_component_driver rsnd_soc_component = {
597 static int rsnd_probe(struct platform_device *pdev)
599 struct rcar_snd_info *info;
600 struct rsnd_priv *priv;
601 struct device *dev = &pdev->dev;
604 info = pdev->dev.platform_data;
606 dev_err(dev, "driver needs R-Car sound information\n");
613 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
615 dev_err(dev, "priv allocate failed\n");
621 spin_lock_init(&priv->lock);
626 ret = rsnd_gen_probe(pdev, info, priv);
630 ret = rsnd_dai_probe(pdev, info, priv);
634 ret = rsnd_scu_probe(pdev, info, priv);
641 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
643 dev_err(dev, "cannot snd soc register\n");
647 ret = snd_soc_register_component(dev, &rsnd_soc_component,
648 priv->daidrv, rsnd_dai_nr(priv));
650 dev_err(dev, "cannot snd dai register\n");
654 dev_set_drvdata(dev, priv);
656 pm_runtime_enable(dev);
658 dev_info(dev, "probed\n");
662 snd_soc_unregister_platform(dev);
667 static int rsnd_remove(struct platform_device *pdev)
669 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
671 pm_runtime_disable(&pdev->dev);
676 rsnd_scu_remove(pdev, priv);
677 rsnd_dai_remove(pdev, priv);
678 rsnd_gen_remove(pdev, priv);
683 static struct platform_driver rsnd_driver = {
685 .name = "rcar_sound",
688 .remove = rsnd_remove,
690 module_platform_driver(rsnd_driver);
692 MODULE_LICENSE("GPL");
693 MODULE_DESCRIPTION("Renesas R-Car audio driver");
694 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
695 MODULE_ALIAS("platform:rcar-pcm-audio");