2 * Hitachi Audio Controller (AC97) support for SH7760/SH7780
4 * Copyright (c) 2007 Manuel Lauss <mano@roarinelk.homelinux.net>
5 * licensed under the terms outlined in the file COPYING at the root
6 * of the linux kernel sources.
8 * dont forget to set IPSEL/OMSEL register bits (in your board code) to
9 * enable HAC output pins!
12 /* BIG FAT FIXME: although the SH7760 has 2 independent AC97 units, only
13 * the FIRST can be used since ASoC does not pass any information to the
14 * ac97_read/write() functions regarding WHICH unit to use. You'll have
15 * to edit the code a bit to use the other AC97 unit. --mlau
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/interrupt.h>
22 #include <linux/wait.h>
23 #include <linux/delay.h>
24 #include <sound/driver.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/ac97_codec.h>
28 #include <sound/initval.h>
29 #include <sound/soc.h>
43 #define CR_CR (1 << 15) /* "codec-ready" indicator */
44 #define CR_CDRT (1 << 11) /* cold reset */
45 #define CR_WMRT (1 << 10) /* warm reset */
46 #define CR_B9 (1 << 9) /* the mysterious "bit 9" */
47 #define CR_ST (1 << 5) /* AC97 link start bit */
49 #define CSAR_RD (1 << 19) /* AC97 data read bit */
52 #define TSR_CMDAMT (1 << 31)
53 #define TSR_CMDDMT (1 << 30)
55 #define RSR_STARY (1 << 22)
56 #define RSR_STDRY (1 << 21)
58 #define ACR_DMARX16 (1 << 30)
59 #define ACR_DMATX16 (1 << 29)
60 #define ACR_TX12ATOM (1 << 26)
61 #define ACR_DMARX20 ((1 << 24) | (1 << 22))
62 #define ACR_DMATX20 ((1 << 23) | (1 << 21))
65 #define CSDR_MASK (0xffff << CSDR_SHIFT)
67 #define CSAR_MASK (0x7f << CSAR_SHIFT)
69 #define AC97_WRITE_RETRY 1
70 #define AC97_READ_RETRY 5
72 /* manual-suggested AC97 codec access timeouts (us) */
73 #define TMO_E1 500 /* 21 < E1 < 1000 */
74 #define TMO_E2 13 /* 13 < E2 */
75 #define TMO_E3 21 /* 21 < E3 */
76 #define TMO_E4 500 /* 21 < E4 < 1000 */
79 unsigned long mmio; /* HAC base address */
81 #if defined(CONFIG_CPU_SUBTYPE_SH7760)
88 #elif defined(CONFIG_CPU_SUBTYPE_SH7780)
93 #error "Unsupported SuperH SoC"
97 #define HACREG(reg) (*(unsigned long *)(hac->mmio + (reg)))
100 * AC97 read/write flow as outlined in the SH7760 manual (pages 903-906)
102 static int hac_get_codec_data(struct hac_priv *hac, unsigned short r,
105 unsigned int to1, to2, i;
108 for (i = 0; i < AC97_READ_RETRY; ++i) {
110 /* wait for HAC to receive something from the codec */
112 to1 && !(HACREG(HACRSR) & RSR_STARY);
116 to2 && !(HACREG(HACRSR) & RSR_STDRY);
121 return 0; /* codec comm is down */
123 adr = ((HACREG(HACCSAR) & CSAR_MASK) >> CSAR_SHIFT);
124 *v = ((HACREG(HACCSDR) & CSDR_MASK) >> CSDR_SHIFT);
126 HACREG(HACRSR) &= ~(RSR_STDRY | RSR_STARY);
131 /* manual says: wait at least 21 usec before retrying */
134 HACREG(HACRSR) &= ~(RSR_STDRY | RSR_STARY);
135 return (i < AC97_READ_RETRY);
138 static unsigned short hac_read_codec_aux(struct hac_priv *hac,
144 for (i = 0; i < AC97_READ_RETRY; i++) {
145 /* send_read_request */
147 HACREG(HACTSR) &= ~(TSR_CMDAMT);
148 HACREG(HACCSAR) = (reg << CSAR_SHIFT) | CSAR_RD;
152 to && !(HACREG(HACTSR) & TSR_CMDAMT);
156 HACREG(HACTSR) &= ~TSR_CMDAMT;
158 if (hac_get_codec_data(hac, reg, &val) != 0)
162 if (i == AC97_READ_RETRY)
168 static void hac_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
171 int unit_id = 0 /* ac97->private_data */;
172 struct hac_priv *hac = &hac_cpu_data[unit_id];
174 /* write_codec_aux */
175 for (i = 0; i < AC97_WRITE_RETRY; i++) {
176 /* send_write_request */
178 HACREG(HACTSR) &= ~(TSR_CMDDMT | TSR_CMDAMT);
179 HACREG(HACCSDR) = (val << CSDR_SHIFT);
180 HACREG(HACCSAR) = (reg << CSAR_SHIFT) & (~CSAR_RD);
183 /* poll-wait for CMDAMT and CMDDMT */
185 to && !(HACREG(HACTSR) & (TSR_CMDAMT|TSR_CMDDMT));
189 HACREG(HACTSR) &= ~(TSR_CMDAMT | TSR_CMDDMT);
192 /* timeout, try again */
196 static unsigned short hac_ac97_read(struct snd_ac97 *ac97,
199 int unit_id = 0 /* ac97->private_data */;
200 struct hac_priv *hac = &hac_cpu_data[unit_id];
201 return hac_read_codec_aux(hac, reg);
204 static void hac_ac97_warmrst(struct snd_ac97 *ac97)
206 int unit_id = 0 /* ac97->private_data */;
207 struct hac_priv *hac = &hac_cpu_data[unit_id];
210 HACREG(HACCR) = CR_WMRT | CR_ST | CR_B9;
212 HACREG(HACCR) = CR_ST | CR_B9;
213 for (tmo = 1000; (tmo > 0) && !(HACREG(HACCR) & CR_CR); tmo--)
217 printk(KERN_INFO "hac: reset: AC97 link down!\n");
218 /* settings this bit lets us have a conversation with codec */
219 HACREG(HACACR) |= ACR_TX12ATOM;
222 static void hac_ac97_coldrst(struct snd_ac97 *ac97)
224 int unit_id = 0 /* ac97->private_data */;
225 struct hac_priv *hac;
226 hac = &hac_cpu_data[unit_id];
229 HACREG(HACCR) = CR_CDRT | CR_ST | CR_B9;
231 hac_ac97_warmrst(ac97);
234 struct snd_ac97_bus_ops soc_ac97_ops = {
235 .read = hac_ac97_read,
236 .write = hac_ac97_write,
237 .reset = hac_ac97_coldrst,
238 .warm_reset = hac_ac97_warmrst,
240 EXPORT_SYMBOL_GPL(soc_ac97_ops);
242 static int hac_hw_params(struct snd_pcm_substream *substream,
243 struct snd_pcm_hw_params *params)
245 struct snd_soc_pcm_runtime *rtd = substream->private_data;
246 struct hac_priv *hac = &hac_cpu_data[rtd->dai->cpu_dai->id];
247 int d = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1;
249 switch (params->msbits) {
251 HACREG(HACACR) |= d ? ACR_DMARX16 : ACR_DMATX16;
252 HACREG(HACACR) &= d ? ~ACR_DMARX20 : ~ACR_DMATX20;
255 HACREG(HACACR) &= d ? ~ACR_DMARX16 : ~ACR_DMATX16;
256 HACREG(HACACR) |= d ? ACR_DMARX20 : ACR_DMATX20;
259 pr_debug("hac: invalid depth %d bit\n", params->msbits);
268 SNDRV_PCM_RATE_8000_192000
271 SNDRV_PCM_FMTBIT_S16_LE
273 struct snd_soc_cpu_dai sh4_hac_dai[] = {
277 .type = SND_SOC_DAI_AC97,
280 .formats = AC97_FMTS,
286 .formats = AC97_FMTS,
291 .hw_params = hac_hw_params,
294 #ifdef CONFIG_CPU_SUBTYPE_SH7760
298 .type = SND_SOC_DAI_AC97,
301 .formats = AC97_FMTS,
307 .formats = AC97_FMTS,
312 .hw_params = hac_hw_params,
318 EXPORT_SYMBOL_GPL(sh4_hac_dai);
320 MODULE_LICENSE("GPL");
321 MODULE_DESCRIPTION("SuperH onchip HAC (AC97) audio driver");
322 MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");