1 // SPDX-License-Identifier: GPL-2.0+
3 // siu_dai.c - ALSA SoC driver for Renesas SH7343, SH7722 SIU peripheral.
5 // Copyright (C) 2009-2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6 // Copyright (C) 2006 Carlos Munoz <carlos@kenati.com>
8 #include <linux/delay.h>
9 #include <linux/firmware.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
14 #include <asm/clock.h>
17 #include <sound/control.h>
18 #include <sound/soc.h>
23 #if defined(CONFIG_CPU_SUBTYPE_SH7722)
24 # define SIU_MAX_VOLUME 0x1000
26 # define SIU_MAX_VOLUME 0x7fff
29 #define PRAM_SIZE 0x2000
30 #define XRAM_SIZE 0x800
31 #define YRAM_SIZE 0x800
33 #define XRAM_OFFSET 0x4000
34 #define YRAM_OFFSET 0x6000
35 #define REG_OFFSET 0xc000
37 #define PLAYBACK_ENABLED 1
38 #define CAPTURE_ENABLED 2
40 #define VOLUME_CAPTURE 0
41 #define VOLUME_PLAYBACK 1
42 #define DFLT_VOLUME_LEVEL 0x08000800
45 * SPDIF is only available on port A and on some SIU implementations it is only
46 * available for input. Due to the lack of hardware to test it, SPDIF is left
47 * disabled in this driver version
57 struct format_flag playback
;
58 struct format_flag capture
;
61 struct siu_info
*siu_i2s_data
;
63 static struct port_flag siu_flags
[SIU_PORT_NUM
] = {
68 .spdif
= 0x80000000, /* not on all SIU versions */
82 .spdif
= 0, /* impossible - turn off */
88 .spdif
= 0, /* impossible - turn off */
94 static void siu_dai_start(struct siu_port
*port_info
)
96 struct siu_info
*info
= siu_i2s_data
;
97 u32 __iomem
*base
= info
->reg
;
99 dev_dbg(port_info
->pcm
->card
->dev
, "%s\n", __func__
);
101 /* Issue software reset to siu */
102 siu_write32(base
+ SIU_SRCTL
, 0);
104 /* Wait for the reset to take effect */
107 port_info
->stfifo
= 0;
108 port_info
->trdat
= 0;
110 /* portA, portB, SIU operate */
111 siu_write32(base
+ SIU_SRCTL
, 0x301);
113 /* portA=256fs, portB=256fs */
114 siu_write32(base
+ SIU_CKCTL
, 0x40400000);
116 /* portA's BRG does not divide SIUCKA */
117 siu_write32(base
+ SIU_BRGASEL
, 0);
118 siu_write32(base
+ SIU_BRRA
, 0);
120 /* portB's BRG divides SIUCKB by half */
121 siu_write32(base
+ SIU_BRGBSEL
, 1);
122 siu_write32(base
+ SIU_BRRB
, 0);
124 siu_write32(base
+ SIU_IFCTL
, 0x44440000);
126 /* portA: 32 bit/fs, master; portB: 32 bit/fs, master */
127 siu_write32(base
+ SIU_SFORM
, 0x0c0c0000);
130 * Volume levels: looks like the DSP firmware implements volume controls
131 * differently from what's described in the datasheet
133 siu_write32(base
+ SIU_SBDVCA
, port_info
->playback
.volume
);
134 siu_write32(base
+ SIU_SBDVCB
, port_info
->capture
.volume
);
137 static void siu_dai_stop(struct siu_port
*port_info
)
139 struct siu_info
*info
= siu_i2s_data
;
140 u32 __iomem
*base
= info
->reg
;
142 /* SIU software reset */
143 siu_write32(base
+ SIU_SRCTL
, 0);
146 static void siu_dai_spbAselect(struct siu_port
*port_info
)
148 struct siu_info
*info
= siu_i2s_data
;
149 struct siu_firmware
*fw
= &info
->fw
;
150 u32
*ydef
= fw
->yram0
;
159 ydef
[0] = (fw
->spbpar
[idx
].ab1a
<< 16) |
160 (fw
->spbpar
[idx
].ab0a
<< 8) |
161 (fw
->spbpar
[idx
].dir
<< 7) | 3;
162 ydef
[1] = fw
->yram0
[1]; /* 0x03000300 */
163 ydef
[2] = (16 / 2) << 24;
164 ydef
[3] = fw
->yram0
[3]; /* 0 */
165 ydef
[4] = fw
->yram0
[4]; /* 0 */
166 ydef
[7] = fw
->spbpar
[idx
].event
;
167 port_info
->stfifo
|= fw
->spbpar
[idx
].stfifo
;
168 port_info
->trdat
|= fw
->spbpar
[idx
].trdat
;
171 static void siu_dai_spbBselect(struct siu_port
*port_info
)
173 struct siu_info
*info
= siu_i2s_data
;
174 struct siu_firmware
*fw
= &info
->fw
;
175 u32
*ydef
= fw
->yram0
;
184 ydef
[5] = (fw
->spbpar
[idx
].ab1a
<< 16) |
185 (fw
->spbpar
[idx
].ab0a
<< 8) | 1;
186 ydef
[6] = fw
->spbpar
[idx
].event
;
187 port_info
->stfifo
|= fw
->spbpar
[idx
].stfifo
;
188 port_info
->trdat
|= fw
->spbpar
[idx
].trdat
;
191 static void siu_dai_open(struct siu_stream
*siu_stream
)
193 struct siu_info
*info
= siu_i2s_data
;
194 u32 __iomem
*base
= info
->reg
;
197 srctl
= siu_read32(base
+ SIU_SRCTL
);
198 ifctl
= siu_read32(base
+ SIU_IFCTL
);
200 switch (info
->port_id
) {
213 siu_write32(base
+ SIU_SRCTL
, srctl
);
214 /* Unmute and configure portA */
215 siu_write32(base
+ SIU_IFCTL
, ifctl
);
219 * At the moment only fixed Left-upper, Left-lower, Right-upper, Right-lower
220 * packing is supported
222 static void siu_dai_pcmdatapack(struct siu_stream
*siu_stream
)
224 struct siu_info
*info
= siu_i2s_data
;
225 u32 __iomem
*base
= info
->reg
;
228 dpak
= siu_read32(base
+ SIU_DPAK
);
230 switch (info
->port_id
) {
239 siu_write32(base
+ SIU_DPAK
, dpak
);
242 static int siu_dai_spbstart(struct siu_port
*port_info
)
244 struct siu_info
*info
= siu_i2s_data
;
245 u32 __iomem
*base
= info
->reg
;
246 struct siu_firmware
*fw
= &info
->fw
;
247 u32
*ydef
= fw
->yram0
;
252 /* Load SPB Program in PRAM */
255 for (cnt
= 0; cnt
< PRAM0_SIZE
; cnt
++, add
++, ptr
++)
256 siu_write32(add
, *ptr
);
259 add
= info
->pram
+ (0x0100 / sizeof(u32
));
260 for (cnt
= 0; cnt
< PRAM1_SIZE
; cnt
++, add
++, ptr
++)
261 siu_write32(add
, *ptr
);
263 /* XRAM initialization */
265 for (cnt
= 0; cnt
< XRAM0_SIZE
+ XRAM1_SIZE
+ XRAM2_SIZE
; cnt
++, add
++)
268 /* YRAM variable area initialization */
270 for (cnt
= 0; cnt
< YRAM_DEF_SIZE
; cnt
++, add
++)
271 siu_write32(add
, ydef
[cnt
]);
273 /* YRAM FIR coefficient area initialization */
274 add
= info
->yram
+ (0x0200 / sizeof(u32
));
275 for (cnt
= 0; cnt
< YRAM_FIR_SIZE
; cnt
++, add
++)
276 siu_write32(add
, fw
->yram_fir_coeff
[cnt
]);
278 /* YRAM IIR coefficient area initialization */
279 add
= info
->yram
+ (0x0600 / sizeof(u32
));
280 for (cnt
= 0; cnt
< YRAM_IIR_SIZE
; cnt
++, add
++)
283 siu_write32(base
+ SIU_TRDAT
, port_info
->trdat
);
284 port_info
->trdat
= 0x0;
287 /* SPB start condition: software */
288 siu_write32(base
+ SIU_SBACTIV
, 0);
290 siu_write32(base
+ SIU_SBCTL
, 0xc0000000);
291 /* Wait for program to halt */
293 while (--cnt
&& siu_read32(base
+ SIU_SBCTL
) != 0x80000000)
299 /* SPB program start address setting */
300 siu_write32(base
+ SIU_SBPSET
, 0x00400000);
301 /* SPB hardware start(FIFOCTL source) */
302 siu_write32(base
+ SIU_SBACTIV
, 0xc0000000);
307 static void siu_dai_spbstop(struct siu_port
*port_info
)
309 struct siu_info
*info
= siu_i2s_data
;
310 u32 __iomem
*base
= info
->reg
;
312 siu_write32(base
+ SIU_SBACTIV
, 0);
314 siu_write32(base
+ SIU_SBCTL
, 0);
316 port_info
->stfifo
= 0;
321 /* Playback and capture hardware properties are identical */
322 static const struct snd_pcm_hardware siu_dai_pcm_hw
= {
323 .info
= SNDRV_PCM_INFO_INTERLEAVED
,
324 .formats
= SNDRV_PCM_FMTBIT_S16
,
325 .rates
= SNDRV_PCM_RATE_8000_48000
,
330 .buffer_bytes_max
= SIU_BUFFER_BYTES_MAX
,
331 .period_bytes_min
= SIU_PERIOD_BYTES_MIN
,
332 .period_bytes_max
= SIU_PERIOD_BYTES_MAX
,
333 .periods_min
= SIU_PERIODS_MIN
,
334 .periods_max
= SIU_PERIODS_MAX
,
337 static int siu_dai_info_volume(struct snd_kcontrol
*kctrl
,
338 struct snd_ctl_elem_info
*uinfo
)
340 struct siu_port
*port_info
= snd_kcontrol_chip(kctrl
);
342 dev_dbg(port_info
->pcm
->card
->dev
, "%s\n", __func__
);
344 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
346 uinfo
->value
.integer
.min
= 0;
347 uinfo
->value
.integer
.max
= SIU_MAX_VOLUME
;
352 static int siu_dai_get_volume(struct snd_kcontrol
*kctrl
,
353 struct snd_ctl_elem_value
*ucontrol
)
355 struct siu_port
*port_info
= snd_kcontrol_chip(kctrl
);
356 struct device
*dev
= port_info
->pcm
->card
->dev
;
359 dev_dbg(dev
, "%s\n", __func__
);
361 switch (kctrl
->private_value
) {
362 case VOLUME_PLAYBACK
:
363 /* Playback is always on port 0 */
364 vol
= port_info
->playback
.volume
;
365 ucontrol
->value
.integer
.value
[0] = vol
& 0xffff;
366 ucontrol
->value
.integer
.value
[1] = vol
>> 16 & 0xffff;
369 /* Capture is always on port 1 */
370 vol
= port_info
->capture
.volume
;
371 ucontrol
->value
.integer
.value
[0] = vol
& 0xffff;
372 ucontrol
->value
.integer
.value
[1] = vol
>> 16 & 0xffff;
375 dev_err(dev
, "%s() invalid private_value=%ld\n",
376 __func__
, kctrl
->private_value
);
383 static int siu_dai_put_volume(struct snd_kcontrol
*kctrl
,
384 struct snd_ctl_elem_value
*ucontrol
)
386 struct siu_port
*port_info
= snd_kcontrol_chip(kctrl
);
387 struct device
*dev
= port_info
->pcm
->card
->dev
;
388 struct siu_info
*info
= siu_i2s_data
;
389 u32 __iomem
*base
= info
->reg
;
393 dev_dbg(dev
, "%s\n", __func__
);
395 if (ucontrol
->value
.integer
.value
[0] < 0 ||
396 ucontrol
->value
.integer
.value
[0] > SIU_MAX_VOLUME
||
397 ucontrol
->value
.integer
.value
[1] < 0 ||
398 ucontrol
->value
.integer
.value
[1] > SIU_MAX_VOLUME
)
401 new_vol
= ucontrol
->value
.integer
.value
[0] |
402 ucontrol
->value
.integer
.value
[1] << 16;
404 /* See comment above - DSP firmware implementation */
405 switch (kctrl
->private_value
) {
406 case VOLUME_PLAYBACK
:
407 /* Playback is always on port 0 */
408 cur_vol
= port_info
->playback
.volume
;
409 siu_write32(base
+ SIU_SBDVCA
, new_vol
);
410 port_info
->playback
.volume
= new_vol
;
413 /* Capture is always on port 1 */
414 cur_vol
= port_info
->capture
.volume
;
415 siu_write32(base
+ SIU_SBDVCB
, new_vol
);
416 port_info
->capture
.volume
= new_vol
;
419 dev_err(dev
, "%s() invalid private_value=%ld\n",
420 __func__
, kctrl
->private_value
);
424 if (cur_vol
!= new_vol
)
430 static const struct snd_kcontrol_new playback_controls
= {
431 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
432 .name
= "PCM Playback Volume",
434 .info
= siu_dai_info_volume
,
435 .get
= siu_dai_get_volume
,
436 .put
= siu_dai_put_volume
,
437 .private_value
= VOLUME_PLAYBACK
,
440 static const struct snd_kcontrol_new capture_controls
= {
441 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
442 .name
= "PCM Capture Volume",
444 .info
= siu_dai_info_volume
,
445 .get
= siu_dai_get_volume
,
446 .put
= siu_dai_put_volume
,
447 .private_value
= VOLUME_CAPTURE
,
450 int siu_init_port(int port
, struct siu_port
**port_info
, struct snd_card
*card
)
452 struct device
*dev
= card
->dev
;
453 struct snd_kcontrol
*kctrl
;
456 *port_info
= kzalloc(sizeof(**port_info
), GFP_KERNEL
);
460 dev_dbg(dev
, "%s: port #%d@%p\n", __func__
, port
, *port_info
);
462 (*port_info
)->playback
.volume
= DFLT_VOLUME_LEVEL
;
463 (*port_info
)->capture
.volume
= DFLT_VOLUME_LEVEL
;
466 * Add mixer support. The SPB is used to change the volume. Both
467 * ports use the same SPB. Therefore, we only register one
468 * control instance since it will be used by both channels.
469 * In error case we continue without controls.
471 kctrl
= snd_ctl_new1(&playback_controls
, *port_info
);
472 ret
= snd_ctl_add(card
, kctrl
);
475 "failed to add playback controls %p port=%d err=%d\n",
478 kctrl
= snd_ctl_new1(&capture_controls
, *port_info
);
479 ret
= snd_ctl_add(card
, kctrl
);
482 "failed to add capture controls %p port=%d err=%d\n",
488 void siu_free_port(struct siu_port
*port_info
)
493 static int siu_dai_startup(struct snd_pcm_substream
*substream
,
494 struct snd_soc_dai
*dai
)
496 struct siu_info
*info
= snd_soc_dai_get_drvdata(dai
);
497 struct snd_pcm_runtime
*rt
= substream
->runtime
;
498 struct siu_port
*port_info
= siu_port_info(substream
);
501 dev_dbg(substream
->pcm
->card
->dev
, "%s: port=%d@%p\n", __func__
,
502 info
->port_id
, port_info
);
504 snd_soc_set_runtime_hwparams(substream
, &siu_dai_pcm_hw
);
506 ret
= snd_pcm_hw_constraint_integer(rt
, SNDRV_PCM_HW_PARAM_PERIODS
);
507 if (unlikely(ret
< 0))
510 siu_dai_start(port_info
);
515 static void siu_dai_shutdown(struct snd_pcm_substream
*substream
,
516 struct snd_soc_dai
*dai
)
518 struct siu_info
*info
= snd_soc_dai_get_drvdata(dai
);
519 struct siu_port
*port_info
= siu_port_info(substream
);
521 dev_dbg(substream
->pcm
->card
->dev
, "%s: port=%d@%p\n", __func__
,
522 info
->port_id
, port_info
);
524 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
525 port_info
->play_cap
&= ~PLAYBACK_ENABLED
;
527 port_info
->play_cap
&= ~CAPTURE_ENABLED
;
529 /* Stop the siu if the other stream is not using it */
530 if (!port_info
->play_cap
) {
531 /* during stmread or stmwrite ? */
532 if (WARN_ON(port_info
->playback
.rw_flg
|| port_info
->capture
.rw_flg
))
534 siu_dai_spbstop(port_info
);
535 siu_dai_stop(port_info
);
539 /* PCM part of siu_dai_playback_prepare() / siu_dai_capture_prepare() */
540 static int siu_dai_prepare(struct snd_pcm_substream
*substream
,
541 struct snd_soc_dai
*dai
)
543 struct siu_info
*info
= snd_soc_dai_get_drvdata(dai
);
544 struct snd_pcm_runtime
*rt
= substream
->runtime
;
545 struct siu_port
*port_info
= siu_port_info(substream
);
546 struct siu_stream
*siu_stream
;
549 dev_dbg(substream
->pcm
->card
->dev
,
550 "%s: port %d, active streams %lx, %d channels\n",
551 __func__
, info
->port_id
, port_info
->play_cap
, rt
->channels
);
553 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
554 self
= PLAYBACK_ENABLED
;
555 siu_stream
= &port_info
->playback
;
557 self
= CAPTURE_ENABLED
;
558 siu_stream
= &port_info
->capture
;
561 /* Set up the siu if not already done */
562 if (!port_info
->play_cap
) {
563 siu_stream
->rw_flg
= 0; /* stream-data transfer flag */
565 siu_dai_spbAselect(port_info
);
566 siu_dai_spbBselect(port_info
);
568 siu_dai_open(siu_stream
);
570 siu_dai_pcmdatapack(siu_stream
);
572 ret
= siu_dai_spbstart(port_info
);
579 port_info
->play_cap
|= self
;
586 * SIU can set bus format to I2S / PCM / SPDIF independently for playback and
587 * capture, however, the current API sets the bus format globally for a DAI.
589 static int siu_dai_set_fmt(struct snd_soc_dai
*dai
,
592 struct siu_info
*info
= snd_soc_dai_get_drvdata(dai
);
593 u32 __iomem
*base
= info
->reg
;
596 dev_dbg(dai
->dev
, "%s: fmt 0x%x on port %d\n",
597 __func__
, fmt
, info
->port_id
);
599 if (info
->port_id
< 0)
602 /* Here select between I2S / PCM / SPDIF */
603 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
604 case SND_SOC_DAIFMT_I2S
:
605 ifctl
= siu_flags
[info
->port_id
].playback
.i2s
|
606 siu_flags
[info
->port_id
].capture
.i2s
;
608 case SND_SOC_DAIFMT_LEFT_J
:
609 ifctl
= siu_flags
[info
->port_id
].playback
.pcm
|
610 siu_flags
[info
->port_id
].capture
.pcm
;
612 /* SPDIF disabled - see comment at the top */
617 ifctl
|= ~(siu_flags
[info
->port_id
].playback
.mask
|
618 siu_flags
[info
->port_id
].capture
.mask
) &
619 siu_read32(base
+ SIU_IFCTL
);
620 siu_write32(base
+ SIU_IFCTL
, ifctl
);
625 static int siu_dai_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
626 unsigned int freq
, int dir
)
628 struct clk
*siu_clk
, *parent_clk
;
629 char *siu_name
, *parent_name
;
632 if (dir
!= SND_SOC_CLOCK_IN
)
635 dev_dbg(dai
->dev
, "%s: using clock %d\n", __func__
, clk_id
);
639 siu_name
= "siua_clk";
640 parent_name
= "pll_clk";
643 siu_name
= "siua_clk";
644 parent_name
= "siumcka_clk";
647 siu_name
= "siub_clk";
648 parent_name
= "pll_clk";
651 siu_name
= "siub_clk";
652 parent_name
= "siumckb_clk";
658 siu_clk
= clk_get(dai
->dev
, siu_name
);
659 if (IS_ERR(siu_clk
)) {
660 dev_err(dai
->dev
, "%s: cannot get a SIU clock: %ld\n", __func__
,
662 return PTR_ERR(siu_clk
);
665 parent_clk
= clk_get(dai
->dev
, parent_name
);
666 if (IS_ERR(parent_clk
)) {
667 ret
= PTR_ERR(parent_clk
);
668 dev_err(dai
->dev
, "cannot get a SIU clock parent: %d\n", ret
);
672 ret
= clk_set_parent(siu_clk
, parent_clk
);
674 dev_err(dai
->dev
, "cannot reparent the SIU clock: %d\n", ret
);
678 ret
= clk_set_rate(siu_clk
, freq
);
680 dev_err(dai
->dev
, "cannot set SIU clock rate: %d\n", ret
);
682 /* TODO: when clkdev gets reference counting we'll move these to siu_dai_shutdown() */
691 static const struct snd_soc_dai_ops siu_dai_ops
= {
692 .startup
= siu_dai_startup
,
693 .shutdown
= siu_dai_shutdown
,
694 .prepare
= siu_dai_prepare
,
695 .set_sysclk
= siu_dai_set_sysclk
,
696 .set_fmt
= siu_dai_set_fmt
,
699 static struct snd_soc_dai_driver siu_i2s_dai
= {
700 .name
= "siu-i2s-dai",
704 .formats
= SNDRV_PCM_FMTBIT_S16
,
705 .rates
= SNDRV_PCM_RATE_8000_48000
,
710 .formats
= SNDRV_PCM_FMTBIT_S16
,
711 .rates
= SNDRV_PCM_RATE_8000_48000
,
716 static int siu_probe(struct platform_device
*pdev
)
718 const struct firmware
*fw_entry
;
719 struct resource
*res
, *region
;
720 struct siu_info
*info
;
723 info
= devm_kmalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
727 info
->dev
= &pdev
->dev
;
729 ret
= request_firmware(&fw_entry
, "siu_spb.bin", &pdev
->dev
);
734 * Loaded firmware is "const" - read only, but we have to modify it in
735 * snd_siu_sh7343_spbAselect() and snd_siu_sh7343_spbBselect()
737 memcpy(&info
->fw
, fw_entry
->data
, fw_entry
->size
);
739 release_firmware(fw_entry
);
741 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
745 region
= devm_request_mem_region(&pdev
->dev
, res
->start
,
746 resource_size(res
), pdev
->name
);
748 dev_err(&pdev
->dev
, "SIU region already claimed\n");
752 info
->pram
= devm_ioremap(&pdev
->dev
, res
->start
, PRAM_SIZE
);
755 info
->xram
= devm_ioremap(&pdev
->dev
, res
->start
+ XRAM_OFFSET
,
759 info
->yram
= devm_ioremap(&pdev
->dev
, res
->start
+ YRAM_OFFSET
,
763 info
->reg
= devm_ioremap(&pdev
->dev
, res
->start
+ REG_OFFSET
,
764 resource_size(res
) - REG_OFFSET
);
768 dev_set_drvdata(&pdev
->dev
, info
);
770 /* register using ARRAY version so we can keep dai name */
771 ret
= devm_snd_soc_register_component(&pdev
->dev
, &siu_component
,
776 pm_runtime_enable(&pdev
->dev
);
781 static int siu_remove(struct platform_device
*pdev
)
783 pm_runtime_disable(&pdev
->dev
);
787 static struct platform_driver siu_driver
= {
789 .name
= "siu-pcm-audio",
792 .remove
= siu_remove
,
795 module_platform_driver(siu_driver
);
797 MODULE_AUTHOR("Carlos Munoz <carlos@kenati.com>");
798 MODULE_DESCRIPTION("ALSA SoC SH7722 SIU driver");
799 MODULE_LICENSE("GPL");