1 // SPDX-License-Identifier: GPL-2.0
3 // Hitachi Audio Controller (AC97) support for SH7760/SH7780
5 // Copyright (c) 2007 Manuel Lauss <mano@roarinelk.homelinux.net>
7 // dont forget to set IPSEL/OMSEL register bits (in your board code) to
8 // enable HAC output pins!
10 /* BIG FAT FIXME: although the SH7760 has 2 independent AC97 units, only
11 * the FIRST can be used since ASoC does not pass any information to the
12 * ac97_read/write() functions regarding WHICH unit to use. You'll have
13 * to edit the code a bit to use the other AC97 unit. --mlau
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/interrupt.h>
20 #include <linux/wait.h>
21 #include <linux/delay.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/ac97_codec.h>
25 #include <sound/initval.h>
26 #include <sound/soc.h>
40 #define CR_CR (1 << 15) /* "codec-ready" indicator */
41 #define CR_CDRT (1 << 11) /* cold reset */
42 #define CR_WMRT (1 << 10) /* warm reset */
43 #define CR_B9 (1 << 9) /* the mysterious "bit 9" */
44 #define CR_ST (1 << 5) /* AC97 link start bit */
46 #define CSAR_RD (1 << 19) /* AC97 data read bit */
49 #define TSR_CMDAMT (1 << 31)
50 #define TSR_CMDDMT (1 << 30)
52 #define RSR_STARY (1 << 22)
53 #define RSR_STDRY (1 << 21)
55 #define ACR_DMARX16 (1 << 30)
56 #define ACR_DMATX16 (1 << 29)
57 #define ACR_TX12ATOM (1 << 26)
58 #define ACR_DMARX20 ((1 << 24) | (1 << 22))
59 #define ACR_DMATX20 ((1 << 23) | (1 << 21))
62 #define CSDR_MASK (0xffff << CSDR_SHIFT)
64 #define CSAR_MASK (0x7f << CSAR_SHIFT)
66 #define AC97_WRITE_RETRY 1
67 #define AC97_READ_RETRY 5
69 /* manual-suggested AC97 codec access timeouts (us) */
70 #define TMO_E1 500 /* 21 < E1 < 1000 */
71 #define TMO_E2 13 /* 13 < E2 */
72 #define TMO_E3 21 /* 21 < E3 */
73 #define TMO_E4 500 /* 21 < E4 < 1000 */
76 unsigned long mmio
; /* HAC base address */
78 #if defined(CONFIG_CPU_SUBTYPE_SH7760)
85 #elif defined(CONFIG_CPU_SUBTYPE_SH7780)
90 #error "Unsupported SuperH SoC"
94 #define HACREG(reg) (*(unsigned long *)(hac->mmio + (reg)))
97 * AC97 read/write flow as outlined in the SH7760 manual (pages 903-906)
99 static int hac_get_codec_data(struct hac_priv
*hac
, unsigned short r
,
102 unsigned int to1
, to2
, i
;
105 for (i
= AC97_READ_RETRY
; i
; i
--) {
107 /* wait for HAC to receive something from the codec */
109 to1
&& !(HACREG(HACRSR
) & RSR_STARY
);
113 to2
&& !(HACREG(HACRSR
) & RSR_STDRY
);
118 return 0; /* codec comm is down */
120 adr
= ((HACREG(HACCSAR
) & CSAR_MASK
) >> CSAR_SHIFT
);
121 *v
= ((HACREG(HACCSDR
) & CSDR_MASK
) >> CSDR_SHIFT
);
123 HACREG(HACRSR
) &= ~(RSR_STDRY
| RSR_STARY
);
128 /* manual says: wait at least 21 usec before retrying */
131 HACREG(HACRSR
) &= ~(RSR_STDRY
| RSR_STARY
);
135 static unsigned short hac_read_codec_aux(struct hac_priv
*hac
,
141 for (i
= AC97_READ_RETRY
; i
; i
--) {
142 /* send_read_request */
144 HACREG(HACTSR
) &= ~(TSR_CMDAMT
);
145 HACREG(HACCSAR
) = (reg
<< CSAR_SHIFT
) | CSAR_RD
;
149 to
&& !(HACREG(HACTSR
) & TSR_CMDAMT
);
153 HACREG(HACTSR
) &= ~TSR_CMDAMT
;
155 if (hac_get_codec_data(hac
, reg
, &val
) != 0)
162 static void hac_ac97_write(struct snd_ac97
*ac97
, unsigned short reg
,
165 int unit_id
= 0 /* ac97->private_data */;
166 struct hac_priv
*hac
= &hac_cpu_data
[unit_id
];
168 /* write_codec_aux */
169 for (i
= AC97_WRITE_RETRY
; i
; i
--) {
170 /* send_write_request */
172 HACREG(HACTSR
) &= ~(TSR_CMDDMT
| TSR_CMDAMT
);
173 HACREG(HACCSDR
) = (val
<< CSDR_SHIFT
);
174 HACREG(HACCSAR
) = (reg
<< CSAR_SHIFT
) & (~CSAR_RD
);
177 /* poll-wait for CMDAMT and CMDDMT */
179 to
&& !(HACREG(HACTSR
) & (TSR_CMDAMT
|TSR_CMDDMT
));
183 HACREG(HACTSR
) &= ~(TSR_CMDAMT
| TSR_CMDDMT
);
186 /* timeout, try again */
190 static unsigned short hac_ac97_read(struct snd_ac97
*ac97
,
193 int unit_id
= 0 /* ac97->private_data */;
194 struct hac_priv
*hac
= &hac_cpu_data
[unit_id
];
195 return hac_read_codec_aux(hac
, reg
);
198 static void hac_ac97_warmrst(struct snd_ac97
*ac97
)
200 int unit_id
= 0 /* ac97->private_data */;
201 struct hac_priv
*hac
= &hac_cpu_data
[unit_id
];
204 HACREG(HACCR
) = CR_WMRT
| CR_ST
| CR_B9
;
206 HACREG(HACCR
) = CR_ST
| CR_B9
;
207 for (tmo
= 1000; (tmo
> 0) && !(HACREG(HACCR
) & CR_CR
); tmo
--)
211 printk(KERN_INFO
"hac: reset: AC97 link down!\n");
212 /* settings this bit lets us have a conversation with codec */
213 HACREG(HACACR
) |= ACR_TX12ATOM
;
216 static void hac_ac97_coldrst(struct snd_ac97
*ac97
)
218 int unit_id
= 0 /* ac97->private_data */;
219 struct hac_priv
*hac
;
220 hac
= &hac_cpu_data
[unit_id
];
223 HACREG(HACCR
) = CR_CDRT
| CR_ST
| CR_B9
;
225 hac_ac97_warmrst(ac97
);
228 static struct snd_ac97_bus_ops hac_ac97_ops
= {
229 .read
= hac_ac97_read
,
230 .write
= hac_ac97_write
,
231 .reset
= hac_ac97_coldrst
,
232 .warm_reset
= hac_ac97_warmrst
,
235 static int hac_hw_params(struct snd_pcm_substream
*substream
,
236 struct snd_pcm_hw_params
*params
,
237 struct snd_soc_dai
*dai
)
239 struct hac_priv
*hac
= &hac_cpu_data
[dai
->id
];
240 int d
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
? 0 : 1;
242 switch (params
->msbits
) {
244 HACREG(HACACR
) |= d
? ACR_DMARX16
: ACR_DMATX16
;
245 HACREG(HACACR
) &= d
? ~ACR_DMARX20
: ~ACR_DMATX20
;
248 HACREG(HACACR
) &= d
? ~ACR_DMARX16
: ~ACR_DMATX16
;
249 HACREG(HACACR
) |= d
? ACR_DMARX20
: ACR_DMATX20
;
252 pr_debug("hac: invalid depth %d bit\n", params
->msbits
);
261 SNDRV_PCM_RATE_8000_192000
264 SNDRV_PCM_FMTBIT_S16_LE
266 static const struct snd_soc_dai_ops hac_dai_ops
= {
267 .hw_params
= hac_hw_params
,
270 static struct snd_soc_dai_driver sh4_hac_dai
[] = {
275 .formats
= AC97_FMTS
,
281 .formats
= AC97_FMTS
,
287 #ifdef CONFIG_CPU_SUBTYPE_SH7760
293 .formats
= AC97_FMTS
,
299 .formats
= AC97_FMTS
,
309 static const struct snd_soc_component_driver sh4_hac_component
= {
313 static int hac_soc_platform_probe(struct platform_device
*pdev
)
317 ret
= snd_soc_set_ac97_ops(&hac_ac97_ops
);
321 return devm_snd_soc_register_component(&pdev
->dev
, &sh4_hac_component
,
322 sh4_hac_dai
, ARRAY_SIZE(sh4_hac_dai
));
325 static int hac_soc_platform_remove(struct platform_device
*pdev
)
327 snd_soc_set_ac97_ops(NULL
);
331 static struct platform_driver hac_pcm_driver
= {
333 .name
= "hac-pcm-audio",
336 .probe
= hac_soc_platform_probe
,
337 .remove
= hac_soc_platform_remove
,
340 module_platform_driver(hac_pcm_driver
);
342 MODULE_LICENSE("GPL v2");
343 MODULE_DESCRIPTION("SuperH onchip HAC (AC97) audio driver");
344 MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");