blob: ad8d950fddc6681f15de085bac9b34e6a3331d75 [file] [log] [blame]
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001/*
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)
Clemens Ladisch7113e952008-01-14 08:55:03 +010022 * SPI 1 -> 2nd AK4396 (surround)
Clemens Ladischd0ce9942007-12-23 19:50:57 +010023 * SPI 2 -> 3rd AK4396 (center/LFE)
24 * SPI 3 -> WM8785
Clemens Ladisch7113e952008-01-14 08:55:03 +010025 * SPI 4 -> 4th AK4396 (back)
Clemens Ladischd0ce9942007-12-23 19:50:57 +010026 *
27 * GPIO 0 -> DFS0 of AK5385
28 * GPIO 1 -> DFS1 of AK5385
29 */
30
Clemens Ladisch902b05c2008-02-22 18:40:56 +010031#include <linux/mutex.h>
Clemens Ladischd0ce9942007-12-23 19:50:57 +010032#include <linux/pci.h>
Clemens Ladisch902b05c2008-02-22 18:40:56 +010033#include <sound/ac97_codec.h>
Clemens Ladischccc80fb2008-01-16 08:32:08 +010034#include <sound/control.h>
Clemens Ladischd0ce9942007-12-23 19:50:57 +010035#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"
Clemens Ladischc6260262008-01-25 08:41:52 +010041#include "ak4396.h"
Clemens Ladischf5b23682008-03-19 08:14:01 +010042#include "wm8785.h"
Clemens Ladischd0ce9942007-12-23 19:50:57 +010043
44MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
45MODULE_DESCRIPTION("C-Media CMI8788 driver");
Clemens Ladischd023dc02008-05-13 09:18:27 +020046MODULE_LICENSE("GPL v2");
Clemens Ladischd0ce9942007-12-23 19:50:57 +010047MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8788}}");
48
49static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
50static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
51static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
52
53module_param_array(index, int, NULL, 0444);
54MODULE_PARM_DESC(index, "card index");
55module_param_array(id, charp, NULL, 0444);
56MODULE_PARM_DESC(id, "ID string");
57module_param_array(enable, bool, NULL, 0444);
58MODULE_PARM_DESC(enable, "enable card");
59
60static 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) },
Clemens Ladischd0ce9942007-12-23 19:50:57 +010068 { OXYGEN_PCI_SUBID(0x1a58, 0x0910) },
69 { OXYGEN_PCI_SUBID(0x415a, 0x5431), .driver_data = 1 },
70 { OXYGEN_PCI_SUBID(0x7284, 0x9761) },
71 { }
72};
73MODULE_DEVICE_TABLE(pci, oxygen_ids);
74
Clemens Ladisch878ac3e2008-01-21 08:50:19 +010075
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
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +010081struct generic_data {
82 u8 ak4396_ctl2;
Clemens Ladische58aee92008-05-13 09:20:51 +020083 u16 saved_wm8785_registers[2];
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +010084};
85
Clemens Ladischd0ce9942007-12-23 19:50:57 +010086static void ak4396_write(struct oxygen *chip, unsigned int codec,
87 u8 reg, u8 value)
88{
89 /* maps ALSA channel pair number to SPI output */
90 static const u8 codec_spi_map[4] = {
Clemens Ladisch7113e952008-01-14 08:55:03 +010091 0, 1, 2, 4
Clemens Ladischd0ce9942007-12-23 19:50:57 +010092 };
Clemens Ladischc2353a02008-01-18 09:17:53 +010093 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
Clemens Ladischd0ce9942007-12-23 19:50:57 +010094 OXYGEN_SPI_DATA_LENGTH_2 |
Clemens Ladisch2ea85982008-01-30 08:38:30 +010095 OXYGEN_SPI_CLOCK_160 |
Clemens Ladischd0ce9942007-12-23 19:50:57 +010096 (codec_spi_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
Clemens Ladischc2353a02008-01-18 09:17:53 +010097 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
Clemens Ladischd0ce9942007-12-23 19:50:57 +010098 AK4396_WRITE | (reg << 8) | value);
99}
100
101static void wm8785_write(struct oxygen *chip, u8 reg, unsigned int value)
102{
Clemens Ladische58aee92008-05-13 09:20:51 +0200103 struct generic_data *data = chip->model_data;
104
Clemens Ladischc2353a02008-01-18 09:17:53 +0100105 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100106 OXYGEN_SPI_DATA_LENGTH_2 |
Clemens Ladisch2ea85982008-01-30 08:38:30 +0100107 OXYGEN_SPI_CLOCK_160 |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100108 (3 << OXYGEN_SPI_CODEC_SHIFT) |
109 OXYGEN_SPI_CEN_LATCH_CLOCK_LO,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100110 (reg << 9) | value);
Clemens Ladische58aee92008-05-13 09:20:51 +0200111 if (reg < ARRAY_SIZE(data->saved_wm8785_registers))
112 data->saved_wm8785_registers[reg] = value;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100113}
114
Clemens Ladischbbbfb552008-05-13 09:21:48 +0200115static void update_ak4396_volume(struct oxygen *chip)
116{
117 unsigned int i;
118
119 for (i = 0; i < 4; ++i) {
120 ak4396_write(chip, i,
121 AK4396_LCH_ATT, chip->dac_volume[i * 2]);
122 ak4396_write(chip, i,
123 AK4396_RCH_ATT, chip->dac_volume[i * 2 + 1]);
124 }
125}
126
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100127static void ak4396_init(struct oxygen *chip)
128{
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100129 struct generic_data *data = chip->model_data;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100130 unsigned int i;
131
Clemens Ladische9835322008-04-16 09:14:30 +0200132 data->ak4396_ctl2 = AK4396_SMUTE | AK4396_DEM_OFF | AK4396_DFS_NORMAL;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100133 for (i = 0; i < 4; ++i) {
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100134 ak4396_write(chip, i,
135 AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN);
136 ak4396_write(chip, i,
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100137 AK4396_CONTROL_2, data->ak4396_ctl2);
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100138 ak4396_write(chip, i,
139 AK4396_CONTROL_3, AK4396_PCM);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100140 }
Clemens Ladischbbbfb552008-05-13 09:21:48 +0200141 update_ak4396_volume(chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100142 snd_component_add(chip->card, "AK4396");
143}
144
145static void ak5385_init(struct oxygen *chip)
146{
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100147 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_AK5385_DFS_MASK);
148 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_AK5385_DFS_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100149 snd_component_add(chip->card, "AK5385");
150}
151
152static void wm8785_init(struct oxygen *chip)
153{
Clemens Ladische58aee92008-05-13 09:20:51 +0200154 struct generic_data *data = chip->model_data;
155
156 data->saved_wm8785_registers[0] = WM8785_MCR_SLAVE |
157 WM8785_OSR_SINGLE | WM8785_FORMAT_LJUST;
158 data->saved_wm8785_registers[1] = WM8785_WL_24;
159
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100160 wm8785_write(chip, WM8785_R7, 0);
Clemens Ladische58aee92008-05-13 09:20:51 +0200161 wm8785_write(chip, WM8785_R0, data->saved_wm8785_registers[0]);
162 wm8785_write(chip, WM8785_R1, data->saved_wm8785_registers[1]);
163
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100164 snd_component_add(chip->card, "WM8785");
165}
166
167static void generic_init(struct oxygen *chip)
168{
169 ak4396_init(chip);
170 wm8785_init(chip);
171}
172
173static void meridian_init(struct oxygen *chip)
174{
175 ak4396_init(chip);
176 ak5385_init(chip);
177}
178
179static void generic_cleanup(struct oxygen *chip)
180{
181}
182
183static void set_ak4396_params(struct oxygen *chip,
184 struct snd_pcm_hw_params *params)
185{
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100186 struct generic_data *data = chip->model_data;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100187 unsigned int i;
188 u8 value;
189
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100190 value = data->ak4396_ctl2 & ~AK4396_DFS_MASK;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100191 if (params_rate(params) <= 54000)
192 value |= AK4396_DFS_NORMAL;
Clemens Ladisch236c4922008-01-28 08:32:58 +0100193 else if (params_rate(params) <= 108000)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100194 value |= AK4396_DFS_DOUBLE;
195 else
196 value |= AK4396_DFS_QUAD;
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100197 data->ak4396_ctl2 = value;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100198 for (i = 0; i < 4; ++i) {
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100199 ak4396_write(chip, i,
200 AK4396_CONTROL_1, AK4396_DIF_24_MSB);
201 ak4396_write(chip, i,
202 AK4396_CONTROL_2, value);
203 ak4396_write(chip, i,
204 AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100205 }
206}
207
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100208static void update_ak4396_mute(struct oxygen *chip)
209{
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100210 struct generic_data *data = chip->model_data;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100211 unsigned int i;
212 u8 value;
213
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100214 value = data->ak4396_ctl2 & ~AK4396_SMUTE;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100215 if (chip->dac_mute)
216 value |= AK4396_SMUTE;
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100217 data->ak4396_ctl2 = value;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100218 for (i = 0; i < 4; ++i)
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100219 ak4396_write(chip, i, AK4396_CONTROL_2, value);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100220}
221
222static void set_wm8785_params(struct oxygen *chip,
223 struct snd_pcm_hw_params *params)
224{
225 unsigned int value;
226
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100227 wm8785_write(chip, WM8785_R7, 0);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100228
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100229 value = WM8785_MCR_SLAVE | WM8785_FORMAT_LJUST;
Clemens Ladisch71e22a42008-01-21 08:50:51 +0100230 if (params_rate(params) <= 48000)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100231 value |= WM8785_OSR_SINGLE;
Clemens Ladisch71e22a42008-01-21 08:50:51 +0100232 else if (params_rate(params) <= 96000)
233 value |= WM8785_OSR_DOUBLE;
234 else
235 value |= WM8785_OSR_QUAD;
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100236 wm8785_write(chip, WM8785_R0, value);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100237
238 if (snd_pcm_format_width(params_format(params)) <= 16)
239 value = WM8785_WL_16;
240 else
241 value = WM8785_WL_24;
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100242 wm8785_write(chip, WM8785_R1, value);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100243}
244
245static void set_ak5385_params(struct oxygen *chip,
246 struct snd_pcm_hw_params *params)
247{
248 unsigned int value;
249
250 if (params_rate(params) <= 54000)
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100251 value = GPIO_AK5385_DFS_NORMAL;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100252 else if (params_rate(params) <= 108000)
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100253 value = GPIO_AK5385_DFS_DOUBLE;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100254 else
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100255 value = GPIO_AK5385_DFS_QUAD;
256 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
257 value, GPIO_AK5385_DFS_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100258}
259
260static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0);
261
262static const struct oxygen_model model_generic = {
263 .shortname = "C-Media CMI8788",
264 .longname = "C-Media Oxygen HD Audio",
265 .chip = "CMI8788",
266 .owner = THIS_MODULE,
267 .init = generic_init,
268 .cleanup = generic_cleanup,
269 .set_dac_params = set_ak4396_params,
270 .set_adc_params = set_wm8785_params,
271 .update_dac_volume = update_ak4396_volume,
272 .update_dac_mute = update_ak4396_mute,
Clemens Ladisch4972a172008-04-16 09:15:45 +0200273 .dac_tlv = ak4396_db_scale,
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100274 .model_data_size = sizeof(struct generic_data),
Clemens Ladischf009ad92008-03-19 08:19:41 +0100275 .pcm_dev_cfg = PLAYBACK_0_TO_I2S |
276 PLAYBACK_1_TO_SPDIF |
277 PLAYBACK_2_TO_AC97_1 |
278 CAPTURE_0_FROM_I2S_1 |
279 CAPTURE_1_FROM_SPDIF |
280 CAPTURE_2_FROM_AC97_1,
Clemens Ladisch976cd622008-01-25 08:37:49 +0100281 .dac_channels = 8,
Clemens Ladisch193e8132008-04-16 09:13:36 +0200282 .dac_volume_min = 0,
283 .dac_volume_max = 255,
Clemens Ladisch87eedd22008-03-19 08:20:13 +0100284 .function_flags = OXYGEN_FUNCTION_SPI |
285 OXYGEN_FUNCTION_ENABLE_SPI_4_5,
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100286 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
287 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100288};
289static const struct oxygen_model model_meridian = {
290 .shortname = "C-Media CMI8788",
291 .longname = "C-Media Oxygen HD Audio",
292 .chip = "CMI8788",
293 .owner = THIS_MODULE,
294 .init = meridian_init,
295 .cleanup = generic_cleanup,
296 .set_dac_params = set_ak4396_params,
297 .set_adc_params = set_ak5385_params,
298 .update_dac_volume = update_ak4396_volume,
299 .update_dac_mute = update_ak4396_mute,
Clemens Ladisch4972a172008-04-16 09:15:45 +0200300 .dac_tlv = ak4396_db_scale,
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100301 .model_data_size = sizeof(struct generic_data),
Clemens Ladischf009ad92008-03-19 08:19:41 +0100302 .pcm_dev_cfg = PLAYBACK_0_TO_I2S |
303 PLAYBACK_1_TO_SPDIF |
304 PLAYBACK_2_TO_AC97_1 |
305 CAPTURE_0_FROM_I2S_2 |
306 CAPTURE_1_FROM_SPDIF |
307 CAPTURE_2_FROM_AC97_1,
Clemens Ladisch976cd622008-01-25 08:37:49 +0100308 .dac_channels = 8,
Clemens Ladisch193e8132008-04-16 09:13:36 +0200309 .dac_volume_min = 0,
310 .dac_volume_max = 255,
Clemens Ladischdb12b8e2008-03-19 08:20:59 +0100311 .misc_flags = OXYGEN_MISC_MIDI,
Clemens Ladisch87eedd22008-03-19 08:20:13 +0100312 .function_flags = OXYGEN_FUNCTION_SPI |
313 OXYGEN_FUNCTION_ENABLE_SPI_4_5,
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100314 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
315 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100316};
317
318static int __devinit generic_oxygen_probe(struct pci_dev *pci,
319 const struct pci_device_id *pci_id)
320{
321 static int dev;
Clemens Ladischcd93dc82008-01-24 08:43:39 +0100322 int is_meridian;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100323 int err;
324
325 if (dev >= SNDRV_CARDS)
326 return -ENODEV;
327 if (!enable[dev]) {
328 ++dev;
329 return -ENOENT;
330 }
Clemens Ladischcd93dc82008-01-24 08:43:39 +0100331 is_meridian = pci_id->driver_data;
Clemens Ladischdb12b8e2008-03-19 08:20:59 +0100332 err = oxygen_pci_probe(pci, index[dev], id[dev],
Clemens Ladischcd93dc82008-01-24 08:43:39 +0100333 is_meridian ? &model_meridian : &model_generic);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100334 if (err >= 0)
335 ++dev;
336 return err;
337}
338
339static struct pci_driver oxygen_driver = {
340 .name = "CMI8788",
341 .id_table = oxygen_ids,
342 .probe = generic_oxygen_probe,
343 .remove = __devexit_p(oxygen_pci_remove),
344};
345
346static int __init alsa_card_oxygen_init(void)
347{
348 return pci_register_driver(&oxygen_driver);
349}
350
351static void __exit alsa_card_oxygen_exit(void)
352{
353 pci_unregister_driver(&oxygen_driver);
354}
355
356module_init(alsa_card_oxygen_init)
357module_exit(alsa_card_oxygen_exit)