Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / sound / pci / oxygen / oxygen.c
1 /*
2  * C-Media CMI8788 driver for C-Media's reference design and for the X-Meridian
3  *
4  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5  *
6  *
7  *  This driver is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License, version 2.
9  *
10  *  This driver is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this driver; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  */
19
20 /*
21  * SPI 0 -> 1st AK4396 (front)
22  * SPI 1 -> 2nd AK4396 (surround)
23  * SPI 2 -> 3rd AK4396 (center/LFE)
24  * SPI 3 -> WM8785
25  * SPI 4 -> 4th AK4396 (back)
26  *
27  * GPIO 0 -> DFS0 of AK5385
28  * GPIO 1 -> DFS1 of AK5385
29  */
30
31 #include <linux/mutex.h>
32 #include <linux/pci.h>
33 #include <sound/ac97_codec.h>
34 #include <sound/control.h>
35 #include <sound/core.h>
36 #include <sound/initval.h>
37 #include <sound/pcm.h>
38 #include <sound/pcm_params.h>
39 #include <sound/tlv.h>
40 #include "oxygen.h"
41 #include "ak4396.h"
42 #include "wm8785.h"
43
44 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
45 MODULE_DESCRIPTION("C-Media CMI8788 driver");
46 MODULE_LICENSE("GPL");
47 MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8788}}");
48
49 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
50 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
51 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
52
53 module_param_array(index, int, NULL, 0444);
54 MODULE_PARM_DESC(index, "card index");
55 module_param_array(id, charp, NULL, 0444);
56 MODULE_PARM_DESC(id, "ID string");
57 module_param_array(enable, bool, NULL, 0444);
58 MODULE_PARM_DESC(enable, "enable card");
59
60 static struct pci_device_id oxygen_ids[] __devinitdata = {
61         { OXYGEN_PCI_SUBID(0x10b0, 0x0216) },
62         { OXYGEN_PCI_SUBID(0x10b0, 0x0218) },
63         { OXYGEN_PCI_SUBID(0x10b0, 0x0219) },
64         { OXYGEN_PCI_SUBID(0x13f6, 0x0001) },
65         { OXYGEN_PCI_SUBID(0x13f6, 0x0010) },
66         { OXYGEN_PCI_SUBID(0x13f6, 0x8788) },
67         { OXYGEN_PCI_SUBID(0x147a, 0xa017) },
68         { OXYGEN_PCI_SUBID(0x1a58, 0x0910) },
69         { OXYGEN_PCI_SUBID(0x415a, 0x5431), .driver_data = 1 },
70         { OXYGEN_PCI_SUBID(0x7284, 0x9761) },
71         { }
72 };
73 MODULE_DEVICE_TABLE(pci, oxygen_ids);
74
75
76 #define GPIO_AK5385_DFS_MASK    0x0003
77 #define GPIO_AK5385_DFS_NORMAL  0x0000
78 #define GPIO_AK5385_DFS_DOUBLE  0x0001
79 #define GPIO_AK5385_DFS_QUAD    0x0002
80
81 struct generic_data {
82         u8 ak4396_ctl2;
83 };
84
85 static void ak4396_write(struct oxygen *chip, unsigned int codec,
86                          u8 reg, u8 value)
87 {
88         /* maps ALSA channel pair number to SPI output */
89         static const u8 codec_spi_map[4] = {
90                 0, 1, 2, 4
91         };
92         oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
93                          OXYGEN_SPI_DATA_LENGTH_2 |
94                          OXYGEN_SPI_CLOCK_160 |
95                          (codec_spi_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
96                          OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
97                          AK4396_WRITE | (reg << 8) | value);
98 }
99
100 static void wm8785_write(struct oxygen *chip, u8 reg, unsigned int value)
101 {
102         oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
103                          OXYGEN_SPI_DATA_LENGTH_2 |
104                          OXYGEN_SPI_CLOCK_160 |
105                          (3 << OXYGEN_SPI_CODEC_SHIFT) |
106                          OXYGEN_SPI_CEN_LATCH_CLOCK_LO,
107                          (reg << 9) | value);
108 }
109
110 static void ak4396_init(struct oxygen *chip)
111 {
112         struct generic_data *data = chip->model_data;
113         unsigned int i;
114
115         data->ak4396_ctl2 = AK4396_SMUTE | AK4396_DEM_OFF | AK4396_DFS_NORMAL;
116         for (i = 0; i < 4; ++i) {
117                 ak4396_write(chip, i,
118                              AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN);
119                 ak4396_write(chip, i,
120                              AK4396_CONTROL_2, data->ak4396_ctl2);
121                 ak4396_write(chip, i,
122                              AK4396_CONTROL_3, AK4396_PCM);
123                 ak4396_write(chip, i, AK4396_LCH_ATT, 0);
124                 ak4396_write(chip, i, AK4396_RCH_ATT, 0);
125         }
126         snd_component_add(chip->card, "AK4396");
127 }
128
129 static void ak5385_init(struct oxygen *chip)
130 {
131         oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_AK5385_DFS_MASK);
132         oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_AK5385_DFS_MASK);
133         snd_component_add(chip->card, "AK5385");
134 }
135
136 static void wm8785_init(struct oxygen *chip)
137 {
138         wm8785_write(chip, WM8785_R7, 0);
139         wm8785_write(chip, WM8785_R0, WM8785_MCR_SLAVE |
140                      WM8785_OSR_SINGLE | WM8785_FORMAT_LJUST);
141         wm8785_write(chip, WM8785_R1, WM8785_WL_24);
142         snd_component_add(chip->card, "WM8785");
143 }
144
145 static void generic_init(struct oxygen *chip)
146 {
147         ak4396_init(chip);
148         wm8785_init(chip);
149 }
150
151 static void meridian_init(struct oxygen *chip)
152 {
153         ak4396_init(chip);
154         ak5385_init(chip);
155 }
156
157 static void generic_cleanup(struct oxygen *chip)
158 {
159 }
160
161 static void set_ak4396_params(struct oxygen *chip,
162                               struct snd_pcm_hw_params *params)
163 {
164         struct generic_data *data = chip->model_data;
165         unsigned int i;
166         u8 value;
167
168         value = data->ak4396_ctl2 & ~AK4396_DFS_MASK;
169         if (params_rate(params) <= 54000)
170                 value |= AK4396_DFS_NORMAL;
171         else if (params_rate(params) <= 108000)
172                 value |= AK4396_DFS_DOUBLE;
173         else
174                 value |= AK4396_DFS_QUAD;
175         data->ak4396_ctl2 = value;
176         for (i = 0; i < 4; ++i) {
177                 ak4396_write(chip, i,
178                              AK4396_CONTROL_1, AK4396_DIF_24_MSB);
179                 ak4396_write(chip, i,
180                              AK4396_CONTROL_2, value);
181                 ak4396_write(chip, i,
182                              AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN);
183         }
184 }
185
186 static void update_ak4396_volume(struct oxygen *chip)
187 {
188         unsigned int i;
189
190         for (i = 0; i < 4; ++i) {
191                 ak4396_write(chip, i,
192                              AK4396_LCH_ATT, chip->dac_volume[i * 2]);
193                 ak4396_write(chip, i,
194                              AK4396_RCH_ATT, chip->dac_volume[i * 2 + 1]);
195         }
196 }
197
198 static void update_ak4396_mute(struct oxygen *chip)
199 {
200         struct generic_data *data = chip->model_data;
201         unsigned int i;
202         u8 value;
203
204         value = data->ak4396_ctl2 & ~AK4396_SMUTE;
205         if (chip->dac_mute)
206                 value |= AK4396_SMUTE;
207         data->ak4396_ctl2 = value;
208         for (i = 0; i < 4; ++i)
209                 ak4396_write(chip, i, AK4396_CONTROL_2, value);
210 }
211
212 static void set_wm8785_params(struct oxygen *chip,
213                               struct snd_pcm_hw_params *params)
214 {
215         unsigned int value;
216
217         wm8785_write(chip, WM8785_R7, 0);
218
219         value = WM8785_MCR_SLAVE | WM8785_FORMAT_LJUST;
220         if (params_rate(params) <= 48000)
221                 value |= WM8785_OSR_SINGLE;
222         else if (params_rate(params) <= 96000)
223                 value |= WM8785_OSR_DOUBLE;
224         else
225                 value |= WM8785_OSR_QUAD;
226         wm8785_write(chip, WM8785_R0, value);
227
228         if (snd_pcm_format_width(params_format(params)) <= 16)
229                 value = WM8785_WL_16;
230         else
231                 value = WM8785_WL_24;
232         wm8785_write(chip, WM8785_R1, value);
233 }
234
235 static void set_ak5385_params(struct oxygen *chip,
236                               struct snd_pcm_hw_params *params)
237 {
238         unsigned int value;
239
240         if (params_rate(params) <= 54000)
241                 value = GPIO_AK5385_DFS_NORMAL;
242         else if (params_rate(params) <= 108000)
243                 value = GPIO_AK5385_DFS_DOUBLE;
244         else
245                 value = GPIO_AK5385_DFS_QUAD;
246         oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
247                               value, GPIO_AK5385_DFS_MASK);
248 }
249
250 static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0);
251
252 static const struct oxygen_model model_generic = {
253         .shortname = "C-Media CMI8788",
254         .longname = "C-Media Oxygen HD Audio",
255         .chip = "CMI8788",
256         .owner = THIS_MODULE,
257         .init = generic_init,
258         .cleanup = generic_cleanup,
259         .set_dac_params = set_ak4396_params,
260         .set_adc_params = set_wm8785_params,
261         .update_dac_volume = update_ak4396_volume,
262         .update_dac_mute = update_ak4396_mute,
263         .dac_tlv = ak4396_db_scale,
264         .model_data_size = sizeof(struct generic_data),
265         .pcm_dev_cfg = PLAYBACK_0_TO_I2S |
266                        PLAYBACK_1_TO_SPDIF |
267                        PLAYBACK_2_TO_AC97_1 |
268                        CAPTURE_0_FROM_I2S_1 |
269                        CAPTURE_1_FROM_SPDIF |
270                        CAPTURE_2_FROM_AC97_1,
271         .dac_channels = 8,
272         .dac_volume_min = 0,
273         .dac_volume_max = 255,
274         .function_flags = OXYGEN_FUNCTION_SPI |
275                           OXYGEN_FUNCTION_ENABLE_SPI_4_5,
276         .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
277         .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
278 };
279 static const struct oxygen_model model_meridian = {
280         .shortname = "C-Media CMI8788",
281         .longname = "C-Media Oxygen HD Audio",
282         .chip = "CMI8788",
283         .owner = THIS_MODULE,
284         .init = meridian_init,
285         .cleanup = generic_cleanup,
286         .set_dac_params = set_ak4396_params,
287         .set_adc_params = set_ak5385_params,
288         .update_dac_volume = update_ak4396_volume,
289         .update_dac_mute = update_ak4396_mute,
290         .dac_tlv = ak4396_db_scale,
291         .model_data_size = sizeof(struct generic_data),
292         .pcm_dev_cfg = PLAYBACK_0_TO_I2S |
293                        PLAYBACK_1_TO_SPDIF |
294                        PLAYBACK_2_TO_AC97_1 |
295                        CAPTURE_0_FROM_I2S_2 |
296                        CAPTURE_1_FROM_SPDIF |
297                        CAPTURE_2_FROM_AC97_1,
298         .dac_channels = 8,
299         .dac_volume_min = 0,
300         .dac_volume_max = 255,
301         .misc_flags = OXYGEN_MISC_MIDI,
302         .function_flags = OXYGEN_FUNCTION_SPI |
303                           OXYGEN_FUNCTION_ENABLE_SPI_4_5,
304         .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
305         .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
306 };
307
308 static int __devinit generic_oxygen_probe(struct pci_dev *pci,
309                                           const struct pci_device_id *pci_id)
310 {
311         static int dev;
312         int is_meridian;
313         int err;
314
315         if (dev >= SNDRV_CARDS)
316                 return -ENODEV;
317         if (!enable[dev]) {
318                 ++dev;
319                 return -ENOENT;
320         }
321         is_meridian = pci_id->driver_data;
322         err = oxygen_pci_probe(pci, index[dev], id[dev],
323                                is_meridian ? &model_meridian : &model_generic);
324         if (err >= 0)
325                 ++dev;
326         return err;
327 }
328
329 static struct pci_driver oxygen_driver = {
330         .name = "CMI8788",
331         .id_table = oxygen_ids,
332         .probe = generic_oxygen_probe,
333         .remove = __devexit_p(oxygen_pci_remove),
334 };
335
336 static int __init alsa_card_oxygen_init(void)
337 {
338         return pci_register_driver(&oxygen_driver);
339 }
340
341 static void __exit alsa_card_oxygen_exit(void)
342 {
343         pci_unregister_driver(&oxygen_driver);
344 }
345
346 module_init(alsa_card_oxygen_init)
347 module_exit(alsa_card_oxygen_exit)