]> err.no Git - linux-2.6/blob - sound/soc/sh/hac.c
[ALSA] ASoC: sh: improve generated code for HAC module (AC97)
[linux-2.6] / sound / soc / sh / hac.c
1 /*
2  * Hitachi Audio Controller (AC97) support for SH7760/SH7780
3  *
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.
7  *
8  * dont forget to set IPSEL/OMSEL register bits (in your board code) to
9  * enable HAC output pins!
10  */
11
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
16  */
17
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>
30
31 /* regs and bits */
32 #define HACCR           0x08
33 #define HACCSAR         0x20
34 #define HACCSDR         0x24
35 #define HACPCML         0x28
36 #define HACPCMR         0x2C
37 #define HACTIER         0x50
38 #define HACTSR          0x54
39 #define HACRIER         0x58
40 #define HACRSR          0x5C
41 #define HACACR          0x60
42
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 */
48
49 #define CSAR_RD         (1 << 19)       /* AC97 data read bit */
50 #define CSAR_WR         (0)
51
52 #define TSR_CMDAMT      (1 << 31)
53 #define TSR_CMDDMT      (1 << 30)
54
55 #define RSR_STARY       (1 << 22)
56 #define RSR_STDRY       (1 << 21)
57
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))
63
64 #define CSDR_SHIFT      4
65 #define CSDR_MASK       (0xffff << CSDR_SHIFT)
66 #define CSAR_SHIFT      12
67 #define CSAR_MASK       (0x7f << CSAR_SHIFT)
68
69 #define AC97_WRITE_RETRY        1
70 #define AC97_READ_RETRY         5
71
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 */
77
78 struct hac_priv {
79         unsigned long mmio;     /* HAC base address */
80 } hac_cpu_data[] = {
81 #if defined(CONFIG_CPU_SUBTYPE_SH7760)
82         {
83                 .mmio   = 0xFE240000,
84         },
85         {
86                 .mmio   = 0xFE250000,
87         },
88 #elif defined(CONFIG_CPU_SUBTYPE_SH7780)
89         {
90                 .mmio   = 0xFFE40000,
91         },
92 #else
93 #error "Unsupported SuperH SoC"
94 #endif
95 };
96
97 #define HACREG(reg)     (*(unsigned long *)(hac->mmio + (reg)))
98
99 /*
100  * AC97 read/write flow as outlined in the SH7760 manual (pages 903-906)
101  */
102 static int hac_get_codec_data(struct hac_priv *hac, unsigned short r,
103                               unsigned short *v)
104 {
105         unsigned int to1, to2, i;
106         unsigned short adr;
107
108         for (i = AC97_READ_RETRY; i; i--) {
109                 *v = 0;
110                 /* wait for HAC to receive something from the codec */
111                 for (to1 = TMO_E4;
112                      to1 && !(HACREG(HACRSR) & RSR_STARY);
113                      --to1)
114                         udelay(1);
115                 for (to2 = TMO_E4; 
116                      to2 && !(HACREG(HACRSR) & RSR_STDRY);
117                      --to2)
118                         udelay(1);
119
120                 if (!to1 && !to2)
121                         return 0;       /* codec comm is down */
122
123                 adr = ((HACREG(HACCSAR) & CSAR_MASK) >> CSAR_SHIFT);
124                 *v  = ((HACREG(HACCSDR) & CSDR_MASK) >> CSDR_SHIFT);
125
126                 HACREG(HACRSR) &= ~(RSR_STDRY | RSR_STARY);
127
128                 if (r == adr)
129                         break;
130
131                 /* manual says: wait at least 21 usec before retrying */
132                 udelay(21);
133         }
134         HACREG(HACRSR) &= ~(RSR_STDRY | RSR_STARY);
135         return i;
136 }
137
138 static unsigned short hac_read_codec_aux(struct hac_priv *hac,
139                                          unsigned short reg)
140 {
141         unsigned short val;
142         unsigned int i, to;
143
144         for (i = AC97_READ_RETRY; i; i--) {
145                 /* send_read_request */
146                 local_irq_disable();
147                 HACREG(HACTSR) &= ~(TSR_CMDAMT);
148                 HACREG(HACCSAR) = (reg << CSAR_SHIFT) | CSAR_RD;
149                 local_irq_enable();
150
151                 for (to = TMO_E3;
152                      to && !(HACREG(HACTSR) & TSR_CMDAMT);
153                      --to)
154                         udelay(1);
155
156                 HACREG(HACTSR) &= ~TSR_CMDAMT;
157                 val = 0;
158                 if (hac_get_codec_data(hac, reg, &val) != 0)
159                         break;
160         }
161
162         return i ? val : ~0;
163 }
164
165 static void hac_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
166                            unsigned short val)
167 {
168         int unit_id = 0 /* ac97->private_data */;
169         struct hac_priv *hac = &hac_cpu_data[unit_id];
170         unsigned int i, to;
171         /* write_codec_aux */
172         for (i = AC97_WRITE_RETRY; i; i--) {
173                 /* send_write_request */
174                 local_irq_disable();
175                 HACREG(HACTSR) &= ~(TSR_CMDDMT | TSR_CMDAMT);
176                 HACREG(HACCSDR) = (val << CSDR_SHIFT);
177                 HACREG(HACCSAR) = (reg << CSAR_SHIFT) & (~CSAR_RD);
178                 local_irq_enable();
179
180                 /* poll-wait for CMDAMT and CMDDMT */
181                 for (to = TMO_E1;
182                      to && !(HACREG(HACTSR) & (TSR_CMDAMT|TSR_CMDDMT));
183                      --to)
184                         udelay(1);
185
186                 HACREG(HACTSR) &= ~(TSR_CMDAMT | TSR_CMDDMT);
187                 if (to)
188                         break;
189                 /* timeout, try again */
190         }
191 }
192
193 static unsigned short hac_ac97_read(struct snd_ac97 *ac97,
194                                     unsigned short reg)
195 {
196         int unit_id = 0 /* ac97->private_data */;
197         struct hac_priv *hac = &hac_cpu_data[unit_id];
198         return hac_read_codec_aux(hac, reg);
199 }
200
201 static void hac_ac97_warmrst(struct snd_ac97 *ac97)
202 {
203         int unit_id = 0 /* ac97->private_data */;
204         struct hac_priv *hac = &hac_cpu_data[unit_id];
205         unsigned int tmo;
206
207         HACREG(HACCR) = CR_WMRT | CR_ST | CR_B9;
208         msleep(10);
209         HACREG(HACCR) = CR_ST | CR_B9;
210         for (tmo = 1000; (tmo > 0) && !(HACREG(HACCR) & CR_CR); tmo--)
211                 udelay(1);
212
213         if (!tmo)
214                 printk(KERN_INFO "hac: reset: AC97 link down!\n");
215         /* settings this bit lets us have a conversation with codec */
216         HACREG(HACACR) |= ACR_TX12ATOM;
217 }
218
219 static void hac_ac97_coldrst(struct snd_ac97 *ac97)
220 {
221         int unit_id = 0 /* ac97->private_data */;
222         struct hac_priv *hac;
223         hac = &hac_cpu_data[unit_id];
224
225         HACREG(HACCR) = 0;
226         HACREG(HACCR) = CR_CDRT | CR_ST | CR_B9;
227         msleep(10);
228         hac_ac97_warmrst(ac97);
229 }
230
231 struct snd_ac97_bus_ops soc_ac97_ops = {
232         .read   = hac_ac97_read,
233         .write  = hac_ac97_write,
234         .reset  = hac_ac97_coldrst,
235         .warm_reset = hac_ac97_warmrst,
236 };
237 EXPORT_SYMBOL_GPL(soc_ac97_ops);
238
239 static int hac_hw_params(struct snd_pcm_substream *substream,
240                          struct snd_pcm_hw_params *params)
241 {
242         struct snd_soc_pcm_runtime *rtd = substream->private_data;
243         struct hac_priv *hac = &hac_cpu_data[rtd->dai->cpu_dai->id];
244         int d = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1;
245
246         switch (params->msbits) {
247         case 16:
248                 HACREG(HACACR) |= d ?  ACR_DMARX16 :  ACR_DMATX16;
249                 HACREG(HACACR) &= d ? ~ACR_DMARX20 : ~ACR_DMATX20;
250                 break;
251         case 20:
252                 HACREG(HACACR) &= d ? ~ACR_DMARX16 : ~ACR_DMATX16;
253                 HACREG(HACACR) |= d ?  ACR_DMARX20 :  ACR_DMATX20;
254                 break;
255         default:
256                 pr_debug("hac: invalid depth %d bit\n", params->msbits);
257                 return -EINVAL;
258                 break;
259         }
260
261         return 0;
262 }
263
264 #define AC97_RATES      \
265         SNDRV_PCM_RATE_8000_192000
266
267 #define AC97_FMTS       \
268         SNDRV_PCM_FMTBIT_S16_LE
269
270 struct snd_soc_cpu_dai sh4_hac_dai[] = {
271 {
272         .name                   = "HAC0",
273         .id                     = 0,
274         .type                   = SND_SOC_DAI_AC97,
275         .playback = {
276                 .rates          = AC97_RATES,
277                 .formats        = AC97_FMTS,
278                 .channels_min   = 2,
279                 .channels_max   = 2,
280         },
281         .capture = {
282                 .rates          = AC97_RATES,
283                 .formats        = AC97_FMTS,
284                 .channels_min   = 2,
285                 .channels_max   = 2,
286         },
287         .ops = {
288                 .hw_params      = hac_hw_params,
289         },
290 },
291 #ifdef CONFIG_CPU_SUBTYPE_SH7760
292 {
293         .name                   = "HAC1",
294         .id                     = 1,
295         .type                   = SND_SOC_DAI_AC97,
296         .playback = {
297                 .rates          = AC97_RATES,
298                 .formats        = AC97_FMTS,
299                 .channels_min   = 2,
300                 .channels_max   = 2,
301         },
302         .capture = {
303                 .rates          = AC97_RATES,
304                 .formats        = AC97_FMTS,
305                 .channels_min   = 2,
306                 .channels_max   = 2,
307         },
308         .ops = {
309                 .hw_params      = hac_hw_params,
310         },
311
312 },
313 #endif
314 };
315 EXPORT_SYMBOL_GPL(sh4_hac_dai);
316
317 MODULE_LICENSE("GPL");
318 MODULE_DESCRIPTION("SuperH onchip HAC (AC97) audio driver");
319 MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");