1 /* 2 * C-Media CMI8788 driver - mixer code 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 #include <linux/mutex.h> 21 #include <sound/ac97_codec.h> 22 #include <sound/asoundef.h> 23 #include <sound/control.h> 24 #include <sound/tlv.h> 25 #include "oxygen.h" 26 #include "cm9780.h" 27 28 static int dac_volume_info(struct snd_kcontrol *ctl, 29 struct snd_ctl_elem_info *info) 30 { 31 struct oxygen *chip = ctl->private_data; 32 33 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 34 info->count = chip->model.dac_channels_mixer; 35 info->value.integer.min = chip->model.dac_volume_min; 36 info->value.integer.max = chip->model.dac_volume_max; 37 return 0; 38 } 39 40 static int dac_volume_get(struct snd_kcontrol *ctl, 41 struct snd_ctl_elem_value *value) 42 { 43 struct oxygen *chip = ctl->private_data; 44 unsigned int i; 45 46 mutex_lock(&chip->mutex); 47 for (i = 0; i < chip->model.dac_channels_mixer; ++i) 48 value->value.integer.value[i] = chip->dac_volume[i]; 49 mutex_unlock(&chip->mutex); 50 return 0; 51 } 52 53 static int dac_volume_put(struct snd_kcontrol *ctl, 54 struct snd_ctl_elem_value *value) 55 { 56 struct oxygen *chip = ctl->private_data; 57 unsigned int i; 58 int changed; 59 60 changed = 0; 61 mutex_lock(&chip->mutex); 62 for (i = 0; i < chip->model.dac_channels_mixer; ++i) 63 if (value->value.integer.value[i] != chip->dac_volume[i]) { 64 chip->dac_volume[i] = value->value.integer.value[i]; 65 changed = 1; 66 } 67 if (changed) 68 chip->model.update_dac_volume(chip); 69 mutex_unlock(&chip->mutex); 70 return changed; 71 } 72 73 static int dac_mute_get(struct snd_kcontrol *ctl, 74 struct snd_ctl_elem_value *value) 75 { 76 struct oxygen *chip = ctl->private_data; 77 78 mutex_lock(&chip->mutex); 79 value->value.integer.value[0] = !chip->dac_mute; 80 mutex_unlock(&chip->mutex); 81 return 0; 82 } 83 84 static int dac_mute_put(struct snd_kcontrol *ctl, 85 struct snd_ctl_elem_value *value) 86 { 87 struct oxygen *chip = ctl->private_data; 88 int changed; 89 90 mutex_lock(&chip->mutex); 91 changed = !value->value.integer.value[0] != chip->dac_mute; 92 if (changed) { 93 chip->dac_mute = !value->value.integer.value[0]; 94 chip->model.update_dac_mute(chip); 95 } 96 mutex_unlock(&chip->mutex); 97 return changed; 98 } 99 100 static unsigned int upmix_item_count(struct oxygen *chip) 101 { 102 if (chip->model.dac_channels_pcm < 8) 103 return 2; 104 else if (chip->model.update_center_lfe_mix) 105 return 5; 106 else 107 return 3; 108 } 109 110 static int upmix_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info) 111 { 112 static const char *const names[5] = { 113 "Front", 114 "Front+Surround", 115 "Front+Surround+Back", 116 "Front+Surround+Center/LFE", 117 "Front+Surround+Center/LFE+Back", 118 }; 119 struct oxygen *chip = ctl->private_data; 120 unsigned int count = upmix_item_count(chip); 121 122 return snd_ctl_enum_info(info, 1, count, names); 123 } 124 125 static int upmix_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value) 126 { 127 struct oxygen *chip = ctl->private_data; 128 129 mutex_lock(&chip->mutex); 130 value->value.enumerated.item[0] = chip->dac_routing; 131 mutex_unlock(&chip->mutex); 132 return 0; 133 } 134 135 void oxygen_update_dac_routing(struct oxygen *chip) 136 { 137 /* DAC 0: front, DAC 1: surround, DAC 2: center/LFE, DAC 3: back */ 138 static const unsigned int reg_values[5] = { 139 /* stereo -> front */ 140 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) | 141 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) | 142 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) | 143 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT), 144 /* stereo -> front+surround */ 145 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) | 146 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) | 147 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) | 148 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT), 149 /* stereo -> front+surround+back */ 150 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) | 151 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) | 152 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) | 153 (0 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT), 154 /* stereo -> front+surround+center/LFE */ 155 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) | 156 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) | 157 (0 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) | 158 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT), 159 /* stereo -> front+surround+center/LFE+back */ 160 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) | 161 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) | 162 (0 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) | 163 (0 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT), 164 }; 165 u8 channels; 166 unsigned int reg_value; 167 168 channels = oxygen_read8(chip, OXYGEN_PLAY_CHANNELS) & 169 OXYGEN_PLAY_CHANNELS_MASK; 170 if (channels == OXYGEN_PLAY_CHANNELS_2) 171 reg_value = reg_values[chip->dac_routing]; 172 else if (channels == OXYGEN_PLAY_CHANNELS_8) 173 /* in 7.1 mode, "rear" channels go to the "back" jack */ 174 reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) | 175 (3 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) | 176 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) | 177 (1 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT); 178 else 179 reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) | 180 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) | 181 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) | 182 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT); 183 if (chip->model.adjust_dac_routing) 184 reg_value = chip->model.adjust_dac_routing(chip, reg_value); 185 oxygen_write16_masked(chip, OXYGEN_PLAY_ROUTING, reg_value, 186 OXYGEN_PLAY_DAC0_SOURCE_MASK | 187 OXYGEN_PLAY_DAC1_SOURCE_MASK | 188 OXYGEN_PLAY_DAC2_SOURCE_MASK | 189 OXYGEN_PLAY_DAC3_SOURCE_MASK); 190 if (chip->model.update_center_lfe_mix) 191 chip->model.update_center_lfe_mix(chip, chip->dac_routing > 2); 192 } 193 EXPORT_SYMBOL(oxygen_update_dac_routing); 194 195 static int upmix_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value) 196 { 197 struct oxygen *chip = ctl->private_data; 198 unsigned int count = upmix_item_count(chip); 199 int changed; 200 201 if (value->value.enumerated.item[0] >= count) 202 return -EINVAL; 203 mutex_lock(&chip->mutex); 204 changed = value->value.enumerated.item[0] != chip->dac_routing; 205 if (changed) { 206 chip->dac_routing = value->value.enumerated.item[0]; 207 oxygen_update_dac_routing(chip); 208 } 209 mutex_unlock(&chip->mutex); 210 return changed; 211 } 212 213 static int spdif_switch_get(struct snd_kcontrol *ctl, 214 struct snd_ctl_elem_value *value) 215 { 216 struct oxygen *chip = ctl->private_data; 217 218 mutex_lock(&chip->mutex); 219 value->value.integer.value[0] = chip->spdif_playback_enable; 220 mutex_unlock(&chip->mutex); 221 return 0; 222 } 223 224 static unsigned int oxygen_spdif_rate(unsigned int oxygen_rate) 225 { 226 switch (oxygen_rate) { 227 case OXYGEN_RATE_32000: 228 return IEC958_AES3_CON_FS_32000 << OXYGEN_SPDIF_CS_RATE_SHIFT; 229 case OXYGEN_RATE_44100: 230 return IEC958_AES3_CON_FS_44100 << OXYGEN_SPDIF_CS_RATE_SHIFT; 231 default: /* OXYGEN_RATE_48000 */ 232 return IEC958_AES3_CON_FS_48000 << OXYGEN_SPDIF_CS_RATE_SHIFT; 233 case OXYGEN_RATE_64000: 234 return 0xb << OXYGEN_SPDIF_CS_RATE_SHIFT; 235 case OXYGEN_RATE_88200: 236 return IEC958_AES3_CON_FS_88200 << OXYGEN_SPDIF_CS_RATE_SHIFT; 237 case OXYGEN_RATE_96000: 238 return IEC958_AES3_CON_FS_96000 << OXYGEN_SPDIF_CS_RATE_SHIFT; 239 case OXYGEN_RATE_176400: 240 return IEC958_AES3_CON_FS_176400 << OXYGEN_SPDIF_CS_RATE_SHIFT; 241 case OXYGEN_RATE_192000: 242 return IEC958_AES3_CON_FS_192000 << OXYGEN_SPDIF_CS_RATE_SHIFT; 243 } 244 } 245 246 void oxygen_update_spdif_source(struct oxygen *chip) 247 { 248 u32 old_control, new_control; 249 u16 old_routing, new_routing; 250 unsigned int oxygen_rate; 251 252 old_control = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL); 253 old_routing = oxygen_read16(chip, OXYGEN_PLAY_ROUTING); 254 if (chip->pcm_active & (1 << PCM_SPDIF)) { 255 new_control = old_control | OXYGEN_SPDIF_OUT_ENABLE; 256 new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK) 257 | OXYGEN_PLAY_SPDIF_SPDIF; 258 oxygen_rate = (old_control >> OXYGEN_SPDIF_OUT_RATE_SHIFT) 259 & OXYGEN_I2S_RATE_MASK; 260 /* S/PDIF rate was already set by the caller */ 261 } else if ((chip->pcm_active & (1 << PCM_MULTICH)) && 262 chip->spdif_playback_enable) { 263 new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK) 264 | OXYGEN_PLAY_SPDIF_MULTICH_01; 265 oxygen_rate = oxygen_read16(chip, OXYGEN_I2S_MULTICH_FORMAT) 266 & OXYGEN_I2S_RATE_MASK; 267 new_control = (old_control & ~OXYGEN_SPDIF_OUT_RATE_MASK) | 268 (oxygen_rate << OXYGEN_SPDIF_OUT_RATE_SHIFT) | 269 OXYGEN_SPDIF_OUT_ENABLE; 270 } else { 271 new_control = old_control & ~OXYGEN_SPDIF_OUT_ENABLE; 272 new_routing = old_routing; 273 oxygen_rate = OXYGEN_RATE_44100; 274 } 275 if (old_routing != new_routing) { 276 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, 277 new_control & ~OXYGEN_SPDIF_OUT_ENABLE); 278 oxygen_write16(chip, OXYGEN_PLAY_ROUTING, new_routing); 279 } 280 if (new_control & OXYGEN_SPDIF_OUT_ENABLE) 281 oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS, 282 oxygen_spdif_rate(oxygen_rate) | 283 ((chip->pcm_active & (1 << PCM_SPDIF)) ? 284 chip->spdif_pcm_bits : chip->spdif_bits)); 285 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, new_control); 286 } 287 288 static int spdif_switch_put(struct snd_kcontrol *ctl, 289 struct snd_ctl_elem_value *value) 290 { 291 struct oxygen *chip = ctl->private_data; 292 int changed; 293 294 mutex_lock(&chip->mutex); 295 changed = value->value.integer.value[0] != chip->spdif_playback_enable; 296 if (changed) { 297 chip->spdif_playback_enable = !!value->value.integer.value[0]; 298 spin_lock_irq(&chip->reg_lock); 299 oxygen_update_spdif_source(chip); 300 spin_unlock_irq(&chip->reg_lock); 301 } 302 mutex_unlock(&chip->mutex); 303 return changed; 304 } 305 306 static int spdif_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info) 307 { 308 info->type = SNDRV_CTL_ELEM_TYPE_IEC958; 309 info->count = 1; 310 return 0; 311 } 312 313 static void oxygen_to_iec958(u32 bits, struct snd_ctl_elem_value *value) 314 { 315 value->value.iec958.status[0] = 316 bits & (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C | 317 OXYGEN_SPDIF_PREEMPHASIS); 318 value->value.iec958.status[1] = /* category and original */ 319 bits >> OXYGEN_SPDIF_CATEGORY_SHIFT; 320 } 321 322 static u32 iec958_to_oxygen(struct snd_ctl_elem_value *value) 323 { 324 u32 bits; 325 326 bits = value->value.iec958.status[0] & 327 (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C | 328 OXYGEN_SPDIF_PREEMPHASIS); 329 bits |= value->value.iec958.status[1] << OXYGEN_SPDIF_CATEGORY_SHIFT; 330 if (bits & OXYGEN_SPDIF_NONAUDIO) 331 bits |= OXYGEN_SPDIF_V; 332 return bits; 333 } 334 335 static inline void write_spdif_bits(struct oxygen *chip, u32 bits) 336 { 337 oxygen_write32_masked(chip, OXYGEN_SPDIF_OUTPUT_BITS, bits, 338 OXYGEN_SPDIF_NONAUDIO | 339 OXYGEN_SPDIF_C | 340 OXYGEN_SPDIF_PREEMPHASIS | 341 OXYGEN_SPDIF_CATEGORY_MASK | 342 OXYGEN_SPDIF_ORIGINAL | 343 OXYGEN_SPDIF_V); 344 } 345 346 static int spdif_default_get(struct snd_kcontrol *ctl, 347 struct snd_ctl_elem_value *value) 348 { 349 struct oxygen *chip = ctl->private_data; 350 351 mutex_lock(&chip->mutex); 352 oxygen_to_iec958(chip->spdif_bits, value); 353 mutex_unlock(&chip->mutex); 354 return 0; 355 } 356 357 static int spdif_default_put(struct snd_kcontrol *ctl, 358 struct snd_ctl_elem_value *value) 359 { 360 struct oxygen *chip = ctl->private_data; 361 u32 new_bits; 362 int changed; 363 364 new_bits = iec958_to_oxygen(value); 365 mutex_lock(&chip->mutex); 366 changed = new_bits != chip->spdif_bits; 367 if (changed) { 368 chip->spdif_bits = new_bits; 369 if (!(chip->pcm_active & (1 << PCM_SPDIF))) 370 write_spdif_bits(chip, new_bits); 371 } 372 mutex_unlock(&chip->mutex); 373 return changed; 374 } 375 376 static int spdif_mask_get(struct snd_kcontrol *ctl, 377 struct snd_ctl_elem_value *value) 378 { 379 value->value.iec958.status[0] = IEC958_AES0_NONAUDIO | 380 IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS; 381 value->value.iec958.status[1] = 382 IEC958_AES1_CON_CATEGORY | IEC958_AES1_CON_ORIGINAL; 383 return 0; 384 } 385 386 static int spdif_pcm_get(struct snd_kcontrol *ctl, 387 struct snd_ctl_elem_value *value) 388 { 389 struct oxygen *chip = ctl->private_data; 390 391 mutex_lock(&chip->mutex); 392 oxygen_to_iec958(chip->spdif_pcm_bits, value); 393 mutex_unlock(&chip->mutex); 394 return 0; 395 } 396 397 static int spdif_pcm_put(struct snd_kcontrol *ctl, 398 struct snd_ctl_elem_value *value) 399 { 400 struct oxygen *chip = ctl->private_data; 401 u32 new_bits; 402 int changed; 403 404 new_bits = iec958_to_oxygen(value); 405 mutex_lock(&chip->mutex); 406 changed = new_bits != chip->spdif_pcm_bits; 407 if (changed) { 408 chip->spdif_pcm_bits = new_bits; 409 if (chip->pcm_active & (1 << PCM_SPDIF)) 410 write_spdif_bits(chip, new_bits); 411 } 412 mutex_unlock(&chip->mutex); 413 return changed; 414 } 415 416 static int spdif_input_mask_get(struct snd_kcontrol *ctl, 417 struct snd_ctl_elem_value *value) 418 { 419 value->value.iec958.status[0] = 0xff; 420 value->value.iec958.status[1] = 0xff; 421 value->value.iec958.status[2] = 0xff; 422 value->value.iec958.status[3] = 0xff; 423 return 0; 424 } 425 426 static int spdif_input_default_get(struct snd_kcontrol *ctl, 427 struct snd_ctl_elem_value *value) 428 { 429 struct oxygen *chip = ctl->private_data; 430 u32 bits; 431 432 bits = oxygen_read32(chip, OXYGEN_SPDIF_INPUT_BITS); 433 value->value.iec958.status[0] = bits; 434 value->value.iec958.status[1] = bits >> 8; 435 value->value.iec958.status[2] = bits >> 16; 436 value->value.iec958.status[3] = bits >> 24; 437 return 0; 438 } 439 440 static int spdif_bit_switch_get(struct snd_kcontrol *ctl, 441 struct snd_ctl_elem_value *value) 442 { 443 struct oxygen *chip = ctl->private_data; 444 u32 bit = ctl->private_value; 445 446 value->value.integer.value[0] = 447 !!(oxygen_read32(chip, OXYGEN_SPDIF_CONTROL) & bit); 448 return 0; 449 } 450 451 static int spdif_bit_switch_put(struct snd_kcontrol *ctl, 452 struct snd_ctl_elem_value *value) 453 { 454 struct oxygen *chip = ctl->private_data; 455 u32 bit = ctl->private_value; 456 u32 oldreg, newreg; 457 int changed; 458 459 spin_lock_irq(&chip->reg_lock); 460 oldreg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL); 461 if (value->value.integer.value[0]) 462 newreg = oldreg | bit; 463 else 464 newreg = oldreg & ~bit; 465 changed = newreg != oldreg; 466 if (changed) 467 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, newreg); 468 spin_unlock_irq(&chip->reg_lock); 469 return changed; 470 } 471 472 static int monitor_volume_info(struct snd_kcontrol *ctl, 473 struct snd_ctl_elem_info *info) 474 { 475 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 476 info->count = 1; 477 info->value.integer.min = 0; 478 info->value.integer.max = 1; 479 return 0; 480 } 481 482 static int monitor_get(struct snd_kcontrol *ctl, 483 struct snd_ctl_elem_value *value) 484 { 485 struct oxygen *chip = ctl->private_data; 486 u8 bit = ctl->private_value; 487 int invert = ctl->private_value & (1 << 8); 488 489 value->value.integer.value[0] = 490 !!invert ^ !!(oxygen_read8(chip, OXYGEN_ADC_MONITOR) & bit); 491 return 0; 492 } 493 494 static int monitor_put(struct snd_kcontrol *ctl, 495 struct snd_ctl_elem_value *value) 496 { 497 struct oxygen *chip = ctl->private_data; 498 u8 bit = ctl->private_value; 499 int invert = ctl->private_value & (1 << 8); 500 u8 oldreg, newreg; 501 int changed; 502 503 spin_lock_irq(&chip->reg_lock); 504 oldreg = oxygen_read8(chip, OXYGEN_ADC_MONITOR); 505 if ((!!value->value.integer.value[0] ^ !!invert) != 0) 506 newreg = oldreg | bit; 507 else 508 newreg = oldreg & ~bit; 509 changed = newreg != oldreg; 510 if (changed) 511 oxygen_write8(chip, OXYGEN_ADC_MONITOR, newreg); 512 spin_unlock_irq(&chip->reg_lock); 513 return changed; 514 } 515 516 static int ac97_switch_get(struct snd_kcontrol *ctl, 517 struct snd_ctl_elem_value *value) 518 { 519 struct oxygen *chip = ctl->private_data; 520 unsigned int codec = (ctl->private_value >> 24) & 1; 521 unsigned int index = ctl->private_value & 0xff; 522 unsigned int bitnr = (ctl->private_value >> 8) & 0xff; 523 int invert = ctl->private_value & (1 << 16); 524 u16 reg; 525 526 mutex_lock(&chip->mutex); 527 reg = oxygen_read_ac97(chip, codec, index); 528 mutex_unlock(&chip->mutex); 529 if (!(reg & (1 << bitnr)) ^ !invert) 530 value->value.integer.value[0] = 1; 531 else 532 value->value.integer.value[0] = 0; 533 return 0; 534 } 535 536 static void mute_ac97_ctl(struct oxygen *chip, unsigned int control) 537 { 538 unsigned int priv_idx; 539 u16 value; 540 541 if (!chip->controls[control]) 542 return; 543 priv_idx = chip->controls[control]->private_value & 0xff; 544 value = oxygen_read_ac97(chip, 0, priv_idx); 545 if (!(value & 0x8000)) { 546 oxygen_write_ac97(chip, 0, priv_idx, value | 0x8000); 547 if (chip->model.ac97_switch) 548 chip->model.ac97_switch(chip, priv_idx, 0x8000); 549 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, 550 &chip->controls[control]->id); 551 } 552 } 553 554 static int ac97_switch_put(struct snd_kcontrol *ctl, 555 struct snd_ctl_elem_value *value) 556 { 557 struct oxygen *chip = ctl->private_data; 558 unsigned int codec = (ctl->private_value >> 24) & 1; 559 unsigned int index = ctl->private_value & 0xff; 560 unsigned int bitnr = (ctl->private_value >> 8) & 0xff; 561 int invert = ctl->private_value & (1 << 16); 562 u16 oldreg, newreg; 563 int change; 564 565 mutex_lock(&chip->mutex); 566 oldreg = oxygen_read_ac97(chip, codec, index); 567 newreg = oldreg; 568 if (!value->value.integer.value[0] ^ !invert) 569 newreg |= 1 << bitnr; 570 else 571 newreg &= ~(1 << bitnr); 572 change = newreg != oldreg; 573 if (change) { 574 oxygen_write_ac97(chip, codec, index, newreg); 575 if (codec == 0 && chip->model.ac97_switch) 576 chip->model.ac97_switch(chip, index, newreg & 0x8000); 577 if (index == AC97_LINE) { 578 oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS, 579 newreg & 0x8000 ? 580 CM9780_GPO0 : 0, CM9780_GPO0); 581 if (!(newreg & 0x8000)) { 582 mute_ac97_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH); 583 mute_ac97_ctl(chip, CONTROL_CD_CAPTURE_SWITCH); 584 mute_ac97_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH); 585 } 586 } else if ((index == AC97_MIC || index == AC97_CD || 587 index == AC97_VIDEO || index == AC97_AUX) && 588 bitnr == 15 && !(newreg & 0x8000)) { 589 mute_ac97_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH); 590 oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS, 591 CM9780_GPO0, CM9780_GPO0); 592 } 593 } 594 mutex_unlock(&chip->mutex); 595 return change; 596 } 597 598 static int ac97_volume_info(struct snd_kcontrol *ctl, 599 struct snd_ctl_elem_info *info) 600 { 601 int stereo = (ctl->private_value >> 16) & 1; 602 603 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 604 info->count = stereo ? 2 : 1; 605 info->value.integer.min = 0; 606 info->value.integer.max = 0x1f; 607 return 0; 608 } 609 610 static int ac97_volume_get(struct snd_kcontrol *ctl, 611 struct snd_ctl_elem_value *value) 612 { 613 struct oxygen *chip = ctl->private_data; 614 unsigned int codec = (ctl->private_value >> 24) & 1; 615 int stereo = (ctl->private_value >> 16) & 1; 616 unsigned int index = ctl->private_value & 0xff; 617 u16 reg; 618 619 mutex_lock(&chip->mutex); 620 reg = oxygen_read_ac97(chip, codec, index); 621 mutex_unlock(&chip->mutex); 622 if (!stereo) { 623 value->value.integer.value[0] = 31 - (reg & 0x1f); 624 } else { 625 value->value.integer.value[0] = 31 - ((reg >> 8) & 0x1f); 626 value->value.integer.value[1] = 31 - (reg & 0x1f); 627 } 628 return 0; 629 } 630 631 static int ac97_volume_put(struct snd_kcontrol *ctl, 632 struct snd_ctl_elem_value *value) 633 { 634 struct oxygen *chip = ctl->private_data; 635 unsigned int codec = (ctl->private_value >> 24) & 1; 636 int stereo = (ctl->private_value >> 16) & 1; 637 unsigned int index = ctl->private_value & 0xff; 638 u16 oldreg, newreg; 639 int change; 640 641 mutex_lock(&chip->mutex); 642 oldreg = oxygen_read_ac97(chip, codec, index); 643 if (!stereo) { 644 newreg = oldreg & ~0x1f; 645 newreg |= 31 - (value->value.integer.value[0] & 0x1f); 646 } else { 647 newreg = oldreg & ~0x1f1f; 648 newreg |= (31 - (value->value.integer.value[0] & 0x1f)) << 8; 649 newreg |= 31 - (value->value.integer.value[1] & 0x1f); 650 } 651 change = newreg != oldreg; 652 if (change) 653 oxygen_write_ac97(chip, codec, index, newreg); 654 mutex_unlock(&chip->mutex); 655 return change; 656 } 657 658 static int mic_fmic_source_info(struct snd_kcontrol *ctl, 659 struct snd_ctl_elem_info *info) 660 { 661 static const char *const names[] = { "Mic Jack", "Front Panel" }; 662 663 return snd_ctl_enum_info(info, 1, 2, names); 664 } 665 666 static int mic_fmic_source_get(struct snd_kcontrol *ctl, 667 struct snd_ctl_elem_value *value) 668 { 669 struct oxygen *chip = ctl->private_data; 670 671 mutex_lock(&chip->mutex); 672 value->value.enumerated.item[0] = 673 !!(oxygen_read_ac97(chip, 0, CM9780_JACK) & CM9780_FMIC2MIC); 674 mutex_unlock(&chip->mutex); 675 return 0; 676 } 677 678 static int mic_fmic_source_put(struct snd_kcontrol *ctl, 679 struct snd_ctl_elem_value *value) 680 { 681 struct oxygen *chip = ctl->private_data; 682 u16 oldreg, newreg; 683 int change; 684 685 mutex_lock(&chip->mutex); 686 oldreg = oxygen_read_ac97(chip, 0, CM9780_JACK); 687 if (value->value.enumerated.item[0]) 688 newreg = oldreg | CM9780_FMIC2MIC; 689 else 690 newreg = oldreg & ~CM9780_FMIC2MIC; 691 change = newreg != oldreg; 692 if (change) 693 oxygen_write_ac97(chip, 0, CM9780_JACK, newreg); 694 mutex_unlock(&chip->mutex); 695 return change; 696 } 697 698 static int ac97_fp_rec_volume_info(struct snd_kcontrol *ctl, 699 struct snd_ctl_elem_info *info) 700 { 701 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 702 info->count = 2; 703 info->value.integer.min = 0; 704 info->value.integer.max = 7; 705 return 0; 706 } 707 708 static int ac97_fp_rec_volume_get(struct snd_kcontrol *ctl, 709 struct snd_ctl_elem_value *value) 710 { 711 struct oxygen *chip = ctl->private_data; 712 u16 reg; 713 714 mutex_lock(&chip->mutex); 715 reg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN); 716 mutex_unlock(&chip->mutex); 717 value->value.integer.value[0] = reg & 7; 718 value->value.integer.value[1] = (reg >> 8) & 7; 719 return 0; 720 } 721 722 static int ac97_fp_rec_volume_put(struct snd_kcontrol *ctl, 723 struct snd_ctl_elem_value *value) 724 { 725 struct oxygen *chip = ctl->private_data; 726 u16 oldreg, newreg; 727 int change; 728 729 mutex_lock(&chip->mutex); 730 oldreg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN); 731 newreg = oldreg & ~0x0707; 732 newreg = newreg | (value->value.integer.value[0] & 7); 733 newreg = newreg | ((value->value.integer.value[0] & 7) << 8); 734 change = newreg != oldreg; 735 if (change) 736 oxygen_write_ac97(chip, 1, AC97_REC_GAIN, newreg); 737 mutex_unlock(&chip->mutex); 738 return change; 739 } 740 741 #define AC97_SWITCH(xname, codec, index, bitnr, invert) { \ 742 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ 743 .name = xname, \ 744 .info = snd_ctl_boolean_mono_info, \ 745 .get = ac97_switch_get, \ 746 .put = ac97_switch_put, \ 747 .private_value = ((codec) << 24) | ((invert) << 16) | \ 748 ((bitnr) << 8) | (index), \ 749 } 750 #define AC97_VOLUME(xname, codec, index, stereo) { \ 751 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ 752 .name = xname, \ 753 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \ 754 SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ 755 .info = ac97_volume_info, \ 756 .get = ac97_volume_get, \ 757 .put = ac97_volume_put, \ 758 .tlv = { .p = ac97_db_scale, }, \ 759 .private_value = ((codec) << 24) | ((stereo) << 16) | (index), \ 760 } 761 762 static DECLARE_TLV_DB_SCALE(monitor_db_scale, -600, 600, 0); 763 static DECLARE_TLV_DB_SCALE(ac97_db_scale, -3450, 150, 0); 764 static DECLARE_TLV_DB_SCALE(ac97_rec_db_scale, 0, 150, 0); 765 766 static const struct snd_kcontrol_new controls[] = { 767 { 768 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 769 .name = "Master Playback Volume", 770 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 771 .info = dac_volume_info, 772 .get = dac_volume_get, 773 .put = dac_volume_put, 774 }, 775 { 776 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 777 .name = "Master Playback Switch", 778 .info = snd_ctl_boolean_mono_info, 779 .get = dac_mute_get, 780 .put = dac_mute_put, 781 }, 782 { 783 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 784 .name = "Stereo Upmixing", 785 .info = upmix_info, 786 .get = upmix_get, 787 .put = upmix_put, 788 }, 789 { 790 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 791 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), 792 .info = snd_ctl_boolean_mono_info, 793 .get = spdif_switch_get, 794 .put = spdif_switch_put, 795 }, 796 { 797 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 798 .device = 1, 799 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), 800 .info = spdif_info, 801 .get = spdif_default_get, 802 .put = spdif_default_put, 803 }, 804 { 805 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 806 .device = 1, 807 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), 808 .access = SNDRV_CTL_ELEM_ACCESS_READ, 809 .info = spdif_info, 810 .get = spdif_mask_get, 811 }, 812 { 813 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 814 .device = 1, 815 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM), 816 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 817 SNDRV_CTL_ELEM_ACCESS_INACTIVE, 818 .info = spdif_info, 819 .get = spdif_pcm_get, 820 .put = spdif_pcm_put, 821 }, 822 }; 823 824 static const struct snd_kcontrol_new spdif_input_controls[] = { 825 { 826 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 827 .device = 1, 828 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, MASK), 829 .access = SNDRV_CTL_ELEM_ACCESS_READ, 830 .info = spdif_info, 831 .get = spdif_input_mask_get, 832 }, 833 { 834 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 835 .device = 1, 836 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), 837 .access = SNDRV_CTL_ELEM_ACCESS_READ, 838 .info = spdif_info, 839 .get = spdif_input_default_get, 840 }, 841 { 842 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 843 .name = SNDRV_CTL_NAME_IEC958("Loopback ", NONE, SWITCH), 844 .info = snd_ctl_boolean_mono_info, 845 .get = spdif_bit_switch_get, 846 .put = spdif_bit_switch_put, 847 .private_value = OXYGEN_SPDIF_LOOPBACK, 848 }, 849 { 850 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 851 .name = SNDRV_CTL_NAME_IEC958("Validity Check ",CAPTURE,SWITCH), 852 .info = snd_ctl_boolean_mono_info, 853 .get = spdif_bit_switch_get, 854 .put = spdif_bit_switch_put, 855 .private_value = OXYGEN_SPDIF_SPDVALID, 856 }, 857 }; 858 859 static const struct { 860 unsigned int pcm_dev; 861 struct snd_kcontrol_new controls[2]; 862 } monitor_controls[] = { 863 { 864 .pcm_dev = CAPTURE_0_FROM_I2S_1, 865 .controls = { 866 { 867 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 868 .name = "Analog Input Monitor Playback Switch", 869 .info = snd_ctl_boolean_mono_info, 870 .get = monitor_get, 871 .put = monitor_put, 872 .private_value = OXYGEN_ADC_MONITOR_A, 873 }, 874 { 875 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 876 .name = "Analog Input Monitor Playback Volume", 877 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 878 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 879 .info = monitor_volume_info, 880 .get = monitor_get, 881 .put = monitor_put, 882 .private_value = OXYGEN_ADC_MONITOR_A_HALF_VOL 883 | (1 << 8), 884 .tlv = { .p = monitor_db_scale, }, 885 }, 886 }, 887 }, 888 { 889 .pcm_dev = CAPTURE_0_FROM_I2S_2, 890 .controls = { 891 { 892 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 893 .name = "Analog Input Monitor Playback Switch", 894 .info = snd_ctl_boolean_mono_info, 895 .get = monitor_get, 896 .put = monitor_put, 897 .private_value = OXYGEN_ADC_MONITOR_B, 898 }, 899 { 900 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 901 .name = "Analog Input Monitor Playback Volume", 902 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 903 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 904 .info = monitor_volume_info, 905 .get = monitor_get, 906 .put = monitor_put, 907 .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL 908 | (1 << 8), 909 .tlv = { .p = monitor_db_scale, }, 910 }, 911 }, 912 }, 913 { 914 .pcm_dev = CAPTURE_2_FROM_I2S_2, 915 .controls = { 916 { 917 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 918 .name = "Analog Input Monitor Playback Switch", 919 .index = 1, 920 .info = snd_ctl_boolean_mono_info, 921 .get = monitor_get, 922 .put = monitor_put, 923 .private_value = OXYGEN_ADC_MONITOR_B, 924 }, 925 { 926 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 927 .name = "Analog Input Monitor Playback Volume", 928 .index = 1, 929 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 930 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 931 .info = monitor_volume_info, 932 .get = monitor_get, 933 .put = monitor_put, 934 .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL 935 | (1 << 8), 936 .tlv = { .p = monitor_db_scale, }, 937 }, 938 }, 939 }, 940 { 941 .pcm_dev = CAPTURE_1_FROM_SPDIF, 942 .controls = { 943 { 944 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 945 .name = "Digital Input Monitor Playback Switch", 946 .info = snd_ctl_boolean_mono_info, 947 .get = monitor_get, 948 .put = monitor_put, 949 .private_value = OXYGEN_ADC_MONITOR_C, 950 }, 951 { 952 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 953 .name = "Digital Input Monitor Playback Volume", 954 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 955 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 956 .info = monitor_volume_info, 957 .get = monitor_get, 958 .put = monitor_put, 959 .private_value = OXYGEN_ADC_MONITOR_C_HALF_VOL 960 | (1 << 8), 961 .tlv = { .p = monitor_db_scale, }, 962 }, 963 }, 964 }, 965 }; 966 967 static const struct snd_kcontrol_new ac97_controls[] = { 968 AC97_VOLUME("Mic Capture Volume", 0, AC97_MIC, 0), 969 AC97_SWITCH("Mic Capture Switch", 0, AC97_MIC, 15, 1), 970 AC97_SWITCH("Mic Boost (+20dB)", 0, AC97_MIC, 6, 0), 971 { 972 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 973 .name = "Mic Source Capture Enum", 974 .info = mic_fmic_source_info, 975 .get = mic_fmic_source_get, 976 .put = mic_fmic_source_put, 977 }, 978 AC97_SWITCH("Line Capture Switch", 0, AC97_LINE, 15, 1), 979 AC97_VOLUME("CD Capture Volume", 0, AC97_CD, 1), 980 AC97_SWITCH("CD Capture Switch", 0, AC97_CD, 15, 1), 981 AC97_VOLUME("Aux Capture Volume", 0, AC97_AUX, 1), 982 AC97_SWITCH("Aux Capture Switch", 0, AC97_AUX, 15, 1), 983 }; 984 985 static const struct snd_kcontrol_new ac97_fp_controls[] = { 986 AC97_VOLUME("Front Panel Playback Volume", 1, AC97_HEADPHONE, 1), 987 AC97_SWITCH("Front Panel Playback Switch", 1, AC97_HEADPHONE, 15, 1), 988 { 989 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 990 .name = "Front Panel Capture Volume", 991 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 992 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 993 .info = ac97_fp_rec_volume_info, 994 .get = ac97_fp_rec_volume_get, 995 .put = ac97_fp_rec_volume_put, 996 .tlv = { .p = ac97_rec_db_scale, }, 997 }, 998 AC97_SWITCH("Front Panel Capture Switch", 1, AC97_REC_GAIN, 15, 1), 999 }; 1000 1001 static void oxygen_any_ctl_free(struct snd_kcontrol *ctl) 1002 { 1003 struct oxygen *chip = ctl->private_data; 1004 unsigned int i; 1005 1006 /* I'm too lazy to write a function for each control :-) */ 1007 for (i = 0; i < ARRAY_SIZE(chip->controls); ++i) 1008 chip->controls[i] = NULL; 1009 } 1010 1011 static int add_controls(struct oxygen *chip, 1012 const struct snd_kcontrol_new controls[], 1013 unsigned int count) 1014 { 1015 static const char *const known_ctl_names[CONTROL_COUNT] = { 1016 [CONTROL_SPDIF_PCM] = 1017 SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM), 1018 [CONTROL_SPDIF_INPUT_BITS] = 1019 SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), 1020 [CONTROL_MIC_CAPTURE_SWITCH] = "Mic Capture Switch", 1021 [CONTROL_LINE_CAPTURE_SWITCH] = "Line Capture Switch", 1022 [CONTROL_CD_CAPTURE_SWITCH] = "CD Capture Switch", 1023 [CONTROL_AUX_CAPTURE_SWITCH] = "Aux Capture Switch", 1024 }; 1025 unsigned int i, j; 1026 struct snd_kcontrol_new template; 1027 struct snd_kcontrol *ctl; 1028 int err; 1029 1030 for (i = 0; i < count; ++i) { 1031 template = controls[i]; 1032 if (chip->model.control_filter) { 1033 err = chip->model.control_filter(&template); 1034 if (err < 0) 1035 return err; 1036 if (err == 1) 1037 continue; 1038 } 1039 if (!strcmp(template.name, "Stereo Upmixing") && 1040 chip->model.dac_channels_pcm == 2) 1041 continue; 1042 if (!strcmp(template.name, "Mic Source Capture Enum") && 1043 !(chip->model.device_config & AC97_FMIC_SWITCH)) 1044 continue; 1045 if (!strncmp(template.name, "CD Capture ", 11) && 1046 !(chip->model.device_config & AC97_CD_INPUT)) 1047 continue; 1048 if (!strcmp(template.name, "Master Playback Volume") && 1049 chip->model.dac_tlv) { 1050 template.tlv.p = chip->model.dac_tlv; 1051 template.access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ; 1052 } 1053 ctl = snd_ctl_new1(&template, chip); 1054 if (!ctl) 1055 return -ENOMEM; 1056 err = snd_ctl_add(chip->card, ctl); 1057 if (err < 0) 1058 return err; 1059 for (j = 0; j < CONTROL_COUNT; ++j) 1060 if (!strcmp(ctl->id.name, known_ctl_names[j])) { 1061 chip->controls[j] = ctl; 1062 ctl->private_free = oxygen_any_ctl_free; 1063 } 1064 } 1065 return 0; 1066 } 1067 1068 int oxygen_mixer_init(struct oxygen *chip) 1069 { 1070 unsigned int i; 1071 int err; 1072 1073 err = add_controls(chip, controls, ARRAY_SIZE(controls)); 1074 if (err < 0) 1075 return err; 1076 if (chip->model.device_config & CAPTURE_1_FROM_SPDIF) { 1077 err = add_controls(chip, spdif_input_controls, 1078 ARRAY_SIZE(spdif_input_controls)); 1079 if (err < 0) 1080 return err; 1081 } 1082 for (i = 0; i < ARRAY_SIZE(monitor_controls); ++i) { 1083 if (!(chip->model.device_config & monitor_controls[i].pcm_dev)) 1084 continue; 1085 err = add_controls(chip, monitor_controls[i].controls, 1086 ARRAY_SIZE(monitor_controls[i].controls)); 1087 if (err < 0) 1088 return err; 1089 } 1090 if (chip->has_ac97_0) { 1091 err = add_controls(chip, ac97_controls, 1092 ARRAY_SIZE(ac97_controls)); 1093 if (err < 0) 1094 return err; 1095 } 1096 if (chip->has_ac97_1) { 1097 err = add_controls(chip, ac97_fp_controls, 1098 ARRAY_SIZE(ac97_fp_controls)); 1099 if (err < 0) 1100 return err; 1101 } 1102 return chip->model.mixer_init ? chip->model.mixer_init(chip) : 0; 1103 } 1104