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/core.h>
34 #include <sound/pcm.h>
35 #include <sound/initval.h>
36 #include <sound/soc.h>
42 #define CR_DMAEN (1 << 28)
43 #define CR_CHNL_SHIFT 22
44 #define CR_CHNL_MASK (3 << CR_CHNL_SHIFT)
45 #define CR_DWL_SHIFT 19
46 #define CR_DWL_MASK (7 << CR_DWL_SHIFT)
47 #define CR_SWL_SHIFT 16
48 #define CR_SWL_MASK (7 << CR_SWL_SHIFT)
49 #define CR_SCK_MASTER (1 << 15) /* bitclock master bit */
50 #define CR_SWS_MASTER (1 << 14) /* wordselect master bit */
51 #define CR_SCKP (1 << 13) /* I2Sclock polarity */
52 #define CR_SWSP (1 << 12) /* LRCK polarity */
53 #define CR_SPDP (1 << 11)
54 #define CR_SDTA (1 << 10) /* i2s alignment (msb/lsb) */
55 #define CR_PDTA (1 << 9) /* fifo data alignment */
56 #define CR_DEL (1 << 8) /* delay data by 1 i2sclk */
57 #define CR_BREN (1 << 7) /* clock gating in burst mode */
58 #define CR_CKDIV_SHIFT 4
59 #define CR_CKDIV_MASK (7 << CR_CKDIV_SHIFT) /* bitclock divider */
60 #define CR_MUTE (1 << 3) /* SSI mute */
61 #define CR_CPEN (1 << 2) /* compressed mode */
62 #define CR_TRMD (1 << 1) /* transmit/receive select */
63 #define CR_EN (1 << 0) /* enable SSI */
65 #define SSIREG(reg) (*(unsigned long *)(ssi->mmio + (reg)))
72 #if defined(CONFIG_CPU_SUBTYPE_SH7760)
79 #elif defined(CONFIG_CPU_SUBTYPE_SH7780)
84 #error "Unsupported SuperH SoC"
89 * track usage of the SSI; it is simplex-only so prevent attempts of
90 * concurrent playback + capture. FIXME: any locking required?
92 static int ssi_startup(struct snd_pcm_substream *substream)
94 struct snd_soc_pcm_runtime *rtd = substream->private_data;
95 struct ssi_priv *ssi = &ssi_cpu_data[rtd->dai->cpu_dai->id];
97 pr_debug("ssi: already in use!\n");
104 static void ssi_shutdown(struct snd_pcm_substream *substream)
106 struct snd_soc_pcm_runtime *rtd = substream->private_data;
107 struct ssi_priv *ssi = &ssi_cpu_data[rtd->dai->cpu_dai->id];
112 static int ssi_trigger(struct snd_pcm_substream *substream, int cmd)
114 struct snd_soc_pcm_runtime *rtd = substream->private_data;
115 struct ssi_priv *ssi = &ssi_cpu_data[rtd->dai->cpu_dai->id];
118 case SNDRV_PCM_TRIGGER_START:
119 SSIREG(SSICR) |= CR_DMAEN | CR_EN;
121 case SNDRV_PCM_TRIGGER_STOP:
122 SSIREG(SSICR) &= ~(CR_DMAEN | CR_EN);
131 static int ssi_hw_params(struct snd_pcm_substream *substream,
132 struct snd_pcm_hw_params *params)
134 struct snd_soc_pcm_runtime *rtd = substream->private_data;
135 struct ssi_priv *ssi = &ssi_cpu_data[rtd->dai->cpu_dai->id];
136 unsigned long ssicr = SSIREG(SSICR);
137 unsigned int bits, channels, swl, recv, i;
139 channels = params_channels(params);
140 bits = params->msbits;
141 recv = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 0 : 1;
143 pr_debug("ssi_hw_params() enter\nssicr was %08lx\n", ssicr);
144 pr_debug("bits: %d channels: %d\n", bits, channels);
146 ssicr &= ~(CR_TRMD | CR_CHNL_MASK | CR_DWL_MASK | CR_PDTA |
149 /* direction (send/receive) */
151 ssicr |= CR_TRMD; /* transmit */
154 if ((channels < 2) || (channels > 8) || (channels & 1)) {
155 pr_debug("ssi: invalid number of channels\n");
158 ssicr |= ((channels >> 1) - 1) << CR_CHNL_SHIFT;
160 /* DATA WORD LENGTH (DWL): databits in audio sample */
169 ssicr |= i << CR_DWL_SHIFT;
172 pr_debug("ssi: invalid sample width\n");
177 * SYSTEM WORD LENGTH: size in bits of half a frame over the I2S
178 * wires. This is usually bits_per_sample x channels/2; i.e. in
179 * Stereo mode the SWL equals DWL. SWL can be bigger than the
180 * product of (channels_per_slot x samplebits), e.g. for codecs
181 * like the AD1939 which only accept 32bit wide TDM slots. For
182 * "standard" I2S operation we set SWL = chans / 2 * DWL here.
183 * Waiting for ASoC to get TDM support ;-)
185 if ((bits > 16) && (bits <= 24)) {
186 bits = 24; /* these are padded by the SSI */
187 /*ssicr |= CR_PDTA;*/ /* cpu/data endianness ? */
190 swl = (bits * channels) / 2;
198 ssicr |= i << CR_SWL_SHIFT;
201 pr_debug("ssi: invalid system word length computed\n");
205 SSIREG(SSICR) = ssicr;
207 pr_debug("ssi_hw_params() leave\nssicr is now %08lx\n", ssicr);
211 static int ssi_set_sysclk(struct snd_soc_dai *cpu_dai, int clk_id,
212 unsigned int freq, int dir)
214 struct ssi_priv *ssi = &ssi_cpu_data[cpu_dai->id];
222 * This divider is used to generate the SSI_SCK (I2S bitclock) from the
223 * clock at the HAC_BIT_CLK ("oversampling clock") pin.
225 static int ssi_set_clkdiv(struct snd_soc_dai *dai, int did, int div)
227 struct ssi_priv *ssi = &ssi_cpu_data[dai->id];
232 ssicr = SSIREG(SSICR) & ~CR_CKDIV_MASK;
238 SSIREG(SSICR) = ssicr | (i << CR_CKDIV_SHIFT);
241 pr_debug("ssi: invalid sck divider %d\n", div);
248 static int ssi_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
250 struct ssi_priv *ssi = &ssi_cpu_data[dai->id];
251 unsigned long ssicr = SSIREG(SSICR);
253 pr_debug("ssi_set_fmt()\nssicr was 0x%08lx\n", ssicr);
255 ssicr &= ~(CR_DEL | CR_PDTA | CR_BREN | CR_SWSP | CR_SCKP |
256 CR_SWS_MASTER | CR_SCK_MASTER);
258 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
259 case SND_SOC_DAIFMT_I2S:
261 case SND_SOC_DAIFMT_RIGHT_J:
262 ssicr |= CR_DEL | CR_PDTA;
264 case SND_SOC_DAIFMT_LEFT_J:
268 pr_debug("ssi: unsupported format\n");
272 switch (fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
273 case SND_SOC_DAIFMT_CONT:
275 case SND_SOC_DAIFMT_GATED:
280 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
281 case SND_SOC_DAIFMT_NB_NF:
282 ssicr |= CR_SCKP; /* sample data at low clkedge */
284 case SND_SOC_DAIFMT_NB_IF:
285 ssicr |= CR_SCKP | CR_SWSP;
287 case SND_SOC_DAIFMT_IB_NF:
289 case SND_SOC_DAIFMT_IB_IF:
290 ssicr |= CR_SWSP; /* word select starts low */
293 pr_debug("ssi: invalid inversion\n");
297 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
298 case SND_SOC_DAIFMT_CBM_CFM:
300 case SND_SOC_DAIFMT_CBS_CFM:
301 ssicr |= CR_SCK_MASTER;
303 case SND_SOC_DAIFMT_CBM_CFS:
304 ssicr |= CR_SWS_MASTER;
306 case SND_SOC_DAIFMT_CBS_CFS:
307 ssicr |= CR_SWS_MASTER | CR_SCK_MASTER;
310 pr_debug("ssi: invalid master/slave configuration\n");
314 SSIREG(SSICR) = ssicr;
315 pr_debug("ssi_set_fmt() leave\nssicr is now 0x%08lx\n", ssicr);
320 /* the SSI depends on an external clocksource (at HAC_BIT_CLK) even in
321 * Master mode, so really this is board specific; the SSI can do any
322 * rate with the right bitclk and divider settings.
325 SNDRV_PCM_RATE_8000_192000
327 /* the SSI can do 8-32 bit samples, with 8 possible channels */
329 (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | \
330 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE | \
331 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_U20_3LE | \
332 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3LE | \
333 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_U32_LE)
335 struct snd_soc_dai sh4_ssi_dai[] = {
339 .type = SND_SOC_DAI_I2S,
353 .startup = ssi_startup,
354 .shutdown = ssi_shutdown,
355 .trigger = ssi_trigger,
356 .hw_params = ssi_hw_params,
359 .set_sysclk = ssi_set_sysclk,
360 .set_clkdiv = ssi_set_clkdiv,
361 .set_fmt = ssi_set_fmt,
364 #ifdef CONFIG_CPU_SUBTYPE_SH7760
368 .type = SND_SOC_DAI_I2S,
382 .startup = ssi_startup,
383 .shutdown = ssi_shutdown,
384 .trigger = ssi_trigger,
385 .hw_params = ssi_hw_params,
388 .set_sysclk = ssi_set_sysclk,
389 .set_clkdiv = ssi_set_clkdiv,
390 .set_fmt = ssi_set_fmt,
395 EXPORT_SYMBOL_GPL(sh4_ssi_dai);
397 MODULE_LICENSE("GPL");
398 MODULE_DESCRIPTION("SuperH onchip SSI (I2S) audio driver");
399 MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");