[ALSA] Clean up and add TLV support to AK4xxx i2c driver
[linux-2.6.git] / sound / pci / ice1712 / revo.c
1 /*
2  *   ALSA driver for ICEnsemble ICE1712 (Envy24)
3  *
4  *   Lowlevel functions for M-Audio Revolution 7.1
5  *
6  *      Copyright (c) 2003 Takashi Iwai <tiwai@suse.de>
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  *
22  */      
23
24 #include <sound/driver.h>
25 #include <asm/io.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <sound/core.h>
31
32 #include "ice1712.h"
33 #include "envy24ht.h"
34 #include "revo.h"
35
36 static void revo_i2s_mclk_changed(struct snd_ice1712 *ice)
37 {
38         /* assert PRST# to converters; MT05 bit 7 */
39         outb(inb(ICEMT1724(ice, AC97_CMD)) | 0x80, ICEMT1724(ice, AC97_CMD));
40         mdelay(5);
41         /* deassert PRST# */
42         outb(inb(ICEMT1724(ice, AC97_CMD)) & ~0x80, ICEMT1724(ice, AC97_CMD));
43 }
44
45 /*
46  * change the rate of envy24HT, AK4355 and AK4381
47  */
48 static void revo_set_rate_val(struct snd_akm4xxx *ak, unsigned int rate)
49 {
50         unsigned char old, tmp, dfs;
51         int reg, shift;
52
53         if (rate == 0)  /* no hint - S/PDIF input is master, simply return */
54                 return;
55
56         /* adjust DFS on codecs */
57         if (rate > 96000)
58                 dfs = 2;
59         else if (rate > 48000)
60                 dfs = 1;
61         else
62                 dfs = 0;
63
64         if (ak->type == SND_AK4355 || ak->type == SND_AK4358) {
65                 reg = 2;
66                 shift = 4;
67         } else {
68                 reg = 1;
69                 shift = 3;
70         }
71         tmp = snd_akm4xxx_get(ak, 0, reg);
72         old = (tmp >> shift) & 0x03;
73         if (old == dfs)
74                 return;
75
76         /* reset DFS */
77         snd_akm4xxx_reset(ak, 1);
78         tmp = snd_akm4xxx_get(ak, 0, reg);
79         tmp &= ~(0x03 << shift);
80         tmp |= dfs << shift;
81         // snd_akm4xxx_write(ak, 0, reg, tmp);
82         snd_akm4xxx_set(ak, 0, reg, tmp); /* the value is written in reset(0) */
83         snd_akm4xxx_reset(ak, 0);
84 }
85
86 /*
87  * initialize the chips on M-Audio Revolution cards
88  */
89
90 #define AK_DAC(xname,xch) { .name = xname, .num_channels = xch }
91
92 static struct snd_akm4xxx_dac_channel revo71_front[] = {
93         AK_DAC("PCM Playback Volume", 2)
94 };
95
96 static struct snd_akm4xxx_dac_channel revo71_surround[] = {
97         AK_DAC("PCM Center Playback Volume", 1),
98         AK_DAC("PCM LFE Playback Volume", 1),
99         AK_DAC("PCM Side Playback Volume", 2),
100         AK_DAC("PCM Rear Playback Volume", 2),
101 };
102
103 static struct snd_akm4xxx_dac_channel revo51_dac[] = {
104         AK_DAC("PCM Playback Volume", 2),
105         AK_DAC("PCM Center Playback Volume", 1),
106         AK_DAC("PCM LFE Playback Volume", 1),
107         AK_DAC("PCM Rear Playback Volume", 2),
108 };
109
110 static struct snd_akm4xxx_adc_channel revo51_adc[] = {
111         {
112                 .name = "PCM Capture Volume",
113                 .gain_name = "PCM Capture Gain Volume",
114                 .switch_name = "PCM Capture Switch",
115                 .num_channels = 2
116         },
117 };
118
119 static struct snd_akm4xxx akm_revo_front __devinitdata = {
120         .type = SND_AK4381,
121         .num_dacs = 2,
122         .ops = {
123                 .set_rate_val = revo_set_rate_val
124         },
125         .dac_info = revo71_front,
126 };
127
128 static struct snd_ak4xxx_private akm_revo_front_priv __devinitdata = {
129         .caddr = 1,
130         .cif = 0,
131         .data_mask = VT1724_REVO_CDOUT,
132         .clk_mask = VT1724_REVO_CCLK,
133         .cs_mask = VT1724_REVO_CS0 | VT1724_REVO_CS1 | VT1724_REVO_CS2,
134         .cs_addr = VT1724_REVO_CS0 | VT1724_REVO_CS2,
135         .cs_none = VT1724_REVO_CS0 | VT1724_REVO_CS1 | VT1724_REVO_CS2,
136         .add_flags = VT1724_REVO_CCLK, /* high at init */
137         .mask_flags = 0,
138 };
139
140 static struct snd_akm4xxx akm_revo_surround __devinitdata = {
141         .type = SND_AK4355,
142         .idx_offset = 1,
143         .num_dacs = 6,
144         .ops = {
145                 .set_rate_val = revo_set_rate_val
146         },
147         .dac_info = revo71_surround,
148 };
149
150 static struct snd_ak4xxx_private akm_revo_surround_priv __devinitdata = {
151         .caddr = 3,
152         .cif = 0,
153         .data_mask = VT1724_REVO_CDOUT,
154         .clk_mask = VT1724_REVO_CCLK,
155         .cs_mask = VT1724_REVO_CS0 | VT1724_REVO_CS1 | VT1724_REVO_CS2,
156         .cs_addr = VT1724_REVO_CS0 | VT1724_REVO_CS1,
157         .cs_none = VT1724_REVO_CS0 | VT1724_REVO_CS1 | VT1724_REVO_CS2,
158         .add_flags = VT1724_REVO_CCLK, /* high at init */
159         .mask_flags = 0,
160 };
161
162 static struct snd_akm4xxx akm_revo51 __devinitdata = {
163         .type = SND_AK4358,
164         .num_dacs = 6,
165         .ops = {
166                 .set_rate_val = revo_set_rate_val
167         },
168         .dac_info = revo51_dac,
169 };
170
171 static struct snd_ak4xxx_private akm_revo51_priv __devinitdata = {
172         .caddr = 2,
173         .cif = 0,
174         .data_mask = VT1724_REVO_CDOUT,
175         .clk_mask = VT1724_REVO_CCLK,
176         .cs_mask = VT1724_REVO_CS0 | VT1724_REVO_CS1 | VT1724_REVO_CS2,
177         .cs_addr = VT1724_REVO_CS1 | VT1724_REVO_CS2,
178         .cs_none = VT1724_REVO_CS0 | VT1724_REVO_CS1 | VT1724_REVO_CS2,
179         .add_flags = VT1724_REVO_CCLK, /* high at init */
180         .mask_flags = 0,
181 };
182
183 static struct snd_akm4xxx akm_revo51_adc __devinitdata = {
184         .type = SND_AK5365,
185         .num_adcs = 2,
186         .adc_info = revo51_adc,
187 };
188
189 static struct snd_ak4xxx_private akm_revo51_adc_priv __devinitdata = {
190         .caddr = 2,
191         .cif = 0,
192         .data_mask = VT1724_REVO_CDOUT,
193         .clk_mask = VT1724_REVO_CCLK,
194         .cs_mask = VT1724_REVO_CS0 | VT1724_REVO_CS1 | VT1724_REVO_CS2,
195         .cs_addr = VT1724_REVO_CS0 | VT1724_REVO_CS2,
196         .cs_none = VT1724_REVO_CS0 | VT1724_REVO_CS1 | VT1724_REVO_CS2,
197         .add_flags = VT1724_REVO_CCLK, /* high at init */
198         .mask_flags = 0,
199 };
200
201 static int __devinit revo_init(struct snd_ice1712 *ice)
202 {
203         struct snd_akm4xxx *ak;
204         int err;
205
206         /* determine I2C, DACs and ADCs */
207         switch (ice->eeprom.subvendor) {
208         case VT1724_SUBDEVICE_REVOLUTION71:
209                 ice->num_total_dacs = 8;
210                 ice->num_total_adcs = 2;
211                 ice->gpio.i2s_mclk_changed = revo_i2s_mclk_changed;
212                 break;
213         case VT1724_SUBDEVICE_REVOLUTION51:
214                 ice->num_total_dacs = 6;
215                 ice->num_total_adcs = 2;
216                 break;
217         default:
218                 snd_BUG();
219                 return -EINVAL;
220         }
221
222         /* second stage of initialization, analog parts and others */
223         ak = ice->akm = kcalloc(2, sizeof(struct snd_akm4xxx), GFP_KERNEL);
224         if (! ak)
225                 return -ENOMEM;
226         ice->akm_codecs = 2;
227         switch (ice->eeprom.subvendor) {
228         case VT1724_SUBDEVICE_REVOLUTION71:
229                 ice->akm_codecs = 2;
230                 if ((err = snd_ice1712_akm4xxx_init(ak, &akm_revo_front, &akm_revo_front_priv, ice)) < 0)
231                         return err;
232                 if ((err = snd_ice1712_akm4xxx_init(ak + 1, &akm_revo_surround, &akm_revo_surround_priv, ice)) < 0)
233                         return err;
234                 /* unmute all codecs */
235                 snd_ice1712_gpio_write_bits(ice, VT1724_REVO_MUTE, VT1724_REVO_MUTE);
236                 break;
237         case VT1724_SUBDEVICE_REVOLUTION51:
238                 ice->akm_codecs = 2;
239                 if ((err = snd_ice1712_akm4xxx_init(ak, &akm_revo51, &akm_revo51_priv, ice)) < 0)
240                         return err;
241                 err = snd_ice1712_akm4xxx_init(ak + 1, &akm_revo51_adc,
242                                                &akm_revo51_adc_priv, ice);
243                 if (err < 0)
244                         return err;
245                 /* unmute all codecs - needed! */
246                 snd_ice1712_gpio_write_bits(ice, VT1724_REVO_MUTE, VT1724_REVO_MUTE);
247                 break;
248         }
249
250         return 0;
251 }
252
253
254 static int __devinit revo_add_controls(struct snd_ice1712 *ice)
255 {
256         int err;
257
258         switch (ice->eeprom.subvendor) {
259         case VT1724_SUBDEVICE_REVOLUTION71:
260         case VT1724_SUBDEVICE_REVOLUTION51:
261                 err = snd_ice1712_akm4xxx_build_controls(ice);
262                 if (err < 0)
263                         return err;
264         }
265         return 0;
266 }
267
268 /* entry point */
269 struct snd_ice1712_card_info snd_vt1724_revo_cards[] __devinitdata = {
270         {
271                 .subvendor = VT1724_SUBDEVICE_REVOLUTION71,
272                 .name = "M Audio Revolution-7.1",
273                 .model = "revo71",
274                 .chip_init = revo_init,
275                 .build_controls = revo_add_controls,
276         },
277         {
278                 .subvendor = VT1724_SUBDEVICE_REVOLUTION51,
279                 .name = "M Audio Revolution-5.1",
280                 .model = "revo51",
281                 .chip_init = revo_init,
282                 .build_controls = revo_add_controls,
283         },
284         { } /* terminator */
285 };