Import changeset
[linux-flexiantxendom0-3.2.10.git] / drivers / sound / emu10k1 / ecard.c
1 /*     
2  **********************************************************************
3  *     ecard.c - E-card initialization code
4  *     Copyright 1999, 2000 Creative Labs, Inc. 
5  * 
6  ********************************************************************** 
7  * 
8  *     Date                 Author          Summary of changes 
9  *     ----                 ------          ------------------ 
10  *     October 20, 1999     Bertrand Lee    base code release 
11  * 
12  ********************************************************************** 
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 as 
16  *     published by the Free Software Foundation; either version 2 of 
17  *     the License, or (at your option) any later version. 
18  * 
19  *     This program is distributed in the hope that it will be useful, 
20  *     but WITHOUT ANY WARRANTY; without even the implied warranty of 
21  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
22  *     GNU General Public License for more details. 
23  * 
24  *     You should have received a copy of the GNU General Public 
25  *     License along with this program; if not, write to the Free 
26  *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 
27  *     USA. 
28  * 
29  ********************************************************************** 
30  */ 
31
32 #include "ecard.h"
33 #include "hwaccess.h"
34
35 /* Private routines */
36 static void ecard_setadcgain(struct emu10k1_card *, struct ecard_state *, u16);
37 static void ecard_write(struct emu10k1_card *, u32);
38
39 /**************************************************************************
40  * @func Set the gain of the ECARD's CS3310 Trim/gain controller.  The
41  * trim value consists of a 16bit value which is composed of two
42  * 8 bit gain/trim values, one for the left channel and one for the
43  * right channel.  The following table maps from the Gain/Attenuation
44  * value in decibels into the corresponding bit pattern for a single
45  * channel.
46  */
47
48 static void ecard_setadcgain(struct emu10k1_card *card, struct ecard_state *ecard, u16 gain)
49 {
50         u32 currbit;
51         ecard->adc_gain = gain;
52
53         /* Enable writing to the TRIM registers */
54         ecard_write(card, ecard->control_bits & ~EC_TRIM_CSN);
55
56         /* Do it again to insure that we meet hold time requirements */
57         ecard_write(card, ecard->control_bits & ~EC_TRIM_CSN);
58
59         for (currbit = (1L << 15); currbit; currbit >>= 1) {
60
61                 u32 value = ecard->control_bits & ~(EC_TRIM_CSN|EC_TRIM_SDATA);
62
63                 if (gain & currbit)
64                       value |= EC_TRIM_SDATA;
65
66                 /* Clock the bit */
67                 ecard_write(card, value);
68                 ecard_write(card, value | EC_TRIM_SCLK);
69                 ecard_write(card, value);
70         }
71
72         ecard_write(card, ecard->control_bits);
73 }
74
75 /**************************************************************************
76  * @func Clock bits into the Ecard's control latch.  The Ecard uses a
77  *  control latch will is loaded bit-serially by toggling the Modem control
78  *  lines from function 2 on the E8010.  This function hides these details
79  *  and presents the illusion that we are actually writing to a distinct
80  *  register.
81  */
82 static void ecard_write(struct emu10k1_card *card, u32 value)
83 {
84         u16 count;
85         u32 data, hcvalue;
86
87         hcvalue = emu10k1_readfn0(card, HCFG) & ~(HOOKN_BIT|HANDN_BIT|PULSEN_BIT);
88
89         emu10k1_writefn0(card, HCFG, hcvalue);
90
91         for (count = 0 ; count < EC_NUM_CONTROL_BITS; count++) {
92         
93                 /* Set up the value */
94                 data = ((value & 0x1) ? PULSEN_BIT : 0);
95                 value >>= 1;
96
97                 emu10k1_writefn0(card, HCFG, hcvalue | data);
98
99                 /* Clock the shift register */
100                 emu10k1_writefn0(card, HCFG, hcvalue | data | HANDN_BIT);
101                 emu10k1_writefn0(card, HCFG, hcvalue | data);
102         }
103
104         /* Latch the bits */
105         emu10k1_writefn0(card, HCFG, hcvalue | HOOKN_BIT);
106         emu10k1_writefn0(card, HCFG, hcvalue);
107 }
108
109 int __devinit emu10k1_ecard_init(struct emu10k1_card *card)
110 {
111         u32 hcvalue;
112         struct ecard_state ecard;
113
114         /* Set up the initial settings */
115         ecard.mux0_setting = EC_DEFAULT_SPDIF0_SEL;
116         ecard.mux1_setting = EC_DEFAULT_SPDIF1_SEL;
117         ecard.mux2_setting = 0;
118         ecard.adc_gain = EC_DEFAULT_ADC_GAIN;
119         ecard.control_bits = EC_RAW_RUN_MODE | 
120                              EC_SPDIF0_SELECT(ecard.mux0_setting) |
121                              EC_SPDIF1_SELECT(ecard.mux1_setting);
122
123
124         /* Step 0: Set the codec type in the hardware control register 
125          * and enable audio output */
126         hcvalue = emu10k1_readfn0(card, HCFG);
127         emu10k1_writefn0(card, HCFG, hcvalue | HCFG_AUDIOENABLE | HCFG_CODECFORMAT_I2S);
128         emu10k1_readfn0(card, HCFG);
129
130         /* Step 1: Turn off the led and deassert TRIM_CS */
131         ecard_write(card, EC_ADCCAL | EC_LEDN | EC_TRIM_CSN);
132
133         /* Step 2: Calibrate the ADC and DAC */
134         ecard_write(card, EC_DACCAL | EC_LEDN | EC_TRIM_CSN);
135
136         /* Step 3: Wait for awhile; FIXME: Is this correct? */
137
138         current->state = TASK_INTERRUPTIBLE;
139         schedule_timeout(HZ);
140
141         /* Step 4: Switch off the DAC and ADC calibration.  Note
142          * That ADC_CAL is actually an inverted signal, so we assert
143          * it here to stop calibration.  */
144         ecard_write(card, EC_ADCCAL | EC_LEDN | EC_TRIM_CSN);
145
146         /* Step 4: Switch into run mode */
147         ecard_write(card, ecard.control_bits);
148
149         /* Step 5: Set the analog input gain */
150         ecard_setadcgain(card, &ecard, ecard.adc_gain);
151         
152         return 0;
153 }
154
155