1 /* 2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 3 * Universal interface for Audio Codec '97 4 * 5 * For more details look to AC '97 component specification revision 2.2 6 * by Intel Corporation (http://developer.intel.com). 7 * 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 * 23 */ 24 25 #include <linux/delay.h> 26 #include <linux/init.h> 27 #include <linux/slab.h> 28 #include <linux/pci.h> 29 #include <linux/moduleparam.h> 30 #include <linux/mutex.h> 31 #include <sound/core.h> 32 #include <sound/pcm.h> 33 #include <sound/tlv.h> 34 #include <sound/ac97_codec.h> 35 #include <sound/asoundef.h> 36 #include <sound/initval.h> 37 #include "ac97_id.h" 38 39 #include "ac97_patch.c" 40 41 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); 42 MODULE_DESCRIPTION("Universal interface for Audio Codec '97"); 43 MODULE_LICENSE("GPL"); 44 45 static int enable_loopback; 46 47 module_param(enable_loopback, bool, 0444); 48 MODULE_PARM_DESC(enable_loopback, "Enable AC97 ADC/DAC Loopback Control"); 49 50 #ifdef CONFIG_SND_AC97_POWER_SAVE 51 static int power_save = CONFIG_SND_AC97_POWER_SAVE_DEFAULT; 52 module_param(power_save, int, 0644); 53 MODULE_PARM_DESC(power_save, "Automatic power-saving timeout " 54 "(in second, 0 = disable)."); 55 #endif 56 /* 57 58 */ 59 60 struct ac97_codec_id { 61 unsigned int id; 62 unsigned int mask; 63 const char *name; 64 int (*patch)(struct snd_ac97 *ac97); 65 int (*mpatch)(struct snd_ac97 *ac97); 66 unsigned int flags; 67 }; 68 69 static const struct ac97_codec_id snd_ac97_codec_id_vendors[] = { 70 { 0x41445300, 0xffffff00, "Analog Devices", NULL, NULL }, 71 { 0x414b4d00, 0xffffff00, "Asahi Kasei", NULL, NULL }, 72 { 0x414c4300, 0xffffff00, "Realtek", NULL, NULL }, 73 { 0x414c4700, 0xffffff00, "Realtek", NULL, NULL }, 74 { 0x434d4900, 0xffffff00, "C-Media Electronics", NULL, NULL }, 75 { 0x43525900, 0xffffff00, "Cirrus Logic", NULL, NULL }, 76 { 0x43585400, 0xffffff00, "Conexant", NULL, NULL }, 77 { 0x44543000, 0xffffff00, "Diamond Technology", NULL, NULL }, 78 { 0x454d4300, 0xffffff00, "eMicro", NULL, NULL }, 79 { 0x45838300, 0xffffff00, "ESS Technology", NULL, NULL }, 80 { 0x48525300, 0xffffff00, "Intersil", NULL, NULL }, 81 { 0x49434500, 0xffffff00, "ICEnsemble", NULL, NULL }, 82 { 0x49544500, 0xffffff00, "ITE Tech.Inc", NULL, NULL }, 83 { 0x4e534300, 0xffffff00, "National Semiconductor", NULL, NULL }, 84 { 0x50534300, 0xffffff00, "Philips", NULL, NULL }, 85 { 0x53494c00, 0xffffff00, "Silicon Laboratory", NULL, NULL }, 86 { 0x53544d00, 0xffffff00, "STMicroelectronics", NULL, NULL }, 87 { 0x54524100, 0xffffff00, "TriTech", NULL, NULL }, 88 { 0x54584e00, 0xffffff00, "Texas Instruments", NULL, NULL }, 89 { 0x56494100, 0xffffff00, "VIA Technologies", NULL, NULL }, 90 { 0x57454300, 0xffffff00, "Winbond", NULL, NULL }, 91 { 0x574d4c00, 0xffffff00, "Wolfson", NULL, NULL }, 92 { 0x594d4800, 0xffffff00, "Yamaha", NULL, NULL }, 93 { 0x83847600, 0xffffff00, "SigmaTel", NULL, NULL }, 94 { 0, 0, NULL, NULL, NULL } 95 }; 96 97 static const struct ac97_codec_id snd_ac97_codec_ids[] = { 98 { 0x41445303, 0xffffffff, "AD1819", patch_ad1819, NULL }, 99 { 0x41445340, 0xffffffff, "AD1881", patch_ad1881, NULL }, 100 { 0x41445348, 0xffffffff, "AD1881A", patch_ad1881, NULL }, 101 { 0x41445360, 0xffffffff, "AD1885", patch_ad1885, NULL }, 102 { 0x41445361, 0xffffffff, "AD1886", patch_ad1886, NULL }, 103 { 0x41445362, 0xffffffff, "AD1887", patch_ad1881, NULL }, 104 { 0x41445363, 0xffffffff, "AD1886A", patch_ad1881, NULL }, 105 { 0x41445368, 0xffffffff, "AD1888", patch_ad1888, NULL }, 106 { 0x41445370, 0xffffffff, "AD1980", patch_ad1980, NULL }, 107 { 0x41445372, 0xffffffff, "AD1981A", patch_ad1981a, NULL }, 108 { 0x41445374, 0xffffffff, "AD1981B", patch_ad1981b, NULL }, 109 { 0x41445375, 0xffffffff, "AD1985", patch_ad1985, NULL }, 110 { 0x41445378, 0xffffffff, "AD1986", patch_ad1986, NULL }, 111 { 0x414b4d00, 0xffffffff, "AK4540", NULL, NULL }, 112 { 0x414b4d01, 0xffffffff, "AK4542", NULL, NULL }, 113 { 0x414b4d02, 0xffffffff, "AK4543", NULL, NULL }, 114 { 0x414b4d06, 0xffffffff, "AK4544A", NULL, NULL }, 115 { 0x414b4d07, 0xffffffff, "AK4545", NULL, NULL }, 116 { 0x414c4300, 0xffffff00, "ALC100,100P", NULL, NULL }, 117 { 0x414c4710, 0xfffffff0, "ALC200,200P", NULL, NULL }, 118 { 0x414c4721, 0xffffffff, "ALC650D", NULL, NULL }, /* already patched */ 119 { 0x414c4722, 0xffffffff, "ALC650E", NULL, NULL }, /* already patched */ 120 { 0x414c4723, 0xffffffff, "ALC650F", NULL, NULL }, /* already patched */ 121 { 0x414c4720, 0xfffffff0, "ALC650", patch_alc650, NULL }, 122 { 0x414c4730, 0xffffffff, "ALC101", NULL, NULL }, 123 { 0x414c4740, 0xfffffff0, "ALC202", NULL, NULL }, 124 { 0x414c4750, 0xfffffff0, "ALC250", NULL, NULL }, 125 { 0x414c4760, 0xfffffff0, "ALC655", patch_alc655, NULL }, 126 { 0x414c4770, 0xfffffff0, "ALC203", patch_alc203, NULL }, 127 { 0x414c4781, 0xffffffff, "ALC658D", NULL, NULL }, /* already patched */ 128 { 0x414c4780, 0xfffffff0, "ALC658", patch_alc655, NULL }, 129 { 0x414c4790, 0xfffffff0, "ALC850", patch_alc850, NULL }, 130 { 0x434d4941, 0xffffffff, "CMI9738", patch_cm9738, NULL }, 131 { 0x434d4961, 0xffffffff, "CMI9739", patch_cm9739, NULL }, 132 { 0x434d4969, 0xffffffff, "CMI9780", patch_cm9780, NULL }, 133 { 0x434d4978, 0xffffffff, "CMI9761A", patch_cm9761, NULL }, 134 { 0x434d4982, 0xffffffff, "CMI9761B", patch_cm9761, NULL }, 135 { 0x434d4983, 0xffffffff, "CMI9761A+", patch_cm9761, NULL }, 136 { 0x43525900, 0xfffffff8, "CS4297", NULL, NULL }, 137 { 0x43525910, 0xfffffff8, "CS4297A", patch_cirrus_spdif, NULL }, 138 { 0x43525920, 0xfffffff8, "CS4298", patch_cirrus_spdif, NULL }, 139 { 0x43525928, 0xfffffff8, "CS4294", NULL, NULL }, 140 { 0x43525930, 0xfffffff8, "CS4299", patch_cirrus_cs4299, NULL }, 141 { 0x43525948, 0xfffffff8, "CS4201", NULL, NULL }, 142 { 0x43525958, 0xfffffff8, "CS4205", patch_cirrus_spdif, NULL }, 143 { 0x43525960, 0xfffffff8, "CS4291", NULL, NULL }, 144 { 0x43525970, 0xfffffff8, "CS4202", NULL, NULL }, 145 { 0x43585421, 0xffffffff, "HSD11246", NULL, NULL }, // SmartMC II 146 { 0x43585428, 0xfffffff8, "Cx20468", patch_conexant, NULL }, // SmartAMC fixme: the mask might be different 147 { 0x43585430, 0xffffffff, "Cx20468-31", patch_conexant, NULL }, 148 { 0x43585431, 0xffffffff, "Cx20551", patch_cx20551, NULL }, 149 { 0x44543031, 0xfffffff0, "DT0398", NULL, NULL }, 150 { 0x454d4328, 0xffffffff, "EM28028", NULL, NULL }, // same as TR28028? 151 { 0x45838308, 0xffffffff, "ESS1988", NULL, NULL }, 152 { 0x48525300, 0xffffff00, "HMP9701", NULL, NULL }, 153 { 0x49434501, 0xffffffff, "ICE1230", NULL, NULL }, 154 { 0x49434511, 0xffffffff, "ICE1232", NULL, NULL }, // alias VIA VT1611A? 155 { 0x49434514, 0xffffffff, "ICE1232A", NULL, NULL }, 156 { 0x49434551, 0xffffffff, "VT1616", patch_vt1616, NULL }, 157 { 0x49434552, 0xffffffff, "VT1616i", patch_vt1616, NULL }, // VT1616 compatible (chipset integrated) 158 { 0x49544520, 0xffffffff, "IT2226E", NULL, NULL }, 159 { 0x49544561, 0xffffffff, "IT2646E", patch_it2646, NULL }, 160 { 0x4e534300, 0xffffffff, "LM4540,43,45,46,48", NULL, NULL }, // only guess --jk 161 { 0x4e534331, 0xffffffff, "LM4549", NULL, NULL }, 162 { 0x4e534350, 0xffffffff, "LM4550", patch_lm4550, NULL }, // volume wrap fix 163 { 0x50534304, 0xffffffff, "UCB1400", patch_ucb1400, NULL }, 164 { 0x53494c20, 0xffffffe0, "Si3036,8", mpatch_si3036, mpatch_si3036, AC97_MODEM_PATCH }, 165 { 0x53544d02, 0xffffffff, "ST7597", NULL, NULL }, 166 { 0x54524102, 0xffffffff, "TR28022", NULL, NULL }, 167 { 0x54524103, 0xffffffff, "TR28023", NULL, NULL }, 168 { 0x54524106, 0xffffffff, "TR28026", NULL, NULL }, 169 { 0x54524108, 0xffffffff, "TR28028", patch_tritech_tr28028, NULL }, // added by xin jin [07/09/99] 170 { 0x54524123, 0xffffffff, "TR28602", NULL, NULL }, // only guess --jk [TR28023 = eMicro EM28023 (new CT1297)] 171 { 0x54584e20, 0xffffffff, "TLC320AD9xC", NULL, NULL }, 172 { 0x56494161, 0xffffffff, "VIA1612A", NULL, NULL }, // modified ICE1232 with S/PDIF 173 { 0x56494170, 0xffffffff, "VIA1617A", patch_vt1617a, NULL }, // modified VT1616 with S/PDIF 174 { 0x56494182, 0xffffffff, "VIA1618", patch_vt1618, NULL }, 175 { 0x57454301, 0xffffffff, "W83971D", NULL, NULL }, 176 { 0x574d4c00, 0xffffffff, "WM9701,WM9701A", NULL, NULL }, 177 { 0x574d4C03, 0xffffffff, "WM9703,WM9707,WM9708,WM9717", patch_wolfson03, NULL}, 178 { 0x574d4C04, 0xffffffff, "WM9704M,WM9704Q", patch_wolfson04, NULL}, 179 { 0x574d4C05, 0xffffffff, "WM9705,WM9710", patch_wolfson05, NULL}, 180 { 0x574d4C09, 0xffffffff, "WM9709", NULL, NULL}, 181 { 0x574d4C12, 0xffffffff, "WM9711,WM9712,WM9715", patch_wolfson11, NULL}, 182 { 0x574d4c13, 0xffffffff, "WM9713,WM9714", patch_wolfson13, NULL, AC97_DEFAULT_POWER_OFF}, 183 { 0x594d4800, 0xffffffff, "YMF743", patch_yamaha_ymf743, NULL }, 184 { 0x594d4802, 0xffffffff, "YMF752", NULL, NULL }, 185 { 0x594d4803, 0xffffffff, "YMF753", patch_yamaha_ymf753, NULL }, 186 { 0x83847600, 0xffffffff, "STAC9700,83,84", patch_sigmatel_stac9700, NULL }, 187 { 0x83847604, 0xffffffff, "STAC9701,3,4,5", NULL, NULL }, 188 { 0x83847605, 0xffffffff, "STAC9704", NULL, NULL }, 189 { 0x83847608, 0xffffffff, "STAC9708,11", patch_sigmatel_stac9708, NULL }, 190 { 0x83847609, 0xffffffff, "STAC9721,23", patch_sigmatel_stac9721, NULL }, 191 { 0x83847644, 0xffffffff, "STAC9744", patch_sigmatel_stac9744, NULL }, 192 { 0x83847650, 0xffffffff, "STAC9750,51", NULL, NULL }, // patch? 193 { 0x83847652, 0xffffffff, "STAC9752,53", NULL, NULL }, // patch? 194 { 0x83847656, 0xffffffff, "STAC9756,57", patch_sigmatel_stac9756, NULL }, 195 { 0x83847658, 0xffffffff, "STAC9758,59", patch_sigmatel_stac9758, NULL }, 196 { 0x83847666, 0xffffffff, "STAC9766,67", NULL, NULL }, // patch? 197 { 0, 0, NULL, NULL, NULL } 198 }; 199 200 201 static void update_power_regs(struct snd_ac97 *ac97); 202 #ifdef CONFIG_SND_AC97_POWER_SAVE 203 #define ac97_is_power_save_mode(ac97) \ 204 ((ac97->scaps & AC97_SCAP_POWER_SAVE) && power_save) 205 #else 206 #define ac97_is_power_save_mode(ac97) 0 207 #endif 208 209 210 /* 211 * I/O routines 212 */ 213 214 static int snd_ac97_valid_reg(struct snd_ac97 *ac97, unsigned short reg) 215 { 216 /* filter some registers for buggy codecs */ 217 switch (ac97->id) { 218 case AC97_ID_ST_AC97_ID4: 219 if (reg == 0x08) 220 return 0; 221 /* fall through */ 222 case AC97_ID_ST7597: 223 if (reg == 0x22 || reg == 0x7a) 224 return 1; 225 /* fall through */ 226 case AC97_ID_AK4540: 227 case AC97_ID_AK4542: 228 if (reg <= 0x1c || reg == 0x20 || reg == 0x26 || reg >= 0x7c) 229 return 1; 230 return 0; 231 case AC97_ID_AD1819: /* AD1819 */ 232 case AC97_ID_AD1881: /* AD1881 */ 233 case AC97_ID_AD1881A: /* AD1881A */ 234 if (reg >= 0x3a && reg <= 0x6e) /* 0x59 */ 235 return 0; 236 return 1; 237 case AC97_ID_AD1885: /* AD1885 */ 238 case AC97_ID_AD1886: /* AD1886 */ 239 case AC97_ID_AD1886A: /* AD1886A - !!verify!! --jk */ 240 case AC97_ID_AD1887: /* AD1887 - !!verify!! --jk */ 241 if (reg == 0x5a) 242 return 1; 243 if (reg >= 0x3c && reg <= 0x6e) /* 0x59 */ 244 return 0; 245 return 1; 246 case AC97_ID_STAC9700: 247 case AC97_ID_STAC9704: 248 case AC97_ID_STAC9705: 249 case AC97_ID_STAC9708: 250 case AC97_ID_STAC9721: 251 case AC97_ID_STAC9744: 252 case AC97_ID_STAC9756: 253 if (reg <= 0x3a || reg >= 0x5a) 254 return 1; 255 return 0; 256 } 257 return 1; 258 } 259 260 /** 261 * snd_ac97_write - write a value on the given register 262 * @ac97: the ac97 instance 263 * @reg: the register to change 264 * @value: the value to set 265 * 266 * Writes a value on the given register. This will invoke the write 267 * callback directly after the register check. 268 * This function doesn't change the register cache unlike 269 * #snd_ca97_write_cache(), so use this only when you don't want to 270 * reflect the change to the suspend/resume state. 271 */ 272 void snd_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short value) 273 { 274 if (!snd_ac97_valid_reg(ac97, reg)) 275 return; 276 if ((ac97->id & 0xffffff00) == AC97_ID_ALC100) { 277 /* Fix H/W bug of ALC100/100P */ 278 if (reg == AC97_MASTER || reg == AC97_HEADPHONE) 279 ac97->bus->ops->write(ac97, AC97_RESET, 0); /* reset audio codec */ 280 } 281 ac97->bus->ops->write(ac97, reg, value); 282 } 283 284 EXPORT_SYMBOL(snd_ac97_write); 285 286 /** 287 * snd_ac97_read - read a value from the given register 288 * 289 * @ac97: the ac97 instance 290 * @reg: the register to read 291 * 292 * Reads a value from the given register. This will invoke the read 293 * callback directly after the register check. 294 * 295 * Returns the read value. 296 */ 297 unsigned short snd_ac97_read(struct snd_ac97 *ac97, unsigned short reg) 298 { 299 if (!snd_ac97_valid_reg(ac97, reg)) 300 return 0; 301 return ac97->bus->ops->read(ac97, reg); 302 } 303 304 /* read a register - return the cached value if already read */ 305 static inline unsigned short snd_ac97_read_cache(struct snd_ac97 *ac97, unsigned short reg) 306 { 307 if (! test_bit(reg, ac97->reg_accessed)) { 308 ac97->regs[reg] = ac97->bus->ops->read(ac97, reg); 309 // set_bit(reg, ac97->reg_accessed); 310 } 311 return ac97->regs[reg]; 312 } 313 314 EXPORT_SYMBOL(snd_ac97_read); 315 316 /** 317 * snd_ac97_write_cache - write a value on the given register and update the cache 318 * @ac97: the ac97 instance 319 * @reg: the register to change 320 * @value: the value to set 321 * 322 * Writes a value on the given register and updates the register 323 * cache. The cached values are used for the cached-read and the 324 * suspend/resume. 325 */ 326 void snd_ac97_write_cache(struct snd_ac97 *ac97, unsigned short reg, unsigned short value) 327 { 328 if (!snd_ac97_valid_reg(ac97, reg)) 329 return; 330 mutex_lock(&ac97->reg_mutex); 331 ac97->regs[reg] = value; 332 ac97->bus->ops->write(ac97, reg, value); 333 set_bit(reg, ac97->reg_accessed); 334 mutex_unlock(&ac97->reg_mutex); 335 } 336 337 EXPORT_SYMBOL(snd_ac97_write_cache); 338 339 /** 340 * snd_ac97_update - update the value on the given register 341 * @ac97: the ac97 instance 342 * @reg: the register to change 343 * @value: the value to set 344 * 345 * Compares the value with the register cache and updates the value 346 * only when the value is changed. 347 * 348 * Returns 1 if the value is changed, 0 if no change, or a negative 349 * code on failure. 350 */ 351 int snd_ac97_update(struct snd_ac97 *ac97, unsigned short reg, unsigned short value) 352 { 353 int change; 354 355 if (!snd_ac97_valid_reg(ac97, reg)) 356 return -EINVAL; 357 mutex_lock(&ac97->reg_mutex); 358 change = ac97->regs[reg] != value; 359 if (change) { 360 ac97->regs[reg] = value; 361 ac97->bus->ops->write(ac97, reg, value); 362 } 363 set_bit(reg, ac97->reg_accessed); 364 mutex_unlock(&ac97->reg_mutex); 365 return change; 366 } 367 368 EXPORT_SYMBOL(snd_ac97_update); 369 370 /** 371 * snd_ac97_update_bits - update the bits on the given register 372 * @ac97: the ac97 instance 373 * @reg: the register to change 374 * @mask: the bit-mask to change 375 * @value: the value to set 376 * 377 * Updates the masked-bits on the given register only when the value 378 * is changed. 379 * 380 * Returns 1 if the bits are changed, 0 if no change, or a negative 381 * code on failure. 382 */ 383 int snd_ac97_update_bits(struct snd_ac97 *ac97, unsigned short reg, unsigned short mask, unsigned short value) 384 { 385 int change; 386 387 if (!snd_ac97_valid_reg(ac97, reg)) 388 return -EINVAL; 389 mutex_lock(&ac97->reg_mutex); 390 change = snd_ac97_update_bits_nolock(ac97, reg, mask, value); 391 mutex_unlock(&ac97->reg_mutex); 392 return change; 393 } 394 395 EXPORT_SYMBOL(snd_ac97_update_bits); 396 397 /* no lock version - see snd_ac97_update_bits() */ 398 int snd_ac97_update_bits_nolock(struct snd_ac97 *ac97, unsigned short reg, 399 unsigned short mask, unsigned short value) 400 { 401 int change; 402 unsigned short old, new; 403 404 old = snd_ac97_read_cache(ac97, reg); 405 new = (old & ~mask) | (value & mask); 406 change = old != new; 407 if (change) { 408 ac97->regs[reg] = new; 409 ac97->bus->ops->write(ac97, reg, new); 410 } 411 set_bit(reg, ac97->reg_accessed); 412 return change; 413 } 414 415 static int snd_ac97_ad18xx_update_pcm_bits(struct snd_ac97 *ac97, int codec, unsigned short mask, unsigned short value) 416 { 417 int change; 418 unsigned short old, new, cfg; 419 420 mutex_lock(&ac97->page_mutex); 421 old = ac97->spec.ad18xx.pcmreg[codec]; 422 new = (old & ~mask) | (value & mask); 423 change = old != new; 424 if (change) { 425 mutex_lock(&ac97->reg_mutex); 426 cfg = snd_ac97_read_cache(ac97, AC97_AD_SERIAL_CFG); 427 ac97->spec.ad18xx.pcmreg[codec] = new; 428 /* select single codec */ 429 ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG, 430 (cfg & ~0x7000) | 431 ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]); 432 /* update PCM bits */ 433 ac97->bus->ops->write(ac97, AC97_PCM, new); 434 /* select all codecs */ 435 ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG, 436 cfg | 0x7000); 437 mutex_unlock(&ac97->reg_mutex); 438 } 439 mutex_unlock(&ac97->page_mutex); 440 return change; 441 } 442 443 /* 444 * Controls 445 */ 446 447 static int snd_ac97_info_enum_double(struct snd_kcontrol *kcontrol, 448 struct snd_ctl_elem_info *uinfo) 449 { 450 struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value; 451 452 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 453 uinfo->count = e->shift_l == e->shift_r ? 1 : 2; 454 uinfo->value.enumerated.items = e->mask; 455 456 if (uinfo->value.enumerated.item > e->mask - 1) 457 uinfo->value.enumerated.item = e->mask - 1; 458 strcpy(uinfo->value.enumerated.name, e->texts[uinfo->value.enumerated.item]); 459 return 0; 460 } 461 462 static int snd_ac97_get_enum_double(struct snd_kcontrol *kcontrol, 463 struct snd_ctl_elem_value *ucontrol) 464 { 465 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol); 466 struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value; 467 unsigned short val, bitmask; 468 469 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1) 470 ; 471 val = snd_ac97_read_cache(ac97, e->reg); 472 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1); 473 if (e->shift_l != e->shift_r) 474 ucontrol->value.enumerated.item[1] = (val >> e->shift_r) & (bitmask - 1); 475 476 return 0; 477 } 478 479 static int snd_ac97_put_enum_double(struct snd_kcontrol *kcontrol, 480 struct snd_ctl_elem_value *ucontrol) 481 { 482 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol); 483 struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value; 484 unsigned short val; 485 unsigned short mask, bitmask; 486 487 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1) 488 ; 489 if (ucontrol->value.enumerated.item[0] > e->mask - 1) 490 return -EINVAL; 491 val = ucontrol->value.enumerated.item[0] << e->shift_l; 492 mask = (bitmask - 1) << e->shift_l; 493 if (e->shift_l != e->shift_r) { 494 if (ucontrol->value.enumerated.item[1] > e->mask - 1) 495 return -EINVAL; 496 val |= ucontrol->value.enumerated.item[1] << e->shift_r; 497 mask |= (bitmask - 1) << e->shift_r; 498 } 499 return snd_ac97_update_bits(ac97, e->reg, mask, val); 500 } 501 502 /* save/restore ac97 v2.3 paging */ 503 static int snd_ac97_page_save(struct snd_ac97 *ac97, int reg, struct snd_kcontrol *kcontrol) 504 { 505 int page_save = -1; 506 if ((kcontrol->private_value & (1<<25)) && 507 (ac97->ext_id & AC97_EI_REV_MASK) >= AC97_EI_REV_23 && 508 (reg >= 0x60 && reg < 0x70)) { 509 unsigned short page = (kcontrol->private_value >> 26) & 0x0f; 510 mutex_lock(&ac97->page_mutex); /* lock paging */ 511 page_save = snd_ac97_read(ac97, AC97_INT_PAGING) & AC97_PAGE_MASK; 512 snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page); 513 } 514 return page_save; 515 } 516 517 static void snd_ac97_page_restore(struct snd_ac97 *ac97, int page_save) 518 { 519 if (page_save >= 0) { 520 snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page_save); 521 mutex_unlock(&ac97->page_mutex); /* unlock paging */ 522 } 523 } 524 525 /* volume and switch controls */ 526 static int snd_ac97_info_volsw(struct snd_kcontrol *kcontrol, 527 struct snd_ctl_elem_info *uinfo) 528 { 529 int mask = (kcontrol->private_value >> 16) & 0xff; 530 int shift = (kcontrol->private_value >> 8) & 0x0f; 531 int rshift = (kcontrol->private_value >> 12) & 0x0f; 532 533 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; 534 uinfo->count = shift == rshift ? 1 : 2; 535 uinfo->value.integer.min = 0; 536 uinfo->value.integer.max = mask; 537 return 0; 538 } 539 540 static int snd_ac97_get_volsw(struct snd_kcontrol *kcontrol, 541 struct snd_ctl_elem_value *ucontrol) 542 { 543 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol); 544 int reg = kcontrol->private_value & 0xff; 545 int shift = (kcontrol->private_value >> 8) & 0x0f; 546 int rshift = (kcontrol->private_value >> 12) & 0x0f; 547 int mask = (kcontrol->private_value >> 16) & 0xff; 548 int invert = (kcontrol->private_value >> 24) & 0x01; 549 int page_save; 550 551 page_save = snd_ac97_page_save(ac97, reg, kcontrol); 552 ucontrol->value.integer.value[0] = (snd_ac97_read_cache(ac97, reg) >> shift) & mask; 553 if (shift != rshift) 554 ucontrol->value.integer.value[1] = (snd_ac97_read_cache(ac97, reg) >> rshift) & mask; 555 if (invert) { 556 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; 557 if (shift != rshift) 558 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1]; 559 } 560 snd_ac97_page_restore(ac97, page_save); 561 return 0; 562 } 563 564 static int snd_ac97_put_volsw(struct snd_kcontrol *kcontrol, 565 struct snd_ctl_elem_value *ucontrol) 566 { 567 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol); 568 int reg = kcontrol->private_value & 0xff; 569 int shift = (kcontrol->private_value >> 8) & 0x0f; 570 int rshift = (kcontrol->private_value >> 12) & 0x0f; 571 int mask = (kcontrol->private_value >> 16) & 0xff; 572 int invert = (kcontrol->private_value >> 24) & 0x01; 573 int err, page_save; 574 unsigned short val, val2, val_mask; 575 576 page_save = snd_ac97_page_save(ac97, reg, kcontrol); 577 val = (ucontrol->value.integer.value[0] & mask); 578 if (invert) 579 val = mask - val; 580 val_mask = mask << shift; 581 val = val << shift; 582 if (shift != rshift) { 583 val2 = (ucontrol->value.integer.value[1] & mask); 584 if (invert) 585 val2 = mask - val2; 586 val_mask |= mask << rshift; 587 val |= val2 << rshift; 588 } 589 err = snd_ac97_update_bits(ac97, reg, val_mask, val); 590 snd_ac97_page_restore(ac97, page_save); 591 #ifdef CONFIG_SND_AC97_POWER_SAVE 592 /* check analog mixer power-down */ 593 if ((val_mask & 0x8000) && 594 (kcontrol->private_value & (1<<30))) { 595 if (val & 0x8000) 596 ac97->power_up &= ~(1 << (reg>>1)); 597 else 598 ac97->power_up |= 1 << (reg>>1); 599 update_power_regs(ac97); 600 } 601 #endif 602 return err; 603 } 604 605 static const struct snd_kcontrol_new snd_ac97_controls_master_mono[2] = { 606 AC97_SINGLE("Master Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1), 607 AC97_SINGLE("Master Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1) 608 }; 609 610 static const struct snd_kcontrol_new snd_ac97_controls_tone[2] = { 611 AC97_SINGLE("Tone Control - Bass", AC97_MASTER_TONE, 8, 15, 1), 612 AC97_SINGLE("Tone Control - Treble", AC97_MASTER_TONE, 0, 15, 1) 613 }; 614 615 static const struct snd_kcontrol_new snd_ac97_controls_pc_beep[2] = { 616 AC97_SINGLE("Beep Playback Switch", AC97_PC_BEEP, 15, 1, 1), 617 AC97_SINGLE("Beep Playback Volume", AC97_PC_BEEP, 1, 15, 1) 618 }; 619 620 static const struct snd_kcontrol_new snd_ac97_controls_mic_boost = 621 AC97_SINGLE("Mic Boost (+20dB)", AC97_MIC, 6, 1, 0); 622 623 624 static const char* std_rec_sel[] = {"Mic", "CD", "Video", "Aux", "Line", "Mix", "Mix Mono", "Phone"}; 625 static const char* std_3d_path[] = {"pre 3D", "post 3D"}; 626 static const char* std_mix[] = {"Mix", "Mic"}; 627 static const char* std_mic[] = {"Mic1", "Mic2"}; 628 629 static const struct ac97_enum std_enum[] = { 630 AC97_ENUM_DOUBLE(AC97_REC_SEL, 8, 0, 8, std_rec_sel), 631 AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, std_3d_path), 632 AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 9, 2, std_mix), 633 AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 8, 2, std_mic), 634 }; 635 636 static const struct snd_kcontrol_new snd_ac97_control_capture_src = 637 AC97_ENUM("Capture Source", std_enum[0]); 638 639 static const struct snd_kcontrol_new snd_ac97_control_capture_vol = 640 AC97_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0); 641 642 static const struct snd_kcontrol_new snd_ac97_controls_mic_capture[2] = { 643 AC97_SINGLE("Mic Capture Switch", AC97_REC_GAIN_MIC, 15, 1, 1), 644 AC97_SINGLE("Mic Capture Volume", AC97_REC_GAIN_MIC, 0, 15, 0) 645 }; 646 647 enum { 648 AC97_GENERAL_PCM_OUT = 0, 649 AC97_GENERAL_STEREO_ENHANCEMENT, 650 AC97_GENERAL_3D, 651 AC97_GENERAL_LOUDNESS, 652 AC97_GENERAL_MONO, 653 AC97_GENERAL_MIC, 654 AC97_GENERAL_LOOPBACK 655 }; 656 657 static const struct snd_kcontrol_new snd_ac97_controls_general[7] = { 658 AC97_ENUM("PCM Out Path & Mute", std_enum[1]), 659 AC97_SINGLE("Simulated Stereo Enhancement", AC97_GENERAL_PURPOSE, 14, 1, 0), 660 AC97_SINGLE("3D Control - Switch", AC97_GENERAL_PURPOSE, 13, 1, 0), 661 AC97_SINGLE("Loudness (bass boost)", AC97_GENERAL_PURPOSE, 12, 1, 0), 662 AC97_ENUM("Mono Output Select", std_enum[2]), 663 AC97_ENUM("Mic Select", std_enum[3]), 664 AC97_SINGLE("ADC/DAC Loopback", AC97_GENERAL_PURPOSE, 7, 1, 0) 665 }; 666 667 static const struct snd_kcontrol_new snd_ac97_controls_3d[2] = { 668 AC97_SINGLE("3D Control - Center", AC97_3D_CONTROL, 8, 15, 0), 669 AC97_SINGLE("3D Control - Depth", AC97_3D_CONTROL, 0, 15, 0) 670 }; 671 672 static const struct snd_kcontrol_new snd_ac97_controls_center[2] = { 673 AC97_SINGLE("Center Playback Switch", AC97_CENTER_LFE_MASTER, 7, 1, 1), 674 AC97_SINGLE("Center Playback Volume", AC97_CENTER_LFE_MASTER, 0, 31, 1) 675 }; 676 677 static const struct snd_kcontrol_new snd_ac97_controls_lfe[2] = { 678 AC97_SINGLE("LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 1, 1), 679 AC97_SINGLE("LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 31, 1) 680 }; 681 682 static const struct snd_kcontrol_new snd_ac97_control_eapd = 683 AC97_SINGLE("External Amplifier", AC97_POWERDOWN, 15, 1, 1); 684 685 static const struct snd_kcontrol_new snd_ac97_controls_modem_switches[2] = { 686 AC97_SINGLE("Off-hook Switch", AC97_GPIO_STATUS, 0, 1, 0), 687 AC97_SINGLE("Caller ID Switch", AC97_GPIO_STATUS, 2, 1, 0) 688 }; 689 690 /* change the existing EAPD control as inverted */ 691 static void set_inv_eapd(struct snd_ac97 *ac97, struct snd_kcontrol *kctl) 692 { 693 kctl->private_value = AC97_SINGLE_VALUE(AC97_POWERDOWN, 15, 1, 0); 694 snd_ac97_update_bits(ac97, AC97_POWERDOWN, (1<<15), (1<<15)); /* EAPD up */ 695 ac97->scaps |= AC97_SCAP_INV_EAPD; 696 } 697 698 static int snd_ac97_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 699 { 700 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; 701 uinfo->count = 1; 702 return 0; 703 } 704 705 static int snd_ac97_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 706 { 707 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | 708 IEC958_AES0_NONAUDIO | 709 IEC958_AES0_CON_EMPHASIS_5015 | 710 IEC958_AES0_CON_NOT_COPYRIGHT; 711 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | 712 IEC958_AES1_CON_ORIGINAL; 713 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS; 714 return 0; 715 } 716 717 static int snd_ac97_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 718 { 719 /* FIXME: AC'97 spec doesn't say which bits are used for what */ 720 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | 721 IEC958_AES0_NONAUDIO | 722 IEC958_AES0_PRO_FS | 723 IEC958_AES0_PRO_EMPHASIS_5015; 724 return 0; 725 } 726 727 static int snd_ac97_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 728 { 729 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol); 730 731 mutex_lock(&ac97->reg_mutex); 732 ucontrol->value.iec958.status[0] = ac97->spdif_status & 0xff; 733 ucontrol->value.iec958.status[1] = (ac97->spdif_status >> 8) & 0xff; 734 ucontrol->value.iec958.status[2] = (ac97->spdif_status >> 16) & 0xff; 735 ucontrol->value.iec958.status[3] = (ac97->spdif_status >> 24) & 0xff; 736 mutex_unlock(&ac97->reg_mutex); 737 return 0; 738 } 739 740 static int snd_ac97_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 741 { 742 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol); 743 unsigned int new = 0; 744 unsigned short val = 0; 745 int change; 746 747 new = val = ucontrol->value.iec958.status[0] & (IEC958_AES0_PROFESSIONAL|IEC958_AES0_NONAUDIO); 748 if (ucontrol->value.iec958.status[0] & IEC958_AES0_PROFESSIONAL) { 749 new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_PRO_FS|IEC958_AES0_PRO_EMPHASIS_5015); 750 switch (new & IEC958_AES0_PRO_FS) { 751 case IEC958_AES0_PRO_FS_44100: val |= 0<<12; break; 752 case IEC958_AES0_PRO_FS_48000: val |= 2<<12; break; 753 case IEC958_AES0_PRO_FS_32000: val |= 3<<12; break; 754 default: val |= 1<<12; break; 755 } 756 if ((new & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015) 757 val |= 1<<3; 758 } else { 759 new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT); 760 new |= ((ucontrol->value.iec958.status[1] & (IEC958_AES1_CON_CATEGORY|IEC958_AES1_CON_ORIGINAL)) << 8); 761 new |= ((ucontrol->value.iec958.status[3] & IEC958_AES3_CON_FS) << 24); 762 if ((new & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015) 763 val |= 1<<3; 764 if (!(new & IEC958_AES0_CON_NOT_COPYRIGHT)) 765 val |= 1<<2; 766 val |= ((new >> 8) & 0xff) << 4; // category + original 767 switch ((new >> 24) & 0xff) { 768 case IEC958_AES3_CON_FS_44100: val |= 0<<12; break; 769 case IEC958_AES3_CON_FS_48000: val |= 2<<12; break; 770 case IEC958_AES3_CON_FS_32000: val |= 3<<12; break; 771 default: val |= 1<<12; break; 772 } 773 } 774 775 mutex_lock(&ac97->reg_mutex); 776 change = ac97->spdif_status != new; 777 ac97->spdif_status = new; 778 779 if (ac97->flags & AC97_CS_SPDIF) { 780 int x = (val >> 12) & 0x03; 781 switch (x) { 782 case 0: x = 1; break; // 44.1 783 case 2: x = 0; break; // 48.0 784 default: x = 0; break; // illegal. 785 } 786 change |= snd_ac97_update_bits_nolock(ac97, AC97_CSR_SPDIF, 0x3fff, ((val & 0xcfff) | (x << 12))); 787 } else if (ac97->flags & AC97_CX_SPDIF) { 788 int v; 789 v = new & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT) ? 0 : AC97_CXR_COPYRGT; 790 v |= new & IEC958_AES0_NONAUDIO ? AC97_CXR_SPDIF_AC3 : AC97_CXR_SPDIF_PCM; 791 change |= snd_ac97_update_bits_nolock(ac97, AC97_CXR_AUDIO_MISC, 792 AC97_CXR_SPDIF_MASK | AC97_CXR_COPYRGT, 793 v); 794 } else if (ac97->id == AC97_ID_YMF743) { 795 change |= snd_ac97_update_bits_nolock(ac97, 796 AC97_YMF7X3_DIT_CTRL, 797 0xff38, 798 ((val << 4) & 0xff00) | 799 ((val << 2) & 0x0038)); 800 } else { 801 unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS); 802 snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */ 803 804 change |= snd_ac97_update_bits_nolock(ac97, AC97_SPDIF, 0x3fff, val); 805 if (extst & AC97_EA_SPDIF) { 806 snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */ 807 } 808 } 809 mutex_unlock(&ac97->reg_mutex); 810 811 return change; 812 } 813 814 static int snd_ac97_put_spsa(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 815 { 816 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol); 817 int reg = kcontrol->private_value & 0xff; 818 int shift = (kcontrol->private_value >> 8) & 0xff; 819 int mask = (kcontrol->private_value >> 16) & 0xff; 820 // int invert = (kcontrol->private_value >> 24) & 0xff; 821 unsigned short value, old, new; 822 int change; 823 824 value = (ucontrol->value.integer.value[0] & mask); 825 826 mutex_lock(&ac97->reg_mutex); 827 mask <<= shift; 828 value <<= shift; 829 old = snd_ac97_read_cache(ac97, reg); 830 new = (old & ~mask) | value; 831 change = old != new; 832 833 if (change) { 834 unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS); 835 snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */ 836 change = snd_ac97_update_bits_nolock(ac97, reg, mask, value); 837 if (extst & AC97_EA_SPDIF) 838 snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */ 839 } 840 mutex_unlock(&ac97->reg_mutex); 841 return change; 842 } 843 844 static const struct snd_kcontrol_new snd_ac97_controls_spdif[5] = { 845 { 846 .access = SNDRV_CTL_ELEM_ACCESS_READ, 847 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 848 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK), 849 .info = snd_ac97_spdif_mask_info, 850 .get = snd_ac97_spdif_cmask_get, 851 }, 852 { 853 .access = SNDRV_CTL_ELEM_ACCESS_READ, 854 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 855 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK), 856 .info = snd_ac97_spdif_mask_info, 857 .get = snd_ac97_spdif_pmask_get, 858 }, 859 { 860 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 861 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT), 862 .info = snd_ac97_spdif_mask_info, 863 .get = snd_ac97_spdif_default_get, 864 .put = snd_ac97_spdif_default_put, 865 }, 866 867 AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),AC97_EXTENDED_STATUS, 2, 1, 0), 868 { 869 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 870 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "AC97-SPSA", 871 .info = snd_ac97_info_volsw, 872 .get = snd_ac97_get_volsw, 873 .put = snd_ac97_put_spsa, 874 .private_value = AC97_SINGLE_VALUE(AC97_EXTENDED_STATUS, 4, 3, 0) 875 }, 876 }; 877 878 #define AD18XX_PCM_BITS(xname, codec, lshift, rshift, mask) \ 879 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_bits, \ 880 .get = snd_ac97_ad18xx_pcm_get_bits, .put = snd_ac97_ad18xx_pcm_put_bits, \ 881 .private_value = (codec) | ((lshift) << 8) | ((rshift) << 12) | ((mask) << 16) } 882 883 static int snd_ac97_ad18xx_pcm_info_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 884 { 885 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol); 886 int mask = (kcontrol->private_value >> 16) & 0x0f; 887 int lshift = (kcontrol->private_value >> 8) & 0x0f; 888 int rshift = (kcontrol->private_value >> 12) & 0x0f; 889 890 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; 891 if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES)) 892 uinfo->count = 2; 893 else 894 uinfo->count = 1; 895 uinfo->value.integer.min = 0; 896 uinfo->value.integer.max = mask; 897 return 0; 898 } 899 900 static int snd_ac97_ad18xx_pcm_get_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 901 { 902 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol); 903 int codec = kcontrol->private_value & 3; 904 int lshift = (kcontrol->private_value >> 8) & 0x0f; 905 int rshift = (kcontrol->private_value >> 12) & 0x0f; 906 int mask = (kcontrol->private_value >> 16) & 0xff; 907 908 ucontrol->value.integer.value[0] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> lshift) & mask); 909 if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES)) 910 ucontrol->value.integer.value[1] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> rshift) & mask); 911 return 0; 912 } 913 914 static int snd_ac97_ad18xx_pcm_put_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 915 { 916 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol); 917 int codec = kcontrol->private_value & 3; 918 int lshift = (kcontrol->private_value >> 8) & 0x0f; 919 int rshift = (kcontrol->private_value >> 12) & 0x0f; 920 int mask = (kcontrol->private_value >> 16) & 0xff; 921 unsigned short val, valmask; 922 923 val = (mask - (ucontrol->value.integer.value[0] & mask)) << lshift; 924 valmask = mask << lshift; 925 if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES)) { 926 val |= (mask - (ucontrol->value.integer.value[1] & mask)) << rshift; 927 valmask |= mask << rshift; 928 } 929 return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, valmask, val); 930 } 931 932 #define AD18XX_PCM_VOLUME(xname, codec) \ 933 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_volume, \ 934 .get = snd_ac97_ad18xx_pcm_get_volume, .put = snd_ac97_ad18xx_pcm_put_volume, \ 935 .private_value = codec } 936 937 static int snd_ac97_ad18xx_pcm_info_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 938 { 939 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 940 uinfo->count = 2; 941 uinfo->value.integer.min = 0; 942 uinfo->value.integer.max = 31; 943 return 0; 944 } 945 946 static int snd_ac97_ad18xx_pcm_get_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 947 { 948 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol); 949 int codec = kcontrol->private_value & 3; 950 951 mutex_lock(&ac97->page_mutex); 952 ucontrol->value.integer.value[0] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 0) & 31); 953 ucontrol->value.integer.value[1] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 8) & 31); 954 mutex_unlock(&ac97->page_mutex); 955 return 0; 956 } 957 958 static int snd_ac97_ad18xx_pcm_put_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 959 { 960 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol); 961 int codec = kcontrol->private_value & 3; 962 unsigned short val1, val2; 963 964 val1 = 31 - (ucontrol->value.integer.value[0] & 31); 965 val2 = 31 - (ucontrol->value.integer.value[1] & 31); 966 return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, 0x1f1f, (val1 << 8) | val2); 967 } 968 969 static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_pcm[2] = { 970 AD18XX_PCM_BITS("PCM Playback Switch", 0, 15, 7, 1), 971 AD18XX_PCM_VOLUME("PCM Playback Volume", 0) 972 }; 973 974 static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_surround[2] = { 975 AD18XX_PCM_BITS("Surround Playback Switch", 1, 15, 7, 1), 976 AD18XX_PCM_VOLUME("Surround Playback Volume", 1) 977 }; 978 979 static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_center[2] = { 980 AD18XX_PCM_BITS("Center Playback Switch", 2, 15, 15, 1), 981 AD18XX_PCM_BITS("Center Playback Volume", 2, 8, 8, 31) 982 }; 983 984 static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_lfe[2] = { 985 AD18XX_PCM_BITS("LFE Playback Switch", 2, 7, 7, 1), 986 AD18XX_PCM_BITS("LFE Playback Volume", 2, 0, 0, 31) 987 }; 988 989 /* 990 * 991 */ 992 993 static void snd_ac97_powerdown(struct snd_ac97 *ac97); 994 995 static int snd_ac97_bus_free(struct snd_ac97_bus *bus) 996 { 997 if (bus) { 998 snd_ac97_bus_proc_done(bus); 999 kfree(bus->pcms); 1000 if (bus->private_free) 1001 bus->private_free(bus); 1002 kfree(bus); 1003 } 1004 return 0; 1005 } 1006 1007 static int snd_ac97_bus_dev_free(struct snd_device *device) 1008 { 1009 struct snd_ac97_bus *bus = device->device_data; 1010 return snd_ac97_bus_free(bus); 1011 } 1012 1013 static int snd_ac97_free(struct snd_ac97 *ac97) 1014 { 1015 if (ac97) { 1016 #ifdef CONFIG_SND_AC97_POWER_SAVE 1017 cancel_delayed_work_sync(&ac97->power_work); 1018 #endif 1019 snd_ac97_proc_done(ac97); 1020 if (ac97->bus) 1021 ac97->bus->codec[ac97->num] = NULL; 1022 if (ac97->private_free) 1023 ac97->private_free(ac97); 1024 kfree(ac97); 1025 } 1026 return 0; 1027 } 1028 1029 static int snd_ac97_dev_free(struct snd_device *device) 1030 { 1031 struct snd_ac97 *ac97 = device->device_data; 1032 snd_ac97_powerdown(ac97); /* for avoiding click noises during shut down */ 1033 return snd_ac97_free(ac97); 1034 } 1035 1036 static int snd_ac97_try_volume_mix(struct snd_ac97 * ac97, int reg) 1037 { 1038 unsigned short val, mask = 0x8000; 1039 1040 if (! snd_ac97_valid_reg(ac97, reg)) 1041 return 0; 1042 1043 switch (reg) { 1044 case AC97_MASTER_TONE: 1045 return ac97->caps & 0x04 ? 1 : 0; 1046 case AC97_HEADPHONE: 1047 return ac97->caps & 0x10 ? 1 : 0; 1048 case AC97_REC_GAIN_MIC: 1049 return ac97->caps & 0x01 ? 1 : 0; 1050 case AC97_3D_CONTROL: 1051 if (ac97->caps & 0x7c00) { 1052 val = snd_ac97_read(ac97, reg); 1053 /* if nonzero - fixed and we can't set it */ 1054 return val == 0; 1055 } 1056 return 0; 1057 case AC97_CENTER_LFE_MASTER: /* center */ 1058 if ((ac97->ext_id & AC97_EI_CDAC) == 0) 1059 return 0; 1060 break; 1061 case AC97_CENTER_LFE_MASTER+1: /* lfe */ 1062 if ((ac97->ext_id & AC97_EI_LDAC) == 0) 1063 return 0; 1064 reg = AC97_CENTER_LFE_MASTER; 1065 mask = 0x0080; 1066 break; 1067 case AC97_SURROUND_MASTER: 1068 if ((ac97->ext_id & AC97_EI_SDAC) == 0) 1069 return 0; 1070 break; 1071 } 1072 1073 val = snd_ac97_read(ac97, reg); 1074 if (!(val & mask)) { 1075 /* nothing seems to be here - mute flag is not set */ 1076 /* try another test */ 1077 snd_ac97_write_cache(ac97, reg, val | mask); 1078 val = snd_ac97_read(ac97, reg); 1079 val = snd_ac97_read(ac97, reg); 1080 if (!(val & mask)) 1081 return 0; /* nothing here */ 1082 } 1083 return 1; /* success, useable */ 1084 } 1085 1086 static void check_volume_resolution(struct snd_ac97 *ac97, int reg, unsigned char *lo_max, unsigned char *hi_max) 1087 { 1088 unsigned short cbit[3] = { 0x20, 0x10, 0x01 }; 1089 unsigned char max[3] = { 63, 31, 15 }; 1090 int i; 1091 1092 /* first look up the static resolution table */ 1093 if (ac97->res_table) { 1094 const struct snd_ac97_res_table *tbl; 1095 for (tbl = ac97->res_table; tbl->reg; tbl++) { 1096 if (tbl->reg == reg) { 1097 *lo_max = tbl->bits & 0xff; 1098 *hi_max = (tbl->bits >> 8) & 0xff; 1099 return; 1100 } 1101 } 1102 } 1103 1104 *lo_max = *hi_max = 0; 1105 for (i = 0 ; i < ARRAY_SIZE(cbit); i++) { 1106 unsigned short val; 1107 snd_ac97_write(ac97, reg, 0x8080 | cbit[i] | (cbit[i] << 8)); 1108 /* Do the read twice due to buffers on some ac97 codecs. 1109 * e.g. The STAC9704 returns exactly what you wrote to the register 1110 * if you read it immediately. This causes the detect routine to fail. 1111 */ 1112 val = snd_ac97_read(ac97, reg); 1113 val = snd_ac97_read(ac97, reg); 1114 if (! *lo_max && (val & 0x7f) == cbit[i]) 1115 *lo_max = max[i]; 1116 if (! *hi_max && ((val >> 8) & 0x7f) == cbit[i]) 1117 *hi_max = max[i]; 1118 if (*lo_max && *hi_max) 1119 break; 1120 } 1121 } 1122 1123 static int snd_ac97_try_bit(struct snd_ac97 * ac97, int reg, int bit) 1124 { 1125 unsigned short mask, val, orig, res; 1126 1127 mask = 1 << bit; 1128 orig = snd_ac97_read(ac97, reg); 1129 val = orig ^ mask; 1130 snd_ac97_write(ac97, reg, val); 1131 res = snd_ac97_read(ac97, reg); 1132 snd_ac97_write_cache(ac97, reg, orig); 1133 return res == val; 1134 } 1135 1136 /* check the volume resolution of center/lfe */ 1137 static void snd_ac97_change_volume_params2(struct snd_ac97 * ac97, int reg, int shift, unsigned char *max) 1138 { 1139 unsigned short val, val1; 1140 1141 *max = 63; 1142 val = 0x8080 | (0x20 << shift); 1143 snd_ac97_write(ac97, reg, val); 1144 val1 = snd_ac97_read(ac97, reg); 1145 if (val != val1) { 1146 *max = 31; 1147 } 1148 /* reset volume to zero */ 1149 snd_ac97_write_cache(ac97, reg, 0x8080); 1150 } 1151 1152 static inline int printable(unsigned int x) 1153 { 1154 x &= 0xff; 1155 if (x < ' ' || x >= 0x71) { 1156 if (x <= 0x89) 1157 return x - 0x71 + 'A'; 1158 return '?'; 1159 } 1160 return x; 1161 } 1162 1163 static struct snd_kcontrol *snd_ac97_cnew(const struct snd_kcontrol_new *_template, 1164 struct snd_ac97 * ac97) 1165 { 1166 struct snd_kcontrol_new template; 1167 memcpy(&template, _template, sizeof(template)); 1168 template.index = ac97->num; 1169 return snd_ctl_new1(&template, ac97); 1170 } 1171 1172 /* 1173 * create mute switch(es) for normal stereo controls 1174 */ 1175 static int snd_ac97_cmute_new_stereo(struct snd_card *card, char *name, int reg, 1176 int check_stereo, int check_amix, 1177 struct snd_ac97 *ac97) 1178 { 1179 struct snd_kcontrol *kctl; 1180 int err; 1181 unsigned short val, val1, mute_mask; 1182 1183 if (! snd_ac97_valid_reg(ac97, reg)) 1184 return 0; 1185 1186 mute_mask = 0x8000; 1187 val = snd_ac97_read(ac97, reg); 1188 if (check_stereo || (ac97->flags & AC97_STEREO_MUTES)) { 1189 /* check whether both mute bits work */ 1190 val1 = val | 0x8080; 1191 snd_ac97_write(ac97, reg, val1); 1192 if (val1 == snd_ac97_read(ac97, reg)) 1193 mute_mask = 0x8080; 1194 } 1195 if (mute_mask == 0x8080) { 1196 struct snd_kcontrol_new tmp = AC97_DOUBLE(name, reg, 15, 7, 1, 1); 1197 if (check_amix) 1198 tmp.private_value |= (1 << 30); 1199 tmp.index = ac97->num; 1200 kctl = snd_ctl_new1(&tmp, ac97); 1201 } else { 1202 struct snd_kcontrol_new tmp = AC97_SINGLE(name, reg, 15, 1, 1); 1203 if (check_amix) 1204 tmp.private_value |= (1 << 30); 1205 tmp.index = ac97->num; 1206 kctl = snd_ctl_new1(&tmp, ac97); 1207 } 1208 err = snd_ctl_add(card, kctl); 1209 if (err < 0) 1210 return err; 1211 /* mute as default */ 1212 snd_ac97_write_cache(ac97, reg, val | mute_mask); 1213 return 0; 1214 } 1215 1216 /* 1217 * set dB information 1218 */ 1219 static const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0); 1220 static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0); 1221 static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0); 1222 static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0); 1223 static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0); 1224 1225 static const unsigned int *find_db_scale(unsigned int maxval) 1226 { 1227 switch (maxval) { 1228 case 0x0f: return db_scale_4bit; 1229 case 0x1f: return db_scale_5bit; 1230 case 0x3f: return db_scale_6bit; 1231 } 1232 return NULL; 1233 } 1234 1235 static void set_tlv_db_scale(struct snd_kcontrol *kctl, const unsigned int *tlv) 1236 { 1237 kctl->tlv.p = tlv; 1238 if (tlv) 1239 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ; 1240 } 1241 1242 /* 1243 * create a volume for normal stereo/mono controls 1244 */ 1245 static int snd_ac97_cvol_new(struct snd_card *card, char *name, int reg, unsigned int lo_max, 1246 unsigned int hi_max, struct snd_ac97 *ac97) 1247 { 1248 int err; 1249 struct snd_kcontrol *kctl; 1250 1251 if (! snd_ac97_valid_reg(ac97, reg)) 1252 return 0; 1253 if (hi_max) { 1254 /* invert */ 1255 struct snd_kcontrol_new tmp = AC97_DOUBLE(name, reg, 8, 0, lo_max, 1); 1256 tmp.index = ac97->num; 1257 kctl = snd_ctl_new1(&tmp, ac97); 1258 } else { 1259 /* invert */ 1260 struct snd_kcontrol_new tmp = AC97_SINGLE(name, reg, 0, lo_max, 1); 1261 tmp.index = ac97->num; 1262 kctl = snd_ctl_new1(&tmp, ac97); 1263 } 1264 if (reg >= AC97_PHONE && reg <= AC97_PCM) 1265 set_tlv_db_scale(kctl, db_scale_5bit_12db_max); 1266 else 1267 set_tlv_db_scale(kctl, find_db_scale(lo_max)); 1268 err = snd_ctl_add(card, kctl); 1269 if (err < 0) 1270 return err; 1271 snd_ac97_write_cache(ac97, reg, 1272 (snd_ac97_read(ac97, reg) & 0x8080) | 1273 lo_max | (hi_max << 8)); 1274 return 0; 1275 } 1276 1277 /* 1278 * create a mute-switch and a volume for normal stereo/mono controls 1279 */ 1280 static int snd_ac97_cmix_new_stereo(struct snd_card *card, const char *pfx, 1281 int reg, int check_stereo, int check_amix, 1282 struct snd_ac97 *ac97) 1283 { 1284 int err; 1285 char name[44]; 1286 unsigned char lo_max, hi_max; 1287 1288 if (! snd_ac97_valid_reg(ac97, reg)) 1289 return 0; 1290 1291 if (snd_ac97_try_bit(ac97, reg, 15)) { 1292 sprintf(name, "%s Switch", pfx); 1293 if ((err = snd_ac97_cmute_new_stereo(card, name, reg, 1294 check_stereo, check_amix, 1295 ac97)) < 0) 1296 return err; 1297 } 1298 check_volume_resolution(ac97, reg, &lo_max, &hi_max); 1299 if (lo_max) { 1300 sprintf(name, "%s Volume", pfx); 1301 if ((err = snd_ac97_cvol_new(card, name, reg, lo_max, hi_max, ac97)) < 0) 1302 return err; 1303 } 1304 return 0; 1305 } 1306 1307 #define snd_ac97_cmix_new(card, pfx, reg, acheck, ac97) \ 1308 snd_ac97_cmix_new_stereo(card, pfx, reg, 0, acheck, ac97) 1309 #define snd_ac97_cmute_new(card, name, reg, acheck, ac97) \ 1310 snd_ac97_cmute_new_stereo(card, name, reg, 0, acheck, ac97) 1311 1312 static unsigned int snd_ac97_determine_spdif_rates(struct snd_ac97 *ac97); 1313 1314 static int snd_ac97_mixer_build(struct snd_ac97 * ac97) 1315 { 1316 struct snd_card *card = ac97->bus->card; 1317 struct snd_kcontrol *kctl; 1318 int err; 1319 unsigned int idx; 1320 unsigned char max; 1321 1322 /* build master controls */ 1323 /* AD claims to remove this control from AD1887, although spec v2.2 does not allow this */ 1324 if (snd_ac97_try_volume_mix(ac97, AC97_MASTER)) { 1325 if (ac97->flags & AC97_HAS_NO_MASTER_VOL) 1326 err = snd_ac97_cmute_new(card, "Master Playback Switch", 1327 AC97_MASTER, 0, ac97); 1328 else 1329 err = snd_ac97_cmix_new(card, "Master Playback", 1330 AC97_MASTER, 0, ac97); 1331 if (err < 0) 1332 return err; 1333 } 1334 1335 ac97->regs[AC97_CENTER_LFE_MASTER] = 0x8080; 1336 1337 /* build center controls */ 1338 if ((snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER)) 1339 && !(ac97->flags & AC97_AD_MULTI)) { 1340 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_center[0], ac97))) < 0) 1341 return err; 1342 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_center[1], ac97))) < 0) 1343 return err; 1344 snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 0, &max); 1345 kctl->private_value &= ~(0xff << 16); 1346 kctl->private_value |= (int)max << 16; 1347 set_tlv_db_scale(kctl, find_db_scale(max)); 1348 snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max); 1349 } 1350 1351 /* build LFE controls */ 1352 if ((snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER+1)) 1353 && !(ac97->flags & AC97_AD_MULTI)) { 1354 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_lfe[0], ac97))) < 0) 1355 return err; 1356 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_lfe[1], ac97))) < 0) 1357 return err; 1358 snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 8, &max); 1359 kctl->private_value &= ~(0xff << 16); 1360 kctl->private_value |= (int)max << 16; 1361 set_tlv_db_scale(kctl, find_db_scale(max)); 1362 snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max << 8); 1363 } 1364 1365 /* build surround controls */ 1366 if ((snd_ac97_try_volume_mix(ac97, AC97_SURROUND_MASTER)) 1367 && !(ac97->flags & AC97_AD_MULTI)) { 1368 /* Surround Master (0x38) is with stereo mutes */ 1369 if ((err = snd_ac97_cmix_new_stereo(card, "Surround Playback", 1370 AC97_SURROUND_MASTER, 1, 0, 1371 ac97)) < 0) 1372 return err; 1373 } 1374 1375 /* build headphone controls */ 1376 if (snd_ac97_try_volume_mix(ac97, AC97_HEADPHONE)) { 1377 if ((err = snd_ac97_cmix_new(card, "Headphone Playback", 1378 AC97_HEADPHONE, 0, ac97)) < 0) 1379 return err; 1380 } 1381 1382 /* build master mono controls */ 1383 if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_MONO)) { 1384 if ((err = snd_ac97_cmix_new(card, "Master Mono Playback", 1385 AC97_MASTER_MONO, 0, ac97)) < 0) 1386 return err; 1387 } 1388 1389 /* build master tone controls */ 1390 if (!(ac97->flags & AC97_HAS_NO_TONE)) { 1391 if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_TONE)) { 1392 for (idx = 0; idx < 2; idx++) { 1393 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_tone[idx], ac97))) < 0) 1394 return err; 1395 if (ac97->id == AC97_ID_YMF743 || 1396 ac97->id == AC97_ID_YMF753) { 1397 kctl->private_value &= ~(0xff << 16); 1398 kctl->private_value |= 7 << 16; 1399 } 1400 } 1401 snd_ac97_write_cache(ac97, AC97_MASTER_TONE, 0x0f0f); 1402 } 1403 } 1404 1405 /* build Beep controls */ 1406 if (!(ac97->flags & AC97_HAS_NO_PC_BEEP) && 1407 ((ac97->flags & AC97_HAS_PC_BEEP) || 1408 snd_ac97_try_volume_mix(ac97, AC97_PC_BEEP))) { 1409 for (idx = 0; idx < 2; idx++) 1410 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_pc_beep[idx], ac97))) < 0) 1411 return err; 1412 set_tlv_db_scale(kctl, db_scale_4bit); 1413 snd_ac97_write_cache(ac97, AC97_PC_BEEP, 1414 snd_ac97_read(ac97, AC97_PC_BEEP) | 0x801e); 1415 } 1416 1417 /* build Phone controls */ 1418 if (!(ac97->flags & AC97_HAS_NO_PHONE)) { 1419 if (snd_ac97_try_volume_mix(ac97, AC97_PHONE)) { 1420 if ((err = snd_ac97_cmix_new(card, "Phone Playback", 1421 AC97_PHONE, 1, ac97)) < 0) 1422 return err; 1423 } 1424 } 1425 1426 /* build MIC controls */ 1427 if (!(ac97->flags & AC97_HAS_NO_MIC)) { 1428 if (snd_ac97_try_volume_mix(ac97, AC97_MIC)) { 1429 if ((err = snd_ac97_cmix_new(card, "Mic Playback", 1430 AC97_MIC, 1, ac97)) < 0) 1431 return err; 1432 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_mic_boost, ac97))) < 0) 1433 return err; 1434 } 1435 } 1436 1437 /* build Line controls */ 1438 if (snd_ac97_try_volume_mix(ac97, AC97_LINE)) { 1439 if ((err = snd_ac97_cmix_new(card, "Line Playback", 1440 AC97_LINE, 1, ac97)) < 0) 1441 return err; 1442 } 1443 1444 /* build CD controls */ 1445 if (!(ac97->flags & AC97_HAS_NO_CD)) { 1446 if (snd_ac97_try_volume_mix(ac97, AC97_CD)) { 1447 if ((err = snd_ac97_cmix_new(card, "CD Playback", 1448 AC97_CD, 1, ac97)) < 0) 1449 return err; 1450 } 1451 } 1452 1453 /* build Video controls */ 1454 if (!(ac97->flags & AC97_HAS_NO_VIDEO)) { 1455 if (snd_ac97_try_volume_mix(ac97, AC97_VIDEO)) { 1456 if ((err = snd_ac97_cmix_new(card, "Video Playback", 1457 AC97_VIDEO, 1, ac97)) < 0) 1458 return err; 1459 } 1460 } 1461 1462 /* build Aux controls */ 1463 if (!(ac97->flags & AC97_HAS_NO_AUX)) { 1464 if (snd_ac97_try_volume_mix(ac97, AC97_AUX)) { 1465 if ((err = snd_ac97_cmix_new(card, "Aux Playback", 1466 AC97_AUX, 1, ac97)) < 0) 1467 return err; 1468 } 1469 } 1470 1471 /* build PCM controls */ 1472 if (ac97->flags & AC97_AD_MULTI) { 1473 unsigned short init_val; 1474 if (ac97->flags & AC97_STEREO_MUTES) 1475 init_val = 0x9f9f; 1476 else 1477 init_val = 0x9f1f; 1478 for (idx = 0; idx < 2; idx++) 1479 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_pcm[idx], ac97))) < 0) 1480 return err; 1481 set_tlv_db_scale(kctl, db_scale_5bit); 1482 ac97->spec.ad18xx.pcmreg[0] = init_val; 1483 if (ac97->scaps & AC97_SCAP_SURROUND_DAC) { 1484 for (idx = 0; idx < 2; idx++) 1485 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_surround[idx], ac97))) < 0) 1486 return err; 1487 set_tlv_db_scale(kctl, db_scale_5bit); 1488 ac97->spec.ad18xx.pcmreg[1] = init_val; 1489 } 1490 if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC) { 1491 for (idx = 0; idx < 2; idx++) 1492 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_center[idx], ac97))) < 0) 1493 return err; 1494 set_tlv_db_scale(kctl, db_scale_5bit); 1495 for (idx = 0; idx < 2; idx++) 1496 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_lfe[idx], ac97))) < 0) 1497 return err; 1498 set_tlv_db_scale(kctl, db_scale_5bit); 1499 ac97->spec.ad18xx.pcmreg[2] = init_val; 1500 } 1501 snd_ac97_write_cache(ac97, AC97_PCM, init_val); 1502 } else { 1503 if (!(ac97->flags & AC97_HAS_NO_STD_PCM)) { 1504 if (ac97->flags & AC97_HAS_NO_PCM_VOL) 1505 err = snd_ac97_cmute_new(card, 1506 "PCM Playback Switch", 1507 AC97_PCM, 0, ac97); 1508 else 1509 err = snd_ac97_cmix_new(card, "PCM Playback", 1510 AC97_PCM, 0, ac97); 1511 if (err < 0) 1512 return err; 1513 } 1514 } 1515 1516 /* build Capture controls */ 1517 if (!(ac97->flags & AC97_HAS_NO_REC_GAIN)) { 1518 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_control_capture_src, ac97))) < 0) 1519 return err; 1520 if (snd_ac97_try_bit(ac97, AC97_REC_GAIN, 15)) { 1521 err = snd_ac97_cmute_new(card, "Capture Switch", 1522 AC97_REC_GAIN, 0, ac97); 1523 if (err < 0) 1524 return err; 1525 } 1526 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_control_capture_vol, ac97))) < 0) 1527 return err; 1528 set_tlv_db_scale(kctl, db_scale_rec_gain); 1529 snd_ac97_write_cache(ac97, AC97_REC_SEL, 0x0000); 1530 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x0000); 1531 } 1532 /* build MIC Capture controls */ 1533 if (snd_ac97_try_volume_mix(ac97, AC97_REC_GAIN_MIC)) { 1534 for (idx = 0; idx < 2; idx++) 1535 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_mic_capture[idx], ac97))) < 0) 1536 return err; 1537 set_tlv_db_scale(kctl, db_scale_rec_gain); 1538 snd_ac97_write_cache(ac97, AC97_REC_GAIN_MIC, 0x0000); 1539 } 1540 1541 /* build PCM out path & mute control */ 1542 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 15)) { 1543 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_PCM_OUT], ac97))) < 0) 1544 return err; 1545 } 1546 1547 /* build Simulated Stereo Enhancement control */ 1548 if (ac97->caps & 0x0008) { 1549 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_STEREO_ENHANCEMENT], ac97))) < 0) 1550 return err; 1551 } 1552 1553 /* build 3D Stereo Enhancement control */ 1554 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 13)) { 1555 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_3D], ac97))) < 0) 1556 return err; 1557 } 1558 1559 /* build Loudness control */ 1560 if (ac97->caps & 0x0020) { 1561 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOUDNESS], ac97))) < 0) 1562 return err; 1563 } 1564 1565 /* build Mono output select control */ 1566 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 9)) { 1567 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MONO], ac97))) < 0) 1568 return err; 1569 } 1570 1571 /* build Mic select control */ 1572 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 8)) { 1573 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MIC], ac97))) < 0) 1574 return err; 1575 } 1576 1577 /* build ADC/DAC loopback control */ 1578 if (enable_loopback && snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 7)) { 1579 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOOPBACK], ac97))) < 0) 1580 return err; 1581 } 1582 1583 snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, ~AC97_GP_DRSS_MASK, 0x0000); 1584 1585 /* build 3D controls */ 1586 if (ac97->build_ops->build_3d) { 1587 ac97->build_ops->build_3d(ac97); 1588 } else { 1589 if (snd_ac97_try_volume_mix(ac97, AC97_3D_CONTROL)) { 1590 unsigned short val; 1591 val = 0x0707; 1592 snd_ac97_write(ac97, AC97_3D_CONTROL, val); 1593 val = snd_ac97_read(ac97, AC97_3D_CONTROL); 1594 val = val == 0x0606; 1595 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0) 1596 return err; 1597 if (val) 1598 kctl->private_value = AC97_3D_CONTROL | (9 << 8) | (7 << 16); 1599 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[1], ac97))) < 0) 1600 return err; 1601 if (val) 1602 kctl->private_value = AC97_3D_CONTROL | (1 << 8) | (7 << 16); 1603 snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000); 1604 } 1605 } 1606 1607 /* build S/PDIF controls */ 1608 1609 /* Hack for ASUS P5P800-VM, which does not indicate S/PDIF capability */ 1610 if (ac97->subsystem_vendor == 0x1043 && 1611 ac97->subsystem_device == 0x810f) 1612 ac97->ext_id |= AC97_EI_SPDIF; 1613 1614 if ((ac97->ext_id & AC97_EI_SPDIF) && !(ac97->scaps & AC97_SCAP_NO_SPDIF)) { 1615 if (ac97->build_ops->build_spdif) { 1616 if ((err = ac97->build_ops->build_spdif(ac97)) < 0) 1617 return err; 1618 } else { 1619 for (idx = 0; idx < 5; idx++) 1620 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_spdif[idx], ac97))) < 0) 1621 return err; 1622 if (ac97->build_ops->build_post_spdif) { 1623 if ((err = ac97->build_ops->build_post_spdif(ac97)) < 0) 1624 return err; 1625 } 1626 /* set default PCM S/PDIF params */ 1627 /* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */ 1628 snd_ac97_write_cache(ac97, AC97_SPDIF, 0x2a20); 1629 ac97->rates[AC97_RATES_SPDIF] = snd_ac97_determine_spdif_rates(ac97); 1630 } 1631 ac97->spdif_status = SNDRV_PCM_DEFAULT_CON_SPDIF; 1632 } 1633 1634 /* build chip specific controls */ 1635 if (ac97->build_ops->build_specific) 1636 if ((err = ac97->build_ops->build_specific(ac97)) < 0) 1637 return err; 1638 1639 if (snd_ac97_try_bit(ac97, AC97_POWERDOWN, 15)) { 1640 kctl = snd_ac97_cnew(&snd_ac97_control_eapd, ac97); 1641 if (! kctl) 1642 return -ENOMEM; 1643 if (ac97->scaps & AC97_SCAP_INV_EAPD) 1644 set_inv_eapd(ac97, kctl); 1645 if ((err = snd_ctl_add(card, kctl)) < 0) 1646 return err; 1647 } 1648 1649 return 0; 1650 } 1651 1652 static int snd_ac97_modem_build(struct snd_card *card, struct snd_ac97 * ac97) 1653 { 1654 int err, idx; 1655 1656 /* 1657 printk(KERN_DEBUG "AC97_GPIO_CFG = %x\n", 1658 snd_ac97_read(ac97,AC97_GPIO_CFG)); 1659 */ 1660 snd_ac97_write(ac97, AC97_GPIO_CFG, 0xffff & ~(AC97_GPIO_LINE1_OH)); 1661 snd_ac97_write(ac97, AC97_GPIO_POLARITY, 0xffff & ~(AC97_GPIO_LINE1_OH)); 1662 snd_ac97_write(ac97, AC97_GPIO_STICKY, 0xffff); 1663 snd_ac97_write(ac97, AC97_GPIO_WAKEUP, 0x0); 1664 snd_ac97_write(ac97, AC97_MISC_AFE, 0x0); 1665 1666 /* build modem switches */ 1667 for (idx = 0; idx < ARRAY_SIZE(snd_ac97_controls_modem_switches); idx++) 1668 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ac97_controls_modem_switches[idx], ac97))) < 0) 1669 return err; 1670 1671 /* build chip specific controls */ 1672 if (ac97->build_ops->build_specific) 1673 if ((err = ac97->build_ops->build_specific(ac97)) < 0) 1674 return err; 1675 1676 return 0; 1677 } 1678 1679 static int snd_ac97_test_rate(struct snd_ac97 *ac97, int reg, int shadow_reg, int rate) 1680 { 1681 unsigned short val; 1682 unsigned int tmp; 1683 1684 tmp = ((unsigned int)rate * ac97->bus->clock) / 48000; 1685 snd_ac97_write_cache(ac97, reg, tmp & 0xffff); 1686 if (shadow_reg) 1687 snd_ac97_write_cache(ac97, shadow_reg, tmp & 0xffff); 1688 val = snd_ac97_read(ac97, reg); 1689 return val == (tmp & 0xffff); 1690 } 1691 1692 static void snd_ac97_determine_rates(struct snd_ac97 *ac97, int reg, int shadow_reg, unsigned int *r_result) 1693 { 1694 unsigned int result = 0; 1695 unsigned short saved; 1696 1697 if (ac97->bus->no_vra) { 1698 *r_result = SNDRV_PCM_RATE_48000; 1699 if ((ac97->flags & AC97_DOUBLE_RATE) && 1700 reg == AC97_PCM_FRONT_DAC_RATE) 1701 *r_result |= SNDRV_PCM_RATE_96000; 1702 return; 1703 } 1704 1705 saved = snd_ac97_read(ac97, reg); 1706 if ((ac97->ext_id & AC97_EI_DRA) && reg == AC97_PCM_FRONT_DAC_RATE) 1707 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, 1708 AC97_EA_DRA, 0); 1709 /* test a non-standard rate */ 1710 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11000)) 1711 result |= SNDRV_PCM_RATE_CONTINUOUS; 1712 /* let's try to obtain standard rates */ 1713 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 8000)) 1714 result |= SNDRV_PCM_RATE_8000; 1715 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11025)) 1716 result |= SNDRV_PCM_RATE_11025; 1717 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 16000)) 1718 result |= SNDRV_PCM_RATE_16000; 1719 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 22050)) 1720 result |= SNDRV_PCM_RATE_22050; 1721 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 32000)) 1722 result |= SNDRV_PCM_RATE_32000; 1723 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 44100)) 1724 result |= SNDRV_PCM_RATE_44100; 1725 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 48000)) 1726 result |= SNDRV_PCM_RATE_48000; 1727 if ((ac97->flags & AC97_DOUBLE_RATE) && 1728 reg == AC97_PCM_FRONT_DAC_RATE) { 1729 /* test standard double rates */ 1730 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, 1731 AC97_EA_DRA, AC97_EA_DRA); 1732 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 64000 / 2)) 1733 result |= SNDRV_PCM_RATE_64000; 1734 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 88200 / 2)) 1735 result |= SNDRV_PCM_RATE_88200; 1736 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 96000 / 2)) 1737 result |= SNDRV_PCM_RATE_96000; 1738 /* some codecs don't support variable double rates */ 1739 if (!snd_ac97_test_rate(ac97, reg, shadow_reg, 76100 / 2)) 1740 result &= ~SNDRV_PCM_RATE_CONTINUOUS; 1741 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, 1742 AC97_EA_DRA, 0); 1743 } 1744 /* restore the default value */ 1745 snd_ac97_write_cache(ac97, reg, saved); 1746 if (shadow_reg) 1747 snd_ac97_write_cache(ac97, shadow_reg, saved); 1748 *r_result = result; 1749 } 1750 1751 /* check AC97_SPDIF register to accept which sample rates */ 1752 static unsigned int snd_ac97_determine_spdif_rates(struct snd_ac97 *ac97) 1753 { 1754 unsigned int result = 0; 1755 int i; 1756 static unsigned short ctl_bits[] = { 1757 AC97_SC_SPSR_44K, AC97_SC_SPSR_32K, AC97_SC_SPSR_48K 1758 }; 1759 static unsigned int rate_bits[] = { 1760 SNDRV_PCM_RATE_44100, SNDRV_PCM_RATE_32000, SNDRV_PCM_RATE_48000 1761 }; 1762 1763 for (i = 0; i < (int)ARRAY_SIZE(ctl_bits); i++) { 1764 snd_ac97_update_bits(ac97, AC97_SPDIF, AC97_SC_SPSR_MASK, ctl_bits[i]); 1765 if ((snd_ac97_read(ac97, AC97_SPDIF) & AC97_SC_SPSR_MASK) == ctl_bits[i]) 1766 result |= rate_bits[i]; 1767 } 1768 return result; 1769 } 1770 1771 /* look for the codec id table matching with the given id */ 1772 static const struct ac97_codec_id *look_for_codec_id(const struct ac97_codec_id *table, 1773 unsigned int id) 1774 { 1775 const struct ac97_codec_id *pid; 1776 1777 for (pid = table; pid->id; pid++) 1778 if (pid->id == (id & pid->mask)) 1779 return pid; 1780 return NULL; 1781 } 1782 1783 void snd_ac97_get_name(struct snd_ac97 *ac97, unsigned int id, char *name, int modem) 1784 { 1785 const struct ac97_codec_id *pid; 1786 1787 sprintf(name, "0x%x %c%c%c", id, 1788 printable(id >> 24), 1789 printable(id >> 16), 1790 printable(id >> 8)); 1791 pid = look_for_codec_id(snd_ac97_codec_id_vendors, id); 1792 if (! pid) 1793 return; 1794 1795 strcpy(name, pid->name); 1796 if (ac97 && pid->patch) { 1797 if ((modem && (pid->flags & AC97_MODEM_PATCH)) || 1798 (! modem && ! (pid->flags & AC97_MODEM_PATCH))) 1799 pid->patch(ac97); 1800 } 1801 1802 pid = look_for_codec_id(snd_ac97_codec_ids, id); 1803 if (pid) { 1804 strcat(name, " "); 1805 strcat(name, pid->name); 1806 if (pid->mask != 0xffffffff) 1807 sprintf(name + strlen(name), " rev %d", id & ~pid->mask); 1808 if (ac97 && pid->patch) { 1809 if ((modem && (pid->flags & AC97_MODEM_PATCH)) || 1810 (! modem && ! (pid->flags & AC97_MODEM_PATCH))) 1811 pid->patch(ac97); 1812 } 1813 } else 1814 sprintf(name + strlen(name), " id %x", id & 0xff); 1815 } 1816 1817 /** 1818 * snd_ac97_get_short_name - retrieve codec name 1819 * @ac97: the codec instance 1820 * 1821 * Returns the short identifying name of the codec. 1822 */ 1823 const char *snd_ac97_get_short_name(struct snd_ac97 *ac97) 1824 { 1825 const struct ac97_codec_id *pid; 1826 1827 for (pid = snd_ac97_codec_ids; pid->id; pid++) 1828 if (pid->id == (ac97->id & pid->mask)) 1829 return pid->name; 1830 return "unknown codec"; 1831 } 1832 1833 EXPORT_SYMBOL(snd_ac97_get_short_name); 1834 1835 /* wait for a while until registers are accessible after RESET 1836 * return 0 if ok, negative not ready 1837 */ 1838 static int ac97_reset_wait(struct snd_ac97 *ac97, int timeout, int with_modem) 1839 { 1840 unsigned long end_time; 1841 unsigned short val; 1842 1843 end_time = jiffies + timeout; 1844 do { 1845 1846 /* use preliminary reads to settle the communication */ 1847 snd_ac97_read(ac97, AC97_RESET); 1848 snd_ac97_read(ac97, AC97_VENDOR_ID1); 1849 snd_ac97_read(ac97, AC97_VENDOR_ID2); 1850 /* modem? */ 1851 if (with_modem) { 1852 val = snd_ac97_read(ac97, AC97_EXTENDED_MID); 1853 if (val != 0xffff && (val & 1) != 0) 1854 return 0; 1855 } 1856 if (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) { 1857 /* probably only Xbox issue - all registers are read as zero */ 1858 val = snd_ac97_read(ac97, AC97_VENDOR_ID1); 1859 if (val != 0 && val != 0xffff) 1860 return 0; 1861 } else { 1862 /* because the PCM or MASTER volume registers can be modified, 1863 * the REC_GAIN register is used for tests 1864 */ 1865 /* test if we can write to the record gain volume register */ 1866 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a05); 1867 if ((snd_ac97_read(ac97, AC97_REC_GAIN) & 0x7fff) == 0x0a05) 1868 return 0; 1869 } 1870 schedule_timeout_uninterruptible(1); 1871 } while (time_after_eq(end_time, jiffies)); 1872 return -ENODEV; 1873 } 1874 1875 /** 1876 * snd_ac97_bus - create an AC97 bus component 1877 * @card: the card instance 1878 * @num: the bus number 1879 * @ops: the bus callbacks table 1880 * @private_data: private data pointer for the new instance 1881 * @rbus: the pointer to store the new AC97 bus instance. 1882 * 1883 * Creates an AC97 bus component. An struct snd_ac97_bus instance is newly 1884 * allocated and initialized. 1885 * 1886 * The ops table must include valid callbacks (at least read and 1887 * write). The other callbacks, wait and reset, are not mandatory. 1888 * 1889 * The clock is set to 48000. If another clock is needed, set 1890 * (*rbus)->clock manually. 1891 * 1892 * The AC97 bus instance is registered as a low-level device, so you don't 1893 * have to release it manually. 1894 * 1895 * Returns zero if successful, or a negative error code on failure. 1896 */ 1897 int snd_ac97_bus(struct snd_card *card, int num, struct snd_ac97_bus_ops *ops, 1898 void *private_data, struct snd_ac97_bus **rbus) 1899 { 1900 int err; 1901 struct snd_ac97_bus *bus; 1902 static struct snd_device_ops dev_ops = { 1903 .dev_free = snd_ac97_bus_dev_free, 1904 }; 1905 1906 if (snd_BUG_ON(!card)) 1907 return -EINVAL; 1908 bus = kzalloc(sizeof(*bus), GFP_KERNEL); 1909 if (bus == NULL) 1910 return -ENOMEM; 1911 bus->card = card; 1912 bus->num = num; 1913 bus->ops = ops; 1914 bus->private_data = private_data; 1915 bus->clock = 48000; 1916 spin_lock_init(&bus->bus_lock); 1917 snd_ac97_bus_proc_init(bus); 1918 if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) { 1919 snd_ac97_bus_free(bus); 1920 return err; 1921 } 1922 if (rbus) 1923 *rbus = bus; 1924 return 0; 1925 } 1926 1927 EXPORT_SYMBOL(snd_ac97_bus); 1928 1929 /* stop no dev release warning */ 1930 static void ac97_device_release(struct device * dev) 1931 { 1932 } 1933 1934 /* register ac97 codec to bus */ 1935 static int snd_ac97_dev_register(struct snd_device *device) 1936 { 1937 struct snd_ac97 *ac97 = device->device_data; 1938 int err; 1939 1940 ac97->dev.bus = &ac97_bus_type; 1941 ac97->dev.parent = ac97->bus->card->dev; 1942 ac97->dev.release = ac97_device_release; 1943 dev_set_name(&ac97->dev, "%d-%d:%s", 1944 ac97->bus->card->number, ac97->num, 1945 snd_ac97_get_short_name(ac97)); 1946 if ((err = device_register(&ac97->dev)) < 0) { 1947 snd_printk(KERN_ERR "Can't register ac97 bus\n"); 1948 ac97->dev.bus = NULL; 1949 return err; 1950 } 1951 return 0; 1952 } 1953 1954 /* disconnect ac97 codec */ 1955 static int snd_ac97_dev_disconnect(struct snd_device *device) 1956 { 1957 struct snd_ac97 *ac97 = device->device_data; 1958 if (ac97->dev.bus) 1959 device_unregister(&ac97->dev); 1960 return 0; 1961 } 1962 1963 /* build_ops to do nothing */ 1964 static const struct snd_ac97_build_ops null_build_ops; 1965 1966 #ifdef CONFIG_SND_AC97_POWER_SAVE 1967 static void do_update_power(struct work_struct *work) 1968 { 1969 update_power_regs( 1970 container_of(work, struct snd_ac97, power_work.work)); 1971 } 1972 #endif 1973 1974 /** 1975 * snd_ac97_mixer - create an Codec97 component 1976 * @bus: the AC97 bus which codec is attached to 1977 * @template: the template of ac97, including index, callbacks and 1978 * the private data. 1979 * @rac97: the pointer to store the new ac97 instance. 1980 * 1981 * Creates an Codec97 component. An struct snd_ac97 instance is newly 1982 * allocated and initialized from the template. The codec 1983 * is then initialized by the standard procedure. 1984 * 1985 * The template must include the codec number (num) and address (addr), 1986 * and the private data (private_data). 1987 * 1988 * The ac97 instance is registered as a low-level device, so you don't 1989 * have to release it manually. 1990 * 1991 * Returns zero if successful, or a negative error code on failure. 1992 */ 1993 int snd_ac97_mixer(struct snd_ac97_bus *bus, struct snd_ac97_template *template, struct snd_ac97 **rac97) 1994 { 1995 int err; 1996 struct snd_ac97 *ac97; 1997 struct snd_card *card; 1998 char name[64]; 1999 unsigned long end_time; 2000 unsigned int reg; 2001 const struct ac97_codec_id *pid; 2002 static struct snd_device_ops ops = { 2003 .dev_free = snd_ac97_dev_free, 2004 .dev_register = snd_ac97_dev_register, 2005 .dev_disconnect = snd_ac97_dev_disconnect, 2006 }; 2007 2008 if (rac97) 2009 *rac97 = NULL; 2010 if (snd_BUG_ON(!bus || !template)) 2011 return -EINVAL; 2012 if (snd_BUG_ON(template->num >= 4)) 2013 return -EINVAL; 2014 if (bus->codec[template->num]) 2015 return -EBUSY; 2016 2017 card = bus->card; 2018 ac97 = kzalloc(sizeof(*ac97), GFP_KERNEL); 2019 if (ac97 == NULL) 2020 return -ENOMEM; 2021 ac97->private_data = template->private_data; 2022 ac97->private_free = template->private_free; 2023 ac97->bus = bus; 2024 ac97->pci = template->pci; 2025 ac97->num = template->num; 2026 ac97->addr = template->addr; 2027 ac97->scaps = template->scaps; 2028 ac97->res_table = template->res_table; 2029 bus->codec[ac97->num] = ac97; 2030 mutex_init(&ac97->reg_mutex); 2031 mutex_init(&ac97->page_mutex); 2032 #ifdef CONFIG_SND_AC97_POWER_SAVE 2033 INIT_DELAYED_WORK(&ac97->power_work, do_update_power); 2034 #endif 2035 2036 #ifdef CONFIG_PCI 2037 if (ac97->pci) { 2038 pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_VENDOR_ID, &ac97->subsystem_vendor); 2039 pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_ID, &ac97->subsystem_device); 2040 } 2041 #endif 2042 if (bus->ops->reset) { 2043 bus->ops->reset(ac97); 2044 goto __access_ok; 2045 } 2046 2047 ac97->id = snd_ac97_read(ac97, AC97_VENDOR_ID1) << 16; 2048 ac97->id |= snd_ac97_read(ac97, AC97_VENDOR_ID2); 2049 if (ac97->id && ac97->id != (unsigned int)-1) { 2050 pid = look_for_codec_id(snd_ac97_codec_ids, ac97->id); 2051 if (pid && (pid->flags & AC97_DEFAULT_POWER_OFF)) 2052 goto __access_ok; 2053 } 2054 2055 /* reset to defaults */ 2056 if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO)) 2057 snd_ac97_write(ac97, AC97_RESET, 0); 2058 if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM)) 2059 snd_ac97_write(ac97, AC97_EXTENDED_MID, 0); 2060 if (bus->ops->wait) 2061 bus->ops->wait(ac97); 2062 else { 2063 udelay(50); 2064 if (ac97->scaps & AC97_SCAP_SKIP_AUDIO) 2065 err = ac97_reset_wait(ac97, msecs_to_jiffies(500), 1); 2066 else { 2067 err = ac97_reset_wait(ac97, msecs_to_jiffies(500), 0); 2068 if (err < 0) 2069 err = ac97_reset_wait(ac97, 2070 msecs_to_jiffies(500), 1); 2071 } 2072 if (err < 0) { 2073 snd_printk(KERN_WARNING "AC'97 %d does not respond - RESET\n", ac97->num); 2074 /* proceed anyway - it's often non-critical */ 2075 } 2076 } 2077 __access_ok: 2078 ac97->id = snd_ac97_read(ac97, AC97_VENDOR_ID1) << 16; 2079 ac97->id |= snd_ac97_read(ac97, AC97_VENDOR_ID2); 2080 if (! (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) && 2081 (ac97->id == 0x00000000 || ac97->id == 0xffffffff)) { 2082 snd_printk(KERN_ERR "AC'97 %d access is not valid [0x%x], removing mixer.\n", ac97->num, ac97->id); 2083 snd_ac97_free(ac97); 2084 return -EIO; 2085 } 2086 pid = look_for_codec_id(snd_ac97_codec_ids, ac97->id); 2087 if (pid) 2088 ac97->flags |= pid->flags; 2089 2090 /* test for AC'97 */ 2091 if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO) && !(ac97->scaps & AC97_SCAP_AUDIO)) { 2092 /* test if we can write to the record gain volume register */ 2093 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a06); 2094 if (((err = snd_ac97_read(ac97, AC97_REC_GAIN)) & 0x7fff) == 0x0a06) 2095 ac97->scaps |= AC97_SCAP_AUDIO; 2096 } 2097 if (ac97->scaps & AC97_SCAP_AUDIO) { 2098 ac97->caps = snd_ac97_read(ac97, AC97_RESET); 2099 ac97->ext_id = snd_ac97_read(ac97, AC97_EXTENDED_ID); 2100 if (ac97->ext_id == 0xffff) /* invalid combination */ 2101 ac97->ext_id = 0; 2102 } 2103 2104 /* test for MC'97 */ 2105 if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM) && !(ac97->scaps & AC97_SCAP_MODEM)) { 2106 ac97->ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID); 2107 if (ac97->ext_mid == 0xffff) /* invalid combination */ 2108 ac97->ext_mid = 0; 2109 if (ac97->ext_mid & 1) 2110 ac97->scaps |= AC97_SCAP_MODEM; 2111 } 2112 2113 if (!ac97_is_audio(ac97) && !ac97_is_modem(ac97)) { 2114 if (!(ac97->scaps & (AC97_SCAP_SKIP_AUDIO|AC97_SCAP_SKIP_MODEM))) 2115 snd_printk(KERN_ERR "AC'97 %d access error (not audio or modem codec)\n", ac97->num); 2116 snd_ac97_free(ac97); 2117 return -EACCES; 2118 } 2119 2120 if (bus->ops->reset) // FIXME: always skipping? 2121 goto __ready_ok; 2122 2123 /* FIXME: add powerdown control */ 2124 if (ac97_is_audio(ac97)) { 2125 /* nothing should be in powerdown mode */ 2126 snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0); 2127 if (! (ac97->flags & AC97_DEFAULT_POWER_OFF)) { 2128 snd_ac97_write_cache(ac97, AC97_RESET, 0); /* reset to defaults */ 2129 udelay(100); 2130 snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0); 2131 } 2132 /* nothing should be in powerdown mode */ 2133 snd_ac97_write_cache(ac97, AC97_GENERAL_PURPOSE, 0); 2134 end_time = jiffies + msecs_to_jiffies(5000); 2135 do { 2136 if ((snd_ac97_read(ac97, AC97_POWERDOWN) & 0x0f) == 0x0f) 2137 goto __ready_ok; 2138 schedule_timeout_uninterruptible(1); 2139 } while (time_after_eq(end_time, jiffies)); 2140 snd_printk(KERN_WARNING "AC'97 %d analog subsections not ready\n", ac97->num); 2141 } 2142 2143 /* FIXME: add powerdown control */ 2144 if (ac97_is_modem(ac97)) { 2145 unsigned char tmp; 2146 2147 /* nothing should be in powerdown mode */ 2148 /* note: it's important to set the rate at first */ 2149 tmp = AC97_MEA_GPIO; 2150 if (ac97->ext_mid & AC97_MEI_LINE1) { 2151 snd_ac97_write_cache(ac97, AC97_LINE1_RATE, 8000); 2152 tmp |= AC97_MEA_ADC1 | AC97_MEA_DAC1; 2153 } 2154 if (ac97->ext_mid & AC97_MEI_LINE2) { 2155 snd_ac97_write_cache(ac97, AC97_LINE2_RATE, 8000); 2156 tmp |= AC97_MEA_ADC2 | AC97_MEA_DAC2; 2157 } 2158 if (ac97->ext_mid & AC97_MEI_HANDSET) { 2159 snd_ac97_write_cache(ac97, AC97_HANDSET_RATE, 8000); 2160 tmp |= AC97_MEA_HADC | AC97_MEA_HDAC; 2161 } 2162 snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0); 2163 udelay(100); 2164 /* nothing should be in powerdown mode */ 2165 snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0); 2166 end_time = jiffies + msecs_to_jiffies(100); 2167 do { 2168 if ((snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS) & tmp) == tmp) 2169 goto __ready_ok; 2170 schedule_timeout_uninterruptible(1); 2171 } while (time_after_eq(end_time, jiffies)); 2172 snd_printk(KERN_WARNING "MC'97 %d converters and GPIO not ready (0x%x)\n", ac97->num, snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS)); 2173 } 2174 2175 __ready_ok: 2176 if (ac97_is_audio(ac97)) 2177 ac97->addr = (ac97->ext_id & AC97_EI_ADDR_MASK) >> AC97_EI_ADDR_SHIFT; 2178 else 2179 ac97->addr = (ac97->ext_mid & AC97_MEI_ADDR_MASK) >> AC97_MEI_ADDR_SHIFT; 2180 if (ac97->ext_id & 0x01c9) { /* L/R, MIC, SDAC, LDAC VRA support */ 2181 reg = snd_ac97_read(ac97, AC97_EXTENDED_STATUS); 2182 reg |= ac97->ext_id & 0x01c0; /* LDAC/SDAC/CDAC */ 2183 if (! bus->no_vra) 2184 reg |= ac97->ext_id & 0x0009; /* VRA/VRM */ 2185 snd_ac97_write_cache(ac97, AC97_EXTENDED_STATUS, reg); 2186 } 2187 if ((ac97->ext_id & AC97_EI_DRA) && bus->dra) { 2188 /* Intel controllers require double rate data to be put in 2189 * slots 7+8, so let's hope the codec supports it. */ 2190 snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, AC97_GP_DRSS_MASK, AC97_GP_DRSS_78); 2191 if ((snd_ac97_read(ac97, AC97_GENERAL_PURPOSE) & AC97_GP_DRSS_MASK) == AC97_GP_DRSS_78) 2192 ac97->flags |= AC97_DOUBLE_RATE; 2193 /* restore to slots 10/11 to avoid the confliction with surrounds */ 2194 snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, AC97_GP_DRSS_MASK, 0); 2195 } 2196 if (ac97->ext_id & AC97_EI_VRA) { /* VRA support */ 2197 snd_ac97_determine_rates(ac97, AC97_PCM_FRONT_DAC_RATE, 0, &ac97->rates[AC97_RATES_FRONT_DAC]); 2198 snd_ac97_determine_rates(ac97, AC97_PCM_LR_ADC_RATE, 0, &ac97->rates[AC97_RATES_ADC]); 2199 } else { 2200 ac97->rates[AC97_RATES_FRONT_DAC] = SNDRV_PCM_RATE_48000; 2201 if (ac97->flags & AC97_DOUBLE_RATE) 2202 ac97->rates[AC97_RATES_FRONT_DAC] |= SNDRV_PCM_RATE_96000; 2203 ac97->rates[AC97_RATES_ADC] = SNDRV_PCM_RATE_48000; 2204 } 2205 if (ac97->ext_id & AC97_EI_SPDIF) { 2206 /* codec specific code (patch) should override these values */ 2207 ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_32000; 2208 } 2209 if (ac97->ext_id & AC97_EI_VRM) { /* MIC VRA support */ 2210 snd_ac97_determine_rates(ac97, AC97_PCM_MIC_ADC_RATE, 0, &ac97->rates[AC97_RATES_MIC_ADC]); 2211 } else { 2212 ac97->rates[AC97_RATES_MIC_ADC] = SNDRV_PCM_RATE_48000; 2213 } 2214 if (ac97->ext_id & AC97_EI_SDAC) { /* SDAC support */ 2215 snd_ac97_determine_rates(ac97, AC97_PCM_SURR_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_SURR_DAC]); 2216 ac97->scaps |= AC97_SCAP_SURROUND_DAC; 2217 } 2218 if (ac97->ext_id & AC97_EI_LDAC) { /* LDAC support */ 2219 snd_ac97_determine_rates(ac97, AC97_PCM_LFE_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_LFE_DAC]); 2220 ac97->scaps |= AC97_SCAP_CENTER_LFE_DAC; 2221 } 2222 /* additional initializations */ 2223 if (bus->ops->init) 2224 bus->ops->init(ac97); 2225 snd_ac97_get_name(ac97, ac97->id, name, !ac97_is_audio(ac97)); 2226 snd_ac97_get_name(NULL, ac97->id, name, !ac97_is_audio(ac97)); // ac97->id might be changed in the special setup code 2227 if (! ac97->build_ops) 2228 ac97->build_ops = &null_build_ops; 2229 2230 if (ac97_is_audio(ac97)) { 2231 char comp[16]; 2232 if (card->mixername[0] == '\0') { 2233 strcpy(card->mixername, name); 2234 } else { 2235 if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) { 2236 strcat(card->mixername, ","); 2237 strcat(card->mixername, name); 2238 } 2239 } 2240 sprintf(comp, "AC97a:%08x", ac97->id); 2241 if ((err = snd_component_add(card, comp)) < 0) { 2242 snd_ac97_free(ac97); 2243 return err; 2244 } 2245 if (snd_ac97_mixer_build(ac97) < 0) { 2246 snd_ac97_free(ac97); 2247 return -ENOMEM; 2248 } 2249 } 2250 if (ac97_is_modem(ac97)) { 2251 char comp[16]; 2252 if (card->mixername[0] == '\0') { 2253 strcpy(card->mixername, name); 2254 } else { 2255 if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) { 2256 strcat(card->mixername, ","); 2257 strcat(card->mixername, name); 2258 } 2259 } 2260 sprintf(comp, "AC97m:%08x", ac97->id); 2261 if ((err = snd_component_add(card, comp)) < 0) { 2262 snd_ac97_free(ac97); 2263 return err; 2264 } 2265 if (snd_ac97_modem_build(card, ac97) < 0) { 2266 snd_ac97_free(ac97); 2267 return -ENOMEM; 2268 } 2269 } 2270 if (ac97_is_audio(ac97)) 2271 update_power_regs(ac97); 2272 snd_ac97_proc_init(ac97); 2273 if ((err = snd_device_new(card, SNDRV_DEV_CODEC, ac97, &ops)) < 0) { 2274 snd_ac97_free(ac97); 2275 return err; 2276 } 2277 *rac97 = ac97; 2278 return 0; 2279 } 2280 2281 EXPORT_SYMBOL(snd_ac97_mixer); 2282 2283 /* 2284 * Power down the chip. 2285 * 2286 * MASTER and HEADPHONE registers are muted but the register cache values 2287 * are not changed, so that the values can be restored in snd_ac97_resume(). 2288 */ 2289 static void snd_ac97_powerdown(struct snd_ac97 *ac97) 2290 { 2291 unsigned short power; 2292 2293 if (ac97_is_audio(ac97)) { 2294 /* some codecs have stereo mute bits */ 2295 snd_ac97_write(ac97, AC97_MASTER, 0x9f9f); 2296 snd_ac97_write(ac97, AC97_HEADPHONE, 0x9f9f); 2297 } 2298 2299 /* surround, CLFE, mic powerdown */ 2300 power = ac97->regs[AC97_EXTENDED_STATUS]; 2301 if (ac97->scaps & AC97_SCAP_SURROUND_DAC) 2302 power |= AC97_EA_PRJ; 2303 if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC) 2304 power |= AC97_EA_PRI | AC97_EA_PRK; 2305 power |= AC97_EA_PRL; 2306 snd_ac97_write(ac97, AC97_EXTENDED_STATUS, power); 2307 2308 /* powerdown external amplifier */ 2309 if (ac97->scaps & AC97_SCAP_INV_EAPD) 2310 power = ac97->regs[AC97_POWERDOWN] & ~AC97_PD_EAPD; 2311 else if (! (ac97->scaps & AC97_SCAP_EAPD_LED)) 2312 power = ac97->regs[AC97_POWERDOWN] | AC97_PD_EAPD; 2313 power |= AC97_PD_PR6; /* Headphone amplifier powerdown */ 2314 power |= AC97_PD_PR0 | AC97_PD_PR1; /* ADC & DAC powerdown */ 2315 snd_ac97_write(ac97, AC97_POWERDOWN, power); 2316 udelay(100); 2317 power |= AC97_PD_PR2; /* Analog Mixer powerdown (Vref on) */ 2318 snd_ac97_write(ac97, AC97_POWERDOWN, power); 2319 if (ac97_is_power_save_mode(ac97)) { 2320 power |= AC97_PD_PR3; /* Analog Mixer powerdown */ 2321 snd_ac97_write(ac97, AC97_POWERDOWN, power); 2322 udelay(100); 2323 /* AC-link powerdown, internal Clk disable */ 2324 /* FIXME: this may cause click noises on some boards */ 2325 power |= AC97_PD_PR4 | AC97_PD_PR5; 2326 snd_ac97_write(ac97, AC97_POWERDOWN, power); 2327 } 2328 } 2329 2330 2331 struct ac97_power_reg { 2332 unsigned short reg; 2333 unsigned short power_reg; 2334 unsigned short mask; 2335 }; 2336 2337 enum { PWIDX_ADC, PWIDX_FRONT, PWIDX_CLFE, PWIDX_SURR, PWIDX_MIC, PWIDX_SIZE }; 2338 2339 static struct ac97_power_reg power_regs[PWIDX_SIZE] = { 2340 [PWIDX_ADC] = { AC97_PCM_LR_ADC_RATE, AC97_POWERDOWN, AC97_PD_PR0}, 2341 [PWIDX_FRONT] = { AC97_PCM_FRONT_DAC_RATE, AC97_POWERDOWN, AC97_PD_PR1}, 2342 [PWIDX_CLFE] = { AC97_PCM_LFE_DAC_RATE, AC97_EXTENDED_STATUS, 2343 AC97_EA_PRI | AC97_EA_PRK}, 2344 [PWIDX_SURR] = { AC97_PCM_SURR_DAC_RATE, AC97_EXTENDED_STATUS, 2345 AC97_EA_PRJ}, 2346 [PWIDX_MIC] = { AC97_PCM_MIC_ADC_RATE, AC97_EXTENDED_STATUS, 2347 AC97_EA_PRL}, 2348 }; 2349 2350 #ifdef CONFIG_SND_AC97_POWER_SAVE 2351 /** 2352 * snd_ac97_update_power - update the powerdown register 2353 * @ac97: the codec instance 2354 * @reg: the rate register, e.g. AC97_PCM_FRONT_DAC_RATE 2355 * @powerup: non-zero when power up the part 2356 * 2357 * Update the AC97 powerdown register bits of the given part. 2358 */ 2359 int snd_ac97_update_power(struct snd_ac97 *ac97, int reg, int powerup) 2360 { 2361 int i; 2362 2363 if (! ac97) 2364 return 0; 2365 2366 if (reg) { 2367 /* SPDIF requires DAC power, too */ 2368 if (reg == AC97_SPDIF) 2369 reg = AC97_PCM_FRONT_DAC_RATE; 2370 for (i = 0; i < PWIDX_SIZE; i++) { 2371 if (power_regs[i].reg == reg) { 2372 if (powerup) 2373 ac97->power_up |= (1 << i); 2374 else 2375 ac97->power_up &= ~(1 << i); 2376 break; 2377 } 2378 } 2379 } 2380 2381 if (ac97_is_power_save_mode(ac97) && !powerup) 2382 /* adjust power-down bits after two seconds delay 2383 * (for avoiding loud click noises for many (OSS) apps 2384 * that open/close frequently) 2385 */ 2386 schedule_delayed_work(&ac97->power_work, 2387 msecs_to_jiffies(power_save * 1000)); 2388 else { 2389 cancel_delayed_work(&ac97->power_work); 2390 update_power_regs(ac97); 2391 } 2392 2393 return 0; 2394 } 2395 2396 EXPORT_SYMBOL(snd_ac97_update_power); 2397 #endif /* CONFIG_SND_AC97_POWER_SAVE */ 2398 2399 static void update_power_regs(struct snd_ac97 *ac97) 2400 { 2401 unsigned int power_up, bits; 2402 int i; 2403 2404 power_up = (1 << PWIDX_FRONT) | (1 << PWIDX_ADC); 2405 power_up |= (1 << PWIDX_MIC); 2406 if (ac97->scaps & AC97_SCAP_SURROUND_DAC) 2407 power_up |= (1 << PWIDX_SURR); 2408 if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC) 2409 power_up |= (1 << PWIDX_CLFE); 2410 #ifdef CONFIG_SND_AC97_POWER_SAVE 2411 if (ac97_is_power_save_mode(ac97)) 2412 power_up = ac97->power_up; 2413 #endif 2414 if (power_up) { 2415 if (ac97->regs[AC97_POWERDOWN] & AC97_PD_PR2) { 2416 /* needs power-up analog mix and vref */ 2417 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 2418 AC97_PD_PR3, 0); 2419 msleep(1); 2420 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 2421 AC97_PD_PR2, 0); 2422 } 2423 } 2424 for (i = 0; i < PWIDX_SIZE; i++) { 2425 if (power_up & (1 << i)) 2426 bits = 0; 2427 else 2428 bits = power_regs[i].mask; 2429 snd_ac97_update_bits(ac97, power_regs[i].power_reg, 2430 power_regs[i].mask, bits); 2431 } 2432 if (! power_up) { 2433 if (! (ac97->regs[AC97_POWERDOWN] & AC97_PD_PR2)) { 2434 /* power down analog mix and vref */ 2435 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 2436 AC97_PD_PR2, AC97_PD_PR2); 2437 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 2438 AC97_PD_PR3, AC97_PD_PR3); 2439 } 2440 } 2441 } 2442 2443 2444 #ifdef CONFIG_PM 2445 /** 2446 * snd_ac97_suspend - General suspend function for AC97 codec 2447 * @ac97: the ac97 instance 2448 * 2449 * Suspends the codec, power down the chip. 2450 */ 2451 void snd_ac97_suspend(struct snd_ac97 *ac97) 2452 { 2453 if (! ac97) 2454 return; 2455 if (ac97->build_ops->suspend) 2456 ac97->build_ops->suspend(ac97); 2457 #ifdef CONFIG_SND_AC97_POWER_SAVE 2458 cancel_delayed_work_sync(&ac97->power_work); 2459 #endif 2460 snd_ac97_powerdown(ac97); 2461 } 2462 2463 EXPORT_SYMBOL(snd_ac97_suspend); 2464 2465 /* 2466 * restore ac97 status 2467 */ 2468 static void snd_ac97_restore_status(struct snd_ac97 *ac97) 2469 { 2470 int i; 2471 2472 for (i = 2; i < 0x7c ; i += 2) { 2473 if (i == AC97_POWERDOWN || i == AC97_EXTENDED_ID) 2474 continue; 2475 /* restore only accessible registers 2476 * some chip (e.g. nm256) may hang up when unsupported registers 2477 * are accessed..! 2478 */ 2479 if (test_bit(i, ac97->reg_accessed)) { 2480 snd_ac97_write(ac97, i, ac97->regs[i]); 2481 snd_ac97_read(ac97, i); 2482 } 2483 } 2484 } 2485 2486 /* 2487 * restore IEC958 status 2488 */ 2489 static void snd_ac97_restore_iec958(struct snd_ac97 *ac97) 2490 { 2491 if (ac97->ext_id & AC97_EI_SPDIF) { 2492 if (ac97->regs[AC97_EXTENDED_STATUS] & AC97_EA_SPDIF) { 2493 /* reset spdif status */ 2494 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); 2495 snd_ac97_write(ac97, AC97_EXTENDED_STATUS, ac97->regs[AC97_EXTENDED_STATUS]); 2496 if (ac97->flags & AC97_CS_SPDIF) 2497 snd_ac97_write(ac97, AC97_CSR_SPDIF, ac97->regs[AC97_CSR_SPDIF]); 2498 else 2499 snd_ac97_write(ac97, AC97_SPDIF, ac97->regs[AC97_SPDIF]); 2500 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */ 2501 } 2502 } 2503 } 2504 2505 /** 2506 * snd_ac97_resume - General resume function for AC97 codec 2507 * @ac97: the ac97 instance 2508 * 2509 * Do the standard resume procedure, power up and restoring the 2510 * old register values. 2511 */ 2512 void snd_ac97_resume(struct snd_ac97 *ac97) 2513 { 2514 unsigned long end_time; 2515 2516 if (! ac97) 2517 return; 2518 2519 if (ac97->bus->ops->reset) { 2520 ac97->bus->ops->reset(ac97); 2521 goto __reset_ready; 2522 } 2523 2524 snd_ac97_write(ac97, AC97_POWERDOWN, 0); 2525 if (! (ac97->flags & AC97_DEFAULT_POWER_OFF)) { 2526 if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO)) 2527 snd_ac97_write(ac97, AC97_RESET, 0); 2528 else if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM)) 2529 snd_ac97_write(ac97, AC97_EXTENDED_MID, 0); 2530 udelay(100); 2531 snd_ac97_write(ac97, AC97_POWERDOWN, 0); 2532 } 2533 snd_ac97_write(ac97, AC97_GENERAL_PURPOSE, 0); 2534 2535 snd_ac97_write(ac97, AC97_POWERDOWN, ac97->regs[AC97_POWERDOWN]); 2536 if (ac97_is_audio(ac97)) { 2537 ac97->bus->ops->write(ac97, AC97_MASTER, 0x8101); 2538 end_time = jiffies + msecs_to_jiffies(100); 2539 do { 2540 if (snd_ac97_read(ac97, AC97_MASTER) == 0x8101) 2541 break; 2542 schedule_timeout_uninterruptible(1); 2543 } while (time_after_eq(end_time, jiffies)); 2544 /* FIXME: extra delay */ 2545 ac97->bus->ops->write(ac97, AC97_MASTER, 0x8000); 2546 if (snd_ac97_read(ac97, AC97_MASTER) != 0x8000) 2547 msleep(250); 2548 } else { 2549 end_time = jiffies + msecs_to_jiffies(100); 2550 do { 2551 unsigned short val = snd_ac97_read(ac97, AC97_EXTENDED_MID); 2552 if (val != 0xffff && (val & 1) != 0) 2553 break; 2554 schedule_timeout_uninterruptible(1); 2555 } while (time_after_eq(end_time, jiffies)); 2556 } 2557 __reset_ready: 2558 2559 if (ac97->bus->ops->init) 2560 ac97->bus->ops->init(ac97); 2561 2562 if (ac97->build_ops->resume) 2563 ac97->build_ops->resume(ac97); 2564 else { 2565 snd_ac97_restore_status(ac97); 2566 snd_ac97_restore_iec958(ac97); 2567 } 2568 } 2569 2570 EXPORT_SYMBOL(snd_ac97_resume); 2571 #endif 2572 2573 2574 /* 2575 * Hardware tuning 2576 */ 2577 static void set_ctl_name(char *dst, const char *src, const char *suffix) 2578 { 2579 if (suffix) 2580 sprintf(dst, "%s %s", src, suffix); 2581 else 2582 strcpy(dst, src); 2583 } 2584 2585 /* remove the control with the given name and optional suffix */ 2586 static int snd_ac97_remove_ctl(struct snd_ac97 *ac97, const char *name, 2587 const char *suffix) 2588 { 2589 struct snd_ctl_elem_id id; 2590 memset(&id, 0, sizeof(id)); 2591 set_ctl_name(id.name, name, suffix); 2592 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 2593 return snd_ctl_remove_id(ac97->bus->card, &id); 2594 } 2595 2596 static struct snd_kcontrol *ctl_find(struct snd_ac97 *ac97, const char *name, const char *suffix) 2597 { 2598 struct snd_ctl_elem_id sid; 2599 memset(&sid, 0, sizeof(sid)); 2600 set_ctl_name(sid.name, name, suffix); 2601 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 2602 return snd_ctl_find_id(ac97->bus->card, &sid); 2603 } 2604 2605 /* rename the control with the given name and optional suffix */ 2606 static int snd_ac97_rename_ctl(struct snd_ac97 *ac97, const char *src, 2607 const char *dst, const char *suffix) 2608 { 2609 struct snd_kcontrol *kctl = ctl_find(ac97, src, suffix); 2610 if (kctl) { 2611 set_ctl_name(kctl->id.name, dst, suffix); 2612 return 0; 2613 } 2614 return -ENOENT; 2615 } 2616 2617 /* rename both Volume and Switch controls - don't check the return value */ 2618 static void snd_ac97_rename_vol_ctl(struct snd_ac97 *ac97, const char *src, 2619 const char *dst) 2620 { 2621 snd_ac97_rename_ctl(ac97, src, dst, "Switch"); 2622 snd_ac97_rename_ctl(ac97, src, dst, "Volume"); 2623 } 2624 2625 /* swap controls */ 2626 static int snd_ac97_swap_ctl(struct snd_ac97 *ac97, const char *s1, 2627 const char *s2, const char *suffix) 2628 { 2629 struct snd_kcontrol *kctl1, *kctl2; 2630 kctl1 = ctl_find(ac97, s1, suffix); 2631 kctl2 = ctl_find(ac97, s2, suffix); 2632 if (kctl1 && kctl2) { 2633 set_ctl_name(kctl1->id.name, s2, suffix); 2634 set_ctl_name(kctl2->id.name, s1, suffix); 2635 return 0; 2636 } 2637 return -ENOENT; 2638 } 2639 2640 #if 1 2641 /* bind hp and master controls instead of using only hp control */ 2642 static int bind_hp_volsw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 2643 { 2644 int err = snd_ac97_put_volsw(kcontrol, ucontrol); 2645 if (err > 0) { 2646 unsigned long priv_saved = kcontrol->private_value; 2647 kcontrol->private_value = (kcontrol->private_value & ~0xff) | AC97_HEADPHONE; 2648 snd_ac97_put_volsw(kcontrol, ucontrol); 2649 kcontrol->private_value = priv_saved; 2650 } 2651 return err; 2652 } 2653 2654 /* ac97 tune: bind Master and Headphone controls */ 2655 static int tune_hp_only(struct snd_ac97 *ac97) 2656 { 2657 struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL); 2658 struct snd_kcontrol *mvol = ctl_find(ac97, "Master Playback Volume", NULL); 2659 if (! msw || ! mvol) 2660 return -ENOENT; 2661 msw->put = bind_hp_volsw_put; 2662 mvol->put = bind_hp_volsw_put; 2663 snd_ac97_remove_ctl(ac97, "Headphone Playback", "Switch"); 2664 snd_ac97_remove_ctl(ac97, "Headphone Playback", "Volume"); 2665 return 0; 2666 } 2667 2668 #else 2669 /* ac97 tune: use Headphone control as master */ 2670 static int tune_hp_only(struct snd_ac97 *ac97) 2671 { 2672 if (ctl_find(ac97, "Headphone Playback Switch", NULL) == NULL) 2673 return -ENOENT; 2674 snd_ac97_remove_ctl(ac97, "Master Playback", "Switch"); 2675 snd_ac97_remove_ctl(ac97, "Master Playback", "Volume"); 2676 snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback"); 2677 return 0; 2678 } 2679 #endif 2680 2681 /* ac97 tune: swap Headphone and Master controls */ 2682 static int tune_swap_hp(struct snd_ac97 *ac97) 2683 { 2684 if (ctl_find(ac97, "Headphone Playback Switch", NULL) == NULL) 2685 return -ENOENT; 2686 snd_ac97_rename_vol_ctl(ac97, "Master Playback", "Line-Out Playback"); 2687 snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback"); 2688 return 0; 2689 } 2690 2691 /* ac97 tune: swap Surround and Master controls */ 2692 static int tune_swap_surround(struct snd_ac97 *ac97) 2693 { 2694 if (snd_ac97_swap_ctl(ac97, "Master Playback", "Surround Playback", "Switch") || 2695 snd_ac97_swap_ctl(ac97, "Master Playback", "Surround Playback", "Volume")) 2696 return -ENOENT; 2697 return 0; 2698 } 2699 2700 /* ac97 tune: set up mic sharing for AD codecs */ 2701 static int tune_ad_sharing(struct snd_ac97 *ac97) 2702 { 2703 unsigned short scfg; 2704 if ((ac97->id & 0xffffff00) != 0x41445300) { 2705 snd_printk(KERN_ERR "ac97_quirk AD_SHARING is only for AD codecs\n"); 2706 return -EINVAL; 2707 } 2708 /* Turn on OMS bit to route microphone to back panel */ 2709 scfg = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG); 2710 snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, scfg | 0x0200); 2711 return 0; 2712 } 2713 2714 static const struct snd_kcontrol_new snd_ac97_alc_jack_detect = 2715 AC97_SINGLE("Jack Detect", AC97_ALC650_CLOCK, 5, 1, 0); 2716 2717 /* ac97 tune: set up ALC jack-select */ 2718 static int tune_alc_jack(struct snd_ac97 *ac97) 2719 { 2720 if ((ac97->id & 0xffffff00) != 0x414c4700) { 2721 snd_printk(KERN_ERR "ac97_quirk ALC_JACK is only for Realtek codecs\n"); 2722 return -EINVAL; 2723 } 2724 snd_ac97_update_bits(ac97, 0x7a, 0x20, 0x20); /* select jack detect function */ 2725 snd_ac97_update_bits(ac97, 0x7a, 0x01, 0x01); /* Line-out auto mute */ 2726 if (ac97->id == AC97_ID_ALC658D) 2727 snd_ac97_update_bits(ac97, 0x74, 0x0800, 0x0800); 2728 return snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&snd_ac97_alc_jack_detect, ac97)); 2729 } 2730 2731 /* ac97 tune: inversed EAPD bit */ 2732 static int tune_inv_eapd(struct snd_ac97 *ac97) 2733 { 2734 struct snd_kcontrol *kctl = ctl_find(ac97, "External Amplifier", NULL); 2735 if (! kctl) 2736 return -ENOENT; 2737 set_inv_eapd(ac97, kctl); 2738 return 0; 2739 } 2740 2741 static int master_mute_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 2742 { 2743 int err = snd_ac97_put_volsw(kcontrol, ucontrol); 2744 if (err > 0) { 2745 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol); 2746 int shift = (kcontrol->private_value >> 8) & 0x0f; 2747 int rshift = (kcontrol->private_value >> 12) & 0x0f; 2748 unsigned short mask; 2749 if (shift != rshift) 2750 mask = 0x8080; 2751 else 2752 mask = 0x8000; 2753 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000, 2754 (ac97->regs[AC97_MASTER] & mask) == mask ? 2755 0x8000 : 0); 2756 } 2757 return err; 2758 } 2759 2760 /* ac97 tune: EAPD controls mute LED bound with the master mute */ 2761 static int tune_mute_led(struct snd_ac97 *ac97) 2762 { 2763 struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL); 2764 if (! msw) 2765 return -ENOENT; 2766 msw->put = master_mute_sw_put; 2767 snd_ac97_remove_ctl(ac97, "External Amplifier", NULL); 2768 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000, 0x8000); /* mute LED on */ 2769 ac97->scaps |= AC97_SCAP_EAPD_LED; 2770 return 0; 2771 } 2772 2773 static int hp_master_mute_sw_put(struct snd_kcontrol *kcontrol, 2774 struct snd_ctl_elem_value *ucontrol) 2775 { 2776 int err = bind_hp_volsw_put(kcontrol, ucontrol); 2777 if (err > 0) { 2778 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol); 2779 int shift = (kcontrol->private_value >> 8) & 0x0f; 2780 int rshift = (kcontrol->private_value >> 12) & 0x0f; 2781 unsigned short mask; 2782 if (shift != rshift) 2783 mask = 0x8080; 2784 else 2785 mask = 0x8000; 2786 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000, 2787 (ac97->regs[AC97_MASTER] & mask) == mask ? 2788 0x8000 : 0); 2789 } 2790 return err; 2791 } 2792 2793 static int tune_hp_mute_led(struct snd_ac97 *ac97) 2794 { 2795 struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL); 2796 struct snd_kcontrol *mvol = ctl_find(ac97, "Master Playback Volume", NULL); 2797 if (! msw || ! mvol) 2798 return -ENOENT; 2799 msw->put = hp_master_mute_sw_put; 2800 mvol->put = bind_hp_volsw_put; 2801 snd_ac97_remove_ctl(ac97, "External Amplifier", NULL); 2802 snd_ac97_remove_ctl(ac97, "Headphone Playback", "Switch"); 2803 snd_ac97_remove_ctl(ac97, "Headphone Playback", "Volume"); 2804 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000, 0x8000); /* mute LED on */ 2805 return 0; 2806 } 2807 2808 struct quirk_table { 2809 const char *name; 2810 int (*func)(struct snd_ac97 *); 2811 }; 2812 2813 static struct quirk_table applicable_quirks[] = { 2814 { "none", NULL }, 2815 { "hp_only", tune_hp_only }, 2816 { "swap_hp", tune_swap_hp }, 2817 { "swap_surround", tune_swap_surround }, 2818 { "ad_sharing", tune_ad_sharing }, 2819 { "alc_jack", tune_alc_jack }, 2820 { "inv_eapd", tune_inv_eapd }, 2821 { "mute_led", tune_mute_led }, 2822 { "hp_mute_led", tune_hp_mute_led }, 2823 }; 2824 2825 /* apply the quirk with the given type */ 2826 static int apply_quirk(struct snd_ac97 *ac97, int type) 2827 { 2828 if (type <= 0) 2829 return 0; 2830 else if (type >= ARRAY_SIZE(applicable_quirks)) 2831 return -EINVAL; 2832 if (applicable_quirks[type].func) 2833 return applicable_quirks[type].func(ac97); 2834 return 0; 2835 } 2836 2837 /* apply the quirk with the given name */ 2838 static int apply_quirk_str(struct snd_ac97 *ac97, const char *typestr) 2839 { 2840 int i; 2841 struct quirk_table *q; 2842 2843 for (i = 0; i < ARRAY_SIZE(applicable_quirks); i++) { 2844 q = &applicable_quirks[i]; 2845 if (q->name && ! strcmp(typestr, q->name)) 2846 return apply_quirk(ac97, i); 2847 } 2848 /* for compatibility, accept the numbers, too */ 2849 if (*typestr >= '0' && *typestr <= '9') 2850 return apply_quirk(ac97, (int)simple_strtoul(typestr, NULL, 10)); 2851 return -EINVAL; 2852 } 2853 2854 /** 2855 * snd_ac97_tune_hardware - tune up the hardware 2856 * @ac97: the ac97 instance 2857 * @quirk: quirk list 2858 * @override: explicit quirk value (overrides the list if non-NULL) 2859 * 2860 * Do some workaround for each pci device, such as renaming of the 2861 * headphone (true line-out) control as "Master". 2862 * The quirk-list must be terminated with a zero-filled entry. 2863 * 2864 * Returns zero if successful, or a negative error code on failure. 2865 */ 2866 2867 int snd_ac97_tune_hardware(struct snd_ac97 *ac97, struct ac97_quirk *quirk, const char *override) 2868 { 2869 int result; 2870 2871 /* quirk overriden? */ 2872 if (override && strcmp(override, "-1") && strcmp(override, "default")) { 2873 result = apply_quirk_str(ac97, override); 2874 if (result < 0) 2875 snd_printk(KERN_ERR "applying quirk type %s failed (%d)\n", override, result); 2876 return result; 2877 } 2878 2879 if (! quirk) 2880 return -EINVAL; 2881 2882 for (; quirk->subvendor; quirk++) { 2883 if (quirk->subvendor != ac97->subsystem_vendor) 2884 continue; 2885 if ((! quirk->mask && quirk->subdevice == ac97->subsystem_device) || 2886 quirk->subdevice == (quirk->mask & ac97->subsystem_device)) { 2887 if (quirk->codec_id && quirk->codec_id != ac97->id) 2888 continue; 2889 snd_printdd("ac97 quirk for %s (%04x:%04x)\n", quirk->name, ac97->subsystem_vendor, ac97->subsystem_device); 2890 result = apply_quirk(ac97, quirk->type); 2891 if (result < 0) 2892 snd_printk(KERN_ERR "applying quirk type %d for %s failed (%d)\n", quirk->type, quirk->name, result); 2893 return result; 2894 } 2895 } 2896 return 0; 2897 } 2898 2899 EXPORT_SYMBOL(snd_ac97_tune_hardware); 2900 2901 /* 2902 * INIT part 2903 */ 2904 2905 static int __init alsa_ac97_init(void) 2906 { 2907 return 0; 2908 } 2909 2910 static void __exit alsa_ac97_exit(void) 2911 { 2912 } 2913 2914 module_init(alsa_ac97_init) 2915 module_exit(alsa_ac97_exit) 2916