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