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