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