2 * siu_dai.c - ALSA SoC driver for Renesas SH7343, SH7722 SIU peripheral.
4 * Copyright (C) 2009-2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
5 * Copyright (C) 2006 Carlos Munoz <carlos@kenati.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/delay.h>
23 #include <linux/firmware.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
26 #include <linux/module.h>
28 #include <asm/clock.h>
31 #include <sound/control.h>
32 #include <sound/soc.h>
37 #if defined(CONFIG_CPU_SUBTYPE_SH7722)
38 # define SIU_MAX_VOLUME 0x1000
40 # define SIU_MAX_VOLUME 0x7fff
43 #define PRAM_SIZE 0x2000
44 #define XRAM_SIZE 0x800
45 #define YRAM_SIZE 0x800
47 #define XRAM_OFFSET 0x4000
48 #define YRAM_OFFSET 0x6000
49 #define REG_OFFSET 0xc000
51 #define PLAYBACK_ENABLED 1
52 #define CAPTURE_ENABLED 2
54 #define VOLUME_CAPTURE 0
55 #define VOLUME_PLAYBACK 1
56 #define DFLT_VOLUME_LEVEL 0x08000800
59 * SPDIF is only available on port A and on some SIU implementations it is only
60 * available for input. Due to the lack of hardware to test it, SPDIF is left
61 * disabled in this driver version
71 struct format_flag playback;
72 struct format_flag capture;
75 struct siu_info *siu_i2s_data;
77 static struct port_flag siu_flags[SIU_PORT_NUM] = {
82 .spdif = 0x80000000, /* not on all SIU versions */
96 .spdif = 0, /* impossible - turn off */
102 .spdif = 0, /* impossible - turn off */
108 static void siu_dai_start(struct siu_port *port_info)
110 struct siu_info *info = siu_i2s_data;
111 u32 __iomem *base = info->reg;
113 dev_dbg(port_info->pcm->card->dev, "%s\n", __func__);
115 /* Issue software reset to siu */
116 siu_write32(base + SIU_SRCTL, 0);
118 /* Wait for the reset to take effect */
121 port_info->stfifo = 0;
122 port_info->trdat = 0;
124 /* portA, portB, SIU operate */
125 siu_write32(base + SIU_SRCTL, 0x301);
127 /* portA=256fs, portB=256fs */
128 siu_write32(base + SIU_CKCTL, 0x40400000);
130 /* portA's BRG does not divide SIUCKA */
131 siu_write32(base + SIU_BRGASEL, 0);
132 siu_write32(base + SIU_BRRA, 0);
134 /* portB's BRG divides SIUCKB by half */
135 siu_write32(base + SIU_BRGBSEL, 1);
136 siu_write32(base + SIU_BRRB, 0);
138 siu_write32(base + SIU_IFCTL, 0x44440000);
140 /* portA: 32 bit/fs, master; portB: 32 bit/fs, master */
141 siu_write32(base + SIU_SFORM, 0x0c0c0000);
144 * Volume levels: looks like the DSP firmware implements volume controls
145 * differently from what's described in the datasheet
147 siu_write32(base + SIU_SBDVCA, port_info->playback.volume);
148 siu_write32(base + SIU_SBDVCB, port_info->capture.volume);
151 static void siu_dai_stop(struct siu_port *port_info)
153 struct siu_info *info = siu_i2s_data;
154 u32 __iomem *base = info->reg;
156 /* SIU software reset */
157 siu_write32(base + SIU_SRCTL, 0);
160 static void siu_dai_spbAselect(struct siu_port *port_info)
162 struct siu_info *info = siu_i2s_data;
163 struct siu_firmware *fw = &info->fw;
164 u32 *ydef = fw->yram0;
173 ydef[0] = (fw->spbpar[idx].ab1a << 16) |
174 (fw->spbpar[idx].ab0a << 8) |
175 (fw->spbpar[idx].dir << 7) | 3;
176 ydef[1] = fw->yram0[1]; /* 0x03000300 */
177 ydef[2] = (16 / 2) << 24;
178 ydef[3] = fw->yram0[3]; /* 0 */
179 ydef[4] = fw->yram0[4]; /* 0 */
180 ydef[7] = fw->spbpar[idx].event;
181 port_info->stfifo |= fw->spbpar[idx].stfifo;
182 port_info->trdat |= fw->spbpar[idx].trdat;
185 static void siu_dai_spbBselect(struct siu_port *port_info)
187 struct siu_info *info = siu_i2s_data;
188 struct siu_firmware *fw = &info->fw;
189 u32 *ydef = fw->yram0;
198 ydef[5] = (fw->spbpar[idx].ab1a << 16) |
199 (fw->spbpar[idx].ab0a << 8) | 1;
200 ydef[6] = fw->spbpar[idx].event;
201 port_info->stfifo |= fw->spbpar[idx].stfifo;
202 port_info->trdat |= fw->spbpar[idx].trdat;
205 static void siu_dai_open(struct siu_stream *siu_stream)
207 struct siu_info *info = siu_i2s_data;
208 u32 __iomem *base = info->reg;
211 srctl = siu_read32(base + SIU_SRCTL);
212 ifctl = siu_read32(base + SIU_IFCTL);
214 switch (info->port_id) {
227 siu_write32(base + SIU_SRCTL, srctl);
228 /* Unmute and configure portA */
229 siu_write32(base + SIU_IFCTL, ifctl);
233 * At the moment only fixed Left-upper, Left-lower, Right-upper, Right-lower
234 * packing is supported
236 static void siu_dai_pcmdatapack(struct siu_stream *siu_stream)
238 struct siu_info *info = siu_i2s_data;
239 u32 __iomem *base = info->reg;
242 dpak = siu_read32(base + SIU_DPAK);
244 switch (info->port_id) {
253 siu_write32(base + SIU_DPAK, dpak);
256 static int siu_dai_spbstart(struct siu_port *port_info)
258 struct siu_info *info = siu_i2s_data;
259 u32 __iomem *base = info->reg;
260 struct siu_firmware *fw = &info->fw;
261 u32 *ydef = fw->yram0;
266 /* Load SPB Program in PRAM */
269 for (cnt = 0; cnt < PRAM0_SIZE; cnt++, add++, ptr++)
270 siu_write32(add, *ptr);
273 add = info->pram + (0x0100 / sizeof(u32));
274 for (cnt = 0; cnt < PRAM1_SIZE; cnt++, add++, ptr++)
275 siu_write32(add, *ptr);
277 /* XRAM initialization */
279 for (cnt = 0; cnt < XRAM0_SIZE + XRAM1_SIZE + XRAM2_SIZE; cnt++, add++)
282 /* YRAM variable area initialization */
284 for (cnt = 0; cnt < YRAM_DEF_SIZE; cnt++, add++)
285 siu_write32(add, ydef[cnt]);
287 /* YRAM FIR coefficient area initialization */
288 add = info->yram + (0x0200 / sizeof(u32));
289 for (cnt = 0; cnt < YRAM_FIR_SIZE; cnt++, add++)
290 siu_write32(add, fw->yram_fir_coeff[cnt]);
292 /* YRAM IIR coefficient area initialization */
293 add = info->yram + (0x0600 / sizeof(u32));
294 for (cnt = 0; cnt < YRAM_IIR_SIZE; cnt++, add++)
297 siu_write32(base + SIU_TRDAT, port_info->trdat);
298 port_info->trdat = 0x0;
301 /* SPB start condition: software */
302 siu_write32(base + SIU_SBACTIV, 0);
304 siu_write32(base + SIU_SBCTL, 0xc0000000);
305 /* Wait for program to halt */
307 while (--cnt && siu_read32(base + SIU_SBCTL) != 0x80000000)
313 /* SPB program start address setting */
314 siu_write32(base + SIU_SBPSET, 0x00400000);
315 /* SPB hardware start(FIFOCTL source) */
316 siu_write32(base + SIU_SBACTIV, 0xc0000000);
321 static void siu_dai_spbstop(struct siu_port *port_info)
323 struct siu_info *info = siu_i2s_data;
324 u32 __iomem *base = info->reg;
326 siu_write32(base + SIU_SBACTIV, 0);
328 siu_write32(base + SIU_SBCTL, 0);
330 port_info->stfifo = 0;
335 /* Playback and capture hardware properties are identical */
336 static struct snd_pcm_hardware siu_dai_pcm_hw = {
337 .info = SNDRV_PCM_INFO_INTERLEAVED,
338 .formats = SNDRV_PCM_FMTBIT_S16,
339 .rates = SNDRV_PCM_RATE_8000_48000,
344 .buffer_bytes_max = SIU_BUFFER_BYTES_MAX,
345 .period_bytes_min = SIU_PERIOD_BYTES_MIN,
346 .period_bytes_max = SIU_PERIOD_BYTES_MAX,
347 .periods_min = SIU_PERIODS_MIN,
348 .periods_max = SIU_PERIODS_MAX,
351 static int siu_dai_info_volume(struct snd_kcontrol *kctrl,
352 struct snd_ctl_elem_info *uinfo)
354 struct siu_port *port_info = snd_kcontrol_chip(kctrl);
356 dev_dbg(port_info->pcm->card->dev, "%s\n", __func__);
358 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
360 uinfo->value.integer.min = 0;
361 uinfo->value.integer.max = SIU_MAX_VOLUME;
366 static int siu_dai_get_volume(struct snd_kcontrol *kctrl,
367 struct snd_ctl_elem_value *ucontrol)
369 struct siu_port *port_info = snd_kcontrol_chip(kctrl);
370 struct device *dev = port_info->pcm->card->dev;
373 dev_dbg(dev, "%s\n", __func__);
375 switch (kctrl->private_value) {
376 case VOLUME_PLAYBACK:
377 /* Playback is always on port 0 */
378 vol = port_info->playback.volume;
379 ucontrol->value.integer.value[0] = vol & 0xffff;
380 ucontrol->value.integer.value[1] = vol >> 16 & 0xffff;
383 /* Capture is always on port 1 */
384 vol = port_info->capture.volume;
385 ucontrol->value.integer.value[0] = vol & 0xffff;
386 ucontrol->value.integer.value[1] = vol >> 16 & 0xffff;
389 dev_err(dev, "%s() invalid private_value=%ld\n",
390 __func__, kctrl->private_value);
397 static int siu_dai_put_volume(struct snd_kcontrol *kctrl,
398 struct snd_ctl_elem_value *ucontrol)
400 struct siu_port *port_info = snd_kcontrol_chip(kctrl);
401 struct device *dev = port_info->pcm->card->dev;
402 struct siu_info *info = siu_i2s_data;
403 u32 __iomem *base = info->reg;
407 dev_dbg(dev, "%s\n", __func__);
409 if (ucontrol->value.integer.value[0] < 0 ||
410 ucontrol->value.integer.value[0] > SIU_MAX_VOLUME ||
411 ucontrol->value.integer.value[1] < 0 ||
412 ucontrol->value.integer.value[1] > SIU_MAX_VOLUME)
415 new_vol = ucontrol->value.integer.value[0] |
416 ucontrol->value.integer.value[1] << 16;
418 /* See comment above - DSP firmware implementation */
419 switch (kctrl->private_value) {
420 case VOLUME_PLAYBACK:
421 /* Playback is always on port 0 */
422 cur_vol = port_info->playback.volume;
423 siu_write32(base + SIU_SBDVCA, new_vol);
424 port_info->playback.volume = new_vol;
427 /* Capture is always on port 1 */
428 cur_vol = port_info->capture.volume;
429 siu_write32(base + SIU_SBDVCB, new_vol);
430 port_info->capture.volume = new_vol;
433 dev_err(dev, "%s() invalid private_value=%ld\n",
434 __func__, kctrl->private_value);
438 if (cur_vol != new_vol)
444 static struct snd_kcontrol_new playback_controls = {
445 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
446 .name = "PCM Playback Volume",
448 .info = siu_dai_info_volume,
449 .get = siu_dai_get_volume,
450 .put = siu_dai_put_volume,
451 .private_value = VOLUME_PLAYBACK,
454 static struct snd_kcontrol_new capture_controls = {
455 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
456 .name = "PCM Capture Volume",
458 .info = siu_dai_info_volume,
459 .get = siu_dai_get_volume,
460 .put = siu_dai_put_volume,
461 .private_value = VOLUME_CAPTURE,
464 int siu_init_port(int port, struct siu_port **port_info, struct snd_card *card)
466 struct device *dev = card->dev;
467 struct snd_kcontrol *kctrl;
470 *port_info = kzalloc(sizeof(**port_info), GFP_KERNEL);
474 dev_dbg(dev, "%s: port #%d@%p\n", __func__, port, *port_info);
476 (*port_info)->playback.volume = DFLT_VOLUME_LEVEL;
477 (*port_info)->capture.volume = DFLT_VOLUME_LEVEL;
480 * Add mixer support. The SPB is used to change the volume. Both
481 * ports use the same SPB. Therefore, we only register one
482 * control instance since it will be used by both channels.
483 * In error case we continue without controls.
485 kctrl = snd_ctl_new1(&playback_controls, *port_info);
486 ret = snd_ctl_add(card, kctrl);
489 "failed to add playback controls %p port=%d err=%d\n",
492 kctrl = snd_ctl_new1(&capture_controls, *port_info);
493 ret = snd_ctl_add(card, kctrl);
496 "failed to add capture controls %p port=%d err=%d\n",
502 void siu_free_port(struct siu_port *port_info)
507 static int siu_dai_startup(struct snd_pcm_substream *substream,
508 struct snd_soc_dai *dai)
510 struct siu_info *info = snd_soc_dai_get_drvdata(dai);
511 struct snd_pcm_runtime *rt = substream->runtime;
512 struct siu_port *port_info = siu_port_info(substream);
515 dev_dbg(substream->pcm->card->dev, "%s: port=%d@%p\n", __func__,
516 info->port_id, port_info);
518 snd_soc_set_runtime_hwparams(substream, &siu_dai_pcm_hw);
520 ret = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
521 if (unlikely(ret < 0))
524 siu_dai_start(port_info);
529 static void siu_dai_shutdown(struct snd_pcm_substream *substream,
530 struct snd_soc_dai *dai)
532 struct siu_info *info = snd_soc_dai_get_drvdata(dai);
533 struct siu_port *port_info = siu_port_info(substream);
535 dev_dbg(substream->pcm->card->dev, "%s: port=%d@%p\n", __func__,
536 info->port_id, port_info);
538 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
539 port_info->play_cap &= ~PLAYBACK_ENABLED;
541 port_info->play_cap &= ~CAPTURE_ENABLED;
543 /* Stop the siu if the other stream is not using it */
544 if (!port_info->play_cap) {
545 /* during stmread or stmwrite ? */
546 BUG_ON(port_info->playback.rw_flg || port_info->capture.rw_flg);
547 siu_dai_spbstop(port_info);
548 siu_dai_stop(port_info);
552 /* PCM part of siu_dai_playback_prepare() / siu_dai_capture_prepare() */
553 static int siu_dai_prepare(struct snd_pcm_substream *substream,
554 struct snd_soc_dai *dai)
556 struct siu_info *info = snd_soc_dai_get_drvdata(dai);
557 struct snd_pcm_runtime *rt = substream->runtime;
558 struct siu_port *port_info = siu_port_info(substream);
559 struct siu_stream *siu_stream;
562 dev_dbg(substream->pcm->card->dev,
563 "%s: port %d, active streams %lx, %d channels\n",
564 __func__, info->port_id, port_info->play_cap, rt->channels);
566 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
567 self = PLAYBACK_ENABLED;
568 siu_stream = &port_info->playback;
570 self = CAPTURE_ENABLED;
571 siu_stream = &port_info->capture;
574 /* Set up the siu if not already done */
575 if (!port_info->play_cap) {
576 siu_stream->rw_flg = 0; /* stream-data transfer flag */
578 siu_dai_spbAselect(port_info);
579 siu_dai_spbBselect(port_info);
581 siu_dai_open(siu_stream);
583 siu_dai_pcmdatapack(siu_stream);
585 ret = siu_dai_spbstart(port_info);
592 port_info->play_cap |= self;
599 * SIU can set bus format to I2S / PCM / SPDIF independently for playback and
600 * capture, however, the current API sets the bus format globally for a DAI.
602 static int siu_dai_set_fmt(struct snd_soc_dai *dai,
605 struct siu_info *info = snd_soc_dai_get_drvdata(dai);
606 u32 __iomem *base = info->reg;
609 dev_dbg(dai->dev, "%s: fmt 0x%x on port %d\n",
610 __func__, fmt, info->port_id);
612 if (info->port_id < 0)
615 /* Here select between I2S / PCM / SPDIF */
616 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
617 case SND_SOC_DAIFMT_I2S:
618 ifctl = siu_flags[info->port_id].playback.i2s |
619 siu_flags[info->port_id].capture.i2s;
621 case SND_SOC_DAIFMT_LEFT_J:
622 ifctl = siu_flags[info->port_id].playback.pcm |
623 siu_flags[info->port_id].capture.pcm;
625 /* SPDIF disabled - see comment at the top */
630 ifctl |= ~(siu_flags[info->port_id].playback.mask |
631 siu_flags[info->port_id].capture.mask) &
632 siu_read32(base + SIU_IFCTL);
633 siu_write32(base + SIU_IFCTL, ifctl);
638 static int siu_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
639 unsigned int freq, int dir)
641 struct clk *siu_clk, *parent_clk;
642 char *siu_name, *parent_name;
645 if (dir != SND_SOC_CLOCK_IN)
648 dev_dbg(dai->dev, "%s: using clock %d\n", __func__, clk_id);
652 siu_name = "siua_clk";
653 parent_name = "pll_clk";
656 siu_name = "siua_clk";
657 parent_name = "siumcka_clk";
660 siu_name = "siub_clk";
661 parent_name = "pll_clk";
664 siu_name = "siub_clk";
665 parent_name = "siumckb_clk";
671 siu_clk = clk_get(dai->dev, siu_name);
672 if (IS_ERR(siu_clk)) {
673 dev_err(dai->dev, "%s: cannot get a SIU clock: %ld\n", __func__,
675 return PTR_ERR(siu_clk);
678 parent_clk = clk_get(dai->dev, parent_name);
679 if (IS_ERR(parent_clk)) {
680 ret = PTR_ERR(parent_clk);
681 dev_err(dai->dev, "cannot get a SIU clock parent: %d\n", ret);
685 ret = clk_set_parent(siu_clk, parent_clk);
687 dev_err(dai->dev, "cannot reparent the SIU clock: %d\n", ret);
691 ret = clk_set_rate(siu_clk, freq);
693 dev_err(dai->dev, "cannot set SIU clock rate: %d\n", ret);
695 /* TODO: when clkdev gets reference counting we'll move these to siu_dai_shutdown() */
704 static const struct snd_soc_dai_ops siu_dai_ops = {
705 .startup = siu_dai_startup,
706 .shutdown = siu_dai_shutdown,
707 .prepare = siu_dai_prepare,
708 .set_sysclk = siu_dai_set_sysclk,
709 .set_fmt = siu_dai_set_fmt,
712 static struct snd_soc_dai_driver siu_i2s_dai = {
713 .name = "siu-i2s-dai",
717 .formats = SNDRV_PCM_FMTBIT_S16,
718 .rates = SNDRV_PCM_RATE_8000_48000,
723 .formats = SNDRV_PCM_FMTBIT_S16,
724 .rates = SNDRV_PCM_RATE_8000_48000,
729 static int siu_probe(struct platform_device *pdev)
731 const struct firmware *fw_entry;
732 struct resource *res, *region;
733 struct siu_info *info;
736 info = kmalloc(sizeof(*info), GFP_KERNEL);
740 info->dev = &pdev->dev;
742 ret = request_firmware(&fw_entry, "siu_spb.bin", &pdev->dev);
747 * Loaded firmware is "const" - read only, but we have to modify it in
748 * snd_siu_sh7343_spbAselect() and snd_siu_sh7343_spbBselect()
750 memcpy(&info->fw, fw_entry->data, fw_entry->size);
752 release_firmware(fw_entry);
754 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
760 region = request_mem_region(res->start, resource_size(res),
763 dev_err(&pdev->dev, "SIU region already claimed\n");
769 info->pram = ioremap(res->start, PRAM_SIZE);
772 info->xram = ioremap(res->start + XRAM_OFFSET, XRAM_SIZE);
775 info->yram = ioremap(res->start + YRAM_OFFSET, YRAM_SIZE);
778 info->reg = ioremap(res->start + REG_OFFSET, resource_size(res) -
783 dev_set_drvdata(&pdev->dev, info);
785 /* register using ARRAY version so we can keep dai name */
786 ret = snd_soc_register_dais(&pdev->dev, &siu_i2s_dai, 1);
790 ret = snd_soc_register_platform(&pdev->dev, &siu_platform);
794 pm_runtime_enable(&pdev->dev);
799 snd_soc_unregister_dai(&pdev->dev);
809 release_mem_region(res->start, resource_size(res));
818 static int siu_remove(struct platform_device *pdev)
820 struct siu_info *info = dev_get_drvdata(&pdev->dev);
821 struct resource *res;
823 pm_runtime_disable(&pdev->dev);
825 snd_soc_unregister_platform(&pdev->dev);
826 snd_soc_unregister_dai(&pdev->dev);
832 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
834 release_mem_region(res->start, resource_size(res));
840 static struct platform_driver siu_driver = {
842 .owner = THIS_MODULE,
843 .name = "siu-pcm-audio",
846 .remove = siu_remove,
849 module_platform_driver(siu_driver);
851 MODULE_AUTHOR("Carlos Munoz <carlos@kenati.com>");
852 MODULE_DESCRIPTION("ALSA SoC SH7722 SIU driver");
853 MODULE_LICENSE("GPL");