Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 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 | |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 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" |
Clemens Ladisch | 878ac3e | 2008-01-21 08:50:19 +0100 | [diff] [blame^] | 26 | #include "cm9780.h" |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 27 | |
| 28 | static int dac_volume_info(struct snd_kcontrol *ctl, |
| 29 | struct snd_ctl_elem_info *info) |
| 30 | { |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 31 | info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 32 | info->count = 8; |
Clemens Ladisch | ccc80fb | 2008-01-16 08:32:08 +0100 | [diff] [blame] | 33 | info->value.integer.min = 0; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 34 | info->value.integer.max = 0xff; |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | static int dac_volume_get(struct snd_kcontrol *ctl, |
| 39 | struct snd_ctl_elem_value *value) |
| 40 | { |
| 41 | struct oxygen *chip = ctl->private_data; |
| 42 | unsigned int i; |
| 43 | |
| 44 | mutex_lock(&chip->mutex); |
| 45 | for (i = 0; i < 8; ++i) |
| 46 | value->value.integer.value[i] = chip->dac_volume[i]; |
| 47 | mutex_unlock(&chip->mutex); |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int dac_volume_put(struct snd_kcontrol *ctl, |
| 52 | struct snd_ctl_elem_value *value) |
| 53 | { |
| 54 | struct oxygen *chip = ctl->private_data; |
| 55 | unsigned int i; |
| 56 | int changed; |
| 57 | |
| 58 | changed = 0; |
| 59 | mutex_lock(&chip->mutex); |
| 60 | for (i = 0; i < 8; ++i) |
| 61 | if (value->value.integer.value[i] != chip->dac_volume[i]) { |
| 62 | chip->dac_volume[i] = value->value.integer.value[i]; |
| 63 | changed = 1; |
| 64 | } |
| 65 | if (changed) |
| 66 | chip->model->update_dac_volume(chip); |
| 67 | mutex_unlock(&chip->mutex); |
| 68 | return changed; |
| 69 | } |
| 70 | |
| 71 | static int dac_mute_get(struct snd_kcontrol *ctl, |
| 72 | struct snd_ctl_elem_value *value) |
| 73 | { |
| 74 | struct oxygen *chip = ctl->private_data; |
| 75 | |
| 76 | mutex_lock(&chip->mutex); |
| 77 | value->value.integer.value[0] = !chip->dac_mute; |
| 78 | mutex_unlock(&chip->mutex); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static int dac_mute_put(struct snd_kcontrol *ctl, |
| 83 | struct snd_ctl_elem_value *value) |
| 84 | { |
| 85 | struct oxygen *chip = ctl->private_data; |
| 86 | int changed; |
| 87 | |
| 88 | mutex_lock(&chip->mutex); |
| 89 | changed = !value->value.integer.value[0] != chip->dac_mute; |
| 90 | if (changed) { |
| 91 | chip->dac_mute = !value->value.integer.value[0]; |
| 92 | chip->model->update_dac_mute(chip); |
| 93 | } |
| 94 | mutex_unlock(&chip->mutex); |
| 95 | return changed; |
| 96 | } |
| 97 | |
| 98 | static int upmix_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info) |
| 99 | { |
| 100 | static const char *const names[3] = { |
Clemens Ladisch | 7113e95 | 2008-01-14 08:55:03 +0100 | [diff] [blame] | 101 | "Front", "Front+Surround", "Front+Surround+Back" |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 102 | }; |
| 103 | info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 104 | info->count = 1; |
| 105 | info->value.enumerated.items = 3; |
| 106 | if (info->value.enumerated.item > 2) |
| 107 | info->value.enumerated.item = 2; |
| 108 | strcpy(info->value.enumerated.name, names[info->value.enumerated.item]); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int upmix_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value) |
| 113 | { |
| 114 | struct oxygen *chip = ctl->private_data; |
| 115 | |
| 116 | mutex_lock(&chip->mutex); |
| 117 | value->value.enumerated.item[0] = chip->dac_routing; |
| 118 | mutex_unlock(&chip->mutex); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | void oxygen_update_dac_routing(struct oxygen *chip) |
| 123 | { |
Clemens Ladisch | c9946b2 | 2008-01-21 08:44:24 +0100 | [diff] [blame] | 124 | /* DAC 0: front, DAC 1: surround, DAC 2: center/LFE, DAC 3: back */ |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 125 | static const unsigned int reg_values[3] = { |
Clemens Ladisch | c9946b2 | 2008-01-21 08:44:24 +0100 | [diff] [blame] | 126 | /* stereo -> front */ |
| 127 | (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) | |
| 128 | (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) | |
| 129 | (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) | |
| 130 | (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT), |
| 131 | /* stereo -> front+surround */ |
| 132 | (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) | |
| 133 | (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) | |
| 134 | (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) | |
| 135 | (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT), |
| 136 | /* stereo -> front+surround+back */ |
| 137 | (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) | |
| 138 | (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) | |
| 139 | (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) | |
| 140 | (0 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT), |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 141 | }; |
Clemens Ladisch | 7113e95 | 2008-01-14 08:55:03 +0100 | [diff] [blame] | 142 | u8 channels; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 143 | unsigned int reg_value; |
| 144 | |
Clemens Ladisch | 7113e95 | 2008-01-14 08:55:03 +0100 | [diff] [blame] | 145 | channels = oxygen_read8(chip, OXYGEN_PLAY_CHANNELS) & |
| 146 | OXYGEN_PLAY_CHANNELS_MASK; |
| 147 | if (channels == OXYGEN_PLAY_CHANNELS_2) |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 148 | reg_value = reg_values[chip->dac_routing]; |
Clemens Ladisch | 7113e95 | 2008-01-14 08:55:03 +0100 | [diff] [blame] | 149 | else if (channels == OXYGEN_PLAY_CHANNELS_8) |
Clemens Ladisch | c9946b2 | 2008-01-21 08:44:24 +0100 | [diff] [blame] | 150 | /* in 7.1 mode, "rear" channels go to the "back" jack */ |
| 151 | reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) | |
| 152 | (3 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) | |
| 153 | (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) | |
| 154 | (1 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 155 | else |
Clemens Ladisch | c9946b2 | 2008-01-21 08:44:24 +0100 | [diff] [blame] | 156 | reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) | |
| 157 | (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) | |
| 158 | (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) | |
| 159 | (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT); |
| 160 | oxygen_write16_masked(chip, OXYGEN_PLAY_ROUTING, reg_value, |
| 161 | OXYGEN_PLAY_DAC0_SOURCE_MASK | |
| 162 | OXYGEN_PLAY_DAC1_SOURCE_MASK | |
| 163 | OXYGEN_PLAY_DAC2_SOURCE_MASK | |
| 164 | OXYGEN_PLAY_DAC3_SOURCE_MASK); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static int upmix_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value) |
| 168 | { |
| 169 | struct oxygen *chip = ctl->private_data; |
| 170 | int changed; |
| 171 | |
| 172 | mutex_lock(&chip->mutex); |
| 173 | changed = value->value.enumerated.item[0] != chip->dac_routing; |
| 174 | if (changed) { |
| 175 | chip->dac_routing = min(value->value.enumerated.item[0], 2u); |
| 176 | spin_lock_irq(&chip->reg_lock); |
| 177 | oxygen_update_dac_routing(chip); |
| 178 | spin_unlock_irq(&chip->reg_lock); |
| 179 | } |
| 180 | mutex_unlock(&chip->mutex); |
| 181 | return changed; |
| 182 | } |
| 183 | |
| 184 | static int spdif_switch_get(struct snd_kcontrol *ctl, |
| 185 | struct snd_ctl_elem_value *value) |
| 186 | { |
| 187 | struct oxygen *chip = ctl->private_data; |
| 188 | |
| 189 | mutex_lock(&chip->mutex); |
| 190 | value->value.integer.value[0] = chip->spdif_playback_enable; |
| 191 | mutex_unlock(&chip->mutex); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static unsigned int oxygen_spdif_rate(unsigned int oxygen_rate) |
| 196 | { |
| 197 | switch (oxygen_rate) { |
| 198 | case OXYGEN_RATE_32000: |
| 199 | return IEC958_AES3_CON_FS_32000 << OXYGEN_SPDIF_CS_RATE_SHIFT; |
| 200 | case OXYGEN_RATE_44100: |
| 201 | return IEC958_AES3_CON_FS_44100 << OXYGEN_SPDIF_CS_RATE_SHIFT; |
| 202 | default: /* OXYGEN_RATE_48000 */ |
| 203 | return IEC958_AES3_CON_FS_48000 << OXYGEN_SPDIF_CS_RATE_SHIFT; |
| 204 | case OXYGEN_RATE_64000: |
| 205 | return 0xb << OXYGEN_SPDIF_CS_RATE_SHIFT; |
| 206 | case OXYGEN_RATE_88200: |
| 207 | return 0x8 << OXYGEN_SPDIF_CS_RATE_SHIFT; |
| 208 | case OXYGEN_RATE_96000: |
| 209 | return 0xa << OXYGEN_SPDIF_CS_RATE_SHIFT; |
| 210 | case OXYGEN_RATE_176400: |
| 211 | return 0xc << OXYGEN_SPDIF_CS_RATE_SHIFT; |
| 212 | case OXYGEN_RATE_192000: |
| 213 | return 0xe << OXYGEN_SPDIF_CS_RATE_SHIFT; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void oxygen_update_spdif_source(struct oxygen *chip) |
| 218 | { |
| 219 | u32 old_control, new_control; |
| 220 | u16 old_routing, new_routing; |
| 221 | unsigned int oxygen_rate; |
| 222 | |
| 223 | old_control = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL); |
| 224 | old_routing = oxygen_read16(chip, OXYGEN_PLAY_ROUTING); |
| 225 | if (chip->pcm_active & (1 << PCM_SPDIF)) { |
| 226 | new_control = old_control | OXYGEN_SPDIF_OUT_ENABLE; |
Clemens Ladisch | c9946b2 | 2008-01-21 08:44:24 +0100 | [diff] [blame] | 227 | new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK) |
| 228 | | OXYGEN_PLAY_SPDIF_SPDIF; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 229 | oxygen_rate = (old_control >> OXYGEN_SPDIF_OUT_RATE_SHIFT) |
| 230 | & OXYGEN_I2S_RATE_MASK; |
| 231 | /* S/PDIF rate was already set by the caller */ |
| 232 | } else if ((chip->pcm_active & (1 << PCM_MULTICH)) && |
| 233 | chip->spdif_playback_enable) { |
Clemens Ladisch | c9946b2 | 2008-01-21 08:44:24 +0100 | [diff] [blame] | 234 | new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK) |
| 235 | | OXYGEN_PLAY_SPDIF_MULTICH_01; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 236 | oxygen_rate = oxygen_read16(chip, OXYGEN_I2S_MULTICH_FORMAT) |
| 237 | & OXYGEN_I2S_RATE_MASK; |
| 238 | new_control = (old_control & ~OXYGEN_SPDIF_OUT_RATE_MASK) | |
| 239 | (oxygen_rate << OXYGEN_SPDIF_OUT_RATE_SHIFT) | |
| 240 | OXYGEN_SPDIF_OUT_ENABLE; |
| 241 | } else { |
| 242 | new_control = old_control & ~OXYGEN_SPDIF_OUT_ENABLE; |
| 243 | new_routing = old_routing; |
| 244 | oxygen_rate = OXYGEN_RATE_44100; |
| 245 | } |
| 246 | if (old_routing != new_routing) { |
| 247 | oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, |
| 248 | new_control & ~OXYGEN_SPDIF_OUT_ENABLE); |
| 249 | oxygen_write16(chip, OXYGEN_PLAY_ROUTING, new_routing); |
| 250 | } |
| 251 | if (new_control & OXYGEN_SPDIF_OUT_ENABLE) |
| 252 | oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS, |
| 253 | oxygen_spdif_rate(oxygen_rate) | |
| 254 | ((chip->pcm_active & (1 << PCM_SPDIF)) ? |
| 255 | chip->spdif_pcm_bits : chip->spdif_bits)); |
| 256 | oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, new_control); |
| 257 | } |
| 258 | |
| 259 | static int spdif_switch_put(struct snd_kcontrol *ctl, |
| 260 | struct snd_ctl_elem_value *value) |
| 261 | { |
| 262 | struct oxygen *chip = ctl->private_data; |
| 263 | int changed; |
| 264 | |
| 265 | mutex_lock(&chip->mutex); |
| 266 | changed = value->value.integer.value[0] != chip->spdif_playback_enable; |
| 267 | if (changed) { |
| 268 | chip->spdif_playback_enable = !!value->value.integer.value[0]; |
| 269 | spin_lock_irq(&chip->reg_lock); |
| 270 | oxygen_update_spdif_source(chip); |
| 271 | spin_unlock_irq(&chip->reg_lock); |
| 272 | } |
| 273 | mutex_unlock(&chip->mutex); |
| 274 | return changed; |
| 275 | } |
| 276 | |
| 277 | static int spdif_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info) |
| 278 | { |
| 279 | info->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 280 | info->count = 1; |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | static void oxygen_to_iec958(u32 bits, struct snd_ctl_elem_value *value) |
| 285 | { |
| 286 | value->value.iec958.status[0] = |
| 287 | bits & (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C | |
| 288 | OXYGEN_SPDIF_PREEMPHASIS); |
| 289 | value->value.iec958.status[1] = /* category and original */ |
| 290 | bits >> OXYGEN_SPDIF_CATEGORY_SHIFT; |
| 291 | } |
| 292 | |
| 293 | static u32 iec958_to_oxygen(struct snd_ctl_elem_value *value) |
| 294 | { |
| 295 | u32 bits; |
| 296 | |
| 297 | bits = value->value.iec958.status[0] & |
| 298 | (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C | |
| 299 | OXYGEN_SPDIF_PREEMPHASIS); |
| 300 | bits |= value->value.iec958.status[1] << OXYGEN_SPDIF_CATEGORY_SHIFT; |
| 301 | if (bits & OXYGEN_SPDIF_NONAUDIO) |
| 302 | bits |= OXYGEN_SPDIF_V; |
| 303 | return bits; |
| 304 | } |
| 305 | |
| 306 | static inline void write_spdif_bits(struct oxygen *chip, u32 bits) |
| 307 | { |
| 308 | oxygen_write32_masked(chip, OXYGEN_SPDIF_OUTPUT_BITS, bits, |
| 309 | OXYGEN_SPDIF_NONAUDIO | |
| 310 | OXYGEN_SPDIF_C | |
| 311 | OXYGEN_SPDIF_PREEMPHASIS | |
| 312 | OXYGEN_SPDIF_CATEGORY_MASK | |
| 313 | OXYGEN_SPDIF_ORIGINAL | |
| 314 | OXYGEN_SPDIF_V); |
| 315 | } |
| 316 | |
| 317 | static int spdif_default_get(struct snd_kcontrol *ctl, |
| 318 | struct snd_ctl_elem_value *value) |
| 319 | { |
| 320 | struct oxygen *chip = ctl->private_data; |
| 321 | |
| 322 | mutex_lock(&chip->mutex); |
| 323 | oxygen_to_iec958(chip->spdif_bits, value); |
| 324 | mutex_unlock(&chip->mutex); |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static int spdif_default_put(struct snd_kcontrol *ctl, |
| 329 | struct snd_ctl_elem_value *value) |
| 330 | { |
| 331 | struct oxygen *chip = ctl->private_data; |
| 332 | u32 new_bits; |
| 333 | int changed; |
| 334 | |
| 335 | new_bits = iec958_to_oxygen(value); |
| 336 | mutex_lock(&chip->mutex); |
| 337 | changed = new_bits != chip->spdif_bits; |
| 338 | if (changed) { |
| 339 | chip->spdif_bits = new_bits; |
| 340 | if (!(chip->pcm_active & (1 << PCM_SPDIF))) |
| 341 | write_spdif_bits(chip, new_bits); |
| 342 | } |
| 343 | mutex_unlock(&chip->mutex); |
| 344 | return changed; |
| 345 | } |
| 346 | |
| 347 | static int spdif_mask_get(struct snd_kcontrol *ctl, |
| 348 | struct snd_ctl_elem_value *value) |
| 349 | { |
| 350 | value->value.iec958.status[0] = IEC958_AES0_NONAUDIO | |
| 351 | IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS; |
| 352 | value->value.iec958.status[1] = |
| 353 | IEC958_AES1_CON_CATEGORY | IEC958_AES1_CON_ORIGINAL; |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static int spdif_pcm_get(struct snd_kcontrol *ctl, |
| 358 | struct snd_ctl_elem_value *value) |
| 359 | { |
| 360 | struct oxygen *chip = ctl->private_data; |
| 361 | |
| 362 | mutex_lock(&chip->mutex); |
| 363 | oxygen_to_iec958(chip->spdif_pcm_bits, value); |
| 364 | mutex_unlock(&chip->mutex); |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | static int spdif_pcm_put(struct snd_kcontrol *ctl, |
| 369 | struct snd_ctl_elem_value *value) |
| 370 | { |
| 371 | struct oxygen *chip = ctl->private_data; |
| 372 | u32 new_bits; |
| 373 | int changed; |
| 374 | |
| 375 | new_bits = iec958_to_oxygen(value); |
| 376 | mutex_lock(&chip->mutex); |
| 377 | changed = new_bits != chip->spdif_pcm_bits; |
| 378 | if (changed) { |
| 379 | chip->spdif_pcm_bits = new_bits; |
| 380 | if (chip->pcm_active & (1 << PCM_SPDIF)) |
| 381 | write_spdif_bits(chip, new_bits); |
| 382 | } |
| 383 | mutex_unlock(&chip->mutex); |
| 384 | return changed; |
| 385 | } |
| 386 | |
| 387 | static int spdif_input_mask_get(struct snd_kcontrol *ctl, |
| 388 | struct snd_ctl_elem_value *value) |
| 389 | { |
| 390 | value->value.iec958.status[0] = 0xff; |
| 391 | value->value.iec958.status[1] = 0xff; |
| 392 | value->value.iec958.status[2] = 0xff; |
| 393 | value->value.iec958.status[3] = 0xff; |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static int spdif_input_default_get(struct snd_kcontrol *ctl, |
| 398 | struct snd_ctl_elem_value *value) |
| 399 | { |
| 400 | struct oxygen *chip = ctl->private_data; |
| 401 | u32 bits; |
| 402 | |
| 403 | bits = oxygen_read32(chip, OXYGEN_SPDIF_INPUT_BITS); |
| 404 | value->value.iec958.status[0] = bits; |
| 405 | value->value.iec958.status[1] = bits >> 8; |
| 406 | value->value.iec958.status[2] = bits >> 16; |
| 407 | value->value.iec958.status[3] = bits >> 24; |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static int ac97_switch_get(struct snd_kcontrol *ctl, |
| 412 | struct snd_ctl_elem_value *value) |
| 413 | { |
| 414 | struct oxygen *chip = ctl->private_data; |
| 415 | unsigned int index = ctl->private_value & 0xff; |
| 416 | unsigned int bitnr = (ctl->private_value >> 8) & 0xff; |
| 417 | int invert = ctl->private_value & (1 << 16); |
| 418 | u16 reg; |
| 419 | |
| 420 | mutex_lock(&chip->mutex); |
| 421 | reg = oxygen_read_ac97(chip, 0, index); |
| 422 | mutex_unlock(&chip->mutex); |
| 423 | if (!(reg & (1 << bitnr)) ^ !invert) |
| 424 | value->value.integer.value[0] = 1; |
| 425 | else |
| 426 | value->value.integer.value[0] = 0; |
| 427 | return 0; |
| 428 | } |
| 429 | |
Clemens Ladisch | 893e44b | 2008-01-14 08:57:05 +0100 | [diff] [blame] | 430 | static void ac97_mute_ctl(struct oxygen *chip, unsigned int control) |
| 431 | { |
| 432 | unsigned int index = chip->controls[control]->private_value & 0xff; |
| 433 | u16 value; |
| 434 | |
| 435 | value = oxygen_read_ac97(chip, 0, index); |
| 436 | if (!(value & 0x8000)) { |
| 437 | oxygen_write_ac97(chip, 0, index, value | 0x8000); |
| 438 | snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 439 | &chip->controls[control]->id); |
| 440 | } |
| 441 | } |
| 442 | |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 443 | static int ac97_switch_put(struct snd_kcontrol *ctl, |
| 444 | struct snd_ctl_elem_value *value) |
| 445 | { |
| 446 | struct oxygen *chip = ctl->private_data; |
| 447 | unsigned int index = ctl->private_value & 0xff; |
| 448 | unsigned int bitnr = (ctl->private_value >> 8) & 0xff; |
| 449 | int invert = ctl->private_value & (1 << 16); |
| 450 | u16 oldreg, newreg; |
| 451 | int change; |
| 452 | |
| 453 | mutex_lock(&chip->mutex); |
| 454 | oldreg = oxygen_read_ac97(chip, 0, index); |
| 455 | newreg = oldreg; |
| 456 | if (!value->value.integer.value[0] ^ !invert) |
| 457 | newreg |= 1 << bitnr; |
| 458 | else |
| 459 | newreg &= ~(1 << bitnr); |
| 460 | change = newreg != oldreg; |
| 461 | if (change) { |
| 462 | oxygen_write_ac97(chip, 0, index, newreg); |
Clemens Ladisch | 893e44b | 2008-01-14 08:57:05 +0100 | [diff] [blame] | 463 | if (index == AC97_LINE) { |
Clemens Ladisch | 878ac3e | 2008-01-21 08:50:19 +0100 | [diff] [blame^] | 464 | oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS, |
| 465 | newreg & 0x8000 ? |
| 466 | CM9780_GPO0 : 0, CM9780_GPO0); |
Clemens Ladisch | 893e44b | 2008-01-14 08:57:05 +0100 | [diff] [blame] | 467 | if (!(newreg & 0x8000)) { |
| 468 | ac97_mute_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH); |
| 469 | ac97_mute_ctl(chip, CONTROL_CD_CAPTURE_SWITCH); |
| 470 | ac97_mute_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH); |
| 471 | } |
| 472 | } else if ((index == AC97_MIC || index == AC97_CD || |
| 473 | index == AC97_VIDEO || index == AC97_AUX) && |
| 474 | bitnr == 15 && !(newreg & 0x8000)) { |
| 475 | ac97_mute_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH); |
Clemens Ladisch | 878ac3e | 2008-01-21 08:50:19 +0100 | [diff] [blame^] | 476 | oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS, |
| 477 | CM9780_GPO0, CM9780_GPO0); |
Clemens Ladisch | 893e44b | 2008-01-14 08:57:05 +0100 | [diff] [blame] | 478 | } |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 479 | } |
| 480 | mutex_unlock(&chip->mutex); |
| 481 | return change; |
| 482 | } |
| 483 | |
| 484 | static int ac97_volume_info(struct snd_kcontrol *ctl, |
| 485 | struct snd_ctl_elem_info *info) |
| 486 | { |
| 487 | info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 488 | info->count = 2; |
| 489 | info->value.integer.min = 0; |
| 490 | info->value.integer.max = 0x1f; |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | static int ac97_volume_get(struct snd_kcontrol *ctl, |
| 495 | struct snd_ctl_elem_value *value) |
| 496 | { |
| 497 | struct oxygen *chip = ctl->private_data; |
| 498 | unsigned int index = ctl->private_value; |
| 499 | u16 reg; |
| 500 | |
| 501 | mutex_lock(&chip->mutex); |
| 502 | reg = oxygen_read_ac97(chip, 0, index); |
| 503 | mutex_unlock(&chip->mutex); |
| 504 | value->value.integer.value[0] = 31 - (reg & 0x1f); |
| 505 | value->value.integer.value[1] = 31 - ((reg >> 8) & 0x1f); |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static int ac97_volume_put(struct snd_kcontrol *ctl, |
| 510 | struct snd_ctl_elem_value *value) |
| 511 | { |
| 512 | struct oxygen *chip = ctl->private_data; |
| 513 | unsigned int index = ctl->private_value; |
| 514 | u16 oldreg, newreg; |
| 515 | int change; |
| 516 | |
| 517 | mutex_lock(&chip->mutex); |
| 518 | oldreg = oxygen_read_ac97(chip, 0, index); |
| 519 | newreg = oldreg; |
| 520 | newreg = (newreg & ~0x1f) | |
| 521 | (31 - (value->value.integer.value[0] & 0x1f)); |
| 522 | newreg = (newreg & ~0x1f00) | |
| 523 | ((31 - (value->value.integer.value[0] & 0x1f)) << 8); |
| 524 | change = newreg != oldreg; |
| 525 | if (change) |
| 526 | oxygen_write_ac97(chip, 0, index, newreg); |
| 527 | mutex_unlock(&chip->mutex); |
| 528 | return change; |
| 529 | } |
| 530 | |
| 531 | #define AC97_SWITCH(xname, index, bitnr, invert) { \ |
| 532 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ |
| 533 | .name = xname, \ |
| 534 | .info = snd_ctl_boolean_mono_info, \ |
| 535 | .get = ac97_switch_get, \ |
| 536 | .put = ac97_switch_put, \ |
| 537 | .private_value = ((invert) << 16) | ((bitnr) << 8) | (index), \ |
| 538 | } |
| 539 | #define AC97_VOLUME(xname, index) { \ |
| 540 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ |
| 541 | .name = xname, \ |
| 542 | .info = ac97_volume_info, \ |
| 543 | .get = ac97_volume_get, \ |
| 544 | .put = ac97_volume_put, \ |
| 545 | .tlv = { .p = ac97_db_scale, }, \ |
| 546 | .private_value = (index), \ |
| 547 | } |
| 548 | |
| 549 | static DECLARE_TLV_DB_SCALE(ac97_db_scale, -3450, 150, 0); |
| 550 | |
| 551 | static const struct snd_kcontrol_new controls[] = { |
| 552 | { |
| 553 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Clemens Ladisch | fb920b7 | 2008-01-15 08:39:06 +0100 | [diff] [blame] | 554 | .name = "Master Playback Volume", |
Clemens Ladisch | ccc80fb | 2008-01-16 08:32:08 +0100 | [diff] [blame] | 555 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 556 | .info = dac_volume_info, |
| 557 | .get = dac_volume_get, |
| 558 | .put = dac_volume_put, |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 559 | }, |
| 560 | { |
| 561 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Clemens Ladisch | fb920b7 | 2008-01-15 08:39:06 +0100 | [diff] [blame] | 562 | .name = "Master Playback Switch", |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 563 | .info = snd_ctl_boolean_mono_info, |
| 564 | .get = dac_mute_get, |
| 565 | .put = dac_mute_put, |
| 566 | }, |
| 567 | { |
| 568 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 569 | .name = "Stereo Upmixing", |
| 570 | .info = upmix_info, |
| 571 | .get = upmix_get, |
| 572 | .put = upmix_put, |
| 573 | }, |
| 574 | { |
| 575 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 576 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
| 577 | .info = snd_ctl_boolean_mono_info, |
| 578 | .get = spdif_switch_get, |
| 579 | .put = spdif_switch_put, |
| 580 | }, |
| 581 | { |
| 582 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 583 | .device = 1, |
| 584 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
| 585 | .info = spdif_info, |
| 586 | .get = spdif_default_get, |
| 587 | .put = spdif_default_put, |
| 588 | }, |
| 589 | { |
| 590 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 591 | .device = 1, |
| 592 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
| 593 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 594 | .info = spdif_info, |
| 595 | .get = spdif_mask_get, |
| 596 | }, |
| 597 | { |
| 598 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 599 | .device = 1, |
| 600 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM), |
| 601 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 602 | SNDRV_CTL_ELEM_ACCESS_INACTIVE, |
| 603 | .info = spdif_info, |
| 604 | .get = spdif_pcm_get, |
| 605 | .put = spdif_pcm_put, |
| 606 | }, |
| 607 | { |
| 608 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 609 | .device = 1, |
| 610 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, MASK), |
| 611 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 612 | .info = spdif_info, |
| 613 | .get = spdif_input_mask_get, |
| 614 | }, |
| 615 | { |
| 616 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 617 | .device = 1, |
| 618 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
| 619 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 620 | .info = spdif_info, |
| 621 | .get = spdif_input_default_get, |
| 622 | }, |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 623 | }; |
| 624 | |
| 625 | static const struct snd_kcontrol_new ac97_controls[] = { |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 626 | AC97_VOLUME("Mic Capture Volume", AC97_MIC), |
| 627 | AC97_SWITCH("Mic Capture Switch", AC97_MIC, 15, 1), |
| 628 | AC97_SWITCH("Mic Boost (+20dB)", AC97_MIC, 6, 0), |
| 629 | AC97_SWITCH("Line Capture Switch", AC97_LINE, 15, 1), |
| 630 | AC97_VOLUME("CD Capture Volume", AC97_CD), |
| 631 | AC97_SWITCH("CD Capture Switch", AC97_CD, 15, 1), |
| 632 | AC97_VOLUME("Aux Capture Volume", AC97_AUX), |
| 633 | AC97_SWITCH("Aux Capture Switch", AC97_AUX, 15, 1), |
| 634 | }; |
| 635 | |
| 636 | static void oxygen_any_ctl_free(struct snd_kcontrol *ctl) |
| 637 | { |
| 638 | struct oxygen *chip = ctl->private_data; |
Clemens Ladisch | 01a3aff | 2008-01-14 08:56:01 +0100 | [diff] [blame] | 639 | unsigned int i; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 640 | |
| 641 | /* I'm too lazy to write a function for each control :-) */ |
Clemens Ladisch | 01a3aff | 2008-01-14 08:56:01 +0100 | [diff] [blame] | 642 | for (i = 0; i < ARRAY_SIZE(chip->controls); ++i) |
| 643 | chip->controls[i] = NULL; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 644 | } |
| 645 | |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 646 | static int add_controls(struct oxygen *chip, |
| 647 | const struct snd_kcontrol_new controls[], |
| 648 | unsigned int count) |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 649 | { |
Clemens Ladisch | 01a3aff | 2008-01-14 08:56:01 +0100 | [diff] [blame] | 650 | static const char *const known_ctl_names[CONTROL_COUNT] = { |
| 651 | [CONTROL_SPDIF_PCM] = |
| 652 | SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM), |
| 653 | [CONTROL_SPDIF_INPUT_BITS] = |
| 654 | SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Clemens Ladisch | 893e44b | 2008-01-14 08:57:05 +0100 | [diff] [blame] | 655 | [CONTROL_MIC_CAPTURE_SWITCH] = "Mic Capture Switch", |
| 656 | [CONTROL_LINE_CAPTURE_SWITCH] = "Line Capture Switch", |
| 657 | [CONTROL_CD_CAPTURE_SWITCH] = "CD Capture Switch", |
| 658 | [CONTROL_AUX_CAPTURE_SWITCH] = "Aux Capture Switch", |
Clemens Ladisch | 01a3aff | 2008-01-14 08:56:01 +0100 | [diff] [blame] | 659 | }; |
| 660 | unsigned int i, j; |
Clemens Ladisch | ccc80fb | 2008-01-16 08:32:08 +0100 | [diff] [blame] | 661 | struct snd_kcontrol_new template; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 662 | struct snd_kcontrol *ctl; |
| 663 | int err; |
| 664 | |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 665 | for (i = 0; i < count; ++i) { |
Clemens Ladisch | ccc80fb | 2008-01-16 08:32:08 +0100 | [diff] [blame] | 666 | template = controls[i]; |
| 667 | err = chip->model->control_filter(&template); |
| 668 | if (err < 0) |
| 669 | return err; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 670 | ctl = snd_ctl_new1(&controls[i], chip); |
| 671 | if (!ctl) |
| 672 | return -ENOMEM; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 673 | err = snd_ctl_add(chip->card, ctl); |
| 674 | if (err < 0) |
| 675 | return err; |
Clemens Ladisch | 01a3aff | 2008-01-14 08:56:01 +0100 | [diff] [blame] | 676 | for (j = 0; j < CONTROL_COUNT; ++j) |
| 677 | if (!strcmp(ctl->id.name, known_ctl_names[j])) { |
| 678 | chip->controls[j] = ctl; |
| 679 | ctl->private_free = oxygen_any_ctl_free; |
| 680 | } |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 681 | } |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | int oxygen_mixer_init(struct oxygen *chip) |
| 686 | { |
| 687 | int err; |
| 688 | |
| 689 | err = add_controls(chip, controls, ARRAY_SIZE(controls)); |
| 690 | if (err < 0) |
| 691 | return err; |
| 692 | if (chip->has_ac97_0) { |
| 693 | err = add_controls(chip, ac97_controls, |
| 694 | ARRAY_SIZE(ac97_controls)); |
| 695 | if (err < 0) |
| 696 | return err; |
| 697 | } |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 698 | return chip->model->mixer_init ? chip->model->mixer_init(chip) : 0; |
| 699 | } |