2 * C-Media CMI8788 driver for C-Media's reference design and similar models
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
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.
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.
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
23 * SPI 0 -> 1st AK4396 (front)
24 * SPI 1 -> 2nd AK4396 (surround)
25 * SPI 2 -> 3rd AK4396 (center/LFE)
27 * SPI 4 -> 4th AK4396 (back)
29 * GPIO 0 -> DFS0 of AK5385
30 * GPIO 1 -> DFS1 of AK5385
31 * GPIO 8 -> enable headphone amplifier on HT-Omega models
35 * LINE_OUT -> input of ADC
41 * GPO 0 -> route line-in (0) or AC97 output (1) to ADC input
44 #include <linux/delay.h>
45 #include <linux/mutex.h>
46 #include <linux/pci.h>
47 #include <sound/ac97_codec.h>
48 #include <sound/control.h>
49 #include <sound/core.h>
50 #include <sound/info.h>
51 #include <sound/initval.h>
52 #include <sound/pcm.h>
53 #include <sound/pcm_params.h>
54 #include <sound/tlv.h>
59 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
60 MODULE_DESCRIPTION("C-Media CMI8788 driver");
61 MODULE_LICENSE("GPL v2");
62 MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8788}}");
64 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
65 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
66 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
68 module_param_array(index, int, NULL, 0444);
69 MODULE_PARM_DESC(index, "card index");
70 module_param_array(id, charp, NULL, 0444);
71 MODULE_PARM_DESC(id, "ID string");
72 module_param_array(enable, bool, NULL, 0444);
73 MODULE_PARM_DESC(enable, "enable card");
84 static DEFINE_PCI_DEVICE_TABLE(oxygen_ids) = {
85 /* C-Media's reference design */
86 { OXYGEN_PCI_SUBID(0x10b0, 0x0216), .driver_data = MODEL_CMEDIA_REF },
87 { OXYGEN_PCI_SUBID(0x10b0, 0x0217), .driver_data = MODEL_CMEDIA_REF },
88 { OXYGEN_PCI_SUBID(0x10b0, 0x0218), .driver_data = MODEL_CMEDIA_REF },
89 { OXYGEN_PCI_SUBID(0x10b0, 0x0219), .driver_data = MODEL_CMEDIA_REF },
90 { OXYGEN_PCI_SUBID(0x13f6, 0x0001), .driver_data = MODEL_CMEDIA_REF },
91 { OXYGEN_PCI_SUBID(0x13f6, 0x0010), .driver_data = MODEL_CMEDIA_REF },
92 { OXYGEN_PCI_SUBID(0x13f6, 0x8788), .driver_data = MODEL_CMEDIA_REF },
93 { OXYGEN_PCI_SUBID(0x147a, 0xa017), .driver_data = MODEL_CMEDIA_REF },
94 { OXYGEN_PCI_SUBID(0x1a58, 0x0910), .driver_data = MODEL_CMEDIA_REF },
95 /* PCI 2.0 HD Audio */
96 { OXYGEN_PCI_SUBID(0x13f6, 0x8782), .driver_data = MODEL_2CH_OUTPUT },
97 /* Kuroutoshikou CMI8787-HG2PCI */
98 { OXYGEN_PCI_SUBID(0x13f6, 0xffff), .driver_data = MODEL_2CH_OUTPUT },
99 /* TempoTec HiFier Fantasia */
100 { OXYGEN_PCI_SUBID(0x14c3, 0x1710), .driver_data = MODEL_FANTASIA },
101 /* TempoTec HiFier Serenade */
102 { OXYGEN_PCI_SUBID(0x14c3, 0x1711), .driver_data = MODEL_2CH_OUTPUT },
103 /* AuzenTech X-Meridian */
104 { OXYGEN_PCI_SUBID(0x415a, 0x5431), .driver_data = MODEL_MERIDIAN },
105 /* AuzenTech X-Meridian 2G */
106 { OXYGEN_PCI_SUBID(0x5431, 0x017a), .driver_data = MODEL_MERIDIAN },
108 { OXYGEN_PCI_SUBID(0x7284, 0x9761), .driver_data = MODEL_CLARO },
109 /* HT-Omega Claro halo */
110 { OXYGEN_PCI_SUBID(0x7284, 0x9781), .driver_data = MODEL_CLARO_HALO },
113 MODULE_DEVICE_TABLE(pci, oxygen_ids);
116 #define GPIO_AK5385_DFS_MASK 0x0003
117 #define GPIO_AK5385_DFS_NORMAL 0x0000
118 #define GPIO_AK5385_DFS_DOUBLE 0x0001
119 #define GPIO_AK5385_DFS_QUAD 0x0002
121 #define GPIO_CLARO_HP 0x0100
123 struct generic_data {
125 u8 ak4396_regs[4][5];
129 static void ak4396_write(struct oxygen *chip, unsigned int codec,
132 /* maps ALSA channel pair number to SPI output */
133 static const u8 codec_spi_map[4] = {
136 struct generic_data *data = chip->model_data;
138 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
139 OXYGEN_SPI_DATA_LENGTH_2 |
140 OXYGEN_SPI_CLOCK_160 |
141 (codec_spi_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
142 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
143 AK4396_WRITE | (reg << 8) | value);
144 data->ak4396_regs[codec][reg] = value;
147 static void ak4396_write_cached(struct oxygen *chip, unsigned int codec,
150 struct generic_data *data = chip->model_data;
152 if (value != data->ak4396_regs[codec][reg])
153 ak4396_write(chip, codec, reg, value);
156 static void wm8785_write(struct oxygen *chip, u8 reg, unsigned int value)
158 struct generic_data *data = chip->model_data;
160 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
161 OXYGEN_SPI_DATA_LENGTH_2 |
162 OXYGEN_SPI_CLOCK_160 |
163 (3 << OXYGEN_SPI_CODEC_SHIFT) |
164 OXYGEN_SPI_CEN_LATCH_CLOCK_LO,
166 if (reg < ARRAY_SIZE(data->wm8785_regs))
167 data->wm8785_regs[reg] = value;
170 static void ak4396_registers_init(struct oxygen *chip)
172 struct generic_data *data = chip->model_data;
175 for (i = 0; i < data->dacs; ++i) {
176 ak4396_write(chip, i, AK4396_CONTROL_1,
177 AK4396_DIF_24_MSB | AK4396_RSTN);
178 ak4396_write(chip, i, AK4396_CONTROL_2,
179 data->ak4396_regs[0][AK4396_CONTROL_2]);
180 ak4396_write(chip, i, AK4396_CONTROL_3,
182 ak4396_write(chip, i, AK4396_LCH_ATT,
183 chip->dac_volume[i * 2]);
184 ak4396_write(chip, i, AK4396_RCH_ATT,
185 chip->dac_volume[i * 2 + 1]);
189 static void ak4396_init(struct oxygen *chip)
191 struct generic_data *data = chip->model_data;
193 data->dacs = chip->model.dac_channels_pcm / 2;
194 data->ak4396_regs[0][AK4396_CONTROL_2] =
195 AK4396_SMUTE | AK4396_DEM_OFF | AK4396_DFS_NORMAL;
196 ak4396_registers_init(chip);
197 snd_component_add(chip->card, "AK4396");
200 static void ak5385_init(struct oxygen *chip)
202 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_AK5385_DFS_MASK);
203 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_AK5385_DFS_MASK);
204 snd_component_add(chip->card, "AK5385");
207 static void wm8785_registers_init(struct oxygen *chip)
209 struct generic_data *data = chip->model_data;
211 wm8785_write(chip, WM8785_R7, 0);
212 wm8785_write(chip, WM8785_R0, data->wm8785_regs[0]);
213 wm8785_write(chip, WM8785_R2, data->wm8785_regs[2]);
216 static void wm8785_init(struct oxygen *chip)
218 struct generic_data *data = chip->model_data;
220 data->wm8785_regs[0] =
221 WM8785_MCR_SLAVE | WM8785_OSR_SINGLE | WM8785_FORMAT_LJUST;
222 data->wm8785_regs[2] = WM8785_HPFR | WM8785_HPFL;
223 wm8785_registers_init(chip);
224 snd_component_add(chip->card, "WM8785");
227 static void generic_init(struct oxygen *chip)
233 static void meridian_init(struct oxygen *chip)
239 static void claro_enable_hp(struct oxygen *chip)
242 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_CLARO_HP);
243 oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, GPIO_CLARO_HP);
246 static void claro_init(struct oxygen *chip)
250 claro_enable_hp(chip);
253 static void claro_halo_init(struct oxygen *chip)
257 claro_enable_hp(chip);
260 static void fantasia_init(struct oxygen *chip)
263 snd_component_add(chip->card, "CS5340");
266 static void stereo_output_init(struct oxygen *chip)
271 static void generic_cleanup(struct oxygen *chip)
275 static void claro_disable_hp(struct oxygen *chip)
277 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_CLARO_HP);
280 static void claro_cleanup(struct oxygen *chip)
282 claro_disable_hp(chip);
285 static void claro_suspend(struct oxygen *chip)
287 claro_disable_hp(chip);
290 static void generic_resume(struct oxygen *chip)
292 ak4396_registers_init(chip);
293 wm8785_registers_init(chip);
296 static void meridian_resume(struct oxygen *chip)
298 ak4396_registers_init(chip);
301 static void claro_resume(struct oxygen *chip)
303 ak4396_registers_init(chip);
304 claro_enable_hp(chip);
307 static void stereo_resume(struct oxygen *chip)
309 ak4396_registers_init(chip);
312 static void set_ak4396_params(struct oxygen *chip,
313 struct snd_pcm_hw_params *params)
315 struct generic_data *data = chip->model_data;
319 value = data->ak4396_regs[0][AK4396_CONTROL_2] & ~AK4396_DFS_MASK;
320 if (params_rate(params) <= 54000)
321 value |= AK4396_DFS_NORMAL;
322 else if (params_rate(params) <= 108000)
323 value |= AK4396_DFS_DOUBLE;
325 value |= AK4396_DFS_QUAD;
327 msleep(1); /* wait for the new MCLK to become stable */
329 if (value != data->ak4396_regs[0][AK4396_CONTROL_2]) {
330 for (i = 0; i < data->dacs; ++i) {
331 ak4396_write(chip, i, AK4396_CONTROL_1,
333 ak4396_write(chip, i, AK4396_CONTROL_2, value);
334 ak4396_write(chip, i, AK4396_CONTROL_1,
335 AK4396_DIF_24_MSB | AK4396_RSTN);
340 static void update_ak4396_volume(struct oxygen *chip)
342 struct generic_data *data = chip->model_data;
345 for (i = 0; i < data->dacs; ++i) {
346 ak4396_write_cached(chip, i, AK4396_LCH_ATT,
347 chip->dac_volume[i * 2]);
348 ak4396_write_cached(chip, i, AK4396_RCH_ATT,
349 chip->dac_volume[i * 2 + 1]);
353 static void update_ak4396_mute(struct oxygen *chip)
355 struct generic_data *data = chip->model_data;
359 value = data->ak4396_regs[0][AK4396_CONTROL_2] & ~AK4396_SMUTE;
361 value |= AK4396_SMUTE;
362 for (i = 0; i < data->dacs; ++i)
363 ak4396_write_cached(chip, i, AK4396_CONTROL_2, value);
366 static void set_wm8785_params(struct oxygen *chip,
367 struct snd_pcm_hw_params *params)
369 struct generic_data *data = chip->model_data;
372 value = WM8785_MCR_SLAVE | WM8785_FORMAT_LJUST;
373 if (params_rate(params) <= 48000)
374 value |= WM8785_OSR_SINGLE;
375 else if (params_rate(params) <= 96000)
376 value |= WM8785_OSR_DOUBLE;
378 value |= WM8785_OSR_QUAD;
379 if (value != data->wm8785_regs[0]) {
380 wm8785_write(chip, WM8785_R7, 0);
381 wm8785_write(chip, WM8785_R0, value);
382 wm8785_write(chip, WM8785_R2, data->wm8785_regs[2]);
386 static void set_ak5385_params(struct oxygen *chip,
387 struct snd_pcm_hw_params *params)
391 if (params_rate(params) <= 54000)
392 value = GPIO_AK5385_DFS_NORMAL;
393 else if (params_rate(params) <= 108000)
394 value = GPIO_AK5385_DFS_DOUBLE;
396 value = GPIO_AK5385_DFS_QUAD;
397 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
398 value, GPIO_AK5385_DFS_MASK);
401 static void set_no_params(struct oxygen *chip, struct snd_pcm_hw_params *params)
405 static int rolloff_info(struct snd_kcontrol *ctl,
406 struct snd_ctl_elem_info *info)
408 static const char *const names[2] = {
409 "Sharp Roll-off", "Slow Roll-off"
412 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
414 info->value.enumerated.items = 2;
415 if (info->value.enumerated.item >= 2)
416 info->value.enumerated.item = 1;
417 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
421 static int rolloff_get(struct snd_kcontrol *ctl,
422 struct snd_ctl_elem_value *value)
424 struct oxygen *chip = ctl->private_data;
425 struct generic_data *data = chip->model_data;
427 value->value.enumerated.item[0] =
428 (data->ak4396_regs[0][AK4396_CONTROL_2] & AK4396_SLOW) != 0;
432 static int rolloff_put(struct snd_kcontrol *ctl,
433 struct snd_ctl_elem_value *value)
435 struct oxygen *chip = ctl->private_data;
436 struct generic_data *data = chip->model_data;
441 mutex_lock(&chip->mutex);
442 reg = data->ak4396_regs[0][AK4396_CONTROL_2];
443 if (value->value.enumerated.item[0])
447 changed = reg != data->ak4396_regs[0][AK4396_CONTROL_2];
449 for (i = 0; i < data->dacs; ++i)
450 ak4396_write(chip, i, AK4396_CONTROL_2, reg);
452 mutex_unlock(&chip->mutex);
456 static const struct snd_kcontrol_new rolloff_control = {
457 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
458 .name = "DAC Filter Playback Enum",
459 .info = rolloff_info,
464 static int hpf_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
466 static const char *const names[2] = {
467 "None", "High-pass Filter"
470 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
472 info->value.enumerated.items = 2;
473 if (info->value.enumerated.item >= 2)
474 info->value.enumerated.item = 1;
475 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
479 static int hpf_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
481 struct oxygen *chip = ctl->private_data;
482 struct generic_data *data = chip->model_data;
484 value->value.enumerated.item[0] =
485 (data->wm8785_regs[WM8785_R2] & WM8785_HPFR) != 0;
489 static int hpf_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
491 struct oxygen *chip = ctl->private_data;
492 struct generic_data *data = chip->model_data;
496 mutex_lock(&chip->mutex);
497 reg = data->wm8785_regs[WM8785_R2] & ~(WM8785_HPFR | WM8785_HPFL);
498 if (value->value.enumerated.item[0])
499 reg |= WM8785_HPFR | WM8785_HPFL;
500 changed = reg != data->wm8785_regs[WM8785_R2];
502 wm8785_write(chip, WM8785_R2, reg);
503 mutex_unlock(&chip->mutex);
507 static const struct snd_kcontrol_new hpf_control = {
508 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
509 .name = "ADC Filter Capture Enum",
515 static int generic_mixer_init(struct oxygen *chip)
517 return snd_ctl_add(chip->card, snd_ctl_new1(&rolloff_control, chip));
520 static int generic_wm8785_mixer_init(struct oxygen *chip)
524 err = generic_mixer_init(chip);
527 err = snd_ctl_add(chip->card, snd_ctl_new1(&hpf_control, chip));
533 static void dump_ak4396_registers(struct oxygen *chip,
534 struct snd_info_buffer *buffer)
536 struct generic_data *data = chip->model_data;
539 for (dac = 0; dac < data->dacs; ++dac) {
540 snd_iprintf(buffer, "\nAK4396 %u:", dac + 1);
541 for (i = 0; i < 5; ++i)
542 snd_iprintf(buffer, " %02x", data->ak4396_regs[dac][i]);
544 snd_iprintf(buffer, "\n");
547 static void dump_wm8785_registers(struct oxygen *chip,
548 struct snd_info_buffer *buffer)
550 struct generic_data *data = chip->model_data;
553 snd_iprintf(buffer, "\nWM8785:");
554 for (i = 0; i < 3; ++i)
555 snd_iprintf(buffer, " %03x", data->wm8785_regs[i]);
556 snd_iprintf(buffer, "\n");
559 static void dump_oxygen_registers(struct oxygen *chip,
560 struct snd_info_buffer *buffer)
562 dump_ak4396_registers(chip, buffer);
563 dump_wm8785_registers(chip, buffer);
566 static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0);
568 static const struct oxygen_model model_generic = {
569 .shortname = "C-Media CMI8788",
570 .longname = "C-Media Oxygen HD Audio",
572 .init = generic_init,
573 .mixer_init = generic_wm8785_mixer_init,
574 .cleanup = generic_cleanup,
575 .resume = generic_resume,
576 .set_dac_params = set_ak4396_params,
577 .set_adc_params = set_wm8785_params,
578 .update_dac_volume = update_ak4396_volume,
579 .update_dac_mute = update_ak4396_mute,
580 .dump_registers = dump_oxygen_registers,
581 .dac_tlv = ak4396_db_scale,
582 .model_data_size = sizeof(struct generic_data),
583 .device_config = PLAYBACK_0_TO_I2S |
584 PLAYBACK_1_TO_SPDIF |
585 PLAYBACK_2_TO_AC97_1 |
586 CAPTURE_0_FROM_I2S_1 |
587 CAPTURE_1_FROM_SPDIF |
588 CAPTURE_2_FROM_AC97_1 |
590 .dac_channels_pcm = 8,
591 .dac_channels_mixer = 8,
593 .dac_volume_max = 255,
594 .function_flags = OXYGEN_FUNCTION_SPI |
595 OXYGEN_FUNCTION_ENABLE_SPI_4_5,
596 .dac_mclks = OXYGEN_MCLKS(256, 128, 128),
597 .adc_mclks = OXYGEN_MCLKS(256, 256, 128),
598 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
599 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
602 static int __devinit get_oxygen_model(struct oxygen *chip,
603 const struct pci_device_id *id)
605 chip->model = model_generic;
606 switch (id->driver_data) {
608 chip->model.init = meridian_init;
609 chip->model.mixer_init = generic_mixer_init;
610 chip->model.resume = meridian_resume;
611 chip->model.set_adc_params = set_ak5385_params;
612 chip->model.dump_registers = dump_ak4396_registers;
613 chip->model.device_config = PLAYBACK_0_TO_I2S |
614 PLAYBACK_1_TO_SPDIF |
615 CAPTURE_0_FROM_I2S_2 |
616 CAPTURE_1_FROM_SPDIF;
619 chip->model.init = claro_init;
620 chip->model.cleanup = claro_cleanup;
621 chip->model.suspend = claro_suspend;
622 chip->model.resume = claro_resume;
624 case MODEL_CLARO_HALO:
625 chip->model.init = claro_halo_init;
626 chip->model.mixer_init = generic_mixer_init;
627 chip->model.cleanup = claro_cleanup;
628 chip->model.suspend = claro_suspend;
629 chip->model.resume = claro_resume;
630 chip->model.set_adc_params = set_ak5385_params;
631 chip->model.dump_registers = dump_ak4396_registers;
632 chip->model.device_config = PLAYBACK_0_TO_I2S |
633 PLAYBACK_1_TO_SPDIF |
634 CAPTURE_0_FROM_I2S_2 |
635 CAPTURE_1_FROM_SPDIF;
638 case MODEL_2CH_OUTPUT:
639 chip->model.shortname = "C-Media CMI8787";
640 chip->model.chip = "CMI8787";
641 if (id->driver_data == MODEL_FANTASIA)
642 chip->model.init = fantasia_init;
644 chip->model.init = stereo_output_init;
645 chip->model.resume = stereo_resume;
646 chip->model.mixer_init = generic_mixer_init;
647 chip->model.set_adc_params = set_no_params;
648 chip->model.dump_registers = dump_ak4396_registers;
649 chip->model.device_config = PLAYBACK_0_TO_I2S |
651 if (id->driver_data == MODEL_FANTASIA) {
652 chip->model.device_config |= CAPTURE_0_FROM_I2S_1;
653 chip->model.adc_mclks = OXYGEN_MCLKS(256, 128, 128);
655 chip->model.dac_channels_pcm = 2;
656 chip->model.dac_channels_mixer = 2;
659 if (id->driver_data == MODEL_MERIDIAN ||
660 id->driver_data == MODEL_CLARO_HALO) {
661 chip->model.misc_flags = OXYGEN_MISC_MIDI;
662 chip->model.device_config |= MIDI_OUTPUT | MIDI_INPUT;
667 static int __devinit generic_oxygen_probe(struct pci_dev *pci,
668 const struct pci_device_id *pci_id)
673 if (dev >= SNDRV_CARDS)
679 err = oxygen_pci_probe(pci, index[dev], id[dev], THIS_MODULE,
680 oxygen_ids, get_oxygen_model);
686 static struct pci_driver oxygen_driver = {
688 .id_table = oxygen_ids,
689 .probe = generic_oxygen_probe,
690 .remove = __devexit_p(oxygen_pci_remove),
692 .suspend = oxygen_pci_suspend,
693 .resume = oxygen_pci_resume,
697 static int __init alsa_card_oxygen_init(void)
699 return pci_register_driver(&oxygen_driver);
702 static void __exit alsa_card_oxygen_exit(void)
704 pci_unregister_driver(&oxygen_driver);
707 module_init(alsa_card_oxygen_init)
708 module_exit(alsa_card_oxygen_exit)