2 * Serial Sound Interface (I2S) support for SH7760/SH7780
4 * Copyright (c) 2007 Manuel Lauss <mano@roarinelk.homelinux.net>
6 * licensed under the terms outlined in the file COPYING at the root
7 * of the linux kernel sources.
9 * dont forget to set IPSEL/OMSEL register bits (in your board code) to
10 * enable SSI output pins!
15 * The SSI unit has only one physical data line, so full duplex is
16 * impossible. This can be remedied on the SH7760 by using the
17 * other SSI unit for recording; however the SH7780 has only 1 SSI
18 * unit, and its pins are shared with the AC97 unit, among others.
21 * The SSI features "compressed mode": in this mode it continuously
22 * streams PCM data over the I2S lines and uses LRCK as a handshake
23 * signal. Can be used to send compressed data (AC3/DTS) to a DSP.
24 * The number of bits sent over the wire in a frame can be adjusted
25 * and can be independent from the actual sample bit depth. This is
26 * useful to support TDM mode codecs like the AD1939 which have a
27 * fixed TDM slot size, regardless of sample resolution.
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/platform_device.h>
33 #include <sound/driver.h>
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/initval.h>
37 #include <sound/soc.h>
43 #define CR_DMAEN (1 << 28)
44 #define CR_CHNL_SHIFT 22
45 #define CR_CHNL_MASK (3 << CR_CHNL_SHIFT)
46 #define CR_DWL_SHIFT 19
47 #define CR_DWL_MASK (7 << CR_DWL_SHIFT)
48 #define CR_SWL_SHIFT 16
49 #define CR_SWL_MASK (7 << CR_SWL_SHIFT)
50 #define CR_SCK_MASTER (1 << 15) /* bitclock master bit */
51 #define CR_SWS_MASTER (1 << 14) /* wordselect master bit */
52 #define CR_SCKP (1 << 13) /* I2Sclock polarity */
53 #define CR_SWSP (1 << 12) /* LRCK polarity */
54 #define CR_SPDP (1 << 11)
55 #define CR_SDTA (1 << 10) /* i2s alignment (msb/lsb) */
56 #define CR_PDTA (1 << 9) /* fifo data alignment */
57 #define CR_DEL (1 << 8) /* delay data by 1 i2sclk */
58 #define CR_BREN (1 << 7) /* clock gating in burst mode */
59 #define CR_CKDIV_SHIFT 4
60 #define CR_CKDIV_MASK (7 << CR_CKDIV_SHIFT) /* bitclock divider */
61 #define CR_MUTE (1 << 3) /* SSI mute */
62 #define CR_CPEN (1 << 2) /* compressed mode */
63 #define CR_TRMD (1 << 1) /* transmit/receive select */
64 #define CR_EN (1 << 0) /* enable SSI */
66 #define SSIREG(reg) (*(unsigned long *)(ssi->mmio + (reg)))
73 #if defined(CONFIG_CPU_SUBTYPE_SH7760)
80 #elif defined(CONFIG_CPU_SUBTYPE_SH7780)
85 #error "Unsupported SuperH SoC"
90 * track usage of the SSI; it is simplex-only so prevent attempts of
91 * concurrent playback + capture. FIXME: any locking required?
93 static int ssi_startup(struct snd_pcm_substream *substream)
95 struct snd_soc_pcm_runtime *rtd = substream->private_data;
96 struct ssi_priv *ssi = &ssi_cpu_data[rtd->dai->cpu_dai->id];
98 pr_debug("ssi: already in use!\n");
105 static void ssi_shutdown(struct snd_pcm_substream *substream)
107 struct snd_soc_pcm_runtime *rtd = substream->private_data;
108 struct ssi_priv *ssi = &ssi_cpu_data[rtd->dai->cpu_dai->id];
113 static int ssi_trigger(struct snd_pcm_substream *substream, int cmd)
115 struct snd_soc_pcm_runtime *rtd = substream->private_data;
116 struct ssi_priv *ssi = &ssi_cpu_data[rtd->dai->cpu_dai->id];
119 case SNDRV_PCM_TRIGGER_START:
120 SSIREG(SSICR) |= CR_DMAEN | CR_EN;
122 case SNDRV_PCM_TRIGGER_STOP:
123 SSIREG(SSICR) &= ~(CR_DMAEN | CR_EN);
132 static int ssi_hw_params(struct snd_pcm_substream *substream,
133 struct snd_pcm_hw_params *params)
135 struct snd_soc_pcm_runtime *rtd = substream->private_data;
136 struct ssi_priv *ssi = &ssi_cpu_data[rtd->dai->cpu_dai->id];
137 unsigned long ssicr = SSIREG(SSICR);
138 unsigned int bits, channels, swl, recv, i;
140 channels = params_channels(params);
141 bits = params->msbits;
142 recv = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 0 : 1;
144 pr_debug("ssi_hw_params() enter\nssicr was %08lx\n", ssicr);
145 pr_debug("bits: %d channels: %d\n", bits, channels);
147 ssicr &= ~(CR_TRMD | CR_CHNL_MASK | CR_DWL_MASK | CR_PDTA |
150 /* direction (send/receive) */
152 ssicr |= CR_TRMD; /* transmit */
155 if ((channels < 2) || (channels > 8) || (channels & 1)) {
156 pr_debug("ssi: invalid number of channels\n");
159 ssicr |= ((channels >> 1) - 1) << CR_CHNL_SHIFT;
161 /* DATA WORD LENGTH (DWL): databits in audio sample */
170 ssicr |= i << CR_DWL_SHIFT;
173 pr_debug("ssi: invalid sample width\n");
178 * SYSTEM WORD LENGTH: size in bits of half a frame over the I2S
179 * wires. This is usually bits_per_sample x channels/2; i.e. in
180 * Stereo mode the SWL equals DWL. SWL can be bigger than the
181 * product of (channels_per_slot x samplebits), e.g. for codecs
182 * like the AD1939 which only accept 32bit wide TDM slots. For
183 * "standard" I2S operation we set SWL = chans / 2 * DWL here.
184 * Waiting for ASoC to get TDM support ;-)
186 if ((bits > 16) && (bits <= 24)) {
187 bits = 24; /* these are padded by the SSI */
188 /*ssicr |= CR_PDTA;*/ /* cpu/data endianness ? */
191 swl = (bits * channels) / 2;
199 ssicr |= i << CR_SWL_SHIFT;
202 pr_debug("ssi: invalid system word length computed\n");
206 SSIREG(SSICR) = ssicr;
208 pr_debug("ssi_hw_params() leave\nssicr is now %08lx\n", ssicr);
212 static int ssi_set_sysclk(struct snd_soc_cpu_dai *cpu_dai, int clk_id,
213 unsigned int freq, int dir)
215 struct ssi_priv *ssi = &ssi_cpu_data[cpu_dai->id];
223 * This divider is used to generate the SSI_SCK (I2S bitclock) from the
224 * clock at the HAC_BIT_CLK ("oversampling clock") pin.
226 static int ssi_set_clkdiv(struct snd_soc_cpu_dai *dai, int did, int div)
228 struct ssi_priv *ssi = &ssi_cpu_data[dai->id];
233 ssicr = SSIREG(SSICR) & ~CR_CKDIV_MASK;
239 SSIREG(SSICR) = ssicr | (i << CR_CKDIV_SHIFT);
242 pr_debug("ssi: invalid sck divider %d\n", div);
249 static int ssi_set_fmt(struct snd_soc_cpu_dai *dai, unsigned int fmt)
251 struct ssi_priv *ssi = &ssi_cpu_data[dai->id];
252 unsigned long ssicr = SSIREG(SSICR);
254 pr_debug("ssi_set_fmt()\nssicr was 0x%08lx\n", ssicr);
256 ssicr &= ~(CR_DEL | CR_PDTA | CR_BREN | CR_SWSP | CR_SCKP |
257 CR_SWS_MASTER | CR_SCK_MASTER);
259 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
260 case SND_SOC_DAIFMT_I2S:
262 case SND_SOC_DAIFMT_RIGHT_J:
263 ssicr |= CR_DEL | CR_PDTA;
265 case SND_SOC_DAIFMT_LEFT_J:
269 pr_debug("ssi: unsupported format\n");
273 switch (fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
274 case SND_SOC_DAIFMT_CONT:
276 case SND_SOC_DAIFMT_GATED:
281 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
282 case SND_SOC_DAIFMT_NB_NF:
283 ssicr |= CR_SCKP; /* sample data at low clkedge */
285 case SND_SOC_DAIFMT_NB_IF:
286 ssicr |= CR_SCKP | CR_SWSP;
288 case SND_SOC_DAIFMT_IB_NF:
290 case SND_SOC_DAIFMT_IB_IF:
291 ssicr |= CR_SWSP; /* word select starts low */
294 pr_debug("ssi: invalid inversion\n");
298 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
299 case SND_SOC_DAIFMT_CBM_CFM:
301 case SND_SOC_DAIFMT_CBS_CFM:
302 ssicr |= CR_SCK_MASTER;
304 case SND_SOC_DAIFMT_CBM_CFS:
305 ssicr |= CR_SWS_MASTER;
307 case SND_SOC_DAIFMT_CBS_CFS:
308 ssicr |= CR_SWS_MASTER | CR_SCK_MASTER;
311 pr_debug("ssi: invalid master/slave configuration\n");
315 SSIREG(SSICR) = ssicr;
316 pr_debug("ssi_set_fmt() leave\nssicr is now 0x%08lx\n", ssicr);
321 /* the SSI depends on an external clocksource (at HAC_BIT_CLK) even in
322 * Master mode, so really this is board specific; the SSI can do any
323 * rate with the right bitclk and divider settings.
326 SNDRV_PCM_RATE_8000_192000
328 /* the SSI can do 8-32 bit samples, with 8 possible channels */
330 (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | \
331 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE | \
332 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_U20_3LE | \
333 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3LE | \
334 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_U32_LE)
336 struct snd_soc_cpu_dai sh4_ssi_dai[] = {
340 .type = SND_SOC_DAI_I2S,
354 .startup = ssi_startup,
355 .shutdown = ssi_shutdown,
356 .trigger = ssi_trigger,
357 .hw_params = ssi_hw_params,
360 .set_sysclk = ssi_set_sysclk,
361 .set_clkdiv = ssi_set_clkdiv,
362 .set_fmt = ssi_set_fmt,
365 #ifdef CONFIG_CPU_SUBTYPE_SH7760
369 .type = SND_SOC_DAI_I2S,
383 .startup = ssi_startup,
384 .shutdown = ssi_shutdown,
385 .trigger = ssi_trigger,
386 .hw_params = ssi_hw_params,
389 .set_sysclk = ssi_set_sysclk,
390 .set_clkdiv = ssi_set_clkdiv,
391 .set_fmt = ssi_set_fmt,
396 EXPORT_SYMBOL_GPL(sh4_ssi_dai);
398 MODULE_LICENSE("GPL");
399 MODULE_DESCRIPTION("SuperH onchip SSI (I2S) audio driver");
400 MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");