blob: 7152607879539a8bf4be5e25060c810a82da5985 [file] [log] [blame]
Matt2f2f4252005-04-13 14:45:30 +02001/*
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.
Matt Porter403d1942005-11-29 15:00:51 +01007 * Matt Porter <mporter@embeddedalley.com>
Matt2f2f4252005-04-13 14:45:30 +02008 *
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>
Mattc7d4b2f2005-06-27 14:59:41 +020033#include <sound/asoundef.h>
Matt2f2f4252005-04-13 14:45:30 +020034#include "hda_codec.h"
35#include "hda_local.h"
36
Matt4e550962005-07-04 17:51:39 +020037#define NUM_CONTROL_ALLOC 32
38#define STAC_HP_EVENT 0x37
39#define STAC_UNSOL_ENABLE (AC_USRSP_EN | STAC_HP_EVENT)
40
Matt Porter403d1942005-11-29 15:00:51 +010041#define STAC_REF 0
42#define STAC_D945GTP3 1
43#define STAC_D945GTP5 2
44
Matt2f2f4252005-04-13 14:45:30 +020045struct sigmatel_spec {
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +010046 struct snd_kcontrol_new *mixers[4];
Mattc7d4b2f2005-06-27 14:59:41 +020047 unsigned int num_mixers;
48
Matt Porter403d1942005-11-29 15:00:51 +010049 int board_config;
Mattc7d4b2f2005-06-27 14:59:41 +020050 unsigned int surr_switch: 1;
Matt Porter403d1942005-11-29 15:00:51 +010051 unsigned int line_switch: 1;
52 unsigned int mic_switch: 1;
Matt Porter3cc08dc2006-01-23 15:27:49 +010053 unsigned int alt_switch: 1;
Takashi Iwai82bc9552006-03-21 11:24:42 +010054 unsigned int hp_detect: 1;
Mattc7d4b2f2005-06-27 14:59:41 +020055
Matt2f2f4252005-04-13 14:45:30 +020056 /* playback */
57 struct hda_multi_out multiout;
Matt Porter3cc08dc2006-01-23 15:27:49 +010058 hda_nid_t dac_nids[5];
Matt2f2f4252005-04-13 14:45:30 +020059
60 /* capture */
61 hda_nid_t *adc_nids;
Matt2f2f4252005-04-13 14:45:30 +020062 unsigned int num_adcs;
Mattdabbed62005-06-14 10:19:34 +020063 hda_nid_t *mux_nids;
64 unsigned int num_muxes;
Mattdabbed62005-06-14 10:19:34 +020065 hda_nid_t dig_in_nid;
Matt2f2f4252005-04-13 14:45:30 +020066
Matt2f2f4252005-04-13 14:45:30 +020067 /* pin widgets */
68 hda_nid_t *pin_nids;
69 unsigned int num_pins;
Matt2f2f4252005-04-13 14:45:30 +020070 unsigned int *pin_configs;
Matt2f2f4252005-04-13 14:45:30 +020071
72 /* codec specific stuff */
73 struct hda_verb *init;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +010074 struct snd_kcontrol_new *mixer;
Matt2f2f4252005-04-13 14:45:30 +020075
76 /* capture source */
Mattc7d4b2f2005-06-27 14:59:41 +020077 struct hda_input_mux *input_mux;
Matt Porter3cc08dc2006-01-23 15:27:49 +010078 unsigned int cur_mux[3];
Matt2f2f4252005-04-13 14:45:30 +020079
Matt Porter403d1942005-11-29 15:00:51 +010080 /* i/o switches */
81 unsigned int io_switch[2];
Matt2f2f4252005-04-13 14:45:30 +020082
Mattc7d4b2f2005-06-27 14:59:41 +020083 struct hda_pcm pcm_rec[2]; /* PCM information */
84
85 /* dynamic controls and input_mux */
86 struct auto_pin_cfg autocfg;
87 unsigned int num_kctl_alloc, num_kctl_used;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +010088 struct snd_kcontrol_new *kctl_alloc;
Mattc7d4b2f2005-06-27 14:59:41 +020089 struct hda_input_mux private_imux;
Matt2f2f4252005-04-13 14:45:30 +020090};
91
92static hda_nid_t stac9200_adc_nids[1] = {
93 0x03,
94};
95
96static hda_nid_t stac9200_mux_nids[1] = {
97 0x0c,
98};
99
100static hda_nid_t stac9200_dac_nids[1] = {
101 0x02,
102};
103
Matt2f2f4252005-04-13 14:45:30 +0200104static hda_nid_t stac922x_adc_nids[2] = {
105 0x06, 0x07,
106};
107
108static hda_nid_t stac922x_mux_nids[2] = {
109 0x12, 0x13,
110};
111
Matt Porter3cc08dc2006-01-23 15:27:49 +0100112static hda_nid_t stac927x_adc_nids[3] = {
113 0x07, 0x08, 0x09
114};
115
116static hda_nid_t stac927x_mux_nids[3] = {
117 0x15, 0x16, 0x17
118};
119
Mattc7d4b2f2005-06-27 14:59:41 +0200120static hda_nid_t stac9200_pin_nids[8] = {
121 0x08, 0x09, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
Matt2f2f4252005-04-13 14:45:30 +0200122};
123
124static hda_nid_t stac922x_pin_nids[10] = {
125 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
126 0x0f, 0x10, 0x11, 0x15, 0x1b,
127};
128
Matt Porter3cc08dc2006-01-23 15:27:49 +0100129static hda_nid_t stac927x_pin_nids[14] = {
130 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
131 0x0f, 0x10, 0x11, 0x12, 0x13,
132 0x14, 0x21, 0x22, 0x23,
133};
134
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100135static int stac92xx_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Matt2f2f4252005-04-13 14:45:30 +0200136{
137 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
138 struct sigmatel_spec *spec = codec->spec;
Mattc7d4b2f2005-06-27 14:59:41 +0200139 return snd_hda_input_mux_info(spec->input_mux, uinfo);
Matt2f2f4252005-04-13 14:45:30 +0200140}
141
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100142static int stac92xx_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Matt2f2f4252005-04-13 14:45:30 +0200143{
144 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
145 struct sigmatel_spec *spec = codec->spec;
146 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
147
148 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
149 return 0;
150}
151
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100152static int stac92xx_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Matt2f2f4252005-04-13 14:45:30 +0200153{
154 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
155 struct sigmatel_spec *spec = codec->spec;
156 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
157
Mattc7d4b2f2005-06-27 14:59:41 +0200158 return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
Matt2f2f4252005-04-13 14:45:30 +0200159 spec->mux_nids[adc_idx], &spec->cur_mux[adc_idx]);
160}
161
Mattc7d4b2f2005-06-27 14:59:41 +0200162static struct hda_verb stac9200_core_init[] = {
Matt2f2f4252005-04-13 14:45:30 +0200163 /* set dac0mux for dac converter */
Mattc7d4b2f2005-06-27 14:59:41 +0200164 { 0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
Matt2f2f4252005-04-13 14:45:30 +0200165 {}
166};
167
Mattc7d4b2f2005-06-27 14:59:41 +0200168static struct hda_verb stac922x_core_init[] = {
Matt2f2f4252005-04-13 14:45:30 +0200169 /* set master volume and direct control */
Mattc7d4b2f2005-06-27 14:59:41 +0200170 { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
Matt2f2f4252005-04-13 14:45:30 +0200171 {}
172};
173
Matt Porter3cc08dc2006-01-23 15:27:49 +0100174static struct hda_verb stac927x_core_init[] = {
175 /* set master volume and direct control */
176 { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
177 {}
178};
179
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100180static struct snd_kcontrol_new stac9200_mixer[] = {
Matt2f2f4252005-04-13 14:45:30 +0200181 HDA_CODEC_VOLUME("Master Playback Volume", 0xb, 0, HDA_OUTPUT),
182 HDA_CODEC_MUTE("Master Playback Switch", 0xb, 0, HDA_OUTPUT),
183 {
184 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
185 .name = "Input Source",
186 .count = 1,
187 .info = stac92xx_mux_enum_info,
188 .get = stac92xx_mux_enum_get,
189 .put = stac92xx_mux_enum_put,
190 },
191 HDA_CODEC_VOLUME("Capture Volume", 0x0a, 0, HDA_OUTPUT),
192 HDA_CODEC_MUTE("Capture Switch", 0x0a, 0, HDA_OUTPUT),
Mattc7d4b2f2005-06-27 14:59:41 +0200193 HDA_CODEC_VOLUME("Capture Mux Volume", 0x0c, 0, HDA_OUTPUT),
Matt2f2f4252005-04-13 14:45:30 +0200194 { } /* end */
195};
196
Mattc7d4b2f2005-06-27 14:59:41 +0200197/* This needs to be generated dynamically based on sequence */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100198static struct snd_kcontrol_new stac922x_mixer[] = {
Matt2f2f4252005-04-13 14:45:30 +0200199 {
200 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
201 .name = "Input Source",
202 .count = 1,
203 .info = stac92xx_mux_enum_info,
204 .get = stac92xx_mux_enum_get,
205 .put = stac92xx_mux_enum_put,
206 },
207 HDA_CODEC_VOLUME("Capture Volume", 0x17, 0x0, HDA_INPUT),
Takashi Iwai0fd17082006-01-13 18:46:21 +0100208 HDA_CODEC_MUTE("Capture Switch", 0x17, 0x0, HDA_INPUT),
Matt2f2f4252005-04-13 14:45:30 +0200209 HDA_CODEC_VOLUME("Mux Capture Volume", 0x12, 0x0, HDA_OUTPUT),
210 { } /* end */
211};
212
Matt Porter3cc08dc2006-01-23 15:27:49 +0100213static snd_kcontrol_new_t stac927x_mixer[] = {
214 {
215 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
216 .name = "Input Source",
217 .count = 1,
218 .info = stac92xx_mux_enum_info,
219 .get = stac92xx_mux_enum_get,
220 .put = stac92xx_mux_enum_put,
221 },
222 HDA_CODEC_VOLUME("InMux Capture Volume", 0x15, 0x0, HDA_OUTPUT),
223 HDA_CODEC_VOLUME("InVol Capture Volume", 0x18, 0x0, HDA_INPUT),
224 HDA_CODEC_MUTE("ADCMux Capture Switch", 0x1b, 0x0, HDA_OUTPUT),
225 { } /* end */
226};
227
Matt2f2f4252005-04-13 14:45:30 +0200228static int stac92xx_build_controls(struct hda_codec *codec)
229{
230 struct sigmatel_spec *spec = codec->spec;
231 int err;
Mattc7d4b2f2005-06-27 14:59:41 +0200232 int i;
Matt2f2f4252005-04-13 14:45:30 +0200233
234 err = snd_hda_add_new_ctls(codec, spec->mixer);
235 if (err < 0)
236 return err;
Mattc7d4b2f2005-06-27 14:59:41 +0200237
238 for (i = 0; i < spec->num_mixers; i++) {
239 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
240 if (err < 0)
241 return err;
242 }
243
Mattdabbed62005-06-14 10:19:34 +0200244 if (spec->multiout.dig_out_nid) {
245 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
246 if (err < 0)
247 return err;
248 }
249 if (spec->dig_in_nid) {
250 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
251 if (err < 0)
252 return err;
253 }
254 return 0;
Matt2f2f4252005-04-13 14:45:30 +0200255}
256
Matt Porter403d1942005-11-29 15:00:51 +0100257static unsigned int ref9200_pin_configs[8] = {
Mattdabbed62005-06-14 10:19:34 +0200258 0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
Matt2f2f4252005-04-13 14:45:30 +0200259 0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
260};
261
Matt Porter403d1942005-11-29 15:00:51 +0100262static unsigned int *stac9200_brd_tbl[] = {
263 ref9200_pin_configs,
264};
265
266static struct hda_board_config stac9200_cfg_tbl[] = {
267 { .modelname = "ref",
268 .pci_subvendor = PCI_VENDOR_ID_INTEL,
269 .pci_subdevice = 0x2668, /* DFI LanParty */
270 .config = STAC_REF },
271 {} /* terminator */
272};
273
274static unsigned int ref922x_pin_configs[10] = {
275 0x01014010, 0x01016011, 0x01012012, 0x0221401f,
276 0x01813122, 0x01011014, 0x01441030, 0x01c41030,
Matt2f2f4252005-04-13 14:45:30 +0200277 0x40000100, 0x40000100,
278};
279
Matt Porter403d1942005-11-29 15:00:51 +0100280static unsigned int d945gtp3_pin_configs[10] = {
Matt Porter869264c2006-01-25 19:20:50 +0100281 0x0221401f, 0x01a19022, 0x01813021, 0x01014010,
Matt Porter403d1942005-11-29 15:00:51 +0100282 0x40000100, 0x40000100, 0x40000100, 0x40000100,
283 0x02a19120, 0x40000100,
284};
285
286static unsigned int d945gtp5_pin_configs[10] = {
Matt Porter869264c2006-01-25 19:20:50 +0100287 0x0221401f, 0x01011012, 0x01813024, 0x01014010,
288 0x01a19021, 0x01016011, 0x01452130, 0x40000100,
Matt Porter403d1942005-11-29 15:00:51 +0100289 0x02a19320, 0x40000100,
290};
291
292static unsigned int *stac922x_brd_tbl[] = {
293 ref922x_pin_configs,
294 d945gtp3_pin_configs,
295 d945gtp5_pin_configs,
296};
297
298static struct hda_board_config stac922x_cfg_tbl[] = {
299 { .modelname = "ref",
300 .pci_subvendor = PCI_VENDOR_ID_INTEL,
301 .pci_subdevice = 0x2668, /* DFI LanParty */
302 .config = STAC_REF }, /* SigmaTel reference board */
303 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
304 .pci_subdevice = 0x0101,
305 .config = STAC_D945GTP3 }, /* Intel D945GTP - 3 Stack */
306 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
Takashi Iwai353b9e62006-02-16 18:16:17 +0100307 .pci_subdevice = 0x0202,
308 .config = STAC_D945GTP3 }, /* Intel D945GNT - 3 Stack, 9221 A1 */
309 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
310 .pci_subdevice = 0x0b0b,
311 .config = STAC_D945GTP3 }, /* Intel D945PSN - 3 Stack, 9221 A1 */
312 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
Ashley Clarka29b6c82006-04-10 11:31:03 +0200313 .pci_subdevice = 0x0707,
314 .config = STAC_D945GTP5 }, /* Intel D945PSV - 5 Stack */
315 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
Matt Porter403d1942005-11-29 15:00:51 +0100316 .pci_subdevice = 0x0404,
317 .config = STAC_D945GTP5 }, /* Intel D945GTP - 5 Stack */
318 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
319 .pci_subdevice = 0x0303,
320 .config = STAC_D945GTP5 }, /* Intel D945GNT - 5 Stack */
321 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
322 .pci_subdevice = 0x0013,
323 .config = STAC_D945GTP5 }, /* Intel D955XBK - 5 Stack */
Matt Porterd62c40e2006-01-23 15:26:27 +0100324 { .pci_subvendor = PCI_VENDOR_ID_INTEL,
325 .pci_subdevice = 0x0417,
326 .config = STAC_D945GTP5 }, /* Intel D975XBK - 5 Stack */
Matt Porter403d1942005-11-29 15:00:51 +0100327 {} /* terminator */
328};
329
Matt Porter3cc08dc2006-01-23 15:27:49 +0100330static unsigned int ref927x_pin_configs[14] = {
331 0x01813122, 0x01a19021, 0x01014010, 0x01016011,
332 0x01012012, 0x01011014, 0x40000100, 0x40000100,
333 0x40000100, 0x40000100, 0x40000100, 0x01441030,
334 0x01c41030, 0x40000100,
335};
336
337static unsigned int *stac927x_brd_tbl[] = {
338 ref927x_pin_configs,
339};
340
341static struct hda_board_config stac927x_cfg_tbl[] = {
342 { .modelname = "ref",
343 .pci_subvendor = PCI_VENDOR_ID_INTEL,
344 .pci_subdevice = 0x2668, /* DFI LanParty */
345 .config = STAC_REF }, /* SigmaTel reference board */
346 {} /* terminator */
347};
348
Matt2f2f4252005-04-13 14:45:30 +0200349static void stac92xx_set_config_regs(struct hda_codec *codec)
350{
351 int i;
352 struct sigmatel_spec *spec = codec->spec;
353 unsigned int pin_cfg;
354
355 for (i=0; i < spec->num_pins; i++) {
356 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
357 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
358 spec->pin_configs[i] & 0x000000ff);
359 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
360 AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
361 (spec->pin_configs[i] & 0x0000ff00) >> 8);
362 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
363 AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
364 (spec->pin_configs[i] & 0x00ff0000) >> 16);
365 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
366 AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
367 spec->pin_configs[i] >> 24);
368 pin_cfg = snd_hda_codec_read(codec, spec->pin_nids[i], 0,
369 AC_VERB_GET_CONFIG_DEFAULT,
370 0x00);
Matt Porter403d1942005-11-29 15:00:51 +0100371 snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x pin config %8.8x\n", spec->pin_nids[i], pin_cfg);
Matt2f2f4252005-04-13 14:45:30 +0200372 }
373}
Matt2f2f4252005-04-13 14:45:30 +0200374
Matt2f2f4252005-04-13 14:45:30 +0200375/*
376 * Analog playback callbacks
377 */
378static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
379 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100380 struct snd_pcm_substream *substream)
Matt2f2f4252005-04-13 14:45:30 +0200381{
382 struct sigmatel_spec *spec = codec->spec;
383 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
384}
385
386static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
387 struct hda_codec *codec,
388 unsigned int stream_tag,
389 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100390 struct snd_pcm_substream *substream)
Matt2f2f4252005-04-13 14:45:30 +0200391{
392 struct sigmatel_spec *spec = codec->spec;
Matt Porter403d1942005-11-29 15:00:51 +0100393 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
Matt2f2f4252005-04-13 14:45:30 +0200394}
395
396static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
397 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100398 struct snd_pcm_substream *substream)
Matt2f2f4252005-04-13 14:45:30 +0200399{
400 struct sigmatel_spec *spec = codec->spec;
401 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
402}
403
404/*
Mattdabbed62005-06-14 10:19:34 +0200405 * Digital playback callbacks
406 */
407static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
408 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100409 struct snd_pcm_substream *substream)
Mattdabbed62005-06-14 10:19:34 +0200410{
411 struct sigmatel_spec *spec = codec->spec;
412 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
413}
414
415static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
416 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100417 struct snd_pcm_substream *substream)
Mattdabbed62005-06-14 10:19:34 +0200418{
419 struct sigmatel_spec *spec = codec->spec;
420 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
421}
422
423
424/*
Matt2f2f4252005-04-13 14:45:30 +0200425 * Analog capture callbacks
426 */
427static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
428 struct hda_codec *codec,
429 unsigned int stream_tag,
430 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100431 struct snd_pcm_substream *substream)
Matt2f2f4252005-04-13 14:45:30 +0200432{
433 struct sigmatel_spec *spec = codec->spec;
434
435 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
436 stream_tag, 0, format);
437 return 0;
438}
439
440static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
441 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100442 struct snd_pcm_substream *substream)
Matt2f2f4252005-04-13 14:45:30 +0200443{
444 struct sigmatel_spec *spec = codec->spec;
445
446 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
447 return 0;
448}
449
Mattdabbed62005-06-14 10:19:34 +0200450static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
451 .substreams = 1,
452 .channels_min = 2,
453 .channels_max = 2,
454 /* NID is set in stac92xx_build_pcms */
455 .ops = {
456 .open = stac92xx_dig_playback_pcm_open,
457 .close = stac92xx_dig_playback_pcm_close
458 },
459};
460
461static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
462 .substreams = 1,
463 .channels_min = 2,
464 .channels_max = 2,
465 /* NID is set in stac92xx_build_pcms */
466};
467
Matt2f2f4252005-04-13 14:45:30 +0200468static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
469 .substreams = 1,
470 .channels_min = 2,
Mattc7d4b2f2005-06-27 14:59:41 +0200471 .channels_max = 8,
Matt2f2f4252005-04-13 14:45:30 +0200472 .nid = 0x02, /* NID to query formats and rates */
473 .ops = {
474 .open = stac92xx_playback_pcm_open,
475 .prepare = stac92xx_playback_pcm_prepare,
476 .cleanup = stac92xx_playback_pcm_cleanup
477 },
478};
479
Matt Porter3cc08dc2006-01-23 15:27:49 +0100480static struct hda_pcm_stream stac92xx_pcm_analog_alt_playback = {
481 .substreams = 1,
482 .channels_min = 2,
483 .channels_max = 2,
484 .nid = 0x06, /* NID to query formats and rates */
485 .ops = {
486 .open = stac92xx_playback_pcm_open,
487 .prepare = stac92xx_playback_pcm_prepare,
488 .cleanup = stac92xx_playback_pcm_cleanup
489 },
490};
491
Matt2f2f4252005-04-13 14:45:30 +0200492static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
493 .substreams = 2,
494 .channels_min = 2,
495 .channels_max = 2,
Matt Porter3cc08dc2006-01-23 15:27:49 +0100496 /* NID is set in stac92xx_build_pcms */
Matt2f2f4252005-04-13 14:45:30 +0200497 .ops = {
498 .prepare = stac92xx_capture_pcm_prepare,
499 .cleanup = stac92xx_capture_pcm_cleanup
500 },
501};
502
503static int stac92xx_build_pcms(struct hda_codec *codec)
504{
505 struct sigmatel_spec *spec = codec->spec;
506 struct hda_pcm *info = spec->pcm_rec;
507
508 codec->num_pcms = 1;
509 codec->pcm_info = info;
510
Mattc7d4b2f2005-06-27 14:59:41 +0200511 info->name = "STAC92xx Analog";
Matt2f2f4252005-04-13 14:45:30 +0200512 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
Matt2f2f4252005-04-13 14:45:30 +0200513 info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
Matt Porter3cc08dc2006-01-23 15:27:49 +0100514 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
515
516 if (spec->alt_switch) {
517 codec->num_pcms++;
518 info++;
519 info->name = "STAC92xx Analog Alt";
520 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_alt_playback;
521 }
Matt2f2f4252005-04-13 14:45:30 +0200522
Mattdabbed62005-06-14 10:19:34 +0200523 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
524 codec->num_pcms++;
525 info++;
526 info->name = "STAC92xx Digital";
527 if (spec->multiout.dig_out_nid) {
528 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
529 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
530 }
531 if (spec->dig_in_nid) {
532 info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
533 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
534 }
535 }
536
Matt2f2f4252005-04-13 14:45:30 +0200537 return 0;
538}
539
Takashi Iwaic960a032006-03-23 17:06:28 +0100540static unsigned int stac92xx_get_vref(struct hda_codec *codec, hda_nid_t nid)
541{
542 unsigned int pincap = snd_hda_param_read(codec, nid,
543 AC_PAR_PIN_CAP);
544 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
545 if (pincap & AC_PINCAP_VREF_100)
546 return AC_PINCTL_VREF_100;
547 if (pincap & AC_PINCAP_VREF_80)
548 return AC_PINCTL_VREF_80;
549 if (pincap & AC_PINCAP_VREF_50)
550 return AC_PINCTL_VREF_50;
551 if (pincap & AC_PINCAP_VREF_GRD)
552 return AC_PINCTL_VREF_GRD;
553 return 0;
554}
555
Matt Porter403d1942005-11-29 15:00:51 +0100556static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
557
558{
559 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
560}
561
562static int stac92xx_io_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
563{
564 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
565 uinfo->count = 1;
566 uinfo->value.integer.min = 0;
567 uinfo->value.integer.max = 1;
568 return 0;
569}
570
571static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
572{
573 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
574 struct sigmatel_spec *spec = codec->spec;
575 int io_idx = kcontrol-> private_value & 0xff;
576
577 ucontrol->value.integer.value[0] = spec->io_switch[io_idx];
578 return 0;
579}
580
581static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
582{
583 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
584 struct sigmatel_spec *spec = codec->spec;
585 hda_nid_t nid = kcontrol->private_value >> 8;
586 int io_idx = kcontrol-> private_value & 0xff;
587 unsigned short val = ucontrol->value.integer.value[0];
588
589 spec->io_switch[io_idx] = val;
590
591 if (val)
592 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
Takashi Iwaic960a032006-03-23 17:06:28 +0100593 else {
594 unsigned int pinctl = AC_PINCTL_IN_EN;
595 if (io_idx) /* set VREF for mic */
596 pinctl |= stac92xx_get_vref(codec, nid);
597 stac92xx_auto_set_pinctl(codec, nid, pinctl);
598 }
Matt Porter403d1942005-11-29 15:00:51 +0100599 return 1;
600}
601
602#define STAC_CODEC_IO_SWITCH(xname, xpval) \
603 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
604 .name = xname, \
605 .index = 0, \
606 .info = stac92xx_io_switch_info, \
607 .get = stac92xx_io_switch_get, \
608 .put = stac92xx_io_switch_put, \
609 .private_value = xpval, \
610 }
611
612
Mattc7d4b2f2005-06-27 14:59:41 +0200613enum {
614 STAC_CTL_WIDGET_VOL,
615 STAC_CTL_WIDGET_MUTE,
Matt Porter403d1942005-11-29 15:00:51 +0100616 STAC_CTL_WIDGET_IO_SWITCH,
Mattc7d4b2f2005-06-27 14:59:41 +0200617};
618
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100619static struct snd_kcontrol_new stac92xx_control_templates[] = {
Mattc7d4b2f2005-06-27 14:59:41 +0200620 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
621 HDA_CODEC_MUTE(NULL, 0, 0, 0),
Matt Porter403d1942005-11-29 15:00:51 +0100622 STAC_CODEC_IO_SWITCH(NULL, 0),
Mattc7d4b2f2005-06-27 14:59:41 +0200623};
624
625/* add dynamic controls */
626static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
627{
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100628 struct snd_kcontrol_new *knew;
Mattc7d4b2f2005-06-27 14:59:41 +0200629
630 if (spec->num_kctl_used >= spec->num_kctl_alloc) {
631 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
632
633 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
634 if (! knew)
635 return -ENOMEM;
636 if (spec->kctl_alloc) {
637 memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
638 kfree(spec->kctl_alloc);
639 }
640 spec->kctl_alloc = knew;
641 spec->num_kctl_alloc = num;
642 }
643
644 knew = &spec->kctl_alloc[spec->num_kctl_used];
645 *knew = stac92xx_control_templates[type];
Takashi Iwai82fe0c52005-06-30 10:54:33 +0200646 knew->name = kstrdup(name, GFP_KERNEL);
Mattc7d4b2f2005-06-27 14:59:41 +0200647 if (! knew->name)
648 return -ENOMEM;
649 knew->private_value = val;
650 spec->num_kctl_used++;
651 return 0;
652}
653
Matt Porter403d1942005-11-29 15:00:51 +0100654/* flag inputs as additional dynamic lineouts */
655static int stac92xx_add_dyn_out_pins(struct hda_codec *codec, struct auto_pin_cfg *cfg)
656{
657 struct sigmatel_spec *spec = codec->spec;
658
659 switch (cfg->line_outs) {
660 case 3:
661 /* add line-in as side */
662 if (cfg->input_pins[AUTO_PIN_LINE]) {
663 cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_LINE];
664 spec->line_switch = 1;
665 cfg->line_outs++;
666 }
667 break;
668 case 2:
669 /* add line-in as clfe and mic as side */
670 if (cfg->input_pins[AUTO_PIN_LINE]) {
671 cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_LINE];
672 spec->line_switch = 1;
673 cfg->line_outs++;
674 }
675 if (cfg->input_pins[AUTO_PIN_MIC]) {
676 cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_MIC];
677 spec->mic_switch = 1;
678 cfg->line_outs++;
679 }
680 break;
681 case 1:
682 /* add line-in as surr and mic as clfe */
683 if (cfg->input_pins[AUTO_PIN_LINE]) {
684 cfg->line_out_pins[1] = cfg->input_pins[AUTO_PIN_LINE];
685 spec->line_switch = 1;
686 cfg->line_outs++;
687 }
688 if (cfg->input_pins[AUTO_PIN_MIC]) {
689 cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_MIC];
690 spec->mic_switch = 1;
691 cfg->line_outs++;
692 }
693 break;
694 }
695
696 return 0;
697}
698
Matt Porter3cc08dc2006-01-23 15:27:49 +0100699/*
700 * XXX The line_out pin widget connection list may not be set to the
701 * desired DAC nid. This is the case on 927x where ports A and B can
702 * be routed to several DACs.
703 *
704 * This requires an analysis of the line-out/hp pin configuration
705 * to provide a best fit for pin/DAC configurations that are routable.
706 * For now, 927x DAC4 is not supported and 927x DAC1 output to ports
707 * A and B is not supported.
708 */
Mattc7d4b2f2005-06-27 14:59:41 +0200709/* fill in the dac_nids table from the parsed pin configuration */
710static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
711{
712 struct sigmatel_spec *spec = codec->spec;
713 hda_nid_t nid;
714 int i;
715
716 /* check the pins hardwired to audio widget */
717 for (i = 0; i < cfg->line_outs; i++) {
718 nid = cfg->line_out_pins[i];
719 spec->multiout.dac_nids[i] = snd_hda_codec_read(codec, nid, 0,
720 AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
721 }
722
Takashi Iwai82bc9552006-03-21 11:24:42 +0100723 spec->multiout.num_dacs = cfg->line_outs;
Mattc7d4b2f2005-06-27 14:59:41 +0200724
725 return 0;
726}
727
728/* add playback controls from the parsed DAC table */
729static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec, const struct auto_pin_cfg *cfg)
730{
731 char name[32];
732 static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
733 hda_nid_t nid;
734 int i, err;
735
736 for (i = 0; i < cfg->line_outs; i++) {
Matt Porter403d1942005-11-29 15:00:51 +0100737 if (!spec->multiout.dac_nids[i])
Mattc7d4b2f2005-06-27 14:59:41 +0200738 continue;
739
740 nid = spec->multiout.dac_nids[i];
741
742 if (i == 2) {
743 /* Center/LFE */
744 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Center Playback Volume",
745 HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
746 return err;
747 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "LFE Playback Volume",
748 HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
749 return err;
750 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Center Playback Switch",
751 HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
752 return err;
753 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "LFE Playback Switch",
754 HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
755 return err;
756 } else {
757 sprintf(name, "%s Playback Volume", chname[i]);
758 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
759 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
760 return err;
761 sprintf(name, "%s Playback Switch", chname[i]);
762 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
763 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
764 return err;
765 }
766 }
767
Matt Porter403d1942005-11-29 15:00:51 +0100768 if (spec->line_switch)
769 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Line In as Output Switch", cfg->input_pins[AUTO_PIN_LINE] << 8)) < 0)
770 return err;
771
772 if (spec->mic_switch)
773 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Mic as Output Switch", (cfg->input_pins[AUTO_PIN_MIC] << 8) | 1)) < 0)
774 return err;
775
Mattc7d4b2f2005-06-27 14:59:41 +0200776 return 0;
777}
778
779/* add playback controls for HP output */
780static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec, struct auto_pin_cfg *cfg)
781{
782 struct sigmatel_spec *spec = codec->spec;
783 hda_nid_t pin = cfg->hp_pin;
784 hda_nid_t nid;
785 int i, err;
Matt4e550962005-07-04 17:51:39 +0200786 unsigned int wid_caps;
Mattc7d4b2f2005-06-27 14:59:41 +0200787
788 if (! pin)
789 return 0;
790
Takashi Iwai54d17402005-11-21 16:33:22 +0100791 wid_caps = get_wcaps(codec, pin);
Takashi Iwai505cb342006-03-27 12:51:52 +0200792 if (wid_caps & AC_WCAP_UNSOL_CAP)
Takashi Iwai82bc9552006-03-21 11:24:42 +0100793 spec->hp_detect = 1;
Matt4e550962005-07-04 17:51:39 +0200794
Mattc7d4b2f2005-06-27 14:59:41 +0200795 nid = snd_hda_codec_read(codec, pin, 0, AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
796 for (i = 0; i < cfg->line_outs; i++) {
797 if (! spec->multiout.dac_nids[i])
798 continue;
799 if (spec->multiout.dac_nids[i] == nid)
800 return 0;
801 }
802
803 spec->multiout.hp_nid = nid;
804
805 /* control HP volume/switch on the output mixer amp */
806 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Headphone Playback Volume",
807 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
808 return err;
809 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
810 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
811 return err;
812
813 return 0;
814}
815
816/* create playback/capture controls for input pins */
817static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
818{
819 struct sigmatel_spec *spec = codec->spec;
Mattc7d4b2f2005-06-27 14:59:41 +0200820 struct hda_input_mux *imux = &spec->private_imux;
821 hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
822 int i, j, k;
823
824 for (i = 0; i < AUTO_PIN_LAST; i++) {
825 int index = -1;
826 if (cfg->input_pins[i]) {
Takashi Iwai4a471b72005-12-07 13:56:29 +0100827 imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
Mattc7d4b2f2005-06-27 14:59:41 +0200828
829 for (j=0; j<spec->num_muxes; j++) {
830 int num_cons = snd_hda_get_connections(codec, spec->mux_nids[j], con_lst, HDA_MAX_NUM_INPUTS);
831 for (k=0; k<num_cons; k++)
832 if (con_lst[k] == cfg->input_pins[i]) {
833 index = k;
834 break;
835 }
836 if (index >= 0)
837 break;
838 }
839 imux->items[imux->num_items].index = index;
840 imux->num_items++;
841 }
842 }
843
844 return 0;
845}
846
Mattc7d4b2f2005-06-27 14:59:41 +0200847static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
848{
849 struct sigmatel_spec *spec = codec->spec;
850 int i;
851
852 for (i = 0; i < spec->autocfg.line_outs; i++) {
853 hda_nid_t nid = spec->autocfg.line_out_pins[i];
854 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
855 }
856}
857
858static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
859{
860 struct sigmatel_spec *spec = codec->spec;
861 hda_nid_t pin;
862
863 pin = spec->autocfg.hp_pin;
864 if (pin) /* connect to front */
865 stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
866}
867
Matt Porter3cc08dc2006-01-23 15:27:49 +0100868static int stac92xx_parse_auto_config(struct hda_codec *codec, hda_nid_t dig_out, hda_nid_t dig_in)
Mattc7d4b2f2005-06-27 14:59:41 +0200869{
870 struct sigmatel_spec *spec = codec->spec;
871 int err;
872
Kailang Yangdf694da2005-12-05 19:42:22 +0100873 if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
Mattc7d4b2f2005-06-27 14:59:41 +0200874 return err;
Takashi Iwai82bc9552006-03-21 11:24:42 +0100875 if (! spec->autocfg.line_outs)
Matt Porter869264c2006-01-25 19:20:50 +0100876 return 0; /* can't find valid pin config */
Matt Porter403d1942005-11-29 15:00:51 +0100877 if ((err = stac92xx_add_dyn_out_pins(codec, &spec->autocfg)) < 0)
878 return err;
Mattc7d4b2f2005-06-27 14:59:41 +0200879 if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
880 return err;
Mattc7d4b2f2005-06-27 14:59:41 +0200881
882 if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
883 (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
884 (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
885 return err;
886
887 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
Matt Porter403d1942005-11-29 15:00:51 +0100888 if (spec->multiout.max_channels > 2)
Mattc7d4b2f2005-06-27 14:59:41 +0200889 spec->surr_switch = 1;
Mattc7d4b2f2005-06-27 14:59:41 +0200890
Takashi Iwai82bc9552006-03-21 11:24:42 +0100891 if (spec->autocfg.dig_out_pin)
Matt Porter3cc08dc2006-01-23 15:27:49 +0100892 spec->multiout.dig_out_nid = dig_out;
Takashi Iwai82bc9552006-03-21 11:24:42 +0100893 if (spec->autocfg.dig_in_pin)
Matt Porter3cc08dc2006-01-23 15:27:49 +0100894 spec->dig_in_nid = dig_in;
Mattc7d4b2f2005-06-27 14:59:41 +0200895
896 if (spec->kctl_alloc)
897 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
898
899 spec->input_mux = &spec->private_imux;
900
901 return 1;
902}
903
Takashi Iwai82bc9552006-03-21 11:24:42 +0100904/* add playback controls for HP output */
905static int stac9200_auto_create_hp_ctls(struct hda_codec *codec,
906 struct auto_pin_cfg *cfg)
907{
908 struct sigmatel_spec *spec = codec->spec;
909 hda_nid_t pin = cfg->hp_pin;
910 unsigned int wid_caps;
911
912 if (! pin)
913 return 0;
914
915 wid_caps = get_wcaps(codec, pin);
Takashi Iwai505cb342006-03-27 12:51:52 +0200916 if (wid_caps & AC_WCAP_UNSOL_CAP)
Takashi Iwai82bc9552006-03-21 11:24:42 +0100917 spec->hp_detect = 1;
Takashi Iwai82bc9552006-03-21 11:24:42 +0100918
919 return 0;
920}
921
Mattc7d4b2f2005-06-27 14:59:41 +0200922static int stac9200_parse_auto_config(struct hda_codec *codec)
923{
924 struct sigmatel_spec *spec = codec->spec;
925 int err;
926
Kailang Yangdf694da2005-12-05 19:42:22 +0100927 if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
Mattc7d4b2f2005-06-27 14:59:41 +0200928 return err;
929
930 if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
931 return err;
932
Takashi Iwai82bc9552006-03-21 11:24:42 +0100933 if ((err = stac9200_auto_create_hp_ctls(codec, &spec->autocfg)) < 0)
934 return err;
935
936 if (spec->autocfg.dig_out_pin)
Mattc7d4b2f2005-06-27 14:59:41 +0200937 spec->multiout.dig_out_nid = 0x05;
Takashi Iwai82bc9552006-03-21 11:24:42 +0100938 if (spec->autocfg.dig_in_pin)
Mattc7d4b2f2005-06-27 14:59:41 +0200939 spec->dig_in_nid = 0x04;
Mattc7d4b2f2005-06-27 14:59:41 +0200940
941 if (spec->kctl_alloc)
942 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
943
944 spec->input_mux = &spec->private_imux;
945
946 return 1;
947}
948
Mattc7d4b2f2005-06-27 14:59:41 +0200949static int stac92xx_init(struct hda_codec *codec)
950{
951 struct sigmatel_spec *spec = codec->spec;
Takashi Iwai82bc9552006-03-21 11:24:42 +0100952 struct auto_pin_cfg *cfg = &spec->autocfg;
953 int i;
Mattc7d4b2f2005-06-27 14:59:41 +0200954
Mattc7d4b2f2005-06-27 14:59:41 +0200955 snd_hda_sequence_write(codec, spec->init);
956
Takashi Iwai82bc9552006-03-21 11:24:42 +0100957 /* set up pins */
958 if (spec->hp_detect) {
Takashi Iwai505cb342006-03-27 12:51:52 +0200959 /* Enable unsolicited responses on the HP widget */
960 snd_hda_codec_write(codec, cfg->hp_pin, 0,
961 AC_VERB_SET_UNSOLICITED_ENABLE,
962 STAC_UNSOL_ENABLE);
Takashi Iwai82bc9552006-03-21 11:24:42 +0100963 /* fake event to set up pins */
964 codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
965 } else {
966 stac92xx_auto_init_multi_out(codec);
967 stac92xx_auto_init_hp_out(codec);
968 }
969 for (i = 0; i < AUTO_PIN_LAST; i++) {
Takashi Iwaic960a032006-03-23 17:06:28 +0100970 hda_nid_t nid = cfg->input_pins[i];
971 if (nid) {
972 unsigned int pinctl = AC_PINCTL_IN_EN;
973 if (i == AUTO_PIN_MIC || i == AUTO_PIN_FRONT_MIC)
974 pinctl |= stac92xx_get_vref(codec, nid);
975 stac92xx_auto_set_pinctl(codec, nid, pinctl);
976 }
Takashi Iwai82bc9552006-03-21 11:24:42 +0100977 }
978 if (cfg->dig_out_pin)
979 stac92xx_auto_set_pinctl(codec, cfg->dig_out_pin,
980 AC_PINCTL_OUT_EN);
981 if (cfg->dig_in_pin)
982 stac92xx_auto_set_pinctl(codec, cfg->dig_in_pin,
983 AC_PINCTL_IN_EN);
984
Mattc7d4b2f2005-06-27 14:59:41 +0200985 return 0;
986}
987
Matt2f2f4252005-04-13 14:45:30 +0200988static void stac92xx_free(struct hda_codec *codec)
989{
Mattc7d4b2f2005-06-27 14:59:41 +0200990 struct sigmatel_spec *spec = codec->spec;
991 int i;
992
993 if (! spec)
994 return;
995
996 if (spec->kctl_alloc) {
997 for (i = 0; i < spec->num_kctl_used; i++)
998 kfree(spec->kctl_alloc[i].name);
999 kfree(spec->kctl_alloc);
1000 }
1001
1002 kfree(spec);
Matt2f2f4252005-04-13 14:45:30 +02001003}
1004
Matt4e550962005-07-04 17:51:39 +02001005static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
1006 unsigned int flag)
1007{
1008 unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1009 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1010 snd_hda_codec_write(codec, nid, 0,
1011 AC_VERB_SET_PIN_WIDGET_CONTROL,
1012 pin_ctl | flag);
1013}
1014
1015static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
1016 unsigned int flag)
1017{
1018 unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1019 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1020 snd_hda_codec_write(codec, nid, 0,
1021 AC_VERB_SET_PIN_WIDGET_CONTROL,
1022 pin_ctl & ~flag);
1023}
1024
1025static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
1026{
1027 struct sigmatel_spec *spec = codec->spec;
1028 struct auto_pin_cfg *cfg = &spec->autocfg;
1029 int i, presence;
1030
1031 if ((res >> 26) != STAC_HP_EVENT)
1032 return;
1033
1034 presence = snd_hda_codec_read(codec, cfg->hp_pin, 0,
1035 AC_VERB_GET_PIN_SENSE, 0x00) >> 31;
1036
1037 if (presence) {
1038 /* disable lineouts, enable hp */
1039 for (i = 0; i < cfg->line_outs; i++)
1040 stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
1041 AC_PINCTL_OUT_EN);
1042 stac92xx_set_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
1043 } else {
1044 /* enable lineouts, disable hp */
1045 for (i = 0; i < cfg->line_outs; i++)
1046 stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
1047 AC_PINCTL_OUT_EN);
1048 stac92xx_reset_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
1049 }
1050}
1051
Mattff6fdc32005-06-27 15:06:52 +02001052#ifdef CONFIG_PM
1053static int stac92xx_resume(struct hda_codec *codec)
1054{
1055 struct sigmatel_spec *spec = codec->spec;
1056 int i;
1057
1058 stac92xx_init(codec);
1059 for (i = 0; i < spec->num_mixers; i++)
1060 snd_hda_resume_ctls(codec, spec->mixers[i]);
1061 if (spec->multiout.dig_out_nid)
1062 snd_hda_resume_spdif_out(codec);
1063 if (spec->dig_in_nid)
1064 snd_hda_resume_spdif_in(codec);
1065
1066 return 0;
1067}
1068#endif
1069
Matt2f2f4252005-04-13 14:45:30 +02001070static struct hda_codec_ops stac92xx_patch_ops = {
1071 .build_controls = stac92xx_build_controls,
1072 .build_pcms = stac92xx_build_pcms,
1073 .init = stac92xx_init,
1074 .free = stac92xx_free,
Matt4e550962005-07-04 17:51:39 +02001075 .unsol_event = stac92xx_unsol_event,
Mattff6fdc32005-06-27 15:06:52 +02001076#ifdef CONFIG_PM
1077 .resume = stac92xx_resume,
1078#endif
Matt2f2f4252005-04-13 14:45:30 +02001079};
1080
1081static int patch_stac9200(struct hda_codec *codec)
1082{
1083 struct sigmatel_spec *spec;
Mattc7d4b2f2005-06-27 14:59:41 +02001084 int err;
Matt2f2f4252005-04-13 14:45:30 +02001085
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001086 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Matt2f2f4252005-04-13 14:45:30 +02001087 if (spec == NULL)
1088 return -ENOMEM;
1089
1090 codec->spec = spec;
Matt Porter403d1942005-11-29 15:00:51 +01001091 spec->board_config = snd_hda_check_board_config(codec, stac9200_cfg_tbl);
1092 if (spec->board_config < 0)
1093 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
1094 else {
1095 spec->num_pins = 8;
1096 spec->pin_nids = stac9200_pin_nids;
1097 spec->pin_configs = stac9200_brd_tbl[spec->board_config];
1098 stac92xx_set_config_regs(codec);
1099 }
Matt2f2f4252005-04-13 14:45:30 +02001100
1101 spec->multiout.max_channels = 2;
1102 spec->multiout.num_dacs = 1;
1103 spec->multiout.dac_nids = stac9200_dac_nids;
1104 spec->adc_nids = stac9200_adc_nids;
1105 spec->mux_nids = stac9200_mux_nids;
Mattdabbed62005-06-14 10:19:34 +02001106 spec->num_muxes = 1;
Mattc7d4b2f2005-06-27 14:59:41 +02001107
1108 spec->init = stac9200_core_init;
Matt2f2f4252005-04-13 14:45:30 +02001109 spec->mixer = stac9200_mixer;
Mattc7d4b2f2005-06-27 14:59:41 +02001110
1111 err = stac9200_parse_auto_config(codec);
1112 if (err < 0) {
1113 stac92xx_free(codec);
1114 return err;
1115 }
Matt2f2f4252005-04-13 14:45:30 +02001116
1117 codec->patch_ops = stac92xx_patch_ops;
1118
1119 return 0;
1120}
1121
1122static int patch_stac922x(struct hda_codec *codec)
1123{
1124 struct sigmatel_spec *spec;
Mattc7d4b2f2005-06-27 14:59:41 +02001125 int err;
Matt2f2f4252005-04-13 14:45:30 +02001126
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001127 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Matt2f2f4252005-04-13 14:45:30 +02001128 if (spec == NULL)
1129 return -ENOMEM;
1130
1131 codec->spec = spec;
Matt Porter403d1942005-11-29 15:00:51 +01001132 spec->board_config = snd_hda_check_board_config(codec, stac922x_cfg_tbl);
1133 if (spec->board_config < 0)
1134 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, using BIOS defaults\n");
1135 else {
1136 spec->num_pins = 10;
1137 spec->pin_nids = stac922x_pin_nids;
1138 spec->pin_configs = stac922x_brd_tbl[spec->board_config];
1139 stac92xx_set_config_regs(codec);
1140 }
Matt2f2f4252005-04-13 14:45:30 +02001141
Matt2f2f4252005-04-13 14:45:30 +02001142 spec->adc_nids = stac922x_adc_nids;
1143 spec->mux_nids = stac922x_mux_nids;
Mattdabbed62005-06-14 10:19:34 +02001144 spec->num_muxes = 2;
Mattc7d4b2f2005-06-27 14:59:41 +02001145
1146 spec->init = stac922x_core_init;
Matt2f2f4252005-04-13 14:45:30 +02001147 spec->mixer = stac922x_mixer;
Mattc7d4b2f2005-06-27 14:59:41 +02001148
1149 spec->multiout.dac_nids = spec->dac_nids;
1150
Matt Porter3cc08dc2006-01-23 15:27:49 +01001151 err = stac92xx_parse_auto_config(codec, 0x08, 0x09);
1152 if (err < 0) {
1153 stac92xx_free(codec);
1154 return err;
1155 }
1156
1157 codec->patch_ops = stac92xx_patch_ops;
1158
1159 return 0;
1160}
1161
1162static int patch_stac927x(struct hda_codec *codec)
1163{
1164 struct sigmatel_spec *spec;
1165 int err;
1166
1167 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1168 if (spec == NULL)
1169 return -ENOMEM;
1170
1171 codec->spec = spec;
1172 spec->board_config = snd_hda_check_board_config(codec, stac927x_cfg_tbl);
1173 if (spec->board_config < 0)
1174 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC927x, using BIOS defaults\n");
1175 else {
1176 spec->num_pins = 14;
1177 spec->pin_nids = stac927x_pin_nids;
1178 spec->pin_configs = stac927x_brd_tbl[spec->board_config];
1179 stac92xx_set_config_regs(codec);
1180 }
1181
1182 spec->adc_nids = stac927x_adc_nids;
1183 spec->mux_nids = stac927x_mux_nids;
1184 spec->num_muxes = 3;
1185
1186 spec->init = stac927x_core_init;
1187 spec->mixer = stac927x_mixer;
1188
1189 spec->multiout.dac_nids = spec->dac_nids;
1190
1191 err = stac92xx_parse_auto_config(codec, 0x1e, 0x20);
Mattc7d4b2f2005-06-27 14:59:41 +02001192 if (err < 0) {
1193 stac92xx_free(codec);
1194 return err;
1195 }
Matt2f2f4252005-04-13 14:45:30 +02001196
1197 codec->patch_ops = stac92xx_patch_ops;
1198
1199 return 0;
1200}
1201
1202/*
Takashi Iwaidb064e52006-03-16 16:04:58 +01001203 * STAC 7661(?) hack
1204 */
1205
1206/* static config for Sony VAIO FE550G */
1207static hda_nid_t vaio_dacs[] = { 0x2 };
1208#define VAIO_HP_DAC 0x5
1209static hda_nid_t vaio_adcs[] = { 0x8 /*,0x6*/ };
1210static hda_nid_t vaio_mux_nids[] = { 0x15 };
1211
1212static struct hda_input_mux vaio_mux = {
1213 .num_items = 2,
1214 .items = {
1215 /* { "HP", 0x0 },
1216 { "Unknown", 0x1 }, */
1217 { "Mic", 0x2 },
1218 { "PCM", 0x3 },
1219 }
1220};
1221
1222static struct hda_verb vaio_init[] = {
1223 {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
1224 {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
1225 {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
1226 {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
1227 {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
1228 {0x15, AC_VERB_SET_CONNECT_SEL, 0x2}, /* mic-sel: 0a,0d,14,02 */
1229 {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
1230 {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
1231 {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
1232 {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
1233 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
1234 {}
1235};
1236
1237/* bind volumes of both NID 0x02 and 0x05 */
1238static int vaio_master_vol_put(struct snd_kcontrol *kcontrol,
1239 struct snd_ctl_elem_value *ucontrol)
1240{
1241 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1242 long *valp = ucontrol->value.integer.value;
1243 int change;
1244
1245 change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
1246 0x7f, valp[0] & 0x7f);
1247 change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
1248 0x7f, valp[1] & 0x7f);
1249 snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
1250 0x7f, valp[0] & 0x7f);
1251 snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
1252 0x7f, valp[1] & 0x7f);
1253 return change;
1254}
1255
1256/* bind volumes of both NID 0x02 and 0x05 */
1257static int vaio_master_sw_put(struct snd_kcontrol *kcontrol,
1258 struct snd_ctl_elem_value *ucontrol)
1259{
1260 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1261 long *valp = ucontrol->value.integer.value;
1262 int change;
1263
1264 change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
1265 0x80, valp[0] & 0x80);
1266 change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
1267 0x80, valp[1] & 0x80);
1268 snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
1269 0x80, valp[0] & 0x80);
1270 snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
1271 0x80, valp[1] & 0x80);
1272 return change;
1273}
1274
1275static struct snd_kcontrol_new vaio_mixer[] = {
1276 {
1277 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1278 .name = "Master Playback Volume",
1279 .info = snd_hda_mixer_amp_volume_info,
1280 .get = snd_hda_mixer_amp_volume_get,
1281 .put = vaio_master_vol_put,
1282 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
1283 },
1284 {
1285 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1286 .name = "Master Playback Switch",
1287 .info = snd_hda_mixer_amp_switch_info,
1288 .get = snd_hda_mixer_amp_switch_get,
1289 .put = vaio_master_sw_put,
1290 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
1291 },
1292 /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
1293 HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
1294 HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
1295 {
1296 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1297 .name = "Capture Source",
1298 .count = 1,
1299 .info = stac92xx_mux_enum_info,
1300 .get = stac92xx_mux_enum_get,
1301 .put = stac92xx_mux_enum_put,
1302 },
1303 {}
1304};
1305
1306static struct hda_codec_ops stac7661_patch_ops = {
1307 .build_controls = stac92xx_build_controls,
1308 .build_pcms = stac92xx_build_pcms,
1309 .init = stac92xx_init,
1310 .free = stac92xx_free,
1311#ifdef CONFIG_PM
1312 .resume = stac92xx_resume,
1313#endif
1314};
1315
1316enum { STAC7661_VAIO };
1317
1318static struct hda_board_config stac7661_cfg_tbl[] = {
1319 { .modelname = "vaio", .config = STAC7661_VAIO },
1320 { .pci_subvendor = 0x104d, .pci_subdevice = 0x81e6,
1321 .config = STAC7661_VAIO },
1322 { .pci_subvendor = 0x104d, .pci_subdevice = 0x81ef,
1323 .config = STAC7661_VAIO },
1324 {}
1325};
1326
1327static int patch_stac7661(struct hda_codec *codec)
1328{
1329 struct sigmatel_spec *spec;
1330 int board_config;
1331
1332 board_config = snd_hda_check_board_config(codec, stac7661_cfg_tbl);
1333 if (board_config < 0)
1334 /* unknown config, let generic-parser do its job... */
1335 return snd_hda_parse_generic_codec(codec);
1336
1337 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1338 if (spec == NULL)
1339 return -ENOMEM;
1340
1341 codec->spec = spec;
1342 switch (board_config) {
1343 case STAC7661_VAIO:
1344 spec->mixer = vaio_mixer;
1345 spec->init = vaio_init;
1346 spec->multiout.max_channels = 2;
1347 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
1348 spec->multiout.dac_nids = vaio_dacs;
1349 spec->multiout.hp_nid = VAIO_HP_DAC;
1350 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
1351 spec->adc_nids = vaio_adcs;
1352 spec->input_mux = &vaio_mux;
1353 spec->mux_nids = vaio_mux_nids;
1354 break;
1355 }
1356
1357 codec->patch_ops = stac7661_patch_ops;
1358 return 0;
1359}
1360
1361
1362/*
Matt2f2f4252005-04-13 14:45:30 +02001363 * patch entries
1364 */
1365struct hda_codec_preset snd_hda_preset_sigmatel[] = {
1366 { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
1367 { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
1368 { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
1369 { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
1370 { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
1371 { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
1372 { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
Matt Porter3cc08dc2006-01-23 15:27:49 +01001373 { .id = 0x83847620, .name = "STAC9274", .patch = patch_stac927x },
1374 { .id = 0x83847621, .name = "STAC9274D", .patch = patch_stac927x },
1375 { .id = 0x83847622, .name = "STAC9273X", .patch = patch_stac927x },
1376 { .id = 0x83847623, .name = "STAC9273D", .patch = patch_stac927x },
1377 { .id = 0x83847624, .name = "STAC9272X", .patch = patch_stac927x },
1378 { .id = 0x83847625, .name = "STAC9272D", .patch = patch_stac927x },
1379 { .id = 0x83847626, .name = "STAC9271X", .patch = patch_stac927x },
1380 { .id = 0x83847627, .name = "STAC9271D", .patch = patch_stac927x },
1381 { .id = 0x83847628, .name = "STAC9274X5NH", .patch = patch_stac927x },
1382 { .id = 0x83847629, .name = "STAC9274D5NH", .patch = patch_stac927x },
Takashi Iwaidb064e52006-03-16 16:04:58 +01001383 { .id = 0x83847661, .name = "STAC7661", .patch = patch_stac7661 },
Matt2f2f4252005-04-13 14:45:30 +02001384 {} /* terminator */
1385};