blob: 72a2f60b087c8ba066b52e5aaf4cda500227471f [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 {
Takashi Iwai7819d1c2012-10-10 08:41:42 +0200121 struct hda_gen_spec gen;
122
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800123 /* codec parameterization */
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200124 const struct snd_kcontrol_new *mixers[6];
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800125 unsigned int num_mixers;
126
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200127 const struct hda_verb *init_verbs[5];
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800128 unsigned int num_iverbs;
129
Takashi Iwai82673bc2011-06-17 16:24:21 +0200130 char stream_name_analog[32];
Takashi Iwai7eb56e82011-06-18 16:40:14 +0200131 char stream_name_hp[32];
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200132 const struct hda_pcm_stream *stream_analog_playback;
133 const struct hda_pcm_stream *stream_analog_capture;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800134
Takashi Iwai82673bc2011-06-17 16:24:21 +0200135 char stream_name_digital[32];
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200136 const struct hda_pcm_stream *stream_digital_playback;
137 const struct hda_pcm_stream *stream_digital_capture;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800138
139 /* playback */
140 struct hda_multi_out multiout;
141 hda_nid_t slave_dig_outs[2];
Takashi Iwaiece8d042011-06-19 16:24:21 +0200142 hda_nid_t hp_dac_nid;
Takashi Iwai3214b962011-07-18 12:49:25 +0200143 hda_nid_t speaker_dac_nid;
144 int hp_indep_shared; /* indep HP-DAC is shared with side ch */
Takashi Iwai3b607e32011-07-18 16:54:40 +0200145 int opened_streams; /* STREAM_* bits */
146 int active_streams; /* STREAM_* bits */
Takashi Iwai3214b962011-07-18 12:49:25 +0200147 int aamix_mode; /* loopback is enabled for output-path? */
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800148
Takashi Iwai3214b962011-07-18 12:49:25 +0200149 /* Output-paths:
150 * There are different output-paths depending on the setup.
151 * out_path, hp_path and speaker_path are primary paths. If both
152 * direct DAC and aa-loopback routes are available, these contain
153 * the former paths. Meanwhile *_mix_path contain the paths with
154 * loopback mixer. (Since the loopback is only for front channel,
155 * no out_mix_path for surround channels.)
156 * The HP output has another path, hp_indep_path, which is used in
157 * the independent-HP mode.
158 */
Takashi Iwaide6c74f2011-07-04 14:46:42 +0200159 struct nid_path out_path[HDA_SIDE + 1];
Takashi Iwai3214b962011-07-18 12:49:25 +0200160 struct nid_path out_mix_path;
Takashi Iwai4a796162011-06-17 17:53:38 +0200161 struct nid_path hp_path;
Takashi Iwai3214b962011-07-18 12:49:25 +0200162 struct nid_path hp_mix_path;
163 struct nid_path hp_indep_path;
Takashi Iwai4a918ff2011-06-20 12:39:26 +0200164 struct nid_path speaker_path;
Takashi Iwai3214b962011-07-18 12:49:25 +0200165 struct nid_path speaker_mix_path;
Takashi Iwai4a796162011-06-17 17:53:38 +0200166
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800167 /* capture */
168 unsigned int num_adc_nids;
Takashi Iwaide6c74f2011-07-04 14:46:42 +0200169 hda_nid_t adc_nids[VIA_MAX_ADCS];
170 hda_nid_t mux_nids[VIA_MAX_ADCS];
Takashi Iwai620e2b22011-06-17 17:19:19 +0200171 hda_nid_t aa_mix_nid;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800172 hda_nid_t dig_in_nid;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800173
174 /* capture source */
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200175 bool dyn_adc_switch;
176 int num_inputs;
177 struct via_input inputs[AUTO_CFG_MAX_INS + 1];
Takashi Iwaide6c74f2011-07-04 14:46:42 +0200178 unsigned int cur_mux[VIA_MAX_ADCS];
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800179
Takashi Iwai3b607e32011-07-18 16:54:40 +0200180 /* dynamic DAC switching */
181 unsigned int cur_dac_stream_tag;
182 unsigned int cur_dac_format;
183 unsigned int cur_hp_stream_tag;
184 unsigned int cur_hp_format;
185
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200186 /* dynamic ADC switching */
187 hda_nid_t cur_adc;
188 unsigned int cur_adc_stream_tag;
189 unsigned int cur_adc_format;
190
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800191 /* PCM information */
192 struct hda_pcm pcm_rec[3];
193
194 /* dynamic controls, init_verbs and input_mux */
195 struct auto_pin_cfg autocfg;
196 struct snd_array kctls;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800197 hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
198
199 /* HP mode source */
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800200 unsigned int hp_independent_mode;
Lydia Wangf3db4232009-10-10 19:08:41 +0800201 unsigned int dmic_enabled;
Takashi Iwai24088a52011-06-17 16:59:21 +0200202 unsigned int no_pin_power_ctl;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800203 enum VIA_HDA_CODEC codec_type;
204
Takashi Iwaie9d010c2012-02-01 10:33:23 +0100205 /* analog low-power control */
206 bool alc_mode;
207
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200208 /* smart51 setup */
209 unsigned int smart51_nums;
210 hda_nid_t smart51_pins[2];
211 int smart51_idxs[2];
212 const char *smart51_labels[2];
213 unsigned int smart51_enabled;
214
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800215 /* work to check hp jack state */
216 struct hda_codec *codec;
217 struct delayed_work vt1708_hp_work;
Takashi Iwai187d3332011-11-24 16:33:09 +0100218 int hp_work_active;
Takashi Iwaie06e5a22011-06-17 15:46:13 +0200219 int vt1708_jack_detect;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800220 int vt1708_hp_present;
Lydia Wang3e95b9a2011-03-23 15:13:28 +0800221
222 void (*set_widgets_power_state)(struct hda_codec *codec);
223
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800224 struct hda_loopback_check loopback;
Takashi Iwai13af8e72011-06-20 14:05:46 +0200225 int num_loopbacks;
226 struct hda_amp_list loopback_list[8];
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200227
228 /* bind capture-volume */
229 struct hda_bind_ctls *bind_cap_vol;
230 struct hda_bind_ctls *bind_cap_sw;
Takashi Iwai3b607e32011-07-18 16:54:40 +0200231
232 struct mutex config_mutex;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800233};
234
Lydia Wang0341ccd2011-03-22 16:25:03 +0800235static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100236static struct via_spec * via_new_spec(struct hda_codec *codec)
237{
238 struct via_spec *spec;
239
240 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
241 if (spec == NULL)
242 return NULL;
243
Takashi Iwai3b607e32011-07-18 16:54:40 +0200244 mutex_init(&spec->config_mutex);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100245 codec->spec = spec;
246 spec->codec = codec;
Lydia Wang0341ccd2011-03-22 16:25:03 +0800247 spec->codec_type = get_codec_type(codec);
248 /* VT1708BCE & VT1708S are almost same */
249 if (spec->codec_type == VT1708BCE)
250 spec->codec_type = VT1708S;
Takashi Iwai7819d1c2012-10-10 08:41:42 +0200251 snd_hda_gen_init(&spec->gen);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100252 return spec;
253}
254
Lydia Wang744ff5f2009-10-10 19:07:26 +0800255static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
Harald Welted7426322008-09-15 22:43:23 +0800256{
Lydia Wang744ff5f2009-10-10 19:07:26 +0800257 u32 vendor_id = codec->vendor_id;
Harald Welted7426322008-09-15 22:43:23 +0800258 u16 ven_id = vendor_id >> 16;
259 u16 dev_id = vendor_id & 0xffff;
260 enum VIA_HDA_CODEC codec_type;
261
262 /* get codec type */
263 if (ven_id != 0x1106)
264 codec_type = UNKNOWN;
265 else if (dev_id >= 0x1708 && dev_id <= 0x170b)
266 codec_type = VT1708;
267 else if (dev_id >= 0xe710 && dev_id <= 0xe713)
268 codec_type = VT1709_10CH;
269 else if (dev_id >= 0xe714 && dev_id <= 0xe717)
270 codec_type = VT1709_6CH;
Lydia Wang518bf3b2009-10-10 19:07:29 +0800271 else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
Harald Welted7426322008-09-15 22:43:23 +0800272 codec_type = VT1708B_8CH;
Lydia Wang518bf3b2009-10-10 19:07:29 +0800273 if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
274 codec_type = VT1708BCE;
275 } else if (dev_id >= 0xe724 && dev_id <= 0xe727)
Harald Welted7426322008-09-15 22:43:23 +0800276 codec_type = VT1708B_4CH;
277 else if ((dev_id & 0xfff) == 0x397
278 && (dev_id >> 12) < 8)
279 codec_type = VT1708S;
280 else if ((dev_id & 0xfff) == 0x398
281 && (dev_id >> 12) < 8)
282 codec_type = VT1702;
Lydia Wangeb7188c2009-10-10 19:08:34 +0800283 else if ((dev_id & 0xfff) == 0x428
284 && (dev_id >> 12) < 8)
285 codec_type = VT1718S;
Lydia Wangf3db4232009-10-10 19:08:41 +0800286 else if (dev_id == 0x0433 || dev_id == 0xa721)
287 codec_type = VT1716S;
Lydia Wangbb3c6bfc2009-10-10 19:08:39 +0800288 else if (dev_id == 0x0441 || dev_id == 0x4441)
289 codec_type = VT1718S;
Lydia Wang25eaba22009-10-10 19:08:43 +0800290 else if (dev_id == 0x0438 || dev_id == 0x4438)
291 codec_type = VT2002P;
Lydia Wangab6734e2009-10-10 19:08:46 +0800292 else if (dev_id == 0x0448)
293 codec_type = VT1812;
Lydia Wang36dd5c42009-10-20 13:18:04 +0800294 else if (dev_id == 0x0440)
295 codec_type = VT1708S;
Lydia Wang118909562011-03-23 17:57:34 +0800296 else if ((dev_id & 0xfff) == 0x446)
297 codec_type = VT1802;
Harald Welted7426322008-09-15 22:43:23 +0800298 else
299 codec_type = UNKNOWN;
300 return codec_type;
301};
302
Lydia Wangec7e7e42011-03-24 12:43:44 +0800303#define VIA_JACK_EVENT 0x20
Harald Welte69e52a82008-09-09 15:57:32 +0800304#define VIA_HP_EVENT 0x01
Takashi Iwai4a918ff2011-06-20 12:39:26 +0200305#define VIA_LINE_EVENT 0x03
Harald Welte69e52a82008-09-09 15:57:32 +0800306
Joseph Chanc577b8a2006-11-29 15:29:40 +0100307enum {
308 VIA_CTL_WIDGET_VOL,
309 VIA_CTL_WIDGET_MUTE,
Lydia Wangf5271102009-10-10 19:07:35 +0800310 VIA_CTL_WIDGET_ANALOG_MUTE,
Joseph Chanc577b8a2006-11-29 15:29:40 +0100311};
312
Takashi Iwaiada509e2011-06-20 15:40:19 +0200313static void analog_low_current_mode(struct hda_codec *codec);
314static bool is_aa_path_mute(struct hda_codec *codec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800315
Takashi Iwai187d3332011-11-24 16:33:09 +0100316#define hp_detect_with_aa(codec) \
317 (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1 && \
318 !is_aa_path_mute(codec))
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800319
320static void vt1708_stop_hp_work(struct via_spec *spec)
321{
322 if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
323 return;
Takashi Iwai187d3332011-11-24 16:33:09 +0100324 if (spec->hp_work_active) {
325 snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 1);
326 cancel_delayed_work_sync(&spec->vt1708_hp_work);
327 spec->hp_work_active = 0;
328 }
329}
330
331static void vt1708_update_hp_work(struct via_spec *spec)
332{
333 if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800334 return;
Takashi Iwai187d3332011-11-24 16:33:09 +0100335 if (spec->vt1708_jack_detect &&
336 (spec->active_streams || hp_detect_with_aa(spec->codec))) {
337 if (!spec->hp_work_active) {
338 snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 0);
339 schedule_delayed_work(&spec->vt1708_hp_work,
340 msecs_to_jiffies(100));
341 spec->hp_work_active = 1;
342 }
343 } else if (!hp_detect_with_aa(spec->codec))
344 vt1708_stop_hp_work(spec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800345}
Lydia Wangf5271102009-10-10 19:07:35 +0800346
Lydia Wang3e95b9a2011-03-23 15:13:28 +0800347static void set_widgets_power_state(struct hda_codec *codec)
348{
349 struct via_spec *spec = codec->spec;
350 if (spec->set_widgets_power_state)
351 spec->set_widgets_power_state(codec);
352}
Lydia Wang25eaba22009-10-10 19:08:43 +0800353
Lydia Wangf5271102009-10-10 19:07:35 +0800354static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
355 struct snd_ctl_elem_value *ucontrol)
356{
357 int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
358 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
359
Lydia Wang3e95b9a2011-03-23 15:13:28 +0800360 set_widgets_power_state(codec);
Takashi Iwaiada509e2011-06-20 15:40:19 +0200361 analog_low_current_mode(snd_kcontrol_chip(kcontrol));
Takashi Iwai187d3332011-11-24 16:33:09 +0100362 vt1708_update_hp_work(codec->spec);
Lydia Wangf5271102009-10-10 19:07:35 +0800363 return change;
364}
365
366/* modify .put = snd_hda_mixer_amp_switch_put */
367#define ANALOG_INPUT_MUTE \
368 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
369 .name = NULL, \
370 .index = 0, \
371 .info = snd_hda_mixer_amp_switch_info, \
372 .get = snd_hda_mixer_amp_switch_get, \
373 .put = analog_input_switch_put, \
374 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
375
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200376static const struct snd_kcontrol_new via_control_templates[] = {
Joseph Chanc577b8a2006-11-29 15:29:40 +0100377 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
378 HDA_CODEC_MUTE(NULL, 0, 0, 0),
Lydia Wangf5271102009-10-10 19:07:35 +0800379 ANALOG_INPUT_MUTE,
Joseph Chanc577b8a2006-11-29 15:29:40 +0100380};
381
Lydia Wangab6734e2009-10-10 19:08:46 +0800382
Joseph Chanc577b8a2006-11-29 15:29:40 +0100383/* add dynamic controls */
Takashi Iwai291c9e32011-06-17 16:15:26 +0200384static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
385 const struct snd_kcontrol_new *tmpl,
386 const char *name)
Joseph Chanc577b8a2006-11-29 15:29:40 +0100387{
388 struct snd_kcontrol_new *knew;
389
Takashi Iwai603c4012008-07-30 15:01:44 +0200390 snd_array_init(&spec->kctls, sizeof(*knew), 32);
391 knew = snd_array_new(&spec->kctls);
392 if (!knew)
Takashi Iwai291c9e32011-06-17 16:15:26 +0200393 return NULL;
394 *knew = *tmpl;
395 if (!name)
396 name = tmpl->name;
397 if (name) {
398 knew->name = kstrdup(name, GFP_KERNEL);
399 if (!knew->name)
400 return NULL;
401 }
402 return knew;
403}
404
405static int __via_add_control(struct via_spec *spec, int type, const char *name,
406 int idx, unsigned long val)
407{
408 struct snd_kcontrol_new *knew;
409
410 knew = __via_clone_ctl(spec, &via_control_templates[type], name);
411 if (!knew)
Joseph Chanc577b8a2006-11-29 15:29:40 +0100412 return -ENOMEM;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +0200413 knew->index = idx;
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +0100414 if (get_amp_nid_(val))
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100415 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100416 knew->private_value = val;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100417 return 0;
418}
419
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200420#define via_add_control(spec, type, name, val) \
421 __via_add_control(spec, type, name, 0, val)
422
Takashi Iwai291c9e32011-06-17 16:15:26 +0200423#define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100424
Takashi Iwai603c4012008-07-30 15:01:44 +0200425static void via_free_kctls(struct hda_codec *codec)
426{
427 struct via_spec *spec = codec->spec;
428
429 if (spec->kctls.list) {
430 struct snd_kcontrol_new *kctl = spec->kctls.list;
431 int i;
432 for (i = 0; i < spec->kctls.used; i++)
433 kfree(kctl[i].name);
434 }
435 snd_array_free(&spec->kctls);
436}
437
Joseph Chanc577b8a2006-11-29 15:29:40 +0100438/* create input playback/capture controls for the given pin */
Lydia Wang9510e8d2009-10-10 19:07:39 +0800439static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200440 int type_idx, int idx, int mix_nid)
Joseph Chanc577b8a2006-11-29 15:29:40 +0100441{
442 char name[32];
443 int err;
444
445 sprintf(name, "%s Playback Volume", ctlname);
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200446 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
Joseph Chanc577b8a2006-11-29 15:29:40 +0100447 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
448 if (err < 0)
449 return err;
450 sprintf(name, "%s Playback Switch", ctlname);
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200451 err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
Joseph Chanc577b8a2006-11-29 15:29:40 +0100452 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
453 if (err < 0)
454 return err;
455 return 0;
456}
457
Takashi Iwai5d417622011-06-20 11:32:27 +0200458#define get_connection_index(codec, mux, nid) \
Takashi Iwai8d087c72011-06-28 12:45:47 +0200459 snd_hda_get_conn_index(codec, mux, nid, 0)
Takashi Iwai5d417622011-06-20 11:32:27 +0200460
Takashi Iwai8df2a312011-06-21 11:48:29 +0200461static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
462 unsigned int mask)
463{
Takashi Iwaia934d5a2011-06-21 14:22:14 +0200464 unsigned int caps;
465 if (!nid)
466 return false;
467 caps = get_wcaps(codec, nid);
Takashi Iwai8df2a312011-06-21 11:48:29 +0200468 if (dir == HDA_INPUT)
469 caps &= AC_WCAP_IN_AMP;
470 else
471 caps &= AC_WCAP_OUT_AMP;
472 if (!caps)
473 return false;
474 if (query_amp_caps(codec, nid, dir) & mask)
475 return true;
476 return false;
477}
478
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200479#define have_mute(codec, nid, dir) \
480 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
Takashi Iwai8df2a312011-06-21 11:48:29 +0200481
Lydia Wangd69607b32011-07-08 14:02:52 +0800482/* enable/disable the output-route mixers */
483static void activate_output_mix(struct hda_codec *codec, struct nid_path *path,
Takashi Iwai3214b962011-07-18 12:49:25 +0200484 hda_nid_t mix_nid, int idx, bool enable)
Lydia Wangd69607b32011-07-08 14:02:52 +0800485{
486 int i, num, val;
Lydia Wangd69607b32011-07-08 14:02:52 +0800487
488 if (!path)
489 return;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200490 num = snd_hda_get_num_conns(codec, mix_nid);
Lydia Wangd69607b32011-07-08 14:02:52 +0800491 for (i = 0; i < num; i++) {
Takashi Iwai3214b962011-07-18 12:49:25 +0200492 if (i == idx)
493 val = AMP_IN_UNMUTE(i);
494 else
495 val = AMP_IN_MUTE(i);
Lydia Wangd69607b32011-07-08 14:02:52 +0800496 snd_hda_codec_write(codec, mix_nid, 0,
497 AC_VERB_SET_AMP_GAIN_MUTE, val);
498 }
499}
500
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200501/* enable/disable the output-route */
502static void activate_output_path(struct hda_codec *codec, struct nid_path *path,
503 bool enable, bool force)
Takashi Iwai5d417622011-06-20 11:32:27 +0200504{
Lydia Wangd69607b32011-07-08 14:02:52 +0800505 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200506 int i;
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200507 for (i = 0; i < path->depth; i++) {
508 hda_nid_t src, dst;
509 int idx = path->idx[i];
510 src = path->path[i];
511 if (i < path->depth - 1)
512 dst = path->path[i + 1];
513 else
514 dst = 0;
515 if (enable && path->multi[i])
516 snd_hda_codec_write(codec, dst, 0,
517 AC_VERB_SET_CONNECT_SEL, idx);
Takashi Iwai3214b962011-07-18 12:49:25 +0200518 if (!force && (dst == spec->aa_mix_nid))
Lydia Wange5e14682011-07-01 10:55:07 +0800519 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +0200520 if (have_mute(codec, dst, HDA_INPUT))
521 activate_output_mix(codec, path, dst, idx, enable);
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200522 if (!force && (src == path->vol_ctl || src == path->mute_ctl))
523 continue;
524 if (have_mute(codec, src, HDA_OUTPUT)) {
525 int val = enable ? AMP_OUT_UNMUTE : AMP_OUT_MUTE;
526 snd_hda_codec_write(codec, src, 0,
527 AC_VERB_SET_AMP_GAIN_MUTE, val);
528 }
529 }
Takashi Iwai5d417622011-06-20 11:32:27 +0200530}
531
532/* set the given pin as output */
533static void init_output_pin(struct hda_codec *codec, hda_nid_t pin,
534 int pin_type)
535{
536 if (!pin)
537 return;
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200538 snd_hda_set_pin_ctl(codec, pin, pin_type);
Takashi Iwai5d417622011-06-20 11:32:27 +0200539 if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
540 snd_hda_codec_write(codec, pin, 0,
Takashi Iwaid3a11e62009-07-07 13:43:35 +0200541 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
Joseph Chanc577b8a2006-11-29 15:29:40 +0100542}
543
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200544static void via_auto_init_output(struct hda_codec *codec,
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200545 struct nid_path *path, int pin_type)
Takashi Iwai5d417622011-06-20 11:32:27 +0200546{
Takashi Iwai5d417622011-06-20 11:32:27 +0200547 unsigned int caps;
Lydia Wangd69607b32011-07-08 14:02:52 +0800548 hda_nid_t pin;
Takashi Iwai5d417622011-06-20 11:32:27 +0200549
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200550 if (!path->depth)
Takashi Iwai5d417622011-06-20 11:32:27 +0200551 return;
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200552 pin = path->path[path->depth - 1];
Takashi Iwai5d417622011-06-20 11:32:27 +0200553
554 init_output_pin(codec, pin, pin_type);
Takashi Iwai77e314f2012-02-22 12:34:08 +0100555 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
556 caps = query_amp_caps(codec, pin, HDA_OUTPUT);
557 else
558 caps = 0;
Takashi Iwai5d417622011-06-20 11:32:27 +0200559 if (caps & AC_AMPCAP_MUTE) {
560 unsigned int val;
561 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
562 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
563 AMP_OUT_MUTE | val);
564 }
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200565 activate_output_path(codec, path, true, true); /* force on */
Takashi Iwai5d417622011-06-20 11:32:27 +0200566}
567
Joseph Chanc577b8a2006-11-29 15:29:40 +0100568static void via_auto_init_multi_out(struct hda_codec *codec)
569{
570 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200571 struct nid_path *path;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100572 int i;
573
Takashi Iwai3214b962011-07-18 12:49:25 +0200574 for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++) {
575 path = &spec->out_path[i];
576 if (!i && spec->aamix_mode && spec->out_mix_path.depth)
577 path = &spec->out_mix_path;
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200578 via_auto_init_output(codec, path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200579 }
Joseph Chanc577b8a2006-11-29 15:29:40 +0100580}
581
Takashi Iwai020066d2011-07-21 13:45:56 +0200582/* deactivate the inactive headphone-paths */
583static void deactivate_hp_paths(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +0100584{
585 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200586 int shared = spec->hp_indep_shared;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100587
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200588 if (spec->hp_independent_mode) {
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200589 activate_output_path(codec, &spec->hp_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200590 activate_output_path(codec, &spec->hp_mix_path, false, false);
591 if (shared)
592 activate_output_path(codec, &spec->out_path[shared],
593 false, false);
Takashi Iwai020066d2011-07-21 13:45:56 +0200594 } else if (spec->aamix_mode || !spec->hp_path.depth) {
595 activate_output_path(codec, &spec->hp_indep_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200596 activate_output_path(codec, &spec->hp_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200597 } else {
Takashi Iwai020066d2011-07-21 13:45:56 +0200598 activate_output_path(codec, &spec->hp_indep_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200599 activate_output_path(codec, &spec->hp_mix_path, false, false);
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200600 }
Joseph Chanc577b8a2006-11-29 15:29:40 +0100601}
602
Takashi Iwai020066d2011-07-21 13:45:56 +0200603static void via_auto_init_hp_out(struct hda_codec *codec)
604{
605 struct via_spec *spec = codec->spec;
606
607 if (!spec->hp_path.depth) {
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200608 via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200609 return;
610 }
611 deactivate_hp_paths(codec);
612 if (spec->hp_independent_mode)
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200613 via_auto_init_output(codec, &spec->hp_indep_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200614 else if (spec->aamix_mode)
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200615 via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200616 else
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200617 via_auto_init_output(codec, &spec->hp_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200618}
619
Takashi Iwai4a918ff2011-06-20 12:39:26 +0200620static void via_auto_init_speaker_out(struct hda_codec *codec)
621{
622 struct via_spec *spec = codec->spec;
623
Takashi Iwai3214b962011-07-18 12:49:25 +0200624 if (!spec->autocfg.speaker_outs)
625 return;
626 if (!spec->speaker_path.depth) {
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200627 via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200628 return;
629 }
630 if (!spec->aamix_mode) {
631 activate_output_path(codec, &spec->speaker_mix_path,
632 false, false);
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200633 via_auto_init_output(codec, &spec->speaker_path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200634 } else {
635 activate_output_path(codec, &spec->speaker_path, false, false);
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200636 via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200637 }
Takashi Iwai4a918ff2011-06-20 12:39:26 +0200638}
639
Takashi Iwaif4a78282011-06-17 18:46:48 +0200640static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
Takashi Iwai6e969d92011-07-11 11:28:13 +0200641static void via_hp_automute(struct hda_codec *codec);
Clemens Ladisch32e01912010-07-12 16:28:50 +0200642
Joseph Chanc577b8a2006-11-29 15:29:40 +0100643static void via_auto_init_analog_input(struct hda_codec *codec)
644{
645 struct via_spec *spec = codec->spec;
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200646 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai096a8852011-06-20 12:09:02 +0200647 hda_nid_t conn[HDA_MAX_CONNECTIONS];
Clemens Ladisch32e01912010-07-12 16:28:50 +0200648 unsigned int ctl;
Takashi Iwai096a8852011-06-20 12:09:02 +0200649 int i, num_conns;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100650
Takashi Iwai096a8852011-06-20 12:09:02 +0200651 /* init ADCs */
652 for (i = 0; i < spec->num_adc_nids; i++) {
Takashi Iwai77e314f2012-02-22 12:34:08 +0100653 hda_nid_t nid = spec->adc_nids[i];
654 if (!(get_wcaps(codec, nid) & AC_WCAP_IN_AMP) ||
655 !(query_amp_caps(codec, nid, HDA_INPUT) & AC_AMPCAP_MUTE))
656 continue;
Takashi Iwai096a8852011-06-20 12:09:02 +0200657 snd_hda_codec_write(codec, spec->adc_nids[i], 0,
658 AC_VERB_SET_AMP_GAIN_MUTE,
659 AMP_IN_UNMUTE(0));
660 }
661
662 /* init pins */
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200663 for (i = 0; i < cfg->num_inputs; i++) {
664 hda_nid_t nid = cfg->inputs[i].pin;
Takashi Iwaif4a78282011-06-17 18:46:48 +0200665 if (spec->smart51_enabled && is_smart51_pins(codec, nid))
Clemens Ladisch32e01912010-07-12 16:28:50 +0200666 ctl = PIN_OUT;
Takashi Iwai47408602012-04-20 13:06:53 +0200667 else {
Clemens Ladisch32e01912010-07-12 16:28:50 +0200668 ctl = PIN_IN;
Takashi Iwai47408602012-04-20 13:06:53 +0200669 if (cfg->inputs[i].type == AUTO_PIN_MIC)
670 ctl |= snd_hda_get_default_vref(codec, nid);
671 }
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200672 snd_hda_set_pin_ctl(codec, nid, ctl);
Joseph Chanc577b8a2006-11-29 15:29:40 +0100673 }
Takashi Iwai096a8852011-06-20 12:09:02 +0200674
675 /* init input-src */
676 for (i = 0; i < spec->num_adc_nids; i++) {
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200677 int adc_idx = spec->inputs[spec->cur_mux[i]].adc_idx;
Takashi Iwaifc1156c2012-02-13 15:04:06 +0100678 /* secondary ADCs must have the unique MUX */
679 if (i > 0 && !spec->mux_nids[i])
680 break;
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200681 if (spec->mux_nids[adc_idx]) {
682 int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
683 snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
684 AC_VERB_SET_CONNECT_SEL,
685 mux_idx);
686 }
687 if (spec->dyn_adc_switch)
688 break; /* only one input-src */
Takashi Iwai096a8852011-06-20 12:09:02 +0200689 }
690
691 /* init aa-mixer */
692 if (!spec->aa_mix_nid)
693 return;
694 num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
695 ARRAY_SIZE(conn));
696 for (i = 0; i < num_conns; i++) {
697 unsigned int caps = get_wcaps(codec, conn[i]);
698 if (get_wcaps_type(caps) == AC_WID_PIN)
699 snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
700 AC_VERB_SET_AMP_GAIN_MUTE,
701 AMP_IN_MUTE(i));
702 }
Joseph Chanc577b8a2006-11-29 15:29:40 +0100703}
Lydia Wangf5271102009-10-10 19:07:35 +0800704
Takashi Iwai054d8672012-01-24 12:25:50 +0100705static void update_power_state(struct hda_codec *codec, hda_nid_t nid,
706 unsigned int parm)
707{
708 if (snd_hda_codec_read(codec, nid, 0,
709 AC_VERB_GET_POWER_STATE, 0) == parm)
710 return;
711 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
712}
713
Lydia Wangf5271102009-10-10 19:07:35 +0800714static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
715 unsigned int *affected_parm)
716{
717 unsigned parm;
718 unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
719 unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
720 >> AC_DEFCFG_MISC_SHIFT
721 & AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
Lydia Wang1564b282009-10-10 19:07:52 +0800722 struct via_spec *spec = codec->spec;
Takashi Iwai24088a52011-06-17 16:59:21 +0200723 unsigned present = 0;
724
725 no_presence |= spec->no_pin_power_ctl;
726 if (!no_presence)
727 present = snd_hda_jack_detect(codec, nid);
Takashi Iwaif4a78282011-06-17 18:46:48 +0200728 if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
Lydia Wang1564b282009-10-10 19:07:52 +0800729 || ((no_presence || present)
730 && get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
Lydia Wangf5271102009-10-10 19:07:35 +0800731 *affected_parm = AC_PWRST_D0; /* if it's connected */
732 parm = AC_PWRST_D0;
733 } else
734 parm = AC_PWRST_D3;
735
Takashi Iwai054d8672012-01-24 12:25:50 +0100736 update_power_state(codec, nid, parm);
Lydia Wangf5271102009-10-10 19:07:35 +0800737}
738
Takashi Iwai24088a52011-06-17 16:59:21 +0200739static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
740 struct snd_ctl_elem_info *uinfo)
741{
742 static const char * const texts[] = {
743 "Disabled", "Enabled"
744 };
745
746 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
747 uinfo->count = 1;
748 uinfo->value.enumerated.items = 2;
749 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
750 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
751 strcpy(uinfo->value.enumerated.name,
752 texts[uinfo->value.enumerated.item]);
753 return 0;
754}
755
756static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
757 struct snd_ctl_elem_value *ucontrol)
758{
759 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
760 struct via_spec *spec = codec->spec;
761 ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
762 return 0;
763}
764
765static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
766 struct snd_ctl_elem_value *ucontrol)
767{
768 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
769 struct via_spec *spec = codec->spec;
770 unsigned int val = !ucontrol->value.enumerated.item[0];
771
772 if (val == spec->no_pin_power_ctl)
773 return 0;
774 spec->no_pin_power_ctl = val;
775 set_widgets_power_state(codec);
Takashi Iwaie9d010c2012-02-01 10:33:23 +0100776 analog_low_current_mode(codec);
Takashi Iwai24088a52011-06-17 16:59:21 +0200777 return 1;
778}
779
780static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
781 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
782 .name = "Dynamic Power-Control",
783 .info = via_pin_power_ctl_info,
784 .get = via_pin_power_ctl_get,
785 .put = via_pin_power_ctl_put,
786};
787
788
Harald Welte0aa62ae2008-09-09 15:58:27 +0800789static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
790 struct snd_ctl_elem_info *uinfo)
791{
Takashi Iwai8df2a312011-06-21 11:48:29 +0200792 static const char * const texts[] = { "OFF", "ON" };
793
794 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
795 uinfo->count = 1;
796 uinfo->value.enumerated.items = 2;
797 if (uinfo->value.enumerated.item >= 2)
798 uinfo->value.enumerated.item = 1;
799 strcpy(uinfo->value.enumerated.name,
800 texts[uinfo->value.enumerated.item]);
801 return 0;
Harald Welte0aa62ae2008-09-09 15:58:27 +0800802}
803
804static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
805 struct snd_ctl_elem_value *ucontrol)
806{
807 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Lydia Wangcdc17842009-10-10 19:07:47 +0800808 struct via_spec *spec = codec->spec;
Lydia Wangcdc17842009-10-10 19:07:47 +0800809
Takashi Iwaiece8d042011-06-19 16:24:21 +0200810 ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
Lydia Wangcdc17842009-10-10 19:07:47 +0800811 return 0;
812}
813
Takashi Iwai3b607e32011-07-18 16:54:40 +0200814/* adjust spec->multiout setup according to the current flags */
815static void setup_playback_multi_pcm(struct via_spec *spec)
816{
817 const struct auto_pin_cfg *cfg = &spec->autocfg;
818 spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
819 spec->multiout.hp_nid = 0;
820 if (!spec->hp_independent_mode) {
821 if (!spec->hp_indep_shared)
822 spec->multiout.hp_nid = spec->hp_dac_nid;
823 } else {
824 if (spec->hp_indep_shared)
825 spec->multiout.num_dacs = cfg->line_outs - 1;
826 }
827}
828
829/* update DAC setups according to indep-HP switch;
830 * this function is called only when indep-HP is modified
831 */
832static void switch_indep_hp_dacs(struct hda_codec *codec)
833{
834 struct via_spec *spec = codec->spec;
835 int shared = spec->hp_indep_shared;
836 hda_nid_t shared_dac, hp_dac;
837
838 if (!spec->opened_streams)
839 return;
840
841 shared_dac = shared ? spec->multiout.dac_nids[shared] : 0;
842 hp_dac = spec->hp_dac_nid;
843 if (spec->hp_independent_mode) {
844 /* switch to indep-HP mode */
845 if (spec->active_streams & STREAM_MULTI_OUT) {
846 __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
847 __snd_hda_codec_cleanup_stream(codec, shared_dac, 1);
848 }
849 if (spec->active_streams & STREAM_INDEP_HP)
850 snd_hda_codec_setup_stream(codec, hp_dac,
851 spec->cur_hp_stream_tag, 0,
852 spec->cur_hp_format);
853 } else {
854 /* back to HP or shared-DAC */
855 if (spec->active_streams & STREAM_INDEP_HP)
856 __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
857 if (spec->active_streams & STREAM_MULTI_OUT) {
858 hda_nid_t dac;
859 int ch;
860 if (shared_dac) { /* reset mutli-ch DAC */
861 dac = shared_dac;
862 ch = shared * 2;
863 } else { /* reset HP DAC */
864 dac = hp_dac;
865 ch = 0;
866 }
867 snd_hda_codec_setup_stream(codec, dac,
868 spec->cur_dac_stream_tag, ch,
869 spec->cur_dac_format);
870 }
871 }
872 setup_playback_multi_pcm(spec);
873}
874
Harald Welte0aa62ae2008-09-09 15:58:27 +0800875static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
876 struct snd_ctl_elem_value *ucontrol)
877{
878 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
879 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200880 int cur, shared;
Takashi Iwai8df2a312011-06-21 11:48:29 +0200881
Takashi Iwai3b607e32011-07-18 16:54:40 +0200882 mutex_lock(&spec->config_mutex);
Takashi Iwai25250502011-06-30 17:24:47 +0200883 cur = !!ucontrol->value.enumerated.item[0];
Takashi Iwai3b607e32011-07-18 16:54:40 +0200884 if (spec->hp_independent_mode == cur) {
885 mutex_unlock(&spec->config_mutex);
Takashi Iwai25250502011-06-30 17:24:47 +0200886 return 0;
Takashi Iwai3b607e32011-07-18 16:54:40 +0200887 }
Takashi Iwai25250502011-06-30 17:24:47 +0200888 spec->hp_independent_mode = cur;
Takashi Iwai3214b962011-07-18 12:49:25 +0200889 shared = spec->hp_indep_shared;
Takashi Iwai020066d2011-07-21 13:45:56 +0200890 deactivate_hp_paths(codec);
891 if (cur)
892 activate_output_path(codec, &spec->hp_indep_path, true, false);
893 else {
Takashi Iwai3214b962011-07-18 12:49:25 +0200894 if (shared)
895 activate_output_path(codec, &spec->out_path[shared],
Takashi Iwai25250502011-06-30 17:24:47 +0200896 true, false);
Takashi Iwai020066d2011-07-21 13:45:56 +0200897 if (spec->aamix_mode || !spec->hp_path.depth)
898 activate_output_path(codec, &spec->hp_mix_path,
899 true, false);
900 else
901 activate_output_path(codec, &spec->hp_path,
902 true, false);
Takashi Iwai8df2a312011-06-21 11:48:29 +0200903 }
Harald Welte0aa62ae2008-09-09 15:58:27 +0800904
Takashi Iwai3b607e32011-07-18 16:54:40 +0200905 switch_indep_hp_dacs(codec);
906 mutex_unlock(&spec->config_mutex);
907
Lydia Wangce0e5a92011-03-22 16:22:37 +0800908 /* update jack power state */
Lydia Wang3e95b9a2011-03-23 15:13:28 +0800909 set_widgets_power_state(codec);
Takashi Iwai6e969d92011-07-11 11:28:13 +0200910 via_hp_automute(codec);
Takashi Iwai25250502011-06-30 17:24:47 +0200911 return 1;
Harald Welte0aa62ae2008-09-09 15:58:27 +0800912}
913
Takashi Iwaiece8d042011-06-19 16:24:21 +0200914static const struct snd_kcontrol_new via_hp_mixer = {
915 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
916 .name = "Independent HP",
917 .info = via_independent_hp_info,
918 .get = via_independent_hp_get,
919 .put = via_independent_hp_put,
Harald Welte0aa62ae2008-09-09 15:58:27 +0800920};
921
Takashi Iwai3d83e572010-04-14 14:36:23 +0200922static int via_hp_build(struct hda_codec *codec)
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100923{
Takashi Iwai3d83e572010-04-14 14:36:23 +0200924 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100925 struct snd_kcontrol_new *knew;
926 hda_nid_t nid;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100927
Takashi Iwaiece8d042011-06-19 16:24:21 +0200928 nid = spec->autocfg.hp_pins[0];
929 knew = via_clone_control(spec, &via_hp_mixer);
Takashi Iwai3d83e572010-04-14 14:36:23 +0200930 if (knew == NULL)
931 return -ENOMEM;
932
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100933 knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100934
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100935 return 0;
936}
937
Lydia Wang1564b282009-10-10 19:07:52 +0800938static void notify_aa_path_ctls(struct hda_codec *codec)
939{
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200940 struct via_spec *spec = codec->spec;
Lydia Wang1564b282009-10-10 19:07:52 +0800941 int i;
Lydia Wang1564b282009-10-10 19:07:52 +0800942
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200943 for (i = 0; i < spec->smart51_nums; i++) {
944 struct snd_kcontrol *ctl;
945 struct snd_ctl_elem_id id;
946 memset(&id, 0, sizeof(id));
947 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
948 sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
Lydia Wang525566c2011-04-28 16:03:39 +0800949 ctl = snd_hda_find_mixer_ctl(codec, id.name);
950 if (ctl)
951 snd_ctl_notify(codec->bus->card,
952 SNDRV_CTL_EVENT_MASK_VALUE,
953 &ctl->id);
Lydia Wang1564b282009-10-10 19:07:52 +0800954 }
955}
956
957static void mute_aa_path(struct hda_codec *codec, int mute)
958{
959 struct via_spec *spec = codec->spec;
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200960 int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
Lydia Wang1564b282009-10-10 19:07:52 +0800961 int i;
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200962
Lydia Wang1564b282009-10-10 19:07:52 +0800963 /* check AA path's mute status */
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200964 for (i = 0; i < spec->smart51_nums; i++) {
965 if (spec->smart51_idxs[i] < 0)
966 continue;
967 snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
968 HDA_INPUT, spec->smart51_idxs[i],
Lydia Wang1564b282009-10-10 19:07:52 +0800969 HDA_AMP_MUTE, val);
970 }
971}
Takashi Iwaif4a78282011-06-17 18:46:48 +0200972
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200973static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
974{
975 struct via_spec *spec = codec->spec;
976 int i;
977
978 for (i = 0; i < spec->smart51_nums; i++)
979 if (spec->smart51_pins[i] == pin)
980 return true;
981 return false;
982}
983
Lydia Wang1564b282009-10-10 19:07:52 +0800984static int via_smart51_get(struct snd_kcontrol *kcontrol,
985 struct snd_ctl_elem_value *ucontrol)
986{
987 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
988 struct via_spec *spec = codec->spec;
Lydia Wang1564b282009-10-10 19:07:52 +0800989
Takashi Iwaif2b1c9f2011-06-21 16:52:39 +0200990 *ucontrol->value.integer.value = spec->smart51_enabled;
Lydia Wang1564b282009-10-10 19:07:52 +0800991 return 0;
992}
993
994static int via_smart51_put(struct snd_kcontrol *kcontrol,
995 struct snd_ctl_elem_value *ucontrol)
996{
997 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
998 struct via_spec *spec = codec->spec;
999 int out_in = *ucontrol->value.integer.value
1000 ? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
Lydia Wang1564b282009-10-10 19:07:52 +08001001 int i;
1002
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001003 for (i = 0; i < spec->smart51_nums; i++) {
1004 hda_nid_t nid = spec->smart51_pins[i];
Takashi Iwai7b315bb2010-08-30 13:06:30 +02001005 unsigned int parm;
1006
Takashi Iwai7b315bb2010-08-30 13:06:30 +02001007 parm = snd_hda_codec_read(codec, nid, 0,
1008 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1009 parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
1010 parm |= out_in;
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001011 snd_hda_set_pin_ctl(codec, nid, parm);
Takashi Iwai7b315bb2010-08-30 13:06:30 +02001012 if (out_in == AC_PINCTL_OUT_EN) {
1013 mute_aa_path(codec, 1);
1014 notify_aa_path_ctls(codec);
1015 }
Lydia Wang1564b282009-10-10 19:07:52 +08001016 }
1017 spec->smart51_enabled = *ucontrol->value.integer.value;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08001018 set_widgets_power_state(codec);
Lydia Wang1564b282009-10-10 19:07:52 +08001019 return 1;
1020}
1021
Takashi Iwai5f4b36d2011-06-17 14:55:02 +02001022static const struct snd_kcontrol_new via_smart51_mixer = {
1023 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1024 .name = "Smart 5.1",
1025 .count = 1,
Takashi Iwaif2b1c9f2011-06-21 16:52:39 +02001026 .info = snd_ctl_boolean_mono_info,
Takashi Iwai5f4b36d2011-06-17 14:55:02 +02001027 .get = via_smart51_get,
1028 .put = via_smart51_put,
Lydia Wang1564b282009-10-10 19:07:52 +08001029};
1030
Takashi Iwaif4a78282011-06-17 18:46:48 +02001031static int via_smart51_build(struct hda_codec *codec)
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001032{
Takashi Iwaif4a78282011-06-17 18:46:48 +02001033 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001034
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001035 if (!spec->smart51_nums)
Lydia Wangcb34c202011-04-27 17:44:16 +08001036 return 0;
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001037 if (!via_clone_control(spec, &via_smart51_mixer))
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001038 return -ENOMEM;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001039 return 0;
1040}
1041
Takashi Iwaiada509e2011-06-20 15:40:19 +02001042/* check AA path's mute status */
1043static bool is_aa_path_mute(struct hda_codec *codec)
Lydia Wangf5271102009-10-10 19:07:35 +08001044{
Lydia Wangf5271102009-10-10 19:07:35 +08001045 struct via_spec *spec = codec->spec;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001046 const struct hda_amp_list *p;
1047 int i, ch, v;
1048
1049 for (i = 0; i < spec->num_loopbacks; i++) {
1050 p = &spec->loopback_list[i];
1051 for (ch = 0; ch < 2; ch++) {
1052 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
1053 p->idx);
1054 if (!(v & HDA_AMP_MUTE) && v > 0)
1055 return false;
Lydia Wangf5271102009-10-10 19:07:35 +08001056 }
1057 }
Takashi Iwaiada509e2011-06-20 15:40:19 +02001058 return true;
Lydia Wangf5271102009-10-10 19:07:35 +08001059}
1060
1061/* enter/exit analog low-current mode */
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001062static void __analog_low_current_mode(struct hda_codec *codec, bool force)
Lydia Wangf5271102009-10-10 19:07:35 +08001063{
1064 struct via_spec *spec = codec->spec;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001065 bool enable;
1066 unsigned int verb, parm;
Lydia Wangf5271102009-10-10 19:07:35 +08001067
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001068 if (spec->no_pin_power_ctl)
1069 enable = false;
1070 else
1071 enable = is_aa_path_mute(codec) && !spec->opened_streams;
1072 if (enable == spec->alc_mode && !force)
1073 return;
1074 spec->alc_mode = enable;
Lydia Wangf5271102009-10-10 19:07:35 +08001075
1076 /* decide low current mode's verb & parameter */
1077 switch (spec->codec_type) {
1078 case VT1708B_8CH:
1079 case VT1708B_4CH:
1080 verb = 0xf70;
1081 parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1082 break;
1083 case VT1708S:
Lydia Wangeb7188c2009-10-10 19:08:34 +08001084 case VT1718S:
Lydia Wangf3db4232009-10-10 19:08:41 +08001085 case VT1716S:
Lydia Wangf5271102009-10-10 19:07:35 +08001086 verb = 0xf73;
1087 parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1088 break;
1089 case VT1702:
1090 verb = 0xf73;
1091 parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1092 break;
Lydia Wang25eaba22009-10-10 19:08:43 +08001093 case VT2002P:
Lydia Wangab6734e2009-10-10 19:08:46 +08001094 case VT1812:
Lydia Wang118909562011-03-23 17:57:34 +08001095 case VT1802:
Lydia Wang25eaba22009-10-10 19:08:43 +08001096 verb = 0xf93;
1097 parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
1098 break;
Lydia Wangf5271102009-10-10 19:07:35 +08001099 default:
1100 return; /* other codecs are not supported */
1101 }
1102 /* send verb */
1103 snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1104}
1105
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001106static void analog_low_current_mode(struct hda_codec *codec)
1107{
1108 return __analog_low_current_mode(codec, false);
1109}
1110
Joseph Chanc577b8a2006-11-29 15:29:40 +01001111/*
1112 * generic initialization of ADC, input mixers and output mixers
1113 */
Takashi Iwai096a8852011-06-20 12:09:02 +02001114static const struct hda_verb vt1708_init_verbs[] = {
Lydia Wangaa266fc2011-03-24 12:41:01 +08001115 /* power down jack detect function */
1116 {0x1, 0xf81, 0x1},
Josepch Chanf7278fd2007-12-13 16:40:40 +01001117 { }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001118};
1119
Takashi Iwai3b607e32011-07-18 16:54:40 +02001120static void set_stream_open(struct hda_codec *codec, int bit, bool active)
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001121{
Takashi Iwaiada509e2011-06-20 15:40:19 +02001122 struct via_spec *spec = codec->spec;
1123
1124 if (active)
Takashi Iwai3b607e32011-07-18 16:54:40 +02001125 spec->opened_streams |= bit;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001126 else
Takashi Iwai3b607e32011-07-18 16:54:40 +02001127 spec->opened_streams &= ~bit;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001128 analog_low_current_mode(codec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001129}
1130
Takashi Iwaiece8d042011-06-19 16:24:21 +02001131static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001132 struct hda_codec *codec,
1133 struct snd_pcm_substream *substream)
1134{
1135 struct via_spec *spec = codec->spec;
Takashi Iwai25250502011-06-30 17:24:47 +02001136 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001137 int err;
Takashi Iwaiece8d042011-06-19 16:24:21 +02001138
Takashi Iwai25250502011-06-30 17:24:47 +02001139 spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
Takashi Iwai25250502011-06-30 17:24:47 +02001140 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001141 set_stream_open(codec, STREAM_MULTI_OUT, true);
Takashi Iwaiada509e2011-06-20 15:40:19 +02001142 err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
1143 hinfo);
1144 if (err < 0) {
Takashi Iwai3b607e32011-07-18 16:54:40 +02001145 set_stream_open(codec, STREAM_MULTI_OUT, false);
Takashi Iwaiada509e2011-06-20 15:40:19 +02001146 return err;
1147 }
1148 return 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001149}
1150
Takashi Iwaiece8d042011-06-19 16:24:21 +02001151static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
Takashi Iwai9af74212011-06-18 16:17:45 +02001152 struct hda_codec *codec,
1153 struct snd_pcm_substream *substream)
1154{
Takashi Iwai3b607e32011-07-18 16:54:40 +02001155 set_stream_open(codec, STREAM_MULTI_OUT, false);
Takashi Iwai9af74212011-06-18 16:17:45 +02001156 return 0;
1157}
1158
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001159static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
1160 struct hda_codec *codec,
1161 struct snd_pcm_substream *substream)
1162{
1163 struct via_spec *spec = codec->spec;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001164
Takashi Iwaiece8d042011-06-19 16:24:21 +02001165 if (snd_BUG_ON(!spec->hp_dac_nid))
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001166 return -EINVAL;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001167 set_stream_open(codec, STREAM_INDEP_HP, true);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001168 return 0;
1169}
1170
1171static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1172 struct hda_codec *codec,
1173 struct snd_pcm_substream *substream)
1174{
Takashi Iwai3b607e32011-07-18 16:54:40 +02001175 set_stream_open(codec, STREAM_INDEP_HP, false);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001176 return 0;
1177}
1178
1179static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
1180 struct hda_codec *codec,
1181 unsigned int stream_tag,
1182 unsigned int format,
1183 struct snd_pcm_substream *substream)
Harald Welte0aa62ae2008-09-09 15:58:27 +08001184{
1185 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001186
Takashi Iwai3b607e32011-07-18 16:54:40 +02001187 mutex_lock(&spec->config_mutex);
1188 setup_playback_multi_pcm(spec);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001189 snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1190 format, substream);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001191 /* remember for dynamic DAC switch with indep-HP */
1192 spec->active_streams |= STREAM_MULTI_OUT;
1193 spec->cur_dac_stream_tag = stream_tag;
1194 spec->cur_dac_format = format;
1195 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001196 vt1708_update_hp_work(spec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001197 return 0;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001198}
1199
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001200static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
1201 struct hda_codec *codec,
1202 unsigned int stream_tag,
1203 unsigned int format,
1204 struct snd_pcm_substream *substream)
Harald Welte0aa62ae2008-09-09 15:58:27 +08001205{
1206 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001207
Takashi Iwai3b607e32011-07-18 16:54:40 +02001208 mutex_lock(&spec->config_mutex);
1209 if (spec->hp_independent_mode)
1210 snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1211 stream_tag, 0, format);
1212 spec->active_streams |= STREAM_INDEP_HP;
1213 spec->cur_hp_stream_tag = stream_tag;
1214 spec->cur_hp_format = format;
1215 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001216 vt1708_update_hp_work(spec);
Harald Welte0aa62ae2008-09-09 15:58:27 +08001217 return 0;
1218}
1219
1220static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
1221 struct hda_codec *codec,
1222 struct snd_pcm_substream *substream)
1223{
1224 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001225
Takashi Iwai3b607e32011-07-18 16:54:40 +02001226 mutex_lock(&spec->config_mutex);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001227 snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001228 spec->active_streams &= ~STREAM_MULTI_OUT;
1229 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001230 vt1708_update_hp_work(spec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001231 return 0;
1232}
1233
1234static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
1235 struct hda_codec *codec,
1236 struct snd_pcm_substream *substream)
1237{
1238 struct via_spec *spec = codec->spec;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001239
Takashi Iwai3b607e32011-07-18 16:54:40 +02001240 mutex_lock(&spec->config_mutex);
1241 if (spec->hp_independent_mode)
1242 snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
1243 spec->active_streams &= ~STREAM_INDEP_HP;
1244 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001245 vt1708_update_hp_work(spec);
Harald Welte0aa62ae2008-09-09 15:58:27 +08001246 return 0;
1247}
1248
Joseph Chanc577b8a2006-11-29 15:29:40 +01001249/*
1250 * Digital out
1251 */
1252static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1253 struct hda_codec *codec,
1254 struct snd_pcm_substream *substream)
1255{
1256 struct via_spec *spec = codec->spec;
1257 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1258}
1259
1260static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1261 struct hda_codec *codec,
1262 struct snd_pcm_substream *substream)
1263{
1264 struct via_spec *spec = codec->spec;
1265 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1266}
1267
Harald Welte5691ec72008-09-15 22:42:26 +08001268static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Harald Welte98aa34c2008-09-09 16:02:09 +08001269 struct hda_codec *codec,
1270 unsigned int stream_tag,
1271 unsigned int format,
1272 struct snd_pcm_substream *substream)
1273{
1274 struct via_spec *spec = codec->spec;
Takashi Iwai9da29272009-05-07 16:31:14 +02001275 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1276 stream_tag, format, substream);
1277}
Harald Welte5691ec72008-09-15 22:42:26 +08001278
Takashi Iwai9da29272009-05-07 16:31:14 +02001279static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1280 struct hda_codec *codec,
1281 struct snd_pcm_substream *substream)
1282{
1283 struct via_spec *spec = codec->spec;
1284 snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
Harald Welte98aa34c2008-09-09 16:02:09 +08001285 return 0;
1286}
1287
Joseph Chanc577b8a2006-11-29 15:29:40 +01001288/*
1289 * Analog capture
1290 */
1291static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1292 struct hda_codec *codec,
1293 unsigned int stream_tag,
1294 unsigned int format,
1295 struct snd_pcm_substream *substream)
1296{
1297 struct via_spec *spec = codec->spec;
1298
1299 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1300 stream_tag, 0, format);
1301 return 0;
1302}
1303
1304static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1305 struct hda_codec *codec,
1306 struct snd_pcm_substream *substream)
1307{
1308 struct via_spec *spec = codec->spec;
Takashi Iwai888afa12008-03-18 09:57:50 +01001309 snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001310 return 0;
1311}
1312
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001313/* analog capture with dynamic ADC switching */
1314static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1315 struct hda_codec *codec,
1316 unsigned int stream_tag,
1317 unsigned int format,
1318 struct snd_pcm_substream *substream)
1319{
1320 struct via_spec *spec = codec->spec;
1321 int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
1322
Takashi Iwai3b607e32011-07-18 16:54:40 +02001323 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001324 spec->cur_adc = spec->adc_nids[adc_idx];
1325 spec->cur_adc_stream_tag = stream_tag;
1326 spec->cur_adc_format = format;
1327 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001328 mutex_unlock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001329 return 0;
1330}
1331
1332static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1333 struct hda_codec *codec,
1334 struct snd_pcm_substream *substream)
1335{
1336 struct via_spec *spec = codec->spec;
1337
Takashi Iwai3b607e32011-07-18 16:54:40 +02001338 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001339 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1340 spec->cur_adc = 0;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001341 mutex_unlock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001342 return 0;
1343}
1344
1345/* re-setup the stream if running; called from input-src put */
1346static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
1347{
1348 struct via_spec *spec = codec->spec;
1349 int adc_idx = spec->inputs[cur].adc_idx;
1350 hda_nid_t adc = spec->adc_nids[adc_idx];
Takashi Iwai3b607e32011-07-18 16:54:40 +02001351 bool ret = false;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001352
Takashi Iwai3b607e32011-07-18 16:54:40 +02001353 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001354 if (spec->cur_adc && spec->cur_adc != adc) {
1355 /* stream is running, let's swap the current ADC */
1356 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1357 spec->cur_adc = adc;
1358 snd_hda_codec_setup_stream(codec, adc,
1359 spec->cur_adc_stream_tag, 0,
1360 spec->cur_adc_format);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001361 ret = true;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001362 }
Takashi Iwai3b607e32011-07-18 16:54:40 +02001363 mutex_unlock(&spec->config_mutex);
1364 return ret;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001365}
1366
Takashi Iwai9af74212011-06-18 16:17:45 +02001367static const struct hda_pcm_stream via_pcm_analog_playback = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001368 .substreams = 1,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001369 .channels_min = 2,
1370 .channels_max = 8,
Takashi Iwai9af74212011-06-18 16:17:45 +02001371 /* NID is set in via_build_pcms */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001372 .ops = {
Takashi Iwaiece8d042011-06-19 16:24:21 +02001373 .open = via_playback_multi_pcm_open,
1374 .close = via_playback_multi_pcm_close,
Harald Welte0aa62ae2008-09-09 15:58:27 +08001375 .prepare = via_playback_multi_pcm_prepare,
1376 .cleanup = via_playback_multi_pcm_cleanup
Joseph Chanc577b8a2006-11-29 15:29:40 +01001377 },
1378};
1379
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001380static const struct hda_pcm_stream via_pcm_hp_playback = {
1381 .substreams = 1,
1382 .channels_min = 2,
1383 .channels_max = 2,
1384 /* NID is set in via_build_pcms */
1385 .ops = {
1386 .open = via_playback_hp_pcm_open,
Takashi Iwaiece8d042011-06-19 16:24:21 +02001387 .close = via_playback_hp_pcm_close,
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001388 .prepare = via_playback_hp_pcm_prepare,
1389 .cleanup = via_playback_hp_pcm_cleanup
1390 },
1391};
1392
Takashi Iwai90dd48a2011-05-02 12:38:19 +02001393static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001394 .substreams = 1,
Takashi Iwaibc9b562382008-05-23 17:50:27 +02001395 .channels_min = 2,
1396 .channels_max = 8,
Takashi Iwai9af74212011-06-18 16:17:45 +02001397 /* NID is set in via_build_pcms */
Takashi Iwaibc9b562382008-05-23 17:50:27 +02001398 /* We got noisy outputs on the right channel on VT1708 when
1399 * 24bit samples are used. Until any workaround is found,
1400 * disable the 24bit format, so far.
1401 */
1402 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1403 .ops = {
Takashi Iwaiece8d042011-06-19 16:24:21 +02001404 .open = via_playback_multi_pcm_open,
1405 .close = via_playback_multi_pcm_close,
Lydia Wangc873cc22009-10-10 19:08:21 +08001406 .prepare = via_playback_multi_pcm_prepare,
1407 .cleanup = via_playback_multi_pcm_cleanup
Takashi Iwaibc9b562382008-05-23 17:50:27 +02001408 },
1409};
1410
Takashi Iwai9af74212011-06-18 16:17:45 +02001411static const struct hda_pcm_stream via_pcm_analog_capture = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001412 .substreams = 1, /* will be changed in via_build_pcms() */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001413 .channels_min = 2,
1414 .channels_max = 2,
Takashi Iwai9af74212011-06-18 16:17:45 +02001415 /* NID is set in via_build_pcms */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001416 .ops = {
1417 .prepare = via_capture_pcm_prepare,
1418 .cleanup = via_capture_pcm_cleanup
1419 },
1420};
1421
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001422static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
1423 .substreams = 1,
1424 .channels_min = 2,
1425 .channels_max = 2,
1426 /* NID is set in via_build_pcms */
1427 .ops = {
1428 .prepare = via_dyn_adc_capture_pcm_prepare,
1429 .cleanup = via_dyn_adc_capture_pcm_cleanup,
1430 },
1431};
1432
Takashi Iwai9af74212011-06-18 16:17:45 +02001433static const struct hda_pcm_stream via_pcm_digital_playback = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001434 .substreams = 1,
1435 .channels_min = 2,
1436 .channels_max = 2,
1437 /* NID is set in via_build_pcms */
1438 .ops = {
1439 .open = via_dig_playback_pcm_open,
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001440 .close = via_dig_playback_pcm_close,
Takashi Iwai9da29272009-05-07 16:31:14 +02001441 .prepare = via_dig_playback_pcm_prepare,
1442 .cleanup = via_dig_playback_pcm_cleanup
Joseph Chanc577b8a2006-11-29 15:29:40 +01001443 },
1444};
1445
Takashi Iwai9af74212011-06-18 16:17:45 +02001446static const struct hda_pcm_stream via_pcm_digital_capture = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001447 .substreams = 1,
1448 .channels_min = 2,
1449 .channels_max = 2,
1450};
1451
Takashi Iwai370bafb2011-06-20 12:47:45 +02001452/*
1453 * slave controls for virtual master
1454 */
Takashi Iwai9322ca52012-02-03 14:28:01 +01001455static const char * const via_slave_pfxs[] = {
1456 "Front", "Surround", "Center", "LFE", "Side",
1457 "Headphone", "Speaker",
Takashi Iwai370bafb2011-06-20 12:47:45 +02001458 NULL,
1459};
1460
Joseph Chanc577b8a2006-11-29 15:29:40 +01001461static int via_build_controls(struct hda_codec *codec)
1462{
1463 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001464 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001465 int err, i;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001466
Takashi Iwaib5bcc182012-02-02 10:30:17 +01001467 spec->no_pin_power_ctl = 1;
Takashi Iwai24088a52011-06-17 16:59:21 +02001468 if (spec->set_widgets_power_state)
1469 if (!via_clone_control(spec, &via_pin_power_ctl_enum))
1470 return -ENOMEM;
1471
Joseph Chanc577b8a2006-11-29 15:29:40 +01001472 for (i = 0; i < spec->num_mixers; i++) {
1473 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1474 if (err < 0)
1475 return err;
1476 }
1477
1478 if (spec->multiout.dig_out_nid) {
1479 err = snd_hda_create_spdif_out_ctls(codec,
Stephen Warren74b654c2011-06-01 11:14:18 -06001480 spec->multiout.dig_out_nid,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001481 spec->multiout.dig_out_nid);
1482 if (err < 0)
1483 return err;
Takashi Iwai9a081602008-02-12 18:37:26 +01001484 err = snd_hda_create_spdif_share_sw(codec,
1485 &spec->multiout);
1486 if (err < 0)
1487 return err;
1488 spec->multiout.share_spdif = 1;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001489 }
1490 if (spec->dig_in_nid) {
1491 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1492 if (err < 0)
1493 return err;
1494 }
Lydia Wang17314372009-10-10 19:07:37 +08001495
Takashi Iwai370bafb2011-06-20 12:47:45 +02001496 /* if we have no master control, let's create it */
1497 if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1498 unsigned int vmaster_tlv[4];
1499 snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1500 HDA_OUTPUT, vmaster_tlv);
1501 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai9322ca52012-02-03 14:28:01 +01001502 vmaster_tlv, via_slave_pfxs,
1503 "Playback Volume");
Takashi Iwai370bafb2011-06-20 12:47:45 +02001504 if (err < 0)
1505 return err;
1506 }
1507 if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1508 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
Takashi Iwai9322ca52012-02-03 14:28:01 +01001509 NULL, via_slave_pfxs,
1510 "Playback Switch");
Takashi Iwai370bafb2011-06-20 12:47:45 +02001511 if (err < 0)
1512 return err;
1513 }
1514
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001515 /* assign Capture Source enums to NID */
1516 kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1517 for (i = 0; kctl && i < kctl->count; i++) {
Takashi Iwai77e314f2012-02-22 12:34:08 +01001518 if (!spec->mux_nids[i])
1519 continue;
Takashi Iwai21949f02009-12-23 08:31:59 +01001520 err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001521 if (err < 0)
1522 return err;
1523 }
1524
Takashi Iwai603c4012008-07-30 15:01:44 +02001525 via_free_kctls(codec); /* no longer needed */
Takashi Iwai01a61e12011-10-28 00:03:22 +02001526
1527 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1528 if (err < 0)
1529 return err;
1530
Joseph Chanc577b8a2006-11-29 15:29:40 +01001531 return 0;
1532}
1533
1534static int via_build_pcms(struct hda_codec *codec)
1535{
1536 struct via_spec *spec = codec->spec;
1537 struct hda_pcm *info = spec->pcm_rec;
1538
Takashi Iwaia5973102011-09-28 16:43:36 +02001539 codec->num_pcms = 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001540 codec->pcm_info = info;
1541
Takashi Iwaia5973102011-09-28 16:43:36 +02001542 if (spec->multiout.num_dacs || spec->num_adc_nids) {
1543 snprintf(spec->stream_name_analog,
1544 sizeof(spec->stream_name_analog),
1545 "%s Analog", codec->chip_name);
1546 info->name = spec->stream_name_analog;
Takashi Iwai9af74212011-06-18 16:17:45 +02001547
Takashi Iwaia5973102011-09-28 16:43:36 +02001548 if (spec->multiout.num_dacs) {
1549 if (!spec->stream_analog_playback)
1550 spec->stream_analog_playback =
1551 &via_pcm_analog_playback;
1552 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1553 *spec->stream_analog_playback;
1554 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1555 spec->multiout.dac_nids[0];
1556 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1557 spec->multiout.max_channels;
1558 }
Takashi Iwai9af74212011-06-18 16:17:45 +02001559
Takashi Iwaia5973102011-09-28 16:43:36 +02001560 if (!spec->stream_analog_capture) {
1561 if (spec->dyn_adc_switch)
1562 spec->stream_analog_capture =
1563 &via_pcm_dyn_adc_analog_capture;
1564 else
1565 spec->stream_analog_capture =
1566 &via_pcm_analog_capture;
1567 }
1568 if (spec->num_adc_nids) {
1569 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1570 *spec->stream_analog_capture;
1571 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1572 spec->adc_nids[0];
1573 if (!spec->dyn_adc_switch)
1574 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
1575 spec->num_adc_nids;
1576 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001577 codec->num_pcms++;
1578 info++;
Takashi Iwaia5973102011-09-28 16:43:36 +02001579 }
1580
1581 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwai82673bc2011-06-17 16:24:21 +02001582 snprintf(spec->stream_name_digital,
1583 sizeof(spec->stream_name_digital),
1584 "%s Digital", codec->chip_name);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001585 info->name = spec->stream_name_digital;
Takashi Iwai7ba72ba2008-02-06 14:03:20 +01001586 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001587 if (spec->multiout.dig_out_nid) {
Takashi Iwai9af74212011-06-18 16:17:45 +02001588 if (!spec->stream_digital_playback)
1589 spec->stream_digital_playback =
1590 &via_pcm_digital_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001591 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
Takashi Iwai9af74212011-06-18 16:17:45 +02001592 *spec->stream_digital_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001593 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1594 spec->multiout.dig_out_nid;
1595 }
1596 if (spec->dig_in_nid) {
Takashi Iwai9af74212011-06-18 16:17:45 +02001597 if (!spec->stream_digital_capture)
1598 spec->stream_digital_capture =
1599 &via_pcm_digital_capture;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001600 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
Takashi Iwai9af74212011-06-18 16:17:45 +02001601 *spec->stream_digital_capture;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001602 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1603 spec->dig_in_nid;
1604 }
Takashi Iwaia5973102011-09-28 16:43:36 +02001605 codec->num_pcms++;
1606 info++;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001607 }
1608
Takashi Iwaiece8d042011-06-19 16:24:21 +02001609 if (spec->hp_dac_nid) {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001610 snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
1611 "%s HP", codec->chip_name);
1612 info->name = spec->stream_name_hp;
1613 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
1614 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
Takashi Iwaiece8d042011-06-19 16:24:21 +02001615 spec->hp_dac_nid;
Takashi Iwaia5973102011-09-28 16:43:36 +02001616 codec->num_pcms++;
1617 info++;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001618 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001619 return 0;
1620}
1621
1622static void via_free(struct hda_codec *codec)
1623{
1624 struct via_spec *spec = codec->spec;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001625
1626 if (!spec)
1627 return;
1628
Takashi Iwai603c4012008-07-30 15:01:44 +02001629 via_free_kctls(codec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001630 vt1708_stop_hp_work(spec);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001631 kfree(spec->bind_cap_vol);
1632 kfree(spec->bind_cap_sw);
Takashi Iwai7819d1c2012-10-10 08:41:42 +02001633 snd_hda_gen_free(&spec->gen);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001634 kfree(spec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001635}
1636
Takashi Iwai64be2852011-06-17 16:51:39 +02001637/* mute/unmute outputs */
1638static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
1639 hda_nid_t *pins, bool mute)
1640{
1641 int i;
Takashi Iwai94994732011-07-11 11:36:44 +02001642 for (i = 0; i < num_pins; i++) {
1643 unsigned int parm = snd_hda_codec_read(codec, pins[i], 0,
1644 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1645 if (parm & AC_PINCTL_IN_EN)
1646 continue;
1647 if (mute)
1648 parm &= ~AC_PINCTL_OUT_EN;
1649 else
1650 parm |= AC_PINCTL_OUT_EN;
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001651 snd_hda_set_pin_ctl(codec, pins[i], parm);
Takashi Iwai94994732011-07-11 11:36:44 +02001652 }
Takashi Iwai64be2852011-06-17 16:51:39 +02001653}
1654
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001655/* mute internal speaker if line-out is plugged */
1656static void via_line_automute(struct hda_codec *codec, int present)
1657{
1658 struct via_spec *spec = codec->spec;
1659
1660 if (!spec->autocfg.speaker_outs)
1661 return;
1662 if (!present)
1663 present = snd_hda_jack_detect(codec,
1664 spec->autocfg.line_out_pins[0]);
1665 toggle_output_mutes(codec, spec->autocfg.speaker_outs,
1666 spec->autocfg.speaker_pins,
1667 present);
1668}
1669
Harald Welte69e52a82008-09-09 15:57:32 +08001670/* mute internal speaker if HP is plugged */
1671static void via_hp_automute(struct hda_codec *codec)
1672{
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001673 int present = 0;
Takashi Iwai6e969d92011-07-11 11:28:13 +02001674 int nums;
Harald Welte69e52a82008-09-09 15:57:32 +08001675 struct via_spec *spec = codec->spec;
1676
Takashi Iwai187d3332011-11-24 16:33:09 +01001677 if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0] &&
Herton Ronaldo Krzesinskicf55e902012-09-21 20:45:19 -03001678 (spec->codec_type != VT1708 || spec->vt1708_jack_detect) &&
1679 is_jack_detectable(codec, spec->autocfg.hp_pins[0]))
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001680 present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
Takashi Iwai6e969d92011-07-11 11:28:13 +02001681
1682 if (spec->smart51_enabled)
1683 nums = spec->autocfg.line_outs + spec->smart51_nums;
1684 else
1685 nums = spec->autocfg.line_outs;
1686 toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
1687
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001688 via_line_automute(codec, present);
Lydia Wangf3db4232009-10-10 19:08:41 +08001689}
1690
Takashi Iwai2a439522011-07-26 09:52:50 +02001691#ifdef CONFIG_PM
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001692static int via_suspend(struct hda_codec *codec)
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001693{
1694 struct via_spec *spec = codec->spec;
1695 vt1708_stop_hp_work(spec);
David Henningsson94c142a2012-08-09 10:56:12 +02001696
1697 if (spec->codec_type == VT1802) {
1698 /* Fix pop noise on headphones */
1699 int i;
1700 for (i = 0; i < spec->autocfg.hp_outs; i++)
1701 snd_hda_set_pin_ctl(codec, spec->autocfg.hp_pins[i], 0);
1702 }
1703
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001704 return 0;
1705}
1706#endif
1707
Takashi Iwai83012a72012-08-24 18:38:08 +02001708#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001709static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1710{
1711 struct via_spec *spec = codec->spec;
1712 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1713}
1714#endif
1715
Joseph Chanc577b8a2006-11-29 15:29:40 +01001716/*
1717 */
Takashi Iwai5d417622011-06-20 11:32:27 +02001718
1719static int via_init(struct hda_codec *codec);
1720
Takashi Iwai90dd48a2011-05-02 12:38:19 +02001721static const struct hda_codec_ops via_patch_ops = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001722 .build_controls = via_build_controls,
1723 .build_pcms = via_build_pcms,
1724 .init = via_init,
1725 .free = via_free,
David Henningsson4e2d16d2012-10-08 15:44:14 +02001726 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai2a439522011-07-26 09:52:50 +02001727#ifdef CONFIG_PM
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001728 .suspend = via_suspend,
Takashi Iwaicb53c622007-08-10 17:21:45 +02001729 .check_power_status = via_check_power_status,
1730#endif
Joseph Chanc577b8a2006-11-29 15:29:40 +01001731};
1732
Takashi Iwai4a796162011-06-17 17:53:38 +02001733static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
Joseph Chanc577b8a2006-11-29 15:29:40 +01001734{
Takashi Iwai4a796162011-06-17 17:53:38 +02001735 struct via_spec *spec = codec->spec;
1736 int i;
1737
1738 for (i = 0; i < spec->multiout.num_dacs; i++) {
1739 if (spec->multiout.dac_nids[i] == dac)
1740 return false;
1741 }
Takashi Iwaiece8d042011-06-19 16:24:21 +02001742 if (spec->hp_dac_nid == dac)
Takashi Iwai4a796162011-06-17 17:53:38 +02001743 return false;
1744 return true;
1745}
1746
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001747static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3214b962011-07-18 12:49:25 +02001748 hda_nid_t target_dac, int with_aa_mix,
1749 struct nid_path *path, int depth)
Takashi Iwai4a796162011-06-17 17:53:38 +02001750{
Takashi Iwai3214b962011-07-18 12:49:25 +02001751 struct via_spec *spec = codec->spec;
Takashi Iwai4a796162011-06-17 17:53:38 +02001752 hda_nid_t conn[8];
1753 int i, nums;
1754
Takashi Iwai3214b962011-07-18 12:49:25 +02001755 if (nid == spec->aa_mix_nid) {
1756 if (!with_aa_mix)
1757 return false;
1758 with_aa_mix = 2; /* mark aa-mix is included */
1759 }
1760
Takashi Iwai4a796162011-06-17 17:53:38 +02001761 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
1762 for (i = 0; i < nums; i++) {
1763 if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
1764 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02001765 if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
1766 /* aa-mix is requested but not included? */
1767 if (!(spec->aa_mix_nid && with_aa_mix == 1))
1768 goto found;
1769 }
Takashi Iwai4a796162011-06-17 17:53:38 +02001770 }
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001771 if (depth >= MAX_NID_PATH_DEPTH)
Takashi Iwai4a796162011-06-17 17:53:38 +02001772 return false;
1773 for (i = 0; i < nums; i++) {
1774 unsigned int type;
1775 type = get_wcaps_type(get_wcaps(codec, conn[i]));
Takashi Iwai3214b962011-07-18 12:49:25 +02001776 if (type == AC_WID_AUD_OUT)
Takashi Iwai4a796162011-06-17 17:53:38 +02001777 continue;
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001778 if (__parse_output_path(codec, conn[i], target_dac,
Takashi Iwai3214b962011-07-18 12:49:25 +02001779 with_aa_mix, path, depth + 1))
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001780 goto found;
Takashi Iwai4a796162011-06-17 17:53:38 +02001781 }
1782 return false;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001783
1784 found:
1785 path->path[path->depth] = conn[i];
1786 path->idx[path->depth] = i;
1787 if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
1788 path->multi[path->depth] = 1;
1789 path->depth++;
1790 return true;
Takashi Iwai4a796162011-06-17 17:53:38 +02001791}
1792
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001793static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3214b962011-07-18 12:49:25 +02001794 hda_nid_t target_dac, int with_aa_mix,
1795 struct nid_path *path)
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001796{
Takashi Iwai3214b962011-07-18 12:49:25 +02001797 if (__parse_output_path(codec, nid, target_dac, with_aa_mix, path, 1)) {
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001798 path->path[path->depth] = nid;
1799 path->depth++;
Takashi Iwai3214b962011-07-18 12:49:25 +02001800 snd_printdd("output-path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
1801 path->depth, path->path[0], path->path[1],
1802 path->path[2], path->path[3], path->path[4]);
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001803 return true;
1804 }
1805 return false;
1806}
1807
Takashi Iwai4a796162011-06-17 17:53:38 +02001808static int via_auto_fill_dac_nids(struct hda_codec *codec)
1809{
1810 struct via_spec *spec = codec->spec;
1811 const struct auto_pin_cfg *cfg = &spec->autocfg;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001812 int i, dac_num;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001813 hda_nid_t nid;
1814
Joseph Chanc577b8a2006-11-29 15:29:40 +01001815 spec->multiout.dac_nids = spec->private_dac_nids;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001816 dac_num = 0;
Takashi Iwai4a796162011-06-17 17:53:38 +02001817 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai3214b962011-07-18 12:49:25 +02001818 hda_nid_t dac = 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001819 nid = cfg->line_out_pins[i];
Takashi Iwai4a796162011-06-17 17:53:38 +02001820 if (!nid)
1821 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02001822 if (parse_output_path(codec, nid, 0, 0, &spec->out_path[i]))
1823 dac = spec->out_path[i].path[0];
1824 if (!i && parse_output_path(codec, nid, dac, 1,
1825 &spec->out_mix_path))
1826 dac = spec->out_mix_path.path[0];
1827 if (dac) {
1828 spec->private_dac_nids[i] = dac;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001829 dac_num++;
1830 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001831 }
Takashi Iwai3214b962011-07-18 12:49:25 +02001832 if (!spec->out_path[0].depth && spec->out_mix_path.depth) {
1833 spec->out_path[0] = spec->out_mix_path;
1834 spec->out_mix_path.depth = 0;
1835 }
Lydia Wang5c9a5612011-07-08 14:03:43 +08001836 spec->multiout.num_dacs = dac_num;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001837 return 0;
1838}
1839
Takashi Iwai4a796162011-06-17 17:53:38 +02001840static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001841 int chs, bool check_dac, struct nid_path *path)
Joseph Chanc577b8a2006-11-29 15:29:40 +01001842{
Takashi Iwai4a796162011-06-17 17:53:38 +02001843 struct via_spec *spec = codec->spec;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001844 char name[32];
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001845 hda_nid_t dac, pin, sel, nid;
1846 int err;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001847
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001848 dac = check_dac ? path->path[0] : 0;
1849 pin = path->path[path->depth - 1];
1850 sel = path->depth > 1 ? path->path[1] : 0;
Takashi Iwai4a796162011-06-17 17:53:38 +02001851
Takashi Iwai8df2a312011-06-21 11:48:29 +02001852 if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
Takashi Iwai4a796162011-06-17 17:53:38 +02001853 nid = dac;
Takashi Iwai8df2a312011-06-21 11:48:29 +02001854 else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
Takashi Iwai4a796162011-06-17 17:53:38 +02001855 nid = pin;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001856 else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1857 nid = sel;
Takashi Iwai4a796162011-06-17 17:53:38 +02001858 else
1859 nid = 0;
1860 if (nid) {
1861 sprintf(name, "%s Playback Volume", pfx);
1862 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
Lydia Wanga00a5fa2011-06-21 16:11:11 +08001863 HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
Takashi Iwai4a796162011-06-17 17:53:38 +02001864 if (err < 0)
1865 return err;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001866 path->vol_ctl = nid;
Takashi Iwai4a796162011-06-17 17:53:38 +02001867 }
1868
Takashi Iwai8df2a312011-06-21 11:48:29 +02001869 if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
Takashi Iwai4a796162011-06-17 17:53:38 +02001870 nid = dac;
Takashi Iwai8df2a312011-06-21 11:48:29 +02001871 else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
Takashi Iwai4a796162011-06-17 17:53:38 +02001872 nid = pin;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001873 else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1874 nid = sel;
Takashi Iwai4a796162011-06-17 17:53:38 +02001875 else
1876 nid = 0;
1877 if (nid) {
1878 sprintf(name, "%s Playback Switch", pfx);
1879 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1880 HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1881 if (err < 0)
1882 return err;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001883 path->mute_ctl = nid;
Takashi Iwai4a796162011-06-17 17:53:38 +02001884 }
1885 return 0;
1886}
1887
Takashi Iwaif4a78282011-06-17 18:46:48 +02001888static void mangle_smart51(struct hda_codec *codec)
1889{
1890 struct via_spec *spec = codec->spec;
1891 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001892 struct auto_pin_cfg_item *ins = cfg->inputs;
1893 int i, j, nums, attr;
1894 int pins[AUTO_CFG_MAX_INS];
Takashi Iwaif4a78282011-06-17 18:46:48 +02001895
Takashi Iwai0f98c242011-06-21 12:51:33 +02001896 for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
1897 nums = 0;
1898 for (i = 0; i < cfg->num_inputs; i++) {
1899 unsigned int def;
1900 if (ins[i].type > AUTO_PIN_LINE_IN)
1901 continue;
1902 def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
1903 if (snd_hda_get_input_pin_attr(def) != attr)
1904 continue;
1905 for (j = 0; j < nums; j++)
1906 if (ins[pins[j]].type < ins[i].type) {
1907 memmove(pins + j + 1, pins + j,
Takashi Iwai21d45d22011-07-08 11:35:11 +02001908 (nums - j) * sizeof(int));
Takashi Iwai0f98c242011-06-21 12:51:33 +02001909 break;
1910 }
1911 pins[j] = i;
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001912 nums++;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001913 }
1914 if (cfg->line_outs + nums < 3)
Takashi Iwaif4a78282011-06-17 18:46:48 +02001915 continue;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001916 for (i = 0; i < nums; i++) {
1917 hda_nid_t pin = ins[pins[i]].pin;
1918 spec->smart51_pins[spec->smart51_nums++] = pin;
1919 cfg->line_out_pins[cfg->line_outs++] = pin;
1920 if (cfg->line_outs == 3)
1921 break;
1922 }
1923 return;
Takashi Iwaif4a78282011-06-17 18:46:48 +02001924 }
1925}
1926
Takashi Iwai020066d2011-07-21 13:45:56 +02001927static void copy_path_mixer_ctls(struct nid_path *dst, struct nid_path *src)
1928{
1929 dst->vol_ctl = src->vol_ctl;
1930 dst->mute_ctl = src->mute_ctl;
1931}
1932
Takashi Iwai4a796162011-06-17 17:53:38 +02001933/* add playback controls from the parsed DAC table */
1934static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
1935{
1936 struct via_spec *spec = codec->spec;
Takashi Iwaif4a78282011-06-17 18:46:48 +02001937 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai3214b962011-07-18 12:49:25 +02001938 struct nid_path *path;
Takashi Iwaiea734962011-01-17 11:29:34 +01001939 static const char * const chname[4] = {
1940 "Front", "Surround", "C/LFE", "Side"
1941 };
Takashi Iwai4a796162011-06-17 17:53:38 +02001942 int i, idx, err;
Takashi Iwaif4a78282011-06-17 18:46:48 +02001943 int old_line_outs;
1944
1945 /* check smart51 */
1946 old_line_outs = cfg->line_outs;
1947 if (cfg->line_outs == 1)
1948 mangle_smart51(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001949
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001950 err = via_auto_fill_dac_nids(codec);
1951 if (err < 0)
1952 return err;
1953
Lydia Wang5c9a5612011-07-08 14:03:43 +08001954 if (spec->multiout.num_dacs < 3) {
1955 spec->smart51_nums = 0;
1956 cfg->line_outs = old_line_outs;
1957 }
Takashi Iwai4a796162011-06-17 17:53:38 +02001958 for (i = 0; i < cfg->line_outs; i++) {
1959 hda_nid_t pin, dac;
1960 pin = cfg->line_out_pins[i];
1961 dac = spec->multiout.dac_nids[i];
1962 if (!pin || !dac)
Joseph Chanc577b8a2006-11-29 15:29:40 +01001963 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02001964 path = spec->out_path + i;
Takashi Iwai0fe0adf2011-06-19 16:27:53 +02001965 if (i == HDA_CLFE) {
Takashi Iwai3214b962011-07-18 12:49:25 +02001966 err = create_ch_ctls(codec, "Center", 1, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001967 if (err < 0)
1968 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02001969 err = create_ch_ctls(codec, "LFE", 2, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001970 if (err < 0)
1971 return err;
1972 } else {
Takashi Iwai6aadf412011-06-20 14:09:02 +02001973 const char *pfx = chname[i];
1974 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
1975 cfg->line_outs == 1)
1976 pfx = "Speaker";
Takashi Iwai3214b962011-07-18 12:49:25 +02001977 err = create_ch_ctls(codec, pfx, 3, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001978 if (err < 0)
1979 return err;
1980 }
Takashi Iwai020066d2011-07-21 13:45:56 +02001981 if (path != spec->out_path + i)
1982 copy_path_mixer_ctls(&spec->out_path[i], path);
1983 if (path == spec->out_path && spec->out_mix_path.depth)
1984 copy_path_mixer_ctls(&spec->out_mix_path, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001985 }
1986
Takashi Iwai4a796162011-06-17 17:53:38 +02001987 idx = get_connection_index(codec, spec->aa_mix_nid,
1988 spec->multiout.dac_nids[0]);
1989 if (idx >= 0) {
1990 /* add control to mixer */
Takashi Iwai3214b962011-07-18 12:49:25 +02001991 const char *name;
1992 name = spec->out_mix_path.depth ?
1993 "PCM Loopback Playback Volume" : "PCM Playback Volume";
1994 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
Takashi Iwai4a796162011-06-17 17:53:38 +02001995 HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
1996 idx, HDA_INPUT));
1997 if (err < 0)
1998 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02001999 name = spec->out_mix_path.depth ?
2000 "PCM Loopback Playback Switch" : "PCM Playback Switch";
2001 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
Takashi Iwai4a796162011-06-17 17:53:38 +02002002 HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2003 idx, HDA_INPUT));
2004 if (err < 0)
2005 return err;
2006 }
2007
Takashi Iwaif4a78282011-06-17 18:46:48 +02002008 cfg->line_outs = old_line_outs;
2009
Joseph Chanc577b8a2006-11-29 15:29:40 +01002010 return 0;
2011}
2012
Takashi Iwai4a796162011-06-17 17:53:38 +02002013static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002014{
Takashi Iwai4a796162011-06-17 17:53:38 +02002015 struct via_spec *spec = codec->spec;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02002016 struct nid_path *path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002017 bool check_dac;
Takashi Iwai3214b962011-07-18 12:49:25 +02002018 int i, err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002019
2020 if (!pin)
2021 return 0;
2022
Takashi Iwai3214b962011-07-18 12:49:25 +02002023 if (!parse_output_path(codec, pin, 0, 0, &spec->hp_indep_path)) {
2024 for (i = HDA_SIDE; i >= HDA_CLFE; i--) {
2025 if (i < spec->multiout.num_dacs &&
2026 parse_output_path(codec, pin,
2027 spec->multiout.dac_nids[i], 0,
2028 &spec->hp_indep_path)) {
2029 spec->hp_indep_shared = i;
2030 break;
2031 }
2032 }
Takashi Iwai25250502011-06-30 17:24:47 +02002033 }
Takashi Iwai3214b962011-07-18 12:49:25 +02002034 if (spec->hp_indep_path.depth) {
2035 spec->hp_dac_nid = spec->hp_indep_path.path[0];
2036 if (!spec->hp_indep_shared)
2037 spec->hp_path = spec->hp_indep_path;
2038 }
2039 /* optionally check front-path w/o AA-mix */
2040 if (!spec->hp_path.depth)
2041 parse_output_path(codec, pin,
2042 spec->multiout.dac_nids[HDA_FRONT], 0,
2043 &spec->hp_path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002044
Takashi Iwaiece8d042011-06-19 16:24:21 +02002045 if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
Takashi Iwai3214b962011-07-18 12:49:25 +02002046 1, &spec->hp_mix_path) && !spec->hp_path.depth)
Takashi Iwaiece8d042011-06-19 16:24:21 +02002047 return 0;
2048
Takashi Iwai3214b962011-07-18 12:49:25 +02002049 if (spec->hp_path.depth) {
Takashi Iwai09a9ad692011-06-21 15:57:44 +02002050 path = &spec->hp_path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002051 check_dac = true;
2052 } else {
Takashi Iwai3214b962011-07-18 12:49:25 +02002053 path = &spec->hp_mix_path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002054 check_dac = false;
2055 }
2056 err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002057 if (err < 0)
2058 return err;
Takashi Iwai020066d2011-07-21 13:45:56 +02002059 if (check_dac)
2060 copy_path_mixer_ctls(&spec->hp_mix_path, path);
2061 else
2062 copy_path_mixer_ctls(&spec->hp_path, path);
2063 if (spec->hp_indep_path.depth)
2064 copy_path_mixer_ctls(&spec->hp_indep_path, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002065 return 0;
2066}
2067
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002068static int via_auto_create_speaker_ctls(struct hda_codec *codec)
2069{
2070 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +02002071 struct nid_path *path;
2072 bool check_dac;
Wang Shaoyan81c0a782011-08-05 18:51:29 +08002073 hda_nid_t pin, dac = 0;
Takashi Iwai3214b962011-07-18 12:49:25 +02002074 int err;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002075
2076 pin = spec->autocfg.speaker_pins[0];
2077 if (!spec->autocfg.speaker_outs || !pin)
2078 return 0;
2079
Takashi Iwai3214b962011-07-18 12:49:25 +02002080 if (parse_output_path(codec, pin, 0, 0, &spec->speaker_path))
Takashi Iwai8e3679d2011-06-21 09:01:36 +02002081 dac = spec->speaker_path.path[0];
Takashi Iwai3214b962011-07-18 12:49:25 +02002082 if (!dac)
2083 parse_output_path(codec, pin,
2084 spec->multiout.dac_nids[HDA_FRONT], 0,
2085 &spec->speaker_path);
2086 if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
2087 1, &spec->speaker_mix_path) && !dac)
2088 return 0;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002089
Takashi Iwai3214b962011-07-18 12:49:25 +02002090 /* no AA-path for front? */
2091 if (!spec->out_mix_path.depth && spec->speaker_mix_path.depth)
2092 dac = 0;
2093
2094 spec->speaker_dac_nid = dac;
2095 spec->multiout.extra_out_nid[0] = dac;
2096 if (dac) {
2097 path = &spec->speaker_path;
2098 check_dac = true;
2099 } else {
2100 path = &spec->speaker_mix_path;
2101 check_dac = false;
2102 }
2103 err = create_ch_ctls(codec, "Speaker", 3, check_dac, path);
2104 if (err < 0)
2105 return err;
Takashi Iwai020066d2011-07-21 13:45:56 +02002106 if (check_dac)
2107 copy_path_mixer_ctls(&spec->speaker_mix_path, path);
2108 else
2109 copy_path_mixer_ctls(&spec->speaker_path, path);
Takashi Iwai3214b962011-07-18 12:49:25 +02002110 return 0;
2111}
2112
2113#define via_aamix_ctl_info via_pin_power_ctl_info
2114
2115static int via_aamix_ctl_get(struct snd_kcontrol *kcontrol,
2116 struct snd_ctl_elem_value *ucontrol)
2117{
2118 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2119 struct via_spec *spec = codec->spec;
2120 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2121 return 0;
2122}
2123
2124static void update_aamix_paths(struct hda_codec *codec, int do_mix,
2125 struct nid_path *nomix, struct nid_path *mix)
2126{
2127 if (do_mix) {
2128 activate_output_path(codec, nomix, false, false);
2129 activate_output_path(codec, mix, true, false);
2130 } else {
2131 activate_output_path(codec, mix, false, false);
2132 activate_output_path(codec, nomix, true, false);
2133 }
2134}
2135
2136static int via_aamix_ctl_put(struct snd_kcontrol *kcontrol,
2137 struct snd_ctl_elem_value *ucontrol)
2138{
2139 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2140 struct via_spec *spec = codec->spec;
2141 unsigned int val = ucontrol->value.enumerated.item[0];
2142
2143 if (val == spec->aamix_mode)
2144 return 0;
2145 spec->aamix_mode = val;
2146 /* update front path */
2147 update_aamix_paths(codec, val, &spec->out_path[0], &spec->out_mix_path);
2148 /* update HP path */
2149 if (!spec->hp_independent_mode) {
2150 update_aamix_paths(codec, val, &spec->hp_path,
2151 &spec->hp_mix_path);
2152 }
2153 /* update speaker path */
2154 update_aamix_paths(codec, val, &spec->speaker_path,
2155 &spec->speaker_mix_path);
2156 return 1;
2157}
2158
2159static const struct snd_kcontrol_new via_aamix_ctl_enum = {
2160 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2161 .name = "Loopback Mixing",
2162 .info = via_aamix_ctl_info,
2163 .get = via_aamix_ctl_get,
2164 .put = via_aamix_ctl_put,
2165};
2166
2167static int via_auto_create_loopback_switch(struct hda_codec *codec)
2168{
2169 struct via_spec *spec = codec->spec;
2170
Takashi Iwai4808d122012-01-10 15:16:02 +01002171 if (!spec->aa_mix_nid)
2172 return 0; /* no loopback switching available */
2173 if (!(spec->out_mix_path.depth || spec->hp_mix_path.depth ||
2174 spec->speaker_path.depth))
Takashi Iwai3214b962011-07-18 12:49:25 +02002175 return 0; /* no loopback switching available */
2176 if (!via_clone_control(spec, &via_aamix_ctl_enum))
2177 return -ENOMEM;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002178 return 0;
2179}
2180
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002181/* look for ADCs */
2182static int via_fill_adcs(struct hda_codec *codec)
2183{
2184 struct via_spec *spec = codec->spec;
2185 hda_nid_t nid = codec->start_nid;
2186 int i;
2187
2188 for (i = 0; i < codec->num_nodes; i++, nid++) {
2189 unsigned int wcaps = get_wcaps(codec, nid);
2190 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2191 continue;
2192 if (wcaps & AC_WCAP_DIGITAL)
2193 continue;
2194 if (!(wcaps & AC_WCAP_CONN_LIST))
2195 continue;
2196 if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2197 return -ENOMEM;
2198 spec->adc_nids[spec->num_adc_nids++] = nid;
2199 }
2200 return 0;
2201}
2202
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002203/* input-src control */
2204static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
2205 struct snd_ctl_elem_info *uinfo)
2206{
2207 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2208 struct via_spec *spec = codec->spec;
2209
2210 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2211 uinfo->count = 1;
2212 uinfo->value.enumerated.items = spec->num_inputs;
2213 if (uinfo->value.enumerated.item >= spec->num_inputs)
2214 uinfo->value.enumerated.item = spec->num_inputs - 1;
2215 strcpy(uinfo->value.enumerated.name,
2216 spec->inputs[uinfo->value.enumerated.item].label);
2217 return 0;
2218}
2219
2220static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
2221 struct snd_ctl_elem_value *ucontrol)
2222{
2223 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2224 struct via_spec *spec = codec->spec;
2225 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2226
2227 ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
2228 return 0;
2229}
2230
2231static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
2232 struct snd_ctl_elem_value *ucontrol)
2233{
2234 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2235 struct via_spec *spec = codec->spec;
2236 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2237 hda_nid_t mux;
2238 int cur;
2239
2240 cur = ucontrol->value.enumerated.item[0];
2241 if (cur < 0 || cur >= spec->num_inputs)
2242 return -EINVAL;
2243 if (spec->cur_mux[idx] == cur)
2244 return 0;
2245 spec->cur_mux[idx] = cur;
2246 if (spec->dyn_adc_switch) {
2247 int adc_idx = spec->inputs[cur].adc_idx;
2248 mux = spec->mux_nids[adc_idx];
2249 via_dyn_adc_pcm_resetup(codec, cur);
2250 } else {
2251 mux = spec->mux_nids[idx];
2252 if (snd_BUG_ON(!mux))
2253 return -EINVAL;
2254 }
2255
2256 if (mux) {
2257 /* switch to D0 beofre change index */
Takashi Iwai054d8672012-01-24 12:25:50 +01002258 update_power_state(codec, mux, AC_PWRST_D0);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002259 snd_hda_codec_write(codec, mux, 0,
2260 AC_VERB_SET_CONNECT_SEL,
2261 spec->inputs[cur].mux_idx);
2262 }
2263
2264 /* update jack power state */
2265 set_widgets_power_state(codec);
2266 return 0;
2267}
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002268
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002269static const struct snd_kcontrol_new via_input_src_ctl = {
2270 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2271 /* The multiple "Capture Source" controls confuse alsamixer
2272 * So call somewhat different..
2273 */
2274 /* .name = "Capture Source", */
2275 .name = "Input Source",
2276 .info = via_mux_enum_info,
2277 .get = via_mux_enum_get,
2278 .put = via_mux_enum_put,
2279};
2280
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002281static int create_input_src_ctls(struct hda_codec *codec, int count)
2282{
2283 struct via_spec *spec = codec->spec;
2284 struct snd_kcontrol_new *knew;
2285
2286 if (spec->num_inputs <= 1 || !count)
2287 return 0; /* no need for single src */
2288
2289 knew = via_clone_control(spec, &via_input_src_ctl);
2290 if (!knew)
2291 return -ENOMEM;
2292 knew->count = count;
2293 return 0;
2294}
2295
2296/* add the powersave loopback-list entry */
Takashi Iwai13af8e72011-06-20 14:05:46 +02002297static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
2298{
2299 struct hda_amp_list *list;
2300
2301 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2302 return;
2303 list = spec->loopback_list + spec->num_loopbacks;
2304 list->nid = mix;
2305 list->dir = HDA_INPUT;
2306 list->idx = idx;
2307 spec->num_loopbacks++;
2308 spec->loopback.amplist = spec->loopback_list;
2309}
Takashi Iwai13af8e72011-06-20 14:05:46 +02002310
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002311static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
Takashi Iwai8d087c72011-06-28 12:45:47 +02002312 hda_nid_t dst)
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002313{
Takashi Iwai8d087c72011-06-28 12:45:47 +02002314 return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002315}
2316
2317/* add the input-route to the given pin */
2318static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002319{
Takashi Iwai10a20af2010-09-09 16:28:02 +02002320 struct via_spec *spec = codec->spec;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002321 int c, idx;
2322
2323 spec->inputs[spec->num_inputs].adc_idx = -1;
2324 spec->inputs[spec->num_inputs].pin = pin;
2325 for (c = 0; c < spec->num_adc_nids; c++) {
2326 if (spec->mux_nids[c]) {
2327 idx = get_connection_index(codec, spec->mux_nids[c],
2328 pin);
2329 if (idx < 0)
2330 continue;
2331 spec->inputs[spec->num_inputs].mux_idx = idx;
2332 } else {
Takashi Iwai8d087c72011-06-28 12:45:47 +02002333 if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002334 continue;
2335 }
2336 spec->inputs[spec->num_inputs].adc_idx = c;
2337 /* Can primary ADC satisfy all inputs? */
2338 if (!spec->dyn_adc_switch &&
2339 spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
2340 snd_printd(KERN_INFO
2341 "via: dynamic ADC switching enabled\n");
2342 spec->dyn_adc_switch = 1;
2343 }
2344 return true;
2345 }
2346 return false;
2347}
2348
2349static int get_mux_nids(struct hda_codec *codec);
2350
2351/* parse input-routes; fill ADCs, MUXs and input-src entries */
2352static int parse_analog_inputs(struct hda_codec *codec)
2353{
2354 struct via_spec *spec = codec->spec;
2355 const struct auto_pin_cfg *cfg = &spec->autocfg;
2356 int i, err;
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002357
2358 err = via_fill_adcs(codec);
2359 if (err < 0)
2360 return err;
2361 err = get_mux_nids(codec);
2362 if (err < 0)
2363 return err;
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002364
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002365 /* fill all input-routes */
2366 for (i = 0; i < cfg->num_inputs; i++) {
2367 if (add_input_route(codec, cfg->inputs[i].pin))
2368 spec->inputs[spec->num_inputs++].label =
2369 hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwaif3268512010-08-30 11:00:19 +02002370 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002371
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002372 /* check for internal loopback recording */
2373 if (spec->aa_mix_nid &&
2374 add_input_route(codec, spec->aa_mix_nid))
2375 spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
2376
2377 return 0;
2378}
2379
2380/* create analog-loopback volume/switch controls */
2381static int create_loopback_ctls(struct hda_codec *codec)
2382{
2383 struct via_spec *spec = codec->spec;
2384 const struct auto_pin_cfg *cfg = &spec->autocfg;
2385 const char *prev_label = NULL;
2386 int type_idx = 0;
2387 int i, j, err, idx;
2388
2389 if (!spec->aa_mix_nid)
2390 return 0;
2391
Takashi Iwai7b315bb2010-08-30 13:06:30 +02002392 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002393 hda_nid_t pin = cfg->inputs[i].pin;
2394 const char *label = hda_get_autocfg_input_label(codec, cfg, i);
2395
Takashi Iwai1e11cae2011-06-21 12:57:22 +02002396 if (prev_label && !strcmp(label, prev_label))
Takashi Iwai7b315bb2010-08-30 13:06:30 +02002397 type_idx++;
2398 else
2399 type_idx = 0;
Takashi Iwai1e11cae2011-06-21 12:57:22 +02002400 prev_label = label;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002401 idx = get_connection_index(codec, spec->aa_mix_nid, pin);
2402 if (idx >= 0) {
Lydia Wang16922282011-03-22 16:24:10 +08002403 err = via_new_analog_input(spec, label, type_idx,
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002404 idx, spec->aa_mix_nid);
Takashi Iwai13af8e72011-06-20 14:05:46 +02002405 if (err < 0)
2406 return err;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002407 add_loopback_list(spec, spec->aa_mix_nid, idx);
Takashi Iwai13af8e72011-06-20 14:05:46 +02002408 }
Takashi Iwaie3d7a142011-06-20 13:52:33 +02002409
2410 /* remember the label for smart51 control */
2411 for (j = 0; j < spec->smart51_nums; j++) {
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002412 if (spec->smart51_pins[j] == pin) {
Takashi Iwaie3d7a142011-06-20 13:52:33 +02002413 spec->smart51_idxs[j] = idx;
2414 spec->smart51_labels[j] = label;
2415 break;
2416 }
2417 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002418 }
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002419 return 0;
2420}
2421
2422/* create mic-boost controls (if present) */
2423static int create_mic_boost_ctls(struct hda_codec *codec)
2424{
2425 struct via_spec *spec = codec->spec;
2426 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai8d8bbc62012-02-22 12:26:38 +01002427 const char *prev_label = NULL;
2428 int type_idx = 0;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002429 int i, err;
2430
2431 for (i = 0; i < cfg->num_inputs; i++) {
2432 hda_nid_t pin = cfg->inputs[i].pin;
2433 unsigned int caps;
2434 const char *label;
2435 char name[32];
2436
2437 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2438 continue;
2439 caps = query_amp_caps(codec, pin, HDA_INPUT);
2440 if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2441 continue;
2442 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai8d8bbc62012-02-22 12:26:38 +01002443 if (prev_label && !strcmp(label, prev_label))
2444 type_idx++;
2445 else
2446 type_idx = 0;
2447 prev_label = label;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002448 snprintf(name, sizeof(name), "%s Boost Volume", label);
Takashi Iwai8d8bbc62012-02-22 12:26:38 +01002449 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002450 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2451 if (err < 0)
2452 return err;
2453 }
2454 return 0;
2455}
2456
2457/* create capture and input-src controls for multiple streams */
2458static int create_multi_adc_ctls(struct hda_codec *codec)
2459{
2460 struct via_spec *spec = codec->spec;
2461 int i, err;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002462
2463 /* create capture mixer elements */
2464 for (i = 0; i < spec->num_adc_nids; i++) {
2465 hda_nid_t adc = spec->adc_nids[i];
2466 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2467 "Capture Volume", i,
2468 HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2469 HDA_INPUT));
2470 if (err < 0)
2471 return err;
2472 err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2473 "Capture Switch", i,
2474 HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2475 HDA_INPUT));
2476 if (err < 0)
2477 return err;
2478 }
2479
2480 /* input-source control */
2481 for (i = 0; i < spec->num_adc_nids; i++)
2482 if (!spec->mux_nids[i])
2483 break;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002484 err = create_input_src_ctls(codec, i);
2485 if (err < 0)
2486 return err;
2487 return 0;
2488}
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002489
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002490/* bind capture volume/switch */
2491static struct snd_kcontrol_new via_bind_cap_vol_ctl =
2492 HDA_BIND_VOL("Capture Volume", 0);
2493static struct snd_kcontrol_new via_bind_cap_sw_ctl =
2494 HDA_BIND_SW("Capture Switch", 0);
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002495
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002496static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
2497 struct hda_ctl_ops *ops)
2498{
2499 struct hda_bind_ctls *ctl;
2500 int i;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002501
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002502 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
2503 if (!ctl)
2504 return -ENOMEM;
2505 ctl->ops = ops;
2506 for (i = 0; i < spec->num_adc_nids; i++)
2507 ctl->values[i] =
2508 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
2509 *ctl_ret = ctl;
2510 return 0;
2511}
2512
2513/* create capture and input-src controls for dynamic ADC-switch case */
2514static int create_dyn_adc_ctls(struct hda_codec *codec)
2515{
2516 struct via_spec *spec = codec->spec;
2517 struct snd_kcontrol_new *knew;
2518 int err;
2519
2520 /* set up the bind capture ctls */
2521 err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
2522 if (err < 0)
2523 return err;
2524 err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
2525 if (err < 0)
2526 return err;
2527
2528 /* create capture mixer elements */
2529 knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
2530 if (!knew)
2531 return -ENOMEM;
2532 knew->private_value = (long)spec->bind_cap_vol;
2533
2534 knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
2535 if (!knew)
2536 return -ENOMEM;
2537 knew->private_value = (long)spec->bind_cap_sw;
2538
2539 /* input-source control */
2540 err = create_input_src_ctls(codec, 1);
2541 if (err < 0)
2542 return err;
2543 return 0;
2544}
2545
2546/* parse and create capture-related stuff */
2547static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
2548{
2549 struct via_spec *spec = codec->spec;
2550 int err;
2551
2552 err = parse_analog_inputs(codec);
2553 if (err < 0)
2554 return err;
2555 if (spec->dyn_adc_switch)
2556 err = create_dyn_adc_ctls(codec);
2557 else
2558 err = create_multi_adc_ctls(codec);
2559 if (err < 0)
2560 return err;
2561 err = create_loopback_ctls(codec);
2562 if (err < 0)
2563 return err;
2564 err = create_mic_boost_ctls(codec);
2565 if (err < 0)
2566 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002567 return 0;
2568}
2569
Harald Welte76d9b0d2008-09-09 15:50:37 +08002570static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
2571{
2572 unsigned int def_conf;
2573 unsigned char seqassoc;
2574
Takashi Iwai2f334f92009-02-20 14:37:42 +01002575 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Harald Welte76d9b0d2008-09-09 15:50:37 +08002576 seqassoc = (unsigned char) get_defcfg_association(def_conf);
2577 seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
Lydia Wang82ef9e42009-10-10 19:08:19 +08002578 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
2579 && (seqassoc == 0xf0 || seqassoc == 0xff)) {
2580 def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
2581 snd_hda_codec_set_pincfg(codec, nid, def_conf);
Harald Welte76d9b0d2008-09-09 15:50:37 +08002582 }
2583
2584 return;
2585}
2586
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002587static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002588 struct snd_ctl_elem_value *ucontrol)
2589{
2590 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2591 struct via_spec *spec = codec->spec;
2592
2593 if (spec->codec_type != VT1708)
2594 return 0;
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002595 ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002596 return 0;
2597}
2598
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002599static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002600 struct snd_ctl_elem_value *ucontrol)
2601{
2602 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2603 struct via_spec *spec = codec->spec;
Takashi Iwai187d3332011-11-24 16:33:09 +01002604 int val;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002605
2606 if (spec->codec_type != VT1708)
2607 return 0;
Takashi Iwai187d3332011-11-24 16:33:09 +01002608 val = !!ucontrol->value.integer.value[0];
2609 if (spec->vt1708_jack_detect == val)
2610 return 0;
2611 spec->vt1708_jack_detect = val;
2612 if (spec->vt1708_jack_detect &&
2613 snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") != 1) {
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002614 mute_aa_path(codec, 1);
2615 notify_aa_path_ctls(codec);
2616 }
Takashi Iwai187d3332011-11-24 16:33:09 +01002617 via_hp_automute(codec);
2618 vt1708_update_hp_work(spec);
2619 return 1;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002620}
2621
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002622static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
2623 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2624 .name = "Jack Detect",
2625 .count = 1,
2626 .info = snd_ctl_boolean_mono_info,
2627 .get = vt1708_jack_detect_get,
2628 .put = vt1708_jack_detect_put,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002629};
2630
Takashi Iwai12daef62011-06-18 17:45:49 +02002631static void fill_dig_outs(struct hda_codec *codec);
2632static void fill_dig_in(struct hda_codec *codec);
2633
2634static int via_parse_auto_config(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002635{
2636 struct via_spec *spec = codec->spec;
2637 int err;
2638
2639 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2640 if (err < 0)
2641 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002642 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
Takashi Iwai7f0df882011-06-18 17:33:34 +02002643 return -EINVAL;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002644
Takashi Iwai4a796162011-06-17 17:53:38 +02002645 err = via_auto_create_multi_out_ctls(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002646 if (err < 0)
2647 return err;
Takashi Iwai4a796162011-06-17 17:53:38 +02002648 err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002649 if (err < 0)
2650 return err;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002651 err = via_auto_create_speaker_ctls(codec);
2652 if (err < 0)
2653 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02002654 err = via_auto_create_loopback_switch(codec);
2655 if (err < 0)
2656 return err;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002657 err = via_auto_create_analog_input_ctls(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002658 if (err < 0)
2659 return err;
2660
2661 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2662
Takashi Iwai12daef62011-06-18 17:45:49 +02002663 fill_dig_outs(codec);
2664 fill_dig_in(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002665
Takashi Iwai603c4012008-07-30 15:01:44 +02002666 if (spec->kctls.list)
2667 spec->mixers[spec->num_mixers++] = spec->kctls.list;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002668
Joseph Chanc577b8a2006-11-29 15:29:40 +01002669
Takashi Iwai3214b962011-07-18 12:49:25 +02002670 if (spec->hp_dac_nid && spec->hp_mix_path.depth) {
Takashi Iwaiece8d042011-06-19 16:24:21 +02002671 err = via_hp_build(codec);
2672 if (err < 0)
2673 return err;
2674 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002675
Takashi Iwaif4a78282011-06-17 18:46:48 +02002676 err = via_smart51_build(codec);
2677 if (err < 0)
2678 return err;
2679
Takashi Iwai5d417622011-06-20 11:32:27 +02002680 /* assign slave outs */
2681 if (spec->slave_dig_outs[0])
2682 codec->slave_dig_outs = spec->slave_dig_outs;
2683
Joseph Chanc577b8a2006-11-29 15:29:40 +01002684 return 1;
2685}
2686
Takashi Iwai5d417622011-06-20 11:32:27 +02002687static void via_auto_init_dig_outs(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002688{
Lydia Wang25eaba22009-10-10 19:08:43 +08002689 struct via_spec *spec = codec->spec;
Takashi Iwai5d417622011-06-20 11:32:27 +02002690 if (spec->multiout.dig_out_nid)
2691 init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
2692 if (spec->slave_dig_outs[0])
2693 init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
2694}
Lydia Wang25eaba22009-10-10 19:08:43 +08002695
Takashi Iwai5d417622011-06-20 11:32:27 +02002696static void via_auto_init_dig_in(struct hda_codec *codec)
2697{
2698 struct via_spec *spec = codec->spec;
2699 if (!spec->dig_in_nid)
2700 return;
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02002701 snd_hda_set_pin_ctl(codec, spec->autocfg.dig_in_pin, PIN_IN);
Takashi Iwai5d417622011-06-20 11:32:27 +02002702}
2703
David Henningsson4e2d16d2012-10-08 15:44:14 +02002704static void via_jack_output_event(struct hda_codec *codec, struct hda_jack_tbl *tbl)
2705{
2706 set_widgets_power_state(codec);
2707 via_hp_automute(codec);
2708}
2709
2710static void via_jack_powerstate_event(struct hda_codec *codec, struct hda_jack_tbl *tbl)
2711{
2712 set_widgets_power_state(codec);
2713}
2714
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002715/* initialize the unsolicited events */
2716static void via_auto_init_unsol_event(struct hda_codec *codec)
2717{
2718 struct via_spec *spec = codec->spec;
2719 struct auto_pin_cfg *cfg = &spec->autocfg;
2720 unsigned int ev;
2721 int i;
David Henningsson4e2d16d2012-10-08 15:44:14 +02002722 hda_jack_callback cb;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002723
2724 if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
David Henningsson4e2d16d2012-10-08 15:44:14 +02002725 snd_hda_jack_detect_enable_callback(codec, cfg->hp_pins[0],
2726 VIA_HP_EVENT | VIA_JACK_EVENT,
2727 via_jack_output_event);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002728
2729 if (cfg->speaker_pins[0])
2730 ev = VIA_LINE_EVENT;
2731 else
2732 ev = 0;
David Henningsson4e2d16d2012-10-08 15:44:14 +02002733 cb = ev ? via_jack_output_event : via_jack_powerstate_event;
2734
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002735 for (i = 0; i < cfg->line_outs; i++) {
2736 if (cfg->line_out_pins[i] &&
2737 is_jack_detectable(codec, cfg->line_out_pins[i]))
David Henningsson4e2d16d2012-10-08 15:44:14 +02002738 snd_hda_jack_detect_enable_callback(codec, cfg->line_out_pins[i],
2739 ev | VIA_JACK_EVENT, cb);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002740 }
2741
2742 for (i = 0; i < cfg->num_inputs; i++) {
2743 if (is_jack_detectable(codec, cfg->inputs[i].pin))
David Henningsson4e2d16d2012-10-08 15:44:14 +02002744 snd_hda_jack_detect_enable_callback(codec, cfg->inputs[i].pin,
2745 VIA_JACK_EVENT,
2746 via_jack_powerstate_event);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002747 }
2748}
2749
Takashi Iwai5d417622011-06-20 11:32:27 +02002750static int via_init(struct hda_codec *codec)
2751{
2752 struct via_spec *spec = codec->spec;
2753 int i;
2754
2755 for (i = 0; i < spec->num_iverbs; i++)
2756 snd_hda_sequence_write(codec, spec->init_verbs[i]);
2757
Takashi Iwaie9d010c2012-02-01 10:33:23 +01002758 /* init power states */
2759 set_widgets_power_state(codec);
2760 __analog_low_current_mode(codec, true);
2761
Joseph Chanc577b8a2006-11-29 15:29:40 +01002762 via_auto_init_multi_out(codec);
2763 via_auto_init_hp_out(codec);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002764 via_auto_init_speaker_out(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002765 via_auto_init_analog_input(codec);
Takashi Iwai5d417622011-06-20 11:32:27 +02002766 via_auto_init_dig_outs(codec);
2767 via_auto_init_dig_in(codec);
Lydia Wang118909562011-03-23 17:57:34 +08002768
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002769 via_auto_init_unsol_event(codec);
2770
2771 via_hp_automute(codec);
Takashi Iwai187d3332011-11-24 16:33:09 +01002772 vt1708_update_hp_work(spec);
Lydia Wang25eaba22009-10-10 19:08:43 +08002773
Joseph Chanc577b8a2006-11-29 15:29:40 +01002774 return 0;
2775}
2776
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002777static void vt1708_update_hp_jack_state(struct work_struct *work)
2778{
2779 struct via_spec *spec = container_of(work, struct via_spec,
2780 vt1708_hp_work.work);
2781 if (spec->codec_type != VT1708)
2782 return;
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002783 snd_hda_jack_set_dirty_all(spec->codec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002784 /* if jack state toggled */
2785 if (spec->vt1708_hp_present
Takashi Iwaid56757a2009-11-18 08:00:14 +01002786 != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002787 spec->vt1708_hp_present ^= 1;
2788 via_hp_automute(spec->codec);
2789 }
Takashi Iwai187d3332011-11-24 16:33:09 +01002790 if (spec->vt1708_jack_detect)
2791 schedule_delayed_work(&spec->vt1708_hp_work,
2792 msecs_to_jiffies(100));
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002793}
2794
Takashi Iwai337b9d02009-07-07 18:18:59 +02002795static int get_mux_nids(struct hda_codec *codec)
2796{
2797 struct via_spec *spec = codec->spec;
2798 hda_nid_t nid, conn[8];
2799 unsigned int type;
2800 int i, n;
2801
2802 for (i = 0; i < spec->num_adc_nids; i++) {
2803 nid = spec->adc_nids[i];
2804 while (nid) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002805 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai1c55d522009-07-08 07:45:46 +02002806 if (type == AC_WID_PIN)
2807 break;
Takashi Iwai337b9d02009-07-07 18:18:59 +02002808 n = snd_hda_get_connections(codec, nid, conn,
2809 ARRAY_SIZE(conn));
2810 if (n <= 0)
2811 break;
2812 if (n > 1) {
2813 spec->mux_nids[i] = nid;
2814 break;
2815 }
2816 nid = conn[0];
2817 }
2818 }
Takashi Iwai1c55d522009-07-08 07:45:46 +02002819 return 0;
Takashi Iwai337b9d02009-07-07 18:18:59 +02002820}
2821
Joseph Chanc577b8a2006-11-29 15:29:40 +01002822static int patch_vt1708(struct hda_codec *codec)
2823{
2824 struct via_spec *spec;
2825 int err;
2826
2827 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002828 spec = via_new_spec(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002829 if (spec == NULL)
2830 return -ENOMEM;
2831
Takashi Iwai620e2b22011-06-17 17:19:19 +02002832 spec->aa_mix_nid = 0x17;
2833
Takashi Iwai12daef62011-06-18 17:45:49 +02002834 /* Add HP and CD pin config connect bit re-config action */
2835 vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
2836 vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
2837
Joseph Chanc577b8a2006-11-29 15:29:40 +01002838 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02002839 err = via_parse_auto_config(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002840 if (err < 0) {
2841 via_free(codec);
2842 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002843 }
2844
Takashi Iwai12daef62011-06-18 17:45:49 +02002845 /* add jack detect on/off control */
2846 if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
2847 return -ENOMEM;
2848
Takashi Iwaibc9b562382008-05-23 17:50:27 +02002849 /* disable 32bit format on VT1708 */
2850 if (codec->vendor_id == 0x11061708)
2851 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002852
Lydia Wange322a362011-06-29 13:52:02 +08002853 spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2854
Joseph Chanc577b8a2006-11-29 15:29:40 +01002855 codec->patch_ops = via_patch_ops;
2856
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002857 INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002858 return 0;
2859}
2860
Takashi Iwaiddd304d2011-06-21 16:33:55 +02002861static int patch_vt1709(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002862{
2863 struct via_spec *spec;
2864 int err;
2865
2866 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002867 spec = via_new_spec(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002868 if (spec == NULL)
2869 return -ENOMEM;
2870
Takashi Iwai620e2b22011-06-17 17:19:19 +02002871 spec->aa_mix_nid = 0x18;
2872
Takashi Iwai12daef62011-06-18 17:45:49 +02002873 err = via_parse_auto_config(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002874 if (err < 0) {
2875 via_free(codec);
2876 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002877 }
2878
Joseph Chanc577b8a2006-11-29 15:29:40 +01002879 codec->patch_ops = via_patch_ops;
2880
Josepch Chanf7278fd2007-12-13 16:40:40 +01002881 return 0;
2882}
2883
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002884static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
2885{
2886 struct via_spec *spec = codec->spec;
2887 int imux_is_smixer;
2888 unsigned int parm;
2889 int is_8ch = 0;
Lydia Wangbc92df72011-03-23 17:56:05 +08002890 if ((spec->codec_type != VT1708B_4CH) &&
2891 (codec->vendor_id != 0x11064397))
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002892 is_8ch = 1;
2893
2894 /* SW0 (17h) = stereo mixer */
2895 imux_is_smixer =
2896 (snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
2897 == ((spec->codec_type == VT1708S) ? 5 : 0));
2898 /* inputs */
2899 /* PW 1/2/5 (1ah/1bh/1eh) */
2900 parm = AC_PWRST_D3;
2901 set_pin_power_state(codec, 0x1a, &parm);
2902 set_pin_power_state(codec, 0x1b, &parm);
2903 set_pin_power_state(codec, 0x1e, &parm);
2904 if (imux_is_smixer)
2905 parm = AC_PWRST_D0;
2906 /* SW0 (17h), AIW 0/1 (13h/14h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01002907 update_power_state(codec, 0x17, parm);
2908 update_power_state(codec, 0x13, parm);
2909 update_power_state(codec, 0x14, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002910
2911 /* outputs */
2912 /* PW0 (19h), SW1 (18h), AOW1 (11h) */
2913 parm = AC_PWRST_D3;
2914 set_pin_power_state(codec, 0x19, &parm);
2915 if (spec->smart51_enabled)
2916 set_pin_power_state(codec, 0x1b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002917 update_power_state(codec, 0x18, parm);
2918 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002919
2920 /* PW6 (22h), SW2 (26h), AOW2 (24h) */
2921 if (is_8ch) {
2922 parm = AC_PWRST_D3;
2923 set_pin_power_state(codec, 0x22, &parm);
2924 if (spec->smart51_enabled)
2925 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002926 update_power_state(codec, 0x26, parm);
2927 update_power_state(codec, 0x24, parm);
Lydia Wangbc92df72011-03-23 17:56:05 +08002928 } else if (codec->vendor_id == 0x11064397) {
2929 /* PW7(23h), SW2(27h), AOW2(25h) */
2930 parm = AC_PWRST_D3;
2931 set_pin_power_state(codec, 0x23, &parm);
2932 if (spec->smart51_enabled)
2933 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002934 update_power_state(codec, 0x27, parm);
2935 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002936 }
2937
2938 /* PW 3/4/7 (1ch/1dh/23h) */
2939 parm = AC_PWRST_D3;
2940 /* force to D0 for internal Speaker */
2941 set_pin_power_state(codec, 0x1c, &parm);
2942 set_pin_power_state(codec, 0x1d, &parm);
2943 if (is_8ch)
2944 set_pin_power_state(codec, 0x23, &parm);
2945
2946 /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01002947 update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
2948 update_power_state(codec, 0x10, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002949 if (is_8ch) {
Takashi Iwai054d8672012-01-24 12:25:50 +01002950 update_power_state(codec, 0x25, parm);
2951 update_power_state(codec, 0x27, parm);
Lydia Wangbc92df72011-03-23 17:56:05 +08002952 } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01002953 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002954}
2955
Lydia Wang518bf3b2009-10-10 19:07:29 +08002956static int patch_vt1708S(struct hda_codec *codec);
Takashi Iwaiddd304d2011-06-21 16:33:55 +02002957static int patch_vt1708B(struct hda_codec *codec)
Josepch Chanf7278fd2007-12-13 16:40:40 +01002958{
2959 struct via_spec *spec;
2960 int err;
2961
Lydia Wang518bf3b2009-10-10 19:07:29 +08002962 if (get_codec_type(codec) == VT1708BCE)
2963 return patch_vt1708S(codec);
Takashi Iwaiddd304d2011-06-21 16:33:55 +02002964
Josepch Chanf7278fd2007-12-13 16:40:40 +01002965 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002966 spec = via_new_spec(codec);
Josepch Chanf7278fd2007-12-13 16:40:40 +01002967 if (spec == NULL)
2968 return -ENOMEM;
2969
Takashi Iwai620e2b22011-06-17 17:19:19 +02002970 spec->aa_mix_nid = 0x16;
2971
Josepch Chanf7278fd2007-12-13 16:40:40 +01002972 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02002973 err = via_parse_auto_config(codec);
Josepch Chanf7278fd2007-12-13 16:40:40 +01002974 if (err < 0) {
2975 via_free(codec);
2976 return err;
Josepch Chanf7278fd2007-12-13 16:40:40 +01002977 }
2978
Josepch Chanf7278fd2007-12-13 16:40:40 +01002979 codec->patch_ops = via_patch_ops;
2980
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002981 spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
2982
Josepch Chanf7278fd2007-12-13 16:40:40 +01002983 return 0;
2984}
2985
Harald Welted949cac2008-09-09 15:56:01 +08002986/* Patch for VT1708S */
Takashi Iwai096a8852011-06-20 12:09:02 +02002987static const struct hda_verb vt1708S_init_verbs[] = {
Harald Welted7426322008-09-15 22:43:23 +08002988 /* Enable Mic Boost Volume backdoor */
2989 {0x1, 0xf98, 0x1},
Lydia Wangbc7e7e52009-10-10 19:08:32 +08002990 /* don't bybass mixer */
2991 {0x1, 0xf88, 0xc0},
Harald Welted949cac2008-09-09 15:56:01 +08002992 { }
2993};
2994
Takashi Iwai9da29272009-05-07 16:31:14 +02002995/* fill out digital output widgets; one for master and one for slave outputs */
2996static void fill_dig_outs(struct hda_codec *codec)
2997{
2998 struct via_spec *spec = codec->spec;
2999 int i;
3000
3001 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3002 hda_nid_t nid;
3003 int conn;
3004
3005 nid = spec->autocfg.dig_out_pins[i];
3006 if (!nid)
3007 continue;
3008 conn = snd_hda_get_connections(codec, nid, &nid, 1);
3009 if (conn < 1)
3010 continue;
3011 if (!spec->multiout.dig_out_nid)
3012 spec->multiout.dig_out_nid = nid;
3013 else {
3014 spec->slave_dig_outs[0] = nid;
3015 break; /* at most two dig outs */
3016 }
3017 }
3018}
3019
Takashi Iwai12daef62011-06-18 17:45:49 +02003020static void fill_dig_in(struct hda_codec *codec)
Harald Welted949cac2008-09-09 15:56:01 +08003021{
3022 struct via_spec *spec = codec->spec;
Takashi Iwai12daef62011-06-18 17:45:49 +02003023 hda_nid_t dig_nid;
3024 int i, err;
Harald Welted949cac2008-09-09 15:56:01 +08003025
Takashi Iwai12daef62011-06-18 17:45:49 +02003026 if (!spec->autocfg.dig_in_pin)
3027 return;
Harald Welted949cac2008-09-09 15:56:01 +08003028
Takashi Iwai12daef62011-06-18 17:45:49 +02003029 dig_nid = codec->start_nid;
3030 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3031 unsigned int wcaps = get_wcaps(codec, dig_nid);
3032 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3033 continue;
3034 if (!(wcaps & AC_WCAP_DIGITAL))
3035 continue;
3036 if (!(wcaps & AC_WCAP_CONN_LIST))
3037 continue;
3038 err = get_connection_index(codec, dig_nid,
3039 spec->autocfg.dig_in_pin);
3040 if (err >= 0) {
3041 spec->dig_in_nid = dig_nid;
3042 break;
3043 }
3044 }
Harald Welted949cac2008-09-09 15:56:01 +08003045}
3046
Lydia Wang6369bcf2009-10-10 19:08:31 +08003047static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
3048 int offset, int num_steps, int step_size)
3049{
3050 snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
3051 (offset << AC_AMPCAP_OFFSET_SHIFT) |
3052 (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
3053 (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
3054 (0 << AC_AMPCAP_MUTE_SHIFT));
3055}
3056
Harald Welted949cac2008-09-09 15:56:01 +08003057static int patch_vt1708S(struct hda_codec *codec)
3058{
3059 struct via_spec *spec;
3060 int err;
3061
3062 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003063 spec = via_new_spec(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003064 if (spec == NULL)
3065 return -ENOMEM;
3066
Takashi Iwai620e2b22011-06-17 17:19:19 +02003067 spec->aa_mix_nid = 0x16;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003068 override_mic_boost(codec, 0x1a, 0, 3, 40);
3069 override_mic_boost(codec, 0x1e, 0, 3, 40);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003070
Harald Welted949cac2008-09-09 15:56:01 +08003071 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003072 err = via_parse_auto_config(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003073 if (err < 0) {
3074 via_free(codec);
3075 return err;
Harald Welted949cac2008-09-09 15:56:01 +08003076 }
3077
Takashi Iwai096a8852011-06-20 12:09:02 +02003078 spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
Harald Welted949cac2008-09-09 15:56:01 +08003079
Harald Welted949cac2008-09-09 15:56:01 +08003080 codec->patch_ops = via_patch_ops;
3081
Lydia Wang518bf3b2009-10-10 19:07:29 +08003082 /* correct names for VT1708BCE */
3083 if (get_codec_type(codec) == VT1708BCE) {
3084 kfree(codec->chip_name);
3085 codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3086 snprintf(codec->bus->card->mixername,
3087 sizeof(codec->bus->card->mixername),
3088 "%s %s", codec->vendor_name, codec->chip_name);
Lydia Wang970f630f2011-03-22 16:25:56 +08003089 }
Lydia Wangbc92df72011-03-23 17:56:05 +08003090 /* correct names for VT1705 */
3091 if (codec->vendor_id == 0x11064397) {
3092 kfree(codec->chip_name);
3093 codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3094 snprintf(codec->bus->card->mixername,
3095 sizeof(codec->bus->card->mixername),
3096 "%s %s", codec->vendor_name, codec->chip_name);
3097 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003098 spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
Harald Welted949cac2008-09-09 15:56:01 +08003099 return 0;
3100}
3101
3102/* Patch for VT1702 */
3103
Takashi Iwai096a8852011-06-20 12:09:02 +02003104static const struct hda_verb vt1702_init_verbs[] = {
Lydia Wangbc7e7e52009-10-10 19:08:32 +08003105 /* mixer enable */
3106 {0x1, 0xF88, 0x3},
3107 /* GPIO 0~2 */
3108 {0x1, 0xF82, 0x3F},
Harald Welted949cac2008-09-09 15:56:01 +08003109 { }
3110};
3111
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003112static void set_widgets_power_state_vt1702(struct hda_codec *codec)
3113{
3114 int imux_is_smixer =
3115 snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3116 unsigned int parm;
3117 /* inputs */
3118 /* PW 1/2/5 (14h/15h/18h) */
3119 parm = AC_PWRST_D3;
3120 set_pin_power_state(codec, 0x14, &parm);
3121 set_pin_power_state(codec, 0x15, &parm);
3122 set_pin_power_state(codec, 0x18, &parm);
3123 if (imux_is_smixer)
3124 parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
3125 /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003126 update_power_state(codec, 0x13, parm);
3127 update_power_state(codec, 0x12, parm);
3128 update_power_state(codec, 0x1f, parm);
3129 update_power_state(codec, 0x20, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003130
3131 /* outputs */
3132 /* PW 3/4 (16h/17h) */
3133 parm = AC_PWRST_D3;
3134 set_pin_power_state(codec, 0x17, &parm);
3135 set_pin_power_state(codec, 0x16, &parm);
3136 /* MW0 (1ah), AOW 0/1 (10h/1dh) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003137 update_power_state(codec, 0x1a, imux_is_smixer ? AC_PWRST_D0 : parm);
3138 update_power_state(codec, 0x10, parm);
3139 update_power_state(codec, 0x1d, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003140}
3141
Harald Welted949cac2008-09-09 15:56:01 +08003142static int patch_vt1702(struct hda_codec *codec)
3143{
3144 struct via_spec *spec;
3145 int err;
Harald Welted949cac2008-09-09 15:56:01 +08003146
3147 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003148 spec = via_new_spec(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003149 if (spec == NULL)
3150 return -ENOMEM;
3151
Takashi Iwai620e2b22011-06-17 17:19:19 +02003152 spec->aa_mix_nid = 0x1a;
3153
Takashi Iwai12daef62011-06-18 17:45:49 +02003154 /* limit AA path volume to 0 dB */
3155 snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
3156 (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
3157 (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
3158 (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
3159 (1 << AC_AMPCAP_MUTE_SHIFT));
3160
Harald Welted949cac2008-09-09 15:56:01 +08003161 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003162 err = via_parse_auto_config(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003163 if (err < 0) {
3164 via_free(codec);
3165 return err;
Harald Welted949cac2008-09-09 15:56:01 +08003166 }
3167
Takashi Iwai096a8852011-06-20 12:09:02 +02003168 spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
Harald Welted949cac2008-09-09 15:56:01 +08003169
Harald Welted949cac2008-09-09 15:56:01 +08003170 codec->patch_ops = via_patch_ops;
3171
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003172 spec->set_widgets_power_state = set_widgets_power_state_vt1702;
Harald Welted949cac2008-09-09 15:56:01 +08003173 return 0;
3174}
3175
Lydia Wangeb7188c2009-10-10 19:08:34 +08003176/* Patch for VT1718S */
3177
Takashi Iwai096a8852011-06-20 12:09:02 +02003178static const struct hda_verb vt1718S_init_verbs[] = {
Lydia Wang4ab2d532011-03-24 12:42:03 +08003179 /* Enable MW0 adjust Gain 5 */
3180 {0x1, 0xfb2, 0x10},
Lydia Wangeb7188c2009-10-10 19:08:34 +08003181 /* Enable Boost Volume backdoor */
3182 {0x1, 0xf88, 0x8},
Takashi Iwai5d417622011-06-20 11:32:27 +02003183
Lydia Wangeb7188c2009-10-10 19:08:34 +08003184 { }
3185};
3186
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003187static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
3188{
3189 struct via_spec *spec = codec->spec;
3190 int imux_is_smixer;
Takashi Iwai61625522012-07-25 13:54:55 +02003191 unsigned int parm, parm2;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003192 /* MUX6 (1eh) = stereo mixer */
3193 imux_is_smixer =
3194 snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
3195 /* inputs */
3196 /* PW 5/6/7 (29h/2ah/2bh) */
3197 parm = AC_PWRST_D3;
3198 set_pin_power_state(codec, 0x29, &parm);
3199 set_pin_power_state(codec, 0x2a, &parm);
3200 set_pin_power_state(codec, 0x2b, &parm);
3201 if (imux_is_smixer)
3202 parm = AC_PWRST_D0;
3203 /* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003204 update_power_state(codec, 0x1e, parm);
3205 update_power_state(codec, 0x1f, parm);
3206 update_power_state(codec, 0x10, parm);
3207 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003208
3209 /* outputs */
3210 /* PW3 (27h), MW2 (1ah), AOW3 (bh) */
3211 parm = AC_PWRST_D3;
3212 set_pin_power_state(codec, 0x27, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003213 update_power_state(codec, 0x1a, parm);
Takashi Iwai61625522012-07-25 13:54:55 +02003214 parm2 = parm; /* for pin 0x0b */
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003215
3216 /* PW2 (26h), AOW2 (ah) */
3217 parm = AC_PWRST_D3;
3218 set_pin_power_state(codec, 0x26, &parm);
3219 if (spec->smart51_enabled)
3220 set_pin_power_state(codec, 0x2b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003221 update_power_state(codec, 0xa, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003222
3223 /* PW0 (24h), AOW0 (8h) */
3224 parm = AC_PWRST_D3;
3225 set_pin_power_state(codec, 0x24, &parm);
3226 if (!spec->hp_independent_mode) /* check for redirected HP */
3227 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003228 update_power_state(codec, 0x8, parm);
Takashi Iwai61625522012-07-25 13:54:55 +02003229 if (!spec->hp_independent_mode && parm2 != AC_PWRST_D3)
3230 parm = parm2;
3231 update_power_state(codec, 0xb, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003232 /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003233 update_power_state(codec, 0x21, imux_is_smixer ? AC_PWRST_D0 : parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003234
3235 /* PW1 (25h), AOW1 (9h) */
3236 parm = AC_PWRST_D3;
3237 set_pin_power_state(codec, 0x25, &parm);
3238 if (spec->smart51_enabled)
3239 set_pin_power_state(codec, 0x2a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003240 update_power_state(codec, 0x9, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003241
3242 if (spec->hp_independent_mode) {
3243 /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
3244 parm = AC_PWRST_D3;
3245 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003246 update_power_state(codec, 0x1b, parm);
3247 update_power_state(codec, 0x34, parm);
3248 update_power_state(codec, 0xc, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003249 }
3250}
3251
Takashi Iwai30b45032011-07-11 17:05:04 +02003252/* Add a connection to the primary DAC from AA-mixer for some codecs
3253 * This isn't listed from the raw info, but the chip has a secret connection.
3254 */
3255static int add_secret_dac_path(struct hda_codec *codec)
3256{
3257 struct via_spec *spec = codec->spec;
3258 int i, nums;
3259 hda_nid_t conn[8];
3260 hda_nid_t nid;
3261
3262 if (!spec->aa_mix_nid)
3263 return 0;
3264 nums = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
3265 ARRAY_SIZE(conn) - 1);
3266 for (i = 0; i < nums; i++) {
3267 if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
3268 return 0;
3269 }
3270
3271 /* find the primary DAC and add to the connection list */
3272 nid = codec->start_nid;
3273 for (i = 0; i < codec->num_nodes; i++, nid++) {
3274 unsigned int caps = get_wcaps(codec, nid);
3275 if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
3276 !(caps & AC_WCAP_DIGITAL)) {
3277 conn[nums++] = nid;
3278 return snd_hda_override_conn_list(codec,
3279 spec->aa_mix_nid,
3280 nums, conn);
3281 }
3282 }
3283 return 0;
3284}
3285
3286
Lydia Wangeb7188c2009-10-10 19:08:34 +08003287static int patch_vt1718S(struct hda_codec *codec)
3288{
3289 struct via_spec *spec;
3290 int err;
3291
3292 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003293 spec = via_new_spec(codec);
Lydia Wangeb7188c2009-10-10 19:08:34 +08003294 if (spec == NULL)
3295 return -ENOMEM;
3296
Takashi Iwai620e2b22011-06-17 17:19:19 +02003297 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003298 override_mic_boost(codec, 0x2b, 0, 3, 40);
3299 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003300 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003301
Lydia Wangeb7188c2009-10-10 19:08:34 +08003302 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003303 err = via_parse_auto_config(codec);
Lydia Wangeb7188c2009-10-10 19:08:34 +08003304 if (err < 0) {
3305 via_free(codec);
3306 return err;
Lydia Wangeb7188c2009-10-10 19:08:34 +08003307 }
3308
Takashi Iwai096a8852011-06-20 12:09:02 +02003309 spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
Lydia Wangeb7188c2009-10-10 19:08:34 +08003310
Lydia Wangeb7188c2009-10-10 19:08:34 +08003311 codec->patch_ops = via_patch_ops;
3312
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003313 spec->set_widgets_power_state = set_widgets_power_state_vt1718S;
3314
Lydia Wangeb7188c2009-10-10 19:08:34 +08003315 return 0;
3316}
Lydia Wangf3db4232009-10-10 19:08:41 +08003317
3318/* Patch for VT1716S */
3319
3320static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
3321 struct snd_ctl_elem_info *uinfo)
3322{
3323 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3324 uinfo->count = 1;
3325 uinfo->value.integer.min = 0;
3326 uinfo->value.integer.max = 1;
3327 return 0;
3328}
3329
3330static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
3331 struct snd_ctl_elem_value *ucontrol)
3332{
3333 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3334 int index = 0;
3335
3336 index = snd_hda_codec_read(codec, 0x26, 0,
3337 AC_VERB_GET_CONNECT_SEL, 0);
3338 if (index != -1)
3339 *ucontrol->value.integer.value = index;
3340
3341 return 0;
3342}
3343
3344static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
3345 struct snd_ctl_elem_value *ucontrol)
3346{
3347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3348 struct via_spec *spec = codec->spec;
3349 int index = *ucontrol->value.integer.value;
3350
3351 snd_hda_codec_write(codec, 0x26, 0,
3352 AC_VERB_SET_CONNECT_SEL, index);
3353 spec->dmic_enabled = index;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003354 set_widgets_power_state(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003355 return 1;
3356}
3357
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003358static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003359 HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
3360 {
3361 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3362 .name = "Digital Mic Capture Switch",
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003363 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
Lydia Wangf3db4232009-10-10 19:08:41 +08003364 .count = 1,
3365 .info = vt1716s_dmic_info,
3366 .get = vt1716s_dmic_get,
3367 .put = vt1716s_dmic_put,
3368 },
3369 {} /* end */
3370};
3371
3372
3373/* mono-out mixer elements */
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003374static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003375 HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
3376 { } /* end */
3377};
3378
Takashi Iwai096a8852011-06-20 12:09:02 +02003379static const struct hda_verb vt1716S_init_verbs[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003380 /* Enable Boost Volume backdoor */
3381 {0x1, 0xf8a, 0x80},
3382 /* don't bybass mixer */
3383 {0x1, 0xf88, 0xc0},
3384 /* Enable mono output */
3385 {0x1, 0xf90, 0x08},
3386 { }
3387};
3388
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003389static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
3390{
3391 struct via_spec *spec = codec->spec;
3392 int imux_is_smixer;
3393 unsigned int parm;
3394 unsigned int mono_out, present;
3395 /* SW0 (17h) = stereo mixer */
3396 imux_is_smixer =
3397 (snd_hda_codec_read(codec, 0x17, 0,
3398 AC_VERB_GET_CONNECT_SEL, 0x00) == 5);
3399 /* inputs */
3400 /* PW 1/2/5 (1ah/1bh/1eh) */
3401 parm = AC_PWRST_D3;
3402 set_pin_power_state(codec, 0x1a, &parm);
3403 set_pin_power_state(codec, 0x1b, &parm);
3404 set_pin_power_state(codec, 0x1e, &parm);
3405 if (imux_is_smixer)
3406 parm = AC_PWRST_D0;
3407 /* SW0 (17h), AIW0(13h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003408 update_power_state(codec, 0x17, parm);
3409 update_power_state(codec, 0x13, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003410
3411 parm = AC_PWRST_D3;
3412 set_pin_power_state(codec, 0x1e, &parm);
3413 /* PW11 (22h) */
3414 if (spec->dmic_enabled)
3415 set_pin_power_state(codec, 0x22, &parm);
3416 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003417 update_power_state(codec, 0x22, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003418
3419 /* SW2(26h), AIW1(14h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003420 update_power_state(codec, 0x26, parm);
3421 update_power_state(codec, 0x14, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003422
3423 /* outputs */
3424 /* PW0 (19h), SW1 (18h), AOW1 (11h) */
3425 parm = AC_PWRST_D3;
3426 set_pin_power_state(codec, 0x19, &parm);
3427 /* Smart 5.1 PW2(1bh) */
3428 if (spec->smart51_enabled)
3429 set_pin_power_state(codec, 0x1b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003430 update_power_state(codec, 0x18, parm);
3431 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003432
3433 /* PW7 (23h), SW3 (27h), AOW3 (25h) */
3434 parm = AC_PWRST_D3;
3435 set_pin_power_state(codec, 0x23, &parm);
3436 /* Smart 5.1 PW1(1ah) */
3437 if (spec->smart51_enabled)
3438 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003439 update_power_state(codec, 0x27, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003440
3441 /* Smart 5.1 PW5(1eh) */
3442 if (spec->smart51_enabled)
3443 set_pin_power_state(codec, 0x1e, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003444 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003445
3446 /* Mono out */
3447 /* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
3448 present = snd_hda_jack_detect(codec, 0x1c);
3449
3450 if (present)
3451 mono_out = 0;
3452 else {
3453 present = snd_hda_jack_detect(codec, 0x1d);
3454 if (!spec->hp_independent_mode && present)
3455 mono_out = 0;
3456 else
3457 mono_out = 1;
3458 }
3459 parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
Takashi Iwai054d8672012-01-24 12:25:50 +01003460 update_power_state(codec, 0x28, parm);
3461 update_power_state(codec, 0x29, parm);
3462 update_power_state(codec, 0x2a, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003463
3464 /* PW 3/4 (1ch/1dh) */
3465 parm = AC_PWRST_D3;
3466 set_pin_power_state(codec, 0x1c, &parm);
3467 set_pin_power_state(codec, 0x1d, &parm);
3468 /* HP Independent Mode, power on AOW3 */
3469 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003470 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003471
3472 /* force to D0 for internal Speaker */
3473 /* MW0 (16h), AOW0 (10h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003474 update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
3475 update_power_state(codec, 0x10, mono_out ? AC_PWRST_D0 : parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003476}
3477
Lydia Wangf3db4232009-10-10 19:08:41 +08003478static int patch_vt1716S(struct hda_codec *codec)
3479{
3480 struct via_spec *spec;
3481 int err;
3482
3483 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003484 spec = via_new_spec(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003485 if (spec == NULL)
3486 return -ENOMEM;
3487
Takashi Iwai620e2b22011-06-17 17:19:19 +02003488 spec->aa_mix_nid = 0x16;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003489 override_mic_boost(codec, 0x1a, 0, 3, 40);
3490 override_mic_boost(codec, 0x1e, 0, 3, 40);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003491
Lydia Wangf3db4232009-10-10 19:08:41 +08003492 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003493 err = via_parse_auto_config(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003494 if (err < 0) {
3495 via_free(codec);
3496 return err;
Lydia Wangf3db4232009-10-10 19:08:41 +08003497 }
3498
Takashi Iwai096a8852011-06-20 12:09:02 +02003499 spec->init_verbs[spec->num_iverbs++] = vt1716S_init_verbs;
Lydia Wangf3db4232009-10-10 19:08:41 +08003500
Lydia Wangf3db4232009-10-10 19:08:41 +08003501 spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
3502 spec->num_mixers++;
3503
3504 spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3505
3506 codec->patch_ops = via_patch_ops;
3507
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003508 spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
Lydia Wangf3db4232009-10-10 19:08:41 +08003509 return 0;
3510}
Lydia Wang25eaba22009-10-10 19:08:43 +08003511
3512/* for vt2002P */
3513
Takashi Iwai096a8852011-06-20 12:09:02 +02003514static const struct hda_verb vt2002P_init_verbs[] = {
Lydia Wangeadb9a82011-03-24 12:43:02 +08003515 /* Class-D speaker related verbs */
3516 {0x1, 0xfe0, 0x4},
3517 {0x1, 0xfe9, 0x80},
3518 {0x1, 0xfe2, 0x22},
Lydia Wang25eaba22009-10-10 19:08:43 +08003519 /* Enable Boost Volume backdoor */
3520 {0x1, 0xfb9, 0x24},
Lydia Wang25eaba22009-10-10 19:08:43 +08003521 /* Enable AOW0 to MW9 */
3522 {0x1, 0xfb8, 0x88},
3523 { }
3524};
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003525
Takashi Iwai096a8852011-06-20 12:09:02 +02003526static const struct hda_verb vt1802_init_verbs[] = {
Lydia Wang118909562011-03-23 17:57:34 +08003527 /* Enable Boost Volume backdoor */
3528 {0x1, 0xfb9, 0x24},
Lydia Wang118909562011-03-23 17:57:34 +08003529 /* Enable AOW0 to MW9 */
3530 {0x1, 0xfb8, 0x88},
3531 { }
3532};
Lydia Wang25eaba22009-10-10 19:08:43 +08003533
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003534static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
3535{
3536 struct via_spec *spec = codec->spec;
3537 int imux_is_smixer;
3538 unsigned int parm;
3539 unsigned int present;
3540 /* MUX9 (1eh) = stereo mixer */
3541 imux_is_smixer =
3542 snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3543 /* inputs */
3544 /* PW 5/6/7 (29h/2ah/2bh) */
3545 parm = AC_PWRST_D3;
3546 set_pin_power_state(codec, 0x29, &parm);
3547 set_pin_power_state(codec, 0x2a, &parm);
3548 set_pin_power_state(codec, 0x2b, &parm);
3549 parm = AC_PWRST_D0;
3550 /* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003551 update_power_state(codec, 0x1e, parm);
3552 update_power_state(codec, 0x1f, parm);
3553 update_power_state(codec, 0x10, parm);
3554 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003555
3556 /* outputs */
3557 /* AOW0 (8h)*/
Takashi Iwai054d8672012-01-24 12:25:50 +01003558 update_power_state(codec, 0x8, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003559
Lydia Wang118909562011-03-23 17:57:34 +08003560 if (spec->codec_type == VT1802) {
3561 /* PW4 (28h), MW4 (18h), MUX4(38h) */
3562 parm = AC_PWRST_D3;
3563 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003564 update_power_state(codec, 0x18, parm);
3565 update_power_state(codec, 0x38, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003566 } else {
3567 /* PW4 (26h), MW4 (1ch), MUX4(37h) */
3568 parm = AC_PWRST_D3;
3569 set_pin_power_state(codec, 0x26, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003570 update_power_state(codec, 0x1c, parm);
3571 update_power_state(codec, 0x37, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003572 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003573
Lydia Wang118909562011-03-23 17:57:34 +08003574 if (spec->codec_type == VT1802) {
3575 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3576 parm = AC_PWRST_D3;
3577 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003578 update_power_state(codec, 0x15, parm);
3579 update_power_state(codec, 0x35, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003580 } else {
3581 /* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
3582 parm = AC_PWRST_D3;
3583 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003584 update_power_state(codec, 0x19, parm);
3585 update_power_state(codec, 0x35, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003586 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003587
3588 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003589 update_power_state(codec, 0x9, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003590
3591 /* Class-D */
3592 /* PW0 (24h), MW0(18h/14h), MUX0(34h) */
3593 present = snd_hda_jack_detect(codec, 0x25);
3594
3595 parm = AC_PWRST_D3;
3596 set_pin_power_state(codec, 0x24, &parm);
3597 parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
Lydia Wang118909562011-03-23 17:57:34 +08003598 if (spec->codec_type == VT1802)
Takashi Iwai054d8672012-01-24 12:25:50 +01003599 update_power_state(codec, 0x14, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003600 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003601 update_power_state(codec, 0x18, parm);
3602 update_power_state(codec, 0x34, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003603
3604 /* Mono Out */
3605 present = snd_hda_jack_detect(codec, 0x26);
3606
3607 parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
Lydia Wang118909562011-03-23 17:57:34 +08003608 if (spec->codec_type == VT1802) {
3609 /* PW15 (33h), MW8(1ch), MUX8(3ch) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003610 update_power_state(codec, 0x33, parm);
3611 update_power_state(codec, 0x1c, parm);
3612 update_power_state(codec, 0x3c, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003613 } else {
3614 /* PW15 (31h), MW8(17h), MUX8(3bh) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003615 update_power_state(codec, 0x31, parm);
3616 update_power_state(codec, 0x17, parm);
3617 update_power_state(codec, 0x3b, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003618 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003619 /* MW9 (21h) */
3620 if (imux_is_smixer || !is_aa_path_mute(codec))
Takashi Iwai054d8672012-01-24 12:25:50 +01003621 update_power_state(codec, 0x21, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003622 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003623 update_power_state(codec, 0x21, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003624}
Lydia Wang25eaba22009-10-10 19:08:43 +08003625
David Henningsson4b527b62012-09-18 14:26:59 +02003626/*
3627 * pin fix-up
3628 */
3629enum {
3630 VIA_FIXUP_INTMIC_BOOST,
3631};
3632
3633static void via_fixup_intmic_boost(struct hda_codec *codec,
3634 const struct hda_fixup *fix, int action)
3635{
3636 if (action == HDA_FIXUP_ACT_PRE_PROBE)
3637 override_mic_boost(codec, 0x30, 0, 2, 40);
3638}
3639
3640static const struct hda_fixup via_fixups[] = {
3641 [VIA_FIXUP_INTMIC_BOOST] = {
3642 .type = HDA_FIXUP_FUNC,
3643 .v.func = via_fixup_intmic_boost,
3644 },
3645};
3646
3647static const struct snd_pci_quirk vt2002p_fixups[] = {
3648 SND_PCI_QUIRK(0x1043, 0x8532, "Asus X202E", VIA_FIXUP_INTMIC_BOOST),
3649 {}
3650};
3651
Lydia Wang25eaba22009-10-10 19:08:43 +08003652/* patch for vt2002P */
3653static int patch_vt2002P(struct hda_codec *codec)
3654{
3655 struct via_spec *spec;
3656 int err;
3657
3658 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003659 spec = via_new_spec(codec);
Lydia Wang25eaba22009-10-10 19:08:43 +08003660 if (spec == NULL)
3661 return -ENOMEM;
3662
Takashi Iwai620e2b22011-06-17 17:19:19 +02003663 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003664 override_mic_boost(codec, 0x2b, 0, 3, 40);
3665 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003666 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003667
David Henningsson4b527b62012-09-18 14:26:59 +02003668 snd_hda_pick_fixup(codec, NULL, vt2002p_fixups, via_fixups);
3669 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
3670
Lydia Wang25eaba22009-10-10 19:08:43 +08003671 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003672 err = via_parse_auto_config(codec);
Lydia Wang25eaba22009-10-10 19:08:43 +08003673 if (err < 0) {
3674 via_free(codec);
3675 return err;
Lydia Wang25eaba22009-10-10 19:08:43 +08003676 }
3677
Lydia Wang118909562011-03-23 17:57:34 +08003678 if (spec->codec_type == VT1802)
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003679 spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
Lydia Wang118909562011-03-23 17:57:34 +08003680 else
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003681 spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
Lydia Wang118909562011-03-23 17:57:34 +08003682
Lydia Wang25eaba22009-10-10 19:08:43 +08003683 codec->patch_ops = via_patch_ops;
3684
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003685 spec->set_widgets_power_state = set_widgets_power_state_vt2002P;
Lydia Wang25eaba22009-10-10 19:08:43 +08003686 return 0;
3687}
Lydia Wangab6734e2009-10-10 19:08:46 +08003688
3689/* for vt1812 */
3690
Takashi Iwai096a8852011-06-20 12:09:02 +02003691static const struct hda_verb vt1812_init_verbs[] = {
Lydia Wangab6734e2009-10-10 19:08:46 +08003692 /* Enable Boost Volume backdoor */
3693 {0x1, 0xfb9, 0x24},
Lydia Wangab6734e2009-10-10 19:08:46 +08003694 /* Enable AOW0 to MW9 */
3695 {0x1, 0xfb8, 0xa8},
3696 { }
3697};
3698
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003699static void set_widgets_power_state_vt1812(struct hda_codec *codec)
3700{
3701 struct via_spec *spec = codec->spec;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003702 unsigned int parm;
3703 unsigned int present;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003704 /* inputs */
3705 /* PW 5/6/7 (29h/2ah/2bh) */
3706 parm = AC_PWRST_D3;
3707 set_pin_power_state(codec, 0x29, &parm);
3708 set_pin_power_state(codec, 0x2a, &parm);
3709 set_pin_power_state(codec, 0x2b, &parm);
3710 parm = AC_PWRST_D0;
3711 /* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003712 update_power_state(codec, 0x1e, parm);
3713 update_power_state(codec, 0x1f, parm);
3714 update_power_state(codec, 0x10, parm);
3715 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003716
3717 /* outputs */
3718 /* AOW0 (8h)*/
Takashi Iwai054d8672012-01-24 12:25:50 +01003719 update_power_state(codec, 0x8, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003720
3721 /* PW4 (28h), MW4 (18h), MUX4(38h) */
3722 parm = AC_PWRST_D3;
3723 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003724 update_power_state(codec, 0x18, parm);
3725 update_power_state(codec, 0x38, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003726
3727 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3728 parm = AC_PWRST_D3;
3729 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003730 update_power_state(codec, 0x15, parm);
3731 update_power_state(codec, 0x35, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003732 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003733 update_power_state(codec, 0x9, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003734
3735 /* Internal Speaker */
3736 /* PW0 (24h), MW0(14h), MUX0(34h) */
3737 present = snd_hda_jack_detect(codec, 0x25);
3738
3739 parm = AC_PWRST_D3;
3740 set_pin_power_state(codec, 0x24, &parm);
3741 if (present) {
Takashi Iwai054d8672012-01-24 12:25:50 +01003742 update_power_state(codec, 0x14, AC_PWRST_D3);
3743 update_power_state(codec, 0x34, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003744 } else {
Takashi Iwai054d8672012-01-24 12:25:50 +01003745 update_power_state(codec, 0x14, AC_PWRST_D0);
3746 update_power_state(codec, 0x34, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003747 }
3748
3749
3750 /* Mono Out */
3751 /* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
3752 present = snd_hda_jack_detect(codec, 0x28);
3753
3754 parm = AC_PWRST_D3;
3755 set_pin_power_state(codec, 0x31, &parm);
3756 if (present) {
Takashi Iwai054d8672012-01-24 12:25:50 +01003757 update_power_state(codec, 0x1c, AC_PWRST_D3);
3758 update_power_state(codec, 0x3c, AC_PWRST_D3);
3759 update_power_state(codec, 0x3e, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003760 } else {
Takashi Iwai054d8672012-01-24 12:25:50 +01003761 update_power_state(codec, 0x1c, AC_PWRST_D0);
3762 update_power_state(codec, 0x3c, AC_PWRST_D0);
3763 update_power_state(codec, 0x3e, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003764 }
3765
3766 /* PW15 (33h), MW15 (1dh), MUX15(3dh) */
3767 parm = AC_PWRST_D3;
3768 set_pin_power_state(codec, 0x33, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003769 update_power_state(codec, 0x1d, parm);
3770 update_power_state(codec, 0x3d, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003771
3772}
Lydia Wangab6734e2009-10-10 19:08:46 +08003773
3774/* patch for vt1812 */
3775static int patch_vt1812(struct hda_codec *codec)
3776{
3777 struct via_spec *spec;
3778 int err;
3779
3780 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003781 spec = via_new_spec(codec);
Lydia Wangab6734e2009-10-10 19:08:46 +08003782 if (spec == NULL)
3783 return -ENOMEM;
3784
Takashi Iwai620e2b22011-06-17 17:19:19 +02003785 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003786 override_mic_boost(codec, 0x2b, 0, 3, 40);
3787 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003788 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003789
Lydia Wangab6734e2009-10-10 19:08:46 +08003790 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003791 err = via_parse_auto_config(codec);
Lydia Wangab6734e2009-10-10 19:08:46 +08003792 if (err < 0) {
3793 via_free(codec);
3794 return err;
Lydia Wangab6734e2009-10-10 19:08:46 +08003795 }
3796
Takashi Iwai096a8852011-06-20 12:09:02 +02003797 spec->init_verbs[spec->num_iverbs++] = vt1812_init_verbs;
Lydia Wangab6734e2009-10-10 19:08:46 +08003798
Lydia Wangab6734e2009-10-10 19:08:46 +08003799 codec->patch_ops = via_patch_ops;
3800
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003801 spec->set_widgets_power_state = set_widgets_power_state_vt1812;
Lydia Wangab6734e2009-10-10 19:08:46 +08003802 return 0;
3803}
3804
Joseph Chanc577b8a2006-11-29 15:29:40 +01003805/*
3806 * patch entries
3807 */
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003808static const struct hda_codec_preset snd_hda_preset_via[] = {
Takashi Iwai3218c172008-12-18 09:17:56 +01003809 { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
3810 { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
3811 { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
3812 { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
3813 { .id = 0x1106e710, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003814 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003815 { .id = 0x1106e711, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003816 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003817 { .id = 0x1106e712, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003818 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003819 { .id = 0x1106e713, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003820 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003821 { .id = 0x1106e714, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003822 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003823 { .id = 0x1106e715, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003824 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003825 { .id = 0x1106e716, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003826 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003827 { .id = 0x1106e717, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003828 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003829 { .id = 0x1106e720, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003830 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003831 { .id = 0x1106e721, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003832 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003833 { .id = 0x1106e722, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003834 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003835 { .id = 0x1106e723, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003836 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003837 { .id = 0x1106e724, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003838 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003839 { .id = 0x1106e725, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003840 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003841 { .id = 0x1106e726, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003842 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003843 { .id = 0x1106e727, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003844 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003845 { .id = 0x11060397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003846 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003847 { .id = 0x11061397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003848 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003849 { .id = 0x11062397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003850 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003851 { .id = 0x11063397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003852 .patch = patch_vt1708S},
Lydia Wangbc92df72011-03-23 17:56:05 +08003853 { .id = 0x11064397, .name = "VT1705",
Harald Welted949cac2008-09-09 15:56:01 +08003854 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003855 { .id = 0x11065397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003856 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003857 { .id = 0x11066397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003858 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003859 { .id = 0x11067397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003860 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003861 { .id = 0x11060398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003862 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003863 { .id = 0x11061398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003864 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003865 { .id = 0x11062398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003866 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003867 { .id = 0x11063398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003868 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003869 { .id = 0x11064398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003870 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003871 { .id = 0x11065398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003872 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003873 { .id = 0x11066398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003874 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003875 { .id = 0x11067398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003876 .patch = patch_vt1702},
Lydia Wangeb7188c2009-10-10 19:08:34 +08003877 { .id = 0x11060428, .name = "VT1718S",
3878 .patch = patch_vt1718S},
3879 { .id = 0x11064428, .name = "VT1718S",
3880 .patch = patch_vt1718S},
Lydia Wangbb3c6bfc2009-10-10 19:08:39 +08003881 { .id = 0x11060441, .name = "VT2020",
3882 .patch = patch_vt1718S},
3883 { .id = 0x11064441, .name = "VT1828S",
3884 .patch = patch_vt1718S},
Lydia Wangf3db4232009-10-10 19:08:41 +08003885 { .id = 0x11060433, .name = "VT1716S",
3886 .patch = patch_vt1716S},
3887 { .id = 0x1106a721, .name = "VT1716S",
3888 .patch = patch_vt1716S},
Lydia Wang25eaba22009-10-10 19:08:43 +08003889 { .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
3890 { .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
Lydia Wangab6734e2009-10-10 19:08:46 +08003891 { .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
Lydia Wang36dd5c42009-10-20 13:18:04 +08003892 { .id = 0x11060440, .name = "VT1818S",
3893 .patch = patch_vt1708S},
Lydia Wang118909562011-03-23 17:57:34 +08003894 { .id = 0x11060446, .name = "VT1802",
3895 .patch = patch_vt2002P},
3896 { .id = 0x11068446, .name = "VT1802",
3897 .patch = patch_vt2002P},
Joseph Chanc577b8a2006-11-29 15:29:40 +01003898 {} /* terminator */
3899};
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003900
3901MODULE_ALIAS("snd-hda-codec-id:1106*");
3902
3903static struct hda_codec_preset_list via_list = {
3904 .preset = snd_hda_preset_via,
3905 .owner = THIS_MODULE,
3906};
3907
3908MODULE_LICENSE("GPL");
3909MODULE_DESCRIPTION("VIA HD-audio codec");
3910
3911static int __init patch_via_init(void)
3912{
3913 return snd_hda_add_codec_preset(&via_list);
3914}
3915
3916static void __exit patch_via_exit(void)
3917{
3918 snd_hda_delete_codec_preset(&via_list);
3919}
3920
3921module_init(patch_via_init)
3922module_exit(patch_via_exit)