[ALSA] ali5451 - Code clean up, irq handler fix
[linux-block.git] / sound / pci / hda / patch_sigmatel.c
CommitLineData
2f2f4251
M
1/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * HD audio interface patch for SigmaTel STAC92xx
5 *
6 * Copyright (c) 2005 Embedded Alley Solutions, Inc.
403d1944 7 * Matt Porter <mporter@embeddedalley.com>
2f2f4251
M
8 *
9 * Based on patch_cmedia.c and patch_realtek.c
10 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
11 *
12 * This driver is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This driver is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27#include <sound/driver.h>
28#include <linux/init.h>
29#include <linux/delay.h>
30#include <linux/slab.h>
31#include <linux/pci.h>
32#include <sound/core.h>
c7d4b2fa 33#include <sound/asoundef.h>
2f2f4251
M
34#include "hda_codec.h"
35#include "hda_local.h"
36
4e55096e
M
37#define NUM_CONTROL_ALLOC 32
38#define STAC_HP_EVENT 0x37
4e55096e 39
f5fcc13c
TI
40enum {
41 STAC_REF,
42 STAC_9200_MODELS
43};
44
45enum {
46 STAC_9205_REF,
47 STAC_9205_MODELS
48};
49
8e21c34c
TD
50enum {
51 STAC_925x_REF,
52 STAC_M2_2,
53 STAC_MA6,
54 STAC_925x_MODELS
55};
56
f5fcc13c
TI
57enum {
58 STAC_D945_REF,
59 STAC_D945GTP3,
60 STAC_D945GTP5,
61 STAC_MACMINI,
3fc24d85 62 STAC_MACBOOK,
6f0778d8
NB
63 STAC_MACBOOK_PRO_V1,
64 STAC_MACBOOK_PRO_V2,
f5fcc13c
TI
65 STAC_922X_MODELS
66};
67
68enum {
69 STAC_D965_REF,
70 STAC_D965_3ST,
71 STAC_D965_5ST,
72 STAC_927X_MODELS
73};
403d1944 74
2f2f4251 75struct sigmatel_spec {
c8b6bf9b 76 struct snd_kcontrol_new *mixers[4];
c7d4b2fa
M
77 unsigned int num_mixers;
78
403d1944 79 int board_config;
c7d4b2fa 80 unsigned int surr_switch: 1;
403d1944
MP
81 unsigned int line_switch: 1;
82 unsigned int mic_switch: 1;
3cc08dc6 83 unsigned int alt_switch: 1;
82bc955f 84 unsigned int hp_detect: 1;
62fe78e9 85 unsigned int gpio_mute: 1;
c7d4b2fa 86
2f2f4251
M
87 /* playback */
88 struct hda_multi_out multiout;
3cc08dc6 89 hda_nid_t dac_nids[5];
2f2f4251
M
90
91 /* capture */
92 hda_nid_t *adc_nids;
2f2f4251 93 unsigned int num_adcs;
dabbed6f
M
94 hda_nid_t *mux_nids;
95 unsigned int num_muxes;
8b65727b
MP
96 hda_nid_t *dmic_nids;
97 unsigned int num_dmics;
98 hda_nid_t dmux_nid;
dabbed6f 99 hda_nid_t dig_in_nid;
2f2f4251 100
2f2f4251
M
101 /* pin widgets */
102 hda_nid_t *pin_nids;
103 unsigned int num_pins;
2f2f4251 104 unsigned int *pin_configs;
11b44bbd 105 unsigned int *bios_pin_configs;
2f2f4251
M
106
107 /* codec specific stuff */
108 struct hda_verb *init;
c8b6bf9b 109 struct snd_kcontrol_new *mixer;
2f2f4251
M
110
111 /* capture source */
8b65727b
MP
112 struct hda_input_mux *dinput_mux;
113 unsigned int cur_dmux;
c7d4b2fa 114 struct hda_input_mux *input_mux;
3cc08dc6 115 unsigned int cur_mux[3];
2f2f4251 116
403d1944
MP
117 /* i/o switches */
118 unsigned int io_switch[2];
2f2f4251 119
c7d4b2fa
M
120 struct hda_pcm pcm_rec[2]; /* PCM information */
121
122 /* dynamic controls and input_mux */
123 struct auto_pin_cfg autocfg;
124 unsigned int num_kctl_alloc, num_kctl_used;
c8b6bf9b 125 struct snd_kcontrol_new *kctl_alloc;
8b65727b 126 struct hda_input_mux private_dimux;
c7d4b2fa 127 struct hda_input_mux private_imux;
2f2f4251
M
128};
129
130static hda_nid_t stac9200_adc_nids[1] = {
131 0x03,
132};
133
134static hda_nid_t stac9200_mux_nids[1] = {
135 0x0c,
136};
137
138static hda_nid_t stac9200_dac_nids[1] = {
139 0x02,
140};
141
8e21c34c
TD
142static hda_nid_t stac925x_adc_nids[1] = {
143 0x03,
144};
145
146static hda_nid_t stac925x_mux_nids[1] = {
147 0x0f,
148};
149
150static hda_nid_t stac925x_dac_nids[1] = {
151 0x02,
152};
153
2f2f4251
M
154static hda_nid_t stac922x_adc_nids[2] = {
155 0x06, 0x07,
156};
157
158static hda_nid_t stac922x_mux_nids[2] = {
159 0x12, 0x13,
160};
161
3cc08dc6
MP
162static hda_nid_t stac927x_adc_nids[3] = {
163 0x07, 0x08, 0x09
164};
165
166static hda_nid_t stac927x_mux_nids[3] = {
167 0x15, 0x16, 0x17
168};
169
f3302a59
MP
170static hda_nid_t stac9205_adc_nids[2] = {
171 0x12, 0x13
172};
173
174static hda_nid_t stac9205_mux_nids[2] = {
175 0x19, 0x1a
176};
177
2549413e
TI
178static hda_nid_t stac9205_dmic_nids[2] = {
179 0x17, 0x18,
8b65727b
MP
180};
181
c7d4b2fa 182static hda_nid_t stac9200_pin_nids[8] = {
93ed1503
TD
183 0x08, 0x09, 0x0d, 0x0e,
184 0x0f, 0x10, 0x11, 0x12,
2f2f4251
M
185};
186
8e21c34c
TD
187static hda_nid_t stac925x_pin_nids[8] = {
188 0x07, 0x08, 0x0a, 0x0b,
189 0x0c, 0x0d, 0x10, 0x11,
190};
191
2f2f4251
M
192static hda_nid_t stac922x_pin_nids[10] = {
193 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
194 0x0f, 0x10, 0x11, 0x15, 0x1b,
195};
196
3cc08dc6
MP
197static hda_nid_t stac927x_pin_nids[14] = {
198 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
199 0x0f, 0x10, 0x11, 0x12, 0x13,
200 0x14, 0x21, 0x22, 0x23,
201};
202
f3302a59
MP
203static hda_nid_t stac9205_pin_nids[12] = {
204 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
205 0x0f, 0x14, 0x16, 0x17, 0x18,
206 0x21, 0x22,
207
208};
209
8b65727b
MP
210static int stac92xx_dmux_enum_info(struct snd_kcontrol *kcontrol,
211 struct snd_ctl_elem_info *uinfo)
212{
213 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
214 struct sigmatel_spec *spec = codec->spec;
215 return snd_hda_input_mux_info(spec->dinput_mux, uinfo);
216}
217
218static int stac92xx_dmux_enum_get(struct snd_kcontrol *kcontrol,
219 struct snd_ctl_elem_value *ucontrol)
220{
221 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
222 struct sigmatel_spec *spec = codec->spec;
223
224 ucontrol->value.enumerated.item[0] = spec->cur_dmux;
225 return 0;
226}
227
228static int stac92xx_dmux_enum_put(struct snd_kcontrol *kcontrol,
229 struct snd_ctl_elem_value *ucontrol)
230{
231 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
232 struct sigmatel_spec *spec = codec->spec;
233
234 return snd_hda_input_mux_put(codec, spec->dinput_mux, ucontrol,
235 spec->dmux_nid, &spec->cur_dmux);
236}
237
c8b6bf9b 238static int stac92xx_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2f2f4251
M
239{
240 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
241 struct sigmatel_spec *spec = codec->spec;
c7d4b2fa 242 return snd_hda_input_mux_info(spec->input_mux, uinfo);
2f2f4251
M
243}
244
c8b6bf9b 245static int stac92xx_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2f2f4251
M
246{
247 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
248 struct sigmatel_spec *spec = codec->spec;
249 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
250
251 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
252 return 0;
253}
254
c8b6bf9b 255static int stac92xx_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2f2f4251
M
256{
257 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
258 struct sigmatel_spec *spec = codec->spec;
259 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
260
c7d4b2fa 261 return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
2f2f4251
M
262 spec->mux_nids[adc_idx], &spec->cur_mux[adc_idx]);
263}
264
c7d4b2fa 265static struct hda_verb stac9200_core_init[] = {
2f2f4251 266 /* set dac0mux for dac converter */
c7d4b2fa 267 { 0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
2f2f4251
M
268 {}
269};
270
8e21c34c
TD
271static struct hda_verb stac925x_core_init[] = {
272 /* set dac0mux for dac converter */
273 { 0x06, AC_VERB_SET_CONNECT_SEL, 0x00},
274 {}
275};
276
c7d4b2fa 277static struct hda_verb stac922x_core_init[] = {
2f2f4251 278 /* set master volume and direct control */
c7d4b2fa 279 { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
2f2f4251
M
280 {}
281};
282
93ed1503 283static struct hda_verb d965_core_init[] = {
19039bd0 284 /* set master volume and direct control */
93ed1503 285 { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
19039bd0
TI
286 /* unmute node 0x1b */
287 { 0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
288 /* select node 0x03 as DAC */
289 { 0x0b, AC_VERB_SET_CONNECT_SEL, 0x01},
290 {}
291};
292
3cc08dc6
MP
293static struct hda_verb stac927x_core_init[] = {
294 /* set master volume and direct control */
295 { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
296 {}
297};
298
f3302a59
MP
299static struct hda_verb stac9205_core_init[] = {
300 /* set master volume and direct control */
301 { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
302 {}
303};
304
c8b6bf9b 305static struct snd_kcontrol_new stac9200_mixer[] = {
2f2f4251
M
306 HDA_CODEC_VOLUME("Master Playback Volume", 0xb, 0, HDA_OUTPUT),
307 HDA_CODEC_MUTE("Master Playback Switch", 0xb, 0, HDA_OUTPUT),
308 {
309 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
310 .name = "Input Source",
311 .count = 1,
312 .info = stac92xx_mux_enum_info,
313 .get = stac92xx_mux_enum_get,
314 .put = stac92xx_mux_enum_put,
315 },
316 HDA_CODEC_VOLUME("Capture Volume", 0x0a, 0, HDA_OUTPUT),
317 HDA_CODEC_MUTE("Capture Switch", 0x0a, 0, HDA_OUTPUT),
c7d4b2fa 318 HDA_CODEC_VOLUME("Capture Mux Volume", 0x0c, 0, HDA_OUTPUT),
2f2f4251
M
319 { } /* end */
320};
321
8e21c34c
TD
322static struct snd_kcontrol_new stac925x_mixer[] = {
323 HDA_CODEC_VOLUME("Master Playback Volume", 0xe, 0, HDA_OUTPUT),
324 HDA_CODEC_MUTE("Master Playback Switch", 0xe, 0, HDA_OUTPUT),
325 {
326 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
327 .name = "Input Source",
328 .count = 1,
329 .info = stac92xx_mux_enum_info,
330 .get = stac92xx_mux_enum_get,
331 .put = stac92xx_mux_enum_put,
332 },
333 HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_OUTPUT),
334 HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_OUTPUT),
335 HDA_CODEC_VOLUME("Capture Mux Volume", 0x0f, 0, HDA_OUTPUT),
336 { } /* end */
337};
338
c7d4b2fa 339/* This needs to be generated dynamically based on sequence */
c8b6bf9b 340static struct snd_kcontrol_new stac922x_mixer[] = {
2f2f4251
M
341 {
342 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
343 .name = "Input Source",
344 .count = 1,
345 .info = stac92xx_mux_enum_info,
346 .get = stac92xx_mux_enum_get,
347 .put = stac92xx_mux_enum_put,
348 },
349 HDA_CODEC_VOLUME("Capture Volume", 0x17, 0x0, HDA_INPUT),
0fd1708a 350 HDA_CODEC_MUTE("Capture Switch", 0x17, 0x0, HDA_INPUT),
2f2f4251
M
351 HDA_CODEC_VOLUME("Mux Capture Volume", 0x12, 0x0, HDA_OUTPUT),
352 { } /* end */
353};
354
19039bd0
TI
355/* This needs to be generated dynamically based on sequence */
356static struct snd_kcontrol_new stac9227_mixer[] = {
357 {
358 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
359 .name = "Input Source",
360 .count = 1,
361 .info = stac92xx_mux_enum_info,
362 .get = stac92xx_mux_enum_get,
363 .put = stac92xx_mux_enum_put,
364 },
365 HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
366 HDA_CODEC_MUTE("Capture Switch", 0x1b, 0x0, HDA_OUTPUT),
367 { } /* end */
368};
369
d1d985f0 370static struct snd_kcontrol_new stac927x_mixer[] = {
3cc08dc6
MP
371 {
372 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
373 .name = "Input Source",
374 .count = 1,
375 .info = stac92xx_mux_enum_info,
376 .get = stac92xx_mux_enum_get,
377 .put = stac92xx_mux_enum_put,
378 },
379 HDA_CODEC_VOLUME("InMux Capture Volume", 0x15, 0x0, HDA_OUTPUT),
380 HDA_CODEC_VOLUME("InVol Capture Volume", 0x18, 0x0, HDA_INPUT),
381 HDA_CODEC_MUTE("ADCMux Capture Switch", 0x1b, 0x0, HDA_OUTPUT),
382 { } /* end */
383};
384
d1d985f0 385static struct snd_kcontrol_new stac9205_mixer[] = {
8b65727b
MP
386 {
387 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
388 .name = "Digital Input Source",
389 .count = 1,
390 .info = stac92xx_dmux_enum_info,
391 .get = stac92xx_dmux_enum_get,
392 .put = stac92xx_dmux_enum_put,
393 },
f3302a59
MP
394 {
395 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
396 .name = "Input Source",
397 .count = 1,
398 .info = stac92xx_mux_enum_info,
399 .get = stac92xx_mux_enum_get,
400 .put = stac92xx_mux_enum_put,
401 },
402 HDA_CODEC_VOLUME("InMux Capture Volume", 0x19, 0x0, HDA_OUTPUT),
403 HDA_CODEC_VOLUME("InVol Capture Volume", 0x1b, 0x0, HDA_INPUT),
404 HDA_CODEC_MUTE("ADCMux Capture Switch", 0x1d, 0x0, HDA_OUTPUT),
405 { } /* end */
406};
407
2f2f4251
M
408static int stac92xx_build_controls(struct hda_codec *codec)
409{
410 struct sigmatel_spec *spec = codec->spec;
411 int err;
c7d4b2fa 412 int i;
2f2f4251
M
413
414 err = snd_hda_add_new_ctls(codec, spec->mixer);
415 if (err < 0)
416 return err;
c7d4b2fa
M
417
418 for (i = 0; i < spec->num_mixers; i++) {
419 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
420 if (err < 0)
421 return err;
422 }
423
dabbed6f
M
424 if (spec->multiout.dig_out_nid) {
425 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
426 if (err < 0)
427 return err;
428 }
429 if (spec->dig_in_nid) {
430 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
431 if (err < 0)
432 return err;
433 }
434 return 0;
2f2f4251
M
435}
436
403d1944 437static unsigned int ref9200_pin_configs[8] = {
dabbed6f 438 0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
2f2f4251
M
439 0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
440};
441
f5fcc13c
TI
442static unsigned int *stac9200_brd_tbl[STAC_9200_MODELS] = {
443 [STAC_REF] = ref9200_pin_configs,
403d1944
MP
444};
445
f5fcc13c
TI
446static const char *stac9200_models[STAC_9200_MODELS] = {
447 [STAC_REF] = "ref",
448};
449
450static struct snd_pci_quirk stac9200_cfg_tbl[] = {
451 /* SigmaTel reference board */
452 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
453 "DFI LanParty", STAC_REF),
e7377071 454 /* Dell laptops have BIOS problem */
f5fcc13c
TI
455 SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01b5,
456 "Dell Inspiron 630m", STAC_REF),
457 SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c2,
458 "Dell Latitude D620", STAC_REF),
459 SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cb,
460 "Dell Latitude 120L", STAC_REF),
877b866d
CT
461 SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cc,
462 "Dell Latitude D820", STAC_REF),
46f02ca3
MN
463 SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cd,
464 "Dell Inspiron E1705/9400", STAC_REF),
465 SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ce,
466 "Dell XPS M1710", STAC_REF),
f0f96745
TI
467 SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cf,
468 "Dell Precision M90", STAC_REF),
403d1944
MP
469 {} /* terminator */
470};
471
8e21c34c
TD
472static unsigned int ref925x_pin_configs[8] = {
473 0x40c003f0, 0x424503f2, 0x01813022, 0x02a19021,
474 0x90a70320, 0x02214210, 0x400003f1, 0x9033032e,
475};
476
477static unsigned int stac925x_MA6_pin_configs[8] = {
478 0x40c003f0, 0x424503f2, 0x01813022, 0x02a19021,
479 0x90a70320, 0x90100211, 0x400003f1, 0x9033032e,
480};
481
482static unsigned int stac925xM2_2_pin_configs[8] = {
483 0x40c003f3, 0x424503f2, 0x041800f4, 0x02a19020,
484 0x50a103F0, 0x90100210, 0x400003f1, 0x9033032e,
485};
486
487static unsigned int *stac925x_brd_tbl[STAC_925x_MODELS] = {
488 [STAC_REF] = ref925x_pin_configs,
489 [STAC_M2_2] = stac925xM2_2_pin_configs,
490 [STAC_MA6] = stac925x_MA6_pin_configs,
491};
492
493static const char *stac925x_models[STAC_925x_MODELS] = {
494 [STAC_REF] = "ref",
495 [STAC_M2_2] = "m2-2",
496 [STAC_MA6] = "m6",
497};
498
499static struct snd_pci_quirk stac925x_cfg_tbl[] = {
500 /* SigmaTel reference board */
501 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668, "DFI LanParty", STAC_REF),
502 SND_PCI_QUIRK(0x107b, 0x0316, "Gateway M255", STAC_REF),
503 SND_PCI_QUIRK(0x107b, 0x0366, "Gateway MP6954", STAC_REF),
504 SND_PCI_QUIRK(0x107b, 0x0461, "Gateway NX560XL", STAC_MA6),
505 SND_PCI_QUIRK(0x1002, 0x437b, "Gateway MX6453", STAC_M2_2),
506 {} /* terminator */
507};
508
403d1944
MP
509static unsigned int ref922x_pin_configs[10] = {
510 0x01014010, 0x01016011, 0x01012012, 0x0221401f,
511 0x01813122, 0x01011014, 0x01441030, 0x01c41030,
2f2f4251
M
512 0x40000100, 0x40000100,
513};
514
403d1944 515static unsigned int d945gtp3_pin_configs[10] = {
869264c4 516 0x0221401f, 0x01a19022, 0x01813021, 0x01014010,
403d1944
MP
517 0x40000100, 0x40000100, 0x40000100, 0x40000100,
518 0x02a19120, 0x40000100,
519};
520
521static unsigned int d945gtp5_pin_configs[10] = {
869264c4
MP
522 0x0221401f, 0x01011012, 0x01813024, 0x01014010,
523 0x01a19021, 0x01016011, 0x01452130, 0x40000100,
403d1944
MP
524 0x02a19320, 0x40000100,
525};
526
6f0778d8
NB
527static unsigned int macbook_pro_v1_pin_configs[10] = {
528 0x0321e230, 0x03a1e020, 0x9017e110, 0x01014010,
529 0x01a19021, 0x0381e021, 0x1345e240, 0x13c5e22e,
530 0x02a19320, 0x400000fb
531};
532
533static unsigned int macbook_pro_v2_pin_configs[10] = {
3fc24d85
TI
534 0x0221401f, 0x90a70120, 0x01813024, 0x01014010,
535 0x400000fd, 0x01016011, 0x1345e240, 0x13c5e22e,
536 0x400000fc, 0x400000fb,
537};
538
19039bd0 539static unsigned int *stac922x_brd_tbl[STAC_922X_MODELS] = {
f5fcc13c 540 [STAC_D945_REF] = ref922x_pin_configs,
19039bd0
TI
541 [STAC_D945GTP3] = d945gtp3_pin_configs,
542 [STAC_D945GTP5] = d945gtp5_pin_configs,
02a5039f
TI
543 [STAC_MACMINI] = macbook_pro_v1_pin_configs,
544 [STAC_MACBOOK] = macbook_pro_v1_pin_configs,
6f0778d8
NB
545 [STAC_MACBOOK_PRO_V1] = macbook_pro_v1_pin_configs,
546 [STAC_MACBOOK_PRO_V2] = macbook_pro_v2_pin_configs,
403d1944
MP
547};
548
f5fcc13c
TI
549static const char *stac922x_models[STAC_922X_MODELS] = {
550 [STAC_D945_REF] = "ref",
551 [STAC_D945GTP5] = "5stack",
552 [STAC_D945GTP3] = "3stack",
553 [STAC_MACMINI] = "macmini",
3fc24d85 554 [STAC_MACBOOK] = "macbook",
6f0778d8
NB
555 [STAC_MACBOOK_PRO_V1] = "macbook-pro-v1",
556 [STAC_MACBOOK_PRO_V2] = "macbook-pro",
f5fcc13c
TI
557};
558
559static struct snd_pci_quirk stac922x_cfg_tbl[] = {
560 /* SigmaTel reference board */
561 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
562 "DFI LanParty", STAC_D945_REF),
563 /* Intel 945G based systems */
564 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0101,
565 "Intel D945G", STAC_D945GTP3),
566 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0202,
567 "Intel D945G", STAC_D945GTP3),
568 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0606,
569 "Intel D945G", STAC_D945GTP3),
570 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0601,
571 "Intel D945G", STAC_D945GTP3),
572 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0111,
573 "Intel D945G", STAC_D945GTP3),
574 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1115,
575 "Intel D945G", STAC_D945GTP3),
576 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1116,
577 "Intel D945G", STAC_D945GTP3),
578 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1117,
579 "Intel D945G", STAC_D945GTP3),
580 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1118,
581 "Intel D945G", STAC_D945GTP3),
582 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1119,
583 "Intel D945G", STAC_D945GTP3),
584 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x8826,
585 "Intel D945G", STAC_D945GTP3),
586 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5049,
587 "Intel D945G", STAC_D945GTP3),
588 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5055,
589 "Intel D945G", STAC_D945GTP3),
590 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5048,
591 "Intel D945G", STAC_D945GTP3),
592 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0110,
593 "Intel D945G", STAC_D945GTP3),
594 /* Intel D945G 5-stack systems */
595 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0404,
596 "Intel D945G", STAC_D945GTP5),
597 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0303,
598 "Intel D945G", STAC_D945GTP5),
599 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0013,
600 "Intel D945G", STAC_D945GTP5),
601 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0417,
602 "Intel D945G", STAC_D945GTP5),
603 /* Intel 945P based systems */
604 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0b0b,
605 "Intel D945P", STAC_D945GTP3),
606 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0112,
607 "Intel D945P", STAC_D945GTP3),
608 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0d0d,
609 "Intel D945P", STAC_D945GTP3),
610 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0909,
611 "Intel D945P", STAC_D945GTP3),
612 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0505,
613 "Intel D945P", STAC_D945GTP3),
614 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0707,
615 "Intel D945P", STAC_D945GTP5),
616 /* other systems */
617 /* Apple Mac Mini (early 2006) */
618 SND_PCI_QUIRK(0x8384, 0x7680,
619 "Mac Mini", STAC_MACMINI),
403d1944
MP
620 {} /* terminator */
621};
622
3cc08dc6 623static unsigned int ref927x_pin_configs[14] = {
93ed1503
TD
624 0x02214020, 0x02a19080, 0x0181304e, 0x01014010,
625 0x01a19040, 0x01011012, 0x01016011, 0x0101201f,
626 0x183301f0, 0x18a001f0, 0x18a001f0, 0x01442070,
627 0x01c42190, 0x40000100,
3cc08dc6
MP
628};
629
93ed1503 630static unsigned int d965_3st_pin_configs[14] = {
81d3dbde
TD
631 0x0221401f, 0x02a19120, 0x40000100, 0x01014011,
632 0x01a19021, 0x01813024, 0x40000100, 0x40000100,
633 0x40000100, 0x40000100, 0x40000100, 0x40000100,
634 0x40000100, 0x40000100
635};
636
93ed1503
TD
637static unsigned int d965_5st_pin_configs[14] = {
638 0x02214020, 0x02a19080, 0x0181304e, 0x01014010,
639 0x01a19040, 0x01011012, 0x01016011, 0x40000100,
640 0x40000100, 0x40000100, 0x40000100, 0x01442070,
641 0x40000100, 0x40000100
642};
643
644static unsigned int *stac927x_brd_tbl[STAC_927X_MODELS] = {
f5fcc13c 645 [STAC_D965_REF] = ref927x_pin_configs,
93ed1503
TD
646 [STAC_D965_3ST] = d965_3st_pin_configs,
647 [STAC_D965_5ST] = d965_5st_pin_configs,
3cc08dc6
MP
648};
649
f5fcc13c
TI
650static const char *stac927x_models[STAC_927X_MODELS] = {
651 [STAC_D965_REF] = "ref",
652 [STAC_D965_3ST] = "3stack",
653 [STAC_D965_5ST] = "5stack",
654};
655
656static struct snd_pci_quirk stac927x_cfg_tbl[] = {
657 /* SigmaTel reference board */
658 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
659 "DFI LanParty", STAC_D965_REF),
81d3dbde 660 /* Intel 946 based systems */
f5fcc13c
TI
661 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x3d01, "Intel D946", STAC_D965_3ST),
662 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0xa301, "Intel D946", STAC_D965_3ST),
93ed1503 663 /* 965 based 3 stack systems */
f5fcc13c
TI
664 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2116, "Intel D965", STAC_D965_3ST),
665 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2115, "Intel D965", STAC_D965_3ST),
666 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2114, "Intel D965", STAC_D965_3ST),
667 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2113, "Intel D965", STAC_D965_3ST),
668 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2112, "Intel D965", STAC_D965_3ST),
669 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2111, "Intel D965", STAC_D965_3ST),
670 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2110, "Intel D965", STAC_D965_3ST),
671 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2009, "Intel D965", STAC_D965_3ST),
672 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2008, "Intel D965", STAC_D965_3ST),
673 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2007, "Intel D965", STAC_D965_3ST),
674 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2006, "Intel D965", STAC_D965_3ST),
675 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2005, "Intel D965", STAC_D965_3ST),
676 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2004, "Intel D965", STAC_D965_3ST),
677 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2003, "Intel D965", STAC_D965_3ST),
678 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2002, "Intel D965", STAC_D965_3ST),
679 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2001, "Intel D965", STAC_D965_3ST),
93ed1503 680 /* 965 based 5 stack systems */
f5fcc13c
TI
681 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2301, "Intel D965", STAC_D965_5ST),
682 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2302, "Intel D965", STAC_D965_5ST),
683 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2303, "Intel D965", STAC_D965_5ST),
684 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2304, "Intel D965", STAC_D965_5ST),
685 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2305, "Intel D965", STAC_D965_5ST),
686 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2501, "Intel D965", STAC_D965_5ST),
687 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2502, "Intel D965", STAC_D965_5ST),
688 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2503, "Intel D965", STAC_D965_5ST),
689 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2504, "Intel D965", STAC_D965_5ST),
3cc08dc6
MP
690 {} /* terminator */
691};
692
f3302a59
MP
693static unsigned int ref9205_pin_configs[12] = {
694 0x40000100, 0x40000100, 0x01016011, 0x01014010,
8b65727b
MP
695 0x01813122, 0x01a19021, 0x40000100, 0x40000100,
696 0x90a000f0, 0x90a000f0, 0x01441030, 0x01c41030
f3302a59
MP
697};
698
f5fcc13c 699static unsigned int *stac9205_brd_tbl[STAC_9205_MODELS] = {
f3302a59
MP
700 ref9205_pin_configs,
701};
702
f5fcc13c
TI
703static const char *stac9205_models[STAC_9205_MODELS] = {
704 [STAC_9205_REF] = "ref",
705};
706
707static struct snd_pci_quirk stac9205_cfg_tbl[] = {
708 /* SigmaTel reference board */
709 SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
710 "DFI LanParty", STAC_9205_REF),
f3302a59
MP
711 {} /* terminator */
712};
713
11b44bbd
RF
714static int stac92xx_save_bios_config_regs(struct hda_codec *codec)
715{
716 int i;
717 struct sigmatel_spec *spec = codec->spec;
718
719 if (! spec->bios_pin_configs) {
720 spec->bios_pin_configs = kcalloc(spec->num_pins,
721 sizeof(*spec->bios_pin_configs), GFP_KERNEL);
722 if (! spec->bios_pin_configs)
723 return -ENOMEM;
724 }
725
726 for (i = 0; i < spec->num_pins; i++) {
727 hda_nid_t nid = spec->pin_nids[i];
728 unsigned int pin_cfg;
729
730 pin_cfg = snd_hda_codec_read(codec, nid, 0,
731 AC_VERB_GET_CONFIG_DEFAULT, 0x00);
732 snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x bios pin config %8.8x\n",
733 nid, pin_cfg);
734 spec->bios_pin_configs[i] = pin_cfg;
735 }
736
737 return 0;
738}
739
2f2f4251
M
740static void stac92xx_set_config_regs(struct hda_codec *codec)
741{
742 int i;
743 struct sigmatel_spec *spec = codec->spec;
744 unsigned int pin_cfg;
745
11b44bbd
RF
746 if (! spec->pin_nids || ! spec->pin_configs)
747 return;
748
749 for (i = 0; i < spec->num_pins; i++) {
2f2f4251
M
750 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
751 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
752 spec->pin_configs[i] & 0x000000ff);
753 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
754 AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
755 (spec->pin_configs[i] & 0x0000ff00) >> 8);
756 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
757 AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
758 (spec->pin_configs[i] & 0x00ff0000) >> 16);
759 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
760 AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
761 spec->pin_configs[i] >> 24);
762 pin_cfg = snd_hda_codec_read(codec, spec->pin_nids[i], 0,
763 AC_VERB_GET_CONFIG_DEFAULT,
764 0x00);
403d1944 765 snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x pin config %8.8x\n", spec->pin_nids[i], pin_cfg);
2f2f4251
M
766 }
767}
2f2f4251 768
dabbed6f 769/*
c7d4b2fa 770 * Analog playback callbacks
dabbed6f 771 */
c7d4b2fa
M
772static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
773 struct hda_codec *codec,
c8b6bf9b 774 struct snd_pcm_substream *substream)
2f2f4251 775{
dabbed6f 776 struct sigmatel_spec *spec = codec->spec;
c7d4b2fa 777 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
2f2f4251
M
778}
779
2f2f4251
M
780static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
781 struct hda_codec *codec,
782 unsigned int stream_tag,
783 unsigned int format,
c8b6bf9b 784 struct snd_pcm_substream *substream)
2f2f4251
M
785{
786 struct sigmatel_spec *spec = codec->spec;
403d1944 787 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
2f2f4251
M
788}
789
790static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
791 struct hda_codec *codec,
c8b6bf9b 792 struct snd_pcm_substream *substream)
2f2f4251
M
793{
794 struct sigmatel_spec *spec = codec->spec;
795 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
796}
797
dabbed6f
M
798/*
799 * Digital playback callbacks
800 */
801static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
802 struct hda_codec *codec,
c8b6bf9b 803 struct snd_pcm_substream *substream)
dabbed6f
M
804{
805 struct sigmatel_spec *spec = codec->spec;
806 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
807}
808
809static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
810 struct hda_codec *codec,
c8b6bf9b 811 struct snd_pcm_substream *substream)
dabbed6f
M
812{
813 struct sigmatel_spec *spec = codec->spec;
814 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
815}
816
817
2f2f4251
M
818/*
819 * Analog capture callbacks
820 */
821static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
822 struct hda_codec *codec,
823 unsigned int stream_tag,
824 unsigned int format,
c8b6bf9b 825 struct snd_pcm_substream *substream)
2f2f4251
M
826{
827 struct sigmatel_spec *spec = codec->spec;
828
829 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
830 stream_tag, 0, format);
831 return 0;
832}
833
834static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
835 struct hda_codec *codec,
c8b6bf9b 836 struct snd_pcm_substream *substream)
2f2f4251
M
837{
838 struct sigmatel_spec *spec = codec->spec;
839
840 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
841 return 0;
842}
843
dabbed6f
M
844static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
845 .substreams = 1,
846 .channels_min = 2,
847 .channels_max = 2,
848 /* NID is set in stac92xx_build_pcms */
849 .ops = {
850 .open = stac92xx_dig_playback_pcm_open,
851 .close = stac92xx_dig_playback_pcm_close
852 },
853};
854
855static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
856 .substreams = 1,
857 .channels_min = 2,
858 .channels_max = 2,
859 /* NID is set in stac92xx_build_pcms */
860};
861
2f2f4251
M
862static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
863 .substreams = 1,
864 .channels_min = 2,
c7d4b2fa 865 .channels_max = 8,
2f2f4251
M
866 .nid = 0x02, /* NID to query formats and rates */
867 .ops = {
868 .open = stac92xx_playback_pcm_open,
869 .prepare = stac92xx_playback_pcm_prepare,
870 .cleanup = stac92xx_playback_pcm_cleanup
871 },
872};
873
3cc08dc6
MP
874static struct hda_pcm_stream stac92xx_pcm_analog_alt_playback = {
875 .substreams = 1,
876 .channels_min = 2,
877 .channels_max = 2,
878 .nid = 0x06, /* NID to query formats and rates */
879 .ops = {
880 .open = stac92xx_playback_pcm_open,
881 .prepare = stac92xx_playback_pcm_prepare,
882 .cleanup = stac92xx_playback_pcm_cleanup
883 },
884};
885
2f2f4251
M
886static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
887 .substreams = 2,
888 .channels_min = 2,
889 .channels_max = 2,
3cc08dc6 890 /* NID is set in stac92xx_build_pcms */
2f2f4251
M
891 .ops = {
892 .prepare = stac92xx_capture_pcm_prepare,
893 .cleanup = stac92xx_capture_pcm_cleanup
894 },
895};
896
897static int stac92xx_build_pcms(struct hda_codec *codec)
898{
899 struct sigmatel_spec *spec = codec->spec;
900 struct hda_pcm *info = spec->pcm_rec;
901
902 codec->num_pcms = 1;
903 codec->pcm_info = info;
904
c7d4b2fa 905 info->name = "STAC92xx Analog";
2f2f4251 906 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
2f2f4251 907 info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
3cc08dc6
MP
908 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
909
910 if (spec->alt_switch) {
911 codec->num_pcms++;
912 info++;
913 info->name = "STAC92xx Analog Alt";
914 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_alt_playback;
915 }
2f2f4251 916
dabbed6f
M
917 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
918 codec->num_pcms++;
919 info++;
920 info->name = "STAC92xx Digital";
921 if (spec->multiout.dig_out_nid) {
922 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
923 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
924 }
925 if (spec->dig_in_nid) {
926 info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
927 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
928 }
929 }
930
2f2f4251
M
931 return 0;
932}
933
c960a03b
TI
934static unsigned int stac92xx_get_vref(struct hda_codec *codec, hda_nid_t nid)
935{
936 unsigned int pincap = snd_hda_param_read(codec, nid,
937 AC_PAR_PIN_CAP);
938 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
939 if (pincap & AC_PINCAP_VREF_100)
940 return AC_PINCTL_VREF_100;
941 if (pincap & AC_PINCAP_VREF_80)
942 return AC_PINCTL_VREF_80;
943 if (pincap & AC_PINCAP_VREF_50)
944 return AC_PINCTL_VREF_50;
945 if (pincap & AC_PINCAP_VREF_GRD)
946 return AC_PINCTL_VREF_GRD;
947 return 0;
948}
949
403d1944
MP
950static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
951
952{
953 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
954}
955
956static int stac92xx_io_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
957{
958 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
959 uinfo->count = 1;
960 uinfo->value.integer.min = 0;
961 uinfo->value.integer.max = 1;
962 return 0;
963}
964
965static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
966{
967 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
968 struct sigmatel_spec *spec = codec->spec;
969 int io_idx = kcontrol-> private_value & 0xff;
970
971 ucontrol->value.integer.value[0] = spec->io_switch[io_idx];
972 return 0;
973}
974
975static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
976{
977 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
978 struct sigmatel_spec *spec = codec->spec;
979 hda_nid_t nid = kcontrol->private_value >> 8;
980 int io_idx = kcontrol-> private_value & 0xff;
981 unsigned short val = ucontrol->value.integer.value[0];
982
983 spec->io_switch[io_idx] = val;
984
985 if (val)
986 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
c960a03b
TI
987 else {
988 unsigned int pinctl = AC_PINCTL_IN_EN;
989 if (io_idx) /* set VREF for mic */
990 pinctl |= stac92xx_get_vref(codec, nid);
991 stac92xx_auto_set_pinctl(codec, nid, pinctl);
992 }
403d1944
MP
993 return 1;
994}
995
996#define STAC_CODEC_IO_SWITCH(xname, xpval) \
997 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
998 .name = xname, \
999 .index = 0, \
1000 .info = stac92xx_io_switch_info, \
1001 .get = stac92xx_io_switch_get, \
1002 .put = stac92xx_io_switch_put, \
1003 .private_value = xpval, \
1004 }
1005
1006
c7d4b2fa
M
1007enum {
1008 STAC_CTL_WIDGET_VOL,
1009 STAC_CTL_WIDGET_MUTE,
403d1944 1010 STAC_CTL_WIDGET_IO_SWITCH,
c7d4b2fa
M
1011};
1012
c8b6bf9b 1013static struct snd_kcontrol_new stac92xx_control_templates[] = {
c7d4b2fa
M
1014 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
1015 HDA_CODEC_MUTE(NULL, 0, 0, 0),
403d1944 1016 STAC_CODEC_IO_SWITCH(NULL, 0),
c7d4b2fa
M
1017};
1018
1019/* add dynamic controls */
1020static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
1021{
c8b6bf9b 1022 struct snd_kcontrol_new *knew;
c7d4b2fa
M
1023
1024 if (spec->num_kctl_used >= spec->num_kctl_alloc) {
1025 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
1026
1027 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
1028 if (! knew)
1029 return -ENOMEM;
1030 if (spec->kctl_alloc) {
1031 memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
1032 kfree(spec->kctl_alloc);
1033 }
1034 spec->kctl_alloc = knew;
1035 spec->num_kctl_alloc = num;
1036 }
1037
1038 knew = &spec->kctl_alloc[spec->num_kctl_used];
1039 *knew = stac92xx_control_templates[type];
82fe0c58 1040 knew->name = kstrdup(name, GFP_KERNEL);
c7d4b2fa
M
1041 if (! knew->name)
1042 return -ENOMEM;
1043 knew->private_value = val;
1044 spec->num_kctl_used++;
1045 return 0;
1046}
1047
403d1944
MP
1048/* flag inputs as additional dynamic lineouts */
1049static int stac92xx_add_dyn_out_pins(struct hda_codec *codec, struct auto_pin_cfg *cfg)
1050{
1051 struct sigmatel_spec *spec = codec->spec;
1052
1053 switch (cfg->line_outs) {
1054 case 3:
1055 /* add line-in as side */
1056 if (cfg->input_pins[AUTO_PIN_LINE]) {
1057 cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_LINE];
1058 spec->line_switch = 1;
1059 cfg->line_outs++;
1060 }
1061 break;
1062 case 2:
1063 /* add line-in as clfe and mic as side */
1064 if (cfg->input_pins[AUTO_PIN_LINE]) {
1065 cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_LINE];
1066 spec->line_switch = 1;
1067 cfg->line_outs++;
1068 }
1069 if (cfg->input_pins[AUTO_PIN_MIC]) {
1070 cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_MIC];
1071 spec->mic_switch = 1;
1072 cfg->line_outs++;
1073 }
1074 break;
1075 case 1:
1076 /* add line-in as surr and mic as clfe */
1077 if (cfg->input_pins[AUTO_PIN_LINE]) {
1078 cfg->line_out_pins[1] = cfg->input_pins[AUTO_PIN_LINE];
1079 spec->line_switch = 1;
1080 cfg->line_outs++;
1081 }
1082 if (cfg->input_pins[AUTO_PIN_MIC]) {
1083 cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_MIC];
1084 spec->mic_switch = 1;
1085 cfg->line_outs++;
1086 }
1087 break;
1088 }
1089
1090 return 0;
1091}
1092
3cc08dc6
MP
1093/*
1094 * XXX The line_out pin widget connection list may not be set to the
1095 * desired DAC nid. This is the case on 927x where ports A and B can
1096 * be routed to several DACs.
1097 *
1098 * This requires an analysis of the line-out/hp pin configuration
1099 * to provide a best fit for pin/DAC configurations that are routable.
1100 * For now, 927x DAC4 is not supported and 927x DAC1 output to ports
1101 * A and B is not supported.
1102 */
c7d4b2fa 1103/* fill in the dac_nids table from the parsed pin configuration */
19039bd0
TI
1104static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec,
1105 const struct auto_pin_cfg *cfg)
c7d4b2fa
M
1106{
1107 struct sigmatel_spec *spec = codec->spec;
1108 hda_nid_t nid;
1109 int i;
1110
1111 /* check the pins hardwired to audio widget */
1112 for (i = 0; i < cfg->line_outs; i++) {
1113 nid = cfg->line_out_pins[i];
1114 spec->multiout.dac_nids[i] = snd_hda_codec_read(codec, nid, 0,
1115 AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1116 }
1117
82bc955f 1118 spec->multiout.num_dacs = cfg->line_outs;
c7d4b2fa
M
1119
1120 return 0;
1121}
1122
eb06ed8f
TI
1123/* create volume control/switch for the given prefx type */
1124static int create_controls(struct sigmatel_spec *spec, const char *pfx, hda_nid_t nid, int chs)
1125{
1126 char name[32];
1127 int err;
1128
1129 sprintf(name, "%s Playback Volume", pfx);
1130 err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
1131 HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1132 if (err < 0)
1133 return err;
1134 sprintf(name, "%s Playback Switch", pfx);
1135 err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
1136 HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1137 if (err < 0)
1138 return err;
1139 return 0;
1140}
1141
c7d4b2fa 1142/* add playback controls from the parsed DAC table */
19039bd0
TI
1143static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec,
1144 const struct auto_pin_cfg *cfg)
c7d4b2fa 1145{
19039bd0
TI
1146 static const char *chname[4] = {
1147 "Front", "Surround", NULL /*CLFE*/, "Side"
1148 };
c7d4b2fa
M
1149 hda_nid_t nid;
1150 int i, err;
1151
1152 for (i = 0; i < cfg->line_outs; i++) {
403d1944 1153 if (!spec->multiout.dac_nids[i])
c7d4b2fa
M
1154 continue;
1155
1156 nid = spec->multiout.dac_nids[i];
1157
1158 if (i == 2) {
1159 /* Center/LFE */
eb06ed8f
TI
1160 err = create_controls(spec, "Center", nid, 1);
1161 if (err < 0)
c7d4b2fa 1162 return err;
eb06ed8f
TI
1163 err = create_controls(spec, "LFE", nid, 2);
1164 if (err < 0)
c7d4b2fa
M
1165 return err;
1166 } else {
eb06ed8f
TI
1167 err = create_controls(spec, chname[i], nid, 3);
1168 if (err < 0)
c7d4b2fa
M
1169 return err;
1170 }
1171 }
1172
403d1944
MP
1173 if (spec->line_switch)
1174 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Line In as Output Switch", cfg->input_pins[AUTO_PIN_LINE] << 8)) < 0)
1175 return err;
1176
1177 if (spec->mic_switch)
1178 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Mic as Output Switch", (cfg->input_pins[AUTO_PIN_MIC] << 8) | 1)) < 0)
1179 return err;
1180
c7d4b2fa
M
1181 return 0;
1182}
1183
eb06ed8f 1184static int check_in_dac_nids(struct sigmatel_spec *spec, hda_nid_t nid)
c7d4b2fa 1185{
eb06ed8f 1186 int i;
c7d4b2fa 1187
eb06ed8f
TI
1188 for (i = 0; i < spec->multiout.num_dacs; i++) {
1189 if (spec->multiout.dac_nids[i] == nid)
1190 return 1;
1191 }
1192 if (spec->multiout.hp_nid == nid)
1193 return 1;
1194 return 0;
1195}
c7d4b2fa 1196
eb06ed8f
TI
1197static int add_spec_dacs(struct sigmatel_spec *spec, hda_nid_t nid)
1198{
1199 if (!spec->multiout.hp_nid)
1200 spec->multiout.hp_nid = nid;
1201 else if (spec->multiout.num_dacs > 4) {
1202 printk(KERN_WARNING "stac92xx: No space for DAC 0x%x\n", nid);
1203 return 1;
1204 } else {
1205 spec->multiout.dac_nids[spec->multiout.num_dacs] = nid;
1206 spec->multiout.num_dacs++;
1207 }
1208 return 0;
1209}
4e55096e 1210
eb06ed8f
TI
1211/* add playback controls for Speaker and HP outputs */
1212static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec,
1213 struct auto_pin_cfg *cfg)
1214{
1215 struct sigmatel_spec *spec = codec->spec;
1216 hda_nid_t nid;
1217 int i, old_num_dacs, err;
1218
1219 old_num_dacs = spec->multiout.num_dacs;
1220 for (i = 0; i < cfg->hp_outs; i++) {
1221 unsigned int wid_caps = get_wcaps(codec, cfg->hp_pins[i]);
1222 if (wid_caps & AC_WCAP_UNSOL_CAP)
1223 spec->hp_detect = 1;
1224 nid = snd_hda_codec_read(codec, cfg->hp_pins[i], 0,
1225 AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1226 if (check_in_dac_nids(spec, nid))
1227 nid = 0;
1228 if (! nid)
c7d4b2fa 1229 continue;
eb06ed8f
TI
1230 add_spec_dacs(spec, nid);
1231 }
1232 for (i = 0; i < cfg->speaker_outs; i++) {
1233 nid = snd_hda_codec_read(codec, cfg->speaker_pins[0], 0,
1234 AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1235 if (check_in_dac_nids(spec, nid))
1236 nid = 0;
1237 if (check_in_dac_nids(spec, nid))
1238 nid = 0;
1239 if (! nid)
1240 continue;
1241 add_spec_dacs(spec, nid);
c7d4b2fa
M
1242 }
1243
eb06ed8f
TI
1244 for (i = old_num_dacs; i < spec->multiout.num_dacs; i++) {
1245 static const char *pfxs[] = {
1246 "Speaker", "External Speaker", "Speaker2",
1247 };
1248 err = create_controls(spec, pfxs[i - old_num_dacs],
1249 spec->multiout.dac_nids[i], 3);
1250 if (err < 0)
1251 return err;
1252 }
1253 if (spec->multiout.hp_nid) {
1254 const char *pfx;
1255 if (old_num_dacs == spec->multiout.num_dacs)
1256 pfx = "Master";
1257 else
1258 pfx = "Headphone";
1259 err = create_controls(spec, pfx, spec->multiout.hp_nid, 3);
1260 if (err < 0)
1261 return err;
1262 }
c7d4b2fa
M
1263
1264 return 0;
1265}
1266
8b65727b 1267/* labels for dmic mux inputs */
ddc2cec4 1268static const char *stac92xx_dmic_labels[5] = {
8b65727b
MP
1269 "Analog Inputs", "Digital Mic 1", "Digital Mic 2",
1270 "Digital Mic 3", "Digital Mic 4"
1271};
1272
1273/* create playback/capture controls for input pins on dmic capable codecs */
1274static int stac92xx_auto_create_dmic_input_ctls(struct hda_codec *codec,
1275 const struct auto_pin_cfg *cfg)
1276{
1277 struct sigmatel_spec *spec = codec->spec;
1278 struct hda_input_mux *dimux = &spec->private_dimux;
1279 hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
1280 int i, j;
1281
1282 dimux->items[dimux->num_items].label = stac92xx_dmic_labels[0];
1283 dimux->items[dimux->num_items].index = 0;
1284 dimux->num_items++;
1285
1286 for (i = 0; i < spec->num_dmics; i++) {
1287 int index;
1288 int num_cons;
1289 unsigned int def_conf;
1290
1291 def_conf = snd_hda_codec_read(codec,
1292 spec->dmic_nids[i],
1293 0,
1294 AC_VERB_GET_CONFIG_DEFAULT,
1295 0);
1296 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
1297 continue;
1298
1299 num_cons = snd_hda_get_connections(codec,
1300 spec->dmux_nid,
1301 con_lst,
1302 HDA_MAX_NUM_INPUTS);
1303 for (j = 0; j < num_cons; j++)
1304 if (con_lst[j] == spec->dmic_nids[i]) {
1305 index = j;
1306 goto found;
1307 }
1308 continue;
1309found:
1310 dimux->items[dimux->num_items].label =
1311 stac92xx_dmic_labels[dimux->num_items];
1312 dimux->items[dimux->num_items].index = index;
1313 dimux->num_items++;
1314 }
1315
1316 return 0;
1317}
1318
c7d4b2fa
M
1319/* create playback/capture controls for input pins */
1320static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
1321{
1322 struct sigmatel_spec *spec = codec->spec;
c7d4b2fa
M
1323 struct hda_input_mux *imux = &spec->private_imux;
1324 hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
1325 int i, j, k;
1326
1327 for (i = 0; i < AUTO_PIN_LAST; i++) {
314634bc
TI
1328 int index;
1329
1330 if (!cfg->input_pins[i])
1331 continue;
1332 index = -1;
1333 for (j = 0; j < spec->num_muxes; j++) {
1334 int num_cons;
1335 num_cons = snd_hda_get_connections(codec,
1336 spec->mux_nids[j],
1337 con_lst,
1338 HDA_MAX_NUM_INPUTS);
1339 for (k = 0; k < num_cons; k++)
1340 if (con_lst[k] == cfg->input_pins[i]) {
1341 index = k;
1342 goto found;
1343 }
c7d4b2fa 1344 }
314634bc
TI
1345 continue;
1346 found:
1347 imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
1348 imux->items[imux->num_items].index = index;
1349 imux->num_items++;
c7d4b2fa
M
1350 }
1351
62fe78e9
SR
1352 if (imux->num_items == 1) {
1353 /*
1354 * Set the current input for the muxes.
1355 * The STAC9221 has two input muxes with identical source
1356 * NID lists. Hopefully this won't get confused.
1357 */
1358 for (i = 0; i < spec->num_muxes; i++) {
1359 snd_hda_codec_write(codec, spec->mux_nids[i], 0,
1360 AC_VERB_SET_CONNECT_SEL,
1361 imux->items[0].index);
1362 }
1363 }
1364
c7d4b2fa
M
1365 return 0;
1366}
1367
c7d4b2fa
M
1368static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
1369{
1370 struct sigmatel_spec *spec = codec->spec;
1371 int i;
1372
1373 for (i = 0; i < spec->autocfg.line_outs; i++) {
1374 hda_nid_t nid = spec->autocfg.line_out_pins[i];
1375 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
1376 }
1377}
1378
1379static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
1380{
1381 struct sigmatel_spec *spec = codec->spec;
eb06ed8f 1382 int i;
c7d4b2fa 1383
eb06ed8f
TI
1384 for (i = 0; i < spec->autocfg.hp_outs; i++) {
1385 hda_nid_t pin;
1386 pin = spec->autocfg.hp_pins[i];
1387 if (pin) /* connect to front */
1388 stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
1389 }
1390 for (i = 0; i < spec->autocfg.speaker_outs; i++) {
1391 hda_nid_t pin;
1392 pin = spec->autocfg.speaker_pins[i];
1393 if (pin) /* connect to front */
1394 stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN);
1395 }
c7d4b2fa
M
1396}
1397
3cc08dc6 1398static int stac92xx_parse_auto_config(struct hda_codec *codec, hda_nid_t dig_out, hda_nid_t dig_in)
c7d4b2fa
M
1399{
1400 struct sigmatel_spec *spec = codec->spec;
1401 int err;
1402
8b65727b
MP
1403 if ((err = snd_hda_parse_pin_def_config(codec,
1404 &spec->autocfg,
1405 spec->dmic_nids)) < 0)
c7d4b2fa 1406 return err;
82bc955f 1407 if (! spec->autocfg.line_outs)
869264c4 1408 return 0; /* can't find valid pin config */
19039bd0 1409
403d1944
MP
1410 if ((err = stac92xx_add_dyn_out_pins(codec, &spec->autocfg)) < 0)
1411 return err;
19039bd0
TI
1412 if (spec->multiout.num_dacs == 0)
1413 if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
1414 return err;
c7d4b2fa
M
1415
1416 if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
1417 (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
1418 (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
1419 return err;
1420
8b65727b
MP
1421 if (spec->num_dmics > 0)
1422 if ((err = stac92xx_auto_create_dmic_input_ctls(codec,
1423 &spec->autocfg)) < 0)
1424 return err;
1425
c7d4b2fa 1426 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
403d1944 1427 if (spec->multiout.max_channels > 2)
c7d4b2fa 1428 spec->surr_switch = 1;
c7d4b2fa 1429
82bc955f 1430 if (spec->autocfg.dig_out_pin)
3cc08dc6 1431 spec->multiout.dig_out_nid = dig_out;
82bc955f 1432 if (spec->autocfg.dig_in_pin)
3cc08dc6 1433 spec->dig_in_nid = dig_in;
c7d4b2fa
M
1434
1435 if (spec->kctl_alloc)
1436 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
1437
1438 spec->input_mux = &spec->private_imux;
8b65727b 1439 spec->dinput_mux = &spec->private_dimux;
c7d4b2fa
M
1440
1441 return 1;
1442}
1443
82bc955f
TI
1444/* add playback controls for HP output */
1445static int stac9200_auto_create_hp_ctls(struct hda_codec *codec,
1446 struct auto_pin_cfg *cfg)
1447{
1448 struct sigmatel_spec *spec = codec->spec;
eb06ed8f 1449 hda_nid_t pin = cfg->hp_pins[0];
82bc955f
TI
1450 unsigned int wid_caps;
1451
1452 if (! pin)
1453 return 0;
1454
1455 wid_caps = get_wcaps(codec, pin);
505cb341 1456 if (wid_caps & AC_WCAP_UNSOL_CAP)
82bc955f 1457 spec->hp_detect = 1;
82bc955f
TI
1458
1459 return 0;
1460}
1461
160ea0dc
RF
1462/* add playback controls for LFE output */
1463static int stac9200_auto_create_lfe_ctls(struct hda_codec *codec,
1464 struct auto_pin_cfg *cfg)
1465{
1466 struct sigmatel_spec *spec = codec->spec;
1467 int err;
1468 hda_nid_t lfe_pin = 0x0;
1469 int i;
1470
1471 /*
1472 * search speaker outs and line outs for a mono speaker pin
1473 * with an amp. If one is found, add LFE controls
1474 * for it.
1475 */
1476 for (i = 0; i < spec->autocfg.speaker_outs && lfe_pin == 0x0; i++) {
1477 hda_nid_t pin = spec->autocfg.speaker_pins[i];
1478 unsigned long wcaps = get_wcaps(codec, pin);
1479 wcaps &= (AC_WCAP_STEREO | AC_WCAP_OUT_AMP);
1480 if (wcaps == AC_WCAP_OUT_AMP)
1481 /* found a mono speaker with an amp, must be lfe */
1482 lfe_pin = pin;
1483 }
1484
1485 /* if speaker_outs is 0, then speakers may be in line_outs */
1486 if (lfe_pin == 0 && spec->autocfg.speaker_outs == 0) {
1487 for (i = 0; i < spec->autocfg.line_outs && lfe_pin == 0x0; i++) {
1488 hda_nid_t pin = spec->autocfg.line_out_pins[i];
1489 unsigned long cfg;
1490 cfg = snd_hda_codec_read(codec, pin, 0,
1491 AC_VERB_GET_CONFIG_DEFAULT,
1492 0x00);
1493 if (get_defcfg_device(cfg) == AC_JACK_SPEAKER) {
1494 unsigned long wcaps = get_wcaps(codec, pin);
1495 wcaps &= (AC_WCAP_STEREO | AC_WCAP_OUT_AMP);
1496 if (wcaps == AC_WCAP_OUT_AMP)
1497 /* found a mono speaker with an amp,
1498 must be lfe */
1499 lfe_pin = pin;
1500 }
1501 }
1502 }
1503
1504 if (lfe_pin) {
eb06ed8f 1505 err = create_controls(spec, "LFE", lfe_pin, 1);
160ea0dc
RF
1506 if (err < 0)
1507 return err;
1508 }
1509
1510 return 0;
1511}
1512
c7d4b2fa
M
1513static int stac9200_parse_auto_config(struct hda_codec *codec)
1514{
1515 struct sigmatel_spec *spec = codec->spec;
1516 int err;
1517
df694daa 1518 if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
c7d4b2fa
M
1519 return err;
1520
1521 if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
1522 return err;
1523
82bc955f
TI
1524 if ((err = stac9200_auto_create_hp_ctls(codec, &spec->autocfg)) < 0)
1525 return err;
1526
160ea0dc
RF
1527 if ((err = stac9200_auto_create_lfe_ctls(codec, &spec->autocfg)) < 0)
1528 return err;
1529
82bc955f 1530 if (spec->autocfg.dig_out_pin)
c7d4b2fa 1531 spec->multiout.dig_out_nid = 0x05;
82bc955f 1532 if (spec->autocfg.dig_in_pin)
c7d4b2fa 1533 spec->dig_in_nid = 0x04;
c7d4b2fa
M
1534
1535 if (spec->kctl_alloc)
1536 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
1537
1538 spec->input_mux = &spec->private_imux;
8b65727b 1539 spec->dinput_mux = &spec->private_dimux;
c7d4b2fa
M
1540
1541 return 1;
1542}
1543
62fe78e9
SR
1544/*
1545 * Early 2006 Intel Macintoshes with STAC9220X5 codecs seem to have a
1546 * funky external mute control using GPIO pins.
1547 */
1548
1549static void stac922x_gpio_mute(struct hda_codec *codec, int pin, int muted)
1550{
1551 unsigned int gpiostate, gpiomask, gpiodir;
1552
1553 gpiostate = snd_hda_codec_read(codec, codec->afg, 0,
1554 AC_VERB_GET_GPIO_DATA, 0);
1555
1556 if (!muted)
1557 gpiostate |= (1 << pin);
1558 else
1559 gpiostate &= ~(1 << pin);
1560
1561 gpiomask = snd_hda_codec_read(codec, codec->afg, 0,
1562 AC_VERB_GET_GPIO_MASK, 0);
1563 gpiomask |= (1 << pin);
1564
1565 gpiodir = snd_hda_codec_read(codec, codec->afg, 0,
1566 AC_VERB_GET_GPIO_DIRECTION, 0);
1567 gpiodir |= (1 << pin);
1568
1569 /* AppleHDA seems to do this -- WTF is this verb?? */
1570 snd_hda_codec_write(codec, codec->afg, 0, 0x7e7, 0);
1571
1572 snd_hda_codec_write(codec, codec->afg, 0,
1573 AC_VERB_SET_GPIO_MASK, gpiomask);
1574 snd_hda_codec_write(codec, codec->afg, 0,
1575 AC_VERB_SET_GPIO_DIRECTION, gpiodir);
1576
1577 msleep(1);
1578
1579 snd_hda_codec_write(codec, codec->afg, 0,
1580 AC_VERB_SET_GPIO_DATA, gpiostate);
1581}
1582
314634bc
TI
1583static void enable_pin_detect(struct hda_codec *codec, hda_nid_t nid,
1584 unsigned int event)
1585{
1586 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP)
1587 snd_hda_codec_write(codec, nid, 0,
1588 AC_VERB_SET_UNSOLICITED_ENABLE,
1589 (AC_USRSP_EN | event));
1590}
1591
c7d4b2fa
M
1592static int stac92xx_init(struct hda_codec *codec)
1593{
1594 struct sigmatel_spec *spec = codec->spec;
82bc955f
TI
1595 struct auto_pin_cfg *cfg = &spec->autocfg;
1596 int i;
c7d4b2fa 1597
c7d4b2fa
M
1598 snd_hda_sequence_write(codec, spec->init);
1599
82bc955f
TI
1600 /* set up pins */
1601 if (spec->hp_detect) {
505cb341 1602 /* Enable unsolicited responses on the HP widget */
eb06ed8f 1603 for (i = 0; i < cfg->hp_outs; i++)
314634bc
TI
1604 enable_pin_detect(codec, cfg->hp_pins[i],
1605 STAC_HP_EVENT);
0a07acaf
TI
1606 /* force to enable the first line-out; the others are set up
1607 * in unsol_event
1608 */
1609 stac92xx_auto_set_pinctl(codec, spec->autocfg.line_out_pins[0],
1610 AC_PINCTL_OUT_EN);
eb995a8c 1611 stac92xx_auto_init_hp_out(codec);
82bc955f
TI
1612 /* fake event to set up pins */
1613 codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
1614 } else {
1615 stac92xx_auto_init_multi_out(codec);
1616 stac92xx_auto_init_hp_out(codec);
1617 }
1618 for (i = 0; i < AUTO_PIN_LAST; i++) {
c960a03b
TI
1619 hda_nid_t nid = cfg->input_pins[i];
1620 if (nid) {
1621 unsigned int pinctl = AC_PINCTL_IN_EN;
1622 if (i == AUTO_PIN_MIC || i == AUTO_PIN_FRONT_MIC)
1623 pinctl |= stac92xx_get_vref(codec, nid);
1624 stac92xx_auto_set_pinctl(codec, nid, pinctl);
1625 }
82bc955f 1626 }
8b65727b
MP
1627 if (spec->num_dmics > 0)
1628 for (i = 0; i < spec->num_dmics; i++)
1629 stac92xx_auto_set_pinctl(codec, spec->dmic_nids[i],
1630 AC_PINCTL_IN_EN);
1631
82bc955f
TI
1632 if (cfg->dig_out_pin)
1633 stac92xx_auto_set_pinctl(codec, cfg->dig_out_pin,
1634 AC_PINCTL_OUT_EN);
1635 if (cfg->dig_in_pin)
1636 stac92xx_auto_set_pinctl(codec, cfg->dig_in_pin,
1637 AC_PINCTL_IN_EN);
1638
62fe78e9
SR
1639 if (spec->gpio_mute) {
1640 stac922x_gpio_mute(codec, 0, 0);
1641 stac922x_gpio_mute(codec, 1, 0);
1642 }
1643
c7d4b2fa
M
1644 return 0;
1645}
1646
2f2f4251
M
1647static void stac92xx_free(struct hda_codec *codec)
1648{
c7d4b2fa
M
1649 struct sigmatel_spec *spec = codec->spec;
1650 int i;
1651
1652 if (! spec)
1653 return;
1654
1655 if (spec->kctl_alloc) {
1656 for (i = 0; i < spec->num_kctl_used; i++)
1657 kfree(spec->kctl_alloc[i].name);
1658 kfree(spec->kctl_alloc);
1659 }
1660
11b44bbd
RF
1661 if (spec->bios_pin_configs)
1662 kfree(spec->bios_pin_configs);
1663
c7d4b2fa 1664 kfree(spec);
2f2f4251
M
1665}
1666
4e55096e
M
1667static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
1668 unsigned int flag)
1669{
1670 unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1671 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
314634bc
TI
1672 if (flag == AC_PINCTL_OUT_EN && (pin_ctl & AC_PINCTL_IN_EN))
1673 return;
4e55096e
M
1674 snd_hda_codec_write(codec, nid, 0,
1675 AC_VERB_SET_PIN_WIDGET_CONTROL,
1676 pin_ctl | flag);
1677}
1678
1679static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
1680 unsigned int flag)
1681{
1682 unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1683 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1684 snd_hda_codec_write(codec, nid, 0,
1685 AC_VERB_SET_PIN_WIDGET_CONTROL,
1686 pin_ctl & ~flag);
1687}
1688
314634bc
TI
1689static int get_pin_presence(struct hda_codec *codec, hda_nid_t nid)
1690{
1691 if (!nid)
1692 return 0;
1693 if (snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_PIN_SENSE, 0x00)
1694 & (1 << 31))
1695 return 1;
1696 return 0;
1697}
1698
1699static void stac92xx_hp_detect(struct hda_codec *codec, unsigned int res)
4e55096e
M
1700{
1701 struct sigmatel_spec *spec = codec->spec;
1702 struct auto_pin_cfg *cfg = &spec->autocfg;
1703 int i, presence;
1704
eb06ed8f
TI
1705 presence = 0;
1706 for (i = 0; i < cfg->hp_outs; i++) {
314634bc
TI
1707 presence = get_pin_presence(codec, cfg->hp_pins[i]);
1708 if (presence)
1709 break;
eb06ed8f 1710 }
4e55096e
M
1711
1712 if (presence) {
1713 /* disable lineouts, enable hp */
1714 for (i = 0; i < cfg->line_outs; i++)
1715 stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
1716 AC_PINCTL_OUT_EN);
eb06ed8f
TI
1717 for (i = 0; i < cfg->speaker_outs; i++)
1718 stac92xx_reset_pinctl(codec, cfg->speaker_pins[i],
1719 AC_PINCTL_OUT_EN);
4e55096e
M
1720 } else {
1721 /* enable lineouts, disable hp */
1722 for (i = 0; i < cfg->line_outs; i++)
1723 stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
1724 AC_PINCTL_OUT_EN);
eb06ed8f
TI
1725 for (i = 0; i < cfg->speaker_outs; i++)
1726 stac92xx_set_pinctl(codec, cfg->speaker_pins[i],
1727 AC_PINCTL_OUT_EN);
4e55096e
M
1728 }
1729}
1730
314634bc
TI
1731static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
1732{
1733 switch (res >> 26) {
1734 case STAC_HP_EVENT:
1735 stac92xx_hp_detect(codec, res);
1736 break;
1737 }
1738}
1739
ff6fdc37
M
1740#ifdef CONFIG_PM
1741static int stac92xx_resume(struct hda_codec *codec)
1742{
1743 struct sigmatel_spec *spec = codec->spec;
1744 int i;
1745
1746 stac92xx_init(codec);
11b44bbd 1747 stac92xx_set_config_regs(codec);
ff6fdc37
M
1748 for (i = 0; i < spec->num_mixers; i++)
1749 snd_hda_resume_ctls(codec, spec->mixers[i]);
1750 if (spec->multiout.dig_out_nid)
1751 snd_hda_resume_spdif_out(codec);
1752 if (spec->dig_in_nid)
1753 snd_hda_resume_spdif_in(codec);
1754
1755 return 0;
1756}
1757#endif
1758
2f2f4251
M
1759static struct hda_codec_ops stac92xx_patch_ops = {
1760 .build_controls = stac92xx_build_controls,
1761 .build_pcms = stac92xx_build_pcms,
1762 .init = stac92xx_init,
1763 .free = stac92xx_free,
4e55096e 1764 .unsol_event = stac92xx_unsol_event,
ff6fdc37
M
1765#ifdef CONFIG_PM
1766 .resume = stac92xx_resume,
1767#endif
2f2f4251
M
1768};
1769
1770static int patch_stac9200(struct hda_codec *codec)
1771{
1772 struct sigmatel_spec *spec;
c7d4b2fa 1773 int err;
2f2f4251 1774
e560d8d8 1775 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2f2f4251
M
1776 if (spec == NULL)
1777 return -ENOMEM;
1778
1779 codec->spec = spec;
11b44bbd
RF
1780 spec->num_pins = 8;
1781 spec->pin_nids = stac9200_pin_nids;
f5fcc13c
TI
1782 spec->board_config = snd_hda_check_board_config(codec, STAC_9200_MODELS,
1783 stac9200_models,
1784 stac9200_cfg_tbl);
11b44bbd
RF
1785 if (spec->board_config < 0) {
1786 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
1787 err = stac92xx_save_bios_config_regs(codec);
1788 if (err < 0) {
1789 stac92xx_free(codec);
1790 return err;
1791 }
1792 spec->pin_configs = spec->bios_pin_configs;
1793 } else {
403d1944
MP
1794 spec->pin_configs = stac9200_brd_tbl[spec->board_config];
1795 stac92xx_set_config_regs(codec);
1796 }
2f2f4251
M
1797
1798 spec->multiout.max_channels = 2;
1799 spec->multiout.num_dacs = 1;
1800 spec->multiout.dac_nids = stac9200_dac_nids;
1801 spec->adc_nids = stac9200_adc_nids;
1802 spec->mux_nids = stac9200_mux_nids;
dabbed6f 1803 spec->num_muxes = 1;
8b65727b 1804 spec->num_dmics = 0;
c7d4b2fa
M
1805
1806 spec->init = stac9200_core_init;
2f2f4251 1807 spec->mixer = stac9200_mixer;
c7d4b2fa
M
1808
1809 err = stac9200_parse_auto_config(codec);
1810 if (err < 0) {
1811 stac92xx_free(codec);
1812 return err;
1813 }
2f2f4251
M
1814
1815 codec->patch_ops = stac92xx_patch_ops;
1816
1817 return 0;
1818}
1819
8e21c34c
TD
1820static int patch_stac925x(struct hda_codec *codec)
1821{
1822 struct sigmatel_spec *spec;
1823 int err;
1824
1825 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1826 if (spec == NULL)
1827 return -ENOMEM;
1828
1829 codec->spec = spec;
1830 spec->num_pins = 8;
1831 spec->pin_nids = stac925x_pin_nids;
1832 spec->board_config = snd_hda_check_board_config(codec, STAC_925x_MODELS,
1833 stac925x_models,
1834 stac925x_cfg_tbl);
9e507abd 1835 again:
8e21c34c
TD
1836 if (spec->board_config < 0) {
1837 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC925x, using BIOS defaults\n");
1838 err = stac92xx_save_bios_config_regs(codec);
1839 if (err < 0) {
1840 stac92xx_free(codec);
1841 return err;
1842 }
1843 spec->pin_configs = spec->bios_pin_configs;
1844 } else if (stac925x_brd_tbl[spec->board_config] != NULL){
1845 spec->pin_configs = stac925x_brd_tbl[spec->board_config];
1846 stac92xx_set_config_regs(codec);
1847 }
1848
1849 spec->multiout.max_channels = 2;
1850 spec->multiout.num_dacs = 1;
1851 spec->multiout.dac_nids = stac925x_dac_nids;
1852 spec->adc_nids = stac925x_adc_nids;
1853 spec->mux_nids = stac925x_mux_nids;
1854 spec->num_muxes = 1;
1855 spec->num_dmics = 0;
1856
1857 spec->init = stac925x_core_init;
1858 spec->mixer = stac925x_mixer;
1859
1860 err = stac92xx_parse_auto_config(codec, 0x8, 0x7);
9e507abd
TI
1861 if (!err) {
1862 if (spec->board_config < 0) {
1863 printk(KERN_WARNING "hda_codec: No auto-config is "
1864 "available, default to model=ref\n");
1865 spec->board_config = STAC_925x_REF;
1866 goto again;
1867 }
1868 err = -EINVAL;
1869 }
8e21c34c
TD
1870 if (err < 0) {
1871 stac92xx_free(codec);
1872 return err;
1873 }
1874
1875 codec->patch_ops = stac92xx_patch_ops;
1876
1877 return 0;
1878}
1879
2f2f4251
M
1880static int patch_stac922x(struct hda_codec *codec)
1881{
1882 struct sigmatel_spec *spec;
c7d4b2fa 1883 int err;
2f2f4251 1884
e560d8d8 1885 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2f2f4251
M
1886 if (spec == NULL)
1887 return -ENOMEM;
1888
1889 codec->spec = spec;
11b44bbd
RF
1890 spec->num_pins = 10;
1891 spec->pin_nids = stac922x_pin_nids;
f5fcc13c
TI
1892 spec->board_config = snd_hda_check_board_config(codec, STAC_922X_MODELS,
1893 stac922x_models,
1894 stac922x_cfg_tbl);
3fc24d85
TI
1895 if (spec->board_config == STAC_MACMINI) {
1896 spec->gpio_mute = 1;
1897 /* Intel Macs have all same PCI SSID, so we need to check
1898 * codec SSID to distinguish the exact models
1899 */
6f0778d8 1900 printk(KERN_INFO "hda_codec: STAC922x, Apple subsys_id=%x\n", codec->subsystem_id);
3fc24d85 1901 switch (codec->subsystem_id) {
6f0778d8
NB
1902 case 0x106b0200: /* MacBook Pro first generation */
1903 spec->board_config = STAC_MACBOOK_PRO_V1;
1904 break;
1905 case 0x106b1e00: /* MacBook Pro second generation */
1906 spec->board_config = STAC_MACBOOK_PRO_V2;
3fc24d85
TI
1907 break;
1908 }
1909 }
1910
9e507abd 1911 again:
11b44bbd
RF
1912 if (spec->board_config < 0) {
1913 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, "
1914 "using BIOS defaults\n");
1915 err = stac92xx_save_bios_config_regs(codec);
1916 if (err < 0) {
1917 stac92xx_free(codec);
1918 return err;
1919 }
1920 spec->pin_configs = spec->bios_pin_configs;
1921 } else if (stac922x_brd_tbl[spec->board_config] != NULL) {
403d1944
MP
1922 spec->pin_configs = stac922x_brd_tbl[spec->board_config];
1923 stac92xx_set_config_regs(codec);
1924 }
2f2f4251 1925
c7d4b2fa
M
1926 spec->adc_nids = stac922x_adc_nids;
1927 spec->mux_nids = stac922x_mux_nids;
2549413e 1928 spec->num_muxes = ARRAY_SIZE(stac922x_mux_nids);
8b65727b 1929 spec->num_dmics = 0;
c7d4b2fa
M
1930
1931 spec->init = stac922x_core_init;
2f2f4251 1932 spec->mixer = stac922x_mixer;
c7d4b2fa
M
1933
1934 spec->multiout.dac_nids = spec->dac_nids;
19039bd0 1935
3cc08dc6 1936 err = stac92xx_parse_auto_config(codec, 0x08, 0x09);
9e507abd
TI
1937 if (!err) {
1938 if (spec->board_config < 0) {
1939 printk(KERN_WARNING "hda_codec: No auto-config is "
1940 "available, default to model=ref\n");
1941 spec->board_config = STAC_D945_REF;
1942 goto again;
1943 }
1944 err = -EINVAL;
1945 }
3cc08dc6
MP
1946 if (err < 0) {
1947 stac92xx_free(codec);
1948 return err;
1949 }
1950
1951 codec->patch_ops = stac92xx_patch_ops;
1952
1953 return 0;
1954}
1955
1956static int patch_stac927x(struct hda_codec *codec)
1957{
1958 struct sigmatel_spec *spec;
1959 int err;
1960
1961 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1962 if (spec == NULL)
1963 return -ENOMEM;
1964
1965 codec->spec = spec;
11b44bbd
RF
1966 spec->num_pins = 14;
1967 spec->pin_nids = stac927x_pin_nids;
f5fcc13c
TI
1968 spec->board_config = snd_hda_check_board_config(codec, STAC_927X_MODELS,
1969 stac927x_models,
1970 stac927x_cfg_tbl);
9e507abd 1971 again:
11b44bbd 1972 if (spec->board_config < 0) {
3cc08dc6 1973 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC927x, using BIOS defaults\n");
11b44bbd
RF
1974 err = stac92xx_save_bios_config_regs(codec);
1975 if (err < 0) {
1976 stac92xx_free(codec);
1977 return err;
1978 }
1979 spec->pin_configs = spec->bios_pin_configs;
1980 } else if (stac927x_brd_tbl[spec->board_config] != NULL) {
3cc08dc6
MP
1981 spec->pin_configs = stac927x_brd_tbl[spec->board_config];
1982 stac92xx_set_config_regs(codec);
1983 }
1984
81d3dbde 1985 switch (spec->board_config) {
93ed1503 1986 case STAC_D965_3ST:
81d3dbde
TD
1987 spec->adc_nids = stac927x_adc_nids;
1988 spec->mux_nids = stac927x_mux_nids;
2549413e 1989 spec->num_muxes = ARRAY_SIZE(stac927x_mux_nids);
8b65727b 1990 spec->num_dmics = 0;
93ed1503 1991 spec->init = d965_core_init;
81d3dbde
TD
1992 spec->mixer = stac9227_mixer;
1993 break;
93ed1503
TD
1994 case STAC_D965_5ST:
1995 spec->adc_nids = stac927x_adc_nids;
1996 spec->mux_nids = stac927x_mux_nids;
2549413e 1997 spec->num_muxes = ARRAY_SIZE(stac927x_mux_nids);
8b65727b 1998 spec->num_dmics = 0;
93ed1503 1999 spec->init = d965_core_init;
81d3dbde
TD
2000 spec->mixer = stac9227_mixer;
2001 break;
2002 default:
2003 spec->adc_nids = stac927x_adc_nids;
2004 spec->mux_nids = stac927x_mux_nids;
2549413e 2005 spec->num_muxes = ARRAY_SIZE(stac927x_mux_nids);
8b65727b 2006 spec->num_dmics = 0;
81d3dbde
TD
2007 spec->init = stac927x_core_init;
2008 spec->mixer = stac927x_mixer;
2009 }
3cc08dc6
MP
2010
2011 spec->multiout.dac_nids = spec->dac_nids;
2012
2013 err = stac92xx_parse_auto_config(codec, 0x1e, 0x20);
9e507abd
TI
2014 if (!err) {
2015 if (spec->board_config < 0) {
2016 printk(KERN_WARNING "hda_codec: No auto-config is "
2017 "available, default to model=ref\n");
2018 spec->board_config = STAC_D965_REF;
2019 goto again;
2020 }
2021 err = -EINVAL;
2022 }
c7d4b2fa
M
2023 if (err < 0) {
2024 stac92xx_free(codec);
2025 return err;
2026 }
2f2f4251
M
2027
2028 codec->patch_ops = stac92xx_patch_ops;
2029
2030 return 0;
2031}
2032
f3302a59
MP
2033static int patch_stac9205(struct hda_codec *codec)
2034{
2035 struct sigmatel_spec *spec;
2036 int err;
2037
2038 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2039 if (spec == NULL)
2040 return -ENOMEM;
2041
2042 codec->spec = spec;
11b44bbd
RF
2043 spec->num_pins = 14;
2044 spec->pin_nids = stac9205_pin_nids;
f5fcc13c
TI
2045 spec->board_config = snd_hda_check_board_config(codec, STAC_9205_MODELS,
2046 stac9205_models,
2047 stac9205_cfg_tbl);
9e507abd 2048 again:
11b44bbd
RF
2049 if (spec->board_config < 0) {
2050 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9205, using BIOS defaults\n");
2051 err = stac92xx_save_bios_config_regs(codec);
2052 if (err < 0) {
2053 stac92xx_free(codec);
2054 return err;
2055 }
2056 spec->pin_configs = spec->bios_pin_configs;
2057 } else {
f3302a59
MP
2058 spec->pin_configs = stac9205_brd_tbl[spec->board_config];
2059 stac92xx_set_config_regs(codec);
2060 }
2061
2062 spec->adc_nids = stac9205_adc_nids;
2063 spec->mux_nids = stac9205_mux_nids;
2549413e 2064 spec->num_muxes = ARRAY_SIZE(stac9205_mux_nids);
8b65727b 2065 spec->dmic_nids = stac9205_dmic_nids;
2549413e 2066 spec->num_dmics = ARRAY_SIZE(stac9205_dmic_nids);
8b65727b 2067 spec->dmux_nid = 0x1d;
f3302a59
MP
2068
2069 spec->init = stac9205_core_init;
2070 spec->mixer = stac9205_mixer;
2071
2072 spec->multiout.dac_nids = spec->dac_nids;
2073
33382403
MP
2074 /* Configure GPIO0 as EAPD output */
2075 snd_hda_codec_write(codec, codec->afg, 0,
2076 AC_VERB_SET_GPIO_DIRECTION, 0x00000001);
2077 /* Configure GPIO0 as CMOS */
2078 snd_hda_codec_write(codec, codec->afg, 0, 0x7e7, 0x00000000);
2079 /* Assert GPIO0 high */
2080 snd_hda_codec_write(codec, codec->afg, 0,
2081 AC_VERB_SET_GPIO_DATA, 0x00000001);
2082 /* Enable GPIO0 */
2083 snd_hda_codec_write(codec, codec->afg, 0,
2084 AC_VERB_SET_GPIO_MASK, 0x00000001);
2085
f3302a59 2086 err = stac92xx_parse_auto_config(codec, 0x1f, 0x20);
9e507abd
TI
2087 if (!err) {
2088 if (spec->board_config < 0) {
2089 printk(KERN_WARNING "hda_codec: No auto-config is "
2090 "available, default to model=ref\n");
2091 spec->board_config = STAC_9205_REF;
2092 goto again;
2093 }
2094 err = -EINVAL;
2095 }
f3302a59
MP
2096 if (err < 0) {
2097 stac92xx_free(codec);
2098 return err;
2099 }
2100
2101 codec->patch_ops = stac92xx_patch_ops;
2102
2103 return 0;
2104}
2105
db064e50 2106/*
6d859065 2107 * STAC9872 hack
db064e50
TI
2108 */
2109
99ccc560 2110/* static config for Sony VAIO FE550G and Sony VAIO AR */
db064e50
TI
2111static hda_nid_t vaio_dacs[] = { 0x2 };
2112#define VAIO_HP_DAC 0x5
2113static hda_nid_t vaio_adcs[] = { 0x8 /*,0x6*/ };
2114static hda_nid_t vaio_mux_nids[] = { 0x15 };
2115
2116static struct hda_input_mux vaio_mux = {
2117 .num_items = 2,
2118 .items = {
d773781c
TI
2119 /* { "HP", 0x0 }, */
2120 { "Line", 0x1 },
db064e50
TI
2121 { "Mic", 0x2 },
2122 { "PCM", 0x3 },
2123 }
2124};
2125
2126static struct hda_verb vaio_init[] = {
2127 {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
2128 {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
2129 {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
2130 {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
2131 {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
2132 {0x15, AC_VERB_SET_CONNECT_SEL, 0x2}, /* mic-sel: 0a,0d,14,02 */
2133 {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
2134 {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
2135 {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
2136 {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
2137 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
2138 {}
2139};
2140
6d859065
GM
2141static struct hda_verb vaio_ar_init[] = {
2142 {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
2143 {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
2144 {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
2145 {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
2146/* {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },*/ /* Optical Out */
2147 {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
2148 {0x15, AC_VERB_SET_CONNECT_SEL, 0x2}, /* mic-sel: 0a,0d,14,02 */
2149 {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
2150 {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
2151/* {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},*/ /* Optical Out */
2152 {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
2153 {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
2154 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
2155 {}
2156};
2157
db064e50
TI
2158/* bind volumes of both NID 0x02 and 0x05 */
2159static int vaio_master_vol_put(struct snd_kcontrol *kcontrol,
2160 struct snd_ctl_elem_value *ucontrol)
2161{
2162 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2163 long *valp = ucontrol->value.integer.value;
2164 int change;
2165
2166 change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
2167 0x7f, valp[0] & 0x7f);
2168 change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
2169 0x7f, valp[1] & 0x7f);
2170 snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
2171 0x7f, valp[0] & 0x7f);
2172 snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
2173 0x7f, valp[1] & 0x7f);
2174 return change;
2175}
2176
2177/* bind volumes of both NID 0x02 and 0x05 */
2178static int vaio_master_sw_put(struct snd_kcontrol *kcontrol,
2179 struct snd_ctl_elem_value *ucontrol)
2180{
2181 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2182 long *valp = ucontrol->value.integer.value;
2183 int change;
2184
2185 change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
a9393d70 2186 0x80, (valp[0] ? 0 : 0x80));
db064e50 2187 change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
a9393d70 2188 0x80, (valp[1] ? 0 : 0x80));
db064e50 2189 snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
a9393d70 2190 0x80, (valp[0] ? 0 : 0x80));
db064e50 2191 snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
a9393d70 2192 0x80, (valp[1] ? 0 : 0x80));
db064e50
TI
2193 return change;
2194}
2195
2196static struct snd_kcontrol_new vaio_mixer[] = {
2197 {
2198 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2199 .name = "Master Playback Volume",
2200 .info = snd_hda_mixer_amp_volume_info,
2201 .get = snd_hda_mixer_amp_volume_get,
2202 .put = vaio_master_vol_put,
c2566524 2203 .tlv = { .c = snd_hda_mixer_amp_tlv },
db064e50
TI
2204 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
2205 },
2206 {
2207 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2208 .name = "Master Playback Switch",
2209 .info = snd_hda_mixer_amp_switch_info,
2210 .get = snd_hda_mixer_amp_switch_get,
2211 .put = vaio_master_sw_put,
2212 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
2213 },
2214 /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
2215 HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
2216 HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
2217 {
2218 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2219 .name = "Capture Source",
2220 .count = 1,
2221 .info = stac92xx_mux_enum_info,
2222 .get = stac92xx_mux_enum_get,
2223 .put = stac92xx_mux_enum_put,
2224 },
2225 {}
2226};
2227
6d859065
GM
2228static struct snd_kcontrol_new vaio_ar_mixer[] = {
2229 {
2230 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2231 .name = "Master Playback Volume",
2232 .info = snd_hda_mixer_amp_volume_info,
2233 .get = snd_hda_mixer_amp_volume_get,
2234 .put = vaio_master_vol_put,
2235 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
2236 },
2237 {
2238 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2239 .name = "Master Playback Switch",
2240 .info = snd_hda_mixer_amp_switch_info,
2241 .get = snd_hda_mixer_amp_switch_get,
2242 .put = vaio_master_sw_put,
2243 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
2244 },
2245 /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
2246 HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
2247 HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
2248 /*HDA_CODEC_MUTE("Optical Out Switch", 0x10, 0, HDA_OUTPUT),
2249 HDA_CODEC_VOLUME("Optical Out Volume", 0x10, 0, HDA_OUTPUT),*/
2250 {
2251 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2252 .name = "Capture Source",
2253 .count = 1,
2254 .info = stac92xx_mux_enum_info,
2255 .get = stac92xx_mux_enum_get,
2256 .put = stac92xx_mux_enum_put,
2257 },
2258 {}
2259};
2260
2261static struct hda_codec_ops stac9872_patch_ops = {
db064e50
TI
2262 .build_controls = stac92xx_build_controls,
2263 .build_pcms = stac92xx_build_pcms,
2264 .init = stac92xx_init,
2265 .free = stac92xx_free,
2266#ifdef CONFIG_PM
2267 .resume = stac92xx_resume,
2268#endif
2269};
2270
6d859065
GM
2271enum { /* FE and SZ series. id=0x83847661 and subsys=0x104D0700 or 104D1000. */
2272 CXD9872RD_VAIO,
2273 /* Unknown. id=0x83847662 and subsys=0x104D1200 or 104D1000. */
2274 STAC9872AK_VAIO,
2275 /* Unknown. id=0x83847661 and subsys=0x104D1200. */
2276 STAC9872K_VAIO,
2277 /* AR Series. id=0x83847664 and subsys=104D1300 */
f5fcc13c
TI
2278 CXD9872AKD_VAIO,
2279 STAC_9872_MODELS,
2280};
2281
2282static const char *stac9872_models[STAC_9872_MODELS] = {
2283 [CXD9872RD_VAIO] = "vaio",
2284 [CXD9872AKD_VAIO] = "vaio-ar",
2285};
2286
2287static struct snd_pci_quirk stac9872_cfg_tbl[] = {
2288 SND_PCI_QUIRK(0x104d, 0x81e6, "Sony VAIO F/S", CXD9872RD_VAIO),
2289 SND_PCI_QUIRK(0x104d, 0x81ef, "Sony VAIO F/S", CXD9872RD_VAIO),
2290 SND_PCI_QUIRK(0x104d, 0x81fd, "Sony VAIO AR", CXD9872AKD_VAIO),
68e22543 2291 SND_PCI_QUIRK(0x104d, 0x8205, "Sony VAIO AR", CXD9872AKD_VAIO),
db064e50
TI
2292 {}
2293};
2294
6d859065 2295static int patch_stac9872(struct hda_codec *codec)
db064e50
TI
2296{
2297 struct sigmatel_spec *spec;
2298 int board_config;
2299
f5fcc13c
TI
2300 board_config = snd_hda_check_board_config(codec, STAC_9872_MODELS,
2301 stac9872_models,
2302 stac9872_cfg_tbl);
db064e50
TI
2303 if (board_config < 0)
2304 /* unknown config, let generic-parser do its job... */
2305 return snd_hda_parse_generic_codec(codec);
2306
2307 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2308 if (spec == NULL)
2309 return -ENOMEM;
2310
2311 codec->spec = spec;
2312 switch (board_config) {
6d859065
GM
2313 case CXD9872RD_VAIO:
2314 case STAC9872AK_VAIO:
2315 case STAC9872K_VAIO:
db064e50
TI
2316 spec->mixer = vaio_mixer;
2317 spec->init = vaio_init;
2318 spec->multiout.max_channels = 2;
2319 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
2320 spec->multiout.dac_nids = vaio_dacs;
2321 spec->multiout.hp_nid = VAIO_HP_DAC;
2322 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
2323 spec->adc_nids = vaio_adcs;
2324 spec->input_mux = &vaio_mux;
2325 spec->mux_nids = vaio_mux_nids;
2326 break;
6d859065
GM
2327
2328 case CXD9872AKD_VAIO:
2329 spec->mixer = vaio_ar_mixer;
2330 spec->init = vaio_ar_init;
2331 spec->multiout.max_channels = 2;
2332 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
2333 spec->multiout.dac_nids = vaio_dacs;
2334 spec->multiout.hp_nid = VAIO_HP_DAC;
2335 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
2336 spec->adc_nids = vaio_adcs;
2337 spec->input_mux = &vaio_mux;
2338 spec->mux_nids = vaio_mux_nids;
2339 break;
db064e50
TI
2340 }
2341
6d859065 2342 codec->patch_ops = stac9872_patch_ops;
db064e50
TI
2343 return 0;
2344}
2345
2346
2f2f4251
M
2347/*
2348 * patch entries
2349 */
2350struct hda_codec_preset snd_hda_preset_sigmatel[] = {
2351 { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
2352 { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
2353 { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
2354 { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
2355 { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
2356 { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
2357 { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
22a27c7f
MP
2358 { .id = 0x83847618, .name = "STAC9227", .patch = patch_stac927x },
2359 { .id = 0x83847619, .name = "STAC9227", .patch = patch_stac927x },
2360 { .id = 0x83847616, .name = "STAC9228", .patch = patch_stac927x },
2361 { .id = 0x83847617, .name = "STAC9228", .patch = patch_stac927x },
2362 { .id = 0x83847614, .name = "STAC9229", .patch = patch_stac927x },
2363 { .id = 0x83847615, .name = "STAC9229", .patch = patch_stac927x },
3cc08dc6
MP
2364 { .id = 0x83847620, .name = "STAC9274", .patch = patch_stac927x },
2365 { .id = 0x83847621, .name = "STAC9274D", .patch = patch_stac927x },
2366 { .id = 0x83847622, .name = "STAC9273X", .patch = patch_stac927x },
2367 { .id = 0x83847623, .name = "STAC9273D", .patch = patch_stac927x },
2368 { .id = 0x83847624, .name = "STAC9272X", .patch = patch_stac927x },
2369 { .id = 0x83847625, .name = "STAC9272D", .patch = patch_stac927x },
2370 { .id = 0x83847626, .name = "STAC9271X", .patch = patch_stac927x },
2371 { .id = 0x83847627, .name = "STAC9271D", .patch = patch_stac927x },
2372 { .id = 0x83847628, .name = "STAC9274X5NH", .patch = patch_stac927x },
2373 { .id = 0x83847629, .name = "STAC9274D5NH", .patch = patch_stac927x },
8e21c34c
TD
2374 { .id = 0x83847632, .name = "STAC9202", .patch = patch_stac925x },
2375 { .id = 0x83847633, .name = "STAC9202D", .patch = patch_stac925x },
2376 { .id = 0x83847634, .name = "STAC9250", .patch = patch_stac925x },
2377 { .id = 0x83847635, .name = "STAC9250D", .patch = patch_stac925x },
2378 { .id = 0x83847636, .name = "STAC9251", .patch = patch_stac925x },
2379 { .id = 0x83847637, .name = "STAC9250D", .patch = patch_stac925x },
6d859065
GM
2380 /* The following does not take into account .id=0x83847661 when subsys =
2381 * 104D0C00 which is STAC9225s. Because of this, some SZ Notebooks are
2382 * currently not fully supported.
2383 */
2384 { .id = 0x83847661, .name = "CXD9872RD/K", .patch = patch_stac9872 },
2385 { .id = 0x83847662, .name = "STAC9872AK", .patch = patch_stac9872 },
2386 { .id = 0x83847664, .name = "CXD9872AKD", .patch = patch_stac9872 },
f3302a59
MP
2387 { .id = 0x838476a0, .name = "STAC9205", .patch = patch_stac9205 },
2388 { .id = 0x838476a1, .name = "STAC9205D", .patch = patch_stac9205 },
2389 { .id = 0x838476a2, .name = "STAC9204", .patch = patch_stac9205 },
2390 { .id = 0x838476a3, .name = "STAC9204D", .patch = patch_stac9205 },
2391 { .id = 0x838476a4, .name = "STAC9255", .patch = patch_stac9205 },
2392 { .id = 0x838476a5, .name = "STAC9255D", .patch = patch_stac9205 },
2393 { .id = 0x838476a6, .name = "STAC9254", .patch = patch_stac9205 },
2394 { .id = 0x838476a7, .name = "STAC9254D", .patch = patch_stac9205 },
2f2f4251
M
2395 {} /* terminator */
2396};