]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - sound/arm/omap/eac.c
OMAP2EVM: twl4030 keypad Kconfig dependency fix
[linux-2.6-omap-h63xx.git] / sound / arm / omap / eac.c
1 /*
2  * linux/sound/arm/omap/omap-alsa-eac.c
3  *
4  * OMAP24xx Enhanced Audio Controller sound driver
5  *
6  * Copyright (C) 2006 Nokia Corporation
7  *
8  * Contact: Jarkko Nikula <jarkko.nikula@nokia.com>
9  *          Juha Yrjölä
10  *
11  * Definitions:
12  * Copyright (C) 2004 Texas Instruments, Inc.
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * version 2 as published by the Free Software Foundation.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26  * 02110-1301 USA
27  *
28  */
29
30 #define DEBUG
31
32 #include <linux/device.h>
33 #include <linux/init.h>
34 #include <linux/kernel.h>
35 #include <linux/err.h>
36 #include <linux/module.h>
37 #include <linux/moduleparam.h>
38 #include <linux/delay.h>
39 #include <linux/platform_device.h>
40 #include <linux/clk.h>
41
42 #include <asm/io.h>
43 #include <asm/arch/eac.h>
44
45 #include <sound/core.h>
46 #include <sound/initval.h>
47
48
49 #define EAC_CPCFR1              0x0000
50 #define EAC_CPCFR2              0x0004
51 #define EAC_CPCFR3              0x0008
52 #define EAC_CPCFR4              0x000C
53 #define EAC_CPTCTL              0x0010
54 #define EAC_CPTTADR             0x0014
55 #define EAC_CPTDATL             0x0018
56 #define EAC_CPTDATH             0x001C
57 #define EAC_CPTVSLL             0x0020
58 #define EAC_CPTVSLH             0x0024
59 #define EAC_MPCTR               0x0040
60 #define EAC_MPMCCFR             0x0044
61 #define EAC_BPCTR               0x0060
62 #define EAC_BPMCCFR             0x0064
63 #define EAC_AMSCFR              0x0080
64 #define EAC_AMVCTR              0x0084
65 #define EAC_AM1VCTR             0x0088
66 #define EAC_AM2VCTR             0x008C
67 #define EAC_AM3VCTR             0x0090
68 #define EAC_ASTCTR              0x0094
69 #define EAC_APD1LCR             0x0098
70 #define EAC_APD1RCR             0x009C
71 #define EAC_APD2LCR             0x00A0
72 #define EAC_APD2RCR             0x00A4
73 #define EAC_APD3LCR             0x00A8
74 #define EAC_APD3RCR             0x00AC
75 #define EAC_APD4R               0x00B0
76 #define EAC_ADWR                0x00B4
77 #define EAC_ADRDR               0x00B8
78 #define EAC_AGCFR               0x00BC
79 #define EAC_AGCTR               0x00C0
80 #define EAC_AGCFR2              0x00C4
81 #define EAC_AGCFR3              0x00C8
82 #define EAC_MBPDMACTR           0x00CC
83 #define EAC_MPDDMARR            0x00D0
84 #define EAC_MPDDMAWR            0x00D4
85 #define EAC_MPUDMARR            0x00D8
86 #define EAC_MPUDMAWR            0x00E0
87 #define EAC_BPDDMARR            0x00E4
88 #define EAC_BPDDMAWR            0x00E8
89 #define EAC_BPUDMARR            0x00EC
90 #define EAC_BPUDMAWR            0x00F0
91 #define EAC_VERSION             0x0100
92 #define EAC_SYSCONFIG           0x0104
93 #define EAC_SYSSTATUS           0x0108
94
95 /* CPTCTL */
96 #define CPTCTL_RXF              (1 << 7)        /* receive data register full */
97 #define CPTCTL_RXIE             (1 << 6)        /* receive interrupt enable */
98 #define CPTCTL_TXE              (1 << 5)        /* transmit register empty */
99 #define CPTCTL_TXIE             (1 << 4)        /* transmit interrupt enable */
100 #define CPTCTL_CPEN             (1 << 3)        /* codec port enable */
101 #define CPTCTL_CRST             (1 << 0)        /* external codec reset */
102
103 /* CPCFR1 */
104 #define CPCFR1_MTSL(val)        ((val & 0x1f) << 3)     /* number of time slots per frame */
105 #define CPCFR1_MTSL_BITS        (0x1f << 3)
106 #define CPCFR1_MODE(val)        ((val & 0x7) << 0)      /* codec port interface mode */
107 #define CPCFR1_MODE_BITS        (0x7 << 0)
108
109 /* CPCFR2 */
110 #define CPCFR2_TSLOL(val)       ((val & 0x3) << 6)      /* time slot 0 length in number of serial clock (CLK_BIT) cycles */
111 #define CPCFR2_TSLOL_BITS       (0x3 << 6)
112 #define CPCFR2_BPTSL(val)       ((val & 0x7) << 3)      /* number of data bits per audio time slot */
113 #define CPCFR2_BPTSL_BITS       (0x7 << 3)
114 #define CPCFR2_TSLL(val)        ((val & 0x7) << 0)      /* time slot lenght (except slot 0) in number of serial clock cycles */
115 #define CPCFR2_TSLL_BITS        (0x7 << 0)
116
117 /* CPCFR3 */
118 #define CPCFR3_DDLY             (1 << 7)        /* data delay: data bits start according to SYNC signal leading edge */
119 #define CPCFR3_TRSEN            (1 << 6)        /* 3-state enable: data serial output state during nonvalid audio frames */
120 #define CPCFR3_CLKBP            (1 << 5)        /* clock polarity */
121 #define CPCFR3_CSYNCP           (1 << 4)        /* cp_sync(synchro) polarity */
122 #define CPCFR3_CSYNCL           (1 << 3)        /* csync length */
123 /* bit 2 reserved */
124 #define CPCFR3_CSCLKD           (1 << 1)        /* cp_sclk port (serial clock) direction */
125 #define CPCFR3_CSYNCD           (1 << 0)        /* cp_sync (synchro) direction */
126
127 /* CPCFR4 */
128 #define CPCFR4_ATSL(val)        ((val & 0xf) << 4)      /* audio time slots for secondary communication address and data values */
129 #define CPCFR4_ATSL_BITS        (0xf << 4)
130 #define CPCFR4_CLKS             (1 << 3)                /* clock source */
131 #define CPCFR4_DIVB(val)        ((val & 0x7) << 0)      /* cp_sclk driver value */
132 #define CPCFR4_DIVB_BITS        (0x7 << 0)
133
134 /* AGCFR */
135 #define AGCFR_MN_ST             (1 << 10)       /* mono/stereo audio file */
136 #define AGCFR_B8_16             (1 << 9)        /* 8 bits/16 bits audio file */
137 #define AGCFR_LI_BI             (1 << 8)        /* audio file endianism */
138 #define AGCFR_FSINT(val)        ((val & 0x3) << 6) /* intermediate sample frequency for DMA read and write operations */
139 #define AGCFR_FINST_BITS        (0x3 << 6)
140
141 #define AGCFR_FSINT_8000        (0)             /* 8000  Hz */
142 #define AGCFR_FSINT_11025       (1)             /* 11025 Hz */
143 #define AGCFR_FSINT_22050       (2)             /* 22050 Hz */
144 #define AGCFR_FSINT_44100       (3)             /* 44100 Hz */
145
146 #define AGCFR_AUD_CKSRC(val)((val & 0x3) << 4)  /* audio processing clock source */
147 #define AGCFR_AUD_CKSRC_BITS    (0x3 << 4)
148 #define AGCFR_M_CKSRC           (1 << 3)        /* modem interface clock source */
149 #define AGCFR_MCLK_OUT          (1 << 1)
150 #define AGCFR_MCLK              (1 << 0)
151
152
153 /* AGCTR */
154 #define AGCTR_AUDRD             (1 << 15)       /* audio ready */
155 #define AGCTR_AUDRDI            (1 << 14)       /* audio ready interrupt status */
156 #define AGCTR_AUDRDIEN          (1 << 13)       /* audio ready interrupt enable */
157 #define AGCTR_DMAREN            (1 << 12)       /* audio files play operation */
158 #define AGCTR_DMAWEN            (1 << 11)       /* audio file record operation */
159 /* bits 10:4 reserved */
160 #define AGCTR_MCLK_EN           (1 << 3)        /* internal MCLK enable */
161 #define AGCTR_OSCMCLK_EN        (1 << 2)        /* OSCMCLK_EN output for MCLK oscillator control */
162 #define AGCTR_AUDEN             (1 << 1)        /* audio processing enable/disable */
163 #define AGCTR_EACPWD            (1 << 0)        /* EAC operation */
164
165 /* AGCFR2 */
166 #define AGCFR2_BT_MD_WIDEBAND   (1 << 5)        /* the BT device and modem AuSPIs wide-band mode */
167 #define AGCFR2_MCLK_I2S_N11M_12M (1 << 4)       /* MCLK freq indicator for audio operations */
168 #define AGCFR2_I2S_N44K_48K     (1 << 3)        /* Frame sample frecuency of I2S codec port, does not generate value */
169 #define AGCFR2_FSINT2(val)      ((val & 0x7) << 0) /* intermediate sample frequency for DMA channel read and write operations */
170 #define AGCFR2_FSINT2_BITS      (0x7 << 0)
171
172 #define AGCFR2_FSINT2_8000      (0)             /* 8000  Hz */
173 #define AGCFR2_FSINT2_11025     (1)             /* 11025 Hz */
174 #define AGCFR2_FSINT2_22050     (2)             /* 22050 Hz */
175 #define AGCFR2_FSINT2_44100     (3)             /* 44100 Hz */
176 #define AGCFR2_FSINT2_48000     (4)             /* 48000 Hz */
177 #define AGCFR2_FSINT2_FSINT     (7)             /* based on AGCFR/FSINT */
178
179
180 /* AGCFR3 */
181 #define AGCFR3_CP_TR_DMA        (1 << 15)       /* codec port transparent DMA (to audio DMAs) */
182 #define AGCFR3_BT_TR_DMA        (1 << 14)       /* BT transparent DMA (to BT UL write & DL read DMAs */     
183 #define AGCFR3_MD_TR_DMA        (1 << 13)       /* modem transparent DMA (to modem UL write and DL read DMAs) */
184 #define AGCFR3_FSINT(val)       ((val & 0xf) << 9) /* FSINT */
185 #define AGCFR3_FSINT_BITS       (0xf << 9)
186
187 #define AGCFR3_FSINT_8000       (0)             /* 8000  Hz */
188 #define AGCFR3_FSINT_11025      (1)             /* 11025 Hz */
189 #define AGCFR3_FSINT_16000      (2)             /* 16000 Hz */
190 #define AGCFR3_FSINT_22050      (3)             /* 22050 Hz */
191 #define AGCFR3_FSINT_24000      (4)             /* 24000 Hz */
192 #define AGCFR3_FSINT_32000      (5)             /* 32000 Hz */
193 #define AGCFR3_FSINT_44100      (6)             /* 44100 Hz */
194 #define AGCFR3_FSINT_48000      (7)             /* 48000 Hz */
195 #define AGCFR3_FSINT_FSINT      (15)            /* based on AGCFR2/AGCFR */
196
197
198 #define AGCFR3_BT_CKSRC(val)    ((val & 0x3) << 7)      /* BT port clock selection */
199 #define AGCFR3_BT_CKSRC_BITS    (0x3 << 7)
200 #define AGCFR3_MD_CKSRC(val)    ((val & 0x3) << 5)      /* modem port clock source */
201 #define AGCFR3_MD_CKSRC_BITS    (0x3 << 5)
202 #define AGCFR3_AUD_CKSRC(val)   ((val & 0x7) << 2)      /* audio and codec port clock source */
203 #define AGCFR3_AUD_CKSRC_BITS   (0x7 << 2)
204 #define AGCFR3_CLK12MINT_SEL    (1 << 1)                /* internal 12MHz clock source */
205 #define AGCFR3_MCLKINT_SEL      (1 << 0)                /* internal codec master clock source */
206
207 /* AMSCFR */
208 #define AMSCFR_K12              (1 << 11)               /* K12 switch open/close */
209 #define AMSCFR_K11              (1 << 10)
210 #define AMSCFR_K10              (1 << 9)
211 #define AMSCFR_K9               (1 << 8)
212 #define AMSCFR_K8               (1 << 7)
213 #define AMSCFR_K7               (1 << 6)
214 #define AMSCFR_K6               (1 << 5)
215 #define AMSCFR_K5               (1 << 4)
216 #define AMSCFR_K4               (1 << 3)
217 #define AMSCFR_K3               (1 << 2)
218 #define AMSCFR_K2               (1 << 1)
219 #define AMSCFR_K1               (1 << 0)
220
221 /* AMVCTR */
222 #define AMVCTR_GWO_BITS         (0xff << 8)
223 #define AMVCTR_GWO(val)         ((val & 0xff) << 8)     /* Gain on write DMA operation */
224 #define AMVCTR_GRO_BITS         (0xff << 0)
225 #define AMVCTR_GRO(val)         ((val & 0xff) << 0)     /* Gain on read DMA operation */
226
227 /* AM1VCTR */
228 #define AM1VCTR_MUTE            (1 << 15)               /* mute/no mute on mixer output */
229 #define AM1VCTR_GINB(val)       ((val & 0x7f) << 8)     /* gain on input B */
230 #define AM1VCTR_GINB_BITS       (0x7f << 8)
231 #define AM1VCTR_GINA(val)       ((val & 0x7f) << 0)     /* gain on input A */
232 #define AM1VCTR_GINA_BITS       (0x7f << 0)
233
234 /* AM2VCTR */
235 #define AM2VCTR_MUTE            (1 << 15)               /* mute/no mute on mixer output */
236 #define AM2VCTR_GINB(val)       ((val & 0x7f) << 8)     /* gain on input B */
237 #define AM2VCTR_GINB_BITS       (0x7f << 8)
238 #define AM2VCTR_GINA(val)       ((val & 0x7f) << 0)     /* gain on input A */
239 #define AM2VCTR_GINA_BITS       (0x7f << 0)
240
241 /* AM3VCTR */
242 #define AM3VCTR_MUTE            (1 << 15)               /* mute/no mute */
243 #define AM3VCTR_GINB(val)       ((val & 0x7f) << 8)     /* gain on input B */
244 #define AM3VCTR_GINB_BITS       (0x7f << 8)
245 #define AM3VCTR_GINA(val)       ((val & 0x7f) << 0)     /* gain on input A */
246 #define AM3VCTR_GINA_BITS       (0x7f << 0)
247
248 /* ASTCTR */
249 #define ASTCTR_ATT(val)         ((val & 0x7f) << 1)     /* Attenuation of side tone */
250 #define ASTCTR_ATT_BITS         (0x7f << 1)
251 #define ASTCTR_ATTEN            (1 << 0)                /* side tone enabled/disabled */
252
253
254 /* internal structure of the EAC driver */
255 struct omap_eac {
256         struct mutex                    mutex;
257         void __iomem *                  base;
258         struct platform_device *        pdev;
259         struct eac_platform_data *      pdata;
260         struct snd_card *               card;
261         struct clk *                    fck;
262         struct clk *                    ick;
263         struct eac_codec *              codec;
264
265         unsigned                        clocks_enabled:1;
266 };
267
268 static char *id = SNDRV_DEFAULT_STR1;
269 module_param(id, charp, 0444);
270 MODULE_PARM_DESC(id, "ID string for OMAP24xx EAC");
271
272
273 #define MOD_REG_BIT(val, mask, set) do { \
274         if (set) \
275                 val |= mask; \
276         else \
277                 val &= ~mask; \
278 } while(0)
279
280 static inline void eac_write_reg(struct omap_eac *eac, int idx, u16 val)
281 {
282         __raw_writew(val, eac->base + idx);
283 }
284
285 static inline u16 eac_read_reg(struct omap_eac *eac, int idx)
286 {
287         return __raw_readw(eac->base + idx);
288 }
289
290 static int eac_get_clocks(struct omap_eac *eac)
291 {
292         eac->ick = clk_get(NULL, "eac_ick");
293         if (IS_ERR(eac->ick)) {
294                 dev_err(&eac->pdev->dev, "Could not get eac_ick");
295                 return -ENODEV;
296         }
297
298         eac->fck = clk_get(NULL, "eac_fck");
299         if (IS_ERR(eac->fck)) {
300                 dev_err(&eac->pdev->dev, "Could not get eac_fck");
301                 clk_put(eac->ick);
302                 return -ENODEV;
303         }
304
305         return 0;
306 }
307
308 static void eac_put_clocks(struct omap_eac *eac)
309 {
310         clk_put(eac->fck);
311         clk_put(eac->ick);
312 }
313
314 static int eac_enable_clocks(struct omap_eac *eac)
315 {
316         int err = 0;
317
318         if (eac->clocks_enabled)
319                 return 0;
320
321         if (eac->pdata != NULL && eac->pdata->enable_ext_clocks != NULL) {
322                 if ((err = eac->pdata->enable_ext_clocks(&eac->pdev->dev)) != 0)
323                         return err;
324         }
325         clk_enable(eac->ick);
326         clk_enable(eac->fck);
327         eac->clocks_enabled = 1;
328
329         return 0;
330 }
331
332 static void eac_disable_clocks(struct omap_eac *eac)
333 {
334         if (!eac->clocks_enabled)
335                 return;
336         eac->clocks_enabled = 0;
337
338         clk_disable(eac->fck);
339         clk_disable(eac->ick);
340         if (eac->pdata != NULL && eac->pdata->disable_ext_clocks != NULL)
341                 eac->pdata->disable_ext_clocks(&eac->pdev->dev);
342 }
343
344 static int eac_reset(struct omap_eac *eac)
345 {
346         int i;
347
348         /* step 1 (see TRM) */
349         /* first, let's reset the EAC */
350         eac_write_reg(eac, EAC_SYSCONFIG, 0x2);
351         /* step 2 (see TRM) */
352         eac_write_reg(eac, EAC_AGCTR, AGCTR_MCLK_EN | AGCTR_AUDEN);
353         /* step 3 (see TRM) */
354         /* wait until reset done */
355         i = 10000;
356         while (!(eac_read_reg(eac, EAC_SYSSTATUS) & 1)) {
357                 if (--i == 0)
358                         return -ENODEV;
359                 udelay(1);
360         }
361
362         return 0;
363 }
364
365 static int eac_calc_agcfr3_fsint(int rate)
366 {
367         int fsint;
368
369         if (rate >= 48000)
370                 fsint = AGCFR3_FSINT_48000;
371         else if (rate >= 44100)
372                 fsint = AGCFR3_FSINT_44100;
373         else if (rate >= 32000)
374                 fsint = AGCFR3_FSINT_32000;
375         else if (rate >= 24000)
376                 fsint = AGCFR3_FSINT_24000;
377         else if (rate >= 22050)
378                 fsint = AGCFR3_FSINT_22050;
379         else if (rate >= 16000)
380                 fsint = AGCFR3_FSINT_16000;
381         else if (rate >= 11025)
382                 fsint = AGCFR3_FSINT_11025;
383         else
384                 fsint = AGCFR3_FSINT_8000;
385
386         return fsint;
387 }
388
389 static int eac_configure_pcm(struct omap_eac *eac, struct eac_codec *conf)
390 {
391         dev_err(&eac->pdev->dev,
392                 "EAC codec port configuration for PCM not implemented\n");
393
394         return -ENODEV;
395 }
396
397 static int eac_configure_ac97(struct omap_eac *eac, struct eac_codec *conf)
398 {
399         dev_err(&eac->pdev->dev,
400                 "EAC codec port configuration for AC97 not implemented\n");
401
402         return -ENODEV;
403 }
404
405 static int eac_configure_i2s(struct omap_eac *eac, struct eac_codec *conf)
406 {
407         u16 cpcfr1, cpcfr2, cpcfr3, cpcfr4;
408
409         cpcfr1 = eac_read_reg(eac, EAC_CPCFR1);
410         cpcfr2 = eac_read_reg(eac, EAC_CPCFR2);
411         cpcfr3 = eac_read_reg(eac, EAC_CPCFR3);
412         cpcfr4 = eac_read_reg(eac, EAC_CPCFR4);
413
414         cpcfr1 &= ~(CPCFR1_MODE_BITS | CPCFR1_MTSL_BITS);
415         cpcfr1 |= CPCFR1_MTSL(1); /* 2 timeslots per frame (I2S default) */
416
417         /* audio time slot configuration for I2S mode */
418         cpcfr2 &= ~(CPCFR2_TSLL_BITS | CPCFR2_BPTSL_BITS | CPCFR2_TSLOL_BITS);
419         cpcfr2 |= CPCFR2_TSLOL(0); /* time slot 0 length same as TSLL */
420         cpcfr2 |= CPCFR2_BPTSL(1); /* 16 data bits per time slot */
421         cpcfr2 |= CPCFR2_TSLL(1); /* time slot length 16 serial clock cycles */
422
423         /* I2S link configuration */
424         MOD_REG_BIT(cpcfr3, CPCFR3_DDLY,
425                 conf->codec_conf.i2s.sync_delay_enable); /* 0/1 clk delay */
426         /* data serial output enabled during nonvalid audio frames, clock
427          * polarity = falling edge, CSYNC lenght equal to time slot0 length */
428         MOD_REG_BIT(cpcfr3, CPCFR3_TRSEN, 1);
429         MOD_REG_BIT(cpcfr3, CPCFR3_CLKBP, 1);
430         MOD_REG_BIT(cpcfr3, CPCFR3_CSYNCL, 1);
431
432         cpcfr4 &= ~(CPCFR4_DIVB_BITS | CPCFR4_ATSL_BITS);
433         cpcfr4 |= CPCFR4_DIVB(7); /* CP_SCLK = MCLK / 8 */
434
435         /* configuration for normal I2S or polarity-changed I2S */
436         if (!conf->codec_conf.i2s.polarity_changed_mode) {
437                 cpcfr1 |= CPCFR1_MODE(4); /* I2S mode */
438                 MOD_REG_BIT(cpcfr3, CPCFR3_CSYNCP, 0); /* CP_SYNC active low */
439                 /* audio time slots configuration for I2S */
440                 cpcfr4 |= CPCFR4_ATSL(0);
441         } else {
442                 cpcfr1 |= CPCFR1_MODE(1); /* PCM mode/polarity-changed I2S */
443                 MOD_REG_BIT(cpcfr3, CPCFR3_CSYNCP, 1); /* CP_SYNC active
444                                                           high */
445                 /* audio time slots configuration for polarity-changed I2S */
446                 cpcfr4 |= CPCFR4_ATSL(0xf);
447         };
448
449         /* master/slave configuration */
450         if (conf->codec_mode == EAC_CODEC_I2S_MASTER) {
451                 /* EAC is master. Set CP_SCLK and CP_SYNC as outputs */
452                 MOD_REG_BIT(cpcfr3, CPCFR3_CSCLKD, 0);
453                 MOD_REG_BIT(cpcfr3, CPCFR3_CSYNCD, 0);
454         } else {
455                 /* EAC is slave. Set CP_SCLK and CP_SYNC as inputs */
456                 MOD_REG_BIT(cpcfr3, CPCFR3_CSCLKD, 1);
457                 MOD_REG_BIT(cpcfr3, CPCFR3_CSYNCD, 1);
458         }
459
460         eac_write_reg(eac, EAC_CPCFR1, cpcfr1);
461         eac_write_reg(eac, EAC_CPCFR2, cpcfr2);
462         eac_write_reg(eac, EAC_CPCFR3, cpcfr3);
463         eac_write_reg(eac, EAC_CPCFR4, cpcfr4);
464
465         return 0;
466 }
467
468 static int eac_codec_port_init(struct omap_eac *eac, struct eac_codec *conf)
469 {
470         u16 agcfr, agcfr2, agcfr3, agctr;
471         u16 cpctl, reg;
472         int err = 0, i;
473
474         /* use internal MCLK gating before doing full configuration for it.
475          * Partial or misconfigured MCLK will cause that access to some of the
476          * EAC registers causes "external abort on linefetch". Same happens
477          * also when using external clock as a MCLK source and if that clock is
478          * either missing or not having a right rate (e.g. half of it) */
479         agcfr3 = eac_read_reg(eac, EAC_AGCFR3);
480         MOD_REG_BIT(agcfr3, AGCFR3_MCLKINT_SEL, 1); /* 96 Mhz / 8.5 */
481         eac_write_reg(eac, EAC_AGCFR3, agcfr3);
482
483         /* disable codec port, enable access to config registers */
484         cpctl = eac_read_reg(eac, EAC_CPTCTL);
485         MOD_REG_BIT(cpctl, CPTCTL_CPEN, 0);
486         eac_write_reg(eac, EAC_CPTCTL, cpctl);
487
488         agcfr = eac_read_reg(eac, EAC_AGCFR);
489         agctr = eac_read_reg(eac, EAC_AGCTR);
490         agcfr2 = eac_read_reg(eac, EAC_AGCFR2);
491
492         /* MCLK source and frequency configuration */
493         MOD_REG_BIT(agcfr, AGCFR_MCLK, 0);
494         switch (conf->mclk_src) {
495         case EAC_MCLK_EXT_2x11289600:
496                 MOD_REG_BIT(agcfr, AGCFR_MCLK, 1); /* div by 2 path */
497                 MOD_REG_BIT(agcfr, AGCFR_MCLK_OUT, 1); /* div by 2 */
498         case EAC_MCLK_EXT_11289600:
499                 MOD_REG_BIT(agcfr, AGCFR_MCLK, 1);
500                 MOD_REG_BIT(agcfr2, AGCFR2_I2S_N44K_48K, 0); /* 44.1 kHz */
501                 MOD_REG_BIT(agcfr2, AGCFR2_MCLK_I2S_N11M_12M, 0); /* 11.2896 */
502                 MOD_REG_BIT(agcfr3, AGCFR3_MCLKINT_SEL, 0);
503                 break;
504
505         case EAC_MCLK_EXT_2x12288000:
506                 MOD_REG_BIT(agcfr, AGCFR_MCLK, 1); /* div by 2 path */
507                 MOD_REG_BIT(agcfr, AGCFR_MCLK_OUT, 1); /* div by 2 */
508         case EAC_MCLK_EXT_12288000:
509                 MOD_REG_BIT(agcfr2, AGCFR2_I2S_N44K_48K, 1); /* 48 kHz */
510                 MOD_REG_BIT(agcfr2, AGCFR2_MCLK_I2S_N11M_12M, 1); /* 12.288 */
511                 MOD_REG_BIT(agcfr3, AGCFR3_MCLKINT_SEL, 0);
512                 break;
513
514         default:
515                 /* internal MCLK gating */
516                 break;
517         }
518         MOD_REG_BIT(agctr, AGCTR_MCLK_EN, 1);
519         MOD_REG_BIT(agctr, AGCTR_OSCMCLK_EN, 1); /* oscillator enabled? */
520         /* use MCLK just configured above as audio & codec port clock source */
521         agcfr3 &= ~AGCFR3_AUD_CKSRC_BITS;
522         agcfr3 |= AGCFR3_AUD_CKSRC(0);
523
524         /* audio data format */
525         MOD_REG_BIT(agcfr, AGCFR_MN_ST, 1);     /* stereo file */
526         MOD_REG_BIT(agcfr, AGCFR_B8_16, 1);     /* 16 bit audio file */
527         MOD_REG_BIT(agcfr, AGCFR_LI_BI, 0);     /* little endian stream */
528
529         /* there are FSINT configuration bits in AGCFR, AGCFR2 and AGCFR3
530          * registers but it seems that it is just enough to set in AGCFR3
531          * only */
532         agcfr3 &= ~AGCFR3_FSINT_BITS;
533         agcfr3 |= AGCFR3_FSINT(eac_calc_agcfr3_fsint(conf->default_rate));
534
535         /* transparent DMA enable bits */
536         MOD_REG_BIT(agcfr3, AGCFR3_MD_TR_DMA, 1); /* modem */
537         MOD_REG_BIT(agcfr3, AGCFR3_BT_TR_DMA, 1); /* BT */
538         if (conf->codec_mode != EAC_CODEC_I2S_SLAVE)
539                 MOD_REG_BIT(agcfr3, AGCFR3_CP_TR_DMA, 0);
540         else
541                 MOD_REG_BIT(agcfr3, AGCFR3_CP_TR_DMA, 1);
542
543         /* step 4 (see TRM) */
544         eac_write_reg(eac, EAC_AGCFR3, agcfr3);
545         /* pre-write AGCTR now (finally in step 10) in order to get MCLK
546          * settings effective (especially when using external MCLK) */
547         eac_write_reg(eac, EAC_AGCTR, agctr);
548         eac_write_reg(eac, EAC_AGCFR2, agcfr2);
549
550         /* step 5 (see TRM) */
551         eac_write_reg(eac, EAC_AGCFR, agcfr);
552
553         /* step 6 (see TRM) */
554         /* wait until audio reset done */
555         i = 10000;
556         while (!(eac_read_reg(eac, EAC_SYSSTATUS) & (1 << 3))) {
557                 if (--i == 0)
558                         return -ETIMEDOUT;
559                 udelay(1);
560         }
561
562         /* step 7 (see TRM) */
563         reg = eac_read_reg(eac, EAC_AMSCFR);
564         MOD_REG_BIT(reg, AMSCFR_K1, 1);         /* K1 switch closed */
565         MOD_REG_BIT(reg, AMSCFR_K5, 1);         /* K5 switch closed */
566         MOD_REG_BIT(reg, AMSCFR_K2, 0);         /* K2 switch open */
567         MOD_REG_BIT(reg, AMSCFR_K6, 0);         /* K6 switch open */
568         eac_write_reg(eac, EAC_AMSCFR, reg);
569
570         /* step 8 (see TRM) */
571         switch (conf->codec_mode) {
572         case EAC_CODEC_PCM:
573                 err = eac_configure_pcm(eac, conf);
574                 break;
575         case EAC_CODEC_AC97:
576                 err = eac_configure_ac97(eac, conf);
577                 break;
578         default:
579                 err = eac_configure_i2s(eac, conf);
580                 break;
581         }
582
583         /* step 9 (see TRM) */
584         MOD_REG_BIT(cpctl, CPTCTL_CPEN, 1);     /* codec port enable */
585         MOD_REG_BIT(cpctl, CPTCTL_RXIE, 1);     /* receive int enable */
586         MOD_REG_BIT(cpctl, CPTCTL_TXIE, 1);     /* transmit int enable */
587         eac_write_reg(eac, EAC_CPTCTL, cpctl);
588
589         /* step 10 (see TRM) */
590         /* enable playing & recording */
591         MOD_REG_BIT(agctr, AGCTR_DMAREN, 1);    /* playing enabled (DMA R) */
592         MOD_REG_BIT(agctr, AGCTR_DMAWEN, 1);    /* recording enabled (DMA W) */
593         MOD_REG_BIT(agctr, AGCTR_AUDEN, 1);     /* audio processing enabled */
594         eac_write_reg(eac, EAC_AGCTR, agctr);
595
596         /* audio mixer1, no mute on mixer output, gain = 0 dB */
597         reg = eac_read_reg(eac, EAC_AM1VCTR);
598         MOD_REG_BIT(reg, AM1VCTR_MUTE, 0);
599         reg = ((reg & ~AM1VCTR_GINB_BITS) | (AM1VCTR_GINB(0x67)));
600         eac_write_reg(eac, EAC_AM1VCTR, reg);
601
602         /* audio mixer3, no mute on mixer output, gain = 0 dB */
603         reg = eac_read_reg(eac, EAC_AM3VCTR);
604         MOD_REG_BIT(reg, AM3VCTR_MUTE, 0);
605         reg = ((reg & ~AM3VCTR_GINB_BITS) | (AM3VCTR_GINB(0x67)));
606         eac_write_reg(eac, EAC_AM3VCTR, reg);
607
608         /* audio side tone disabled */
609         eac_write_reg(eac, EAC_ASTCTR, 0x0);
610
611         return 0;
612 }
613
614 int eac_set_mode(struct device *dev, int play, int rec)
615 {
616         struct omap_eac *eac = dev_get_drvdata(dev);
617
618 #ifdef DEBUG
619         printk(KERN_DEBUG "EAC mode: play %s, rec %s\n",
620                play ? "enabled" : "disabled",
621                rec  ? "enabled" : "disabled");
622 #endif
623         BUG_ON(eac == NULL);
624         mutex_lock(&eac->mutex);
625         if (play || rec) {
626                 /* activate clocks */
627                 eac_enable_clocks(eac);
628
629                 /* power-up codec */
630                 if (eac->codec != NULL && eac->codec->set_power != NULL)
631                         eac->codec->set_power(eac->codec->private_data,
632                                 play, rec);
633         } else {
634                 /* shutdown codec */
635                 if (eac->codec != NULL && eac->codec->set_power != NULL)
636                         eac->codec->set_power(eac->codec->private_data, 0, 0);
637
638                 /* de-activate clocks */
639                 eac_disable_clocks(eac);
640         }
641         mutex_unlock(&eac->mutex);
642
643         return 0;
644 }
645
646 int eac_register_codec(struct device *dev, struct eac_codec *codec)
647 {
648         struct omap_eac *eac = dev_get_drvdata(dev);
649         struct snd_card *card = eac->card;
650         int err;
651
652         BUG_ON(eac->codec != NULL);
653
654         mutex_lock(&eac->mutex);
655         eac->codec = codec;
656         eac_enable_clocks(eac);
657         err = eac_codec_port_init(eac, codec);
658         eac_disable_clocks(eac);
659         mutex_unlock(&eac->mutex);
660         if (err)
661                 return err;
662
663         /* register mixer controls implemented by a codec driver */
664         if (codec->register_controls != NULL) {
665                 err = codec->register_controls(codec->private_data, card);
666                 if (err)
667                         return err;
668         }
669
670         if (codec->short_name != NULL) {
671                 sprintf(card->longname, "%s with codec %s", card->shortname,
672                         codec->short_name);
673                 strcpy(card->mixername, codec->short_name);
674         }
675
676         err = snd_card_register(card);
677         return err;
678 }
679
680 void eac_unregister_codec(struct device *dev)
681 {
682         struct omap_eac *eac = dev_get_drvdata(dev);
683
684         BUG_ON(eac->codec == NULL);
685         eac_set_mode(dev, 0, 0);
686         snd_card_disconnect(eac->card);
687         eac->codec = NULL;
688 }
689
690 static int __devinit eac_probe(struct platform_device *pdev)
691 {
692         struct eac_platform_data *pdata = pdev->dev.platform_data;
693         struct snd_card *card;
694         struct omap_eac *eac;
695         struct resource *res;
696         int err;
697
698         eac = kzalloc(sizeof(*eac), GFP_KERNEL);
699         if (!eac)
700                 return -ENOMEM;
701
702         mutex_init(&eac->mutex);
703         eac->pdev = pdev;
704         platform_set_drvdata(pdev, eac);
705         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
706         if (res == NULL) {
707                 err = -ENODEV;
708                 goto err1;
709         }
710         eac->base = (void __iomem *)io_p2v(res->start);
711         eac->pdata = pdata;
712
713         /* pre-initialize EAC hw */
714         err = eac_get_clocks(eac);
715         if (err)
716                 goto err1;
717         err = eac_enable_clocks(eac);
718         if (err)
719                 goto err2;
720
721         err = eac_reset(eac);
722         if (err)
723                 goto err3;
724
725         dev_info(&pdev->dev, "EAC version: %d.%d\n",
726                  eac_read_reg(eac, EAC_VERSION) >> 4,
727                  eac_read_reg(eac, EAC_VERSION) & 0x0f);
728         eac_disable_clocks(eac);
729
730         /* create soundcard instance */
731         card = snd_card_new(-1, id, THIS_MODULE, 0);
732         if (card == NULL) {
733                 err = -ENOMEM;
734                 goto err3;
735         }
736         eac->card = card;
737         strcpy(card->driver, "EAC");
738         strcpy(card->shortname, "OMAP24xx EAC");
739
740         sprintf(card->longname, "%s", card->shortname);
741         strcpy(card->mixername, "EAC Mixer");
742
743         if (eac->pdata->init) {
744                 err = eac->pdata->init(&pdev->dev);
745                 if (err < 0) {
746                         printk("init %d\n", err);
747                         goto err4;
748                 }
749         }
750
751         return 0;
752
753 err4:
754         snd_card_free(card);
755 err3:
756         eac_disable_clocks(eac);
757 err2:
758         eac_put_clocks(eac);
759 err1:
760         kfree(eac);
761         return err;
762 }
763
764 static int __devexit eac_remove(struct platform_device *pdev)
765 {
766         struct omap_eac *eac = platform_get_drvdata(pdev);
767         struct snd_card *card = eac->card;
768
769         snd_card_free(card);
770
771         eac_disable_clocks(eac);
772         eac_put_clocks(eac);
773
774         platform_set_drvdata(pdev, NULL);
775
776         return 0;
777 }
778
779 static struct platform_driver eac_driver = {
780         .driver = {
781                 .name           = "omap24xx-eac",
782                 .bus            = &platform_bus_type,
783         },
784         .probe          = eac_probe,
785         .remove         = eac_remove,
786 };
787
788 int __init eac_init(void)
789 {
790         return platform_driver_register(&eac_driver);
791 }
792
793 void __exit eac_exit(void)
794 {
795         platform_driver_unregister(&eac_driver);
796 }
797
798 module_init(eac_init);
799 module_exit(eac_exit);
800 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>");
801 MODULE_LICENSE("GPL");