]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/pci/hda/patch_sigmatel.c
[ALSA] snd-emu10k1: Add a comment explaining the conversion function for dB gain.
[net-next-2.6.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
39#define STAC_UNSOL_ENABLE (AC_USRSP_EN | STAC_HP_EVENT)
40
403d1944
MP
41#define STAC_REF 0
42#define STAC_D945GTP3 1
43#define STAC_D945GTP5 2
62fe78e9 44#define STAC_MACMINI 3
19039bd0
TI
45#define STAC_D965_2112 4
46#define STAC_D965_284B 5
47#define STAC_922X_MODELS 6 /* number of 922x models */
403d1944 48
2f2f4251 49struct sigmatel_spec {
c8b6bf9b 50 struct snd_kcontrol_new *mixers[4];
c7d4b2fa
M
51 unsigned int num_mixers;
52
403d1944 53 int board_config;
c7d4b2fa 54 unsigned int surr_switch: 1;
403d1944
MP
55 unsigned int line_switch: 1;
56 unsigned int mic_switch: 1;
3cc08dc6 57 unsigned int alt_switch: 1;
82bc955f 58 unsigned int hp_detect: 1;
62fe78e9 59 unsigned int gpio_mute: 1;
c7d4b2fa 60
2f2f4251
M
61 /* playback */
62 struct hda_multi_out multiout;
3cc08dc6 63 hda_nid_t dac_nids[5];
2f2f4251
M
64
65 /* capture */
66 hda_nid_t *adc_nids;
2f2f4251 67 unsigned int num_adcs;
dabbed6f
M
68 hda_nid_t *mux_nids;
69 unsigned int num_muxes;
dabbed6f 70 hda_nid_t dig_in_nid;
2f2f4251 71
2f2f4251
M
72 /* pin widgets */
73 hda_nid_t *pin_nids;
74 unsigned int num_pins;
2f2f4251 75 unsigned int *pin_configs;
2f2f4251
M
76
77 /* codec specific stuff */
78 struct hda_verb *init;
c8b6bf9b 79 struct snd_kcontrol_new *mixer;
2f2f4251
M
80
81 /* capture source */
c7d4b2fa 82 struct hda_input_mux *input_mux;
3cc08dc6 83 unsigned int cur_mux[3];
2f2f4251 84
403d1944
MP
85 /* i/o switches */
86 unsigned int io_switch[2];
2f2f4251 87
c7d4b2fa
M
88 struct hda_pcm pcm_rec[2]; /* PCM information */
89
90 /* dynamic controls and input_mux */
91 struct auto_pin_cfg autocfg;
92 unsigned int num_kctl_alloc, num_kctl_used;
c8b6bf9b 93 struct snd_kcontrol_new *kctl_alloc;
c7d4b2fa 94 struct hda_input_mux private_imux;
2f2f4251
M
95};
96
97static hda_nid_t stac9200_adc_nids[1] = {
98 0x03,
99};
100
101static hda_nid_t stac9200_mux_nids[1] = {
102 0x0c,
103};
104
105static hda_nid_t stac9200_dac_nids[1] = {
106 0x02,
107};
108
2f2f4251
M
109static hda_nid_t stac922x_adc_nids[2] = {
110 0x06, 0x07,
111};
112
19039bd0
TI
113static hda_nid_t stac9227_adc_nids[2] = {
114 0x07, 0x08,
115};
116
117#if 0
118static hda_nid_t d965_2112_dac_nids[3] = {
119 0x02, 0x03, 0x05,
120};
121#endif
122
2f2f4251
M
123static hda_nid_t stac922x_mux_nids[2] = {
124 0x12, 0x13,
125};
126
19039bd0
TI
127static hda_nid_t stac9227_mux_nids[2] = {
128 0x15, 0x16,
129};
130
3cc08dc6
MP
131static hda_nid_t stac927x_adc_nids[3] = {
132 0x07, 0x08, 0x09
133};
134
135static hda_nid_t stac927x_mux_nids[3] = {
136 0x15, 0x16, 0x17
137};
138
c7d4b2fa
M
139static hda_nid_t stac9200_pin_nids[8] = {
140 0x08, 0x09, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
2f2f4251
M
141};
142
143static hda_nid_t stac922x_pin_nids[10] = {
144 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
145 0x0f, 0x10, 0x11, 0x15, 0x1b,
146};
147
3cc08dc6
MP
148static hda_nid_t stac927x_pin_nids[14] = {
149 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
150 0x0f, 0x10, 0x11, 0x12, 0x13,
151 0x14, 0x21, 0x22, 0x23,
152};
153
c8b6bf9b 154static int stac92xx_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2f2f4251
M
155{
156 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
157 struct sigmatel_spec *spec = codec->spec;
c7d4b2fa 158 return snd_hda_input_mux_info(spec->input_mux, uinfo);
2f2f4251
M
159}
160
c8b6bf9b 161static int stac92xx_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2f2f4251
M
162{
163 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
164 struct sigmatel_spec *spec = codec->spec;
165 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
166
167 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
168 return 0;
169}
170
c8b6bf9b 171static int stac92xx_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2f2f4251
M
172{
173 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
174 struct sigmatel_spec *spec = codec->spec;
175 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
176
c7d4b2fa 177 return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
2f2f4251
M
178 spec->mux_nids[adc_idx], &spec->cur_mux[adc_idx]);
179}
180
c7d4b2fa 181static struct hda_verb stac9200_core_init[] = {
2f2f4251 182 /* set dac0mux for dac converter */
c7d4b2fa 183 { 0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
2f2f4251
M
184 {}
185};
186
c7d4b2fa 187static struct hda_verb stac922x_core_init[] = {
2f2f4251 188 /* set master volume and direct control */
c7d4b2fa 189 { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
2f2f4251
M
190 {}
191};
192
19039bd0
TI
193static struct hda_verb stac9227_core_init[] = {
194 /* set master volume and direct control */
195 { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
196 /* unmute node 0x1b */
197 { 0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
198 {}
199};
200
201static struct hda_verb d965_2112_core_init[] = {
202 /* set master volume and direct control */
203 { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
204 /* unmute node 0x1b */
205 { 0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
206 /* select node 0x03 as DAC */
207 { 0x0b, AC_VERB_SET_CONNECT_SEL, 0x01},
208 {}
209};
210
3cc08dc6
MP
211static struct hda_verb stac927x_core_init[] = {
212 /* set master volume and direct control */
213 { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
214 {}
215};
216
c8b6bf9b 217static struct snd_kcontrol_new stac9200_mixer[] = {
2f2f4251
M
218 HDA_CODEC_VOLUME("Master Playback Volume", 0xb, 0, HDA_OUTPUT),
219 HDA_CODEC_MUTE("Master Playback Switch", 0xb, 0, HDA_OUTPUT),
220 {
221 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
222 .name = "Input Source",
223 .count = 1,
224 .info = stac92xx_mux_enum_info,
225 .get = stac92xx_mux_enum_get,
226 .put = stac92xx_mux_enum_put,
227 },
228 HDA_CODEC_VOLUME("Capture Volume", 0x0a, 0, HDA_OUTPUT),
229 HDA_CODEC_MUTE("Capture Switch", 0x0a, 0, HDA_OUTPUT),
c7d4b2fa 230 HDA_CODEC_VOLUME("Capture Mux Volume", 0x0c, 0, HDA_OUTPUT),
2f2f4251
M
231 { } /* end */
232};
233
c7d4b2fa 234/* This needs to be generated dynamically based on sequence */
c8b6bf9b 235static struct snd_kcontrol_new stac922x_mixer[] = {
2f2f4251
M
236 {
237 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
238 .name = "Input Source",
239 .count = 1,
240 .info = stac92xx_mux_enum_info,
241 .get = stac92xx_mux_enum_get,
242 .put = stac92xx_mux_enum_put,
243 },
244 HDA_CODEC_VOLUME("Capture Volume", 0x17, 0x0, HDA_INPUT),
0fd1708a 245 HDA_CODEC_MUTE("Capture Switch", 0x17, 0x0, HDA_INPUT),
2f2f4251
M
246 HDA_CODEC_VOLUME("Mux Capture Volume", 0x12, 0x0, HDA_OUTPUT),
247 { } /* end */
248};
249
19039bd0
TI
250/* This needs to be generated dynamically based on sequence */
251static struct snd_kcontrol_new stac9227_mixer[] = {
252 {
253 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
254 .name = "Input Source",
255 .count = 1,
256 .info = stac92xx_mux_enum_info,
257 .get = stac92xx_mux_enum_get,
258 .put = stac92xx_mux_enum_put,
259 },
260 HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
261 HDA_CODEC_MUTE("Capture Switch", 0x1b, 0x0, HDA_OUTPUT),
262 { } /* end */
263};
264
3cc08dc6
MP
265static snd_kcontrol_new_t stac927x_mixer[] = {
266 {
267 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
268 .name = "Input Source",
269 .count = 1,
270 .info = stac92xx_mux_enum_info,
271 .get = stac92xx_mux_enum_get,
272 .put = stac92xx_mux_enum_put,
273 },
274 HDA_CODEC_VOLUME("InMux Capture Volume", 0x15, 0x0, HDA_OUTPUT),
275 HDA_CODEC_VOLUME("InVol Capture Volume", 0x18, 0x0, HDA_INPUT),
276 HDA_CODEC_MUTE("ADCMux Capture Switch", 0x1b, 0x0, HDA_OUTPUT),
277 { } /* end */
278};
279
2f2f4251
M
280static int stac92xx_build_controls(struct hda_codec *codec)
281{
282 struct sigmatel_spec *spec = codec->spec;
283 int err;
c7d4b2fa 284 int i;
2f2f4251
M
285
286 err = snd_hda_add_new_ctls(codec, spec->mixer);
287 if (err < 0)
288 return err;
c7d4b2fa
M
289
290 for (i = 0; i < spec->num_mixers; i++) {
291 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
292 if (err < 0)
293 return err;
294 }
295
dabbed6f
M
296 if (spec->multiout.dig_out_nid) {
297 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
298 if (err < 0)
299 return err;
300 }
301 if (spec->dig_in_nid) {
302 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
303 if (err < 0)
304 return err;
305 }
306 return 0;
2f2f4251
M
307}
308
403d1944 309static unsigned int ref9200_pin_configs[8] = {
dabbed6f 310 0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
2f2f4251
M
311 0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
312};
313
403d1944
MP
314static unsigned int *stac9200_brd_tbl[] = {
315 ref9200_pin_configs,
316};
317
318static struct hda_board_config stac9200_cfg_tbl[] = {
319 { .modelname = "ref",
320 .pci_subvendor = PCI_VENDOR_ID_INTEL,
321 .pci_subdevice = 0x2668, /* DFI LanParty */
322 .config = STAC_REF },
323 {} /* terminator */
324};
325
326static unsigned int ref922x_pin_configs[10] = {
327 0x01014010, 0x01016011, 0x01012012, 0x0221401f,
328 0x01813122, 0x01011014, 0x01441030, 0x01c41030,
2f2f4251
M
329 0x40000100, 0x40000100,
330};
331
403d1944 332static unsigned int d945gtp3_pin_configs[10] = {
869264c4 333 0x0221401f, 0x01a19022, 0x01813021, 0x01014010,
403d1944
MP
334 0x40000100, 0x40000100, 0x40000100, 0x40000100,
335 0x02a19120, 0x40000100,
336};
337
338static unsigned int d945gtp5_pin_configs[10] = {
869264c4
MP
339 0x0221401f, 0x01011012, 0x01813024, 0x01014010,
340 0x01a19021, 0x01016011, 0x01452130, 0x40000100,
403d1944
MP
341 0x02a19320, 0x40000100,
342};
343
19039bd0
TI
344static unsigned int d965_2112_pin_configs[10] = {
345 0x0221401f, 0x40000100, 0x40000100, 0x01014011,
346 0x01a19021, 0x01813024, 0x01452130, 0x40000100,
347 0x02a19320, 0x40000100,
348};
349
350static unsigned int *stac922x_brd_tbl[STAC_922X_MODELS] = {
351 [STAC_REF] = ref922x_pin_configs,
352 [STAC_D945GTP3] = d945gtp3_pin_configs,
353 [STAC_D945GTP5] = d945gtp5_pin_configs,
7c3dec06 354 [STAC_MACMINI] = d945gtp5_pin_configs,
19039bd0 355 [STAC_D965_2112] = d965_2112_pin_configs,
403d1944
MP
356};
357
358static struct hda_board_config stac922x_cfg_tbl[] = {
359 { .modelname = "ref",
360 .pci_subvendor = PCI_VENDOR_ID_INTEL,
361 .pci_subdevice = 0x2668, /* DFI LanParty */
362 .config = STAC_REF }, /* SigmaTel reference board */
363 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
364 .pci_subdevice = 0x0101,
365 .config = STAC_D945GTP3 }, /* Intel D945GTP - 3 Stack */
353b9e66
TI
366 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
367 .pci_subdevice = 0x0202,
368 .config = STAC_D945GTP3 }, /* Intel D945GNT - 3 Stack, 9221 A1 */
369 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
370 .pci_subdevice = 0x0b0b,
371 .config = STAC_D945GTP3 }, /* Intel D945PSN - 3 Stack, 9221 A1 */
403d1944 372 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
a29b6c88
AC
373 .pci_subdevice = 0x0707,
374 .config = STAC_D945GTP5 }, /* Intel D945PSV - 5 Stack */
375 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
403d1944
MP
376 .pci_subdevice = 0x0404,
377 .config = STAC_D945GTP5 }, /* Intel D945GTP - 5 Stack */
378 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
379 .pci_subdevice = 0x0303,
380 .config = STAC_D945GTP5 }, /* Intel D945GNT - 5 Stack */
381 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
382 .pci_subdevice = 0x0013,
383 .config = STAC_D945GTP5 }, /* Intel D955XBK - 5 Stack */
d62c40e0
MP
384 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
385 .pci_subdevice = 0x0417,
386 .config = STAC_D945GTP5 }, /* Intel D975XBK - 5 Stack */
62fe78e9
SR
387 { .pci_subvendor = 0x8384,
388 .pci_subdevice = 0x7680,
389 .config = STAC_MACMINI }, /* Apple Mac Mini (early 2006) */
19039bd0
TI
390 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
391 .pci_subdevice = 0x2112,
392 .config = STAC_D965_2112 },
393 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
394 .pci_subdevice = 0x284b,
395 .config = STAC_D965_284B },
403d1944
MP
396 {} /* terminator */
397};
398
3cc08dc6
MP
399static unsigned int ref927x_pin_configs[14] = {
400 0x01813122, 0x01a19021, 0x01014010, 0x01016011,
401 0x01012012, 0x01011014, 0x40000100, 0x40000100,
402 0x40000100, 0x40000100, 0x40000100, 0x01441030,
403 0x01c41030, 0x40000100,
404};
405
406static unsigned int *stac927x_brd_tbl[] = {
407 ref927x_pin_configs,
408};
409
410static struct hda_board_config stac927x_cfg_tbl[] = {
411 { .modelname = "ref",
412 .pci_subvendor = PCI_VENDOR_ID_INTEL,
413 .pci_subdevice = 0x2668, /* DFI LanParty */
414 .config = STAC_REF }, /* SigmaTel reference board */
415 {} /* terminator */
416};
417
2f2f4251
M
418static void stac92xx_set_config_regs(struct hda_codec *codec)
419{
420 int i;
421 struct sigmatel_spec *spec = codec->spec;
422 unsigned int pin_cfg;
423
424 for (i=0; i < spec->num_pins; i++) {
425 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
426 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
427 spec->pin_configs[i] & 0x000000ff);
428 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
429 AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
430 (spec->pin_configs[i] & 0x0000ff00) >> 8);
431 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
432 AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
433 (spec->pin_configs[i] & 0x00ff0000) >> 16);
434 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
435 AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
436 spec->pin_configs[i] >> 24);
437 pin_cfg = snd_hda_codec_read(codec, spec->pin_nids[i], 0,
438 AC_VERB_GET_CONFIG_DEFAULT,
439 0x00);
403d1944 440 snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x pin config %8.8x\n", spec->pin_nids[i], pin_cfg);
2f2f4251
M
441 }
442}
2f2f4251 443
dabbed6f 444/*
c7d4b2fa 445 * Analog playback callbacks
dabbed6f 446 */
c7d4b2fa
M
447static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
448 struct hda_codec *codec,
c8b6bf9b 449 struct snd_pcm_substream *substream)
2f2f4251 450{
dabbed6f 451 struct sigmatel_spec *spec = codec->spec;
c7d4b2fa 452 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
2f2f4251
M
453}
454
2f2f4251
M
455static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
456 struct hda_codec *codec,
457 unsigned int stream_tag,
458 unsigned int format,
c8b6bf9b 459 struct snd_pcm_substream *substream)
2f2f4251
M
460{
461 struct sigmatel_spec *spec = codec->spec;
403d1944 462 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
2f2f4251
M
463}
464
465static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
466 struct hda_codec *codec,
c8b6bf9b 467 struct snd_pcm_substream *substream)
2f2f4251
M
468{
469 struct sigmatel_spec *spec = codec->spec;
470 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
471}
472
dabbed6f
M
473/*
474 * Digital playback callbacks
475 */
476static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
477 struct hda_codec *codec,
c8b6bf9b 478 struct snd_pcm_substream *substream)
dabbed6f
M
479{
480 struct sigmatel_spec *spec = codec->spec;
481 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
482}
483
484static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
485 struct hda_codec *codec,
c8b6bf9b 486 struct snd_pcm_substream *substream)
dabbed6f
M
487{
488 struct sigmatel_spec *spec = codec->spec;
489 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
490}
491
492
2f2f4251
M
493/*
494 * Analog capture callbacks
495 */
496static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
497 struct hda_codec *codec,
498 unsigned int stream_tag,
499 unsigned int format,
c8b6bf9b 500 struct snd_pcm_substream *substream)
2f2f4251
M
501{
502 struct sigmatel_spec *spec = codec->spec;
503
504 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
505 stream_tag, 0, format);
506 return 0;
507}
508
509static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
510 struct hda_codec *codec,
c8b6bf9b 511 struct snd_pcm_substream *substream)
2f2f4251
M
512{
513 struct sigmatel_spec *spec = codec->spec;
514
515 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
516 return 0;
517}
518
dabbed6f
M
519static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
520 .substreams = 1,
521 .channels_min = 2,
522 .channels_max = 2,
523 /* NID is set in stac92xx_build_pcms */
524 .ops = {
525 .open = stac92xx_dig_playback_pcm_open,
526 .close = stac92xx_dig_playback_pcm_close
527 },
528};
529
530static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
531 .substreams = 1,
532 .channels_min = 2,
533 .channels_max = 2,
534 /* NID is set in stac92xx_build_pcms */
535};
536
2f2f4251
M
537static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
538 .substreams = 1,
539 .channels_min = 2,
c7d4b2fa 540 .channels_max = 8,
2f2f4251
M
541 .nid = 0x02, /* NID to query formats and rates */
542 .ops = {
543 .open = stac92xx_playback_pcm_open,
544 .prepare = stac92xx_playback_pcm_prepare,
545 .cleanup = stac92xx_playback_pcm_cleanup
546 },
547};
548
3cc08dc6
MP
549static struct hda_pcm_stream stac92xx_pcm_analog_alt_playback = {
550 .substreams = 1,
551 .channels_min = 2,
552 .channels_max = 2,
553 .nid = 0x06, /* NID to query formats and rates */
554 .ops = {
555 .open = stac92xx_playback_pcm_open,
556 .prepare = stac92xx_playback_pcm_prepare,
557 .cleanup = stac92xx_playback_pcm_cleanup
558 },
559};
560
2f2f4251
M
561static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
562 .substreams = 2,
563 .channels_min = 2,
564 .channels_max = 2,
3cc08dc6 565 /* NID is set in stac92xx_build_pcms */
2f2f4251
M
566 .ops = {
567 .prepare = stac92xx_capture_pcm_prepare,
568 .cleanup = stac92xx_capture_pcm_cleanup
569 },
570};
571
572static int stac92xx_build_pcms(struct hda_codec *codec)
573{
574 struct sigmatel_spec *spec = codec->spec;
575 struct hda_pcm *info = spec->pcm_rec;
576
577 codec->num_pcms = 1;
578 codec->pcm_info = info;
579
c7d4b2fa 580 info->name = "STAC92xx Analog";
2f2f4251 581 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
2f2f4251 582 info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
3cc08dc6
MP
583 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
584
585 if (spec->alt_switch) {
586 codec->num_pcms++;
587 info++;
588 info->name = "STAC92xx Analog Alt";
589 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_alt_playback;
590 }
2f2f4251 591
dabbed6f
M
592 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
593 codec->num_pcms++;
594 info++;
595 info->name = "STAC92xx Digital";
596 if (spec->multiout.dig_out_nid) {
597 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
598 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
599 }
600 if (spec->dig_in_nid) {
601 info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
602 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
603 }
604 }
605
2f2f4251
M
606 return 0;
607}
608
c960a03b
TI
609static unsigned int stac92xx_get_vref(struct hda_codec *codec, hda_nid_t nid)
610{
611 unsigned int pincap = snd_hda_param_read(codec, nid,
612 AC_PAR_PIN_CAP);
613 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
614 if (pincap & AC_PINCAP_VREF_100)
615 return AC_PINCTL_VREF_100;
616 if (pincap & AC_PINCAP_VREF_80)
617 return AC_PINCTL_VREF_80;
618 if (pincap & AC_PINCAP_VREF_50)
619 return AC_PINCTL_VREF_50;
620 if (pincap & AC_PINCAP_VREF_GRD)
621 return AC_PINCTL_VREF_GRD;
622 return 0;
623}
624
403d1944
MP
625static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
626
627{
628 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
629}
630
631static int stac92xx_io_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
632{
633 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
634 uinfo->count = 1;
635 uinfo->value.integer.min = 0;
636 uinfo->value.integer.max = 1;
637 return 0;
638}
639
640static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
641{
642 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
643 struct sigmatel_spec *spec = codec->spec;
644 int io_idx = kcontrol-> private_value & 0xff;
645
646 ucontrol->value.integer.value[0] = spec->io_switch[io_idx];
647 return 0;
648}
649
650static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
651{
652 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
653 struct sigmatel_spec *spec = codec->spec;
654 hda_nid_t nid = kcontrol->private_value >> 8;
655 int io_idx = kcontrol-> private_value & 0xff;
656 unsigned short val = ucontrol->value.integer.value[0];
657
658 spec->io_switch[io_idx] = val;
659
660 if (val)
661 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
c960a03b
TI
662 else {
663 unsigned int pinctl = AC_PINCTL_IN_EN;
664 if (io_idx) /* set VREF for mic */
665 pinctl |= stac92xx_get_vref(codec, nid);
666 stac92xx_auto_set_pinctl(codec, nid, pinctl);
667 }
403d1944
MP
668 return 1;
669}
670
671#define STAC_CODEC_IO_SWITCH(xname, xpval) \
672 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
673 .name = xname, \
674 .index = 0, \
675 .info = stac92xx_io_switch_info, \
676 .get = stac92xx_io_switch_get, \
677 .put = stac92xx_io_switch_put, \
678 .private_value = xpval, \
679 }
680
681
c7d4b2fa
M
682enum {
683 STAC_CTL_WIDGET_VOL,
684 STAC_CTL_WIDGET_MUTE,
403d1944 685 STAC_CTL_WIDGET_IO_SWITCH,
c7d4b2fa
M
686};
687
c8b6bf9b 688static struct snd_kcontrol_new stac92xx_control_templates[] = {
c7d4b2fa
M
689 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
690 HDA_CODEC_MUTE(NULL, 0, 0, 0),
403d1944 691 STAC_CODEC_IO_SWITCH(NULL, 0),
c7d4b2fa
M
692};
693
694/* add dynamic controls */
695static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
696{
c8b6bf9b 697 struct snd_kcontrol_new *knew;
c7d4b2fa
M
698
699 if (spec->num_kctl_used >= spec->num_kctl_alloc) {
700 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
701
702 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
703 if (! knew)
704 return -ENOMEM;
705 if (spec->kctl_alloc) {
706 memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
707 kfree(spec->kctl_alloc);
708 }
709 spec->kctl_alloc = knew;
710 spec->num_kctl_alloc = num;
711 }
712
713 knew = &spec->kctl_alloc[spec->num_kctl_used];
714 *knew = stac92xx_control_templates[type];
82fe0c58 715 knew->name = kstrdup(name, GFP_KERNEL);
c7d4b2fa
M
716 if (! knew->name)
717 return -ENOMEM;
718 knew->private_value = val;
719 spec->num_kctl_used++;
720 return 0;
721}
722
403d1944
MP
723/* flag inputs as additional dynamic lineouts */
724static int stac92xx_add_dyn_out_pins(struct hda_codec *codec, struct auto_pin_cfg *cfg)
725{
726 struct sigmatel_spec *spec = codec->spec;
727
728 switch (cfg->line_outs) {
729 case 3:
730 /* add line-in as side */
731 if (cfg->input_pins[AUTO_PIN_LINE]) {
732 cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_LINE];
733 spec->line_switch = 1;
734 cfg->line_outs++;
735 }
736 break;
737 case 2:
738 /* add line-in as clfe and mic as side */
739 if (cfg->input_pins[AUTO_PIN_LINE]) {
740 cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_LINE];
741 spec->line_switch = 1;
742 cfg->line_outs++;
743 }
744 if (cfg->input_pins[AUTO_PIN_MIC]) {
745 cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_MIC];
746 spec->mic_switch = 1;
747 cfg->line_outs++;
748 }
749 break;
750 case 1:
751 /* add line-in as surr and mic as clfe */
752 if (cfg->input_pins[AUTO_PIN_LINE]) {
753 cfg->line_out_pins[1] = cfg->input_pins[AUTO_PIN_LINE];
754 spec->line_switch = 1;
755 cfg->line_outs++;
756 }
757 if (cfg->input_pins[AUTO_PIN_MIC]) {
758 cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_MIC];
759 spec->mic_switch = 1;
760 cfg->line_outs++;
761 }
762 break;
763 }
764
765 return 0;
766}
767
3cc08dc6
MP
768/*
769 * XXX The line_out pin widget connection list may not be set to the
770 * desired DAC nid. This is the case on 927x where ports A and B can
771 * be routed to several DACs.
772 *
773 * This requires an analysis of the line-out/hp pin configuration
774 * to provide a best fit for pin/DAC configurations that are routable.
775 * For now, 927x DAC4 is not supported and 927x DAC1 output to ports
776 * A and B is not supported.
777 */
c7d4b2fa 778/* fill in the dac_nids table from the parsed pin configuration */
19039bd0
TI
779static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec,
780 const struct auto_pin_cfg *cfg)
c7d4b2fa
M
781{
782 struct sigmatel_spec *spec = codec->spec;
783 hda_nid_t nid;
784 int i;
785
786 /* check the pins hardwired to audio widget */
787 for (i = 0; i < cfg->line_outs; i++) {
788 nid = cfg->line_out_pins[i];
789 spec->multiout.dac_nids[i] = snd_hda_codec_read(codec, nid, 0,
790 AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
791 }
792
82bc955f 793 spec->multiout.num_dacs = cfg->line_outs;
c7d4b2fa
M
794
795 return 0;
796}
797
798/* add playback controls from the parsed DAC table */
19039bd0
TI
799static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec,
800 const struct auto_pin_cfg *cfg)
c7d4b2fa
M
801{
802 char name[32];
19039bd0
TI
803 static const char *chname[4] = {
804 "Front", "Surround", NULL /*CLFE*/, "Side"
805 };
c7d4b2fa
M
806 hda_nid_t nid;
807 int i, err;
808
809 for (i = 0; i < cfg->line_outs; i++) {
403d1944 810 if (!spec->multiout.dac_nids[i])
c7d4b2fa
M
811 continue;
812
813 nid = spec->multiout.dac_nids[i];
814
815 if (i == 2) {
816 /* Center/LFE */
817 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Center Playback Volume",
818 HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
819 return err;
820 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "LFE Playback Volume",
821 HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
822 return err;
823 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Center Playback Switch",
824 HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
825 return err;
826 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "LFE Playback Switch",
827 HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
828 return err;
829 } else {
830 sprintf(name, "%s Playback Volume", chname[i]);
831 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
832 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
833 return err;
834 sprintf(name, "%s Playback Switch", chname[i]);
835 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
836 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
837 return err;
838 }
839 }
840
403d1944
MP
841 if (spec->line_switch)
842 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Line In as Output Switch", cfg->input_pins[AUTO_PIN_LINE] << 8)) < 0)
843 return err;
844
845 if (spec->mic_switch)
846 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Mic as Output Switch", (cfg->input_pins[AUTO_PIN_MIC] << 8) | 1)) < 0)
847 return err;
848
c7d4b2fa
M
849 return 0;
850}
851
852/* add playback controls for HP output */
853static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec, struct auto_pin_cfg *cfg)
854{
855 struct sigmatel_spec *spec = codec->spec;
856 hda_nid_t pin = cfg->hp_pin;
857 hda_nid_t nid;
858 int i, err;
4e55096e 859 unsigned int wid_caps;
c7d4b2fa
M
860
861 if (! pin)
862 return 0;
863
54d17403 864 wid_caps = get_wcaps(codec, pin);
505cb341 865 if (wid_caps & AC_WCAP_UNSOL_CAP)
82bc955f 866 spec->hp_detect = 1;
4e55096e 867
c7d4b2fa
M
868 nid = snd_hda_codec_read(codec, pin, 0, AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
869 for (i = 0; i < cfg->line_outs; i++) {
870 if (! spec->multiout.dac_nids[i])
871 continue;
872 if (spec->multiout.dac_nids[i] == nid)
873 return 0;
874 }
875
876 spec->multiout.hp_nid = nid;
877
878 /* control HP volume/switch on the output mixer amp */
879 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Headphone Playback Volume",
880 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
881 return err;
882 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
883 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
884 return err;
885
886 return 0;
887}
888
889/* create playback/capture controls for input pins */
890static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
891{
892 struct sigmatel_spec *spec = codec->spec;
c7d4b2fa
M
893 struct hda_input_mux *imux = &spec->private_imux;
894 hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
895 int i, j, k;
896
897 for (i = 0; i < AUTO_PIN_LAST; i++) {
898 int index = -1;
899 if (cfg->input_pins[i]) {
4a471b7d 900 imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
c7d4b2fa
M
901
902 for (j=0; j<spec->num_muxes; j++) {
903 int num_cons = snd_hda_get_connections(codec, spec->mux_nids[j], con_lst, HDA_MAX_NUM_INPUTS);
904 for (k=0; k<num_cons; k++)
905 if (con_lst[k] == cfg->input_pins[i]) {
906 index = k;
907 break;
908 }
909 if (index >= 0)
910 break;
911 }
912 imux->items[imux->num_items].index = index;
913 imux->num_items++;
914 }
915 }
916
62fe78e9
SR
917 if (imux->num_items == 1) {
918 /*
919 * Set the current input for the muxes.
920 * The STAC9221 has two input muxes with identical source
921 * NID lists. Hopefully this won't get confused.
922 */
923 for (i = 0; i < spec->num_muxes; i++) {
924 snd_hda_codec_write(codec, spec->mux_nids[i], 0,
925 AC_VERB_SET_CONNECT_SEL,
926 imux->items[0].index);
927 }
928 }
929
c7d4b2fa
M
930 return 0;
931}
932
c7d4b2fa
M
933static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
934{
935 struct sigmatel_spec *spec = codec->spec;
936 int i;
937
938 for (i = 0; i < spec->autocfg.line_outs; i++) {
939 hda_nid_t nid = spec->autocfg.line_out_pins[i];
940 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
941 }
942}
943
944static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
945{
946 struct sigmatel_spec *spec = codec->spec;
947 hda_nid_t pin;
948
949 pin = spec->autocfg.hp_pin;
950 if (pin) /* connect to front */
951 stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
952}
953
3cc08dc6 954static int stac92xx_parse_auto_config(struct hda_codec *codec, hda_nid_t dig_out, hda_nid_t dig_in)
c7d4b2fa
M
955{
956 struct sigmatel_spec *spec = codec->spec;
957 int err;
958
df694daa 959 if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
c7d4b2fa 960 return err;
82bc955f 961 if (! spec->autocfg.line_outs)
869264c4 962 return 0; /* can't find valid pin config */
19039bd0 963
403d1944
MP
964 if ((err = stac92xx_add_dyn_out_pins(codec, &spec->autocfg)) < 0)
965 return err;
19039bd0
TI
966 if (spec->multiout.num_dacs == 0)
967 if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
968 return err;
c7d4b2fa
M
969
970 if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
971 (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
972 (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
973 return err;
974
975 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
403d1944 976 if (spec->multiout.max_channels > 2)
c7d4b2fa 977 spec->surr_switch = 1;
c7d4b2fa 978
82bc955f 979 if (spec->autocfg.dig_out_pin)
3cc08dc6 980 spec->multiout.dig_out_nid = dig_out;
82bc955f 981 if (spec->autocfg.dig_in_pin)
3cc08dc6 982 spec->dig_in_nid = dig_in;
c7d4b2fa
M
983
984 if (spec->kctl_alloc)
985 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
986
987 spec->input_mux = &spec->private_imux;
988
989 return 1;
990}
991
82bc955f
TI
992/* add playback controls for HP output */
993static int stac9200_auto_create_hp_ctls(struct hda_codec *codec,
994 struct auto_pin_cfg *cfg)
995{
996 struct sigmatel_spec *spec = codec->spec;
997 hda_nid_t pin = cfg->hp_pin;
998 unsigned int wid_caps;
999
1000 if (! pin)
1001 return 0;
1002
1003 wid_caps = get_wcaps(codec, pin);
505cb341 1004 if (wid_caps & AC_WCAP_UNSOL_CAP)
82bc955f 1005 spec->hp_detect = 1;
82bc955f
TI
1006
1007 return 0;
1008}
1009
c7d4b2fa
M
1010static int stac9200_parse_auto_config(struct hda_codec *codec)
1011{
1012 struct sigmatel_spec *spec = codec->spec;
1013 int err;
1014
df694daa 1015 if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
c7d4b2fa
M
1016 return err;
1017
1018 if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
1019 return err;
1020
82bc955f
TI
1021 if ((err = stac9200_auto_create_hp_ctls(codec, &spec->autocfg)) < 0)
1022 return err;
1023
1024 if (spec->autocfg.dig_out_pin)
c7d4b2fa 1025 spec->multiout.dig_out_nid = 0x05;
82bc955f 1026 if (spec->autocfg.dig_in_pin)
c7d4b2fa 1027 spec->dig_in_nid = 0x04;
c7d4b2fa
M
1028
1029 if (spec->kctl_alloc)
1030 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
1031
1032 spec->input_mux = &spec->private_imux;
1033
1034 return 1;
1035}
1036
62fe78e9
SR
1037/*
1038 * Early 2006 Intel Macintoshes with STAC9220X5 codecs seem to have a
1039 * funky external mute control using GPIO pins.
1040 */
1041
1042static void stac922x_gpio_mute(struct hda_codec *codec, int pin, int muted)
1043{
1044 unsigned int gpiostate, gpiomask, gpiodir;
1045
1046 gpiostate = snd_hda_codec_read(codec, codec->afg, 0,
1047 AC_VERB_GET_GPIO_DATA, 0);
1048
1049 if (!muted)
1050 gpiostate |= (1 << pin);
1051 else
1052 gpiostate &= ~(1 << pin);
1053
1054 gpiomask = snd_hda_codec_read(codec, codec->afg, 0,
1055 AC_VERB_GET_GPIO_MASK, 0);
1056 gpiomask |= (1 << pin);
1057
1058 gpiodir = snd_hda_codec_read(codec, codec->afg, 0,
1059 AC_VERB_GET_GPIO_DIRECTION, 0);
1060 gpiodir |= (1 << pin);
1061
1062 /* AppleHDA seems to do this -- WTF is this verb?? */
1063 snd_hda_codec_write(codec, codec->afg, 0, 0x7e7, 0);
1064
1065 snd_hda_codec_write(codec, codec->afg, 0,
1066 AC_VERB_SET_GPIO_MASK, gpiomask);
1067 snd_hda_codec_write(codec, codec->afg, 0,
1068 AC_VERB_SET_GPIO_DIRECTION, gpiodir);
1069
1070 msleep(1);
1071
1072 snd_hda_codec_write(codec, codec->afg, 0,
1073 AC_VERB_SET_GPIO_DATA, gpiostate);
1074}
1075
c7d4b2fa
M
1076static int stac92xx_init(struct hda_codec *codec)
1077{
1078 struct sigmatel_spec *spec = codec->spec;
82bc955f
TI
1079 struct auto_pin_cfg *cfg = &spec->autocfg;
1080 int i;
c7d4b2fa 1081
c7d4b2fa
M
1082 snd_hda_sequence_write(codec, spec->init);
1083
82bc955f
TI
1084 /* set up pins */
1085 if (spec->hp_detect) {
505cb341
TI
1086 /* Enable unsolicited responses on the HP widget */
1087 snd_hda_codec_write(codec, cfg->hp_pin, 0,
1088 AC_VERB_SET_UNSOLICITED_ENABLE,
1089 STAC_UNSOL_ENABLE);
82bc955f
TI
1090 /* fake event to set up pins */
1091 codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
1092 } else {
1093 stac92xx_auto_init_multi_out(codec);
1094 stac92xx_auto_init_hp_out(codec);
1095 }
1096 for (i = 0; i < AUTO_PIN_LAST; i++) {
c960a03b
TI
1097 hda_nid_t nid = cfg->input_pins[i];
1098 if (nid) {
1099 unsigned int pinctl = AC_PINCTL_IN_EN;
1100 if (i == AUTO_PIN_MIC || i == AUTO_PIN_FRONT_MIC)
1101 pinctl |= stac92xx_get_vref(codec, nid);
1102 stac92xx_auto_set_pinctl(codec, nid, pinctl);
1103 }
82bc955f
TI
1104 }
1105 if (cfg->dig_out_pin)
1106 stac92xx_auto_set_pinctl(codec, cfg->dig_out_pin,
1107 AC_PINCTL_OUT_EN);
1108 if (cfg->dig_in_pin)
1109 stac92xx_auto_set_pinctl(codec, cfg->dig_in_pin,
1110 AC_PINCTL_IN_EN);
1111
62fe78e9
SR
1112 if (spec->gpio_mute) {
1113 stac922x_gpio_mute(codec, 0, 0);
1114 stac922x_gpio_mute(codec, 1, 0);
1115 }
1116
c7d4b2fa
M
1117 return 0;
1118}
1119
2f2f4251
M
1120static void stac92xx_free(struct hda_codec *codec)
1121{
c7d4b2fa
M
1122 struct sigmatel_spec *spec = codec->spec;
1123 int i;
1124
1125 if (! spec)
1126 return;
1127
1128 if (spec->kctl_alloc) {
1129 for (i = 0; i < spec->num_kctl_used; i++)
1130 kfree(spec->kctl_alloc[i].name);
1131 kfree(spec->kctl_alloc);
1132 }
1133
1134 kfree(spec);
2f2f4251
M
1135}
1136
4e55096e
M
1137static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
1138 unsigned int flag)
1139{
1140 unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1141 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1142 snd_hda_codec_write(codec, nid, 0,
1143 AC_VERB_SET_PIN_WIDGET_CONTROL,
1144 pin_ctl | flag);
1145}
1146
1147static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
1148 unsigned int flag)
1149{
1150 unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1151 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1152 snd_hda_codec_write(codec, nid, 0,
1153 AC_VERB_SET_PIN_WIDGET_CONTROL,
1154 pin_ctl & ~flag);
1155}
1156
1157static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
1158{
1159 struct sigmatel_spec *spec = codec->spec;
1160 struct auto_pin_cfg *cfg = &spec->autocfg;
1161 int i, presence;
1162
1163 if ((res >> 26) != STAC_HP_EVENT)
1164 return;
1165
1166 presence = snd_hda_codec_read(codec, cfg->hp_pin, 0,
1167 AC_VERB_GET_PIN_SENSE, 0x00) >> 31;
1168
1169 if (presence) {
1170 /* disable lineouts, enable hp */
1171 for (i = 0; i < cfg->line_outs; i++)
1172 stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
1173 AC_PINCTL_OUT_EN);
1174 stac92xx_set_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
1175 } else {
1176 /* enable lineouts, disable hp */
1177 for (i = 0; i < cfg->line_outs; i++)
1178 stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
1179 AC_PINCTL_OUT_EN);
1180 stac92xx_reset_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
1181 }
1182}
1183
ff6fdc37
M
1184#ifdef CONFIG_PM
1185static int stac92xx_resume(struct hda_codec *codec)
1186{
1187 struct sigmatel_spec *spec = codec->spec;
1188 int i;
1189
1190 stac92xx_init(codec);
1191 for (i = 0; i < spec->num_mixers; i++)
1192 snd_hda_resume_ctls(codec, spec->mixers[i]);
1193 if (spec->multiout.dig_out_nid)
1194 snd_hda_resume_spdif_out(codec);
1195 if (spec->dig_in_nid)
1196 snd_hda_resume_spdif_in(codec);
1197
1198 return 0;
1199}
1200#endif
1201
2f2f4251
M
1202static struct hda_codec_ops stac92xx_patch_ops = {
1203 .build_controls = stac92xx_build_controls,
1204 .build_pcms = stac92xx_build_pcms,
1205 .init = stac92xx_init,
1206 .free = stac92xx_free,
4e55096e 1207 .unsol_event = stac92xx_unsol_event,
ff6fdc37
M
1208#ifdef CONFIG_PM
1209 .resume = stac92xx_resume,
1210#endif
2f2f4251
M
1211};
1212
1213static int patch_stac9200(struct hda_codec *codec)
1214{
1215 struct sigmatel_spec *spec;
c7d4b2fa 1216 int err;
2f2f4251 1217
e560d8d8 1218 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2f2f4251
M
1219 if (spec == NULL)
1220 return -ENOMEM;
1221
1222 codec->spec = spec;
403d1944
MP
1223 spec->board_config = snd_hda_check_board_config(codec, stac9200_cfg_tbl);
1224 if (spec->board_config < 0)
1225 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
1226 else {
1227 spec->num_pins = 8;
1228 spec->pin_nids = stac9200_pin_nids;
1229 spec->pin_configs = stac9200_brd_tbl[spec->board_config];
1230 stac92xx_set_config_regs(codec);
1231 }
2f2f4251
M
1232
1233 spec->multiout.max_channels = 2;
1234 spec->multiout.num_dacs = 1;
1235 spec->multiout.dac_nids = stac9200_dac_nids;
1236 spec->adc_nids = stac9200_adc_nids;
1237 spec->mux_nids = stac9200_mux_nids;
dabbed6f 1238 spec->num_muxes = 1;
c7d4b2fa
M
1239
1240 spec->init = stac9200_core_init;
2f2f4251 1241 spec->mixer = stac9200_mixer;
c7d4b2fa
M
1242
1243 err = stac9200_parse_auto_config(codec);
1244 if (err < 0) {
1245 stac92xx_free(codec);
1246 return err;
1247 }
2f2f4251
M
1248
1249 codec->patch_ops = stac92xx_patch_ops;
1250
1251 return 0;
1252}
1253
1254static int patch_stac922x(struct hda_codec *codec)
1255{
1256 struct sigmatel_spec *spec;
c7d4b2fa 1257 int err;
2f2f4251 1258
e560d8d8 1259 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2f2f4251
M
1260 if (spec == NULL)
1261 return -ENOMEM;
1262
1263 codec->spec = spec;
403d1944
MP
1264 spec->board_config = snd_hda_check_board_config(codec, stac922x_cfg_tbl);
1265 if (spec->board_config < 0)
19039bd0
TI
1266 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, "
1267 "using BIOS defaults\n");
62fe78e9 1268 else if (stac922x_brd_tbl[spec->board_config] != NULL) {
403d1944
MP
1269 spec->num_pins = 10;
1270 spec->pin_nids = stac922x_pin_nids;
1271 spec->pin_configs = stac922x_brd_tbl[spec->board_config];
1272 stac92xx_set_config_regs(codec);
1273 }
2f2f4251 1274
c7d4b2fa
M
1275 spec->adc_nids = stac922x_adc_nids;
1276 spec->mux_nids = stac922x_mux_nids;
1277 spec->num_muxes = 2;
1278
1279 spec->init = stac922x_core_init;
2f2f4251 1280 spec->mixer = stac922x_mixer;
c7d4b2fa
M
1281
1282 spec->multiout.dac_nids = spec->dac_nids;
19039bd0
TI
1283
1284 switch (spec->board_config) {
1285 case STAC_D965_2112:
1286 spec->adc_nids = stac9227_adc_nids;
1287 spec->mux_nids = stac9227_mux_nids;
1288#if 0
1289 spec->multiout.dac_nids = d965_2112_dac_nids;
1290 spec->multiout.num_dacs = ARRAY_SIZE(d965_2112_dac_nids);
1291#endif
1292 spec->init = d965_2112_core_init;
1293 spec->mixer = stac9227_mixer;
1294 break;
1295 case STAC_D965_284B:
1296 spec->adc_nids = stac9227_adc_nids;
1297 spec->mux_nids = stac9227_mux_nids;
1298 spec->init = stac9227_core_init;
1299 spec->mixer = stac9227_mixer;
1300 break;
1301 }
c7d4b2fa 1302
3cc08dc6
MP
1303 err = stac92xx_parse_auto_config(codec, 0x08, 0x09);
1304 if (err < 0) {
1305 stac92xx_free(codec);
1306 return err;
1307 }
1308
62fe78e9
SR
1309 if (spec->board_config == STAC_MACMINI)
1310 spec->gpio_mute = 1;
1311
3cc08dc6
MP
1312 codec->patch_ops = stac92xx_patch_ops;
1313
1314 return 0;
1315}
1316
1317static int patch_stac927x(struct hda_codec *codec)
1318{
1319 struct sigmatel_spec *spec;
1320 int err;
1321
1322 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1323 if (spec == NULL)
1324 return -ENOMEM;
1325
1326 codec->spec = spec;
1327 spec->board_config = snd_hda_check_board_config(codec, stac927x_cfg_tbl);
1328 if (spec->board_config < 0)
1329 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC927x, using BIOS defaults\n");
1330 else {
1331 spec->num_pins = 14;
1332 spec->pin_nids = stac927x_pin_nids;
1333 spec->pin_configs = stac927x_brd_tbl[spec->board_config];
1334 stac92xx_set_config_regs(codec);
1335 }
1336
1337 spec->adc_nids = stac927x_adc_nids;
1338 spec->mux_nids = stac927x_mux_nids;
1339 spec->num_muxes = 3;
1340
1341 spec->init = stac927x_core_init;
1342 spec->mixer = stac927x_mixer;
1343
1344 spec->multiout.dac_nids = spec->dac_nids;
1345
1346 err = stac92xx_parse_auto_config(codec, 0x1e, 0x20);
c7d4b2fa
M
1347 if (err < 0) {
1348 stac92xx_free(codec);
1349 return err;
1350 }
2f2f4251
M
1351
1352 codec->patch_ops = stac92xx_patch_ops;
1353
1354 return 0;
1355}
1356
db064e50
TI
1357/*
1358 * STAC 7661(?) hack
1359 */
1360
1361/* static config for Sony VAIO FE550G */
1362static hda_nid_t vaio_dacs[] = { 0x2 };
1363#define VAIO_HP_DAC 0x5
1364static hda_nid_t vaio_adcs[] = { 0x8 /*,0x6*/ };
1365static hda_nid_t vaio_mux_nids[] = { 0x15 };
1366
1367static struct hda_input_mux vaio_mux = {
1368 .num_items = 2,
1369 .items = {
d773781c
TI
1370 /* { "HP", 0x0 }, */
1371 { "Line", 0x1 },
db064e50
TI
1372 { "Mic", 0x2 },
1373 { "PCM", 0x3 },
1374 }
1375};
1376
1377static struct hda_verb vaio_init[] = {
1378 {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
1379 {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
1380 {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
1381 {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
1382 {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
1383 {0x15, AC_VERB_SET_CONNECT_SEL, 0x2}, /* mic-sel: 0a,0d,14,02 */
1384 {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
1385 {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
1386 {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
1387 {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
1388 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
1389 {}
1390};
1391
1392/* bind volumes of both NID 0x02 and 0x05 */
1393static int vaio_master_vol_put(struct snd_kcontrol *kcontrol,
1394 struct snd_ctl_elem_value *ucontrol)
1395{
1396 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1397 long *valp = ucontrol->value.integer.value;
1398 int change;
1399
1400 change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
1401 0x7f, valp[0] & 0x7f);
1402 change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
1403 0x7f, valp[1] & 0x7f);
1404 snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
1405 0x7f, valp[0] & 0x7f);
1406 snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
1407 0x7f, valp[1] & 0x7f);
1408 return change;
1409}
1410
1411/* bind volumes of both NID 0x02 and 0x05 */
1412static int vaio_master_sw_put(struct snd_kcontrol *kcontrol,
1413 struct snd_ctl_elem_value *ucontrol)
1414{
1415 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1416 long *valp = ucontrol->value.integer.value;
1417 int change;
1418
1419 change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
a9393d70 1420 0x80, (valp[0] ? 0 : 0x80));
db064e50 1421 change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
a9393d70 1422 0x80, (valp[1] ? 0 : 0x80));
db064e50 1423 snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
a9393d70 1424 0x80, (valp[0] ? 0 : 0x80));
db064e50 1425 snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
a9393d70 1426 0x80, (valp[1] ? 0 : 0x80));
db064e50
TI
1427 return change;
1428}
1429
1430static struct snd_kcontrol_new vaio_mixer[] = {
1431 {
1432 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1433 .name = "Master Playback Volume",
1434 .info = snd_hda_mixer_amp_volume_info,
1435 .get = snd_hda_mixer_amp_volume_get,
1436 .put = vaio_master_vol_put,
1437 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
1438 },
1439 {
1440 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1441 .name = "Master Playback Switch",
1442 .info = snd_hda_mixer_amp_switch_info,
1443 .get = snd_hda_mixer_amp_switch_get,
1444 .put = vaio_master_sw_put,
1445 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
1446 },
1447 /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
1448 HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
1449 HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
1450 {
1451 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1452 .name = "Capture Source",
1453 .count = 1,
1454 .info = stac92xx_mux_enum_info,
1455 .get = stac92xx_mux_enum_get,
1456 .put = stac92xx_mux_enum_put,
1457 },
1458 {}
1459};
1460
1461static struct hda_codec_ops stac7661_patch_ops = {
1462 .build_controls = stac92xx_build_controls,
1463 .build_pcms = stac92xx_build_pcms,
1464 .init = stac92xx_init,
1465 .free = stac92xx_free,
1466#ifdef CONFIG_PM
1467 .resume = stac92xx_resume,
1468#endif
1469};
1470
1471enum { STAC7661_VAIO };
1472
1473static struct hda_board_config stac7661_cfg_tbl[] = {
1474 { .modelname = "vaio", .config = STAC7661_VAIO },
1475 { .pci_subvendor = 0x104d, .pci_subdevice = 0x81e6,
1476 .config = STAC7661_VAIO },
1477 { .pci_subvendor = 0x104d, .pci_subdevice = 0x81ef,
1478 .config = STAC7661_VAIO },
1479 {}
1480};
1481
1482static int patch_stac7661(struct hda_codec *codec)
1483{
1484 struct sigmatel_spec *spec;
1485 int board_config;
1486
1487 board_config = snd_hda_check_board_config(codec, stac7661_cfg_tbl);
1488 if (board_config < 0)
1489 /* unknown config, let generic-parser do its job... */
1490 return snd_hda_parse_generic_codec(codec);
1491
1492 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1493 if (spec == NULL)
1494 return -ENOMEM;
1495
1496 codec->spec = spec;
1497 switch (board_config) {
1498 case STAC7661_VAIO:
1499 spec->mixer = vaio_mixer;
1500 spec->init = vaio_init;
1501 spec->multiout.max_channels = 2;
1502 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
1503 spec->multiout.dac_nids = vaio_dacs;
1504 spec->multiout.hp_nid = VAIO_HP_DAC;
1505 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
1506 spec->adc_nids = vaio_adcs;
1507 spec->input_mux = &vaio_mux;
1508 spec->mux_nids = vaio_mux_nids;
1509 break;
1510 }
1511
1512 codec->patch_ops = stac7661_patch_ops;
1513 return 0;
1514}
1515
1516
2f2f4251
M
1517/*
1518 * patch entries
1519 */
1520struct hda_codec_preset snd_hda_preset_sigmatel[] = {
1521 { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
1522 { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
1523 { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
1524 { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
1525 { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
1526 { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
1527 { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
22a27c7f
MP
1528 { .id = 0x83847618, .name = "STAC9227", .patch = patch_stac927x },
1529 { .id = 0x83847619, .name = "STAC9227", .patch = patch_stac927x },
1530 { .id = 0x83847616, .name = "STAC9228", .patch = patch_stac927x },
1531 { .id = 0x83847617, .name = "STAC9228", .patch = patch_stac927x },
1532 { .id = 0x83847614, .name = "STAC9229", .patch = patch_stac927x },
1533 { .id = 0x83847615, .name = "STAC9229", .patch = patch_stac927x },
3cc08dc6
MP
1534 { .id = 0x83847620, .name = "STAC9274", .patch = patch_stac927x },
1535 { .id = 0x83847621, .name = "STAC9274D", .patch = patch_stac927x },
1536 { .id = 0x83847622, .name = "STAC9273X", .patch = patch_stac927x },
1537 { .id = 0x83847623, .name = "STAC9273D", .patch = patch_stac927x },
1538 { .id = 0x83847624, .name = "STAC9272X", .patch = patch_stac927x },
1539 { .id = 0x83847625, .name = "STAC9272D", .patch = patch_stac927x },
1540 { .id = 0x83847626, .name = "STAC9271X", .patch = patch_stac927x },
1541 { .id = 0x83847627, .name = "STAC9271D", .patch = patch_stac927x },
1542 { .id = 0x83847628, .name = "STAC9274X5NH", .patch = patch_stac927x },
1543 { .id = 0x83847629, .name = "STAC9274D5NH", .patch = patch_stac927x },
db064e50 1544 { .id = 0x83847661, .name = "STAC7661", .patch = patch_stac7661 },
2f2f4251
M
1545 {} /* terminator */
1546};