blob: 5a45a912aedcc22ef543a57229fcfa26880c51b7 [file] [log] [blame]
Joseph Chanc577b8a2006-11-29 15:29:40 +01001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
Lydia Wang8e865972009-10-10 19:08:52 +08004 * HD audio interface patch for VIA VT17xx/VT18xx/VT20xx codec
Joseph Chanc577b8a2006-11-29 15:29:40 +01005 *
Lydia Wang8e865972009-10-10 19:08:52 +08006 * (C) 2006-2009 VIA Technology, Inc.
7 * (C) 2006-2008 Takashi Iwai <tiwai@suse.de>
Joseph Chanc577b8a2006-11-29 15:29:40 +01008 *
9 * This driver is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This driver is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24/* * * * * * * * * * * * * * Release History * * * * * * * * * * * * * * * * */
Lydia Wang377ff312009-10-10 19:08:55 +080025/* */
Joseph Chanc577b8a2006-11-29 15:29:40 +010026/* 2006-03-03 Lydia Wang Create the basic patch to support VT1708 codec */
Lydia Wang377ff312009-10-10 19:08:55 +080027/* 2006-03-14 Lydia Wang Modify hard code for some pin widget nid */
28/* 2006-08-02 Lydia Wang Add support to VT1709 codec */
Joseph Chanc577b8a2006-11-29 15:29:40 +010029/* 2006-09-08 Lydia Wang Fix internal loopback recording source select bug */
Lydia Wang377ff312009-10-10 19:08:55 +080030/* 2007-09-12 Lydia Wang Add EAPD enable during driver initialization */
31/* 2007-09-17 Lydia Wang Add VT1708B codec support */
Harald Welte76d9b0d2008-09-09 15:50:37 +080032/* 2007-11-14 Lydia Wang Add VT1708A codec HP and CD pin connect config */
Harald Weltefb4cb772008-09-09 15:53:36 +080033/* 2008-02-03 Lydia Wang Fix Rear channels and Back channels inverse issue */
Lydia Wang377ff312009-10-10 19:08:55 +080034/* 2008-03-06 Lydia Wang Add VT1702 codec and VT1708S codec support */
35/* 2008-04-09 Lydia Wang Add mute front speaker when HP plugin */
36/* 2008-04-09 Lydia Wang Add Independent HP feature */
Harald Welte98aa34c2008-09-09 16:02:09 +080037/* 2008-05-28 Lydia Wang Add second S/PDIF Out support for VT1702 */
Lydia Wang377ff312009-10-10 19:08:55 +080038/* 2008-09-15 Logan Li Add VT1708S Mic Boost workaround/backdoor */
Lydia Wang8e865972009-10-10 19:08:52 +080039/* 2009-02-16 Logan Li Add support for VT1718S */
40/* 2009-03-13 Logan Li Add support for VT1716S */
41/* 2009-04-14 Lydai Wang Add support for VT1828S and VT2020 */
42/* 2009-07-08 Lydia Wang Add support for VT2002P */
43/* 2009-07-21 Lydia Wang Add support for VT1812 */
Lydia Wang36dd5c42009-10-20 13:18:04 +080044/* 2009-09-19 Lydia Wang Add support for VT1818S */
Lydia Wang377ff312009-10-10 19:08:55 +080045/* */
Joseph Chanc577b8a2006-11-29 15:29:40 +010046/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47
48
Joseph Chanc577b8a2006-11-29 15:29:40 +010049#include <linux/init.h>
50#include <linux/delay.h>
51#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040052#include <linux/module.h>
Joseph Chanc577b8a2006-11-29 15:29:40 +010053#include <sound/core.h>
Harald Welte0aa62ae2008-09-09 15:58:27 +080054#include <sound/asoundef.h>
Joseph Chanc577b8a2006-11-29 15:29:40 +010055#include "hda_codec.h"
56#include "hda_local.h"
Takashi Iwai128bc4b2012-05-07 17:42:31 +020057#include "hda_auto_parser.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020058#include "hda_jack.h"
Joseph Chanc577b8a2006-11-29 15:29:40 +010059
Joseph Chanc577b8a2006-11-29 15:29:40 +010060/* Pin Widget NID */
Harald Welted949cac2008-09-09 15:56:01 +080061#define VT1708_HP_PIN_NID 0x20
62#define VT1708_CD_PIN_NID 0x24
Joseph Chanc577b8a2006-11-29 15:29:40 +010063
Harald Welted7426322008-09-15 22:43:23 +080064enum VIA_HDA_CODEC {
65 UNKNOWN = -1,
66 VT1708,
67 VT1709_10CH,
68 VT1709_6CH,
69 VT1708B_8CH,
70 VT1708B_4CH,
71 VT1708S,
Lydia Wang518bf3b2009-10-10 19:07:29 +080072 VT1708BCE,
Harald Welted7426322008-09-15 22:43:23 +080073 VT1702,
Lydia Wangeb7188c2009-10-10 19:08:34 +080074 VT1718S,
Lydia Wangf3db4232009-10-10 19:08:41 +080075 VT1716S,
Lydia Wang25eaba22009-10-10 19:08:43 +080076 VT2002P,
Lydia Wangab6734e2009-10-10 19:08:46 +080077 VT1812,
Lydia Wang118909562011-03-23 17:57:34 +080078 VT1802,
Harald Welted7426322008-09-15 22:43:23 +080079 CODEC_TYPES,
80};
81
Lydia Wang118909562011-03-23 17:57:34 +080082#define VT2002P_COMPATIBLE(spec) \
83 ((spec)->codec_type == VT2002P ||\
84 (spec)->codec_type == VT1812 ||\
85 (spec)->codec_type == VT1802)
86
Takashi Iwai8e3679d2011-06-21 09:01:36 +020087#define MAX_NID_PATH_DEPTH 5
88
Takashi Iwai09a9ad692011-06-21 15:57:44 +020089/* output-path: DAC -> ... -> pin
90 * idx[] contains the source index number of the next widget;
91 * e.g. idx[0] is the index of the DAC selected by path[1] widget
92 * multi[] indicates whether it's a selector widget with multi-connectors
93 * (i.e. the connection selection is mandatory)
94 * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
95 */
Takashi Iwai4a796162011-06-17 17:53:38 +020096struct nid_path {
97 int depth;
Takashi Iwai8e3679d2011-06-21 09:01:36 +020098 hda_nid_t path[MAX_NID_PATH_DEPTH];
Takashi Iwai09a9ad692011-06-21 15:57:44 +020099 unsigned char idx[MAX_NID_PATH_DEPTH];
100 unsigned char multi[MAX_NID_PATH_DEPTH];
101 unsigned int vol_ctl;
102 unsigned int mute_ctl;
Takashi Iwai4a796162011-06-17 17:53:38 +0200103};
104
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200105/* input-path */
106struct via_input {
107 hda_nid_t pin; /* input-pin or aa-mix */
108 int adc_idx; /* ADC index to be used */
109 int mux_idx; /* MUX index (if any) */
110 const char *label; /* input-source label */
111};
112
Takashi Iwaide6c74f2011-07-04 14:46:42 +0200113#define VIA_MAX_ADCS 3
114
Takashi Iwai3b607e32011-07-18 16:54:40 +0200115enum {
116 STREAM_MULTI_OUT = (1 << 0),
117 STREAM_INDEP_HP = (1 << 1),
118};
119
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800120struct via_spec {
121 /* codec parameterization */
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200122 const struct snd_kcontrol_new *mixers[6];
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800123 unsigned int num_mixers;
124
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200125 const struct hda_verb *init_verbs[5];
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800126 unsigned int num_iverbs;
127
Takashi Iwai82673bc2011-06-17 16:24:21 +0200128 char stream_name_analog[32];
Takashi Iwai7eb56e82011-06-18 16:40:14 +0200129 char stream_name_hp[32];
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200130 const struct hda_pcm_stream *stream_analog_playback;
131 const struct hda_pcm_stream *stream_analog_capture;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800132
Takashi Iwai82673bc2011-06-17 16:24:21 +0200133 char stream_name_digital[32];
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200134 const struct hda_pcm_stream *stream_digital_playback;
135 const struct hda_pcm_stream *stream_digital_capture;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800136
137 /* playback */
138 struct hda_multi_out multiout;
139 hda_nid_t slave_dig_outs[2];
Takashi Iwaiece8d042011-06-19 16:24:21 +0200140 hda_nid_t hp_dac_nid;
Takashi Iwai3214b962011-07-18 12:49:25 +0200141 hda_nid_t speaker_dac_nid;
142 int hp_indep_shared; /* indep HP-DAC is shared with side ch */
Takashi Iwai3b607e32011-07-18 16:54:40 +0200143 int opened_streams; /* STREAM_* bits */
144 int active_streams; /* STREAM_* bits */
Takashi Iwai3214b962011-07-18 12:49:25 +0200145 int aamix_mode; /* loopback is enabled for output-path? */
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800146
Takashi Iwai3214b962011-07-18 12:49:25 +0200147 /* Output-paths:
148 * There are different output-paths depending on the setup.
149 * out_path, hp_path and speaker_path are primary paths. If both
150 * direct DAC and aa-loopback routes are available, these contain
151 * the former paths. Meanwhile *_mix_path contain the paths with
152 * loopback mixer. (Since the loopback is only for front channel,
153 * no out_mix_path for surround channels.)
154 * The HP output has another path, hp_indep_path, which is used in
155 * the independent-HP mode.
156 */
Takashi Iwaide6c74f2011-07-04 14:46:42 +0200157 struct nid_path out_path[HDA_SIDE + 1];
Takashi Iwai3214b962011-07-18 12:49:25 +0200158 struct nid_path out_mix_path;
Takashi Iwai4a796162011-06-17 17:53:38 +0200159 struct nid_path hp_path;
Takashi Iwai3214b962011-07-18 12:49:25 +0200160 struct nid_path hp_mix_path;
161 struct nid_path hp_indep_path;
Takashi Iwai4a918ff2011-06-20 12:39:26 +0200162 struct nid_path speaker_path;
Takashi Iwai3214b962011-07-18 12:49:25 +0200163 struct nid_path speaker_mix_path;
Takashi Iwai4a796162011-06-17 17:53:38 +0200164
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800165 /* capture */
166 unsigned int num_adc_nids;
Takashi Iwaide6c74f2011-07-04 14:46:42 +0200167 hda_nid_t adc_nids[VIA_MAX_ADCS];
168 hda_nid_t mux_nids[VIA_MAX_ADCS];
Takashi Iwai620e2b22011-06-17 17:19:19 +0200169 hda_nid_t aa_mix_nid;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800170 hda_nid_t dig_in_nid;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800171
172 /* capture source */
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200173 bool dyn_adc_switch;
174 int num_inputs;
175 struct via_input inputs[AUTO_CFG_MAX_INS + 1];
Takashi Iwaide6c74f2011-07-04 14:46:42 +0200176 unsigned int cur_mux[VIA_MAX_ADCS];
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800177
Takashi Iwai3b607e32011-07-18 16:54:40 +0200178 /* dynamic DAC switching */
179 unsigned int cur_dac_stream_tag;
180 unsigned int cur_dac_format;
181 unsigned int cur_hp_stream_tag;
182 unsigned int cur_hp_format;
183
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200184 /* dynamic ADC switching */
185 hda_nid_t cur_adc;
186 unsigned int cur_adc_stream_tag;
187 unsigned int cur_adc_format;
188
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800189 /* PCM information */
190 struct hda_pcm pcm_rec[3];
191
192 /* dynamic controls, init_verbs and input_mux */
193 struct auto_pin_cfg autocfg;
194 struct snd_array kctls;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800195 hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
196
197 /* HP mode source */
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800198 unsigned int hp_independent_mode;
Lydia Wangf3db4232009-10-10 19:08:41 +0800199 unsigned int dmic_enabled;
Takashi Iwai24088a52011-06-17 16:59:21 +0200200 unsigned int no_pin_power_ctl;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800201 enum VIA_HDA_CODEC codec_type;
202
Takashi Iwaie9d010c2012-02-01 10:33:23 +0100203 /* analog low-power control */
204 bool alc_mode;
205
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200206 /* smart51 setup */
207 unsigned int smart51_nums;
208 hda_nid_t smart51_pins[2];
209 int smart51_idxs[2];
210 const char *smart51_labels[2];
211 unsigned int smart51_enabled;
212
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800213 /* work to check hp jack state */
214 struct hda_codec *codec;
215 struct delayed_work vt1708_hp_work;
Takashi Iwai187d3332011-11-24 16:33:09 +0100216 int hp_work_active;
Takashi Iwaie06e5a22011-06-17 15:46:13 +0200217 int vt1708_jack_detect;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800218 int vt1708_hp_present;
Lydia Wang3e95b9a2011-03-23 15:13:28 +0800219
220 void (*set_widgets_power_state)(struct hda_codec *codec);
221
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800222 struct hda_loopback_check loopback;
Takashi Iwai13af8e72011-06-20 14:05:46 +0200223 int num_loopbacks;
224 struct hda_amp_list loopback_list[8];
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200225
226 /* bind capture-volume */
227 struct hda_bind_ctls *bind_cap_vol;
228 struct hda_bind_ctls *bind_cap_sw;
Takashi Iwai3b607e32011-07-18 16:54:40 +0200229
230 struct mutex config_mutex;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800231};
232
Lydia Wang0341ccd2011-03-22 16:25:03 +0800233static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100234static struct via_spec * via_new_spec(struct hda_codec *codec)
235{
236 struct via_spec *spec;
237
238 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
239 if (spec == NULL)
240 return NULL;
241
Takashi Iwai3b607e32011-07-18 16:54:40 +0200242 mutex_init(&spec->config_mutex);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100243 codec->spec = spec;
244 spec->codec = codec;
Lydia Wang0341ccd2011-03-22 16:25:03 +0800245 spec->codec_type = get_codec_type(codec);
246 /* VT1708BCE & VT1708S are almost same */
247 if (spec->codec_type == VT1708BCE)
248 spec->codec_type = VT1708S;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100249 return spec;
250}
251
Lydia Wang744ff5f2009-10-10 19:07:26 +0800252static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
Harald Welted7426322008-09-15 22:43:23 +0800253{
Lydia Wang744ff5f2009-10-10 19:07:26 +0800254 u32 vendor_id = codec->vendor_id;
Harald Welted7426322008-09-15 22:43:23 +0800255 u16 ven_id = vendor_id >> 16;
256 u16 dev_id = vendor_id & 0xffff;
257 enum VIA_HDA_CODEC codec_type;
258
259 /* get codec type */
260 if (ven_id != 0x1106)
261 codec_type = UNKNOWN;
262 else if (dev_id >= 0x1708 && dev_id <= 0x170b)
263 codec_type = VT1708;
264 else if (dev_id >= 0xe710 && dev_id <= 0xe713)
265 codec_type = VT1709_10CH;
266 else if (dev_id >= 0xe714 && dev_id <= 0xe717)
267 codec_type = VT1709_6CH;
Lydia Wang518bf3b2009-10-10 19:07:29 +0800268 else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
Harald Welted7426322008-09-15 22:43:23 +0800269 codec_type = VT1708B_8CH;
Lydia Wang518bf3b2009-10-10 19:07:29 +0800270 if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
271 codec_type = VT1708BCE;
272 } else if (dev_id >= 0xe724 && dev_id <= 0xe727)
Harald Welted7426322008-09-15 22:43:23 +0800273 codec_type = VT1708B_4CH;
274 else if ((dev_id & 0xfff) == 0x397
275 && (dev_id >> 12) < 8)
276 codec_type = VT1708S;
277 else if ((dev_id & 0xfff) == 0x398
278 && (dev_id >> 12) < 8)
279 codec_type = VT1702;
Lydia Wangeb7188c2009-10-10 19:08:34 +0800280 else if ((dev_id & 0xfff) == 0x428
281 && (dev_id >> 12) < 8)
282 codec_type = VT1718S;
Lydia Wangf3db4232009-10-10 19:08:41 +0800283 else if (dev_id == 0x0433 || dev_id == 0xa721)
284 codec_type = VT1716S;
Lydia Wangbb3c6bfc2009-10-10 19:08:39 +0800285 else if (dev_id == 0x0441 || dev_id == 0x4441)
286 codec_type = VT1718S;
Lydia Wang25eaba22009-10-10 19:08:43 +0800287 else if (dev_id == 0x0438 || dev_id == 0x4438)
288 codec_type = VT2002P;
Lydia Wangab6734e2009-10-10 19:08:46 +0800289 else if (dev_id == 0x0448)
290 codec_type = VT1812;
Lydia Wang36dd5c42009-10-20 13:18:04 +0800291 else if (dev_id == 0x0440)
292 codec_type = VT1708S;
Lydia Wang118909562011-03-23 17:57:34 +0800293 else if ((dev_id & 0xfff) == 0x446)
294 codec_type = VT1802;
Harald Welted7426322008-09-15 22:43:23 +0800295 else
296 codec_type = UNKNOWN;
297 return codec_type;
298};
299
Lydia Wangec7e7e42011-03-24 12:43:44 +0800300#define VIA_JACK_EVENT 0x20
Harald Welte69e52a82008-09-09 15:57:32 +0800301#define VIA_HP_EVENT 0x01
302#define VIA_GPIO_EVENT 0x02
Takashi Iwai4a918ff2011-06-20 12:39:26 +0200303#define VIA_LINE_EVENT 0x03
Harald Welte69e52a82008-09-09 15:57:32 +0800304
Joseph Chanc577b8a2006-11-29 15:29:40 +0100305enum {
306 VIA_CTL_WIDGET_VOL,
307 VIA_CTL_WIDGET_MUTE,
Lydia Wangf5271102009-10-10 19:07:35 +0800308 VIA_CTL_WIDGET_ANALOG_MUTE,
Joseph Chanc577b8a2006-11-29 15:29:40 +0100309};
310
Takashi Iwaiada509e2011-06-20 15:40:19 +0200311static void analog_low_current_mode(struct hda_codec *codec);
312static bool is_aa_path_mute(struct hda_codec *codec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800313
Takashi Iwai187d3332011-11-24 16:33:09 +0100314#define hp_detect_with_aa(codec) \
315 (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1 && \
316 !is_aa_path_mute(codec))
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800317
318static void vt1708_stop_hp_work(struct via_spec *spec)
319{
320 if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
321 return;
Takashi Iwai187d3332011-11-24 16:33:09 +0100322 if (spec->hp_work_active) {
323 snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 1);
324 cancel_delayed_work_sync(&spec->vt1708_hp_work);
325 spec->hp_work_active = 0;
326 }
327}
328
329static void vt1708_update_hp_work(struct via_spec *spec)
330{
331 if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800332 return;
Takashi Iwai187d3332011-11-24 16:33:09 +0100333 if (spec->vt1708_jack_detect &&
334 (spec->active_streams || hp_detect_with_aa(spec->codec))) {
335 if (!spec->hp_work_active) {
336 snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 0);
337 schedule_delayed_work(&spec->vt1708_hp_work,
338 msecs_to_jiffies(100));
339 spec->hp_work_active = 1;
340 }
341 } else if (!hp_detect_with_aa(spec->codec))
342 vt1708_stop_hp_work(spec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800343}
Lydia Wangf5271102009-10-10 19:07:35 +0800344
Lydia Wang3e95b9a2011-03-23 15:13:28 +0800345static void set_widgets_power_state(struct hda_codec *codec)
346{
347 struct via_spec *spec = codec->spec;
348 if (spec->set_widgets_power_state)
349 spec->set_widgets_power_state(codec);
350}
Lydia Wang25eaba22009-10-10 19:08:43 +0800351
Lydia Wangf5271102009-10-10 19:07:35 +0800352static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
353 struct snd_ctl_elem_value *ucontrol)
354{
355 int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
356 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
357
Lydia Wang3e95b9a2011-03-23 15:13:28 +0800358 set_widgets_power_state(codec);
Takashi Iwaiada509e2011-06-20 15:40:19 +0200359 analog_low_current_mode(snd_kcontrol_chip(kcontrol));
Takashi Iwai187d3332011-11-24 16:33:09 +0100360 vt1708_update_hp_work(codec->spec);
Lydia Wangf5271102009-10-10 19:07:35 +0800361 return change;
362}
363
364/* modify .put = snd_hda_mixer_amp_switch_put */
365#define ANALOG_INPUT_MUTE \
366 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
367 .name = NULL, \
368 .index = 0, \
369 .info = snd_hda_mixer_amp_switch_info, \
370 .get = snd_hda_mixer_amp_switch_get, \
371 .put = analog_input_switch_put, \
372 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
373
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200374static const struct snd_kcontrol_new via_control_templates[] = {
Joseph Chanc577b8a2006-11-29 15:29:40 +0100375 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
376 HDA_CODEC_MUTE(NULL, 0, 0, 0),
Lydia Wangf5271102009-10-10 19:07:35 +0800377 ANALOG_INPUT_MUTE,
Joseph Chanc577b8a2006-11-29 15:29:40 +0100378};
379
Lydia Wangab6734e2009-10-10 19:08:46 +0800380
Joseph Chanc577b8a2006-11-29 15:29:40 +0100381/* add dynamic controls */
Takashi Iwai291c9e32011-06-17 16:15:26 +0200382static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
383 const struct snd_kcontrol_new *tmpl,
384 const char *name)
Joseph Chanc577b8a2006-11-29 15:29:40 +0100385{
386 struct snd_kcontrol_new *knew;
387
Takashi Iwai603c4012008-07-30 15:01:44 +0200388 snd_array_init(&spec->kctls, sizeof(*knew), 32);
389 knew = snd_array_new(&spec->kctls);
390 if (!knew)
Takashi Iwai291c9e32011-06-17 16:15:26 +0200391 return NULL;
392 *knew = *tmpl;
393 if (!name)
394 name = tmpl->name;
395 if (name) {
396 knew->name = kstrdup(name, GFP_KERNEL);
397 if (!knew->name)
398 return NULL;
399 }
400 return knew;
401}
402
403static int __via_add_control(struct via_spec *spec, int type, const char *name,
404 int idx, unsigned long val)
405{
406 struct snd_kcontrol_new *knew;
407
408 knew = __via_clone_ctl(spec, &via_control_templates[type], name);
409 if (!knew)
Joseph Chanc577b8a2006-11-29 15:29:40 +0100410 return -ENOMEM;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +0200411 knew->index = idx;
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +0100412 if (get_amp_nid_(val))
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100413 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100414 knew->private_value = val;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100415 return 0;
416}
417
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200418#define via_add_control(spec, type, name, val) \
419 __via_add_control(spec, type, name, 0, val)
420
Takashi Iwai291c9e32011-06-17 16:15:26 +0200421#define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100422
Takashi Iwai603c4012008-07-30 15:01:44 +0200423static void via_free_kctls(struct hda_codec *codec)
424{
425 struct via_spec *spec = codec->spec;
426
427 if (spec->kctls.list) {
428 struct snd_kcontrol_new *kctl = spec->kctls.list;
429 int i;
430 for (i = 0; i < spec->kctls.used; i++)
431 kfree(kctl[i].name);
432 }
433 snd_array_free(&spec->kctls);
434}
435
Joseph Chanc577b8a2006-11-29 15:29:40 +0100436/* create input playback/capture controls for the given pin */
Lydia Wang9510e8d2009-10-10 19:07:39 +0800437static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200438 int type_idx, int idx, int mix_nid)
Joseph Chanc577b8a2006-11-29 15:29:40 +0100439{
440 char name[32];
441 int err;
442
443 sprintf(name, "%s Playback Volume", ctlname);
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200444 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
Joseph Chanc577b8a2006-11-29 15:29:40 +0100445 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
446 if (err < 0)
447 return err;
448 sprintf(name, "%s Playback Switch", ctlname);
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200449 err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
Joseph Chanc577b8a2006-11-29 15:29:40 +0100450 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
451 if (err < 0)
452 return err;
453 return 0;
454}
455
Takashi Iwai5d417622011-06-20 11:32:27 +0200456#define get_connection_index(codec, mux, nid) \
Takashi Iwai8d087c72011-06-28 12:45:47 +0200457 snd_hda_get_conn_index(codec, mux, nid, 0)
Takashi Iwai5d417622011-06-20 11:32:27 +0200458
Takashi Iwai8df2a312011-06-21 11:48:29 +0200459static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
460 unsigned int mask)
461{
Takashi Iwaia934d5a2011-06-21 14:22:14 +0200462 unsigned int caps;
463 if (!nid)
464 return false;
465 caps = get_wcaps(codec, nid);
Takashi Iwai8df2a312011-06-21 11:48:29 +0200466 if (dir == HDA_INPUT)
467 caps &= AC_WCAP_IN_AMP;
468 else
469 caps &= AC_WCAP_OUT_AMP;
470 if (!caps)
471 return false;
472 if (query_amp_caps(codec, nid, dir) & mask)
473 return true;
474 return false;
475}
476
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200477#define have_mute(codec, nid, dir) \
478 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
Takashi Iwai8df2a312011-06-21 11:48:29 +0200479
Lydia Wangd69607b2011-07-08 14:02:52 +0800480/* enable/disable the output-route mixers */
481static void activate_output_mix(struct hda_codec *codec, struct nid_path *path,
Takashi Iwai3214b962011-07-18 12:49:25 +0200482 hda_nid_t mix_nid, int idx, bool enable)
Lydia Wangd69607b2011-07-08 14:02:52 +0800483{
484 int i, num, val;
Lydia Wangd69607b2011-07-08 14:02:52 +0800485
486 if (!path)
487 return;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200488 num = snd_hda_get_num_conns(codec, mix_nid);
Lydia Wangd69607b2011-07-08 14:02:52 +0800489 for (i = 0; i < num; i++) {
Takashi Iwai3214b962011-07-18 12:49:25 +0200490 if (i == idx)
491 val = AMP_IN_UNMUTE(i);
492 else
493 val = AMP_IN_MUTE(i);
Lydia Wangd69607b2011-07-08 14:02:52 +0800494 snd_hda_codec_write(codec, mix_nid, 0,
495 AC_VERB_SET_AMP_GAIN_MUTE, val);
496 }
497}
498
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200499/* enable/disable the output-route */
500static void activate_output_path(struct hda_codec *codec, struct nid_path *path,
501 bool enable, bool force)
Takashi Iwai5d417622011-06-20 11:32:27 +0200502{
Lydia Wangd69607b2011-07-08 14:02:52 +0800503 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200504 int i;
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200505 for (i = 0; i < path->depth; i++) {
506 hda_nid_t src, dst;
507 int idx = path->idx[i];
508 src = path->path[i];
509 if (i < path->depth - 1)
510 dst = path->path[i + 1];
511 else
512 dst = 0;
513 if (enable && path->multi[i])
514 snd_hda_codec_write(codec, dst, 0,
515 AC_VERB_SET_CONNECT_SEL, idx);
Takashi Iwai3214b962011-07-18 12:49:25 +0200516 if (!force && (dst == spec->aa_mix_nid))
Lydia Wange5e14682011-07-01 10:55:07 +0800517 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +0200518 if (have_mute(codec, dst, HDA_INPUT))
519 activate_output_mix(codec, path, dst, idx, enable);
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200520 if (!force && (src == path->vol_ctl || src == path->mute_ctl))
521 continue;
522 if (have_mute(codec, src, HDA_OUTPUT)) {
523 int val = enable ? AMP_OUT_UNMUTE : AMP_OUT_MUTE;
524 snd_hda_codec_write(codec, src, 0,
525 AC_VERB_SET_AMP_GAIN_MUTE, val);
526 }
527 }
Takashi Iwai5d417622011-06-20 11:32:27 +0200528}
529
530/* set the given pin as output */
531static void init_output_pin(struct hda_codec *codec, hda_nid_t pin,
532 int pin_type)
533{
534 if (!pin)
535 return;
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200536 snd_hda_set_pin_ctl(codec, pin, pin_type);
Takashi Iwai5d417622011-06-20 11:32:27 +0200537 if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
538 snd_hda_codec_write(codec, pin, 0,
Takashi Iwaid3a11e62009-07-07 13:43:35 +0200539 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
Joseph Chanc577b8a2006-11-29 15:29:40 +0100540}
541
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200542static void via_auto_init_output(struct hda_codec *codec,
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200543 struct nid_path *path, int pin_type)
Takashi Iwai5d417622011-06-20 11:32:27 +0200544{
Takashi Iwai5d417622011-06-20 11:32:27 +0200545 unsigned int caps;
Lydia Wangd69607b2011-07-08 14:02:52 +0800546 hda_nid_t pin;
Takashi Iwai5d417622011-06-20 11:32:27 +0200547
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200548 if (!path->depth)
Takashi Iwai5d417622011-06-20 11:32:27 +0200549 return;
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200550 pin = path->path[path->depth - 1];
Takashi Iwai5d417622011-06-20 11:32:27 +0200551
552 init_output_pin(codec, pin, pin_type);
Takashi Iwai77e314f2012-02-22 12:34:08 +0100553 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
554 caps = query_amp_caps(codec, pin, HDA_OUTPUT);
555 else
556 caps = 0;
Takashi Iwai5d417622011-06-20 11:32:27 +0200557 if (caps & AC_AMPCAP_MUTE) {
558 unsigned int val;
559 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
560 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
561 AMP_OUT_MUTE | val);
562 }
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200563 activate_output_path(codec, path, true, true); /* force on */
Takashi Iwai5d417622011-06-20 11:32:27 +0200564}
565
Joseph Chanc577b8a2006-11-29 15:29:40 +0100566static void via_auto_init_multi_out(struct hda_codec *codec)
567{
568 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200569 struct nid_path *path;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100570 int i;
571
Takashi Iwai3214b962011-07-18 12:49:25 +0200572 for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++) {
573 path = &spec->out_path[i];
574 if (!i && spec->aamix_mode && spec->out_mix_path.depth)
575 path = &spec->out_mix_path;
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200576 via_auto_init_output(codec, path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200577 }
Joseph Chanc577b8a2006-11-29 15:29:40 +0100578}
579
Takashi Iwai020066d2011-07-21 13:45:56 +0200580/* deactivate the inactive headphone-paths */
581static void deactivate_hp_paths(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +0100582{
583 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200584 int shared = spec->hp_indep_shared;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100585
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200586 if (spec->hp_independent_mode) {
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200587 activate_output_path(codec, &spec->hp_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200588 activate_output_path(codec, &spec->hp_mix_path, false, false);
589 if (shared)
590 activate_output_path(codec, &spec->out_path[shared],
591 false, false);
Takashi Iwai020066d2011-07-21 13:45:56 +0200592 } else if (spec->aamix_mode || !spec->hp_path.depth) {
593 activate_output_path(codec, &spec->hp_indep_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200594 activate_output_path(codec, &spec->hp_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200595 } else {
Takashi Iwai020066d2011-07-21 13:45:56 +0200596 activate_output_path(codec, &spec->hp_indep_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200597 activate_output_path(codec, &spec->hp_mix_path, false, false);
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200598 }
Joseph Chanc577b8a2006-11-29 15:29:40 +0100599}
600
Takashi Iwai020066d2011-07-21 13:45:56 +0200601static void via_auto_init_hp_out(struct hda_codec *codec)
602{
603 struct via_spec *spec = codec->spec;
604
605 if (!spec->hp_path.depth) {
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200606 via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200607 return;
608 }
609 deactivate_hp_paths(codec);
610 if (spec->hp_independent_mode)
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200611 via_auto_init_output(codec, &spec->hp_indep_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200612 else if (spec->aamix_mode)
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200613 via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200614 else
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200615 via_auto_init_output(codec, &spec->hp_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200616}
617
Takashi Iwai4a918ff2011-06-20 12:39:26 +0200618static void via_auto_init_speaker_out(struct hda_codec *codec)
619{
620 struct via_spec *spec = codec->spec;
621
Takashi Iwai3214b962011-07-18 12:49:25 +0200622 if (!spec->autocfg.speaker_outs)
623 return;
624 if (!spec->speaker_path.depth) {
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200625 via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200626 return;
627 }
628 if (!spec->aamix_mode) {
629 activate_output_path(codec, &spec->speaker_mix_path,
630 false, false);
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200631 via_auto_init_output(codec, &spec->speaker_path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200632 } else {
633 activate_output_path(codec, &spec->speaker_path, false, false);
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200634 via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200635 }
Takashi Iwai4a918ff2011-06-20 12:39:26 +0200636}
637
Takashi Iwaif4a78282011-06-17 18:46:48 +0200638static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
Takashi Iwai6e969d92011-07-11 11:28:13 +0200639static void via_hp_automute(struct hda_codec *codec);
Clemens Ladisch32e01912010-07-12 16:28:50 +0200640
Joseph Chanc577b8a2006-11-29 15:29:40 +0100641static void via_auto_init_analog_input(struct hda_codec *codec)
642{
643 struct via_spec *spec = codec->spec;
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200644 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai096a8852011-06-20 12:09:02 +0200645 hda_nid_t conn[HDA_MAX_CONNECTIONS];
Clemens Ladisch32e01912010-07-12 16:28:50 +0200646 unsigned int ctl;
Takashi Iwai096a8852011-06-20 12:09:02 +0200647 int i, num_conns;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100648
Takashi Iwai096a8852011-06-20 12:09:02 +0200649 /* init ADCs */
650 for (i = 0; i < spec->num_adc_nids; i++) {
Takashi Iwai77e314f2012-02-22 12:34:08 +0100651 hda_nid_t nid = spec->adc_nids[i];
652 if (!(get_wcaps(codec, nid) & AC_WCAP_IN_AMP) ||
653 !(query_amp_caps(codec, nid, HDA_INPUT) & AC_AMPCAP_MUTE))
654 continue;
Takashi Iwai096a8852011-06-20 12:09:02 +0200655 snd_hda_codec_write(codec, spec->adc_nids[i], 0,
656 AC_VERB_SET_AMP_GAIN_MUTE,
657 AMP_IN_UNMUTE(0));
658 }
659
660 /* init pins */
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200661 for (i = 0; i < cfg->num_inputs; i++) {
662 hda_nid_t nid = cfg->inputs[i].pin;
Takashi Iwaif4a78282011-06-17 18:46:48 +0200663 if (spec->smart51_enabled && is_smart51_pins(codec, nid))
Clemens Ladisch32e01912010-07-12 16:28:50 +0200664 ctl = PIN_OUT;
Takashi Iwai47408602012-04-20 13:06:53 +0200665 else {
Clemens Ladisch32e01912010-07-12 16:28:50 +0200666 ctl = PIN_IN;
Takashi Iwai47408602012-04-20 13:06:53 +0200667 if (cfg->inputs[i].type == AUTO_PIN_MIC)
668 ctl |= snd_hda_get_default_vref(codec, nid);
669 }
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200670 snd_hda_set_pin_ctl(codec, nid, ctl);
Joseph Chanc577b8a2006-11-29 15:29:40 +0100671 }
Takashi Iwai096a8852011-06-20 12:09:02 +0200672
673 /* init input-src */
674 for (i = 0; i < spec->num_adc_nids; i++) {
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200675 int adc_idx = spec->inputs[spec->cur_mux[i]].adc_idx;
Takashi Iwaifc1156c2012-02-13 15:04:06 +0100676 /* secondary ADCs must have the unique MUX */
677 if (i > 0 && !spec->mux_nids[i])
678 break;
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200679 if (spec->mux_nids[adc_idx]) {
680 int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
681 snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
682 AC_VERB_SET_CONNECT_SEL,
683 mux_idx);
684 }
685 if (spec->dyn_adc_switch)
686 break; /* only one input-src */
Takashi Iwai096a8852011-06-20 12:09:02 +0200687 }
688
689 /* init aa-mixer */
690 if (!spec->aa_mix_nid)
691 return;
692 num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
693 ARRAY_SIZE(conn));
694 for (i = 0; i < num_conns; i++) {
695 unsigned int caps = get_wcaps(codec, conn[i]);
696 if (get_wcaps_type(caps) == AC_WID_PIN)
697 snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
698 AC_VERB_SET_AMP_GAIN_MUTE,
699 AMP_IN_MUTE(i));
700 }
Joseph Chanc577b8a2006-11-29 15:29:40 +0100701}
Lydia Wangf5271102009-10-10 19:07:35 +0800702
Takashi Iwai054d8672012-01-24 12:25:50 +0100703static void update_power_state(struct hda_codec *codec, hda_nid_t nid,
704 unsigned int parm)
705{
706 if (snd_hda_codec_read(codec, nid, 0,
707 AC_VERB_GET_POWER_STATE, 0) == parm)
708 return;
709 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
710}
711
Lydia Wangf5271102009-10-10 19:07:35 +0800712static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
713 unsigned int *affected_parm)
714{
715 unsigned parm;
716 unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
717 unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
718 >> AC_DEFCFG_MISC_SHIFT
719 & AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
Lydia Wang1564b282009-10-10 19:07:52 +0800720 struct via_spec *spec = codec->spec;
Takashi Iwai24088a52011-06-17 16:59:21 +0200721 unsigned present = 0;
722
723 no_presence |= spec->no_pin_power_ctl;
724 if (!no_presence)
725 present = snd_hda_jack_detect(codec, nid);
Takashi Iwaif4a78282011-06-17 18:46:48 +0200726 if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
Lydia Wang1564b282009-10-10 19:07:52 +0800727 || ((no_presence || present)
728 && get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
Lydia Wangf5271102009-10-10 19:07:35 +0800729 *affected_parm = AC_PWRST_D0; /* if it's connected */
730 parm = AC_PWRST_D0;
731 } else
732 parm = AC_PWRST_D3;
733
Takashi Iwai054d8672012-01-24 12:25:50 +0100734 update_power_state(codec, nid, parm);
Lydia Wangf5271102009-10-10 19:07:35 +0800735}
736
Takashi Iwai24088a52011-06-17 16:59:21 +0200737static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
738 struct snd_ctl_elem_info *uinfo)
739{
740 static const char * const texts[] = {
741 "Disabled", "Enabled"
742 };
743
744 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
745 uinfo->count = 1;
746 uinfo->value.enumerated.items = 2;
747 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
748 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
749 strcpy(uinfo->value.enumerated.name,
750 texts[uinfo->value.enumerated.item]);
751 return 0;
752}
753
754static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
755 struct snd_ctl_elem_value *ucontrol)
756{
757 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
758 struct via_spec *spec = codec->spec;
759 ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
760 return 0;
761}
762
763static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
764 struct snd_ctl_elem_value *ucontrol)
765{
766 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
767 struct via_spec *spec = codec->spec;
768 unsigned int val = !ucontrol->value.enumerated.item[0];
769
770 if (val == spec->no_pin_power_ctl)
771 return 0;
772 spec->no_pin_power_ctl = val;
773 set_widgets_power_state(codec);
Takashi Iwaie9d010c2012-02-01 10:33:23 +0100774 analog_low_current_mode(codec);
Takashi Iwai24088a52011-06-17 16:59:21 +0200775 return 1;
776}
777
778static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
779 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
780 .name = "Dynamic Power-Control",
781 .info = via_pin_power_ctl_info,
782 .get = via_pin_power_ctl_get,
783 .put = via_pin_power_ctl_put,
784};
785
786
Harald Welte0aa62ae2008-09-09 15:58:27 +0800787static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
788 struct snd_ctl_elem_info *uinfo)
789{
Takashi Iwai8df2a312011-06-21 11:48:29 +0200790 static const char * const texts[] = { "OFF", "ON" };
791
792 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
793 uinfo->count = 1;
794 uinfo->value.enumerated.items = 2;
795 if (uinfo->value.enumerated.item >= 2)
796 uinfo->value.enumerated.item = 1;
797 strcpy(uinfo->value.enumerated.name,
798 texts[uinfo->value.enumerated.item]);
799 return 0;
Harald Welte0aa62ae2008-09-09 15:58:27 +0800800}
801
802static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
803 struct snd_ctl_elem_value *ucontrol)
804{
805 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Lydia Wangcdc17842009-10-10 19:07:47 +0800806 struct via_spec *spec = codec->spec;
Lydia Wangcdc17842009-10-10 19:07:47 +0800807
Takashi Iwaiece8d042011-06-19 16:24:21 +0200808 ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
Lydia Wangcdc17842009-10-10 19:07:47 +0800809 return 0;
810}
811
Takashi Iwai3b607e32011-07-18 16:54:40 +0200812/* adjust spec->multiout setup according to the current flags */
813static void setup_playback_multi_pcm(struct via_spec *spec)
814{
815 const struct auto_pin_cfg *cfg = &spec->autocfg;
816 spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
817 spec->multiout.hp_nid = 0;
818 if (!spec->hp_independent_mode) {
819 if (!spec->hp_indep_shared)
820 spec->multiout.hp_nid = spec->hp_dac_nid;
821 } else {
822 if (spec->hp_indep_shared)
823 spec->multiout.num_dacs = cfg->line_outs - 1;
824 }
825}
826
827/* update DAC setups according to indep-HP switch;
828 * this function is called only when indep-HP is modified
829 */
830static void switch_indep_hp_dacs(struct hda_codec *codec)
831{
832 struct via_spec *spec = codec->spec;
833 int shared = spec->hp_indep_shared;
834 hda_nid_t shared_dac, hp_dac;
835
836 if (!spec->opened_streams)
837 return;
838
839 shared_dac = shared ? spec->multiout.dac_nids[shared] : 0;
840 hp_dac = spec->hp_dac_nid;
841 if (spec->hp_independent_mode) {
842 /* switch to indep-HP mode */
843 if (spec->active_streams & STREAM_MULTI_OUT) {
844 __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
845 __snd_hda_codec_cleanup_stream(codec, shared_dac, 1);
846 }
847 if (spec->active_streams & STREAM_INDEP_HP)
848 snd_hda_codec_setup_stream(codec, hp_dac,
849 spec->cur_hp_stream_tag, 0,
850 spec->cur_hp_format);
851 } else {
852 /* back to HP or shared-DAC */
853 if (spec->active_streams & STREAM_INDEP_HP)
854 __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
855 if (spec->active_streams & STREAM_MULTI_OUT) {
856 hda_nid_t dac;
857 int ch;
858 if (shared_dac) { /* reset mutli-ch DAC */
859 dac = shared_dac;
860 ch = shared * 2;
861 } else { /* reset HP DAC */
862 dac = hp_dac;
863 ch = 0;
864 }
865 snd_hda_codec_setup_stream(codec, dac,
866 spec->cur_dac_stream_tag, ch,
867 spec->cur_dac_format);
868 }
869 }
870 setup_playback_multi_pcm(spec);
871}
872
Harald Welte0aa62ae2008-09-09 15:58:27 +0800873static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
874 struct snd_ctl_elem_value *ucontrol)
875{
876 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
877 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200878 int cur, shared;
Takashi Iwai8df2a312011-06-21 11:48:29 +0200879
Takashi Iwai3b607e32011-07-18 16:54:40 +0200880 mutex_lock(&spec->config_mutex);
Takashi Iwai25250502011-06-30 17:24:47 +0200881 cur = !!ucontrol->value.enumerated.item[0];
Takashi Iwai3b607e32011-07-18 16:54:40 +0200882 if (spec->hp_independent_mode == cur) {
883 mutex_unlock(&spec->config_mutex);
Takashi Iwai25250502011-06-30 17:24:47 +0200884 return 0;
Takashi Iwai3b607e32011-07-18 16:54:40 +0200885 }
Takashi Iwai25250502011-06-30 17:24:47 +0200886 spec->hp_independent_mode = cur;
Takashi Iwai3214b962011-07-18 12:49:25 +0200887 shared = spec->hp_indep_shared;
Takashi Iwai020066d2011-07-21 13:45:56 +0200888 deactivate_hp_paths(codec);
889 if (cur)
890 activate_output_path(codec, &spec->hp_indep_path, true, false);
891 else {
Takashi Iwai3214b962011-07-18 12:49:25 +0200892 if (shared)
893 activate_output_path(codec, &spec->out_path[shared],
Takashi Iwai25250502011-06-30 17:24:47 +0200894 true, false);
Takashi Iwai020066d2011-07-21 13:45:56 +0200895 if (spec->aamix_mode || !spec->hp_path.depth)
896 activate_output_path(codec, &spec->hp_mix_path,
897 true, false);
898 else
899 activate_output_path(codec, &spec->hp_path,
900 true, false);
Takashi Iwai8df2a312011-06-21 11:48:29 +0200901 }
Harald Welte0aa62ae2008-09-09 15:58:27 +0800902
Takashi Iwai3b607e32011-07-18 16:54:40 +0200903 switch_indep_hp_dacs(codec);
904 mutex_unlock(&spec->config_mutex);
905
Lydia Wangce0e5a92011-03-22 16:22:37 +0800906 /* update jack power state */
Lydia Wang3e95b9a2011-03-23 15:13:28 +0800907 set_widgets_power_state(codec);
Takashi Iwai6e969d92011-07-11 11:28:13 +0200908 via_hp_automute(codec);
Takashi Iwai25250502011-06-30 17:24:47 +0200909 return 1;
Harald Welte0aa62ae2008-09-09 15:58:27 +0800910}
911
Takashi Iwaiece8d042011-06-19 16:24:21 +0200912static const struct snd_kcontrol_new via_hp_mixer = {
913 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
914 .name = "Independent HP",
915 .info = via_independent_hp_info,
916 .get = via_independent_hp_get,
917 .put = via_independent_hp_put,
Harald Welte0aa62ae2008-09-09 15:58:27 +0800918};
919
Takashi Iwai3d83e572010-04-14 14:36:23 +0200920static int via_hp_build(struct hda_codec *codec)
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100921{
Takashi Iwai3d83e572010-04-14 14:36:23 +0200922 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100923 struct snd_kcontrol_new *knew;
924 hda_nid_t nid;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100925
Takashi Iwaiece8d042011-06-19 16:24:21 +0200926 nid = spec->autocfg.hp_pins[0];
927 knew = via_clone_control(spec, &via_hp_mixer);
Takashi Iwai3d83e572010-04-14 14:36:23 +0200928 if (knew == NULL)
929 return -ENOMEM;
930
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100931 knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100932
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100933 return 0;
934}
935
Lydia Wang1564b282009-10-10 19:07:52 +0800936static void notify_aa_path_ctls(struct hda_codec *codec)
937{
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200938 struct via_spec *spec = codec->spec;
Lydia Wang1564b282009-10-10 19:07:52 +0800939 int i;
Lydia Wang1564b282009-10-10 19:07:52 +0800940
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200941 for (i = 0; i < spec->smart51_nums; i++) {
942 struct snd_kcontrol *ctl;
943 struct snd_ctl_elem_id id;
944 memset(&id, 0, sizeof(id));
945 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
946 sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
Lydia Wang525566c2011-04-28 16:03:39 +0800947 ctl = snd_hda_find_mixer_ctl(codec, id.name);
948 if (ctl)
949 snd_ctl_notify(codec->bus->card,
950 SNDRV_CTL_EVENT_MASK_VALUE,
951 &ctl->id);
Lydia Wang1564b282009-10-10 19:07:52 +0800952 }
953}
954
955static void mute_aa_path(struct hda_codec *codec, int mute)
956{
957 struct via_spec *spec = codec->spec;
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200958 int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
Lydia Wang1564b282009-10-10 19:07:52 +0800959 int i;
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200960
Lydia Wang1564b282009-10-10 19:07:52 +0800961 /* check AA path's mute status */
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200962 for (i = 0; i < spec->smart51_nums; i++) {
963 if (spec->smart51_idxs[i] < 0)
964 continue;
965 snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
966 HDA_INPUT, spec->smart51_idxs[i],
Lydia Wang1564b282009-10-10 19:07:52 +0800967 HDA_AMP_MUTE, val);
968 }
969}
Takashi Iwaif4a78282011-06-17 18:46:48 +0200970
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200971static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
972{
973 struct via_spec *spec = codec->spec;
974 int i;
975
976 for (i = 0; i < spec->smart51_nums; i++)
977 if (spec->smart51_pins[i] == pin)
978 return true;
979 return false;
980}
981
Lydia Wang1564b282009-10-10 19:07:52 +0800982static int via_smart51_get(struct snd_kcontrol *kcontrol,
983 struct snd_ctl_elem_value *ucontrol)
984{
985 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
986 struct via_spec *spec = codec->spec;
Lydia Wang1564b282009-10-10 19:07:52 +0800987
Takashi Iwaif2b1c9f2011-06-21 16:52:39 +0200988 *ucontrol->value.integer.value = spec->smart51_enabled;
Lydia Wang1564b282009-10-10 19:07:52 +0800989 return 0;
990}
991
992static int via_smart51_put(struct snd_kcontrol *kcontrol,
993 struct snd_ctl_elem_value *ucontrol)
994{
995 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
996 struct via_spec *spec = codec->spec;
997 int out_in = *ucontrol->value.integer.value
998 ? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
Lydia Wang1564b282009-10-10 19:07:52 +0800999 int i;
1000
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001001 for (i = 0; i < spec->smart51_nums; i++) {
1002 hda_nid_t nid = spec->smart51_pins[i];
Takashi Iwai7b315bb2010-08-30 13:06:30 +02001003 unsigned int parm;
1004
Takashi Iwai7b315bb2010-08-30 13:06:30 +02001005 parm = snd_hda_codec_read(codec, nid, 0,
1006 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1007 parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
1008 parm |= out_in;
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001009 snd_hda_set_pin_ctl(codec, nid, parm);
Takashi Iwai7b315bb2010-08-30 13:06:30 +02001010 if (out_in == AC_PINCTL_OUT_EN) {
1011 mute_aa_path(codec, 1);
1012 notify_aa_path_ctls(codec);
1013 }
Lydia Wang1564b282009-10-10 19:07:52 +08001014 }
1015 spec->smart51_enabled = *ucontrol->value.integer.value;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08001016 set_widgets_power_state(codec);
Lydia Wang1564b282009-10-10 19:07:52 +08001017 return 1;
1018}
1019
Takashi Iwai5f4b36d2011-06-17 14:55:02 +02001020static const struct snd_kcontrol_new via_smart51_mixer = {
1021 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1022 .name = "Smart 5.1",
1023 .count = 1,
Takashi Iwaif2b1c9f2011-06-21 16:52:39 +02001024 .info = snd_ctl_boolean_mono_info,
Takashi Iwai5f4b36d2011-06-17 14:55:02 +02001025 .get = via_smart51_get,
1026 .put = via_smart51_put,
Lydia Wang1564b282009-10-10 19:07:52 +08001027};
1028
Takashi Iwaif4a78282011-06-17 18:46:48 +02001029static int via_smart51_build(struct hda_codec *codec)
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001030{
Takashi Iwaif4a78282011-06-17 18:46:48 +02001031 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001032
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001033 if (!spec->smart51_nums)
Lydia Wangcb34c202011-04-27 17:44:16 +08001034 return 0;
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001035 if (!via_clone_control(spec, &via_smart51_mixer))
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001036 return -ENOMEM;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001037 return 0;
1038}
1039
Takashi Iwaiada509e2011-06-20 15:40:19 +02001040/* check AA path's mute status */
1041static bool is_aa_path_mute(struct hda_codec *codec)
Lydia Wangf5271102009-10-10 19:07:35 +08001042{
Lydia Wangf5271102009-10-10 19:07:35 +08001043 struct via_spec *spec = codec->spec;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001044 const struct hda_amp_list *p;
1045 int i, ch, v;
1046
1047 for (i = 0; i < spec->num_loopbacks; i++) {
1048 p = &spec->loopback_list[i];
1049 for (ch = 0; ch < 2; ch++) {
1050 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
1051 p->idx);
1052 if (!(v & HDA_AMP_MUTE) && v > 0)
1053 return false;
Lydia Wangf5271102009-10-10 19:07:35 +08001054 }
1055 }
Takashi Iwaiada509e2011-06-20 15:40:19 +02001056 return true;
Lydia Wangf5271102009-10-10 19:07:35 +08001057}
1058
1059/* enter/exit analog low-current mode */
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001060static void __analog_low_current_mode(struct hda_codec *codec, bool force)
Lydia Wangf5271102009-10-10 19:07:35 +08001061{
1062 struct via_spec *spec = codec->spec;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001063 bool enable;
1064 unsigned int verb, parm;
Lydia Wangf5271102009-10-10 19:07:35 +08001065
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001066 if (spec->no_pin_power_ctl)
1067 enable = false;
1068 else
1069 enable = is_aa_path_mute(codec) && !spec->opened_streams;
1070 if (enable == spec->alc_mode && !force)
1071 return;
1072 spec->alc_mode = enable;
Lydia Wangf5271102009-10-10 19:07:35 +08001073
1074 /* decide low current mode's verb & parameter */
1075 switch (spec->codec_type) {
1076 case VT1708B_8CH:
1077 case VT1708B_4CH:
1078 verb = 0xf70;
1079 parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1080 break;
1081 case VT1708S:
Lydia Wangeb7188c2009-10-10 19:08:34 +08001082 case VT1718S:
Lydia Wangf3db4232009-10-10 19:08:41 +08001083 case VT1716S:
Lydia Wangf5271102009-10-10 19:07:35 +08001084 verb = 0xf73;
1085 parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1086 break;
1087 case VT1702:
1088 verb = 0xf73;
1089 parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1090 break;
Lydia Wang25eaba22009-10-10 19:08:43 +08001091 case VT2002P:
Lydia Wangab6734e2009-10-10 19:08:46 +08001092 case VT1812:
Lydia Wang118909562011-03-23 17:57:34 +08001093 case VT1802:
Lydia Wang25eaba22009-10-10 19:08:43 +08001094 verb = 0xf93;
1095 parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
1096 break;
Lydia Wangf5271102009-10-10 19:07:35 +08001097 default:
1098 return; /* other codecs are not supported */
1099 }
1100 /* send verb */
1101 snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1102}
1103
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001104static void analog_low_current_mode(struct hda_codec *codec)
1105{
1106 return __analog_low_current_mode(codec, false);
1107}
1108
Joseph Chanc577b8a2006-11-29 15:29:40 +01001109/*
1110 * generic initialization of ADC, input mixers and output mixers
1111 */
Takashi Iwai096a8852011-06-20 12:09:02 +02001112static const struct hda_verb vt1708_init_verbs[] = {
Lydia Wangaa266fc2011-03-24 12:41:01 +08001113 /* power down jack detect function */
1114 {0x1, 0xf81, 0x1},
Josepch Chanf7278fd2007-12-13 16:40:40 +01001115 { }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001116};
1117
Takashi Iwai3b607e32011-07-18 16:54:40 +02001118static void set_stream_open(struct hda_codec *codec, int bit, bool active)
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001119{
Takashi Iwaiada509e2011-06-20 15:40:19 +02001120 struct via_spec *spec = codec->spec;
1121
1122 if (active)
Takashi Iwai3b607e32011-07-18 16:54:40 +02001123 spec->opened_streams |= bit;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001124 else
Takashi Iwai3b607e32011-07-18 16:54:40 +02001125 spec->opened_streams &= ~bit;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001126 analog_low_current_mode(codec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001127}
1128
Takashi Iwaiece8d042011-06-19 16:24:21 +02001129static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001130 struct hda_codec *codec,
1131 struct snd_pcm_substream *substream)
1132{
1133 struct via_spec *spec = codec->spec;
Takashi Iwai25250502011-06-30 17:24:47 +02001134 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001135 int err;
Takashi Iwaiece8d042011-06-19 16:24:21 +02001136
Takashi Iwai25250502011-06-30 17:24:47 +02001137 spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
Takashi Iwai25250502011-06-30 17:24:47 +02001138 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001139 set_stream_open(codec, STREAM_MULTI_OUT, true);
Takashi Iwaiada509e2011-06-20 15:40:19 +02001140 err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
1141 hinfo);
1142 if (err < 0) {
Takashi Iwai3b607e32011-07-18 16:54:40 +02001143 set_stream_open(codec, STREAM_MULTI_OUT, false);
Takashi Iwaiada509e2011-06-20 15:40:19 +02001144 return err;
1145 }
1146 return 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001147}
1148
Takashi Iwaiece8d042011-06-19 16:24:21 +02001149static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
Takashi Iwai9af74212011-06-18 16:17:45 +02001150 struct hda_codec *codec,
1151 struct snd_pcm_substream *substream)
1152{
Takashi Iwai3b607e32011-07-18 16:54:40 +02001153 set_stream_open(codec, STREAM_MULTI_OUT, false);
Takashi Iwai9af74212011-06-18 16:17:45 +02001154 return 0;
1155}
1156
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001157static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
1158 struct hda_codec *codec,
1159 struct snd_pcm_substream *substream)
1160{
1161 struct via_spec *spec = codec->spec;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001162
Takashi Iwaiece8d042011-06-19 16:24:21 +02001163 if (snd_BUG_ON(!spec->hp_dac_nid))
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001164 return -EINVAL;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001165 set_stream_open(codec, STREAM_INDEP_HP, true);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001166 return 0;
1167}
1168
1169static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1170 struct hda_codec *codec,
1171 struct snd_pcm_substream *substream)
1172{
Takashi Iwai3b607e32011-07-18 16:54:40 +02001173 set_stream_open(codec, STREAM_INDEP_HP, false);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001174 return 0;
1175}
1176
1177static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
1178 struct hda_codec *codec,
1179 unsigned int stream_tag,
1180 unsigned int format,
1181 struct snd_pcm_substream *substream)
Harald Welte0aa62ae2008-09-09 15:58:27 +08001182{
1183 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001184
Takashi Iwai3b607e32011-07-18 16:54:40 +02001185 mutex_lock(&spec->config_mutex);
1186 setup_playback_multi_pcm(spec);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001187 snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1188 format, substream);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001189 /* remember for dynamic DAC switch with indep-HP */
1190 spec->active_streams |= STREAM_MULTI_OUT;
1191 spec->cur_dac_stream_tag = stream_tag;
1192 spec->cur_dac_format = format;
1193 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001194 vt1708_update_hp_work(spec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001195 return 0;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001196}
1197
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001198static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
1199 struct hda_codec *codec,
1200 unsigned int stream_tag,
1201 unsigned int format,
1202 struct snd_pcm_substream *substream)
Harald Welte0aa62ae2008-09-09 15:58:27 +08001203{
1204 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001205
Takashi Iwai3b607e32011-07-18 16:54:40 +02001206 mutex_lock(&spec->config_mutex);
1207 if (spec->hp_independent_mode)
1208 snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1209 stream_tag, 0, format);
1210 spec->active_streams |= STREAM_INDEP_HP;
1211 spec->cur_hp_stream_tag = stream_tag;
1212 spec->cur_hp_format = format;
1213 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001214 vt1708_update_hp_work(spec);
Harald Welte0aa62ae2008-09-09 15:58:27 +08001215 return 0;
1216}
1217
1218static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
1219 struct hda_codec *codec,
1220 struct snd_pcm_substream *substream)
1221{
1222 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001223
Takashi Iwai3b607e32011-07-18 16:54:40 +02001224 mutex_lock(&spec->config_mutex);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001225 snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001226 spec->active_streams &= ~STREAM_MULTI_OUT;
1227 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001228 vt1708_update_hp_work(spec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001229 return 0;
1230}
1231
1232static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
1233 struct hda_codec *codec,
1234 struct snd_pcm_substream *substream)
1235{
1236 struct via_spec *spec = codec->spec;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001237
Takashi Iwai3b607e32011-07-18 16:54:40 +02001238 mutex_lock(&spec->config_mutex);
1239 if (spec->hp_independent_mode)
1240 snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
1241 spec->active_streams &= ~STREAM_INDEP_HP;
1242 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001243 vt1708_update_hp_work(spec);
Harald Welte0aa62ae2008-09-09 15:58:27 +08001244 return 0;
1245}
1246
Joseph Chanc577b8a2006-11-29 15:29:40 +01001247/*
1248 * Digital out
1249 */
1250static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1251 struct hda_codec *codec,
1252 struct snd_pcm_substream *substream)
1253{
1254 struct via_spec *spec = codec->spec;
1255 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1256}
1257
1258static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1259 struct hda_codec *codec,
1260 struct snd_pcm_substream *substream)
1261{
1262 struct via_spec *spec = codec->spec;
1263 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1264}
1265
Harald Welte5691ec72008-09-15 22:42:26 +08001266static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Harald Welte98aa34c2008-09-09 16:02:09 +08001267 struct hda_codec *codec,
1268 unsigned int stream_tag,
1269 unsigned int format,
1270 struct snd_pcm_substream *substream)
1271{
1272 struct via_spec *spec = codec->spec;
Takashi Iwai9da29272009-05-07 16:31:14 +02001273 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1274 stream_tag, format, substream);
1275}
Harald Welte5691ec72008-09-15 22:42:26 +08001276
Takashi Iwai9da29272009-05-07 16:31:14 +02001277static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1278 struct hda_codec *codec,
1279 struct snd_pcm_substream *substream)
1280{
1281 struct via_spec *spec = codec->spec;
1282 snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
Harald Welte98aa34c2008-09-09 16:02:09 +08001283 return 0;
1284}
1285
Joseph Chanc577b8a2006-11-29 15:29:40 +01001286/*
1287 * Analog capture
1288 */
1289static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1290 struct hda_codec *codec,
1291 unsigned int stream_tag,
1292 unsigned int format,
1293 struct snd_pcm_substream *substream)
1294{
1295 struct via_spec *spec = codec->spec;
1296
1297 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1298 stream_tag, 0, format);
1299 return 0;
1300}
1301
1302static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1303 struct hda_codec *codec,
1304 struct snd_pcm_substream *substream)
1305{
1306 struct via_spec *spec = codec->spec;
Takashi Iwai888afa12008-03-18 09:57:50 +01001307 snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001308 return 0;
1309}
1310
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001311/* analog capture with dynamic ADC switching */
1312static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1313 struct hda_codec *codec,
1314 unsigned int stream_tag,
1315 unsigned int format,
1316 struct snd_pcm_substream *substream)
1317{
1318 struct via_spec *spec = codec->spec;
1319 int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
1320
Takashi Iwai3b607e32011-07-18 16:54:40 +02001321 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001322 spec->cur_adc = spec->adc_nids[adc_idx];
1323 spec->cur_adc_stream_tag = stream_tag;
1324 spec->cur_adc_format = format;
1325 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001326 mutex_unlock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001327 return 0;
1328}
1329
1330static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1331 struct hda_codec *codec,
1332 struct snd_pcm_substream *substream)
1333{
1334 struct via_spec *spec = codec->spec;
1335
Takashi Iwai3b607e32011-07-18 16:54:40 +02001336 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001337 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1338 spec->cur_adc = 0;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001339 mutex_unlock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001340 return 0;
1341}
1342
1343/* re-setup the stream if running; called from input-src put */
1344static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
1345{
1346 struct via_spec *spec = codec->spec;
1347 int adc_idx = spec->inputs[cur].adc_idx;
1348 hda_nid_t adc = spec->adc_nids[adc_idx];
Takashi Iwai3b607e32011-07-18 16:54:40 +02001349 bool ret = false;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001350
Takashi Iwai3b607e32011-07-18 16:54:40 +02001351 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001352 if (spec->cur_adc && spec->cur_adc != adc) {
1353 /* stream is running, let's swap the current ADC */
1354 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1355 spec->cur_adc = adc;
1356 snd_hda_codec_setup_stream(codec, adc,
1357 spec->cur_adc_stream_tag, 0,
1358 spec->cur_adc_format);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001359 ret = true;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001360 }
Takashi Iwai3b607e32011-07-18 16:54:40 +02001361 mutex_unlock(&spec->config_mutex);
1362 return ret;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001363}
1364
Takashi Iwai9af74212011-06-18 16:17:45 +02001365static const struct hda_pcm_stream via_pcm_analog_playback = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001366 .substreams = 1,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001367 .channels_min = 2,
1368 .channels_max = 8,
Takashi Iwai9af74212011-06-18 16:17:45 +02001369 /* NID is set in via_build_pcms */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001370 .ops = {
Takashi Iwaiece8d042011-06-19 16:24:21 +02001371 .open = via_playback_multi_pcm_open,
1372 .close = via_playback_multi_pcm_close,
Harald Welte0aa62ae2008-09-09 15:58:27 +08001373 .prepare = via_playback_multi_pcm_prepare,
1374 .cleanup = via_playback_multi_pcm_cleanup
Joseph Chanc577b8a2006-11-29 15:29:40 +01001375 },
1376};
1377
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001378static const struct hda_pcm_stream via_pcm_hp_playback = {
1379 .substreams = 1,
1380 .channels_min = 2,
1381 .channels_max = 2,
1382 /* NID is set in via_build_pcms */
1383 .ops = {
1384 .open = via_playback_hp_pcm_open,
Takashi Iwaiece8d042011-06-19 16:24:21 +02001385 .close = via_playback_hp_pcm_close,
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001386 .prepare = via_playback_hp_pcm_prepare,
1387 .cleanup = via_playback_hp_pcm_cleanup
1388 },
1389};
1390
Takashi Iwai90dd48a2011-05-02 12:38:19 +02001391static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001392 .substreams = 1,
Takashi Iwaibc9b5622008-05-23 17:50:27 +02001393 .channels_min = 2,
1394 .channels_max = 8,
Takashi Iwai9af74212011-06-18 16:17:45 +02001395 /* NID is set in via_build_pcms */
Takashi Iwaibc9b5622008-05-23 17:50:27 +02001396 /* We got noisy outputs on the right channel on VT1708 when
1397 * 24bit samples are used. Until any workaround is found,
1398 * disable the 24bit format, so far.
1399 */
1400 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1401 .ops = {
Takashi Iwaiece8d042011-06-19 16:24:21 +02001402 .open = via_playback_multi_pcm_open,
1403 .close = via_playback_multi_pcm_close,
Lydia Wangc873cc22009-10-10 19:08:21 +08001404 .prepare = via_playback_multi_pcm_prepare,
1405 .cleanup = via_playback_multi_pcm_cleanup
Takashi Iwaibc9b5622008-05-23 17:50:27 +02001406 },
1407};
1408
Takashi Iwai9af74212011-06-18 16:17:45 +02001409static const struct hda_pcm_stream via_pcm_analog_capture = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001410 .substreams = 1, /* will be changed in via_build_pcms() */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001411 .channels_min = 2,
1412 .channels_max = 2,
Takashi Iwai9af74212011-06-18 16:17:45 +02001413 /* NID is set in via_build_pcms */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001414 .ops = {
1415 .prepare = via_capture_pcm_prepare,
1416 .cleanup = via_capture_pcm_cleanup
1417 },
1418};
1419
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001420static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
1421 .substreams = 1,
1422 .channels_min = 2,
1423 .channels_max = 2,
1424 /* NID is set in via_build_pcms */
1425 .ops = {
1426 .prepare = via_dyn_adc_capture_pcm_prepare,
1427 .cleanup = via_dyn_adc_capture_pcm_cleanup,
1428 },
1429};
1430
Takashi Iwai9af74212011-06-18 16:17:45 +02001431static const struct hda_pcm_stream via_pcm_digital_playback = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001432 .substreams = 1,
1433 .channels_min = 2,
1434 .channels_max = 2,
1435 /* NID is set in via_build_pcms */
1436 .ops = {
1437 .open = via_dig_playback_pcm_open,
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001438 .close = via_dig_playback_pcm_close,
Takashi Iwai9da29272009-05-07 16:31:14 +02001439 .prepare = via_dig_playback_pcm_prepare,
1440 .cleanup = via_dig_playback_pcm_cleanup
Joseph Chanc577b8a2006-11-29 15:29:40 +01001441 },
1442};
1443
Takashi Iwai9af74212011-06-18 16:17:45 +02001444static const struct hda_pcm_stream via_pcm_digital_capture = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001445 .substreams = 1,
1446 .channels_min = 2,
1447 .channels_max = 2,
1448};
1449
Takashi Iwai370bafb2011-06-20 12:47:45 +02001450/*
1451 * slave controls for virtual master
1452 */
Takashi Iwai9322ca52012-02-03 14:28:01 +01001453static const char * const via_slave_pfxs[] = {
1454 "Front", "Surround", "Center", "LFE", "Side",
1455 "Headphone", "Speaker",
Takashi Iwai370bafb2011-06-20 12:47:45 +02001456 NULL,
1457};
1458
Joseph Chanc577b8a2006-11-29 15:29:40 +01001459static int via_build_controls(struct hda_codec *codec)
1460{
1461 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001462 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001463 int err, i;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001464
Takashi Iwaib5bcc182012-02-02 10:30:17 +01001465 spec->no_pin_power_ctl = 1;
Takashi Iwai24088a52011-06-17 16:59:21 +02001466 if (spec->set_widgets_power_state)
1467 if (!via_clone_control(spec, &via_pin_power_ctl_enum))
1468 return -ENOMEM;
1469
Joseph Chanc577b8a2006-11-29 15:29:40 +01001470 for (i = 0; i < spec->num_mixers; i++) {
1471 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1472 if (err < 0)
1473 return err;
1474 }
1475
1476 if (spec->multiout.dig_out_nid) {
1477 err = snd_hda_create_spdif_out_ctls(codec,
Stephen Warren74b654c2011-06-01 11:14:18 -06001478 spec->multiout.dig_out_nid,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001479 spec->multiout.dig_out_nid);
1480 if (err < 0)
1481 return err;
Takashi Iwai9a081602008-02-12 18:37:26 +01001482 err = snd_hda_create_spdif_share_sw(codec,
1483 &spec->multiout);
1484 if (err < 0)
1485 return err;
1486 spec->multiout.share_spdif = 1;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001487 }
1488 if (spec->dig_in_nid) {
1489 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1490 if (err < 0)
1491 return err;
1492 }
Lydia Wang17314372009-10-10 19:07:37 +08001493
Takashi Iwai370bafb2011-06-20 12:47:45 +02001494 /* if we have no master control, let's create it */
1495 if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1496 unsigned int vmaster_tlv[4];
1497 snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1498 HDA_OUTPUT, vmaster_tlv);
1499 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai9322ca52012-02-03 14:28:01 +01001500 vmaster_tlv, via_slave_pfxs,
1501 "Playback Volume");
Takashi Iwai370bafb2011-06-20 12:47:45 +02001502 if (err < 0)
1503 return err;
1504 }
1505 if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1506 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
Takashi Iwai9322ca52012-02-03 14:28:01 +01001507 NULL, via_slave_pfxs,
1508 "Playback Switch");
Takashi Iwai370bafb2011-06-20 12:47:45 +02001509 if (err < 0)
1510 return err;
1511 }
1512
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001513 /* assign Capture Source enums to NID */
1514 kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1515 for (i = 0; kctl && i < kctl->count; i++) {
Takashi Iwai77e314f2012-02-22 12:34:08 +01001516 if (!spec->mux_nids[i])
1517 continue;
Takashi Iwai21949f02009-12-23 08:31:59 +01001518 err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001519 if (err < 0)
1520 return err;
1521 }
1522
Takashi Iwai603c4012008-07-30 15:01:44 +02001523 via_free_kctls(codec); /* no longer needed */
Takashi Iwai01a61e12011-10-28 00:03:22 +02001524
1525 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1526 if (err < 0)
1527 return err;
1528
Joseph Chanc577b8a2006-11-29 15:29:40 +01001529 return 0;
1530}
1531
1532static int via_build_pcms(struct hda_codec *codec)
1533{
1534 struct via_spec *spec = codec->spec;
1535 struct hda_pcm *info = spec->pcm_rec;
1536
Takashi Iwaia5973102011-09-28 16:43:36 +02001537 codec->num_pcms = 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001538 codec->pcm_info = info;
1539
Takashi Iwaia5973102011-09-28 16:43:36 +02001540 if (spec->multiout.num_dacs || spec->num_adc_nids) {
1541 snprintf(spec->stream_name_analog,
1542 sizeof(spec->stream_name_analog),
1543 "%s Analog", codec->chip_name);
1544 info->name = spec->stream_name_analog;
Takashi Iwai9af74212011-06-18 16:17:45 +02001545
Takashi Iwaia5973102011-09-28 16:43:36 +02001546 if (spec->multiout.num_dacs) {
1547 if (!spec->stream_analog_playback)
1548 spec->stream_analog_playback =
1549 &via_pcm_analog_playback;
1550 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1551 *spec->stream_analog_playback;
1552 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1553 spec->multiout.dac_nids[0];
1554 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1555 spec->multiout.max_channels;
1556 }
Takashi Iwai9af74212011-06-18 16:17:45 +02001557
Takashi Iwaia5973102011-09-28 16:43:36 +02001558 if (!spec->stream_analog_capture) {
1559 if (spec->dyn_adc_switch)
1560 spec->stream_analog_capture =
1561 &via_pcm_dyn_adc_analog_capture;
1562 else
1563 spec->stream_analog_capture =
1564 &via_pcm_analog_capture;
1565 }
1566 if (spec->num_adc_nids) {
1567 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1568 *spec->stream_analog_capture;
1569 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1570 spec->adc_nids[0];
1571 if (!spec->dyn_adc_switch)
1572 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
1573 spec->num_adc_nids;
1574 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001575 codec->num_pcms++;
1576 info++;
Takashi Iwaia5973102011-09-28 16:43:36 +02001577 }
1578
1579 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwai82673bc2011-06-17 16:24:21 +02001580 snprintf(spec->stream_name_digital,
1581 sizeof(spec->stream_name_digital),
1582 "%s Digital", codec->chip_name);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001583 info->name = spec->stream_name_digital;
Takashi Iwai7ba72ba2008-02-06 14:03:20 +01001584 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001585 if (spec->multiout.dig_out_nid) {
Takashi Iwai9af74212011-06-18 16:17:45 +02001586 if (!spec->stream_digital_playback)
1587 spec->stream_digital_playback =
1588 &via_pcm_digital_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001589 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
Takashi Iwai9af74212011-06-18 16:17:45 +02001590 *spec->stream_digital_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001591 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1592 spec->multiout.dig_out_nid;
1593 }
1594 if (spec->dig_in_nid) {
Takashi Iwai9af74212011-06-18 16:17:45 +02001595 if (!spec->stream_digital_capture)
1596 spec->stream_digital_capture =
1597 &via_pcm_digital_capture;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001598 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
Takashi Iwai9af74212011-06-18 16:17:45 +02001599 *spec->stream_digital_capture;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001600 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1601 spec->dig_in_nid;
1602 }
Takashi Iwaia5973102011-09-28 16:43:36 +02001603 codec->num_pcms++;
1604 info++;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001605 }
1606
Takashi Iwaiece8d042011-06-19 16:24:21 +02001607 if (spec->hp_dac_nid) {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001608 snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
1609 "%s HP", codec->chip_name);
1610 info->name = spec->stream_name_hp;
1611 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
1612 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
Takashi Iwaiece8d042011-06-19 16:24:21 +02001613 spec->hp_dac_nid;
Takashi Iwaia5973102011-09-28 16:43:36 +02001614 codec->num_pcms++;
1615 info++;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001616 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001617 return 0;
1618}
1619
1620static void via_free(struct hda_codec *codec)
1621{
1622 struct via_spec *spec = codec->spec;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001623
1624 if (!spec)
1625 return;
1626
Takashi Iwai603c4012008-07-30 15:01:44 +02001627 via_free_kctls(codec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001628 vt1708_stop_hp_work(spec);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001629 kfree(spec->bind_cap_vol);
1630 kfree(spec->bind_cap_sw);
1631 kfree(spec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001632}
1633
Takashi Iwai64be2852011-06-17 16:51:39 +02001634/* mute/unmute outputs */
1635static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
1636 hda_nid_t *pins, bool mute)
1637{
1638 int i;
Takashi Iwai94994732011-07-11 11:36:44 +02001639 for (i = 0; i < num_pins; i++) {
1640 unsigned int parm = snd_hda_codec_read(codec, pins[i], 0,
1641 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1642 if (parm & AC_PINCTL_IN_EN)
1643 continue;
1644 if (mute)
1645 parm &= ~AC_PINCTL_OUT_EN;
1646 else
1647 parm |= AC_PINCTL_OUT_EN;
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001648 snd_hda_set_pin_ctl(codec, pins[i], parm);
Takashi Iwai94994732011-07-11 11:36:44 +02001649 }
Takashi Iwai64be2852011-06-17 16:51:39 +02001650}
1651
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001652/* mute internal speaker if line-out is plugged */
1653static void via_line_automute(struct hda_codec *codec, int present)
1654{
1655 struct via_spec *spec = codec->spec;
1656
1657 if (!spec->autocfg.speaker_outs)
1658 return;
1659 if (!present)
1660 present = snd_hda_jack_detect(codec,
1661 spec->autocfg.line_out_pins[0]);
1662 toggle_output_mutes(codec, spec->autocfg.speaker_outs,
1663 spec->autocfg.speaker_pins,
1664 present);
1665}
1666
Harald Welte69e52a82008-09-09 15:57:32 +08001667/* mute internal speaker if HP is plugged */
1668static void via_hp_automute(struct hda_codec *codec)
1669{
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001670 int present = 0;
Takashi Iwai6e969d92011-07-11 11:28:13 +02001671 int nums;
Harald Welte69e52a82008-09-09 15:57:32 +08001672 struct via_spec *spec = codec->spec;
1673
Takashi Iwai187d3332011-11-24 16:33:09 +01001674 if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0] &&
Herton Ronaldo Krzesinskicf55e902012-09-21 20:45:19 -03001675 (spec->codec_type != VT1708 || spec->vt1708_jack_detect) &&
1676 is_jack_detectable(codec, spec->autocfg.hp_pins[0]))
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001677 present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
Takashi Iwai6e969d92011-07-11 11:28:13 +02001678
1679 if (spec->smart51_enabled)
1680 nums = spec->autocfg.line_outs + spec->smart51_nums;
1681 else
1682 nums = spec->autocfg.line_outs;
1683 toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
1684
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001685 via_line_automute(codec, present);
Lydia Wangf3db4232009-10-10 19:08:41 +08001686}
1687
Harald Welte69e52a82008-09-09 15:57:32 +08001688static void via_gpio_control(struct hda_codec *codec)
1689{
1690 unsigned int gpio_data;
1691 unsigned int vol_counter;
1692 unsigned int vol;
1693 unsigned int master_vol;
1694
1695 struct via_spec *spec = codec->spec;
1696
1697 gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
1698 AC_VERB_GET_GPIO_DATA, 0) & 0x03;
1699
1700 vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
1701 0xF84, 0) & 0x3F0000) >> 16;
1702
1703 vol = vol_counter & 0x1F;
1704 master_vol = snd_hda_codec_read(codec, 0x1A, 0,
1705 AC_VERB_GET_AMP_GAIN_MUTE,
1706 AC_AMP_GET_INPUT);
1707
1708 if (gpio_data == 0x02) {
1709 /* unmute line out */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001710 snd_hda_set_pin_ctl(codec, spec->autocfg.line_out_pins[0],
Takashi Iwai3e0693e2011-06-17 16:37:45 +02001711 PIN_OUT);
Harald Welte69e52a82008-09-09 15:57:32 +08001712 if (vol_counter & 0x20) {
1713 /* decrease volume */
1714 if (vol > master_vol)
1715 vol = master_vol;
1716 snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
1717 0, HDA_AMP_VOLMASK,
1718 master_vol-vol);
1719 } else {
1720 /* increase volume */
1721 snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
1722 HDA_AMP_VOLMASK,
1723 ((master_vol+vol) > 0x2A) ? 0x2A :
1724 (master_vol+vol));
1725 }
1726 } else if (!(gpio_data & 0x02)) {
1727 /* mute line out */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001728 snd_hda_set_pin_ctl(codec, spec->autocfg.line_out_pins[0], 0);
Harald Welte69e52a82008-09-09 15:57:32 +08001729 }
1730}
1731
1732/* unsolicited event for jack sensing */
1733static void via_unsol_event(struct hda_codec *codec,
1734 unsigned int res)
1735{
1736 res >>= 26;
Takashi Iwai3a938972011-10-28 01:16:55 +02001737 res = snd_hda_jack_get_action(codec, res);
Lydia Wangec7e7e42011-03-24 12:43:44 +08001738
Lydia Wanga34df192009-10-10 19:08:01 +08001739 if (res & VIA_JACK_EVENT)
Lydia Wang3e95b9a2011-03-23 15:13:28 +08001740 set_widgets_power_state(codec);
Lydia Wangec7e7e42011-03-24 12:43:44 +08001741
1742 res &= ~VIA_JACK_EVENT;
1743
Takashi Iwai21ce0b62011-07-11 10:33:47 +02001744 if (res == VIA_HP_EVENT || res == VIA_LINE_EVENT)
Lydia Wangec7e7e42011-03-24 12:43:44 +08001745 via_hp_automute(codec);
1746 else if (res == VIA_GPIO_EVENT)
1747 via_gpio_control(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001748 snd_hda_jack_report_sync(codec);
Harald Welte69e52a82008-09-09 15:57:32 +08001749}
1750
Takashi Iwai2a439522011-07-26 09:52:50 +02001751#ifdef CONFIG_PM
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001752static int via_suspend(struct hda_codec *codec)
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001753{
1754 struct via_spec *spec = codec->spec;
1755 vt1708_stop_hp_work(spec);
David Henningsson94c142a2012-08-09 10:56:12 +02001756
1757 if (spec->codec_type == VT1802) {
1758 /* Fix pop noise on headphones */
1759 int i;
1760 for (i = 0; i < spec->autocfg.hp_outs; i++)
1761 snd_hda_set_pin_ctl(codec, spec->autocfg.hp_pins[i], 0);
1762 }
1763
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001764 return 0;
1765}
1766#endif
1767
Takashi Iwai83012a72012-08-24 18:38:08 +02001768#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001769static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1770{
1771 struct via_spec *spec = codec->spec;
1772 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1773}
1774#endif
1775
Joseph Chanc577b8a2006-11-29 15:29:40 +01001776/*
1777 */
Takashi Iwai5d417622011-06-20 11:32:27 +02001778
1779static int via_init(struct hda_codec *codec);
1780
Takashi Iwai90dd48a2011-05-02 12:38:19 +02001781static const struct hda_codec_ops via_patch_ops = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001782 .build_controls = via_build_controls,
1783 .build_pcms = via_build_pcms,
1784 .init = via_init,
1785 .free = via_free,
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001786 .unsol_event = via_unsol_event,
Takashi Iwai2a439522011-07-26 09:52:50 +02001787#ifdef CONFIG_PM
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001788 .suspend = via_suspend,
Takashi Iwaicb53c622007-08-10 17:21:45 +02001789 .check_power_status = via_check_power_status,
1790#endif
Joseph Chanc577b8a2006-11-29 15:29:40 +01001791};
1792
Takashi Iwai4a796162011-06-17 17:53:38 +02001793static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
Joseph Chanc577b8a2006-11-29 15:29:40 +01001794{
Takashi Iwai4a796162011-06-17 17:53:38 +02001795 struct via_spec *spec = codec->spec;
1796 int i;
1797
1798 for (i = 0; i < spec->multiout.num_dacs; i++) {
1799 if (spec->multiout.dac_nids[i] == dac)
1800 return false;
1801 }
Takashi Iwaiece8d042011-06-19 16:24:21 +02001802 if (spec->hp_dac_nid == dac)
Takashi Iwai4a796162011-06-17 17:53:38 +02001803 return false;
1804 return true;
1805}
1806
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001807static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3214b962011-07-18 12:49:25 +02001808 hda_nid_t target_dac, int with_aa_mix,
1809 struct nid_path *path, int depth)
Takashi Iwai4a796162011-06-17 17:53:38 +02001810{
Takashi Iwai3214b962011-07-18 12:49:25 +02001811 struct via_spec *spec = codec->spec;
Takashi Iwai4a796162011-06-17 17:53:38 +02001812 hda_nid_t conn[8];
1813 int i, nums;
1814
Takashi Iwai3214b962011-07-18 12:49:25 +02001815 if (nid == spec->aa_mix_nid) {
1816 if (!with_aa_mix)
1817 return false;
1818 with_aa_mix = 2; /* mark aa-mix is included */
1819 }
1820
Takashi Iwai4a796162011-06-17 17:53:38 +02001821 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
1822 for (i = 0; i < nums; i++) {
1823 if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
1824 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02001825 if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
1826 /* aa-mix is requested but not included? */
1827 if (!(spec->aa_mix_nid && with_aa_mix == 1))
1828 goto found;
1829 }
Takashi Iwai4a796162011-06-17 17:53:38 +02001830 }
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001831 if (depth >= MAX_NID_PATH_DEPTH)
Takashi Iwai4a796162011-06-17 17:53:38 +02001832 return false;
1833 for (i = 0; i < nums; i++) {
1834 unsigned int type;
1835 type = get_wcaps_type(get_wcaps(codec, conn[i]));
Takashi Iwai3214b962011-07-18 12:49:25 +02001836 if (type == AC_WID_AUD_OUT)
Takashi Iwai4a796162011-06-17 17:53:38 +02001837 continue;
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001838 if (__parse_output_path(codec, conn[i], target_dac,
Takashi Iwai3214b962011-07-18 12:49:25 +02001839 with_aa_mix, path, depth + 1))
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001840 goto found;
Takashi Iwai4a796162011-06-17 17:53:38 +02001841 }
1842 return false;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001843
1844 found:
1845 path->path[path->depth] = conn[i];
1846 path->idx[path->depth] = i;
1847 if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
1848 path->multi[path->depth] = 1;
1849 path->depth++;
1850 return true;
Takashi Iwai4a796162011-06-17 17:53:38 +02001851}
1852
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001853static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3214b962011-07-18 12:49:25 +02001854 hda_nid_t target_dac, int with_aa_mix,
1855 struct nid_path *path)
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001856{
Takashi Iwai3214b962011-07-18 12:49:25 +02001857 if (__parse_output_path(codec, nid, target_dac, with_aa_mix, path, 1)) {
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001858 path->path[path->depth] = nid;
1859 path->depth++;
Takashi Iwai3214b962011-07-18 12:49:25 +02001860 snd_printdd("output-path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
1861 path->depth, path->path[0], path->path[1],
1862 path->path[2], path->path[3], path->path[4]);
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001863 return true;
1864 }
1865 return false;
1866}
1867
Takashi Iwai4a796162011-06-17 17:53:38 +02001868static int via_auto_fill_dac_nids(struct hda_codec *codec)
1869{
1870 struct via_spec *spec = codec->spec;
1871 const struct auto_pin_cfg *cfg = &spec->autocfg;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001872 int i, dac_num;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001873 hda_nid_t nid;
1874
Joseph Chanc577b8a2006-11-29 15:29:40 +01001875 spec->multiout.dac_nids = spec->private_dac_nids;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001876 dac_num = 0;
Takashi Iwai4a796162011-06-17 17:53:38 +02001877 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai3214b962011-07-18 12:49:25 +02001878 hda_nid_t dac = 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001879 nid = cfg->line_out_pins[i];
Takashi Iwai4a796162011-06-17 17:53:38 +02001880 if (!nid)
1881 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02001882 if (parse_output_path(codec, nid, 0, 0, &spec->out_path[i]))
1883 dac = spec->out_path[i].path[0];
1884 if (!i && parse_output_path(codec, nid, dac, 1,
1885 &spec->out_mix_path))
1886 dac = spec->out_mix_path.path[0];
1887 if (dac) {
1888 spec->private_dac_nids[i] = dac;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001889 dac_num++;
1890 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001891 }
Takashi Iwai3214b962011-07-18 12:49:25 +02001892 if (!spec->out_path[0].depth && spec->out_mix_path.depth) {
1893 spec->out_path[0] = spec->out_mix_path;
1894 spec->out_mix_path.depth = 0;
1895 }
Lydia Wang5c9a5612011-07-08 14:03:43 +08001896 spec->multiout.num_dacs = dac_num;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001897 return 0;
1898}
1899
Takashi Iwai4a796162011-06-17 17:53:38 +02001900static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001901 int chs, bool check_dac, struct nid_path *path)
Joseph Chanc577b8a2006-11-29 15:29:40 +01001902{
Takashi Iwai4a796162011-06-17 17:53:38 +02001903 struct via_spec *spec = codec->spec;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001904 char name[32];
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001905 hda_nid_t dac, pin, sel, nid;
1906 int err;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001907
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001908 dac = check_dac ? path->path[0] : 0;
1909 pin = path->path[path->depth - 1];
1910 sel = path->depth > 1 ? path->path[1] : 0;
Takashi Iwai4a796162011-06-17 17:53:38 +02001911
Takashi Iwai8df2a312011-06-21 11:48:29 +02001912 if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
Takashi Iwai4a796162011-06-17 17:53:38 +02001913 nid = dac;
Takashi Iwai8df2a312011-06-21 11:48:29 +02001914 else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
Takashi Iwai4a796162011-06-17 17:53:38 +02001915 nid = pin;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001916 else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1917 nid = sel;
Takashi Iwai4a796162011-06-17 17:53:38 +02001918 else
1919 nid = 0;
1920 if (nid) {
1921 sprintf(name, "%s Playback Volume", pfx);
1922 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
Lydia Wanga00a5fa2011-06-21 16:11:11 +08001923 HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
Takashi Iwai4a796162011-06-17 17:53:38 +02001924 if (err < 0)
1925 return err;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001926 path->vol_ctl = nid;
Takashi Iwai4a796162011-06-17 17:53:38 +02001927 }
1928
Takashi Iwai8df2a312011-06-21 11:48:29 +02001929 if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
Takashi Iwai4a796162011-06-17 17:53:38 +02001930 nid = dac;
Takashi Iwai8df2a312011-06-21 11:48:29 +02001931 else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
Takashi Iwai4a796162011-06-17 17:53:38 +02001932 nid = pin;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001933 else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1934 nid = sel;
Takashi Iwai4a796162011-06-17 17:53:38 +02001935 else
1936 nid = 0;
1937 if (nid) {
1938 sprintf(name, "%s Playback Switch", pfx);
1939 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1940 HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1941 if (err < 0)
1942 return err;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001943 path->mute_ctl = nid;
Takashi Iwai4a796162011-06-17 17:53:38 +02001944 }
1945 return 0;
1946}
1947
Takashi Iwaif4a78282011-06-17 18:46:48 +02001948static void mangle_smart51(struct hda_codec *codec)
1949{
1950 struct via_spec *spec = codec->spec;
1951 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001952 struct auto_pin_cfg_item *ins = cfg->inputs;
1953 int i, j, nums, attr;
1954 int pins[AUTO_CFG_MAX_INS];
Takashi Iwaif4a78282011-06-17 18:46:48 +02001955
Takashi Iwai0f98c242011-06-21 12:51:33 +02001956 for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
1957 nums = 0;
1958 for (i = 0; i < cfg->num_inputs; i++) {
1959 unsigned int def;
1960 if (ins[i].type > AUTO_PIN_LINE_IN)
1961 continue;
1962 def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
1963 if (snd_hda_get_input_pin_attr(def) != attr)
1964 continue;
1965 for (j = 0; j < nums; j++)
1966 if (ins[pins[j]].type < ins[i].type) {
1967 memmove(pins + j + 1, pins + j,
Takashi Iwai21d45d22011-07-08 11:35:11 +02001968 (nums - j) * sizeof(int));
Takashi Iwai0f98c242011-06-21 12:51:33 +02001969 break;
1970 }
1971 pins[j] = i;
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001972 nums++;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001973 }
1974 if (cfg->line_outs + nums < 3)
Takashi Iwaif4a78282011-06-17 18:46:48 +02001975 continue;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001976 for (i = 0; i < nums; i++) {
1977 hda_nid_t pin = ins[pins[i]].pin;
1978 spec->smart51_pins[spec->smart51_nums++] = pin;
1979 cfg->line_out_pins[cfg->line_outs++] = pin;
1980 if (cfg->line_outs == 3)
1981 break;
1982 }
1983 return;
Takashi Iwaif4a78282011-06-17 18:46:48 +02001984 }
1985}
1986
Takashi Iwai020066d2011-07-21 13:45:56 +02001987static void copy_path_mixer_ctls(struct nid_path *dst, struct nid_path *src)
1988{
1989 dst->vol_ctl = src->vol_ctl;
1990 dst->mute_ctl = src->mute_ctl;
1991}
1992
Takashi Iwai4a796162011-06-17 17:53:38 +02001993/* add playback controls from the parsed DAC table */
1994static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
1995{
1996 struct via_spec *spec = codec->spec;
Takashi Iwaif4a78282011-06-17 18:46:48 +02001997 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai3214b962011-07-18 12:49:25 +02001998 struct nid_path *path;
Takashi Iwaiea734962011-01-17 11:29:34 +01001999 static const char * const chname[4] = {
2000 "Front", "Surround", "C/LFE", "Side"
2001 };
Takashi Iwai4a796162011-06-17 17:53:38 +02002002 int i, idx, err;
Takashi Iwaif4a78282011-06-17 18:46:48 +02002003 int old_line_outs;
2004
2005 /* check smart51 */
2006 old_line_outs = cfg->line_outs;
2007 if (cfg->line_outs == 1)
2008 mangle_smart51(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002009
Takashi Iwaie3d7a142011-06-20 13:52:33 +02002010 err = via_auto_fill_dac_nids(codec);
2011 if (err < 0)
2012 return err;
2013
Lydia Wang5c9a5612011-07-08 14:03:43 +08002014 if (spec->multiout.num_dacs < 3) {
2015 spec->smart51_nums = 0;
2016 cfg->line_outs = old_line_outs;
2017 }
Takashi Iwai4a796162011-06-17 17:53:38 +02002018 for (i = 0; i < cfg->line_outs; i++) {
2019 hda_nid_t pin, dac;
2020 pin = cfg->line_out_pins[i];
2021 dac = spec->multiout.dac_nids[i];
2022 if (!pin || !dac)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002023 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02002024 path = spec->out_path + i;
Takashi Iwai0fe0adf2011-06-19 16:27:53 +02002025 if (i == HDA_CLFE) {
Takashi Iwai3214b962011-07-18 12:49:25 +02002026 err = create_ch_ctls(codec, "Center", 1, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002027 if (err < 0)
2028 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02002029 err = create_ch_ctls(codec, "LFE", 2, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002030 if (err < 0)
2031 return err;
2032 } else {
Takashi Iwai6aadf412011-06-20 14:09:02 +02002033 const char *pfx = chname[i];
2034 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2035 cfg->line_outs == 1)
2036 pfx = "Speaker";
Takashi Iwai3214b962011-07-18 12:49:25 +02002037 err = create_ch_ctls(codec, pfx, 3, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002038 if (err < 0)
2039 return err;
2040 }
Takashi Iwai020066d2011-07-21 13:45:56 +02002041 if (path != spec->out_path + i)
2042 copy_path_mixer_ctls(&spec->out_path[i], path);
2043 if (path == spec->out_path && spec->out_mix_path.depth)
2044 copy_path_mixer_ctls(&spec->out_mix_path, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002045 }
2046
Takashi Iwai4a796162011-06-17 17:53:38 +02002047 idx = get_connection_index(codec, spec->aa_mix_nid,
2048 spec->multiout.dac_nids[0]);
2049 if (idx >= 0) {
2050 /* add control to mixer */
Takashi Iwai3214b962011-07-18 12:49:25 +02002051 const char *name;
2052 name = spec->out_mix_path.depth ?
2053 "PCM Loopback Playback Volume" : "PCM Playback Volume";
2054 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
Takashi Iwai4a796162011-06-17 17:53:38 +02002055 HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2056 idx, HDA_INPUT));
2057 if (err < 0)
2058 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02002059 name = spec->out_mix_path.depth ?
2060 "PCM Loopback Playback Switch" : "PCM Playback Switch";
2061 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
Takashi Iwai4a796162011-06-17 17:53:38 +02002062 HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2063 idx, HDA_INPUT));
2064 if (err < 0)
2065 return err;
2066 }
2067
Takashi Iwaif4a78282011-06-17 18:46:48 +02002068 cfg->line_outs = old_line_outs;
2069
Joseph Chanc577b8a2006-11-29 15:29:40 +01002070 return 0;
2071}
2072
Takashi Iwai4a796162011-06-17 17:53:38 +02002073static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002074{
Takashi Iwai4a796162011-06-17 17:53:38 +02002075 struct via_spec *spec = codec->spec;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02002076 struct nid_path *path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002077 bool check_dac;
Takashi Iwai3214b962011-07-18 12:49:25 +02002078 int i, err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002079
2080 if (!pin)
2081 return 0;
2082
Takashi Iwai3214b962011-07-18 12:49:25 +02002083 if (!parse_output_path(codec, pin, 0, 0, &spec->hp_indep_path)) {
2084 for (i = HDA_SIDE; i >= HDA_CLFE; i--) {
2085 if (i < spec->multiout.num_dacs &&
2086 parse_output_path(codec, pin,
2087 spec->multiout.dac_nids[i], 0,
2088 &spec->hp_indep_path)) {
2089 spec->hp_indep_shared = i;
2090 break;
2091 }
2092 }
Takashi Iwai25250502011-06-30 17:24:47 +02002093 }
Takashi Iwai3214b962011-07-18 12:49:25 +02002094 if (spec->hp_indep_path.depth) {
2095 spec->hp_dac_nid = spec->hp_indep_path.path[0];
2096 if (!spec->hp_indep_shared)
2097 spec->hp_path = spec->hp_indep_path;
2098 }
2099 /* optionally check front-path w/o AA-mix */
2100 if (!spec->hp_path.depth)
2101 parse_output_path(codec, pin,
2102 spec->multiout.dac_nids[HDA_FRONT], 0,
2103 &spec->hp_path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002104
Takashi Iwaiece8d042011-06-19 16:24:21 +02002105 if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
Takashi Iwai3214b962011-07-18 12:49:25 +02002106 1, &spec->hp_mix_path) && !spec->hp_path.depth)
Takashi Iwaiece8d042011-06-19 16:24:21 +02002107 return 0;
2108
Takashi Iwai3214b962011-07-18 12:49:25 +02002109 if (spec->hp_path.depth) {
Takashi Iwai09a9ad692011-06-21 15:57:44 +02002110 path = &spec->hp_path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002111 check_dac = true;
2112 } else {
Takashi Iwai3214b962011-07-18 12:49:25 +02002113 path = &spec->hp_mix_path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002114 check_dac = false;
2115 }
2116 err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002117 if (err < 0)
2118 return err;
Takashi Iwai020066d2011-07-21 13:45:56 +02002119 if (check_dac)
2120 copy_path_mixer_ctls(&spec->hp_mix_path, path);
2121 else
2122 copy_path_mixer_ctls(&spec->hp_path, path);
2123 if (spec->hp_indep_path.depth)
2124 copy_path_mixer_ctls(&spec->hp_indep_path, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002125 return 0;
2126}
2127
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002128static int via_auto_create_speaker_ctls(struct hda_codec *codec)
2129{
2130 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +02002131 struct nid_path *path;
2132 bool check_dac;
Wang Shaoyan81c0a782011-08-05 18:51:29 +08002133 hda_nid_t pin, dac = 0;
Takashi Iwai3214b962011-07-18 12:49:25 +02002134 int err;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002135
2136 pin = spec->autocfg.speaker_pins[0];
2137 if (!spec->autocfg.speaker_outs || !pin)
2138 return 0;
2139
Takashi Iwai3214b962011-07-18 12:49:25 +02002140 if (parse_output_path(codec, pin, 0, 0, &spec->speaker_path))
Takashi Iwai8e3679d2011-06-21 09:01:36 +02002141 dac = spec->speaker_path.path[0];
Takashi Iwai3214b962011-07-18 12:49:25 +02002142 if (!dac)
2143 parse_output_path(codec, pin,
2144 spec->multiout.dac_nids[HDA_FRONT], 0,
2145 &spec->speaker_path);
2146 if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
2147 1, &spec->speaker_mix_path) && !dac)
2148 return 0;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002149
Takashi Iwai3214b962011-07-18 12:49:25 +02002150 /* no AA-path for front? */
2151 if (!spec->out_mix_path.depth && spec->speaker_mix_path.depth)
2152 dac = 0;
2153
2154 spec->speaker_dac_nid = dac;
2155 spec->multiout.extra_out_nid[0] = dac;
2156 if (dac) {
2157 path = &spec->speaker_path;
2158 check_dac = true;
2159 } else {
2160 path = &spec->speaker_mix_path;
2161 check_dac = false;
2162 }
2163 err = create_ch_ctls(codec, "Speaker", 3, check_dac, path);
2164 if (err < 0)
2165 return err;
Takashi Iwai020066d2011-07-21 13:45:56 +02002166 if (check_dac)
2167 copy_path_mixer_ctls(&spec->speaker_mix_path, path);
2168 else
2169 copy_path_mixer_ctls(&spec->speaker_path, path);
Takashi Iwai3214b962011-07-18 12:49:25 +02002170 return 0;
2171}
2172
2173#define via_aamix_ctl_info via_pin_power_ctl_info
2174
2175static int via_aamix_ctl_get(struct snd_kcontrol *kcontrol,
2176 struct snd_ctl_elem_value *ucontrol)
2177{
2178 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2179 struct via_spec *spec = codec->spec;
2180 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2181 return 0;
2182}
2183
2184static void update_aamix_paths(struct hda_codec *codec, int do_mix,
2185 struct nid_path *nomix, struct nid_path *mix)
2186{
2187 if (do_mix) {
2188 activate_output_path(codec, nomix, false, false);
2189 activate_output_path(codec, mix, true, false);
2190 } else {
2191 activate_output_path(codec, mix, false, false);
2192 activate_output_path(codec, nomix, true, false);
2193 }
2194}
2195
2196static int via_aamix_ctl_put(struct snd_kcontrol *kcontrol,
2197 struct snd_ctl_elem_value *ucontrol)
2198{
2199 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2200 struct via_spec *spec = codec->spec;
2201 unsigned int val = ucontrol->value.enumerated.item[0];
2202
2203 if (val == spec->aamix_mode)
2204 return 0;
2205 spec->aamix_mode = val;
2206 /* update front path */
2207 update_aamix_paths(codec, val, &spec->out_path[0], &spec->out_mix_path);
2208 /* update HP path */
2209 if (!spec->hp_independent_mode) {
2210 update_aamix_paths(codec, val, &spec->hp_path,
2211 &spec->hp_mix_path);
2212 }
2213 /* update speaker path */
2214 update_aamix_paths(codec, val, &spec->speaker_path,
2215 &spec->speaker_mix_path);
2216 return 1;
2217}
2218
2219static const struct snd_kcontrol_new via_aamix_ctl_enum = {
2220 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2221 .name = "Loopback Mixing",
2222 .info = via_aamix_ctl_info,
2223 .get = via_aamix_ctl_get,
2224 .put = via_aamix_ctl_put,
2225};
2226
2227static int via_auto_create_loopback_switch(struct hda_codec *codec)
2228{
2229 struct via_spec *spec = codec->spec;
2230
Takashi Iwai4808d122012-01-10 15:16:02 +01002231 if (!spec->aa_mix_nid)
2232 return 0; /* no loopback switching available */
2233 if (!(spec->out_mix_path.depth || spec->hp_mix_path.depth ||
2234 spec->speaker_path.depth))
Takashi Iwai3214b962011-07-18 12:49:25 +02002235 return 0; /* no loopback switching available */
2236 if (!via_clone_control(spec, &via_aamix_ctl_enum))
2237 return -ENOMEM;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002238 return 0;
2239}
2240
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002241/* look for ADCs */
2242static int via_fill_adcs(struct hda_codec *codec)
2243{
2244 struct via_spec *spec = codec->spec;
2245 hda_nid_t nid = codec->start_nid;
2246 int i;
2247
2248 for (i = 0; i < codec->num_nodes; i++, nid++) {
2249 unsigned int wcaps = get_wcaps(codec, nid);
2250 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2251 continue;
2252 if (wcaps & AC_WCAP_DIGITAL)
2253 continue;
2254 if (!(wcaps & AC_WCAP_CONN_LIST))
2255 continue;
2256 if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2257 return -ENOMEM;
2258 spec->adc_nids[spec->num_adc_nids++] = nid;
2259 }
2260 return 0;
2261}
2262
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002263/* input-src control */
2264static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
2265 struct snd_ctl_elem_info *uinfo)
2266{
2267 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2268 struct via_spec *spec = codec->spec;
2269
2270 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2271 uinfo->count = 1;
2272 uinfo->value.enumerated.items = spec->num_inputs;
2273 if (uinfo->value.enumerated.item >= spec->num_inputs)
2274 uinfo->value.enumerated.item = spec->num_inputs - 1;
2275 strcpy(uinfo->value.enumerated.name,
2276 spec->inputs[uinfo->value.enumerated.item].label);
2277 return 0;
2278}
2279
2280static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
2281 struct snd_ctl_elem_value *ucontrol)
2282{
2283 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2284 struct via_spec *spec = codec->spec;
2285 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2286
2287 ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
2288 return 0;
2289}
2290
2291static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
2292 struct snd_ctl_elem_value *ucontrol)
2293{
2294 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2295 struct via_spec *spec = codec->spec;
2296 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2297 hda_nid_t mux;
2298 int cur;
2299
2300 cur = ucontrol->value.enumerated.item[0];
2301 if (cur < 0 || cur >= spec->num_inputs)
2302 return -EINVAL;
2303 if (spec->cur_mux[idx] == cur)
2304 return 0;
2305 spec->cur_mux[idx] = cur;
2306 if (spec->dyn_adc_switch) {
2307 int adc_idx = spec->inputs[cur].adc_idx;
2308 mux = spec->mux_nids[adc_idx];
2309 via_dyn_adc_pcm_resetup(codec, cur);
2310 } else {
2311 mux = spec->mux_nids[idx];
2312 if (snd_BUG_ON(!mux))
2313 return -EINVAL;
2314 }
2315
2316 if (mux) {
2317 /* switch to D0 beofre change index */
Takashi Iwai054d8672012-01-24 12:25:50 +01002318 update_power_state(codec, mux, AC_PWRST_D0);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002319 snd_hda_codec_write(codec, mux, 0,
2320 AC_VERB_SET_CONNECT_SEL,
2321 spec->inputs[cur].mux_idx);
2322 }
2323
2324 /* update jack power state */
2325 set_widgets_power_state(codec);
2326 return 0;
2327}
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002328
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002329static const struct snd_kcontrol_new via_input_src_ctl = {
2330 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2331 /* The multiple "Capture Source" controls confuse alsamixer
2332 * So call somewhat different..
2333 */
2334 /* .name = "Capture Source", */
2335 .name = "Input Source",
2336 .info = via_mux_enum_info,
2337 .get = via_mux_enum_get,
2338 .put = via_mux_enum_put,
2339};
2340
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002341static int create_input_src_ctls(struct hda_codec *codec, int count)
2342{
2343 struct via_spec *spec = codec->spec;
2344 struct snd_kcontrol_new *knew;
2345
2346 if (spec->num_inputs <= 1 || !count)
2347 return 0; /* no need for single src */
2348
2349 knew = via_clone_control(spec, &via_input_src_ctl);
2350 if (!knew)
2351 return -ENOMEM;
2352 knew->count = count;
2353 return 0;
2354}
2355
2356/* add the powersave loopback-list entry */
Takashi Iwai13af8e72011-06-20 14:05:46 +02002357static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
2358{
2359 struct hda_amp_list *list;
2360
2361 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2362 return;
2363 list = spec->loopback_list + spec->num_loopbacks;
2364 list->nid = mix;
2365 list->dir = HDA_INPUT;
2366 list->idx = idx;
2367 spec->num_loopbacks++;
2368 spec->loopback.amplist = spec->loopback_list;
2369}
Takashi Iwai13af8e72011-06-20 14:05:46 +02002370
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002371static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
Takashi Iwai8d087c72011-06-28 12:45:47 +02002372 hda_nid_t dst)
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002373{
Takashi Iwai8d087c72011-06-28 12:45:47 +02002374 return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002375}
2376
2377/* add the input-route to the given pin */
2378static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002379{
Takashi Iwai10a20af2010-09-09 16:28:02 +02002380 struct via_spec *spec = codec->spec;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002381 int c, idx;
2382
2383 spec->inputs[spec->num_inputs].adc_idx = -1;
2384 spec->inputs[spec->num_inputs].pin = pin;
2385 for (c = 0; c < spec->num_adc_nids; c++) {
2386 if (spec->mux_nids[c]) {
2387 idx = get_connection_index(codec, spec->mux_nids[c],
2388 pin);
2389 if (idx < 0)
2390 continue;
2391 spec->inputs[spec->num_inputs].mux_idx = idx;
2392 } else {
Takashi Iwai8d087c72011-06-28 12:45:47 +02002393 if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002394 continue;
2395 }
2396 spec->inputs[spec->num_inputs].adc_idx = c;
2397 /* Can primary ADC satisfy all inputs? */
2398 if (!spec->dyn_adc_switch &&
2399 spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
2400 snd_printd(KERN_INFO
2401 "via: dynamic ADC switching enabled\n");
2402 spec->dyn_adc_switch = 1;
2403 }
2404 return true;
2405 }
2406 return false;
2407}
2408
2409static int get_mux_nids(struct hda_codec *codec);
2410
2411/* parse input-routes; fill ADCs, MUXs and input-src entries */
2412static int parse_analog_inputs(struct hda_codec *codec)
2413{
2414 struct via_spec *spec = codec->spec;
2415 const struct auto_pin_cfg *cfg = &spec->autocfg;
2416 int i, err;
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002417
2418 err = via_fill_adcs(codec);
2419 if (err < 0)
2420 return err;
2421 err = get_mux_nids(codec);
2422 if (err < 0)
2423 return err;
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002424
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002425 /* fill all input-routes */
2426 for (i = 0; i < cfg->num_inputs; i++) {
2427 if (add_input_route(codec, cfg->inputs[i].pin))
2428 spec->inputs[spec->num_inputs++].label =
2429 hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwaif3268512010-08-30 11:00:19 +02002430 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002431
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002432 /* check for internal loopback recording */
2433 if (spec->aa_mix_nid &&
2434 add_input_route(codec, spec->aa_mix_nid))
2435 spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
2436
2437 return 0;
2438}
2439
2440/* create analog-loopback volume/switch controls */
2441static int create_loopback_ctls(struct hda_codec *codec)
2442{
2443 struct via_spec *spec = codec->spec;
2444 const struct auto_pin_cfg *cfg = &spec->autocfg;
2445 const char *prev_label = NULL;
2446 int type_idx = 0;
2447 int i, j, err, idx;
2448
2449 if (!spec->aa_mix_nid)
2450 return 0;
2451
Takashi Iwai7b315bb2010-08-30 13:06:30 +02002452 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002453 hda_nid_t pin = cfg->inputs[i].pin;
2454 const char *label = hda_get_autocfg_input_label(codec, cfg, i);
2455
Takashi Iwai1e11cae2011-06-21 12:57:22 +02002456 if (prev_label && !strcmp(label, prev_label))
Takashi Iwai7b315bb2010-08-30 13:06:30 +02002457 type_idx++;
2458 else
2459 type_idx = 0;
Takashi Iwai1e11cae2011-06-21 12:57:22 +02002460 prev_label = label;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002461 idx = get_connection_index(codec, spec->aa_mix_nid, pin);
2462 if (idx >= 0) {
Lydia Wang16922282011-03-22 16:24:10 +08002463 err = via_new_analog_input(spec, label, type_idx,
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002464 idx, spec->aa_mix_nid);
Takashi Iwai13af8e72011-06-20 14:05:46 +02002465 if (err < 0)
2466 return err;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002467 add_loopback_list(spec, spec->aa_mix_nid, idx);
Takashi Iwai13af8e72011-06-20 14:05:46 +02002468 }
Takashi Iwaie3d7a142011-06-20 13:52:33 +02002469
2470 /* remember the label for smart51 control */
2471 for (j = 0; j < spec->smart51_nums; j++) {
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002472 if (spec->smart51_pins[j] == pin) {
Takashi Iwaie3d7a142011-06-20 13:52:33 +02002473 spec->smart51_idxs[j] = idx;
2474 spec->smart51_labels[j] = label;
2475 break;
2476 }
2477 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002478 }
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002479 return 0;
2480}
2481
2482/* create mic-boost controls (if present) */
2483static int create_mic_boost_ctls(struct hda_codec *codec)
2484{
2485 struct via_spec *spec = codec->spec;
2486 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai8d8bbc62012-02-22 12:26:38 +01002487 const char *prev_label = NULL;
2488 int type_idx = 0;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002489 int i, err;
2490
2491 for (i = 0; i < cfg->num_inputs; i++) {
2492 hda_nid_t pin = cfg->inputs[i].pin;
2493 unsigned int caps;
2494 const char *label;
2495 char name[32];
2496
2497 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2498 continue;
2499 caps = query_amp_caps(codec, pin, HDA_INPUT);
2500 if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2501 continue;
2502 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai8d8bbc62012-02-22 12:26:38 +01002503 if (prev_label && !strcmp(label, prev_label))
2504 type_idx++;
2505 else
2506 type_idx = 0;
2507 prev_label = label;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002508 snprintf(name, sizeof(name), "%s Boost Volume", label);
Takashi Iwai8d8bbc62012-02-22 12:26:38 +01002509 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002510 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2511 if (err < 0)
2512 return err;
2513 }
2514 return 0;
2515}
2516
2517/* create capture and input-src controls for multiple streams */
2518static int create_multi_adc_ctls(struct hda_codec *codec)
2519{
2520 struct via_spec *spec = codec->spec;
2521 int i, err;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002522
2523 /* create capture mixer elements */
2524 for (i = 0; i < spec->num_adc_nids; i++) {
2525 hda_nid_t adc = spec->adc_nids[i];
2526 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2527 "Capture Volume", i,
2528 HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2529 HDA_INPUT));
2530 if (err < 0)
2531 return err;
2532 err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2533 "Capture Switch", i,
2534 HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2535 HDA_INPUT));
2536 if (err < 0)
2537 return err;
2538 }
2539
2540 /* input-source control */
2541 for (i = 0; i < spec->num_adc_nids; i++)
2542 if (!spec->mux_nids[i])
2543 break;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002544 err = create_input_src_ctls(codec, i);
2545 if (err < 0)
2546 return err;
2547 return 0;
2548}
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002549
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002550/* bind capture volume/switch */
2551static struct snd_kcontrol_new via_bind_cap_vol_ctl =
2552 HDA_BIND_VOL("Capture Volume", 0);
2553static struct snd_kcontrol_new via_bind_cap_sw_ctl =
2554 HDA_BIND_SW("Capture Switch", 0);
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002555
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002556static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
2557 struct hda_ctl_ops *ops)
2558{
2559 struct hda_bind_ctls *ctl;
2560 int i;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002561
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002562 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
2563 if (!ctl)
2564 return -ENOMEM;
2565 ctl->ops = ops;
2566 for (i = 0; i < spec->num_adc_nids; i++)
2567 ctl->values[i] =
2568 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
2569 *ctl_ret = ctl;
2570 return 0;
2571}
2572
2573/* create capture and input-src controls for dynamic ADC-switch case */
2574static int create_dyn_adc_ctls(struct hda_codec *codec)
2575{
2576 struct via_spec *spec = codec->spec;
2577 struct snd_kcontrol_new *knew;
2578 int err;
2579
2580 /* set up the bind capture ctls */
2581 err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
2582 if (err < 0)
2583 return err;
2584 err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
2585 if (err < 0)
2586 return err;
2587
2588 /* create capture mixer elements */
2589 knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
2590 if (!knew)
2591 return -ENOMEM;
2592 knew->private_value = (long)spec->bind_cap_vol;
2593
2594 knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
2595 if (!knew)
2596 return -ENOMEM;
2597 knew->private_value = (long)spec->bind_cap_sw;
2598
2599 /* input-source control */
2600 err = create_input_src_ctls(codec, 1);
2601 if (err < 0)
2602 return err;
2603 return 0;
2604}
2605
2606/* parse and create capture-related stuff */
2607static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
2608{
2609 struct via_spec *spec = codec->spec;
2610 int err;
2611
2612 err = parse_analog_inputs(codec);
2613 if (err < 0)
2614 return err;
2615 if (spec->dyn_adc_switch)
2616 err = create_dyn_adc_ctls(codec);
2617 else
2618 err = create_multi_adc_ctls(codec);
2619 if (err < 0)
2620 return err;
2621 err = create_loopback_ctls(codec);
2622 if (err < 0)
2623 return err;
2624 err = create_mic_boost_ctls(codec);
2625 if (err < 0)
2626 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002627 return 0;
2628}
2629
Harald Welte76d9b0d2008-09-09 15:50:37 +08002630static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
2631{
2632 unsigned int def_conf;
2633 unsigned char seqassoc;
2634
Takashi Iwai2f334f92009-02-20 14:37:42 +01002635 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Harald Welte76d9b0d2008-09-09 15:50:37 +08002636 seqassoc = (unsigned char) get_defcfg_association(def_conf);
2637 seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
Lydia Wang82ef9e42009-10-10 19:08:19 +08002638 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
2639 && (seqassoc == 0xf0 || seqassoc == 0xff)) {
2640 def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
2641 snd_hda_codec_set_pincfg(codec, nid, def_conf);
Harald Welte76d9b0d2008-09-09 15:50:37 +08002642 }
2643
2644 return;
2645}
2646
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002647static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002648 struct snd_ctl_elem_value *ucontrol)
2649{
2650 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2651 struct via_spec *spec = codec->spec;
2652
2653 if (spec->codec_type != VT1708)
2654 return 0;
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002655 ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002656 return 0;
2657}
2658
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002659static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002660 struct snd_ctl_elem_value *ucontrol)
2661{
2662 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2663 struct via_spec *spec = codec->spec;
Takashi Iwai187d3332011-11-24 16:33:09 +01002664 int val;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002665
2666 if (spec->codec_type != VT1708)
2667 return 0;
Takashi Iwai187d3332011-11-24 16:33:09 +01002668 val = !!ucontrol->value.integer.value[0];
2669 if (spec->vt1708_jack_detect == val)
2670 return 0;
2671 spec->vt1708_jack_detect = val;
2672 if (spec->vt1708_jack_detect &&
2673 snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") != 1) {
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002674 mute_aa_path(codec, 1);
2675 notify_aa_path_ctls(codec);
2676 }
Takashi Iwai187d3332011-11-24 16:33:09 +01002677 via_hp_automute(codec);
2678 vt1708_update_hp_work(spec);
2679 return 1;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002680}
2681
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002682static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
2683 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2684 .name = "Jack Detect",
2685 .count = 1,
2686 .info = snd_ctl_boolean_mono_info,
2687 .get = vt1708_jack_detect_get,
2688 .put = vt1708_jack_detect_put,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002689};
2690
Takashi Iwai12daef62011-06-18 17:45:49 +02002691static void fill_dig_outs(struct hda_codec *codec);
2692static void fill_dig_in(struct hda_codec *codec);
2693
2694static int via_parse_auto_config(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002695{
2696 struct via_spec *spec = codec->spec;
2697 int err;
2698
2699 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2700 if (err < 0)
2701 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002702 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
Takashi Iwai7f0df882011-06-18 17:33:34 +02002703 return -EINVAL;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002704
Takashi Iwai4a796162011-06-17 17:53:38 +02002705 err = via_auto_create_multi_out_ctls(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002706 if (err < 0)
2707 return err;
Takashi Iwai4a796162011-06-17 17:53:38 +02002708 err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002709 if (err < 0)
2710 return err;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002711 err = via_auto_create_speaker_ctls(codec);
2712 if (err < 0)
2713 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02002714 err = via_auto_create_loopback_switch(codec);
2715 if (err < 0)
2716 return err;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002717 err = via_auto_create_analog_input_ctls(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002718 if (err < 0)
2719 return err;
2720
2721 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2722
Takashi Iwai12daef62011-06-18 17:45:49 +02002723 fill_dig_outs(codec);
2724 fill_dig_in(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002725
Takashi Iwai603c4012008-07-30 15:01:44 +02002726 if (spec->kctls.list)
2727 spec->mixers[spec->num_mixers++] = spec->kctls.list;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002728
Joseph Chanc577b8a2006-11-29 15:29:40 +01002729
Takashi Iwai3214b962011-07-18 12:49:25 +02002730 if (spec->hp_dac_nid && spec->hp_mix_path.depth) {
Takashi Iwaiece8d042011-06-19 16:24:21 +02002731 err = via_hp_build(codec);
2732 if (err < 0)
2733 return err;
2734 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002735
Takashi Iwaif4a78282011-06-17 18:46:48 +02002736 err = via_smart51_build(codec);
2737 if (err < 0)
2738 return err;
2739
Takashi Iwai5d417622011-06-20 11:32:27 +02002740 /* assign slave outs */
2741 if (spec->slave_dig_outs[0])
2742 codec->slave_dig_outs = spec->slave_dig_outs;
2743
Joseph Chanc577b8a2006-11-29 15:29:40 +01002744 return 1;
2745}
2746
Takashi Iwai5d417622011-06-20 11:32:27 +02002747static void via_auto_init_dig_outs(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002748{
Lydia Wang25eaba22009-10-10 19:08:43 +08002749 struct via_spec *spec = codec->spec;
Takashi Iwai5d417622011-06-20 11:32:27 +02002750 if (spec->multiout.dig_out_nid)
2751 init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
2752 if (spec->slave_dig_outs[0])
2753 init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
2754}
Lydia Wang25eaba22009-10-10 19:08:43 +08002755
Takashi Iwai5d417622011-06-20 11:32:27 +02002756static void via_auto_init_dig_in(struct hda_codec *codec)
2757{
2758 struct via_spec *spec = codec->spec;
2759 if (!spec->dig_in_nid)
2760 return;
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02002761 snd_hda_set_pin_ctl(codec, spec->autocfg.dig_in_pin, PIN_IN);
Takashi Iwai5d417622011-06-20 11:32:27 +02002762}
2763
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002764/* initialize the unsolicited events */
2765static void via_auto_init_unsol_event(struct hda_codec *codec)
2766{
2767 struct via_spec *spec = codec->spec;
2768 struct auto_pin_cfg *cfg = &spec->autocfg;
2769 unsigned int ev;
2770 int i;
2771
2772 if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002773 snd_hda_jack_detect_enable(codec, cfg->hp_pins[0],
2774 VIA_HP_EVENT | VIA_JACK_EVENT);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002775
2776 if (cfg->speaker_pins[0])
2777 ev = VIA_LINE_EVENT;
2778 else
2779 ev = 0;
2780 for (i = 0; i < cfg->line_outs; i++) {
2781 if (cfg->line_out_pins[i] &&
2782 is_jack_detectable(codec, cfg->line_out_pins[i]))
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002783 snd_hda_jack_detect_enable(codec, cfg->line_out_pins[i],
2784 ev | VIA_JACK_EVENT);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002785 }
2786
2787 for (i = 0; i < cfg->num_inputs; i++) {
2788 if (is_jack_detectable(codec, cfg->inputs[i].pin))
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002789 snd_hda_jack_detect_enable(codec, cfg->inputs[i].pin,
2790 VIA_JACK_EVENT);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002791 }
2792}
2793
Takashi Iwai5d417622011-06-20 11:32:27 +02002794static int via_init(struct hda_codec *codec)
2795{
2796 struct via_spec *spec = codec->spec;
2797 int i;
2798
2799 for (i = 0; i < spec->num_iverbs; i++)
2800 snd_hda_sequence_write(codec, spec->init_verbs[i]);
2801
Takashi Iwaie9d010c2012-02-01 10:33:23 +01002802 /* init power states */
2803 set_widgets_power_state(codec);
2804 __analog_low_current_mode(codec, true);
2805
Joseph Chanc577b8a2006-11-29 15:29:40 +01002806 via_auto_init_multi_out(codec);
2807 via_auto_init_hp_out(codec);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002808 via_auto_init_speaker_out(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002809 via_auto_init_analog_input(codec);
Takashi Iwai5d417622011-06-20 11:32:27 +02002810 via_auto_init_dig_outs(codec);
2811 via_auto_init_dig_in(codec);
Lydia Wang118909562011-03-23 17:57:34 +08002812
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002813 via_auto_init_unsol_event(codec);
2814
2815 via_hp_automute(codec);
Takashi Iwai187d3332011-11-24 16:33:09 +01002816 vt1708_update_hp_work(spec);
Lydia Wang25eaba22009-10-10 19:08:43 +08002817
Joseph Chanc577b8a2006-11-29 15:29:40 +01002818 return 0;
2819}
2820
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002821static void vt1708_update_hp_jack_state(struct work_struct *work)
2822{
2823 struct via_spec *spec = container_of(work, struct via_spec,
2824 vt1708_hp_work.work);
2825 if (spec->codec_type != VT1708)
2826 return;
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002827 snd_hda_jack_set_dirty_all(spec->codec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002828 /* if jack state toggled */
2829 if (spec->vt1708_hp_present
Takashi Iwaid56757a2009-11-18 08:00:14 +01002830 != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002831 spec->vt1708_hp_present ^= 1;
2832 via_hp_automute(spec->codec);
2833 }
Takashi Iwai187d3332011-11-24 16:33:09 +01002834 if (spec->vt1708_jack_detect)
2835 schedule_delayed_work(&spec->vt1708_hp_work,
2836 msecs_to_jiffies(100));
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002837}
2838
Takashi Iwai337b9d02009-07-07 18:18:59 +02002839static int get_mux_nids(struct hda_codec *codec)
2840{
2841 struct via_spec *spec = codec->spec;
2842 hda_nid_t nid, conn[8];
2843 unsigned int type;
2844 int i, n;
2845
2846 for (i = 0; i < spec->num_adc_nids; i++) {
2847 nid = spec->adc_nids[i];
2848 while (nid) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002849 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai1c55d522009-07-08 07:45:46 +02002850 if (type == AC_WID_PIN)
2851 break;
Takashi Iwai337b9d02009-07-07 18:18:59 +02002852 n = snd_hda_get_connections(codec, nid, conn,
2853 ARRAY_SIZE(conn));
2854 if (n <= 0)
2855 break;
2856 if (n > 1) {
2857 spec->mux_nids[i] = nid;
2858 break;
2859 }
2860 nid = conn[0];
2861 }
2862 }
Takashi Iwai1c55d522009-07-08 07:45:46 +02002863 return 0;
Takashi Iwai337b9d02009-07-07 18:18:59 +02002864}
2865
Joseph Chanc577b8a2006-11-29 15:29:40 +01002866static int patch_vt1708(struct hda_codec *codec)
2867{
2868 struct via_spec *spec;
2869 int err;
2870
2871 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002872 spec = via_new_spec(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002873 if (spec == NULL)
2874 return -ENOMEM;
2875
Takashi Iwai620e2b22011-06-17 17:19:19 +02002876 spec->aa_mix_nid = 0x17;
2877
Takashi Iwai12daef62011-06-18 17:45:49 +02002878 /* Add HP and CD pin config connect bit re-config action */
2879 vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
2880 vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
2881
Joseph Chanc577b8a2006-11-29 15:29:40 +01002882 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02002883 err = via_parse_auto_config(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002884 if (err < 0) {
2885 via_free(codec);
2886 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002887 }
2888
Takashi Iwai12daef62011-06-18 17:45:49 +02002889 /* add jack detect on/off control */
2890 if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
2891 return -ENOMEM;
2892
Takashi Iwaibc9b5622008-05-23 17:50:27 +02002893 /* disable 32bit format on VT1708 */
2894 if (codec->vendor_id == 0x11061708)
2895 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002896
Lydia Wange322a362011-06-29 13:52:02 +08002897 spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2898
Joseph Chanc577b8a2006-11-29 15:29:40 +01002899 codec->patch_ops = via_patch_ops;
2900
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002901 INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002902 return 0;
2903}
2904
Takashi Iwaiddd304d2011-06-21 16:33:55 +02002905static int patch_vt1709(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002906{
2907 struct via_spec *spec;
2908 int err;
2909
2910 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002911 spec = via_new_spec(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002912 if (spec == NULL)
2913 return -ENOMEM;
2914
Takashi Iwai620e2b22011-06-17 17:19:19 +02002915 spec->aa_mix_nid = 0x18;
2916
Takashi Iwai12daef62011-06-18 17:45:49 +02002917 err = via_parse_auto_config(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002918 if (err < 0) {
2919 via_free(codec);
2920 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002921 }
2922
Joseph Chanc577b8a2006-11-29 15:29:40 +01002923 codec->patch_ops = via_patch_ops;
2924
Josepch Chanf7278fd2007-12-13 16:40:40 +01002925 return 0;
2926}
2927
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002928static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
2929{
2930 struct via_spec *spec = codec->spec;
2931 int imux_is_smixer;
2932 unsigned int parm;
2933 int is_8ch = 0;
Lydia Wangbc92df72011-03-23 17:56:05 +08002934 if ((spec->codec_type != VT1708B_4CH) &&
2935 (codec->vendor_id != 0x11064397))
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002936 is_8ch = 1;
2937
2938 /* SW0 (17h) = stereo mixer */
2939 imux_is_smixer =
2940 (snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
2941 == ((spec->codec_type == VT1708S) ? 5 : 0));
2942 /* inputs */
2943 /* PW 1/2/5 (1ah/1bh/1eh) */
2944 parm = AC_PWRST_D3;
2945 set_pin_power_state(codec, 0x1a, &parm);
2946 set_pin_power_state(codec, 0x1b, &parm);
2947 set_pin_power_state(codec, 0x1e, &parm);
2948 if (imux_is_smixer)
2949 parm = AC_PWRST_D0;
2950 /* SW0 (17h), AIW 0/1 (13h/14h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01002951 update_power_state(codec, 0x17, parm);
2952 update_power_state(codec, 0x13, parm);
2953 update_power_state(codec, 0x14, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002954
2955 /* outputs */
2956 /* PW0 (19h), SW1 (18h), AOW1 (11h) */
2957 parm = AC_PWRST_D3;
2958 set_pin_power_state(codec, 0x19, &parm);
2959 if (spec->smart51_enabled)
2960 set_pin_power_state(codec, 0x1b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002961 update_power_state(codec, 0x18, parm);
2962 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002963
2964 /* PW6 (22h), SW2 (26h), AOW2 (24h) */
2965 if (is_8ch) {
2966 parm = AC_PWRST_D3;
2967 set_pin_power_state(codec, 0x22, &parm);
2968 if (spec->smart51_enabled)
2969 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002970 update_power_state(codec, 0x26, parm);
2971 update_power_state(codec, 0x24, parm);
Lydia Wangbc92df72011-03-23 17:56:05 +08002972 } else if (codec->vendor_id == 0x11064397) {
2973 /* PW7(23h), SW2(27h), AOW2(25h) */
2974 parm = AC_PWRST_D3;
2975 set_pin_power_state(codec, 0x23, &parm);
2976 if (spec->smart51_enabled)
2977 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002978 update_power_state(codec, 0x27, parm);
2979 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002980 }
2981
2982 /* PW 3/4/7 (1ch/1dh/23h) */
2983 parm = AC_PWRST_D3;
2984 /* force to D0 for internal Speaker */
2985 set_pin_power_state(codec, 0x1c, &parm);
2986 set_pin_power_state(codec, 0x1d, &parm);
2987 if (is_8ch)
2988 set_pin_power_state(codec, 0x23, &parm);
2989
2990 /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01002991 update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
2992 update_power_state(codec, 0x10, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002993 if (is_8ch) {
Takashi Iwai054d8672012-01-24 12:25:50 +01002994 update_power_state(codec, 0x25, parm);
2995 update_power_state(codec, 0x27, parm);
Lydia Wangbc92df72011-03-23 17:56:05 +08002996 } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01002997 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002998}
2999
Lydia Wang518bf3b2009-10-10 19:07:29 +08003000static int patch_vt1708S(struct hda_codec *codec);
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003001static int patch_vt1708B(struct hda_codec *codec)
Josepch Chanf7278fd2007-12-13 16:40:40 +01003002{
3003 struct via_spec *spec;
3004 int err;
3005
Lydia Wang518bf3b2009-10-10 19:07:29 +08003006 if (get_codec_type(codec) == VT1708BCE)
3007 return patch_vt1708S(codec);
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003008
Josepch Chanf7278fd2007-12-13 16:40:40 +01003009 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003010 spec = via_new_spec(codec);
Josepch Chanf7278fd2007-12-13 16:40:40 +01003011 if (spec == NULL)
3012 return -ENOMEM;
3013
Takashi Iwai620e2b22011-06-17 17:19:19 +02003014 spec->aa_mix_nid = 0x16;
3015
Josepch Chanf7278fd2007-12-13 16:40:40 +01003016 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003017 err = via_parse_auto_config(codec);
Josepch Chanf7278fd2007-12-13 16:40:40 +01003018 if (err < 0) {
3019 via_free(codec);
3020 return err;
Josepch Chanf7278fd2007-12-13 16:40:40 +01003021 }
3022
Josepch Chanf7278fd2007-12-13 16:40:40 +01003023 codec->patch_ops = via_patch_ops;
3024
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003025 spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
3026
Josepch Chanf7278fd2007-12-13 16:40:40 +01003027 return 0;
3028}
3029
Harald Welted949cac2008-09-09 15:56:01 +08003030/* Patch for VT1708S */
Takashi Iwai096a8852011-06-20 12:09:02 +02003031static const struct hda_verb vt1708S_init_verbs[] = {
Harald Welted7426322008-09-15 22:43:23 +08003032 /* Enable Mic Boost Volume backdoor */
3033 {0x1, 0xf98, 0x1},
Lydia Wangbc7e7e52009-10-10 19:08:32 +08003034 /* don't bybass mixer */
3035 {0x1, 0xf88, 0xc0},
Harald Welted949cac2008-09-09 15:56:01 +08003036 { }
3037};
3038
Takashi Iwai9da29272009-05-07 16:31:14 +02003039/* fill out digital output widgets; one for master and one for slave outputs */
3040static void fill_dig_outs(struct hda_codec *codec)
3041{
3042 struct via_spec *spec = codec->spec;
3043 int i;
3044
3045 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3046 hda_nid_t nid;
3047 int conn;
3048
3049 nid = spec->autocfg.dig_out_pins[i];
3050 if (!nid)
3051 continue;
3052 conn = snd_hda_get_connections(codec, nid, &nid, 1);
3053 if (conn < 1)
3054 continue;
3055 if (!spec->multiout.dig_out_nid)
3056 spec->multiout.dig_out_nid = nid;
3057 else {
3058 spec->slave_dig_outs[0] = nid;
3059 break; /* at most two dig outs */
3060 }
3061 }
3062}
3063
Takashi Iwai12daef62011-06-18 17:45:49 +02003064static void fill_dig_in(struct hda_codec *codec)
Harald Welted949cac2008-09-09 15:56:01 +08003065{
3066 struct via_spec *spec = codec->spec;
Takashi Iwai12daef62011-06-18 17:45:49 +02003067 hda_nid_t dig_nid;
3068 int i, err;
Harald Welted949cac2008-09-09 15:56:01 +08003069
Takashi Iwai12daef62011-06-18 17:45:49 +02003070 if (!spec->autocfg.dig_in_pin)
3071 return;
Harald Welted949cac2008-09-09 15:56:01 +08003072
Takashi Iwai12daef62011-06-18 17:45:49 +02003073 dig_nid = codec->start_nid;
3074 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3075 unsigned int wcaps = get_wcaps(codec, dig_nid);
3076 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3077 continue;
3078 if (!(wcaps & AC_WCAP_DIGITAL))
3079 continue;
3080 if (!(wcaps & AC_WCAP_CONN_LIST))
3081 continue;
3082 err = get_connection_index(codec, dig_nid,
3083 spec->autocfg.dig_in_pin);
3084 if (err >= 0) {
3085 spec->dig_in_nid = dig_nid;
3086 break;
3087 }
3088 }
Harald Welted949cac2008-09-09 15:56:01 +08003089}
3090
Lydia Wang6369bcf2009-10-10 19:08:31 +08003091static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
3092 int offset, int num_steps, int step_size)
3093{
3094 snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
3095 (offset << AC_AMPCAP_OFFSET_SHIFT) |
3096 (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
3097 (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
3098 (0 << AC_AMPCAP_MUTE_SHIFT));
3099}
3100
Harald Welted949cac2008-09-09 15:56:01 +08003101static int patch_vt1708S(struct hda_codec *codec)
3102{
3103 struct via_spec *spec;
3104 int err;
3105
3106 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003107 spec = via_new_spec(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003108 if (spec == NULL)
3109 return -ENOMEM;
3110
Takashi Iwai620e2b22011-06-17 17:19:19 +02003111 spec->aa_mix_nid = 0x16;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003112 override_mic_boost(codec, 0x1a, 0, 3, 40);
3113 override_mic_boost(codec, 0x1e, 0, 3, 40);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003114
Harald Welted949cac2008-09-09 15:56:01 +08003115 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003116 err = via_parse_auto_config(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003117 if (err < 0) {
3118 via_free(codec);
3119 return err;
Harald Welted949cac2008-09-09 15:56:01 +08003120 }
3121
Takashi Iwai096a8852011-06-20 12:09:02 +02003122 spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
Harald Welted949cac2008-09-09 15:56:01 +08003123
Harald Welted949cac2008-09-09 15:56:01 +08003124 codec->patch_ops = via_patch_ops;
3125
Lydia Wang518bf3b2009-10-10 19:07:29 +08003126 /* correct names for VT1708BCE */
3127 if (get_codec_type(codec) == VT1708BCE) {
3128 kfree(codec->chip_name);
3129 codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3130 snprintf(codec->bus->card->mixername,
3131 sizeof(codec->bus->card->mixername),
3132 "%s %s", codec->vendor_name, codec->chip_name);
Lydia Wang970f6302011-03-22 16:25:56 +08003133 }
Lydia Wangbc92df72011-03-23 17:56:05 +08003134 /* correct names for VT1705 */
3135 if (codec->vendor_id == 0x11064397) {
3136 kfree(codec->chip_name);
3137 codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3138 snprintf(codec->bus->card->mixername,
3139 sizeof(codec->bus->card->mixername),
3140 "%s %s", codec->vendor_name, codec->chip_name);
3141 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003142 spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
Harald Welted949cac2008-09-09 15:56:01 +08003143 return 0;
3144}
3145
3146/* Patch for VT1702 */
3147
Takashi Iwai096a8852011-06-20 12:09:02 +02003148static const struct hda_verb vt1702_init_verbs[] = {
Lydia Wangbc7e7e52009-10-10 19:08:32 +08003149 /* mixer enable */
3150 {0x1, 0xF88, 0x3},
3151 /* GPIO 0~2 */
3152 {0x1, 0xF82, 0x3F},
Harald Welted949cac2008-09-09 15:56:01 +08003153 { }
3154};
3155
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003156static void set_widgets_power_state_vt1702(struct hda_codec *codec)
3157{
3158 int imux_is_smixer =
3159 snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3160 unsigned int parm;
3161 /* inputs */
3162 /* PW 1/2/5 (14h/15h/18h) */
3163 parm = AC_PWRST_D3;
3164 set_pin_power_state(codec, 0x14, &parm);
3165 set_pin_power_state(codec, 0x15, &parm);
3166 set_pin_power_state(codec, 0x18, &parm);
3167 if (imux_is_smixer)
3168 parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
3169 /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003170 update_power_state(codec, 0x13, parm);
3171 update_power_state(codec, 0x12, parm);
3172 update_power_state(codec, 0x1f, parm);
3173 update_power_state(codec, 0x20, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003174
3175 /* outputs */
3176 /* PW 3/4 (16h/17h) */
3177 parm = AC_PWRST_D3;
3178 set_pin_power_state(codec, 0x17, &parm);
3179 set_pin_power_state(codec, 0x16, &parm);
3180 /* MW0 (1ah), AOW 0/1 (10h/1dh) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003181 update_power_state(codec, 0x1a, imux_is_smixer ? AC_PWRST_D0 : parm);
3182 update_power_state(codec, 0x10, parm);
3183 update_power_state(codec, 0x1d, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003184}
3185
Harald Welted949cac2008-09-09 15:56:01 +08003186static int patch_vt1702(struct hda_codec *codec)
3187{
3188 struct via_spec *spec;
3189 int err;
Harald Welted949cac2008-09-09 15:56:01 +08003190
3191 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003192 spec = via_new_spec(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003193 if (spec == NULL)
3194 return -ENOMEM;
3195
Takashi Iwai620e2b22011-06-17 17:19:19 +02003196 spec->aa_mix_nid = 0x1a;
3197
Takashi Iwai12daef62011-06-18 17:45:49 +02003198 /* limit AA path volume to 0 dB */
3199 snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
3200 (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
3201 (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
3202 (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
3203 (1 << AC_AMPCAP_MUTE_SHIFT));
3204
Harald Welted949cac2008-09-09 15:56:01 +08003205 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003206 err = via_parse_auto_config(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003207 if (err < 0) {
3208 via_free(codec);
3209 return err;
Harald Welted949cac2008-09-09 15:56:01 +08003210 }
3211
Takashi Iwai096a8852011-06-20 12:09:02 +02003212 spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
Harald Welted949cac2008-09-09 15:56:01 +08003213
Harald Welted949cac2008-09-09 15:56:01 +08003214 codec->patch_ops = via_patch_ops;
3215
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003216 spec->set_widgets_power_state = set_widgets_power_state_vt1702;
Harald Welted949cac2008-09-09 15:56:01 +08003217 return 0;
3218}
3219
Lydia Wangeb7188c2009-10-10 19:08:34 +08003220/* Patch for VT1718S */
3221
Takashi Iwai096a8852011-06-20 12:09:02 +02003222static const struct hda_verb vt1718S_init_verbs[] = {
Lydia Wang4ab2d532011-03-24 12:42:03 +08003223 /* Enable MW0 adjust Gain 5 */
3224 {0x1, 0xfb2, 0x10},
Lydia Wangeb7188c2009-10-10 19:08:34 +08003225 /* Enable Boost Volume backdoor */
3226 {0x1, 0xf88, 0x8},
Takashi Iwai5d417622011-06-20 11:32:27 +02003227
Lydia Wangeb7188c2009-10-10 19:08:34 +08003228 { }
3229};
3230
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003231static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
3232{
3233 struct via_spec *spec = codec->spec;
3234 int imux_is_smixer;
Takashi Iwai61625522012-07-25 13:54:55 +02003235 unsigned int parm, parm2;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003236 /* MUX6 (1eh) = stereo mixer */
3237 imux_is_smixer =
3238 snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
3239 /* inputs */
3240 /* PW 5/6/7 (29h/2ah/2bh) */
3241 parm = AC_PWRST_D3;
3242 set_pin_power_state(codec, 0x29, &parm);
3243 set_pin_power_state(codec, 0x2a, &parm);
3244 set_pin_power_state(codec, 0x2b, &parm);
3245 if (imux_is_smixer)
3246 parm = AC_PWRST_D0;
3247 /* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003248 update_power_state(codec, 0x1e, parm);
3249 update_power_state(codec, 0x1f, parm);
3250 update_power_state(codec, 0x10, parm);
3251 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003252
3253 /* outputs */
3254 /* PW3 (27h), MW2 (1ah), AOW3 (bh) */
3255 parm = AC_PWRST_D3;
3256 set_pin_power_state(codec, 0x27, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003257 update_power_state(codec, 0x1a, parm);
Takashi Iwai61625522012-07-25 13:54:55 +02003258 parm2 = parm; /* for pin 0x0b */
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003259
3260 /* PW2 (26h), AOW2 (ah) */
3261 parm = AC_PWRST_D3;
3262 set_pin_power_state(codec, 0x26, &parm);
3263 if (spec->smart51_enabled)
3264 set_pin_power_state(codec, 0x2b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003265 update_power_state(codec, 0xa, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003266
3267 /* PW0 (24h), AOW0 (8h) */
3268 parm = AC_PWRST_D3;
3269 set_pin_power_state(codec, 0x24, &parm);
3270 if (!spec->hp_independent_mode) /* check for redirected HP */
3271 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003272 update_power_state(codec, 0x8, parm);
Takashi Iwai61625522012-07-25 13:54:55 +02003273 if (!spec->hp_independent_mode && parm2 != AC_PWRST_D3)
3274 parm = parm2;
3275 update_power_state(codec, 0xb, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003276 /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003277 update_power_state(codec, 0x21, imux_is_smixer ? AC_PWRST_D0 : parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003278
3279 /* PW1 (25h), AOW1 (9h) */
3280 parm = AC_PWRST_D3;
3281 set_pin_power_state(codec, 0x25, &parm);
3282 if (spec->smart51_enabled)
3283 set_pin_power_state(codec, 0x2a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003284 update_power_state(codec, 0x9, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003285
3286 if (spec->hp_independent_mode) {
3287 /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
3288 parm = AC_PWRST_D3;
3289 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003290 update_power_state(codec, 0x1b, parm);
3291 update_power_state(codec, 0x34, parm);
3292 update_power_state(codec, 0xc, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003293 }
3294}
3295
Takashi Iwai30b45032011-07-11 17:05:04 +02003296/* Add a connection to the primary DAC from AA-mixer for some codecs
3297 * This isn't listed from the raw info, but the chip has a secret connection.
3298 */
3299static int add_secret_dac_path(struct hda_codec *codec)
3300{
3301 struct via_spec *spec = codec->spec;
3302 int i, nums;
3303 hda_nid_t conn[8];
3304 hda_nid_t nid;
3305
3306 if (!spec->aa_mix_nid)
3307 return 0;
3308 nums = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
3309 ARRAY_SIZE(conn) - 1);
3310 for (i = 0; i < nums; i++) {
3311 if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
3312 return 0;
3313 }
3314
3315 /* find the primary DAC and add to the connection list */
3316 nid = codec->start_nid;
3317 for (i = 0; i < codec->num_nodes; i++, nid++) {
3318 unsigned int caps = get_wcaps(codec, nid);
3319 if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
3320 !(caps & AC_WCAP_DIGITAL)) {
3321 conn[nums++] = nid;
3322 return snd_hda_override_conn_list(codec,
3323 spec->aa_mix_nid,
3324 nums, conn);
3325 }
3326 }
3327 return 0;
3328}
3329
3330
Lydia Wangeb7188c2009-10-10 19:08:34 +08003331static int patch_vt1718S(struct hda_codec *codec)
3332{
3333 struct via_spec *spec;
3334 int err;
3335
3336 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003337 spec = via_new_spec(codec);
Lydia Wangeb7188c2009-10-10 19:08:34 +08003338 if (spec == NULL)
3339 return -ENOMEM;
3340
Takashi Iwai620e2b22011-06-17 17:19:19 +02003341 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003342 override_mic_boost(codec, 0x2b, 0, 3, 40);
3343 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003344 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003345
Lydia Wangeb7188c2009-10-10 19:08:34 +08003346 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003347 err = via_parse_auto_config(codec);
Lydia Wangeb7188c2009-10-10 19:08:34 +08003348 if (err < 0) {
3349 via_free(codec);
3350 return err;
Lydia Wangeb7188c2009-10-10 19:08:34 +08003351 }
3352
Takashi Iwai096a8852011-06-20 12:09:02 +02003353 spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
Lydia Wangeb7188c2009-10-10 19:08:34 +08003354
Lydia Wangeb7188c2009-10-10 19:08:34 +08003355 codec->patch_ops = via_patch_ops;
3356
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003357 spec->set_widgets_power_state = set_widgets_power_state_vt1718S;
3358
Lydia Wangeb7188c2009-10-10 19:08:34 +08003359 return 0;
3360}
Lydia Wangf3db4232009-10-10 19:08:41 +08003361
3362/* Patch for VT1716S */
3363
3364static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
3365 struct snd_ctl_elem_info *uinfo)
3366{
3367 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3368 uinfo->count = 1;
3369 uinfo->value.integer.min = 0;
3370 uinfo->value.integer.max = 1;
3371 return 0;
3372}
3373
3374static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
3375 struct snd_ctl_elem_value *ucontrol)
3376{
3377 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3378 int index = 0;
3379
3380 index = snd_hda_codec_read(codec, 0x26, 0,
3381 AC_VERB_GET_CONNECT_SEL, 0);
3382 if (index != -1)
3383 *ucontrol->value.integer.value = index;
3384
3385 return 0;
3386}
3387
3388static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
3389 struct snd_ctl_elem_value *ucontrol)
3390{
3391 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3392 struct via_spec *spec = codec->spec;
3393 int index = *ucontrol->value.integer.value;
3394
3395 snd_hda_codec_write(codec, 0x26, 0,
3396 AC_VERB_SET_CONNECT_SEL, index);
3397 spec->dmic_enabled = index;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003398 set_widgets_power_state(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003399 return 1;
3400}
3401
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003402static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003403 HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
3404 {
3405 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3406 .name = "Digital Mic Capture Switch",
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003407 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
Lydia Wangf3db4232009-10-10 19:08:41 +08003408 .count = 1,
3409 .info = vt1716s_dmic_info,
3410 .get = vt1716s_dmic_get,
3411 .put = vt1716s_dmic_put,
3412 },
3413 {} /* end */
3414};
3415
3416
3417/* mono-out mixer elements */
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003418static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003419 HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
3420 { } /* end */
3421};
3422
Takashi Iwai096a8852011-06-20 12:09:02 +02003423static const struct hda_verb vt1716S_init_verbs[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003424 /* Enable Boost Volume backdoor */
3425 {0x1, 0xf8a, 0x80},
3426 /* don't bybass mixer */
3427 {0x1, 0xf88, 0xc0},
3428 /* Enable mono output */
3429 {0x1, 0xf90, 0x08},
3430 { }
3431};
3432
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003433static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
3434{
3435 struct via_spec *spec = codec->spec;
3436 int imux_is_smixer;
3437 unsigned int parm;
3438 unsigned int mono_out, present;
3439 /* SW0 (17h) = stereo mixer */
3440 imux_is_smixer =
3441 (snd_hda_codec_read(codec, 0x17, 0,
3442 AC_VERB_GET_CONNECT_SEL, 0x00) == 5);
3443 /* inputs */
3444 /* PW 1/2/5 (1ah/1bh/1eh) */
3445 parm = AC_PWRST_D3;
3446 set_pin_power_state(codec, 0x1a, &parm);
3447 set_pin_power_state(codec, 0x1b, &parm);
3448 set_pin_power_state(codec, 0x1e, &parm);
3449 if (imux_is_smixer)
3450 parm = AC_PWRST_D0;
3451 /* SW0 (17h), AIW0(13h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003452 update_power_state(codec, 0x17, parm);
3453 update_power_state(codec, 0x13, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003454
3455 parm = AC_PWRST_D3;
3456 set_pin_power_state(codec, 0x1e, &parm);
3457 /* PW11 (22h) */
3458 if (spec->dmic_enabled)
3459 set_pin_power_state(codec, 0x22, &parm);
3460 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003461 update_power_state(codec, 0x22, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003462
3463 /* SW2(26h), AIW1(14h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003464 update_power_state(codec, 0x26, parm);
3465 update_power_state(codec, 0x14, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003466
3467 /* outputs */
3468 /* PW0 (19h), SW1 (18h), AOW1 (11h) */
3469 parm = AC_PWRST_D3;
3470 set_pin_power_state(codec, 0x19, &parm);
3471 /* Smart 5.1 PW2(1bh) */
3472 if (spec->smart51_enabled)
3473 set_pin_power_state(codec, 0x1b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003474 update_power_state(codec, 0x18, parm);
3475 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003476
3477 /* PW7 (23h), SW3 (27h), AOW3 (25h) */
3478 parm = AC_PWRST_D3;
3479 set_pin_power_state(codec, 0x23, &parm);
3480 /* Smart 5.1 PW1(1ah) */
3481 if (spec->smart51_enabled)
3482 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003483 update_power_state(codec, 0x27, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003484
3485 /* Smart 5.1 PW5(1eh) */
3486 if (spec->smart51_enabled)
3487 set_pin_power_state(codec, 0x1e, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003488 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003489
3490 /* Mono out */
3491 /* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
3492 present = snd_hda_jack_detect(codec, 0x1c);
3493
3494 if (present)
3495 mono_out = 0;
3496 else {
3497 present = snd_hda_jack_detect(codec, 0x1d);
3498 if (!spec->hp_independent_mode && present)
3499 mono_out = 0;
3500 else
3501 mono_out = 1;
3502 }
3503 parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
Takashi Iwai054d8672012-01-24 12:25:50 +01003504 update_power_state(codec, 0x28, parm);
3505 update_power_state(codec, 0x29, parm);
3506 update_power_state(codec, 0x2a, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003507
3508 /* PW 3/4 (1ch/1dh) */
3509 parm = AC_PWRST_D3;
3510 set_pin_power_state(codec, 0x1c, &parm);
3511 set_pin_power_state(codec, 0x1d, &parm);
3512 /* HP Independent Mode, power on AOW3 */
3513 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003514 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003515
3516 /* force to D0 for internal Speaker */
3517 /* MW0 (16h), AOW0 (10h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003518 update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
3519 update_power_state(codec, 0x10, mono_out ? AC_PWRST_D0 : parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003520}
3521
Lydia Wangf3db4232009-10-10 19:08:41 +08003522static int patch_vt1716S(struct hda_codec *codec)
3523{
3524 struct via_spec *spec;
3525 int err;
3526
3527 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003528 spec = via_new_spec(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003529 if (spec == NULL)
3530 return -ENOMEM;
3531
Takashi Iwai620e2b22011-06-17 17:19:19 +02003532 spec->aa_mix_nid = 0x16;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003533 override_mic_boost(codec, 0x1a, 0, 3, 40);
3534 override_mic_boost(codec, 0x1e, 0, 3, 40);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003535
Lydia Wangf3db4232009-10-10 19:08:41 +08003536 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003537 err = via_parse_auto_config(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003538 if (err < 0) {
3539 via_free(codec);
3540 return err;
Lydia Wangf3db4232009-10-10 19:08:41 +08003541 }
3542
Takashi Iwai096a8852011-06-20 12:09:02 +02003543 spec->init_verbs[spec->num_iverbs++] = vt1716S_init_verbs;
Lydia Wangf3db4232009-10-10 19:08:41 +08003544
Lydia Wangf3db4232009-10-10 19:08:41 +08003545 spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
3546 spec->num_mixers++;
3547
3548 spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3549
3550 codec->patch_ops = via_patch_ops;
3551
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003552 spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
Lydia Wangf3db4232009-10-10 19:08:41 +08003553 return 0;
3554}
Lydia Wang25eaba22009-10-10 19:08:43 +08003555
3556/* for vt2002P */
3557
Takashi Iwai096a8852011-06-20 12:09:02 +02003558static const struct hda_verb vt2002P_init_verbs[] = {
Lydia Wangeadb9a82011-03-24 12:43:02 +08003559 /* Class-D speaker related verbs */
3560 {0x1, 0xfe0, 0x4},
3561 {0x1, 0xfe9, 0x80},
3562 {0x1, 0xfe2, 0x22},
Lydia Wang25eaba22009-10-10 19:08:43 +08003563 /* Enable Boost Volume backdoor */
3564 {0x1, 0xfb9, 0x24},
Lydia Wang25eaba22009-10-10 19:08:43 +08003565 /* Enable AOW0 to MW9 */
3566 {0x1, 0xfb8, 0x88},
3567 { }
3568};
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003569
Takashi Iwai096a8852011-06-20 12:09:02 +02003570static const struct hda_verb vt1802_init_verbs[] = {
Lydia Wang118909562011-03-23 17:57:34 +08003571 /* Enable Boost Volume backdoor */
3572 {0x1, 0xfb9, 0x24},
Lydia Wang118909562011-03-23 17:57:34 +08003573 /* Enable AOW0 to MW9 */
3574 {0x1, 0xfb8, 0x88},
3575 { }
3576};
Lydia Wang25eaba22009-10-10 19:08:43 +08003577
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003578static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
3579{
3580 struct via_spec *spec = codec->spec;
3581 int imux_is_smixer;
3582 unsigned int parm;
3583 unsigned int present;
3584 /* MUX9 (1eh) = stereo mixer */
3585 imux_is_smixer =
3586 snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3587 /* inputs */
3588 /* PW 5/6/7 (29h/2ah/2bh) */
3589 parm = AC_PWRST_D3;
3590 set_pin_power_state(codec, 0x29, &parm);
3591 set_pin_power_state(codec, 0x2a, &parm);
3592 set_pin_power_state(codec, 0x2b, &parm);
3593 parm = AC_PWRST_D0;
3594 /* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003595 update_power_state(codec, 0x1e, parm);
3596 update_power_state(codec, 0x1f, parm);
3597 update_power_state(codec, 0x10, parm);
3598 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003599
3600 /* outputs */
3601 /* AOW0 (8h)*/
Takashi Iwai054d8672012-01-24 12:25:50 +01003602 update_power_state(codec, 0x8, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003603
Lydia Wang118909562011-03-23 17:57:34 +08003604 if (spec->codec_type == VT1802) {
3605 /* PW4 (28h), MW4 (18h), MUX4(38h) */
3606 parm = AC_PWRST_D3;
3607 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003608 update_power_state(codec, 0x18, parm);
3609 update_power_state(codec, 0x38, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003610 } else {
3611 /* PW4 (26h), MW4 (1ch), MUX4(37h) */
3612 parm = AC_PWRST_D3;
3613 set_pin_power_state(codec, 0x26, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003614 update_power_state(codec, 0x1c, parm);
3615 update_power_state(codec, 0x37, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003616 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003617
Lydia Wang118909562011-03-23 17:57:34 +08003618 if (spec->codec_type == VT1802) {
3619 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3620 parm = AC_PWRST_D3;
3621 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003622 update_power_state(codec, 0x15, parm);
3623 update_power_state(codec, 0x35, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003624 } else {
3625 /* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
3626 parm = AC_PWRST_D3;
3627 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003628 update_power_state(codec, 0x19, parm);
3629 update_power_state(codec, 0x35, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003630 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003631
3632 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003633 update_power_state(codec, 0x9, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003634
3635 /* Class-D */
3636 /* PW0 (24h), MW0(18h/14h), MUX0(34h) */
3637 present = snd_hda_jack_detect(codec, 0x25);
3638
3639 parm = AC_PWRST_D3;
3640 set_pin_power_state(codec, 0x24, &parm);
3641 parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
Lydia Wang118909562011-03-23 17:57:34 +08003642 if (spec->codec_type == VT1802)
Takashi Iwai054d8672012-01-24 12:25:50 +01003643 update_power_state(codec, 0x14, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003644 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003645 update_power_state(codec, 0x18, parm);
3646 update_power_state(codec, 0x34, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003647
3648 /* Mono Out */
3649 present = snd_hda_jack_detect(codec, 0x26);
3650
3651 parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
Lydia Wang118909562011-03-23 17:57:34 +08003652 if (spec->codec_type == VT1802) {
3653 /* PW15 (33h), MW8(1ch), MUX8(3ch) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003654 update_power_state(codec, 0x33, parm);
3655 update_power_state(codec, 0x1c, parm);
3656 update_power_state(codec, 0x3c, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003657 } else {
3658 /* PW15 (31h), MW8(17h), MUX8(3bh) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003659 update_power_state(codec, 0x31, parm);
3660 update_power_state(codec, 0x17, parm);
3661 update_power_state(codec, 0x3b, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003662 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003663 /* MW9 (21h) */
3664 if (imux_is_smixer || !is_aa_path_mute(codec))
Takashi Iwai054d8672012-01-24 12:25:50 +01003665 update_power_state(codec, 0x21, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003666 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003667 update_power_state(codec, 0x21, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003668}
Lydia Wang25eaba22009-10-10 19:08:43 +08003669
David Henningsson4b527b62012-09-18 14:26:59 +02003670/*
3671 * pin fix-up
3672 */
3673enum {
3674 VIA_FIXUP_INTMIC_BOOST,
3675};
3676
3677static void via_fixup_intmic_boost(struct hda_codec *codec,
3678 const struct hda_fixup *fix, int action)
3679{
3680 if (action == HDA_FIXUP_ACT_PRE_PROBE)
3681 override_mic_boost(codec, 0x30, 0, 2, 40);
3682}
3683
3684static const struct hda_fixup via_fixups[] = {
3685 [VIA_FIXUP_INTMIC_BOOST] = {
3686 .type = HDA_FIXUP_FUNC,
3687 .v.func = via_fixup_intmic_boost,
3688 },
3689};
3690
3691static const struct snd_pci_quirk vt2002p_fixups[] = {
3692 SND_PCI_QUIRK(0x1043, 0x8532, "Asus X202E", VIA_FIXUP_INTMIC_BOOST),
3693 {}
3694};
3695
Lydia Wang25eaba22009-10-10 19:08:43 +08003696/* patch for vt2002P */
3697static int patch_vt2002P(struct hda_codec *codec)
3698{
3699 struct via_spec *spec;
3700 int err;
3701
3702 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003703 spec = via_new_spec(codec);
Lydia Wang25eaba22009-10-10 19:08:43 +08003704 if (spec == NULL)
3705 return -ENOMEM;
3706
Takashi Iwai620e2b22011-06-17 17:19:19 +02003707 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003708 override_mic_boost(codec, 0x2b, 0, 3, 40);
3709 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003710 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003711
David Henningsson4b527b62012-09-18 14:26:59 +02003712 snd_hda_pick_fixup(codec, NULL, vt2002p_fixups, via_fixups);
3713 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
3714
Lydia Wang25eaba22009-10-10 19:08:43 +08003715 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003716 err = via_parse_auto_config(codec);
Lydia Wang25eaba22009-10-10 19:08:43 +08003717 if (err < 0) {
3718 via_free(codec);
3719 return err;
Lydia Wang25eaba22009-10-10 19:08:43 +08003720 }
3721
Lydia Wang118909562011-03-23 17:57:34 +08003722 if (spec->codec_type == VT1802)
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003723 spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
Lydia Wang118909562011-03-23 17:57:34 +08003724 else
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003725 spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
Lydia Wang118909562011-03-23 17:57:34 +08003726
Lydia Wang25eaba22009-10-10 19:08:43 +08003727 codec->patch_ops = via_patch_ops;
3728
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003729 spec->set_widgets_power_state = set_widgets_power_state_vt2002P;
Lydia Wang25eaba22009-10-10 19:08:43 +08003730 return 0;
3731}
Lydia Wangab6734e2009-10-10 19:08:46 +08003732
3733/* for vt1812 */
3734
Takashi Iwai096a8852011-06-20 12:09:02 +02003735static const struct hda_verb vt1812_init_verbs[] = {
Lydia Wangab6734e2009-10-10 19:08:46 +08003736 /* Enable Boost Volume backdoor */
3737 {0x1, 0xfb9, 0x24},
Lydia Wangab6734e2009-10-10 19:08:46 +08003738 /* Enable AOW0 to MW9 */
3739 {0x1, 0xfb8, 0xa8},
3740 { }
3741};
3742
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003743static void set_widgets_power_state_vt1812(struct hda_codec *codec)
3744{
3745 struct via_spec *spec = codec->spec;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003746 unsigned int parm;
3747 unsigned int present;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003748 /* inputs */
3749 /* PW 5/6/7 (29h/2ah/2bh) */
3750 parm = AC_PWRST_D3;
3751 set_pin_power_state(codec, 0x29, &parm);
3752 set_pin_power_state(codec, 0x2a, &parm);
3753 set_pin_power_state(codec, 0x2b, &parm);
3754 parm = AC_PWRST_D0;
3755 /* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003756 update_power_state(codec, 0x1e, parm);
3757 update_power_state(codec, 0x1f, parm);
3758 update_power_state(codec, 0x10, parm);
3759 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003760
3761 /* outputs */
3762 /* AOW0 (8h)*/
Takashi Iwai054d8672012-01-24 12:25:50 +01003763 update_power_state(codec, 0x8, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003764
3765 /* PW4 (28h), MW4 (18h), MUX4(38h) */
3766 parm = AC_PWRST_D3;
3767 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003768 update_power_state(codec, 0x18, parm);
3769 update_power_state(codec, 0x38, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003770
3771 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3772 parm = AC_PWRST_D3;
3773 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003774 update_power_state(codec, 0x15, parm);
3775 update_power_state(codec, 0x35, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003776 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003777 update_power_state(codec, 0x9, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003778
3779 /* Internal Speaker */
3780 /* PW0 (24h), MW0(14h), MUX0(34h) */
3781 present = snd_hda_jack_detect(codec, 0x25);
3782
3783 parm = AC_PWRST_D3;
3784 set_pin_power_state(codec, 0x24, &parm);
3785 if (present) {
Takashi Iwai054d8672012-01-24 12:25:50 +01003786 update_power_state(codec, 0x14, AC_PWRST_D3);
3787 update_power_state(codec, 0x34, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003788 } else {
Takashi Iwai054d8672012-01-24 12:25:50 +01003789 update_power_state(codec, 0x14, AC_PWRST_D0);
3790 update_power_state(codec, 0x34, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003791 }
3792
3793
3794 /* Mono Out */
3795 /* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
3796 present = snd_hda_jack_detect(codec, 0x28);
3797
3798 parm = AC_PWRST_D3;
3799 set_pin_power_state(codec, 0x31, &parm);
3800 if (present) {
Takashi Iwai054d8672012-01-24 12:25:50 +01003801 update_power_state(codec, 0x1c, AC_PWRST_D3);
3802 update_power_state(codec, 0x3c, AC_PWRST_D3);
3803 update_power_state(codec, 0x3e, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003804 } else {
Takashi Iwai054d8672012-01-24 12:25:50 +01003805 update_power_state(codec, 0x1c, AC_PWRST_D0);
3806 update_power_state(codec, 0x3c, AC_PWRST_D0);
3807 update_power_state(codec, 0x3e, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003808 }
3809
3810 /* PW15 (33h), MW15 (1dh), MUX15(3dh) */
3811 parm = AC_PWRST_D3;
3812 set_pin_power_state(codec, 0x33, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003813 update_power_state(codec, 0x1d, parm);
3814 update_power_state(codec, 0x3d, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003815
3816}
Lydia Wangab6734e2009-10-10 19:08:46 +08003817
3818/* patch for vt1812 */
3819static int patch_vt1812(struct hda_codec *codec)
3820{
3821 struct via_spec *spec;
3822 int err;
3823
3824 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003825 spec = via_new_spec(codec);
Lydia Wangab6734e2009-10-10 19:08:46 +08003826 if (spec == NULL)
3827 return -ENOMEM;
3828
Takashi Iwai620e2b22011-06-17 17:19:19 +02003829 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003830 override_mic_boost(codec, 0x2b, 0, 3, 40);
3831 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003832 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003833
Lydia Wangab6734e2009-10-10 19:08:46 +08003834 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003835 err = via_parse_auto_config(codec);
Lydia Wangab6734e2009-10-10 19:08:46 +08003836 if (err < 0) {
3837 via_free(codec);
3838 return err;
Lydia Wangab6734e2009-10-10 19:08:46 +08003839 }
3840
Takashi Iwai096a8852011-06-20 12:09:02 +02003841 spec->init_verbs[spec->num_iverbs++] = vt1812_init_verbs;
Lydia Wangab6734e2009-10-10 19:08:46 +08003842
Lydia Wangab6734e2009-10-10 19:08:46 +08003843 codec->patch_ops = via_patch_ops;
3844
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003845 spec->set_widgets_power_state = set_widgets_power_state_vt1812;
Lydia Wangab6734e2009-10-10 19:08:46 +08003846 return 0;
3847}
3848
Joseph Chanc577b8a2006-11-29 15:29:40 +01003849/*
3850 * patch entries
3851 */
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003852static const struct hda_codec_preset snd_hda_preset_via[] = {
Takashi Iwai3218c172008-12-18 09:17:56 +01003853 { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
3854 { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
3855 { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
3856 { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
3857 { .id = 0x1106e710, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003858 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003859 { .id = 0x1106e711, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003860 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003861 { .id = 0x1106e712, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003862 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003863 { .id = 0x1106e713, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003864 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003865 { .id = 0x1106e714, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003866 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003867 { .id = 0x1106e715, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003868 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003869 { .id = 0x1106e716, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003870 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003871 { .id = 0x1106e717, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003872 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003873 { .id = 0x1106e720, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003874 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003875 { .id = 0x1106e721, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003876 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003877 { .id = 0x1106e722, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003878 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003879 { .id = 0x1106e723, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003880 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003881 { .id = 0x1106e724, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003882 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003883 { .id = 0x1106e725, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003884 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003885 { .id = 0x1106e726, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003886 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003887 { .id = 0x1106e727, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003888 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003889 { .id = 0x11060397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003890 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003891 { .id = 0x11061397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003892 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003893 { .id = 0x11062397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003894 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003895 { .id = 0x11063397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003896 .patch = patch_vt1708S},
Lydia Wangbc92df72011-03-23 17:56:05 +08003897 { .id = 0x11064397, .name = "VT1705",
Harald Welted949cac2008-09-09 15:56:01 +08003898 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003899 { .id = 0x11065397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003900 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003901 { .id = 0x11066397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003902 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003903 { .id = 0x11067397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003904 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003905 { .id = 0x11060398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003906 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003907 { .id = 0x11061398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003908 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003909 { .id = 0x11062398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003910 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003911 { .id = 0x11063398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003912 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003913 { .id = 0x11064398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003914 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003915 { .id = 0x11065398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003916 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003917 { .id = 0x11066398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003918 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003919 { .id = 0x11067398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003920 .patch = patch_vt1702},
Lydia Wangeb7188c2009-10-10 19:08:34 +08003921 { .id = 0x11060428, .name = "VT1718S",
3922 .patch = patch_vt1718S},
3923 { .id = 0x11064428, .name = "VT1718S",
3924 .patch = patch_vt1718S},
Lydia Wangbb3c6bfc2009-10-10 19:08:39 +08003925 { .id = 0x11060441, .name = "VT2020",
3926 .patch = patch_vt1718S},
3927 { .id = 0x11064441, .name = "VT1828S",
3928 .patch = patch_vt1718S},
Lydia Wangf3db4232009-10-10 19:08:41 +08003929 { .id = 0x11060433, .name = "VT1716S",
3930 .patch = patch_vt1716S},
3931 { .id = 0x1106a721, .name = "VT1716S",
3932 .patch = patch_vt1716S},
Lydia Wang25eaba22009-10-10 19:08:43 +08003933 { .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
3934 { .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
Lydia Wangab6734e2009-10-10 19:08:46 +08003935 { .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
Lydia Wang36dd5c42009-10-20 13:18:04 +08003936 { .id = 0x11060440, .name = "VT1818S",
3937 .patch = patch_vt1708S},
Lydia Wang118909562011-03-23 17:57:34 +08003938 { .id = 0x11060446, .name = "VT1802",
3939 .patch = patch_vt2002P},
3940 { .id = 0x11068446, .name = "VT1802",
3941 .patch = patch_vt2002P},
Joseph Chanc577b8a2006-11-29 15:29:40 +01003942 {} /* terminator */
3943};
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003944
3945MODULE_ALIAS("snd-hda-codec-id:1106*");
3946
3947static struct hda_codec_preset_list via_list = {
3948 .preset = snd_hda_preset_via,
3949 .owner = THIS_MODULE,
3950};
3951
3952MODULE_LICENSE("GPL");
3953MODULE_DESCRIPTION("VIA HD-audio codec");
3954
3955static int __init patch_via_init(void)
3956{
3957 return snd_hda_add_codec_preset(&via_list);
3958}
3959
3960static void __exit patch_via_exit(void)
3961{
3962 snd_hda_delete_codec_preset(&via_list);
3963}
3964
3965module_init(patch_via_init)
3966module_exit(patch_via_exit)