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