blob: 92e11672b91cd620ed0a05a696d489e2c50cfd3b [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 Iwai1835a0f2011-10-27 22:12:46 +020057#include "hda_jack.h"
Joseph Chanc577b8a2006-11-29 15:29:40 +010058
Joseph Chanc577b8a2006-11-29 15:29:40 +010059/* Pin Widget NID */
Harald Welted949cac2008-09-09 15:56:01 +080060#define VT1708_HP_PIN_NID 0x20
61#define VT1708_CD_PIN_NID 0x24
Joseph Chanc577b8a2006-11-29 15:29:40 +010062
Harald Welted7426322008-09-15 22:43:23 +080063enum VIA_HDA_CODEC {
64 UNKNOWN = -1,
65 VT1708,
66 VT1709_10CH,
67 VT1709_6CH,
68 VT1708B_8CH,
69 VT1708B_4CH,
70 VT1708S,
Lydia Wang518bf3b2009-10-10 19:07:29 +080071 VT1708BCE,
Harald Welted7426322008-09-15 22:43:23 +080072 VT1702,
Lydia Wangeb7188c2009-10-10 19:08:34 +080073 VT1718S,
Lydia Wangf3db4232009-10-10 19:08:41 +080074 VT1716S,
Lydia Wang25eaba22009-10-10 19:08:43 +080075 VT2002P,
Lydia Wangab6734e2009-10-10 19:08:46 +080076 VT1812,
Lydia Wang118909562011-03-23 17:57:34 +080077 VT1802,
Harald Welted7426322008-09-15 22:43:23 +080078 CODEC_TYPES,
79};
80
Lydia Wang118909562011-03-23 17:57:34 +080081#define VT2002P_COMPATIBLE(spec) \
82 ((spec)->codec_type == VT2002P ||\
83 (spec)->codec_type == VT1812 ||\
84 (spec)->codec_type == VT1802)
85
Takashi Iwai8e3679d2011-06-21 09:01:36 +020086#define MAX_NID_PATH_DEPTH 5
87
Takashi Iwai09a9ad692011-06-21 15:57:44 +020088/* output-path: DAC -> ... -> pin
89 * idx[] contains the source index number of the next widget;
90 * e.g. idx[0] is the index of the DAC selected by path[1] widget
91 * multi[] indicates whether it's a selector widget with multi-connectors
92 * (i.e. the connection selection is mandatory)
93 * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
94 */
Takashi Iwai4a796162011-06-17 17:53:38 +020095struct nid_path {
96 int depth;
Takashi Iwai8e3679d2011-06-21 09:01:36 +020097 hda_nid_t path[MAX_NID_PATH_DEPTH];
Takashi Iwai09a9ad692011-06-21 15:57:44 +020098 unsigned char idx[MAX_NID_PATH_DEPTH];
99 unsigned char multi[MAX_NID_PATH_DEPTH];
100 unsigned int vol_ctl;
101 unsigned int mute_ctl;
Takashi Iwai4a796162011-06-17 17:53:38 +0200102};
103
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200104/* input-path */
105struct via_input {
106 hda_nid_t pin; /* input-pin or aa-mix */
107 int adc_idx; /* ADC index to be used */
108 int mux_idx; /* MUX index (if any) */
109 const char *label; /* input-source label */
110};
111
Takashi Iwaide6c74f2011-07-04 14:46:42 +0200112#define VIA_MAX_ADCS 3
113
Takashi Iwai3b607e32011-07-18 16:54:40 +0200114enum {
115 STREAM_MULTI_OUT = (1 << 0),
116 STREAM_INDEP_HP = (1 << 1),
117};
118
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800119struct via_spec {
120 /* codec parameterization */
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200121 const struct snd_kcontrol_new *mixers[6];
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800122 unsigned int num_mixers;
123
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200124 const struct hda_verb *init_verbs[5];
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800125 unsigned int num_iverbs;
126
Takashi Iwai82673bc2011-06-17 16:24:21 +0200127 char stream_name_analog[32];
Takashi Iwai7eb56e82011-06-18 16:40:14 +0200128 char stream_name_hp[32];
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200129 const struct hda_pcm_stream *stream_analog_playback;
130 const struct hda_pcm_stream *stream_analog_capture;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800131
Takashi Iwai82673bc2011-06-17 16:24:21 +0200132 char stream_name_digital[32];
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200133 const struct hda_pcm_stream *stream_digital_playback;
134 const struct hda_pcm_stream *stream_digital_capture;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800135
136 /* playback */
137 struct hda_multi_out multiout;
138 hda_nid_t slave_dig_outs[2];
Takashi Iwaiece8d042011-06-19 16:24:21 +0200139 hda_nid_t hp_dac_nid;
Takashi Iwai3214b962011-07-18 12:49:25 +0200140 hda_nid_t speaker_dac_nid;
141 int hp_indep_shared; /* indep HP-DAC is shared with side ch */
Takashi Iwai3b607e32011-07-18 16:54:40 +0200142 int opened_streams; /* STREAM_* bits */
143 int active_streams; /* STREAM_* bits */
Takashi Iwai3214b962011-07-18 12:49:25 +0200144 int aamix_mode; /* loopback is enabled for output-path? */
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800145
Takashi Iwai3214b962011-07-18 12:49:25 +0200146 /* Output-paths:
147 * There are different output-paths depending on the setup.
148 * out_path, hp_path and speaker_path are primary paths. If both
149 * direct DAC and aa-loopback routes are available, these contain
150 * the former paths. Meanwhile *_mix_path contain the paths with
151 * loopback mixer. (Since the loopback is only for front channel,
152 * no out_mix_path for surround channels.)
153 * The HP output has another path, hp_indep_path, which is used in
154 * the independent-HP mode.
155 */
Takashi Iwaide6c74f2011-07-04 14:46:42 +0200156 struct nid_path out_path[HDA_SIDE + 1];
Takashi Iwai3214b962011-07-18 12:49:25 +0200157 struct nid_path out_mix_path;
Takashi Iwai4a796162011-06-17 17:53:38 +0200158 struct nid_path hp_path;
Takashi Iwai3214b962011-07-18 12:49:25 +0200159 struct nid_path hp_mix_path;
160 struct nid_path hp_indep_path;
Takashi Iwai4a918ff2011-06-20 12:39:26 +0200161 struct nid_path speaker_path;
Takashi Iwai3214b962011-07-18 12:49:25 +0200162 struct nid_path speaker_mix_path;
Takashi Iwai4a796162011-06-17 17:53:38 +0200163
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800164 /* capture */
165 unsigned int num_adc_nids;
Takashi Iwaide6c74f2011-07-04 14:46:42 +0200166 hda_nid_t adc_nids[VIA_MAX_ADCS];
167 hda_nid_t mux_nids[VIA_MAX_ADCS];
Takashi Iwai620e2b22011-06-17 17:19:19 +0200168 hda_nid_t aa_mix_nid;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800169 hda_nid_t dig_in_nid;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800170
171 /* capture source */
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200172 bool dyn_adc_switch;
173 int num_inputs;
174 struct via_input inputs[AUTO_CFG_MAX_INS + 1];
Takashi Iwaide6c74f2011-07-04 14:46:42 +0200175 unsigned int cur_mux[VIA_MAX_ADCS];
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800176
Takashi Iwai3b607e32011-07-18 16:54:40 +0200177 /* dynamic DAC switching */
178 unsigned int cur_dac_stream_tag;
179 unsigned int cur_dac_format;
180 unsigned int cur_hp_stream_tag;
181 unsigned int cur_hp_format;
182
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200183 /* dynamic ADC switching */
184 hda_nid_t cur_adc;
185 unsigned int cur_adc_stream_tag;
186 unsigned int cur_adc_format;
187
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800188 /* PCM information */
189 struct hda_pcm pcm_rec[3];
190
191 /* dynamic controls, init_verbs and input_mux */
192 struct auto_pin_cfg autocfg;
193 struct snd_array kctls;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800194 hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
195
196 /* HP mode source */
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800197 unsigned int hp_independent_mode;
Lydia Wangf3db4232009-10-10 19:08:41 +0800198 unsigned int dmic_enabled;
Takashi Iwai24088a52011-06-17 16:59:21 +0200199 unsigned int no_pin_power_ctl;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800200 enum VIA_HDA_CODEC codec_type;
201
Takashi Iwaie9d010c2012-02-01 10:33:23 +0100202 /* analog low-power control */
203 bool alc_mode;
204
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200205 /* smart51 setup */
206 unsigned int smart51_nums;
207 hda_nid_t smart51_pins[2];
208 int smart51_idxs[2];
209 const char *smart51_labels[2];
210 unsigned int smart51_enabled;
211
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800212 /* work to check hp jack state */
213 struct hda_codec *codec;
214 struct delayed_work vt1708_hp_work;
Takashi Iwai187d3332011-11-24 16:33:09 +0100215 int hp_work_active;
Takashi Iwaie06e5a22011-06-17 15:46:13 +0200216 int vt1708_jack_detect;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800217 int vt1708_hp_present;
Lydia Wang3e95b9a2011-03-23 15:13:28 +0800218
219 void (*set_widgets_power_state)(struct hda_codec *codec);
220
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800221 struct hda_loopback_check loopback;
Takashi Iwai13af8e72011-06-20 14:05:46 +0200222 int num_loopbacks;
223 struct hda_amp_list loopback_list[8];
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200224
225 /* bind capture-volume */
226 struct hda_bind_ctls *bind_cap_vol;
227 struct hda_bind_ctls *bind_cap_sw;
Takashi Iwai3b607e32011-07-18 16:54:40 +0200228
229 struct mutex config_mutex;
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800230};
231
Lydia Wang0341ccd2011-03-22 16:25:03 +0800232static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100233static struct via_spec * via_new_spec(struct hda_codec *codec)
234{
235 struct via_spec *spec;
236
237 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
238 if (spec == NULL)
239 return NULL;
240
Takashi Iwai3b607e32011-07-18 16:54:40 +0200241 mutex_init(&spec->config_mutex);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100242 codec->spec = spec;
243 spec->codec = codec;
Lydia Wang0341ccd2011-03-22 16:25:03 +0800244 spec->codec_type = get_codec_type(codec);
245 /* VT1708BCE & VT1708S are almost same */
246 if (spec->codec_type == VT1708BCE)
247 spec->codec_type = VT1708S;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100248 return spec;
249}
250
Lydia Wang744ff5f2009-10-10 19:07:26 +0800251static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
Harald Welted7426322008-09-15 22:43:23 +0800252{
Lydia Wang744ff5f2009-10-10 19:07:26 +0800253 u32 vendor_id = codec->vendor_id;
Harald Welted7426322008-09-15 22:43:23 +0800254 u16 ven_id = vendor_id >> 16;
255 u16 dev_id = vendor_id & 0xffff;
256 enum VIA_HDA_CODEC codec_type;
257
258 /* get codec type */
259 if (ven_id != 0x1106)
260 codec_type = UNKNOWN;
261 else if (dev_id >= 0x1708 && dev_id <= 0x170b)
262 codec_type = VT1708;
263 else if (dev_id >= 0xe710 && dev_id <= 0xe713)
264 codec_type = VT1709_10CH;
265 else if (dev_id >= 0xe714 && dev_id <= 0xe717)
266 codec_type = VT1709_6CH;
Lydia Wang518bf3b2009-10-10 19:07:29 +0800267 else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
Harald Welted7426322008-09-15 22:43:23 +0800268 codec_type = VT1708B_8CH;
Lydia Wang518bf3b2009-10-10 19:07:29 +0800269 if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
270 codec_type = VT1708BCE;
271 } else if (dev_id >= 0xe724 && dev_id <= 0xe727)
Harald Welted7426322008-09-15 22:43:23 +0800272 codec_type = VT1708B_4CH;
273 else if ((dev_id & 0xfff) == 0x397
274 && (dev_id >> 12) < 8)
275 codec_type = VT1708S;
276 else if ((dev_id & 0xfff) == 0x398
277 && (dev_id >> 12) < 8)
278 codec_type = VT1702;
Lydia Wangeb7188c2009-10-10 19:08:34 +0800279 else if ((dev_id & 0xfff) == 0x428
280 && (dev_id >> 12) < 8)
281 codec_type = VT1718S;
Lydia Wangf3db4232009-10-10 19:08:41 +0800282 else if (dev_id == 0x0433 || dev_id == 0xa721)
283 codec_type = VT1716S;
Lydia Wangbb3c6bfc2009-10-10 19:08:39 +0800284 else if (dev_id == 0x0441 || dev_id == 0x4441)
285 codec_type = VT1718S;
Lydia Wang25eaba22009-10-10 19:08:43 +0800286 else if (dev_id == 0x0438 || dev_id == 0x4438)
287 codec_type = VT2002P;
Lydia Wangab6734e2009-10-10 19:08:46 +0800288 else if (dev_id == 0x0448)
289 codec_type = VT1812;
Lydia Wang36dd5c42009-10-20 13:18:04 +0800290 else if (dev_id == 0x0440)
291 codec_type = VT1708S;
Lydia Wang118909562011-03-23 17:57:34 +0800292 else if ((dev_id & 0xfff) == 0x446)
293 codec_type = VT1802;
Harald Welted7426322008-09-15 22:43:23 +0800294 else
295 codec_type = UNKNOWN;
296 return codec_type;
297};
298
Lydia Wangec7e7e42011-03-24 12:43:44 +0800299#define VIA_JACK_EVENT 0x20
Harald Welte69e52a82008-09-09 15:57:32 +0800300#define VIA_HP_EVENT 0x01
301#define VIA_GPIO_EVENT 0x02
Takashi Iwai4a918ff2011-06-20 12:39:26 +0200302#define VIA_LINE_EVENT 0x03
Harald Welte69e52a82008-09-09 15:57:32 +0800303
Joseph Chanc577b8a2006-11-29 15:29:40 +0100304enum {
305 VIA_CTL_WIDGET_VOL,
306 VIA_CTL_WIDGET_MUTE,
Lydia Wangf5271102009-10-10 19:07:35 +0800307 VIA_CTL_WIDGET_ANALOG_MUTE,
Joseph Chanc577b8a2006-11-29 15:29:40 +0100308};
309
Takashi Iwaiada509e2011-06-20 15:40:19 +0200310static void analog_low_current_mode(struct hda_codec *codec);
311static bool is_aa_path_mute(struct hda_codec *codec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800312
Takashi Iwai187d3332011-11-24 16:33:09 +0100313#define hp_detect_with_aa(codec) \
314 (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1 && \
315 !is_aa_path_mute(codec))
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800316
317static void vt1708_stop_hp_work(struct via_spec *spec)
318{
319 if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
320 return;
Takashi Iwai187d3332011-11-24 16:33:09 +0100321 if (spec->hp_work_active) {
322 snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 1);
323 cancel_delayed_work_sync(&spec->vt1708_hp_work);
324 spec->hp_work_active = 0;
325 }
326}
327
328static void vt1708_update_hp_work(struct via_spec *spec)
329{
330 if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800331 return;
Takashi Iwai187d3332011-11-24 16:33:09 +0100332 if (spec->vt1708_jack_detect &&
333 (spec->active_streams || hp_detect_with_aa(spec->codec))) {
334 if (!spec->hp_work_active) {
335 snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 0);
336 schedule_delayed_work(&spec->vt1708_hp_work,
337 msecs_to_jiffies(100));
338 spec->hp_work_active = 1;
339 }
340 } else if (!hp_detect_with_aa(spec->codec))
341 vt1708_stop_hp_work(spec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +0800342}
Lydia Wangf5271102009-10-10 19:07:35 +0800343
Lydia Wang3e95b9a2011-03-23 15:13:28 +0800344static void set_widgets_power_state(struct hda_codec *codec)
345{
346 struct via_spec *spec = codec->spec;
347 if (spec->set_widgets_power_state)
348 spec->set_widgets_power_state(codec);
349}
Lydia Wang25eaba22009-10-10 19:08:43 +0800350
Lydia Wangf5271102009-10-10 19:07:35 +0800351static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
352 struct snd_ctl_elem_value *ucontrol)
353{
354 int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
355 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
356
Lydia Wang3e95b9a2011-03-23 15:13:28 +0800357 set_widgets_power_state(codec);
Takashi Iwaiada509e2011-06-20 15:40:19 +0200358 analog_low_current_mode(snd_kcontrol_chip(kcontrol));
Takashi Iwai187d3332011-11-24 16:33:09 +0100359 vt1708_update_hp_work(codec->spec);
Lydia Wangf5271102009-10-10 19:07:35 +0800360 return change;
361}
362
363/* modify .put = snd_hda_mixer_amp_switch_put */
364#define ANALOG_INPUT_MUTE \
365 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
366 .name = NULL, \
367 .index = 0, \
368 .info = snd_hda_mixer_amp_switch_info, \
369 .get = snd_hda_mixer_amp_switch_get, \
370 .put = analog_input_switch_put, \
371 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
372
Takashi Iwai90dd48a2011-05-02 12:38:19 +0200373static const struct snd_kcontrol_new via_control_templates[] = {
Joseph Chanc577b8a2006-11-29 15:29:40 +0100374 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
375 HDA_CODEC_MUTE(NULL, 0, 0, 0),
Lydia Wangf5271102009-10-10 19:07:35 +0800376 ANALOG_INPUT_MUTE,
Joseph Chanc577b8a2006-11-29 15:29:40 +0100377};
378
Lydia Wangab6734e2009-10-10 19:08:46 +0800379
Joseph Chanc577b8a2006-11-29 15:29:40 +0100380/* add dynamic controls */
Takashi Iwai291c9e32011-06-17 16:15:26 +0200381static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
382 const struct snd_kcontrol_new *tmpl,
383 const char *name)
Joseph Chanc577b8a2006-11-29 15:29:40 +0100384{
385 struct snd_kcontrol_new *knew;
386
Takashi Iwai603c4012008-07-30 15:01:44 +0200387 snd_array_init(&spec->kctls, sizeof(*knew), 32);
388 knew = snd_array_new(&spec->kctls);
389 if (!knew)
Takashi Iwai291c9e32011-06-17 16:15:26 +0200390 return NULL;
391 *knew = *tmpl;
392 if (!name)
393 name = tmpl->name;
394 if (name) {
395 knew->name = kstrdup(name, GFP_KERNEL);
396 if (!knew->name)
397 return NULL;
398 }
399 return knew;
400}
401
402static int __via_add_control(struct via_spec *spec, int type, const char *name,
403 int idx, unsigned long val)
404{
405 struct snd_kcontrol_new *knew;
406
407 knew = __via_clone_ctl(spec, &via_control_templates[type], name);
408 if (!knew)
Joseph Chanc577b8a2006-11-29 15:29:40 +0100409 return -ENOMEM;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +0200410 knew->index = idx;
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +0100411 if (get_amp_nid_(val))
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100412 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100413 knew->private_value = val;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100414 return 0;
415}
416
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200417#define via_add_control(spec, type, name, val) \
418 __via_add_control(spec, type, name, 0, val)
419
Takashi Iwai291c9e32011-06-17 16:15:26 +0200420#define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100421
Takashi Iwai603c4012008-07-30 15:01:44 +0200422static void via_free_kctls(struct hda_codec *codec)
423{
424 struct via_spec *spec = codec->spec;
425
426 if (spec->kctls.list) {
427 struct snd_kcontrol_new *kctl = spec->kctls.list;
428 int i;
429 for (i = 0; i < spec->kctls.used; i++)
430 kfree(kctl[i].name);
431 }
432 snd_array_free(&spec->kctls);
433}
434
Joseph Chanc577b8a2006-11-29 15:29:40 +0100435/* create input playback/capture controls for the given pin */
Lydia Wang9510e8d2009-10-10 19:07:39 +0800436static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200437 int type_idx, int idx, int mix_nid)
Joseph Chanc577b8a2006-11-29 15:29:40 +0100438{
439 char name[32];
440 int err;
441
442 sprintf(name, "%s Playback Volume", ctlname);
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200443 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
Joseph Chanc577b8a2006-11-29 15:29:40 +0100444 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
445 if (err < 0)
446 return err;
447 sprintf(name, "%s Playback Switch", ctlname);
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200448 err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
Joseph Chanc577b8a2006-11-29 15:29:40 +0100449 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
450 if (err < 0)
451 return err;
452 return 0;
453}
454
Takashi Iwai5d417622011-06-20 11:32:27 +0200455#define get_connection_index(codec, mux, nid) \
Takashi Iwai8d087c72011-06-28 12:45:47 +0200456 snd_hda_get_conn_index(codec, mux, nid, 0)
Takashi Iwai5d417622011-06-20 11:32:27 +0200457
Takashi Iwai8df2a312011-06-21 11:48:29 +0200458static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
459 unsigned int mask)
460{
Takashi Iwaia934d5a2011-06-21 14:22:14 +0200461 unsigned int caps;
462 if (!nid)
463 return false;
464 caps = get_wcaps(codec, nid);
Takashi Iwai8df2a312011-06-21 11:48:29 +0200465 if (dir == HDA_INPUT)
466 caps &= AC_WCAP_IN_AMP;
467 else
468 caps &= AC_WCAP_OUT_AMP;
469 if (!caps)
470 return false;
471 if (query_amp_caps(codec, nid, dir) & mask)
472 return true;
473 return false;
474}
475
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200476#define have_mute(codec, nid, dir) \
477 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
Takashi Iwai8df2a312011-06-21 11:48:29 +0200478
Lydia Wangd69607b32011-07-08 14:02:52 +0800479/* enable/disable the output-route mixers */
480static void activate_output_mix(struct hda_codec *codec, struct nid_path *path,
Takashi Iwai3214b962011-07-18 12:49:25 +0200481 hda_nid_t mix_nid, int idx, bool enable)
Lydia Wangd69607b32011-07-08 14:02:52 +0800482{
483 int i, num, val;
Lydia Wangd69607b32011-07-08 14:02:52 +0800484
485 if (!path)
486 return;
487 num = snd_hda_get_conn_list(codec, mix_nid, NULL);
Lydia Wangd69607b32011-07-08 14:02:52 +0800488 for (i = 0; i < num; i++) {
Takashi Iwai3214b962011-07-18 12:49:25 +0200489 if (i == idx)
490 val = AMP_IN_UNMUTE(i);
491 else
492 val = AMP_IN_MUTE(i);
Lydia Wangd69607b32011-07-08 14:02:52 +0800493 snd_hda_codec_write(codec, mix_nid, 0,
494 AC_VERB_SET_AMP_GAIN_MUTE, val);
495 }
496}
497
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200498/* enable/disable the output-route */
499static void activate_output_path(struct hda_codec *codec, struct nid_path *path,
500 bool enable, bool force)
Takashi Iwai5d417622011-06-20 11:32:27 +0200501{
Lydia Wangd69607b32011-07-08 14:02:52 +0800502 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200503 int i;
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200504 for (i = 0; i < path->depth; i++) {
505 hda_nid_t src, dst;
506 int idx = path->idx[i];
507 src = path->path[i];
508 if (i < path->depth - 1)
509 dst = path->path[i + 1];
510 else
511 dst = 0;
512 if (enable && path->multi[i])
513 snd_hda_codec_write(codec, dst, 0,
514 AC_VERB_SET_CONNECT_SEL, idx);
Takashi Iwai3214b962011-07-18 12:49:25 +0200515 if (!force && (dst == spec->aa_mix_nid))
Lydia Wange5e14682011-07-01 10:55:07 +0800516 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +0200517 if (have_mute(codec, dst, HDA_INPUT))
518 activate_output_mix(codec, path, dst, idx, enable);
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200519 if (!force && (src == path->vol_ctl || src == path->mute_ctl))
520 continue;
521 if (have_mute(codec, src, HDA_OUTPUT)) {
522 int val = enable ? AMP_OUT_UNMUTE : AMP_OUT_MUTE;
523 snd_hda_codec_write(codec, src, 0,
524 AC_VERB_SET_AMP_GAIN_MUTE, val);
525 }
526 }
Takashi Iwai5d417622011-06-20 11:32:27 +0200527}
528
529/* set the given pin as output */
530static void init_output_pin(struct hda_codec *codec, hda_nid_t pin,
531 int pin_type)
532{
533 if (!pin)
534 return;
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200535 snd_hda_set_pin_ctl(codec, pin, pin_type);
Takashi Iwai5d417622011-06-20 11:32:27 +0200536 if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
537 snd_hda_codec_write(codec, pin, 0,
Takashi Iwaid3a11e62009-07-07 13:43:35 +0200538 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
Joseph Chanc577b8a2006-11-29 15:29:40 +0100539}
540
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200541static void via_auto_init_output(struct hda_codec *codec,
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200542 struct nid_path *path, int pin_type)
Takashi Iwai5d417622011-06-20 11:32:27 +0200543{
Takashi Iwai5d417622011-06-20 11:32:27 +0200544 unsigned int caps;
Lydia Wangd69607b32011-07-08 14:02:52 +0800545 hda_nid_t pin;
Takashi Iwai5d417622011-06-20 11:32:27 +0200546
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200547 if (!path->depth)
Takashi Iwai5d417622011-06-20 11:32:27 +0200548 return;
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200549 pin = path->path[path->depth - 1];
Takashi Iwai5d417622011-06-20 11:32:27 +0200550
551 init_output_pin(codec, pin, pin_type);
Takashi Iwai77e314f2012-02-22 12:34:08 +0100552 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
553 caps = query_amp_caps(codec, pin, HDA_OUTPUT);
554 else
555 caps = 0;
Takashi Iwai5d417622011-06-20 11:32:27 +0200556 if (caps & AC_AMPCAP_MUTE) {
557 unsigned int val;
558 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
559 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
560 AMP_OUT_MUTE | val);
561 }
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200562 activate_output_path(codec, path, true, true); /* force on */
Takashi Iwai5d417622011-06-20 11:32:27 +0200563}
564
Joseph Chanc577b8a2006-11-29 15:29:40 +0100565static void via_auto_init_multi_out(struct hda_codec *codec)
566{
567 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200568 struct nid_path *path;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100569 int i;
570
Takashi Iwai3214b962011-07-18 12:49:25 +0200571 for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++) {
572 path = &spec->out_path[i];
573 if (!i && spec->aamix_mode && spec->out_mix_path.depth)
574 path = &spec->out_mix_path;
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200575 via_auto_init_output(codec, path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200576 }
Joseph Chanc577b8a2006-11-29 15:29:40 +0100577}
578
Takashi Iwai020066d2011-07-21 13:45:56 +0200579/* deactivate the inactive headphone-paths */
580static void deactivate_hp_paths(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +0100581{
582 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200583 int shared = spec->hp_indep_shared;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100584
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200585 if (spec->hp_independent_mode) {
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200586 activate_output_path(codec, &spec->hp_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200587 activate_output_path(codec, &spec->hp_mix_path, false, false);
588 if (shared)
589 activate_output_path(codec, &spec->out_path[shared],
590 false, false);
Takashi Iwai020066d2011-07-21 13:45:56 +0200591 } else if (spec->aamix_mode || !spec->hp_path.depth) {
592 activate_output_path(codec, &spec->hp_indep_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200593 activate_output_path(codec, &spec->hp_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200594 } else {
Takashi Iwai020066d2011-07-21 13:45:56 +0200595 activate_output_path(codec, &spec->hp_indep_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200596 activate_output_path(codec, &spec->hp_mix_path, false, false);
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200597 }
Joseph Chanc577b8a2006-11-29 15:29:40 +0100598}
599
Takashi Iwai020066d2011-07-21 13:45:56 +0200600static void via_auto_init_hp_out(struct hda_codec *codec)
601{
602 struct via_spec *spec = codec->spec;
603
604 if (!spec->hp_path.depth) {
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200605 via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200606 return;
607 }
608 deactivate_hp_paths(codec);
609 if (spec->hp_independent_mode)
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200610 via_auto_init_output(codec, &spec->hp_indep_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200611 else if (spec->aamix_mode)
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200612 via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200613 else
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200614 via_auto_init_output(codec, &spec->hp_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200615}
616
Takashi Iwai4a918ff2011-06-20 12:39:26 +0200617static void via_auto_init_speaker_out(struct hda_codec *codec)
618{
619 struct via_spec *spec = codec->spec;
620
Takashi Iwai3214b962011-07-18 12:49:25 +0200621 if (!spec->autocfg.speaker_outs)
622 return;
623 if (!spec->speaker_path.depth) {
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200624 via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200625 return;
626 }
627 if (!spec->aamix_mode) {
628 activate_output_path(codec, &spec->speaker_mix_path,
629 false, false);
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200630 via_auto_init_output(codec, &spec->speaker_path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200631 } else {
632 activate_output_path(codec, &spec->speaker_path, false, false);
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200633 via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200634 }
Takashi Iwai4a918ff2011-06-20 12:39:26 +0200635}
636
Takashi Iwaif4a78282011-06-17 18:46:48 +0200637static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
Takashi Iwai6e969d92011-07-11 11:28:13 +0200638static void via_hp_automute(struct hda_codec *codec);
Clemens Ladisch32e01912010-07-12 16:28:50 +0200639
Joseph Chanc577b8a2006-11-29 15:29:40 +0100640static void via_auto_init_analog_input(struct hda_codec *codec)
641{
642 struct via_spec *spec = codec->spec;
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200643 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai096a8852011-06-20 12:09:02 +0200644 hda_nid_t conn[HDA_MAX_CONNECTIONS];
Clemens Ladisch32e01912010-07-12 16:28:50 +0200645 unsigned int ctl;
Takashi Iwai096a8852011-06-20 12:09:02 +0200646 int i, num_conns;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100647
Takashi Iwai096a8852011-06-20 12:09:02 +0200648 /* init ADCs */
649 for (i = 0; i < spec->num_adc_nids; i++) {
Takashi Iwai77e314f2012-02-22 12:34:08 +0100650 hda_nid_t nid = spec->adc_nids[i];
651 if (!(get_wcaps(codec, nid) & AC_WCAP_IN_AMP) ||
652 !(query_amp_caps(codec, nid, HDA_INPUT) & AC_AMPCAP_MUTE))
653 continue;
Takashi Iwai096a8852011-06-20 12:09:02 +0200654 snd_hda_codec_write(codec, spec->adc_nids[i], 0,
655 AC_VERB_SET_AMP_GAIN_MUTE,
656 AMP_IN_UNMUTE(0));
657 }
658
659 /* init pins */
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200660 for (i = 0; i < cfg->num_inputs; i++) {
661 hda_nid_t nid = cfg->inputs[i].pin;
Takashi Iwaif4a78282011-06-17 18:46:48 +0200662 if (spec->smart51_enabled && is_smart51_pins(codec, nid))
Clemens Ladisch32e01912010-07-12 16:28:50 +0200663 ctl = PIN_OUT;
Takashi Iwai47408602012-04-20 13:06:53 +0200664 else {
Clemens Ladisch32e01912010-07-12 16:28:50 +0200665 ctl = PIN_IN;
Takashi Iwai47408602012-04-20 13:06:53 +0200666 if (cfg->inputs[i].type == AUTO_PIN_MIC)
667 ctl |= snd_hda_get_default_vref(codec, nid);
668 }
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200669 snd_hda_set_pin_ctl(codec, nid, ctl);
Joseph Chanc577b8a2006-11-29 15:29:40 +0100670 }
Takashi Iwai096a8852011-06-20 12:09:02 +0200671
672 /* init input-src */
673 for (i = 0; i < spec->num_adc_nids; i++) {
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200674 int adc_idx = spec->inputs[spec->cur_mux[i]].adc_idx;
Takashi Iwaifc1156c2012-02-13 15:04:06 +0100675 /* secondary ADCs must have the unique MUX */
676 if (i > 0 && !spec->mux_nids[i])
677 break;
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200678 if (spec->mux_nids[adc_idx]) {
679 int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
680 snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
681 AC_VERB_SET_CONNECT_SEL,
682 mux_idx);
683 }
684 if (spec->dyn_adc_switch)
685 break; /* only one input-src */
Takashi Iwai096a8852011-06-20 12:09:02 +0200686 }
687
688 /* init aa-mixer */
689 if (!spec->aa_mix_nid)
690 return;
691 num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
692 ARRAY_SIZE(conn));
693 for (i = 0; i < num_conns; i++) {
694 unsigned int caps = get_wcaps(codec, conn[i]);
695 if (get_wcaps_type(caps) == AC_WID_PIN)
696 snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
697 AC_VERB_SET_AMP_GAIN_MUTE,
698 AMP_IN_MUTE(i));
699 }
Joseph Chanc577b8a2006-11-29 15:29:40 +0100700}
Lydia Wangf5271102009-10-10 19:07:35 +0800701
Takashi Iwai054d8672012-01-24 12:25:50 +0100702static void update_power_state(struct hda_codec *codec, hda_nid_t nid,
703 unsigned int parm)
704{
705 if (snd_hda_codec_read(codec, nid, 0,
706 AC_VERB_GET_POWER_STATE, 0) == parm)
707 return;
708 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
709}
710
Lydia Wangf5271102009-10-10 19:07:35 +0800711static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
712 unsigned int *affected_parm)
713{
714 unsigned parm;
715 unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
716 unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
717 >> AC_DEFCFG_MISC_SHIFT
718 & AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
Lydia Wang1564b282009-10-10 19:07:52 +0800719 struct via_spec *spec = codec->spec;
Takashi Iwai24088a52011-06-17 16:59:21 +0200720 unsigned present = 0;
721
722 no_presence |= spec->no_pin_power_ctl;
723 if (!no_presence)
724 present = snd_hda_jack_detect(codec, nid);
Takashi Iwaif4a78282011-06-17 18:46:48 +0200725 if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
Lydia Wang1564b282009-10-10 19:07:52 +0800726 || ((no_presence || present)
727 && get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
Lydia Wangf5271102009-10-10 19:07:35 +0800728 *affected_parm = AC_PWRST_D0; /* if it's connected */
729 parm = AC_PWRST_D0;
730 } else
731 parm = AC_PWRST_D3;
732
Takashi Iwai054d8672012-01-24 12:25:50 +0100733 update_power_state(codec, nid, parm);
Lydia Wangf5271102009-10-10 19:07:35 +0800734}
735
Takashi Iwai24088a52011-06-17 16:59:21 +0200736static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
737 struct snd_ctl_elem_info *uinfo)
738{
739 static const char * const texts[] = {
740 "Disabled", "Enabled"
741 };
742
743 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
744 uinfo->count = 1;
745 uinfo->value.enumerated.items = 2;
746 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
747 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
748 strcpy(uinfo->value.enumerated.name,
749 texts[uinfo->value.enumerated.item]);
750 return 0;
751}
752
753static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
754 struct snd_ctl_elem_value *ucontrol)
755{
756 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
757 struct via_spec *spec = codec->spec;
758 ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
759 return 0;
760}
761
762static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
763 struct snd_ctl_elem_value *ucontrol)
764{
765 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
766 struct via_spec *spec = codec->spec;
767 unsigned int val = !ucontrol->value.enumerated.item[0];
768
769 if (val == spec->no_pin_power_ctl)
770 return 0;
771 spec->no_pin_power_ctl = val;
772 set_widgets_power_state(codec);
Takashi Iwaie9d010c2012-02-01 10:33:23 +0100773 analog_low_current_mode(codec);
Takashi Iwai24088a52011-06-17 16:59:21 +0200774 return 1;
775}
776
777static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
778 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
779 .name = "Dynamic Power-Control",
780 .info = via_pin_power_ctl_info,
781 .get = via_pin_power_ctl_get,
782 .put = via_pin_power_ctl_put,
783};
784
785
Harald Welte0aa62ae2008-09-09 15:58:27 +0800786static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
787 struct snd_ctl_elem_info *uinfo)
788{
Takashi Iwai8df2a312011-06-21 11:48:29 +0200789 static const char * const texts[] = { "OFF", "ON" };
790
791 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
792 uinfo->count = 1;
793 uinfo->value.enumerated.items = 2;
794 if (uinfo->value.enumerated.item >= 2)
795 uinfo->value.enumerated.item = 1;
796 strcpy(uinfo->value.enumerated.name,
797 texts[uinfo->value.enumerated.item]);
798 return 0;
Harald Welte0aa62ae2008-09-09 15:58:27 +0800799}
800
801static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
802 struct snd_ctl_elem_value *ucontrol)
803{
804 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Lydia Wangcdc17842009-10-10 19:07:47 +0800805 struct via_spec *spec = codec->spec;
Lydia Wangcdc17842009-10-10 19:07:47 +0800806
Takashi Iwaiece8d042011-06-19 16:24:21 +0200807 ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
Lydia Wangcdc17842009-10-10 19:07:47 +0800808 return 0;
809}
810
Takashi Iwai3b607e32011-07-18 16:54:40 +0200811/* adjust spec->multiout setup according to the current flags */
812static void setup_playback_multi_pcm(struct via_spec *spec)
813{
814 const struct auto_pin_cfg *cfg = &spec->autocfg;
815 spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
816 spec->multiout.hp_nid = 0;
817 if (!spec->hp_independent_mode) {
818 if (!spec->hp_indep_shared)
819 spec->multiout.hp_nid = spec->hp_dac_nid;
820 } else {
821 if (spec->hp_indep_shared)
822 spec->multiout.num_dacs = cfg->line_outs - 1;
823 }
824}
825
826/* update DAC setups according to indep-HP switch;
827 * this function is called only when indep-HP is modified
828 */
829static void switch_indep_hp_dacs(struct hda_codec *codec)
830{
831 struct via_spec *spec = codec->spec;
832 int shared = spec->hp_indep_shared;
833 hda_nid_t shared_dac, hp_dac;
834
835 if (!spec->opened_streams)
836 return;
837
838 shared_dac = shared ? spec->multiout.dac_nids[shared] : 0;
839 hp_dac = spec->hp_dac_nid;
840 if (spec->hp_independent_mode) {
841 /* switch to indep-HP mode */
842 if (spec->active_streams & STREAM_MULTI_OUT) {
843 __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
844 __snd_hda_codec_cleanup_stream(codec, shared_dac, 1);
845 }
846 if (spec->active_streams & STREAM_INDEP_HP)
847 snd_hda_codec_setup_stream(codec, hp_dac,
848 spec->cur_hp_stream_tag, 0,
849 spec->cur_hp_format);
850 } else {
851 /* back to HP or shared-DAC */
852 if (spec->active_streams & STREAM_INDEP_HP)
853 __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
854 if (spec->active_streams & STREAM_MULTI_OUT) {
855 hda_nid_t dac;
856 int ch;
857 if (shared_dac) { /* reset mutli-ch DAC */
858 dac = shared_dac;
859 ch = shared * 2;
860 } else { /* reset HP DAC */
861 dac = hp_dac;
862 ch = 0;
863 }
864 snd_hda_codec_setup_stream(codec, dac,
865 spec->cur_dac_stream_tag, ch,
866 spec->cur_dac_format);
867 }
868 }
869 setup_playback_multi_pcm(spec);
870}
871
Harald Welte0aa62ae2008-09-09 15:58:27 +0800872static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
873 struct snd_ctl_elem_value *ucontrol)
874{
875 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
876 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200877 int cur, shared;
Takashi Iwai8df2a312011-06-21 11:48:29 +0200878
Takashi Iwai3b607e32011-07-18 16:54:40 +0200879 mutex_lock(&spec->config_mutex);
Takashi Iwai25250502011-06-30 17:24:47 +0200880 cur = !!ucontrol->value.enumerated.item[0];
Takashi Iwai3b607e32011-07-18 16:54:40 +0200881 if (spec->hp_independent_mode == cur) {
882 mutex_unlock(&spec->config_mutex);
Takashi Iwai25250502011-06-30 17:24:47 +0200883 return 0;
Takashi Iwai3b607e32011-07-18 16:54:40 +0200884 }
Takashi Iwai25250502011-06-30 17:24:47 +0200885 spec->hp_independent_mode = cur;
Takashi Iwai3214b962011-07-18 12:49:25 +0200886 shared = spec->hp_indep_shared;
Takashi Iwai020066d2011-07-21 13:45:56 +0200887 deactivate_hp_paths(codec);
888 if (cur)
889 activate_output_path(codec, &spec->hp_indep_path, true, false);
890 else {
Takashi Iwai3214b962011-07-18 12:49:25 +0200891 if (shared)
892 activate_output_path(codec, &spec->out_path[shared],
Takashi Iwai25250502011-06-30 17:24:47 +0200893 true, false);
Takashi Iwai020066d2011-07-21 13:45:56 +0200894 if (spec->aamix_mode || !spec->hp_path.depth)
895 activate_output_path(codec, &spec->hp_mix_path,
896 true, false);
897 else
898 activate_output_path(codec, &spec->hp_path,
899 true, false);
Takashi Iwai8df2a312011-06-21 11:48:29 +0200900 }
Harald Welte0aa62ae2008-09-09 15:58:27 +0800901
Takashi Iwai3b607e32011-07-18 16:54:40 +0200902 switch_indep_hp_dacs(codec);
903 mutex_unlock(&spec->config_mutex);
904
Lydia Wangce0e5a92011-03-22 16:22:37 +0800905 /* update jack power state */
Lydia Wang3e95b9a2011-03-23 15:13:28 +0800906 set_widgets_power_state(codec);
Takashi Iwai6e969d92011-07-11 11:28:13 +0200907 via_hp_automute(codec);
Takashi Iwai25250502011-06-30 17:24:47 +0200908 return 1;
Harald Welte0aa62ae2008-09-09 15:58:27 +0800909}
910
Takashi Iwaiece8d042011-06-19 16:24:21 +0200911static const struct snd_kcontrol_new via_hp_mixer = {
912 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
913 .name = "Independent HP",
914 .info = via_independent_hp_info,
915 .get = via_independent_hp_get,
916 .put = via_independent_hp_put,
Harald Welte0aa62ae2008-09-09 15:58:27 +0800917};
918
Takashi Iwai3d83e572010-04-14 14:36:23 +0200919static int via_hp_build(struct hda_codec *codec)
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100920{
Takashi Iwai3d83e572010-04-14 14:36:23 +0200921 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100922 struct snd_kcontrol_new *knew;
923 hda_nid_t nid;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100924
Takashi Iwaiece8d042011-06-19 16:24:21 +0200925 nid = spec->autocfg.hp_pins[0];
926 knew = via_clone_control(spec, &via_hp_mixer);
Takashi Iwai3d83e572010-04-14 14:36:23 +0200927 if (knew == NULL)
928 return -ENOMEM;
929
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100930 knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100931
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100932 return 0;
933}
934
Lydia Wang1564b282009-10-10 19:07:52 +0800935static void notify_aa_path_ctls(struct hda_codec *codec)
936{
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200937 struct via_spec *spec = codec->spec;
Lydia Wang1564b282009-10-10 19:07:52 +0800938 int i;
Lydia Wang1564b282009-10-10 19:07:52 +0800939
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200940 for (i = 0; i < spec->smart51_nums; i++) {
941 struct snd_kcontrol *ctl;
942 struct snd_ctl_elem_id id;
943 memset(&id, 0, sizeof(id));
944 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
945 sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
Lydia Wang525566c2011-04-28 16:03:39 +0800946 ctl = snd_hda_find_mixer_ctl(codec, id.name);
947 if (ctl)
948 snd_ctl_notify(codec->bus->card,
949 SNDRV_CTL_EVENT_MASK_VALUE,
950 &ctl->id);
Lydia Wang1564b282009-10-10 19:07:52 +0800951 }
952}
953
954static void mute_aa_path(struct hda_codec *codec, int mute)
955{
956 struct via_spec *spec = codec->spec;
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200957 int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
Lydia Wang1564b282009-10-10 19:07:52 +0800958 int i;
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200959
Lydia Wang1564b282009-10-10 19:07:52 +0800960 /* check AA path's mute status */
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200961 for (i = 0; i < spec->smart51_nums; i++) {
962 if (spec->smart51_idxs[i] < 0)
963 continue;
964 snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
965 HDA_INPUT, spec->smart51_idxs[i],
Lydia Wang1564b282009-10-10 19:07:52 +0800966 HDA_AMP_MUTE, val);
967 }
968}
Takashi Iwaif4a78282011-06-17 18:46:48 +0200969
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200970static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
971{
972 struct via_spec *spec = codec->spec;
973 int i;
974
975 for (i = 0; i < spec->smart51_nums; i++)
976 if (spec->smart51_pins[i] == pin)
977 return true;
978 return false;
979}
980
Lydia Wang1564b282009-10-10 19:07:52 +0800981static int via_smart51_get(struct snd_kcontrol *kcontrol,
982 struct snd_ctl_elem_value *ucontrol)
983{
984 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
985 struct via_spec *spec = codec->spec;
Lydia Wang1564b282009-10-10 19:07:52 +0800986
Takashi Iwaif2b1c9f2011-06-21 16:52:39 +0200987 *ucontrol->value.integer.value = spec->smart51_enabled;
Lydia Wang1564b282009-10-10 19:07:52 +0800988 return 0;
989}
990
991static int via_smart51_put(struct snd_kcontrol *kcontrol,
992 struct snd_ctl_elem_value *ucontrol)
993{
994 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
995 struct via_spec *spec = codec->spec;
996 int out_in = *ucontrol->value.integer.value
997 ? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
Lydia Wang1564b282009-10-10 19:07:52 +0800998 int i;
999
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001000 for (i = 0; i < spec->smart51_nums; i++) {
1001 hda_nid_t nid = spec->smart51_pins[i];
Takashi Iwai7b315bb2010-08-30 13:06:30 +02001002 unsigned int parm;
1003
Takashi Iwai7b315bb2010-08-30 13:06:30 +02001004 parm = snd_hda_codec_read(codec, nid, 0,
1005 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1006 parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
1007 parm |= out_in;
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001008 snd_hda_set_pin_ctl(codec, nid, parm);
Takashi Iwai7b315bb2010-08-30 13:06:30 +02001009 if (out_in == AC_PINCTL_OUT_EN) {
1010 mute_aa_path(codec, 1);
1011 notify_aa_path_ctls(codec);
1012 }
Lydia Wang1564b282009-10-10 19:07:52 +08001013 }
1014 spec->smart51_enabled = *ucontrol->value.integer.value;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08001015 set_widgets_power_state(codec);
Lydia Wang1564b282009-10-10 19:07:52 +08001016 return 1;
1017}
1018
Takashi Iwai5f4b36d2011-06-17 14:55:02 +02001019static const struct snd_kcontrol_new via_smart51_mixer = {
1020 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1021 .name = "Smart 5.1",
1022 .count = 1,
Takashi Iwaif2b1c9f2011-06-21 16:52:39 +02001023 .info = snd_ctl_boolean_mono_info,
Takashi Iwai5f4b36d2011-06-17 14:55:02 +02001024 .get = via_smart51_get,
1025 .put = via_smart51_put,
Lydia Wang1564b282009-10-10 19:07:52 +08001026};
1027
Takashi Iwaif4a78282011-06-17 18:46:48 +02001028static int via_smart51_build(struct hda_codec *codec)
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001029{
Takashi Iwaif4a78282011-06-17 18:46:48 +02001030 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001031
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001032 if (!spec->smart51_nums)
Lydia Wangcb34c202011-04-27 17:44:16 +08001033 return 0;
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001034 if (!via_clone_control(spec, &via_smart51_mixer))
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001035 return -ENOMEM;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001036 return 0;
1037}
1038
Takashi Iwaiada509e2011-06-20 15:40:19 +02001039/* check AA path's mute status */
1040static bool is_aa_path_mute(struct hda_codec *codec)
Lydia Wangf5271102009-10-10 19:07:35 +08001041{
Lydia Wangf5271102009-10-10 19:07:35 +08001042 struct via_spec *spec = codec->spec;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001043 const struct hda_amp_list *p;
1044 int i, ch, v;
1045
1046 for (i = 0; i < spec->num_loopbacks; i++) {
1047 p = &spec->loopback_list[i];
1048 for (ch = 0; ch < 2; ch++) {
1049 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
1050 p->idx);
1051 if (!(v & HDA_AMP_MUTE) && v > 0)
1052 return false;
Lydia Wangf5271102009-10-10 19:07:35 +08001053 }
1054 }
Takashi Iwaiada509e2011-06-20 15:40:19 +02001055 return true;
Lydia Wangf5271102009-10-10 19:07:35 +08001056}
1057
1058/* enter/exit analog low-current mode */
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001059static void __analog_low_current_mode(struct hda_codec *codec, bool force)
Lydia Wangf5271102009-10-10 19:07:35 +08001060{
1061 struct via_spec *spec = codec->spec;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001062 bool enable;
1063 unsigned int verb, parm;
Lydia Wangf5271102009-10-10 19:07:35 +08001064
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001065 if (spec->no_pin_power_ctl)
1066 enable = false;
1067 else
1068 enable = is_aa_path_mute(codec) && !spec->opened_streams;
1069 if (enable == spec->alc_mode && !force)
1070 return;
1071 spec->alc_mode = enable;
Lydia Wangf5271102009-10-10 19:07:35 +08001072
1073 /* decide low current mode's verb & parameter */
1074 switch (spec->codec_type) {
1075 case VT1708B_8CH:
1076 case VT1708B_4CH:
1077 verb = 0xf70;
1078 parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1079 break;
1080 case VT1708S:
Lydia Wangeb7188c2009-10-10 19:08:34 +08001081 case VT1718S:
Lydia Wangf3db4232009-10-10 19:08:41 +08001082 case VT1716S:
Lydia Wangf5271102009-10-10 19:07:35 +08001083 verb = 0xf73;
1084 parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1085 break;
1086 case VT1702:
1087 verb = 0xf73;
1088 parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1089 break;
Lydia Wang25eaba22009-10-10 19:08:43 +08001090 case VT2002P:
Lydia Wangab6734e2009-10-10 19:08:46 +08001091 case VT1812:
Lydia Wang118909562011-03-23 17:57:34 +08001092 case VT1802:
Lydia Wang25eaba22009-10-10 19:08:43 +08001093 verb = 0xf93;
1094 parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
1095 break;
Lydia Wangf5271102009-10-10 19:07:35 +08001096 default:
1097 return; /* other codecs are not supported */
1098 }
1099 /* send verb */
1100 snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1101}
1102
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001103static void analog_low_current_mode(struct hda_codec *codec)
1104{
1105 return __analog_low_current_mode(codec, false);
1106}
1107
Joseph Chanc577b8a2006-11-29 15:29:40 +01001108/*
1109 * generic initialization of ADC, input mixers and output mixers
1110 */
Takashi Iwai096a8852011-06-20 12:09:02 +02001111static const struct hda_verb vt1708_init_verbs[] = {
Lydia Wangaa266fc2011-03-24 12:41:01 +08001112 /* power down jack detect function */
1113 {0x1, 0xf81, 0x1},
Josepch Chanf7278fd2007-12-13 16:40:40 +01001114 { }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001115};
1116
Takashi Iwai3b607e32011-07-18 16:54:40 +02001117static void set_stream_open(struct hda_codec *codec, int bit, bool active)
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001118{
Takashi Iwaiada509e2011-06-20 15:40:19 +02001119 struct via_spec *spec = codec->spec;
1120
1121 if (active)
Takashi Iwai3b607e32011-07-18 16:54:40 +02001122 spec->opened_streams |= bit;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001123 else
Takashi Iwai3b607e32011-07-18 16:54:40 +02001124 spec->opened_streams &= ~bit;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001125 analog_low_current_mode(codec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001126}
1127
Takashi Iwaiece8d042011-06-19 16:24:21 +02001128static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001129 struct hda_codec *codec,
1130 struct snd_pcm_substream *substream)
1131{
1132 struct via_spec *spec = codec->spec;
Takashi Iwai25250502011-06-30 17:24:47 +02001133 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001134 int err;
Takashi Iwaiece8d042011-06-19 16:24:21 +02001135
Takashi Iwai25250502011-06-30 17:24:47 +02001136 spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
Takashi Iwai25250502011-06-30 17:24:47 +02001137 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001138 set_stream_open(codec, STREAM_MULTI_OUT, true);
Takashi Iwaiada509e2011-06-20 15:40:19 +02001139 err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
1140 hinfo);
1141 if (err < 0) {
Takashi Iwai3b607e32011-07-18 16:54:40 +02001142 set_stream_open(codec, STREAM_MULTI_OUT, false);
Takashi Iwaiada509e2011-06-20 15:40:19 +02001143 return err;
1144 }
1145 return 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001146}
1147
Takashi Iwaiece8d042011-06-19 16:24:21 +02001148static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
Takashi Iwai9af74212011-06-18 16:17:45 +02001149 struct hda_codec *codec,
1150 struct snd_pcm_substream *substream)
1151{
Takashi Iwai3b607e32011-07-18 16:54:40 +02001152 set_stream_open(codec, STREAM_MULTI_OUT, false);
Takashi Iwai9af74212011-06-18 16:17:45 +02001153 return 0;
1154}
1155
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001156static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
1157 struct hda_codec *codec,
1158 struct snd_pcm_substream *substream)
1159{
1160 struct via_spec *spec = codec->spec;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001161
Takashi Iwaiece8d042011-06-19 16:24:21 +02001162 if (snd_BUG_ON(!spec->hp_dac_nid))
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001163 return -EINVAL;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001164 set_stream_open(codec, STREAM_INDEP_HP, true);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001165 return 0;
1166}
1167
1168static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1169 struct hda_codec *codec,
1170 struct snd_pcm_substream *substream)
1171{
Takashi Iwai3b607e32011-07-18 16:54:40 +02001172 set_stream_open(codec, STREAM_INDEP_HP, false);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001173 return 0;
1174}
1175
1176static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
1177 struct hda_codec *codec,
1178 unsigned int stream_tag,
1179 unsigned int format,
1180 struct snd_pcm_substream *substream)
Harald Welte0aa62ae2008-09-09 15:58:27 +08001181{
1182 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001183
Takashi Iwai3b607e32011-07-18 16:54:40 +02001184 mutex_lock(&spec->config_mutex);
1185 setup_playback_multi_pcm(spec);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001186 snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1187 format, substream);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001188 /* remember for dynamic DAC switch with indep-HP */
1189 spec->active_streams |= STREAM_MULTI_OUT;
1190 spec->cur_dac_stream_tag = stream_tag;
1191 spec->cur_dac_format = format;
1192 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001193 vt1708_update_hp_work(spec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001194 return 0;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001195}
1196
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001197static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
1198 struct hda_codec *codec,
1199 unsigned int stream_tag,
1200 unsigned int format,
1201 struct snd_pcm_substream *substream)
Harald Welte0aa62ae2008-09-09 15:58:27 +08001202{
1203 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001204
Takashi Iwai3b607e32011-07-18 16:54:40 +02001205 mutex_lock(&spec->config_mutex);
1206 if (spec->hp_independent_mode)
1207 snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1208 stream_tag, 0, format);
1209 spec->active_streams |= STREAM_INDEP_HP;
1210 spec->cur_hp_stream_tag = stream_tag;
1211 spec->cur_hp_format = format;
1212 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001213 vt1708_update_hp_work(spec);
Harald Welte0aa62ae2008-09-09 15:58:27 +08001214 return 0;
1215}
1216
1217static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
1218 struct hda_codec *codec,
1219 struct snd_pcm_substream *substream)
1220{
1221 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001222
Takashi Iwai3b607e32011-07-18 16:54:40 +02001223 mutex_lock(&spec->config_mutex);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001224 snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001225 spec->active_streams &= ~STREAM_MULTI_OUT;
1226 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001227 vt1708_update_hp_work(spec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001228 return 0;
1229}
1230
1231static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
1232 struct hda_codec *codec,
1233 struct snd_pcm_substream *substream)
1234{
1235 struct via_spec *spec = codec->spec;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001236
Takashi Iwai3b607e32011-07-18 16:54:40 +02001237 mutex_lock(&spec->config_mutex);
1238 if (spec->hp_independent_mode)
1239 snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
1240 spec->active_streams &= ~STREAM_INDEP_HP;
1241 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001242 vt1708_update_hp_work(spec);
Harald Welte0aa62ae2008-09-09 15:58:27 +08001243 return 0;
1244}
1245
Joseph Chanc577b8a2006-11-29 15:29:40 +01001246/*
1247 * Digital out
1248 */
1249static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1250 struct hda_codec *codec,
1251 struct snd_pcm_substream *substream)
1252{
1253 struct via_spec *spec = codec->spec;
1254 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1255}
1256
1257static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1258 struct hda_codec *codec,
1259 struct snd_pcm_substream *substream)
1260{
1261 struct via_spec *spec = codec->spec;
1262 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1263}
1264
Harald Welte5691ec72008-09-15 22:42:26 +08001265static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Harald Welte98aa34c2008-09-09 16:02:09 +08001266 struct hda_codec *codec,
1267 unsigned int stream_tag,
1268 unsigned int format,
1269 struct snd_pcm_substream *substream)
1270{
1271 struct via_spec *spec = codec->spec;
Takashi Iwai9da29272009-05-07 16:31:14 +02001272 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1273 stream_tag, format, substream);
1274}
Harald Welte5691ec72008-09-15 22:42:26 +08001275
Takashi Iwai9da29272009-05-07 16:31:14 +02001276static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1277 struct hda_codec *codec,
1278 struct snd_pcm_substream *substream)
1279{
1280 struct via_spec *spec = codec->spec;
1281 snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
Harald Welte98aa34c2008-09-09 16:02:09 +08001282 return 0;
1283}
1284
Joseph Chanc577b8a2006-11-29 15:29:40 +01001285/*
1286 * Analog capture
1287 */
1288static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1289 struct hda_codec *codec,
1290 unsigned int stream_tag,
1291 unsigned int format,
1292 struct snd_pcm_substream *substream)
1293{
1294 struct via_spec *spec = codec->spec;
1295
1296 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1297 stream_tag, 0, format);
1298 return 0;
1299}
1300
1301static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1302 struct hda_codec *codec,
1303 struct snd_pcm_substream *substream)
1304{
1305 struct via_spec *spec = codec->spec;
Takashi Iwai888afa12008-03-18 09:57:50 +01001306 snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001307 return 0;
1308}
1309
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001310/* analog capture with dynamic ADC switching */
1311static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1312 struct hda_codec *codec,
1313 unsigned int stream_tag,
1314 unsigned int format,
1315 struct snd_pcm_substream *substream)
1316{
1317 struct via_spec *spec = codec->spec;
1318 int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
1319
Takashi Iwai3b607e32011-07-18 16:54:40 +02001320 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001321 spec->cur_adc = spec->adc_nids[adc_idx];
1322 spec->cur_adc_stream_tag = stream_tag;
1323 spec->cur_adc_format = format;
1324 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001325 mutex_unlock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001326 return 0;
1327}
1328
1329static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1330 struct hda_codec *codec,
1331 struct snd_pcm_substream *substream)
1332{
1333 struct via_spec *spec = codec->spec;
1334
Takashi Iwai3b607e32011-07-18 16:54:40 +02001335 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001336 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1337 spec->cur_adc = 0;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001338 mutex_unlock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001339 return 0;
1340}
1341
1342/* re-setup the stream if running; called from input-src put */
1343static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
1344{
1345 struct via_spec *spec = codec->spec;
1346 int adc_idx = spec->inputs[cur].adc_idx;
1347 hda_nid_t adc = spec->adc_nids[adc_idx];
Takashi Iwai3b607e32011-07-18 16:54:40 +02001348 bool ret = false;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001349
Takashi Iwai3b607e32011-07-18 16:54:40 +02001350 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001351 if (spec->cur_adc && spec->cur_adc != adc) {
1352 /* stream is running, let's swap the current ADC */
1353 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1354 spec->cur_adc = adc;
1355 snd_hda_codec_setup_stream(codec, adc,
1356 spec->cur_adc_stream_tag, 0,
1357 spec->cur_adc_format);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001358 ret = true;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001359 }
Takashi Iwai3b607e32011-07-18 16:54:40 +02001360 mutex_unlock(&spec->config_mutex);
1361 return ret;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001362}
1363
Takashi Iwai9af74212011-06-18 16:17:45 +02001364static const struct hda_pcm_stream via_pcm_analog_playback = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001365 .substreams = 1,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001366 .channels_min = 2,
1367 .channels_max = 8,
Takashi Iwai9af74212011-06-18 16:17:45 +02001368 /* NID is set in via_build_pcms */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001369 .ops = {
Takashi Iwaiece8d042011-06-19 16:24:21 +02001370 .open = via_playback_multi_pcm_open,
1371 .close = via_playback_multi_pcm_close,
Harald Welte0aa62ae2008-09-09 15:58:27 +08001372 .prepare = via_playback_multi_pcm_prepare,
1373 .cleanup = via_playback_multi_pcm_cleanup
Joseph Chanc577b8a2006-11-29 15:29:40 +01001374 },
1375};
1376
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001377static const struct hda_pcm_stream via_pcm_hp_playback = {
1378 .substreams = 1,
1379 .channels_min = 2,
1380 .channels_max = 2,
1381 /* NID is set in via_build_pcms */
1382 .ops = {
1383 .open = via_playback_hp_pcm_open,
Takashi Iwaiece8d042011-06-19 16:24:21 +02001384 .close = via_playback_hp_pcm_close,
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001385 .prepare = via_playback_hp_pcm_prepare,
1386 .cleanup = via_playback_hp_pcm_cleanup
1387 },
1388};
1389
Takashi Iwai90dd48a2011-05-02 12:38:19 +02001390static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001391 .substreams = 1,
Takashi Iwaibc9b562382008-05-23 17:50:27 +02001392 .channels_min = 2,
1393 .channels_max = 8,
Takashi Iwai9af74212011-06-18 16:17:45 +02001394 /* NID is set in via_build_pcms */
Takashi Iwaibc9b562382008-05-23 17:50:27 +02001395 /* We got noisy outputs on the right channel on VT1708 when
1396 * 24bit samples are used. Until any workaround is found,
1397 * disable the 24bit format, so far.
1398 */
1399 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1400 .ops = {
Takashi Iwaiece8d042011-06-19 16:24:21 +02001401 .open = via_playback_multi_pcm_open,
1402 .close = via_playback_multi_pcm_close,
Lydia Wangc873cc22009-10-10 19:08:21 +08001403 .prepare = via_playback_multi_pcm_prepare,
1404 .cleanup = via_playback_multi_pcm_cleanup
Takashi Iwaibc9b562382008-05-23 17:50:27 +02001405 },
1406};
1407
Takashi Iwai9af74212011-06-18 16:17:45 +02001408static const struct hda_pcm_stream via_pcm_analog_capture = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001409 .substreams = 1, /* will be changed in via_build_pcms() */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001410 .channels_min = 2,
1411 .channels_max = 2,
Takashi Iwai9af74212011-06-18 16:17:45 +02001412 /* NID is set in via_build_pcms */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001413 .ops = {
1414 .prepare = via_capture_pcm_prepare,
1415 .cleanup = via_capture_pcm_cleanup
1416 },
1417};
1418
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001419static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
1420 .substreams = 1,
1421 .channels_min = 2,
1422 .channels_max = 2,
1423 /* NID is set in via_build_pcms */
1424 .ops = {
1425 .prepare = via_dyn_adc_capture_pcm_prepare,
1426 .cleanup = via_dyn_adc_capture_pcm_cleanup,
1427 },
1428};
1429
Takashi Iwai9af74212011-06-18 16:17:45 +02001430static const struct hda_pcm_stream via_pcm_digital_playback = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001431 .substreams = 1,
1432 .channels_min = 2,
1433 .channels_max = 2,
1434 /* NID is set in via_build_pcms */
1435 .ops = {
1436 .open = via_dig_playback_pcm_open,
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001437 .close = via_dig_playback_pcm_close,
Takashi Iwai9da29272009-05-07 16:31:14 +02001438 .prepare = via_dig_playback_pcm_prepare,
1439 .cleanup = via_dig_playback_pcm_cleanup
Joseph Chanc577b8a2006-11-29 15:29:40 +01001440 },
1441};
1442
Takashi Iwai9af74212011-06-18 16:17:45 +02001443static const struct hda_pcm_stream via_pcm_digital_capture = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001444 .substreams = 1,
1445 .channels_min = 2,
1446 .channels_max = 2,
1447};
1448
Takashi Iwai370bafb2011-06-20 12:47:45 +02001449/*
1450 * slave controls for virtual master
1451 */
Takashi Iwai9322ca52012-02-03 14:28:01 +01001452static const char * const via_slave_pfxs[] = {
1453 "Front", "Surround", "Center", "LFE", "Side",
1454 "Headphone", "Speaker",
Takashi Iwai370bafb2011-06-20 12:47:45 +02001455 NULL,
1456};
1457
Joseph Chanc577b8a2006-11-29 15:29:40 +01001458static int via_build_controls(struct hda_codec *codec)
1459{
1460 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001461 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001462 int err, i;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001463
Takashi Iwaib5bcc182012-02-02 10:30:17 +01001464 spec->no_pin_power_ctl = 1;
Takashi Iwai24088a52011-06-17 16:59:21 +02001465 if (spec->set_widgets_power_state)
1466 if (!via_clone_control(spec, &via_pin_power_ctl_enum))
1467 return -ENOMEM;
1468
Joseph Chanc577b8a2006-11-29 15:29:40 +01001469 for (i = 0; i < spec->num_mixers; i++) {
1470 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1471 if (err < 0)
1472 return err;
1473 }
1474
1475 if (spec->multiout.dig_out_nid) {
1476 err = snd_hda_create_spdif_out_ctls(codec,
Stephen Warren74b654c2011-06-01 11:14:18 -06001477 spec->multiout.dig_out_nid,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001478 spec->multiout.dig_out_nid);
1479 if (err < 0)
1480 return err;
Takashi Iwai9a081602008-02-12 18:37:26 +01001481 err = snd_hda_create_spdif_share_sw(codec,
1482 &spec->multiout);
1483 if (err < 0)
1484 return err;
1485 spec->multiout.share_spdif = 1;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001486 }
1487 if (spec->dig_in_nid) {
1488 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1489 if (err < 0)
1490 return err;
1491 }
Lydia Wang17314372009-10-10 19:07:37 +08001492
Takashi Iwai370bafb2011-06-20 12:47:45 +02001493 /* if we have no master control, let's create it */
1494 if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1495 unsigned int vmaster_tlv[4];
1496 snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1497 HDA_OUTPUT, vmaster_tlv);
1498 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai9322ca52012-02-03 14:28:01 +01001499 vmaster_tlv, via_slave_pfxs,
1500 "Playback Volume");
Takashi Iwai370bafb2011-06-20 12:47:45 +02001501 if (err < 0)
1502 return err;
1503 }
1504 if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1505 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
Takashi Iwai9322ca52012-02-03 14:28:01 +01001506 NULL, via_slave_pfxs,
1507 "Playback Switch");
Takashi Iwai370bafb2011-06-20 12:47:45 +02001508 if (err < 0)
1509 return err;
1510 }
1511
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001512 /* assign Capture Source enums to NID */
1513 kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1514 for (i = 0; kctl && i < kctl->count; i++) {
Takashi Iwai77e314f2012-02-22 12:34:08 +01001515 if (!spec->mux_nids[i])
1516 continue;
Takashi Iwai21949f02009-12-23 08:31:59 +01001517 err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001518 if (err < 0)
1519 return err;
1520 }
1521
Takashi Iwai603c4012008-07-30 15:01:44 +02001522 via_free_kctls(codec); /* no longer needed */
Takashi Iwai01a61e12011-10-28 00:03:22 +02001523
1524 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1525 if (err < 0)
1526 return err;
1527
Joseph Chanc577b8a2006-11-29 15:29:40 +01001528 return 0;
1529}
1530
1531static int via_build_pcms(struct hda_codec *codec)
1532{
1533 struct via_spec *spec = codec->spec;
1534 struct hda_pcm *info = spec->pcm_rec;
1535
Takashi Iwaia5973102011-09-28 16:43:36 +02001536 codec->num_pcms = 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001537 codec->pcm_info = info;
1538
Takashi Iwaia5973102011-09-28 16:43:36 +02001539 if (spec->multiout.num_dacs || spec->num_adc_nids) {
1540 snprintf(spec->stream_name_analog,
1541 sizeof(spec->stream_name_analog),
1542 "%s Analog", codec->chip_name);
1543 info->name = spec->stream_name_analog;
Takashi Iwai9af74212011-06-18 16:17:45 +02001544
Takashi Iwaia5973102011-09-28 16:43:36 +02001545 if (spec->multiout.num_dacs) {
1546 if (!spec->stream_analog_playback)
1547 spec->stream_analog_playback =
1548 &via_pcm_analog_playback;
1549 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1550 *spec->stream_analog_playback;
1551 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1552 spec->multiout.dac_nids[0];
1553 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1554 spec->multiout.max_channels;
1555 }
Takashi Iwai9af74212011-06-18 16:17:45 +02001556
Takashi Iwaia5973102011-09-28 16:43:36 +02001557 if (!spec->stream_analog_capture) {
1558 if (spec->dyn_adc_switch)
1559 spec->stream_analog_capture =
1560 &via_pcm_dyn_adc_analog_capture;
1561 else
1562 spec->stream_analog_capture =
1563 &via_pcm_analog_capture;
1564 }
1565 if (spec->num_adc_nids) {
1566 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1567 *spec->stream_analog_capture;
1568 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1569 spec->adc_nids[0];
1570 if (!spec->dyn_adc_switch)
1571 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
1572 spec->num_adc_nids;
1573 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001574 codec->num_pcms++;
1575 info++;
Takashi Iwaia5973102011-09-28 16:43:36 +02001576 }
1577
1578 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwai82673bc2011-06-17 16:24:21 +02001579 snprintf(spec->stream_name_digital,
1580 sizeof(spec->stream_name_digital),
1581 "%s Digital", codec->chip_name);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001582 info->name = spec->stream_name_digital;
Takashi Iwai7ba72ba2008-02-06 14:03:20 +01001583 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001584 if (spec->multiout.dig_out_nid) {
Takashi Iwai9af74212011-06-18 16:17:45 +02001585 if (!spec->stream_digital_playback)
1586 spec->stream_digital_playback =
1587 &via_pcm_digital_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001588 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
Takashi Iwai9af74212011-06-18 16:17:45 +02001589 *spec->stream_digital_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001590 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1591 spec->multiout.dig_out_nid;
1592 }
1593 if (spec->dig_in_nid) {
Takashi Iwai9af74212011-06-18 16:17:45 +02001594 if (!spec->stream_digital_capture)
1595 spec->stream_digital_capture =
1596 &via_pcm_digital_capture;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001597 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
Takashi Iwai9af74212011-06-18 16:17:45 +02001598 *spec->stream_digital_capture;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001599 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1600 spec->dig_in_nid;
1601 }
Takashi Iwaia5973102011-09-28 16:43:36 +02001602 codec->num_pcms++;
1603 info++;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001604 }
1605
Takashi Iwaiece8d042011-06-19 16:24:21 +02001606 if (spec->hp_dac_nid) {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001607 snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
1608 "%s HP", codec->chip_name);
1609 info->name = spec->stream_name_hp;
1610 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
1611 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
Takashi Iwaiece8d042011-06-19 16:24:21 +02001612 spec->hp_dac_nid;
Takashi Iwaia5973102011-09-28 16:43:36 +02001613 codec->num_pcms++;
1614 info++;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001615 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001616 return 0;
1617}
1618
1619static void via_free(struct hda_codec *codec)
1620{
1621 struct via_spec *spec = codec->spec;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001622
1623 if (!spec)
1624 return;
1625
Takashi Iwai603c4012008-07-30 15:01:44 +02001626 via_free_kctls(codec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001627 vt1708_stop_hp_work(spec);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001628 kfree(spec->bind_cap_vol);
1629 kfree(spec->bind_cap_sw);
1630 kfree(spec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001631}
1632
Takashi Iwai64be2852011-06-17 16:51:39 +02001633/* mute/unmute outputs */
1634static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
1635 hda_nid_t *pins, bool mute)
1636{
1637 int i;
Takashi Iwai94994732011-07-11 11:36:44 +02001638 for (i = 0; i < num_pins; i++) {
1639 unsigned int parm = snd_hda_codec_read(codec, pins[i], 0,
1640 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1641 if (parm & AC_PINCTL_IN_EN)
1642 continue;
1643 if (mute)
1644 parm &= ~AC_PINCTL_OUT_EN;
1645 else
1646 parm |= AC_PINCTL_OUT_EN;
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001647 snd_hda_set_pin_ctl(codec, pins[i], parm);
Takashi Iwai94994732011-07-11 11:36:44 +02001648 }
Takashi Iwai64be2852011-06-17 16:51:39 +02001649}
1650
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001651/* mute internal speaker if line-out is plugged */
1652static void via_line_automute(struct hda_codec *codec, int present)
1653{
1654 struct via_spec *spec = codec->spec;
1655
1656 if (!spec->autocfg.speaker_outs)
1657 return;
1658 if (!present)
1659 present = snd_hda_jack_detect(codec,
1660 spec->autocfg.line_out_pins[0]);
1661 toggle_output_mutes(codec, spec->autocfg.speaker_outs,
1662 spec->autocfg.speaker_pins,
1663 present);
1664}
1665
Harald Welte69e52a82008-09-09 15:57:32 +08001666/* mute internal speaker if HP is plugged */
1667static void via_hp_automute(struct hda_codec *codec)
1668{
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001669 int present = 0;
Takashi Iwai6e969d92011-07-11 11:28:13 +02001670 int nums;
Harald Welte69e52a82008-09-09 15:57:32 +08001671 struct via_spec *spec = codec->spec;
1672
Takashi Iwai187d3332011-11-24 16:33:09 +01001673 if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0] &&
1674 (spec->codec_type != VT1708 || spec->vt1708_jack_detect))
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001675 present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
Takashi Iwai6e969d92011-07-11 11:28:13 +02001676
1677 if (spec->smart51_enabled)
1678 nums = spec->autocfg.line_outs + spec->smart51_nums;
1679 else
1680 nums = spec->autocfg.line_outs;
1681 toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
1682
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001683 via_line_automute(codec, present);
Lydia Wangf3db4232009-10-10 19:08:41 +08001684}
1685
Harald Welte69e52a82008-09-09 15:57:32 +08001686static void via_gpio_control(struct hda_codec *codec)
1687{
1688 unsigned int gpio_data;
1689 unsigned int vol_counter;
1690 unsigned int vol;
1691 unsigned int master_vol;
1692
1693 struct via_spec *spec = codec->spec;
1694
1695 gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
1696 AC_VERB_GET_GPIO_DATA, 0) & 0x03;
1697
1698 vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
1699 0xF84, 0) & 0x3F0000) >> 16;
1700
1701 vol = vol_counter & 0x1F;
1702 master_vol = snd_hda_codec_read(codec, 0x1A, 0,
1703 AC_VERB_GET_AMP_GAIN_MUTE,
1704 AC_AMP_GET_INPUT);
1705
1706 if (gpio_data == 0x02) {
1707 /* unmute line out */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001708 snd_hda_set_pin_ctl(codec, spec->autocfg.line_out_pins[0],
Takashi Iwai3e0693e2011-06-17 16:37:45 +02001709 PIN_OUT);
Harald Welte69e52a82008-09-09 15:57:32 +08001710 if (vol_counter & 0x20) {
1711 /* decrease volume */
1712 if (vol > master_vol)
1713 vol = master_vol;
1714 snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
1715 0, HDA_AMP_VOLMASK,
1716 master_vol-vol);
1717 } else {
1718 /* increase volume */
1719 snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
1720 HDA_AMP_VOLMASK,
1721 ((master_vol+vol) > 0x2A) ? 0x2A :
1722 (master_vol+vol));
1723 }
1724 } else if (!(gpio_data & 0x02)) {
1725 /* mute line out */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001726 snd_hda_set_pin_ctl(codec, spec->autocfg.line_out_pins[0], 0);
Harald Welte69e52a82008-09-09 15:57:32 +08001727 }
1728}
1729
1730/* unsolicited event for jack sensing */
1731static void via_unsol_event(struct hda_codec *codec,
1732 unsigned int res)
1733{
1734 res >>= 26;
Takashi Iwai3a938972011-10-28 01:16:55 +02001735 res = snd_hda_jack_get_action(codec, res);
Lydia Wangec7e7e42011-03-24 12:43:44 +08001736
Lydia Wanga34df192009-10-10 19:08:01 +08001737 if (res & VIA_JACK_EVENT)
Lydia Wang3e95b9a2011-03-23 15:13:28 +08001738 set_widgets_power_state(codec);
Lydia Wangec7e7e42011-03-24 12:43:44 +08001739
1740 res &= ~VIA_JACK_EVENT;
1741
Takashi Iwai21ce0b62011-07-11 10:33:47 +02001742 if (res == VIA_HP_EVENT || res == VIA_LINE_EVENT)
Lydia Wangec7e7e42011-03-24 12:43:44 +08001743 via_hp_automute(codec);
1744 else if (res == VIA_GPIO_EVENT)
1745 via_gpio_control(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001746 snd_hda_jack_report_sync(codec);
Harald Welte69e52a82008-09-09 15:57:32 +08001747}
1748
Takashi Iwai2a439522011-07-26 09:52:50 +02001749#ifdef CONFIG_PM
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001750static int via_suspend(struct hda_codec *codec, pm_message_t state)
1751{
1752 struct via_spec *spec = codec->spec;
1753 vt1708_stop_hp_work(spec);
1754 return 0;
1755}
1756#endif
1757
Takashi Iwaicb53c622007-08-10 17:21:45 +02001758#ifdef CONFIG_SND_HDA_POWER_SAVE
1759static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1760{
1761 struct via_spec *spec = codec->spec;
1762 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1763}
1764#endif
1765
Joseph Chanc577b8a2006-11-29 15:29:40 +01001766/*
1767 */
Takashi Iwai5d417622011-06-20 11:32:27 +02001768
1769static int via_init(struct hda_codec *codec);
1770
Takashi Iwai90dd48a2011-05-02 12:38:19 +02001771static const struct hda_codec_ops via_patch_ops = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001772 .build_controls = via_build_controls,
1773 .build_pcms = via_build_pcms,
1774 .init = via_init,
1775 .free = via_free,
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001776 .unsol_event = via_unsol_event,
Takashi Iwai2a439522011-07-26 09:52:50 +02001777#ifdef CONFIG_PM
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001778 .suspend = via_suspend,
1779#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02001780#ifdef CONFIG_SND_HDA_POWER_SAVE
1781 .check_power_status = via_check_power_status,
1782#endif
Joseph Chanc577b8a2006-11-29 15:29:40 +01001783};
1784
Takashi Iwai4a796162011-06-17 17:53:38 +02001785static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
Joseph Chanc577b8a2006-11-29 15:29:40 +01001786{
Takashi Iwai4a796162011-06-17 17:53:38 +02001787 struct via_spec *spec = codec->spec;
1788 int i;
1789
1790 for (i = 0; i < spec->multiout.num_dacs; i++) {
1791 if (spec->multiout.dac_nids[i] == dac)
1792 return false;
1793 }
Takashi Iwaiece8d042011-06-19 16:24:21 +02001794 if (spec->hp_dac_nid == dac)
Takashi Iwai4a796162011-06-17 17:53:38 +02001795 return false;
1796 return true;
1797}
1798
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001799static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3214b962011-07-18 12:49:25 +02001800 hda_nid_t target_dac, int with_aa_mix,
1801 struct nid_path *path, int depth)
Takashi Iwai4a796162011-06-17 17:53:38 +02001802{
Takashi Iwai3214b962011-07-18 12:49:25 +02001803 struct via_spec *spec = codec->spec;
Takashi Iwai4a796162011-06-17 17:53:38 +02001804 hda_nid_t conn[8];
1805 int i, nums;
1806
Takashi Iwai3214b962011-07-18 12:49:25 +02001807 if (nid == spec->aa_mix_nid) {
1808 if (!with_aa_mix)
1809 return false;
1810 with_aa_mix = 2; /* mark aa-mix is included */
1811 }
1812
Takashi Iwai4a796162011-06-17 17:53:38 +02001813 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
1814 for (i = 0; i < nums; i++) {
1815 if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
1816 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02001817 if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
1818 /* aa-mix is requested but not included? */
1819 if (!(spec->aa_mix_nid && with_aa_mix == 1))
1820 goto found;
1821 }
Takashi Iwai4a796162011-06-17 17:53:38 +02001822 }
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001823 if (depth >= MAX_NID_PATH_DEPTH)
Takashi Iwai4a796162011-06-17 17:53:38 +02001824 return false;
1825 for (i = 0; i < nums; i++) {
1826 unsigned int type;
1827 type = get_wcaps_type(get_wcaps(codec, conn[i]));
Takashi Iwai3214b962011-07-18 12:49:25 +02001828 if (type == AC_WID_AUD_OUT)
Takashi Iwai4a796162011-06-17 17:53:38 +02001829 continue;
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001830 if (__parse_output_path(codec, conn[i], target_dac,
Takashi Iwai3214b962011-07-18 12:49:25 +02001831 with_aa_mix, path, depth + 1))
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001832 goto found;
Takashi Iwai4a796162011-06-17 17:53:38 +02001833 }
1834 return false;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001835
1836 found:
1837 path->path[path->depth] = conn[i];
1838 path->idx[path->depth] = i;
1839 if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
1840 path->multi[path->depth] = 1;
1841 path->depth++;
1842 return true;
Takashi Iwai4a796162011-06-17 17:53:38 +02001843}
1844
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001845static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3214b962011-07-18 12:49:25 +02001846 hda_nid_t target_dac, int with_aa_mix,
1847 struct nid_path *path)
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001848{
Takashi Iwai3214b962011-07-18 12:49:25 +02001849 if (__parse_output_path(codec, nid, target_dac, with_aa_mix, path, 1)) {
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001850 path->path[path->depth] = nid;
1851 path->depth++;
Takashi Iwai3214b962011-07-18 12:49:25 +02001852 snd_printdd("output-path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
1853 path->depth, path->path[0], path->path[1],
1854 path->path[2], path->path[3], path->path[4]);
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001855 return true;
1856 }
1857 return false;
1858}
1859
Takashi Iwai4a796162011-06-17 17:53:38 +02001860static int via_auto_fill_dac_nids(struct hda_codec *codec)
1861{
1862 struct via_spec *spec = codec->spec;
1863 const struct auto_pin_cfg *cfg = &spec->autocfg;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001864 int i, dac_num;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001865 hda_nid_t nid;
1866
Joseph Chanc577b8a2006-11-29 15:29:40 +01001867 spec->multiout.dac_nids = spec->private_dac_nids;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001868 dac_num = 0;
Takashi Iwai4a796162011-06-17 17:53:38 +02001869 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai3214b962011-07-18 12:49:25 +02001870 hda_nid_t dac = 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001871 nid = cfg->line_out_pins[i];
Takashi Iwai4a796162011-06-17 17:53:38 +02001872 if (!nid)
1873 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02001874 if (parse_output_path(codec, nid, 0, 0, &spec->out_path[i]))
1875 dac = spec->out_path[i].path[0];
1876 if (!i && parse_output_path(codec, nid, dac, 1,
1877 &spec->out_mix_path))
1878 dac = spec->out_mix_path.path[0];
1879 if (dac) {
1880 spec->private_dac_nids[i] = dac;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001881 dac_num++;
1882 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001883 }
Takashi Iwai3214b962011-07-18 12:49:25 +02001884 if (!spec->out_path[0].depth && spec->out_mix_path.depth) {
1885 spec->out_path[0] = spec->out_mix_path;
1886 spec->out_mix_path.depth = 0;
1887 }
Lydia Wang5c9a5612011-07-08 14:03:43 +08001888 spec->multiout.num_dacs = dac_num;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001889 return 0;
1890}
1891
Takashi Iwai4a796162011-06-17 17:53:38 +02001892static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001893 int chs, bool check_dac, struct nid_path *path)
Joseph Chanc577b8a2006-11-29 15:29:40 +01001894{
Takashi Iwai4a796162011-06-17 17:53:38 +02001895 struct via_spec *spec = codec->spec;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001896 char name[32];
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001897 hda_nid_t dac, pin, sel, nid;
1898 int err;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001899
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001900 dac = check_dac ? path->path[0] : 0;
1901 pin = path->path[path->depth - 1];
1902 sel = path->depth > 1 ? path->path[1] : 0;
Takashi Iwai4a796162011-06-17 17:53:38 +02001903
Takashi Iwai8df2a312011-06-21 11:48:29 +02001904 if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
Takashi Iwai4a796162011-06-17 17:53:38 +02001905 nid = dac;
Takashi Iwai8df2a312011-06-21 11:48:29 +02001906 else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
Takashi Iwai4a796162011-06-17 17:53:38 +02001907 nid = pin;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001908 else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1909 nid = sel;
Takashi Iwai4a796162011-06-17 17:53:38 +02001910 else
1911 nid = 0;
1912 if (nid) {
1913 sprintf(name, "%s Playback Volume", pfx);
1914 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
Lydia Wanga00a5fa2011-06-21 16:11:11 +08001915 HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
Takashi Iwai4a796162011-06-17 17:53:38 +02001916 if (err < 0)
1917 return err;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001918 path->vol_ctl = nid;
Takashi Iwai4a796162011-06-17 17:53:38 +02001919 }
1920
Takashi Iwai8df2a312011-06-21 11:48:29 +02001921 if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
Takashi Iwai4a796162011-06-17 17:53:38 +02001922 nid = dac;
Takashi Iwai8df2a312011-06-21 11:48:29 +02001923 else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
Takashi Iwai4a796162011-06-17 17:53:38 +02001924 nid = pin;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001925 else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1926 nid = sel;
Takashi Iwai4a796162011-06-17 17:53:38 +02001927 else
1928 nid = 0;
1929 if (nid) {
1930 sprintf(name, "%s Playback Switch", pfx);
1931 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1932 HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1933 if (err < 0)
1934 return err;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001935 path->mute_ctl = nid;
Takashi Iwai4a796162011-06-17 17:53:38 +02001936 }
1937 return 0;
1938}
1939
Takashi Iwaif4a78282011-06-17 18:46:48 +02001940static void mangle_smart51(struct hda_codec *codec)
1941{
1942 struct via_spec *spec = codec->spec;
1943 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001944 struct auto_pin_cfg_item *ins = cfg->inputs;
1945 int i, j, nums, attr;
1946 int pins[AUTO_CFG_MAX_INS];
Takashi Iwaif4a78282011-06-17 18:46:48 +02001947
Takashi Iwai0f98c242011-06-21 12:51:33 +02001948 for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
1949 nums = 0;
1950 for (i = 0; i < cfg->num_inputs; i++) {
1951 unsigned int def;
1952 if (ins[i].type > AUTO_PIN_LINE_IN)
1953 continue;
1954 def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
1955 if (snd_hda_get_input_pin_attr(def) != attr)
1956 continue;
1957 for (j = 0; j < nums; j++)
1958 if (ins[pins[j]].type < ins[i].type) {
1959 memmove(pins + j + 1, pins + j,
Takashi Iwai21d45d22011-07-08 11:35:11 +02001960 (nums - j) * sizeof(int));
Takashi Iwai0f98c242011-06-21 12:51:33 +02001961 break;
1962 }
1963 pins[j] = i;
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001964 nums++;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001965 }
1966 if (cfg->line_outs + nums < 3)
Takashi Iwaif4a78282011-06-17 18:46:48 +02001967 continue;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001968 for (i = 0; i < nums; i++) {
1969 hda_nid_t pin = ins[pins[i]].pin;
1970 spec->smart51_pins[spec->smart51_nums++] = pin;
1971 cfg->line_out_pins[cfg->line_outs++] = pin;
1972 if (cfg->line_outs == 3)
1973 break;
1974 }
1975 return;
Takashi Iwaif4a78282011-06-17 18:46:48 +02001976 }
1977}
1978
Takashi Iwai020066d2011-07-21 13:45:56 +02001979static void copy_path_mixer_ctls(struct nid_path *dst, struct nid_path *src)
1980{
1981 dst->vol_ctl = src->vol_ctl;
1982 dst->mute_ctl = src->mute_ctl;
1983}
1984
Takashi Iwai4a796162011-06-17 17:53:38 +02001985/* add playback controls from the parsed DAC table */
1986static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
1987{
1988 struct via_spec *spec = codec->spec;
Takashi Iwaif4a78282011-06-17 18:46:48 +02001989 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai3214b962011-07-18 12:49:25 +02001990 struct nid_path *path;
Takashi Iwaiea734962011-01-17 11:29:34 +01001991 static const char * const chname[4] = {
1992 "Front", "Surround", "C/LFE", "Side"
1993 };
Takashi Iwai4a796162011-06-17 17:53:38 +02001994 int i, idx, err;
Takashi Iwaif4a78282011-06-17 18:46:48 +02001995 int old_line_outs;
1996
1997 /* check smart51 */
1998 old_line_outs = cfg->line_outs;
1999 if (cfg->line_outs == 1)
2000 mangle_smart51(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002001
Takashi Iwaie3d7a142011-06-20 13:52:33 +02002002 err = via_auto_fill_dac_nids(codec);
2003 if (err < 0)
2004 return err;
2005
Lydia Wang5c9a5612011-07-08 14:03:43 +08002006 if (spec->multiout.num_dacs < 3) {
2007 spec->smart51_nums = 0;
2008 cfg->line_outs = old_line_outs;
2009 }
Takashi Iwai4a796162011-06-17 17:53:38 +02002010 for (i = 0; i < cfg->line_outs; i++) {
2011 hda_nid_t pin, dac;
2012 pin = cfg->line_out_pins[i];
2013 dac = spec->multiout.dac_nids[i];
2014 if (!pin || !dac)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002015 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02002016 path = spec->out_path + i;
Takashi Iwai0fe0adf2011-06-19 16:27:53 +02002017 if (i == HDA_CLFE) {
Takashi Iwai3214b962011-07-18 12:49:25 +02002018 err = create_ch_ctls(codec, "Center", 1, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002019 if (err < 0)
2020 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02002021 err = create_ch_ctls(codec, "LFE", 2, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002022 if (err < 0)
2023 return err;
2024 } else {
Takashi Iwai6aadf412011-06-20 14:09:02 +02002025 const char *pfx = chname[i];
2026 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2027 cfg->line_outs == 1)
2028 pfx = "Speaker";
Takashi Iwai3214b962011-07-18 12:49:25 +02002029 err = create_ch_ctls(codec, pfx, 3, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002030 if (err < 0)
2031 return err;
2032 }
Takashi Iwai020066d2011-07-21 13:45:56 +02002033 if (path != spec->out_path + i)
2034 copy_path_mixer_ctls(&spec->out_path[i], path);
2035 if (path == spec->out_path && spec->out_mix_path.depth)
2036 copy_path_mixer_ctls(&spec->out_mix_path, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002037 }
2038
Takashi Iwai4a796162011-06-17 17:53:38 +02002039 idx = get_connection_index(codec, spec->aa_mix_nid,
2040 spec->multiout.dac_nids[0]);
2041 if (idx >= 0) {
2042 /* add control to mixer */
Takashi Iwai3214b962011-07-18 12:49:25 +02002043 const char *name;
2044 name = spec->out_mix_path.depth ?
2045 "PCM Loopback Playback Volume" : "PCM Playback Volume";
2046 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
Takashi Iwai4a796162011-06-17 17:53:38 +02002047 HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2048 idx, HDA_INPUT));
2049 if (err < 0)
2050 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02002051 name = spec->out_mix_path.depth ?
2052 "PCM Loopback Playback Switch" : "PCM Playback Switch";
2053 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
Takashi Iwai4a796162011-06-17 17:53:38 +02002054 HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2055 idx, HDA_INPUT));
2056 if (err < 0)
2057 return err;
2058 }
2059
Takashi Iwaif4a78282011-06-17 18:46:48 +02002060 cfg->line_outs = old_line_outs;
2061
Joseph Chanc577b8a2006-11-29 15:29:40 +01002062 return 0;
2063}
2064
Takashi Iwai4a796162011-06-17 17:53:38 +02002065static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002066{
Takashi Iwai4a796162011-06-17 17:53:38 +02002067 struct via_spec *spec = codec->spec;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02002068 struct nid_path *path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002069 bool check_dac;
Takashi Iwai3214b962011-07-18 12:49:25 +02002070 int i, err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002071
2072 if (!pin)
2073 return 0;
2074
Takashi Iwai3214b962011-07-18 12:49:25 +02002075 if (!parse_output_path(codec, pin, 0, 0, &spec->hp_indep_path)) {
2076 for (i = HDA_SIDE; i >= HDA_CLFE; i--) {
2077 if (i < spec->multiout.num_dacs &&
2078 parse_output_path(codec, pin,
2079 spec->multiout.dac_nids[i], 0,
2080 &spec->hp_indep_path)) {
2081 spec->hp_indep_shared = i;
2082 break;
2083 }
2084 }
Takashi Iwai25250502011-06-30 17:24:47 +02002085 }
Takashi Iwai3214b962011-07-18 12:49:25 +02002086 if (spec->hp_indep_path.depth) {
2087 spec->hp_dac_nid = spec->hp_indep_path.path[0];
2088 if (!spec->hp_indep_shared)
2089 spec->hp_path = spec->hp_indep_path;
2090 }
2091 /* optionally check front-path w/o AA-mix */
2092 if (!spec->hp_path.depth)
2093 parse_output_path(codec, pin,
2094 spec->multiout.dac_nids[HDA_FRONT], 0,
2095 &spec->hp_path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002096
Takashi Iwaiece8d042011-06-19 16:24:21 +02002097 if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
Takashi Iwai3214b962011-07-18 12:49:25 +02002098 1, &spec->hp_mix_path) && !spec->hp_path.depth)
Takashi Iwaiece8d042011-06-19 16:24:21 +02002099 return 0;
2100
Takashi Iwai3214b962011-07-18 12:49:25 +02002101 if (spec->hp_path.depth) {
Takashi Iwai09a9ad692011-06-21 15:57:44 +02002102 path = &spec->hp_path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002103 check_dac = true;
2104 } else {
Takashi Iwai3214b962011-07-18 12:49:25 +02002105 path = &spec->hp_mix_path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002106 check_dac = false;
2107 }
2108 err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002109 if (err < 0)
2110 return err;
Takashi Iwai020066d2011-07-21 13:45:56 +02002111 if (check_dac)
2112 copy_path_mixer_ctls(&spec->hp_mix_path, path);
2113 else
2114 copy_path_mixer_ctls(&spec->hp_path, path);
2115 if (spec->hp_indep_path.depth)
2116 copy_path_mixer_ctls(&spec->hp_indep_path, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002117 return 0;
2118}
2119
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002120static int via_auto_create_speaker_ctls(struct hda_codec *codec)
2121{
2122 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +02002123 struct nid_path *path;
2124 bool check_dac;
Wang Shaoyan81c0a782011-08-05 18:51:29 +08002125 hda_nid_t pin, dac = 0;
Takashi Iwai3214b962011-07-18 12:49:25 +02002126 int err;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002127
2128 pin = spec->autocfg.speaker_pins[0];
2129 if (!spec->autocfg.speaker_outs || !pin)
2130 return 0;
2131
Takashi Iwai3214b962011-07-18 12:49:25 +02002132 if (parse_output_path(codec, pin, 0, 0, &spec->speaker_path))
Takashi Iwai8e3679d2011-06-21 09:01:36 +02002133 dac = spec->speaker_path.path[0];
Takashi Iwai3214b962011-07-18 12:49:25 +02002134 if (!dac)
2135 parse_output_path(codec, pin,
2136 spec->multiout.dac_nids[HDA_FRONT], 0,
2137 &spec->speaker_path);
2138 if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
2139 1, &spec->speaker_mix_path) && !dac)
2140 return 0;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002141
Takashi Iwai3214b962011-07-18 12:49:25 +02002142 /* no AA-path for front? */
2143 if (!spec->out_mix_path.depth && spec->speaker_mix_path.depth)
2144 dac = 0;
2145
2146 spec->speaker_dac_nid = dac;
2147 spec->multiout.extra_out_nid[0] = dac;
2148 if (dac) {
2149 path = &spec->speaker_path;
2150 check_dac = true;
2151 } else {
2152 path = &spec->speaker_mix_path;
2153 check_dac = false;
2154 }
2155 err = create_ch_ctls(codec, "Speaker", 3, check_dac, path);
2156 if (err < 0)
2157 return err;
Takashi Iwai020066d2011-07-21 13:45:56 +02002158 if (check_dac)
2159 copy_path_mixer_ctls(&spec->speaker_mix_path, path);
2160 else
2161 copy_path_mixer_ctls(&spec->speaker_path, path);
Takashi Iwai3214b962011-07-18 12:49:25 +02002162 return 0;
2163}
2164
2165#define via_aamix_ctl_info via_pin_power_ctl_info
2166
2167static int via_aamix_ctl_get(struct snd_kcontrol *kcontrol,
2168 struct snd_ctl_elem_value *ucontrol)
2169{
2170 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2171 struct via_spec *spec = codec->spec;
2172 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2173 return 0;
2174}
2175
2176static void update_aamix_paths(struct hda_codec *codec, int do_mix,
2177 struct nid_path *nomix, struct nid_path *mix)
2178{
2179 if (do_mix) {
2180 activate_output_path(codec, nomix, false, false);
2181 activate_output_path(codec, mix, true, false);
2182 } else {
2183 activate_output_path(codec, mix, false, false);
2184 activate_output_path(codec, nomix, true, false);
2185 }
2186}
2187
2188static int via_aamix_ctl_put(struct snd_kcontrol *kcontrol,
2189 struct snd_ctl_elem_value *ucontrol)
2190{
2191 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2192 struct via_spec *spec = codec->spec;
2193 unsigned int val = ucontrol->value.enumerated.item[0];
2194
2195 if (val == spec->aamix_mode)
2196 return 0;
2197 spec->aamix_mode = val;
2198 /* update front path */
2199 update_aamix_paths(codec, val, &spec->out_path[0], &spec->out_mix_path);
2200 /* update HP path */
2201 if (!spec->hp_independent_mode) {
2202 update_aamix_paths(codec, val, &spec->hp_path,
2203 &spec->hp_mix_path);
2204 }
2205 /* update speaker path */
2206 update_aamix_paths(codec, val, &spec->speaker_path,
2207 &spec->speaker_mix_path);
2208 return 1;
2209}
2210
2211static const struct snd_kcontrol_new via_aamix_ctl_enum = {
2212 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2213 .name = "Loopback Mixing",
2214 .info = via_aamix_ctl_info,
2215 .get = via_aamix_ctl_get,
2216 .put = via_aamix_ctl_put,
2217};
2218
2219static int via_auto_create_loopback_switch(struct hda_codec *codec)
2220{
2221 struct via_spec *spec = codec->spec;
2222
Takashi Iwai4808d122012-01-10 15:16:02 +01002223 if (!spec->aa_mix_nid)
2224 return 0; /* no loopback switching available */
2225 if (!(spec->out_mix_path.depth || spec->hp_mix_path.depth ||
2226 spec->speaker_path.depth))
Takashi Iwai3214b962011-07-18 12:49:25 +02002227 return 0; /* no loopback switching available */
2228 if (!via_clone_control(spec, &via_aamix_ctl_enum))
2229 return -ENOMEM;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002230 return 0;
2231}
2232
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002233/* look for ADCs */
2234static int via_fill_adcs(struct hda_codec *codec)
2235{
2236 struct via_spec *spec = codec->spec;
2237 hda_nid_t nid = codec->start_nid;
2238 int i;
2239
2240 for (i = 0; i < codec->num_nodes; i++, nid++) {
2241 unsigned int wcaps = get_wcaps(codec, nid);
2242 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2243 continue;
2244 if (wcaps & AC_WCAP_DIGITAL)
2245 continue;
2246 if (!(wcaps & AC_WCAP_CONN_LIST))
2247 continue;
2248 if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2249 return -ENOMEM;
2250 spec->adc_nids[spec->num_adc_nids++] = nid;
2251 }
2252 return 0;
2253}
2254
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002255/* input-src control */
2256static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
2257 struct snd_ctl_elem_info *uinfo)
2258{
2259 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2260 struct via_spec *spec = codec->spec;
2261
2262 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2263 uinfo->count = 1;
2264 uinfo->value.enumerated.items = spec->num_inputs;
2265 if (uinfo->value.enumerated.item >= spec->num_inputs)
2266 uinfo->value.enumerated.item = spec->num_inputs - 1;
2267 strcpy(uinfo->value.enumerated.name,
2268 spec->inputs[uinfo->value.enumerated.item].label);
2269 return 0;
2270}
2271
2272static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
2273 struct snd_ctl_elem_value *ucontrol)
2274{
2275 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2276 struct via_spec *spec = codec->spec;
2277 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2278
2279 ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
2280 return 0;
2281}
2282
2283static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
2284 struct snd_ctl_elem_value *ucontrol)
2285{
2286 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2287 struct via_spec *spec = codec->spec;
2288 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2289 hda_nid_t mux;
2290 int cur;
2291
2292 cur = ucontrol->value.enumerated.item[0];
2293 if (cur < 0 || cur >= spec->num_inputs)
2294 return -EINVAL;
2295 if (spec->cur_mux[idx] == cur)
2296 return 0;
2297 spec->cur_mux[idx] = cur;
2298 if (spec->dyn_adc_switch) {
2299 int adc_idx = spec->inputs[cur].adc_idx;
2300 mux = spec->mux_nids[adc_idx];
2301 via_dyn_adc_pcm_resetup(codec, cur);
2302 } else {
2303 mux = spec->mux_nids[idx];
2304 if (snd_BUG_ON(!mux))
2305 return -EINVAL;
2306 }
2307
2308 if (mux) {
2309 /* switch to D0 beofre change index */
Takashi Iwai054d8672012-01-24 12:25:50 +01002310 update_power_state(codec, mux, AC_PWRST_D0);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002311 snd_hda_codec_write(codec, mux, 0,
2312 AC_VERB_SET_CONNECT_SEL,
2313 spec->inputs[cur].mux_idx);
2314 }
2315
2316 /* update jack power state */
2317 set_widgets_power_state(codec);
2318 return 0;
2319}
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002320
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002321static const struct snd_kcontrol_new via_input_src_ctl = {
2322 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2323 /* The multiple "Capture Source" controls confuse alsamixer
2324 * So call somewhat different..
2325 */
2326 /* .name = "Capture Source", */
2327 .name = "Input Source",
2328 .info = via_mux_enum_info,
2329 .get = via_mux_enum_get,
2330 .put = via_mux_enum_put,
2331};
2332
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002333static int create_input_src_ctls(struct hda_codec *codec, int count)
2334{
2335 struct via_spec *spec = codec->spec;
2336 struct snd_kcontrol_new *knew;
2337
2338 if (spec->num_inputs <= 1 || !count)
2339 return 0; /* no need for single src */
2340
2341 knew = via_clone_control(spec, &via_input_src_ctl);
2342 if (!knew)
2343 return -ENOMEM;
2344 knew->count = count;
2345 return 0;
2346}
2347
2348/* add the powersave loopback-list entry */
Takashi Iwai13af8e72011-06-20 14:05:46 +02002349static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
2350{
2351 struct hda_amp_list *list;
2352
2353 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2354 return;
2355 list = spec->loopback_list + spec->num_loopbacks;
2356 list->nid = mix;
2357 list->dir = HDA_INPUT;
2358 list->idx = idx;
2359 spec->num_loopbacks++;
2360 spec->loopback.amplist = spec->loopback_list;
2361}
Takashi Iwai13af8e72011-06-20 14:05:46 +02002362
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002363static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
Takashi Iwai8d087c72011-06-28 12:45:47 +02002364 hda_nid_t dst)
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002365{
Takashi Iwai8d087c72011-06-28 12:45:47 +02002366 return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002367}
2368
2369/* add the input-route to the given pin */
2370static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002371{
Takashi Iwai10a20af2010-09-09 16:28:02 +02002372 struct via_spec *spec = codec->spec;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002373 int c, idx;
2374
2375 spec->inputs[spec->num_inputs].adc_idx = -1;
2376 spec->inputs[spec->num_inputs].pin = pin;
2377 for (c = 0; c < spec->num_adc_nids; c++) {
2378 if (spec->mux_nids[c]) {
2379 idx = get_connection_index(codec, spec->mux_nids[c],
2380 pin);
2381 if (idx < 0)
2382 continue;
2383 spec->inputs[spec->num_inputs].mux_idx = idx;
2384 } else {
Takashi Iwai8d087c72011-06-28 12:45:47 +02002385 if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002386 continue;
2387 }
2388 spec->inputs[spec->num_inputs].adc_idx = c;
2389 /* Can primary ADC satisfy all inputs? */
2390 if (!spec->dyn_adc_switch &&
2391 spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
2392 snd_printd(KERN_INFO
2393 "via: dynamic ADC switching enabled\n");
2394 spec->dyn_adc_switch = 1;
2395 }
2396 return true;
2397 }
2398 return false;
2399}
2400
2401static int get_mux_nids(struct hda_codec *codec);
2402
2403/* parse input-routes; fill ADCs, MUXs and input-src entries */
2404static int parse_analog_inputs(struct hda_codec *codec)
2405{
2406 struct via_spec *spec = codec->spec;
2407 const struct auto_pin_cfg *cfg = &spec->autocfg;
2408 int i, err;
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002409
2410 err = via_fill_adcs(codec);
2411 if (err < 0)
2412 return err;
2413 err = get_mux_nids(codec);
2414 if (err < 0)
2415 return err;
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002416
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002417 /* fill all input-routes */
2418 for (i = 0; i < cfg->num_inputs; i++) {
2419 if (add_input_route(codec, cfg->inputs[i].pin))
2420 spec->inputs[spec->num_inputs++].label =
2421 hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwaif3268512010-08-30 11:00:19 +02002422 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002423
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002424 /* check for internal loopback recording */
2425 if (spec->aa_mix_nid &&
2426 add_input_route(codec, spec->aa_mix_nid))
2427 spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
2428
2429 return 0;
2430}
2431
2432/* create analog-loopback volume/switch controls */
2433static int create_loopback_ctls(struct hda_codec *codec)
2434{
2435 struct via_spec *spec = codec->spec;
2436 const struct auto_pin_cfg *cfg = &spec->autocfg;
2437 const char *prev_label = NULL;
2438 int type_idx = 0;
2439 int i, j, err, idx;
2440
2441 if (!spec->aa_mix_nid)
2442 return 0;
2443
Takashi Iwai7b315bb2010-08-30 13:06:30 +02002444 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002445 hda_nid_t pin = cfg->inputs[i].pin;
2446 const char *label = hda_get_autocfg_input_label(codec, cfg, i);
2447
Takashi Iwai1e11cae2011-06-21 12:57:22 +02002448 if (prev_label && !strcmp(label, prev_label))
Takashi Iwai7b315bb2010-08-30 13:06:30 +02002449 type_idx++;
2450 else
2451 type_idx = 0;
Takashi Iwai1e11cae2011-06-21 12:57:22 +02002452 prev_label = label;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002453 idx = get_connection_index(codec, spec->aa_mix_nid, pin);
2454 if (idx >= 0) {
Lydia Wang16922282011-03-22 16:24:10 +08002455 err = via_new_analog_input(spec, label, type_idx,
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002456 idx, spec->aa_mix_nid);
Takashi Iwai13af8e72011-06-20 14:05:46 +02002457 if (err < 0)
2458 return err;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002459 add_loopback_list(spec, spec->aa_mix_nid, idx);
Takashi Iwai13af8e72011-06-20 14:05:46 +02002460 }
Takashi Iwaie3d7a142011-06-20 13:52:33 +02002461
2462 /* remember the label for smart51 control */
2463 for (j = 0; j < spec->smart51_nums; j++) {
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002464 if (spec->smart51_pins[j] == pin) {
Takashi Iwaie3d7a142011-06-20 13:52:33 +02002465 spec->smart51_idxs[j] = idx;
2466 spec->smart51_labels[j] = label;
2467 break;
2468 }
2469 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002470 }
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002471 return 0;
2472}
2473
2474/* create mic-boost controls (if present) */
2475static int create_mic_boost_ctls(struct hda_codec *codec)
2476{
2477 struct via_spec *spec = codec->spec;
2478 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai8d8bbc62012-02-22 12:26:38 +01002479 const char *prev_label = NULL;
2480 int type_idx = 0;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002481 int i, err;
2482
2483 for (i = 0; i < cfg->num_inputs; i++) {
2484 hda_nid_t pin = cfg->inputs[i].pin;
2485 unsigned int caps;
2486 const char *label;
2487 char name[32];
2488
2489 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2490 continue;
2491 caps = query_amp_caps(codec, pin, HDA_INPUT);
2492 if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2493 continue;
2494 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai8d8bbc62012-02-22 12:26:38 +01002495 if (prev_label && !strcmp(label, prev_label))
2496 type_idx++;
2497 else
2498 type_idx = 0;
2499 prev_label = label;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002500 snprintf(name, sizeof(name), "%s Boost Volume", label);
Takashi Iwai8d8bbc62012-02-22 12:26:38 +01002501 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002502 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2503 if (err < 0)
2504 return err;
2505 }
2506 return 0;
2507}
2508
2509/* create capture and input-src controls for multiple streams */
2510static int create_multi_adc_ctls(struct hda_codec *codec)
2511{
2512 struct via_spec *spec = codec->spec;
2513 int i, err;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002514
2515 /* create capture mixer elements */
2516 for (i = 0; i < spec->num_adc_nids; i++) {
2517 hda_nid_t adc = spec->adc_nids[i];
2518 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2519 "Capture Volume", i,
2520 HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2521 HDA_INPUT));
2522 if (err < 0)
2523 return err;
2524 err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2525 "Capture Switch", i,
2526 HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2527 HDA_INPUT));
2528 if (err < 0)
2529 return err;
2530 }
2531
2532 /* input-source control */
2533 for (i = 0; i < spec->num_adc_nids; i++)
2534 if (!spec->mux_nids[i])
2535 break;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002536 err = create_input_src_ctls(codec, i);
2537 if (err < 0)
2538 return err;
2539 return 0;
2540}
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002541
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002542/* bind capture volume/switch */
2543static struct snd_kcontrol_new via_bind_cap_vol_ctl =
2544 HDA_BIND_VOL("Capture Volume", 0);
2545static struct snd_kcontrol_new via_bind_cap_sw_ctl =
2546 HDA_BIND_SW("Capture Switch", 0);
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002547
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002548static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
2549 struct hda_ctl_ops *ops)
2550{
2551 struct hda_bind_ctls *ctl;
2552 int i;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002553
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002554 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
2555 if (!ctl)
2556 return -ENOMEM;
2557 ctl->ops = ops;
2558 for (i = 0; i < spec->num_adc_nids; i++)
2559 ctl->values[i] =
2560 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
2561 *ctl_ret = ctl;
2562 return 0;
2563}
2564
2565/* create capture and input-src controls for dynamic ADC-switch case */
2566static int create_dyn_adc_ctls(struct hda_codec *codec)
2567{
2568 struct via_spec *spec = codec->spec;
2569 struct snd_kcontrol_new *knew;
2570 int err;
2571
2572 /* set up the bind capture ctls */
2573 err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
2574 if (err < 0)
2575 return err;
2576 err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
2577 if (err < 0)
2578 return err;
2579
2580 /* create capture mixer elements */
2581 knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
2582 if (!knew)
2583 return -ENOMEM;
2584 knew->private_value = (long)spec->bind_cap_vol;
2585
2586 knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
2587 if (!knew)
2588 return -ENOMEM;
2589 knew->private_value = (long)spec->bind_cap_sw;
2590
2591 /* input-source control */
2592 err = create_input_src_ctls(codec, 1);
2593 if (err < 0)
2594 return err;
2595 return 0;
2596}
2597
2598/* parse and create capture-related stuff */
2599static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
2600{
2601 struct via_spec *spec = codec->spec;
2602 int err;
2603
2604 err = parse_analog_inputs(codec);
2605 if (err < 0)
2606 return err;
2607 if (spec->dyn_adc_switch)
2608 err = create_dyn_adc_ctls(codec);
2609 else
2610 err = create_multi_adc_ctls(codec);
2611 if (err < 0)
2612 return err;
2613 err = create_loopback_ctls(codec);
2614 if (err < 0)
2615 return err;
2616 err = create_mic_boost_ctls(codec);
2617 if (err < 0)
2618 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002619 return 0;
2620}
2621
Harald Welte76d9b0d2008-09-09 15:50:37 +08002622static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
2623{
2624 unsigned int def_conf;
2625 unsigned char seqassoc;
2626
Takashi Iwai2f334f92009-02-20 14:37:42 +01002627 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Harald Welte76d9b0d2008-09-09 15:50:37 +08002628 seqassoc = (unsigned char) get_defcfg_association(def_conf);
2629 seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
Lydia Wang82ef9e42009-10-10 19:08:19 +08002630 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
2631 && (seqassoc == 0xf0 || seqassoc == 0xff)) {
2632 def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
2633 snd_hda_codec_set_pincfg(codec, nid, def_conf);
Harald Welte76d9b0d2008-09-09 15:50:37 +08002634 }
2635
2636 return;
2637}
2638
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002639static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002640 struct snd_ctl_elem_value *ucontrol)
2641{
2642 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2643 struct via_spec *spec = codec->spec;
2644
2645 if (spec->codec_type != VT1708)
2646 return 0;
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002647 ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002648 return 0;
2649}
2650
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002651static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002652 struct snd_ctl_elem_value *ucontrol)
2653{
2654 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2655 struct via_spec *spec = codec->spec;
Takashi Iwai187d3332011-11-24 16:33:09 +01002656 int val;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002657
2658 if (spec->codec_type != VT1708)
2659 return 0;
Takashi Iwai187d3332011-11-24 16:33:09 +01002660 val = !!ucontrol->value.integer.value[0];
2661 if (spec->vt1708_jack_detect == val)
2662 return 0;
2663 spec->vt1708_jack_detect = val;
2664 if (spec->vt1708_jack_detect &&
2665 snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") != 1) {
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002666 mute_aa_path(codec, 1);
2667 notify_aa_path_ctls(codec);
2668 }
Takashi Iwai187d3332011-11-24 16:33:09 +01002669 via_hp_automute(codec);
2670 vt1708_update_hp_work(spec);
2671 return 1;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002672}
2673
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002674static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
2675 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2676 .name = "Jack Detect",
2677 .count = 1,
2678 .info = snd_ctl_boolean_mono_info,
2679 .get = vt1708_jack_detect_get,
2680 .put = vt1708_jack_detect_put,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002681};
2682
Takashi Iwai12daef62011-06-18 17:45:49 +02002683static void fill_dig_outs(struct hda_codec *codec);
2684static void fill_dig_in(struct hda_codec *codec);
2685
2686static int via_parse_auto_config(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002687{
2688 struct via_spec *spec = codec->spec;
2689 int err;
2690
2691 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2692 if (err < 0)
2693 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002694 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
Takashi Iwai7f0df882011-06-18 17:33:34 +02002695 return -EINVAL;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002696
Takashi Iwai4a796162011-06-17 17:53:38 +02002697 err = via_auto_create_multi_out_ctls(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002698 if (err < 0)
2699 return err;
Takashi Iwai4a796162011-06-17 17:53:38 +02002700 err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002701 if (err < 0)
2702 return err;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002703 err = via_auto_create_speaker_ctls(codec);
2704 if (err < 0)
2705 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02002706 err = via_auto_create_loopback_switch(codec);
2707 if (err < 0)
2708 return err;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002709 err = via_auto_create_analog_input_ctls(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002710 if (err < 0)
2711 return err;
2712
2713 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2714
Takashi Iwai12daef62011-06-18 17:45:49 +02002715 fill_dig_outs(codec);
2716 fill_dig_in(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002717
Takashi Iwai603c4012008-07-30 15:01:44 +02002718 if (spec->kctls.list)
2719 spec->mixers[spec->num_mixers++] = spec->kctls.list;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002720
Joseph Chanc577b8a2006-11-29 15:29:40 +01002721
Takashi Iwai3214b962011-07-18 12:49:25 +02002722 if (spec->hp_dac_nid && spec->hp_mix_path.depth) {
Takashi Iwaiece8d042011-06-19 16:24:21 +02002723 err = via_hp_build(codec);
2724 if (err < 0)
2725 return err;
2726 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002727
Takashi Iwaif4a78282011-06-17 18:46:48 +02002728 err = via_smart51_build(codec);
2729 if (err < 0)
2730 return err;
2731
Takashi Iwai5d417622011-06-20 11:32:27 +02002732 /* assign slave outs */
2733 if (spec->slave_dig_outs[0])
2734 codec->slave_dig_outs = spec->slave_dig_outs;
2735
Joseph Chanc577b8a2006-11-29 15:29:40 +01002736 return 1;
2737}
2738
Takashi Iwai5d417622011-06-20 11:32:27 +02002739static void via_auto_init_dig_outs(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002740{
Lydia Wang25eaba22009-10-10 19:08:43 +08002741 struct via_spec *spec = codec->spec;
Takashi Iwai5d417622011-06-20 11:32:27 +02002742 if (spec->multiout.dig_out_nid)
2743 init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
2744 if (spec->slave_dig_outs[0])
2745 init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
2746}
Lydia Wang25eaba22009-10-10 19:08:43 +08002747
Takashi Iwai5d417622011-06-20 11:32:27 +02002748static void via_auto_init_dig_in(struct hda_codec *codec)
2749{
2750 struct via_spec *spec = codec->spec;
2751 if (!spec->dig_in_nid)
2752 return;
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02002753 snd_hda_set_pin_ctl(codec, spec->autocfg.dig_in_pin, PIN_IN);
Takashi Iwai5d417622011-06-20 11:32:27 +02002754}
2755
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002756/* initialize the unsolicited events */
2757static void via_auto_init_unsol_event(struct hda_codec *codec)
2758{
2759 struct via_spec *spec = codec->spec;
2760 struct auto_pin_cfg *cfg = &spec->autocfg;
2761 unsigned int ev;
2762 int i;
2763
2764 if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002765 snd_hda_jack_detect_enable(codec, cfg->hp_pins[0],
2766 VIA_HP_EVENT | VIA_JACK_EVENT);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002767
2768 if (cfg->speaker_pins[0])
2769 ev = VIA_LINE_EVENT;
2770 else
2771 ev = 0;
2772 for (i = 0; i < cfg->line_outs; i++) {
2773 if (cfg->line_out_pins[i] &&
2774 is_jack_detectable(codec, cfg->line_out_pins[i]))
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002775 snd_hda_jack_detect_enable(codec, cfg->line_out_pins[i],
2776 ev | VIA_JACK_EVENT);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002777 }
2778
2779 for (i = 0; i < cfg->num_inputs; i++) {
2780 if (is_jack_detectable(codec, cfg->inputs[i].pin))
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002781 snd_hda_jack_detect_enable(codec, cfg->inputs[i].pin,
2782 VIA_JACK_EVENT);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002783 }
2784}
2785
Takashi Iwai5d417622011-06-20 11:32:27 +02002786static int via_init(struct hda_codec *codec)
2787{
2788 struct via_spec *spec = codec->spec;
2789 int i;
2790
2791 for (i = 0; i < spec->num_iverbs; i++)
2792 snd_hda_sequence_write(codec, spec->init_verbs[i]);
2793
Takashi Iwaie9d010c2012-02-01 10:33:23 +01002794 /* init power states */
2795 set_widgets_power_state(codec);
2796 __analog_low_current_mode(codec, true);
2797
Joseph Chanc577b8a2006-11-29 15:29:40 +01002798 via_auto_init_multi_out(codec);
2799 via_auto_init_hp_out(codec);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002800 via_auto_init_speaker_out(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002801 via_auto_init_analog_input(codec);
Takashi Iwai5d417622011-06-20 11:32:27 +02002802 via_auto_init_dig_outs(codec);
2803 via_auto_init_dig_in(codec);
Lydia Wang118909562011-03-23 17:57:34 +08002804
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002805 via_auto_init_unsol_event(codec);
2806
2807 via_hp_automute(codec);
Takashi Iwai187d3332011-11-24 16:33:09 +01002808 vt1708_update_hp_work(spec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02002809 snd_hda_jack_report_sync(codec);
Lydia Wang25eaba22009-10-10 19:08:43 +08002810
Joseph Chanc577b8a2006-11-29 15:29:40 +01002811 return 0;
2812}
2813
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002814static void vt1708_update_hp_jack_state(struct work_struct *work)
2815{
2816 struct via_spec *spec = container_of(work, struct via_spec,
2817 vt1708_hp_work.work);
2818 if (spec->codec_type != VT1708)
2819 return;
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002820 snd_hda_jack_set_dirty_all(spec->codec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002821 /* if jack state toggled */
2822 if (spec->vt1708_hp_present
Takashi Iwaid56757a2009-11-18 08:00:14 +01002823 != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002824 spec->vt1708_hp_present ^= 1;
2825 via_hp_automute(spec->codec);
2826 }
Takashi Iwai187d3332011-11-24 16:33:09 +01002827 if (spec->vt1708_jack_detect)
2828 schedule_delayed_work(&spec->vt1708_hp_work,
2829 msecs_to_jiffies(100));
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002830}
2831
Takashi Iwai337b9d02009-07-07 18:18:59 +02002832static int get_mux_nids(struct hda_codec *codec)
2833{
2834 struct via_spec *spec = codec->spec;
2835 hda_nid_t nid, conn[8];
2836 unsigned int type;
2837 int i, n;
2838
2839 for (i = 0; i < spec->num_adc_nids; i++) {
2840 nid = spec->adc_nids[i];
2841 while (nid) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002842 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai1c55d522009-07-08 07:45:46 +02002843 if (type == AC_WID_PIN)
2844 break;
Takashi Iwai337b9d02009-07-07 18:18:59 +02002845 n = snd_hda_get_connections(codec, nid, conn,
2846 ARRAY_SIZE(conn));
2847 if (n <= 0)
2848 break;
2849 if (n > 1) {
2850 spec->mux_nids[i] = nid;
2851 break;
2852 }
2853 nid = conn[0];
2854 }
2855 }
Takashi Iwai1c55d522009-07-08 07:45:46 +02002856 return 0;
Takashi Iwai337b9d02009-07-07 18:18:59 +02002857}
2858
Joseph Chanc577b8a2006-11-29 15:29:40 +01002859static int patch_vt1708(struct hda_codec *codec)
2860{
2861 struct via_spec *spec;
2862 int err;
2863
2864 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002865 spec = via_new_spec(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002866 if (spec == NULL)
2867 return -ENOMEM;
2868
Takashi Iwai620e2b22011-06-17 17:19:19 +02002869 spec->aa_mix_nid = 0x17;
2870
Takashi Iwai12daef62011-06-18 17:45:49 +02002871 /* Add HP and CD pin config connect bit re-config action */
2872 vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
2873 vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
2874
Joseph Chanc577b8a2006-11-29 15:29:40 +01002875 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02002876 err = via_parse_auto_config(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002877 if (err < 0) {
2878 via_free(codec);
2879 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002880 }
2881
Takashi Iwai12daef62011-06-18 17:45:49 +02002882 /* add jack detect on/off control */
2883 if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
2884 return -ENOMEM;
2885
Takashi Iwaibc9b562382008-05-23 17:50:27 +02002886 /* disable 32bit format on VT1708 */
2887 if (codec->vendor_id == 0x11061708)
2888 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002889
Lydia Wange322a362011-06-29 13:52:02 +08002890 spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2891
Joseph Chanc577b8a2006-11-29 15:29:40 +01002892 codec->patch_ops = via_patch_ops;
2893
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002894 INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002895 return 0;
2896}
2897
Takashi Iwaiddd304d2011-06-21 16:33:55 +02002898static int patch_vt1709(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002899{
2900 struct via_spec *spec;
2901 int err;
2902
2903 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002904 spec = via_new_spec(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002905 if (spec == NULL)
2906 return -ENOMEM;
2907
Takashi Iwai620e2b22011-06-17 17:19:19 +02002908 spec->aa_mix_nid = 0x18;
2909
Takashi Iwai12daef62011-06-18 17:45:49 +02002910 err = via_parse_auto_config(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002911 if (err < 0) {
2912 via_free(codec);
2913 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002914 }
2915
Joseph Chanc577b8a2006-11-29 15:29:40 +01002916 codec->patch_ops = via_patch_ops;
2917
Josepch Chanf7278fd2007-12-13 16:40:40 +01002918 return 0;
2919}
2920
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002921static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
2922{
2923 struct via_spec *spec = codec->spec;
2924 int imux_is_smixer;
2925 unsigned int parm;
2926 int is_8ch = 0;
Lydia Wangbc92df72011-03-23 17:56:05 +08002927 if ((spec->codec_type != VT1708B_4CH) &&
2928 (codec->vendor_id != 0x11064397))
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002929 is_8ch = 1;
2930
2931 /* SW0 (17h) = stereo mixer */
2932 imux_is_smixer =
2933 (snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
2934 == ((spec->codec_type == VT1708S) ? 5 : 0));
2935 /* inputs */
2936 /* PW 1/2/5 (1ah/1bh/1eh) */
2937 parm = AC_PWRST_D3;
2938 set_pin_power_state(codec, 0x1a, &parm);
2939 set_pin_power_state(codec, 0x1b, &parm);
2940 set_pin_power_state(codec, 0x1e, &parm);
2941 if (imux_is_smixer)
2942 parm = AC_PWRST_D0;
2943 /* SW0 (17h), AIW 0/1 (13h/14h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01002944 update_power_state(codec, 0x17, parm);
2945 update_power_state(codec, 0x13, parm);
2946 update_power_state(codec, 0x14, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002947
2948 /* outputs */
2949 /* PW0 (19h), SW1 (18h), AOW1 (11h) */
2950 parm = AC_PWRST_D3;
2951 set_pin_power_state(codec, 0x19, &parm);
2952 if (spec->smart51_enabled)
2953 set_pin_power_state(codec, 0x1b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002954 update_power_state(codec, 0x18, parm);
2955 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002956
2957 /* PW6 (22h), SW2 (26h), AOW2 (24h) */
2958 if (is_8ch) {
2959 parm = AC_PWRST_D3;
2960 set_pin_power_state(codec, 0x22, &parm);
2961 if (spec->smart51_enabled)
2962 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002963 update_power_state(codec, 0x26, parm);
2964 update_power_state(codec, 0x24, parm);
Lydia Wangbc92df72011-03-23 17:56:05 +08002965 } else if (codec->vendor_id == 0x11064397) {
2966 /* PW7(23h), SW2(27h), AOW2(25h) */
2967 parm = AC_PWRST_D3;
2968 set_pin_power_state(codec, 0x23, &parm);
2969 if (spec->smart51_enabled)
2970 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002971 update_power_state(codec, 0x27, parm);
2972 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002973 }
2974
2975 /* PW 3/4/7 (1ch/1dh/23h) */
2976 parm = AC_PWRST_D3;
2977 /* force to D0 for internal Speaker */
2978 set_pin_power_state(codec, 0x1c, &parm);
2979 set_pin_power_state(codec, 0x1d, &parm);
2980 if (is_8ch)
2981 set_pin_power_state(codec, 0x23, &parm);
2982
2983 /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01002984 update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
2985 update_power_state(codec, 0x10, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002986 if (is_8ch) {
Takashi Iwai054d8672012-01-24 12:25:50 +01002987 update_power_state(codec, 0x25, parm);
2988 update_power_state(codec, 0x27, parm);
Lydia Wangbc92df72011-03-23 17:56:05 +08002989 } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01002990 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002991}
2992
Lydia Wang518bf3b2009-10-10 19:07:29 +08002993static int patch_vt1708S(struct hda_codec *codec);
Takashi Iwaiddd304d2011-06-21 16:33:55 +02002994static int patch_vt1708B(struct hda_codec *codec)
Josepch Chanf7278fd2007-12-13 16:40:40 +01002995{
2996 struct via_spec *spec;
2997 int err;
2998
Lydia Wang518bf3b2009-10-10 19:07:29 +08002999 if (get_codec_type(codec) == VT1708BCE)
3000 return patch_vt1708S(codec);
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003001
Josepch Chanf7278fd2007-12-13 16:40:40 +01003002 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003003 spec = via_new_spec(codec);
Josepch Chanf7278fd2007-12-13 16:40:40 +01003004 if (spec == NULL)
3005 return -ENOMEM;
3006
Takashi Iwai620e2b22011-06-17 17:19:19 +02003007 spec->aa_mix_nid = 0x16;
3008
Josepch Chanf7278fd2007-12-13 16:40:40 +01003009 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003010 err = via_parse_auto_config(codec);
Josepch Chanf7278fd2007-12-13 16:40:40 +01003011 if (err < 0) {
3012 via_free(codec);
3013 return err;
Josepch Chanf7278fd2007-12-13 16:40:40 +01003014 }
3015
Josepch Chanf7278fd2007-12-13 16:40:40 +01003016 codec->patch_ops = via_patch_ops;
3017
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003018 spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
3019
Josepch Chanf7278fd2007-12-13 16:40:40 +01003020 return 0;
3021}
3022
Harald Welted949cac2008-09-09 15:56:01 +08003023/* Patch for VT1708S */
Takashi Iwai096a8852011-06-20 12:09:02 +02003024static const struct hda_verb vt1708S_init_verbs[] = {
Harald Welted7426322008-09-15 22:43:23 +08003025 /* Enable Mic Boost Volume backdoor */
3026 {0x1, 0xf98, 0x1},
Lydia Wangbc7e7e52009-10-10 19:08:32 +08003027 /* don't bybass mixer */
3028 {0x1, 0xf88, 0xc0},
Harald Welted949cac2008-09-09 15:56:01 +08003029 { }
3030};
3031
Takashi Iwai9da29272009-05-07 16:31:14 +02003032/* fill out digital output widgets; one for master and one for slave outputs */
3033static void fill_dig_outs(struct hda_codec *codec)
3034{
3035 struct via_spec *spec = codec->spec;
3036 int i;
3037
3038 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3039 hda_nid_t nid;
3040 int conn;
3041
3042 nid = spec->autocfg.dig_out_pins[i];
3043 if (!nid)
3044 continue;
3045 conn = snd_hda_get_connections(codec, nid, &nid, 1);
3046 if (conn < 1)
3047 continue;
3048 if (!spec->multiout.dig_out_nid)
3049 spec->multiout.dig_out_nid = nid;
3050 else {
3051 spec->slave_dig_outs[0] = nid;
3052 break; /* at most two dig outs */
3053 }
3054 }
3055}
3056
Takashi Iwai12daef62011-06-18 17:45:49 +02003057static void fill_dig_in(struct hda_codec *codec)
Harald Welted949cac2008-09-09 15:56:01 +08003058{
3059 struct via_spec *spec = codec->spec;
Takashi Iwai12daef62011-06-18 17:45:49 +02003060 hda_nid_t dig_nid;
3061 int i, err;
Harald Welted949cac2008-09-09 15:56:01 +08003062
Takashi Iwai12daef62011-06-18 17:45:49 +02003063 if (!spec->autocfg.dig_in_pin)
3064 return;
Harald Welted949cac2008-09-09 15:56:01 +08003065
Takashi Iwai12daef62011-06-18 17:45:49 +02003066 dig_nid = codec->start_nid;
3067 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3068 unsigned int wcaps = get_wcaps(codec, dig_nid);
3069 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3070 continue;
3071 if (!(wcaps & AC_WCAP_DIGITAL))
3072 continue;
3073 if (!(wcaps & AC_WCAP_CONN_LIST))
3074 continue;
3075 err = get_connection_index(codec, dig_nid,
3076 spec->autocfg.dig_in_pin);
3077 if (err >= 0) {
3078 spec->dig_in_nid = dig_nid;
3079 break;
3080 }
3081 }
Harald Welted949cac2008-09-09 15:56:01 +08003082}
3083
Lydia Wang6369bcf2009-10-10 19:08:31 +08003084static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
3085 int offset, int num_steps, int step_size)
3086{
3087 snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
3088 (offset << AC_AMPCAP_OFFSET_SHIFT) |
3089 (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
3090 (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
3091 (0 << AC_AMPCAP_MUTE_SHIFT));
3092}
3093
Harald Welted949cac2008-09-09 15:56:01 +08003094static int patch_vt1708S(struct hda_codec *codec)
3095{
3096 struct via_spec *spec;
3097 int err;
3098
3099 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003100 spec = via_new_spec(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003101 if (spec == NULL)
3102 return -ENOMEM;
3103
Takashi Iwai620e2b22011-06-17 17:19:19 +02003104 spec->aa_mix_nid = 0x16;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003105 override_mic_boost(codec, 0x1a, 0, 3, 40);
3106 override_mic_boost(codec, 0x1e, 0, 3, 40);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003107
Harald Welted949cac2008-09-09 15:56:01 +08003108 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003109 err = via_parse_auto_config(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003110 if (err < 0) {
3111 via_free(codec);
3112 return err;
Harald Welted949cac2008-09-09 15:56:01 +08003113 }
3114
Takashi Iwai096a8852011-06-20 12:09:02 +02003115 spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
Harald Welted949cac2008-09-09 15:56:01 +08003116
Harald Welted949cac2008-09-09 15:56:01 +08003117 codec->patch_ops = via_patch_ops;
3118
Lydia Wang518bf3b2009-10-10 19:07:29 +08003119 /* correct names for VT1708BCE */
3120 if (get_codec_type(codec) == VT1708BCE) {
3121 kfree(codec->chip_name);
3122 codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3123 snprintf(codec->bus->card->mixername,
3124 sizeof(codec->bus->card->mixername),
3125 "%s %s", codec->vendor_name, codec->chip_name);
Lydia Wang970f630f2011-03-22 16:25:56 +08003126 }
Lydia Wangbc92df72011-03-23 17:56:05 +08003127 /* correct names for VT1705 */
3128 if (codec->vendor_id == 0x11064397) {
3129 kfree(codec->chip_name);
3130 codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3131 snprintf(codec->bus->card->mixername,
3132 sizeof(codec->bus->card->mixername),
3133 "%s %s", codec->vendor_name, codec->chip_name);
3134 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003135 spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
Harald Welted949cac2008-09-09 15:56:01 +08003136 return 0;
3137}
3138
3139/* Patch for VT1702 */
3140
Takashi Iwai096a8852011-06-20 12:09:02 +02003141static const struct hda_verb vt1702_init_verbs[] = {
Lydia Wangbc7e7e52009-10-10 19:08:32 +08003142 /* mixer enable */
3143 {0x1, 0xF88, 0x3},
3144 /* GPIO 0~2 */
3145 {0x1, 0xF82, 0x3F},
Harald Welted949cac2008-09-09 15:56:01 +08003146 { }
3147};
3148
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003149static void set_widgets_power_state_vt1702(struct hda_codec *codec)
3150{
3151 int imux_is_smixer =
3152 snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3153 unsigned int parm;
3154 /* inputs */
3155 /* PW 1/2/5 (14h/15h/18h) */
3156 parm = AC_PWRST_D3;
3157 set_pin_power_state(codec, 0x14, &parm);
3158 set_pin_power_state(codec, 0x15, &parm);
3159 set_pin_power_state(codec, 0x18, &parm);
3160 if (imux_is_smixer)
3161 parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
3162 /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003163 update_power_state(codec, 0x13, parm);
3164 update_power_state(codec, 0x12, parm);
3165 update_power_state(codec, 0x1f, parm);
3166 update_power_state(codec, 0x20, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003167
3168 /* outputs */
3169 /* PW 3/4 (16h/17h) */
3170 parm = AC_PWRST_D3;
3171 set_pin_power_state(codec, 0x17, &parm);
3172 set_pin_power_state(codec, 0x16, &parm);
3173 /* MW0 (1ah), AOW 0/1 (10h/1dh) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003174 update_power_state(codec, 0x1a, imux_is_smixer ? AC_PWRST_D0 : parm);
3175 update_power_state(codec, 0x10, parm);
3176 update_power_state(codec, 0x1d, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003177}
3178
Harald Welted949cac2008-09-09 15:56:01 +08003179static int patch_vt1702(struct hda_codec *codec)
3180{
3181 struct via_spec *spec;
3182 int err;
Harald Welted949cac2008-09-09 15:56:01 +08003183
3184 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003185 spec = via_new_spec(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003186 if (spec == NULL)
3187 return -ENOMEM;
3188
Takashi Iwai620e2b22011-06-17 17:19:19 +02003189 spec->aa_mix_nid = 0x1a;
3190
Takashi Iwai12daef62011-06-18 17:45:49 +02003191 /* limit AA path volume to 0 dB */
3192 snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
3193 (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
3194 (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
3195 (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
3196 (1 << AC_AMPCAP_MUTE_SHIFT));
3197
Harald Welted949cac2008-09-09 15:56:01 +08003198 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003199 err = via_parse_auto_config(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003200 if (err < 0) {
3201 via_free(codec);
3202 return err;
Harald Welted949cac2008-09-09 15:56:01 +08003203 }
3204
Takashi Iwai096a8852011-06-20 12:09:02 +02003205 spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
Harald Welted949cac2008-09-09 15:56:01 +08003206
Harald Welted949cac2008-09-09 15:56:01 +08003207 codec->patch_ops = via_patch_ops;
3208
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003209 spec->set_widgets_power_state = set_widgets_power_state_vt1702;
Harald Welted949cac2008-09-09 15:56:01 +08003210 return 0;
3211}
3212
Lydia Wangeb7188c2009-10-10 19:08:34 +08003213/* Patch for VT1718S */
3214
Takashi Iwai096a8852011-06-20 12:09:02 +02003215static const struct hda_verb vt1718S_init_verbs[] = {
Lydia Wang4ab2d532011-03-24 12:42:03 +08003216 /* Enable MW0 adjust Gain 5 */
3217 {0x1, 0xfb2, 0x10},
Lydia Wangeb7188c2009-10-10 19:08:34 +08003218 /* Enable Boost Volume backdoor */
3219 {0x1, 0xf88, 0x8},
Takashi Iwai5d417622011-06-20 11:32:27 +02003220
Lydia Wangeb7188c2009-10-10 19:08:34 +08003221 { }
3222};
3223
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003224static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
3225{
3226 struct via_spec *spec = codec->spec;
3227 int imux_is_smixer;
3228 unsigned int parm;
3229 /* MUX6 (1eh) = stereo mixer */
3230 imux_is_smixer =
3231 snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
3232 /* inputs */
3233 /* PW 5/6/7 (29h/2ah/2bh) */
3234 parm = AC_PWRST_D3;
3235 set_pin_power_state(codec, 0x29, &parm);
3236 set_pin_power_state(codec, 0x2a, &parm);
3237 set_pin_power_state(codec, 0x2b, &parm);
3238 if (imux_is_smixer)
3239 parm = AC_PWRST_D0;
3240 /* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003241 update_power_state(codec, 0x1e, parm);
3242 update_power_state(codec, 0x1f, parm);
3243 update_power_state(codec, 0x10, parm);
3244 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003245
3246 /* outputs */
3247 /* PW3 (27h), MW2 (1ah), AOW3 (bh) */
3248 parm = AC_PWRST_D3;
3249 set_pin_power_state(codec, 0x27, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003250 update_power_state(codec, 0x1a, parm);
3251 update_power_state(codec, 0xb, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003252
3253 /* PW2 (26h), AOW2 (ah) */
3254 parm = AC_PWRST_D3;
3255 set_pin_power_state(codec, 0x26, &parm);
3256 if (spec->smart51_enabled)
3257 set_pin_power_state(codec, 0x2b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003258 update_power_state(codec, 0xa, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003259
3260 /* PW0 (24h), AOW0 (8h) */
3261 parm = AC_PWRST_D3;
3262 set_pin_power_state(codec, 0x24, &parm);
3263 if (!spec->hp_independent_mode) /* check for redirected HP */
3264 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003265 update_power_state(codec, 0x8, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003266 /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003267 update_power_state(codec, 0x21, imux_is_smixer ? AC_PWRST_D0 : parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003268
3269 /* PW1 (25h), AOW1 (9h) */
3270 parm = AC_PWRST_D3;
3271 set_pin_power_state(codec, 0x25, &parm);
3272 if (spec->smart51_enabled)
3273 set_pin_power_state(codec, 0x2a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003274 update_power_state(codec, 0x9, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003275
3276 if (spec->hp_independent_mode) {
3277 /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
3278 parm = AC_PWRST_D3;
3279 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003280 update_power_state(codec, 0x1b, parm);
3281 update_power_state(codec, 0x34, parm);
3282 update_power_state(codec, 0xc, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003283 }
3284}
3285
Takashi Iwai30b45032011-07-11 17:05:04 +02003286/* Add a connection to the primary DAC from AA-mixer for some codecs
3287 * This isn't listed from the raw info, but the chip has a secret connection.
3288 */
3289static int add_secret_dac_path(struct hda_codec *codec)
3290{
3291 struct via_spec *spec = codec->spec;
3292 int i, nums;
3293 hda_nid_t conn[8];
3294 hda_nid_t nid;
3295
3296 if (!spec->aa_mix_nid)
3297 return 0;
3298 nums = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
3299 ARRAY_SIZE(conn) - 1);
3300 for (i = 0; i < nums; i++) {
3301 if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
3302 return 0;
3303 }
3304
3305 /* find the primary DAC and add to the connection list */
3306 nid = codec->start_nid;
3307 for (i = 0; i < codec->num_nodes; i++, nid++) {
3308 unsigned int caps = get_wcaps(codec, nid);
3309 if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
3310 !(caps & AC_WCAP_DIGITAL)) {
3311 conn[nums++] = nid;
3312 return snd_hda_override_conn_list(codec,
3313 spec->aa_mix_nid,
3314 nums, conn);
3315 }
3316 }
3317 return 0;
3318}
3319
3320
Lydia Wangeb7188c2009-10-10 19:08:34 +08003321static int patch_vt1718S(struct hda_codec *codec)
3322{
3323 struct via_spec *spec;
3324 int err;
3325
3326 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003327 spec = via_new_spec(codec);
Lydia Wangeb7188c2009-10-10 19:08:34 +08003328 if (spec == NULL)
3329 return -ENOMEM;
3330
Takashi Iwai620e2b22011-06-17 17:19:19 +02003331 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003332 override_mic_boost(codec, 0x2b, 0, 3, 40);
3333 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003334 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003335
Lydia Wangeb7188c2009-10-10 19:08:34 +08003336 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003337 err = via_parse_auto_config(codec);
Lydia Wangeb7188c2009-10-10 19:08:34 +08003338 if (err < 0) {
3339 via_free(codec);
3340 return err;
Lydia Wangeb7188c2009-10-10 19:08:34 +08003341 }
3342
Takashi Iwai096a8852011-06-20 12:09:02 +02003343 spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
Lydia Wangeb7188c2009-10-10 19:08:34 +08003344
Lydia Wangeb7188c2009-10-10 19:08:34 +08003345 codec->patch_ops = via_patch_ops;
3346
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003347 spec->set_widgets_power_state = set_widgets_power_state_vt1718S;
3348
Lydia Wangeb7188c2009-10-10 19:08:34 +08003349 return 0;
3350}
Lydia Wangf3db4232009-10-10 19:08:41 +08003351
3352/* Patch for VT1716S */
3353
3354static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
3355 struct snd_ctl_elem_info *uinfo)
3356{
3357 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3358 uinfo->count = 1;
3359 uinfo->value.integer.min = 0;
3360 uinfo->value.integer.max = 1;
3361 return 0;
3362}
3363
3364static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
3365 struct snd_ctl_elem_value *ucontrol)
3366{
3367 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3368 int index = 0;
3369
3370 index = snd_hda_codec_read(codec, 0x26, 0,
3371 AC_VERB_GET_CONNECT_SEL, 0);
3372 if (index != -1)
3373 *ucontrol->value.integer.value = index;
3374
3375 return 0;
3376}
3377
3378static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
3379 struct snd_ctl_elem_value *ucontrol)
3380{
3381 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3382 struct via_spec *spec = codec->spec;
3383 int index = *ucontrol->value.integer.value;
3384
3385 snd_hda_codec_write(codec, 0x26, 0,
3386 AC_VERB_SET_CONNECT_SEL, index);
3387 spec->dmic_enabled = index;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003388 set_widgets_power_state(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003389 return 1;
3390}
3391
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003392static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003393 HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
3394 {
3395 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3396 .name = "Digital Mic Capture Switch",
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003397 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
Lydia Wangf3db4232009-10-10 19:08:41 +08003398 .count = 1,
3399 .info = vt1716s_dmic_info,
3400 .get = vt1716s_dmic_get,
3401 .put = vt1716s_dmic_put,
3402 },
3403 {} /* end */
3404};
3405
3406
3407/* mono-out mixer elements */
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003408static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003409 HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
3410 { } /* end */
3411};
3412
Takashi Iwai096a8852011-06-20 12:09:02 +02003413static const struct hda_verb vt1716S_init_verbs[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003414 /* Enable Boost Volume backdoor */
3415 {0x1, 0xf8a, 0x80},
3416 /* don't bybass mixer */
3417 {0x1, 0xf88, 0xc0},
3418 /* Enable mono output */
3419 {0x1, 0xf90, 0x08},
3420 { }
3421};
3422
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003423static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
3424{
3425 struct via_spec *spec = codec->spec;
3426 int imux_is_smixer;
3427 unsigned int parm;
3428 unsigned int mono_out, present;
3429 /* SW0 (17h) = stereo mixer */
3430 imux_is_smixer =
3431 (snd_hda_codec_read(codec, 0x17, 0,
3432 AC_VERB_GET_CONNECT_SEL, 0x00) == 5);
3433 /* inputs */
3434 /* PW 1/2/5 (1ah/1bh/1eh) */
3435 parm = AC_PWRST_D3;
3436 set_pin_power_state(codec, 0x1a, &parm);
3437 set_pin_power_state(codec, 0x1b, &parm);
3438 set_pin_power_state(codec, 0x1e, &parm);
3439 if (imux_is_smixer)
3440 parm = AC_PWRST_D0;
3441 /* SW0 (17h), AIW0(13h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003442 update_power_state(codec, 0x17, parm);
3443 update_power_state(codec, 0x13, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003444
3445 parm = AC_PWRST_D3;
3446 set_pin_power_state(codec, 0x1e, &parm);
3447 /* PW11 (22h) */
3448 if (spec->dmic_enabled)
3449 set_pin_power_state(codec, 0x22, &parm);
3450 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003451 update_power_state(codec, 0x22, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003452
3453 /* SW2(26h), AIW1(14h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003454 update_power_state(codec, 0x26, parm);
3455 update_power_state(codec, 0x14, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003456
3457 /* outputs */
3458 /* PW0 (19h), SW1 (18h), AOW1 (11h) */
3459 parm = AC_PWRST_D3;
3460 set_pin_power_state(codec, 0x19, &parm);
3461 /* Smart 5.1 PW2(1bh) */
3462 if (spec->smart51_enabled)
3463 set_pin_power_state(codec, 0x1b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003464 update_power_state(codec, 0x18, parm);
3465 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003466
3467 /* PW7 (23h), SW3 (27h), AOW3 (25h) */
3468 parm = AC_PWRST_D3;
3469 set_pin_power_state(codec, 0x23, &parm);
3470 /* Smart 5.1 PW1(1ah) */
3471 if (spec->smart51_enabled)
3472 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003473 update_power_state(codec, 0x27, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003474
3475 /* Smart 5.1 PW5(1eh) */
3476 if (spec->smart51_enabled)
3477 set_pin_power_state(codec, 0x1e, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003478 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003479
3480 /* Mono out */
3481 /* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
3482 present = snd_hda_jack_detect(codec, 0x1c);
3483
3484 if (present)
3485 mono_out = 0;
3486 else {
3487 present = snd_hda_jack_detect(codec, 0x1d);
3488 if (!spec->hp_independent_mode && present)
3489 mono_out = 0;
3490 else
3491 mono_out = 1;
3492 }
3493 parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
Takashi Iwai054d8672012-01-24 12:25:50 +01003494 update_power_state(codec, 0x28, parm);
3495 update_power_state(codec, 0x29, parm);
3496 update_power_state(codec, 0x2a, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003497
3498 /* PW 3/4 (1ch/1dh) */
3499 parm = AC_PWRST_D3;
3500 set_pin_power_state(codec, 0x1c, &parm);
3501 set_pin_power_state(codec, 0x1d, &parm);
3502 /* HP Independent Mode, power on AOW3 */
3503 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003504 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003505
3506 /* force to D0 for internal Speaker */
3507 /* MW0 (16h), AOW0 (10h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003508 update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
3509 update_power_state(codec, 0x10, mono_out ? AC_PWRST_D0 : parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003510}
3511
Lydia Wangf3db4232009-10-10 19:08:41 +08003512static int patch_vt1716S(struct hda_codec *codec)
3513{
3514 struct via_spec *spec;
3515 int err;
3516
3517 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003518 spec = via_new_spec(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003519 if (spec == NULL)
3520 return -ENOMEM;
3521
Takashi Iwai620e2b22011-06-17 17:19:19 +02003522 spec->aa_mix_nid = 0x16;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003523 override_mic_boost(codec, 0x1a, 0, 3, 40);
3524 override_mic_boost(codec, 0x1e, 0, 3, 40);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003525
Lydia Wangf3db4232009-10-10 19:08:41 +08003526 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003527 err = via_parse_auto_config(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003528 if (err < 0) {
3529 via_free(codec);
3530 return err;
Lydia Wangf3db4232009-10-10 19:08:41 +08003531 }
3532
Takashi Iwai096a8852011-06-20 12:09:02 +02003533 spec->init_verbs[spec->num_iverbs++] = vt1716S_init_verbs;
Lydia Wangf3db4232009-10-10 19:08:41 +08003534
Lydia Wangf3db4232009-10-10 19:08:41 +08003535 spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
3536 spec->num_mixers++;
3537
3538 spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3539
3540 codec->patch_ops = via_patch_ops;
3541
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003542 spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
Lydia Wangf3db4232009-10-10 19:08:41 +08003543 return 0;
3544}
Lydia Wang25eaba22009-10-10 19:08:43 +08003545
3546/* for vt2002P */
3547
Takashi Iwai096a8852011-06-20 12:09:02 +02003548static const struct hda_verb vt2002P_init_verbs[] = {
Lydia Wangeadb9a82011-03-24 12:43:02 +08003549 /* Class-D speaker related verbs */
3550 {0x1, 0xfe0, 0x4},
3551 {0x1, 0xfe9, 0x80},
3552 {0x1, 0xfe2, 0x22},
Lydia Wang25eaba22009-10-10 19:08:43 +08003553 /* Enable Boost Volume backdoor */
3554 {0x1, 0xfb9, 0x24},
Lydia Wang25eaba22009-10-10 19:08:43 +08003555 /* Enable AOW0 to MW9 */
3556 {0x1, 0xfb8, 0x88},
3557 { }
3558};
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003559
Takashi Iwai096a8852011-06-20 12:09:02 +02003560static const struct hda_verb vt1802_init_verbs[] = {
Lydia Wang118909562011-03-23 17:57:34 +08003561 /* Enable Boost Volume backdoor */
3562 {0x1, 0xfb9, 0x24},
Lydia Wang118909562011-03-23 17:57:34 +08003563 /* Enable AOW0 to MW9 */
3564 {0x1, 0xfb8, 0x88},
3565 { }
3566};
Lydia Wang25eaba22009-10-10 19:08:43 +08003567
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003568static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
3569{
3570 struct via_spec *spec = codec->spec;
3571 int imux_is_smixer;
3572 unsigned int parm;
3573 unsigned int present;
3574 /* MUX9 (1eh) = stereo mixer */
3575 imux_is_smixer =
3576 snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3577 /* inputs */
3578 /* PW 5/6/7 (29h/2ah/2bh) */
3579 parm = AC_PWRST_D3;
3580 set_pin_power_state(codec, 0x29, &parm);
3581 set_pin_power_state(codec, 0x2a, &parm);
3582 set_pin_power_state(codec, 0x2b, &parm);
3583 parm = AC_PWRST_D0;
3584 /* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003585 update_power_state(codec, 0x1e, parm);
3586 update_power_state(codec, 0x1f, parm);
3587 update_power_state(codec, 0x10, parm);
3588 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003589
3590 /* outputs */
3591 /* AOW0 (8h)*/
Takashi Iwai054d8672012-01-24 12:25:50 +01003592 update_power_state(codec, 0x8, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003593
Lydia Wang118909562011-03-23 17:57:34 +08003594 if (spec->codec_type == VT1802) {
3595 /* PW4 (28h), MW4 (18h), MUX4(38h) */
3596 parm = AC_PWRST_D3;
3597 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003598 update_power_state(codec, 0x18, parm);
3599 update_power_state(codec, 0x38, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003600 } else {
3601 /* PW4 (26h), MW4 (1ch), MUX4(37h) */
3602 parm = AC_PWRST_D3;
3603 set_pin_power_state(codec, 0x26, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003604 update_power_state(codec, 0x1c, parm);
3605 update_power_state(codec, 0x37, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003606 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003607
Lydia Wang118909562011-03-23 17:57:34 +08003608 if (spec->codec_type == VT1802) {
3609 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3610 parm = AC_PWRST_D3;
3611 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003612 update_power_state(codec, 0x15, parm);
3613 update_power_state(codec, 0x35, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003614 } else {
3615 /* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
3616 parm = AC_PWRST_D3;
3617 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003618 update_power_state(codec, 0x19, parm);
3619 update_power_state(codec, 0x35, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003620 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003621
3622 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003623 update_power_state(codec, 0x9, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003624
3625 /* Class-D */
3626 /* PW0 (24h), MW0(18h/14h), MUX0(34h) */
3627 present = snd_hda_jack_detect(codec, 0x25);
3628
3629 parm = AC_PWRST_D3;
3630 set_pin_power_state(codec, 0x24, &parm);
3631 parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
Lydia Wang118909562011-03-23 17:57:34 +08003632 if (spec->codec_type == VT1802)
Takashi Iwai054d8672012-01-24 12:25:50 +01003633 update_power_state(codec, 0x14, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003634 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003635 update_power_state(codec, 0x18, parm);
3636 update_power_state(codec, 0x34, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003637
3638 /* Mono Out */
3639 present = snd_hda_jack_detect(codec, 0x26);
3640
3641 parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
Lydia Wang118909562011-03-23 17:57:34 +08003642 if (spec->codec_type == VT1802) {
3643 /* PW15 (33h), MW8(1ch), MUX8(3ch) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003644 update_power_state(codec, 0x33, parm);
3645 update_power_state(codec, 0x1c, parm);
3646 update_power_state(codec, 0x3c, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003647 } else {
3648 /* PW15 (31h), MW8(17h), MUX8(3bh) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003649 update_power_state(codec, 0x31, parm);
3650 update_power_state(codec, 0x17, parm);
3651 update_power_state(codec, 0x3b, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003652 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003653 /* MW9 (21h) */
3654 if (imux_is_smixer || !is_aa_path_mute(codec))
Takashi Iwai054d8672012-01-24 12:25:50 +01003655 update_power_state(codec, 0x21, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003656 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003657 update_power_state(codec, 0x21, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003658}
Lydia Wang25eaba22009-10-10 19:08:43 +08003659
3660/* patch for vt2002P */
3661static int patch_vt2002P(struct hda_codec *codec)
3662{
3663 struct via_spec *spec;
3664 int err;
3665
3666 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003667 spec = via_new_spec(codec);
Lydia Wang25eaba22009-10-10 19:08:43 +08003668 if (spec == NULL)
3669 return -ENOMEM;
3670
Takashi Iwai620e2b22011-06-17 17:19:19 +02003671 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003672 override_mic_boost(codec, 0x2b, 0, 3, 40);
3673 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003674 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003675
Lydia Wang25eaba22009-10-10 19:08:43 +08003676 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003677 err = via_parse_auto_config(codec);
Lydia Wang25eaba22009-10-10 19:08:43 +08003678 if (err < 0) {
3679 via_free(codec);
3680 return err;
Lydia Wang25eaba22009-10-10 19:08:43 +08003681 }
3682
Lydia Wang118909562011-03-23 17:57:34 +08003683 if (spec->codec_type == VT1802)
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003684 spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
Lydia Wang118909562011-03-23 17:57:34 +08003685 else
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003686 spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
Lydia Wang118909562011-03-23 17:57:34 +08003687
Lydia Wang25eaba22009-10-10 19:08:43 +08003688 codec->patch_ops = via_patch_ops;
3689
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003690 spec->set_widgets_power_state = set_widgets_power_state_vt2002P;
Lydia Wang25eaba22009-10-10 19:08:43 +08003691 return 0;
3692}
Lydia Wangab6734e2009-10-10 19:08:46 +08003693
3694/* for vt1812 */
3695
Takashi Iwai096a8852011-06-20 12:09:02 +02003696static const struct hda_verb vt1812_init_verbs[] = {
Lydia Wangab6734e2009-10-10 19:08:46 +08003697 /* Enable Boost Volume backdoor */
3698 {0x1, 0xfb9, 0x24},
Lydia Wangab6734e2009-10-10 19:08:46 +08003699 /* Enable AOW0 to MW9 */
3700 {0x1, 0xfb8, 0xa8},
3701 { }
3702};
3703
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003704static void set_widgets_power_state_vt1812(struct hda_codec *codec)
3705{
3706 struct via_spec *spec = codec->spec;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003707 unsigned int parm;
3708 unsigned int present;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003709 /* inputs */
3710 /* PW 5/6/7 (29h/2ah/2bh) */
3711 parm = AC_PWRST_D3;
3712 set_pin_power_state(codec, 0x29, &parm);
3713 set_pin_power_state(codec, 0x2a, &parm);
3714 set_pin_power_state(codec, 0x2b, &parm);
3715 parm = AC_PWRST_D0;
3716 /* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003717 update_power_state(codec, 0x1e, parm);
3718 update_power_state(codec, 0x1f, parm);
3719 update_power_state(codec, 0x10, parm);
3720 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003721
3722 /* outputs */
3723 /* AOW0 (8h)*/
Takashi Iwai054d8672012-01-24 12:25:50 +01003724 update_power_state(codec, 0x8, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003725
3726 /* PW4 (28h), MW4 (18h), MUX4(38h) */
3727 parm = AC_PWRST_D3;
3728 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003729 update_power_state(codec, 0x18, parm);
3730 update_power_state(codec, 0x38, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003731
3732 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3733 parm = AC_PWRST_D3;
3734 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003735 update_power_state(codec, 0x15, parm);
3736 update_power_state(codec, 0x35, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003737 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003738 update_power_state(codec, 0x9, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003739
3740 /* Internal Speaker */
3741 /* PW0 (24h), MW0(14h), MUX0(34h) */
3742 present = snd_hda_jack_detect(codec, 0x25);
3743
3744 parm = AC_PWRST_D3;
3745 set_pin_power_state(codec, 0x24, &parm);
3746 if (present) {
Takashi Iwai054d8672012-01-24 12:25:50 +01003747 update_power_state(codec, 0x14, AC_PWRST_D3);
3748 update_power_state(codec, 0x34, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003749 } else {
Takashi Iwai054d8672012-01-24 12:25:50 +01003750 update_power_state(codec, 0x14, AC_PWRST_D0);
3751 update_power_state(codec, 0x34, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003752 }
3753
3754
3755 /* Mono Out */
3756 /* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
3757 present = snd_hda_jack_detect(codec, 0x28);
3758
3759 parm = AC_PWRST_D3;
3760 set_pin_power_state(codec, 0x31, &parm);
3761 if (present) {
Takashi Iwai054d8672012-01-24 12:25:50 +01003762 update_power_state(codec, 0x1c, AC_PWRST_D3);
3763 update_power_state(codec, 0x3c, AC_PWRST_D3);
3764 update_power_state(codec, 0x3e, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003765 } else {
Takashi Iwai054d8672012-01-24 12:25:50 +01003766 update_power_state(codec, 0x1c, AC_PWRST_D0);
3767 update_power_state(codec, 0x3c, AC_PWRST_D0);
3768 update_power_state(codec, 0x3e, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003769 }
3770
3771 /* PW15 (33h), MW15 (1dh), MUX15(3dh) */
3772 parm = AC_PWRST_D3;
3773 set_pin_power_state(codec, 0x33, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003774 update_power_state(codec, 0x1d, parm);
3775 update_power_state(codec, 0x3d, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003776
3777}
Lydia Wangab6734e2009-10-10 19:08:46 +08003778
3779/* patch for vt1812 */
3780static int patch_vt1812(struct hda_codec *codec)
3781{
3782 struct via_spec *spec;
3783 int err;
3784
3785 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003786 spec = via_new_spec(codec);
Lydia Wangab6734e2009-10-10 19:08:46 +08003787 if (spec == NULL)
3788 return -ENOMEM;
3789
Takashi Iwai620e2b22011-06-17 17:19:19 +02003790 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003791 override_mic_boost(codec, 0x2b, 0, 3, 40);
3792 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003793 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003794
Lydia Wangab6734e2009-10-10 19:08:46 +08003795 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003796 err = via_parse_auto_config(codec);
Lydia Wangab6734e2009-10-10 19:08:46 +08003797 if (err < 0) {
3798 via_free(codec);
3799 return err;
Lydia Wangab6734e2009-10-10 19:08:46 +08003800 }
3801
Takashi Iwai096a8852011-06-20 12:09:02 +02003802 spec->init_verbs[spec->num_iverbs++] = vt1812_init_verbs;
Lydia Wangab6734e2009-10-10 19:08:46 +08003803
Lydia Wangab6734e2009-10-10 19:08:46 +08003804 codec->patch_ops = via_patch_ops;
3805
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003806 spec->set_widgets_power_state = set_widgets_power_state_vt1812;
Lydia Wangab6734e2009-10-10 19:08:46 +08003807 return 0;
3808}
3809
Joseph Chanc577b8a2006-11-29 15:29:40 +01003810/*
3811 * patch entries
3812 */
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003813static const struct hda_codec_preset snd_hda_preset_via[] = {
Takashi Iwai3218c172008-12-18 09:17:56 +01003814 { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
3815 { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
3816 { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
3817 { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
3818 { .id = 0x1106e710, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003819 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003820 { .id = 0x1106e711, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003821 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003822 { .id = 0x1106e712, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003823 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003824 { .id = 0x1106e713, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003825 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003826 { .id = 0x1106e714, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003827 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003828 { .id = 0x1106e715, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003829 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003830 { .id = 0x1106e716, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003831 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003832 { .id = 0x1106e717, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003833 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003834 { .id = 0x1106e720, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003835 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003836 { .id = 0x1106e721, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003837 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003838 { .id = 0x1106e722, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003839 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003840 { .id = 0x1106e723, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003841 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003842 { .id = 0x1106e724, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003843 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003844 { .id = 0x1106e725, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003845 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003846 { .id = 0x1106e726, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003847 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003848 { .id = 0x1106e727, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003849 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003850 { .id = 0x11060397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003851 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003852 { .id = 0x11061397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003853 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003854 { .id = 0x11062397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003855 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003856 { .id = 0x11063397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003857 .patch = patch_vt1708S},
Lydia Wangbc92df72011-03-23 17:56:05 +08003858 { .id = 0x11064397, .name = "VT1705",
Harald Welted949cac2008-09-09 15:56:01 +08003859 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003860 { .id = 0x11065397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003861 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003862 { .id = 0x11066397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003863 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003864 { .id = 0x11067397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003865 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003866 { .id = 0x11060398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003867 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003868 { .id = 0x11061398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003869 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003870 { .id = 0x11062398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003871 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003872 { .id = 0x11063398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003873 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003874 { .id = 0x11064398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003875 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003876 { .id = 0x11065398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003877 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003878 { .id = 0x11066398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003879 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003880 { .id = 0x11067398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003881 .patch = patch_vt1702},
Lydia Wangeb7188c2009-10-10 19:08:34 +08003882 { .id = 0x11060428, .name = "VT1718S",
3883 .patch = patch_vt1718S},
3884 { .id = 0x11064428, .name = "VT1718S",
3885 .patch = patch_vt1718S},
Lydia Wangbb3c6bfc2009-10-10 19:08:39 +08003886 { .id = 0x11060441, .name = "VT2020",
3887 .patch = patch_vt1718S},
3888 { .id = 0x11064441, .name = "VT1828S",
3889 .patch = patch_vt1718S},
Lydia Wangf3db4232009-10-10 19:08:41 +08003890 { .id = 0x11060433, .name = "VT1716S",
3891 .patch = patch_vt1716S},
3892 { .id = 0x1106a721, .name = "VT1716S",
3893 .patch = patch_vt1716S},
Lydia Wang25eaba22009-10-10 19:08:43 +08003894 { .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
3895 { .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
Lydia Wangab6734e2009-10-10 19:08:46 +08003896 { .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
Lydia Wang36dd5c42009-10-20 13:18:04 +08003897 { .id = 0x11060440, .name = "VT1818S",
3898 .patch = patch_vt1708S},
Lydia Wang118909562011-03-23 17:57:34 +08003899 { .id = 0x11060446, .name = "VT1802",
3900 .patch = patch_vt2002P},
3901 { .id = 0x11068446, .name = "VT1802",
3902 .patch = patch_vt2002P},
Joseph Chanc577b8a2006-11-29 15:29:40 +01003903 {} /* terminator */
3904};
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003905
3906MODULE_ALIAS("snd-hda-codec-id:1106*");
3907
3908static struct hda_codec_preset_list via_list = {
3909 .preset = snd_hda_preset_via,
3910 .owner = THIS_MODULE,
3911};
3912
3913MODULE_LICENSE("GPL");
3914MODULE_DESCRIPTION("VIA HD-audio codec");
3915
3916static int __init patch_via_init(void)
3917{
3918 return snd_hda_add_codec_preset(&via_list);
3919}
3920
3921static void __exit patch_via_exit(void)
3922{
3923 snd_hda_delete_codec_preset(&via_list);
3924}
3925
3926module_init(patch_via_init)
3927module_exit(patch_via_exit)