blob: e5842fe1b1e836bb8a769f96a23b44e6ea68fbbe [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 Wangd69607b2011-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 Wangd69607b2011-07-08 14:02:52 +0800482{
483 int i, num, val;
Lydia Wangd69607b2011-07-08 14:02:52 +0800484
485 if (!path)
486 return;
487 num = snd_hda_get_conn_list(codec, mix_nid, NULL);
Lydia Wangd69607b2011-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 Wangd69607b2011-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 Wangd69607b2011-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 Wangd69607b2011-07-08 14:02:52 +0800546 hda_nid_t pin;
Takashi Iwai5d417622011-06-20 11:32:27 +0200547
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200548 if (!path->depth)
Takashi Iwai5d417622011-06-20 11:32:27 +0200549 return;
Takashi Iwai09a9ad692011-06-21 15:57:44 +0200550 pin = path->path[path->depth - 1];
Takashi Iwai5d417622011-06-20 11:32:27 +0200551
552 init_output_pin(codec, pin, pin_type);
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;
669 if (spec->mux_nids[adc_idx]) {
670 int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
671 snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
672 AC_VERB_SET_CONNECT_SEL,
673 mux_idx);
674 }
675 if (spec->dyn_adc_switch)
676 break; /* only one input-src */
Takashi Iwai096a8852011-06-20 12:09:02 +0200677 }
678
679 /* init aa-mixer */
680 if (!spec->aa_mix_nid)
681 return;
682 num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
683 ARRAY_SIZE(conn));
684 for (i = 0; i < num_conns; i++) {
685 unsigned int caps = get_wcaps(codec, conn[i]);
686 if (get_wcaps_type(caps) == AC_WID_PIN)
687 snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
688 AC_VERB_SET_AMP_GAIN_MUTE,
689 AMP_IN_MUTE(i));
690 }
Joseph Chanc577b8a2006-11-29 15:29:40 +0100691}
Lydia Wangf5271102009-10-10 19:07:35 +0800692
Takashi Iwai054d8672012-01-24 12:25:50 +0100693static void update_power_state(struct hda_codec *codec, hda_nid_t nid,
694 unsigned int parm)
695{
696 if (snd_hda_codec_read(codec, nid, 0,
697 AC_VERB_GET_POWER_STATE, 0) == parm)
698 return;
699 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
700}
701
Lydia Wangf5271102009-10-10 19:07:35 +0800702static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
703 unsigned int *affected_parm)
704{
705 unsigned parm;
706 unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
707 unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
708 >> AC_DEFCFG_MISC_SHIFT
709 & AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
Lydia Wang1564b282009-10-10 19:07:52 +0800710 struct via_spec *spec = codec->spec;
Takashi Iwai24088a52011-06-17 16:59:21 +0200711 unsigned present = 0;
712
713 no_presence |= spec->no_pin_power_ctl;
714 if (!no_presence)
715 present = snd_hda_jack_detect(codec, nid);
Takashi Iwaif4a78282011-06-17 18:46:48 +0200716 if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
Lydia Wang1564b282009-10-10 19:07:52 +0800717 || ((no_presence || present)
718 && get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
Lydia Wangf5271102009-10-10 19:07:35 +0800719 *affected_parm = AC_PWRST_D0; /* if it's connected */
720 parm = AC_PWRST_D0;
721 } else
722 parm = AC_PWRST_D3;
723
Takashi Iwai054d8672012-01-24 12:25:50 +0100724 update_power_state(codec, nid, parm);
Lydia Wangf5271102009-10-10 19:07:35 +0800725}
726
Takashi Iwai24088a52011-06-17 16:59:21 +0200727static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
728 struct snd_ctl_elem_info *uinfo)
729{
730 static const char * const texts[] = {
731 "Disabled", "Enabled"
732 };
733
734 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
735 uinfo->count = 1;
736 uinfo->value.enumerated.items = 2;
737 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
738 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
739 strcpy(uinfo->value.enumerated.name,
740 texts[uinfo->value.enumerated.item]);
741 return 0;
742}
743
744static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
745 struct snd_ctl_elem_value *ucontrol)
746{
747 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
748 struct via_spec *spec = codec->spec;
749 ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
750 return 0;
751}
752
753static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
754 struct snd_ctl_elem_value *ucontrol)
755{
756 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
757 struct via_spec *spec = codec->spec;
758 unsigned int val = !ucontrol->value.enumerated.item[0];
759
760 if (val == spec->no_pin_power_ctl)
761 return 0;
762 spec->no_pin_power_ctl = val;
763 set_widgets_power_state(codec);
Takashi Iwaie9d010c2012-02-01 10:33:23 +0100764 analog_low_current_mode(codec);
Takashi Iwai24088a52011-06-17 16:59:21 +0200765 return 1;
766}
767
768static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
769 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
770 .name = "Dynamic Power-Control",
771 .info = via_pin_power_ctl_info,
772 .get = via_pin_power_ctl_get,
773 .put = via_pin_power_ctl_put,
774};
775
776
Harald Welte0aa62ae2008-09-09 15:58:27 +0800777static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
778 struct snd_ctl_elem_info *uinfo)
779{
Takashi Iwai8df2a312011-06-21 11:48:29 +0200780 static const char * const texts[] = { "OFF", "ON" };
781
782 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
783 uinfo->count = 1;
784 uinfo->value.enumerated.items = 2;
785 if (uinfo->value.enumerated.item >= 2)
786 uinfo->value.enumerated.item = 1;
787 strcpy(uinfo->value.enumerated.name,
788 texts[uinfo->value.enumerated.item]);
789 return 0;
Harald Welte0aa62ae2008-09-09 15:58:27 +0800790}
791
792static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
793 struct snd_ctl_elem_value *ucontrol)
794{
795 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Lydia Wangcdc17842009-10-10 19:07:47 +0800796 struct via_spec *spec = codec->spec;
Lydia Wangcdc17842009-10-10 19:07:47 +0800797
Takashi Iwaiece8d042011-06-19 16:24:21 +0200798 ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
Lydia Wangcdc17842009-10-10 19:07:47 +0800799 return 0;
800}
801
Takashi Iwai3b607e32011-07-18 16:54:40 +0200802/* adjust spec->multiout setup according to the current flags */
803static void setup_playback_multi_pcm(struct via_spec *spec)
804{
805 const struct auto_pin_cfg *cfg = &spec->autocfg;
806 spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
807 spec->multiout.hp_nid = 0;
808 if (!spec->hp_independent_mode) {
809 if (!spec->hp_indep_shared)
810 spec->multiout.hp_nid = spec->hp_dac_nid;
811 } else {
812 if (spec->hp_indep_shared)
813 spec->multiout.num_dacs = cfg->line_outs - 1;
814 }
815}
816
817/* update DAC setups according to indep-HP switch;
818 * this function is called only when indep-HP is modified
819 */
820static void switch_indep_hp_dacs(struct hda_codec *codec)
821{
822 struct via_spec *spec = codec->spec;
823 int shared = spec->hp_indep_shared;
824 hda_nid_t shared_dac, hp_dac;
825
826 if (!spec->opened_streams)
827 return;
828
829 shared_dac = shared ? spec->multiout.dac_nids[shared] : 0;
830 hp_dac = spec->hp_dac_nid;
831 if (spec->hp_independent_mode) {
832 /* switch to indep-HP mode */
833 if (spec->active_streams & STREAM_MULTI_OUT) {
834 __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
835 __snd_hda_codec_cleanup_stream(codec, shared_dac, 1);
836 }
837 if (spec->active_streams & STREAM_INDEP_HP)
838 snd_hda_codec_setup_stream(codec, hp_dac,
839 spec->cur_hp_stream_tag, 0,
840 spec->cur_hp_format);
841 } else {
842 /* back to HP or shared-DAC */
843 if (spec->active_streams & STREAM_INDEP_HP)
844 __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
845 if (spec->active_streams & STREAM_MULTI_OUT) {
846 hda_nid_t dac;
847 int ch;
848 if (shared_dac) { /* reset mutli-ch DAC */
849 dac = shared_dac;
850 ch = shared * 2;
851 } else { /* reset HP DAC */
852 dac = hp_dac;
853 ch = 0;
854 }
855 snd_hda_codec_setup_stream(codec, dac,
856 spec->cur_dac_stream_tag, ch,
857 spec->cur_dac_format);
858 }
859 }
860 setup_playback_multi_pcm(spec);
861}
862
Harald Welte0aa62ae2008-09-09 15:58:27 +0800863static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
864 struct snd_ctl_elem_value *ucontrol)
865{
866 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
867 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +0200868 int cur, shared;
Takashi Iwai8df2a312011-06-21 11:48:29 +0200869
Takashi Iwai3b607e32011-07-18 16:54:40 +0200870 mutex_lock(&spec->config_mutex);
Takashi Iwai25250502011-06-30 17:24:47 +0200871 cur = !!ucontrol->value.enumerated.item[0];
Takashi Iwai3b607e32011-07-18 16:54:40 +0200872 if (spec->hp_independent_mode == cur) {
873 mutex_unlock(&spec->config_mutex);
Takashi Iwai25250502011-06-30 17:24:47 +0200874 return 0;
Takashi Iwai3b607e32011-07-18 16:54:40 +0200875 }
Takashi Iwai25250502011-06-30 17:24:47 +0200876 spec->hp_independent_mode = cur;
Takashi Iwai3214b962011-07-18 12:49:25 +0200877 shared = spec->hp_indep_shared;
Takashi Iwai020066d2011-07-21 13:45:56 +0200878 deactivate_hp_paths(codec);
879 if (cur)
880 activate_output_path(codec, &spec->hp_indep_path, true, false);
881 else {
Takashi Iwai3214b962011-07-18 12:49:25 +0200882 if (shared)
883 activate_output_path(codec, &spec->out_path[shared],
Takashi Iwai25250502011-06-30 17:24:47 +0200884 true, false);
Takashi Iwai020066d2011-07-21 13:45:56 +0200885 if (spec->aamix_mode || !spec->hp_path.depth)
886 activate_output_path(codec, &spec->hp_mix_path,
887 true, false);
888 else
889 activate_output_path(codec, &spec->hp_path,
890 true, false);
Takashi Iwai8df2a312011-06-21 11:48:29 +0200891 }
Harald Welte0aa62ae2008-09-09 15:58:27 +0800892
Takashi Iwai3b607e32011-07-18 16:54:40 +0200893 switch_indep_hp_dacs(codec);
894 mutex_unlock(&spec->config_mutex);
895
Lydia Wangce0e5a92011-03-22 16:22:37 +0800896 /* update jack power state */
Lydia Wang3e95b9a2011-03-23 15:13:28 +0800897 set_widgets_power_state(codec);
Takashi Iwai6e969d92011-07-11 11:28:13 +0200898 via_hp_automute(codec);
Takashi Iwai25250502011-06-30 17:24:47 +0200899 return 1;
Harald Welte0aa62ae2008-09-09 15:58:27 +0800900}
901
Takashi Iwaiece8d042011-06-19 16:24:21 +0200902static const struct snd_kcontrol_new via_hp_mixer = {
903 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
904 .name = "Independent HP",
905 .info = via_independent_hp_info,
906 .get = via_independent_hp_get,
907 .put = via_independent_hp_put,
Harald Welte0aa62ae2008-09-09 15:58:27 +0800908};
909
Takashi Iwai3d83e572010-04-14 14:36:23 +0200910static int via_hp_build(struct hda_codec *codec)
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100911{
Takashi Iwai3d83e572010-04-14 14:36:23 +0200912 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100913 struct snd_kcontrol_new *knew;
914 hda_nid_t nid;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100915
Takashi Iwaiece8d042011-06-19 16:24:21 +0200916 nid = spec->autocfg.hp_pins[0];
917 knew = via_clone_control(spec, &via_hp_mixer);
Takashi Iwai3d83e572010-04-14 14:36:23 +0200918 if (knew == NULL)
919 return -ENOMEM;
920
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100921 knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100922
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100923 return 0;
924}
925
Lydia Wang1564b282009-10-10 19:07:52 +0800926static void notify_aa_path_ctls(struct hda_codec *codec)
927{
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200928 struct via_spec *spec = codec->spec;
Lydia Wang1564b282009-10-10 19:07:52 +0800929 int i;
Lydia Wang1564b282009-10-10 19:07:52 +0800930
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200931 for (i = 0; i < spec->smart51_nums; i++) {
932 struct snd_kcontrol *ctl;
933 struct snd_ctl_elem_id id;
934 memset(&id, 0, sizeof(id));
935 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
936 sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
Lydia Wang525566c2011-04-28 16:03:39 +0800937 ctl = snd_hda_find_mixer_ctl(codec, id.name);
938 if (ctl)
939 snd_ctl_notify(codec->bus->card,
940 SNDRV_CTL_EVENT_MASK_VALUE,
941 &ctl->id);
Lydia Wang1564b282009-10-10 19:07:52 +0800942 }
943}
944
945static void mute_aa_path(struct hda_codec *codec, int mute)
946{
947 struct via_spec *spec = codec->spec;
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200948 int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
Lydia Wang1564b282009-10-10 19:07:52 +0800949 int i;
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200950
Lydia Wang1564b282009-10-10 19:07:52 +0800951 /* check AA path's mute status */
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200952 for (i = 0; i < spec->smart51_nums; i++) {
953 if (spec->smart51_idxs[i] < 0)
954 continue;
955 snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
956 HDA_INPUT, spec->smart51_idxs[i],
Lydia Wang1564b282009-10-10 19:07:52 +0800957 HDA_AMP_MUTE, val);
958 }
959}
Takashi Iwaif4a78282011-06-17 18:46:48 +0200960
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200961static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
962{
963 struct via_spec *spec = codec->spec;
964 int i;
965
966 for (i = 0; i < spec->smart51_nums; i++)
967 if (spec->smart51_pins[i] == pin)
968 return true;
969 return false;
970}
971
Lydia Wang1564b282009-10-10 19:07:52 +0800972static int via_smart51_get(struct snd_kcontrol *kcontrol,
973 struct snd_ctl_elem_value *ucontrol)
974{
975 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
976 struct via_spec *spec = codec->spec;
Lydia Wang1564b282009-10-10 19:07:52 +0800977
Takashi Iwaif2b1c9f2011-06-21 16:52:39 +0200978 *ucontrol->value.integer.value = spec->smart51_enabled;
Lydia Wang1564b282009-10-10 19:07:52 +0800979 return 0;
980}
981
982static int via_smart51_put(struct snd_kcontrol *kcontrol,
983 struct snd_ctl_elem_value *ucontrol)
984{
985 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
986 struct via_spec *spec = codec->spec;
987 int out_in = *ucontrol->value.integer.value
988 ? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
Lydia Wang1564b282009-10-10 19:07:52 +0800989 int i;
990
Takashi Iwaie3d7a142011-06-20 13:52:33 +0200991 for (i = 0; i < spec->smart51_nums; i++) {
992 hda_nid_t nid = spec->smart51_pins[i];
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200993 unsigned int parm;
994
Takashi Iwai7b315bb2010-08-30 13:06:30 +0200995 parm = snd_hda_codec_read(codec, nid, 0,
996 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
997 parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
998 parm |= out_in;
999 snd_hda_codec_write(codec, nid, 0,
1000 AC_VERB_SET_PIN_WIDGET_CONTROL,
1001 parm);
1002 if (out_in == AC_PINCTL_OUT_EN) {
1003 mute_aa_path(codec, 1);
1004 notify_aa_path_ctls(codec);
1005 }
Lydia Wang1564b282009-10-10 19:07:52 +08001006 }
1007 spec->smart51_enabled = *ucontrol->value.integer.value;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08001008 set_widgets_power_state(codec);
Lydia Wang1564b282009-10-10 19:07:52 +08001009 return 1;
1010}
1011
Takashi Iwai5f4b36d2011-06-17 14:55:02 +02001012static const struct snd_kcontrol_new via_smart51_mixer = {
1013 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1014 .name = "Smart 5.1",
1015 .count = 1,
Takashi Iwaif2b1c9f2011-06-21 16:52:39 +02001016 .info = snd_ctl_boolean_mono_info,
Takashi Iwai5f4b36d2011-06-17 14:55:02 +02001017 .get = via_smart51_get,
1018 .put = via_smart51_put,
Lydia Wang1564b282009-10-10 19:07:52 +08001019};
1020
Takashi Iwaif4a78282011-06-17 18:46:48 +02001021static int via_smart51_build(struct hda_codec *codec)
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001022{
Takashi Iwaif4a78282011-06-17 18:46:48 +02001023 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001024
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001025 if (!spec->smart51_nums)
Lydia Wangcb34c202011-04-27 17:44:16 +08001026 return 0;
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001027 if (!via_clone_control(spec, &via_smart51_mixer))
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001028 return -ENOMEM;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001029 return 0;
1030}
1031
Takashi Iwaiada509e2011-06-20 15:40:19 +02001032/* check AA path's mute status */
1033static bool is_aa_path_mute(struct hda_codec *codec)
Lydia Wangf5271102009-10-10 19:07:35 +08001034{
Lydia Wangf5271102009-10-10 19:07:35 +08001035 struct via_spec *spec = codec->spec;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001036 const struct hda_amp_list *p;
1037 int i, ch, v;
1038
1039 for (i = 0; i < spec->num_loopbacks; i++) {
1040 p = &spec->loopback_list[i];
1041 for (ch = 0; ch < 2; ch++) {
1042 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
1043 p->idx);
1044 if (!(v & HDA_AMP_MUTE) && v > 0)
1045 return false;
Lydia Wangf5271102009-10-10 19:07:35 +08001046 }
1047 }
Takashi Iwaiada509e2011-06-20 15:40:19 +02001048 return true;
Lydia Wangf5271102009-10-10 19:07:35 +08001049}
1050
1051/* enter/exit analog low-current mode */
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001052static void __analog_low_current_mode(struct hda_codec *codec, bool force)
Lydia Wangf5271102009-10-10 19:07:35 +08001053{
1054 struct via_spec *spec = codec->spec;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001055 bool enable;
1056 unsigned int verb, parm;
Lydia Wangf5271102009-10-10 19:07:35 +08001057
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001058 if (spec->no_pin_power_ctl)
1059 enable = false;
1060 else
1061 enable = is_aa_path_mute(codec) && !spec->opened_streams;
1062 if (enable == spec->alc_mode && !force)
1063 return;
1064 spec->alc_mode = enable;
Lydia Wangf5271102009-10-10 19:07:35 +08001065
1066 /* decide low current mode's verb & parameter */
1067 switch (spec->codec_type) {
1068 case VT1708B_8CH:
1069 case VT1708B_4CH:
1070 verb = 0xf70;
1071 parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1072 break;
1073 case VT1708S:
Lydia Wangeb7188c2009-10-10 19:08:34 +08001074 case VT1718S:
Lydia Wangf3db4232009-10-10 19:08:41 +08001075 case VT1716S:
Lydia Wangf5271102009-10-10 19:07:35 +08001076 verb = 0xf73;
1077 parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1078 break;
1079 case VT1702:
1080 verb = 0xf73;
1081 parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1082 break;
Lydia Wang25eaba22009-10-10 19:08:43 +08001083 case VT2002P:
Lydia Wangab6734e2009-10-10 19:08:46 +08001084 case VT1812:
Lydia Wang118909562011-03-23 17:57:34 +08001085 case VT1802:
Lydia Wang25eaba22009-10-10 19:08:43 +08001086 verb = 0xf93;
1087 parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
1088 break;
Lydia Wangf5271102009-10-10 19:07:35 +08001089 default:
1090 return; /* other codecs are not supported */
1091 }
1092 /* send verb */
1093 snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1094}
1095
Takashi Iwaie9d010c2012-02-01 10:33:23 +01001096static void analog_low_current_mode(struct hda_codec *codec)
1097{
1098 return __analog_low_current_mode(codec, false);
1099}
1100
Joseph Chanc577b8a2006-11-29 15:29:40 +01001101/*
1102 * generic initialization of ADC, input mixers and output mixers
1103 */
Takashi Iwai096a8852011-06-20 12:09:02 +02001104static const struct hda_verb vt1708_init_verbs[] = {
Lydia Wangaa266fc2011-03-24 12:41:01 +08001105 /* power down jack detect function */
1106 {0x1, 0xf81, 0x1},
Josepch Chanf7278fd2007-12-13 16:40:40 +01001107 { }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001108};
1109
Takashi Iwai3b607e32011-07-18 16:54:40 +02001110static void set_stream_open(struct hda_codec *codec, int bit, bool active)
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001111{
Takashi Iwaiada509e2011-06-20 15:40:19 +02001112 struct via_spec *spec = codec->spec;
1113
1114 if (active)
Takashi Iwai3b607e32011-07-18 16:54:40 +02001115 spec->opened_streams |= bit;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001116 else
Takashi Iwai3b607e32011-07-18 16:54:40 +02001117 spec->opened_streams &= ~bit;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001118 analog_low_current_mode(codec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001119}
1120
Takashi Iwaiece8d042011-06-19 16:24:21 +02001121static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001122 struct hda_codec *codec,
1123 struct snd_pcm_substream *substream)
1124{
1125 struct via_spec *spec = codec->spec;
Takashi Iwai25250502011-06-30 17:24:47 +02001126 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaiada509e2011-06-20 15:40:19 +02001127 int err;
Takashi Iwaiece8d042011-06-19 16:24:21 +02001128
Takashi Iwai25250502011-06-30 17:24:47 +02001129 spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
Takashi Iwai25250502011-06-30 17:24:47 +02001130 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001131 set_stream_open(codec, STREAM_MULTI_OUT, true);
Takashi Iwaiada509e2011-06-20 15:40:19 +02001132 err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
1133 hinfo);
1134 if (err < 0) {
Takashi Iwai3b607e32011-07-18 16:54:40 +02001135 set_stream_open(codec, STREAM_MULTI_OUT, false);
Takashi Iwaiada509e2011-06-20 15:40:19 +02001136 return err;
1137 }
1138 return 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001139}
1140
Takashi Iwaiece8d042011-06-19 16:24:21 +02001141static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
Takashi Iwai9af74212011-06-18 16:17:45 +02001142 struct hda_codec *codec,
1143 struct snd_pcm_substream *substream)
1144{
Takashi Iwai3b607e32011-07-18 16:54:40 +02001145 set_stream_open(codec, STREAM_MULTI_OUT, false);
Takashi Iwai9af74212011-06-18 16:17:45 +02001146 return 0;
1147}
1148
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001149static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
1150 struct hda_codec *codec,
1151 struct snd_pcm_substream *substream)
1152{
1153 struct via_spec *spec = codec->spec;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001154
Takashi Iwaiece8d042011-06-19 16:24:21 +02001155 if (snd_BUG_ON(!spec->hp_dac_nid))
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001156 return -EINVAL;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001157 set_stream_open(codec, STREAM_INDEP_HP, true);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001158 return 0;
1159}
1160
1161static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1162 struct hda_codec *codec,
1163 struct snd_pcm_substream *substream)
1164{
Takashi Iwai3b607e32011-07-18 16:54:40 +02001165 set_stream_open(codec, STREAM_INDEP_HP, false);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001166 return 0;
1167}
1168
1169static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
1170 struct hda_codec *codec,
1171 unsigned int stream_tag,
1172 unsigned int format,
1173 struct snd_pcm_substream *substream)
Harald Welte0aa62ae2008-09-09 15:58:27 +08001174{
1175 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001176
Takashi Iwai3b607e32011-07-18 16:54:40 +02001177 mutex_lock(&spec->config_mutex);
1178 setup_playback_multi_pcm(spec);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001179 snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1180 format, substream);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001181 /* remember for dynamic DAC switch with indep-HP */
1182 spec->active_streams |= STREAM_MULTI_OUT;
1183 spec->cur_dac_stream_tag = stream_tag;
1184 spec->cur_dac_format = format;
1185 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001186 vt1708_update_hp_work(spec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001187 return 0;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001188}
1189
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001190static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
1191 struct hda_codec *codec,
1192 unsigned int stream_tag,
1193 unsigned int format,
1194 struct snd_pcm_substream *substream)
Harald Welte0aa62ae2008-09-09 15:58:27 +08001195{
1196 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001197
Takashi Iwai3b607e32011-07-18 16:54:40 +02001198 mutex_lock(&spec->config_mutex);
1199 if (spec->hp_independent_mode)
1200 snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1201 stream_tag, 0, format);
1202 spec->active_streams |= STREAM_INDEP_HP;
1203 spec->cur_hp_stream_tag = stream_tag;
1204 spec->cur_hp_format = format;
1205 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001206 vt1708_update_hp_work(spec);
Harald Welte0aa62ae2008-09-09 15:58:27 +08001207 return 0;
1208}
1209
1210static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
1211 struct hda_codec *codec,
1212 struct snd_pcm_substream *substream)
1213{
1214 struct via_spec *spec = codec->spec;
Harald Welte0aa62ae2008-09-09 15:58:27 +08001215
Takashi Iwai3b607e32011-07-18 16:54:40 +02001216 mutex_lock(&spec->config_mutex);
Takashi Iwaiece8d042011-06-19 16:24:21 +02001217 snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001218 spec->active_streams &= ~STREAM_MULTI_OUT;
1219 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001220 vt1708_update_hp_work(spec);
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001221 return 0;
1222}
1223
1224static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
1225 struct hda_codec *codec,
1226 struct snd_pcm_substream *substream)
1227{
1228 struct via_spec *spec = codec->spec;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001229
Takashi Iwai3b607e32011-07-18 16:54:40 +02001230 mutex_lock(&spec->config_mutex);
1231 if (spec->hp_independent_mode)
1232 snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
1233 spec->active_streams &= ~STREAM_INDEP_HP;
1234 mutex_unlock(&spec->config_mutex);
Takashi Iwai187d3332011-11-24 16:33:09 +01001235 vt1708_update_hp_work(spec);
Harald Welte0aa62ae2008-09-09 15:58:27 +08001236 return 0;
1237}
1238
Joseph Chanc577b8a2006-11-29 15:29:40 +01001239/*
1240 * Digital out
1241 */
1242static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1243 struct hda_codec *codec,
1244 struct snd_pcm_substream *substream)
1245{
1246 struct via_spec *spec = codec->spec;
1247 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1248}
1249
1250static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1251 struct hda_codec *codec,
1252 struct snd_pcm_substream *substream)
1253{
1254 struct via_spec *spec = codec->spec;
1255 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1256}
1257
Harald Welte5691ec72008-09-15 22:42:26 +08001258static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Harald Welte98aa34c2008-09-09 16:02:09 +08001259 struct hda_codec *codec,
1260 unsigned int stream_tag,
1261 unsigned int format,
1262 struct snd_pcm_substream *substream)
1263{
1264 struct via_spec *spec = codec->spec;
Takashi Iwai9da29272009-05-07 16:31:14 +02001265 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1266 stream_tag, format, substream);
1267}
Harald Welte5691ec72008-09-15 22:42:26 +08001268
Takashi Iwai9da29272009-05-07 16:31:14 +02001269static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1270 struct hda_codec *codec,
1271 struct snd_pcm_substream *substream)
1272{
1273 struct via_spec *spec = codec->spec;
1274 snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
Harald Welte98aa34c2008-09-09 16:02:09 +08001275 return 0;
1276}
1277
Joseph Chanc577b8a2006-11-29 15:29:40 +01001278/*
1279 * Analog capture
1280 */
1281static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1282 struct hda_codec *codec,
1283 unsigned int stream_tag,
1284 unsigned int format,
1285 struct snd_pcm_substream *substream)
1286{
1287 struct via_spec *spec = codec->spec;
1288
1289 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1290 stream_tag, 0, format);
1291 return 0;
1292}
1293
1294static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1295 struct hda_codec *codec,
1296 struct snd_pcm_substream *substream)
1297{
1298 struct via_spec *spec = codec->spec;
Takashi Iwai888afa12008-03-18 09:57:50 +01001299 snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001300 return 0;
1301}
1302
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001303/* analog capture with dynamic ADC switching */
1304static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1305 struct hda_codec *codec,
1306 unsigned int stream_tag,
1307 unsigned int format,
1308 struct snd_pcm_substream *substream)
1309{
1310 struct via_spec *spec = codec->spec;
1311 int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
1312
Takashi Iwai3b607e32011-07-18 16:54:40 +02001313 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001314 spec->cur_adc = spec->adc_nids[adc_idx];
1315 spec->cur_adc_stream_tag = stream_tag;
1316 spec->cur_adc_format = format;
1317 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001318 mutex_unlock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001319 return 0;
1320}
1321
1322static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1323 struct hda_codec *codec,
1324 struct snd_pcm_substream *substream)
1325{
1326 struct via_spec *spec = codec->spec;
1327
Takashi Iwai3b607e32011-07-18 16:54:40 +02001328 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001329 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1330 spec->cur_adc = 0;
Takashi Iwai3b607e32011-07-18 16:54:40 +02001331 mutex_unlock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001332 return 0;
1333}
1334
1335/* re-setup the stream if running; called from input-src put */
1336static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
1337{
1338 struct via_spec *spec = codec->spec;
1339 int adc_idx = spec->inputs[cur].adc_idx;
1340 hda_nid_t adc = spec->adc_nids[adc_idx];
Takashi Iwai3b607e32011-07-18 16:54:40 +02001341 bool ret = false;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001342
Takashi Iwai3b607e32011-07-18 16:54:40 +02001343 mutex_lock(&spec->config_mutex);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001344 if (spec->cur_adc && spec->cur_adc != adc) {
1345 /* stream is running, let's swap the current ADC */
1346 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1347 spec->cur_adc = adc;
1348 snd_hda_codec_setup_stream(codec, adc,
1349 spec->cur_adc_stream_tag, 0,
1350 spec->cur_adc_format);
Takashi Iwai3b607e32011-07-18 16:54:40 +02001351 ret = true;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001352 }
Takashi Iwai3b607e32011-07-18 16:54:40 +02001353 mutex_unlock(&spec->config_mutex);
1354 return ret;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001355}
1356
Takashi Iwai9af74212011-06-18 16:17:45 +02001357static const struct hda_pcm_stream via_pcm_analog_playback = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001358 .substreams = 1,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001359 .channels_min = 2,
1360 .channels_max = 8,
Takashi Iwai9af74212011-06-18 16:17:45 +02001361 /* NID is set in via_build_pcms */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001362 .ops = {
Takashi Iwaiece8d042011-06-19 16:24:21 +02001363 .open = via_playback_multi_pcm_open,
1364 .close = via_playback_multi_pcm_close,
Harald Welte0aa62ae2008-09-09 15:58:27 +08001365 .prepare = via_playback_multi_pcm_prepare,
1366 .cleanup = via_playback_multi_pcm_cleanup
Joseph Chanc577b8a2006-11-29 15:29:40 +01001367 },
1368};
1369
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001370static const struct hda_pcm_stream via_pcm_hp_playback = {
1371 .substreams = 1,
1372 .channels_min = 2,
1373 .channels_max = 2,
1374 /* NID is set in via_build_pcms */
1375 .ops = {
1376 .open = via_playback_hp_pcm_open,
Takashi Iwaiece8d042011-06-19 16:24:21 +02001377 .close = via_playback_hp_pcm_close,
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001378 .prepare = via_playback_hp_pcm_prepare,
1379 .cleanup = via_playback_hp_pcm_cleanup
1380 },
1381};
1382
Takashi Iwai90dd48a2011-05-02 12:38:19 +02001383static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001384 .substreams = 1,
Takashi Iwaibc9b5622008-05-23 17:50:27 +02001385 .channels_min = 2,
1386 .channels_max = 8,
Takashi Iwai9af74212011-06-18 16:17:45 +02001387 /* NID is set in via_build_pcms */
Takashi Iwaibc9b5622008-05-23 17:50:27 +02001388 /* We got noisy outputs on the right channel on VT1708 when
1389 * 24bit samples are used. Until any workaround is found,
1390 * disable the 24bit format, so far.
1391 */
1392 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1393 .ops = {
Takashi Iwaiece8d042011-06-19 16:24:21 +02001394 .open = via_playback_multi_pcm_open,
1395 .close = via_playback_multi_pcm_close,
Lydia Wangc873cc22009-10-10 19:08:21 +08001396 .prepare = via_playback_multi_pcm_prepare,
1397 .cleanup = via_playback_multi_pcm_cleanup
Takashi Iwaibc9b5622008-05-23 17:50:27 +02001398 },
1399};
1400
Takashi Iwai9af74212011-06-18 16:17:45 +02001401static const struct hda_pcm_stream via_pcm_analog_capture = {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001402 .substreams = 1, /* will be changed in via_build_pcms() */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001403 .channels_min = 2,
1404 .channels_max = 2,
Takashi Iwai9af74212011-06-18 16:17:45 +02001405 /* NID is set in via_build_pcms */
Joseph Chanc577b8a2006-11-29 15:29:40 +01001406 .ops = {
1407 .prepare = via_capture_pcm_prepare,
1408 .cleanup = via_capture_pcm_cleanup
1409 },
1410};
1411
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001412static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
1413 .substreams = 1,
1414 .channels_min = 2,
1415 .channels_max = 2,
1416 /* NID is set in via_build_pcms */
1417 .ops = {
1418 .prepare = via_dyn_adc_capture_pcm_prepare,
1419 .cleanup = via_dyn_adc_capture_pcm_cleanup,
1420 },
1421};
1422
Takashi Iwai9af74212011-06-18 16:17:45 +02001423static const struct hda_pcm_stream via_pcm_digital_playback = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001424 .substreams = 1,
1425 .channels_min = 2,
1426 .channels_max = 2,
1427 /* NID is set in via_build_pcms */
1428 .ops = {
1429 .open = via_dig_playback_pcm_open,
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001430 .close = via_dig_playback_pcm_close,
Takashi Iwai9da29272009-05-07 16:31:14 +02001431 .prepare = via_dig_playback_pcm_prepare,
1432 .cleanup = via_dig_playback_pcm_cleanup
Joseph Chanc577b8a2006-11-29 15:29:40 +01001433 },
1434};
1435
Takashi Iwai9af74212011-06-18 16:17:45 +02001436static const struct hda_pcm_stream via_pcm_digital_capture = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001437 .substreams = 1,
1438 .channels_min = 2,
1439 .channels_max = 2,
1440};
1441
Takashi Iwai370bafb2011-06-20 12:47:45 +02001442/*
1443 * slave controls for virtual master
1444 */
Takashi Iwai9322ca52012-02-03 14:28:01 +01001445static const char * const via_slave_pfxs[] = {
1446 "Front", "Surround", "Center", "LFE", "Side",
1447 "Headphone", "Speaker",
Takashi Iwai370bafb2011-06-20 12:47:45 +02001448 NULL,
1449};
1450
Joseph Chanc577b8a2006-11-29 15:29:40 +01001451static int via_build_controls(struct hda_codec *codec)
1452{
1453 struct via_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001454 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001455 int err, i;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001456
Takashi Iwaib5bcc182012-02-02 10:30:17 +01001457 spec->no_pin_power_ctl = 1;
Takashi Iwai24088a52011-06-17 16:59:21 +02001458 if (spec->set_widgets_power_state)
1459 if (!via_clone_control(spec, &via_pin_power_ctl_enum))
1460 return -ENOMEM;
1461
Joseph Chanc577b8a2006-11-29 15:29:40 +01001462 for (i = 0; i < spec->num_mixers; i++) {
1463 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1464 if (err < 0)
1465 return err;
1466 }
1467
1468 if (spec->multiout.dig_out_nid) {
1469 err = snd_hda_create_spdif_out_ctls(codec,
Stephen Warren74b654c2011-06-01 11:14:18 -06001470 spec->multiout.dig_out_nid,
Joseph Chanc577b8a2006-11-29 15:29:40 +01001471 spec->multiout.dig_out_nid);
1472 if (err < 0)
1473 return err;
Takashi Iwai9a081602008-02-12 18:37:26 +01001474 err = snd_hda_create_spdif_share_sw(codec,
1475 &spec->multiout);
1476 if (err < 0)
1477 return err;
1478 spec->multiout.share_spdif = 1;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001479 }
1480 if (spec->dig_in_nid) {
1481 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1482 if (err < 0)
1483 return err;
1484 }
Lydia Wang17314372009-10-10 19:07:37 +08001485
Takashi Iwai370bafb2011-06-20 12:47:45 +02001486 /* if we have no master control, let's create it */
1487 if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1488 unsigned int vmaster_tlv[4];
1489 snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1490 HDA_OUTPUT, vmaster_tlv);
1491 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai9322ca52012-02-03 14:28:01 +01001492 vmaster_tlv, via_slave_pfxs,
1493 "Playback Volume");
Takashi Iwai370bafb2011-06-20 12:47:45 +02001494 if (err < 0)
1495 return err;
1496 }
1497 if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1498 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
Takashi Iwai9322ca52012-02-03 14:28:01 +01001499 NULL, via_slave_pfxs,
1500 "Playback Switch");
Takashi Iwai370bafb2011-06-20 12:47:45 +02001501 if (err < 0)
1502 return err;
1503 }
1504
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001505 /* assign Capture Source enums to NID */
1506 kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1507 for (i = 0; kctl && i < kctl->count; i++) {
Takashi Iwai21949f02009-12-23 08:31:59 +01001508 err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001509 if (err < 0)
1510 return err;
1511 }
1512
Takashi Iwai603c4012008-07-30 15:01:44 +02001513 via_free_kctls(codec); /* no longer needed */
Takashi Iwai01a61e12011-10-28 00:03:22 +02001514
1515 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1516 if (err < 0)
1517 return err;
1518
Joseph Chanc577b8a2006-11-29 15:29:40 +01001519 return 0;
1520}
1521
1522static int via_build_pcms(struct hda_codec *codec)
1523{
1524 struct via_spec *spec = codec->spec;
1525 struct hda_pcm *info = spec->pcm_rec;
1526
Takashi Iwaia5973102011-09-28 16:43:36 +02001527 codec->num_pcms = 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001528 codec->pcm_info = info;
1529
Takashi Iwaia5973102011-09-28 16:43:36 +02001530 if (spec->multiout.num_dacs || spec->num_adc_nids) {
1531 snprintf(spec->stream_name_analog,
1532 sizeof(spec->stream_name_analog),
1533 "%s Analog", codec->chip_name);
1534 info->name = spec->stream_name_analog;
Takashi Iwai9af74212011-06-18 16:17:45 +02001535
Takashi Iwaia5973102011-09-28 16:43:36 +02001536 if (spec->multiout.num_dacs) {
1537 if (!spec->stream_analog_playback)
1538 spec->stream_analog_playback =
1539 &via_pcm_analog_playback;
1540 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1541 *spec->stream_analog_playback;
1542 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1543 spec->multiout.dac_nids[0];
1544 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1545 spec->multiout.max_channels;
1546 }
Takashi Iwai9af74212011-06-18 16:17:45 +02001547
Takashi Iwaia5973102011-09-28 16:43:36 +02001548 if (!spec->stream_analog_capture) {
1549 if (spec->dyn_adc_switch)
1550 spec->stream_analog_capture =
1551 &via_pcm_dyn_adc_analog_capture;
1552 else
1553 spec->stream_analog_capture =
1554 &via_pcm_analog_capture;
1555 }
1556 if (spec->num_adc_nids) {
1557 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1558 *spec->stream_analog_capture;
1559 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1560 spec->adc_nids[0];
1561 if (!spec->dyn_adc_switch)
1562 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
1563 spec->num_adc_nids;
1564 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001565 codec->num_pcms++;
1566 info++;
Takashi Iwaia5973102011-09-28 16:43:36 +02001567 }
1568
1569 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwai82673bc2011-06-17 16:24:21 +02001570 snprintf(spec->stream_name_digital,
1571 sizeof(spec->stream_name_digital),
1572 "%s Digital", codec->chip_name);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001573 info->name = spec->stream_name_digital;
Takashi Iwai7ba72ba2008-02-06 14:03:20 +01001574 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001575 if (spec->multiout.dig_out_nid) {
Takashi Iwai9af74212011-06-18 16:17:45 +02001576 if (!spec->stream_digital_playback)
1577 spec->stream_digital_playback =
1578 &via_pcm_digital_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001579 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
Takashi Iwai9af74212011-06-18 16:17:45 +02001580 *spec->stream_digital_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001581 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1582 spec->multiout.dig_out_nid;
1583 }
1584 if (spec->dig_in_nid) {
Takashi Iwai9af74212011-06-18 16:17:45 +02001585 if (!spec->stream_digital_capture)
1586 spec->stream_digital_capture =
1587 &via_pcm_digital_capture;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001588 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
Takashi Iwai9af74212011-06-18 16:17:45 +02001589 *spec->stream_digital_capture;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001590 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1591 spec->dig_in_nid;
1592 }
Takashi Iwaia5973102011-09-28 16:43:36 +02001593 codec->num_pcms++;
1594 info++;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001595 }
1596
Takashi Iwaiece8d042011-06-19 16:24:21 +02001597 if (spec->hp_dac_nid) {
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001598 snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
1599 "%s HP", codec->chip_name);
1600 info->name = spec->stream_name_hp;
1601 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
1602 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
Takashi Iwaiece8d042011-06-19 16:24:21 +02001603 spec->hp_dac_nid;
Takashi Iwaia5973102011-09-28 16:43:36 +02001604 codec->num_pcms++;
1605 info++;
Takashi Iwai7eb56e82011-06-18 16:40:14 +02001606 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001607 return 0;
1608}
1609
1610static void via_free(struct hda_codec *codec)
1611{
1612 struct via_spec *spec = codec->spec;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001613
1614 if (!spec)
1615 return;
1616
Takashi Iwai603c4012008-07-30 15:01:44 +02001617 via_free_kctls(codec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001618 vt1708_stop_hp_work(spec);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02001619 kfree(spec->bind_cap_vol);
1620 kfree(spec->bind_cap_sw);
1621 kfree(spec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001622}
1623
Takashi Iwai64be2852011-06-17 16:51:39 +02001624/* mute/unmute outputs */
1625static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
1626 hda_nid_t *pins, bool mute)
1627{
1628 int i;
Takashi Iwai94994732011-07-11 11:36:44 +02001629 for (i = 0; i < num_pins; i++) {
1630 unsigned int parm = snd_hda_codec_read(codec, pins[i], 0,
1631 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1632 if (parm & AC_PINCTL_IN_EN)
1633 continue;
1634 if (mute)
1635 parm &= ~AC_PINCTL_OUT_EN;
1636 else
1637 parm |= AC_PINCTL_OUT_EN;
Takashi Iwai64be2852011-06-17 16:51:39 +02001638 snd_hda_codec_write(codec, pins[i], 0,
Takashi Iwai94994732011-07-11 11:36:44 +02001639 AC_VERB_SET_PIN_WIDGET_CONTROL, parm);
1640 }
Takashi Iwai64be2852011-06-17 16:51:39 +02001641}
1642
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001643/* mute internal speaker if line-out is plugged */
1644static void via_line_automute(struct hda_codec *codec, int present)
1645{
1646 struct via_spec *spec = codec->spec;
1647
1648 if (!spec->autocfg.speaker_outs)
1649 return;
1650 if (!present)
1651 present = snd_hda_jack_detect(codec,
1652 spec->autocfg.line_out_pins[0]);
1653 toggle_output_mutes(codec, spec->autocfg.speaker_outs,
1654 spec->autocfg.speaker_pins,
1655 present);
1656}
1657
Harald Welte69e52a82008-09-09 15:57:32 +08001658/* mute internal speaker if HP is plugged */
1659static void via_hp_automute(struct hda_codec *codec)
1660{
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001661 int present = 0;
Takashi Iwai6e969d92011-07-11 11:28:13 +02001662 int nums;
Harald Welte69e52a82008-09-09 15:57:32 +08001663 struct via_spec *spec = codec->spec;
1664
Takashi Iwai187d3332011-11-24 16:33:09 +01001665 if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0] &&
1666 (spec->codec_type != VT1708 || spec->vt1708_jack_detect))
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001667 present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
Takashi Iwai6e969d92011-07-11 11:28:13 +02001668
1669 if (spec->smart51_enabled)
1670 nums = spec->autocfg.line_outs + spec->smart51_nums;
1671 else
1672 nums = spec->autocfg.line_outs;
1673 toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
1674
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001675 via_line_automute(codec, present);
Lydia Wangf3db4232009-10-10 19:08:41 +08001676}
1677
Harald Welte69e52a82008-09-09 15:57:32 +08001678static void via_gpio_control(struct hda_codec *codec)
1679{
1680 unsigned int gpio_data;
1681 unsigned int vol_counter;
1682 unsigned int vol;
1683 unsigned int master_vol;
1684
1685 struct via_spec *spec = codec->spec;
1686
1687 gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
1688 AC_VERB_GET_GPIO_DATA, 0) & 0x03;
1689
1690 vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
1691 0xF84, 0) & 0x3F0000) >> 16;
1692
1693 vol = vol_counter & 0x1F;
1694 master_vol = snd_hda_codec_read(codec, 0x1A, 0,
1695 AC_VERB_GET_AMP_GAIN_MUTE,
1696 AC_AMP_GET_INPUT);
1697
1698 if (gpio_data == 0x02) {
1699 /* unmute line out */
Takashi Iwai3e0693e2011-06-17 16:37:45 +02001700 snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
1701 AC_VERB_SET_PIN_WIDGET_CONTROL,
1702 PIN_OUT);
Harald Welte69e52a82008-09-09 15:57:32 +08001703 if (vol_counter & 0x20) {
1704 /* decrease volume */
1705 if (vol > master_vol)
1706 vol = master_vol;
1707 snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
1708 0, HDA_AMP_VOLMASK,
1709 master_vol-vol);
1710 } else {
1711 /* increase volume */
1712 snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
1713 HDA_AMP_VOLMASK,
1714 ((master_vol+vol) > 0x2A) ? 0x2A :
1715 (master_vol+vol));
1716 }
1717 } else if (!(gpio_data & 0x02)) {
1718 /* mute line out */
Takashi Iwai3e0693e2011-06-17 16:37:45 +02001719 snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
1720 AC_VERB_SET_PIN_WIDGET_CONTROL,
1721 0);
Harald Welte69e52a82008-09-09 15:57:32 +08001722 }
1723}
1724
1725/* unsolicited event for jack sensing */
1726static void via_unsol_event(struct hda_codec *codec,
1727 unsigned int res)
1728{
1729 res >>= 26;
Takashi Iwai3a938972011-10-28 01:16:55 +02001730 res = snd_hda_jack_get_action(codec, res);
Lydia Wangec7e7e42011-03-24 12:43:44 +08001731
Lydia Wanga34df192009-10-10 19:08:01 +08001732 if (res & VIA_JACK_EVENT)
Lydia Wang3e95b9a2011-03-23 15:13:28 +08001733 set_widgets_power_state(codec);
Lydia Wangec7e7e42011-03-24 12:43:44 +08001734
1735 res &= ~VIA_JACK_EVENT;
1736
Takashi Iwai21ce0b62011-07-11 10:33:47 +02001737 if (res == VIA_HP_EVENT || res == VIA_LINE_EVENT)
Lydia Wangec7e7e42011-03-24 12:43:44 +08001738 via_hp_automute(codec);
1739 else if (res == VIA_GPIO_EVENT)
1740 via_gpio_control(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001741 snd_hda_jack_report_sync(codec);
Harald Welte69e52a82008-09-09 15:57:32 +08001742}
1743
Takashi Iwai2a439522011-07-26 09:52:50 +02001744#ifdef CONFIG_PM
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001745static int via_suspend(struct hda_codec *codec, pm_message_t state)
1746{
1747 struct via_spec *spec = codec->spec;
1748 vt1708_stop_hp_work(spec);
1749 return 0;
1750}
1751#endif
1752
Takashi Iwaicb53c622007-08-10 17:21:45 +02001753#ifdef CONFIG_SND_HDA_POWER_SAVE
1754static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1755{
1756 struct via_spec *spec = codec->spec;
1757 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1758}
1759#endif
1760
Joseph Chanc577b8a2006-11-29 15:29:40 +01001761/*
1762 */
Takashi Iwai5d417622011-06-20 11:32:27 +02001763
1764static int via_init(struct hda_codec *codec);
1765
Takashi Iwai90dd48a2011-05-02 12:38:19 +02001766static const struct hda_codec_ops via_patch_ops = {
Joseph Chanc577b8a2006-11-29 15:29:40 +01001767 .build_controls = via_build_controls,
1768 .build_pcms = via_build_pcms,
1769 .init = via_init,
1770 .free = via_free,
Takashi Iwai4a918ff2011-06-20 12:39:26 +02001771 .unsol_event = via_unsol_event,
Takashi Iwai2a439522011-07-26 09:52:50 +02001772#ifdef CONFIG_PM
Lydia Wang1f2e99f2009-10-10 19:08:17 +08001773 .suspend = via_suspend,
1774#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02001775#ifdef CONFIG_SND_HDA_POWER_SAVE
1776 .check_power_status = via_check_power_status,
1777#endif
Joseph Chanc577b8a2006-11-29 15:29:40 +01001778};
1779
Takashi Iwai4a796162011-06-17 17:53:38 +02001780static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
Joseph Chanc577b8a2006-11-29 15:29:40 +01001781{
Takashi Iwai4a796162011-06-17 17:53:38 +02001782 struct via_spec *spec = codec->spec;
1783 int i;
1784
1785 for (i = 0; i < spec->multiout.num_dacs; i++) {
1786 if (spec->multiout.dac_nids[i] == dac)
1787 return false;
1788 }
Takashi Iwaiece8d042011-06-19 16:24:21 +02001789 if (spec->hp_dac_nid == dac)
Takashi Iwai4a796162011-06-17 17:53:38 +02001790 return false;
1791 return true;
1792}
1793
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001794static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3214b962011-07-18 12:49:25 +02001795 hda_nid_t target_dac, int with_aa_mix,
1796 struct nid_path *path, int depth)
Takashi Iwai4a796162011-06-17 17:53:38 +02001797{
Takashi Iwai3214b962011-07-18 12:49:25 +02001798 struct via_spec *spec = codec->spec;
Takashi Iwai4a796162011-06-17 17:53:38 +02001799 hda_nid_t conn[8];
1800 int i, nums;
1801
Takashi Iwai3214b962011-07-18 12:49:25 +02001802 if (nid == spec->aa_mix_nid) {
1803 if (!with_aa_mix)
1804 return false;
1805 with_aa_mix = 2; /* mark aa-mix is included */
1806 }
1807
Takashi Iwai4a796162011-06-17 17:53:38 +02001808 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
1809 for (i = 0; i < nums; i++) {
1810 if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
1811 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02001812 if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
1813 /* aa-mix is requested but not included? */
1814 if (!(spec->aa_mix_nid && with_aa_mix == 1))
1815 goto found;
1816 }
Takashi Iwai4a796162011-06-17 17:53:38 +02001817 }
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001818 if (depth >= MAX_NID_PATH_DEPTH)
Takashi Iwai4a796162011-06-17 17:53:38 +02001819 return false;
1820 for (i = 0; i < nums; i++) {
1821 unsigned int type;
1822 type = get_wcaps_type(get_wcaps(codec, conn[i]));
Takashi Iwai3214b962011-07-18 12:49:25 +02001823 if (type == AC_WID_AUD_OUT)
Takashi Iwai4a796162011-06-17 17:53:38 +02001824 continue;
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001825 if (__parse_output_path(codec, conn[i], target_dac,
Takashi Iwai3214b962011-07-18 12:49:25 +02001826 with_aa_mix, path, depth + 1))
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001827 goto found;
Takashi Iwai4a796162011-06-17 17:53:38 +02001828 }
1829 return false;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001830
1831 found:
1832 path->path[path->depth] = conn[i];
1833 path->idx[path->depth] = i;
1834 if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
1835 path->multi[path->depth] = 1;
1836 path->depth++;
1837 return true;
Takashi Iwai4a796162011-06-17 17:53:38 +02001838}
1839
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001840static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3214b962011-07-18 12:49:25 +02001841 hda_nid_t target_dac, int with_aa_mix,
1842 struct nid_path *path)
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001843{
Takashi Iwai3214b962011-07-18 12:49:25 +02001844 if (__parse_output_path(codec, nid, target_dac, with_aa_mix, path, 1)) {
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001845 path->path[path->depth] = nid;
1846 path->depth++;
Takashi Iwai3214b962011-07-18 12:49:25 +02001847 snd_printdd("output-path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
1848 path->depth, path->path[0], path->path[1],
1849 path->path[2], path->path[3], path->path[4]);
Takashi Iwai8e3679d2011-06-21 09:01:36 +02001850 return true;
1851 }
1852 return false;
1853}
1854
Takashi Iwai4a796162011-06-17 17:53:38 +02001855static int via_auto_fill_dac_nids(struct hda_codec *codec)
1856{
1857 struct via_spec *spec = codec->spec;
1858 const struct auto_pin_cfg *cfg = &spec->autocfg;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001859 int i, dac_num;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001860 hda_nid_t nid;
1861
Joseph Chanc577b8a2006-11-29 15:29:40 +01001862 spec->multiout.dac_nids = spec->private_dac_nids;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001863 dac_num = 0;
Takashi Iwai4a796162011-06-17 17:53:38 +02001864 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai3214b962011-07-18 12:49:25 +02001865 hda_nid_t dac = 0;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001866 nid = cfg->line_out_pins[i];
Takashi Iwai4a796162011-06-17 17:53:38 +02001867 if (!nid)
1868 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02001869 if (parse_output_path(codec, nid, 0, 0, &spec->out_path[i]))
1870 dac = spec->out_path[i].path[0];
1871 if (!i && parse_output_path(codec, nid, dac, 1,
1872 &spec->out_mix_path))
1873 dac = spec->out_mix_path.path[0];
1874 if (dac) {
1875 spec->private_dac_nids[i] = dac;
Lydia Wang5c9a5612011-07-08 14:03:43 +08001876 dac_num++;
1877 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01001878 }
Takashi Iwai3214b962011-07-18 12:49:25 +02001879 if (!spec->out_path[0].depth && spec->out_mix_path.depth) {
1880 spec->out_path[0] = spec->out_mix_path;
1881 spec->out_mix_path.depth = 0;
1882 }
Lydia Wang5c9a5612011-07-08 14:03:43 +08001883 spec->multiout.num_dacs = dac_num;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001884 return 0;
1885}
1886
Takashi Iwai4a796162011-06-17 17:53:38 +02001887static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001888 int chs, bool check_dac, struct nid_path *path)
Joseph Chanc577b8a2006-11-29 15:29:40 +01001889{
Takashi Iwai4a796162011-06-17 17:53:38 +02001890 struct via_spec *spec = codec->spec;
Joseph Chanc577b8a2006-11-29 15:29:40 +01001891 char name[32];
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001892 hda_nid_t dac, pin, sel, nid;
1893 int err;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001894
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001895 dac = check_dac ? path->path[0] : 0;
1896 pin = path->path[path->depth - 1];
1897 sel = path->depth > 1 ? path->path[1] : 0;
Takashi Iwai4a796162011-06-17 17:53:38 +02001898
Takashi Iwai8df2a312011-06-21 11:48:29 +02001899 if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
Takashi Iwai4a796162011-06-17 17:53:38 +02001900 nid = dac;
Takashi Iwai8df2a312011-06-21 11:48:29 +02001901 else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
Takashi Iwai4a796162011-06-17 17:53:38 +02001902 nid = pin;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001903 else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1904 nid = sel;
Takashi Iwai4a796162011-06-17 17:53:38 +02001905 else
1906 nid = 0;
1907 if (nid) {
1908 sprintf(name, "%s Playback Volume", pfx);
1909 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
Lydia Wanga00a5fa2011-06-21 16:11:11 +08001910 HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
Takashi Iwai4a796162011-06-17 17:53:38 +02001911 if (err < 0)
1912 return err;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001913 path->vol_ctl = nid;
Takashi Iwai4a796162011-06-17 17:53:38 +02001914 }
1915
Takashi Iwai8df2a312011-06-21 11:48:29 +02001916 if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
Takashi Iwai4a796162011-06-17 17:53:38 +02001917 nid = dac;
Takashi Iwai8df2a312011-06-21 11:48:29 +02001918 else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
Takashi Iwai4a796162011-06-17 17:53:38 +02001919 nid = pin;
Takashi Iwaia934d5a2011-06-21 14:22:14 +02001920 else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1921 nid = sel;
Takashi Iwai4a796162011-06-17 17:53:38 +02001922 else
1923 nid = 0;
1924 if (nid) {
1925 sprintf(name, "%s Playback Switch", pfx);
1926 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1927 HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1928 if (err < 0)
1929 return err;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02001930 path->mute_ctl = nid;
Takashi Iwai4a796162011-06-17 17:53:38 +02001931 }
1932 return 0;
1933}
1934
Takashi Iwaif4a78282011-06-17 18:46:48 +02001935static void mangle_smart51(struct hda_codec *codec)
1936{
1937 struct via_spec *spec = codec->spec;
1938 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001939 struct auto_pin_cfg_item *ins = cfg->inputs;
1940 int i, j, nums, attr;
1941 int pins[AUTO_CFG_MAX_INS];
Takashi Iwaif4a78282011-06-17 18:46:48 +02001942
Takashi Iwai0f98c242011-06-21 12:51:33 +02001943 for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
1944 nums = 0;
1945 for (i = 0; i < cfg->num_inputs; i++) {
1946 unsigned int def;
1947 if (ins[i].type > AUTO_PIN_LINE_IN)
1948 continue;
1949 def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
1950 if (snd_hda_get_input_pin_attr(def) != attr)
1951 continue;
1952 for (j = 0; j < nums; j++)
1953 if (ins[pins[j]].type < ins[i].type) {
1954 memmove(pins + j + 1, pins + j,
Takashi Iwai21d45d22011-07-08 11:35:11 +02001955 (nums - j) * sizeof(int));
Takashi Iwai0f98c242011-06-21 12:51:33 +02001956 break;
1957 }
1958 pins[j] = i;
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001959 nums++;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001960 }
1961 if (cfg->line_outs + nums < 3)
Takashi Iwaif4a78282011-06-17 18:46:48 +02001962 continue;
Takashi Iwai0f98c242011-06-21 12:51:33 +02001963 for (i = 0; i < nums; i++) {
1964 hda_nid_t pin = ins[pins[i]].pin;
1965 spec->smart51_pins[spec->smart51_nums++] = pin;
1966 cfg->line_out_pins[cfg->line_outs++] = pin;
1967 if (cfg->line_outs == 3)
1968 break;
1969 }
1970 return;
Takashi Iwaif4a78282011-06-17 18:46:48 +02001971 }
1972}
1973
Takashi Iwai020066d2011-07-21 13:45:56 +02001974static void copy_path_mixer_ctls(struct nid_path *dst, struct nid_path *src)
1975{
1976 dst->vol_ctl = src->vol_ctl;
1977 dst->mute_ctl = src->mute_ctl;
1978}
1979
Takashi Iwai4a796162011-06-17 17:53:38 +02001980/* add playback controls from the parsed DAC table */
1981static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
1982{
1983 struct via_spec *spec = codec->spec;
Takashi Iwaif4a78282011-06-17 18:46:48 +02001984 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai3214b962011-07-18 12:49:25 +02001985 struct nid_path *path;
Takashi Iwaiea734962011-01-17 11:29:34 +01001986 static const char * const chname[4] = {
1987 "Front", "Surround", "C/LFE", "Side"
1988 };
Takashi Iwai4a796162011-06-17 17:53:38 +02001989 int i, idx, err;
Takashi Iwaif4a78282011-06-17 18:46:48 +02001990 int old_line_outs;
1991
1992 /* check smart51 */
1993 old_line_outs = cfg->line_outs;
1994 if (cfg->line_outs == 1)
1995 mangle_smart51(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01001996
Takashi Iwaie3d7a142011-06-20 13:52:33 +02001997 err = via_auto_fill_dac_nids(codec);
1998 if (err < 0)
1999 return err;
2000
Lydia Wang5c9a5612011-07-08 14:03:43 +08002001 if (spec->multiout.num_dacs < 3) {
2002 spec->smart51_nums = 0;
2003 cfg->line_outs = old_line_outs;
2004 }
Takashi Iwai4a796162011-06-17 17:53:38 +02002005 for (i = 0; i < cfg->line_outs; i++) {
2006 hda_nid_t pin, dac;
2007 pin = cfg->line_out_pins[i];
2008 dac = spec->multiout.dac_nids[i];
2009 if (!pin || !dac)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002010 continue;
Takashi Iwai3214b962011-07-18 12:49:25 +02002011 path = spec->out_path + i;
Takashi Iwai0fe0adf2011-06-19 16:27:53 +02002012 if (i == HDA_CLFE) {
Takashi Iwai3214b962011-07-18 12:49:25 +02002013 err = create_ch_ctls(codec, "Center", 1, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002014 if (err < 0)
2015 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02002016 err = create_ch_ctls(codec, "LFE", 2, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002017 if (err < 0)
2018 return err;
2019 } else {
Takashi Iwai6aadf412011-06-20 14:09:02 +02002020 const char *pfx = chname[i];
2021 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2022 cfg->line_outs == 1)
2023 pfx = "Speaker";
Takashi Iwai3214b962011-07-18 12:49:25 +02002024 err = create_ch_ctls(codec, pfx, 3, true, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002025 if (err < 0)
2026 return err;
2027 }
Takashi Iwai020066d2011-07-21 13:45:56 +02002028 if (path != spec->out_path + i)
2029 copy_path_mixer_ctls(&spec->out_path[i], path);
2030 if (path == spec->out_path && spec->out_mix_path.depth)
2031 copy_path_mixer_ctls(&spec->out_mix_path, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002032 }
2033
Takashi Iwai4a796162011-06-17 17:53:38 +02002034 idx = get_connection_index(codec, spec->aa_mix_nid,
2035 spec->multiout.dac_nids[0]);
2036 if (idx >= 0) {
2037 /* add control to mixer */
Takashi Iwai3214b962011-07-18 12:49:25 +02002038 const char *name;
2039 name = spec->out_mix_path.depth ?
2040 "PCM Loopback Playback Volume" : "PCM Playback Volume";
2041 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
Takashi Iwai4a796162011-06-17 17:53:38 +02002042 HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2043 idx, HDA_INPUT));
2044 if (err < 0)
2045 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02002046 name = spec->out_mix_path.depth ?
2047 "PCM Loopback Playback Switch" : "PCM Playback Switch";
2048 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
Takashi Iwai4a796162011-06-17 17:53:38 +02002049 HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2050 idx, HDA_INPUT));
2051 if (err < 0)
2052 return err;
2053 }
2054
Takashi Iwaif4a78282011-06-17 18:46:48 +02002055 cfg->line_outs = old_line_outs;
2056
Joseph Chanc577b8a2006-11-29 15:29:40 +01002057 return 0;
2058}
2059
Takashi Iwai4a796162011-06-17 17:53:38 +02002060static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002061{
Takashi Iwai4a796162011-06-17 17:53:38 +02002062 struct via_spec *spec = codec->spec;
Takashi Iwai09a9ad692011-06-21 15:57:44 +02002063 struct nid_path *path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002064 bool check_dac;
Takashi Iwai3214b962011-07-18 12:49:25 +02002065 int i, err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002066
2067 if (!pin)
2068 return 0;
2069
Takashi Iwai3214b962011-07-18 12:49:25 +02002070 if (!parse_output_path(codec, pin, 0, 0, &spec->hp_indep_path)) {
2071 for (i = HDA_SIDE; i >= HDA_CLFE; i--) {
2072 if (i < spec->multiout.num_dacs &&
2073 parse_output_path(codec, pin,
2074 spec->multiout.dac_nids[i], 0,
2075 &spec->hp_indep_path)) {
2076 spec->hp_indep_shared = i;
2077 break;
2078 }
2079 }
Takashi Iwai25250502011-06-30 17:24:47 +02002080 }
Takashi Iwai3214b962011-07-18 12:49:25 +02002081 if (spec->hp_indep_path.depth) {
2082 spec->hp_dac_nid = spec->hp_indep_path.path[0];
2083 if (!spec->hp_indep_shared)
2084 spec->hp_path = spec->hp_indep_path;
2085 }
2086 /* optionally check front-path w/o AA-mix */
2087 if (!spec->hp_path.depth)
2088 parse_output_path(codec, pin,
2089 spec->multiout.dac_nids[HDA_FRONT], 0,
2090 &spec->hp_path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002091
Takashi Iwaiece8d042011-06-19 16:24:21 +02002092 if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
Takashi Iwai3214b962011-07-18 12:49:25 +02002093 1, &spec->hp_mix_path) && !spec->hp_path.depth)
Takashi Iwaiece8d042011-06-19 16:24:21 +02002094 return 0;
2095
Takashi Iwai3214b962011-07-18 12:49:25 +02002096 if (spec->hp_path.depth) {
Takashi Iwai09a9ad692011-06-21 15:57:44 +02002097 path = &spec->hp_path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002098 check_dac = true;
2099 } else {
Takashi Iwai3214b962011-07-18 12:49:25 +02002100 path = &spec->hp_mix_path;
Takashi Iwai18bd2c42011-07-04 15:55:44 +02002101 check_dac = false;
2102 }
2103 err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002104 if (err < 0)
2105 return err;
Takashi Iwai020066d2011-07-21 13:45:56 +02002106 if (check_dac)
2107 copy_path_mixer_ctls(&spec->hp_mix_path, path);
2108 else
2109 copy_path_mixer_ctls(&spec->hp_path, path);
2110 if (spec->hp_indep_path.depth)
2111 copy_path_mixer_ctls(&spec->hp_indep_path, path);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002112 return 0;
2113}
2114
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002115static int via_auto_create_speaker_ctls(struct hda_codec *codec)
2116{
2117 struct via_spec *spec = codec->spec;
Takashi Iwai3214b962011-07-18 12:49:25 +02002118 struct nid_path *path;
2119 bool check_dac;
Wang Shaoyan81c0a782011-08-05 18:51:29 +08002120 hda_nid_t pin, dac = 0;
Takashi Iwai3214b962011-07-18 12:49:25 +02002121 int err;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002122
2123 pin = spec->autocfg.speaker_pins[0];
2124 if (!spec->autocfg.speaker_outs || !pin)
2125 return 0;
2126
Takashi Iwai3214b962011-07-18 12:49:25 +02002127 if (parse_output_path(codec, pin, 0, 0, &spec->speaker_path))
Takashi Iwai8e3679d2011-06-21 09:01:36 +02002128 dac = spec->speaker_path.path[0];
Takashi Iwai3214b962011-07-18 12:49:25 +02002129 if (!dac)
2130 parse_output_path(codec, pin,
2131 spec->multiout.dac_nids[HDA_FRONT], 0,
2132 &spec->speaker_path);
2133 if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
2134 1, &spec->speaker_mix_path) && !dac)
2135 return 0;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002136
Takashi Iwai3214b962011-07-18 12:49:25 +02002137 /* no AA-path for front? */
2138 if (!spec->out_mix_path.depth && spec->speaker_mix_path.depth)
2139 dac = 0;
2140
2141 spec->speaker_dac_nid = dac;
2142 spec->multiout.extra_out_nid[0] = dac;
2143 if (dac) {
2144 path = &spec->speaker_path;
2145 check_dac = true;
2146 } else {
2147 path = &spec->speaker_mix_path;
2148 check_dac = false;
2149 }
2150 err = create_ch_ctls(codec, "Speaker", 3, check_dac, path);
2151 if (err < 0)
2152 return err;
Takashi Iwai020066d2011-07-21 13:45:56 +02002153 if (check_dac)
2154 copy_path_mixer_ctls(&spec->speaker_mix_path, path);
2155 else
2156 copy_path_mixer_ctls(&spec->speaker_path, path);
Takashi Iwai3214b962011-07-18 12:49:25 +02002157 return 0;
2158}
2159
2160#define via_aamix_ctl_info via_pin_power_ctl_info
2161
2162static int via_aamix_ctl_get(struct snd_kcontrol *kcontrol,
2163 struct snd_ctl_elem_value *ucontrol)
2164{
2165 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2166 struct via_spec *spec = codec->spec;
2167 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2168 return 0;
2169}
2170
2171static void update_aamix_paths(struct hda_codec *codec, int do_mix,
2172 struct nid_path *nomix, struct nid_path *mix)
2173{
2174 if (do_mix) {
2175 activate_output_path(codec, nomix, false, false);
2176 activate_output_path(codec, mix, true, false);
2177 } else {
2178 activate_output_path(codec, mix, false, false);
2179 activate_output_path(codec, nomix, true, false);
2180 }
2181}
2182
2183static int via_aamix_ctl_put(struct snd_kcontrol *kcontrol,
2184 struct snd_ctl_elem_value *ucontrol)
2185{
2186 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2187 struct via_spec *spec = codec->spec;
2188 unsigned int val = ucontrol->value.enumerated.item[0];
2189
2190 if (val == spec->aamix_mode)
2191 return 0;
2192 spec->aamix_mode = val;
2193 /* update front path */
2194 update_aamix_paths(codec, val, &spec->out_path[0], &spec->out_mix_path);
2195 /* update HP path */
2196 if (!spec->hp_independent_mode) {
2197 update_aamix_paths(codec, val, &spec->hp_path,
2198 &spec->hp_mix_path);
2199 }
2200 /* update speaker path */
2201 update_aamix_paths(codec, val, &spec->speaker_path,
2202 &spec->speaker_mix_path);
2203 return 1;
2204}
2205
2206static const struct snd_kcontrol_new via_aamix_ctl_enum = {
2207 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2208 .name = "Loopback Mixing",
2209 .info = via_aamix_ctl_info,
2210 .get = via_aamix_ctl_get,
2211 .put = via_aamix_ctl_put,
2212};
2213
2214static int via_auto_create_loopback_switch(struct hda_codec *codec)
2215{
2216 struct via_spec *spec = codec->spec;
2217
Takashi Iwai4808d122012-01-10 15:16:02 +01002218 if (!spec->aa_mix_nid)
2219 return 0; /* no loopback switching available */
2220 if (!(spec->out_mix_path.depth || spec->hp_mix_path.depth ||
2221 spec->speaker_path.depth))
Takashi Iwai3214b962011-07-18 12:49:25 +02002222 return 0; /* no loopback switching available */
2223 if (!via_clone_control(spec, &via_aamix_ctl_enum))
2224 return -ENOMEM;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002225 return 0;
2226}
2227
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002228/* look for ADCs */
2229static int via_fill_adcs(struct hda_codec *codec)
2230{
2231 struct via_spec *spec = codec->spec;
2232 hda_nid_t nid = codec->start_nid;
2233 int i;
2234
2235 for (i = 0; i < codec->num_nodes; i++, nid++) {
2236 unsigned int wcaps = get_wcaps(codec, nid);
2237 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2238 continue;
2239 if (wcaps & AC_WCAP_DIGITAL)
2240 continue;
2241 if (!(wcaps & AC_WCAP_CONN_LIST))
2242 continue;
2243 if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2244 return -ENOMEM;
2245 spec->adc_nids[spec->num_adc_nids++] = nid;
2246 }
2247 return 0;
2248}
2249
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002250/* input-src control */
2251static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
2252 struct snd_ctl_elem_info *uinfo)
2253{
2254 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2255 struct via_spec *spec = codec->spec;
2256
2257 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2258 uinfo->count = 1;
2259 uinfo->value.enumerated.items = spec->num_inputs;
2260 if (uinfo->value.enumerated.item >= spec->num_inputs)
2261 uinfo->value.enumerated.item = spec->num_inputs - 1;
2262 strcpy(uinfo->value.enumerated.name,
2263 spec->inputs[uinfo->value.enumerated.item].label);
2264 return 0;
2265}
2266
2267static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
2268 struct snd_ctl_elem_value *ucontrol)
2269{
2270 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2271 struct via_spec *spec = codec->spec;
2272 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2273
2274 ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
2275 return 0;
2276}
2277
2278static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
2279 struct snd_ctl_elem_value *ucontrol)
2280{
2281 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2282 struct via_spec *spec = codec->spec;
2283 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2284 hda_nid_t mux;
2285 int cur;
2286
2287 cur = ucontrol->value.enumerated.item[0];
2288 if (cur < 0 || cur >= spec->num_inputs)
2289 return -EINVAL;
2290 if (spec->cur_mux[idx] == cur)
2291 return 0;
2292 spec->cur_mux[idx] = cur;
2293 if (spec->dyn_adc_switch) {
2294 int adc_idx = spec->inputs[cur].adc_idx;
2295 mux = spec->mux_nids[adc_idx];
2296 via_dyn_adc_pcm_resetup(codec, cur);
2297 } else {
2298 mux = spec->mux_nids[idx];
2299 if (snd_BUG_ON(!mux))
2300 return -EINVAL;
2301 }
2302
2303 if (mux) {
2304 /* switch to D0 beofre change index */
Takashi Iwai054d8672012-01-24 12:25:50 +01002305 update_power_state(codec, mux, AC_PWRST_D0);
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002306 snd_hda_codec_write(codec, mux, 0,
2307 AC_VERB_SET_CONNECT_SEL,
2308 spec->inputs[cur].mux_idx);
2309 }
2310
2311 /* update jack power state */
2312 set_widgets_power_state(codec);
2313 return 0;
2314}
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002315
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002316static const struct snd_kcontrol_new via_input_src_ctl = {
2317 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2318 /* The multiple "Capture Source" controls confuse alsamixer
2319 * So call somewhat different..
2320 */
2321 /* .name = "Capture Source", */
2322 .name = "Input Source",
2323 .info = via_mux_enum_info,
2324 .get = via_mux_enum_get,
2325 .put = via_mux_enum_put,
2326};
2327
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002328static int create_input_src_ctls(struct hda_codec *codec, int count)
2329{
2330 struct via_spec *spec = codec->spec;
2331 struct snd_kcontrol_new *knew;
2332
2333 if (spec->num_inputs <= 1 || !count)
2334 return 0; /* no need for single src */
2335
2336 knew = via_clone_control(spec, &via_input_src_ctl);
2337 if (!knew)
2338 return -ENOMEM;
2339 knew->count = count;
2340 return 0;
2341}
2342
2343/* add the powersave loopback-list entry */
Takashi Iwai13af8e72011-06-20 14:05:46 +02002344static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
2345{
2346 struct hda_amp_list *list;
2347
2348 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2349 return;
2350 list = spec->loopback_list + spec->num_loopbacks;
2351 list->nid = mix;
2352 list->dir = HDA_INPUT;
2353 list->idx = idx;
2354 spec->num_loopbacks++;
2355 spec->loopback.amplist = spec->loopback_list;
2356}
Takashi Iwai13af8e72011-06-20 14:05:46 +02002357
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002358static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
Takashi Iwai8d087c72011-06-28 12:45:47 +02002359 hda_nid_t dst)
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002360{
Takashi Iwai8d087c72011-06-28 12:45:47 +02002361 return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002362}
2363
2364/* add the input-route to the given pin */
2365static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002366{
Takashi Iwai10a20af2010-09-09 16:28:02 +02002367 struct via_spec *spec = codec->spec;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002368 int c, idx;
2369
2370 spec->inputs[spec->num_inputs].adc_idx = -1;
2371 spec->inputs[spec->num_inputs].pin = pin;
2372 for (c = 0; c < spec->num_adc_nids; c++) {
2373 if (spec->mux_nids[c]) {
2374 idx = get_connection_index(codec, spec->mux_nids[c],
2375 pin);
2376 if (idx < 0)
2377 continue;
2378 spec->inputs[spec->num_inputs].mux_idx = idx;
2379 } else {
Takashi Iwai8d087c72011-06-28 12:45:47 +02002380 if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002381 continue;
2382 }
2383 spec->inputs[spec->num_inputs].adc_idx = c;
2384 /* Can primary ADC satisfy all inputs? */
2385 if (!spec->dyn_adc_switch &&
2386 spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
2387 snd_printd(KERN_INFO
2388 "via: dynamic ADC switching enabled\n");
2389 spec->dyn_adc_switch = 1;
2390 }
2391 return true;
2392 }
2393 return false;
2394}
2395
2396static int get_mux_nids(struct hda_codec *codec);
2397
2398/* parse input-routes; fill ADCs, MUXs and input-src entries */
2399static int parse_analog_inputs(struct hda_codec *codec)
2400{
2401 struct via_spec *spec = codec->spec;
2402 const struct auto_pin_cfg *cfg = &spec->autocfg;
2403 int i, err;
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002404
2405 err = via_fill_adcs(codec);
2406 if (err < 0)
2407 return err;
2408 err = get_mux_nids(codec);
2409 if (err < 0)
2410 return err;
Takashi Iwaia766d0d2011-06-17 09:01:29 +02002411
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002412 /* fill all input-routes */
2413 for (i = 0; i < cfg->num_inputs; i++) {
2414 if (add_input_route(codec, cfg->inputs[i].pin))
2415 spec->inputs[spec->num_inputs++].label =
2416 hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwaif3268512010-08-30 11:00:19 +02002417 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002418
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002419 /* check for internal loopback recording */
2420 if (spec->aa_mix_nid &&
2421 add_input_route(codec, spec->aa_mix_nid))
2422 spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
2423
2424 return 0;
2425}
2426
2427/* create analog-loopback volume/switch controls */
2428static int create_loopback_ctls(struct hda_codec *codec)
2429{
2430 struct via_spec *spec = codec->spec;
2431 const struct auto_pin_cfg *cfg = &spec->autocfg;
2432 const char *prev_label = NULL;
2433 int type_idx = 0;
2434 int i, j, err, idx;
2435
2436 if (!spec->aa_mix_nid)
2437 return 0;
2438
Takashi Iwai7b315bb2010-08-30 13:06:30 +02002439 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002440 hda_nid_t pin = cfg->inputs[i].pin;
2441 const char *label = hda_get_autocfg_input_label(codec, cfg, i);
2442
Takashi Iwai1e11cae2011-06-21 12:57:22 +02002443 if (prev_label && !strcmp(label, prev_label))
Takashi Iwai7b315bb2010-08-30 13:06:30 +02002444 type_idx++;
2445 else
2446 type_idx = 0;
Takashi Iwai1e11cae2011-06-21 12:57:22 +02002447 prev_label = label;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002448 idx = get_connection_index(codec, spec->aa_mix_nid, pin);
2449 if (idx >= 0) {
Lydia Wang16922282011-03-22 16:24:10 +08002450 err = via_new_analog_input(spec, label, type_idx,
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002451 idx, spec->aa_mix_nid);
Takashi Iwai13af8e72011-06-20 14:05:46 +02002452 if (err < 0)
2453 return err;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002454 add_loopback_list(spec, spec->aa_mix_nid, idx);
Takashi Iwai13af8e72011-06-20 14:05:46 +02002455 }
Takashi Iwaie3d7a142011-06-20 13:52:33 +02002456
2457 /* remember the label for smart51 control */
2458 for (j = 0; j < spec->smart51_nums; j++) {
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002459 if (spec->smart51_pins[j] == pin) {
Takashi Iwaie3d7a142011-06-20 13:52:33 +02002460 spec->smart51_idxs[j] = idx;
2461 spec->smart51_labels[j] = label;
2462 break;
2463 }
2464 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002465 }
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002466 return 0;
2467}
2468
2469/* create mic-boost controls (if present) */
2470static int create_mic_boost_ctls(struct hda_codec *codec)
2471{
2472 struct via_spec *spec = codec->spec;
2473 const struct auto_pin_cfg *cfg = &spec->autocfg;
2474 int i, err;
2475
2476 for (i = 0; i < cfg->num_inputs; i++) {
2477 hda_nid_t pin = cfg->inputs[i].pin;
2478 unsigned int caps;
2479 const char *label;
2480 char name[32];
2481
2482 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2483 continue;
2484 caps = query_amp_caps(codec, pin, HDA_INPUT);
2485 if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2486 continue;
2487 label = hda_get_autocfg_input_label(codec, cfg, i);
2488 snprintf(name, sizeof(name), "%s Boost Volume", label);
2489 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2490 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2491 if (err < 0)
2492 return err;
2493 }
2494 return 0;
2495}
2496
2497/* create capture and input-src controls for multiple streams */
2498static int create_multi_adc_ctls(struct hda_codec *codec)
2499{
2500 struct via_spec *spec = codec->spec;
2501 int i, err;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002502
2503 /* create capture mixer elements */
2504 for (i = 0; i < spec->num_adc_nids; i++) {
2505 hda_nid_t adc = spec->adc_nids[i];
2506 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2507 "Capture Volume", i,
2508 HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2509 HDA_INPUT));
2510 if (err < 0)
2511 return err;
2512 err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2513 "Capture Switch", i,
2514 HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2515 HDA_INPUT));
2516 if (err < 0)
2517 return err;
2518 }
2519
2520 /* input-source control */
2521 for (i = 0; i < spec->num_adc_nids; i++)
2522 if (!spec->mux_nids[i])
2523 break;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002524 err = create_input_src_ctls(codec, i);
2525 if (err < 0)
2526 return err;
2527 return 0;
2528}
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002529
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002530/* bind capture volume/switch */
2531static struct snd_kcontrol_new via_bind_cap_vol_ctl =
2532 HDA_BIND_VOL("Capture Volume", 0);
2533static struct snd_kcontrol_new via_bind_cap_sw_ctl =
2534 HDA_BIND_SW("Capture Switch", 0);
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002535
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002536static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
2537 struct hda_ctl_ops *ops)
2538{
2539 struct hda_bind_ctls *ctl;
2540 int i;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02002541
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002542 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
2543 if (!ctl)
2544 return -ENOMEM;
2545 ctl->ops = ops;
2546 for (i = 0; i < spec->num_adc_nids; i++)
2547 ctl->values[i] =
2548 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
2549 *ctl_ret = ctl;
2550 return 0;
2551}
2552
2553/* create capture and input-src controls for dynamic ADC-switch case */
2554static int create_dyn_adc_ctls(struct hda_codec *codec)
2555{
2556 struct via_spec *spec = codec->spec;
2557 struct snd_kcontrol_new *knew;
2558 int err;
2559
2560 /* set up the bind capture ctls */
2561 err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
2562 if (err < 0)
2563 return err;
2564 err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
2565 if (err < 0)
2566 return err;
2567
2568 /* create capture mixer elements */
2569 knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
2570 if (!knew)
2571 return -ENOMEM;
2572 knew->private_value = (long)spec->bind_cap_vol;
2573
2574 knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
2575 if (!knew)
2576 return -ENOMEM;
2577 knew->private_value = (long)spec->bind_cap_sw;
2578
2579 /* input-source control */
2580 err = create_input_src_ctls(codec, 1);
2581 if (err < 0)
2582 return err;
2583 return 0;
2584}
2585
2586/* parse and create capture-related stuff */
2587static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
2588{
2589 struct via_spec *spec = codec->spec;
2590 int err;
2591
2592 err = parse_analog_inputs(codec);
2593 if (err < 0)
2594 return err;
2595 if (spec->dyn_adc_switch)
2596 err = create_dyn_adc_ctls(codec);
2597 else
2598 err = create_multi_adc_ctls(codec);
2599 if (err < 0)
2600 return err;
2601 err = create_loopback_ctls(codec);
2602 if (err < 0)
2603 return err;
2604 err = create_mic_boost_ctls(codec);
2605 if (err < 0)
2606 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002607 return 0;
2608}
2609
Harald Welte76d9b0d2008-09-09 15:50:37 +08002610static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
2611{
2612 unsigned int def_conf;
2613 unsigned char seqassoc;
2614
Takashi Iwai2f334f92009-02-20 14:37:42 +01002615 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Harald Welte76d9b0d2008-09-09 15:50:37 +08002616 seqassoc = (unsigned char) get_defcfg_association(def_conf);
2617 seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
Lydia Wang82ef9e42009-10-10 19:08:19 +08002618 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
2619 && (seqassoc == 0xf0 || seqassoc == 0xff)) {
2620 def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
2621 snd_hda_codec_set_pincfg(codec, nid, def_conf);
Harald Welte76d9b0d2008-09-09 15:50:37 +08002622 }
2623
2624 return;
2625}
2626
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002627static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002628 struct snd_ctl_elem_value *ucontrol)
2629{
2630 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2631 struct via_spec *spec = codec->spec;
2632
2633 if (spec->codec_type != VT1708)
2634 return 0;
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002635 ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002636 return 0;
2637}
2638
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002639static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002640 struct snd_ctl_elem_value *ucontrol)
2641{
2642 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2643 struct via_spec *spec = codec->spec;
Takashi Iwai187d3332011-11-24 16:33:09 +01002644 int val;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002645
2646 if (spec->codec_type != VT1708)
2647 return 0;
Takashi Iwai187d3332011-11-24 16:33:09 +01002648 val = !!ucontrol->value.integer.value[0];
2649 if (spec->vt1708_jack_detect == val)
2650 return 0;
2651 spec->vt1708_jack_detect = val;
2652 if (spec->vt1708_jack_detect &&
2653 snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") != 1) {
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002654 mute_aa_path(codec, 1);
2655 notify_aa_path_ctls(codec);
2656 }
Takashi Iwai187d3332011-11-24 16:33:09 +01002657 via_hp_automute(codec);
2658 vt1708_update_hp_work(spec);
2659 return 1;
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002660}
2661
Takashi Iwaie06e5a22011-06-17 15:46:13 +02002662static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
2663 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2664 .name = "Jack Detect",
2665 .count = 1,
2666 .info = snd_ctl_boolean_mono_info,
2667 .get = vt1708_jack_detect_get,
2668 .put = vt1708_jack_detect_put,
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002669};
2670
Takashi Iwai12daef62011-06-18 17:45:49 +02002671static void fill_dig_outs(struct hda_codec *codec);
2672static void fill_dig_in(struct hda_codec *codec);
2673
2674static int via_parse_auto_config(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002675{
2676 struct via_spec *spec = codec->spec;
2677 int err;
2678
2679 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2680 if (err < 0)
2681 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002682 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
Takashi Iwai7f0df882011-06-18 17:33:34 +02002683 return -EINVAL;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002684
Takashi Iwai4a796162011-06-17 17:53:38 +02002685 err = via_auto_create_multi_out_ctls(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002686 if (err < 0)
2687 return err;
Takashi Iwai4a796162011-06-17 17:53:38 +02002688 err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002689 if (err < 0)
2690 return err;
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002691 err = via_auto_create_speaker_ctls(codec);
2692 if (err < 0)
2693 return err;
Takashi Iwai3214b962011-07-18 12:49:25 +02002694 err = via_auto_create_loopback_switch(codec);
2695 if (err < 0)
2696 return err;
Takashi Iwaia86a88e2011-06-22 15:23:25 +02002697 err = via_auto_create_analog_input_ctls(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002698 if (err < 0)
2699 return err;
2700
2701 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2702
Takashi Iwai12daef62011-06-18 17:45:49 +02002703 fill_dig_outs(codec);
2704 fill_dig_in(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002705
Takashi Iwai603c4012008-07-30 15:01:44 +02002706 if (spec->kctls.list)
2707 spec->mixers[spec->num_mixers++] = spec->kctls.list;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002708
Joseph Chanc577b8a2006-11-29 15:29:40 +01002709
Takashi Iwai3214b962011-07-18 12:49:25 +02002710 if (spec->hp_dac_nid && spec->hp_mix_path.depth) {
Takashi Iwaiece8d042011-06-19 16:24:21 +02002711 err = via_hp_build(codec);
2712 if (err < 0)
2713 return err;
2714 }
Joseph Chanc577b8a2006-11-29 15:29:40 +01002715
Takashi Iwaif4a78282011-06-17 18:46:48 +02002716 err = via_smart51_build(codec);
2717 if (err < 0)
2718 return err;
2719
Takashi Iwai5d417622011-06-20 11:32:27 +02002720 /* assign slave outs */
2721 if (spec->slave_dig_outs[0])
2722 codec->slave_dig_outs = spec->slave_dig_outs;
2723
Joseph Chanc577b8a2006-11-29 15:29:40 +01002724 return 1;
2725}
2726
Takashi Iwai5d417622011-06-20 11:32:27 +02002727static void via_auto_init_dig_outs(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002728{
Lydia Wang25eaba22009-10-10 19:08:43 +08002729 struct via_spec *spec = codec->spec;
Takashi Iwai5d417622011-06-20 11:32:27 +02002730 if (spec->multiout.dig_out_nid)
2731 init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
2732 if (spec->slave_dig_outs[0])
2733 init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
2734}
Lydia Wang25eaba22009-10-10 19:08:43 +08002735
Takashi Iwai5d417622011-06-20 11:32:27 +02002736static void via_auto_init_dig_in(struct hda_codec *codec)
2737{
2738 struct via_spec *spec = codec->spec;
2739 if (!spec->dig_in_nid)
2740 return;
2741 snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
2742 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
2743}
2744
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002745/* initialize the unsolicited events */
2746static void via_auto_init_unsol_event(struct hda_codec *codec)
2747{
2748 struct via_spec *spec = codec->spec;
2749 struct auto_pin_cfg *cfg = &spec->autocfg;
2750 unsigned int ev;
2751 int i;
2752
2753 if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002754 snd_hda_jack_detect_enable(codec, cfg->hp_pins[0],
2755 VIA_HP_EVENT | VIA_JACK_EVENT);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002756
2757 if (cfg->speaker_pins[0])
2758 ev = VIA_LINE_EVENT;
2759 else
2760 ev = 0;
2761 for (i = 0; i < cfg->line_outs; i++) {
2762 if (cfg->line_out_pins[i] &&
2763 is_jack_detectable(codec, cfg->line_out_pins[i]))
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002764 snd_hda_jack_detect_enable(codec, cfg->line_out_pins[i],
2765 ev | VIA_JACK_EVENT);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002766 }
2767
2768 for (i = 0; i < cfg->num_inputs; i++) {
2769 if (is_jack_detectable(codec, cfg->inputs[i].pin))
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002770 snd_hda_jack_detect_enable(codec, cfg->inputs[i].pin,
2771 VIA_JACK_EVENT);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002772 }
2773}
2774
Takashi Iwai5d417622011-06-20 11:32:27 +02002775static int via_init(struct hda_codec *codec)
2776{
2777 struct via_spec *spec = codec->spec;
2778 int i;
2779
2780 for (i = 0; i < spec->num_iverbs; i++)
2781 snd_hda_sequence_write(codec, spec->init_verbs[i]);
2782
Takashi Iwaie9d010c2012-02-01 10:33:23 +01002783 /* init power states */
2784 set_widgets_power_state(codec);
2785 __analog_low_current_mode(codec, true);
2786
Joseph Chanc577b8a2006-11-29 15:29:40 +01002787 via_auto_init_multi_out(codec);
2788 via_auto_init_hp_out(codec);
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002789 via_auto_init_speaker_out(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002790 via_auto_init_analog_input(codec);
Takashi Iwai5d417622011-06-20 11:32:27 +02002791 via_auto_init_dig_outs(codec);
2792 via_auto_init_dig_in(codec);
Lydia Wang118909562011-03-23 17:57:34 +08002793
Takashi Iwai4a918ff2011-06-20 12:39:26 +02002794 via_auto_init_unsol_event(codec);
2795
2796 via_hp_automute(codec);
Takashi Iwai187d3332011-11-24 16:33:09 +01002797 vt1708_update_hp_work(spec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02002798 snd_hda_jack_report_sync(codec);
Lydia Wang25eaba22009-10-10 19:08:43 +08002799
Joseph Chanc577b8a2006-11-29 15:29:40 +01002800 return 0;
2801}
2802
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002803static void vt1708_update_hp_jack_state(struct work_struct *work)
2804{
2805 struct via_spec *spec = container_of(work, struct via_spec,
2806 vt1708_hp_work.work);
2807 if (spec->codec_type != VT1708)
2808 return;
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002809 snd_hda_jack_set_dirty_all(spec->codec);
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002810 /* if jack state toggled */
2811 if (spec->vt1708_hp_present
Takashi Iwaid56757a2009-11-18 08:00:14 +01002812 != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002813 spec->vt1708_hp_present ^= 1;
2814 via_hp_automute(spec->codec);
2815 }
Takashi Iwai187d3332011-11-24 16:33:09 +01002816 if (spec->vt1708_jack_detect)
2817 schedule_delayed_work(&spec->vt1708_hp_work,
2818 msecs_to_jiffies(100));
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002819}
2820
Takashi Iwai337b9d02009-07-07 18:18:59 +02002821static int get_mux_nids(struct hda_codec *codec)
2822{
2823 struct via_spec *spec = codec->spec;
2824 hda_nid_t nid, conn[8];
2825 unsigned int type;
2826 int i, n;
2827
2828 for (i = 0; i < spec->num_adc_nids; i++) {
2829 nid = spec->adc_nids[i];
2830 while (nid) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002831 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai1c55d522009-07-08 07:45:46 +02002832 if (type == AC_WID_PIN)
2833 break;
Takashi Iwai337b9d02009-07-07 18:18:59 +02002834 n = snd_hda_get_connections(codec, nid, conn,
2835 ARRAY_SIZE(conn));
2836 if (n <= 0)
2837 break;
2838 if (n > 1) {
2839 spec->mux_nids[i] = nid;
2840 break;
2841 }
2842 nid = conn[0];
2843 }
2844 }
Takashi Iwai1c55d522009-07-08 07:45:46 +02002845 return 0;
Takashi Iwai337b9d02009-07-07 18:18:59 +02002846}
2847
Joseph Chanc577b8a2006-11-29 15:29:40 +01002848static int patch_vt1708(struct hda_codec *codec)
2849{
2850 struct via_spec *spec;
2851 int err;
2852
2853 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002854 spec = via_new_spec(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002855 if (spec == NULL)
2856 return -ENOMEM;
2857
Takashi Iwai620e2b22011-06-17 17:19:19 +02002858 spec->aa_mix_nid = 0x17;
2859
Takashi Iwai12daef62011-06-18 17:45:49 +02002860 /* Add HP and CD pin config connect bit re-config action */
2861 vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
2862 vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
2863
Joseph Chanc577b8a2006-11-29 15:29:40 +01002864 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02002865 err = via_parse_auto_config(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002866 if (err < 0) {
2867 via_free(codec);
2868 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002869 }
2870
Takashi Iwai12daef62011-06-18 17:45:49 +02002871 /* add jack detect on/off control */
2872 if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
2873 return -ENOMEM;
2874
Takashi Iwaibc9b5622008-05-23 17:50:27 +02002875 /* disable 32bit format on VT1708 */
2876 if (codec->vendor_id == 0x11061708)
2877 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002878
Lydia Wange322a362011-06-29 13:52:02 +08002879 spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2880
Joseph Chanc577b8a2006-11-29 15:29:40 +01002881 codec->patch_ops = via_patch_ops;
2882
Lydia Wang1f2e99f2009-10-10 19:08:17 +08002883 INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002884 return 0;
2885}
2886
Takashi Iwaiddd304d2011-06-21 16:33:55 +02002887static int patch_vt1709(struct hda_codec *codec)
Joseph Chanc577b8a2006-11-29 15:29:40 +01002888{
2889 struct via_spec *spec;
2890 int err;
2891
2892 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002893 spec = via_new_spec(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002894 if (spec == NULL)
2895 return -ENOMEM;
2896
Takashi Iwai620e2b22011-06-17 17:19:19 +02002897 spec->aa_mix_nid = 0x18;
2898
Takashi Iwai12daef62011-06-18 17:45:49 +02002899 err = via_parse_auto_config(codec);
Joseph Chanc577b8a2006-11-29 15:29:40 +01002900 if (err < 0) {
2901 via_free(codec);
2902 return err;
Joseph Chanc577b8a2006-11-29 15:29:40 +01002903 }
2904
Joseph Chanc577b8a2006-11-29 15:29:40 +01002905 codec->patch_ops = via_patch_ops;
2906
Josepch Chanf7278fd2007-12-13 16:40:40 +01002907 return 0;
2908}
2909
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002910static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
2911{
2912 struct via_spec *spec = codec->spec;
2913 int imux_is_smixer;
2914 unsigned int parm;
2915 int is_8ch = 0;
Lydia Wangbc92df72011-03-23 17:56:05 +08002916 if ((spec->codec_type != VT1708B_4CH) &&
2917 (codec->vendor_id != 0x11064397))
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002918 is_8ch = 1;
2919
2920 /* SW0 (17h) = stereo mixer */
2921 imux_is_smixer =
2922 (snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
2923 == ((spec->codec_type == VT1708S) ? 5 : 0));
2924 /* inputs */
2925 /* PW 1/2/5 (1ah/1bh/1eh) */
2926 parm = AC_PWRST_D3;
2927 set_pin_power_state(codec, 0x1a, &parm);
2928 set_pin_power_state(codec, 0x1b, &parm);
2929 set_pin_power_state(codec, 0x1e, &parm);
2930 if (imux_is_smixer)
2931 parm = AC_PWRST_D0;
2932 /* SW0 (17h), AIW 0/1 (13h/14h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01002933 update_power_state(codec, 0x17, parm);
2934 update_power_state(codec, 0x13, parm);
2935 update_power_state(codec, 0x14, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002936
2937 /* outputs */
2938 /* PW0 (19h), SW1 (18h), AOW1 (11h) */
2939 parm = AC_PWRST_D3;
2940 set_pin_power_state(codec, 0x19, &parm);
2941 if (spec->smart51_enabled)
2942 set_pin_power_state(codec, 0x1b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002943 update_power_state(codec, 0x18, parm);
2944 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002945
2946 /* PW6 (22h), SW2 (26h), AOW2 (24h) */
2947 if (is_8ch) {
2948 parm = AC_PWRST_D3;
2949 set_pin_power_state(codec, 0x22, &parm);
2950 if (spec->smart51_enabled)
2951 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002952 update_power_state(codec, 0x26, parm);
2953 update_power_state(codec, 0x24, parm);
Lydia Wangbc92df72011-03-23 17:56:05 +08002954 } else if (codec->vendor_id == 0x11064397) {
2955 /* PW7(23h), SW2(27h), AOW2(25h) */
2956 parm = AC_PWRST_D3;
2957 set_pin_power_state(codec, 0x23, &parm);
2958 if (spec->smart51_enabled)
2959 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01002960 update_power_state(codec, 0x27, parm);
2961 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002962 }
2963
2964 /* PW 3/4/7 (1ch/1dh/23h) */
2965 parm = AC_PWRST_D3;
2966 /* force to D0 for internal Speaker */
2967 set_pin_power_state(codec, 0x1c, &parm);
2968 set_pin_power_state(codec, 0x1d, &parm);
2969 if (is_8ch)
2970 set_pin_power_state(codec, 0x23, &parm);
2971
2972 /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01002973 update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
2974 update_power_state(codec, 0x10, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002975 if (is_8ch) {
Takashi Iwai054d8672012-01-24 12:25:50 +01002976 update_power_state(codec, 0x25, parm);
2977 update_power_state(codec, 0x27, parm);
Lydia Wangbc92df72011-03-23 17:56:05 +08002978 } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01002979 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08002980}
2981
Lydia Wang518bf3b2009-10-10 19:07:29 +08002982static int patch_vt1708S(struct hda_codec *codec);
Takashi Iwaiddd304d2011-06-21 16:33:55 +02002983static int patch_vt1708B(struct hda_codec *codec)
Josepch Chanf7278fd2007-12-13 16:40:40 +01002984{
2985 struct via_spec *spec;
2986 int err;
2987
Lydia Wang518bf3b2009-10-10 19:07:29 +08002988 if (get_codec_type(codec) == VT1708BCE)
2989 return patch_vt1708S(codec);
Takashi Iwaiddd304d2011-06-21 16:33:55 +02002990
Josepch Chanf7278fd2007-12-13 16:40:40 +01002991 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002992 spec = via_new_spec(codec);
Josepch Chanf7278fd2007-12-13 16:40:40 +01002993 if (spec == NULL)
2994 return -ENOMEM;
2995
Takashi Iwai620e2b22011-06-17 17:19:19 +02002996 spec->aa_mix_nid = 0x16;
2997
Josepch Chanf7278fd2007-12-13 16:40:40 +01002998 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02002999 err = via_parse_auto_config(codec);
Josepch Chanf7278fd2007-12-13 16:40:40 +01003000 if (err < 0) {
3001 via_free(codec);
3002 return err;
Josepch Chanf7278fd2007-12-13 16:40:40 +01003003 }
3004
Josepch Chanf7278fd2007-12-13 16:40:40 +01003005 codec->patch_ops = via_patch_ops;
3006
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003007 spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
3008
Josepch Chanf7278fd2007-12-13 16:40:40 +01003009 return 0;
3010}
3011
Harald Welted949cac2008-09-09 15:56:01 +08003012/* Patch for VT1708S */
Takashi Iwai096a8852011-06-20 12:09:02 +02003013static const struct hda_verb vt1708S_init_verbs[] = {
Harald Welted7426322008-09-15 22:43:23 +08003014 /* Enable Mic Boost Volume backdoor */
3015 {0x1, 0xf98, 0x1},
Lydia Wangbc7e7e52009-10-10 19:08:32 +08003016 /* don't bybass mixer */
3017 {0x1, 0xf88, 0xc0},
Harald Welted949cac2008-09-09 15:56:01 +08003018 { }
3019};
3020
Takashi Iwai9da29272009-05-07 16:31:14 +02003021/* fill out digital output widgets; one for master and one for slave outputs */
3022static void fill_dig_outs(struct hda_codec *codec)
3023{
3024 struct via_spec *spec = codec->spec;
3025 int i;
3026
3027 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3028 hda_nid_t nid;
3029 int conn;
3030
3031 nid = spec->autocfg.dig_out_pins[i];
3032 if (!nid)
3033 continue;
3034 conn = snd_hda_get_connections(codec, nid, &nid, 1);
3035 if (conn < 1)
3036 continue;
3037 if (!spec->multiout.dig_out_nid)
3038 spec->multiout.dig_out_nid = nid;
3039 else {
3040 spec->slave_dig_outs[0] = nid;
3041 break; /* at most two dig outs */
3042 }
3043 }
3044}
3045
Takashi Iwai12daef62011-06-18 17:45:49 +02003046static void fill_dig_in(struct hda_codec *codec)
Harald Welted949cac2008-09-09 15:56:01 +08003047{
3048 struct via_spec *spec = codec->spec;
Takashi Iwai12daef62011-06-18 17:45:49 +02003049 hda_nid_t dig_nid;
3050 int i, err;
Harald Welted949cac2008-09-09 15:56:01 +08003051
Takashi Iwai12daef62011-06-18 17:45:49 +02003052 if (!spec->autocfg.dig_in_pin)
3053 return;
Harald Welted949cac2008-09-09 15:56:01 +08003054
Takashi Iwai12daef62011-06-18 17:45:49 +02003055 dig_nid = codec->start_nid;
3056 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3057 unsigned int wcaps = get_wcaps(codec, dig_nid);
3058 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3059 continue;
3060 if (!(wcaps & AC_WCAP_DIGITAL))
3061 continue;
3062 if (!(wcaps & AC_WCAP_CONN_LIST))
3063 continue;
3064 err = get_connection_index(codec, dig_nid,
3065 spec->autocfg.dig_in_pin);
3066 if (err >= 0) {
3067 spec->dig_in_nid = dig_nid;
3068 break;
3069 }
3070 }
Harald Welted949cac2008-09-09 15:56:01 +08003071}
3072
Lydia Wang6369bcf2009-10-10 19:08:31 +08003073static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
3074 int offset, int num_steps, int step_size)
3075{
3076 snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
3077 (offset << AC_AMPCAP_OFFSET_SHIFT) |
3078 (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
3079 (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
3080 (0 << AC_AMPCAP_MUTE_SHIFT));
3081}
3082
Harald Welted949cac2008-09-09 15:56:01 +08003083static int patch_vt1708S(struct hda_codec *codec)
3084{
3085 struct via_spec *spec;
3086 int err;
3087
3088 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003089 spec = via_new_spec(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003090 if (spec == NULL)
3091 return -ENOMEM;
3092
Takashi Iwai620e2b22011-06-17 17:19:19 +02003093 spec->aa_mix_nid = 0x16;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003094 override_mic_boost(codec, 0x1a, 0, 3, 40);
3095 override_mic_boost(codec, 0x1e, 0, 3, 40);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003096
Harald Welted949cac2008-09-09 15:56:01 +08003097 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003098 err = via_parse_auto_config(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003099 if (err < 0) {
3100 via_free(codec);
3101 return err;
Harald Welted949cac2008-09-09 15:56:01 +08003102 }
3103
Takashi Iwai096a8852011-06-20 12:09:02 +02003104 spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
Harald Welted949cac2008-09-09 15:56:01 +08003105
Harald Welted949cac2008-09-09 15:56:01 +08003106 codec->patch_ops = via_patch_ops;
3107
Lydia Wang518bf3b2009-10-10 19:07:29 +08003108 /* correct names for VT1708BCE */
3109 if (get_codec_type(codec) == VT1708BCE) {
3110 kfree(codec->chip_name);
3111 codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3112 snprintf(codec->bus->card->mixername,
3113 sizeof(codec->bus->card->mixername),
3114 "%s %s", codec->vendor_name, codec->chip_name);
Lydia Wang970f6302011-03-22 16:25:56 +08003115 }
Lydia Wangbc92df72011-03-23 17:56:05 +08003116 /* correct names for VT1705 */
3117 if (codec->vendor_id == 0x11064397) {
3118 kfree(codec->chip_name);
3119 codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3120 snprintf(codec->bus->card->mixername,
3121 sizeof(codec->bus->card->mixername),
3122 "%s %s", codec->vendor_name, codec->chip_name);
3123 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003124 spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
Harald Welted949cac2008-09-09 15:56:01 +08003125 return 0;
3126}
3127
3128/* Patch for VT1702 */
3129
Takashi Iwai096a8852011-06-20 12:09:02 +02003130static const struct hda_verb vt1702_init_verbs[] = {
Lydia Wangbc7e7e52009-10-10 19:08:32 +08003131 /* mixer enable */
3132 {0x1, 0xF88, 0x3},
3133 /* GPIO 0~2 */
3134 {0x1, 0xF82, 0x3F},
Harald Welted949cac2008-09-09 15:56:01 +08003135 { }
3136};
3137
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003138static void set_widgets_power_state_vt1702(struct hda_codec *codec)
3139{
3140 int imux_is_smixer =
3141 snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3142 unsigned int parm;
3143 /* inputs */
3144 /* PW 1/2/5 (14h/15h/18h) */
3145 parm = AC_PWRST_D3;
3146 set_pin_power_state(codec, 0x14, &parm);
3147 set_pin_power_state(codec, 0x15, &parm);
3148 set_pin_power_state(codec, 0x18, &parm);
3149 if (imux_is_smixer)
3150 parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
3151 /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003152 update_power_state(codec, 0x13, parm);
3153 update_power_state(codec, 0x12, parm);
3154 update_power_state(codec, 0x1f, parm);
3155 update_power_state(codec, 0x20, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003156
3157 /* outputs */
3158 /* PW 3/4 (16h/17h) */
3159 parm = AC_PWRST_D3;
3160 set_pin_power_state(codec, 0x17, &parm);
3161 set_pin_power_state(codec, 0x16, &parm);
3162 /* MW0 (1ah), AOW 0/1 (10h/1dh) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003163 update_power_state(codec, 0x1a, imux_is_smixer ? AC_PWRST_D0 : parm);
3164 update_power_state(codec, 0x10, parm);
3165 update_power_state(codec, 0x1d, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003166}
3167
Harald Welted949cac2008-09-09 15:56:01 +08003168static int patch_vt1702(struct hda_codec *codec)
3169{
3170 struct via_spec *spec;
3171 int err;
Harald Welted949cac2008-09-09 15:56:01 +08003172
3173 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003174 spec = via_new_spec(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003175 if (spec == NULL)
3176 return -ENOMEM;
3177
Takashi Iwai620e2b22011-06-17 17:19:19 +02003178 spec->aa_mix_nid = 0x1a;
3179
Takashi Iwai12daef62011-06-18 17:45:49 +02003180 /* limit AA path volume to 0 dB */
3181 snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
3182 (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
3183 (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
3184 (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
3185 (1 << AC_AMPCAP_MUTE_SHIFT));
3186
Harald Welted949cac2008-09-09 15:56:01 +08003187 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003188 err = via_parse_auto_config(codec);
Harald Welted949cac2008-09-09 15:56:01 +08003189 if (err < 0) {
3190 via_free(codec);
3191 return err;
Harald Welted949cac2008-09-09 15:56:01 +08003192 }
3193
Takashi Iwai096a8852011-06-20 12:09:02 +02003194 spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
Harald Welted949cac2008-09-09 15:56:01 +08003195
Harald Welted949cac2008-09-09 15:56:01 +08003196 codec->patch_ops = via_patch_ops;
3197
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003198 spec->set_widgets_power_state = set_widgets_power_state_vt1702;
Harald Welted949cac2008-09-09 15:56:01 +08003199 return 0;
3200}
3201
Lydia Wangeb7188c2009-10-10 19:08:34 +08003202/* Patch for VT1718S */
3203
Takashi Iwai096a8852011-06-20 12:09:02 +02003204static const struct hda_verb vt1718S_init_verbs[] = {
Lydia Wang4ab2d532011-03-24 12:42:03 +08003205 /* Enable MW0 adjust Gain 5 */
3206 {0x1, 0xfb2, 0x10},
Lydia Wangeb7188c2009-10-10 19:08:34 +08003207 /* Enable Boost Volume backdoor */
3208 {0x1, 0xf88, 0x8},
Takashi Iwai5d417622011-06-20 11:32:27 +02003209
Lydia Wangeb7188c2009-10-10 19:08:34 +08003210 { }
3211};
3212
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003213static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
3214{
3215 struct via_spec *spec = codec->spec;
3216 int imux_is_smixer;
3217 unsigned int parm;
3218 /* MUX6 (1eh) = stereo mixer */
3219 imux_is_smixer =
3220 snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
3221 /* inputs */
3222 /* PW 5/6/7 (29h/2ah/2bh) */
3223 parm = AC_PWRST_D3;
3224 set_pin_power_state(codec, 0x29, &parm);
3225 set_pin_power_state(codec, 0x2a, &parm);
3226 set_pin_power_state(codec, 0x2b, &parm);
3227 if (imux_is_smixer)
3228 parm = AC_PWRST_D0;
3229 /* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003230 update_power_state(codec, 0x1e, parm);
3231 update_power_state(codec, 0x1f, parm);
3232 update_power_state(codec, 0x10, parm);
3233 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003234
3235 /* outputs */
3236 /* PW3 (27h), MW2 (1ah), AOW3 (bh) */
3237 parm = AC_PWRST_D3;
3238 set_pin_power_state(codec, 0x27, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003239 update_power_state(codec, 0x1a, parm);
3240 update_power_state(codec, 0xb, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003241
3242 /* PW2 (26h), AOW2 (ah) */
3243 parm = AC_PWRST_D3;
3244 set_pin_power_state(codec, 0x26, &parm);
3245 if (spec->smart51_enabled)
3246 set_pin_power_state(codec, 0x2b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003247 update_power_state(codec, 0xa, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003248
3249 /* PW0 (24h), AOW0 (8h) */
3250 parm = AC_PWRST_D3;
3251 set_pin_power_state(codec, 0x24, &parm);
3252 if (!spec->hp_independent_mode) /* check for redirected HP */
3253 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003254 update_power_state(codec, 0x8, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003255 /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003256 update_power_state(codec, 0x21, imux_is_smixer ? AC_PWRST_D0 : parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003257
3258 /* PW1 (25h), AOW1 (9h) */
3259 parm = AC_PWRST_D3;
3260 set_pin_power_state(codec, 0x25, &parm);
3261 if (spec->smart51_enabled)
3262 set_pin_power_state(codec, 0x2a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003263 update_power_state(codec, 0x9, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003264
3265 if (spec->hp_independent_mode) {
3266 /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
3267 parm = AC_PWRST_D3;
3268 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003269 update_power_state(codec, 0x1b, parm);
3270 update_power_state(codec, 0x34, parm);
3271 update_power_state(codec, 0xc, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003272 }
3273}
3274
Takashi Iwai30b45032011-07-11 17:05:04 +02003275/* Add a connection to the primary DAC from AA-mixer for some codecs
3276 * This isn't listed from the raw info, but the chip has a secret connection.
3277 */
3278static int add_secret_dac_path(struct hda_codec *codec)
3279{
3280 struct via_spec *spec = codec->spec;
3281 int i, nums;
3282 hda_nid_t conn[8];
3283 hda_nid_t nid;
3284
3285 if (!spec->aa_mix_nid)
3286 return 0;
3287 nums = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
3288 ARRAY_SIZE(conn) - 1);
3289 for (i = 0; i < nums; i++) {
3290 if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
3291 return 0;
3292 }
3293
3294 /* find the primary DAC and add to the connection list */
3295 nid = codec->start_nid;
3296 for (i = 0; i < codec->num_nodes; i++, nid++) {
3297 unsigned int caps = get_wcaps(codec, nid);
3298 if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
3299 !(caps & AC_WCAP_DIGITAL)) {
3300 conn[nums++] = nid;
3301 return snd_hda_override_conn_list(codec,
3302 spec->aa_mix_nid,
3303 nums, conn);
3304 }
3305 }
3306 return 0;
3307}
3308
3309
Lydia Wangeb7188c2009-10-10 19:08:34 +08003310static int patch_vt1718S(struct hda_codec *codec)
3311{
3312 struct via_spec *spec;
3313 int err;
3314
3315 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003316 spec = via_new_spec(codec);
Lydia Wangeb7188c2009-10-10 19:08:34 +08003317 if (spec == NULL)
3318 return -ENOMEM;
3319
Takashi Iwai620e2b22011-06-17 17:19:19 +02003320 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003321 override_mic_boost(codec, 0x2b, 0, 3, 40);
3322 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003323 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003324
Lydia Wangeb7188c2009-10-10 19:08:34 +08003325 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003326 err = via_parse_auto_config(codec);
Lydia Wangeb7188c2009-10-10 19:08:34 +08003327 if (err < 0) {
3328 via_free(codec);
3329 return err;
Lydia Wangeb7188c2009-10-10 19:08:34 +08003330 }
3331
Takashi Iwai096a8852011-06-20 12:09:02 +02003332 spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
Lydia Wangeb7188c2009-10-10 19:08:34 +08003333
Lydia Wangeb7188c2009-10-10 19:08:34 +08003334 codec->patch_ops = via_patch_ops;
3335
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003336 spec->set_widgets_power_state = set_widgets_power_state_vt1718S;
3337
Lydia Wangeb7188c2009-10-10 19:08:34 +08003338 return 0;
3339}
Lydia Wangf3db4232009-10-10 19:08:41 +08003340
3341/* Patch for VT1716S */
3342
3343static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
3344 struct snd_ctl_elem_info *uinfo)
3345{
3346 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3347 uinfo->count = 1;
3348 uinfo->value.integer.min = 0;
3349 uinfo->value.integer.max = 1;
3350 return 0;
3351}
3352
3353static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
3354 struct snd_ctl_elem_value *ucontrol)
3355{
3356 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3357 int index = 0;
3358
3359 index = snd_hda_codec_read(codec, 0x26, 0,
3360 AC_VERB_GET_CONNECT_SEL, 0);
3361 if (index != -1)
3362 *ucontrol->value.integer.value = index;
3363
3364 return 0;
3365}
3366
3367static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
3368 struct snd_ctl_elem_value *ucontrol)
3369{
3370 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3371 struct via_spec *spec = codec->spec;
3372 int index = *ucontrol->value.integer.value;
3373
3374 snd_hda_codec_write(codec, 0x26, 0,
3375 AC_VERB_SET_CONNECT_SEL, index);
3376 spec->dmic_enabled = index;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003377 set_widgets_power_state(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003378 return 1;
3379}
3380
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003381static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003382 HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
3383 {
3384 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3385 .name = "Digital Mic Capture Switch",
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003386 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
Lydia Wangf3db4232009-10-10 19:08:41 +08003387 .count = 1,
3388 .info = vt1716s_dmic_info,
3389 .get = vt1716s_dmic_get,
3390 .put = vt1716s_dmic_put,
3391 },
3392 {} /* end */
3393};
3394
3395
3396/* mono-out mixer elements */
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003397static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003398 HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
3399 { } /* end */
3400};
3401
Takashi Iwai096a8852011-06-20 12:09:02 +02003402static const struct hda_verb vt1716S_init_verbs[] = {
Lydia Wangf3db4232009-10-10 19:08:41 +08003403 /* Enable Boost Volume backdoor */
3404 {0x1, 0xf8a, 0x80},
3405 /* don't bybass mixer */
3406 {0x1, 0xf88, 0xc0},
3407 /* Enable mono output */
3408 {0x1, 0xf90, 0x08},
3409 { }
3410};
3411
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003412static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
3413{
3414 struct via_spec *spec = codec->spec;
3415 int imux_is_smixer;
3416 unsigned int parm;
3417 unsigned int mono_out, present;
3418 /* SW0 (17h) = stereo mixer */
3419 imux_is_smixer =
3420 (snd_hda_codec_read(codec, 0x17, 0,
3421 AC_VERB_GET_CONNECT_SEL, 0x00) == 5);
3422 /* inputs */
3423 /* PW 1/2/5 (1ah/1bh/1eh) */
3424 parm = AC_PWRST_D3;
3425 set_pin_power_state(codec, 0x1a, &parm);
3426 set_pin_power_state(codec, 0x1b, &parm);
3427 set_pin_power_state(codec, 0x1e, &parm);
3428 if (imux_is_smixer)
3429 parm = AC_PWRST_D0;
3430 /* SW0 (17h), AIW0(13h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003431 update_power_state(codec, 0x17, parm);
3432 update_power_state(codec, 0x13, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003433
3434 parm = AC_PWRST_D3;
3435 set_pin_power_state(codec, 0x1e, &parm);
3436 /* PW11 (22h) */
3437 if (spec->dmic_enabled)
3438 set_pin_power_state(codec, 0x22, &parm);
3439 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003440 update_power_state(codec, 0x22, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003441
3442 /* SW2(26h), AIW1(14h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003443 update_power_state(codec, 0x26, parm);
3444 update_power_state(codec, 0x14, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003445
3446 /* outputs */
3447 /* PW0 (19h), SW1 (18h), AOW1 (11h) */
3448 parm = AC_PWRST_D3;
3449 set_pin_power_state(codec, 0x19, &parm);
3450 /* Smart 5.1 PW2(1bh) */
3451 if (spec->smart51_enabled)
3452 set_pin_power_state(codec, 0x1b, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003453 update_power_state(codec, 0x18, parm);
3454 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003455
3456 /* PW7 (23h), SW3 (27h), AOW3 (25h) */
3457 parm = AC_PWRST_D3;
3458 set_pin_power_state(codec, 0x23, &parm);
3459 /* Smart 5.1 PW1(1ah) */
3460 if (spec->smart51_enabled)
3461 set_pin_power_state(codec, 0x1a, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003462 update_power_state(codec, 0x27, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003463
3464 /* Smart 5.1 PW5(1eh) */
3465 if (spec->smart51_enabled)
3466 set_pin_power_state(codec, 0x1e, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003467 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003468
3469 /* Mono out */
3470 /* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
3471 present = snd_hda_jack_detect(codec, 0x1c);
3472
3473 if (present)
3474 mono_out = 0;
3475 else {
3476 present = snd_hda_jack_detect(codec, 0x1d);
3477 if (!spec->hp_independent_mode && present)
3478 mono_out = 0;
3479 else
3480 mono_out = 1;
3481 }
3482 parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
Takashi Iwai054d8672012-01-24 12:25:50 +01003483 update_power_state(codec, 0x28, parm);
3484 update_power_state(codec, 0x29, parm);
3485 update_power_state(codec, 0x2a, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003486
3487 /* PW 3/4 (1ch/1dh) */
3488 parm = AC_PWRST_D3;
3489 set_pin_power_state(codec, 0x1c, &parm);
3490 set_pin_power_state(codec, 0x1d, &parm);
3491 /* HP Independent Mode, power on AOW3 */
3492 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003493 update_power_state(codec, 0x25, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003494
3495 /* force to D0 for internal Speaker */
3496 /* MW0 (16h), AOW0 (10h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003497 update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
3498 update_power_state(codec, 0x10, mono_out ? AC_PWRST_D0 : parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003499}
3500
Lydia Wangf3db4232009-10-10 19:08:41 +08003501static int patch_vt1716S(struct hda_codec *codec)
3502{
3503 struct via_spec *spec;
3504 int err;
3505
3506 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003507 spec = via_new_spec(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003508 if (spec == NULL)
3509 return -ENOMEM;
3510
Takashi Iwai620e2b22011-06-17 17:19:19 +02003511 spec->aa_mix_nid = 0x16;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003512 override_mic_boost(codec, 0x1a, 0, 3, 40);
3513 override_mic_boost(codec, 0x1e, 0, 3, 40);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003514
Lydia Wangf3db4232009-10-10 19:08:41 +08003515 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003516 err = via_parse_auto_config(codec);
Lydia Wangf3db4232009-10-10 19:08:41 +08003517 if (err < 0) {
3518 via_free(codec);
3519 return err;
Lydia Wangf3db4232009-10-10 19:08:41 +08003520 }
3521
Takashi Iwai096a8852011-06-20 12:09:02 +02003522 spec->init_verbs[spec->num_iverbs++] = vt1716S_init_verbs;
Lydia Wangf3db4232009-10-10 19:08:41 +08003523
Lydia Wangf3db4232009-10-10 19:08:41 +08003524 spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
3525 spec->num_mixers++;
3526
3527 spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3528
3529 codec->patch_ops = via_patch_ops;
3530
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003531 spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
Lydia Wangf3db4232009-10-10 19:08:41 +08003532 return 0;
3533}
Lydia Wang25eaba22009-10-10 19:08:43 +08003534
3535/* for vt2002P */
3536
Takashi Iwai096a8852011-06-20 12:09:02 +02003537static const struct hda_verb vt2002P_init_verbs[] = {
Lydia Wangeadb9a82011-03-24 12:43:02 +08003538 /* Class-D speaker related verbs */
3539 {0x1, 0xfe0, 0x4},
3540 {0x1, 0xfe9, 0x80},
3541 {0x1, 0xfe2, 0x22},
Lydia Wang25eaba22009-10-10 19:08:43 +08003542 /* Enable Boost Volume backdoor */
3543 {0x1, 0xfb9, 0x24},
Lydia Wang25eaba22009-10-10 19:08:43 +08003544 /* Enable AOW0 to MW9 */
3545 {0x1, 0xfb8, 0x88},
3546 { }
3547};
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003548
Takashi Iwai096a8852011-06-20 12:09:02 +02003549static const struct hda_verb vt1802_init_verbs[] = {
Lydia Wang118909562011-03-23 17:57:34 +08003550 /* Enable Boost Volume backdoor */
3551 {0x1, 0xfb9, 0x24},
Lydia Wang118909562011-03-23 17:57:34 +08003552 /* Enable AOW0 to MW9 */
3553 {0x1, 0xfb8, 0x88},
3554 { }
3555};
Lydia Wang25eaba22009-10-10 19:08:43 +08003556
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003557static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
3558{
3559 struct via_spec *spec = codec->spec;
3560 int imux_is_smixer;
3561 unsigned int parm;
3562 unsigned int present;
3563 /* MUX9 (1eh) = stereo mixer */
3564 imux_is_smixer =
3565 snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3566 /* inputs */
3567 /* PW 5/6/7 (29h/2ah/2bh) */
3568 parm = AC_PWRST_D3;
3569 set_pin_power_state(codec, 0x29, &parm);
3570 set_pin_power_state(codec, 0x2a, &parm);
3571 set_pin_power_state(codec, 0x2b, &parm);
3572 parm = AC_PWRST_D0;
3573 /* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003574 update_power_state(codec, 0x1e, parm);
3575 update_power_state(codec, 0x1f, parm);
3576 update_power_state(codec, 0x10, parm);
3577 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003578
3579 /* outputs */
3580 /* AOW0 (8h)*/
Takashi Iwai054d8672012-01-24 12:25:50 +01003581 update_power_state(codec, 0x8, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003582
Lydia Wang118909562011-03-23 17:57:34 +08003583 if (spec->codec_type == VT1802) {
3584 /* PW4 (28h), MW4 (18h), MUX4(38h) */
3585 parm = AC_PWRST_D3;
3586 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003587 update_power_state(codec, 0x18, parm);
3588 update_power_state(codec, 0x38, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003589 } else {
3590 /* PW4 (26h), MW4 (1ch), MUX4(37h) */
3591 parm = AC_PWRST_D3;
3592 set_pin_power_state(codec, 0x26, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003593 update_power_state(codec, 0x1c, parm);
3594 update_power_state(codec, 0x37, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003595 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003596
Lydia Wang118909562011-03-23 17:57:34 +08003597 if (spec->codec_type == VT1802) {
3598 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3599 parm = AC_PWRST_D3;
3600 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003601 update_power_state(codec, 0x15, parm);
3602 update_power_state(codec, 0x35, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003603 } else {
3604 /* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
3605 parm = AC_PWRST_D3;
3606 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003607 update_power_state(codec, 0x19, parm);
3608 update_power_state(codec, 0x35, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003609 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003610
3611 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003612 update_power_state(codec, 0x9, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003613
3614 /* Class-D */
3615 /* PW0 (24h), MW0(18h/14h), MUX0(34h) */
3616 present = snd_hda_jack_detect(codec, 0x25);
3617
3618 parm = AC_PWRST_D3;
3619 set_pin_power_state(codec, 0x24, &parm);
3620 parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
Lydia Wang118909562011-03-23 17:57:34 +08003621 if (spec->codec_type == VT1802)
Takashi Iwai054d8672012-01-24 12:25:50 +01003622 update_power_state(codec, 0x14, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003623 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003624 update_power_state(codec, 0x18, parm);
3625 update_power_state(codec, 0x34, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003626
3627 /* Mono Out */
3628 present = snd_hda_jack_detect(codec, 0x26);
3629
3630 parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
Lydia Wang118909562011-03-23 17:57:34 +08003631 if (spec->codec_type == VT1802) {
3632 /* PW15 (33h), MW8(1ch), MUX8(3ch) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003633 update_power_state(codec, 0x33, parm);
3634 update_power_state(codec, 0x1c, parm);
3635 update_power_state(codec, 0x3c, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003636 } else {
3637 /* PW15 (31h), MW8(17h), MUX8(3bh) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003638 update_power_state(codec, 0x31, parm);
3639 update_power_state(codec, 0x17, parm);
3640 update_power_state(codec, 0x3b, parm);
Lydia Wang118909562011-03-23 17:57:34 +08003641 }
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003642 /* MW9 (21h) */
3643 if (imux_is_smixer || !is_aa_path_mute(codec))
Takashi Iwai054d8672012-01-24 12:25:50 +01003644 update_power_state(codec, 0x21, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003645 else
Takashi Iwai054d8672012-01-24 12:25:50 +01003646 update_power_state(codec, 0x21, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003647}
Lydia Wang25eaba22009-10-10 19:08:43 +08003648
3649/* patch for vt2002P */
3650static int patch_vt2002P(struct hda_codec *codec)
3651{
3652 struct via_spec *spec;
3653 int err;
3654
3655 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003656 spec = via_new_spec(codec);
Lydia Wang25eaba22009-10-10 19:08:43 +08003657 if (spec == NULL)
3658 return -ENOMEM;
3659
Takashi Iwai620e2b22011-06-17 17:19:19 +02003660 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003661 override_mic_boost(codec, 0x2b, 0, 3, 40);
3662 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003663 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003664
Lydia Wang25eaba22009-10-10 19:08:43 +08003665 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003666 err = via_parse_auto_config(codec);
Lydia Wang25eaba22009-10-10 19:08:43 +08003667 if (err < 0) {
3668 via_free(codec);
3669 return err;
Lydia Wang25eaba22009-10-10 19:08:43 +08003670 }
3671
Lydia Wang118909562011-03-23 17:57:34 +08003672 if (spec->codec_type == VT1802)
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003673 spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
Lydia Wang118909562011-03-23 17:57:34 +08003674 else
Takashi Iwai4a918ff2011-06-20 12:39:26 +02003675 spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
Lydia Wang118909562011-03-23 17:57:34 +08003676
Lydia Wang25eaba22009-10-10 19:08:43 +08003677 codec->patch_ops = via_patch_ops;
3678
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003679 spec->set_widgets_power_state = set_widgets_power_state_vt2002P;
Lydia Wang25eaba22009-10-10 19:08:43 +08003680 return 0;
3681}
Lydia Wangab6734e2009-10-10 19:08:46 +08003682
3683/* for vt1812 */
3684
Takashi Iwai096a8852011-06-20 12:09:02 +02003685static const struct hda_verb vt1812_init_verbs[] = {
Lydia Wangab6734e2009-10-10 19:08:46 +08003686 /* Enable Boost Volume backdoor */
3687 {0x1, 0xfb9, 0x24},
Lydia Wangab6734e2009-10-10 19:08:46 +08003688 /* Enable AOW0 to MW9 */
3689 {0x1, 0xfb8, 0xa8},
3690 { }
3691};
3692
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003693static void set_widgets_power_state_vt1812(struct hda_codec *codec)
3694{
3695 struct via_spec *spec = codec->spec;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003696 unsigned int parm;
3697 unsigned int present;
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003698 /* inputs */
3699 /* PW 5/6/7 (29h/2ah/2bh) */
3700 parm = AC_PWRST_D3;
3701 set_pin_power_state(codec, 0x29, &parm);
3702 set_pin_power_state(codec, 0x2a, &parm);
3703 set_pin_power_state(codec, 0x2b, &parm);
3704 parm = AC_PWRST_D0;
3705 /* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
Takashi Iwai054d8672012-01-24 12:25:50 +01003706 update_power_state(codec, 0x1e, parm);
3707 update_power_state(codec, 0x1f, parm);
3708 update_power_state(codec, 0x10, parm);
3709 update_power_state(codec, 0x11, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003710
3711 /* outputs */
3712 /* AOW0 (8h)*/
Takashi Iwai054d8672012-01-24 12:25:50 +01003713 update_power_state(codec, 0x8, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003714
3715 /* PW4 (28h), MW4 (18h), MUX4(38h) */
3716 parm = AC_PWRST_D3;
3717 set_pin_power_state(codec, 0x28, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003718 update_power_state(codec, 0x18, parm);
3719 update_power_state(codec, 0x38, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003720
3721 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3722 parm = AC_PWRST_D3;
3723 set_pin_power_state(codec, 0x25, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003724 update_power_state(codec, 0x15, parm);
3725 update_power_state(codec, 0x35, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003726 if (spec->hp_independent_mode)
Takashi Iwai054d8672012-01-24 12:25:50 +01003727 update_power_state(codec, 0x9, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003728
3729 /* Internal Speaker */
3730 /* PW0 (24h), MW0(14h), MUX0(34h) */
3731 present = snd_hda_jack_detect(codec, 0x25);
3732
3733 parm = AC_PWRST_D3;
3734 set_pin_power_state(codec, 0x24, &parm);
3735 if (present) {
Takashi Iwai054d8672012-01-24 12:25:50 +01003736 update_power_state(codec, 0x14, AC_PWRST_D3);
3737 update_power_state(codec, 0x34, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003738 } else {
Takashi Iwai054d8672012-01-24 12:25:50 +01003739 update_power_state(codec, 0x14, AC_PWRST_D0);
3740 update_power_state(codec, 0x34, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003741 }
3742
3743
3744 /* Mono Out */
3745 /* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
3746 present = snd_hda_jack_detect(codec, 0x28);
3747
3748 parm = AC_PWRST_D3;
3749 set_pin_power_state(codec, 0x31, &parm);
3750 if (present) {
Takashi Iwai054d8672012-01-24 12:25:50 +01003751 update_power_state(codec, 0x1c, AC_PWRST_D3);
3752 update_power_state(codec, 0x3c, AC_PWRST_D3);
3753 update_power_state(codec, 0x3e, AC_PWRST_D3);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003754 } else {
Takashi Iwai054d8672012-01-24 12:25:50 +01003755 update_power_state(codec, 0x1c, AC_PWRST_D0);
3756 update_power_state(codec, 0x3c, AC_PWRST_D0);
3757 update_power_state(codec, 0x3e, AC_PWRST_D0);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003758 }
3759
3760 /* PW15 (33h), MW15 (1dh), MUX15(3dh) */
3761 parm = AC_PWRST_D3;
3762 set_pin_power_state(codec, 0x33, &parm);
Takashi Iwai054d8672012-01-24 12:25:50 +01003763 update_power_state(codec, 0x1d, parm);
3764 update_power_state(codec, 0x3d, parm);
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003765
3766}
Lydia Wangab6734e2009-10-10 19:08:46 +08003767
3768/* patch for vt1812 */
3769static int patch_vt1812(struct hda_codec *codec)
3770{
3771 struct via_spec *spec;
3772 int err;
3773
3774 /* create a codec specific record */
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003775 spec = via_new_spec(codec);
Lydia Wangab6734e2009-10-10 19:08:46 +08003776 if (spec == NULL)
3777 return -ENOMEM;
3778
Takashi Iwai620e2b22011-06-17 17:19:19 +02003779 spec->aa_mix_nid = 0x21;
Takashi Iwaid7a99cc2011-06-18 17:24:46 +02003780 override_mic_boost(codec, 0x2b, 0, 3, 40);
3781 override_mic_boost(codec, 0x29, 0, 3, 40);
Takashi Iwai30b45032011-07-11 17:05:04 +02003782 add_secret_dac_path(codec);
Takashi Iwai620e2b22011-06-17 17:19:19 +02003783
Lydia Wangab6734e2009-10-10 19:08:46 +08003784 /* automatic parse from the BIOS config */
Takashi Iwai12daef62011-06-18 17:45:49 +02003785 err = via_parse_auto_config(codec);
Lydia Wangab6734e2009-10-10 19:08:46 +08003786 if (err < 0) {
3787 via_free(codec);
3788 return err;
Lydia Wangab6734e2009-10-10 19:08:46 +08003789 }
3790
Takashi Iwai096a8852011-06-20 12:09:02 +02003791 spec->init_verbs[spec->num_iverbs++] = vt1812_init_verbs;
Lydia Wangab6734e2009-10-10 19:08:46 +08003792
Lydia Wangab6734e2009-10-10 19:08:46 +08003793 codec->patch_ops = via_patch_ops;
3794
Lydia Wang3e95b9a2011-03-23 15:13:28 +08003795 spec->set_widgets_power_state = set_widgets_power_state_vt1812;
Lydia Wangab6734e2009-10-10 19:08:46 +08003796 return 0;
3797}
3798
Joseph Chanc577b8a2006-11-29 15:29:40 +01003799/*
3800 * patch entries
3801 */
Takashi Iwai90dd48a2011-05-02 12:38:19 +02003802static const struct hda_codec_preset snd_hda_preset_via[] = {
Takashi Iwai3218c172008-12-18 09:17:56 +01003803 { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
3804 { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
3805 { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
3806 { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
3807 { .id = 0x1106e710, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003808 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003809 { .id = 0x1106e711, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003810 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003811 { .id = 0x1106e712, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003812 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003813 { .id = 0x1106e713, .name = "VT1709 10-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003814 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003815 { .id = 0x1106e714, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003816 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003817 { .id = 0x1106e715, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003818 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003819 { .id = 0x1106e716, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003820 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003821 { .id = 0x1106e717, .name = "VT1709 6-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003822 .patch = patch_vt1709},
Takashi Iwai3218c172008-12-18 09:17:56 +01003823 { .id = 0x1106e720, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003824 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003825 { .id = 0x1106e721, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003826 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003827 { .id = 0x1106e722, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003828 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003829 { .id = 0x1106e723, .name = "VT1708B 8-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003830 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003831 { .id = 0x1106e724, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003832 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003833 { .id = 0x1106e725, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003834 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003835 { .id = 0x1106e726, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003836 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003837 { .id = 0x1106e727, .name = "VT1708B 4-Ch",
Takashi Iwaiddd304d2011-06-21 16:33:55 +02003838 .patch = patch_vt1708B},
Takashi Iwai3218c172008-12-18 09:17:56 +01003839 { .id = 0x11060397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003840 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003841 { .id = 0x11061397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003842 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003843 { .id = 0x11062397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003844 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003845 { .id = 0x11063397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003846 .patch = patch_vt1708S},
Lydia Wangbc92df72011-03-23 17:56:05 +08003847 { .id = 0x11064397, .name = "VT1705",
Harald Welted949cac2008-09-09 15:56:01 +08003848 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003849 { .id = 0x11065397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003850 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003851 { .id = 0x11066397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003852 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003853 { .id = 0x11067397, .name = "VT1708S",
Harald Welted949cac2008-09-09 15:56:01 +08003854 .patch = patch_vt1708S},
Takashi Iwai3218c172008-12-18 09:17:56 +01003855 { .id = 0x11060398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003856 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003857 { .id = 0x11061398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003858 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003859 { .id = 0x11062398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003860 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003861 { .id = 0x11063398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003862 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003863 { .id = 0x11064398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003864 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003865 { .id = 0x11065398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003866 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003867 { .id = 0x11066398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003868 .patch = patch_vt1702},
Takashi Iwai3218c172008-12-18 09:17:56 +01003869 { .id = 0x11067398, .name = "VT1702",
Harald Welted949cac2008-09-09 15:56:01 +08003870 .patch = patch_vt1702},
Lydia Wangeb7188c2009-10-10 19:08:34 +08003871 { .id = 0x11060428, .name = "VT1718S",
3872 .patch = patch_vt1718S},
3873 { .id = 0x11064428, .name = "VT1718S",
3874 .patch = patch_vt1718S},
Lydia Wangbb3c6bfc2009-10-10 19:08:39 +08003875 { .id = 0x11060441, .name = "VT2020",
3876 .patch = patch_vt1718S},
3877 { .id = 0x11064441, .name = "VT1828S",
3878 .patch = patch_vt1718S},
Lydia Wangf3db4232009-10-10 19:08:41 +08003879 { .id = 0x11060433, .name = "VT1716S",
3880 .patch = patch_vt1716S},
3881 { .id = 0x1106a721, .name = "VT1716S",
3882 .patch = patch_vt1716S},
Lydia Wang25eaba22009-10-10 19:08:43 +08003883 { .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
3884 { .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
Lydia Wangab6734e2009-10-10 19:08:46 +08003885 { .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
Lydia Wang36dd5c42009-10-20 13:18:04 +08003886 { .id = 0x11060440, .name = "VT1818S",
3887 .patch = patch_vt1708S},
Lydia Wang118909562011-03-23 17:57:34 +08003888 { .id = 0x11060446, .name = "VT1802",
3889 .patch = patch_vt2002P},
3890 { .id = 0x11068446, .name = "VT1802",
3891 .patch = patch_vt2002P},
Joseph Chanc577b8a2006-11-29 15:29:40 +01003892 {} /* terminator */
3893};
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003894
3895MODULE_ALIAS("snd-hda-codec-id:1106*");
3896
3897static struct hda_codec_preset_list via_list = {
3898 .preset = snd_hda_preset_via,
3899 .owner = THIS_MODULE,
3900};
3901
3902MODULE_LICENSE("GPL");
3903MODULE_DESCRIPTION("VIA HD-audio codec");
3904
3905static int __init patch_via_init(void)
3906{
3907 return snd_hda_add_codec_preset(&via_list);
3908}
3909
3910static void __exit patch_via_exit(void)
3911{
3912 snd_hda_delete_codec_preset(&via_list);
3913}
3914
3915module_init(patch_via_init)
3916module_exit(patch_via_exit)