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 },
106 { OXYGEN_PCI_SUBID(0x7284, 0x9761), .driver_data = MODEL_CLARO },
107 /* HT-Omega Claro halo */
108 { OXYGEN_PCI_SUBID(0x7284, 0x9781), .driver_data = MODEL_CLARO_HALO },
111 MODULE_DEVICE_TABLE(pci, oxygen_ids);
114 #define GPIO_AK5385_DFS_MASK 0x0003
115 #define GPIO_AK5385_DFS_NORMAL 0x0000
116 #define GPIO_AK5385_DFS_DOUBLE 0x0001
117 #define GPIO_AK5385_DFS_QUAD 0x0002
119 #define GPIO_CLARO_HP 0x0100
121 struct generic_data {
123 u8 ak4396_regs[4][5];
127 static void ak4396_write(struct oxygen *chip, unsigned int codec,
130 /* maps ALSA channel pair number to SPI output */
131 static const u8 codec_spi_map[4] = {
134 struct generic_data *data = chip->model_data;
136 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
137 OXYGEN_SPI_DATA_LENGTH_2 |
138 OXYGEN_SPI_CLOCK_160 |
139 (codec_spi_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
140 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
141 AK4396_WRITE | (reg << 8) | value);
142 data->ak4396_regs[codec][reg] = value;
145 static void ak4396_write_cached(struct oxygen *chip, unsigned int codec,
148 struct generic_data *data = chip->model_data;
150 if (value != data->ak4396_regs[codec][reg])
151 ak4396_write(chip, codec, reg, value);
154 static void wm8785_write(struct oxygen *chip, u8 reg, unsigned int value)
156 struct generic_data *data = chip->model_data;
158 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
159 OXYGEN_SPI_DATA_LENGTH_2 |
160 OXYGEN_SPI_CLOCK_160 |
161 (3 << OXYGEN_SPI_CODEC_SHIFT) |
162 OXYGEN_SPI_CEN_LATCH_CLOCK_LO,
164 if (reg < ARRAY_SIZE(data->wm8785_regs))
165 data->wm8785_regs[reg] = value;
168 static void ak4396_registers_init(struct oxygen *chip)
170 struct generic_data *data = chip->model_data;
173 for (i = 0; i < data->dacs; ++i) {
174 ak4396_write(chip, i, AK4396_CONTROL_1,
175 AK4396_DIF_24_MSB | AK4396_RSTN);
176 ak4396_write(chip, i, AK4396_CONTROL_2,
177 data->ak4396_regs[0][AK4396_CONTROL_2]);
178 ak4396_write(chip, i, AK4396_CONTROL_3,
180 ak4396_write(chip, i, AK4396_LCH_ATT,
181 chip->dac_volume[i * 2]);
182 ak4396_write(chip, i, AK4396_RCH_ATT,
183 chip->dac_volume[i * 2 + 1]);
187 static void ak4396_init(struct oxygen *chip)
189 struct generic_data *data = chip->model_data;
191 data->dacs = chip->model.dac_channels_pcm / 2;
192 data->ak4396_regs[0][AK4396_CONTROL_2] =
193 AK4396_SMUTE | AK4396_DEM_OFF | AK4396_DFS_NORMAL;
194 ak4396_registers_init(chip);
195 snd_component_add(chip->card, "AK4396");
198 static void ak5385_init(struct oxygen *chip)
200 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_AK5385_DFS_MASK);
201 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_AK5385_DFS_MASK);
202 snd_component_add(chip->card, "AK5385");
205 static void wm8785_registers_init(struct oxygen *chip)
207 struct generic_data *data = chip->model_data;
209 wm8785_write(chip, WM8785_R7, 0);
210 wm8785_write(chip, WM8785_R0, data->wm8785_regs[0]);
211 wm8785_write(chip, WM8785_R2, data->wm8785_regs[2]);
214 static void wm8785_init(struct oxygen *chip)
216 struct generic_data *data = chip->model_data;
218 data->wm8785_regs[0] =
219 WM8785_MCR_SLAVE | WM8785_OSR_SINGLE | WM8785_FORMAT_LJUST;
220 data->wm8785_regs[2] = WM8785_HPFR | WM8785_HPFL;
221 wm8785_registers_init(chip);
222 snd_component_add(chip->card, "WM8785");
225 static void generic_init(struct oxygen *chip)
231 static void meridian_init(struct oxygen *chip)
237 static void claro_enable_hp(struct oxygen *chip)
240 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_CLARO_HP);
241 oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, GPIO_CLARO_HP);
244 static void claro_init(struct oxygen *chip)
248 claro_enable_hp(chip);
251 static void claro_halo_init(struct oxygen *chip)
255 claro_enable_hp(chip);
258 static void fantasia_init(struct oxygen *chip)
261 snd_component_add(chip->card, "CS5340");
264 static void stereo_output_init(struct oxygen *chip)
269 static void generic_cleanup(struct oxygen *chip)
273 static void claro_disable_hp(struct oxygen *chip)
275 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_CLARO_HP);
278 static void claro_cleanup(struct oxygen *chip)
280 claro_disable_hp(chip);
283 static void claro_suspend(struct oxygen *chip)
285 claro_disable_hp(chip);
288 static void generic_resume(struct oxygen *chip)
290 ak4396_registers_init(chip);
291 wm8785_registers_init(chip);
294 static void meridian_resume(struct oxygen *chip)
296 ak4396_registers_init(chip);
299 static void claro_resume(struct oxygen *chip)
301 ak4396_registers_init(chip);
302 claro_enable_hp(chip);
305 static void stereo_resume(struct oxygen *chip)
307 ak4396_registers_init(chip);
310 static void set_ak4396_params(struct oxygen *chip,
311 struct snd_pcm_hw_params *params)
313 struct generic_data *data = chip->model_data;
317 value = data->ak4396_regs[0][AK4396_CONTROL_2] & ~AK4396_DFS_MASK;
318 if (params_rate(params) <= 54000)
319 value |= AK4396_DFS_NORMAL;
320 else if (params_rate(params) <= 108000)
321 value |= AK4396_DFS_DOUBLE;
323 value |= AK4396_DFS_QUAD;
325 msleep(1); /* wait for the new MCLK to become stable */
327 if (value != data->ak4396_regs[0][AK4396_CONTROL_2]) {
328 for (i = 0; i < data->dacs; ++i) {
329 ak4396_write(chip, i, AK4396_CONTROL_1,
331 ak4396_write(chip, i, AK4396_CONTROL_2, value);
332 ak4396_write(chip, i, AK4396_CONTROL_1,
333 AK4396_DIF_24_MSB | AK4396_RSTN);
338 static void update_ak4396_volume(struct oxygen *chip)
340 struct generic_data *data = chip->model_data;
343 for (i = 0; i < data->dacs; ++i) {
344 ak4396_write_cached(chip, i, AK4396_LCH_ATT,
345 chip->dac_volume[i * 2]);
346 ak4396_write_cached(chip, i, AK4396_RCH_ATT,
347 chip->dac_volume[i * 2 + 1]);
351 static void update_ak4396_mute(struct oxygen *chip)
353 struct generic_data *data = chip->model_data;
357 value = data->ak4396_regs[0][AK4396_CONTROL_2] & ~AK4396_SMUTE;
359 value |= AK4396_SMUTE;
360 for (i = 0; i < data->dacs; ++i)
361 ak4396_write_cached(chip, i, AK4396_CONTROL_2, value);
364 static void set_wm8785_params(struct oxygen *chip,
365 struct snd_pcm_hw_params *params)
367 struct generic_data *data = chip->model_data;
370 value = WM8785_MCR_SLAVE | WM8785_FORMAT_LJUST;
371 if (params_rate(params) <= 48000)
372 value |= WM8785_OSR_SINGLE;
373 else if (params_rate(params) <= 96000)
374 value |= WM8785_OSR_DOUBLE;
376 value |= WM8785_OSR_QUAD;
377 if (value != data->wm8785_regs[0]) {
378 wm8785_write(chip, WM8785_R7, 0);
379 wm8785_write(chip, WM8785_R0, value);
380 wm8785_write(chip, WM8785_R2, data->wm8785_regs[2]);
384 static void set_ak5385_params(struct oxygen *chip,
385 struct snd_pcm_hw_params *params)
389 if (params_rate(params) <= 54000)
390 value = GPIO_AK5385_DFS_NORMAL;
391 else if (params_rate(params) <= 108000)
392 value = GPIO_AK5385_DFS_DOUBLE;
394 value = GPIO_AK5385_DFS_QUAD;
395 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
396 value, GPIO_AK5385_DFS_MASK);
399 static void set_no_params(struct oxygen *chip, struct snd_pcm_hw_params *params)
403 static int rolloff_info(struct snd_kcontrol *ctl,
404 struct snd_ctl_elem_info *info)
406 static const char *const names[2] = {
407 "Sharp Roll-off", "Slow Roll-off"
410 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
412 info->value.enumerated.items = 2;
413 if (info->value.enumerated.item >= 2)
414 info->value.enumerated.item = 1;
415 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
419 static int rolloff_get(struct snd_kcontrol *ctl,
420 struct snd_ctl_elem_value *value)
422 struct oxygen *chip = ctl->private_data;
423 struct generic_data *data = chip->model_data;
425 value->value.enumerated.item[0] =
426 (data->ak4396_regs[0][AK4396_CONTROL_2] & AK4396_SLOW) != 0;
430 static int rolloff_put(struct snd_kcontrol *ctl,
431 struct snd_ctl_elem_value *value)
433 struct oxygen *chip = ctl->private_data;
434 struct generic_data *data = chip->model_data;
439 mutex_lock(&chip->mutex);
440 reg = data->ak4396_regs[0][AK4396_CONTROL_2];
441 if (value->value.enumerated.item[0])
445 changed = reg != data->ak4396_regs[0][AK4396_CONTROL_2];
447 for (i = 0; i < data->dacs; ++i)
448 ak4396_write(chip, i, AK4396_CONTROL_2, reg);
450 mutex_unlock(&chip->mutex);
454 static const struct snd_kcontrol_new rolloff_control = {
455 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
456 .name = "DAC Filter Playback Enum",
457 .info = rolloff_info,
462 static int hpf_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
464 static const char *const names[2] = {
465 "None", "High-pass Filter"
468 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
470 info->value.enumerated.items = 2;
471 if (info->value.enumerated.item >= 2)
472 info->value.enumerated.item = 1;
473 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
477 static int hpf_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
479 struct oxygen *chip = ctl->private_data;
480 struct generic_data *data = chip->model_data;
482 value->value.enumerated.item[0] =
483 (data->wm8785_regs[WM8785_R2] & WM8785_HPFR) != 0;
487 static int hpf_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
489 struct oxygen *chip = ctl->private_data;
490 struct generic_data *data = chip->model_data;
494 mutex_lock(&chip->mutex);
495 reg = data->wm8785_regs[WM8785_R2] & ~(WM8785_HPFR | WM8785_HPFL);
496 if (value->value.enumerated.item[0])
497 reg |= WM8785_HPFR | WM8785_HPFL;
498 changed = reg != data->wm8785_regs[WM8785_R2];
500 wm8785_write(chip, WM8785_R2, reg);
501 mutex_unlock(&chip->mutex);
505 static const struct snd_kcontrol_new hpf_control = {
506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
507 .name = "ADC Filter Capture Enum",
513 static int generic_mixer_init(struct oxygen *chip)
515 return snd_ctl_add(chip->card, snd_ctl_new1(&rolloff_control, chip));
518 static int generic_wm8785_mixer_init(struct oxygen *chip)
522 err = generic_mixer_init(chip);
525 err = snd_ctl_add(chip->card, snd_ctl_new1(&hpf_control, chip));
531 static void dump_ak4396_registers(struct oxygen *chip,
532 struct snd_info_buffer *buffer)
534 struct generic_data *data = chip->model_data;
537 for (dac = 0; dac < data->dacs; ++dac) {
538 snd_iprintf(buffer, "\nAK4396 %u:", dac + 1);
539 for (i = 0; i < 5; ++i)
540 snd_iprintf(buffer, " %02x", data->ak4396_regs[dac][i]);
542 snd_iprintf(buffer, "\n");
545 static void dump_wm8785_registers(struct oxygen *chip,
546 struct snd_info_buffer *buffer)
548 struct generic_data *data = chip->model_data;
551 snd_iprintf(buffer, "\nWM8785:");
552 for (i = 0; i < 3; ++i)
553 snd_iprintf(buffer, " %03x", data->wm8785_regs[i]);
554 snd_iprintf(buffer, "\n");
557 static void dump_oxygen_registers(struct oxygen *chip,
558 struct snd_info_buffer *buffer)
560 dump_ak4396_registers(chip, buffer);
561 dump_wm8785_registers(chip, buffer);
564 static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0);
566 static const struct oxygen_model model_generic = {
567 .shortname = "C-Media CMI8788",
568 .longname = "C-Media Oxygen HD Audio",
570 .init = generic_init,
571 .mixer_init = generic_wm8785_mixer_init,
572 .cleanup = generic_cleanup,
573 .resume = generic_resume,
574 .set_dac_params = set_ak4396_params,
575 .set_adc_params = set_wm8785_params,
576 .update_dac_volume = update_ak4396_volume,
577 .update_dac_mute = update_ak4396_mute,
578 .dump_registers = dump_oxygen_registers,
579 .dac_tlv = ak4396_db_scale,
580 .model_data_size = sizeof(struct generic_data),
581 .device_config = PLAYBACK_0_TO_I2S |
582 PLAYBACK_1_TO_SPDIF |
583 PLAYBACK_2_TO_AC97_1 |
584 CAPTURE_0_FROM_I2S_1 |
585 CAPTURE_1_FROM_SPDIF |
586 CAPTURE_2_FROM_AC97_1 |
588 .dac_channels_pcm = 8,
589 .dac_channels_mixer = 8,
591 .dac_volume_max = 255,
592 .function_flags = OXYGEN_FUNCTION_SPI |
593 OXYGEN_FUNCTION_ENABLE_SPI_4_5,
594 .dac_mclks = OXYGEN_MCLKS(256, 128, 128),
595 .adc_mclks = OXYGEN_MCLKS(256, 256, 128),
596 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
597 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
600 static int __devinit get_oxygen_model(struct oxygen *chip,
601 const struct pci_device_id *id)
603 chip->model = model_generic;
604 switch (id->driver_data) {
606 chip->model.init = meridian_init;
607 chip->model.mixer_init = generic_mixer_init;
608 chip->model.resume = meridian_resume;
609 chip->model.set_adc_params = set_ak5385_params;
610 chip->model.dump_registers = dump_ak4396_registers;
611 chip->model.device_config = PLAYBACK_0_TO_I2S |
612 PLAYBACK_1_TO_SPDIF |
613 CAPTURE_0_FROM_I2S_2 |
614 CAPTURE_1_FROM_SPDIF;
617 chip->model.init = claro_init;
618 chip->model.cleanup = claro_cleanup;
619 chip->model.suspend = claro_suspend;
620 chip->model.resume = claro_resume;
622 case MODEL_CLARO_HALO:
623 chip->model.init = claro_halo_init;
624 chip->model.mixer_init = generic_mixer_init;
625 chip->model.cleanup = claro_cleanup;
626 chip->model.suspend = claro_suspend;
627 chip->model.resume = claro_resume;
628 chip->model.set_adc_params = set_ak5385_params;
629 chip->model.dump_registers = dump_ak4396_registers;
630 chip->model.device_config = PLAYBACK_0_TO_I2S |
631 PLAYBACK_1_TO_SPDIF |
632 CAPTURE_0_FROM_I2S_2 |
633 CAPTURE_1_FROM_SPDIF;
636 case MODEL_2CH_OUTPUT:
637 chip->model.shortname = "C-Media CMI8787";
638 chip->model.chip = "CMI8787";
639 if (id->driver_data == MODEL_FANTASIA)
640 chip->model.init = fantasia_init;
642 chip->model.init = stereo_output_init;
643 chip->model.resume = stereo_resume;
644 chip->model.mixer_init = generic_mixer_init;
645 chip->model.set_adc_params = set_no_params;
646 chip->model.dump_registers = dump_ak4396_registers;
647 chip->model.device_config = PLAYBACK_0_TO_I2S |
649 if (id->driver_data == MODEL_FANTASIA) {
650 chip->model.device_config |= CAPTURE_0_FROM_I2S_1;
651 chip->model.adc_mclks = OXYGEN_MCLKS(256, 128, 128);
653 chip->model.dac_channels_pcm = 2;
654 chip->model.dac_channels_mixer = 2;
657 if (id->driver_data == MODEL_MERIDIAN ||
658 id->driver_data == MODEL_CLARO_HALO) {
659 chip->model.misc_flags = OXYGEN_MISC_MIDI;
660 chip->model.device_config |= MIDI_OUTPUT | MIDI_INPUT;
665 static int __devinit generic_oxygen_probe(struct pci_dev *pci,
666 const struct pci_device_id *pci_id)
671 if (dev >= SNDRV_CARDS)
677 err = oxygen_pci_probe(pci, index[dev], id[dev], THIS_MODULE,
678 oxygen_ids, get_oxygen_model);
684 static struct pci_driver oxygen_driver = {
686 .id_table = oxygen_ids,
687 .probe = generic_oxygen_probe,
688 .remove = __devexit_p(oxygen_pci_remove),
690 .suspend = oxygen_pci_suspend,
691 .resume = oxygen_pci_resume,
695 static int __init alsa_card_oxygen_init(void)
697 return pci_register_driver(&oxygen_driver);
700 static void __exit alsa_card_oxygen_exit(void)
702 pci_unregister_driver(&oxygen_driver);
705 module_init(alsa_card_oxygen_init)
706 module_exit(alsa_card_oxygen_exit)