1 /* 2 * C-Media CMI8788 driver for Asus Xonar cards 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 * CMI8788: 22 * 23 * SPI 0 -> 1st PCM1796 (front) 24 * SPI 1 -> 2nd PCM1796 (surround) 25 * SPI 2 -> 3rd PCM1796 (center/LFE) 26 * SPI 4 -> 4th PCM1796 (back) 27 * 28 * GPIO 2 -> M0 of CS5381 29 * GPIO 3 -> M1 of CS5381 30 * GPIO 5 <- external power present (D2X only) 31 * GPIO 7 -> ALT 32 * GPIO 8 -> enable output to speakers 33 */ 34 35 #include <linux/pci.h> 36 #include <linux/delay.h> 37 #include <linux/mutex.h> 38 #include <sound/ac97_codec.h> 39 #include <sound/control.h> 40 #include <sound/core.h> 41 #include <sound/initval.h> 42 #include <sound/pcm.h> 43 #include <sound/tlv.h> 44 #include "oxygen.h" 45 #include "cm9780.h" 46 #include "pcm1796.h" 47 48 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); 49 MODULE_DESCRIPTION("Asus AV200 driver"); 50 MODULE_LICENSE("GPL"); 51 MODULE_SUPPORTED_DEVICE("{{Asus,AV200}}"); 52 53 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; 54 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; 55 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; 56 57 module_param_array(index, int, NULL, 0444); 58 MODULE_PARM_DESC(index, "card index"); 59 module_param_array(id, charp, NULL, 0444); 60 MODULE_PARM_DESC(id, "ID string"); 61 module_param_array(enable, bool, NULL, 0444); 62 MODULE_PARM_DESC(enable, "enable card"); 63 64 enum { 65 MODEL_D2, 66 MODEL_D2X, 67 }; 68 69 static struct pci_device_id xonar_ids[] __devinitdata = { 70 { OXYGEN_PCI_SUBID(0x1043, 0x8269), .driver_data = MODEL_D2 }, 71 { OXYGEN_PCI_SUBID(0x1043, 0x82b7), .driver_data = MODEL_D2X }, 72 { } 73 }; 74 MODULE_DEVICE_TABLE(pci, xonar_ids); 75 76 77 #define GPIO_CS5381_M_MASK 0x000c 78 #define GPIO_CS5381_M_SINGLE 0x0000 79 #define GPIO_CS5381_M_DOUBLE 0x0004 80 #define GPIO_CS5381_M_QUAD 0x0008 81 #define GPIO_EXT_POWER 0x0020 82 #define GPIO_ALT 0x0080 83 #define GPIO_OUTPUT_ENABLE 0x0100 84 85 struct xonar_data { 86 u8 has_power; 87 }; 88 89 static void pcm1796_write(struct oxygen *chip, unsigned int codec, 90 u8 reg, u8 value) 91 { 92 /* maps ALSA channel pair number to SPI output */ 93 static const u8 codec_map[4] = { 94 0, 1, 2, 4 95 }; 96 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER | 97 OXYGEN_SPI_DATA_LENGTH_2 | 98 OXYGEN_SPI_CLOCK_160 | 99 (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) | 100 OXYGEN_SPI_CEN_LATCH_CLOCK_HI, 101 (reg << 8) | value); 102 } 103 104 static void xonar_d2_init(struct oxygen *chip) 105 { 106 unsigned int i; 107 108 for (i = 0; i < 4; ++i) { 109 pcm1796_write(chip, i, 18, PCM1796_FMT_24_LJUST | PCM1796_ATLD); 110 pcm1796_write(chip, i, 19, PCM1796_FLT_SHARP | PCM1796_ATS_1); 111 pcm1796_write(chip, i, 20, PCM1796_OS_64); 112 pcm1796_write(chip, i, 21, 0); 113 pcm1796_write(chip, i, 16, 0xff); /* set ATL/ATR after ATLD */ 114 pcm1796_write(chip, i, 17, 0xff); 115 } 116 117 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 118 GPIO_CS5381_M_MASK | GPIO_ALT); 119 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, 120 GPIO_CS5381_M_SINGLE, 121 GPIO_CS5381_M_MASK | GPIO_ALT); 122 oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC); 123 msleep(300); 124 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_OUTPUT_ENABLE); 125 oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE); 126 127 snd_component_add(chip->card, "PCM1796"); 128 snd_component_add(chip->card, "CS5381"); 129 } 130 131 static void xonar_d2x_init(struct oxygen *chip) 132 { 133 struct xonar_data *data = chip->model_data; 134 135 xonar_d2_init(chip); 136 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_EXT_POWER); 137 oxygen_set_bits16(chip, OXYGEN_GPIO_INTERRUPT_MASK, GPIO_EXT_POWER); 138 chip->interrupt_mask |= OXYGEN_INT_GPIO; 139 data->has_power = !!(oxygen_read16(chip, OXYGEN_GPIO_DATA) 140 & GPIO_EXT_POWER); 141 } 142 143 static void xonar_cleanup(struct oxygen *chip) 144 { 145 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE); 146 } 147 148 static void set_pcm1796_params(struct oxygen *chip, 149 struct snd_pcm_hw_params *params) 150 { 151 #if 0 152 unsigned int i; 153 u8 value; 154 155 value = params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64; 156 for (i = 0; i < 4; ++i) 157 pcm1796_write(chip, i, 20, value); 158 #endif 159 } 160 161 static void update_pcm1796_volume(struct oxygen *chip) 162 { 163 unsigned int i; 164 165 for (i = 0; i < 4; ++i) { 166 pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]); 167 pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]); 168 } 169 } 170 171 static void update_pcm1796_mute(struct oxygen *chip) 172 { 173 unsigned int i; 174 u8 value; 175 176 value = PCM1796_FMT_24_LJUST | PCM1796_ATLD; 177 if (chip->dac_mute) 178 value |= PCM1796_MUTE; 179 for (i = 0; i < 4; ++i) 180 pcm1796_write(chip, i, 18, value); 181 } 182 183 static void set_cs5381_params(struct oxygen *chip, 184 struct snd_pcm_hw_params *params) 185 { 186 unsigned int value; 187 188 if (params_rate(params) <= 54000) 189 value = GPIO_CS5381_M_SINGLE; 190 else if (params_rate(params) <= 108000) 191 value = GPIO_CS5381_M_DOUBLE; 192 else 193 value = GPIO_CS5381_M_QUAD; 194 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, 195 value, GPIO_CS5381_M_MASK); 196 } 197 198 static void xonar_gpio_changed(struct oxygen *chip) 199 { 200 struct xonar_data *data = chip->model_data; 201 u8 has_power; 202 203 has_power = !!(oxygen_read16(chip, OXYGEN_GPIO_DATA) 204 & GPIO_EXT_POWER); 205 if (has_power != data->has_power) { 206 data->has_power = has_power; 207 if (has_power) { 208 snd_printk(KERN_NOTICE "power restored\n"); 209 } else { 210 snd_printk(KERN_CRIT 211 "Hey! Don't unplug the power cable!\n"); 212 /* TODO: stop PCMs */ 213 } 214 } 215 } 216 217 static int pcm1796_volume_info(struct snd_kcontrol *ctl, 218 struct snd_ctl_elem_info *info) 219 { 220 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 221 info->count = 8; 222 info->value.integer.min = 0x0f; 223 info->value.integer.max = 0xff; 224 return 0; 225 } 226 227 static int alt_switch_get(struct snd_kcontrol *ctl, 228 struct snd_ctl_elem_value *value) 229 { 230 struct oxygen *chip = ctl->private_data; 231 232 value->value.integer.value[0] = 233 !!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_ALT); 234 return 0; 235 } 236 237 static int alt_switch_put(struct snd_kcontrol *ctl, 238 struct snd_ctl_elem_value *value) 239 { 240 struct oxygen *chip = ctl->private_data; 241 u16 old_bits, new_bits; 242 int changed; 243 244 spin_lock_irq(&chip->reg_lock); 245 old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA); 246 if (value->value.integer.value[0]) 247 new_bits = old_bits | GPIO_ALT; 248 else 249 new_bits = old_bits & ~GPIO_ALT; 250 changed = new_bits != old_bits; 251 if (changed) 252 oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits); 253 spin_unlock_irq(&chip->reg_lock); 254 return changed; 255 } 256 257 static const struct snd_kcontrol_new alt_switch = { 258 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 259 .name = "Analog Loopback Switch", 260 .info = snd_ctl_boolean_mono_info, 261 .get = alt_switch_get, 262 .put = alt_switch_put, 263 }; 264 265 static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0); 266 267 static int xonar_control_filter(struct snd_kcontrol_new *template) 268 { 269 if (!strcmp(template->name, "Master Playback Volume")) { 270 template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ; 271 template->info = pcm1796_volume_info, 272 template->tlv.p = pcm1796_db_scale; 273 } else if (!strncmp(template->name, "CD Capture ", 11)) { 274 /* CD in is actually connected to the video in pin */ 275 template->private_value ^= AC97_CD ^ AC97_VIDEO; 276 } 277 return 0; 278 } 279 280 static int xonar_mixer_init(struct oxygen *chip) 281 { 282 return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip)); 283 } 284 285 static const struct oxygen_model xonar_models[] = { 286 [MODEL_D2] = { 287 .shortname = "Asus AV200", 288 .longname = "Asus Virtuoso 200", 289 .chip = "AV200", 290 .owner = THIS_MODULE, 291 .init = xonar_d2_init, 292 .control_filter = xonar_control_filter, 293 .mixer_init = xonar_mixer_init, 294 .cleanup = xonar_cleanup, 295 .set_dac_params = set_pcm1796_params, 296 .set_adc_params = set_cs5381_params, 297 .update_dac_volume = update_pcm1796_volume, 298 .update_dac_mute = update_pcm1796_mute, 299 .model_data_size = sizeof(struct xonar_data), 300 .pcm_dev_cfg = PLAYBACK_0_TO_I2S | 301 PLAYBACK_1_TO_SPDIF | 302 CAPTURE_0_FROM_I2S_2 | 303 CAPTURE_1_FROM_SPDIF, 304 .dac_channels = 8, 305 .misc_flags = OXYGEN_MISC_MIDI, 306 .function_flags = OXYGEN_FUNCTION_SPI | 307 OXYGEN_FUNCTION_ENABLE_SPI_4_5, 308 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 309 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 310 }, 311 [MODEL_D2X] = { 312 .shortname = "Asus AV200", 313 .longname = "Asus Virtuoso 200", 314 .chip = "AV200", 315 .owner = THIS_MODULE, 316 .init = xonar_d2x_init, 317 .control_filter = xonar_control_filter, 318 .mixer_init = xonar_mixer_init, 319 .cleanup = xonar_cleanup, 320 .set_dac_params = set_pcm1796_params, 321 .set_adc_params = set_cs5381_params, 322 .update_dac_volume = update_pcm1796_volume, 323 .update_dac_mute = update_pcm1796_mute, 324 .gpio_changed = xonar_gpio_changed, 325 .model_data_size = sizeof(struct xonar_data), 326 .pcm_dev_cfg = PLAYBACK_0_TO_I2S | 327 PLAYBACK_1_TO_SPDIF | 328 CAPTURE_0_FROM_I2S_2 | 329 CAPTURE_1_FROM_SPDIF, 330 .dac_channels = 8, 331 .misc_flags = OXYGEN_MISC_MIDI, 332 .function_flags = OXYGEN_FUNCTION_SPI | 333 OXYGEN_FUNCTION_ENABLE_SPI_4_5, 334 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 335 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 336 }, 337 }; 338 339 static int __devinit xonar_probe(struct pci_dev *pci, 340 const struct pci_device_id *pci_id) 341 { 342 static int dev; 343 int err; 344 345 if (dev >= SNDRV_CARDS) 346 return -ENODEV; 347 if (!enable[dev]) { 348 ++dev; 349 return -ENOENT; 350 } 351 err = oxygen_pci_probe(pci, index[dev], id[dev], 352 &xonar_models[pci_id->driver_data]); 353 if (err >= 0) 354 ++dev; 355 return err; 356 } 357 358 static struct pci_driver xonar_driver = { 359 .name = "AV200", 360 .id_table = xonar_ids, 361 .probe = xonar_probe, 362 .remove = __devexit_p(oxygen_pci_remove), 363 }; 364 365 static int __init alsa_card_xonar_init(void) 366 { 367 return pci_register_driver(&xonar_driver); 368 } 369 370 static void __exit alsa_card_xonar_exit(void) 371 { 372 pci_unregister_driver(&xonar_driver); 373 } 374 375 module_init(alsa_card_xonar_init) 376 module_exit(alsa_card_xonar_exit) 377