blob: 93d52fc605fba636b7a25f0bce74afa755ad97af [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;
535 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
536 pin_type);
537 if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
538 snd_hda_codec_write(codec, pin, 0,
Takashi Iwaid3a11e62009-07-07 13:43:35 +0200539 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
Joseph Chanc577b8a2006-11-29 15:29:40 +0100540}
541
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200542static void via_auto_init_output(struct hda_codec *codec,
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200543 struct nid_path *path, int pin_type)
Takashi Iwai5d417622011-06-20 11:32:27 +0200544{
Takashi Iwai5d417622011-06-20 11:32:27 +0200545 unsigned int caps;
Lydia Wangd69607b32011-07-08 14:02:52 +0800546 hda_nid_t pin;
Takashi Iwai5d417622011-06-20 11:32:27 +0200547
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200548 if (!path->depth)
Takashi Iwai5d417622011-06-20 11:32:27 +0200549 return;
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200550 pin = path->path[path->depth - 1];
Takashi Iwai5d417622011-06-20 11:32:27 +0200551
552 init_output_pin(codec, pin, pin_type);
553 caps = query_amp_caps(codec, pin, HDA_OUTPUT);
554 if (caps & AC_AMPCAP_MUTE) {
555 unsigned int val;
556 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
557 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
558 AMP_OUT_MUTE | val);
559 }
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200560 activate_output_path(codec, path, true, true); /* force on */
Takashi Iwai5d417622011-06-20 11:32:27 +0200561}
562
Joseph Chanc577b8a2006-11-29 15:29:40 +0100563static void via_auto_init_multi_out(struct hda_codec *codec)
564{
565 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200566 struct nid_path *path;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100567 int i;
568
Takashi Iwai3214b962011-07-18 12:49:25 +0200569 for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++) {
570 path = &spec->out_path[i];
571 if (!i && spec->aamix_mode && spec->out_mix_path.depth)
572 path = &spec->out_mix_path;
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200573 via_auto_init_output(codec, path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200574 }
Joseph Chanc577b8a2006-11-29 15:29:40 +0100575}
576
Takashi Iwai020066d2011-07-21 13:45:56 +0200577/* deactivate the inactive headphone-paths */
578static void deactivate_hp_paths(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +0100579{
580 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200581 int shared = spec->hp_indep_shared;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100582
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200583 if (spec->hp_independent_mode) {
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200584 activate_output_path(codec, &spec->hp_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200585 activate_output_path(codec, &spec->hp_mix_path, false, false);
586 if (shared)
587 activate_output_path(codec, &spec->out_path[shared],
588 false, false);
Takashi Iwai020066d2011-07-21 13:45:56 +0200589 } else if (spec->aamix_mode || !spec->hp_path.depth) {
590 activate_output_path(codec, &spec->hp_indep_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200591 activate_output_path(codec, &spec->hp_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200592 } else {
Takashi Iwai020066d2011-07-21 13:45:56 +0200593 activate_output_path(codec, &spec->hp_indep_path, false, false);
Takashi Iwai3214b962011-07-18 12:49:25 +0200594 activate_output_path(codec, &spec->hp_mix_path, false, false);
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200595 }
Joseph Chanc577b8a2006-11-29 15:29:40 +0100596}
597
Takashi Iwai020066d2011-07-21 13:45:56 +0200598static void via_auto_init_hp_out(struct hda_codec *codec)
599{
600 struct via_spec *spec = codec->spec;
601
602 if (!spec->hp_path.depth) {
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200603 via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200604 return;
605 }
606 deactivate_hp_paths(codec);
607 if (spec->hp_independent_mode)
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200608 via_auto_init_output(codec, &spec->hp_indep_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200609 else if (spec->aamix_mode)
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200610 via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200611 else
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200612 via_auto_init_output(codec, &spec->hp_path, PIN_HP);
Takashi Iwai020066d2011-07-21 13:45:56 +0200613}
614
Takashi Iwai4a918ff2011-06-20 12:39:26 +0200615static void via_auto_init_speaker_out(struct hda_codec *codec)
616{
617 struct via_spec *spec = codec->spec;
618
Takashi Iwai3214b962011-07-18 12:49:25 +0200619 if (!spec->autocfg.speaker_outs)
620 return;
621 if (!spec->speaker_path.depth) {
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200622 via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200623 return;
624 }
625 if (!spec->aamix_mode) {
626 activate_output_path(codec, &spec->speaker_mix_path,
627 false, false);
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200628 via_auto_init_output(codec, &spec->speaker_path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200629 } else {
630 activate_output_path(codec, &spec->speaker_path, false, false);
Takashi Iwaia353fbb2011-07-21 14:23:35 +0200631 via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
Takashi Iwai3214b962011-07-18 12:49:25 +0200632 }
Takashi Iwai4a918ff2011-06-20 12:39:26 +0200633}
634
Takashi Iwaif4a78282011-06-17 18:46:48 +0200635static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
Takashi Iwai6e969d92011-07-11 11:28:13 +0200636static void via_hp_automute(struct hda_codec *codec);
Clemens Ladisch32e01912010-07-12 16:28:50 +0200637
Joseph Chanc577b8a2006-11-29 15:29:40 +0100638static void via_auto_init_analog_input(struct hda_codec *codec)
639{
640 struct via_spec *spec = codec->spec;
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200641 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai096a8852011-06-20 12:09:02 +0200642 hda_nid_t conn[HDA_MAX_CONNECTIONS];
Clemens Ladisch32e01912010-07-12 16:28:50 +0200643 unsigned int ctl;
Takashi Iwai096a8852011-06-20 12:09:02 +0200644 int i, num_conns;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100645
Takashi Iwai096a8852011-06-20 12:09:02 +0200646 /* init ADCs */
647 for (i = 0; i < spec->num_adc_nids; i++) {
648 snd_hda_codec_write(codec, spec->adc_nids[i], 0,
649 AC_VERB_SET_AMP_GAIN_MUTE,
650 AMP_IN_UNMUTE(0));
651 }
652
653 /* init pins */
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200654 for (i = 0; i < cfg->num_inputs; i++) {
655 hda_nid_t nid = cfg->inputs[i].pin;
Takashi Iwaif4a78282011-06-17 18:46:48 +0200656 if (spec->smart51_enabled && is_smart51_pins(codec, nid))
Clemens Ladisch32e01912010-07-12 16:28:50 +0200657 ctl = PIN_OUT;
David Henningsson30649672011-02-21 10:23:18 +0100658 else if (cfg->inputs[i].type == AUTO_PIN_MIC)
Clemens Ladisch32e01912010-07-12 16:28:50 +0200659 ctl = PIN_VREF50;
660 else
661 ctl = PIN_IN;
Joseph Chanc577b8a2006-11-29 15:29:40 +0100662 snd_hda_codec_write(codec, nid, 0,
Clemens Ladisch32e01912010-07-12 16:28:50 +0200663 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
Joseph Chanc577b8a2006-11-29 15:29:40 +0100664 }
Takashi Iwai096a8852011-06-20 12:09:02 +0200665
666 /* init input-src */
667 for (i = 0; i < spec->num_adc_nids; i++) {
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200668 int adc_idx = spec->inputs[spec->cur_mux[i]].adc_idx;
Takashi Iwaifc1156c2012-02-13 15:04:06 +0100669 /* secondary ADCs must have the unique MUX */
670 if (i > 0 && !spec->mux_nids[i])
671 break;
Takashi Iwaia86a88e2011-06-22 15:23:25 +0200672 if (spec->mux_nids[adc_idx]) {
673 int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
674 snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
675 AC_VERB_SET_CONNECT_SEL,
676 mux_idx);
677 }
678 if (spec->dyn_adc_switch)
679 break; /* only one input-src */
Takashi Iwai096a8852011-06-20 12:09:02 +0200680 }
681
682 /* init aa-mixer */
683 if (!spec->aa_mix_nid)
684 return;
685 num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
686 ARRAY_SIZE(conn));
687 for (i = 0; i < num_conns; i++) {
688 unsigned int caps = get_wcaps(codec, conn[i]);
689 if (get_wcaps_type(caps) == AC_WID_PIN)
690 snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
691 AC_VERB_SET_AMP_GAIN_MUTE,
692 AMP_IN_MUTE(i));
693 }
Joseph Chanc577b8a2006-11-29 15:29:40 +0100694}
Lydia Wangf5271102009-10-10 19:07:35 +0800695
Takashi Iwai054d8672012-01-24 12:25:50 +0100696static void update_power_state(struct hda_codec *codec, hda_nid_t nid,
697 unsigned int parm)
698{
699 if (snd_hda_codec_read(codec, nid, 0,
700 AC_VERB_GET_POWER_STATE, 0) == parm)
701 return;
702 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
703}
704
Lydia Wangf5271102009-10-10 19:07:35 +0800705static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
706 unsigned int *affected_parm)
707{
708 unsigned parm;
709 unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
710 unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
711 >> AC_DEFCFG_MISC_SHIFT
712 & AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
Lydia Wang1564b282009-10-10 19:07:52 +0800713 struct via_spec *spec = codec->spec;
Takashi Iwai24088a52011-06-17 16:59:21 +0200714 unsigned present = 0;
715
716 no_presence |= spec->no_pin_power_ctl;
717 if (!no_presence)
718 present = snd_hda_jack_detect(codec, nid);
Takashi Iwaif4a78282011-06-17 18:46:48 +0200719 if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
Lydia Wang1564b282009-10-10 19:07:52 +0800720 || ((no_presence || present)
721 && get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
Lydia Wangf5271102009-10-10 19:07:35 +0800722 *affected_parm = AC_PWRST_D0; /* if it's connected */
723 parm = AC_PWRST_D0;
724 } else
725 parm = AC_PWRST_D3;
726
Takashi Iwai054d8672012-01-24 12:25:50 +0100727 update_power_state(codec, nid, parm);
Lydia Wangf5271102009-10-10 19:07:35 +0800728}
729
Takashi Iwai24088a52011-06-17 16:59:21 +0200730static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
731 struct snd_ctl_elem_info *uinfo)
732{
733 static const char * const texts[] = {
734 "Disabled", "Enabled"
735 };
736
737 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
738 uinfo->count = 1;
739 uinfo->value.enumerated.items = 2;
740 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
741 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
742 strcpy(uinfo->value.enumerated.name,
743 texts[uinfo->value.enumerated.item]);
744 return 0;
745}
746
747static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
748 struct snd_ctl_elem_value *ucontrol)
749{
750 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
751 struct via_spec *spec = codec->spec;
752 ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
753 return 0;
754}
755
756static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
757 struct snd_ctl_elem_value *ucontrol)
758{
759 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
760 struct via_spec *spec = codec->spec;
761 unsigned int val = !ucontrol->value.enumerated.item[0];
762
763 if (val == spec->no_pin_power_ctl)
764 return 0;
765 spec->no_pin_power_ctl = val;
766 set_widgets_power_state(codec);
Takashi Iwaie9d010c2012-02-01 10:33:23 +0100767 analog_low_current_mode(codec);
Takashi Iwai24088a52011-06-17 16:59:21 +0200768 return 1;
769}
770
771static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
772 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
773 .name = "Dynamic Power-Control",
774 .info = via_pin_power_ctl_info,
775 .get = via_pin_power_ctl_get,
776 .put = via_pin_power_ctl_put,
777};
778
779
Harald Welte0aa62ae2008-09-09 15:58:27 +0800780static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
781 struct snd_ctl_elem_info *uinfo)
782{
Takashi Iwai8df2a312011-06-21 11:48:29 +0200783 static const char * const texts[] = { "OFF", "ON" };
784
785 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
786 uinfo->count = 1;
787 uinfo->value.enumerated.items = 2;
788 if (uinfo->value.enumerated.item >= 2)
789 uinfo->value.enumerated.item = 1;
790 strcpy(uinfo->value.enumerated.name,
791 texts[uinfo->value.enumerated.item]);
792 return 0;
Harald Welte0aa62ae2008-09-09 15:58:27 +0800793}
794
795static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
796 struct snd_ctl_elem_value *ucontrol)
797{
798 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Lydia Wangcdc17842009-10-10 19:07:47 +0800799 struct via_spec *spec = codec->spec;
Lydia Wangcdc17842009-10-10 19:07:47 +0800800
Takashi Iwaiece8d042011-06-19 16:24:21 +0200801 ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
Lydia Wangcdc17842009-10-10 19:07:47 +0800802 return 0;
803}
804
Takashi Iwai3b607e32011-07-18 16:54:40 +0200805/* adjust spec->multiout setup according to the current flags */
806static void setup_playback_multi_pcm(struct via_spec *spec)
807{
808 const struct auto_pin_cfg *cfg = &spec->autocfg;
809 spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
810 spec->multiout.hp_nid = 0;
811 if (!spec->hp_independent_mode) {
812 if (!spec->hp_indep_shared)
813 spec->multiout.hp_nid = spec->hp_dac_nid;
814 } else {
815 if (spec->hp_indep_shared)
816 spec->multiout.num_dacs = cfg->line_outs - 1;
817 }
818}
819
820/* update DAC setups according to indep-HP switch;
821 * this function is called only when indep-HP is modified
822 */
823static void switch_indep_hp_dacs(struct hda_codec *codec)
824{
825 struct via_spec *spec = codec->spec;
826 int shared = spec->hp_indep_shared;
827 hda_nid_t shared_dac, hp_dac;
828
829 if (!spec->opened_streams)
830 return;
831
832 shared_dac = shared ? spec->multiout.dac_nids[shared] : 0;
833 hp_dac = spec->hp_dac_nid;
834 if (spec->hp_independent_mode) {
835 /* switch to indep-HP mode */
836 if (spec->active_streams & STREAM_MULTI_OUT) {
837 __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
838 __snd_hda_codec_cleanup_stream(codec, shared_dac, 1);
839 }
840 if (spec->active_streams & STREAM_INDEP_HP)
841 snd_hda_codec_setup_stream(codec, hp_dac,
842 spec->cur_hp_stream_tag, 0,
843 spec->cur_hp_format);
844 } else {
845 /* back to HP or shared-DAC */
846 if (spec->active_streams & STREAM_INDEP_HP)
847 __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
848 if (spec->active_streams & STREAM_MULTI_OUT) {
849 hda_nid_t dac;
850 int ch;
851 if (shared_dac) { /* reset mutli-ch DAC */
852 dac = shared_dac;
853 ch = shared * 2;
854 } else { /* reset HP DAC */
855 dac = hp_dac;
856 ch = 0;
857 }
858 snd_hda_codec_setup_stream(codec, dac,
859 spec->cur_dac_stream_tag, ch,
860 spec->cur_dac_format);
861 }
862 }
863 setup_playback_multi_pcm(spec);
864}
865
Harald Welte0aa62ae2008-09-09 15:58:27 +0800866static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
867 struct snd_ctl_elem_value *ucontrol)
868{
869 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
870 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200871 int cur, shared;
Takashi Iwai8df2a312011-06-21 11:48:29 +0200872
Takashi Iwai3b607e32011-07-18 16:54:40 +0200873 mutex_lock(&spec->config_mutex);
Takashi Iwai25250502011-06-30 17:24:47 +0200874 cur = !!ucontrol->value.enumerated.item[0];
Takashi Iwai3b607e32011-07-18 16:54:40 +0200875 if (spec->hp_independent_mode == cur) {
876 mutex_unlock(&spec->config_mutex);
Takashi Iwai25250502011-06-30 17:24:47 +0200877 return 0;
Takashi Iwai3b607e32011-07-18 16:54:40 +0200878 }
Takashi Iwai25250502011-06-30 17:24:47 +0200879 spec->hp_independent_mode = cur;
Takashi Iwai3214b962011-07-18 12:49:25 +0200880 shared = spec->hp_indep_shared;
Takashi Iwai020066d2011-07-21 13:45:56 +0200881 deactivate_hp_paths(codec);
882 if (cur)
883 activate_output_path(codec, &spec->hp_indep_path, true, false);
884 else {
Takashi Iwai3214b962011-07-18 12:49:25 +0200885 if (shared)
886 activate_output_path(codec, &spec->out_path[shared],
Takashi Iwai25250502011-06-30 17:24:47 +0200887 true, false);
Takashi Iwai020066d2011-07-21 13:45:56 +0200888 if (spec->aamix_mode || !spec->hp_path.depth)
889 activate_output_path(codec, &spec->hp_mix_path,
890 true, false);
891 else
892 activate_output_path(codec, &spec->hp_path,
893 true, false);
Takashi Iwai8df2a312011-06-21 11:48:29 +0200894 }
Harald Welte0aa62ae2008-09-09 15:58:27 +0800895
Takashi Iwai3b607e32011-07-18 16:54:40 +0200896 switch_indep_hp_dacs(codec);
897 mutex_unlock(&spec->config_mutex);
898
Lydia Wangce0e5a92011-03-22 16:22:37 +0800899 /* update jack power state */
Lydia Wang3e95b9a2011-03-23 15:13:28 +0800900 set_widgets_power_state(codec);
Takashi Iwai6e969d92011-07-11 11:28:13 +0200901 via_hp_automute(codec);
Takashi Iwai25250502011-06-30 17:24:47 +0200902 return 1;
Harald Welte0aa62ae2008-09-09 15:58:27 +0800903}
904
Takashi Iwaiece8d042011-06-19 16:24:21 +0200905static const struct snd_kcontrol_new via_hp_mixer = {
906 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
907 .name = "Independent HP",
908 .info = via_independent_hp_info,
909 .get = via_independent_hp_get,
910 .put = via_independent_hp_put,
Harald Welte0aa62ae2008-09-09 15:58:27 +0800911};
912
Takashi Iwai3d83e572010-04-14 14:36:23 +0200913static int via_hp_build(struct hda_codec *codec)
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100914{
Takashi Iwai3d83e572010-04-14 14:36:23 +0200915 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100916 struct snd_kcontrol_new *knew;
917 hda_nid_t nid;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100918
Takashi Iwaiece8d042011-06-19 16:24:21 +0200919 nid = spec->autocfg.hp_pins[0];
920 knew = via_clone_control(spec, &via_hp_mixer);
Takashi Iwai3d83e572010-04-14 14:36:23 +0200921 if (knew == NULL)
922 return -ENOMEM;
923
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100924 knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100925
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100926 return 0;
927}
928
Lydia Wang1564b282009-10-10 19:07:52 +0800929static void notify_aa_path_ctls(struct hda_codec *codec)
930{
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200931 struct via_spec *spec = codec->spec;
Lydia Wang1564b282009-10-10 19:07:52 +0800932 int i;
Lydia Wang1564b282009-10-10 19:07:52 +0800933
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200934 for (i = 0; i < spec->smart51_nums; i++) {
935 struct snd_kcontrol *ctl;
936 struct snd_ctl_elem_id id;
937 memset(&id, 0, sizeof(id));
938 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
939 sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
Lydia Wang525566c2011-04-28 16:03:39 +0800940 ctl = snd_hda_find_mixer_ctl(codec, id.name);
941 if (ctl)
942 snd_ctl_notify(codec->bus->card,
943 SNDRV_CTL_EVENT_MASK_VALUE,
944 &ctl->id);
Lydia Wang1564b282009-10-10 19:07:52 +0800945 }
946}
947
948static void mute_aa_path(struct hda_codec *codec, int mute)
949{
950 struct via_spec *spec = codec->spec;
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200951 int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
Lydia Wang1564b282009-10-10 19:07:52 +0800952 int i;
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200953
Lydia Wang1564b282009-10-10 19:07:52 +0800954 /* check AA path's mute status */
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200955 for (i = 0; i < spec->smart51_nums; i++) {
956 if (spec->smart51_idxs[i] < 0)
957 continue;
958 snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
959 HDA_INPUT, spec->smart51_idxs[i],
Lydia Wang1564b282009-10-10 19:07:52 +0800960 HDA_AMP_MUTE, val);
961 }
962}
Takashi Iwaif4a78282011-06-17 18:46:48 +0200963
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200964static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
965{
966 struct via_spec *spec = codec->spec;
967 int i;
968
969 for (i = 0; i < spec->smart51_nums; i++)
970 if (spec->smart51_pins[i] == pin)
971 return true;
972 return false;
973}
974
Lydia Wang1564b282009-10-10 19:07:52 +0800975static int via_smart51_get(struct snd_kcontrol *kcontrol,
976 struct snd_ctl_elem_value *ucontrol)
977{
978 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
979 struct via_spec *spec = codec->spec;
Lydia Wang1564b282009-10-10 19:07:52 +0800980
Takashi Iwaif2b1c9f2011-06-21 16:52:39 +0200981 *ucontrol->value.integer.value = spec->smart51_enabled;
Lydia Wang1564b282009-10-10 19:07:52 +0800982 return 0;
983}
984
985static int via_smart51_put(struct snd_kcontrol *kcontrol,
986 struct snd_ctl_elem_value *ucontrol)
987{
988 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
989 struct via_spec *spec = codec->spec;
990 int out_in = *ucontrol->value.integer.value
991 ? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
Lydia Wang1564b282009-10-10 19:07:52 +0800992 int i;
993
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200994 for (i = 0; i < spec->smart51_nums; i++) {
995 hda_nid_t nid = spec->smart51_pins[i];
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200996 unsigned int parm;
997
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200998 parm = snd_hda_codec_read(codec, nid, 0,
999 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1000 parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
1001 parm |= out_in;
1002 snd_hda_codec_write(codec, nid, 0,
1003 AC_VERB_SET_PIN_WIDGET_CONTROL,
1004 parm);
1005 if (out_in == AC_PINCTL_OUT_EN) {
1006 mute_aa_path(codec, 1);
1007 notify_aa_path_ctls(codec);
1008 }
Lydia Wang1564b282009-10-10 19:07:52 +08001009 }
1010 spec->smart51_enabled = *ucontrol->value.integer.value;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08001011 set_widgets_power_state(codec);
Lydia Wang1564b282009-10-10 19:07:52 +08001012 return 1;
1013}
1014
Takashi Iwai5f4b36d2011-06-17 14:55:02 +02001015static const struct snd_kcontrol_new via_smart51_mixer = {
1016 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1017 .name = "Smart 5.1",
1018 .count = 1,
Takashi Iwaif2b1c9f2011-06-21 16:52:39 +02001019 .info = snd_ctl_boolean_mono_info,
Takashi Iwai5f4b36d2011-06-17 14:55:02 +02001020 .get = via_smart51_get,
1021 .put = via_smart51_put,
Lydia Wang1564b282009-10-10 19:07:52 +08001022};
1023
Takashi Iwaif4a78282011-06-17 18:46:48 +02001024static int via_smart51_build(struct hda_codec *codec)
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001025{
Takashi Iwaif4a78282011-06-17 18:46:48 +02001026 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001027
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001028 if (!spec->smart51_nums)
Lydia Wangcb34c202011-04-27 17:44:16 +08001029 return 0;
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001030 if (!via_clone_control(spec, &via_smart51_mixer))
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001031 return -ENOMEM;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001032 return 0;
1033}
1034
Takashi Iwaiada509e2011-06-20 15:40:19 +02001035/* check AA path's mute status */
1036static bool is_aa_path_mute(struct hda_codec *codec)
Lydia Wangf5271102009-10-10 19:07:35 +08001037{
Lydia Wangf5271102009-10-10 19:07:35 +08001038 struct via_spec *spec = codec->spec;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001039 const struct hda_amp_list *p;
1040 int i, ch, v;
1041
1042 for (i = 0; i < spec->num_loopbacks; i++) {
1043 p = &spec->loopback_list[i];
1044 for (ch = 0; ch < 2; ch++) {
1045 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
1046 p->idx);
1047 if (!(v & HDA_AMP_MUTE) && v > 0)
1048 return false;
Lydia Wangf5271102009-10-10 19:07:35 +08001049 }
1050 }
Takashi Iwaiada509e2011-06-20 15:40:19 +02001051 return true;
Lydia Wangf5271102009-10-10 19:07:35 +08001052}
1053
1054/* enter/exit analog low-current mode */
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001055static void __analog_low_current_mode(struct hda_codec *codec, bool force)
Lydia Wangf5271102009-10-10 19:07:35 +08001056{
1057 struct via_spec *spec = codec->spec;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001058 bool enable;
1059 unsigned int verb, parm;
Lydia Wangf5271102009-10-10 19:07:35 +08001060
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001061 if (spec->no_pin_power_ctl)
1062 enable = false;
1063 else
1064 enable = is_aa_path_mute(codec) && !spec->opened_streams;
1065 if (enable == spec->alc_mode && !force)
1066 return;
1067 spec->alc_mode = enable;
Lydia Wangf5271102009-10-10 19:07:35 +08001068
1069 /* decide low current mode's verb & parameter */
1070 switch (spec->codec_type) {
1071 case VT1708B_8CH:
1072 case VT1708B_4CH:
1073 verb = 0xf70;
1074 parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1075 break;
1076 case VT1708S:
Lydia Wangeb7188c2009-10-10 19:08:34 +08001077 case VT1718S:
Lydia Wangf3db4232009-10-10 19:08:41 +08001078 case VT1716S:
Lydia Wangf5271102009-10-10 19:07:35 +08001079 verb = 0xf73;
1080 parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1081 break;
1082 case VT1702:
1083 verb = 0xf73;
1084 parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1085 break;
Lydia Wang25eaba22009-10-10 19:08:43 +08001086 case VT2002P:
Lydia Wangab6734e2009-10-10 19:08:46 +08001087 case VT1812:
Lydia Wang118909562011-03-23 17:57:34 +08001088 case VT1802:
Lydia Wang25eaba22009-10-10 19:08:43 +08001089 verb = 0xf93;
1090 parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
1091 break;
Lydia Wangf5271102009-10-10 19:07:35 +08001092 default:
1093 return; /* other codecs are not supported */
1094 }
1095 /* send verb */
1096 snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1097}
1098
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001099static void analog_low_current_mode(struct hda_codec *codec)
1100{
1101 return __analog_low_current_mode(codec, false);
1102}
1103
Joseph Chanc577b8a2006-11-29 15:29:40 +01001104/*
1105 * generic initialization of ADC, input mixers and output mixers
1106 */
Takashi Iwai096a8852011-06-20 12:09:02 +02001107static const struct hda_verb vt1708_init_verbs[] = {
Lydia Wangaa266fc2011-03-24 12:41:01 +08001108 /* power down jack detect function */
1109 {0x1, 0xf81, 0x1},
Josepch Chanf7278fd2007-12-13 16:40:40 +01001110 { }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001111};
1112
Takashi Iwai3b607e32011-07-18 16:54:40 +02001113static void set_stream_open(struct hda_codec *codec, int bit, bool active)
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001114{
Takashi Iwaiada509e2011-06-20 15:40:19 +02001115 struct via_spec *spec = codec->spec;
1116
1117 if (active)
Takashi Iwai3b607e32011-07-18 16:54:40 +02001118 spec->opened_streams |= bit;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001119 else
Takashi Iwai3b607e32011-07-18 16:54:40 +02001120 spec->opened_streams &= ~bit;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001121 analog_low_current_mode(codec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001122}
1123
Takashi Iwaiece8d042011-06-19 16:24:21 +02001124static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001125 struct hda_codec *codec,
1126 struct snd_pcm_substream *substream)
1127{
1128 struct via_spec *spec = codec->spec;
Takashi Iwai25250502011-06-30 17:24:47 +02001129 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001130 int err;
Takashi Iwaiece8d042011-06-19 16:24:21 +02001131
Takashi Iwai25250502011-06-30 17:24:47 +02001132 spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
Takashi Iwai25250502011-06-30 17:24:47 +02001133 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001134 set_stream_open(codec, STREAM_MULTI_OUT, true);
Takashi Iwaiada509e2011-06-20 15:40:19 +02001135 err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
1136 hinfo);
1137 if (err < 0) {
Takashi Iwai3b607e32011-07-18 16:54:40 +02001138 set_stream_open(codec, STREAM_MULTI_OUT, false);
Takashi Iwaiada509e2011-06-20 15:40:19 +02001139 return err;
1140 }
1141 return 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001142}
1143
Takashi Iwaiece8d042011-06-19 16:24:21 +02001144static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
Takashi Iwai9af74212011-06-18 16:17:45 +02001145 struct hda_codec *codec,
1146 struct snd_pcm_substream *substream)
1147{
Takashi Iwai3b607e32011-07-18 16:54:40 +02001148 set_stream_open(codec, STREAM_MULTI_OUT, false);
Takashi Iwai9af74212011-06-18 16:17:45 +02001149 return 0;
1150}
1151
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001152static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
1153 struct hda_codec *codec,
1154 struct snd_pcm_substream *substream)
1155{
1156 struct via_spec *spec = codec->spec;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001157
Takashi Iwaiece8d042011-06-19 16:24:21 +02001158 if (snd_BUG_ON(!spec->hp_dac_nid))
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001159 return -EINVAL;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001160 set_stream_open(codec, STREAM_INDEP_HP, true);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001161 return 0;
1162}
1163
1164static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1165 struct hda_codec *codec,
1166 struct snd_pcm_substream *substream)
1167{
Takashi Iwai3b607e32011-07-18 16:54:40 +02001168 set_stream_open(codec, STREAM_INDEP_HP, false);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001169 return 0;
1170}
1171
1172static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
1173 struct hda_codec *codec,
1174 unsigned int stream_tag,
1175 unsigned int format,
1176 struct snd_pcm_substream *substream)
Harald Welte0aa62ae2008-09-09 15:58:27 +08001177{
1178 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001179
Takashi Iwai3b607e32011-07-18 16:54:40 +02001180 mutex_lock(&spec->config_mutex);
1181 setup_playback_multi_pcm(spec);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001182 snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1183 format, substream);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001184 /* remember for dynamic DAC switch with indep-HP */
1185 spec->active_streams |= STREAM_MULTI_OUT;
1186 spec->cur_dac_stream_tag = stream_tag;
1187 spec->cur_dac_format = format;
1188 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001189 vt1708_update_hp_work(spec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001190 return 0;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001191}
1192
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001193static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
1194 struct hda_codec *codec,
1195 unsigned int stream_tag,
1196 unsigned int format,
1197 struct snd_pcm_substream *substream)
Harald Welte0aa62ae2008-09-09 15:58:27 +08001198{
1199 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001200
Takashi Iwai3b607e32011-07-18 16:54:40 +02001201 mutex_lock(&spec->config_mutex);
1202 if (spec->hp_independent_mode)
1203 snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1204 stream_tag, 0, format);
1205 spec->active_streams |= STREAM_INDEP_HP;
1206 spec->cur_hp_stream_tag = stream_tag;
1207 spec->cur_hp_format = format;
1208 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001209 vt1708_update_hp_work(spec);
Harald Welte0aa62ae2008-09-09 15:58:27 +08001210 return 0;
1211}
1212
1213static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
1214 struct hda_codec *codec,
1215 struct snd_pcm_substream *substream)
1216{
1217 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001218
Takashi Iwai3b607e32011-07-18 16:54:40 +02001219 mutex_lock(&spec->config_mutex);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001220 snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001221 spec->active_streams &= ~STREAM_MULTI_OUT;
1222 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001223 vt1708_update_hp_work(spec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001224 return 0;
1225}
1226
1227static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
1228 struct hda_codec *codec,
1229 struct snd_pcm_substream *substream)
1230{
1231 struct via_spec *spec = codec->spec;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001232
Takashi Iwai3b607e32011-07-18 16:54:40 +02001233 mutex_lock(&spec->config_mutex);
1234 if (spec->hp_independent_mode)
1235 snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
1236 spec->active_streams &= ~STREAM_INDEP_HP;
1237 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001238 vt1708_update_hp_work(spec);
Harald Welte0aa62ae2008-09-09 15:58:27 +08001239 return 0;
1240}
1241
Joseph Chanc577b8a2006-11-29 15:29:40 +01001242/*
1243 * Digital out
1244 */
1245static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1246 struct hda_codec *codec,
1247 struct snd_pcm_substream *substream)
1248{
1249 struct via_spec *spec = codec->spec;
1250 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1251}
1252
1253static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1254 struct hda_codec *codec,
1255 struct snd_pcm_substream *substream)
1256{
1257 struct via_spec *spec = codec->spec;
1258 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1259}
1260
Harald Welte5691ec72008-09-15 22:42:26 +08001261static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Harald Welte98aa34c2008-09-09 16:02:09 +08001262 struct hda_codec *codec,
1263 unsigned int stream_tag,
1264 unsigned int format,
1265 struct snd_pcm_substream *substream)
1266{
1267 struct via_spec *spec = codec->spec;
Takashi Iwai9da29272009-05-07 16:31:14 +02001268 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1269 stream_tag, format, substream);
1270}
Harald Welte5691ec72008-09-15 22:42:26 +08001271
Takashi Iwai9da29272009-05-07 16:31:14 +02001272static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1273 struct hda_codec *codec,
1274 struct snd_pcm_substream *substream)
1275{
1276 struct via_spec *spec = codec->spec;
1277 snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
Harald Welte98aa34c2008-09-09 16:02:09 +08001278 return 0;
1279}
1280
Joseph Chanc577b8a2006-11-29 15:29:40 +01001281/*
1282 * Analog capture
1283 */
1284static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1285 struct hda_codec *codec,
1286 unsigned int stream_tag,
1287 unsigned int format,
1288 struct snd_pcm_substream *substream)
1289{
1290 struct via_spec *spec = codec->spec;
1291
1292 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1293 stream_tag, 0, format);
1294 return 0;
1295}
1296
1297static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1298 struct hda_codec *codec,
1299 struct snd_pcm_substream *substream)
1300{
1301 struct via_spec *spec = codec->spec;
Takashi Iwai888afa12008-03-18 09:57:50 +01001302 snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001303 return 0;
1304}
1305
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001306/* analog capture with dynamic ADC switching */
1307static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1308 struct hda_codec *codec,
1309 unsigned int stream_tag,
1310 unsigned int format,
1311 struct snd_pcm_substream *substream)
1312{
1313 struct via_spec *spec = codec->spec;
1314 int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
1315
Takashi Iwai3b607e32011-07-18 16:54:40 +02001316 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001317 spec->cur_adc = spec->adc_nids[adc_idx];
1318 spec->cur_adc_stream_tag = stream_tag;
1319 spec->cur_adc_format = format;
1320 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001321 mutex_unlock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001322 return 0;
1323}
1324
1325static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1326 struct hda_codec *codec,
1327 struct snd_pcm_substream *substream)
1328{
1329 struct via_spec *spec = codec->spec;
1330
Takashi Iwai3b607e32011-07-18 16:54:40 +02001331 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001332 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1333 spec->cur_adc = 0;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001334 mutex_unlock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001335 return 0;
1336}
1337
1338/* re-setup the stream if running; called from input-src put */
1339static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
1340{
1341 struct via_spec *spec = codec->spec;
1342 int adc_idx = spec->inputs[cur].adc_idx;
1343 hda_nid_t adc = spec->adc_nids[adc_idx];
Takashi Iwai3b607e32011-07-18 16:54:40 +02001344 bool ret = false;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001345
Takashi Iwai3b607e32011-07-18 16:54:40 +02001346 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001347 if (spec->cur_adc && spec->cur_adc != adc) {
1348 /* stream is running, let's swap the current ADC */
1349 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1350 spec->cur_adc = adc;
1351 snd_hda_codec_setup_stream(codec, adc,
1352 spec->cur_adc_stream_tag, 0,
1353 spec->cur_adc_format);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001354 ret = true;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001355 }
Takashi Iwai3b607e32011-07-18 16:54:40 +02001356 mutex_unlock(&spec->config_mutex);
1357 return ret;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001358}
1359
Takashi Iwai9af74212011-06-18 16:17:45 +02001360static const struct hda_pcm_stream via_pcm_analog_playback = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001361 .substreams = 1,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001362 .channels_min = 2,
1363 .channels_max = 8,
Takashi Iwai9af74212011-06-18 16:17:45 +02001364 /* NID is set in via_build_pcms */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001365 .ops = {
Takashi Iwaiece8d042011-06-19 16:24:21 +02001366 .open = via_playback_multi_pcm_open,
1367 .close = via_playback_multi_pcm_close,
Harald Welte0aa62ae2008-09-09 15:58:27 +08001368 .prepare = via_playback_multi_pcm_prepare,
1369 .cleanup = via_playback_multi_pcm_cleanup
Joseph Chanc577b8a2006-11-29 15:29:40 +01001370 },
1371};
1372
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001373static const struct hda_pcm_stream via_pcm_hp_playback = {
1374 .substreams = 1,
1375 .channels_min = 2,
1376 .channels_max = 2,
1377 /* NID is set in via_build_pcms */
1378 .ops = {
1379 .open = via_playback_hp_pcm_open,
Takashi Iwaiece8d042011-06-19 16:24:21 +02001380 .close = via_playback_hp_pcm_close,
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001381 .prepare = via_playback_hp_pcm_prepare,
1382 .cleanup = via_playback_hp_pcm_cleanup
1383 },
1384};
1385
Takashi Iwai90dd48a2011-05-02 12:38:19 +02001386static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001387 .substreams = 1,
Takashi Iwaibc9b562382008-05-23 17:50:27 +02001388 .channels_min = 2,
1389 .channels_max = 8,
Takashi Iwai9af74212011-06-18 16:17:45 +02001390 /* NID is set in via_build_pcms */
Takashi Iwaibc9b562382008-05-23 17:50:27 +02001391 /* We got noisy outputs on the right channel on VT1708 when
1392 * 24bit samples are used. Until any workaround is found,
1393 * disable the 24bit format, so far.
1394 */
1395 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1396 .ops = {
Takashi Iwaiece8d042011-06-19 16:24:21 +02001397 .open = via_playback_multi_pcm_open,
1398 .close = via_playback_multi_pcm_close,
Lydia Wangc873cc22009-10-10 19:08:21 +08001399 .prepare = via_playback_multi_pcm_prepare,
1400 .cleanup = via_playback_multi_pcm_cleanup
Takashi Iwaibc9b562382008-05-23 17:50:27 +02001401 },
1402};
1403
Takashi Iwai9af74212011-06-18 16:17:45 +02001404static const struct hda_pcm_stream via_pcm_analog_capture = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001405 .substreams = 1, /* will be changed in via_build_pcms() */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001406 .channels_min = 2,
1407 .channels_max = 2,
Takashi Iwai9af74212011-06-18 16:17:45 +02001408 /* NID is set in via_build_pcms */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001409 .ops = {
1410 .prepare = via_capture_pcm_prepare,
1411 .cleanup = via_capture_pcm_cleanup
1412 },
1413};
1414
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001415static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
1416 .substreams = 1,
1417 .channels_min = 2,
1418 .channels_max = 2,
1419 /* NID is set in via_build_pcms */
1420 .ops = {
1421 .prepare = via_dyn_adc_capture_pcm_prepare,
1422 .cleanup = via_dyn_adc_capture_pcm_cleanup,
1423 },
1424};
1425
Takashi Iwai9af74212011-06-18 16:17:45 +02001426static const struct hda_pcm_stream via_pcm_digital_playback = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001427 .substreams = 1,
1428 .channels_min = 2,
1429 .channels_max = 2,
1430 /* NID is set in via_build_pcms */
1431 .ops = {
1432 .open = via_dig_playback_pcm_open,
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001433 .close = via_dig_playback_pcm_close,
Takashi Iwai9da29272009-05-07 16:31:14 +02001434 .prepare = via_dig_playback_pcm_prepare,
1435 .cleanup = via_dig_playback_pcm_cleanup
Joseph Chanc577b8a2006-11-29 15:29:40 +01001436 },
1437};
1438
Takashi Iwai9af74212011-06-18 16:17:45 +02001439static const struct hda_pcm_stream via_pcm_digital_capture = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001440 .substreams = 1,
1441 .channels_min = 2,
1442 .channels_max = 2,
1443};
1444
Takashi Iwai370bafb2011-06-20 12:47:45 +02001445/*
1446 * slave controls for virtual master
1447 */
Takashi Iwai9322ca52012-02-03 14:28:01 +01001448static const char * const via_slave_pfxs[] = {
1449 "Front", "Surround", "Center", "LFE", "Side",
1450 "Headphone", "Speaker",
Takashi Iwai370bafb2011-06-20 12:47:45 +02001451 NULL,
1452};
1453
Joseph Chanc577b8a2006-11-29 15:29:40 +01001454static int via_build_controls(struct hda_codec *codec)
1455{
1456 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001457 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001458 int err, i;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001459
Takashi Iwaib5bcc182012-02-02 10:30:17 +01001460 spec->no_pin_power_ctl = 1;
Takashi Iwai24088a52011-06-17 16:59:21 +02001461 if (spec->set_widgets_power_state)
1462 if (!via_clone_control(spec, &via_pin_power_ctl_enum))
1463 return -ENOMEM;
1464
Joseph Chanc577b8a2006-11-29 15:29:40 +01001465 for (i = 0; i < spec->num_mixers; i++) {
1466 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1467 if (err < 0)
1468 return err;
1469 }
1470
1471 if (spec->multiout.dig_out_nid) {
1472 err = snd_hda_create_spdif_out_ctls(codec,
Stephen Warren74b654c2011-06-01 11:14:18 -06001473 spec->multiout.dig_out_nid,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001474 spec->multiout.dig_out_nid);
1475 if (err < 0)
1476 return err;
Takashi Iwai9a081602008-02-12 18:37:26 +01001477 err = snd_hda_create_spdif_share_sw(codec,
1478 &spec->multiout);
1479 if (err < 0)
1480 return err;
1481 spec->multiout.share_spdif = 1;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001482 }
1483 if (spec->dig_in_nid) {
1484 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1485 if (err < 0)
1486 return err;
1487 }
Lydia Wang17314372009-10-10 19:07:37 +08001488
Takashi Iwai370bafb2011-06-20 12:47:45 +02001489 /* if we have no master control, let's create it */
1490 if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1491 unsigned int vmaster_tlv[4];
1492 snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1493 HDA_OUTPUT, vmaster_tlv);
1494 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai9322ca52012-02-03 14:28:01 +01001495 vmaster_tlv, via_slave_pfxs,
1496 "Playback Volume");
Takashi Iwai370bafb2011-06-20 12:47:45 +02001497 if (err < 0)
1498 return err;
1499 }
1500 if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1501 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
Takashi Iwai9322ca52012-02-03 14:28:01 +01001502 NULL, via_slave_pfxs,
1503 "Playback Switch");
Takashi Iwai370bafb2011-06-20 12:47:45 +02001504 if (err < 0)
1505 return err;
1506 }
1507
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001508 /* assign Capture Source enums to NID */
1509 kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1510 for (i = 0; kctl && i < kctl->count; i++) {
Takashi Iwai21949f02009-12-23 08:31:59 +01001511 err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001512 if (err < 0)
1513 return err;
1514 }
1515
Takashi Iwai603c4012008-07-30 15:01:44 +02001516 via_free_kctls(codec); /* no longer needed */
Takashi Iwai01a61e12011-10-28 00:03:22 +02001517
1518 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1519 if (err < 0)
1520 return err;
1521
Joseph Chanc577b8a2006-11-29 15:29:40 +01001522 return 0;
1523}
1524
1525static int via_build_pcms(struct hda_codec *codec)
1526{
1527 struct via_spec *spec = codec->spec;
1528 struct hda_pcm *info = spec->pcm_rec;
1529
Takashi Iwaia5973102011-09-28 16:43:36 +02001530 codec->num_pcms = 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001531 codec->pcm_info = info;
1532
Takashi Iwaia5973102011-09-28 16:43:36 +02001533 if (spec->multiout.num_dacs || spec->num_adc_nids) {
1534 snprintf(spec->stream_name_analog,
1535 sizeof(spec->stream_name_analog),
1536 "%s Analog", codec->chip_name);
1537 info->name = spec->stream_name_analog;
Takashi Iwai9af74212011-06-18 16:17:45 +02001538
Takashi Iwaia5973102011-09-28 16:43:36 +02001539 if (spec->multiout.num_dacs) {
1540 if (!spec->stream_analog_playback)
1541 spec->stream_analog_playback =
1542 &via_pcm_analog_playback;
1543 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1544 *spec->stream_analog_playback;
1545 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1546 spec->multiout.dac_nids[0];
1547 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1548 spec->multiout.max_channels;
1549 }
Takashi Iwai9af74212011-06-18 16:17:45 +02001550
Takashi Iwaia5973102011-09-28 16:43:36 +02001551 if (!spec->stream_analog_capture) {
1552 if (spec->dyn_adc_switch)
1553 spec->stream_analog_capture =
1554 &via_pcm_dyn_adc_analog_capture;
1555 else
1556 spec->stream_analog_capture =
1557 &via_pcm_analog_capture;
1558 }
1559 if (spec->num_adc_nids) {
1560 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1561 *spec->stream_analog_capture;
1562 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1563 spec->adc_nids[0];
1564 if (!spec->dyn_adc_switch)
1565 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
1566 spec->num_adc_nids;
1567 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001568 codec->num_pcms++;
1569 info++;
Takashi Iwaia5973102011-09-28 16:43:36 +02001570 }
1571
1572 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwai82673bc2011-06-17 16:24:21 +02001573 snprintf(spec->stream_name_digital,
1574 sizeof(spec->stream_name_digital),
1575 "%s Digital", codec->chip_name);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001576 info->name = spec->stream_name_digital;
Takashi Iwai7ba72ba2008-02-06 14:03:20 +01001577 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001578 if (spec->multiout.dig_out_nid) {
Takashi Iwai9af74212011-06-18 16:17:45 +02001579 if (!spec->stream_digital_playback)
1580 spec->stream_digital_playback =
1581 &via_pcm_digital_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001582 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
Takashi Iwai9af74212011-06-18 16:17:45 +02001583 *spec->stream_digital_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001584 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1585 spec->multiout.dig_out_nid;
1586 }
1587 if (spec->dig_in_nid) {
Takashi Iwai9af74212011-06-18 16:17:45 +02001588 if (!spec->stream_digital_capture)
1589 spec->stream_digital_capture =
1590 &via_pcm_digital_capture;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001591 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
Takashi Iwai9af74212011-06-18 16:17:45 +02001592 *spec->stream_digital_capture;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001593 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1594 spec->dig_in_nid;
1595 }
Takashi Iwaia5973102011-09-28 16:43:36 +02001596 codec->num_pcms++;
1597 info++;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001598 }
1599
Takashi Iwaiece8d042011-06-19 16:24:21 +02001600 if (spec->hp_dac_nid) {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001601 snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
1602 "%s HP", codec->chip_name);
1603 info->name = spec->stream_name_hp;
1604 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
1605 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
Takashi Iwaiece8d042011-06-19 16:24:21 +02001606 spec->hp_dac_nid;
Takashi Iwaia5973102011-09-28 16:43:36 +02001607 codec->num_pcms++;
1608 info++;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001609 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001610 return 0;
1611}
1612
1613static void via_free(struct hda_codec *codec)
1614{
1615 struct via_spec *spec = codec->spec;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001616
1617 if (!spec)
1618 return;
1619
Takashi Iwai603c4012008-07-30 15:01:44 +02001620 via_free_kctls(codec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001621 vt1708_stop_hp_work(spec);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001622 kfree(spec->bind_cap_vol);
1623 kfree(spec->bind_cap_sw);
1624 kfree(spec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001625}
1626
Takashi Iwai64be2852011-06-17 16:51:39 +02001627/* mute/unmute outputs */
1628static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
1629 hda_nid_t *pins, bool mute)
1630{
1631 int i;
Takashi Iwai94994732011-07-11 11:36:44 +02001632 for (i = 0; i < num_pins; i++) {
1633 unsigned int parm = snd_hda_codec_read(codec, pins[i], 0,
1634 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1635 if (parm & AC_PINCTL_IN_EN)
1636 continue;
1637 if (mute)
1638 parm &= ~AC_PINCTL_OUT_EN;
1639 else
1640 parm |= AC_PINCTL_OUT_EN;
Takashi Iwai64be2852011-06-17 16:51:39 +02001641 snd_hda_codec_write(codec, pins[i], 0,
Takashi Iwai94994732011-07-11 11:36:44 +02001642 AC_VERB_SET_PIN_WIDGET_CONTROL, parm);
1643 }
Takashi Iwai64be2852011-06-17 16:51:39 +02001644}
1645
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001646/* mute internal speaker if line-out is plugged */
1647static void via_line_automute(struct hda_codec *codec, int present)
1648{
1649 struct via_spec *spec = codec->spec;
1650
1651 if (!spec->autocfg.speaker_outs)
1652 return;
1653 if (!present)
1654 present = snd_hda_jack_detect(codec,
1655 spec->autocfg.line_out_pins[0]);
1656 toggle_output_mutes(codec, spec->autocfg.speaker_outs,
1657 spec->autocfg.speaker_pins,
1658 present);
1659}
1660
Harald Welte69e52a82008-09-09 15:57:32 +08001661/* mute internal speaker if HP is plugged */
1662static void via_hp_automute(struct hda_codec *codec)
1663{
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001664 int present = 0;
Takashi Iwai6e969d92011-07-11 11:28:13 +02001665 int nums;
Harald Welte69e52a82008-09-09 15:57:32 +08001666 struct via_spec *spec = codec->spec;
1667
Takashi Iwai187d3332011-11-24 16:33:09 +01001668 if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0] &&
1669 (spec->codec_type != VT1708 || spec->vt1708_jack_detect))
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001670 present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
Takashi Iwai6e969d92011-07-11 11:28:13 +02001671
1672 if (spec->smart51_enabled)
1673 nums = spec->autocfg.line_outs + spec->smart51_nums;
1674 else
1675 nums = spec->autocfg.line_outs;
1676 toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
1677
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001678 via_line_automute(codec, present);
Lydia Wangf3db4232009-10-10 19:08:41 +08001679}
1680
Harald Welte69e52a82008-09-09 15:57:32 +08001681static void via_gpio_control(struct hda_codec *codec)
1682{
1683 unsigned int gpio_data;
1684 unsigned int vol_counter;
1685 unsigned int vol;
1686 unsigned int master_vol;
1687
1688 struct via_spec *spec = codec->spec;
1689
1690 gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
1691 AC_VERB_GET_GPIO_DATA, 0) & 0x03;
1692
1693 vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
1694 0xF84, 0) & 0x3F0000) >> 16;
1695
1696 vol = vol_counter & 0x1F;
1697 master_vol = snd_hda_codec_read(codec, 0x1A, 0,
1698 AC_VERB_GET_AMP_GAIN_MUTE,
1699 AC_AMP_GET_INPUT);
1700
1701 if (gpio_data == 0x02) {
1702 /* unmute line out */
Takashi Iwai3e0693e2011-06-17 16:37:45 +02001703 snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
1704 AC_VERB_SET_PIN_WIDGET_CONTROL,
1705 PIN_OUT);
Harald Welte69e52a82008-09-09 15:57:32 +08001706 if (vol_counter & 0x20) {
1707 /* decrease volume */
1708 if (vol > master_vol)
1709 vol = master_vol;
1710 snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
1711 0, HDA_AMP_VOLMASK,
1712 master_vol-vol);
1713 } else {
1714 /* increase volume */
1715 snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
1716 HDA_AMP_VOLMASK,
1717 ((master_vol+vol) > 0x2A) ? 0x2A :
1718 (master_vol+vol));
1719 }
1720 } else if (!(gpio_data & 0x02)) {
1721 /* mute line out */
Takashi Iwai3e0693e2011-06-17 16:37:45 +02001722 snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
1723 AC_VERB_SET_PIN_WIDGET_CONTROL,
1724 0);
Harald Welte69e52a82008-09-09 15:57:32 +08001725 }
1726}
1727
1728/* unsolicited event for jack sensing */
1729static void via_unsol_event(struct hda_codec *codec,
1730 unsigned int res)
1731{
1732 res >>= 26;
Takashi Iwai3a938972011-10-28 01:16:55 +02001733 res = snd_hda_jack_get_action(codec, res);
Lydia Wangec7e7e42011-03-24 12:43:44 +08001734
Lydia Wanga34df192009-10-10 19:08:01 +08001735 if (res & VIA_JACK_EVENT)
Lydia Wang3e95b9a2011-03-23 15:13:28 +08001736 set_widgets_power_state(codec);
Lydia Wangec7e7e42011-03-24 12:43:44 +08001737
1738 res &= ~VIA_JACK_EVENT;
1739
Takashi Iwai21ce0b62011-07-11 10:33:47 +02001740 if (res == VIA_HP_EVENT || res == VIA_LINE_EVENT)
Lydia Wangec7e7e42011-03-24 12:43:44 +08001741 via_hp_automute(codec);
1742 else if (res == VIA_GPIO_EVENT)
1743 via_gpio_control(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001744 snd_hda_jack_report_sync(codec);
Harald Welte69e52a82008-09-09 15:57:32 +08001745}
1746
Takashi Iwai2a439522011-07-26 09:52:50 +02001747#ifdef CONFIG_PM
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001748static int via_suspend(struct hda_codec *codec, pm_message_t state)
1749{
1750 struct via_spec *spec = codec->spec;
1751 vt1708_stop_hp_work(spec);
1752 return 0;
1753}
1754#endif
1755
Takashi Iwaicb53c622007-08-10 17:21:45 +02001756#ifdef CONFIG_SND_HDA_POWER_SAVE
1757static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1758{
1759 struct via_spec *spec = codec->spec;
1760 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1761}
1762#endif
1763
Joseph Chanc577b8a2006-11-29 15:29:40 +01001764/*
1765 */
Takashi Iwai5d417622011-06-20 11:32:27 +02001766
1767static int via_init(struct hda_codec *codec);
1768
Takashi Iwai90dd48a2011-05-02 12:38:19 +02001769static const struct hda_codec_ops via_patch_ops = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001770 .build_controls = via_build_controls,
1771 .build_pcms = via_build_pcms,
1772 .init = via_init,
1773 .free = via_free,
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001774 .unsol_event = via_unsol_event,
Takashi Iwai2a439522011-07-26 09:52:50 +02001775#ifdef CONFIG_PM
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001776 .suspend = via_suspend,
1777#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02001778#ifdef CONFIG_SND_HDA_POWER_SAVE
1779 .check_power_status = via_check_power_status,
1780#endif
Joseph Chanc577b8a2006-11-29 15:29:40 +01001781};
1782
Takashi Iwai4a796162011-06-17 17:53:38 +02001783static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
Joseph Chanc577b8a2006-11-29 15:29:40 +01001784{
Takashi Iwai4a796162011-06-17 17:53:38 +02001785 struct via_spec *spec = codec->spec;
1786 int i;
1787
1788 for (i = 0; i < spec->multiout.num_dacs; i++) {
1789 if (spec->multiout.dac_nids[i] == dac)
1790 return false;
1791 }
Takashi Iwaiece8d042011-06-19 16:24:21 +02001792 if (spec->hp_dac_nid == dac)
Takashi Iwai4a796162011-06-17 17:53:38 +02001793 return false;
1794 return true;
1795}
1796
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001797static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3214b962011-07-18 12:49:25 +02001798 hda_nid_t target_dac, int with_aa_mix,
1799 struct nid_path *path, int depth)
Takashi Iwai4a796162011-06-17 17:53:38 +02001800{
Takashi Iwai3214b962011-07-18 12:49:25 +02001801 struct via_spec *spec = codec->spec;
Takashi Iwai4a796162011-06-17 17:53:38 +02001802 hda_nid_t conn[8];
1803 int i, nums;
1804
Takashi Iwai3214b962011-07-18 12:49:25 +02001805 if (nid == spec->aa_mix_nid) {
1806 if (!with_aa_mix)
1807 return false;
1808 with_aa_mix = 2; /* mark aa-mix is included */
1809 }
1810
Takashi Iwai4a796162011-06-17 17:53:38 +02001811 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
1812 for (i = 0; i < nums; i++) {
1813 if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
1814 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02001815 if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
1816 /* aa-mix is requested but not included? */
1817 if (!(spec->aa_mix_nid && with_aa_mix == 1))
1818 goto found;
1819 }
Takashi Iwai4a796162011-06-17 17:53:38 +02001820 }
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001821 if (depth >= MAX_NID_PATH_DEPTH)
Takashi Iwai4a796162011-06-17 17:53:38 +02001822 return false;
1823 for (i = 0; i < nums; i++) {
1824 unsigned int type;
1825 type = get_wcaps_type(get_wcaps(codec, conn[i]));
Takashi Iwai3214b962011-07-18 12:49:25 +02001826 if (type == AC_WID_AUD_OUT)
Takashi Iwai4a796162011-06-17 17:53:38 +02001827 continue;
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001828 if (__parse_output_path(codec, conn[i], target_dac,
Takashi Iwai3214b962011-07-18 12:49:25 +02001829 with_aa_mix, path, depth + 1))
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001830 goto found;
Takashi Iwai4a796162011-06-17 17:53:38 +02001831 }
1832 return false;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001833
1834 found:
1835 path->path[path->depth] = conn[i];
1836 path->idx[path->depth] = i;
1837 if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
1838 path->multi[path->depth] = 1;
1839 path->depth++;
1840 return true;
Takashi Iwai4a796162011-06-17 17:53:38 +02001841}
1842
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001843static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3214b962011-07-18 12:49:25 +02001844 hda_nid_t target_dac, int with_aa_mix,
1845 struct nid_path *path)
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001846{
Takashi Iwai3214b962011-07-18 12:49:25 +02001847 if (__parse_output_path(codec, nid, target_dac, with_aa_mix, path, 1)) {
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001848 path->path[path->depth] = nid;
1849 path->depth++;
Takashi Iwai3214b962011-07-18 12:49:25 +02001850 snd_printdd("output-path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
1851 path->depth, path->path[0], path->path[1],
1852 path->path[2], path->path[3], path->path[4]);
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001853 return true;
1854 }
1855 return false;
1856}
1857
Takashi Iwai4a796162011-06-17 17:53:38 +02001858static int via_auto_fill_dac_nids(struct hda_codec *codec)
1859{
1860 struct via_spec *spec = codec->spec;
1861 const struct auto_pin_cfg *cfg = &spec->autocfg;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001862 int i, dac_num;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001863 hda_nid_t nid;
1864
Joseph Chanc577b8a2006-11-29 15:29:40 +01001865 spec->multiout.dac_nids = spec->private_dac_nids;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001866 dac_num = 0;
Takashi Iwai4a796162011-06-17 17:53:38 +02001867 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai3214b962011-07-18 12:49:25 +02001868 hda_nid_t dac = 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001869 nid = cfg->line_out_pins[i];
Takashi Iwai4a796162011-06-17 17:53:38 +02001870 if (!nid)
1871 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02001872 if (parse_output_path(codec, nid, 0, 0, &spec->out_path[i]))
1873 dac = spec->out_path[i].path[0];
1874 if (!i && parse_output_path(codec, nid, dac, 1,
1875 &spec->out_mix_path))
1876 dac = spec->out_mix_path.path[0];
1877 if (dac) {
1878 spec->private_dac_nids[i] = dac;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001879 dac_num++;
1880 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001881 }
Takashi Iwai3214b962011-07-18 12:49:25 +02001882 if (!spec->out_path[0].depth && spec->out_mix_path.depth) {
1883 spec->out_path[0] = spec->out_mix_path;
1884 spec->out_mix_path.depth = 0;
1885 }
Lydia Wang5c9a5612011-07-08 14:03:43 +08001886 spec->multiout.num_dacs = dac_num;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001887 return 0;
1888}
1889
Takashi Iwai4a796162011-06-17 17:53:38 +02001890static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001891 int chs, bool check_dac, struct nid_path *path)
Joseph Chanc577b8a2006-11-29 15:29:40 +01001892{
Takashi Iwai4a796162011-06-17 17:53:38 +02001893 struct via_spec *spec = codec->spec;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001894 char name[32];
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001895 hda_nid_t dac, pin, sel, nid;
1896 int err;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001897
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001898 dac = check_dac ? path->path[0] : 0;
1899 pin = path->path[path->depth - 1];
1900 sel = path->depth > 1 ? path->path[1] : 0;
Takashi Iwai4a796162011-06-17 17:53:38 +02001901
Takashi Iwai8df2a312011-06-21 11:48:29 +02001902 if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
Takashi Iwai4a796162011-06-17 17:53:38 +02001903 nid = dac;
Takashi Iwai8df2a312011-06-21 11:48:29 +02001904 else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
Takashi Iwai4a796162011-06-17 17:53:38 +02001905 nid = pin;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001906 else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1907 nid = sel;
Takashi Iwai4a796162011-06-17 17:53:38 +02001908 else
1909 nid = 0;
1910 if (nid) {
1911 sprintf(name, "%s Playback Volume", pfx);
1912 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
Lydia Wanga00a5fa2011-06-21 16:11:11 +08001913 HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
Takashi Iwai4a796162011-06-17 17:53:38 +02001914 if (err < 0)
1915 return err;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001916 path->vol_ctl = nid;
Takashi Iwai4a796162011-06-17 17:53:38 +02001917 }
1918
Takashi Iwai8df2a312011-06-21 11:48:29 +02001919 if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
Takashi Iwai4a796162011-06-17 17:53:38 +02001920 nid = dac;
Takashi Iwai8df2a312011-06-21 11:48:29 +02001921 else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
Takashi Iwai4a796162011-06-17 17:53:38 +02001922 nid = pin;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001923 else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1924 nid = sel;
Takashi Iwai4a796162011-06-17 17:53:38 +02001925 else
1926 nid = 0;
1927 if (nid) {
1928 sprintf(name, "%s Playback Switch", pfx);
1929 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1930 HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1931 if (err < 0)
1932 return err;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001933 path->mute_ctl = nid;
Takashi Iwai4a796162011-06-17 17:53:38 +02001934 }
1935 return 0;
1936}
1937
Takashi Iwaif4a78282011-06-17 18:46:48 +02001938static void mangle_smart51(struct hda_codec *codec)
1939{
1940 struct via_spec *spec = codec->spec;
1941 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001942 struct auto_pin_cfg_item *ins = cfg->inputs;
1943 int i, j, nums, attr;
1944 int pins[AUTO_CFG_MAX_INS];
Takashi Iwaif4a78282011-06-17 18:46:48 +02001945
Takashi Iwai0f98c242011-06-21 12:51:33 +02001946 for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
1947 nums = 0;
1948 for (i = 0; i < cfg->num_inputs; i++) {
1949 unsigned int def;
1950 if (ins[i].type > AUTO_PIN_LINE_IN)
1951 continue;
1952 def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
1953 if (snd_hda_get_input_pin_attr(def) != attr)
1954 continue;
1955 for (j = 0; j < nums; j++)
1956 if (ins[pins[j]].type < ins[i].type) {
1957 memmove(pins + j + 1, pins + j,
Takashi Iwai21d45d22011-07-08 11:35:11 +02001958 (nums - j) * sizeof(int));
Takashi Iwai0f98c242011-06-21 12:51:33 +02001959 break;
1960 }
1961 pins[j] = i;
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001962 nums++;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001963 }
1964 if (cfg->line_outs + nums < 3)
Takashi Iwaif4a78282011-06-17 18:46:48 +02001965 continue;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001966 for (i = 0; i < nums; i++) {
1967 hda_nid_t pin = ins[pins[i]].pin;
1968 spec->smart51_pins[spec->smart51_nums++] = pin;
1969 cfg->line_out_pins[cfg->line_outs++] = pin;
1970 if (cfg->line_outs == 3)
1971 break;
1972 }
1973 return;
Takashi Iwaif4a78282011-06-17 18:46:48 +02001974 }
1975}
1976
Takashi Iwai020066d2011-07-21 13:45:56 +02001977static void copy_path_mixer_ctls(struct nid_path *dst, struct nid_path *src)
1978{
1979 dst->vol_ctl = src->vol_ctl;
1980 dst->mute_ctl = src->mute_ctl;
1981}
1982
Takashi Iwai4a796162011-06-17 17:53:38 +02001983/* add playback controls from the parsed DAC table */
1984static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
1985{
1986 struct via_spec *spec = codec->spec;
Takashi Iwaif4a78282011-06-17 18:46:48 +02001987 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai3214b962011-07-18 12:49:25 +02001988 struct nid_path *path;
Takashi Iwaiea734962011-01-17 11:29:34 +01001989 static const char * const chname[4] = {
1990 "Front", "Surround", "C/LFE", "Side"
1991 };
Takashi Iwai4a796162011-06-17 17:53:38 +02001992 int i, idx, err;
Takashi Iwaif4a78282011-06-17 18:46:48 +02001993 int old_line_outs;
1994
1995 /* check smart51 */
1996 old_line_outs = cfg->line_outs;
1997 if (cfg->line_outs == 1)
1998 mangle_smart51(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001999
Takashi Iwaie3d7a142011-06-20 13:52:33 +02002000 err = via_auto_fill_dac_nids(codec);
2001 if (err < 0)
2002 return err;
2003
Lydia Wang5c9a5612011-07-08 14:03:43 +08002004 if (spec->multiout.num_dacs < 3) {
2005 spec->smart51_nums = 0;
2006 cfg->line_outs = old_line_outs;
2007 }
Takashi Iwai4a796162011-06-17 17:53:38 +02002008 for (i = 0; i < cfg->line_outs; i++) {
2009 hda_nid_t pin, dac;
2010 pin = cfg->line_out_pins[i];
2011 dac = spec->multiout.dac_nids[i];
2012 if (!pin || !dac)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002013 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02002014 path = spec->out_path + i;
Takashi Iwai0fe0adf2011-06-19 16:27:53 +02002015 if (i == HDA_CLFE) {
Takashi Iwai3214b962011-07-18 12:49:25 +02002016 err = create_ch_ctls(codec, "Center", 1, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002017 if (err < 0)
2018 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02002019 err = create_ch_ctls(codec, "LFE", 2, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002020 if (err < 0)
2021 return err;
2022 } else {
Takashi Iwai6aadf412011-06-20 14:09:02 +02002023 const char *pfx = chname[i];
2024 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2025 cfg->line_outs == 1)
2026 pfx = "Speaker";
Takashi Iwai3214b962011-07-18 12:49:25 +02002027 err = create_ch_ctls(codec, pfx, 3, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002028 if (err < 0)
2029 return err;
2030 }
Takashi Iwai020066d2011-07-21 13:45:56 +02002031 if (path != spec->out_path + i)
2032 copy_path_mixer_ctls(&spec->out_path[i], path);
2033 if (path == spec->out_path && spec->out_mix_path.depth)
2034 copy_path_mixer_ctls(&spec->out_mix_path, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002035 }
2036
Takashi Iwai4a796162011-06-17 17:53:38 +02002037 idx = get_connection_index(codec, spec->aa_mix_nid,
2038 spec->multiout.dac_nids[0]);
2039 if (idx >= 0) {
2040 /* add control to mixer */
Takashi Iwai3214b962011-07-18 12:49:25 +02002041 const char *name;
2042 name = spec->out_mix_path.depth ?
2043 "PCM Loopback Playback Volume" : "PCM Playback Volume";
2044 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
Takashi Iwai4a796162011-06-17 17:53:38 +02002045 HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2046 idx, HDA_INPUT));
2047 if (err < 0)
2048 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02002049 name = spec->out_mix_path.depth ?
2050 "PCM Loopback Playback Switch" : "PCM Playback Switch";
2051 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
Takashi Iwai4a796162011-06-17 17:53:38 +02002052 HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2053 idx, HDA_INPUT));
2054 if (err < 0)
2055 return err;
2056 }
2057
Takashi Iwaif4a78282011-06-17 18:46:48 +02002058 cfg->line_outs = old_line_outs;
2059
Joseph Chanc577b8a2006-11-29 15:29:40 +01002060 return 0;
2061}
2062
Takashi Iwai4a796162011-06-17 17:53:38 +02002063static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002064{
Takashi Iwai4a796162011-06-17 17:53:38 +02002065 struct via_spec *spec = codec->spec;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02002066 struct nid_path *path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002067 bool check_dac;
Takashi Iwai3214b962011-07-18 12:49:25 +02002068 int i, err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002069
2070 if (!pin)
2071 return 0;
2072
Takashi Iwai3214b962011-07-18 12:49:25 +02002073 if (!parse_output_path(codec, pin, 0, 0, &spec->hp_indep_path)) {
2074 for (i = HDA_SIDE; i >= HDA_CLFE; i--) {
2075 if (i < spec->multiout.num_dacs &&
2076 parse_output_path(codec, pin,
2077 spec->multiout.dac_nids[i], 0,
2078 &spec->hp_indep_path)) {
2079 spec->hp_indep_shared = i;
2080 break;
2081 }
2082 }
Takashi Iwai25250502011-06-30 17:24:47 +02002083 }
Takashi Iwai3214b962011-07-18 12:49:25 +02002084 if (spec->hp_indep_path.depth) {
2085 spec->hp_dac_nid = spec->hp_indep_path.path[0];
2086 if (!spec->hp_indep_shared)
2087 spec->hp_path = spec->hp_indep_path;
2088 }
2089 /* optionally check front-path w/o AA-mix */
2090 if (!spec->hp_path.depth)
2091 parse_output_path(codec, pin,
2092 spec->multiout.dac_nids[HDA_FRONT], 0,
2093 &spec->hp_path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002094
Takashi Iwaiece8d042011-06-19 16:24:21 +02002095 if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
Takashi Iwai3214b962011-07-18 12:49:25 +02002096 1, &spec->hp_mix_path) && !spec->hp_path.depth)
Takashi Iwaiece8d042011-06-19 16:24:21 +02002097 return 0;
2098
Takashi Iwai3214b962011-07-18 12:49:25 +02002099 if (spec->hp_path.depth) {
Takashi Iwai09a9ad692011-06-21 15:57:44 +02002100 path = &spec->hp_path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002101 check_dac = true;
2102 } else {
Takashi Iwai3214b962011-07-18 12:49:25 +02002103 path = &spec->hp_mix_path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002104 check_dac = false;
2105 }
2106 err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002107 if (err < 0)
2108 return err;
Takashi Iwai020066d2011-07-21 13:45:56 +02002109 if (check_dac)
2110 copy_path_mixer_ctls(&spec->hp_mix_path, path);
2111 else
2112 copy_path_mixer_ctls(&spec->hp_path, path);
2113 if (spec->hp_indep_path.depth)
2114 copy_path_mixer_ctls(&spec->hp_indep_path, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002115 return 0;
2116}
2117
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002118static int via_auto_create_speaker_ctls(struct hda_codec *codec)
2119{
2120 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +02002121 struct nid_path *path;
2122 bool check_dac;
Wang Shaoyan81c0a782011-08-05 18:51:29 +08002123 hda_nid_t pin, dac = 0;
Takashi Iwai3214b962011-07-18 12:49:25 +02002124 int err;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002125
2126 pin = spec->autocfg.speaker_pins[0];
2127 if (!spec->autocfg.speaker_outs || !pin)
2128 return 0;
2129
Takashi Iwai3214b962011-07-18 12:49:25 +02002130 if (parse_output_path(codec, pin, 0, 0, &spec->speaker_path))
Takashi Iwai8e3679d2011-06-21 09:01:36 +02002131 dac = spec->speaker_path.path[0];
Takashi Iwai3214b962011-07-18 12:49:25 +02002132 if (!dac)
2133 parse_output_path(codec, pin,
2134 spec->multiout.dac_nids[HDA_FRONT], 0,
2135 &spec->speaker_path);
2136 if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
2137 1, &spec->speaker_mix_path) && !dac)
2138 return 0;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002139
Takashi Iwai3214b962011-07-18 12:49:25 +02002140 /* no AA-path for front? */
2141 if (!spec->out_mix_path.depth && spec->speaker_mix_path.depth)
2142 dac = 0;
2143
2144 spec->speaker_dac_nid = dac;
2145 spec->multiout.extra_out_nid[0] = dac;
2146 if (dac) {
2147 path = &spec->speaker_path;
2148 check_dac = true;
2149 } else {
2150 path = &spec->speaker_mix_path;
2151 check_dac = false;
2152 }
2153 err = create_ch_ctls(codec, "Speaker", 3, check_dac, path);
2154 if (err < 0)
2155 return err;
Takashi Iwai020066d2011-07-21 13:45:56 +02002156 if (check_dac)
2157 copy_path_mixer_ctls(&spec->speaker_mix_path, path);
2158 else
2159 copy_path_mixer_ctls(&spec->speaker_path, path);
Takashi Iwai3214b962011-07-18 12:49:25 +02002160 return 0;
2161}
2162
2163#define via_aamix_ctl_info via_pin_power_ctl_info
2164
2165static int via_aamix_ctl_get(struct snd_kcontrol *kcontrol,
2166 struct snd_ctl_elem_value *ucontrol)
2167{
2168 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2169 struct via_spec *spec = codec->spec;
2170 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2171 return 0;
2172}
2173
2174static void update_aamix_paths(struct hda_codec *codec, int do_mix,
2175 struct nid_path *nomix, struct nid_path *mix)
2176{
2177 if (do_mix) {
2178 activate_output_path(codec, nomix, false, false);
2179 activate_output_path(codec, mix, true, false);
2180 } else {
2181 activate_output_path(codec, mix, false, false);
2182 activate_output_path(codec, nomix, true, false);
2183 }
2184}
2185
2186static int via_aamix_ctl_put(struct snd_kcontrol *kcontrol,
2187 struct snd_ctl_elem_value *ucontrol)
2188{
2189 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2190 struct via_spec *spec = codec->spec;
2191 unsigned int val = ucontrol->value.enumerated.item[0];
2192
2193 if (val == spec->aamix_mode)
2194 return 0;
2195 spec->aamix_mode = val;
2196 /* update front path */
2197 update_aamix_paths(codec, val, &spec->out_path[0], &spec->out_mix_path);
2198 /* update HP path */
2199 if (!spec->hp_independent_mode) {
2200 update_aamix_paths(codec, val, &spec->hp_path,
2201 &spec->hp_mix_path);
2202 }
2203 /* update speaker path */
2204 update_aamix_paths(codec, val, &spec->speaker_path,
2205 &spec->speaker_mix_path);
2206 return 1;
2207}
2208
2209static const struct snd_kcontrol_new via_aamix_ctl_enum = {
2210 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2211 .name = "Loopback Mixing",
2212 .info = via_aamix_ctl_info,
2213 .get = via_aamix_ctl_get,
2214 .put = via_aamix_ctl_put,
2215};
2216
2217static int via_auto_create_loopback_switch(struct hda_codec *codec)
2218{
2219 struct via_spec *spec = codec->spec;
2220
Takashi Iwai4808d122012-01-10 15:16:02 +01002221 if (!spec->aa_mix_nid)
2222 return 0; /* no loopback switching available */
2223 if (!(spec->out_mix_path.depth || spec->hp_mix_path.depth ||
2224 spec->speaker_path.depth))
Takashi Iwai3214b962011-07-18 12:49:25 +02002225 return 0; /* no loopback switching available */
2226 if (!via_clone_control(spec, &via_aamix_ctl_enum))
2227 return -ENOMEM;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002228 return 0;
2229}
2230
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002231/* look for ADCs */
2232static int via_fill_adcs(struct hda_codec *codec)
2233{
2234 struct via_spec *spec = codec->spec;
2235 hda_nid_t nid = codec->start_nid;
2236 int i;
2237
2238 for (i = 0; i < codec->num_nodes; i++, nid++) {
2239 unsigned int wcaps = get_wcaps(codec, nid);
2240 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2241 continue;
2242 if (wcaps & AC_WCAP_DIGITAL)
2243 continue;
2244 if (!(wcaps & AC_WCAP_CONN_LIST))
2245 continue;
2246 if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2247 return -ENOMEM;
2248 spec->adc_nids[spec->num_adc_nids++] = nid;
2249 }
2250 return 0;
2251}
2252
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002253/* input-src control */
2254static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
2255 struct snd_ctl_elem_info *uinfo)
2256{
2257 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2258 struct via_spec *spec = codec->spec;
2259
2260 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2261 uinfo->count = 1;
2262 uinfo->value.enumerated.items = spec->num_inputs;
2263 if (uinfo->value.enumerated.item >= spec->num_inputs)
2264 uinfo->value.enumerated.item = spec->num_inputs - 1;
2265 strcpy(uinfo->value.enumerated.name,
2266 spec->inputs[uinfo->value.enumerated.item].label);
2267 return 0;
2268}
2269
2270static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
2271 struct snd_ctl_elem_value *ucontrol)
2272{
2273 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2274 struct via_spec *spec = codec->spec;
2275 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2276
2277 ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
2278 return 0;
2279}
2280
2281static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
2282 struct snd_ctl_elem_value *ucontrol)
2283{
2284 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2285 struct via_spec *spec = codec->spec;
2286 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2287 hda_nid_t mux;
2288 int cur;
2289
2290 cur = ucontrol->value.enumerated.item[0];
2291 if (cur < 0 || cur >= spec->num_inputs)
2292 return -EINVAL;
2293 if (spec->cur_mux[idx] == cur)
2294 return 0;
2295 spec->cur_mux[idx] = cur;
2296 if (spec->dyn_adc_switch) {
2297 int adc_idx = spec->inputs[cur].adc_idx;
2298 mux = spec->mux_nids[adc_idx];
2299 via_dyn_adc_pcm_resetup(codec, cur);
2300 } else {
2301 mux = spec->mux_nids[idx];
2302 if (snd_BUG_ON(!mux))
2303 return -EINVAL;
2304 }
2305
2306 if (mux) {
2307 /* switch to D0 beofre change index */
Takashi Iwai054d8672012-01-24 12:25:50 +01002308 update_power_state(codec, mux, AC_PWRST_D0);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002309 snd_hda_codec_write(codec, mux, 0,
2310 AC_VERB_SET_CONNECT_SEL,
2311 spec->inputs[cur].mux_idx);
2312 }
2313
2314 /* update jack power state */
2315 set_widgets_power_state(codec);
2316 return 0;
2317}
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002318
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002319static const struct snd_kcontrol_new via_input_src_ctl = {
2320 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2321 /* The multiple "Capture Source" controls confuse alsamixer
2322 * So call somewhat different..
2323 */
2324 /* .name = "Capture Source", */
2325 .name = "Input Source",
2326 .info = via_mux_enum_info,
2327 .get = via_mux_enum_get,
2328 .put = via_mux_enum_put,
2329};
2330
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002331static int create_input_src_ctls(struct hda_codec *codec, int count)
2332{
2333 struct via_spec *spec = codec->spec;
2334 struct snd_kcontrol_new *knew;
2335
2336 if (spec->num_inputs <= 1 || !count)
2337 return 0; /* no need for single src */
2338
2339 knew = via_clone_control(spec, &via_input_src_ctl);
2340 if (!knew)
2341 return -ENOMEM;
2342 knew->count = count;
2343 return 0;
2344}
2345
2346/* add the powersave loopback-list entry */
Takashi Iwai13af8e72011-06-20 14:05:46 +02002347static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
2348{
2349 struct hda_amp_list *list;
2350
2351 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2352 return;
2353 list = spec->loopback_list + spec->num_loopbacks;
2354 list->nid = mix;
2355 list->dir = HDA_INPUT;
2356 list->idx = idx;
2357 spec->num_loopbacks++;
2358 spec->loopback.amplist = spec->loopback_list;
2359}
Takashi Iwai13af8e72011-06-20 14:05:46 +02002360
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002361static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
Takashi Iwai8d087c72011-06-28 12:45:47 +02002362 hda_nid_t dst)
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002363{
Takashi Iwai8d087c72011-06-28 12:45:47 +02002364 return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002365}
2366
2367/* add the input-route to the given pin */
2368static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002369{
Takashi Iwai10a20af2010-09-09 16:28:02 +02002370 struct via_spec *spec = codec->spec;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002371 int c, idx;
2372
2373 spec->inputs[spec->num_inputs].adc_idx = -1;
2374 spec->inputs[spec->num_inputs].pin = pin;
2375 for (c = 0; c < spec->num_adc_nids; c++) {
2376 if (spec->mux_nids[c]) {
2377 idx = get_connection_index(codec, spec->mux_nids[c],
2378 pin);
2379 if (idx < 0)
2380 continue;
2381 spec->inputs[spec->num_inputs].mux_idx = idx;
2382 } else {
Takashi Iwai8d087c72011-06-28 12:45:47 +02002383 if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002384 continue;
2385 }
2386 spec->inputs[spec->num_inputs].adc_idx = c;
2387 /* Can primary ADC satisfy all inputs? */
2388 if (!spec->dyn_adc_switch &&
2389 spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
2390 snd_printd(KERN_INFO
2391 "via: dynamic ADC switching enabled\n");
2392 spec->dyn_adc_switch = 1;
2393 }
2394 return true;
2395 }
2396 return false;
2397}
2398
2399static int get_mux_nids(struct hda_codec *codec);
2400
2401/* parse input-routes; fill ADCs, MUXs and input-src entries */
2402static int parse_analog_inputs(struct hda_codec *codec)
2403{
2404 struct via_spec *spec = codec->spec;
2405 const struct auto_pin_cfg *cfg = &spec->autocfg;
2406 int i, err;
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002407
2408 err = via_fill_adcs(codec);
2409 if (err < 0)
2410 return err;
2411 err = get_mux_nids(codec);
2412 if (err < 0)
2413 return err;
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002414
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002415 /* fill all input-routes */
2416 for (i = 0; i < cfg->num_inputs; i++) {
2417 if (add_input_route(codec, cfg->inputs[i].pin))
2418 spec->inputs[spec->num_inputs++].label =
2419 hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwaif3268512010-08-30 11:00:19 +02002420 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002421
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002422 /* check for internal loopback recording */
2423 if (spec->aa_mix_nid &&
2424 add_input_route(codec, spec->aa_mix_nid))
2425 spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
2426
2427 return 0;
2428}
2429
2430/* create analog-loopback volume/switch controls */
2431static int create_loopback_ctls(struct hda_codec *codec)
2432{
2433 struct via_spec *spec = codec->spec;
2434 const struct auto_pin_cfg *cfg = &spec->autocfg;
2435 const char *prev_label = NULL;
2436 int type_idx = 0;
2437 int i, j, err, idx;
2438
2439 if (!spec->aa_mix_nid)
2440 return 0;
2441
Takashi Iwai7b315bb2010-08-30 13:06:30 +02002442 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002443 hda_nid_t pin = cfg->inputs[i].pin;
2444 const char *label = hda_get_autocfg_input_label(codec, cfg, i);
2445
Takashi Iwai1e11cae2011-06-21 12:57:22 +02002446 if (prev_label && !strcmp(label, prev_label))
Takashi Iwai7b315bb2010-08-30 13:06:30 +02002447 type_idx++;
2448 else
2449 type_idx = 0;
Takashi Iwai1e11cae2011-06-21 12:57:22 +02002450 prev_label = label;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002451 idx = get_connection_index(codec, spec->aa_mix_nid, pin);
2452 if (idx >= 0) {
Lydia Wang16922282011-03-22 16:24:10 +08002453 err = via_new_analog_input(spec, label, type_idx,
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002454 idx, spec->aa_mix_nid);
Takashi Iwai13af8e72011-06-20 14:05:46 +02002455 if (err < 0)
2456 return err;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002457 add_loopback_list(spec, spec->aa_mix_nid, idx);
Takashi Iwai13af8e72011-06-20 14:05:46 +02002458 }
Takashi Iwaie3d7a142011-06-20 13:52:33 +02002459
2460 /* remember the label for smart51 control */
2461 for (j = 0; j < spec->smart51_nums; j++) {
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002462 if (spec->smart51_pins[j] == pin) {
Takashi Iwaie3d7a142011-06-20 13:52:33 +02002463 spec->smart51_idxs[j] = idx;
2464 spec->smart51_labels[j] = label;
2465 break;
2466 }
2467 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002468 }
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002469 return 0;
2470}
2471
2472/* create mic-boost controls (if present) */
2473static int create_mic_boost_ctls(struct hda_codec *codec)
2474{
2475 struct via_spec *spec = codec->spec;
2476 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai8d8bbc62012-02-22 12:26:38 +01002477 const char *prev_label = NULL;
2478 int type_idx = 0;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002479 int i, err;
2480
2481 for (i = 0; i < cfg->num_inputs; i++) {
2482 hda_nid_t pin = cfg->inputs[i].pin;
2483 unsigned int caps;
2484 const char *label;
2485 char name[32];
2486
2487 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2488 continue;
2489 caps = query_amp_caps(codec, pin, HDA_INPUT);
2490 if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2491 continue;
2492 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai8d8bbc62012-02-22 12:26:38 +01002493 if (prev_label && !strcmp(label, prev_label))
2494 type_idx++;
2495 else
2496 type_idx = 0;
2497 prev_label = label;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002498 snprintf(name, sizeof(name), "%s Boost Volume", label);
Takashi Iwai8d8bbc62012-02-22 12:26:38 +01002499 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002500 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2501 if (err < 0)
2502 return err;
2503 }
2504 return 0;
2505}
2506
2507/* create capture and input-src controls for multiple streams */
2508static int create_multi_adc_ctls(struct hda_codec *codec)
2509{
2510 struct via_spec *spec = codec->spec;
2511 int i, err;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002512
2513 /* create capture mixer elements */
2514 for (i = 0; i < spec->num_adc_nids; i++) {
2515 hda_nid_t adc = spec->adc_nids[i];
2516 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2517 "Capture Volume", i,
2518 HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2519 HDA_INPUT));
2520 if (err < 0)
2521 return err;
2522 err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2523 "Capture Switch", i,
2524 HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2525 HDA_INPUT));
2526 if (err < 0)
2527 return err;
2528 }
2529
2530 /* input-source control */
2531 for (i = 0; i < spec->num_adc_nids; i++)
2532 if (!spec->mux_nids[i])
2533 break;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002534 err = create_input_src_ctls(codec, i);
2535 if (err < 0)
2536 return err;
2537 return 0;
2538}
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002539
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002540/* bind capture volume/switch */
2541static struct snd_kcontrol_new via_bind_cap_vol_ctl =
2542 HDA_BIND_VOL("Capture Volume", 0);
2543static struct snd_kcontrol_new via_bind_cap_sw_ctl =
2544 HDA_BIND_SW("Capture Switch", 0);
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002545
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002546static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
2547 struct hda_ctl_ops *ops)
2548{
2549 struct hda_bind_ctls *ctl;
2550 int i;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002551
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002552 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
2553 if (!ctl)
2554 return -ENOMEM;
2555 ctl->ops = ops;
2556 for (i = 0; i < spec->num_adc_nids; i++)
2557 ctl->values[i] =
2558 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
2559 *ctl_ret = ctl;
2560 return 0;
2561}
2562
2563/* create capture and input-src controls for dynamic ADC-switch case */
2564static int create_dyn_adc_ctls(struct hda_codec *codec)
2565{
2566 struct via_spec *spec = codec->spec;
2567 struct snd_kcontrol_new *knew;
2568 int err;
2569
2570 /* set up the bind capture ctls */
2571 err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
2572 if (err < 0)
2573 return err;
2574 err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
2575 if (err < 0)
2576 return err;
2577
2578 /* create capture mixer elements */
2579 knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
2580 if (!knew)
2581 return -ENOMEM;
2582 knew->private_value = (long)spec->bind_cap_vol;
2583
2584 knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
2585 if (!knew)
2586 return -ENOMEM;
2587 knew->private_value = (long)spec->bind_cap_sw;
2588
2589 /* input-source control */
2590 err = create_input_src_ctls(codec, 1);
2591 if (err < 0)
2592 return err;
2593 return 0;
2594}
2595
2596/* parse and create capture-related stuff */
2597static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
2598{
2599 struct via_spec *spec = codec->spec;
2600 int err;
2601
2602 err = parse_analog_inputs(codec);
2603 if (err < 0)
2604 return err;
2605 if (spec->dyn_adc_switch)
2606 err = create_dyn_adc_ctls(codec);
2607 else
2608 err = create_multi_adc_ctls(codec);
2609 if (err < 0)
2610 return err;
2611 err = create_loopback_ctls(codec);
2612 if (err < 0)
2613 return err;
2614 err = create_mic_boost_ctls(codec);
2615 if (err < 0)
2616 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002617 return 0;
2618}
2619
Harald Welte76d9b0d2008-09-09 15:50:37 +08002620static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
2621{
2622 unsigned int def_conf;
2623 unsigned char seqassoc;
2624
Takashi Iwai2f334f92009-02-20 14:37:42 +01002625 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Harald Welte76d9b0d2008-09-09 15:50:37 +08002626 seqassoc = (unsigned char) get_defcfg_association(def_conf);
2627 seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
Lydia Wang82ef9e42009-10-10 19:08:19 +08002628 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
2629 && (seqassoc == 0xf0 || seqassoc == 0xff)) {
2630 def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
2631 snd_hda_codec_set_pincfg(codec, nid, def_conf);
Harald Welte76d9b0d2008-09-09 15:50:37 +08002632 }
2633
2634 return;
2635}
2636
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002637static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002638 struct snd_ctl_elem_value *ucontrol)
2639{
2640 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2641 struct via_spec *spec = codec->spec;
2642
2643 if (spec->codec_type != VT1708)
2644 return 0;
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002645 ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002646 return 0;
2647}
2648
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002649static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002650 struct snd_ctl_elem_value *ucontrol)
2651{
2652 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2653 struct via_spec *spec = codec->spec;
Takashi Iwai187d3332011-11-24 16:33:09 +01002654 int val;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002655
2656 if (spec->codec_type != VT1708)
2657 return 0;
Takashi Iwai187d3332011-11-24 16:33:09 +01002658 val = !!ucontrol->value.integer.value[0];
2659 if (spec->vt1708_jack_detect == val)
2660 return 0;
2661 spec->vt1708_jack_detect = val;
2662 if (spec->vt1708_jack_detect &&
2663 snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") != 1) {
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002664 mute_aa_path(codec, 1);
2665 notify_aa_path_ctls(codec);
2666 }
Takashi Iwai187d3332011-11-24 16:33:09 +01002667 via_hp_automute(codec);
2668 vt1708_update_hp_work(spec);
2669 return 1;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002670}
2671
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002672static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
2673 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2674 .name = "Jack Detect",
2675 .count = 1,
2676 .info = snd_ctl_boolean_mono_info,
2677 .get = vt1708_jack_detect_get,
2678 .put = vt1708_jack_detect_put,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002679};
2680
Takashi Iwai12daef62011-06-18 17:45:49 +02002681static void fill_dig_outs(struct hda_codec *codec);
2682static void fill_dig_in(struct hda_codec *codec);
2683
2684static int via_parse_auto_config(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002685{
2686 struct via_spec *spec = codec->spec;
2687 int err;
2688
2689 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2690 if (err < 0)
2691 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002692 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
Takashi Iwai7f0df882011-06-18 17:33:34 +02002693 return -EINVAL;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002694
Takashi Iwai4a796162011-06-17 17:53:38 +02002695 err = via_auto_create_multi_out_ctls(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002696 if (err < 0)
2697 return err;
Takashi Iwai4a796162011-06-17 17:53:38 +02002698 err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002699 if (err < 0)
2700 return err;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002701 err = via_auto_create_speaker_ctls(codec);
2702 if (err < 0)
2703 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02002704 err = via_auto_create_loopback_switch(codec);
2705 if (err < 0)
2706 return err;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002707 err = via_auto_create_analog_input_ctls(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002708 if (err < 0)
2709 return err;
2710
2711 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2712
Takashi Iwai12daef62011-06-18 17:45:49 +02002713 fill_dig_outs(codec);
2714 fill_dig_in(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002715
Takashi Iwai603c4012008-07-30 15:01:44 +02002716 if (spec->kctls.list)
2717 spec->mixers[spec->num_mixers++] = spec->kctls.list;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002718
Joseph Chanc577b8a2006-11-29 15:29:40 +01002719
Takashi Iwai3214b962011-07-18 12:49:25 +02002720 if (spec->hp_dac_nid && spec->hp_mix_path.depth) {
Takashi Iwaiece8d042011-06-19 16:24:21 +02002721 err = via_hp_build(codec);
2722 if (err < 0)
2723 return err;
2724 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002725
Takashi Iwaif4a78282011-06-17 18:46:48 +02002726 err = via_smart51_build(codec);
2727 if (err < 0)
2728 return err;
2729
Takashi Iwai5d417622011-06-20 11:32:27 +02002730 /* assign slave outs */
2731 if (spec->slave_dig_outs[0])
2732 codec->slave_dig_outs = spec->slave_dig_outs;
2733
Joseph Chanc577b8a2006-11-29 15:29:40 +01002734 return 1;
2735}
2736
Takashi Iwai5d417622011-06-20 11:32:27 +02002737static void via_auto_init_dig_outs(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002738{
Lydia Wang25eaba22009-10-10 19:08:43 +08002739 struct via_spec *spec = codec->spec;
Takashi Iwai5d417622011-06-20 11:32:27 +02002740 if (spec->multiout.dig_out_nid)
2741 init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
2742 if (spec->slave_dig_outs[0])
2743 init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
2744}
Lydia Wang25eaba22009-10-10 19:08:43 +08002745
Takashi Iwai5d417622011-06-20 11:32:27 +02002746static void via_auto_init_dig_in(struct hda_codec *codec)
2747{
2748 struct via_spec *spec = codec->spec;
2749 if (!spec->dig_in_nid)
2750 return;
2751 snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
2752 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
2753}
2754
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002755/* initialize the unsolicited events */
2756static void via_auto_init_unsol_event(struct hda_codec *codec)
2757{
2758 struct via_spec *spec = codec->spec;
2759 struct auto_pin_cfg *cfg = &spec->autocfg;
2760 unsigned int ev;
2761 int i;
2762
2763 if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002764 snd_hda_jack_detect_enable(codec, cfg->hp_pins[0],
2765 VIA_HP_EVENT | VIA_JACK_EVENT);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002766
2767 if (cfg->speaker_pins[0])
2768 ev = VIA_LINE_EVENT;
2769 else
2770 ev = 0;
2771 for (i = 0; i < cfg->line_outs; i++) {
2772 if (cfg->line_out_pins[i] &&
2773 is_jack_detectable(codec, cfg->line_out_pins[i]))
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002774 snd_hda_jack_detect_enable(codec, cfg->line_out_pins[i],
2775 ev | VIA_JACK_EVENT);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002776 }
2777
2778 for (i = 0; i < cfg->num_inputs; i++) {
2779 if (is_jack_detectable(codec, cfg->inputs[i].pin))
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002780 snd_hda_jack_detect_enable(codec, cfg->inputs[i].pin,
2781 VIA_JACK_EVENT);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002782 }
2783}
2784
Takashi Iwai5d417622011-06-20 11:32:27 +02002785static int via_init(struct hda_codec *codec)
2786{
2787 struct via_spec *spec = codec->spec;
2788 int i;
2789
2790 for (i = 0; i < spec->num_iverbs; i++)
2791 snd_hda_sequence_write(codec, spec->init_verbs[i]);
2792
Takashi Iwaie9d010c2012-02-01 10:33:23 +01002793 /* init power states */
2794 set_widgets_power_state(codec);
2795 __analog_low_current_mode(codec, true);
2796
Joseph Chanc577b8a2006-11-29 15:29:40 +01002797 via_auto_init_multi_out(codec);
2798 via_auto_init_hp_out(codec);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002799 via_auto_init_speaker_out(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002800 via_auto_init_analog_input(codec);
Takashi Iwai5d417622011-06-20 11:32:27 +02002801 via_auto_init_dig_outs(codec);
2802 via_auto_init_dig_in(codec);
Lydia Wang118909562011-03-23 17:57:34 +08002803
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002804 via_auto_init_unsol_event(codec);
2805
2806 via_hp_automute(codec);
Takashi Iwai187d3332011-11-24 16:33:09 +01002807 vt1708_update_hp_work(spec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02002808 snd_hda_jack_report_sync(codec);
Lydia Wang25eaba22009-10-10 19:08:43 +08002809
Joseph Chanc577b8a2006-11-29 15:29:40 +01002810 return 0;
2811}
2812
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002813static void vt1708_update_hp_jack_state(struct work_struct *work)
2814{
2815 struct via_spec *spec = container_of(work, struct via_spec,
2816 vt1708_hp_work.work);
2817 if (spec->codec_type != VT1708)
2818 return;
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002819 snd_hda_jack_set_dirty_all(spec->codec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002820 /* if jack state toggled */
2821 if (spec->vt1708_hp_present
Takashi Iwaid56757a2009-11-18 08:00:14 +01002822 != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002823 spec->vt1708_hp_present ^= 1;
2824 via_hp_automute(spec->codec);
2825 }
Takashi Iwai187d3332011-11-24 16:33:09 +01002826 if (spec->vt1708_jack_detect)
2827 schedule_delayed_work(&spec->vt1708_hp_work,
2828 msecs_to_jiffies(100));
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002829}
2830
Takashi Iwai337b9d02009-07-07 18:18:59 +02002831static int get_mux_nids(struct hda_codec *codec)
2832{
2833 struct via_spec *spec = codec->spec;
2834 hda_nid_t nid, conn[8];
2835 unsigned int type;
2836 int i, n;
2837
2838 for (i = 0; i < spec->num_adc_nids; i++) {
2839 nid = spec->adc_nids[i];
2840 while (nid) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002841 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai1c55d522009-07-08 07:45:46 +02002842 if (type == AC_WID_PIN)
2843 break;
Takashi Iwai337b9d02009-07-07 18:18:59 +02002844 n = snd_hda_get_connections(codec, nid, conn,
2845 ARRAY_SIZE(conn));
2846 if (n <= 0)
2847 break;
2848 if (n > 1) {
2849 spec->mux_nids[i] = nid;
2850 break;
2851 }
2852 nid = conn[0];
2853 }
2854 }
Takashi Iwai1c55d522009-07-08 07:45:46 +02002855 return 0;
Takashi Iwai337b9d02009-07-07 18:18:59 +02002856}
2857
Joseph Chanc577b8a2006-11-29 15:29:40 +01002858static int patch_vt1708(struct hda_codec *codec)
2859{
2860 struct via_spec *spec;
2861 int err;
2862
2863 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002864 spec = via_new_spec(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002865 if (spec == NULL)
2866 return -ENOMEM;
2867
Takashi Iwai620e2b22011-06-17 17:19:19 +02002868 spec->aa_mix_nid = 0x17;
2869
Takashi Iwai12daef62011-06-18 17:45:49 +02002870 /* Add HP and CD pin config connect bit re-config action */
2871 vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
2872 vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
2873
Joseph Chanc577b8a2006-11-29 15:29:40 +01002874 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02002875 err = via_parse_auto_config(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002876 if (err < 0) {
2877 via_free(codec);
2878 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002879 }
2880
Takashi Iwai12daef62011-06-18 17:45:49 +02002881 /* add jack detect on/off control */
2882 if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
2883 return -ENOMEM;
2884
Takashi Iwaibc9b562382008-05-23 17:50:27 +02002885 /* disable 32bit format on VT1708 */
2886 if (codec->vendor_id == 0x11061708)
2887 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002888
Lydia Wange322a362011-06-29 13:52:02 +08002889 spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2890
Joseph Chanc577b8a2006-11-29 15:29:40 +01002891 codec->patch_ops = via_patch_ops;
2892
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002893 INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002894 return 0;
2895}
2896
Takashi Iwaiddd304d2011-06-21 16:33:55 +02002897static int patch_vt1709(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002898{
2899 struct via_spec *spec;
2900 int err;
2901
2902 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002903 spec = via_new_spec(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002904 if (spec == NULL)
2905 return -ENOMEM;
2906
Takashi Iwai620e2b22011-06-17 17:19:19 +02002907 spec->aa_mix_nid = 0x18;
2908
Takashi Iwai12daef62011-06-18 17:45:49 +02002909 err = via_parse_auto_config(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002910 if (err < 0) {
2911 via_free(codec);
2912 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002913 }
2914
Joseph Chanc577b8a2006-11-29 15:29:40 +01002915 codec->patch_ops = via_patch_ops;
2916
Josepch Chanf7278fd2007-12-13 16:40:40 +01002917 return 0;
2918}
2919
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002920static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
2921{
2922 struct via_spec *spec = codec->spec;
2923 int imux_is_smixer;
2924 unsigned int parm;
2925 int is_8ch = 0;
Lydia Wangbc92df72011-03-23 17:56:05 +08002926 if ((spec->codec_type != VT1708B_4CH) &&
2927 (codec->vendor_id != 0x11064397))
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002928 is_8ch = 1;
2929
2930 /* SW0 (17h) = stereo mixer */
2931 imux_is_smixer =
2932 (snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
2933 == ((spec->codec_type == VT1708S) ? 5 : 0));
2934 /* inputs */
2935 /* PW 1/2/5 (1ah/1bh/1eh) */
2936 parm = AC_PWRST_D3;
2937 set_pin_power_state(codec, 0x1a, &parm);
2938 set_pin_power_state(codec, 0x1b, &parm);
2939 set_pin_power_state(codec, 0x1e, &parm);
2940 if (imux_is_smixer)
2941 parm = AC_PWRST_D0;
2942 /* SW0 (17h), AIW 0/1 (13h/14h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01002943 update_power_state(codec, 0x17, parm);
2944 update_power_state(codec, 0x13, parm);
2945 update_power_state(codec, 0x14, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002946
2947 /* outputs */
2948 /* PW0 (19h), SW1 (18h), AOW1 (11h) */
2949 parm = AC_PWRST_D3;
2950 set_pin_power_state(codec, 0x19, &parm);
2951 if (spec->smart51_enabled)
2952 set_pin_power_state(codec, 0x1b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002953 update_power_state(codec, 0x18, parm);
2954 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002955
2956 /* PW6 (22h), SW2 (26h), AOW2 (24h) */
2957 if (is_8ch) {
2958 parm = AC_PWRST_D3;
2959 set_pin_power_state(codec, 0x22, &parm);
2960 if (spec->smart51_enabled)
2961 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002962 update_power_state(codec, 0x26, parm);
2963 update_power_state(codec, 0x24, parm);
Lydia Wangbc92df72011-03-23 17:56:05 +08002964 } else if (codec->vendor_id == 0x11064397) {
2965 /* PW7(23h), SW2(27h), AOW2(25h) */
2966 parm = AC_PWRST_D3;
2967 set_pin_power_state(codec, 0x23, &parm);
2968 if (spec->smart51_enabled)
2969 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002970 update_power_state(codec, 0x27, parm);
2971 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002972 }
2973
2974 /* PW 3/4/7 (1ch/1dh/23h) */
2975 parm = AC_PWRST_D3;
2976 /* force to D0 for internal Speaker */
2977 set_pin_power_state(codec, 0x1c, &parm);
2978 set_pin_power_state(codec, 0x1d, &parm);
2979 if (is_8ch)
2980 set_pin_power_state(codec, 0x23, &parm);
2981
2982 /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01002983 update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
2984 update_power_state(codec, 0x10, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002985 if (is_8ch) {
Takashi Iwai054d8672012-01-24 12:25:50 +01002986 update_power_state(codec, 0x25, parm);
2987 update_power_state(codec, 0x27, parm);
Lydia Wangbc92df72011-03-23 17:56:05 +08002988 } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01002989 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002990}
2991
Lydia Wang518bf3b2009-10-10 19:07:29 +08002992static int patch_vt1708S(struct hda_codec *codec);
Takashi Iwaiddd304d2011-06-21 16:33:55 +02002993static int patch_vt1708B(struct hda_codec *codec)
Josepch Chanf7278fd2007-12-13 16:40:40 +01002994{
2995 struct via_spec *spec;
2996 int err;
2997
Lydia Wang518bf3b2009-10-10 19:07:29 +08002998 if (get_codec_type(codec) == VT1708BCE)
2999 return patch_vt1708S(codec);
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003000
Josepch Chanf7278fd2007-12-13 16:40:40 +01003001 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003002 spec = via_new_spec(codec);
Josepch Chanf7278fd2007-12-13 16:40:40 +01003003 if (spec == NULL)
3004 return -ENOMEM;
3005
Takashi Iwai620e2b22011-06-17 17:19:19 +02003006 spec->aa_mix_nid = 0x16;
3007
Josepch Chanf7278fd2007-12-13 16:40:40 +01003008 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003009 err = via_parse_auto_config(codec);
Josepch Chanf7278fd2007-12-13 16:40:40 +01003010 if (err < 0) {
3011 via_free(codec);
3012 return err;
Josepch Chanf7278fd2007-12-13 16:40:40 +01003013 }
3014
Josepch Chanf7278fd2007-12-13 16:40:40 +01003015 codec->patch_ops = via_patch_ops;
3016
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003017 spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
3018
Josepch Chanf7278fd2007-12-13 16:40:40 +01003019 return 0;
3020}
3021
Harald Welted949cac2008-09-09 15:56:01 +08003022/* Patch for VT1708S */
Takashi Iwai096a8852011-06-20 12:09:02 +02003023static const struct hda_verb vt1708S_init_verbs[] = {
Harald Welted7426322008-09-15 22:43:23 +08003024 /* Enable Mic Boost Volume backdoor */
3025 {0x1, 0xf98, 0x1},
Lydia Wangbc7e7e52009-10-10 19:08:32 +08003026 /* don't bybass mixer */
3027 {0x1, 0xf88, 0xc0},
Harald Welted949cac2008-09-09 15:56:01 +08003028 { }
3029};
3030
Takashi Iwai9da29272009-05-07 16:31:14 +02003031/* fill out digital output widgets; one for master and one for slave outputs */
3032static void fill_dig_outs(struct hda_codec *codec)
3033{
3034 struct via_spec *spec = codec->spec;
3035 int i;
3036
3037 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3038 hda_nid_t nid;
3039 int conn;
3040
3041 nid = spec->autocfg.dig_out_pins[i];
3042 if (!nid)
3043 continue;
3044 conn = snd_hda_get_connections(codec, nid, &nid, 1);
3045 if (conn < 1)
3046 continue;
3047 if (!spec->multiout.dig_out_nid)
3048 spec->multiout.dig_out_nid = nid;
3049 else {
3050 spec->slave_dig_outs[0] = nid;
3051 break; /* at most two dig outs */
3052 }
3053 }
3054}
3055
Takashi Iwai12daef62011-06-18 17:45:49 +02003056static void fill_dig_in(struct hda_codec *codec)
Harald Welted949cac2008-09-09 15:56:01 +08003057{
3058 struct via_spec *spec = codec->spec;
Takashi Iwai12daef62011-06-18 17:45:49 +02003059 hda_nid_t dig_nid;
3060 int i, err;
Harald Welted949cac2008-09-09 15:56:01 +08003061
Takashi Iwai12daef62011-06-18 17:45:49 +02003062 if (!spec->autocfg.dig_in_pin)
3063 return;
Harald Welted949cac2008-09-09 15:56:01 +08003064
Takashi Iwai12daef62011-06-18 17:45:49 +02003065 dig_nid = codec->start_nid;
3066 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3067 unsigned int wcaps = get_wcaps(codec, dig_nid);
3068 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3069 continue;
3070 if (!(wcaps & AC_WCAP_DIGITAL))
3071 continue;
3072 if (!(wcaps & AC_WCAP_CONN_LIST))
3073 continue;
3074 err = get_connection_index(codec, dig_nid,
3075 spec->autocfg.dig_in_pin);
3076 if (err >= 0) {
3077 spec->dig_in_nid = dig_nid;
3078 break;
3079 }
3080 }
Harald Welted949cac2008-09-09 15:56:01 +08003081}
3082
Lydia Wang6369bcf2009-10-10 19:08:31 +08003083static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
3084 int offset, int num_steps, int step_size)
3085{
3086 snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
3087 (offset << AC_AMPCAP_OFFSET_SHIFT) |
3088 (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
3089 (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
3090 (0 << AC_AMPCAP_MUTE_SHIFT));
3091}
3092
Harald Welted949cac2008-09-09 15:56:01 +08003093static int patch_vt1708S(struct hda_codec *codec)
3094{
3095 struct via_spec *spec;
3096 int err;
3097
3098 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003099 spec = via_new_spec(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003100 if (spec == NULL)
3101 return -ENOMEM;
3102
Takashi Iwai620e2b22011-06-17 17:19:19 +02003103 spec->aa_mix_nid = 0x16;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003104 override_mic_boost(codec, 0x1a, 0, 3, 40);
3105 override_mic_boost(codec, 0x1e, 0, 3, 40);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003106
Harald Welted949cac2008-09-09 15:56:01 +08003107 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003108 err = via_parse_auto_config(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003109 if (err < 0) {
3110 via_free(codec);
3111 return err;
Harald Welted949cac2008-09-09 15:56:01 +08003112 }
3113
Takashi Iwai096a8852011-06-20 12:09:02 +02003114 spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
Harald Welted949cac2008-09-09 15:56:01 +08003115
Harald Welted949cac2008-09-09 15:56:01 +08003116 codec->patch_ops = via_patch_ops;
3117
Lydia Wang518bf3b2009-10-10 19:07:29 +08003118 /* correct names for VT1708BCE */
3119 if (get_codec_type(codec) == VT1708BCE) {
3120 kfree(codec->chip_name);
3121 codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3122 snprintf(codec->bus->card->mixername,
3123 sizeof(codec->bus->card->mixername),
3124 "%s %s", codec->vendor_name, codec->chip_name);
Lydia Wang970f630f2011-03-22 16:25:56 +08003125 }
Lydia Wangbc92df72011-03-23 17:56:05 +08003126 /* correct names for VT1705 */
3127 if (codec->vendor_id == 0x11064397) {
3128 kfree(codec->chip_name);
3129 codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3130 snprintf(codec->bus->card->mixername,
3131 sizeof(codec->bus->card->mixername),
3132 "%s %s", codec->vendor_name, codec->chip_name);
3133 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003134 spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
Harald Welted949cac2008-09-09 15:56:01 +08003135 return 0;
3136}
3137
3138/* Patch for VT1702 */
3139
Takashi Iwai096a8852011-06-20 12:09:02 +02003140static const struct hda_verb vt1702_init_verbs[] = {
Lydia Wangbc7e7e52009-10-10 19:08:32 +08003141 /* mixer enable */
3142 {0x1, 0xF88, 0x3},
3143 /* GPIO 0~2 */
3144 {0x1, 0xF82, 0x3F},
Harald Welted949cac2008-09-09 15:56:01 +08003145 { }
3146};
3147
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003148static void set_widgets_power_state_vt1702(struct hda_codec *codec)
3149{
3150 int imux_is_smixer =
3151 snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3152 unsigned int parm;
3153 /* inputs */
3154 /* PW 1/2/5 (14h/15h/18h) */
3155 parm = AC_PWRST_D3;
3156 set_pin_power_state(codec, 0x14, &parm);
3157 set_pin_power_state(codec, 0x15, &parm);
3158 set_pin_power_state(codec, 0x18, &parm);
3159 if (imux_is_smixer)
3160 parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
3161 /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003162 update_power_state(codec, 0x13, parm);
3163 update_power_state(codec, 0x12, parm);
3164 update_power_state(codec, 0x1f, parm);
3165 update_power_state(codec, 0x20, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003166
3167 /* outputs */
3168 /* PW 3/4 (16h/17h) */
3169 parm = AC_PWRST_D3;
3170 set_pin_power_state(codec, 0x17, &parm);
3171 set_pin_power_state(codec, 0x16, &parm);
3172 /* MW0 (1ah), AOW 0/1 (10h/1dh) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003173 update_power_state(codec, 0x1a, imux_is_smixer ? AC_PWRST_D0 : parm);
3174 update_power_state(codec, 0x10, parm);
3175 update_power_state(codec, 0x1d, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003176}
3177
Harald Welted949cac2008-09-09 15:56:01 +08003178static int patch_vt1702(struct hda_codec *codec)
3179{
3180 struct via_spec *spec;
3181 int err;
Harald Welted949cac2008-09-09 15:56:01 +08003182
3183 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003184 spec = via_new_spec(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003185 if (spec == NULL)
3186 return -ENOMEM;
3187
Takashi Iwai620e2b22011-06-17 17:19:19 +02003188 spec->aa_mix_nid = 0x1a;
3189
Takashi Iwai12daef62011-06-18 17:45:49 +02003190 /* limit AA path volume to 0 dB */
3191 snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
3192 (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
3193 (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
3194 (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
3195 (1 << AC_AMPCAP_MUTE_SHIFT));
3196
Harald Welted949cac2008-09-09 15:56:01 +08003197 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003198 err = via_parse_auto_config(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003199 if (err < 0) {
3200 via_free(codec);
3201 return err;
Harald Welted949cac2008-09-09 15:56:01 +08003202 }
3203
Takashi Iwai096a8852011-06-20 12:09:02 +02003204 spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
Harald Welted949cac2008-09-09 15:56:01 +08003205
Harald Welted949cac2008-09-09 15:56:01 +08003206 codec->patch_ops = via_patch_ops;
3207
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003208 spec->set_widgets_power_state = set_widgets_power_state_vt1702;
Harald Welted949cac2008-09-09 15:56:01 +08003209 return 0;
3210}
3211
Lydia Wangeb7188c2009-10-10 19:08:34 +08003212/* Patch for VT1718S */
3213
Takashi Iwai096a8852011-06-20 12:09:02 +02003214static const struct hda_verb vt1718S_init_verbs[] = {
Lydia Wang4ab2d532011-03-24 12:42:03 +08003215 /* Enable MW0 adjust Gain 5 */
3216 {0x1, 0xfb2, 0x10},
Lydia Wangeb7188c2009-10-10 19:08:34 +08003217 /* Enable Boost Volume backdoor */
3218 {0x1, 0xf88, 0x8},
Takashi Iwai5d417622011-06-20 11:32:27 +02003219
Lydia Wangeb7188c2009-10-10 19:08:34 +08003220 { }
3221};
3222
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003223static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
3224{
3225 struct via_spec *spec = codec->spec;
3226 int imux_is_smixer;
3227 unsigned int parm;
3228 /* MUX6 (1eh) = stereo mixer */
3229 imux_is_smixer =
3230 snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
3231 /* inputs */
3232 /* PW 5/6/7 (29h/2ah/2bh) */
3233 parm = AC_PWRST_D3;
3234 set_pin_power_state(codec, 0x29, &parm);
3235 set_pin_power_state(codec, 0x2a, &parm);
3236 set_pin_power_state(codec, 0x2b, &parm);
3237 if (imux_is_smixer)
3238 parm = AC_PWRST_D0;
3239 /* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003240 update_power_state(codec, 0x1e, parm);
3241 update_power_state(codec, 0x1f, parm);
3242 update_power_state(codec, 0x10, parm);
3243 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003244
3245 /* outputs */
3246 /* PW3 (27h), MW2 (1ah), AOW3 (bh) */
3247 parm = AC_PWRST_D3;
3248 set_pin_power_state(codec, 0x27, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003249 update_power_state(codec, 0x1a, parm);
3250 update_power_state(codec, 0xb, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003251
3252 /* PW2 (26h), AOW2 (ah) */
3253 parm = AC_PWRST_D3;
3254 set_pin_power_state(codec, 0x26, &parm);
3255 if (spec->smart51_enabled)
3256 set_pin_power_state(codec, 0x2b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003257 update_power_state(codec, 0xa, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003258
3259 /* PW0 (24h), AOW0 (8h) */
3260 parm = AC_PWRST_D3;
3261 set_pin_power_state(codec, 0x24, &parm);
3262 if (!spec->hp_independent_mode) /* check for redirected HP */
3263 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003264 update_power_state(codec, 0x8, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003265 /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003266 update_power_state(codec, 0x21, imux_is_smixer ? AC_PWRST_D0 : parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003267
3268 /* PW1 (25h), AOW1 (9h) */
3269 parm = AC_PWRST_D3;
3270 set_pin_power_state(codec, 0x25, &parm);
3271 if (spec->smart51_enabled)
3272 set_pin_power_state(codec, 0x2a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003273 update_power_state(codec, 0x9, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003274
3275 if (spec->hp_independent_mode) {
3276 /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
3277 parm = AC_PWRST_D3;
3278 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003279 update_power_state(codec, 0x1b, parm);
3280 update_power_state(codec, 0x34, parm);
3281 update_power_state(codec, 0xc, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003282 }
3283}
3284
Takashi Iwai30b45032011-07-11 17:05:04 +02003285/* Add a connection to the primary DAC from AA-mixer for some codecs
3286 * This isn't listed from the raw info, but the chip has a secret connection.
3287 */
3288static int add_secret_dac_path(struct hda_codec *codec)
3289{
3290 struct via_spec *spec = codec->spec;
3291 int i, nums;
3292 hda_nid_t conn[8];
3293 hda_nid_t nid;
3294
3295 if (!spec->aa_mix_nid)
3296 return 0;
3297 nums = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
3298 ARRAY_SIZE(conn) - 1);
3299 for (i = 0; i < nums; i++) {
3300 if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
3301 return 0;
3302 }
3303
3304 /* find the primary DAC and add to the connection list */
3305 nid = codec->start_nid;
3306 for (i = 0; i < codec->num_nodes; i++, nid++) {
3307 unsigned int caps = get_wcaps(codec, nid);
3308 if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
3309 !(caps & AC_WCAP_DIGITAL)) {
3310 conn[nums++] = nid;
3311 return snd_hda_override_conn_list(codec,
3312 spec->aa_mix_nid,
3313 nums, conn);
3314 }
3315 }
3316 return 0;
3317}
3318
3319
Lydia Wangeb7188c2009-10-10 19:08:34 +08003320static int patch_vt1718S(struct hda_codec *codec)
3321{
3322 struct via_spec *spec;
3323 int err;
3324
3325 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003326 spec = via_new_spec(codec);
Lydia Wangeb7188c2009-10-10 19:08:34 +08003327 if (spec == NULL)
3328 return -ENOMEM;
3329
Takashi Iwai620e2b22011-06-17 17:19:19 +02003330 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003331 override_mic_boost(codec, 0x2b, 0, 3, 40);
3332 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003333 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003334
Lydia Wangeb7188c2009-10-10 19:08:34 +08003335 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003336 err = via_parse_auto_config(codec);
Lydia Wangeb7188c2009-10-10 19:08:34 +08003337 if (err < 0) {
3338 via_free(codec);
3339 return err;
Lydia Wangeb7188c2009-10-10 19:08:34 +08003340 }
3341
Takashi Iwai096a8852011-06-20 12:09:02 +02003342 spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
Lydia Wangeb7188c2009-10-10 19:08:34 +08003343
Lydia Wangeb7188c2009-10-10 19:08:34 +08003344 codec->patch_ops = via_patch_ops;
3345
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003346 spec->set_widgets_power_state = set_widgets_power_state_vt1718S;
3347
Lydia Wangeb7188c2009-10-10 19:08:34 +08003348 return 0;
3349}
Lydia Wangf3db4232009-10-10 19:08:41 +08003350
3351/* Patch for VT1716S */
3352
3353static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
3354 struct snd_ctl_elem_info *uinfo)
3355{
3356 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3357 uinfo->count = 1;
3358 uinfo->value.integer.min = 0;
3359 uinfo->value.integer.max = 1;
3360 return 0;
3361}
3362
3363static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
3364 struct snd_ctl_elem_value *ucontrol)
3365{
3366 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3367 int index = 0;
3368
3369 index = snd_hda_codec_read(codec, 0x26, 0,
3370 AC_VERB_GET_CONNECT_SEL, 0);
3371 if (index != -1)
3372 *ucontrol->value.integer.value = index;
3373
3374 return 0;
3375}
3376
3377static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
3378 struct snd_ctl_elem_value *ucontrol)
3379{
3380 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3381 struct via_spec *spec = codec->spec;
3382 int index = *ucontrol->value.integer.value;
3383
3384 snd_hda_codec_write(codec, 0x26, 0,
3385 AC_VERB_SET_CONNECT_SEL, index);
3386 spec->dmic_enabled = index;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003387 set_widgets_power_state(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003388 return 1;
3389}
3390
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003391static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003392 HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
3393 {
3394 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3395 .name = "Digital Mic Capture Switch",
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003396 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
Lydia Wangf3db4232009-10-10 19:08:41 +08003397 .count = 1,
3398 .info = vt1716s_dmic_info,
3399 .get = vt1716s_dmic_get,
3400 .put = vt1716s_dmic_put,
3401 },
3402 {} /* end */
3403};
3404
3405
3406/* mono-out mixer elements */
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003407static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003408 HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
3409 { } /* end */
3410};
3411
Takashi Iwai096a8852011-06-20 12:09:02 +02003412static const struct hda_verb vt1716S_init_verbs[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003413 /* Enable Boost Volume backdoor */
3414 {0x1, 0xf8a, 0x80},
3415 /* don't bybass mixer */
3416 {0x1, 0xf88, 0xc0},
3417 /* Enable mono output */
3418 {0x1, 0xf90, 0x08},
3419 { }
3420};
3421
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003422static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
3423{
3424 struct via_spec *spec = codec->spec;
3425 int imux_is_smixer;
3426 unsigned int parm;
3427 unsigned int mono_out, present;
3428 /* SW0 (17h) = stereo mixer */
3429 imux_is_smixer =
3430 (snd_hda_codec_read(codec, 0x17, 0,
3431 AC_VERB_GET_CONNECT_SEL, 0x00) == 5);
3432 /* inputs */
3433 /* PW 1/2/5 (1ah/1bh/1eh) */
3434 parm = AC_PWRST_D3;
3435 set_pin_power_state(codec, 0x1a, &parm);
3436 set_pin_power_state(codec, 0x1b, &parm);
3437 set_pin_power_state(codec, 0x1e, &parm);
3438 if (imux_is_smixer)
3439 parm = AC_PWRST_D0;
3440 /* SW0 (17h), AIW0(13h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003441 update_power_state(codec, 0x17, parm);
3442 update_power_state(codec, 0x13, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003443
3444 parm = AC_PWRST_D3;
3445 set_pin_power_state(codec, 0x1e, &parm);
3446 /* PW11 (22h) */
3447 if (spec->dmic_enabled)
3448 set_pin_power_state(codec, 0x22, &parm);
3449 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003450 update_power_state(codec, 0x22, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003451
3452 /* SW2(26h), AIW1(14h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003453 update_power_state(codec, 0x26, parm);
3454 update_power_state(codec, 0x14, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003455
3456 /* outputs */
3457 /* PW0 (19h), SW1 (18h), AOW1 (11h) */
3458 parm = AC_PWRST_D3;
3459 set_pin_power_state(codec, 0x19, &parm);
3460 /* Smart 5.1 PW2(1bh) */
3461 if (spec->smart51_enabled)
3462 set_pin_power_state(codec, 0x1b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003463 update_power_state(codec, 0x18, parm);
3464 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003465
3466 /* PW7 (23h), SW3 (27h), AOW3 (25h) */
3467 parm = AC_PWRST_D3;
3468 set_pin_power_state(codec, 0x23, &parm);
3469 /* Smart 5.1 PW1(1ah) */
3470 if (spec->smart51_enabled)
3471 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003472 update_power_state(codec, 0x27, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003473
3474 /* Smart 5.1 PW5(1eh) */
3475 if (spec->smart51_enabled)
3476 set_pin_power_state(codec, 0x1e, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003477 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003478
3479 /* Mono out */
3480 /* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
3481 present = snd_hda_jack_detect(codec, 0x1c);
3482
3483 if (present)
3484 mono_out = 0;
3485 else {
3486 present = snd_hda_jack_detect(codec, 0x1d);
3487 if (!spec->hp_independent_mode && present)
3488 mono_out = 0;
3489 else
3490 mono_out = 1;
3491 }
3492 parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
Takashi Iwai054d8672012-01-24 12:25:50 +01003493 update_power_state(codec, 0x28, parm);
3494 update_power_state(codec, 0x29, parm);
3495 update_power_state(codec, 0x2a, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003496
3497 /* PW 3/4 (1ch/1dh) */
3498 parm = AC_PWRST_D3;
3499 set_pin_power_state(codec, 0x1c, &parm);
3500 set_pin_power_state(codec, 0x1d, &parm);
3501 /* HP Independent Mode, power on AOW3 */
3502 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003503 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003504
3505 /* force to D0 for internal Speaker */
3506 /* MW0 (16h), AOW0 (10h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003507 update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
3508 update_power_state(codec, 0x10, mono_out ? AC_PWRST_D0 : parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003509}
3510
Lydia Wangf3db4232009-10-10 19:08:41 +08003511static int patch_vt1716S(struct hda_codec *codec)
3512{
3513 struct via_spec *spec;
3514 int err;
3515
3516 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003517 spec = via_new_spec(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003518 if (spec == NULL)
3519 return -ENOMEM;
3520
Takashi Iwai620e2b22011-06-17 17:19:19 +02003521 spec->aa_mix_nid = 0x16;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003522 override_mic_boost(codec, 0x1a, 0, 3, 40);
3523 override_mic_boost(codec, 0x1e, 0, 3, 40);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003524
Lydia Wangf3db4232009-10-10 19:08:41 +08003525 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003526 err = via_parse_auto_config(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003527 if (err < 0) {
3528 via_free(codec);
3529 return err;
Lydia Wangf3db4232009-10-10 19:08:41 +08003530 }
3531
Takashi Iwai096a8852011-06-20 12:09:02 +02003532 spec->init_verbs[spec->num_iverbs++] = vt1716S_init_verbs;
Lydia Wangf3db4232009-10-10 19:08:41 +08003533
Lydia Wangf3db4232009-10-10 19:08:41 +08003534 spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
3535 spec->num_mixers++;
3536
3537 spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3538
3539 codec->patch_ops = via_patch_ops;
3540
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003541 spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
Lydia Wangf3db4232009-10-10 19:08:41 +08003542 return 0;
3543}
Lydia Wang25eaba22009-10-10 19:08:43 +08003544
3545/* for vt2002P */
3546
Takashi Iwai096a8852011-06-20 12:09:02 +02003547static const struct hda_verb vt2002P_init_verbs[] = {
Lydia Wangeadb9a82011-03-24 12:43:02 +08003548 /* Class-D speaker related verbs */
3549 {0x1, 0xfe0, 0x4},
3550 {0x1, 0xfe9, 0x80},
3551 {0x1, 0xfe2, 0x22},
Lydia Wang25eaba22009-10-10 19:08:43 +08003552 /* Enable Boost Volume backdoor */
3553 {0x1, 0xfb9, 0x24},
Lydia Wang25eaba22009-10-10 19:08:43 +08003554 /* Enable AOW0 to MW9 */
3555 {0x1, 0xfb8, 0x88},
3556 { }
3557};
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003558
Takashi Iwai096a8852011-06-20 12:09:02 +02003559static const struct hda_verb vt1802_init_verbs[] = {
Lydia Wang118909562011-03-23 17:57:34 +08003560 /* Enable Boost Volume backdoor */
3561 {0x1, 0xfb9, 0x24},
Lydia Wang118909562011-03-23 17:57:34 +08003562 /* Enable AOW0 to MW9 */
3563 {0x1, 0xfb8, 0x88},
3564 { }
3565};
Lydia Wang25eaba22009-10-10 19:08:43 +08003566
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003567static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
3568{
3569 struct via_spec *spec = codec->spec;
3570 int imux_is_smixer;
3571 unsigned int parm;
3572 unsigned int present;
3573 /* MUX9 (1eh) = stereo mixer */
3574 imux_is_smixer =
3575 snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3576 /* inputs */
3577 /* PW 5/6/7 (29h/2ah/2bh) */
3578 parm = AC_PWRST_D3;
3579 set_pin_power_state(codec, 0x29, &parm);
3580 set_pin_power_state(codec, 0x2a, &parm);
3581 set_pin_power_state(codec, 0x2b, &parm);
3582 parm = AC_PWRST_D0;
3583 /* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003584 update_power_state(codec, 0x1e, parm);
3585 update_power_state(codec, 0x1f, parm);
3586 update_power_state(codec, 0x10, parm);
3587 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003588
3589 /* outputs */
3590 /* AOW0 (8h)*/
Takashi Iwai054d8672012-01-24 12:25:50 +01003591 update_power_state(codec, 0x8, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003592
Lydia Wang118909562011-03-23 17:57:34 +08003593 if (spec->codec_type == VT1802) {
3594 /* PW4 (28h), MW4 (18h), MUX4(38h) */
3595 parm = AC_PWRST_D3;
3596 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003597 update_power_state(codec, 0x18, parm);
3598 update_power_state(codec, 0x38, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003599 } else {
3600 /* PW4 (26h), MW4 (1ch), MUX4(37h) */
3601 parm = AC_PWRST_D3;
3602 set_pin_power_state(codec, 0x26, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003603 update_power_state(codec, 0x1c, parm);
3604 update_power_state(codec, 0x37, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003605 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003606
Lydia Wang118909562011-03-23 17:57:34 +08003607 if (spec->codec_type == VT1802) {
3608 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3609 parm = AC_PWRST_D3;
3610 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003611 update_power_state(codec, 0x15, parm);
3612 update_power_state(codec, 0x35, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003613 } else {
3614 /* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
3615 parm = AC_PWRST_D3;
3616 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003617 update_power_state(codec, 0x19, parm);
3618 update_power_state(codec, 0x35, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003619 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003620
3621 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003622 update_power_state(codec, 0x9, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003623
3624 /* Class-D */
3625 /* PW0 (24h), MW0(18h/14h), MUX0(34h) */
3626 present = snd_hda_jack_detect(codec, 0x25);
3627
3628 parm = AC_PWRST_D3;
3629 set_pin_power_state(codec, 0x24, &parm);
3630 parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
Lydia Wang118909562011-03-23 17:57:34 +08003631 if (spec->codec_type == VT1802)
Takashi Iwai054d8672012-01-24 12:25:50 +01003632 update_power_state(codec, 0x14, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003633 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003634 update_power_state(codec, 0x18, parm);
3635 update_power_state(codec, 0x34, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003636
3637 /* Mono Out */
3638 present = snd_hda_jack_detect(codec, 0x26);
3639
3640 parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
Lydia Wang118909562011-03-23 17:57:34 +08003641 if (spec->codec_type == VT1802) {
3642 /* PW15 (33h), MW8(1ch), MUX8(3ch) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003643 update_power_state(codec, 0x33, parm);
3644 update_power_state(codec, 0x1c, parm);
3645 update_power_state(codec, 0x3c, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003646 } else {
3647 /* PW15 (31h), MW8(17h), MUX8(3bh) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003648 update_power_state(codec, 0x31, parm);
3649 update_power_state(codec, 0x17, parm);
3650 update_power_state(codec, 0x3b, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003651 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003652 /* MW9 (21h) */
3653 if (imux_is_smixer || !is_aa_path_mute(codec))
Takashi Iwai054d8672012-01-24 12:25:50 +01003654 update_power_state(codec, 0x21, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003655 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003656 update_power_state(codec, 0x21, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003657}
Lydia Wang25eaba22009-10-10 19:08:43 +08003658
3659/* patch for vt2002P */
3660static int patch_vt2002P(struct hda_codec *codec)
3661{
3662 struct via_spec *spec;
3663 int err;
3664
3665 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003666 spec = via_new_spec(codec);
Lydia Wang25eaba22009-10-10 19:08:43 +08003667 if (spec == NULL)
3668 return -ENOMEM;
3669
Takashi Iwai620e2b22011-06-17 17:19:19 +02003670 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003671 override_mic_boost(codec, 0x2b, 0, 3, 40);
3672 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003673 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003674
Lydia Wang25eaba22009-10-10 19:08:43 +08003675 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003676 err = via_parse_auto_config(codec);
Lydia Wang25eaba22009-10-10 19:08:43 +08003677 if (err < 0) {
3678 via_free(codec);
3679 return err;
Lydia Wang25eaba22009-10-10 19:08:43 +08003680 }
3681
Lydia Wang118909562011-03-23 17:57:34 +08003682 if (spec->codec_type == VT1802)
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003683 spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
Lydia Wang118909562011-03-23 17:57:34 +08003684 else
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003685 spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
Lydia Wang118909562011-03-23 17:57:34 +08003686
Lydia Wang25eaba22009-10-10 19:08:43 +08003687 codec->patch_ops = via_patch_ops;
3688
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003689 spec->set_widgets_power_state = set_widgets_power_state_vt2002P;
Lydia Wang25eaba22009-10-10 19:08:43 +08003690 return 0;
3691}
Lydia Wangab6734e2009-10-10 19:08:46 +08003692
3693/* for vt1812 */
3694
Takashi Iwai096a8852011-06-20 12:09:02 +02003695static const struct hda_verb vt1812_init_verbs[] = {
Lydia Wangab6734e2009-10-10 19:08:46 +08003696 /* Enable Boost Volume backdoor */
3697 {0x1, 0xfb9, 0x24},
Lydia Wangab6734e2009-10-10 19:08:46 +08003698 /* Enable AOW0 to MW9 */
3699 {0x1, 0xfb8, 0xa8},
3700 { }
3701};
3702
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003703static void set_widgets_power_state_vt1812(struct hda_codec *codec)
3704{
3705 struct via_spec *spec = codec->spec;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003706 unsigned int parm;
3707 unsigned int present;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003708 /* inputs */
3709 /* PW 5/6/7 (29h/2ah/2bh) */
3710 parm = AC_PWRST_D3;
3711 set_pin_power_state(codec, 0x29, &parm);
3712 set_pin_power_state(codec, 0x2a, &parm);
3713 set_pin_power_state(codec, 0x2b, &parm);
3714 parm = AC_PWRST_D0;
3715 /* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003716 update_power_state(codec, 0x1e, parm);
3717 update_power_state(codec, 0x1f, parm);
3718 update_power_state(codec, 0x10, parm);
3719 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003720
3721 /* outputs */
3722 /* AOW0 (8h)*/
Takashi Iwai054d8672012-01-24 12:25:50 +01003723 update_power_state(codec, 0x8, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003724
3725 /* PW4 (28h), MW4 (18h), MUX4(38h) */
3726 parm = AC_PWRST_D3;
3727 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003728 update_power_state(codec, 0x18, parm);
3729 update_power_state(codec, 0x38, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003730
3731 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3732 parm = AC_PWRST_D3;
3733 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003734 update_power_state(codec, 0x15, parm);
3735 update_power_state(codec, 0x35, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003736 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003737 update_power_state(codec, 0x9, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003738
3739 /* Internal Speaker */
3740 /* PW0 (24h), MW0(14h), MUX0(34h) */
3741 present = snd_hda_jack_detect(codec, 0x25);
3742
3743 parm = AC_PWRST_D3;
3744 set_pin_power_state(codec, 0x24, &parm);
3745 if (present) {
Takashi Iwai054d8672012-01-24 12:25:50 +01003746 update_power_state(codec, 0x14, AC_PWRST_D3);
3747 update_power_state(codec, 0x34, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003748 } else {
Takashi Iwai054d8672012-01-24 12:25:50 +01003749 update_power_state(codec, 0x14, AC_PWRST_D0);
3750 update_power_state(codec, 0x34, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003751 }
3752
3753
3754 /* Mono Out */
3755 /* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
3756 present = snd_hda_jack_detect(codec, 0x28);
3757
3758 parm = AC_PWRST_D3;
3759 set_pin_power_state(codec, 0x31, &parm);
3760 if (present) {
Takashi Iwai054d8672012-01-24 12:25:50 +01003761 update_power_state(codec, 0x1c, AC_PWRST_D3);
3762 update_power_state(codec, 0x3c, AC_PWRST_D3);
3763 update_power_state(codec, 0x3e, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003764 } else {
Takashi Iwai054d8672012-01-24 12:25:50 +01003765 update_power_state(codec, 0x1c, AC_PWRST_D0);
3766 update_power_state(codec, 0x3c, AC_PWRST_D0);
3767 update_power_state(codec, 0x3e, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003768 }
3769
3770 /* PW15 (33h), MW15 (1dh), MUX15(3dh) */
3771 parm = AC_PWRST_D3;
3772 set_pin_power_state(codec, 0x33, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003773 update_power_state(codec, 0x1d, parm);
3774 update_power_state(codec, 0x3d, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003775
3776}
Lydia Wangab6734e2009-10-10 19:08:46 +08003777
3778/* patch for vt1812 */
3779static int patch_vt1812(struct hda_codec *codec)
3780{
3781 struct via_spec *spec;
3782 int err;
3783
3784 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003785 spec = via_new_spec(codec);
Lydia Wangab6734e2009-10-10 19:08:46 +08003786 if (spec == NULL)
3787 return -ENOMEM;
3788
Takashi Iwai620e2b22011-06-17 17:19:19 +02003789 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003790 override_mic_boost(codec, 0x2b, 0, 3, 40);
3791 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003792 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003793
Lydia Wangab6734e2009-10-10 19:08:46 +08003794 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003795 err = via_parse_auto_config(codec);
Lydia Wangab6734e2009-10-10 19:08:46 +08003796 if (err < 0) {
3797 via_free(codec);
3798 return err;
Lydia Wangab6734e2009-10-10 19:08:46 +08003799 }
3800
Takashi Iwai096a8852011-06-20 12:09:02 +02003801 spec->init_verbs[spec->num_iverbs++] = vt1812_init_verbs;
Lydia Wangab6734e2009-10-10 19:08:46 +08003802
Lydia Wangab6734e2009-10-10 19:08:46 +08003803 codec->patch_ops = via_patch_ops;
3804
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003805 spec->set_widgets_power_state = set_widgets_power_state_vt1812;
Lydia Wangab6734e2009-10-10 19:08:46 +08003806 return 0;
3807}
3808
Joseph Chanc577b8a2006-11-29 15:29:40 +01003809/*
3810 * patch entries
3811 */
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003812static const struct hda_codec_preset snd_hda_preset_via[] = {
Takashi Iwai3218c172008-12-18 09:17:56 +01003813 { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
3814 { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
3815 { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
3816 { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
3817 { .id = 0x1106e710, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003818 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003819 { .id = 0x1106e711, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003820 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003821 { .id = 0x1106e712, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003822 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003823 { .id = 0x1106e713, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003824 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003825 { .id = 0x1106e714, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003826 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003827 { .id = 0x1106e715, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003828 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003829 { .id = 0x1106e716, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003830 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003831 { .id = 0x1106e717, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003832 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003833 { .id = 0x1106e720, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003834 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003835 { .id = 0x1106e721, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003836 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003837 { .id = 0x1106e722, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003838 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003839 { .id = 0x1106e723, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003840 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003841 { .id = 0x1106e724, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003842 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003843 { .id = 0x1106e725, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003844 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003845 { .id = 0x1106e726, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003846 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003847 { .id = 0x1106e727, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003848 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003849 { .id = 0x11060397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003850 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003851 { .id = 0x11061397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003852 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003853 { .id = 0x11062397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003854 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003855 { .id = 0x11063397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003856 .patch = patch_vt1708S},
Lydia Wangbc92df72011-03-23 17:56:05 +08003857 { .id = 0x11064397, .name = "VT1705",
Harald Welted949cac2008-09-09 15:56:01 +08003858 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003859 { .id = 0x11065397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003860 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003861 { .id = 0x11066397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003862 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003863 { .id = 0x11067397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003864 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003865 { .id = 0x11060398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003866 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003867 { .id = 0x11061398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003868 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003869 { .id = 0x11062398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003870 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003871 { .id = 0x11063398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003872 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003873 { .id = 0x11064398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003874 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003875 { .id = 0x11065398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003876 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003877 { .id = 0x11066398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003878 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003879 { .id = 0x11067398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003880 .patch = patch_vt1702},
Lydia Wangeb7188c2009-10-10 19:08:34 +08003881 { .id = 0x11060428, .name = "VT1718S",
3882 .patch = patch_vt1718S},
3883 { .id = 0x11064428, .name = "VT1718S",
3884 .patch = patch_vt1718S},
Lydia Wangbb3c6bfc2009-10-10 19:08:39 +08003885 { .id = 0x11060441, .name = "VT2020",
3886 .patch = patch_vt1718S},
3887 { .id = 0x11064441, .name = "VT1828S",
3888 .patch = patch_vt1718S},
Lydia Wangf3db4232009-10-10 19:08:41 +08003889 { .id = 0x11060433, .name = "VT1716S",
3890 .patch = patch_vt1716S},
3891 { .id = 0x1106a721, .name = "VT1716S",
3892 .patch = patch_vt1716S},
Lydia Wang25eaba22009-10-10 19:08:43 +08003893 { .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
3894 { .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
Lydia Wangab6734e2009-10-10 19:08:46 +08003895 { .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
Lydia Wang36dd5c42009-10-20 13:18:04 +08003896 { .id = 0x11060440, .name = "VT1818S",
3897 .patch = patch_vt1708S},
Lydia Wang118909562011-03-23 17:57:34 +08003898 { .id = 0x11060446, .name = "VT1802",
3899 .patch = patch_vt2002P},
3900 { .id = 0x11068446, .name = "VT1802",
3901 .patch = patch_vt2002P},
Joseph Chanc577b8a2006-11-29 15:29:40 +01003902 {} /* terminator */
3903};
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003904
3905MODULE_ALIAS("snd-hda-codec-id:1106*");
3906
3907static struct hda_codec_preset_list via_list = {
3908 .preset = snd_hda_preset_via,
3909 .owner = THIS_MODULE,
3910};
3911
3912MODULE_LICENSE("GPL");
3913MODULE_DESCRIPTION("VIA HD-audio codec");
3914
3915static int __init patch_via_init(void)
3916{
3917 return snd_hda_add_codec_preset(&via_list);
3918}
3919
3920static void __exit patch_via_exit(void)
3921{
3922 snd_hda_delete_codec_preset(&via_list);
3923}
3924
3925module_init(patch_via_init)
3926module_exit(patch_via_exit)