ALSA: hda - Allow multiple callbacks for jack
[deliverable/linux.git] / sound / pci / hda / hda_generic.c
CommitLineData
1da177e4
LT
1/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
1da177e4
LT
23#include <linux/init.h>
24#include <linux/slab.h>
d81a6d71 25#include <linux/export.h>
352f7f91 26#include <linux/sort.h>
55196fff 27#include <linux/delay.h>
f873e536
TI
28#include <linux/ctype.h>
29#include <linux/string.h>
29476558 30#include <linux/bitops.h>
b21bdd0d 31#include <linux/module.h>
1da177e4 32#include <sound/core.h>
352f7f91 33#include <sound/jack.h>
d89c6c0c 34#include <sound/tlv.h>
1da177e4
LT
35#include "hda_codec.h"
36#include "hda_local.h"
352f7f91
TI
37#include "hda_auto_parser.h"
38#include "hda_jack.h"
7504b6cd 39#include "hda_beep.h"
352f7f91 40#include "hda_generic.h"
1da177e4 41
a7da6ce5 42
352f7f91
TI
43/* initialize hda_gen_spec struct */
44int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
45{
46 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
352f7f91 47 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
0186f4f4 48 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
38cf6f1a 49 mutex_init(&spec->pcm_mutex);
352f7f91
TI
50 return 0;
51}
2698ea98 52EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
1da177e4 53
12c93df6
TI
54struct snd_kcontrol_new *
55snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
56 const struct snd_kcontrol_new *temp)
352f7f91
TI
57{
58 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
59 if (!knew)
60 return NULL;
61 *knew = *temp;
62 if (name)
63 knew->name = kstrdup(name, GFP_KERNEL);
64 else if (knew->name)
65 knew->name = kstrdup(knew->name, GFP_KERNEL);
66 if (!knew->name)
67 return NULL;
68 return knew;
69}
2698ea98 70EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
1da177e4 71
352f7f91
TI
72static void free_kctls(struct hda_gen_spec *spec)
73{
74 if (spec->kctls.list) {
75 struct snd_kcontrol_new *kctl = spec->kctls.list;
76 int i;
77 for (i = 0; i < spec->kctls.used; i++)
78 kfree(kctl[i].name);
79 }
80 snd_array_free(&spec->kctls);
81}
1da177e4 82
a8dca460 83static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
352f7f91
TI
84{
85 if (!spec)
86 return;
87 free_kctls(spec);
352f7f91 88 snd_array_free(&spec->paths);
0186f4f4 89 snd_array_free(&spec->loopback_list);
352f7f91 90}
1da177e4 91
1c70a583
TI
92/*
93 * store user hints
94 */
95static void parse_user_hints(struct hda_codec *codec)
96{
97 struct hda_gen_spec *spec = codec->spec;
98 int val;
99
100 val = snd_hda_get_bool_hint(codec, "jack_detect");
101 if (val >= 0)
102 codec->no_jack_detect = !val;
103 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
104 if (val >= 0)
105 codec->inv_jack_detect = !!val;
106 val = snd_hda_get_bool_hint(codec, "trigger_sense");
107 if (val >= 0)
108 codec->no_trigger_sense = !val;
109 val = snd_hda_get_bool_hint(codec, "inv_eapd");
110 if (val >= 0)
111 codec->inv_eapd = !!val;
112 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
113 if (val >= 0)
114 codec->pcm_format_first = !!val;
115 val = snd_hda_get_bool_hint(codec, "sticky_stream");
116 if (val >= 0)
117 codec->no_sticky_stream = !val;
118 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
119 if (val >= 0)
120 codec->spdif_status_reset = !!val;
121 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
122 if (val >= 0)
123 codec->pin_amp_workaround = !!val;
124 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
125 if (val >= 0)
126 codec->single_adc_amp = !!val;
127
f72706be
TI
128 val = snd_hda_get_bool_hint(codec, "auto_mute");
129 if (val >= 0)
130 spec->suppress_auto_mute = !val;
1c70a583
TI
131 val = snd_hda_get_bool_hint(codec, "auto_mic");
132 if (val >= 0)
133 spec->suppress_auto_mic = !val;
134 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
135 if (val >= 0)
136 spec->line_in_auto_switch = !!val;
7eebffd3
TI
137 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
138 if (val >= 0)
139 spec->auto_mute_via_amp = !!val;
1c70a583
TI
140 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
141 if (val >= 0)
142 spec->need_dac_fix = !!val;
143 val = snd_hda_get_bool_hint(codec, "primary_hp");
144 if (val >= 0)
145 spec->no_primary_hp = !val;
da96fb5b
TI
146 val = snd_hda_get_bool_hint(codec, "multi_io");
147 if (val >= 0)
148 spec->no_multi_io = !val;
1c70a583
TI
149 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
150 if (val >= 0)
151 spec->multi_cap_vol = !!val;
152 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
153 if (val >= 0)
154 spec->inv_dmic_split = !!val;
155 val = snd_hda_get_bool_hint(codec, "indep_hp");
156 if (val >= 0)
157 spec->indep_hp = !!val;
158 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
159 if (val >= 0)
160 spec->add_stereo_mix_input = !!val;
f811c3cf 161 /* the following two are just for compatibility */
1c70a583
TI
162 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
163 if (val >= 0)
f811c3cf 164 spec->add_jack_modes = !!val;
29476558
TI
165 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
166 if (val >= 0)
f811c3cf
TI
167 spec->add_jack_modes = !!val;
168 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
169 if (val >= 0)
170 spec->add_jack_modes = !!val;
55196fff
TI
171 val = snd_hda_get_bool_hint(codec, "power_down_unused");
172 if (val >= 0)
173 spec->power_down_unused = !!val;
967303da
TI
174 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
175 if (val >= 0)
176 spec->hp_mic = !!val;
177 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
178 if (val >= 0)
179 spec->suppress_hp_mic_detect = !val;
1c70a583
TI
180
181 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
182 spec->mixer_nid = val;
183}
184
2c12c30d
TI
185/*
186 * pin control value accesses
187 */
188
189#define update_pin_ctl(codec, pin, val) \
190 snd_hda_codec_update_cache(codec, pin, 0, \
191 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
192
193/* restore the pinctl based on the cached value */
194static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
195{
196 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
197}
198
199/* set the pinctl target value and write it if requested */
200static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
201 unsigned int val, bool do_write)
202{
203 if (!pin)
204 return;
205 val = snd_hda_correct_pin_ctl(codec, pin, val);
206 snd_hda_codec_set_pin_target(codec, pin, val);
207 if (do_write)
208 update_pin_ctl(codec, pin, val);
209}
210
211/* set pinctl target values for all given pins */
212static void set_pin_targets(struct hda_codec *codec, int num_pins,
213 hda_nid_t *pins, unsigned int val)
214{
215 int i;
216 for (i = 0; i < num_pins; i++)
217 set_pin_target(codec, pins[i], val, false);
218}
219
1da177e4 220/*
352f7f91 221 * parsing paths
1da177e4 222 */
1da177e4 223
3ca529d3
TI
224/* return the position of NID in the list, or -1 if not found */
225static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
226{
227 int i;
228 for (i = 0; i < nums; i++)
229 if (list[i] == nid)
230 return i;
231 return -1;
232}
233
234/* return true if the given NID is contained in the path */
235static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
236{
237 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
238}
239
f5172a7e
TI
240static struct nid_path *get_nid_path(struct hda_codec *codec,
241 hda_nid_t from_nid, hda_nid_t to_nid,
3ca529d3 242 int anchor_nid)
1da177e4 243{
352f7f91
TI
244 struct hda_gen_spec *spec = codec->spec;
245 int i;
1da177e4 246
352f7f91
TI
247 for (i = 0; i < spec->paths.used; i++) {
248 struct nid_path *path = snd_array_elem(&spec->paths, i);
249 if (path->depth <= 0)
250 continue;
251 if ((!from_nid || path->path[0] == from_nid) &&
f5172a7e 252 (!to_nid || path->path[path->depth - 1] == to_nid)) {
3ca529d3
TI
253 if (!anchor_nid ||
254 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
255 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
f5172a7e
TI
256 return path;
257 }
1da177e4 258 }
352f7f91 259 return NULL;
1da177e4 260}
f5172a7e
TI
261
262/* get the path between the given NIDs;
263 * passing 0 to either @pin or @dac behaves as a wildcard
264 */
265struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
266 hda_nid_t from_nid, hda_nid_t to_nid)
267{
3ca529d3 268 return get_nid_path(codec, from_nid, to_nid, 0);
f5172a7e 269}
2698ea98 270EXPORT_SYMBOL_GPL(snd_hda_get_nid_path);
1da177e4 271
196c1766
TI
272/* get the index number corresponding to the path instance;
273 * the index starts from 1, for easier checking the invalid value
274 */
275int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
276{
277 struct hda_gen_spec *spec = codec->spec;
278 struct nid_path *array = spec->paths.list;
279 ssize_t idx;
280
281 if (!spec->paths.used)
282 return 0;
283 idx = path - array;
284 if (idx < 0 || idx >= spec->paths.used)
285 return 0;
286 return idx + 1;
287}
2698ea98 288EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
196c1766
TI
289
290/* get the path instance corresponding to the given index number */
291struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
292{
293 struct hda_gen_spec *spec = codec->spec;
294
295 if (idx <= 0 || idx > spec->paths.used)
296 return NULL;
297 return snd_array_elem(&spec->paths, idx - 1);
298}
2698ea98 299EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
196c1766 300
352f7f91
TI
301/* check whether the given DAC is already found in any existing paths */
302static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
1da177e4 303{
352f7f91
TI
304 struct hda_gen_spec *spec = codec->spec;
305 int i;
1da177e4 306
352f7f91
TI
307 for (i = 0; i < spec->paths.used; i++) {
308 struct nid_path *path = snd_array_elem(&spec->paths, i);
309 if (path->path[0] == nid)
310 return true;
d2569505 311 }
352f7f91
TI
312 return false;
313}
1da177e4 314
352f7f91
TI
315/* check whether the given two widgets can be connected */
316static bool is_reachable_path(struct hda_codec *codec,
317 hda_nid_t from_nid, hda_nid_t to_nid)
318{
319 if (!from_nid || !to_nid)
320 return false;
321 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
322}
1da177e4 323
352f7f91
TI
324/* nid, dir and idx */
325#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
326
327/* check whether the given ctl is already assigned in any path elements */
328static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
329{
330 struct hda_gen_spec *spec = codec->spec;
331 int i;
332
333 val &= AMP_VAL_COMPARE_MASK;
334 for (i = 0; i < spec->paths.used; i++) {
335 struct nid_path *path = snd_array_elem(&spec->paths, i);
336 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
337 return true;
1da177e4 338 }
352f7f91 339 return false;
1da177e4
LT
340}
341
352f7f91
TI
342/* check whether a control with the given (nid, dir, idx) was assigned */
343static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
8999bf0a 344 int dir, int idx, int type)
1da177e4 345{
352f7f91 346 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
8999bf0a 347 return is_ctl_used(codec, val, type);
352f7f91 348}
1da177e4 349
4e76a883
TI
350static void print_nid_path(struct hda_codec *codec,
351 const char *pfx, struct nid_path *path)
0c8c0f56
TI
352{
353 char buf[40];
d82353e5 354 char *pos = buf;
0c8c0f56
TI
355 int i;
356
d82353e5
JP
357 *pos = 0;
358 for (i = 0; i < path->depth; i++)
359 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
360 pos != buf ? ":" : "",
361 path->path[i]);
0c8c0f56 362
d82353e5 363 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
0c8c0f56
TI
364}
365
352f7f91
TI
366/* called recursively */
367static bool __parse_nid_path(struct hda_codec *codec,
368 hda_nid_t from_nid, hda_nid_t to_nid,
3ca529d3
TI
369 int anchor_nid, struct nid_path *path,
370 int depth)
352f7f91 371{
ee8e765b 372 const hda_nid_t *conn;
352f7f91
TI
373 int i, nums;
374
3ca529d3
TI
375 if (to_nid == anchor_nid)
376 anchor_nid = 0; /* anchor passed */
377 else if (to_nid == (hda_nid_t)(-anchor_nid))
378 return false; /* hit the exclusive nid */
1da177e4 379
ee8e765b 380 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
352f7f91
TI
381 for (i = 0; i < nums; i++) {
382 if (conn[i] != from_nid) {
383 /* special case: when from_nid is 0,
384 * try to find an empty DAC
385 */
386 if (from_nid ||
387 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
388 is_dac_already_used(codec, conn[i]))
389 continue;
390 }
3ca529d3
TI
391 /* anchor is not requested or already passed? */
392 if (anchor_nid <= 0)
352f7f91 393 goto found;
1da177e4 394 }
352f7f91
TI
395 if (depth >= MAX_NID_PATH_DEPTH)
396 return false;
397 for (i = 0; i < nums; i++) {
398 unsigned int type;
399 type = get_wcaps_type(get_wcaps(codec, conn[i]));
400 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
401 type == AC_WID_PIN)
402 continue;
403 if (__parse_nid_path(codec, from_nid, conn[i],
3ca529d3 404 anchor_nid, path, depth + 1))
352f7f91
TI
405 goto found;
406 }
407 return false;
408
409 found:
410 path->path[path->depth] = conn[i];
411 path->idx[path->depth + 1] = i;
412 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
413 path->multi[path->depth + 1] = 1;
414 path->depth++;
415 return true;
1da177e4
LT
416}
417
352f7f91
TI
418/* parse the widget path from the given nid to the target nid;
419 * when @from_nid is 0, try to find an empty DAC;
3ca529d3
TI
420 * when @anchor_nid is set to a positive value, only paths through the widget
421 * with the given value are evaluated.
422 * when @anchor_nid is set to a negative value, paths through the widget
423 * with the negative of given value are excluded, only other paths are chosen.
424 * when @anchor_nid is zero, no special handling about path selection.
1da177e4 425 */
352f7f91 426bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
3ca529d3 427 hda_nid_t to_nid, int anchor_nid,
352f7f91 428 struct nid_path *path)
1da177e4 429{
3ca529d3 430 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
352f7f91
TI
431 path->path[path->depth] = to_nid;
432 path->depth++;
352f7f91 433 return true;
1da177e4 434 }
352f7f91 435 return false;
1da177e4 436}
2698ea98 437EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path);
1da177e4
LT
438
439/*
352f7f91
TI
440 * parse the path between the given NIDs and add to the path list.
441 * if no valid path is found, return NULL
1da177e4 442 */
352f7f91
TI
443struct nid_path *
444snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
3ca529d3 445 hda_nid_t to_nid, int anchor_nid)
352f7f91
TI
446{
447 struct hda_gen_spec *spec = codec->spec;
448 struct nid_path *path;
449
450 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
451 return NULL;
452
f5172a7e 453 /* check whether the path has been already added */
3ca529d3 454 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
f5172a7e
TI
455 if (path)
456 return path;
457
352f7f91
TI
458 path = snd_array_new(&spec->paths);
459 if (!path)
460 return NULL;
461 memset(path, 0, sizeof(*path));
3ca529d3 462 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
352f7f91
TI
463 return path;
464 /* push back */
465 spec->paths.used--;
466 return NULL;
1da177e4 467}
2698ea98 468EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
1da177e4 469
980428ce
TI
470/* clear the given path as invalid so that it won't be picked up later */
471static void invalidate_nid_path(struct hda_codec *codec, int idx)
472{
473 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
474 if (!path)
475 return;
476 memset(path, 0, sizeof(*path));
477}
478
3690739b
TI
479/* return a DAC if paired to the given pin by codec driver */
480static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
481{
482 struct hda_gen_spec *spec = codec->spec;
483 const hda_nid_t *list = spec->preferred_dacs;
484
485 if (!list)
486 return 0;
487 for (; *list; list += 2)
488 if (*list == pin)
489 return list[1];
490 return 0;
491}
492
352f7f91
TI
493/* look for an empty DAC slot */
494static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
495 bool is_digital)
496{
497 struct hda_gen_spec *spec = codec->spec;
498 bool cap_digital;
499 int i;
500
501 for (i = 0; i < spec->num_all_dacs; i++) {
502 hda_nid_t nid = spec->all_dacs[i];
503 if (!nid || is_dac_already_used(codec, nid))
504 continue;
505 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
506 if (is_digital != cap_digital)
507 continue;
508 if (is_reachable_path(codec, nid, pin))
509 return nid;
510 }
82beb8fd 511 return 0;
1da177e4
LT
512}
513
352f7f91
TI
514/* replace the channels in the composed amp value with the given number */
515static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
1da177e4 516{
352f7f91
TI
517 val &= ~(0x3U << 16);
518 val |= chs << 16;
519 return val;
1da177e4
LT
520}
521
352f7f91
TI
522/* check whether the widget has the given amp capability for the direction */
523static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
524 int dir, unsigned int bits)
1da177e4 525{
352f7f91
TI
526 if (!nid)
527 return false;
528 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
529 if (query_amp_caps(codec, nid, dir) & bits)
530 return true;
531 return false;
532}
533
99a5592d
DH
534static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
535 hda_nid_t nid2, int dir)
536{
537 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
538 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
539 return (query_amp_caps(codec, nid1, dir) ==
540 query_amp_caps(codec, nid2, dir));
541}
542
352f7f91 543#define nid_has_mute(codec, nid, dir) \
f69910dd 544 check_amp_caps(codec, nid, dir, (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE))
352f7f91
TI
545#define nid_has_volume(codec, nid, dir) \
546 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
547
548/* look for a widget suitable for assigning a mute switch in the path */
549static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
550 struct nid_path *path)
551{
552 int i;
553
554 for (i = path->depth - 1; i >= 0; i--) {
555 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
556 return path->path[i];
557 if (i != path->depth - 1 && i != 0 &&
558 nid_has_mute(codec, path->path[i], HDA_INPUT))
559 return path->path[i];
560 }
561 return 0;
562}
563
564/* look for a widget suitable for assigning a volume ctl in the path */
565static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
566 struct nid_path *path)
567{
a1114a8c 568 struct hda_gen_spec *spec = codec->spec;
352f7f91 569 int i;
1da177e4 570
352f7f91 571 for (i = path->depth - 1; i >= 0; i--) {
a1114a8c
TI
572 hda_nid_t nid = path->path[i];
573 if ((spec->out_vol_mask >> nid) & 1)
574 continue;
575 if (nid_has_volume(codec, nid, HDA_OUTPUT))
576 return nid;
1da177e4 577 }
352f7f91 578 return 0;
1da177e4
LT
579}
580
581/*
352f7f91 582 * path activation / deactivation
1da177e4 583 */
352f7f91
TI
584
585/* can have the amp-in capability? */
586static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
1da177e4 587{
352f7f91
TI
588 hda_nid_t nid = path->path[idx];
589 unsigned int caps = get_wcaps(codec, nid);
590 unsigned int type = get_wcaps_type(caps);
591
592 if (!(caps & AC_WCAP_IN_AMP))
593 return false;
594 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
595 return false;
596 return true;
597}
1da177e4 598
352f7f91
TI
599/* can have the amp-out capability? */
600static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
601{
602 hda_nid_t nid = path->path[idx];
603 unsigned int caps = get_wcaps(codec, nid);
604 unsigned int type = get_wcaps_type(caps);
605
606 if (!(caps & AC_WCAP_OUT_AMP))
607 return false;
608 if (type == AC_WID_PIN && !idx) /* only for output pins */
609 return false;
610 return true;
611}
1da177e4 612
352f7f91
TI
613/* check whether the given (nid,dir,idx) is active */
614static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
7dddf2ae 615 unsigned int dir, unsigned int idx)
352f7f91
TI
616{
617 struct hda_gen_spec *spec = codec->spec;
618 int i, n;
1da177e4 619
352f7f91
TI
620 for (n = 0; n < spec->paths.used; n++) {
621 struct nid_path *path = snd_array_elem(&spec->paths, n);
622 if (!path->active)
1da177e4 623 continue;
352f7f91
TI
624 for (i = 0; i < path->depth; i++) {
625 if (path->path[i] == nid) {
626 if (dir == HDA_OUTPUT || path->idx[i] == idx)
627 return true;
628 break;
1da177e4 629 }
1da177e4
LT
630 }
631 }
352f7f91 632 return false;
1da177e4
LT
633}
634
b1b9fbd0
TI
635/* check whether the NID is referred by any active paths */
636#define is_active_nid_for_any(codec, nid) \
637 is_active_nid(codec, nid, HDA_OUTPUT, 0)
638
352f7f91
TI
639/* get the default amp value for the target state */
640static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
8999bf0a 641 int dir, unsigned int caps, bool enable)
1da177e4 642{
352f7f91
TI
643 unsigned int val = 0;
644
352f7f91
TI
645 if (caps & AC_AMPCAP_NUM_STEPS) {
646 /* set to 0dB */
647 if (enable)
648 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
1da177e4 649 }
f69910dd 650 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
352f7f91
TI
651 if (!enable)
652 val |= HDA_AMP_MUTE;
653 }
654 return val;
1da177e4
LT
655}
656
352f7f91
TI
657/* initialize the amp value (only at the first time) */
658static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
659{
8999bf0a
TI
660 unsigned int caps = query_amp_caps(codec, nid, dir);
661 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
352f7f91
TI
662 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
663}
1da177e4 664
8999bf0a
TI
665/* calculate amp value mask we can modify;
666 * if the given amp is controlled by mixers, don't touch it
667 */
668static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
669 hda_nid_t nid, int dir, int idx,
670 unsigned int caps)
671{
672 unsigned int mask = 0xff;
673
f69910dd 674 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
8999bf0a
TI
675 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
676 mask &= ~0x80;
677 }
678 if (caps & AC_AMPCAP_NUM_STEPS) {
679 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
680 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
681 mask &= ~0x7f;
682 }
683 return mask;
684}
685
352f7f91 686static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
8999bf0a 687 int idx, int idx_to_check, bool enable)
352f7f91 688{
8999bf0a
TI
689 unsigned int caps;
690 unsigned int mask, val;
691
7dddf2ae 692 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
8999bf0a
TI
693 return;
694
695 caps = query_amp_caps(codec, nid, dir);
696 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
697 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
698 if (!mask)
352f7f91 699 return;
8999bf0a
TI
700
701 val &= mask;
702 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
352f7f91
TI
703}
704
705static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
706 int i, bool enable)
707{
708 hda_nid_t nid = path->path[i];
709 init_amp(codec, nid, HDA_OUTPUT, 0);
8999bf0a 710 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
352f7f91
TI
711}
712
713static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
714 int i, bool enable, bool add_aamix)
1da177e4 715{
352f7f91 716 struct hda_gen_spec *spec = codec->spec;
ee8e765b 717 const hda_nid_t *conn;
352f7f91
TI
718 int n, nums, idx;
719 int type;
720 hda_nid_t nid = path->path[i];
721
ee8e765b 722 nums = snd_hda_get_conn_list(codec, nid, &conn);
352f7f91
TI
723 type = get_wcaps_type(get_wcaps(codec, nid));
724 if (type == AC_WID_PIN ||
725 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
726 nums = 1;
727 idx = 0;
728 } else
729 idx = path->idx[i];
730
731 for (n = 0; n < nums; n++)
732 init_amp(codec, nid, HDA_INPUT, n);
733
352f7f91
TI
734 /* here is a little bit tricky in comparison with activate_amp_out();
735 * when aa-mixer is available, we need to enable the path as well
1da177e4 736 */
352f7f91 737 for (n = 0; n < nums; n++) {
e4a395e7 738 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
352f7f91 739 continue;
8999bf0a 740 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
1da177e4 741 }
352f7f91 742}
1da177e4 743
352f7f91
TI
744/* activate or deactivate the given path
745 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
746 */
747void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
748 bool enable, bool add_aamix)
749{
55196fff 750 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
751 int i;
752
753 if (!enable)
754 path->active = false;
755
756 for (i = path->depth - 1; i >= 0; i--) {
55196fff
TI
757 hda_nid_t nid = path->path[i];
758 if (enable && spec->power_down_unused) {
759 /* make sure the widget is powered up */
760 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
761 snd_hda_codec_write(codec, nid, 0,
762 AC_VERB_SET_POWER_STATE,
763 AC_PWRST_D0);
764 }
352f7f91 765 if (enable && path->multi[i])
8f0972df 766 snd_hda_codec_update_cache(codec, nid, 0,
352f7f91
TI
767 AC_VERB_SET_CONNECT_SEL,
768 path->idx[i]);
769 if (has_amp_in(codec, path, i))
770 activate_amp_in(codec, path, i, enable, add_aamix);
771 if (has_amp_out(codec, path, i))
772 activate_amp_out(codec, path, i, enable);
1da177e4
LT
773 }
774
352f7f91
TI
775 if (enable)
776 path->active = true;
1da177e4 777}
2698ea98 778EXPORT_SYMBOL_GPL(snd_hda_activate_path);
352f7f91 779
55196fff
TI
780/* if the given path is inactive, put widgets into D3 (only if suitable) */
781static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
782{
783 struct hda_gen_spec *spec = codec->spec;
868211db 784 bool changed = false;
55196fff
TI
785 int i;
786
787 if (!spec->power_down_unused || path->active)
788 return;
789
790 for (i = 0; i < path->depth; i++) {
791 hda_nid_t nid = path->path[i];
b1b9fbd0
TI
792 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
793 !is_active_nid_for_any(codec, nid)) {
55196fff
TI
794 snd_hda_codec_write(codec, nid, 0,
795 AC_VERB_SET_POWER_STATE,
796 AC_PWRST_D3);
797 changed = true;
798 }
799 }
800
801 if (changed) {
802 msleep(10);
803 snd_hda_codec_read(codec, path->path[0], 0,
804 AC_VERB_GET_POWER_STATE, 0);
805 }
806}
807
d5a9f1bb
TI
808/* turn on/off EAPD on the given pin */
809static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
810{
811 struct hda_gen_spec *spec = codec->spec;
812 if (spec->own_eapd_ctl ||
813 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
814 return;
05909d5c
TI
815 if (spec->keep_eapd_on && !enable)
816 return;
468ac413
TI
817 if (codec->inv_eapd)
818 enable = !enable;
d5a9f1bb
TI
819 snd_hda_codec_update_cache(codec, pin, 0,
820 AC_VERB_SET_EAPD_BTLENABLE,
821 enable ? 0x02 : 0x00);
822}
823
3e367f15
TI
824/* re-initialize the path specified by the given path index */
825static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
826{
827 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
828 if (path)
829 snd_hda_activate_path(codec, path, path->active, false);
830}
831
1da177e4
LT
832
833/*
352f7f91 834 * Helper functions for creating mixer ctl elements
1da177e4
LT
835 */
836
7eebffd3
TI
837static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
838 struct snd_ctl_elem_value *ucontrol);
bc2eee29
TI
839static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
840 struct snd_ctl_elem_value *ucontrol);
7eebffd3 841
352f7f91
TI
842enum {
843 HDA_CTL_WIDGET_VOL,
844 HDA_CTL_WIDGET_MUTE,
845 HDA_CTL_BIND_MUTE,
352f7f91
TI
846};
847static const struct snd_kcontrol_new control_templates[] = {
848 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
7eebffd3
TI
849 /* only the put callback is replaced for handling the special mute */
850 {
851 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
852 .subdevice = HDA_SUBDEV_AMP_FLAG,
853 .info = snd_hda_mixer_amp_switch_info,
854 .get = snd_hda_mixer_amp_switch_get,
855 .put = hda_gen_mixer_mute_put, /* replaced */
856 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
857 },
bc2eee29
TI
858 {
859 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
860 .info = snd_hda_mixer_amp_switch_info,
861 .get = snd_hda_mixer_bind_switch_get,
862 .put = hda_gen_bind_mute_put, /* replaced */
863 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
864 },
352f7f91 865};
1da177e4 866
352f7f91 867/* add dynamic controls from template */
a35bd1e3
TI
868static struct snd_kcontrol_new *
869add_control(struct hda_gen_spec *spec, int type, const char *name,
352f7f91 870 int cidx, unsigned long val)
1da177e4 871{
352f7f91 872 struct snd_kcontrol_new *knew;
1da177e4 873
12c93df6 874 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
352f7f91 875 if (!knew)
a35bd1e3 876 return NULL;
352f7f91
TI
877 knew->index = cidx;
878 if (get_amp_nid_(val))
879 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
880 knew->private_value = val;
a35bd1e3 881 return knew;
1da177e4
LT
882}
883
352f7f91
TI
884static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
885 const char *pfx, const char *dir,
886 const char *sfx, int cidx, unsigned long val)
1da177e4 887{
975cc02a 888 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
352f7f91 889 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
a35bd1e3
TI
890 if (!add_control(spec, type, name, cidx, val))
891 return -ENOMEM;
892 return 0;
1da177e4
LT
893}
894
352f7f91
TI
895#define add_pb_vol_ctrl(spec, type, pfx, val) \
896 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
897#define add_pb_sw_ctrl(spec, type, pfx, val) \
898 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
899#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
900 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
901#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
902 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
903
904static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
905 unsigned int chs, struct nid_path *path)
906{
907 unsigned int val;
908 if (!path)
909 return 0;
910 val = path->ctls[NID_PATH_VOL_CTL];
911 if (!val)
912 return 0;
913 val = amp_val_replace_channels(val, chs);
914 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
915}
916
917/* return the channel bits suitable for the given path->ctls[] */
918static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
919 int type)
920{
921 int chs = 1; /* mono (left only) */
922 if (path) {
923 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
924 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
925 chs = 3; /* stereo */
1da177e4 926 }
352f7f91 927 return chs;
1da177e4
LT
928}
929
352f7f91
TI
930static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
931 struct nid_path *path)
932{
933 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
934 return add_vol_ctl(codec, pfx, cidx, chs, path);
935}
936
937/* create a mute-switch for the given mixer widget;
938 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1da177e4 939 */
352f7f91
TI
940static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
941 unsigned int chs, struct nid_path *path)
1da177e4 942{
352f7f91
TI
943 unsigned int val;
944 int type = HDA_CTL_WIDGET_MUTE;
1da177e4 945
352f7f91 946 if (!path)
1da177e4 947 return 0;
352f7f91
TI
948 val = path->ctls[NID_PATH_MUTE_CTL];
949 if (!val)
1da177e4 950 return 0;
352f7f91
TI
951 val = amp_val_replace_channels(val, chs);
952 if (get_amp_direction_(val) == HDA_INPUT) {
953 hda_nid_t nid = get_amp_nid_(val);
954 int nums = snd_hda_get_num_conns(codec, nid);
955 if (nums > 1) {
956 type = HDA_CTL_BIND_MUTE;
957 val |= nums << 19;
958 }
1da177e4 959 }
352f7f91
TI
960 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
961}
1da177e4 962
352f7f91
TI
963static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
964 int cidx, struct nid_path *path)
965{
966 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
967 return add_sw_ctl(codec, pfx, cidx, chs, path);
968}
1da177e4 969
7eebffd3 970/* playback mute control with the software mute bit check */
bc2eee29
TI
971static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
972 struct snd_ctl_elem_value *ucontrol)
7eebffd3
TI
973{
974 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
975 struct hda_gen_spec *spec = codec->spec;
976
977 if (spec->auto_mute_via_amp) {
978 hda_nid_t nid = get_amp_nid(kcontrol);
979 bool enabled = !((spec->mute_bits >> nid) & 1);
980 ucontrol->value.integer.value[0] &= enabled;
981 ucontrol->value.integer.value[1] &= enabled;
982 }
bc2eee29 983}
7eebffd3 984
bc2eee29
TI
985static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
986 struct snd_ctl_elem_value *ucontrol)
987{
988 sync_auto_mute_bits(kcontrol, ucontrol);
7eebffd3
TI
989 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
990}
991
bc2eee29
TI
992static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
993 struct snd_ctl_elem_value *ucontrol)
994{
995 sync_auto_mute_bits(kcontrol, ucontrol);
996 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
997}
998
247d85ee
TI
999/* any ctl assigned to the path with the given index? */
1000static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1001{
1002 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1003 return path && path->ctls[ctl_type];
1004}
1005
352f7f91
TI
1006static const char * const channel_name[4] = {
1007 "Front", "Surround", "CLFE", "Side"
1008};
97ec558a 1009
352f7f91 1010/* give some appropriate ctl name prefix for the given line out channel */
247d85ee
TI
1011static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1012 int *index, int ctl_type)
352f7f91 1013{
247d85ee 1014 struct hda_gen_spec *spec = codec->spec;
352f7f91 1015 struct auto_pin_cfg *cfg = &spec->autocfg;
1da177e4 1016
352f7f91
TI
1017 *index = 0;
1018 if (cfg->line_outs == 1 && !spec->multi_ios &&
247d85ee 1019 !cfg->hp_outs && !cfg->speaker_outs)
352f7f91 1020 return spec->vmaster_mute.hook ? "PCM" : "Master";
1da177e4 1021
352f7f91
TI
1022 /* if there is really a single DAC used in the whole output paths,
1023 * use it master (or "PCM" if a vmaster hook is present)
1024 */
1025 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1026 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1027 return spec->vmaster_mute.hook ? "PCM" : "Master";
1028
247d85ee
TI
1029 /* multi-io channels */
1030 if (ch >= cfg->line_outs)
1031 return channel_name[ch];
1032
352f7f91
TI
1033 switch (cfg->line_out_type) {
1034 case AUTO_PIN_SPEAKER_OUT:
247d85ee
TI
1035 /* if the primary channel vol/mute is shared with HP volume,
1036 * don't name it as Speaker
1037 */
1038 if (!ch && cfg->hp_outs &&
1039 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1040 break;
352f7f91
TI
1041 if (cfg->line_outs == 1)
1042 return "Speaker";
1043 if (cfg->line_outs == 2)
1044 return ch ? "Bass Speaker" : "Speaker";
1045 break;
1046 case AUTO_PIN_HP_OUT:
247d85ee
TI
1047 /* if the primary channel vol/mute is shared with spk volume,
1048 * don't name it as Headphone
1049 */
1050 if (!ch && cfg->speaker_outs &&
1051 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1052 break;
352f7f91
TI
1053 /* for multi-io case, only the primary out */
1054 if (ch && spec->multi_ios)
1055 break;
1056 *index = ch;
1057 return "Headphone";
352f7f91 1058 }
247d85ee
TI
1059
1060 /* for a single channel output, we don't have to name the channel */
1061 if (cfg->line_outs == 1 && !spec->multi_ios)
1062 return "PCM";
1063
352f7f91
TI
1064 if (ch >= ARRAY_SIZE(channel_name)) {
1065 snd_BUG();
1066 return "PCM";
1da177e4 1067 }
1da177e4 1068
352f7f91 1069 return channel_name[ch];
1da177e4
LT
1070}
1071
1072/*
352f7f91 1073 * Parse output paths
1da177e4 1074 */
352f7f91
TI
1075
1076/* badness definition */
1077enum {
1078 /* No primary DAC is found for the main output */
1079 BAD_NO_PRIMARY_DAC = 0x10000,
1080 /* No DAC is found for the extra output */
1081 BAD_NO_DAC = 0x4000,
1082 /* No possible multi-ios */
1d739066 1083 BAD_MULTI_IO = 0x120,
352f7f91
TI
1084 /* No individual DAC for extra output */
1085 BAD_NO_EXTRA_DAC = 0x102,
1086 /* No individual DAC for extra surrounds */
1087 BAD_NO_EXTRA_SURR_DAC = 0x101,
1088 /* Primary DAC shared with main surrounds */
1089 BAD_SHARED_SURROUND = 0x100,
55a63d4d 1090 /* No independent HP possible */
bec8e680 1091 BAD_NO_INDEP_HP = 0x10,
352f7f91
TI
1092 /* Primary DAC shared with main CLFE */
1093 BAD_SHARED_CLFE = 0x10,
1094 /* Primary DAC shared with extra surrounds */
1095 BAD_SHARED_EXTRA_SURROUND = 0x10,
1096 /* Volume widget is shared */
1097 BAD_SHARED_VOL = 0x10,
1098};
1099
0e614dd0 1100/* look for widgets in the given path which are appropriate for
352f7f91
TI
1101 * volume and mute controls, and assign the values to ctls[].
1102 *
1103 * When no appropriate widget is found in the path, the badness value
1104 * is incremented depending on the situation. The function returns the
1105 * total badness for both volume and mute controls.
1106 */
0e614dd0 1107static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1da177e4 1108{
d89c6c0c 1109 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
1110 hda_nid_t nid;
1111 unsigned int val;
1112 int badness = 0;
1113
1114 if (!path)
1115 return BAD_SHARED_VOL * 2;
0e614dd0
TI
1116
1117 if (path->ctls[NID_PATH_VOL_CTL] ||
1118 path->ctls[NID_PATH_MUTE_CTL])
1119 return 0; /* already evaluated */
1120
352f7f91
TI
1121 nid = look_for_out_vol_nid(codec, path);
1122 if (nid) {
1123 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
d89c6c0c
TI
1124 if (spec->dac_min_mute)
1125 val |= HDA_AMP_VAL_MIN_MUTE;
352f7f91
TI
1126 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1127 badness += BAD_SHARED_VOL;
1128 else
1129 path->ctls[NID_PATH_VOL_CTL] = val;
1130 } else
1131 badness += BAD_SHARED_VOL;
1132 nid = look_for_out_mute_nid(codec, path);
1133 if (nid) {
1134 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1135 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1136 nid_has_mute(codec, nid, HDA_OUTPUT))
1137 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1138 else
1139 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1140 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1141 badness += BAD_SHARED_VOL;
1142 else
1143 path->ctls[NID_PATH_MUTE_CTL] = val;
1144 } else
1145 badness += BAD_SHARED_VOL;
1146 return badness;
1147}
1da177e4 1148
98bd1115 1149const struct badness_table hda_main_out_badness = {
352f7f91
TI
1150 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1151 .no_dac = BAD_NO_DAC,
1152 .shared_primary = BAD_NO_PRIMARY_DAC,
1153 .shared_surr = BAD_SHARED_SURROUND,
1154 .shared_clfe = BAD_SHARED_CLFE,
1155 .shared_surr_main = BAD_SHARED_SURROUND,
1156};
2698ea98 1157EXPORT_SYMBOL_GPL(hda_main_out_badness);
352f7f91 1158
98bd1115 1159const struct badness_table hda_extra_out_badness = {
352f7f91
TI
1160 .no_primary_dac = BAD_NO_DAC,
1161 .no_dac = BAD_NO_DAC,
1162 .shared_primary = BAD_NO_EXTRA_DAC,
1163 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1164 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1165 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1166};
2698ea98 1167EXPORT_SYMBOL_GPL(hda_extra_out_badness);
352f7f91 1168
7385df61
TI
1169/* get the DAC of the primary output corresponding to the given array index */
1170static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1171{
1172 struct hda_gen_spec *spec = codec->spec;
1173 struct auto_pin_cfg *cfg = &spec->autocfg;
1174
1175 if (cfg->line_outs > idx)
1176 return spec->private_dac_nids[idx];
1177 idx -= cfg->line_outs;
1178 if (spec->multi_ios > idx)
1179 return spec->multi_io[idx].dac;
1180 return 0;
1181}
1182
1183/* return the DAC if it's reachable, otherwise zero */
1184static inline hda_nid_t try_dac(struct hda_codec *codec,
1185 hda_nid_t dac, hda_nid_t pin)
1186{
1187 return is_reachable_path(codec, dac, pin) ? dac : 0;
1188}
1189
352f7f91
TI
1190/* try to assign DACs to pins and return the resultant badness */
1191static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1192 const hda_nid_t *pins, hda_nid_t *dacs,
196c1766 1193 int *path_idx,
352f7f91
TI
1194 const struct badness_table *bad)
1195{
1196 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
1197 int i, j;
1198 int badness = 0;
1199 hda_nid_t dac;
1200
1201 if (!num_outs)
1202 return 0;
1203
1204 for (i = 0; i < num_outs; i++) {
0c8c0f56 1205 struct nid_path *path;
352f7f91 1206 hda_nid_t pin = pins[i];
1e0b5286 1207
0e614dd0
TI
1208 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1209 if (path) {
1210 badness += assign_out_path_ctls(codec, path);
1e0b5286
TI
1211 continue;
1212 }
1213
3690739b
TI
1214 dacs[i] = get_preferred_dac(codec, pin);
1215 if (dacs[i]) {
1216 if (is_dac_already_used(codec, dacs[i]))
1217 badness += bad->shared_primary;
1218 }
1219
1220 if (!dacs[i])
1221 dacs[i] = look_for_dac(codec, pin, false);
352f7f91 1222 if (!dacs[i] && !i) {
980428ce 1223 /* try to steal the DAC of surrounds for the front */
352f7f91
TI
1224 for (j = 1; j < num_outs; j++) {
1225 if (is_reachable_path(codec, dacs[j], pin)) {
1226 dacs[0] = dacs[j];
1227 dacs[j] = 0;
980428ce 1228 invalidate_nid_path(codec, path_idx[j]);
196c1766 1229 path_idx[j] = 0;
352f7f91
TI
1230 break;
1231 }
1232 }
071c73ad 1233 }
352f7f91
TI
1234 dac = dacs[i];
1235 if (!dac) {
7385df61
TI
1236 if (num_outs > 2)
1237 dac = try_dac(codec, get_primary_out(codec, i), pin);
1238 if (!dac)
1239 dac = try_dac(codec, dacs[0], pin);
1240 if (!dac)
1241 dac = try_dac(codec, get_primary_out(codec, i), pin);
352f7f91
TI
1242 if (dac) {
1243 if (!i)
1244 badness += bad->shared_primary;
1245 else if (i == 1)
1246 badness += bad->shared_surr;
1247 else
1248 badness += bad->shared_clfe;
1249 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1250 dac = spec->private_dac_nids[0];
1251 badness += bad->shared_surr_main;
1252 } else if (!i)
1253 badness += bad->no_primary_dac;
1254 else
1255 badness += bad->no_dac;
1da177e4 1256 }
1fa335b0
TI
1257 if (!dac)
1258 continue;
3ca529d3 1259 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
117688a9 1260 if (!path && !i && spec->mixer_nid) {
b3a8c745 1261 /* try with aamix */
3ca529d3 1262 path = snd_hda_add_new_path(codec, dac, pin, 0);
b3a8c745 1263 }
1fa335b0 1264 if (!path) {
352f7f91 1265 dac = dacs[i] = 0;
1fa335b0
TI
1266 badness += bad->no_dac;
1267 } else {
4e76a883 1268 /* print_nid_path(codec, "output", path); */
e1284af7 1269 path->active = true;
196c1766 1270 path_idx[i] = snd_hda_get_path_idx(codec, path);
0e614dd0 1271 badness += assign_out_path_ctls(codec, path);
e1284af7 1272 }
1da177e4
LT
1273 }
1274
352f7f91 1275 return badness;
1da177e4
LT
1276}
1277
352f7f91
TI
1278/* return NID if the given pin has only a single connection to a certain DAC */
1279static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1da177e4 1280{
352f7f91
TI
1281 struct hda_gen_spec *spec = codec->spec;
1282 int i;
1283 hda_nid_t nid_found = 0;
1da177e4 1284
352f7f91
TI
1285 for (i = 0; i < spec->num_all_dacs; i++) {
1286 hda_nid_t nid = spec->all_dacs[i];
1287 if (!nid || is_dac_already_used(codec, nid))
1288 continue;
1289 if (is_reachable_path(codec, nid, pin)) {
1290 if (nid_found)
1da177e4 1291 return 0;
352f7f91 1292 nid_found = nid;
1da177e4
LT
1293 }
1294 }
352f7f91 1295 return nid_found;
1da177e4
LT
1296}
1297
352f7f91
TI
1298/* check whether the given pin can be a multi-io pin */
1299static bool can_be_multiio_pin(struct hda_codec *codec,
1300 unsigned int location, hda_nid_t nid)
cb53c626 1301{
352f7f91
TI
1302 unsigned int defcfg, caps;
1303
1304 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1305 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1306 return false;
1307 if (location && get_defcfg_location(defcfg) != location)
1308 return false;
1309 caps = snd_hda_query_pin_caps(codec, nid);
1310 if (!(caps & AC_PINCAP_OUT))
1311 return false;
1312 return true;
cb53c626 1313}
cb53c626 1314
e22aab7d
TI
1315/* count the number of input pins that are capable to be multi-io */
1316static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1317{
1318 struct hda_gen_spec *spec = codec->spec;
1319 struct auto_pin_cfg *cfg = &spec->autocfg;
1320 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1321 unsigned int location = get_defcfg_location(defcfg);
1322 int type, i;
1323 int num_pins = 0;
1324
1325 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1326 for (i = 0; i < cfg->num_inputs; i++) {
1327 if (cfg->inputs[i].type != type)
1328 continue;
1329 if (can_be_multiio_pin(codec, location,
1330 cfg->inputs[i].pin))
1331 num_pins++;
1332 }
1333 }
1334 return num_pins;
1335}
1336
1da177e4 1337/*
352f7f91
TI
1338 * multi-io helper
1339 *
1340 * When hardwired is set, try to fill ony hardwired pins, and returns
1341 * zero if any pins are filled, non-zero if nothing found.
1342 * When hardwired is off, try to fill possible input pins, and returns
1343 * the badness value.
1da177e4 1344 */
352f7f91
TI
1345static int fill_multi_ios(struct hda_codec *codec,
1346 hda_nid_t reference_pin,
e22aab7d 1347 bool hardwired)
1da177e4 1348{
352f7f91
TI
1349 struct hda_gen_spec *spec = codec->spec;
1350 struct auto_pin_cfg *cfg = &spec->autocfg;
e22aab7d 1351 int type, i, j, num_pins, old_pins;
352f7f91
TI
1352 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1353 unsigned int location = get_defcfg_location(defcfg);
1354 int badness = 0;
0e614dd0 1355 struct nid_path *path;
352f7f91
TI
1356
1357 old_pins = spec->multi_ios;
1358 if (old_pins >= 2)
1359 goto end_fill;
1360
e22aab7d 1361 num_pins = count_multiio_pins(codec, reference_pin);
352f7f91
TI
1362 if (num_pins < 2)
1363 goto end_fill;
1da177e4 1364
352f7f91
TI
1365 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1366 for (i = 0; i < cfg->num_inputs; i++) {
1367 hda_nid_t nid = cfg->inputs[i].pin;
1368 hda_nid_t dac = 0;
1da177e4 1369
352f7f91
TI
1370 if (cfg->inputs[i].type != type)
1371 continue;
1372 if (!can_be_multiio_pin(codec, location, nid))
1373 continue;
1374 for (j = 0; j < spec->multi_ios; j++) {
1375 if (nid == spec->multi_io[j].pin)
1376 break;
1377 }
1378 if (j < spec->multi_ios)
1379 continue;
1380
352f7f91
TI
1381 if (hardwired)
1382 dac = get_dac_if_single(codec, nid);
1383 else if (!dac)
1384 dac = look_for_dac(codec, nid, false);
1385 if (!dac) {
1386 badness++;
1387 continue;
1388 }
3ca529d3
TI
1389 path = snd_hda_add_new_path(codec, dac, nid,
1390 -spec->mixer_nid);
0c8c0f56 1391 if (!path) {
352f7f91
TI
1392 badness++;
1393 continue;
1394 }
4e76a883 1395 /* print_nid_path(codec, "multiio", path); */
352f7f91
TI
1396 spec->multi_io[spec->multi_ios].pin = nid;
1397 spec->multi_io[spec->multi_ios].dac = dac;
196c1766
TI
1398 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1399 snd_hda_get_path_idx(codec, path);
352f7f91
TI
1400 spec->multi_ios++;
1401 if (spec->multi_ios >= 2)
1402 break;
1403 }
1404 }
1405 end_fill:
1406 if (badness)
1407 badness = BAD_MULTI_IO;
1408 if (old_pins == spec->multi_ios) {
1409 if (hardwired)
1410 return 1; /* nothing found */
1411 else
1412 return badness; /* no badness if nothing found */
1413 }
1414 if (!hardwired && spec->multi_ios < 2) {
1415 /* cancel newly assigned paths */
1416 spec->paths.used -= spec->multi_ios - old_pins;
1417 spec->multi_ios = old_pins;
1418 return badness;
1419 }
1420
1421 /* assign volume and mute controls */
0e614dd0
TI
1422 for (i = old_pins; i < spec->multi_ios; i++) {
1423 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1424 badness += assign_out_path_ctls(codec, path);
1425 }
352f7f91
TI
1426
1427 return badness;
1428}
1429
1430/* map DACs for all pins in the list if they are single connections */
1431static bool map_singles(struct hda_codec *codec, int outs,
196c1766 1432 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
352f7f91 1433{
b3a8c745 1434 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
1435 int i;
1436 bool found = false;
1437 for (i = 0; i < outs; i++) {
0c8c0f56 1438 struct nid_path *path;
352f7f91
TI
1439 hda_nid_t dac;
1440 if (dacs[i])
1441 continue;
1442 dac = get_dac_if_single(codec, pins[i]);
1443 if (!dac)
1444 continue;
3ca529d3
TI
1445 path = snd_hda_add_new_path(codec, dac, pins[i],
1446 -spec->mixer_nid);
117688a9 1447 if (!path && !i && spec->mixer_nid)
3ca529d3 1448 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
0c8c0f56 1449 if (path) {
352f7f91
TI
1450 dacs[i] = dac;
1451 found = true;
4e76a883 1452 /* print_nid_path(codec, "output", path); */
e1284af7 1453 path->active = true;
196c1766 1454 path_idx[i] = snd_hda_get_path_idx(codec, path);
352f7f91
TI
1455 }
1456 }
1457 return found;
1458}
1459
c30aa7b2
TI
1460/* create a new path including aamix if available, and return its index */
1461static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1462{
3ca529d3 1463 struct hda_gen_spec *spec = codec->spec;
c30aa7b2 1464 struct nid_path *path;
5ead56f2 1465 hda_nid_t path_dac, dac, pin;
c30aa7b2
TI
1466
1467 path = snd_hda_get_path_from_idx(codec, path_idx);
3ca529d3
TI
1468 if (!path || !path->depth ||
1469 is_nid_contained(path, spec->mixer_nid))
c30aa7b2 1470 return 0;
5ead56f2
TI
1471 path_dac = path->path[0];
1472 dac = spec->private_dac_nids[0];
f87498b6
TI
1473 pin = path->path[path->depth - 1];
1474 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1475 if (!path) {
5ead56f2
TI
1476 if (dac != path_dac)
1477 dac = path_dac;
f87498b6
TI
1478 else if (spec->multiout.hp_out_nid[0])
1479 dac = spec->multiout.hp_out_nid[0];
1480 else if (spec->multiout.extra_out_nid[0])
1481 dac = spec->multiout.extra_out_nid[0];
5ead56f2
TI
1482 else
1483 dac = 0;
f87498b6
TI
1484 if (dac)
1485 path = snd_hda_add_new_path(codec, dac, pin,
1486 spec->mixer_nid);
1487 }
c30aa7b2
TI
1488 if (!path)
1489 return 0;
4e76a883 1490 /* print_nid_path(codec, "output-aamix", path); */
c30aa7b2
TI
1491 path->active = false; /* unused as default */
1492 return snd_hda_get_path_idx(codec, path);
1493}
1494
55a63d4d
TI
1495/* check whether the independent HP is available with the current config */
1496static bool indep_hp_possible(struct hda_codec *codec)
1497{
1498 struct hda_gen_spec *spec = codec->spec;
1499 struct auto_pin_cfg *cfg = &spec->autocfg;
1500 struct nid_path *path;
1501 int i, idx;
1502
1503 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1504 idx = spec->out_paths[0];
1505 else
1506 idx = spec->hp_paths[0];
1507 path = snd_hda_get_path_from_idx(codec, idx);
1508 if (!path)
1509 return false;
1510
1511 /* assume no path conflicts unless aamix is involved */
1512 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1513 return true;
1514
1515 /* check whether output paths contain aamix */
1516 for (i = 0; i < cfg->line_outs; i++) {
1517 if (spec->out_paths[i] == idx)
1518 break;
1519 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1520 if (path && is_nid_contained(path, spec->mixer_nid))
1521 return false;
1522 }
1523 for (i = 0; i < cfg->speaker_outs; i++) {
1524 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1525 if (path && is_nid_contained(path, spec->mixer_nid))
1526 return false;
1527 }
1528
1529 return true;
1530}
1531
a07a949b
TI
1532/* fill the empty entries in the dac array for speaker/hp with the
1533 * shared dac pointed by the paths
1534 */
1535static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1536 hda_nid_t *dacs, int *path_idx)
1537{
1538 struct nid_path *path;
1539 int i;
1540
1541 for (i = 0; i < num_outs; i++) {
1542 if (dacs[i])
1543 continue;
1544 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1545 if (!path)
1546 continue;
1547 dacs[i] = path->path[0];
1548 }
1549}
1550
352f7f91
TI
1551/* fill in the dac_nids table from the parsed pin configuration */
1552static int fill_and_eval_dacs(struct hda_codec *codec,
1553 bool fill_hardwired,
1554 bool fill_mio_first)
1555{
1556 struct hda_gen_spec *spec = codec->spec;
1557 struct auto_pin_cfg *cfg = &spec->autocfg;
1558 int i, err, badness;
1559
1560 /* set num_dacs once to full for look_for_dac() */
1561 spec->multiout.num_dacs = cfg->line_outs;
1562 spec->multiout.dac_nids = spec->private_dac_nids;
1563 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1564 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1565 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1566 spec->multi_ios = 0;
1567 snd_array_free(&spec->paths);
cd5be3f9
TI
1568
1569 /* clear path indices */
1570 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1571 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1572 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1573 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1574 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
c697b716 1575 memset(spec->input_paths, 0, sizeof(spec->input_paths));
cd5be3f9
TI
1576 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1577 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1578
352f7f91
TI
1579 badness = 0;
1580
1581 /* fill hard-wired DACs first */
1582 if (fill_hardwired) {
1583 bool mapped;
1584 do {
1585 mapped = map_singles(codec, cfg->line_outs,
1586 cfg->line_out_pins,
196c1766
TI
1587 spec->private_dac_nids,
1588 spec->out_paths);
352f7f91
TI
1589 mapped |= map_singles(codec, cfg->hp_outs,
1590 cfg->hp_pins,
196c1766
TI
1591 spec->multiout.hp_out_nid,
1592 spec->hp_paths);
352f7f91
TI
1593 mapped |= map_singles(codec, cfg->speaker_outs,
1594 cfg->speaker_pins,
196c1766
TI
1595 spec->multiout.extra_out_nid,
1596 spec->speaker_paths);
da96fb5b
TI
1597 if (!spec->no_multi_io &&
1598 fill_mio_first && cfg->line_outs == 1 &&
352f7f91 1599 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
e22aab7d 1600 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
352f7f91
TI
1601 if (!err)
1602 mapped = true;
1603 }
1604 } while (mapped);
1605 }
1606
1607 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
196c1766 1608 spec->private_dac_nids, spec->out_paths,
98bd1115 1609 spec->main_out_badness);
352f7f91 1610
da96fb5b 1611 if (!spec->no_multi_io && fill_mio_first &&
352f7f91
TI
1612 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1613 /* try to fill multi-io first */
e22aab7d 1614 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
352f7f91
TI
1615 if (err < 0)
1616 return err;
1617 /* we don't count badness at this stage yet */
1618 }
1619
1620 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1621 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1622 spec->multiout.hp_out_nid,
196c1766 1623 spec->hp_paths,
98bd1115 1624 spec->extra_out_badness);
352f7f91
TI
1625 if (err < 0)
1626 return err;
1627 badness += err;
1628 }
1629 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1630 err = try_assign_dacs(codec, cfg->speaker_outs,
1631 cfg->speaker_pins,
1632 spec->multiout.extra_out_nid,
196c1766 1633 spec->speaker_paths,
98bd1115 1634 spec->extra_out_badness);
352f7f91
TI
1635 if (err < 0)
1636 return err;
1637 badness += err;
1638 }
da96fb5b
TI
1639 if (!spec->no_multi_io &&
1640 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
e22aab7d 1641 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
352f7f91
TI
1642 if (err < 0)
1643 return err;
1644 badness += err;
1645 }
1646
c30aa7b2
TI
1647 if (spec->mixer_nid) {
1648 spec->aamix_out_paths[0] =
1649 check_aamix_out_path(codec, spec->out_paths[0]);
1650 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1651 spec->aamix_out_paths[1] =
1652 check_aamix_out_path(codec, spec->hp_paths[0]);
1653 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1654 spec->aamix_out_paths[2] =
1655 check_aamix_out_path(codec, spec->speaker_paths[0]);
1656 }
1657
da96fb5b
TI
1658 if (!spec->no_multi_io &&
1659 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
e22aab7d
TI
1660 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1661 spec->multi_ios = 1; /* give badness */
1662
a07a949b
TI
1663 /* re-count num_dacs and squash invalid entries */
1664 spec->multiout.num_dacs = 0;
1665 for (i = 0; i < cfg->line_outs; i++) {
1666 if (spec->private_dac_nids[i])
1667 spec->multiout.num_dacs++;
1668 else {
1669 memmove(spec->private_dac_nids + i,
1670 spec->private_dac_nids + i + 1,
1671 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1672 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1673 }
1674 }
1675
1676 spec->ext_channel_count = spec->min_channel_count =
c0f3b216 1677 spec->multiout.num_dacs * 2;
a07a949b 1678
352f7f91
TI
1679 if (spec->multi_ios == 2) {
1680 for (i = 0; i < 2; i++)
1681 spec->private_dac_nids[spec->multiout.num_dacs++] =
1682 spec->multi_io[i].dac;
352f7f91
TI
1683 } else if (spec->multi_ios) {
1684 spec->multi_ios = 0;
1685 badness += BAD_MULTI_IO;
1686 }
1687
55a63d4d
TI
1688 if (spec->indep_hp && !indep_hp_possible(codec))
1689 badness += BAD_NO_INDEP_HP;
1690
a07a949b
TI
1691 /* re-fill the shared DAC for speaker / headphone */
1692 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1693 refill_shared_dacs(codec, cfg->hp_outs,
1694 spec->multiout.hp_out_nid,
1695 spec->hp_paths);
1696 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1697 refill_shared_dacs(codec, cfg->speaker_outs,
1698 spec->multiout.extra_out_nid,
1699 spec->speaker_paths);
1700
352f7f91
TI
1701 return badness;
1702}
1703
1704#define DEBUG_BADNESS
1705
1706#ifdef DEBUG_BADNESS
d82353e5
JP
1707#define debug_badness(fmt, ...) \
1708 codec_dbg(codec, fmt, ##__VA_ARGS__)
352f7f91 1709#else
d82353e5
JP
1710#define debug_badness(fmt, ...) \
1711 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
352f7f91
TI
1712#endif
1713
a769409c
TI
1714#ifdef DEBUG_BADNESS
1715static inline void print_nid_path_idx(struct hda_codec *codec,
1716 const char *pfx, int idx)
1717{
1718 struct nid_path *path;
1719
1720 path = snd_hda_get_path_from_idx(codec, idx);
1721 if (path)
4e76a883 1722 print_nid_path(codec, pfx, path);
a769409c
TI
1723}
1724
1725static void debug_show_configs(struct hda_codec *codec,
1726 struct auto_pin_cfg *cfg)
352f7f91 1727{
a769409c 1728 struct hda_gen_spec *spec = codec->spec;
a769409c 1729 static const char * const lo_type[3] = { "LO", "SP", "HP" };
a769409c
TI
1730 int i;
1731
1732 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
352f7f91 1733 cfg->line_out_pins[0], cfg->line_out_pins[1],
708122e8 1734 cfg->line_out_pins[2], cfg->line_out_pins[3],
352f7f91
TI
1735 spec->multiout.dac_nids[0],
1736 spec->multiout.dac_nids[1],
1737 spec->multiout.dac_nids[2],
a769409c
TI
1738 spec->multiout.dac_nids[3],
1739 lo_type[cfg->line_out_type]);
1740 for (i = 0; i < cfg->line_outs; i++)
1741 print_nid_path_idx(codec, " out", spec->out_paths[i]);
352f7f91
TI
1742 if (spec->multi_ios > 0)
1743 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1744 spec->multi_ios,
1745 spec->multi_io[0].pin, spec->multi_io[1].pin,
1746 spec->multi_io[0].dac, spec->multi_io[1].dac);
a769409c
TI
1747 for (i = 0; i < spec->multi_ios; i++)
1748 print_nid_path_idx(codec, " mio",
1749 spec->out_paths[cfg->line_outs + i]);
1750 if (cfg->hp_outs)
1751 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
352f7f91 1752 cfg->hp_pins[0], cfg->hp_pins[1],
708122e8 1753 cfg->hp_pins[2], cfg->hp_pins[3],
352f7f91
TI
1754 spec->multiout.hp_out_nid[0],
1755 spec->multiout.hp_out_nid[1],
1756 spec->multiout.hp_out_nid[2],
1757 spec->multiout.hp_out_nid[3]);
a769409c
TI
1758 for (i = 0; i < cfg->hp_outs; i++)
1759 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1760 if (cfg->speaker_outs)
1761 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
352f7f91
TI
1762 cfg->speaker_pins[0], cfg->speaker_pins[1],
1763 cfg->speaker_pins[2], cfg->speaker_pins[3],
1764 spec->multiout.extra_out_nid[0],
1765 spec->multiout.extra_out_nid[1],
1766 spec->multiout.extra_out_nid[2],
1767 spec->multiout.extra_out_nid[3]);
a769409c
TI
1768 for (i = 0; i < cfg->speaker_outs; i++)
1769 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1770 for (i = 0; i < 3; i++)
1771 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
352f7f91 1772}
a769409c
TI
1773#else
1774#define debug_show_configs(codec, cfg) /* NOP */
1775#endif
352f7f91
TI
1776
1777/* find all available DACs of the codec */
1778static void fill_all_dac_nids(struct hda_codec *codec)
1779{
1780 struct hda_gen_spec *spec = codec->spec;
1781 int i;
1782 hda_nid_t nid = codec->start_nid;
1783
1784 spec->num_all_dacs = 0;
1785 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1786 for (i = 0; i < codec->num_nodes; i++, nid++) {
1787 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1788 continue;
1789 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
4e76a883 1790 codec_err(codec, "Too many DACs!\n");
352f7f91
TI
1791 break;
1792 }
1793 spec->all_dacs[spec->num_all_dacs++] = nid;
1794 }
1795}
1796
1797static int parse_output_paths(struct hda_codec *codec)
1798{
1799 struct hda_gen_spec *spec = codec->spec;
1800 struct auto_pin_cfg *cfg = &spec->autocfg;
1801 struct auto_pin_cfg *best_cfg;
9314a581 1802 unsigned int val;
352f7f91
TI
1803 int best_badness = INT_MAX;
1804 int badness;
1805 bool fill_hardwired = true, fill_mio_first = true;
1806 bool best_wired = true, best_mio = true;
1807 bool hp_spk_swapped = false;
1808
352f7f91
TI
1809 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1810 if (!best_cfg)
1811 return -ENOMEM;
1812 *best_cfg = *cfg;
1813
1814 for (;;) {
1815 badness = fill_and_eval_dacs(codec, fill_hardwired,
1816 fill_mio_first);
1817 if (badness < 0) {
1818 kfree(best_cfg);
1819 return badness;
1820 }
1821 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1822 cfg->line_out_type, fill_hardwired, fill_mio_first,
1823 badness);
a769409c 1824 debug_show_configs(codec, cfg);
352f7f91
TI
1825 if (badness < best_badness) {
1826 best_badness = badness;
1827 *best_cfg = *cfg;
1828 best_wired = fill_hardwired;
1829 best_mio = fill_mio_first;
1830 }
1831 if (!badness)
1832 break;
1833 fill_mio_first = !fill_mio_first;
1834 if (!fill_mio_first)
1835 continue;
1836 fill_hardwired = !fill_hardwired;
1837 if (!fill_hardwired)
1838 continue;
1839 if (hp_spk_swapped)
1840 break;
1841 hp_spk_swapped = true;
1842 if (cfg->speaker_outs > 0 &&
1843 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1844 cfg->hp_outs = cfg->line_outs;
1845 memcpy(cfg->hp_pins, cfg->line_out_pins,
1846 sizeof(cfg->hp_pins));
1847 cfg->line_outs = cfg->speaker_outs;
1848 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1849 sizeof(cfg->speaker_pins));
1850 cfg->speaker_outs = 0;
1851 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1852 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1853 fill_hardwired = true;
1854 continue;
1855 }
1856 if (cfg->hp_outs > 0 &&
1857 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1858 cfg->speaker_outs = cfg->line_outs;
1859 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1860 sizeof(cfg->speaker_pins));
1861 cfg->line_outs = cfg->hp_outs;
1862 memcpy(cfg->line_out_pins, cfg->hp_pins,
1863 sizeof(cfg->hp_pins));
1864 cfg->hp_outs = 0;
1865 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1866 cfg->line_out_type = AUTO_PIN_HP_OUT;
1867 fill_hardwired = true;
1868 continue;
1869 }
1870 break;
1871 }
1872
1873 if (badness) {
0c8c0f56 1874 debug_badness("==> restoring best_cfg\n");
352f7f91
TI
1875 *cfg = *best_cfg;
1876 fill_and_eval_dacs(codec, best_wired, best_mio);
1877 }
1878 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1879 cfg->line_out_type, best_wired, best_mio);
a769409c 1880 debug_show_configs(codec, cfg);
352f7f91
TI
1881
1882 if (cfg->line_out_pins[0]) {
1883 struct nid_path *path;
196c1766 1884 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
352f7f91
TI
1885 if (path)
1886 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
d89c6c0c 1887 if (spec->vmaster_nid) {
7a71bbf3
TI
1888 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1889 HDA_OUTPUT, spec->vmaster_tlv);
d89c6c0c
TI
1890 if (spec->dac_min_mute)
1891 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
1892 }
352f7f91
TI
1893 }
1894
9314a581
TI
1895 /* set initial pinctl targets */
1896 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1897 val = PIN_HP;
1898 else
1899 val = PIN_OUT;
1900 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1901 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1902 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1903 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1904 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1905 set_pin_targets(codec, cfg->speaker_outs,
1906 cfg->speaker_pins, val);
1907 }
1908
55a63d4d
TI
1909 /* clear indep_hp flag if not available */
1910 if (spec->indep_hp && !indep_hp_possible(codec))
1911 spec->indep_hp = 0;
1912
352f7f91
TI
1913 kfree(best_cfg);
1914 return 0;
1915}
1916
1917/* add playback controls from the parsed DAC table */
1918static int create_multi_out_ctls(struct hda_codec *codec,
1919 const struct auto_pin_cfg *cfg)
1920{
1921 struct hda_gen_spec *spec = codec->spec;
1922 int i, err, noutputs;
1923
1924 noutputs = cfg->line_outs;
1925 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1926 noutputs += spec->multi_ios;
1927
1928 for (i = 0; i < noutputs; i++) {
1929 const char *name;
1930 int index;
352f7f91
TI
1931 struct nid_path *path;
1932
196c1766 1933 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
352f7f91
TI
1934 if (!path)
1935 continue;
247d85ee
TI
1936
1937 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
352f7f91
TI
1938 if (!name || !strcmp(name, "CLFE")) {
1939 /* Center/LFE */
1940 err = add_vol_ctl(codec, "Center", 0, 1, path);
1941 if (err < 0)
1942 return err;
1943 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1944 if (err < 0)
1945 return err;
247d85ee
TI
1946 } else {
1947 err = add_stereo_vol(codec, name, index, path);
1948 if (err < 0)
1949 return err;
1950 }
1951
1952 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1953 if (!name || !strcmp(name, "CLFE")) {
352f7f91
TI
1954 err = add_sw_ctl(codec, "Center", 0, 1, path);
1955 if (err < 0)
1956 return err;
1957 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1958 if (err < 0)
1959 return err;
1960 } else {
352f7f91
TI
1961 err = add_stereo_sw(codec, name, index, path);
1962 if (err < 0)
1963 return err;
1964 }
1965 }
1966 return 0;
1967}
1968
c2c80383 1969static int create_extra_out(struct hda_codec *codec, int path_idx,
196c1766 1970 const char *pfx, int cidx)
352f7f91
TI
1971{
1972 struct nid_path *path;
1973 int err;
1974
196c1766 1975 path = snd_hda_get_path_from_idx(codec, path_idx);
352f7f91
TI
1976 if (!path)
1977 return 0;
c2c80383
TI
1978 err = add_stereo_vol(codec, pfx, cidx, path);
1979 if (err < 0)
1980 return err;
352f7f91
TI
1981 err = add_stereo_sw(codec, pfx, cidx, path);
1982 if (err < 0)
1983 return err;
1984 return 0;
1985}
1986
1987/* add playback controls for speaker and HP outputs */
1988static int create_extra_outs(struct hda_codec *codec, int num_pins,
196c1766 1989 const int *paths, const char *pfx)
352f7f91 1990{
c2c80383 1991 int i;
352f7f91
TI
1992
1993 for (i = 0; i < num_pins; i++) {
c2c80383 1994 const char *name;
975cc02a 1995 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
c2c80383
TI
1996 int err, idx = 0;
1997
1998 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1999 name = "Bass Speaker";
2000 else if (num_pins >= 3) {
2001 snprintf(tmp, sizeof(tmp), "%s %s",
352f7f91 2002 pfx, channel_name[i]);
c2c80383 2003 name = tmp;
352f7f91 2004 } else {
c2c80383
TI
2005 name = pfx;
2006 idx = i;
352f7f91 2007 }
c2c80383 2008 err = create_extra_out(codec, paths[i], name, idx);
352f7f91
TI
2009 if (err < 0)
2010 return err;
2011 }
2012 return 0;
2013}
2014
2015static int create_hp_out_ctls(struct hda_codec *codec)
2016{
2017 struct hda_gen_spec *spec = codec->spec;
2018 return create_extra_outs(codec, spec->autocfg.hp_outs,
196c1766 2019 spec->hp_paths,
352f7f91
TI
2020 "Headphone");
2021}
2022
2023static int create_speaker_out_ctls(struct hda_codec *codec)
2024{
2025 struct hda_gen_spec *spec = codec->spec;
2026 return create_extra_outs(codec, spec->autocfg.speaker_outs,
196c1766 2027 spec->speaker_paths,
352f7f91
TI
2028 "Speaker");
2029}
2030
38cf6f1a
TI
2031/*
2032 * independent HP controls
2033 */
2034
1a4f69d5
TI
2035static void call_hp_automute(struct hda_codec *codec,
2036 struct hda_jack_callback *jack);
38cf6f1a
TI
2037static int indep_hp_info(struct snd_kcontrol *kcontrol,
2038 struct snd_ctl_elem_info *uinfo)
2039{
2040 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2041}
2042
2043static int indep_hp_get(struct snd_kcontrol *kcontrol,
2044 struct snd_ctl_elem_value *ucontrol)
2045{
2046 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2047 struct hda_gen_spec *spec = codec->spec;
2048 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2049 return 0;
2050}
2051
a1e908ed
TI
2052static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2053 int nomix_path_idx, int mix_path_idx,
2054 int out_type);
2055
38cf6f1a
TI
2056static int indep_hp_put(struct snd_kcontrol *kcontrol,
2057 struct snd_ctl_elem_value *ucontrol)
2058{
2059 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2060 struct hda_gen_spec *spec = codec->spec;
2061 unsigned int select = ucontrol->value.enumerated.item[0];
2062 int ret = 0;
2063
2064 mutex_lock(&spec->pcm_mutex);
2065 if (spec->active_streams) {
2066 ret = -EBUSY;
2067 goto unlock;
2068 }
2069
2070 if (spec->indep_hp_enabled != select) {
a1e908ed
TI
2071 hda_nid_t *dacp;
2072 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2073 dacp = &spec->private_dac_nids[0];
2074 else
2075 dacp = &spec->multiout.hp_out_nid[0];
2076
2077 /* update HP aamix paths in case it conflicts with indep HP */
2078 if (spec->have_aamix_ctl) {
2079 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2080 update_aamix_paths(codec, spec->aamix_mode,
2081 spec->out_paths[0],
2082 spec->aamix_out_paths[0],
2083 spec->autocfg.line_out_type);
2084 else
2085 update_aamix_paths(codec, spec->aamix_mode,
2086 spec->hp_paths[0],
2087 spec->aamix_out_paths[1],
2088 AUTO_PIN_HP_OUT);
2089 }
2090
38cf6f1a
TI
2091 spec->indep_hp_enabled = select;
2092 if (spec->indep_hp_enabled)
a1e908ed 2093 *dacp = 0;
38cf6f1a 2094 else
a1e908ed 2095 *dacp = spec->alt_dac_nid;
92603c59 2096
963afde9 2097 call_hp_automute(codec, NULL);
38cf6f1a
TI
2098 ret = 1;
2099 }
2100 unlock:
2101 mutex_unlock(&spec->pcm_mutex);
2102 return ret;
2103}
2104
2105static const struct snd_kcontrol_new indep_hp_ctl = {
2106 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2107 .name = "Independent HP",
2108 .info = indep_hp_info,
2109 .get = indep_hp_get,
2110 .put = indep_hp_put,
2111};
2112
2113
2114static int create_indep_hp_ctls(struct hda_codec *codec)
2115{
2116 struct hda_gen_spec *spec = codec->spec;
a1e908ed 2117 hda_nid_t dac;
38cf6f1a
TI
2118
2119 if (!spec->indep_hp)
2120 return 0;
a1e908ed
TI
2121 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2122 dac = spec->multiout.dac_nids[0];
2123 else
2124 dac = spec->multiout.hp_out_nid[0];
2125 if (!dac) {
38cf6f1a
TI
2126 spec->indep_hp = 0;
2127 return 0;
2128 }
2129
2130 spec->indep_hp_enabled = false;
a1e908ed 2131 spec->alt_dac_nid = dac;
38cf6f1a
TI
2132 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2133 return -ENOMEM;
2134 return 0;
2135}
2136
352f7f91
TI
2137/*
2138 * channel mode enum control
2139 */
2140
2141static int ch_mode_info(struct snd_kcontrol *kcontrol,
2142 struct snd_ctl_elem_info *uinfo)
2143{
2144 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2145 struct hda_gen_spec *spec = codec->spec;
a07a949b 2146 int chs;
352f7f91
TI
2147
2148 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2149 uinfo->count = 1;
2150 uinfo->value.enumerated.items = spec->multi_ios + 1;
2151 if (uinfo->value.enumerated.item > spec->multi_ios)
2152 uinfo->value.enumerated.item = spec->multi_ios;
a07a949b
TI
2153 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2154 sprintf(uinfo->value.enumerated.name, "%dch", chs);
352f7f91
TI
2155 return 0;
2156}
2157
2158static int ch_mode_get(struct snd_kcontrol *kcontrol,
2159 struct snd_ctl_elem_value *ucontrol)
2160{
2161 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2162 struct hda_gen_spec *spec = codec->spec;
a07a949b
TI
2163 ucontrol->value.enumerated.item[0] =
2164 (spec->ext_channel_count - spec->min_channel_count) / 2;
352f7f91
TI
2165 return 0;
2166}
2167
196c1766
TI
2168static inline struct nid_path *
2169get_multiio_path(struct hda_codec *codec, int idx)
2170{
2171 struct hda_gen_spec *spec = codec->spec;
2172 return snd_hda_get_path_from_idx(codec,
2173 spec->out_paths[spec->autocfg.line_outs + idx]);
2174}
2175
a5cc2509
TI
2176static void update_automute_all(struct hda_codec *codec);
2177
65033cc8
TI
2178/* Default value to be passed as aamix argument for snd_hda_activate_path();
2179 * used for output paths
2180 */
2181static bool aamix_default(struct hda_gen_spec *spec)
2182{
2183 return !spec->have_aamix_ctl || spec->aamix_mode;
2184}
2185
352f7f91
TI
2186static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2187{
2188 struct hda_gen_spec *spec = codec->spec;
2189 hda_nid_t nid = spec->multi_io[idx].pin;
2190 struct nid_path *path;
2191
196c1766 2192 path = get_multiio_path(codec, idx);
352f7f91
TI
2193 if (!path)
2194 return -EINVAL;
2195
2196 if (path->active == output)
2197 return 0;
2198
2199 if (output) {
2c12c30d 2200 set_pin_target(codec, nid, PIN_OUT, true);
65033cc8 2201 snd_hda_activate_path(codec, path, true, aamix_default(spec));
d5a9f1bb 2202 set_pin_eapd(codec, nid, true);
352f7f91 2203 } else {
d5a9f1bb 2204 set_pin_eapd(codec, nid, false);
65033cc8 2205 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2c12c30d 2206 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
55196fff 2207 path_power_down_sync(codec, path);
352f7f91 2208 }
a365fed9
TI
2209
2210 /* update jack retasking in case it modifies any of them */
a5cc2509 2211 update_automute_all(codec);
a365fed9 2212
352f7f91
TI
2213 return 0;
2214}
2215
2216static int ch_mode_put(struct snd_kcontrol *kcontrol,
2217 struct snd_ctl_elem_value *ucontrol)
2218{
2219 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2220 struct hda_gen_spec *spec = codec->spec;
2221 int i, ch;
2222
2223 ch = ucontrol->value.enumerated.item[0];
2224 if (ch < 0 || ch > spec->multi_ios)
2225 return -EINVAL;
a07a949b 2226 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
352f7f91 2227 return 0;
a07a949b 2228 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
352f7f91
TI
2229 for (i = 0; i < spec->multi_ios; i++)
2230 set_multi_io(codec, i, i < ch);
2231 spec->multiout.max_channels = max(spec->ext_channel_count,
2232 spec->const_channel_count);
2233 if (spec->need_dac_fix)
2234 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2235 return 1;
2236}
2237
2238static const struct snd_kcontrol_new channel_mode_enum = {
2239 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2240 .name = "Channel Mode",
2241 .info = ch_mode_info,
2242 .get = ch_mode_get,
2243 .put = ch_mode_put,
2244};
2245
2246static int create_multi_channel_mode(struct hda_codec *codec)
2247{
2248 struct hda_gen_spec *spec = codec->spec;
2249
2250 if (spec->multi_ios > 0) {
12c93df6 2251 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
352f7f91
TI
2252 return -ENOMEM;
2253 }
2254 return 0;
2255}
2256
c30aa7b2
TI
2257/*
2258 * aamix loopback enable/disable switch
2259 */
2260
2261#define loopback_mixing_info indep_hp_info
2262
2263static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2264 struct snd_ctl_elem_value *ucontrol)
2265{
2266 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2267 struct hda_gen_spec *spec = codec->spec;
2268 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2269 return 0;
2270}
2271
2272static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
a1e908ed
TI
2273 int nomix_path_idx, int mix_path_idx,
2274 int out_type)
c30aa7b2 2275{
a1e908ed 2276 struct hda_gen_spec *spec = codec->spec;
c30aa7b2
TI
2277 struct nid_path *nomix_path, *mix_path;
2278
2279 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2280 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2281 if (!nomix_path || !mix_path)
2282 return;
a1e908ed
TI
2283
2284 /* if HP aamix path is driven from a different DAC and the
2285 * independent HP mode is ON, can't turn on aamix path
2286 */
2287 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2288 mix_path->path[0] != spec->alt_dac_nid)
2289 do_mix = false;
2290
c30aa7b2
TI
2291 if (do_mix) {
2292 snd_hda_activate_path(codec, nomix_path, false, true);
2293 snd_hda_activate_path(codec, mix_path, true, true);
55196fff 2294 path_power_down_sync(codec, nomix_path);
c30aa7b2 2295 } else {
65033cc8
TI
2296 snd_hda_activate_path(codec, mix_path, false, false);
2297 snd_hda_activate_path(codec, nomix_path, true, false);
55196fff 2298 path_power_down_sync(codec, mix_path);
c30aa7b2
TI
2299 }
2300}
2301
2302static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2303 struct snd_ctl_elem_value *ucontrol)
2304{
2305 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2306 struct hda_gen_spec *spec = codec->spec;
2307 unsigned int val = ucontrol->value.enumerated.item[0];
2308
2309 if (val == spec->aamix_mode)
2310 return 0;
2311 spec->aamix_mode = val;
2312 update_aamix_paths(codec, val, spec->out_paths[0],
a1e908ed
TI
2313 spec->aamix_out_paths[0],
2314 spec->autocfg.line_out_type);
c30aa7b2 2315 update_aamix_paths(codec, val, spec->hp_paths[0],
a1e908ed
TI
2316 spec->aamix_out_paths[1],
2317 AUTO_PIN_HP_OUT);
c30aa7b2 2318 update_aamix_paths(codec, val, spec->speaker_paths[0],
a1e908ed
TI
2319 spec->aamix_out_paths[2],
2320 AUTO_PIN_SPEAKER_OUT);
c30aa7b2
TI
2321 return 1;
2322}
2323
2324static const struct snd_kcontrol_new loopback_mixing_enum = {
2325 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2326 .name = "Loopback Mixing",
2327 .info = loopback_mixing_info,
2328 .get = loopback_mixing_get,
2329 .put = loopback_mixing_put,
2330};
2331
2332static int create_loopback_mixing_ctl(struct hda_codec *codec)
2333{
2334 struct hda_gen_spec *spec = codec->spec;
2335
2336 if (!spec->mixer_nid)
2337 return 0;
2338 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2339 spec->aamix_out_paths[2]))
2340 return 0;
2341 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2342 return -ENOMEM;
a1e908ed 2343 spec->have_aamix_ctl = 1;
c30aa7b2
TI
2344 return 0;
2345}
2346
352f7f91
TI
2347/*
2348 * shared headphone/mic handling
2349 */
2350
2351static void call_update_outputs(struct hda_codec *codec);
2352
2353/* for shared I/O, change the pin-control accordingly */
967303da 2354static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
352f7f91
TI
2355{
2356 struct hda_gen_spec *spec = codec->spec;
967303da 2357 bool as_mic;
352f7f91 2358 unsigned int val;
967303da 2359 hda_nid_t pin;
352f7f91 2360
967303da
TI
2361 pin = spec->hp_mic_pin;
2362 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
352f7f91 2363
967303da
TI
2364 if (!force) {
2365 val = snd_hda_codec_get_pin_target(codec, pin);
2366 if (as_mic) {
2367 if (val & PIN_IN)
2368 return;
2369 } else {
2370 if (val & PIN_OUT)
2371 return;
2372 }
2373 }
2374
2375 val = snd_hda_get_default_vref(codec, pin);
2376 /* if the HP pin doesn't support VREF and the codec driver gives an
2377 * alternative pin, set up the VREF on that pin instead
2378 */
352f7f91
TI
2379 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2380 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2381 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2382 if (vref_val != AC_PINCTL_VREF_HIZ)
7594aa33 2383 snd_hda_set_pin_ctl_cache(codec, vref_pin,
967303da 2384 PIN_IN | (as_mic ? vref_val : 0));
352f7f91
TI
2385 }
2386
8ba955ce
TI
2387 if (!spec->hp_mic_jack_modes) {
2388 if (as_mic)
2389 val |= PIN_IN;
2390 else
2391 val = PIN_HP;
2392 set_pin_target(codec, pin, val, true);
963afde9 2393 call_hp_automute(codec, NULL);
8ba955ce 2394 }
352f7f91
TI
2395}
2396
2397/* create a shared input with the headphone out */
967303da 2398static int create_hp_mic(struct hda_codec *codec)
352f7f91
TI
2399{
2400 struct hda_gen_spec *spec = codec->spec;
2401 struct auto_pin_cfg *cfg = &spec->autocfg;
2402 unsigned int defcfg;
2403 hda_nid_t nid;
2404
967303da
TI
2405 if (!spec->hp_mic) {
2406 if (spec->suppress_hp_mic_detect)
2407 return 0;
2408 /* automatic detection: only if no input or a single internal
2409 * input pin is found, try to detect the shared hp/mic
2410 */
2411 if (cfg->num_inputs > 1)
2412 return 0;
2413 else if (cfg->num_inputs == 1) {
2414 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2415 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2416 return 0;
2417 }
2418 }
2419
2420 spec->hp_mic = 0; /* clear once */
2421 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
352f7f91
TI
2422 return 0;
2423
967303da
TI
2424 nid = 0;
2425 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2426 nid = cfg->line_out_pins[0];
2427 else if (cfg->hp_outs > 0)
2428 nid = cfg->hp_pins[0];
2429 if (!nid)
2430 return 0;
352f7f91
TI
2431
2432 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2433 return 0; /* no input */
2434
967303da
TI
2435 cfg->inputs[cfg->num_inputs].pin = nid;
2436 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
cb420b11 2437 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
967303da
TI
2438 cfg->num_inputs++;
2439 spec->hp_mic = 1;
2440 spec->hp_mic_pin = nid;
2441 /* we can't handle auto-mic together with HP-mic */
2442 spec->suppress_auto_mic = 1;
4e76a883 2443 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
352f7f91
TI
2444 return 0;
2445}
2446
978e77e7
TI
2447/*
2448 * output jack mode
2449 */
5f171baa
TI
2450
2451static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2452
2453static const char * const out_jack_texts[] = {
2454 "Line Out", "Headphone Out",
2455};
2456
978e77e7
TI
2457static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2458 struct snd_ctl_elem_info *uinfo)
2459{
5f171baa 2460 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
978e77e7
TI
2461}
2462
2463static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2464 struct snd_ctl_elem_value *ucontrol)
2465{
2466 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2467 hda_nid_t nid = kcontrol->private_value;
2468 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2469 ucontrol->value.enumerated.item[0] = 1;
2470 else
2471 ucontrol->value.enumerated.item[0] = 0;
2472 return 0;
2473}
2474
2475static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2476 struct snd_ctl_elem_value *ucontrol)
2477{
2478 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2479 hda_nid_t nid = kcontrol->private_value;
2480 unsigned int val;
2481
2482 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2483 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2484 return 0;
2485 snd_hda_set_pin_ctl_cache(codec, nid, val);
2486 return 1;
2487}
2488
2489static const struct snd_kcontrol_new out_jack_mode_enum = {
2490 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2491 .info = out_jack_mode_info,
2492 .get = out_jack_mode_get,
2493 .put = out_jack_mode_put,
2494};
2495
2496static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2497{
2498 struct hda_gen_spec *spec = codec->spec;
2499 int i;
2500
2501 for (i = 0; i < spec->kctls.used; i++) {
2502 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2503 if (!strcmp(kctl->name, name) && kctl->index == idx)
2504 return true;
2505 }
2506 return false;
2507}
2508
2509static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2510 char *name, size_t name_len)
2511{
2512 struct hda_gen_spec *spec = codec->spec;
2513 int idx = 0;
2514
2515 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2516 strlcat(name, " Jack Mode", name_len);
2517
2518 for (; find_kctl_name(codec, name, idx); idx++)
2519 ;
2520}
2521
5f171baa
TI
2522static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2523{
2524 struct hda_gen_spec *spec = codec->spec;
f811c3cf 2525 if (spec->add_jack_modes) {
5f171baa
TI
2526 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2527 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2528 return 2;
2529 }
2530 return 1;
2531}
2532
978e77e7
TI
2533static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2534 hda_nid_t *pins)
2535{
2536 struct hda_gen_spec *spec = codec->spec;
2537 int i;
2538
2539 for (i = 0; i < num_pins; i++) {
2540 hda_nid_t pin = pins[i];
ced4cefc 2541 if (pin == spec->hp_mic_pin)
5f171baa 2542 continue;
5f171baa 2543 if (get_out_jack_num_items(codec, pin) > 1) {
978e77e7 2544 struct snd_kcontrol_new *knew;
975cc02a 2545 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
978e77e7
TI
2546 get_jack_mode_name(codec, pin, name, sizeof(name));
2547 knew = snd_hda_gen_add_kctl(spec, name,
2548 &out_jack_mode_enum);
2549 if (!knew)
2550 return -ENOMEM;
2551 knew->private_value = pin;
2552 }
2553 }
2554
2555 return 0;
2556}
2557
29476558
TI
2558/*
2559 * input jack mode
2560 */
2561
2562/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2563#define NUM_VREFS 6
2564
2565static const char * const vref_texts[NUM_VREFS] = {
2566 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2567 "", "Mic 80pc Bias", "Mic 100pc Bias"
2568};
2569
2570static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2571{
2572 unsigned int pincap;
2573
2574 pincap = snd_hda_query_pin_caps(codec, pin);
2575 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2576 /* filter out unusual vrefs */
2577 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2578 return pincap;
2579}
2580
2581/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2582static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2583{
2584 unsigned int i, n = 0;
2585
2586 for (i = 0; i < NUM_VREFS; i++) {
2587 if (vref_caps & (1 << i)) {
2588 if (n == item_idx)
2589 return i;
2590 n++;
2591 }
2592 }
2593 return 0;
2594}
2595
2596/* convert back from the vref ctl index to the enum item index */
2597static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2598{
2599 unsigned int i, n = 0;
2600
2601 for (i = 0; i < NUM_VREFS; i++) {
2602 if (i == idx)
2603 return n;
2604 if (vref_caps & (1 << i))
2605 n++;
2606 }
2607 return 0;
2608}
2609
2610static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2611 struct snd_ctl_elem_info *uinfo)
2612{
2613 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2614 hda_nid_t nid = kcontrol->private_value;
2615 unsigned int vref_caps = get_vref_caps(codec, nid);
2616
2617 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2618 vref_texts);
2619 /* set the right text */
2620 strcpy(uinfo->value.enumerated.name,
2621 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2622 return 0;
2623}
2624
2625static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2626 struct snd_ctl_elem_value *ucontrol)
2627{
2628 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2629 hda_nid_t nid = kcontrol->private_value;
2630 unsigned int vref_caps = get_vref_caps(codec, nid);
2631 unsigned int idx;
2632
2633 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2634 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2635 return 0;
2636}
2637
2638static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2639 struct snd_ctl_elem_value *ucontrol)
2640{
2641 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2642 hda_nid_t nid = kcontrol->private_value;
2643 unsigned int vref_caps = get_vref_caps(codec, nid);
2644 unsigned int val, idx;
2645
2646 val = snd_hda_codec_get_pin_target(codec, nid);
2647 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2648 if (idx == ucontrol->value.enumerated.item[0])
2649 return 0;
2650
2651 val &= ~AC_PINCTL_VREFEN;
2652 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2653 snd_hda_set_pin_ctl_cache(codec, nid, val);
2654 return 1;
2655}
2656
2657static const struct snd_kcontrol_new in_jack_mode_enum = {
2658 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2659 .info = in_jack_mode_info,
2660 .get = in_jack_mode_get,
2661 .put = in_jack_mode_put,
2662};
2663
5f171baa
TI
2664static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2665{
2666 struct hda_gen_spec *spec = codec->spec;
2667 int nitems = 0;
f811c3cf 2668 if (spec->add_jack_modes)
5f171baa
TI
2669 nitems = hweight32(get_vref_caps(codec, pin));
2670 return nitems ? nitems : 1;
2671}
2672
29476558
TI
2673static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2674{
2675 struct hda_gen_spec *spec = codec->spec;
29476558 2676 struct snd_kcontrol_new *knew;
975cc02a 2677 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5f171baa
TI
2678 unsigned int defcfg;
2679
f811c3cf
TI
2680 if (pin == spec->hp_mic_pin)
2681 return 0; /* already done in create_out_jack_mode() */
29476558
TI
2682
2683 /* no jack mode for fixed pins */
2684 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2685 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2686 return 0;
2687
2688 /* no multiple vref caps? */
5f171baa 2689 if (get_in_jack_num_items(codec, pin) <= 1)
29476558
TI
2690 return 0;
2691
2692 get_jack_mode_name(codec, pin, name, sizeof(name));
2693 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2694 if (!knew)
2695 return -ENOMEM;
2696 knew->private_value = pin;
2697 return 0;
2698}
2699
5f171baa
TI
2700/*
2701 * HP/mic shared jack mode
2702 */
2703static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2704 struct snd_ctl_elem_info *uinfo)
2705{
2706 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2707 hda_nid_t nid = kcontrol->private_value;
2708 int out_jacks = get_out_jack_num_items(codec, nid);
2709 int in_jacks = get_in_jack_num_items(codec, nid);
2710 const char *text = NULL;
2711 int idx;
2712
2713 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2714 uinfo->count = 1;
2715 uinfo->value.enumerated.items = out_jacks + in_jacks;
2716 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2717 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2718 idx = uinfo->value.enumerated.item;
2719 if (idx < out_jacks) {
2720 if (out_jacks > 1)
2721 text = out_jack_texts[idx];
2722 else
2723 text = "Headphone Out";
2724 } else {
2725 idx -= out_jacks;
2726 if (in_jacks > 1) {
2727 unsigned int vref_caps = get_vref_caps(codec, nid);
2728 text = vref_texts[get_vref_idx(vref_caps, idx)];
2729 } else
2730 text = "Mic In";
2731 }
2732
2733 strcpy(uinfo->value.enumerated.name, text);
2734 return 0;
2735}
2736
2737static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2738{
2739 int out_jacks = get_out_jack_num_items(codec, nid);
2740 int in_jacks = get_in_jack_num_items(codec, nid);
2741 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2742 int idx = 0;
2743
2744 if (val & PIN_OUT) {
2745 if (out_jacks > 1 && val == PIN_HP)
2746 idx = 1;
2747 } else if (val & PIN_IN) {
2748 idx = out_jacks;
2749 if (in_jacks > 1) {
2750 unsigned int vref_caps = get_vref_caps(codec, nid);
2751 val &= AC_PINCTL_VREFEN;
2752 idx += cvt_from_vref_idx(vref_caps, val);
2753 }
2754 }
2755 return idx;
2756}
2757
2758static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2759 struct snd_ctl_elem_value *ucontrol)
2760{
2761 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2762 hda_nid_t nid = kcontrol->private_value;
2763 ucontrol->value.enumerated.item[0] =
2764 get_cur_hp_mic_jack_mode(codec, nid);
2765 return 0;
2766}
2767
2768static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2769 struct snd_ctl_elem_value *ucontrol)
2770{
2771 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2772 hda_nid_t nid = kcontrol->private_value;
2773 int out_jacks = get_out_jack_num_items(codec, nid);
2774 int in_jacks = get_in_jack_num_items(codec, nid);
2775 unsigned int val, oldval, idx;
2776
2777 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2778 idx = ucontrol->value.enumerated.item[0];
2779 if (oldval == idx)
2780 return 0;
2781
2782 if (idx < out_jacks) {
2783 if (out_jacks > 1)
2784 val = idx ? PIN_HP : PIN_OUT;
2785 else
2786 val = PIN_HP;
2787 } else {
2788 idx -= out_jacks;
2789 if (in_jacks > 1) {
2790 unsigned int vref_caps = get_vref_caps(codec, nid);
2791 val = snd_hda_codec_get_pin_target(codec, nid);
3f550e32
TI
2792 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2793 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
5f171baa 2794 } else
16c0cefe 2795 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
5f171baa
TI
2796 }
2797 snd_hda_set_pin_ctl_cache(codec, nid, val);
963afde9 2798 call_hp_automute(codec, NULL);
8ba955ce 2799
5f171baa
TI
2800 return 1;
2801}
2802
2803static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2804 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2805 .info = hp_mic_jack_mode_info,
2806 .get = hp_mic_jack_mode_get,
2807 .put = hp_mic_jack_mode_put,
2808};
2809
2810static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2811{
2812 struct hda_gen_spec *spec = codec->spec;
2813 struct snd_kcontrol_new *knew;
2814
5f171baa
TI
2815 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2816 &hp_mic_jack_mode_enum);
2817 if (!knew)
2818 return -ENOMEM;
2819 knew->private_value = pin;
8ba955ce 2820 spec->hp_mic_jack_modes = 1;
5f171baa
TI
2821 return 0;
2822}
352f7f91
TI
2823
2824/*
2825 * Parse input paths
2826 */
2827
352f7f91 2828/* add the powersave loopback-list entry */
0186f4f4 2829static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
352f7f91
TI
2830{
2831 struct hda_amp_list *list;
2832
0186f4f4
TI
2833 list = snd_array_new(&spec->loopback_list);
2834 if (!list)
2835 return -ENOMEM;
352f7f91
TI
2836 list->nid = mix;
2837 list->dir = HDA_INPUT;
2838 list->idx = idx;
0186f4f4
TI
2839 spec->loopback.amplist = spec->loopback_list.list;
2840 return 0;
352f7f91 2841}
352f7f91 2842
2ded3e5b
TI
2843/* return true if either a volume or a mute amp is found for the given
2844 * aamix path; the amp has to be either in the mixer node or its direct leaf
2845 */
2846static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2847 hda_nid_t pin, unsigned int *mix_val,
2848 unsigned int *mute_val)
2849{
2850 int idx, num_conns;
2851 const hda_nid_t *list;
2852 hda_nid_t nid;
2853
2854 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2855 if (idx < 0)
2856 return false;
2857
2858 *mix_val = *mute_val = 0;
2859 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2860 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2861 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2862 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2863 if (*mix_val && *mute_val)
2864 return true;
2865
2866 /* check leaf node */
2867 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2868 if (num_conns < idx)
2869 return false;
2870 nid = list[idx];
43a8e50a
TI
2871 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2872 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
2ded3e5b 2873 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
43a8e50a
TI
2874 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2875 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
2ded3e5b
TI
2876 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2877
2878 return *mix_val || *mute_val;
2879}
2880
352f7f91 2881/* create input playback/capture controls for the given pin */
196c1766
TI
2882static int new_analog_input(struct hda_codec *codec, int input_idx,
2883 hda_nid_t pin, const char *ctlname, int ctlidx,
352f7f91
TI
2884 hda_nid_t mix_nid)
2885{
2886 struct hda_gen_spec *spec = codec->spec;
2887 struct nid_path *path;
2ded3e5b 2888 unsigned int mix_val, mute_val;
352f7f91
TI
2889 int err, idx;
2890
2ded3e5b
TI
2891 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2892 return 0;
352f7f91 2893
3ca529d3 2894 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
352f7f91
TI
2895 if (!path)
2896 return -EINVAL;
4e76a883 2897 print_nid_path(codec, "loopback", path);
196c1766 2898 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
352f7f91
TI
2899
2900 idx = path->idx[path->depth - 1];
2ded3e5b
TI
2901 if (mix_val) {
2902 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
352f7f91
TI
2903 if (err < 0)
2904 return err;
2ded3e5b 2905 path->ctls[NID_PATH_VOL_CTL] = mix_val;
352f7f91
TI
2906 }
2907
2ded3e5b
TI
2908 if (mute_val) {
2909 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
352f7f91
TI
2910 if (err < 0)
2911 return err;
2ded3e5b 2912 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
352f7f91
TI
2913 }
2914
2915 path->active = true;
0186f4f4
TI
2916 err = add_loopback_list(spec, mix_nid, idx);
2917 if (err < 0)
2918 return err;
e4a395e7
TI
2919
2920 if (spec->mixer_nid != spec->mixer_merge_nid &&
2921 !spec->loopback_merge_path) {
2922 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2923 spec->mixer_merge_nid, 0);
2924 if (path) {
4e76a883 2925 print_nid_path(codec, "loopback-merge", path);
e4a395e7
TI
2926 path->active = true;
2927 spec->loopback_merge_path =
2928 snd_hda_get_path_idx(codec, path);
2929 }
2930 }
2931
352f7f91
TI
2932 return 0;
2933}
2934
2935static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2936{
2937 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2938 return (pincap & AC_PINCAP_IN) != 0;
2939}
2940
2941/* Parse the codec tree and retrieve ADCs */
2942static int fill_adc_nids(struct hda_codec *codec)
2943{
2944 struct hda_gen_spec *spec = codec->spec;
2945 hda_nid_t nid;
2946 hda_nid_t *adc_nids = spec->adc_nids;
2947 int max_nums = ARRAY_SIZE(spec->adc_nids);
2948 int i, nums = 0;
2949
2950 nid = codec->start_nid;
2951 for (i = 0; i < codec->num_nodes; i++, nid++) {
2952 unsigned int caps = get_wcaps(codec, nid);
2953 int type = get_wcaps_type(caps);
2954
2955 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2956 continue;
2957 adc_nids[nums] = nid;
2958 if (++nums >= max_nums)
2959 break;
2960 }
2961 spec->num_adc_nids = nums;
0ffd534e
TI
2962
2963 /* copy the detected ADCs to all_adcs[] */
2964 spec->num_all_adcs = nums;
2965 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2966
352f7f91
TI
2967 return nums;
2968}
2969
2970/* filter out invalid adc_nids that don't give all active input pins;
2971 * if needed, check whether dynamic ADC-switching is available
2972 */
2973static int check_dyn_adc_switch(struct hda_codec *codec)
2974{
2975 struct hda_gen_spec *spec = codec->spec;
2976 struct hda_input_mux *imux = &spec->input_mux;
3a65bcdc 2977 unsigned int ok_bits;
352f7f91 2978 int i, n, nums;
352f7f91 2979
352f7f91 2980 nums = 0;
3a65bcdc 2981 ok_bits = 0;
352f7f91 2982 for (n = 0; n < spec->num_adc_nids; n++) {
352f7f91 2983 for (i = 0; i < imux->num_items; i++) {
3a65bcdc 2984 if (!spec->input_paths[i][n])
352f7f91
TI
2985 break;
2986 }
3a65bcdc
TI
2987 if (i >= imux->num_items) {
2988 ok_bits |= (1 << n);
2989 nums++;
2990 }
352f7f91
TI
2991 }
2992
3a65bcdc 2993 if (!ok_bits) {
352f7f91
TI
2994 /* check whether ADC-switch is possible */
2995 for (i = 0; i < imux->num_items; i++) {
352f7f91 2996 for (n = 0; n < spec->num_adc_nids; n++) {
3a65bcdc 2997 if (spec->input_paths[i][n]) {
352f7f91
TI
2998 spec->dyn_adc_idx[i] = n;
2999 break;
3000 }
3001 }
3002 }
3003
4e76a883 3004 codec_dbg(codec, "enabling ADC switching\n");
352f7f91
TI
3005 spec->dyn_adc_switch = 1;
3006 } else if (nums != spec->num_adc_nids) {
3a65bcdc
TI
3007 /* shrink the invalid adcs and input paths */
3008 nums = 0;
3009 for (n = 0; n < spec->num_adc_nids; n++) {
3010 if (!(ok_bits & (1 << n)))
3011 continue;
3012 if (n != nums) {
3013 spec->adc_nids[nums] = spec->adc_nids[n];
980428ce
TI
3014 for (i = 0; i < imux->num_items; i++) {
3015 invalidate_nid_path(codec,
3016 spec->input_paths[i][nums]);
3a65bcdc
TI
3017 spec->input_paths[i][nums] =
3018 spec->input_paths[i][n];
980428ce 3019 }
3a65bcdc
TI
3020 }
3021 nums++;
3022 }
352f7f91
TI
3023 spec->num_adc_nids = nums;
3024 }
3025
967303da
TI
3026 if (imux->num_items == 1 ||
3027 (imux->num_items == 2 && spec->hp_mic)) {
4e76a883 3028 codec_dbg(codec, "reducing to a single ADC\n");
352f7f91
TI
3029 spec->num_adc_nids = 1; /* reduce to a single ADC */
3030 }
3031
3032 /* single index for individual volumes ctls */
3033 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3034 spec->num_adc_nids = 1;
3035
3036 return 0;
3037}
3038
f3fc0b0b
TI
3039/* parse capture source paths from the given pin and create imux items */
3040static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
9dba205b
TI
3041 int cfg_idx, int num_adcs,
3042 const char *label, int anchor)
f3fc0b0b
TI
3043{
3044 struct hda_gen_spec *spec = codec->spec;
3045 struct hda_input_mux *imux = &spec->input_mux;
3046 int imux_idx = imux->num_items;
3047 bool imux_added = false;
3048 int c;
3049
3050 for (c = 0; c < num_adcs; c++) {
3051 struct nid_path *path;
3052 hda_nid_t adc = spec->adc_nids[c];
3053
3054 if (!is_reachable_path(codec, pin, adc))
3055 continue;
3056 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3057 if (!path)
3058 continue;
4e76a883 3059 print_nid_path(codec, "input", path);
f3fc0b0b
TI
3060 spec->input_paths[imux_idx][c] =
3061 snd_hda_get_path_idx(codec, path);
3062
3063 if (!imux_added) {
967303da
TI
3064 if (spec->hp_mic_pin == pin)
3065 spec->hp_mic_mux_idx = imux->num_items;
f3fc0b0b 3066 spec->imux_pins[imux->num_items] = pin;
6194b99d 3067 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
f3fc0b0b 3068 imux_added = true;
f1e762dd
TI
3069 if (spec->dyn_adc_switch)
3070 spec->dyn_adc_idx[imux_idx] = c;
f3fc0b0b
TI
3071 }
3072 }
3073
3074 return 0;
3075}
3076
352f7f91
TI
3077/*
3078 * create playback/capture controls for input pins
3079 */
9dba205b 3080
c970042c
TI
3081/* fill the label for each input at first */
3082static int fill_input_pin_labels(struct hda_codec *codec)
3083{
3084 struct hda_gen_spec *spec = codec->spec;
3085 const struct auto_pin_cfg *cfg = &spec->autocfg;
3086 int i;
3087
3088 for (i = 0; i < cfg->num_inputs; i++) {
3089 hda_nid_t pin = cfg->inputs[i].pin;
3090 const char *label;
3091 int j, idx;
3092
3093 if (!is_input_pin(codec, pin))
3094 continue;
3095
3096 label = hda_get_autocfg_input_label(codec, cfg, i);
3097 idx = 0;
8e8db7f1 3098 for (j = i - 1; j >= 0; j--) {
c970042c
TI
3099 if (spec->input_labels[j] &&
3100 !strcmp(spec->input_labels[j], label)) {
3101 idx = spec->input_label_idxs[j] + 1;
3102 break;
3103 }
3104 }
3105
3106 spec->input_labels[i] = label;
3107 spec->input_label_idxs[i] = idx;
3108 }
3109
3110 return 0;
3111}
3112
9dba205b
TI
3113#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3114
352f7f91
TI
3115static int create_input_ctls(struct hda_codec *codec)
3116{
3117 struct hda_gen_spec *spec = codec->spec;
3118 const struct auto_pin_cfg *cfg = &spec->autocfg;
3119 hda_nid_t mixer = spec->mixer_nid;
352f7f91 3120 int num_adcs;
c970042c 3121 int i, err;
2c12c30d 3122 unsigned int val;
352f7f91
TI
3123
3124 num_adcs = fill_adc_nids(codec);
3125 if (num_adcs < 0)
3126 return 0;
3127
c970042c
TI
3128 err = fill_input_pin_labels(codec);
3129 if (err < 0)
3130 return err;
3131
352f7f91
TI
3132 for (i = 0; i < cfg->num_inputs; i++) {
3133 hda_nid_t pin;
352f7f91
TI
3134
3135 pin = cfg->inputs[i].pin;
3136 if (!is_input_pin(codec, pin))
3137 continue;
3138
2c12c30d
TI
3139 val = PIN_IN;
3140 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3141 val |= snd_hda_get_default_vref(codec, pin);
93c9d8ae
TI
3142 if (pin != spec->hp_mic_pin)
3143 set_pin_target(codec, pin, val, false);
2c12c30d 3144
352f7f91
TI
3145 if (mixer) {
3146 if (is_reachable_path(codec, pin, mixer)) {
196c1766 3147 err = new_analog_input(codec, i, pin,
c970042c
TI
3148 spec->input_labels[i],
3149 spec->input_label_idxs[i],
3150 mixer);
352f7f91
TI
3151 if (err < 0)
3152 return err;
3153 }
3154 }
3155
c970042c
TI
3156 err = parse_capture_source(codec, pin, i, num_adcs,
3157 spec->input_labels[i], -mixer);
f3fc0b0b
TI
3158 if (err < 0)
3159 return err;
29476558 3160
f811c3cf 3161 if (spec->add_jack_modes) {
29476558
TI
3162 err = create_in_jack_mode(codec, pin);
3163 if (err < 0)
3164 return err;
3165 }
f3fc0b0b 3166 }
352f7f91 3167
f1e762dd
TI
3168 /* add stereo mix when explicitly enabled via hint */
3169 if (mixer && spec->add_stereo_mix_input &&
3170 snd_hda_get_bool_hint(codec, "add_stereo_mix_input") > 0) {
9dba205b 3171 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
f3fc0b0b
TI
3172 "Stereo Mix", 0);
3173 if (err < 0)
3174 return err;
352f7f91
TI
3175 }
3176
3177 return 0;
3178}
3179
3180
3181/*
3182 * input source mux
3183 */
3184
c697b716
TI
3185/* get the input path specified by the given adc and imux indices */
3186static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
352f7f91
TI
3187{
3188 struct hda_gen_spec *spec = codec->spec;
b56fa1ed
DH
3189 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3190 snd_BUG();
3191 return NULL;
3192 }
352f7f91
TI
3193 if (spec->dyn_adc_switch)
3194 adc_idx = spec->dyn_adc_idx[imux_idx];
d3d982f7 3195 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
b56fa1ed
DH
3196 snd_BUG();
3197 return NULL;
3198 }
c697b716 3199 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
352f7f91
TI
3200}
3201
3202static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3203 unsigned int idx);
3204
3205static int mux_enum_info(struct snd_kcontrol *kcontrol,
3206 struct snd_ctl_elem_info *uinfo)
3207{
3208 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3209 struct hda_gen_spec *spec = codec->spec;
3210 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3211}
3212
3213static int mux_enum_get(struct snd_kcontrol *kcontrol,
3214 struct snd_ctl_elem_value *ucontrol)
3215{
3216 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3217 struct hda_gen_spec *spec = codec->spec;
2a8d5391
TI
3218 /* the ctls are created at once with multiple counts */
3219 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
352f7f91
TI
3220
3221 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3222 return 0;
3223}
3224
3225static int mux_enum_put(struct snd_kcontrol *kcontrol,
3226 struct snd_ctl_elem_value *ucontrol)
3227{
3228 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2a8d5391 3229 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
352f7f91
TI
3230 return mux_select(codec, adc_idx,
3231 ucontrol->value.enumerated.item[0]);
3232}
3233
352f7f91
TI
3234static const struct snd_kcontrol_new cap_src_temp = {
3235 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3236 .name = "Input Source",
3237 .info = mux_enum_info,
3238 .get = mux_enum_get,
3239 .put = mux_enum_put,
3240};
3241
47d46abb
TI
3242/*
3243 * capture volume and capture switch ctls
3244 */
3245
352f7f91
TI
3246typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3247 struct snd_ctl_elem_value *ucontrol);
3248
47d46abb 3249/* call the given amp update function for all amps in the imux list at once */
352f7f91
TI
3250static int cap_put_caller(struct snd_kcontrol *kcontrol,
3251 struct snd_ctl_elem_value *ucontrol,
3252 put_call_t func, int type)
3253{
3254 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3255 struct hda_gen_spec *spec = codec->spec;
3256 const struct hda_input_mux *imux;
3257 struct nid_path *path;
3258 int i, adc_idx, err = 0;
3259
3260 imux = &spec->input_mux;
a053d1e3 3261 adc_idx = kcontrol->id.index;
352f7f91 3262 mutex_lock(&codec->control_mutex);
47d46abb
TI
3263 /* we use the cache-only update at first since multiple input paths
3264 * may shared the same amp; by updating only caches, the redundant
3265 * writes to hardware can be reduced.
3266 */
352f7f91
TI
3267 codec->cached_write = 1;
3268 for (i = 0; i < imux->num_items; i++) {
c697b716
TI
3269 path = get_input_path(codec, adc_idx, i);
3270 if (!path || !path->ctls[type])
352f7f91
TI
3271 continue;
3272 kcontrol->private_value = path->ctls[type];
3273 err = func(kcontrol, ucontrol);
3274 if (err < 0)
3275 goto error;
3276 }
3277 error:
3278 codec->cached_write = 0;
3279 mutex_unlock(&codec->control_mutex);
dc870f38 3280 snd_hda_codec_flush_cache(codec); /* flush the updates */
352f7f91 3281 if (err >= 0 && spec->cap_sync_hook)
7fe30711 3282 spec->cap_sync_hook(codec, kcontrol, ucontrol);
352f7f91
TI
3283 return err;
3284}
3285
3286/* capture volume ctl callbacks */
3287#define cap_vol_info snd_hda_mixer_amp_volume_info
3288#define cap_vol_get snd_hda_mixer_amp_volume_get
3289#define cap_vol_tlv snd_hda_mixer_amp_tlv
3290
3291static int cap_vol_put(struct snd_kcontrol *kcontrol,
3292 struct snd_ctl_elem_value *ucontrol)
3293{
3294 return cap_put_caller(kcontrol, ucontrol,
3295 snd_hda_mixer_amp_volume_put,
3296 NID_PATH_VOL_CTL);
3297}
3298
3299static const struct snd_kcontrol_new cap_vol_temp = {
3300 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3301 .name = "Capture Volume",
3302 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3303 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3304 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3305 .info = cap_vol_info,
3306 .get = cap_vol_get,
3307 .put = cap_vol_put,
3308 .tlv = { .c = cap_vol_tlv },
3309};
3310
3311/* capture switch ctl callbacks */
3312#define cap_sw_info snd_ctl_boolean_stereo_info
3313#define cap_sw_get snd_hda_mixer_amp_switch_get
3314
3315static int cap_sw_put(struct snd_kcontrol *kcontrol,
3316 struct snd_ctl_elem_value *ucontrol)
3317{
a90229e0 3318 return cap_put_caller(kcontrol, ucontrol,
352f7f91
TI
3319 snd_hda_mixer_amp_switch_put,
3320 NID_PATH_MUTE_CTL);
3321}
3322
3323static const struct snd_kcontrol_new cap_sw_temp = {
3324 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3325 .name = "Capture Switch",
3326 .info = cap_sw_info,
3327 .get = cap_sw_get,
3328 .put = cap_sw_put,
3329};
3330
3331static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3332{
3333 hda_nid_t nid;
3334 int i, depth;
3335
3336 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3337 for (depth = 0; depth < 3; depth++) {
3338 if (depth >= path->depth)
3339 return -EINVAL;
3340 i = path->depth - depth - 1;
3341 nid = path->path[i];
3342 if (!path->ctls[NID_PATH_VOL_CTL]) {
3343 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3344 path->ctls[NID_PATH_VOL_CTL] =
3345 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3346 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3347 int idx = path->idx[i];
3348 if (!depth && codec->single_adc_amp)
3349 idx = 0;
3350 path->ctls[NID_PATH_VOL_CTL] =
3351 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3352 }
3353 }
3354 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3355 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3356 path->ctls[NID_PATH_MUTE_CTL] =
3357 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3358 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3359 int idx = path->idx[i];
3360 if (!depth && codec->single_adc_amp)
3361 idx = 0;
3362 path->ctls[NID_PATH_MUTE_CTL] =
3363 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3364 }
3365 }
3366 }
3367 return 0;
3368}
3369
3370static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3371{
3372 struct hda_gen_spec *spec = codec->spec;
3373 struct auto_pin_cfg *cfg = &spec->autocfg;
3374 unsigned int val;
3375 int i;
3376
3377 if (!spec->inv_dmic_split)
3378 return false;
3379 for (i = 0; i < cfg->num_inputs; i++) {
3380 if (cfg->inputs[i].pin != nid)
3381 continue;
3382 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3383 return false;
3384 val = snd_hda_codec_get_pincfg(codec, nid);
3385 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3386 }
3387 return false;
3388}
3389
a90229e0 3390/* capture switch put callback for a single control with hook call */
a35bd1e3
TI
3391static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3392 struct snd_ctl_elem_value *ucontrol)
3393{
3394 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3395 struct hda_gen_spec *spec = codec->spec;
3396 int ret;
3397
3398 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3399 if (ret < 0)
3400 return ret;
3401
a90229e0 3402 if (spec->cap_sync_hook)
7fe30711 3403 spec->cap_sync_hook(codec, kcontrol, ucontrol);
a35bd1e3
TI
3404
3405 return ret;
3406}
3407
352f7f91
TI
3408static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3409 int idx, bool is_switch, unsigned int ctl,
3410 bool inv_dmic)
3411{
3412 struct hda_gen_spec *spec = codec->spec;
975cc02a 3413 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
352f7f91
TI
3414 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3415 const char *sfx = is_switch ? "Switch" : "Volume";
3416 unsigned int chs = inv_dmic ? 1 : 3;
a35bd1e3 3417 struct snd_kcontrol_new *knew;
352f7f91
TI
3418
3419 if (!ctl)
3420 return 0;
3421
3422 if (label)
3423 snprintf(tmpname, sizeof(tmpname),
3424 "%s Capture %s", label, sfx);
3425 else
3426 snprintf(tmpname, sizeof(tmpname),
3427 "Capture %s", sfx);
a35bd1e3
TI
3428 knew = add_control(spec, type, tmpname, idx,
3429 amp_val_replace_channels(ctl, chs));
3430 if (!knew)
3431 return -ENOMEM;
a90229e0 3432 if (is_switch)
a35bd1e3
TI
3433 knew->put = cap_single_sw_put;
3434 if (!inv_dmic)
3435 return 0;
352f7f91
TI
3436
3437 /* Make independent right kcontrol */
3438 if (label)
3439 snprintf(tmpname, sizeof(tmpname),
3440 "Inverted %s Capture %s", label, sfx);
3441 else
3442 snprintf(tmpname, sizeof(tmpname),
3443 "Inverted Capture %s", sfx);
a35bd1e3 3444 knew = add_control(spec, type, tmpname, idx,
352f7f91 3445 amp_val_replace_channels(ctl, 2));
a35bd1e3
TI
3446 if (!knew)
3447 return -ENOMEM;
a90229e0 3448 if (is_switch)
a35bd1e3
TI
3449 knew->put = cap_single_sw_put;
3450 return 0;
352f7f91
TI
3451}
3452
3453/* create single (and simple) capture volume and switch controls */
3454static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3455 unsigned int vol_ctl, unsigned int sw_ctl,
3456 bool inv_dmic)
3457{
3458 int err;
3459 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3460 if (err < 0)
3461 return err;
3462 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3463 if (err < 0)
3464 return err;
3465 return 0;
3466}
3467
3468/* create bound capture volume and switch controls */
3469static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3470 unsigned int vol_ctl, unsigned int sw_ctl)
3471{
3472 struct hda_gen_spec *spec = codec->spec;
3473 struct snd_kcontrol_new *knew;
3474
3475 if (vol_ctl) {
12c93df6 3476 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
352f7f91
TI
3477 if (!knew)
3478 return -ENOMEM;
3479 knew->index = idx;
3480 knew->private_value = vol_ctl;
3481 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3482 }
3483 if (sw_ctl) {
12c93df6 3484 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
352f7f91
TI
3485 if (!knew)
3486 return -ENOMEM;
3487 knew->index = idx;
3488 knew->private_value = sw_ctl;
3489 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3490 }
3491 return 0;
3492}
3493
3494/* return the vol ctl when used first in the imux list */
3495static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3496{
352f7f91
TI
3497 struct nid_path *path;
3498 unsigned int ctl;
3499 int i;
3500
c697b716 3501 path = get_input_path(codec, 0, idx);
352f7f91
TI
3502 if (!path)
3503 return 0;
3504 ctl = path->ctls[type];
3505 if (!ctl)
3506 return 0;
3507 for (i = 0; i < idx - 1; i++) {
c697b716 3508 path = get_input_path(codec, 0, i);
352f7f91
TI
3509 if (path && path->ctls[type] == ctl)
3510 return 0;
3511 }
3512 return ctl;
3513}
3514
3515/* create individual capture volume and switch controls per input */
3516static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3517{
3518 struct hda_gen_spec *spec = codec->spec;
3519 struct hda_input_mux *imux = &spec->input_mux;
c970042c 3520 int i, err, type;
352f7f91
TI
3521
3522 for (i = 0; i < imux->num_items; i++) {
352f7f91 3523 bool inv_dmic;
c970042c 3524 int idx;
9dba205b 3525
c970042c
TI
3526 idx = imux->items[i].index;
3527 if (idx >= spec->autocfg.num_inputs)
9dba205b 3528 continue;
352f7f91
TI
3529 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3530
3531 for (type = 0; type < 2; type++) {
c970042c
TI
3532 err = add_single_cap_ctl(codec,
3533 spec->input_labels[idx],
3534 spec->input_label_idxs[idx],
3535 type,
352f7f91
TI
3536 get_first_cap_ctl(codec, i, type),
3537 inv_dmic);
3538 if (err < 0)
3539 return err;
3540 }
3541 }
3542 return 0;
3543}
3544
3545static int create_capture_mixers(struct hda_codec *codec)
3546{
3547 struct hda_gen_spec *spec = codec->spec;
3548 struct hda_input_mux *imux = &spec->input_mux;
3549 int i, n, nums, err;
3550
3551 if (spec->dyn_adc_switch)
3552 nums = 1;
3553 else
3554 nums = spec->num_adc_nids;
3555
3556 if (!spec->auto_mic && imux->num_items > 1) {
3557 struct snd_kcontrol_new *knew;
624d914d
TI
3558 const char *name;
3559 name = nums > 1 ? "Input Source" : "Capture Source";
3560 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
352f7f91
TI
3561 if (!knew)
3562 return -ENOMEM;
3563 knew->count = nums;
3564 }
3565
3566 for (n = 0; n < nums; n++) {
3567 bool multi = false;
99a5592d 3568 bool multi_cap_vol = spec->multi_cap_vol;
352f7f91
TI
3569 bool inv_dmic = false;
3570 int vol, sw;
3571
3572 vol = sw = 0;
3573 for (i = 0; i < imux->num_items; i++) {
3574 struct nid_path *path;
c697b716 3575 path = get_input_path(codec, n, i);
352f7f91
TI
3576 if (!path)
3577 continue;
3578 parse_capvol_in_path(codec, path);
3579 if (!vol)
3580 vol = path->ctls[NID_PATH_VOL_CTL];
99a5592d 3581 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
352f7f91 3582 multi = true;
99a5592d
DH
3583 if (!same_amp_caps(codec, vol,
3584 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3585 multi_cap_vol = true;
3586 }
352f7f91
TI
3587 if (!sw)
3588 sw = path->ctls[NID_PATH_MUTE_CTL];
99a5592d 3589 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
352f7f91 3590 multi = true;
99a5592d
DH
3591 if (!same_amp_caps(codec, sw,
3592 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3593 multi_cap_vol = true;
3594 }
352f7f91
TI
3595 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3596 inv_dmic = true;
3597 }
3598
3599 if (!multi)
3600 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3601 inv_dmic);
ccb04157 3602 else if (!multi_cap_vol && !inv_dmic)
352f7f91
TI
3603 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3604 else
3605 err = create_multi_cap_vol_ctl(codec);
3606 if (err < 0)
3607 return err;
3608 }
3609
3610 return 0;
3611}
3612
3613/*
3614 * add mic boosts if needed
3615 */
6f7c83af
TI
3616
3617/* check whether the given amp is feasible as a boost volume */
3618static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3619 int dir, int idx)
3620{
3621 unsigned int step;
3622
3623 if (!nid_has_volume(codec, nid, dir) ||
3624 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3625 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3626 return false;
3627
3628 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3629 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3630 if (step < 0x20)
3631 return false;
3632 return true;
3633}
3634
3635/* look for a boost amp in a widget close to the pin */
3636static unsigned int look_for_boost_amp(struct hda_codec *codec,
3637 struct nid_path *path)
3638{
3639 unsigned int val = 0;
3640 hda_nid_t nid;
3641 int depth;
3642
3643 for (depth = 0; depth < 3; depth++) {
3644 if (depth >= path->depth - 1)
3645 break;
3646 nid = path->path[depth];
3647 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3648 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3649 break;
3650 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3651 path->idx[depth])) {
3652 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3653 HDA_INPUT);
3654 break;
3655 }
3656 }
3657
3658 return val;
3659}
3660
352f7f91
TI
3661static int parse_mic_boost(struct hda_codec *codec)
3662{
3663 struct hda_gen_spec *spec = codec->spec;
3664 struct auto_pin_cfg *cfg = &spec->autocfg;
6f7c83af 3665 struct hda_input_mux *imux = &spec->input_mux;
a35bd1e3 3666 int i;
352f7f91 3667
6f7c83af
TI
3668 if (!spec->num_adc_nids)
3669 return 0;
352f7f91 3670
6f7c83af
TI
3671 for (i = 0; i < imux->num_items; i++) {
3672 struct nid_path *path;
3673 unsigned int val;
3674 int idx;
975cc02a 3675 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
02aba550 3676
6f7c83af
TI
3677 idx = imux->items[i].index;
3678 if (idx >= imux->num_items)
3679 continue;
352f7f91 3680
6f7c83af 3681 /* check only line-in and mic pins */
1799cdd5 3682 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
6f7c83af
TI
3683 continue;
3684
3685 path = get_input_path(codec, 0, i);
3686 if (!path)
3687 continue;
3688
3689 val = look_for_boost_amp(codec, path);
3690 if (!val)
3691 continue;
3692
3693 /* create a boost control */
3694 snprintf(boost_label, sizeof(boost_label),
3695 "%s Boost Volume", spec->input_labels[idx]);
a35bd1e3
TI
3696 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3697 spec->input_label_idxs[idx], val))
3698 return -ENOMEM;
6f7c83af
TI
3699
3700 path->ctls[NID_PATH_BOOST_CTL] = val;
352f7f91
TI
3701 }
3702 return 0;
3703}
3704
3705/*
3706 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3707 */
3708static void parse_digital(struct hda_codec *codec)
3709{
3710 struct hda_gen_spec *spec = codec->spec;
0c8c0f56 3711 struct nid_path *path;
352f7f91 3712 int i, nums;
2c12c30d 3713 hda_nid_t dig_nid, pin;
352f7f91
TI
3714
3715 /* support multiple SPDIFs; the secondary is set up as a slave */
3716 nums = 0;
3717 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2c12c30d 3718 pin = spec->autocfg.dig_out_pins[i];
352f7f91
TI
3719 dig_nid = look_for_dac(codec, pin, true);
3720 if (!dig_nid)
3721 continue;
3ca529d3 3722 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
0c8c0f56 3723 if (!path)
352f7f91 3724 continue;
4e76a883 3725 print_nid_path(codec, "digout", path);
e1284af7 3726 path->active = true;
196c1766 3727 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
2c12c30d 3728 set_pin_target(codec, pin, PIN_OUT, false);
352f7f91
TI
3729 if (!nums) {
3730 spec->multiout.dig_out_nid = dig_nid;
3731 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3732 } else {
3733 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3734 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
d576422e 3735 break;
352f7f91
TI
3736 spec->slave_dig_outs[nums - 1] = dig_nid;
3737 }
3738 nums++;
3739 }
3740
3741 if (spec->autocfg.dig_in_pin) {
2c12c30d 3742 pin = spec->autocfg.dig_in_pin;
352f7f91
TI
3743 dig_nid = codec->start_nid;
3744 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
352f7f91
TI
3745 unsigned int wcaps = get_wcaps(codec, dig_nid);
3746 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3747 continue;
3748 if (!(wcaps & AC_WCAP_DIGITAL))
3749 continue;
2c12c30d 3750 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
352f7f91 3751 if (path) {
4e76a883 3752 print_nid_path(codec, "digin", path);
352f7f91
TI
3753 path->active = true;
3754 spec->dig_in_nid = dig_nid;
2430d7b7 3755 spec->digin_path = snd_hda_get_path_idx(codec, path);
2c12c30d 3756 set_pin_target(codec, pin, PIN_IN, false);
352f7f91
TI
3757 break;
3758 }
3759 }
3760 }
3761}
3762
3763
3764/*
3765 * input MUX handling
3766 */
3767
3768static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3769
3770/* select the given imux item; either unmute exclusively or select the route */
3771static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3772 unsigned int idx)
3773{
3774 struct hda_gen_spec *spec = codec->spec;
3775 const struct hda_input_mux *imux;
55196fff 3776 struct nid_path *old_path, *path;
352f7f91
TI
3777
3778 imux = &spec->input_mux;
3779 if (!imux->num_items)
3780 return 0;
3781
3782 if (idx >= imux->num_items)
3783 idx = imux->num_items - 1;
3784 if (spec->cur_mux[adc_idx] == idx)
3785 return 0;
3786
55196fff
TI
3787 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3788 if (!old_path)
352f7f91 3789 return 0;
55196fff
TI
3790 if (old_path->active)
3791 snd_hda_activate_path(codec, old_path, false, false);
352f7f91
TI
3792
3793 spec->cur_mux[adc_idx] = idx;
3794
967303da
TI
3795 if (spec->hp_mic)
3796 update_hp_mic(codec, adc_idx, false);
352f7f91
TI
3797
3798 if (spec->dyn_adc_switch)
3799 dyn_adc_pcm_resetup(codec, idx);
3800
c697b716 3801 path = get_input_path(codec, adc_idx, idx);
352f7f91
TI
3802 if (!path)
3803 return 0;
3804 if (path->active)
3805 return 0;
3806 snd_hda_activate_path(codec, path, true, false);
3807 if (spec->cap_sync_hook)
7fe30711 3808 spec->cap_sync_hook(codec, NULL, NULL);
55196fff 3809 path_power_down_sync(codec, old_path);
352f7f91
TI
3810 return 1;
3811}
3812
3813
3814/*
3815 * Jack detections for HP auto-mute and mic-switch
3816 */
3817
3818/* check each pin in the given array; returns true if any of them is plugged */
3819static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3820{
60ea8ca2
TI
3821 int i;
3822 bool present = false;
352f7f91
TI
3823
3824 for (i = 0; i < num_pins; i++) {
3825 hda_nid_t nid = pins[i];
3826 if (!nid)
3827 break;
0b4df931
TI
3828 /* don't detect pins retasked as inputs */
3829 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3830 continue;
60ea8ca2
TI
3831 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3832 present = true;
352f7f91
TI
3833 }
3834 return present;
3835}
3836
3837/* standard HP/line-out auto-mute helper */
3838static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
e80c60f3 3839 int *paths, bool mute)
352f7f91
TI
3840{
3841 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
3842 int i;
3843
3844 for (i = 0; i < num_pins; i++) {
3845 hda_nid_t nid = pins[i];
967303da 3846 unsigned int val, oldval;
352f7f91
TI
3847 if (!nid)
3848 break;
7eebffd3
TI
3849
3850 if (spec->auto_mute_via_amp) {
e80c60f3
TI
3851 struct nid_path *path;
3852 hda_nid_t mute_nid;
3853
3854 path = snd_hda_get_path_from_idx(codec, paths[i]);
3855 if (!path)
3856 continue;
3857 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3858 if (!mute_nid)
3859 continue;
7eebffd3 3860 if (mute)
e80c60f3 3861 spec->mute_bits |= (1ULL << mute_nid);
7eebffd3 3862 else
e80c60f3 3863 spec->mute_bits &= ~(1ULL << mute_nid);
7eebffd3
TI
3864 set_pin_eapd(codec, nid, !mute);
3865 continue;
3866 }
3867
967303da
TI
3868 oldval = snd_hda_codec_get_pin_target(codec, nid);
3869 if (oldval & PIN_IN)
3870 continue; /* no mute for inputs */
352f7f91
TI
3871 /* don't reset VREF value in case it's controlling
3872 * the amp (see alc861_fixup_asus_amp_vref_0f())
3873 */
2c12c30d 3874 if (spec->keep_vref_in_automute)
967303da 3875 val = oldval & ~PIN_HP;
2c12c30d 3876 else
352f7f91 3877 val = 0;
2c12c30d 3878 if (!mute)
967303da 3879 val |= oldval;
2c12c30d
TI
3880 /* here we call update_pin_ctl() so that the pinctl is changed
3881 * without changing the pinctl target value;
3882 * the original target value will be still referred at the
3883 * init / resume again
3884 */
3885 update_pin_ctl(codec, nid, val);
d5a9f1bb 3886 set_pin_eapd(codec, nid, !mute);
352f7f91
TI
3887 }
3888}
3889
3890/* Toggle outputs muting */
5d550e15 3891void snd_hda_gen_update_outputs(struct hda_codec *codec)
352f7f91
TI
3892{
3893 struct hda_gen_spec *spec = codec->spec;
e80c60f3 3894 int *paths;
352f7f91
TI
3895 int on;
3896
3897 /* Control HP pins/amps depending on master_mute state;
3898 * in general, HP pins/amps control should be enabled in all cases,
3899 * but currently set only for master_mute, just to be safe
3900 */
e80c60f3
TI
3901 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3902 paths = spec->out_paths;
3903 else
3904 paths = spec->hp_paths;
967303da 3905 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
e80c60f3 3906 spec->autocfg.hp_pins, paths, spec->master_mute);
352f7f91
TI
3907
3908 if (!spec->automute_speaker)
3909 on = 0;
3910 else
3911 on = spec->hp_jack_present | spec->line_jack_present;
3912 on |= spec->master_mute;
47b9ddb8 3913 spec->speaker_muted = on;
e80c60f3
TI
3914 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3915 paths = spec->out_paths;
3916 else
3917 paths = spec->speaker_paths;
352f7f91 3918 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
e80c60f3 3919 spec->autocfg.speaker_pins, paths, on);
352f7f91
TI
3920
3921 /* toggle line-out mutes if needed, too */
3922 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3923 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3924 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3925 return;
3926 if (!spec->automute_lo)
3927 on = 0;
3928 else
3929 on = spec->hp_jack_present;
3930 on |= spec->master_mute;
47b9ddb8 3931 spec->line_out_muted = on;
e80c60f3 3932 paths = spec->out_paths;
352f7f91 3933 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
e80c60f3 3934 spec->autocfg.line_out_pins, paths, on);
352f7f91 3935}
2698ea98 3936EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
352f7f91
TI
3937
3938static void call_update_outputs(struct hda_codec *codec)
3939{
3940 struct hda_gen_spec *spec = codec->spec;
3941 if (spec->automute_hook)
3942 spec->automute_hook(codec);
3943 else
5d550e15 3944 snd_hda_gen_update_outputs(codec);
7eebffd3
TI
3945
3946 /* sync the whole vmaster slaves to reflect the new auto-mute status */
3947 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
3948 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
352f7f91
TI
3949}
3950
3951/* standard HP-automute helper */
1a4f69d5
TI
3952void snd_hda_gen_hp_automute(struct hda_codec *codec,
3953 struct hda_jack_callback *jack)
352f7f91
TI
3954{
3955 struct hda_gen_spec *spec = codec->spec;
92603c59
TI
3956 hda_nid_t *pins = spec->autocfg.hp_pins;
3957 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
3958
3959 /* No detection for the first HP jack during indep-HP mode */
3960 if (spec->indep_hp_enabled) {
3961 pins++;
3962 num_pins--;
3963 }
352f7f91 3964
92603c59 3965 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
352f7f91
TI
3966 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3967 return;
3968 call_update_outputs(codec);
3969}
2698ea98 3970EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
352f7f91
TI
3971
3972/* standard line-out-automute helper */
1a4f69d5
TI
3973void snd_hda_gen_line_automute(struct hda_codec *codec,
3974 struct hda_jack_callback *jack)
352f7f91
TI
3975{
3976 struct hda_gen_spec *spec = codec->spec;
3977
3978 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3979 return;
3980 /* check LO jack only when it's different from HP */
3981 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3982 return;
3983
3984 spec->line_jack_present =
3985 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3986 spec->autocfg.line_out_pins);
3987 if (!spec->automute_speaker || !spec->detect_lo)
3988 return;
3989 call_update_outputs(codec);
3990}
2698ea98 3991EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
352f7f91
TI
3992
3993/* standard mic auto-switch helper */
1a4f69d5
TI
3994void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
3995 struct hda_jack_callback *jack)
352f7f91
TI
3996{
3997 struct hda_gen_spec *spec = codec->spec;
3998 int i;
3999
4000 if (!spec->auto_mic)
4001 return;
4002
4003 for (i = spec->am_num_entries - 1; i > 0; i--) {
0b4df931
TI
4004 hda_nid_t pin = spec->am_entry[i].pin;
4005 /* don't detect pins retasked as outputs */
4006 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4007 continue;
60ea8ca2 4008 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
352f7f91
TI
4009 mux_select(codec, 0, spec->am_entry[i].idx);
4010 return;
4011 }
4012 }
4013 mux_select(codec, 0, spec->am_entry[0].idx);
4014}
2698ea98 4015EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
352f7f91 4016
77afe0e9 4017/* call appropriate hooks */
1a4f69d5
TI
4018static void call_hp_automute(struct hda_codec *codec,
4019 struct hda_jack_callback *jack)
77afe0e9
TI
4020{
4021 struct hda_gen_spec *spec = codec->spec;
4022 if (spec->hp_automute_hook)
4023 spec->hp_automute_hook(codec, jack);
4024 else
4025 snd_hda_gen_hp_automute(codec, jack);
4026}
4027
4028static void call_line_automute(struct hda_codec *codec,
1a4f69d5 4029 struct hda_jack_callback *jack)
77afe0e9
TI
4030{
4031 struct hda_gen_spec *spec = codec->spec;
4032 if (spec->line_automute_hook)
4033 spec->line_automute_hook(codec, jack);
4034 else
4035 snd_hda_gen_line_automute(codec, jack);
4036}
4037
4038static void call_mic_autoswitch(struct hda_codec *codec,
1a4f69d5 4039 struct hda_jack_callback *jack)
77afe0e9
TI
4040{
4041 struct hda_gen_spec *spec = codec->spec;
4042 if (spec->mic_autoswitch_hook)
4043 spec->mic_autoswitch_hook(codec, jack);
4044 else
4045 snd_hda_gen_mic_autoswitch(codec, jack);
4046}
4047
963afde9
TI
4048/* update jack retasking */
4049static void update_automute_all(struct hda_codec *codec)
4050{
4051 call_hp_automute(codec, NULL);
4052 call_line_automute(codec, NULL);
4053 call_mic_autoswitch(codec, NULL);
4054}
4055
352f7f91
TI
4056/*
4057 * Auto-Mute mode mixer enum support
4058 */
4059static int automute_mode_info(struct snd_kcontrol *kcontrol,
4060 struct snd_ctl_elem_info *uinfo)
4061{
4062 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4063 struct hda_gen_spec *spec = codec->spec;
4064 static const char * const texts3[] = {
4065 "Disabled", "Speaker Only", "Line Out+Speaker"
4066 };
4067
4068 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4069 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4070 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4071}
4072
4073static int automute_mode_get(struct snd_kcontrol *kcontrol,
4074 struct snd_ctl_elem_value *ucontrol)
4075{
4076 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4077 struct hda_gen_spec *spec = codec->spec;
4078 unsigned int val = 0;
4079 if (spec->automute_speaker)
4080 val++;
4081 if (spec->automute_lo)
4082 val++;
4083
4084 ucontrol->value.enumerated.item[0] = val;
4085 return 0;
4086}
4087
4088static int automute_mode_put(struct snd_kcontrol *kcontrol,
4089 struct snd_ctl_elem_value *ucontrol)
4090{
4091 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4092 struct hda_gen_spec *spec = codec->spec;
4093
4094 switch (ucontrol->value.enumerated.item[0]) {
4095 case 0:
4096 if (!spec->automute_speaker && !spec->automute_lo)
4097 return 0;
4098 spec->automute_speaker = 0;
4099 spec->automute_lo = 0;
4100 break;
4101 case 1:
4102 if (spec->automute_speaker_possible) {
4103 if (!spec->automute_lo && spec->automute_speaker)
4104 return 0;
4105 spec->automute_speaker = 1;
4106 spec->automute_lo = 0;
4107 } else if (spec->automute_lo_possible) {
4108 if (spec->automute_lo)
4109 return 0;
4110 spec->automute_lo = 1;
4111 } else
4112 return -EINVAL;
4113 break;
4114 case 2:
4115 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4116 return -EINVAL;
4117 if (spec->automute_speaker && spec->automute_lo)
4118 return 0;
4119 spec->automute_speaker = 1;
4120 spec->automute_lo = 1;
4121 break;
4122 default:
4123 return -EINVAL;
4124 }
4125 call_update_outputs(codec);
4126 return 1;
4127}
4128
4129static const struct snd_kcontrol_new automute_mode_enum = {
4130 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4131 .name = "Auto-Mute Mode",
4132 .info = automute_mode_info,
4133 .get = automute_mode_get,
4134 .put = automute_mode_put,
4135};
4136
4137static int add_automute_mode_enum(struct hda_codec *codec)
4138{
4139 struct hda_gen_spec *spec = codec->spec;
4140
12c93df6 4141 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
352f7f91
TI
4142 return -ENOMEM;
4143 return 0;
4144}
4145
4146/*
4147 * Check the availability of HP/line-out auto-mute;
4148 * Set up appropriately if really supported
4149 */
4150static int check_auto_mute_availability(struct hda_codec *codec)
4151{
4152 struct hda_gen_spec *spec = codec->spec;
4153 struct auto_pin_cfg *cfg = &spec->autocfg;
4154 int present = 0;
4155 int i, err;
4156
f72706be
TI
4157 if (spec->suppress_auto_mute)
4158 return 0;
4159
352f7f91
TI
4160 if (cfg->hp_pins[0])
4161 present++;
4162 if (cfg->line_out_pins[0])
4163 present++;
4164 if (cfg->speaker_pins[0])
4165 present++;
4166 if (present < 2) /* need two different output types */
4167 return 0;
4168
4169 if (!cfg->speaker_pins[0] &&
4170 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4171 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4172 sizeof(cfg->speaker_pins));
4173 cfg->speaker_outs = cfg->line_outs;
4174 }
4175
4176 if (!cfg->hp_pins[0] &&
4177 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4178 memcpy(cfg->hp_pins, cfg->line_out_pins,
4179 sizeof(cfg->hp_pins));
4180 cfg->hp_outs = cfg->line_outs;
4181 }
4182
4183 for (i = 0; i < cfg->hp_outs; i++) {
4184 hda_nid_t nid = cfg->hp_pins[i];
4185 if (!is_jack_detectable(codec, nid))
4186 continue;
4e76a883 4187 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
62f949bf 4188 snd_hda_jack_detect_enable_callback(codec, nid,
77afe0e9 4189 call_hp_automute);
352f7f91
TI
4190 spec->detect_hp = 1;
4191 }
4192
4193 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4194 if (cfg->speaker_outs)
4195 for (i = 0; i < cfg->line_outs; i++) {
4196 hda_nid_t nid = cfg->line_out_pins[i];
4197 if (!is_jack_detectable(codec, nid))
4198 continue;
4e76a883 4199 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
352f7f91 4200 snd_hda_jack_detect_enable_callback(codec, nid,
77afe0e9 4201 call_line_automute);
352f7f91
TI
4202 spec->detect_lo = 1;
4203 }
4204 spec->automute_lo_possible = spec->detect_hp;
4205 }
4206
4207 spec->automute_speaker_possible = cfg->speaker_outs &&
4208 (spec->detect_hp || spec->detect_lo);
4209
4210 spec->automute_lo = spec->automute_lo_possible;
4211 spec->automute_speaker = spec->automute_speaker_possible;
4212
4213 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4214 /* create a control for automute mode */
4215 err = add_automute_mode_enum(codec);
4216 if (err < 0)
4217 return err;
4218 }
4219 return 0;
4220}
352f7f91
TI
4221
4222/* check whether all auto-mic pins are valid; setup indices if OK */
4223static bool auto_mic_check_imux(struct hda_codec *codec)
4224{
4225 struct hda_gen_spec *spec = codec->spec;
4226 const struct hda_input_mux *imux;
4227 int i;
4228
4229 imux = &spec->input_mux;
4230 for (i = 0; i < spec->am_num_entries; i++) {
4231 spec->am_entry[i].idx =
4232 find_idx_in_nid_list(spec->am_entry[i].pin,
4233 spec->imux_pins, imux->num_items);
4234 if (spec->am_entry[i].idx < 0)
4235 return false; /* no corresponding imux */
4236 }
4237
4238 /* we don't need the jack detection for the first pin */
4239 for (i = 1; i < spec->am_num_entries; i++)
4240 snd_hda_jack_detect_enable_callback(codec,
4241 spec->am_entry[i].pin,
77afe0e9 4242 call_mic_autoswitch);
352f7f91
TI
4243 return true;
4244}
4245
4246static int compare_attr(const void *ap, const void *bp)
4247{
4248 const struct automic_entry *a = ap;
4249 const struct automic_entry *b = bp;
4250 return (int)(a->attr - b->attr);
4251}
1da177e4
LT
4252
4253/*
352f7f91
TI
4254 * Check the availability of auto-mic switch;
4255 * Set up if really supported
1da177e4 4256 */
352f7f91
TI
4257static int check_auto_mic_availability(struct hda_codec *codec)
4258{
4259 struct hda_gen_spec *spec = codec->spec;
4260 struct auto_pin_cfg *cfg = &spec->autocfg;
4261 unsigned int types;
4262 int i, num_pins;
4263
d12daf6f
TI
4264 if (spec->suppress_auto_mic)
4265 return 0;
4266
352f7f91
TI
4267 types = 0;
4268 num_pins = 0;
4269 for (i = 0; i < cfg->num_inputs; i++) {
4270 hda_nid_t nid = cfg->inputs[i].pin;
4271 unsigned int attr;
4272 attr = snd_hda_codec_get_pincfg(codec, nid);
4273 attr = snd_hda_get_input_pin_attr(attr);
4274 if (types & (1 << attr))
4275 return 0; /* already occupied */
4276 switch (attr) {
4277 case INPUT_PIN_ATTR_INT:
4278 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4279 return 0; /* invalid type */
4280 break;
4281 case INPUT_PIN_ATTR_UNUSED:
4282 return 0; /* invalid entry */
4283 default:
4284 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4285 return 0; /* invalid type */
4286 if (!spec->line_in_auto_switch &&
4287 cfg->inputs[i].type != AUTO_PIN_MIC)
4288 return 0; /* only mic is allowed */
4289 if (!is_jack_detectable(codec, nid))
4290 return 0; /* no unsol support */
4291 break;
4292 }
4293 if (num_pins >= MAX_AUTO_MIC_PINS)
4294 return 0;
4295 types |= (1 << attr);
4296 spec->am_entry[num_pins].pin = nid;
4297 spec->am_entry[num_pins].attr = attr;
4298 num_pins++;
4299 }
4300
4301 if (num_pins < 2)
4302 return 0;
4303
4304 spec->am_num_entries = num_pins;
4305 /* sort the am_entry in the order of attr so that the pin with a
4306 * higher attr will be selected when the jack is plugged.
4307 */
4308 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4309 compare_attr, NULL);
4310
4311 if (!auto_mic_check_imux(codec))
4312 return 0;
4313
4314 spec->auto_mic = 1;
4315 spec->num_adc_nids = 1;
4316 spec->cur_mux[0] = spec->am_entry[0].idx;
4e76a883 4317 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
352f7f91
TI
4318 spec->am_entry[0].pin,
4319 spec->am_entry[1].pin,
4320 spec->am_entry[2].pin);
4321
1da177e4
LT
4322 return 0;
4323}
4324
55196fff 4325/* power_filter hook; make inactive widgets into power down */
dfc6e469 4326unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
55196fff
TI
4327 hda_nid_t nid,
4328 unsigned int power_state)
4329{
dfc6e469 4330 if (power_state != AC_PWRST_D0 || nid == codec->afg)
55196fff
TI
4331 return power_state;
4332 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4333 return power_state;
b1b9fbd0 4334 if (is_active_nid_for_any(codec, nid))
55196fff
TI
4335 return power_state;
4336 return AC_PWRST_D3;
4337}
dfc6e469 4338EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
55196fff 4339
ebb93c05
TI
4340/* mute all aamix inputs initially; parse up to the first leaves */
4341static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4342{
4343 int i, nums;
4344 const hda_nid_t *conn;
4345 bool has_amp;
4346
4347 nums = snd_hda_get_conn_list(codec, mix, &conn);
4348 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4349 for (i = 0; i < nums; i++) {
4350 if (has_amp)
4351 snd_hda_codec_amp_stereo(codec, mix,
4352 HDA_INPUT, i,
4353 0xff, HDA_AMP_MUTE);
4354 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4355 snd_hda_codec_amp_stereo(codec, conn[i],
4356 HDA_OUTPUT, 0,
4357 0xff, HDA_AMP_MUTE);
4358 }
4359}
1da177e4 4360
9eb413e5
TI
4361/*
4362 * Parse the given BIOS configuration and set up the hda_gen_spec
4363 *
4364 * return 1 if successful, 0 if the proper config is not found,
352f7f91
TI
4365 * or a negative error code
4366 */
4367int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
9eb413e5 4368 struct auto_pin_cfg *cfg)
352f7f91
TI
4369{
4370 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
4371 int err;
4372
1c70a583
TI
4373 parse_user_hints(codec);
4374
e4a395e7
TI
4375 if (spec->mixer_nid && !spec->mixer_merge_nid)
4376 spec->mixer_merge_nid = spec->mixer_nid;
4377
9eb413e5
TI
4378 if (cfg != &spec->autocfg) {
4379 spec->autocfg = *cfg;
4380 cfg = &spec->autocfg;
4381 }
4382
98bd1115
TI
4383 if (!spec->main_out_badness)
4384 spec->main_out_badness = &hda_main_out_badness;
4385 if (!spec->extra_out_badness)
4386 spec->extra_out_badness = &hda_extra_out_badness;
4387
6fc4cb97
DH
4388 fill_all_dac_nids(codec);
4389
352f7f91
TI
4390 if (!cfg->line_outs) {
4391 if (cfg->dig_outs || cfg->dig_in_pin) {
4392 spec->multiout.max_channels = 2;
4393 spec->no_analog = 1;
4394 goto dig_only;
4395 }
c9e4bdb7
TI
4396 if (!cfg->num_inputs && !cfg->dig_in_pin)
4397 return 0; /* can't find valid BIOS pin config */
352f7f91
TI
4398 }
4399
4400 if (!spec->no_primary_hp &&
4401 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4402 cfg->line_outs <= cfg->hp_outs) {
4403 /* use HP as primary out */
4404 cfg->speaker_outs = cfg->line_outs;
4405 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4406 sizeof(cfg->speaker_pins));
4407 cfg->line_outs = cfg->hp_outs;
4408 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4409 cfg->hp_outs = 0;
4410 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4411 cfg->line_out_type = AUTO_PIN_HP_OUT;
4412 }
4413
4414 err = parse_output_paths(codec);
4415 if (err < 0)
4416 return err;
4417 err = create_multi_channel_mode(codec);
4418 if (err < 0)
4419 return err;
4420 err = create_multi_out_ctls(codec, cfg);
4421 if (err < 0)
4422 return err;
4423 err = create_hp_out_ctls(codec);
4424 if (err < 0)
4425 return err;
4426 err = create_speaker_out_ctls(codec);
38cf6f1a
TI
4427 if (err < 0)
4428 return err;
4429 err = create_indep_hp_ctls(codec);
c30aa7b2
TI
4430 if (err < 0)
4431 return err;
4432 err = create_loopback_mixing_ctl(codec);
352f7f91
TI
4433 if (err < 0)
4434 return err;
967303da 4435 err = create_hp_mic(codec);
352f7f91
TI
4436 if (err < 0)
4437 return err;
4438 err = create_input_ctls(codec);
4439 if (err < 0)
4440 return err;
4441
a07a949b
TI
4442 spec->const_channel_count = spec->ext_channel_count;
4443 /* check the multiple speaker and headphone pins */
4444 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4445 spec->const_channel_count = max(spec->const_channel_count,
4446 cfg->speaker_outs * 2);
4447 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4448 spec->const_channel_count = max(spec->const_channel_count,
4449 cfg->hp_outs * 2);
4450 spec->multiout.max_channels = max(spec->ext_channel_count,
4451 spec->const_channel_count);
352f7f91
TI
4452
4453 err = check_auto_mute_availability(codec);
4454 if (err < 0)
4455 return err;
4456
4457 err = check_dyn_adc_switch(codec);
4458 if (err < 0)
4459 return err;
4460
967303da
TI
4461 err = check_auto_mic_availability(codec);
4462 if (err < 0)
4463 return err;
1da177e4 4464
f1e762dd
TI
4465 /* add stereo mix if available and not enabled yet */
4466 if (!spec->auto_mic && spec->mixer_nid &&
4467 spec->add_stereo_mix_input &&
4468 spec->input_mux.num_items > 1 &&
4469 snd_hda_get_bool_hint(codec, "add_stereo_mix_input") < 0) {
4470 err = parse_capture_source(codec, spec->mixer_nid,
4471 CFG_IDX_MIX, spec->num_all_adcs,
4472 "Stereo Mix", 0);
4473 if (err < 0)
4474 return err;
4475 }
4476
4477
352f7f91
TI
4478 err = create_capture_mixers(codec);
4479 if (err < 0)
4480 return err;
a7da6ce5 4481
352f7f91
TI
4482 err = parse_mic_boost(codec);
4483 if (err < 0)
4484 return err;
4485
ced4cefc
TI
4486 /* create "Headphone Mic Jack Mode" if no input selection is
4487 * available (or user specifies add_jack_modes hint)
4488 */
4489 if (spec->hp_mic_pin &&
4490 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4491 spec->add_jack_modes)) {
4492 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4493 if (err < 0)
4494 return err;
4495 }
4496
f811c3cf 4497 if (spec->add_jack_modes) {
978e77e7
TI
4498 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4499 err = create_out_jack_modes(codec, cfg->line_outs,
4500 cfg->line_out_pins);
4501 if (err < 0)
4502 return err;
4503 }
4504 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4505 err = create_out_jack_modes(codec, cfg->hp_outs,
4506 cfg->hp_pins);
4507 if (err < 0)
4508 return err;
4509 }
4510 }
4511
ebb93c05
TI
4512 /* mute all aamix input initially */
4513 if (spec->mixer_nid)
4514 mute_all_mixer_nid(codec, spec->mixer_nid);
4515
352f7f91
TI
4516 dig_only:
4517 parse_digital(codec);
4518
55196fff
TI
4519 if (spec->power_down_unused)
4520 codec->power_filter = snd_hda_gen_path_power_filter;
4521
7504b6cd
TI
4522 if (!spec->no_analog && spec->beep_nid) {
4523 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4524 if (err < 0)
4525 return err;
4526 }
4527
352f7f91 4528 return 1;
a7da6ce5 4529}
2698ea98 4530EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
a7da6ce5 4531
071c73ad 4532
352f7f91
TI
4533/*
4534 * Build control elements
4535 */
4536
4537/* slave controls for virtual master */
4538static const char * const slave_pfxs[] = {
4539 "Front", "Surround", "Center", "LFE", "Side",
4540 "Headphone", "Speaker", "Mono", "Line Out",
4541 "CLFE", "Bass Speaker", "PCM",
ee79c69a
TI
4542 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4543 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4544 "Headphone Side",
352f7f91
TI
4545 NULL,
4546};
4547
4548int snd_hda_gen_build_controls(struct hda_codec *codec)
4549{
4550 struct hda_gen_spec *spec = codec->spec;
4551 int err;
1da177e4 4552
36502d02
TI
4553 if (spec->kctls.used) {
4554 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4555 if (err < 0)
4556 return err;
4557 }
071c73ad 4558
352f7f91
TI
4559 if (spec->multiout.dig_out_nid) {
4560 err = snd_hda_create_dig_out_ctls(codec,
4561 spec->multiout.dig_out_nid,
4562 spec->multiout.dig_out_nid,
4563 spec->pcm_rec[1].pcm_type);
4564 if (err < 0)
4565 return err;
4566 if (!spec->no_analog) {
4567 err = snd_hda_create_spdif_share_sw(codec,
4568 &spec->multiout);
4569 if (err < 0)
4570 return err;
4571 spec->multiout.share_spdif = 1;
4572 }
4573 }
4574 if (spec->dig_in_nid) {
4575 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
071c73ad
TI
4576 if (err < 0)
4577 return err;
071c73ad 4578 }
1da177e4 4579
352f7f91
TI
4580 /* if we have no master control, let's create it */
4581 if (!spec->no_analog &&
4582 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
352f7f91 4583 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
7a71bbf3 4584 spec->vmaster_tlv, slave_pfxs,
352f7f91
TI
4585 "Playback Volume");
4586 if (err < 0)
4587 return err;
4588 }
4589 if (!spec->no_analog &&
4590 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4591 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4592 NULL, slave_pfxs,
4593 "Playback Switch",
4594 true, &spec->vmaster_mute.sw_kctl);
4595 if (err < 0)
4596 return err;
b63eae0a 4597 if (spec->vmaster_mute.hook) {
fd25a97a
TI
4598 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4599 spec->vmaster_mute_enum);
b63eae0a
TI
4600 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4601 }
352f7f91 4602 }
071c73ad 4603
352f7f91 4604 free_kctls(spec); /* no longer needed */
071c73ad 4605
352f7f91
TI
4606 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4607 if (err < 0)
4608 return err;
4609
1da177e4
LT
4610 return 0;
4611}
2698ea98 4612EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
1da177e4
LT
4613
4614
4615/*
352f7f91 4616 * PCM definitions
1da177e4 4617 */
1da177e4 4618
e6b85f3c
TI
4619static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4620 struct hda_codec *codec,
4621 struct snd_pcm_substream *substream,
4622 int action)
4623{
4624 struct hda_gen_spec *spec = codec->spec;
4625 if (spec->pcm_playback_hook)
4626 spec->pcm_playback_hook(hinfo, codec, substream, action);
4627}
4628
ac2e8736
TI
4629static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4630 struct hda_codec *codec,
4631 struct snd_pcm_substream *substream,
4632 int action)
4633{
4634 struct hda_gen_spec *spec = codec->spec;
4635 if (spec->pcm_capture_hook)
4636 spec->pcm_capture_hook(hinfo, codec, substream, action);
4637}
4638
352f7f91
TI
4639/*
4640 * Analog playback callbacks
4641 */
4642static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4643 struct hda_codec *codec,
4644 struct snd_pcm_substream *substream)
4645{
4646 struct hda_gen_spec *spec = codec->spec;
38cf6f1a
TI
4647 int err;
4648
4649 mutex_lock(&spec->pcm_mutex);
4650 err = snd_hda_multi_out_analog_open(codec,
4651 &spec->multiout, substream,
352f7f91 4652 hinfo);
e6b85f3c 4653 if (!err) {
38cf6f1a 4654 spec->active_streams |= 1 << STREAM_MULTI_OUT;
e6b85f3c
TI
4655 call_pcm_playback_hook(hinfo, codec, substream,
4656 HDA_GEN_PCM_ACT_OPEN);
4657 }
38cf6f1a
TI
4658 mutex_unlock(&spec->pcm_mutex);
4659 return err;
352f7f91 4660}
1da177e4 4661
352f7f91
TI
4662static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4663 struct hda_codec *codec,
4664 unsigned int stream_tag,
4665 unsigned int format,
4666 struct snd_pcm_substream *substream)
4667{
4668 struct hda_gen_spec *spec = codec->spec;
e6b85f3c
TI
4669 int err;
4670
4671 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4672 stream_tag, format, substream);
4673 if (!err)
4674 call_pcm_playback_hook(hinfo, codec, substream,
4675 HDA_GEN_PCM_ACT_PREPARE);
4676 return err;
352f7f91 4677}
1da177e4 4678
352f7f91
TI
4679static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4680 struct hda_codec *codec,
4681 struct snd_pcm_substream *substream)
4682{
4683 struct hda_gen_spec *spec = codec->spec;
e6b85f3c
TI
4684 int err;
4685
4686 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4687 if (!err)
4688 call_pcm_playback_hook(hinfo, codec, substream,
4689 HDA_GEN_PCM_ACT_CLEANUP);
4690 return err;
1da177e4
LT
4691}
4692
38cf6f1a
TI
4693static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4694 struct hda_codec *codec,
4695 struct snd_pcm_substream *substream)
4696{
4697 struct hda_gen_spec *spec = codec->spec;
4698 mutex_lock(&spec->pcm_mutex);
4699 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
e6b85f3c
TI
4700 call_pcm_playback_hook(hinfo, codec, substream,
4701 HDA_GEN_PCM_ACT_CLOSE);
38cf6f1a
TI
4702 mutex_unlock(&spec->pcm_mutex);
4703 return 0;
4704}
4705
ac2e8736
TI
4706static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4707 struct hda_codec *codec,
4708 struct snd_pcm_substream *substream)
4709{
4710 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4711 return 0;
4712}
4713
4714static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4715 struct hda_codec *codec,
4716 unsigned int stream_tag,
4717 unsigned int format,
4718 struct snd_pcm_substream *substream)
4719{
4720 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4721 call_pcm_capture_hook(hinfo, codec, substream,
4722 HDA_GEN_PCM_ACT_PREPARE);
4723 return 0;
4724}
4725
4726static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4727 struct hda_codec *codec,
4728 struct snd_pcm_substream *substream)
4729{
4730 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4731 call_pcm_capture_hook(hinfo, codec, substream,
4732 HDA_GEN_PCM_ACT_CLEANUP);
4733 return 0;
4734}
4735
4736static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4737 struct hda_codec *codec,
4738 struct snd_pcm_substream *substream)
4739{
4740 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4741 return 0;
4742}
4743
38cf6f1a
TI
4744static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4745 struct hda_codec *codec,
4746 struct snd_pcm_substream *substream)
4747{
4748 struct hda_gen_spec *spec = codec->spec;
4749 int err = 0;
4750
4751 mutex_lock(&spec->pcm_mutex);
4752 if (!spec->indep_hp_enabled)
4753 err = -EBUSY;
4754 else
4755 spec->active_streams |= 1 << STREAM_INDEP_HP;
e6b85f3c
TI
4756 call_pcm_playback_hook(hinfo, codec, substream,
4757 HDA_GEN_PCM_ACT_OPEN);
38cf6f1a
TI
4758 mutex_unlock(&spec->pcm_mutex);
4759 return err;
4760}
4761
4762static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4763 struct hda_codec *codec,
4764 struct snd_pcm_substream *substream)
4765{
4766 struct hda_gen_spec *spec = codec->spec;
4767 mutex_lock(&spec->pcm_mutex);
4768 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
e6b85f3c
TI
4769 call_pcm_playback_hook(hinfo, codec, substream,
4770 HDA_GEN_PCM_ACT_CLOSE);
38cf6f1a
TI
4771 mutex_unlock(&spec->pcm_mutex);
4772 return 0;
4773}
4774
e6b85f3c
TI
4775static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4776 struct hda_codec *codec,
4777 unsigned int stream_tag,
4778 unsigned int format,
4779 struct snd_pcm_substream *substream)
4780{
4781 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4782 call_pcm_playback_hook(hinfo, codec, substream,
4783 HDA_GEN_PCM_ACT_PREPARE);
4784 return 0;
4785}
4786
4787static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4788 struct hda_codec *codec,
4789 struct snd_pcm_substream *substream)
4790{
4791 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4792 call_pcm_playback_hook(hinfo, codec, substream,
4793 HDA_GEN_PCM_ACT_CLEANUP);
4794 return 0;
4795}
4796
1da177e4 4797/*
352f7f91 4798 * Digital out
1da177e4 4799 */
352f7f91
TI
4800static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4801 struct hda_codec *codec,
4802 struct snd_pcm_substream *substream)
1da177e4 4803{
352f7f91
TI
4804 struct hda_gen_spec *spec = codec->spec;
4805 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4806}
1da177e4 4807
352f7f91
TI
4808static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4809 struct hda_codec *codec,
4810 unsigned int stream_tag,
4811 unsigned int format,
4812 struct snd_pcm_substream *substream)
4813{
4814 struct hda_gen_spec *spec = codec->spec;
4815 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4816 stream_tag, format, substream);
4817}
1da177e4 4818
352f7f91
TI
4819static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4820 struct hda_codec *codec,
4821 struct snd_pcm_substream *substream)
4822{
4823 struct hda_gen_spec *spec = codec->spec;
4824 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4825}
4826
4827static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4828 struct hda_codec *codec,
4829 struct snd_pcm_substream *substream)
4830{
4831 struct hda_gen_spec *spec = codec->spec;
4832 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1da177e4
LT
4833}
4834
4835/*
352f7f91 4836 * Analog capture
1da177e4 4837 */
ac2e8736
TI
4838#define alt_capture_pcm_open capture_pcm_open
4839#define alt_capture_pcm_close capture_pcm_close
4840
352f7f91
TI
4841static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4842 struct hda_codec *codec,
4843 unsigned int stream_tag,
4844 unsigned int format,
4845 struct snd_pcm_substream *substream)
1da177e4 4846{
352f7f91 4847 struct hda_gen_spec *spec = codec->spec;
1da177e4 4848
352f7f91
TI
4849 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
4850 stream_tag, 0, format);
ac2e8736
TI
4851 call_pcm_capture_hook(hinfo, codec, substream,
4852 HDA_GEN_PCM_ACT_PREPARE);
352f7f91
TI
4853 return 0;
4854}
4855
4856static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4857 struct hda_codec *codec,
4858 struct snd_pcm_substream *substream)
4859{
4860 struct hda_gen_spec *spec = codec->spec;
1da177e4 4861
352f7f91
TI
4862 snd_hda_codec_cleanup_stream(codec,
4863 spec->adc_nids[substream->number + 1]);
ac2e8736
TI
4864 call_pcm_capture_hook(hinfo, codec, substream,
4865 HDA_GEN_PCM_ACT_CLEANUP);
1da177e4
LT
4866 return 0;
4867}
4868
4869/*
1da177e4 4870 */
352f7f91
TI
4871static const struct hda_pcm_stream pcm_analog_playback = {
4872 .substreams = 1,
4873 .channels_min = 2,
4874 .channels_max = 8,
4875 /* NID is set in build_pcms */
4876 .ops = {
4877 .open = playback_pcm_open,
38cf6f1a 4878 .close = playback_pcm_close,
352f7f91
TI
4879 .prepare = playback_pcm_prepare,
4880 .cleanup = playback_pcm_cleanup
4881 },
4882};
4883
4884static const struct hda_pcm_stream pcm_analog_capture = {
1da177e4
LT
4885 .substreams = 1,
4886 .channels_min = 2,
4887 .channels_max = 2,
352f7f91 4888 /* NID is set in build_pcms */
ac2e8736
TI
4889 .ops = {
4890 .open = capture_pcm_open,
4891 .close = capture_pcm_close,
4892 .prepare = capture_pcm_prepare,
4893 .cleanup = capture_pcm_cleanup
4894 },
1da177e4
LT
4895};
4896
352f7f91
TI
4897static const struct hda_pcm_stream pcm_analog_alt_playback = {
4898 .substreams = 1,
4899 .channels_min = 2,
4900 .channels_max = 2,
4901 /* NID is set in build_pcms */
38cf6f1a
TI
4902 .ops = {
4903 .open = alt_playback_pcm_open,
e6b85f3c
TI
4904 .close = alt_playback_pcm_close,
4905 .prepare = alt_playback_pcm_prepare,
4906 .cleanup = alt_playback_pcm_cleanup
38cf6f1a 4907 },
352f7f91
TI
4908};
4909
4910static const struct hda_pcm_stream pcm_analog_alt_capture = {
4911 .substreams = 2, /* can be overridden */
4912 .channels_min = 2,
4913 .channels_max = 2,
4914 /* NID is set in build_pcms */
4915 .ops = {
ac2e8736
TI
4916 .open = alt_capture_pcm_open,
4917 .close = alt_capture_pcm_close,
352f7f91
TI
4918 .prepare = alt_capture_pcm_prepare,
4919 .cleanup = alt_capture_pcm_cleanup
4920 },
4921};
4922
4923static const struct hda_pcm_stream pcm_digital_playback = {
4924 .substreams = 1,
4925 .channels_min = 2,
4926 .channels_max = 2,
4927 /* NID is set in build_pcms */
4928 .ops = {
4929 .open = dig_playback_pcm_open,
4930 .close = dig_playback_pcm_close,
4931 .prepare = dig_playback_pcm_prepare,
4932 .cleanup = dig_playback_pcm_cleanup
4933 },
4934};
4935
4936static const struct hda_pcm_stream pcm_digital_capture = {
4937 .substreams = 1,
4938 .channels_min = 2,
4939 .channels_max = 2,
4940 /* NID is set in build_pcms */
4941};
4942
4943/* Used by build_pcms to flag that a PCM has no playback stream */
4944static const struct hda_pcm_stream pcm_null_stream = {
4945 .substreams = 0,
4946 .channels_min = 0,
4947 .channels_max = 0,
4948};
4949
4950/*
4951 * dynamic changing ADC PCM streams
4952 */
4953static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
97ec558a 4954{
352f7f91
TI
4955 struct hda_gen_spec *spec = codec->spec;
4956 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4957
4958 if (spec->cur_adc && spec->cur_adc != new_adc) {
4959 /* stream is running, let's swap the current ADC */
4960 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4961 spec->cur_adc = new_adc;
4962 snd_hda_codec_setup_stream(codec, new_adc,
4963 spec->cur_adc_stream_tag, 0,
4964 spec->cur_adc_format);
4965 return true;
4966 }
4967 return false;
4968}
97ec558a 4969
352f7f91
TI
4970/* analog capture with dynamic dual-adc changes */
4971static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4972 struct hda_codec *codec,
4973 unsigned int stream_tag,
4974 unsigned int format,
4975 struct snd_pcm_substream *substream)
4976{
4977 struct hda_gen_spec *spec = codec->spec;
4978 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4979 spec->cur_adc_stream_tag = stream_tag;
4980 spec->cur_adc_format = format;
4981 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
97ec558a
TI
4982 return 0;
4983}
4984
352f7f91
TI
4985static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4986 struct hda_codec *codec,
4987 struct snd_pcm_substream *substream)
97ec558a 4988{
352f7f91
TI
4989 struct hda_gen_spec *spec = codec->spec;
4990 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4991 spec->cur_adc = 0;
97ec558a
TI
4992 return 0;
4993}
4994
352f7f91
TI
4995static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4996 .substreams = 1,
4997 .channels_min = 2,
4998 .channels_max = 2,
4999 .nid = 0, /* fill later */
5000 .ops = {
5001 .prepare = dyn_adc_capture_pcm_prepare,
5002 .cleanup = dyn_adc_capture_pcm_cleanup
5003 },
5004};
5005
f873e536
TI
5006static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5007 const char *chip_name)
5008{
5009 char *p;
5010
5011 if (*str)
5012 return;
5013 strlcpy(str, chip_name, len);
5014
5015 /* drop non-alnum chars after a space */
5016 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5017 if (!isalnum(p[1])) {
5018 *p = 0;
5019 break;
5020 }
5021 }
5022 strlcat(str, sfx, len);
5023}
5024
352f7f91
TI
5025/* build PCM streams based on the parsed results */
5026int snd_hda_gen_build_pcms(struct hda_codec *codec)
1da177e4 5027{
352f7f91
TI
5028 struct hda_gen_spec *spec = codec->spec;
5029 struct hda_pcm *info = spec->pcm_rec;
5030 const struct hda_pcm_stream *p;
5031 bool have_multi_adcs;
352f7f91
TI
5032
5033 codec->num_pcms = 1;
5034 codec->pcm_info = info;
5035
5036 if (spec->no_analog)
5037 goto skip_analog;
5038
f873e536
TI
5039 fill_pcm_stream_name(spec->stream_name_analog,
5040 sizeof(spec->stream_name_analog),
5041 " Analog", codec->chip_name);
352f7f91
TI
5042 info->name = spec->stream_name_analog;
5043
5044 if (spec->multiout.num_dacs > 0) {
5045 p = spec->stream_analog_playback;
5046 if (!p)
5047 p = &pcm_analog_playback;
5048 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5049 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
5050 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5051 spec->multiout.max_channels;
5052 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5053 spec->autocfg.line_outs == 2)
5054 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5055 snd_pcm_2_1_chmaps;
5056 }
5057 if (spec->num_adc_nids) {
5058 p = spec->stream_analog_capture;
5059 if (!p) {
5060 if (spec->dyn_adc_switch)
5061 p = &dyn_adc_pcm_analog_capture;
5062 else
5063 p = &pcm_analog_capture;
5064 }
5065 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5066 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
5067 }
5068
352f7f91
TI
5069 skip_analog:
5070 /* SPDIF for stream index #1 */
5071 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
f873e536
TI
5072 fill_pcm_stream_name(spec->stream_name_digital,
5073 sizeof(spec->stream_name_digital),
5074 " Digital", codec->chip_name);
352f7f91
TI
5075 codec->num_pcms = 2;
5076 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5077 info = spec->pcm_rec + 1;
5078 info->name = spec->stream_name_digital;
5079 if (spec->dig_out_type)
5080 info->pcm_type = spec->dig_out_type;
5081 else
5082 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5083 if (spec->multiout.dig_out_nid) {
5084 p = spec->stream_digital_playback;
5085 if (!p)
5086 p = &pcm_digital_playback;
5087 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5088 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
5089 }
5090 if (spec->dig_in_nid) {
5091 p = spec->stream_digital_capture;
5092 if (!p)
5093 p = &pcm_digital_capture;
5094 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5095 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
5096 }
5097 }
1da177e4 5098
352f7f91 5099 if (spec->no_analog)
1da177e4 5100 return 0;
352f7f91
TI
5101
5102 /* If the use of more than one ADC is requested for the current
5103 * model, configure a second analog capture-only PCM.
5104 */
5105 have_multi_adcs = (spec->num_adc_nids > 1) &&
5106 !spec->dyn_adc_switch && !spec->auto_mic;
5107 /* Additional Analaog capture for index #2 */
5108 if (spec->alt_dac_nid || have_multi_adcs) {
a607148f
TI
5109 fill_pcm_stream_name(spec->stream_name_alt_analog,
5110 sizeof(spec->stream_name_alt_analog),
5111 " Alt Analog", codec->chip_name);
352f7f91
TI
5112 codec->num_pcms = 3;
5113 info = spec->pcm_rec + 2;
a607148f 5114 info->name = spec->stream_name_alt_analog;
352f7f91
TI
5115 if (spec->alt_dac_nid) {
5116 p = spec->stream_analog_alt_playback;
5117 if (!p)
5118 p = &pcm_analog_alt_playback;
5119 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5120 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5121 spec->alt_dac_nid;
5122 } else {
5123 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5124 pcm_null_stream;
5125 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5126 }
5127 if (have_multi_adcs) {
5128 p = spec->stream_analog_alt_capture;
5129 if (!p)
5130 p = &pcm_analog_alt_capture;
5131 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5132 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5133 spec->adc_nids[1];
5134 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5135 spec->num_adc_nids - 1;
5136 } else {
5137 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5138 pcm_null_stream;
5139 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5140 }
1da177e4
LT
5141 }
5142
352f7f91
TI
5143 return 0;
5144}
2698ea98 5145EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
352f7f91
TI
5146
5147
5148/*
5149 * Standard auto-parser initializations
5150 */
5151
d4156930 5152/* configure the given path as a proper output */
2c12c30d 5153static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
352f7f91
TI
5154{
5155 struct nid_path *path;
d4156930 5156 hda_nid_t pin;
352f7f91 5157
196c1766 5158 path = snd_hda_get_path_from_idx(codec, path_idx);
d4156930 5159 if (!path || !path->depth)
352f7f91 5160 return;
d4156930 5161 pin = path->path[path->depth - 1];
2c12c30d 5162 restore_pin_ctl(codec, pin);
65033cc8
TI
5163 snd_hda_activate_path(codec, path, path->active,
5164 aamix_default(codec->spec));
e1284af7 5165 set_pin_eapd(codec, pin, path->active);
352f7f91
TI
5166}
5167
5168/* initialize primary output paths */
5169static void init_multi_out(struct hda_codec *codec)
5170{
5171 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
5172 int i;
5173
d4156930 5174 for (i = 0; i < spec->autocfg.line_outs; i++)
2c12c30d 5175 set_output_and_unmute(codec, spec->out_paths[i]);
352f7f91
TI
5176}
5177
db23fd19 5178
2c12c30d 5179static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
352f7f91 5180{
352f7f91 5181 int i;
352f7f91 5182
d4156930 5183 for (i = 0; i < num_outs; i++)
2c12c30d 5184 set_output_and_unmute(codec, paths[i]);
352f7f91
TI
5185}
5186
db23fd19
TI
5187/* initialize hp and speaker paths */
5188static void init_extra_out(struct hda_codec *codec)
5189{
5190 struct hda_gen_spec *spec = codec->spec;
5191
5192 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
2c12c30d 5193 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
db23fd19
TI
5194 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5195 __init_extra_out(codec, spec->autocfg.speaker_outs,
2c12c30d 5196 spec->speaker_paths);
db23fd19
TI
5197}
5198
352f7f91
TI
5199/* initialize multi-io paths */
5200static void init_multi_io(struct hda_codec *codec)
5201{
5202 struct hda_gen_spec *spec = codec->spec;
5203 int i;
5204
5205 for (i = 0; i < spec->multi_ios; i++) {
5206 hda_nid_t pin = spec->multi_io[i].pin;
5207 struct nid_path *path;
196c1766 5208 path = get_multiio_path(codec, i);
352f7f91
TI
5209 if (!path)
5210 continue;
5211 if (!spec->multi_io[i].ctl_in)
5212 spec->multi_io[i].ctl_in =
2c12c30d 5213 snd_hda_codec_get_pin_target(codec, pin);
65033cc8
TI
5214 snd_hda_activate_path(codec, path, path->active,
5215 aamix_default(spec));
352f7f91
TI
5216 }
5217}
5218
4f7f67fb
TI
5219static void init_aamix_paths(struct hda_codec *codec)
5220{
5221 struct hda_gen_spec *spec = codec->spec;
5222
5223 if (!spec->have_aamix_ctl)
5224 return;
5225 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5226 spec->aamix_out_paths[0],
5227 spec->autocfg.line_out_type);
5228 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5229 spec->aamix_out_paths[1],
5230 AUTO_PIN_HP_OUT);
5231 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5232 spec->aamix_out_paths[2],
5233 AUTO_PIN_SPEAKER_OUT);
5234}
5235
352f7f91
TI
5236/* set up input pins and loopback paths */
5237static void init_analog_input(struct hda_codec *codec)
5238{
5239 struct hda_gen_spec *spec = codec->spec;
5240 struct auto_pin_cfg *cfg = &spec->autocfg;
5241 int i;
5242
5243 for (i = 0; i < cfg->num_inputs; i++) {
5244 hda_nid_t nid = cfg->inputs[i].pin;
5245 if (is_input_pin(codec, nid))
2c12c30d 5246 restore_pin_ctl(codec, nid);
352f7f91
TI
5247
5248 /* init loopback inputs */
5249 if (spec->mixer_nid) {
3e367f15
TI
5250 resume_path_from_idx(codec, spec->loopback_paths[i]);
5251 resume_path_from_idx(codec, spec->loopback_merge_path);
352f7f91
TI
5252 }
5253 }
5254}
5255
5256/* initialize ADC paths */
5257static void init_input_src(struct hda_codec *codec)
5258{
5259 struct hda_gen_spec *spec = codec->spec;
5260 struct hda_input_mux *imux = &spec->input_mux;
5261 struct nid_path *path;
5262 int i, c, nums;
1da177e4 5263
352f7f91
TI
5264 if (spec->dyn_adc_switch)
5265 nums = 1;
5266 else
5267 nums = spec->num_adc_nids;
5268
5269 for (c = 0; c < nums; c++) {
5270 for (i = 0; i < imux->num_items; i++) {
c697b716 5271 path = get_input_path(codec, c, i);
352f7f91
TI
5272 if (path) {
5273 bool active = path->active;
5274 if (i == spec->cur_mux[c])
5275 active = true;
5276 snd_hda_activate_path(codec, path, active, false);
5277 }
97ec558a 5278 }
967303da
TI
5279 if (spec->hp_mic)
5280 update_hp_mic(codec, c, true);
1da177e4 5281 }
352f7f91 5282
352f7f91 5283 if (spec->cap_sync_hook)
7fe30711 5284 spec->cap_sync_hook(codec, NULL, NULL);
352f7f91
TI
5285}
5286
5287/* set right pin controls for digital I/O */
5288static void init_digital(struct hda_codec *codec)
5289{
5290 struct hda_gen_spec *spec = codec->spec;
5291 int i;
5292 hda_nid_t pin;
5293
d4156930 5294 for (i = 0; i < spec->autocfg.dig_outs; i++)
2c12c30d 5295 set_output_and_unmute(codec, spec->digout_paths[i]);
352f7f91 5296 pin = spec->autocfg.dig_in_pin;
2430d7b7 5297 if (pin) {
2c12c30d 5298 restore_pin_ctl(codec, pin);
3e367f15 5299 resume_path_from_idx(codec, spec->digin_path);
2430d7b7 5300 }
352f7f91
TI
5301}
5302
973e4972
TI
5303/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5304 * invalid unsol tags by some reason
5305 */
5306static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5307{
5308 int i;
5309
5310 for (i = 0; i < codec->init_pins.used; i++) {
5311 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5312 hda_nid_t nid = pin->nid;
5313 if (is_jack_detectable(codec, nid) &&
5314 !snd_hda_jack_tbl_get(codec, nid))
5315 snd_hda_codec_update_cache(codec, nid, 0,
5316 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5317 }
5318}
5319
5187ac16
TI
5320/*
5321 * initialize the generic spec;
5322 * this can be put as patch_ops.init function
5323 */
352f7f91
TI
5324int snd_hda_gen_init(struct hda_codec *codec)
5325{
5326 struct hda_gen_spec *spec = codec->spec;
5327
5328 if (spec->init_hook)
5329 spec->init_hook(codec);
5330
5331 snd_hda_apply_verbs(codec);
5332
3bbcd274
TI
5333 codec->cached_write = 1;
5334
352f7f91
TI
5335 init_multi_out(codec);
5336 init_extra_out(codec);
5337 init_multi_io(codec);
4f7f67fb 5338 init_aamix_paths(codec);
352f7f91
TI
5339 init_analog_input(codec);
5340 init_input_src(codec);
5341 init_digital(codec);
1da177e4 5342
973e4972
TI
5343 clear_unsol_on_unused_pins(codec);
5344
352f7f91 5345 /* call init functions of standard auto-mute helpers */
a5cc2509 5346 update_automute_all(codec);
352f7f91 5347
dc870f38 5348 snd_hda_codec_flush_cache(codec);
3bbcd274 5349
352f7f91
TI
5350 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5351 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5352
5353 hda_call_check_power_status(codec, 0x01);
1da177e4
LT
5354 return 0;
5355}
2698ea98 5356EXPORT_SYMBOL_GPL(snd_hda_gen_init);
352f7f91 5357
5187ac16
TI
5358/*
5359 * free the generic spec;
5360 * this can be put as patch_ops.free function
5361 */
fce52a3b
TI
5362void snd_hda_gen_free(struct hda_codec *codec)
5363{
8a02c0cc 5364 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
fce52a3b
TI
5365 snd_hda_gen_spec_free(codec->spec);
5366 kfree(codec->spec);
5367 codec->spec = NULL;
5368}
2698ea98 5369EXPORT_SYMBOL_GPL(snd_hda_gen_free);
1da177e4 5370
83012a7c 5371#ifdef CONFIG_PM
5187ac16
TI
5372/*
5373 * check the loopback power save state;
5374 * this can be put as patch_ops.check_power_status function
5375 */
fce52a3b 5376int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
cb53c626 5377{
352f7f91 5378 struct hda_gen_spec *spec = codec->spec;
cb53c626
TI
5379 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5380}
2698ea98 5381EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
cb53c626
TI
5382#endif
5383
fce52a3b
TI
5384
5385/*
5386 * the generic codec support
5387 */
1da177e4 5388
352f7f91
TI
5389static const struct hda_codec_ops generic_patch_ops = {
5390 .build_controls = snd_hda_gen_build_controls,
5391 .build_pcms = snd_hda_gen_build_pcms,
5392 .init = snd_hda_gen_init,
fce52a3b 5393 .free = snd_hda_gen_free,
352f7f91 5394 .unsol_event = snd_hda_jack_unsol_event,
83012a7c 5395#ifdef CONFIG_PM
fce52a3b 5396 .check_power_status = snd_hda_gen_check_power_status,
cb53c626 5397#endif
1da177e4
LT
5398};
5399
1da177e4
LT
5400int snd_hda_parse_generic_codec(struct hda_codec *codec)
5401{
352f7f91 5402 struct hda_gen_spec *spec;
1da177e4
LT
5403 int err;
5404
e560d8d8 5405 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
352f7f91 5406 if (!spec)
1da177e4 5407 return -ENOMEM;
352f7f91 5408 snd_hda_gen_spec_init(spec);
1da177e4 5409 codec->spec = spec;
1da177e4 5410
9eb413e5
TI
5411 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5412 if (err < 0)
5413 return err;
5414
5415 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
352f7f91 5416 if (err < 0)
1da177e4
LT
5417 goto error;
5418
5419 codec->patch_ops = generic_patch_ops;
1da177e4
LT
5420 return 0;
5421
352f7f91 5422error:
fce52a3b 5423 snd_hda_gen_free(codec);
1da177e4
LT
5424 return err;
5425}
2698ea98 5426EXPORT_SYMBOL_GPL(snd_hda_parse_generic_codec);
b21bdd0d
TI
5427
5428MODULE_LICENSE("GPL");
5429MODULE_DESCRIPTION("Generic HD-audio codec parser");
This page took 1.262493 seconds and 5 git commands to generate.