blob: 45fd29017ddd59ded7e731ece20c977a4a4bde20 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02002 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/delay.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/pci.h>
29#include <linux/moduleparam.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010030#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include <sound/pcm.h>
Takashi Iwai2f3482f2006-08-21 18:44:31 +020033#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <sound/ac97_codec.h>
35#include <sound/asoundef.h>
36#include <sound/initval.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "ac97_id.h"
Takashi Iwaiac519022007-02-22 12:58:27 +010038
39#include "ac97_patch.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020041MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070042MODULE_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
Takashi Iwai6dbe6622006-06-27 18:28:53 +020050#ifdef CONFIG_SND_AC97_POWER_SAVE
Takashi Iwai7a5a27c2007-09-17 19:07:46 +020051static int power_save = CONFIG_SND_AC97_POWER_SAVE_DEFAULT;
Takashi Iwai6dbe6622006-06-27 18:28:53 +020052module_param(power_save, bool, 0644);
53MODULE_PARM_DESC(power_save, "Enable AC97 power-saving control");
54#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*
56
57 */
58
Takashi Iwaiee423812005-11-17 14:21:36 +010059struct ac97_codec_id {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 unsigned int id;
61 unsigned int mask;
62 const char *name;
Takashi Iwaiee423812005-11-17 14:21:36 +010063 int (*patch)(struct snd_ac97 *ac97);
64 int (*mpatch)(struct snd_ac97 *ac97);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 unsigned int flags;
Takashi Iwaiee423812005-11-17 14:21:36 +010066};
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Takashi Iwaiee423812005-11-17 14:21:36 +010068static const struct ac97_codec_id snd_ac97_codec_id_vendors[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{ 0x414b4d00, 0xffffff00, "Asahi Kasei", NULL, NULL },
70{ 0x41445300, 0xffffff00, "Analog Devices", NULL, NULL },
71{ 0x414c4300, 0xffffff00, "Realtek", NULL, NULL },
72{ 0x414c4700, 0xffffff00, "Realtek", NULL, NULL },
73{ 0x434d4900, 0xffffff00, "C-Media Electronics", NULL, NULL },
74{ 0x43525900, 0xffffff00, "Cirrus Logic", NULL, NULL },
75{ 0x43585400, 0xffffff00, "Conexant", NULL, NULL },
76{ 0x44543000, 0xffffff00, "Diamond Technology", NULL, NULL },
77{ 0x454d4300, 0xffffff00, "eMicro", NULL, NULL },
78{ 0x45838300, 0xffffff00, "ESS Technology", NULL, NULL },
79{ 0x48525300, 0xffffff00, "Intersil", NULL, NULL },
80{ 0x49434500, 0xffffff00, "ICEnsemble", NULL, NULL },
81{ 0x49544500, 0xffffff00, "ITE Tech.Inc", NULL, NULL },
82{ 0x4e534300, 0xffffff00, "National Semiconductor", NULL, NULL },
83{ 0x50534300, 0xffffff00, "Philips", NULL, NULL },
84{ 0x53494c00, 0xffffff00, "Silicon Laboratory", NULL, NULL },
85{ 0x54524100, 0xffffff00, "TriTech", NULL, NULL },
86{ 0x54584e00, 0xffffff00, "Texas Instruments", NULL, NULL },
87{ 0x56494100, 0xffffff00, "VIA Technologies", NULL, NULL },
88{ 0x57454300, 0xffffff00, "Winbond", NULL, NULL },
89{ 0x574d4c00, 0xffffff00, "Wolfson", NULL, NULL },
90{ 0x594d4800, 0xffffff00, "Yamaha", NULL, NULL },
91{ 0x83847600, 0xffffff00, "SigmaTel", NULL, NULL },
92{ 0, 0, NULL, NULL, NULL }
93};
94
Takashi Iwaiee423812005-11-17 14:21:36 +010095static const struct ac97_codec_id snd_ac97_codec_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{ 0x414b4d00, 0xffffffff, "AK4540", NULL, NULL },
97{ 0x414b4d01, 0xffffffff, "AK4542", NULL, NULL },
98{ 0x414b4d02, 0xffffffff, "AK4543", NULL, NULL },
99{ 0x414b4d06, 0xffffffff, "AK4544A", NULL, NULL },
100{ 0x414b4d07, 0xffffffff, "AK4545", NULL, NULL },
101{ 0x41445303, 0xffffffff, "AD1819", patch_ad1819, NULL },
102{ 0x41445340, 0xffffffff, "AD1881", patch_ad1881, NULL },
103{ 0x41445348, 0xffffffff, "AD1881A", patch_ad1881, NULL },
104{ 0x41445360, 0xffffffff, "AD1885", patch_ad1885, NULL },
105{ 0x41445361, 0xffffffff, "AD1886", patch_ad1886, NULL },
106{ 0x41445362, 0xffffffff, "AD1887", patch_ad1881, NULL },
107{ 0x41445363, 0xffffffff, "AD1886A", patch_ad1881, NULL },
108{ 0x41445368, 0xffffffff, "AD1888", patch_ad1888, NULL },
109{ 0x41445370, 0xffffffff, "AD1980", patch_ad1980, NULL },
110{ 0x41445372, 0xffffffff, "AD1981A", patch_ad1981a, NULL },
111{ 0x41445374, 0xffffffff, "AD1981B", patch_ad1981b, NULL },
112{ 0x41445375, 0xffffffff, "AD1985", patch_ad1985, NULL },
Randy Cushman67e9f4b2006-12-22 12:44:25 +0100113{ 0x41445378, 0xffffffff, "AD1986", patch_ad1986, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{ 0x414c4300, 0xffffff00, "ALC100,100P", NULL, NULL },
115{ 0x414c4710, 0xfffffff0, "ALC200,200P", NULL, NULL },
116{ 0x414c4721, 0xffffffff, "ALC650D", NULL, NULL }, /* already patched */
117{ 0x414c4722, 0xffffffff, "ALC650E", NULL, NULL }, /* already patched */
118{ 0x414c4723, 0xffffffff, "ALC650F", NULL, NULL }, /* already patched */
119{ 0x414c4720, 0xfffffff0, "ALC650", patch_alc650, NULL },
120{ 0x414c4760, 0xfffffff0, "ALC655", patch_alc655, NULL },
Takashi Iwaia5022b02005-09-02 14:03:05 +0200121{ 0x414c4781, 0xffffffff, "ALC658D", NULL, NULL }, /* already patched */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{ 0x414c4780, 0xfffffff0, "ALC658", patch_alc655, NULL },
123{ 0x414c4790, 0xfffffff0, "ALC850", patch_alc850, NULL },
124{ 0x414c4730, 0xffffffff, "ALC101", NULL, NULL },
125{ 0x414c4740, 0xfffffff0, "ALC202", NULL, NULL },
126{ 0x414c4750, 0xfffffff0, "ALC250", NULL, NULL },
127{ 0x414c4770, 0xfffffff0, "ALC203", NULL, NULL },
128{ 0x434d4941, 0xffffffff, "CMI9738", patch_cm9738, NULL },
129{ 0x434d4961, 0xffffffff, "CMI9739", patch_cm9739, NULL },
Takashi Iwai5f0dccf2005-04-07 15:53:20 +0200130{ 0x434d4969, 0xffffffff, "CMI9780", patch_cm9780, NULL },
James Courtier-Duttonf8cb2c42006-12-12 17:05:24 +0000131{ 0x434d4978, 0xffffffff, "CMI9761A", patch_cm9761, NULL },
132{ 0x434d4982, 0xffffffff, "CMI9761B", patch_cm9761, NULL },
133{ 0x434d4983, 0xffffffff, "CMI9761A+", patch_cm9761, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{ 0x43525900, 0xfffffff8, "CS4297", NULL, NULL },
135{ 0x43525910, 0xfffffff8, "CS4297A", patch_cirrus_spdif, NULL },
136{ 0x43525920, 0xfffffff8, "CS4298", patch_cirrus_spdif, NULL },
137{ 0x43525928, 0xfffffff8, "CS4294", NULL, NULL },
138{ 0x43525930, 0xfffffff8, "CS4299", patch_cirrus_cs4299, NULL },
139{ 0x43525948, 0xfffffff8, "CS4201", NULL, NULL },
140{ 0x43525958, 0xfffffff8, "CS4205", patch_cirrus_spdif, NULL },
141{ 0x43525960, 0xfffffff8, "CS4291", NULL, NULL },
142{ 0x43525970, 0xfffffff8, "CS4202", NULL, NULL },
143{ 0x43585421, 0xffffffff, "HSD11246", NULL, NULL }, // SmartMC II
144{ 0x43585428, 0xfffffff8, "Cx20468", patch_conexant, NULL }, // SmartAMC fixme: the mask might be different
Takashi Iwai9e292c02007-02-09 12:42:03 +0100145{ 0x43585431, 0xffffffff, "Cx20551", patch_cx20551, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{ 0x44543031, 0xfffffff0, "DT0398", NULL, NULL },
Takashi Iwai93984412005-11-17 15:30:43 +0100147{ 0x454d4328, 0xffffffff, "EM28028", NULL, NULL }, // same as TR28028?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{ 0x45838308, 0xffffffff, "ESS1988", NULL, NULL },
149{ 0x48525300, 0xffffff00, "HMP9701", NULL, NULL },
150{ 0x49434501, 0xffffffff, "ICE1230", NULL, NULL },
151{ 0x49434511, 0xffffffff, "ICE1232", NULL, NULL }, // alias VIA VT1611A?
152{ 0x49434514, 0xffffffff, "ICE1232A", NULL, NULL },
153{ 0x49434551, 0xffffffff, "VT1616", patch_vt1616, NULL },
154{ 0x49434552, 0xffffffff, "VT1616i", patch_vt1616, NULL }, // VT1616 compatible (chipset integrated)
155{ 0x49544520, 0xffffffff, "IT2226E", NULL, NULL },
156{ 0x49544561, 0xffffffff, "IT2646E", patch_it2646, NULL },
157{ 0x4e534300, 0xffffffff, "LM4540,43,45,46,48", NULL, NULL }, // only guess --jk
158{ 0x4e534331, 0xffffffff, "LM4549", NULL, NULL },
Charl Coetzeeba224292006-02-09 11:48:21 +0100159{ 0x4e534350, 0xffffffff, "LM4550", patch_lm4550, NULL }, // volume wrap fix
Mike Rapoport82466ad2006-06-29 17:15:33 +0200160{ 0x50534304, 0xffffffff, "UCB1400", patch_ucb1400, NULL },
Sasha Khapyorsky87d61c22005-05-29 15:08:23 +0200161{ 0x53494c20, 0xffffffe0, "Si3036,8", mpatch_si3036, mpatch_si3036, AC97_MODEM_PATCH },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{ 0x54524102, 0xffffffff, "TR28022", NULL, NULL },
163{ 0x54524106, 0xffffffff, "TR28026", NULL, NULL },
164{ 0x54524108, 0xffffffff, "TR28028", patch_tritech_tr28028, NULL }, // added by xin jin [07/09/99]
165{ 0x54524123, 0xffffffff, "TR28602", NULL, NULL }, // only guess --jk [TR28023 = eMicro EM28023 (new CT1297)]
166{ 0x54584e20, 0xffffffff, "TLC320AD9xC", NULL, NULL },
167{ 0x56494161, 0xffffffff, "VIA1612A", NULL, NULL }, // modified ICE1232 with S/PDIF
Philip Prindeville4b499482005-08-12 16:46:17 +0200168{ 0x56494170, 0xffffffff, "VIA1617A", patch_vt1617a, NULL }, // modified VT1616 with S/PDIF
Takashi Iwai73864fc2006-04-10 19:41:16 +0200169{ 0x56494182, 0xffffffff, "VIA1618", NULL, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{ 0x57454301, 0xffffffff, "W83971D", NULL, NULL },
171{ 0x574d4c00, 0xffffffff, "WM9701A", NULL, NULL },
172{ 0x574d4C03, 0xffffffff, "WM9703,WM9707,WM9708,WM9717", patch_wolfson03, NULL},
173{ 0x574d4C04, 0xffffffff, "WM9704M,WM9704Q", patch_wolfson04, NULL},
174{ 0x574d4C05, 0xffffffff, "WM9705,WM9710", patch_wolfson05, NULL},
175{ 0x574d4C09, 0xffffffff, "WM9709", NULL, NULL},
176{ 0x574d4C12, 0xffffffff, "WM9711,WM9712", patch_wolfson11, NULL},
177{ 0x574d4c13, 0xffffffff, "WM9713,WM9714", patch_wolfson13, NULL, AC97_DEFAULT_POWER_OFF},
Keita Maehara43115f52007-09-19 14:29:37 +0200178{ 0x594d4800, 0xffffffff, "YMF743", patch_yamaha_ymf743, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{ 0x594d4802, 0xffffffff, "YMF752", NULL, NULL },
180{ 0x594d4803, 0xffffffff, "YMF753", patch_yamaha_ymf753, NULL },
181{ 0x83847600, 0xffffffff, "STAC9700,83,84", patch_sigmatel_stac9700, NULL },
182{ 0x83847604, 0xffffffff, "STAC9701,3,4,5", NULL, NULL },
183{ 0x83847605, 0xffffffff, "STAC9704", NULL, NULL },
184{ 0x83847608, 0xffffffff, "STAC9708,11", patch_sigmatel_stac9708, NULL },
185{ 0x83847609, 0xffffffff, "STAC9721,23", patch_sigmatel_stac9721, NULL },
186{ 0x83847644, 0xffffffff, "STAC9744", patch_sigmatel_stac9744, NULL },
187{ 0x83847650, 0xffffffff, "STAC9750,51", NULL, NULL }, // patch?
188{ 0x83847652, 0xffffffff, "STAC9752,53", NULL, NULL }, // patch?
189{ 0x83847656, 0xffffffff, "STAC9756,57", patch_sigmatel_stac9756, NULL },
190{ 0x83847658, 0xffffffff, "STAC9758,59", patch_sigmatel_stac9758, NULL },
191{ 0x83847666, 0xffffffff, "STAC9766,67", NULL, NULL }, // patch?
192{ 0, 0, NULL, NULL, NULL }
193};
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Takashi Iwai6dbe6622006-06-27 18:28:53 +0200196static void update_power_regs(struct snd_ac97 *ac97);
Takashi Iwaif1a63a32006-10-24 18:25:29 +0200197#ifdef CONFIG_SND_AC97_POWER_SAVE
198#define ac97_is_power_save_mode(ac97) \
199 ((ac97->scaps & AC97_SCAP_POWER_SAVE) && power_save)
200#else
201#define ac97_is_power_save_mode(ac97) 0
202#endif
203
Takashi Iwai6dbe6622006-06-27 18:28:53 +0200204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
206 * I/O routines
207 */
208
Takashi Iwaiee423812005-11-17 14:21:36 +0100209static int snd_ac97_valid_reg(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* filter some registers for buggy codecs */
212 switch (ac97->id) {
213 case AC97_ID_AK4540:
214 case AC97_ID_AK4542:
215 if (reg <= 0x1c || reg == 0x20 || reg == 0x26 || reg >= 0x7c)
216 return 1;
217 return 0;
218 case AC97_ID_AD1819: /* AD1819 */
219 case AC97_ID_AD1881: /* AD1881 */
220 case AC97_ID_AD1881A: /* AD1881A */
221 if (reg >= 0x3a && reg <= 0x6e) /* 0x59 */
222 return 0;
223 return 1;
224 case AC97_ID_AD1885: /* AD1885 */
225 case AC97_ID_AD1886: /* AD1886 */
226 case AC97_ID_AD1886A: /* AD1886A - !!verify!! --jk */
227 case AC97_ID_AD1887: /* AD1887 - !!verify!! --jk */
228 if (reg == 0x5a)
229 return 1;
230 if (reg >= 0x3c && reg <= 0x6e) /* 0x59 */
231 return 0;
232 return 1;
233 case AC97_ID_STAC9700:
234 case AC97_ID_STAC9704:
235 case AC97_ID_STAC9705:
236 case AC97_ID_STAC9708:
237 case AC97_ID_STAC9721:
238 case AC97_ID_STAC9744:
239 case AC97_ID_STAC9756:
240 if (reg <= 0x3a || reg >= 0x5a)
241 return 1;
242 return 0;
243 }
244 return 1;
245}
246
247/**
248 * snd_ac97_write - write a value on the given register
249 * @ac97: the ac97 instance
250 * @reg: the register to change
251 * @value: the value to set
252 *
253 * Writes a value on the given register. This will invoke the write
254 * callback directly after the register check.
255 * This function doesn't change the register cache unlike
256 * #snd_ca97_write_cache(), so use this only when you don't want to
257 * reflect the change to the suspend/resume state.
258 */
Takashi Iwaiee423812005-11-17 14:21:36 +0100259void snd_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 if (!snd_ac97_valid_reg(ac97, reg))
262 return;
263 if ((ac97->id & 0xffffff00) == AC97_ID_ALC100) {
264 /* Fix H/W bug of ALC100/100P */
265 if (reg == AC97_MASTER || reg == AC97_HEADPHONE)
266 ac97->bus->ops->write(ac97, AC97_RESET, 0); /* reset audio codec */
267 }
268 ac97->bus->ops->write(ac97, reg, value);
269}
270
Takashi Iwai7b096792006-04-28 15:13:39 +0200271EXPORT_SYMBOL(snd_ac97_write);
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/**
274 * snd_ac97_read - read a value from the given register
275 *
276 * @ac97: the ac97 instance
277 * @reg: the register to read
278 *
279 * Reads a value from the given register. This will invoke the read
280 * callback directly after the register check.
281 *
282 * Returns the read value.
283 */
Takashi Iwaiee423812005-11-17 14:21:36 +0100284unsigned short snd_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 if (!snd_ac97_valid_reg(ac97, reg))
287 return 0;
288 return ac97->bus->ops->read(ac97, reg);
289}
290
291/* read a register - return the cached value if already read */
Takashi Iwaiee423812005-11-17 14:21:36 +0100292static inline unsigned short snd_ac97_read_cache(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 if (! test_bit(reg, ac97->reg_accessed)) {
295 ac97->regs[reg] = ac97->bus->ops->read(ac97, reg);
296 // set_bit(reg, ac97->reg_accessed);
297 }
298 return ac97->regs[reg];
299}
300
Takashi Iwai7b096792006-04-28 15:13:39 +0200301EXPORT_SYMBOL(snd_ac97_read);
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/**
304 * snd_ac97_write_cache - write a value on the given register and update the cache
305 * @ac97: the ac97 instance
306 * @reg: the register to change
307 * @value: the value to set
308 *
309 * Writes a value on the given register and updates the register
310 * cache. The cached values are used for the cached-read and the
311 * suspend/resume.
312 */
Takashi Iwaiee423812005-11-17 14:21:36 +0100313void snd_ac97_write_cache(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 if (!snd_ac97_valid_reg(ac97, reg))
316 return;
Ingo Molnar62932df2006-01-16 16:34:20 +0100317 mutex_lock(&ac97->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 ac97->regs[reg] = value;
319 ac97->bus->ops->write(ac97, reg, value);
320 set_bit(reg, ac97->reg_accessed);
Ingo Molnar62932df2006-01-16 16:34:20 +0100321 mutex_unlock(&ac97->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Takashi Iwai7b096792006-04-28 15:13:39 +0200324EXPORT_SYMBOL(snd_ac97_write_cache);
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/**
327 * snd_ac97_update - update the value on the given register
328 * @ac97: the ac97 instance
329 * @reg: the register to change
330 * @value: the value to set
331 *
332 * Compares the value with the register cache and updates the value
333 * only when the value is changed.
334 *
335 * Returns 1 if the value is changed, 0 if no change, or a negative
336 * code on failure.
337 */
Takashi Iwaiee423812005-11-17 14:21:36 +0100338int snd_ac97_update(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 int change;
341
342 if (!snd_ac97_valid_reg(ac97, reg))
343 return -EINVAL;
Ingo Molnar62932df2006-01-16 16:34:20 +0100344 mutex_lock(&ac97->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 change = ac97->regs[reg] != value;
346 if (change) {
347 ac97->regs[reg] = value;
348 ac97->bus->ops->write(ac97, reg, value);
349 }
Takashi Iwai52b72382005-06-30 13:47:06 +0200350 set_bit(reg, ac97->reg_accessed);
Ingo Molnar62932df2006-01-16 16:34:20 +0100351 mutex_unlock(&ac97->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return change;
353}
354
Takashi Iwai7b096792006-04-28 15:13:39 +0200355EXPORT_SYMBOL(snd_ac97_update);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/**
358 * snd_ac97_update_bits - update the bits on the given register
359 * @ac97: the ac97 instance
360 * @reg: the register to change
361 * @mask: the bit-mask to change
362 * @value: the value to set
363 *
364 * Updates the masked-bits on the given register only when the value
365 * is changed.
366 *
367 * Returns 1 if the bits are changed, 0 if no change, or a negative
368 * code on failure.
369 */
Takashi Iwaiee423812005-11-17 14:21:36 +0100370int snd_ac97_update_bits(struct snd_ac97 *ac97, unsigned short reg, unsigned short mask, unsigned short value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 int change;
373
374 if (!snd_ac97_valid_reg(ac97, reg))
375 return -EINVAL;
Ingo Molnar62932df2006-01-16 16:34:20 +0100376 mutex_lock(&ac97->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 change = snd_ac97_update_bits_nolock(ac97, reg, mask, value);
Ingo Molnar62932df2006-01-16 16:34:20 +0100378 mutex_unlock(&ac97->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return change;
380}
381
Takashi Iwai7b096792006-04-28 15:13:39 +0200382EXPORT_SYMBOL(snd_ac97_update_bits);
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/* no lock version - see snd_ac97_updat_bits() */
Takashi Iwaiee423812005-11-17 14:21:36 +0100385int snd_ac97_update_bits_nolock(struct snd_ac97 *ac97, unsigned short reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 unsigned short mask, unsigned short value)
387{
388 int change;
389 unsigned short old, new;
390
391 old = snd_ac97_read_cache(ac97, reg);
James C Georgase8bb0362006-12-07 08:10:57 +0100392 new = (old & ~mask) | (value & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 change = old != new;
394 if (change) {
395 ac97->regs[reg] = new;
396 ac97->bus->ops->write(ac97, reg, new);
397 }
Takashi Iwai52b72382005-06-30 13:47:06 +0200398 set_bit(reg, ac97->reg_accessed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return change;
400}
401
Takashi Iwaiee423812005-11-17 14:21:36 +0100402static int snd_ac97_ad18xx_update_pcm_bits(struct snd_ac97 *ac97, int codec, unsigned short mask, unsigned short value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 int change;
405 unsigned short old, new, cfg;
406
Ingo Molnar62932df2006-01-16 16:34:20 +0100407 mutex_lock(&ac97->page_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 old = ac97->spec.ad18xx.pcmreg[codec];
James C Georgase8bb0362006-12-07 08:10:57 +0100409 new = (old & ~mask) | (value & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 change = old != new;
411 if (change) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100412 mutex_lock(&ac97->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 cfg = snd_ac97_read_cache(ac97, AC97_AD_SERIAL_CFG);
414 ac97->spec.ad18xx.pcmreg[codec] = new;
415 /* select single codec */
416 ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG,
417 (cfg & ~0x7000) |
418 ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
419 /* update PCM bits */
420 ac97->bus->ops->write(ac97, AC97_PCM, new);
421 /* select all codecs */
422 ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG,
423 cfg | 0x7000);
Ingo Molnar62932df2006-01-16 16:34:20 +0100424 mutex_unlock(&ac97->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100426 mutex_unlock(&ac97->page_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return change;
428}
429
430/*
431 * Controls
432 */
433
Takashi Iwaiac519022007-02-22 12:58:27 +0100434static int snd_ac97_info_enum_double(struct snd_kcontrol *kcontrol,
435 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
438
439 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
440 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
441 uinfo->value.enumerated.items = e->mask;
442
443 if (uinfo->value.enumerated.item > e->mask - 1)
444 uinfo->value.enumerated.item = e->mask - 1;
445 strcpy(uinfo->value.enumerated.name, e->texts[uinfo->value.enumerated.item]);
446 return 0;
447}
448
Takashi Iwaiac519022007-02-22 12:58:27 +0100449static int snd_ac97_get_enum_double(struct snd_kcontrol *kcontrol,
450 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Takashi Iwaiee423812005-11-17 14:21:36 +0100452 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
Takashi Iwai5f0dccf2005-04-07 15:53:20 +0200454 unsigned short val, bitmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Sergey Vlasovb16760b2005-04-25 11:22:20 +0200456 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
Takashi Iwai5f0dccf2005-04-07 15:53:20 +0200457 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 val = snd_ac97_read_cache(ac97, e->reg);
Takashi Iwai5f0dccf2005-04-07 15:53:20 +0200459 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (e->shift_l != e->shift_r)
Takashi Iwai5f0dccf2005-04-07 15:53:20 +0200461 ucontrol->value.enumerated.item[1] = (val >> e->shift_r) & (bitmask - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 return 0;
464}
465
Takashi Iwaiac519022007-02-22 12:58:27 +0100466static int snd_ac97_put_enum_double(struct snd_kcontrol *kcontrol,
467 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Takashi Iwaiee423812005-11-17 14:21:36 +0100469 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
471 unsigned short val;
Takashi Iwai5f0dccf2005-04-07 15:53:20 +0200472 unsigned short mask, bitmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Sergey Vlasovb16760b2005-04-25 11:22:20 +0200474 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
Takashi Iwai5f0dccf2005-04-07 15:53:20 +0200475 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
477 return -EINVAL;
478 val = ucontrol->value.enumerated.item[0] << e->shift_l;
Takashi Iwai5f0dccf2005-04-07 15:53:20 +0200479 mask = (bitmask - 1) << e->shift_l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (e->shift_l != e->shift_r) {
481 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
482 return -EINVAL;
483 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Takashi Iwai5f0dccf2005-04-07 15:53:20 +0200484 mask |= (bitmask - 1) << e->shift_r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 return snd_ac97_update_bits(ac97, e->reg, mask, val);
487}
488
489/* save/restore ac97 v2.3 paging */
Takashi Iwaiee423812005-11-17 14:21:36 +0100490static int snd_ac97_page_save(struct snd_ac97 *ac97, int reg, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 int page_save = -1;
493 if ((kcontrol->private_value & (1<<25)) &&
494 (ac97->ext_id & AC97_EI_REV_MASK) >= AC97_EI_REV_23 &&
495 (reg >= 0x60 && reg < 0x70)) {
496 unsigned short page = (kcontrol->private_value >> 26) & 0x0f;
Ingo Molnar62932df2006-01-16 16:34:20 +0100497 mutex_lock(&ac97->page_mutex); /* lock paging */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 page_save = snd_ac97_read(ac97, AC97_INT_PAGING) & AC97_PAGE_MASK;
499 snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page);
500 }
501 return page_save;
502}
503
Takashi Iwaiee423812005-11-17 14:21:36 +0100504static void snd_ac97_page_restore(struct snd_ac97 *ac97, int page_save)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 if (page_save >= 0) {
507 snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page_save);
Ingo Molnar62932df2006-01-16 16:34:20 +0100508 mutex_unlock(&ac97->page_mutex); /* unlock paging */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510}
511
512/* volume and switch controls */
Takashi Iwaiac519022007-02-22 12:58:27 +0100513static int snd_ac97_info_volsw(struct snd_kcontrol *kcontrol,
514 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 int mask = (kcontrol->private_value >> 16) & 0xff;
517 int shift = (kcontrol->private_value >> 8) & 0x0f;
518 int rshift = (kcontrol->private_value >> 12) & 0x0f;
519
520 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
521 uinfo->count = shift == rshift ? 1 : 2;
522 uinfo->value.integer.min = 0;
523 uinfo->value.integer.max = mask;
524 return 0;
525}
526
Takashi Iwaiac519022007-02-22 12:58:27 +0100527static int snd_ac97_get_volsw(struct snd_kcontrol *kcontrol,
528 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Takashi Iwaiee423812005-11-17 14:21:36 +0100530 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int reg = kcontrol->private_value & 0xff;
532 int shift = (kcontrol->private_value >> 8) & 0x0f;
533 int rshift = (kcontrol->private_value >> 12) & 0x0f;
534 int mask = (kcontrol->private_value >> 16) & 0xff;
535 int invert = (kcontrol->private_value >> 24) & 0x01;
536 int page_save;
537
538 page_save = snd_ac97_page_save(ac97, reg, kcontrol);
539 ucontrol->value.integer.value[0] = (snd_ac97_read_cache(ac97, reg) >> shift) & mask;
540 if (shift != rshift)
541 ucontrol->value.integer.value[1] = (snd_ac97_read_cache(ac97, reg) >> rshift) & mask;
542 if (invert) {
543 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
544 if (shift != rshift)
545 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
546 }
547 snd_ac97_page_restore(ac97, page_save);
548 return 0;
549}
550
Takashi Iwaiac519022007-02-22 12:58:27 +0100551static int snd_ac97_put_volsw(struct snd_kcontrol *kcontrol,
552 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Takashi Iwaiee423812005-11-17 14:21:36 +0100554 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int reg = kcontrol->private_value & 0xff;
556 int shift = (kcontrol->private_value >> 8) & 0x0f;
557 int rshift = (kcontrol->private_value >> 12) & 0x0f;
558 int mask = (kcontrol->private_value >> 16) & 0xff;
559 int invert = (kcontrol->private_value >> 24) & 0x01;
560 int err, page_save;
561 unsigned short val, val2, val_mask;
562
563 page_save = snd_ac97_page_save(ac97, reg, kcontrol);
564 val = (ucontrol->value.integer.value[0] & mask);
565 if (invert)
566 val = mask - val;
567 val_mask = mask << shift;
568 val = val << shift;
569 if (shift != rshift) {
570 val2 = (ucontrol->value.integer.value[1] & mask);
571 if (invert)
572 val2 = mask - val2;
573 val_mask |= mask << rshift;
574 val |= val2 << rshift;
575 }
576 err = snd_ac97_update_bits(ac97, reg, val_mask, val);
577 snd_ac97_page_restore(ac97, page_save);
Takashi Iwai6dbe6622006-06-27 18:28:53 +0200578#ifdef CONFIG_SND_AC97_POWER_SAVE
579 /* check analog mixer power-down */
580 if ((val_mask & 0x8000) &&
581 (kcontrol->private_value & (1<<30))) {
582 if (val & 0x8000)
583 ac97->power_up &= ~(1 << (reg>>1));
584 else
585 ac97->power_up |= 1 << (reg>>1);
Takashi Iwaida43deb2006-10-19 15:20:08 +0200586 update_power_regs(ac97);
Takashi Iwai6dbe6622006-06-27 18:28:53 +0200587 }
588#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return err;
590}
591
Takashi Iwaiee423812005-11-17 14:21:36 +0100592static const struct snd_kcontrol_new snd_ac97_controls_master_mono[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593AC97_SINGLE("Master Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
594AC97_SINGLE("Master Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1)
595};
596
Takashi Iwaiee423812005-11-17 14:21:36 +0100597static const struct snd_kcontrol_new snd_ac97_controls_tone[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598AC97_SINGLE("Tone Control - Bass", AC97_MASTER_TONE, 8, 15, 1),
599AC97_SINGLE("Tone Control - Treble", AC97_MASTER_TONE, 0, 15, 1)
600};
601
Takashi Iwaiee423812005-11-17 14:21:36 +0100602static const struct snd_kcontrol_new snd_ac97_controls_pc_beep[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603AC97_SINGLE("PC Speaker Playback Switch", AC97_PC_BEEP, 15, 1, 1),
604AC97_SINGLE("PC Speaker Playback Volume", AC97_PC_BEEP, 1, 15, 1)
605};
606
Takashi Iwaiee423812005-11-17 14:21:36 +0100607static const struct snd_kcontrol_new snd_ac97_controls_mic_boost =
Takashi Iwai7931e2a92006-08-31 21:27:50 -0700608 AC97_SINGLE("Mic Boost (+20dB)", AC97_MIC, 6, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610
611static const char* std_rec_sel[] = {"Mic", "CD", "Video", "Aux", "Line", "Mix", "Mix Mono", "Phone"};
612static const char* std_3d_path[] = {"pre 3D", "post 3D"};
613static const char* std_mix[] = {"Mix", "Mic"};
614static const char* std_mic[] = {"Mic1", "Mic2"};
615
616static const struct ac97_enum std_enum[] = {
617AC97_ENUM_DOUBLE(AC97_REC_SEL, 8, 0, 8, std_rec_sel),
618AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, std_3d_path),
619AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 9, 2, std_mix),
620AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 8, 2, std_mic),
621};
622
Takashi Iwaiee423812005-11-17 14:21:36 +0100623static const struct snd_kcontrol_new snd_ac97_control_capture_src =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624AC97_ENUM("Capture Source", std_enum[0]);
625
Takashi Iwaiee423812005-11-17 14:21:36 +0100626static const struct snd_kcontrol_new snd_ac97_control_capture_vol =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627AC97_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0);
628
Takashi Iwaiee423812005-11-17 14:21:36 +0100629static const struct snd_kcontrol_new snd_ac97_controls_mic_capture[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630AC97_SINGLE("Mic Capture Switch", AC97_REC_GAIN_MIC, 15, 1, 1),
631AC97_SINGLE("Mic Capture Volume", AC97_REC_GAIN_MIC, 0, 15, 0)
632};
633
Takashi Iwaiee423812005-11-17 14:21:36 +0100634enum {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 AC97_GENERAL_PCM_OUT = 0,
636 AC97_GENERAL_STEREO_ENHANCEMENT,
637 AC97_GENERAL_3D,
638 AC97_GENERAL_LOUDNESS,
639 AC97_GENERAL_MONO,
640 AC97_GENERAL_MIC,
641 AC97_GENERAL_LOOPBACK
Takashi Iwaiee423812005-11-17 14:21:36 +0100642};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Takashi Iwaiee423812005-11-17 14:21:36 +0100644static const struct snd_kcontrol_new snd_ac97_controls_general[7] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645AC97_ENUM("PCM Out Path & Mute", std_enum[1]),
646AC97_SINGLE("Simulated Stereo Enhancement", AC97_GENERAL_PURPOSE, 14, 1, 0),
647AC97_SINGLE("3D Control - Switch", AC97_GENERAL_PURPOSE, 13, 1, 0),
648AC97_SINGLE("Loudness (bass boost)", AC97_GENERAL_PURPOSE, 12, 1, 0),
649AC97_ENUM("Mono Output Select", std_enum[2]),
Takashi Iwai7931e2a92006-08-31 21:27:50 -0700650AC97_ENUM("Mic Select", std_enum[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651AC97_SINGLE("ADC/DAC Loopback", AC97_GENERAL_PURPOSE, 7, 1, 0)
652};
653
Takashi Iwaiac519022007-02-22 12:58:27 +0100654static const struct snd_kcontrol_new snd_ac97_controls_3d[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655AC97_SINGLE("3D Control - Center", AC97_3D_CONTROL, 8, 15, 0),
656AC97_SINGLE("3D Control - Depth", AC97_3D_CONTROL, 0, 15, 0)
657};
658
Takashi Iwaiee423812005-11-17 14:21:36 +0100659static const struct snd_kcontrol_new snd_ac97_controls_center[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660AC97_SINGLE("Center Playback Switch", AC97_CENTER_LFE_MASTER, 7, 1, 1),
661AC97_SINGLE("Center Playback Volume", AC97_CENTER_LFE_MASTER, 0, 31, 1)
662};
663
Takashi Iwaiee423812005-11-17 14:21:36 +0100664static const struct snd_kcontrol_new snd_ac97_controls_lfe[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665AC97_SINGLE("LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 1, 1),
666AC97_SINGLE("LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 31, 1)
667};
668
Takashi Iwaiee423812005-11-17 14:21:36 +0100669static const struct snd_kcontrol_new snd_ac97_control_eapd =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670AC97_SINGLE("External Amplifier", AC97_POWERDOWN, 15, 1, 1);
671
Takashi Iwaiee423812005-11-17 14:21:36 +0100672static const struct snd_kcontrol_new snd_ac97_controls_modem_switches[2] = {
Sasha Khapyorsky87d61c22005-05-29 15:08:23 +0200673AC97_SINGLE("Off-hook Switch", AC97_GPIO_STATUS, 0, 1, 0),
674AC97_SINGLE("Caller ID Switch", AC97_GPIO_STATUS, 2, 1, 0)
675};
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677/* change the existing EAPD control as inverted */
Takashi Iwaiee423812005-11-17 14:21:36 +0100678static void set_inv_eapd(struct snd_ac97 *ac97, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 kctl->private_value = AC97_SINGLE_VALUE(AC97_POWERDOWN, 15, 1, 0);
681 snd_ac97_update_bits(ac97, AC97_POWERDOWN, (1<<15), (1<<15)); /* EAPD up */
682 ac97->scaps |= AC97_SCAP_INV_EAPD;
683}
684
Takashi Iwaiee423812005-11-17 14:21:36 +0100685static int snd_ac97_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
688 uinfo->count = 1;
689 return 0;
690}
691
Takashi Iwaiee423812005-11-17 14:21:36 +0100692static int snd_ac97_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
695 IEC958_AES0_NONAUDIO |
696 IEC958_AES0_CON_EMPHASIS_5015 |
697 IEC958_AES0_CON_NOT_COPYRIGHT;
698 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
699 IEC958_AES1_CON_ORIGINAL;
700 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
701 return 0;
702}
703
Takashi Iwaiee423812005-11-17 14:21:36 +0100704static int snd_ac97_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 /* FIXME: AC'97 spec doesn't say which bits are used for what */
707 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
708 IEC958_AES0_NONAUDIO |
709 IEC958_AES0_PRO_FS |
710 IEC958_AES0_PRO_EMPHASIS_5015;
711 return 0;
712}
713
Takashi Iwaiee423812005-11-17 14:21:36 +0100714static int snd_ac97_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Takashi Iwaiee423812005-11-17 14:21:36 +0100716 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Ingo Molnar62932df2006-01-16 16:34:20 +0100718 mutex_lock(&ac97->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 ucontrol->value.iec958.status[0] = ac97->spdif_status & 0xff;
720 ucontrol->value.iec958.status[1] = (ac97->spdif_status >> 8) & 0xff;
721 ucontrol->value.iec958.status[2] = (ac97->spdif_status >> 16) & 0xff;
722 ucontrol->value.iec958.status[3] = (ac97->spdif_status >> 24) & 0xff;
Ingo Molnar62932df2006-01-16 16:34:20 +0100723 mutex_unlock(&ac97->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return 0;
725}
726
Takashi Iwaiee423812005-11-17 14:21:36 +0100727static int snd_ac97_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Takashi Iwaiee423812005-11-17 14:21:36 +0100729 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 unsigned int new = 0;
731 unsigned short val = 0;
732 int change;
733
734 new = val = ucontrol->value.iec958.status[0] & (IEC958_AES0_PROFESSIONAL|IEC958_AES0_NONAUDIO);
735 if (ucontrol->value.iec958.status[0] & IEC958_AES0_PROFESSIONAL) {
736 new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_PRO_FS|IEC958_AES0_PRO_EMPHASIS_5015);
737 switch (new & IEC958_AES0_PRO_FS) {
738 case IEC958_AES0_PRO_FS_44100: val |= 0<<12; break;
739 case IEC958_AES0_PRO_FS_48000: val |= 2<<12; break;
740 case IEC958_AES0_PRO_FS_32000: val |= 3<<12; break;
741 default: val |= 1<<12; break;
742 }
743 if ((new & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
744 val |= 1<<3;
745 } else {
746 new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT);
747 new |= ((ucontrol->value.iec958.status[1] & (IEC958_AES1_CON_CATEGORY|IEC958_AES1_CON_ORIGINAL)) << 8);
748 new |= ((ucontrol->value.iec958.status[3] & IEC958_AES3_CON_FS) << 24);
749 if ((new & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
750 val |= 1<<3;
751 if (!(new & IEC958_AES0_CON_NOT_COPYRIGHT))
752 val |= 1<<2;
753 val |= ((new >> 8) & 0xff) << 4; // category + original
754 switch ((new >> 24) & 0xff) {
755 case IEC958_AES3_CON_FS_44100: val |= 0<<12; break;
756 case IEC958_AES3_CON_FS_48000: val |= 2<<12; break;
757 case IEC958_AES3_CON_FS_32000: val |= 3<<12; break;
758 default: val |= 1<<12; break;
759 }
760 }
761
Ingo Molnar62932df2006-01-16 16:34:20 +0100762 mutex_lock(&ac97->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 change = ac97->spdif_status != new;
764 ac97->spdif_status = new;
765
766 if (ac97->flags & AC97_CS_SPDIF) {
767 int x = (val >> 12) & 0x03;
768 switch (x) {
769 case 0: x = 1; break; // 44.1
770 case 2: x = 0; break; // 48.0
771 default: x = 0; break; // illegal.
772 }
773 change |= snd_ac97_update_bits_nolock(ac97, AC97_CSR_SPDIF, 0x3fff, ((val & 0xcfff) | (x << 12)));
774 } else if (ac97->flags & AC97_CX_SPDIF) {
775 int v;
776 v = new & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT) ? 0 : AC97_CXR_COPYRGT;
777 v |= new & IEC958_AES0_NONAUDIO ? AC97_CXR_SPDIF_AC3 : AC97_CXR_SPDIF_PCM;
778 change |= snd_ac97_update_bits_nolock(ac97, AC97_CXR_AUDIO_MISC,
779 AC97_CXR_SPDIF_MASK | AC97_CXR_COPYRGT,
780 v);
Keita Maehara43115f52007-09-19 14:29:37 +0200781 } else if (ac97->id == AC97_ID_YMF743) {
782 change |= snd_ac97_update_bits_nolock(ac97,
783 AC97_YMF7X3_DIT_CTRL,
784 0xff38,
785 ((val << 4) & 0xff00) |
786 ((val << 2) & 0x0038));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 } else {
788 unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
789 snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
790
791 change |= snd_ac97_update_bits_nolock(ac97, AC97_SPDIF, 0x3fff, val);
792 if (extst & AC97_EA_SPDIF) {
793 snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
794 }
795 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100796 mutex_unlock(&ac97->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 return change;
799}
800
Takashi Iwaiee423812005-11-17 14:21:36 +0100801static int snd_ac97_put_spsa(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Takashi Iwaiee423812005-11-17 14:21:36 +0100803 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 int reg = kcontrol->private_value & 0xff;
805 int shift = (kcontrol->private_value >> 8) & 0xff;
806 int mask = (kcontrol->private_value >> 16) & 0xff;
807 // int invert = (kcontrol->private_value >> 24) & 0xff;
808 unsigned short value, old, new;
809 int change;
810
811 value = (ucontrol->value.integer.value[0] & mask);
812
Ingo Molnar62932df2006-01-16 16:34:20 +0100813 mutex_lock(&ac97->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 mask <<= shift;
815 value <<= shift;
816 old = snd_ac97_read_cache(ac97, reg);
817 new = (old & ~mask) | value;
818 change = old != new;
819
820 if (change) {
821 unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
822 snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
823 change = snd_ac97_update_bits_nolock(ac97, reg, mask, value);
824 if (extst & AC97_EA_SPDIF)
825 snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
826 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100827 mutex_unlock(&ac97->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return change;
829}
830
Takashi Iwaiac519022007-02-22 12:58:27 +0100831static const struct snd_kcontrol_new snd_ac97_controls_spdif[5] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 {
833 .access = SNDRV_CTL_ELEM_ACCESS_READ,
834 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
835 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
836 .info = snd_ac97_spdif_mask_info,
837 .get = snd_ac97_spdif_cmask_get,
838 },
839 {
840 .access = SNDRV_CTL_ELEM_ACCESS_READ,
841 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
842 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
843 .info = snd_ac97_spdif_mask_info,
844 .get = snd_ac97_spdif_pmask_get,
845 },
846 {
847 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
848 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
849 .info = snd_ac97_spdif_mask_info,
850 .get = snd_ac97_spdif_default_get,
851 .put = snd_ac97_spdif_default_put,
852 },
853
854 AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),AC97_EXTENDED_STATUS, 2, 1, 0),
855 {
856 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
857 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "AC97-SPSA",
858 .info = snd_ac97_info_volsw,
859 .get = snd_ac97_get_volsw,
860 .put = snd_ac97_put_spsa,
861 .private_value = AC97_SINGLE_VALUE(AC97_EXTENDED_STATUS, 4, 3, 0)
862 },
863};
864
865#define AD18XX_PCM_BITS(xname, codec, lshift, rshift, mask) \
866{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_bits, \
867 .get = snd_ac97_ad18xx_pcm_get_bits, .put = snd_ac97_ad18xx_pcm_put_bits, \
868 .private_value = (codec) | ((lshift) << 8) | ((rshift) << 12) | ((mask) << 16) }
869
Takashi Iwaiee423812005-11-17 14:21:36 +0100870static int snd_ac97_ad18xx_pcm_info_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Takashi Iwaiee423812005-11-17 14:21:36 +0100872 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 int mask = (kcontrol->private_value >> 16) & 0x0f;
874 int lshift = (kcontrol->private_value >> 8) & 0x0f;
875 int rshift = (kcontrol->private_value >> 12) & 0x0f;
876
877 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
878 if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
879 uinfo->count = 2;
880 else
881 uinfo->count = 1;
882 uinfo->value.integer.min = 0;
883 uinfo->value.integer.max = mask;
884 return 0;
885}
886
Takashi Iwaiee423812005-11-17 14:21:36 +0100887static int snd_ac97_ad18xx_pcm_get_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Takashi Iwaiee423812005-11-17 14:21:36 +0100889 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 int codec = kcontrol->private_value & 3;
891 int lshift = (kcontrol->private_value >> 8) & 0x0f;
892 int rshift = (kcontrol->private_value >> 12) & 0x0f;
893 int mask = (kcontrol->private_value >> 16) & 0xff;
894
895 ucontrol->value.integer.value[0] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> lshift) & mask);
896 if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
897 ucontrol->value.integer.value[1] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> rshift) & mask);
898 return 0;
899}
900
Takashi Iwaiee423812005-11-17 14:21:36 +0100901static int snd_ac97_ad18xx_pcm_put_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Takashi Iwaiee423812005-11-17 14:21:36 +0100903 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 int codec = kcontrol->private_value & 3;
905 int lshift = (kcontrol->private_value >> 8) & 0x0f;
906 int rshift = (kcontrol->private_value >> 12) & 0x0f;
907 int mask = (kcontrol->private_value >> 16) & 0xff;
908 unsigned short val, valmask;
909
910 val = (mask - (ucontrol->value.integer.value[0] & mask)) << lshift;
911 valmask = mask << lshift;
912 if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES)) {
913 val |= (mask - (ucontrol->value.integer.value[1] & mask)) << rshift;
914 valmask |= mask << rshift;
915 }
916 return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, valmask, val);
917}
918
919#define AD18XX_PCM_VOLUME(xname, codec) \
920{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_volume, \
921 .get = snd_ac97_ad18xx_pcm_get_volume, .put = snd_ac97_ad18xx_pcm_put_volume, \
922 .private_value = codec }
923
Takashi Iwaiee423812005-11-17 14:21:36 +0100924static int snd_ac97_ad18xx_pcm_info_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
927 uinfo->count = 2;
928 uinfo->value.integer.min = 0;
929 uinfo->value.integer.max = 31;
930 return 0;
931}
932
Takashi Iwaiee423812005-11-17 14:21:36 +0100933static int snd_ac97_ad18xx_pcm_get_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Takashi Iwaiee423812005-11-17 14:21:36 +0100935 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int codec = kcontrol->private_value & 3;
937
Ingo Molnar62932df2006-01-16 16:34:20 +0100938 mutex_lock(&ac97->page_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 ucontrol->value.integer.value[0] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 0) & 31);
940 ucontrol->value.integer.value[1] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 8) & 31);
Ingo Molnar62932df2006-01-16 16:34:20 +0100941 mutex_unlock(&ac97->page_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return 0;
943}
944
Takashi Iwaiee423812005-11-17 14:21:36 +0100945static int snd_ac97_ad18xx_pcm_put_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Takashi Iwaiee423812005-11-17 14:21:36 +0100947 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 int codec = kcontrol->private_value & 3;
949 unsigned short val1, val2;
950
951 val1 = 31 - (ucontrol->value.integer.value[0] & 31);
952 val2 = 31 - (ucontrol->value.integer.value[1] & 31);
953 return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, 0x1f1f, (val1 << 8) | val2);
954}
955
Takashi Iwaiee423812005-11-17 14:21:36 +0100956static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_pcm[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957AD18XX_PCM_BITS("PCM Playback Switch", 0, 15, 7, 1),
958AD18XX_PCM_VOLUME("PCM Playback Volume", 0)
959};
960
Takashi Iwaiee423812005-11-17 14:21:36 +0100961static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_surround[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962AD18XX_PCM_BITS("Surround Playback Switch", 1, 15, 7, 1),
963AD18XX_PCM_VOLUME("Surround Playback Volume", 1)
964};
965
Takashi Iwaiee423812005-11-17 14:21:36 +0100966static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_center[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967AD18XX_PCM_BITS("Center Playback Switch", 2, 15, 15, 1),
968AD18XX_PCM_BITS("Center Playback Volume", 2, 8, 8, 31)
969};
970
Takashi Iwaiee423812005-11-17 14:21:36 +0100971static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_lfe[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972AD18XX_PCM_BITS("LFE Playback Switch", 2, 7, 7, 1),
973AD18XX_PCM_BITS("LFE Playback Volume", 2, 0, 0, 31)
974};
975
976/*
977 *
978 */
979
Takashi Iwaiee423812005-11-17 14:21:36 +0100980static void snd_ac97_powerdown(struct snd_ac97 *ac97);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Takashi Iwaiee423812005-11-17 14:21:36 +0100982static int snd_ac97_bus_free(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 if (bus) {
985 snd_ac97_bus_proc_done(bus);
986 kfree(bus->pcms);
987 if (bus->private_free)
988 bus->private_free(bus);
989 kfree(bus);
990 }
991 return 0;
992}
993
Takashi Iwaiee423812005-11-17 14:21:36 +0100994static int snd_ac97_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Takashi Iwaiee423812005-11-17 14:21:36 +0100996 struct snd_ac97_bus *bus = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return snd_ac97_bus_free(bus);
998}
999
Takashi Iwaiee423812005-11-17 14:21:36 +01001000static int snd_ac97_free(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 if (ac97) {
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001003#ifdef CONFIG_SND_AC97_POWER_SAVE
Takashi Iwaif1a63a32006-10-24 18:25:29 +02001004 cancel_delayed_work(&ac97->power_work);
Takashi Iwai3b6baa52007-01-31 14:34:38 +01001005 flush_scheduled_work();
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001006#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 snd_ac97_proc_done(ac97);
Sasha Khapyorsky2ba71972005-09-29 12:58:24 +02001008 if (ac97->bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 ac97->bus->codec[ac97->num] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (ac97->private_free)
1011 ac97->private_free(ac97);
1012 kfree(ac97);
1013 }
1014 return 0;
1015}
1016
Takashi Iwaiee423812005-11-17 14:21:36 +01001017static int snd_ac97_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Takashi Iwaiee423812005-11-17 14:21:36 +01001019 struct snd_ac97 *ac97 = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 snd_ac97_powerdown(ac97); /* for avoiding click noises during shut down */
1021 return snd_ac97_free(ac97);
1022}
1023
Takashi Iwaiee423812005-11-17 14:21:36 +01001024static int snd_ac97_try_volume_mix(struct snd_ac97 * ac97, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 unsigned short val, mask = 0x8000;
1027
1028 if (! snd_ac97_valid_reg(ac97, reg))
1029 return 0;
1030
1031 switch (reg) {
1032 case AC97_MASTER_TONE:
1033 return ac97->caps & 0x04 ? 1 : 0;
1034 case AC97_HEADPHONE:
1035 return ac97->caps & 0x10 ? 1 : 0;
1036 case AC97_REC_GAIN_MIC:
1037 return ac97->caps & 0x01 ? 1 : 0;
1038 case AC97_3D_CONTROL:
1039 if (ac97->caps & 0x7c00) {
1040 val = snd_ac97_read(ac97, reg);
1041 /* if nonzero - fixed and we can't set it */
1042 return val == 0;
1043 }
1044 return 0;
1045 case AC97_CENTER_LFE_MASTER: /* center */
1046 if ((ac97->ext_id & AC97_EI_CDAC) == 0)
1047 return 0;
1048 break;
1049 case AC97_CENTER_LFE_MASTER+1: /* lfe */
1050 if ((ac97->ext_id & AC97_EI_LDAC) == 0)
1051 return 0;
1052 reg = AC97_CENTER_LFE_MASTER;
1053 mask = 0x0080;
1054 break;
1055 case AC97_SURROUND_MASTER:
1056 if ((ac97->ext_id & AC97_EI_SDAC) == 0)
1057 return 0;
1058 break;
1059 }
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 val = snd_ac97_read(ac97, reg);
1062 if (!(val & mask)) {
1063 /* nothing seems to be here - mute flag is not set */
1064 /* try another test */
1065 snd_ac97_write_cache(ac97, reg, val | mask);
1066 val = snd_ac97_read(ac97, reg);
Takashi Iwaiee423812005-11-17 14:21:36 +01001067 val = snd_ac97_read(ac97, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (!(val & mask))
1069 return 0; /* nothing here */
1070 }
1071 return 1; /* success, useable */
1072}
1073
Takashi Iwaiee423812005-11-17 14:21:36 +01001074static void check_volume_resolution(struct snd_ac97 *ac97, int reg, unsigned char *lo_max, unsigned char *hi_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 unsigned short cbit[3] = { 0x20, 0x10, 0x01 };
1077 unsigned char max[3] = { 63, 31, 15 };
1078 int i;
1079
Takashi Iwai50dabc22006-02-09 11:45:20 +01001080 /* first look up the static resolution table */
1081 if (ac97->res_table) {
1082 const struct snd_ac97_res_table *tbl;
1083 for (tbl = ac97->res_table; tbl->reg; tbl++) {
1084 if (tbl->reg == reg) {
1085 *lo_max = tbl->bits & 0xff;
1086 *hi_max = (tbl->bits >> 8) & 0xff;
1087 return;
1088 }
1089 }
1090 }
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 *lo_max = *hi_max = 0;
1093 for (i = 0 ; i < ARRAY_SIZE(cbit); i++) {
1094 unsigned short val;
1095 snd_ac97_write(ac97, reg, 0x8080 | cbit[i] | (cbit[i] << 8));
James Courtier-Duttonc9eab122005-07-10 12:04:29 +02001096 /* Do the read twice due to buffers on some ac97 codecs.
Michael Opdenacker59c51592007-05-09 08:57:56 +02001097 * e.g. The STAC9704 returns exactly what you wrote to the register
James Courtier-Duttonc9eab122005-07-10 12:04:29 +02001098 * if you read it immediately. This causes the detect routine to fail.
1099 */
1100 val = snd_ac97_read(ac97, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 val = snd_ac97_read(ac97, reg);
Takashi Iwaia2142672005-03-29 16:33:28 +02001102 if (! *lo_max && (val & 0x7f) == cbit[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 *lo_max = max[i];
Takashi Iwaia2142672005-03-29 16:33:28 +02001104 if (! *hi_max && ((val >> 8) & 0x7f) == cbit[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 *hi_max = max[i];
1106 if (*lo_max && *hi_max)
1107 break;
1108 }
1109}
1110
Takashi Iwaiac519022007-02-22 12:58:27 +01001111static int snd_ac97_try_bit(struct snd_ac97 * ac97, int reg, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 unsigned short mask, val, orig, res;
1114
1115 mask = 1 << bit;
1116 orig = snd_ac97_read(ac97, reg);
1117 val = orig ^ mask;
1118 snd_ac97_write(ac97, reg, val);
1119 res = snd_ac97_read(ac97, reg);
1120 snd_ac97_write_cache(ac97, reg, orig);
1121 return res == val;
1122}
1123
1124/* check the volume resolution of center/lfe */
Takashi Iwaiee423812005-11-17 14:21:36 +01001125static void snd_ac97_change_volume_params2(struct snd_ac97 * ac97, int reg, int shift, unsigned char *max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 unsigned short val, val1;
1128
1129 *max = 63;
1130 val = 0x8080 | (0x20 << shift);
1131 snd_ac97_write(ac97, reg, val);
1132 val1 = snd_ac97_read(ac97, reg);
1133 if (val != val1) {
1134 *max = 31;
1135 }
1136 /* reset volume to zero */
1137 snd_ac97_write_cache(ac97, reg, 0x8080);
1138}
1139
1140static inline int printable(unsigned int x)
1141{
1142 x &= 0xff;
1143 if (x < ' ' || x >= 0x71) {
1144 if (x <= 0x89)
1145 return x - 0x71 + 'A';
1146 return '?';
1147 }
1148 return x;
1149}
1150
Takashi Iwaiac519022007-02-22 12:58:27 +01001151static struct snd_kcontrol *snd_ac97_cnew(const struct snd_kcontrol_new *_template,
1152 struct snd_ac97 * ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Takashi Iwaiee423812005-11-17 14:21:36 +01001154 struct snd_kcontrol_new template;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 memcpy(&template, _template, sizeof(template));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 template.index = ac97->num;
1157 return snd_ctl_new1(&template, ac97);
1158}
1159
1160/*
1161 * create mute switch(es) for normal stereo controls
1162 */
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001163static int snd_ac97_cmute_new_stereo(struct snd_card *card, char *name, int reg,
1164 int check_stereo, int check_amix,
1165 struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Takashi Iwaiee423812005-11-17 14:21:36 +01001167 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 int err;
1169 unsigned short val, val1, mute_mask;
1170
1171 if (! snd_ac97_valid_reg(ac97, reg))
1172 return 0;
1173
1174 mute_mask = 0x8000;
1175 val = snd_ac97_read(ac97, reg);
1176 if (check_stereo || (ac97->flags & AC97_STEREO_MUTES)) {
1177 /* check whether both mute bits work */
1178 val1 = val | 0x8080;
1179 snd_ac97_write(ac97, reg, val1);
1180 if (val1 == snd_ac97_read(ac97, reg))
1181 mute_mask = 0x8080;
1182 }
1183 if (mute_mask == 0x8080) {
Takashi Iwaiee423812005-11-17 14:21:36 +01001184 struct snd_kcontrol_new tmp = AC97_DOUBLE(name, reg, 15, 7, 1, 1);
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001185 if (check_amix)
1186 tmp.private_value |= (1 << 30);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 tmp.index = ac97->num;
1188 kctl = snd_ctl_new1(&tmp, ac97);
1189 } else {
Takashi Iwaiee423812005-11-17 14:21:36 +01001190 struct snd_kcontrol_new tmp = AC97_SINGLE(name, reg, 15, 1, 1);
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001191 if (check_amix)
1192 tmp.private_value |= (1 << 30);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 tmp.index = ac97->num;
1194 kctl = snd_ctl_new1(&tmp, ac97);
1195 }
1196 err = snd_ctl_add(card, kctl);
1197 if (err < 0)
1198 return err;
1199 /* mute as default */
1200 snd_ac97_write_cache(ac97, reg, val | mute_mask);
1201 return 0;
1202}
1203
1204/*
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001205 * set dB information
1206 */
Takashi Iwai0cb29ea2007-01-29 15:33:49 +01001207static const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0);
1208static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
1209static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
1210static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
1211static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001212
Takashi Iwai0cb29ea2007-01-29 15:33:49 +01001213static const unsigned int *find_db_scale(unsigned int maxval)
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001214{
1215 switch (maxval) {
1216 case 0x0f: return db_scale_4bit;
1217 case 0x1f: return db_scale_5bit;
1218 case 0x3f: return db_scale_6bit;
1219 }
1220 return NULL;
1221}
1222
Takashi Iwai0cb29ea2007-01-29 15:33:49 +01001223static void set_tlv_db_scale(struct snd_kcontrol *kctl, const unsigned int *tlv)
1224{
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001225 kctl->tlv.p = tlv;
1226 if (tlv)
1227 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1228}
1229
1230/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 * create a volume for normal stereo/mono controls
1232 */
Takashi Iwaiee423812005-11-17 14:21:36 +01001233static int snd_ac97_cvol_new(struct snd_card *card, char *name, int reg, unsigned int lo_max,
1234 unsigned int hi_max, struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 int err;
Takashi Iwaiee423812005-11-17 14:21:36 +01001237 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 if (! snd_ac97_valid_reg(ac97, reg))
1240 return 0;
1241 if (hi_max) {
1242 /* invert */
Takashi Iwaiee423812005-11-17 14:21:36 +01001243 struct snd_kcontrol_new tmp = AC97_DOUBLE(name, reg, 8, 0, lo_max, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 tmp.index = ac97->num;
1245 kctl = snd_ctl_new1(&tmp, ac97);
1246 } else {
1247 /* invert */
Takashi Iwaiee423812005-11-17 14:21:36 +01001248 struct snd_kcontrol_new tmp = AC97_SINGLE(name, reg, 0, lo_max, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 tmp.index = ac97->num;
1250 kctl = snd_ctl_new1(&tmp, ac97);
1251 }
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001252 if (reg >= AC97_PHONE && reg <= AC97_PCM)
1253 set_tlv_db_scale(kctl, db_scale_5bit_12db_max);
1254 else
1255 set_tlv_db_scale(kctl, find_db_scale(lo_max));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 err = snd_ctl_add(card, kctl);
1257 if (err < 0)
1258 return err;
1259 snd_ac97_write_cache(ac97, reg,
1260 (snd_ac97_read(ac97, reg) & 0x8080) |
1261 lo_max | (hi_max << 8));
1262 return 0;
1263}
1264
1265/*
1266 * create a mute-switch and a volume for normal stereo/mono controls
1267 */
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001268static int snd_ac97_cmix_new_stereo(struct snd_card *card, const char *pfx,
1269 int reg, int check_stereo, int check_amix,
1270 struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 int err;
1273 char name[44];
1274 unsigned char lo_max, hi_max;
1275
1276 if (! snd_ac97_valid_reg(ac97, reg))
1277 return 0;
1278
1279 if (snd_ac97_try_bit(ac97, reg, 15)) {
1280 sprintf(name, "%s Switch", pfx);
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001281 if ((err = snd_ac97_cmute_new_stereo(card, name, reg,
1282 check_stereo, check_amix,
1283 ac97)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return err;
1285 }
1286 check_volume_resolution(ac97, reg, &lo_max, &hi_max);
1287 if (lo_max) {
1288 sprintf(name, "%s Volume", pfx);
1289 if ((err = snd_ac97_cvol_new(card, name, reg, lo_max, hi_max, ac97)) < 0)
1290 return err;
1291 }
1292 return 0;
1293}
1294
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001295#define snd_ac97_cmix_new(card, pfx, reg, acheck, ac97) \
1296 snd_ac97_cmix_new_stereo(card, pfx, reg, 0, acheck, ac97)
1297#define snd_ac97_cmute_new(card, name, reg, acheck, ac97) \
1298 snd_ac97_cmute_new_stereo(card, name, reg, 0, acheck, ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Takashi Iwaiee423812005-11-17 14:21:36 +01001300static unsigned int snd_ac97_determine_spdif_rates(struct snd_ac97 *ac97);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Takashi Iwaiee423812005-11-17 14:21:36 +01001302static int snd_ac97_mixer_build(struct snd_ac97 * ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Takashi Iwaiee423812005-11-17 14:21:36 +01001304 struct snd_card *card = ac97->bus->card;
1305 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int err;
1307 unsigned int idx;
1308 unsigned char max;
1309
1310 /* build master controls */
1311 /* AD claims to remove this control from AD1887, although spec v2.2 does not allow this */
1312 if (snd_ac97_try_volume_mix(ac97, AC97_MASTER)) {
1313 if (ac97->flags & AC97_HAS_NO_MASTER_VOL)
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001314 err = snd_ac97_cmute_new(card, "Master Playback Switch",
1315 AC97_MASTER, 0, ac97);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 else
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001317 err = snd_ac97_cmix_new(card, "Master Playback",
1318 AC97_MASTER, 0, ac97);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (err < 0)
1320 return err;
1321 }
1322
1323 ac97->regs[AC97_CENTER_LFE_MASTER] = 0x8080;
1324
1325 /* build center controls */
Jaya Kumar70c5acb2006-06-12 10:08:02 +02001326 if ((snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER))
1327 && !(ac97->flags & AC97_AD_MULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_center[0], ac97))) < 0)
1329 return err;
1330 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_center[1], ac97))) < 0)
1331 return err;
1332 snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 0, &max);
1333 kctl->private_value &= ~(0xff << 16);
1334 kctl->private_value |= (int)max << 16;
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001335 set_tlv_db_scale(kctl, find_db_scale(max));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max);
1337 }
1338
1339 /* build LFE controls */
Jaya Kumar70c5acb2006-06-12 10:08:02 +02001340 if ((snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER+1))
1341 && !(ac97->flags & AC97_AD_MULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_lfe[0], ac97))) < 0)
1343 return err;
1344 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_lfe[1], ac97))) < 0)
1345 return err;
1346 snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 8, &max);
1347 kctl->private_value &= ~(0xff << 16);
1348 kctl->private_value |= (int)max << 16;
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001349 set_tlv_db_scale(kctl, find_db_scale(max));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max << 8);
1351 }
1352
1353 /* build surround controls */
Jaya Kumar70c5acb2006-06-12 10:08:02 +02001354 if ((snd_ac97_try_volume_mix(ac97, AC97_SURROUND_MASTER))
1355 && !(ac97->flags & AC97_AD_MULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /* Surround Master (0x38) is with stereo mutes */
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001357 if ((err = snd_ac97_cmix_new_stereo(card, "Surround Playback",
1358 AC97_SURROUND_MASTER, 1, 0,
1359 ac97)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return err;
1361 }
1362
1363 /* build headphone controls */
1364 if (snd_ac97_try_volume_mix(ac97, AC97_HEADPHONE)) {
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001365 if ((err = snd_ac97_cmix_new(card, "Headphone Playback",
1366 AC97_HEADPHONE, 0, ac97)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return err;
1368 }
1369
1370 /* build master mono controls */
1371 if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_MONO)) {
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001372 if ((err = snd_ac97_cmix_new(card, "Master Mono Playback",
1373 AC97_MASTER_MONO, 0, ac97)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return err;
1375 }
1376
1377 /* build master tone controls */
Liam Girdwood3998b702005-07-29 11:41:55 +02001378 if (!(ac97->flags & AC97_HAS_NO_TONE)) {
1379 if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_TONE)) {
1380 for (idx = 0; idx < 2; idx++) {
1381 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_tone[idx], ac97))) < 0)
1382 return err;
Keita Maehara43115f52007-09-19 14:29:37 +02001383 if (ac97->id == AC97_ID_YMF743 ||
1384 ac97->id == AC97_ID_YMF753) {
Liam Girdwood3998b702005-07-29 11:41:55 +02001385 kctl->private_value &= ~(0xff << 16);
1386 kctl->private_value |= 7 << 16;
1387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
Liam Girdwood3998b702005-07-29 11:41:55 +02001389 snd_ac97_write_cache(ac97, AC97_MASTER_TONE, 0x0f0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
1393 /* build PC Speaker controls */
1394 if (!(ac97->flags & AC97_HAS_NO_PC_BEEP) &&
1395 ((ac97->flags & AC97_HAS_PC_BEEP) ||
1396 snd_ac97_try_volume_mix(ac97, AC97_PC_BEEP))) {
1397 for (idx = 0; idx < 2; idx++)
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001398 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_pc_beep[idx], ac97))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return err;
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001400 set_tlv_db_scale(kctl, db_scale_4bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 snd_ac97_write_cache(ac97, AC97_PC_BEEP,
1402 snd_ac97_read(ac97, AC97_PC_BEEP) | 0x801e);
1403 }
1404
1405 /* build Phone controls */
1406 if (!(ac97->flags & AC97_HAS_NO_PHONE)) {
1407 if (snd_ac97_try_volume_mix(ac97, AC97_PHONE)) {
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001408 if ((err = snd_ac97_cmix_new(card, "Phone Playback",
1409 AC97_PHONE, 1, ac97)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return err;
1411 }
1412 }
1413
1414 /* build MIC controls */
Liam Girdwood3998b702005-07-29 11:41:55 +02001415 if (!(ac97->flags & AC97_HAS_NO_MIC)) {
1416 if (snd_ac97_try_volume_mix(ac97, AC97_MIC)) {
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001417 if ((err = snd_ac97_cmix_new(card, "Mic Playback",
1418 AC97_MIC, 1, ac97)) < 0)
Liam Girdwood3998b702005-07-29 11:41:55 +02001419 return err;
1420 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_mic_boost, ac97))) < 0)
1421 return err;
1422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
1425 /* build Line controls */
1426 if (snd_ac97_try_volume_mix(ac97, AC97_LINE)) {
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001427 if ((err = snd_ac97_cmix_new(card, "Line Playback",
1428 AC97_LINE, 1, ac97)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return err;
1430 }
1431
1432 /* build CD controls */
1433 if (!(ac97->flags & AC97_HAS_NO_CD)) {
1434 if (snd_ac97_try_volume_mix(ac97, AC97_CD)) {
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001435 if ((err = snd_ac97_cmix_new(card, "CD Playback",
1436 AC97_CD, 1, ac97)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return err;
1438 }
1439 }
1440
1441 /* build Video controls */
1442 if (!(ac97->flags & AC97_HAS_NO_VIDEO)) {
1443 if (snd_ac97_try_volume_mix(ac97, AC97_VIDEO)) {
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001444 if ((err = snd_ac97_cmix_new(card, "Video Playback",
1445 AC97_VIDEO, 1, ac97)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return err;
1447 }
1448 }
1449
1450 /* build Aux controls */
Rodolfo Giometti1459c782006-06-19 15:04:54 +02001451 if (!(ac97->flags & AC97_HAS_NO_AUX)) {
1452 if (snd_ac97_try_volume_mix(ac97, AC97_AUX)) {
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001453 if ((err = snd_ac97_cmix_new(card, "Aux Playback",
1454 AC97_AUX, 1, ac97)) < 0)
Rodolfo Giometti1459c782006-06-19 15:04:54 +02001455 return err;
1456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
1458
1459 /* build PCM controls */
1460 if (ac97->flags & AC97_AD_MULTI) {
1461 unsigned short init_val;
1462 if (ac97->flags & AC97_STEREO_MUTES)
1463 init_val = 0x9f9f;
1464 else
1465 init_val = 0x9f1f;
1466 for (idx = 0; idx < 2; idx++)
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001467 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_pcm[idx], ac97))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return err;
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001469 set_tlv_db_scale(kctl, db_scale_5bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 ac97->spec.ad18xx.pcmreg[0] = init_val;
1471 if (ac97->scaps & AC97_SCAP_SURROUND_DAC) {
1472 for (idx = 0; idx < 2; idx++)
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001473 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_surround[idx], ac97))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return err;
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001475 set_tlv_db_scale(kctl, db_scale_5bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 ac97->spec.ad18xx.pcmreg[1] = init_val;
1477 }
1478 if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC) {
1479 for (idx = 0; idx < 2; idx++)
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001480 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_center[idx], ac97))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return err;
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001482 set_tlv_db_scale(kctl, db_scale_5bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 for (idx = 0; idx < 2; idx++)
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001484 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_lfe[idx], ac97))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return err;
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001486 set_tlv_db_scale(kctl, db_scale_5bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 ac97->spec.ad18xx.pcmreg[2] = init_val;
1488 }
1489 snd_ac97_write_cache(ac97, AC97_PCM, init_val);
1490 } else {
Liam Girdwood3998b702005-07-29 11:41:55 +02001491 if (!(ac97->flags & AC97_HAS_NO_STD_PCM)) {
1492 if (ac97->flags & AC97_HAS_NO_PCM_VOL)
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001493 err = snd_ac97_cmute_new(card,
1494 "PCM Playback Switch",
1495 AC97_PCM, 0, ac97);
Liam Girdwood3998b702005-07-29 11:41:55 +02001496 else
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001497 err = snd_ac97_cmix_new(card, "PCM Playback",
1498 AC97_PCM, 0, ac97);
Liam Girdwood3998b702005-07-29 11:41:55 +02001499 if (err < 0)
1500 return err;
1501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503
1504 /* build Capture controls */
1505 if (!(ac97->flags & AC97_HAS_NO_REC_GAIN)) {
1506 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_control_capture_src, ac97))) < 0)
1507 return err;
1508 if (snd_ac97_try_bit(ac97, AC97_REC_GAIN, 15)) {
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001509 err = snd_ac97_cmute_new(card, "Capture Switch",
1510 AC97_REC_GAIN, 0, ac97);
1511 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return err;
1513 }
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001514 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_control_capture_vol, ac97))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return err;
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001516 set_tlv_db_scale(kctl, db_scale_rec_gain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 snd_ac97_write_cache(ac97, AC97_REC_SEL, 0x0000);
1518 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x0000);
1519 }
1520 /* build MIC Capture controls */
1521 if (snd_ac97_try_volume_mix(ac97, AC97_REC_GAIN_MIC)) {
1522 for (idx = 0; idx < 2; idx++)
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001523 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_mic_capture[idx], ac97))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return err;
Takashi Iwai2f3482f2006-08-21 18:44:31 +02001525 set_tlv_db_scale(kctl, db_scale_rec_gain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 snd_ac97_write_cache(ac97, AC97_REC_GAIN_MIC, 0x0000);
1527 }
1528
1529 /* build PCM out path & mute control */
1530 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 15)) {
1531 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_PCM_OUT], ac97))) < 0)
1532 return err;
1533 }
1534
1535 /* build Simulated Stereo Enhancement control */
1536 if (ac97->caps & 0x0008) {
1537 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_STEREO_ENHANCEMENT], ac97))) < 0)
1538 return err;
1539 }
1540
1541 /* build 3D Stereo Enhancement control */
1542 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 13)) {
1543 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_3D], ac97))) < 0)
1544 return err;
1545 }
1546
1547 /* build Loudness control */
1548 if (ac97->caps & 0x0020) {
1549 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOUDNESS], ac97))) < 0)
1550 return err;
1551 }
1552
1553 /* build Mono output select control */
1554 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 9)) {
1555 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MONO], ac97))) < 0)
1556 return err;
1557 }
1558
1559 /* build Mic select control */
1560 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 8)) {
1561 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MIC], ac97))) < 0)
1562 return err;
1563 }
1564
1565 /* build ADC/DAC loopback control */
1566 if (enable_loopback && snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 7)) {
1567 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOOPBACK], ac97))) < 0)
1568 return err;
1569 }
1570
1571 snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, ~AC97_GP_DRSS_MASK, 0x0000);
1572
1573 /* build 3D controls */
1574 if (ac97->build_ops->build_3d) {
1575 ac97->build_ops->build_3d(ac97);
1576 } else {
1577 if (snd_ac97_try_volume_mix(ac97, AC97_3D_CONTROL)) {
1578 unsigned short val;
1579 val = 0x0707;
1580 snd_ac97_write(ac97, AC97_3D_CONTROL, val);
1581 val = snd_ac97_read(ac97, AC97_3D_CONTROL);
1582 val = val == 0x0606;
1583 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
1584 return err;
1585 if (val)
1586 kctl->private_value = AC97_3D_CONTROL | (9 << 8) | (7 << 16);
1587 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[1], ac97))) < 0)
1588 return err;
1589 if (val)
1590 kctl->private_value = AC97_3D_CONTROL | (1 << 8) | (7 << 16);
1591 snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
1592 }
1593 }
1594
1595 /* build S/PDIF controls */
Magnus Sandin7b891902006-08-22 13:33:12 +02001596
1597 /* Hack for ASUS P5P800-VM, which does not indicate S/PDIF capability */
1598 if (ac97->subsystem_vendor == 0x1043 &&
1599 ac97->subsystem_device == 0x810f)
1600 ac97->ext_id |= AC97_EI_SPDIF;
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if ((ac97->ext_id & AC97_EI_SPDIF) && !(ac97->scaps & AC97_SCAP_NO_SPDIF)) {
1603 if (ac97->build_ops->build_spdif) {
1604 if ((err = ac97->build_ops->build_spdif(ac97)) < 0)
1605 return err;
1606 } else {
1607 for (idx = 0; idx < 5; idx++)
1608 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_spdif[idx], ac97))) < 0)
1609 return err;
1610 if (ac97->build_ops->build_post_spdif) {
1611 if ((err = ac97->build_ops->build_post_spdif(ac97)) < 0)
1612 return err;
1613 }
1614 /* set default PCM S/PDIF params */
1615 /* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */
1616 snd_ac97_write_cache(ac97, AC97_SPDIF, 0x2a20);
1617 ac97->rates[AC97_RATES_SPDIF] = snd_ac97_determine_spdif_rates(ac97);
1618 }
1619 ac97->spdif_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
1620 }
1621
1622 /* build chip specific controls */
1623 if (ac97->build_ops->build_specific)
1624 if ((err = ac97->build_ops->build_specific(ac97)) < 0)
1625 return err;
1626
1627 if (snd_ac97_try_bit(ac97, AC97_POWERDOWN, 15)) {
1628 kctl = snd_ac97_cnew(&snd_ac97_control_eapd, ac97);
1629 if (! kctl)
1630 return -ENOMEM;
1631 if (ac97->scaps & AC97_SCAP_INV_EAPD)
1632 set_inv_eapd(ac97, kctl);
1633 if ((err = snd_ctl_add(card, kctl)) < 0)
1634 return err;
1635 }
1636
1637 return 0;
1638}
1639
Takashi Iwaiee423812005-11-17 14:21:36 +01001640static int snd_ac97_modem_build(struct snd_card *card, struct snd_ac97 * ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
Sasha Khapyorsky87d61c22005-05-29 15:08:23 +02001642 int err, idx;
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 //printk("AC97_GPIO_CFG = %x\n",snd_ac97_read(ac97,AC97_GPIO_CFG));
1645 snd_ac97_write(ac97, AC97_GPIO_CFG, 0xffff & ~(AC97_GPIO_LINE1_OH));
1646 snd_ac97_write(ac97, AC97_GPIO_POLARITY, 0xffff & ~(AC97_GPIO_LINE1_OH));
1647 snd_ac97_write(ac97, AC97_GPIO_STICKY, 0xffff);
1648 snd_ac97_write(ac97, AC97_GPIO_WAKEUP, 0x0);
1649 snd_ac97_write(ac97, AC97_MISC_AFE, 0x0);
Sasha Khapyorsky87d61c22005-05-29 15:08:23 +02001650
1651 /* build modem switches */
1652 for (idx = 0; idx < ARRAY_SIZE(snd_ac97_controls_modem_switches); idx++)
Sasha Khapyorsky0444e2a2005-09-13 11:21:30 +02001653 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ac97_controls_modem_switches[idx], ac97))) < 0)
Sasha Khapyorsky87d61c22005-05-29 15:08:23 +02001654 return err;
1655
1656 /* build chip specific controls */
1657 if (ac97->build_ops->build_specific)
1658 if ((err = ac97->build_ops->build_specific(ac97)) < 0)
1659 return err;
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return 0;
1662}
1663
Takashi Iwaiee423812005-11-17 14:21:36 +01001664static int snd_ac97_test_rate(struct snd_ac97 *ac97, int reg, int shadow_reg, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 unsigned short val;
1667 unsigned int tmp;
1668
1669 tmp = ((unsigned int)rate * ac97->bus->clock) / 48000;
1670 snd_ac97_write_cache(ac97, reg, tmp & 0xffff);
1671 if (shadow_reg)
1672 snd_ac97_write_cache(ac97, shadow_reg, tmp & 0xffff);
1673 val = snd_ac97_read(ac97, reg);
1674 return val == (tmp & 0xffff);
1675}
1676
Takashi Iwaiee423812005-11-17 14:21:36 +01001677static void snd_ac97_determine_rates(struct snd_ac97 *ac97, int reg, int shadow_reg, unsigned int *r_result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 unsigned int result = 0;
1680 unsigned short saved;
1681
1682 if (ac97->bus->no_vra) {
1683 *r_result = SNDRV_PCM_RATE_48000;
1684 if ((ac97->flags & AC97_DOUBLE_RATE) &&
1685 reg == AC97_PCM_FRONT_DAC_RATE)
1686 *r_result |= SNDRV_PCM_RATE_96000;
1687 return;
1688 }
1689
1690 saved = snd_ac97_read(ac97, reg);
1691 if ((ac97->ext_id & AC97_EI_DRA) && reg == AC97_PCM_FRONT_DAC_RATE)
1692 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
1693 AC97_EA_DRA, 0);
1694 /* test a non-standard rate */
1695 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11000))
1696 result |= SNDRV_PCM_RATE_CONTINUOUS;
1697 /* let's try to obtain standard rates */
1698 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 8000))
1699 result |= SNDRV_PCM_RATE_8000;
1700 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11025))
1701 result |= SNDRV_PCM_RATE_11025;
1702 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 16000))
1703 result |= SNDRV_PCM_RATE_16000;
1704 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 22050))
1705 result |= SNDRV_PCM_RATE_22050;
1706 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 32000))
1707 result |= SNDRV_PCM_RATE_32000;
1708 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 44100))
1709 result |= SNDRV_PCM_RATE_44100;
1710 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 48000))
1711 result |= SNDRV_PCM_RATE_48000;
1712 if ((ac97->flags & AC97_DOUBLE_RATE) &&
1713 reg == AC97_PCM_FRONT_DAC_RATE) {
1714 /* test standard double rates */
1715 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
1716 AC97_EA_DRA, AC97_EA_DRA);
1717 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 64000 / 2))
1718 result |= SNDRV_PCM_RATE_64000;
1719 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 88200 / 2))
1720 result |= SNDRV_PCM_RATE_88200;
1721 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 96000 / 2))
1722 result |= SNDRV_PCM_RATE_96000;
1723 /* some codecs don't support variable double rates */
1724 if (!snd_ac97_test_rate(ac97, reg, shadow_reg, 76100 / 2))
1725 result &= ~SNDRV_PCM_RATE_CONTINUOUS;
1726 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
1727 AC97_EA_DRA, 0);
1728 }
1729 /* restore the default value */
1730 snd_ac97_write_cache(ac97, reg, saved);
1731 if (shadow_reg)
1732 snd_ac97_write_cache(ac97, shadow_reg, saved);
1733 *r_result = result;
1734}
1735
1736/* check AC97_SPDIF register to accept which sample rates */
Takashi Iwaiee423812005-11-17 14:21:36 +01001737static unsigned int snd_ac97_determine_spdif_rates(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
1739 unsigned int result = 0;
1740 int i;
1741 static unsigned short ctl_bits[] = {
1742 AC97_SC_SPSR_44K, AC97_SC_SPSR_32K, AC97_SC_SPSR_48K
1743 };
1744 static unsigned int rate_bits[] = {
1745 SNDRV_PCM_RATE_44100, SNDRV_PCM_RATE_32000, SNDRV_PCM_RATE_48000
1746 };
1747
1748 for (i = 0; i < (int)ARRAY_SIZE(ctl_bits); i++) {
1749 snd_ac97_update_bits(ac97, AC97_SPDIF, AC97_SC_SPSR_MASK, ctl_bits[i]);
1750 if ((snd_ac97_read(ac97, AC97_SPDIF) & AC97_SC_SPSR_MASK) == ctl_bits[i])
1751 result |= rate_bits[i];
1752 }
1753 return result;
1754}
1755
1756/* look for the codec id table matching with the given id */
Takashi Iwaiee423812005-11-17 14:21:36 +01001757static const struct ac97_codec_id *look_for_codec_id(const struct ac97_codec_id *table,
1758 unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Takashi Iwaiee423812005-11-17 14:21:36 +01001760 const struct ac97_codec_id *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 for (pid = table; pid->id; pid++)
1763 if (pid->id == (id & pid->mask))
1764 return pid;
1765 return NULL;
1766}
1767
Takashi Iwaiee423812005-11-17 14:21:36 +01001768void snd_ac97_get_name(struct snd_ac97 *ac97, unsigned int id, char *name, int modem)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Takashi Iwaiee423812005-11-17 14:21:36 +01001770 const struct ac97_codec_id *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 sprintf(name, "0x%x %c%c%c", id,
1773 printable(id >> 24),
1774 printable(id >> 16),
1775 printable(id >> 8));
1776 pid = look_for_codec_id(snd_ac97_codec_id_vendors, id);
1777 if (! pid)
1778 return;
1779
1780 strcpy(name, pid->name);
1781 if (ac97 && pid->patch) {
1782 if ((modem && (pid->flags & AC97_MODEM_PATCH)) ||
1783 (! modem && ! (pid->flags & AC97_MODEM_PATCH)))
1784 pid->patch(ac97);
1785 }
1786
1787 pid = look_for_codec_id(snd_ac97_codec_ids, id);
1788 if (pid) {
1789 strcat(name, " ");
1790 strcat(name, pid->name);
1791 if (pid->mask != 0xffffffff)
1792 sprintf(name + strlen(name), " rev %d", id & ~pid->mask);
1793 if (ac97 && pid->patch) {
1794 if ((modem && (pid->flags & AC97_MODEM_PATCH)) ||
1795 (! modem && ! (pid->flags & AC97_MODEM_PATCH)))
1796 pid->patch(ac97);
1797 }
1798 } else
1799 sprintf(name + strlen(name), " id %x", id & 0xff);
1800}
1801
1802/**
1803 * snd_ac97_get_short_name - retrieve codec name
1804 * @ac97: the codec instance
1805 *
1806 * Returns the short identifying name of the codec.
1807 */
Takashi Iwaiee423812005-11-17 14:21:36 +01001808const char *snd_ac97_get_short_name(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Takashi Iwaiee423812005-11-17 14:21:36 +01001810 const struct ac97_codec_id *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 for (pid = snd_ac97_codec_ids; pid->id; pid++)
1813 if (pid->id == (ac97->id & pid->mask))
1814 return pid->name;
1815 return "unknown codec";
1816}
1817
Takashi Iwai7b096792006-04-28 15:13:39 +02001818EXPORT_SYMBOL(snd_ac97_get_short_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820/* wait for a while until registers are accessible after RESET
1821 * return 0 if ok, negative not ready
1822 */
Takashi Iwaiee423812005-11-17 14:21:36 +01001823static int ac97_reset_wait(struct snd_ac97 *ac97, int timeout, int with_modem)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
1825 unsigned long end_time;
1826 unsigned short val;
1827
1828 end_time = jiffies + timeout;
1829 do {
1830
1831 /* use preliminary reads to settle the communication */
1832 snd_ac97_read(ac97, AC97_RESET);
1833 snd_ac97_read(ac97, AC97_VENDOR_ID1);
1834 snd_ac97_read(ac97, AC97_VENDOR_ID2);
1835 /* modem? */
1836 if (with_modem) {
1837 val = snd_ac97_read(ac97, AC97_EXTENDED_MID);
1838 if (val != 0xffff && (val & 1) != 0)
1839 return 0;
1840 }
1841 if (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) {
1842 /* probably only Xbox issue - all registers are read as zero */
1843 val = snd_ac97_read(ac97, AC97_VENDOR_ID1);
1844 if (val != 0 && val != 0xffff)
1845 return 0;
1846 } else {
1847 /* because the PCM or MASTER volume registers can be modified,
1848 * the REC_GAIN register is used for tests
1849 */
1850 /* test if we can write to the record gain volume register */
1851 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a05);
1852 if ((snd_ac97_read(ac97, AC97_REC_GAIN) & 0x7fff) == 0x0a05)
1853 return 0;
1854 }
Nishanth Aravamudan8433a502005-10-24 15:02:37 +02001855 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 } while (time_after_eq(end_time, jiffies));
1857 return -ENODEV;
1858}
1859
1860/**
1861 * snd_ac97_bus - create an AC97 bus component
1862 * @card: the card instance
1863 * @num: the bus number
1864 * @ops: the bus callbacks table
1865 * @private_data: private data pointer for the new instance
1866 * @rbus: the pointer to store the new AC97 bus instance.
1867 *
Takashi Iwaiee423812005-11-17 14:21:36 +01001868 * Creates an AC97 bus component. An struct snd_ac97_bus instance is newly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 * allocated and initialized.
1870 *
1871 * The ops table must include valid callbacks (at least read and
1872 * write). The other callbacks, wait and reset, are not mandatory.
1873 *
1874 * The clock is set to 48000. If another clock is needed, set
1875 * (*rbus)->clock manually.
1876 *
1877 * The AC97 bus instance is registered as a low-level device, so you don't
1878 * have to release it manually.
1879 *
1880 * Returns zero if successful, or a negative error code on failure.
1881 */
Takashi Iwaiee423812005-11-17 14:21:36 +01001882int snd_ac97_bus(struct snd_card *card, int num, struct snd_ac97_bus_ops *ops,
1883 void *private_data, struct snd_ac97_bus **rbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
1885 int err;
Takashi Iwaiee423812005-11-17 14:21:36 +01001886 struct snd_ac97_bus *bus;
1887 static struct snd_device_ops dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 .dev_free = snd_ac97_bus_dev_free,
1889 };
1890
1891 snd_assert(card != NULL, return -EINVAL);
1892 snd_assert(rbus != NULL, return -EINVAL);
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001893 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (bus == NULL)
1895 return -ENOMEM;
1896 bus->card = card;
1897 bus->num = num;
1898 bus->ops = ops;
1899 bus->private_data = private_data;
1900 bus->clock = 48000;
1901 spin_lock_init(&bus->bus_lock);
1902 snd_ac97_bus_proc_init(bus);
1903 if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
1904 snd_ac97_bus_free(bus);
1905 return err;
1906 }
1907 *rbus = bus;
1908 return 0;
1909}
1910
Takashi Iwai7b096792006-04-28 15:13:39 +02001911EXPORT_SYMBOL(snd_ac97_bus);
1912
Liam Girdwood0ca06a02005-07-29 16:13:36 +02001913/* stop no dev release warning */
1914static void ac97_device_release(struct device * dev)
1915{
1916}
1917
1918/* register ac97 codec to bus */
Takashi Iwaiee423812005-11-17 14:21:36 +01001919static int snd_ac97_dev_register(struct snd_device *device)
Liam Girdwood0ca06a02005-07-29 16:13:36 +02001920{
Takashi Iwaiee423812005-11-17 14:21:36 +01001921 struct snd_ac97 *ac97 = device->device_data;
Liam Girdwood0ca06a02005-07-29 16:13:36 +02001922 int err;
1923
1924 ac97->dev.bus = &ac97_bus_type;
1925 ac97->dev.parent = ac97->bus->card->dev;
Liam Girdwood0ca06a02005-07-29 16:13:36 +02001926 ac97->dev.release = ac97_device_release;
Takashi Iwai93984412005-11-17 15:30:43 +01001927 snprintf(ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
1928 ac97->bus->card->number, ac97->num,
1929 snd_ac97_get_short_name(ac97));
Liam Girdwood0ca06a02005-07-29 16:13:36 +02001930 if ((err = device_register(&ac97->dev)) < 0) {
1931 snd_printk(KERN_ERR "Can't register ac97 bus\n");
1932 ac97->dev.bus = NULL;
1933 return err;
1934 }
1935 return 0;
1936}
1937
Takashi Iwaic4614822006-06-23 14:38:23 +02001938/* disconnect ac97 codec */
1939static int snd_ac97_dev_disconnect(struct snd_device *device)
Liam Girdwood0ca06a02005-07-29 16:13:36 +02001940{
Takashi Iwaiee423812005-11-17 14:21:36 +01001941 struct snd_ac97 *ac97 = device->device_data;
Liam Girdwood0ca06a02005-07-29 16:13:36 +02001942 if (ac97->dev.bus)
1943 device_unregister(&ac97->dev);
Takashi Iwaic4614822006-06-23 14:38:23 +02001944 return 0;
Liam Girdwood0ca06a02005-07-29 16:13:36 +02001945}
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947/* build_ops to do nothing */
1948static struct snd_ac97_build_ops null_build_ops;
1949
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001950#ifdef CONFIG_SND_AC97_POWER_SAVE
David Howellsc4028952006-11-22 14:57:56 +00001951static void do_update_power(struct work_struct *work)
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001952{
David Howellsc4028952006-11-22 14:57:56 +00001953 update_power_regs(
1954 container_of(work, struct snd_ac97, power_work.work));
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001955}
1956#endif
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958/**
1959 * snd_ac97_mixer - create an Codec97 component
1960 * @bus: the AC97 bus which codec is attached to
1961 * @template: the template of ac97, including index, callbacks and
1962 * the private data.
1963 * @rac97: the pointer to store the new ac97 instance.
1964 *
Takashi Iwaiee423812005-11-17 14:21:36 +01001965 * Creates an Codec97 component. An struct snd_ac97 instance is newly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 * allocated and initialized from the template. The codec
1967 * is then initialized by the standard procedure.
1968 *
1969 * The template must include the codec number (num) and address (addr),
1970 * and the private data (private_data).
1971 *
1972 * The ac97 instance is registered as a low-level device, so you don't
1973 * have to release it manually.
1974 *
1975 * Returns zero if successful, or a negative error code on failure.
1976 */
Takashi Iwaiee423812005-11-17 14:21:36 +01001977int snd_ac97_mixer(struct snd_ac97_bus *bus, struct snd_ac97_template *template, struct snd_ac97 **rac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 int err;
Takashi Iwaiee423812005-11-17 14:21:36 +01001980 struct snd_ac97 *ac97;
1981 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 char name[64];
1983 unsigned long end_time;
1984 unsigned int reg;
Takashi Iwaiee423812005-11-17 14:21:36 +01001985 const struct ac97_codec_id *pid;
1986 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 .dev_free = snd_ac97_dev_free,
Liam Girdwood0ca06a02005-07-29 16:13:36 +02001988 .dev_register = snd_ac97_dev_register,
Takashi Iwaic4614822006-06-23 14:38:23 +02001989 .dev_disconnect = snd_ac97_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 };
1991
1992 snd_assert(rac97 != NULL, return -EINVAL);
1993 *rac97 = NULL;
1994 snd_assert(bus != NULL && template != NULL, return -EINVAL);
1995 snd_assert(template->num < 4 && bus->codec[template->num] == NULL, return -EINVAL);
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 card = bus->card;
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001998 ac97 = kzalloc(sizeof(*ac97), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (ac97 == NULL)
2000 return -ENOMEM;
2001 ac97->private_data = template->private_data;
2002 ac97->private_free = template->private_free;
2003 ac97->bus = bus;
2004 ac97->pci = template->pci;
2005 ac97->num = template->num;
2006 ac97->addr = template->addr;
2007 ac97->scaps = template->scaps;
Takashi Iwai7c5706b2006-03-15 13:52:54 +01002008 ac97->res_table = template->res_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 bus->codec[ac97->num] = ac97;
Ingo Molnar62932df2006-01-16 16:34:20 +01002010 mutex_init(&ac97->reg_mutex);
2011 mutex_init(&ac97->page_mutex);
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002012#ifdef CONFIG_SND_AC97_POWER_SAVE
David Howellsc4028952006-11-22 14:57:56 +00002013 INIT_DELAYED_WORK(&ac97->power_work, do_update_power);
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002014#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Takashi Iwaiadf1b3d2005-12-01 10:49:58 +01002016#ifdef CONFIG_PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (ac97->pci) {
2018 pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_VENDOR_ID, &ac97->subsystem_vendor);
2019 pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_ID, &ac97->subsystem_device);
2020 }
Takashi Iwaiadf1b3d2005-12-01 10:49:58 +01002021#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (bus->ops->reset) {
2023 bus->ops->reset(ac97);
2024 goto __access_ok;
2025 }
2026
2027 ac97->id = snd_ac97_read(ac97, AC97_VENDOR_ID1) << 16;
2028 ac97->id |= snd_ac97_read(ac97, AC97_VENDOR_ID2);
2029 if (ac97->id && ac97->id != (unsigned int)-1) {
2030 pid = look_for_codec_id(snd_ac97_codec_ids, ac97->id);
2031 if (pid && (pid->flags & AC97_DEFAULT_POWER_OFF))
2032 goto __access_ok;
2033 }
2034
Sasha Khapyorsky23fea4d2005-04-07 20:23:58 +02002035 /* reset to defaults */
2036 if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO))
2037 snd_ac97_write(ac97, AC97_RESET, 0);
2038 if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM))
2039 snd_ac97_write(ac97, AC97_EXTENDED_MID, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (bus->ops->wait)
2041 bus->ops->wait(ac97);
2042 else {
2043 udelay(50);
2044 if (ac97->scaps & AC97_SCAP_SKIP_AUDIO)
Takashi Iwai45cffef2007-08-10 16:50:37 +02002045 err = ac97_reset_wait(ac97, msecs_to_jiffies(500), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 else {
Takashi Iwai45cffef2007-08-10 16:50:37 +02002047 err = ac97_reset_wait(ac97, msecs_to_jiffies(500), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (err < 0)
Takashi Iwai45cffef2007-08-10 16:50:37 +02002049 err = ac97_reset_wait(ac97,
2050 msecs_to_jiffies(500), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052 if (err < 0) {
2053 snd_printk(KERN_WARNING "AC'97 %d does not respond - RESET\n", ac97->num);
2054 /* proceed anyway - it's often non-critical */
2055 }
2056 }
2057 __access_ok:
2058 ac97->id = snd_ac97_read(ac97, AC97_VENDOR_ID1) << 16;
2059 ac97->id |= snd_ac97_read(ac97, AC97_VENDOR_ID2);
2060 if (! (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) &&
2061 (ac97->id == 0x00000000 || ac97->id == 0xffffffff)) {
2062 snd_printk(KERN_ERR "AC'97 %d access is not valid [0x%x], removing mixer.\n", ac97->num, ac97->id);
2063 snd_ac97_free(ac97);
2064 return -EIO;
2065 }
2066 pid = look_for_codec_id(snd_ac97_codec_ids, ac97->id);
2067 if (pid)
2068 ac97->flags |= pid->flags;
2069
2070 /* test for AC'97 */
2071 if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO) && !(ac97->scaps & AC97_SCAP_AUDIO)) {
2072 /* test if we can write to the record gain volume register */
2073 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a06);
2074 if (((err = snd_ac97_read(ac97, AC97_REC_GAIN)) & 0x7fff) == 0x0a06)
2075 ac97->scaps |= AC97_SCAP_AUDIO;
2076 }
2077 if (ac97->scaps & AC97_SCAP_AUDIO) {
2078 ac97->caps = snd_ac97_read(ac97, AC97_RESET);
2079 ac97->ext_id = snd_ac97_read(ac97, AC97_EXTENDED_ID);
2080 if (ac97->ext_id == 0xffff) /* invalid combination */
2081 ac97->ext_id = 0;
2082 }
2083
2084 /* test for MC'97 */
2085 if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM) && !(ac97->scaps & AC97_SCAP_MODEM)) {
2086 ac97->ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID);
2087 if (ac97->ext_mid == 0xffff) /* invalid combination */
2088 ac97->ext_mid = 0;
2089 if (ac97->ext_mid & 1)
2090 ac97->scaps |= AC97_SCAP_MODEM;
2091 }
2092
2093 if (!ac97_is_audio(ac97) && !ac97_is_modem(ac97)) {
2094 if (!(ac97->scaps & (AC97_SCAP_SKIP_AUDIO|AC97_SCAP_SKIP_MODEM)))
2095 snd_printk(KERN_ERR "AC'97 %d access error (not audio or modem codec)\n", ac97->num);
2096 snd_ac97_free(ac97);
2097 return -EACCES;
2098 }
2099
2100 if (bus->ops->reset) // FIXME: always skipping?
2101 goto __ready_ok;
2102
2103 /* FIXME: add powerdown control */
2104 if (ac97_is_audio(ac97)) {
2105 /* nothing should be in powerdown mode */
2106 snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0);
2107 if (! (ac97->flags & AC97_DEFAULT_POWER_OFF)) {
2108 snd_ac97_write_cache(ac97, AC97_RESET, 0); /* reset to defaults */
2109 udelay(100);
2110 snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0);
2111 }
2112 /* nothing should be in powerdown mode */
2113 snd_ac97_write_cache(ac97, AC97_GENERAL_PURPOSE, 0);
Takashi Iwai45cffef2007-08-10 16:50:37 +02002114 end_time = jiffies + msecs_to_jiffies(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 do {
2116 if ((snd_ac97_read(ac97, AC97_POWERDOWN) & 0x0f) == 0x0f)
2117 goto __ready_ok;
Nishanth Aravamudan8433a502005-10-24 15:02:37 +02002118 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 } while (time_after_eq(end_time, jiffies));
2120 snd_printk(KERN_WARNING "AC'97 %d analog subsections not ready\n", ac97->num);
2121 }
2122
2123 /* FIXME: add powerdown control */
2124 if (ac97_is_modem(ac97)) {
2125 unsigned char tmp;
2126
2127 /* nothing should be in powerdown mode */
2128 /* note: it's important to set the rate at first */
2129 tmp = AC97_MEA_GPIO;
2130 if (ac97->ext_mid & AC97_MEI_LINE1) {
Sasha Khapyorsky8e8311b2005-04-07 20:22:58 +02002131 snd_ac97_write_cache(ac97, AC97_LINE1_RATE, 8000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 tmp |= AC97_MEA_ADC1 | AC97_MEA_DAC1;
2133 }
2134 if (ac97->ext_mid & AC97_MEI_LINE2) {
Sasha Khapyorsky8e8311b2005-04-07 20:22:58 +02002135 snd_ac97_write_cache(ac97, AC97_LINE2_RATE, 8000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 tmp |= AC97_MEA_ADC2 | AC97_MEA_DAC2;
2137 }
2138 if (ac97->ext_mid & AC97_MEI_HANDSET) {
Sasha Khapyorsky8e8311b2005-04-07 20:22:58 +02002139 snd_ac97_write_cache(ac97, AC97_HANDSET_RATE, 8000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 tmp |= AC97_MEA_HADC | AC97_MEA_HDAC;
2141 }
Sasha Khapyorsky8e8311b2005-04-07 20:22:58 +02002142 snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 udelay(100);
2144 /* nothing should be in powerdown mode */
Sasha Khapyorsky8e8311b2005-04-07 20:22:58 +02002145 snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0);
Takashi Iwai45cffef2007-08-10 16:50:37 +02002146 end_time = jiffies + msecs_to_jiffies(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 do {
2148 if ((snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS) & tmp) == tmp)
2149 goto __ready_ok;
Nishanth Aravamudan8433a502005-10-24 15:02:37 +02002150 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 } while (time_after_eq(end_time, jiffies));
2152 snd_printk(KERN_WARNING "MC'97 %d converters and GPIO not ready (0x%x)\n", ac97->num, snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS));
2153 }
2154
2155 __ready_ok:
2156 if (ac97_is_audio(ac97))
2157 ac97->addr = (ac97->ext_id & AC97_EI_ADDR_MASK) >> AC97_EI_ADDR_SHIFT;
2158 else
2159 ac97->addr = (ac97->ext_mid & AC97_MEI_ADDR_MASK) >> AC97_MEI_ADDR_SHIFT;
2160 if (ac97->ext_id & 0x01c9) { /* L/R, MIC, SDAC, LDAC VRA support */
2161 reg = snd_ac97_read(ac97, AC97_EXTENDED_STATUS);
2162 reg |= ac97->ext_id & 0x01c0; /* LDAC/SDAC/CDAC */
2163 if (! bus->no_vra)
2164 reg |= ac97->ext_id & 0x0009; /* VRA/VRM */
2165 snd_ac97_write_cache(ac97, AC97_EXTENDED_STATUS, reg);
2166 }
2167 if ((ac97->ext_id & AC97_EI_DRA) && bus->dra) {
2168 /* Intel controllers require double rate data to be put in
2169 * slots 7+8, so let's hope the codec supports it. */
2170 snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, AC97_GP_DRSS_MASK, AC97_GP_DRSS_78);
2171 if ((snd_ac97_read(ac97, AC97_GENERAL_PURPOSE) & AC97_GP_DRSS_MASK) == AC97_GP_DRSS_78)
2172 ac97->flags |= AC97_DOUBLE_RATE;
Takashi Iwai091e95e2005-10-27 20:56:35 +02002173 /* restore to slots 10/11 to avoid the confliction with surrounds */
2174 snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, AC97_GP_DRSS_MASK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
2176 if (ac97->ext_id & AC97_EI_VRA) { /* VRA support */
2177 snd_ac97_determine_rates(ac97, AC97_PCM_FRONT_DAC_RATE, 0, &ac97->rates[AC97_RATES_FRONT_DAC]);
2178 snd_ac97_determine_rates(ac97, AC97_PCM_LR_ADC_RATE, 0, &ac97->rates[AC97_RATES_ADC]);
2179 } else {
2180 ac97->rates[AC97_RATES_FRONT_DAC] = SNDRV_PCM_RATE_48000;
2181 if (ac97->flags & AC97_DOUBLE_RATE)
2182 ac97->rates[AC97_RATES_FRONT_DAC] |= SNDRV_PCM_RATE_96000;
2183 ac97->rates[AC97_RATES_ADC] = SNDRV_PCM_RATE_48000;
2184 }
2185 if (ac97->ext_id & AC97_EI_SPDIF) {
2186 /* codec specific code (patch) should override these values */
2187 ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_32000;
2188 }
2189 if (ac97->ext_id & AC97_EI_VRM) { /* MIC VRA support */
2190 snd_ac97_determine_rates(ac97, AC97_PCM_MIC_ADC_RATE, 0, &ac97->rates[AC97_RATES_MIC_ADC]);
2191 } else {
2192 ac97->rates[AC97_RATES_MIC_ADC] = SNDRV_PCM_RATE_48000;
2193 }
2194 if (ac97->ext_id & AC97_EI_SDAC) { /* SDAC support */
2195 snd_ac97_determine_rates(ac97, AC97_PCM_SURR_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_SURR_DAC]);
2196 ac97->scaps |= AC97_SCAP_SURROUND_DAC;
2197 }
2198 if (ac97->ext_id & AC97_EI_LDAC) { /* LDAC support */
2199 snd_ac97_determine_rates(ac97, AC97_PCM_LFE_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_LFE_DAC]);
2200 ac97->scaps |= AC97_SCAP_CENTER_LFE_DAC;
2201 }
2202 /* additional initializations */
2203 if (bus->ops->init)
2204 bus->ops->init(ac97);
2205 snd_ac97_get_name(ac97, ac97->id, name, !ac97_is_audio(ac97));
2206 snd_ac97_get_name(NULL, ac97->id, name, !ac97_is_audio(ac97)); // ac97->id might be changed in the special setup code
2207 if (! ac97->build_ops)
2208 ac97->build_ops = &null_build_ops;
2209
2210 if (ac97_is_audio(ac97)) {
2211 char comp[16];
2212 if (card->mixername[0] == '\0') {
2213 strcpy(card->mixername, name);
2214 } else {
2215 if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) {
2216 strcat(card->mixername, ",");
2217 strcat(card->mixername, name);
2218 }
2219 }
2220 sprintf(comp, "AC97a:%08x", ac97->id);
2221 if ((err = snd_component_add(card, comp)) < 0) {
2222 snd_ac97_free(ac97);
2223 return err;
2224 }
2225 if (snd_ac97_mixer_build(ac97) < 0) {
2226 snd_ac97_free(ac97);
2227 return -ENOMEM;
2228 }
2229 }
2230 if (ac97_is_modem(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, "AC97m:%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_modem_build(card, ac97) < 0) {
2246 snd_ac97_free(ac97);
2247 return -ENOMEM;
2248 }
2249 }
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002250 if (ac97_is_audio(ac97))
2251 update_power_regs(ac97);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 snd_ac97_proc_init(ac97);
2253 if ((err = snd_device_new(card, SNDRV_DEV_CODEC, ac97, &ops)) < 0) {
2254 snd_ac97_free(ac97);
2255 return err;
2256 }
2257 *rac97 = ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return 0;
2259}
2260
Takashi Iwai7b096792006-04-28 15:13:39 +02002261EXPORT_SYMBOL(snd_ac97_mixer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263/*
2264 * Power down the chip.
2265 *
2266 * MASTER and HEADPHONE registers are muted but the register cache values
2267 * are not changed, so that the values can be restored in snd_ac97_resume().
2268 */
Takashi Iwaiee423812005-11-17 14:21:36 +01002269static void snd_ac97_powerdown(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 unsigned short power;
2272
2273 if (ac97_is_audio(ac97)) {
2274 /* some codecs have stereo mute bits */
2275 snd_ac97_write(ac97, AC97_MASTER, 0x9f9f);
2276 snd_ac97_write(ac97, AC97_HEADPHONE, 0x9f9f);
2277 }
2278
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002279 /* surround, CLFE, mic powerdown */
2280 power = ac97->regs[AC97_EXTENDED_STATUS];
2281 if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
2282 power |= AC97_EA_PRJ;
2283 if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
2284 power |= AC97_EA_PRI | AC97_EA_PRK;
2285 power |= AC97_EA_PRL;
2286 snd_ac97_write(ac97, AC97_EXTENDED_STATUS, power);
2287
2288 /* powerdown external amplifier */
2289 if (ac97->scaps & AC97_SCAP_INV_EAPD)
2290 power = ac97->regs[AC97_POWERDOWN] & ~AC97_PD_EAPD;
2291 else if (! (ac97->scaps & AC97_SCAP_EAPD_LED))
2292 power = ac97->regs[AC97_POWERDOWN] | AC97_PD_EAPD;
2293 power |= AC97_PD_PR6; /* Headphone amplifier powerdown */
2294 power |= AC97_PD_PR0 | AC97_PD_PR1; /* ADC & DAC powerdown */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 snd_ac97_write(ac97, AC97_POWERDOWN, power);
2296 udelay(100);
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002297 power |= AC97_PD_PR2 | AC97_PD_PR3; /* Analog Mixer powerdown */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 snd_ac97_write(ac97, AC97_POWERDOWN, power);
Takashi Iwaif1a63a32006-10-24 18:25:29 +02002299 if (ac97_is_power_save_mode(ac97)) {
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002300 udelay(100);
2301 /* AC-link powerdown, internal Clk disable */
2302 /* FIXME: this may cause click noises on some boards */
2303 power |= AC97_PD_PR4 | AC97_PD_PR5;
2304 snd_ac97_write(ac97, AC97_POWERDOWN, power);
2305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
2307
2308
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002309struct ac97_power_reg {
2310 unsigned short reg;
2311 unsigned short power_reg;
2312 unsigned short mask;
2313};
2314
2315enum { PWIDX_ADC, PWIDX_FRONT, PWIDX_CLFE, PWIDX_SURR, PWIDX_MIC, PWIDX_SIZE };
2316
2317static struct ac97_power_reg power_regs[PWIDX_SIZE] = {
2318 [PWIDX_ADC] = { AC97_PCM_LR_ADC_RATE, AC97_POWERDOWN, AC97_PD_PR0},
2319 [PWIDX_FRONT] = { AC97_PCM_FRONT_DAC_RATE, AC97_POWERDOWN, AC97_PD_PR1},
2320 [PWIDX_CLFE] = { AC97_PCM_LFE_DAC_RATE, AC97_EXTENDED_STATUS,
2321 AC97_EA_PRI | AC97_EA_PRK},
2322 [PWIDX_SURR] = { AC97_PCM_SURR_DAC_RATE, AC97_EXTENDED_STATUS,
2323 AC97_EA_PRJ},
2324 [PWIDX_MIC] = { AC97_PCM_MIC_ADC_RATE, AC97_EXTENDED_STATUS,
2325 AC97_EA_PRL},
2326};
2327
2328#ifdef CONFIG_SND_AC97_POWER_SAVE
2329/**
2330 * snd_ac97_update_power - update the powerdown register
2331 * @ac97: the codec instance
2332 * @reg: the rate register, e.g. AC97_PCM_FRONT_DAC_RATE
2333 * @powerup: non-zero when power up the part
2334 *
2335 * Update the AC97 powerdown register bits of the given part.
2336 */
2337int snd_ac97_update_power(struct snd_ac97 *ac97, int reg, int powerup)
2338{
2339 int i;
2340
2341 if (! ac97)
2342 return 0;
2343
2344 if (reg) {
2345 /* SPDIF requires DAC power, too */
2346 if (reg == AC97_SPDIF)
2347 reg = AC97_PCM_FRONT_DAC_RATE;
2348 for (i = 0; i < PWIDX_SIZE; i++) {
2349 if (power_regs[i].reg == reg) {
2350 if (powerup)
2351 ac97->power_up |= (1 << i);
2352 else
2353 ac97->power_up &= ~(1 << i);
2354 break;
2355 }
2356 }
2357 }
2358
Takashi Iwaif1a63a32006-10-24 18:25:29 +02002359 if (ac97_is_power_save_mode(ac97) && !powerup)
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002360 /* adjust power-down bits after two seconds delay
2361 * (for avoiding loud click noises for many (OSS) apps
2362 * that open/close frequently)
2363 */
Takashi Iwai45cffef2007-08-10 16:50:37 +02002364 schedule_delayed_work(&ac97->power_work,
2365 msecs_to_jiffies(2000));
Takashi Iwaif1a63a32006-10-24 18:25:29 +02002366 else {
2367 cancel_delayed_work(&ac97->power_work);
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002368 update_power_regs(ac97);
Takashi Iwaif1a63a32006-10-24 18:25:29 +02002369 }
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002370
2371 return 0;
2372}
2373
2374EXPORT_SYMBOL(snd_ac97_update_power);
2375#endif /* CONFIG_SND_AC97_POWER_SAVE */
2376
2377static void update_power_regs(struct snd_ac97 *ac97)
2378{
2379 unsigned int power_up, bits;
2380 int i;
2381
Takashi Iwaif1a63a32006-10-24 18:25:29 +02002382 power_up = (1 << PWIDX_FRONT) | (1 << PWIDX_ADC);
2383 power_up |= (1 << PWIDX_MIC);
2384 if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
2385 power_up |= (1 << PWIDX_SURR);
2386 if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
2387 power_up |= (1 << PWIDX_CLFE);
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002388#ifdef CONFIG_SND_AC97_POWER_SAVE
Takashi Iwaif1a63a32006-10-24 18:25:29 +02002389 if (ac97_is_power_save_mode(ac97))
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002390 power_up = ac97->power_up;
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002391#endif
2392 if (power_up) {
2393 if (ac97->regs[AC97_POWERDOWN] & AC97_PD_PR2) {
2394 /* needs power-up analog mix and vref */
2395 snd_ac97_update_bits(ac97, AC97_POWERDOWN,
2396 AC97_PD_PR3, 0);
2397 msleep(1);
2398 snd_ac97_update_bits(ac97, AC97_POWERDOWN,
2399 AC97_PD_PR2, 0);
2400 }
2401 }
2402 for (i = 0; i < PWIDX_SIZE; i++) {
2403 if (power_up & (1 << i))
2404 bits = 0;
2405 else
2406 bits = power_regs[i].mask;
2407 snd_ac97_update_bits(ac97, power_regs[i].power_reg,
2408 power_regs[i].mask, bits);
2409 }
2410 if (! power_up) {
2411 if (! (ac97->regs[AC97_POWERDOWN] & AC97_PD_PR2)) {
2412 /* power down analog mix and vref */
2413 snd_ac97_update_bits(ac97, AC97_POWERDOWN,
2414 AC97_PD_PR2, AC97_PD_PR2);
2415 snd_ac97_update_bits(ac97, AC97_POWERDOWN,
2416 AC97_PD_PR3, AC97_PD_PR3);
2417 }
2418 }
2419}
2420
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422#ifdef CONFIG_PM
2423/**
2424 * snd_ac97_suspend - General suspend function for AC97 codec
2425 * @ac97: the ac97 instance
2426 *
2427 * Suspends the codec, power down the chip.
2428 */
Takashi Iwaiee423812005-11-17 14:21:36 +01002429void snd_ac97_suspend(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Takashi Iwaie26e7542005-11-17 16:00:01 +01002431 if (! ac97)
2432 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (ac97->build_ops->suspend)
2434 ac97->build_ops->suspend(ac97);
Takashi Iwai3b6baa52007-01-31 14:34:38 +01002435#ifdef CONFIG_SND_AC97_POWER_SAVE
2436 cancel_delayed_work(&ac97->power_work);
2437 flush_scheduled_work();
2438#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 snd_ac97_powerdown(ac97);
2440}
2441
Takashi Iwai7b096792006-04-28 15:13:39 +02002442EXPORT_SYMBOL(snd_ac97_suspend);
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444/*
2445 * restore ac97 status
2446 */
Takashi Iwai6ddc9d22007-07-30 14:52:41 +02002447static void snd_ac97_restore_status(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
2449 int i;
2450
2451 for (i = 2; i < 0x7c ; i += 2) {
2452 if (i == AC97_POWERDOWN || i == AC97_EXTENDED_ID)
2453 continue;
2454 /* restore only accessible registers
2455 * some chip (e.g. nm256) may hang up when unsupported registers
2456 * are accessed..!
2457 */
2458 if (test_bit(i, ac97->reg_accessed)) {
2459 snd_ac97_write(ac97, i, ac97->regs[i]);
2460 snd_ac97_read(ac97, i);
2461 }
2462 }
2463}
2464
2465/*
2466 * restore IEC958 status
2467 */
Takashi Iwai6ddc9d22007-07-30 14:52:41 +02002468static void snd_ac97_restore_iec958(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 if (ac97->ext_id & AC97_EI_SPDIF) {
2471 if (ac97->regs[AC97_EXTENDED_STATUS] & AC97_EA_SPDIF) {
2472 /* reset spdif status */
2473 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
2474 snd_ac97_write(ac97, AC97_EXTENDED_STATUS, ac97->regs[AC97_EXTENDED_STATUS]);
2475 if (ac97->flags & AC97_CS_SPDIF)
2476 snd_ac97_write(ac97, AC97_CSR_SPDIF, ac97->regs[AC97_CSR_SPDIF]);
2477 else
2478 snd_ac97_write(ac97, AC97_SPDIF, ac97->regs[AC97_SPDIF]);
2479 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
2480 }
2481 }
2482}
2483
2484/**
2485 * snd_ac97_resume - General resume function for AC97 codec
2486 * @ac97: the ac97 instance
2487 *
2488 * Do the standard resume procedure, power up and restoring the
2489 * old register values.
2490 */
Takashi Iwaiee423812005-11-17 14:21:36 +01002491void snd_ac97_resume(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002493 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Takashi Iwaie26e7542005-11-17 16:00:01 +01002495 if (! ac97)
2496 return;
2497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 if (ac97->bus->ops->reset) {
2499 ac97->bus->ops->reset(ac97);
2500 goto __reset_ready;
2501 }
2502
2503 snd_ac97_write(ac97, AC97_POWERDOWN, 0);
2504 if (! (ac97->flags & AC97_DEFAULT_POWER_OFF)) {
Takashi Iwaid6c3cf82007-08-29 15:12:46 +02002505 if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO))
2506 snd_ac97_write(ac97, AC97_RESET, 0);
2507 else if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM))
2508 snd_ac97_write(ac97, AC97_EXTENDED_MID, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 udelay(100);
2510 snd_ac97_write(ac97, AC97_POWERDOWN, 0);
2511 }
2512 snd_ac97_write(ac97, AC97_GENERAL_PURPOSE, 0);
2513
2514 snd_ac97_write(ac97, AC97_POWERDOWN, ac97->regs[AC97_POWERDOWN]);
2515 if (ac97_is_audio(ac97)) {
2516 ac97->bus->ops->write(ac97, AC97_MASTER, 0x8101);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002517 end_time = jiffies + msecs_to_jiffies(100);
2518 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (snd_ac97_read(ac97, AC97_MASTER) == 0x8101)
2520 break;
Nishanth Aravamudan8433a502005-10-24 15:02:37 +02002521 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002522 } while (time_after_eq(end_time, jiffies));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 /* FIXME: extra delay */
2524 ac97->bus->ops->write(ac97, AC97_MASTER, 0x8000);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002525 if (snd_ac97_read(ac97, AC97_MASTER) != 0x8000)
2526 msleep(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 } else {
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002528 end_time = jiffies + msecs_to_jiffies(100);
2529 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 unsigned short val = snd_ac97_read(ac97, AC97_EXTENDED_MID);
2531 if (val != 0xffff && (val & 1) != 0)
2532 break;
Nishanth Aravamudan8433a502005-10-24 15:02:37 +02002533 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002534 } while (time_after_eq(end_time, jiffies));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
2536__reset_ready:
2537
2538 if (ac97->bus->ops->init)
2539 ac97->bus->ops->init(ac97);
2540
2541 if (ac97->build_ops->resume)
2542 ac97->build_ops->resume(ac97);
2543 else {
2544 snd_ac97_restore_status(ac97);
2545 snd_ac97_restore_iec958(ac97);
2546 }
2547}
Takashi Iwai7b096792006-04-28 15:13:39 +02002548
2549EXPORT_SYMBOL(snd_ac97_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550#endif
2551
2552
2553/*
2554 * Hardware tuning
2555 */
2556static void set_ctl_name(char *dst, const char *src, const char *suffix)
2557{
2558 if (suffix)
2559 sprintf(dst, "%s %s", src, suffix);
2560 else
2561 strcpy(dst, src);
2562}
2563
2564/* remove the control with the given name and optional suffix */
Takashi Iwaiac519022007-02-22 12:58:27 +01002565static int snd_ac97_remove_ctl(struct snd_ac97 *ac97, const char *name,
2566 const char *suffix)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Takashi Iwaiee423812005-11-17 14:21:36 +01002568 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 memset(&id, 0, sizeof(id));
2570 set_ctl_name(id.name, name, suffix);
2571 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2572 return snd_ctl_remove_id(ac97->bus->card, &id);
2573}
2574
Takashi Iwaiee423812005-11-17 14:21:36 +01002575static struct snd_kcontrol *ctl_find(struct snd_ac97 *ac97, const char *name, const char *suffix)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Takashi Iwaiee423812005-11-17 14:21:36 +01002577 struct snd_ctl_elem_id sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 memset(&sid, 0, sizeof(sid));
2579 set_ctl_name(sid.name, name, suffix);
2580 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2581 return snd_ctl_find_id(ac97->bus->card, &sid);
2582}
2583
2584/* rename the control with the given name and optional suffix */
Takashi Iwaiac519022007-02-22 12:58:27 +01002585static int snd_ac97_rename_ctl(struct snd_ac97 *ac97, const char *src,
2586 const char *dst, const char *suffix)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Takashi Iwaiee423812005-11-17 14:21:36 +01002588 struct snd_kcontrol *kctl = ctl_find(ac97, src, suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 if (kctl) {
2590 set_ctl_name(kctl->id.name, dst, suffix);
2591 return 0;
2592 }
2593 return -ENOENT;
2594}
2595
2596/* rename both Volume and Switch controls - don't check the return value */
Takashi Iwaiac519022007-02-22 12:58:27 +01002597static void snd_ac97_rename_vol_ctl(struct snd_ac97 *ac97, const char *src,
2598 const char *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
2600 snd_ac97_rename_ctl(ac97, src, dst, "Switch");
2601 snd_ac97_rename_ctl(ac97, src, dst, "Volume");
2602}
2603
2604/* swap controls */
Takashi Iwaiac519022007-02-22 12:58:27 +01002605static int snd_ac97_swap_ctl(struct snd_ac97 *ac97, const char *s1,
2606 const char *s2, const char *suffix)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
Takashi Iwaiee423812005-11-17 14:21:36 +01002608 struct snd_kcontrol *kctl1, *kctl2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 kctl1 = ctl_find(ac97, s1, suffix);
2610 kctl2 = ctl_find(ac97, s2, suffix);
2611 if (kctl1 && kctl2) {
2612 set_ctl_name(kctl1->id.name, s2, suffix);
2613 set_ctl_name(kctl2->id.name, s1, suffix);
2614 return 0;
2615 }
2616 return -ENOENT;
2617}
2618
2619#if 1
2620/* bind hp and master controls instead of using only hp control */
Takashi Iwaiee423812005-11-17 14:21:36 +01002621static int bind_hp_volsw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
2623 int err = snd_ac97_put_volsw(kcontrol, ucontrol);
2624 if (err > 0) {
2625 unsigned long priv_saved = kcontrol->private_value;
2626 kcontrol->private_value = (kcontrol->private_value & ~0xff) | AC97_HEADPHONE;
2627 snd_ac97_put_volsw(kcontrol, ucontrol);
2628 kcontrol->private_value = priv_saved;
2629 }
2630 return err;
2631}
2632
2633/* ac97 tune: bind Master and Headphone controls */
Takashi Iwaiee423812005-11-17 14:21:36 +01002634static int tune_hp_only(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
Takashi Iwaiee423812005-11-17 14:21:36 +01002636 struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
2637 struct snd_kcontrol *mvol = ctl_find(ac97, "Master Playback Volume", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 if (! msw || ! mvol)
2639 return -ENOENT;
2640 msw->put = bind_hp_volsw_put;
2641 mvol->put = bind_hp_volsw_put;
2642 snd_ac97_remove_ctl(ac97, "Headphone Playback", "Switch");
2643 snd_ac97_remove_ctl(ac97, "Headphone Playback", "Volume");
2644 return 0;
2645}
2646
2647#else
2648/* ac97 tune: use Headphone control as master */
Takashi Iwaiee423812005-11-17 14:21:36 +01002649static int tune_hp_only(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
2651 if (ctl_find(ac97, "Headphone Playback Switch", NULL) == NULL)
2652 return -ENOENT;
2653 snd_ac97_remove_ctl(ac97, "Master Playback", "Switch");
2654 snd_ac97_remove_ctl(ac97, "Master Playback", "Volume");
2655 snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
2656 return 0;
2657}
2658#endif
2659
2660/* ac97 tune: swap Headphone and Master controls */
Takashi Iwaiee423812005-11-17 14:21:36 +01002661static int tune_swap_hp(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
2663 if (ctl_find(ac97, "Headphone Playback Switch", NULL) == NULL)
2664 return -ENOENT;
2665 snd_ac97_rename_vol_ctl(ac97, "Master Playback", "Line-Out Playback");
2666 snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
2667 return 0;
2668}
2669
2670/* ac97 tune: swap Surround and Master controls */
Takashi Iwaiee423812005-11-17 14:21:36 +01002671static int tune_swap_surround(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
2673 if (snd_ac97_swap_ctl(ac97, "Master Playback", "Surround Playback", "Switch") ||
2674 snd_ac97_swap_ctl(ac97, "Master Playback", "Surround Playback", "Volume"))
2675 return -ENOENT;
2676 return 0;
2677}
2678
2679/* ac97 tune: set up mic sharing for AD codecs */
Takashi Iwaiee423812005-11-17 14:21:36 +01002680static int tune_ad_sharing(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
2682 unsigned short scfg;
2683 if ((ac97->id & 0xffffff00) != 0x41445300) {
2684 snd_printk(KERN_ERR "ac97_quirk AD_SHARING is only for AD codecs\n");
2685 return -EINVAL;
2686 }
2687 /* Turn on OMS bit to route microphone to back panel */
2688 scfg = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
2689 snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, scfg | 0x0200);
2690 return 0;
2691}
2692
Takashi Iwaiee423812005-11-17 14:21:36 +01002693static const struct snd_kcontrol_new snd_ac97_alc_jack_detect =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694AC97_SINGLE("Jack Detect", AC97_ALC650_CLOCK, 5, 1, 0);
2695
2696/* ac97 tune: set up ALC jack-select */
Takashi Iwaiee423812005-11-17 14:21:36 +01002697static int tune_alc_jack(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
2699 if ((ac97->id & 0xffffff00) != 0x414c4700) {
2700 snd_printk(KERN_ERR "ac97_quirk ALC_JACK is only for Realtek codecs\n");
2701 return -EINVAL;
2702 }
2703 snd_ac97_update_bits(ac97, 0x7a, 0x20, 0x20); /* select jack detect function */
2704 snd_ac97_update_bits(ac97, 0x7a, 0x01, 0x01); /* Line-out auto mute */
Takashi Iwai031c95d2005-12-05 20:51:43 +01002705 if (ac97->id == AC97_ID_ALC658D)
2706 snd_ac97_update_bits(ac97, 0x74, 0x0800, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&snd_ac97_alc_jack_detect, ac97));
2708}
2709
2710/* ac97 tune: inversed EAPD bit */
Takashi Iwaiee423812005-11-17 14:21:36 +01002711static int tune_inv_eapd(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
Takashi Iwaiee423812005-11-17 14:21:36 +01002713 struct snd_kcontrol *kctl = ctl_find(ac97, "External Amplifier", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (! kctl)
2715 return -ENOENT;
2716 set_inv_eapd(ac97, kctl);
2717 return 0;
2718}
2719
Takashi Iwaiee423812005-11-17 14:21:36 +01002720static int master_mute_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
2722 int err = snd_ac97_put_volsw(kcontrol, ucontrol);
2723 if (err > 0) {
Takashi Iwaiee423812005-11-17 14:21:36 +01002724 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 int shift = (kcontrol->private_value >> 8) & 0x0f;
2726 int rshift = (kcontrol->private_value >> 12) & 0x0f;
2727 unsigned short mask;
2728 if (shift != rshift)
2729 mask = 0x8080;
2730 else
2731 mask = 0x8000;
2732 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000,
2733 (ac97->regs[AC97_MASTER] & mask) == mask ?
2734 0x8000 : 0);
2735 }
2736 return err;
2737}
2738
2739/* ac97 tune: EAPD controls mute LED bound with the master mute */
Takashi Iwaiee423812005-11-17 14:21:36 +01002740static int tune_mute_led(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
Takashi Iwaiee423812005-11-17 14:21:36 +01002742 struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 if (! msw)
2744 return -ENOENT;
2745 msw->put = master_mute_sw_put;
2746 snd_ac97_remove_ctl(ac97, "External Amplifier", NULL);
2747 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000, 0x8000); /* mute LED on */
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002748 ac97->scaps |= AC97_SCAP_EAPD_LED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 return 0;
2750}
2751
Matthew Garretta0faefe2005-12-06 13:59:12 +01002752static int hp_master_mute_sw_put(struct snd_kcontrol *kcontrol,
2753 struct snd_ctl_elem_value *ucontrol)
2754{
2755 int err = bind_hp_volsw_put(kcontrol, ucontrol);
2756 if (err > 0) {
2757 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2758 int shift = (kcontrol->private_value >> 8) & 0x0f;
2759 int rshift = (kcontrol->private_value >> 12) & 0x0f;
2760 unsigned short mask;
2761 if (shift != rshift)
2762 mask = 0x8080;
2763 else
2764 mask = 0x8000;
2765 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000,
2766 (ac97->regs[AC97_MASTER] & mask) == mask ?
2767 0x8000 : 0);
2768 }
2769 return err;
2770}
2771
2772static int tune_hp_mute_led(struct snd_ac97 *ac97)
2773{
2774 struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
2775 struct snd_kcontrol *mvol = ctl_find(ac97, "Master Playback Volume", NULL);
2776 if (! msw || ! mvol)
2777 return -ENOENT;
2778 msw->put = hp_master_mute_sw_put;
2779 mvol->put = bind_hp_volsw_put;
2780 snd_ac97_remove_ctl(ac97, "External Amplifier", NULL);
2781 snd_ac97_remove_ctl(ac97, "Headphone Playback", "Switch");
2782 snd_ac97_remove_ctl(ac97, "Headphone Playback", "Volume");
2783 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000, 0x8000); /* mute LED on */
2784 return 0;
2785}
2786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787struct quirk_table {
2788 const char *name;
Takashi Iwaiee423812005-11-17 14:21:36 +01002789 int (*func)(struct snd_ac97 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790};
2791
2792static struct quirk_table applicable_quirks[] = {
2793 { "none", NULL },
2794 { "hp_only", tune_hp_only },
2795 { "swap_hp", tune_swap_hp },
2796 { "swap_surround", tune_swap_surround },
2797 { "ad_sharing", tune_ad_sharing },
2798 { "alc_jack", tune_alc_jack },
2799 { "inv_eapd", tune_inv_eapd },
2800 { "mute_led", tune_mute_led },
Matthew Garretta0faefe2005-12-06 13:59:12 +01002801 { "hp_mute_led", tune_hp_mute_led },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802};
2803
2804/* apply the quirk with the given type */
Takashi Iwaiee423812005-11-17 14:21:36 +01002805static int apply_quirk(struct snd_ac97 *ac97, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
2807 if (type <= 0)
2808 return 0;
2809 else if (type >= ARRAY_SIZE(applicable_quirks))
2810 return -EINVAL;
2811 if (applicable_quirks[type].func)
2812 return applicable_quirks[type].func(ac97);
2813 return 0;
2814}
2815
2816/* apply the quirk with the given name */
Takashi Iwaiee423812005-11-17 14:21:36 +01002817static int apply_quirk_str(struct snd_ac97 *ac97, const char *typestr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
2819 int i;
2820 struct quirk_table *q;
2821
2822 for (i = 0; i < ARRAY_SIZE(applicable_quirks); i++) {
2823 q = &applicable_quirks[i];
2824 if (q->name && ! strcmp(typestr, q->name))
2825 return apply_quirk(ac97, i);
2826 }
2827 /* for compatibility, accept the numbers, too */
2828 if (*typestr >= '0' && *typestr <= '9')
2829 return apply_quirk(ac97, (int)simple_strtoul(typestr, NULL, 10));
2830 return -EINVAL;
2831}
2832
2833/**
2834 * snd_ac97_tune_hardware - tune up the hardware
2835 * @ac97: the ac97 instance
2836 * @quirk: quirk list
2837 * @override: explicit quirk value (overrides the list if non-NULL)
2838 *
2839 * Do some workaround for each pci device, such as renaming of the
2840 * headphone (true line-out) control as "Master".
2841 * The quirk-list must be terminated with a zero-filled entry.
2842 *
2843 * Returns zero if successful, or a negative error code on failure.
2844 */
2845
Takashi Iwaiee423812005-11-17 14:21:36 +01002846int snd_ac97_tune_hardware(struct snd_ac97 *ac97, struct ac97_quirk *quirk, const char *override)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
2848 int result;
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 /* quirk overriden? */
2851 if (override && strcmp(override, "-1") && strcmp(override, "default")) {
2852 result = apply_quirk_str(ac97, override);
2853 if (result < 0)
2854 snd_printk(KERN_ERR "applying quirk type %s failed (%d)\n", override, result);
2855 return result;
2856 }
2857
Philip Prindeville4b499482005-08-12 16:46:17 +02002858 if (! quirk)
2859 return -EINVAL;
2860
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002861 for (; quirk->subvendor; quirk++) {
2862 if (quirk->subvendor != ac97->subsystem_vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 continue;
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002864 if ((! quirk->mask && quirk->subdevice == ac97->subsystem_device) ||
2865 quirk->subdevice == (quirk->mask & ac97->subsystem_device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (quirk->codec_id && quirk->codec_id != ac97->id)
2867 continue;
2868 snd_printdd("ac97 quirk for %s (%04x:%04x)\n", quirk->name, ac97->subsystem_vendor, ac97->subsystem_device);
2869 result = apply_quirk(ac97, quirk->type);
2870 if (result < 0)
2871 snd_printk(KERN_ERR "applying quirk type %d for %s failed (%d)\n", quirk->type, quirk->name, result);
2872 return result;
2873 }
2874 }
2875 return 0;
2876}
2877
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878EXPORT_SYMBOL(snd_ac97_tune_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880/*
2881 * INIT part
2882 */
2883
2884static int __init alsa_ac97_init(void)
2885{
2886 return 0;
2887}
2888
2889static void __exit alsa_ac97_exit(void)
2890{
2891}
2892
2893module_init(alsa_ac97_init)
2894module_exit(alsa_ac97_exit)