xref: /linux/sound/hda/codecs/generic.c (revision 59e6295fac26b8e85c1ea859cdd89fa1e47519d7)
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_write_sync(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 only 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 	unsigned int val;
1988 	int best_badness = INT_MAX;
1989 	int badness;
1990 	bool fill_hardwired = true, fill_mio_first = true;
1991 	bool best_wired = true, best_mio = true;
1992 	bool hp_spk_swapped = false;
1993 	struct auto_pin_cfg *best_cfg __free(kfree) =
1994 		kmalloc_obj(*best_cfg);
1995 
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 void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2699 			       char *name, size_t name_len)
2700 {
2701 	struct hda_gen_spec *spec = codec->spec;
2702 
2703 	snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len);
2704 	hda_append_suffix(name, " Jack Mode", name_len);
2705 }
2706 
2707 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2708 {
2709 	struct hda_gen_spec *spec = codec->spec;
2710 	if (spec->add_jack_modes) {
2711 		unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2712 		if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2713 			return 2;
2714 	}
2715 	return 1;
2716 }
2717 
2718 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2719 				 hda_nid_t *pins)
2720 {
2721 	struct hda_gen_spec *spec = codec->spec;
2722 	int i;
2723 
2724 	for (i = 0; i < num_pins; i++) {
2725 		hda_nid_t pin = pins[i];
2726 		if (pin == spec->hp_mic_pin)
2727 			continue;
2728 		if (get_out_jack_num_items(codec, pin) > 1) {
2729 			struct snd_kcontrol_new *knew;
2730 			char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2731 			get_jack_mode_name(codec, pin, name, sizeof(name));
2732 			knew = snd_hda_gen_add_kctl(spec, name,
2733 						    &out_jack_mode_enum);
2734 			if (!knew)
2735 				return -ENOMEM;
2736 			knew->private_value = pin;
2737 		}
2738 	}
2739 
2740 	return 0;
2741 }
2742 
2743 /*
2744  * input jack mode
2745  */
2746 
2747 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2748 #define NUM_VREFS	6
2749 
2750 static const char * const vref_texts[NUM_VREFS] = {
2751 	"Line In", "Mic 50pc Bias", "Mic 0V Bias",
2752 	"", "Mic 80pc Bias", "Mic 100pc Bias"
2753 };
2754 
2755 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2756 {
2757 	unsigned int pincap;
2758 
2759 	pincap = snd_hda_query_pin_caps(codec, pin);
2760 	pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2761 	/* filter out unusual vrefs */
2762 	pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2763 	return pincap;
2764 }
2765 
2766 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2767 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2768 {
2769 	unsigned int i, n = 0;
2770 
2771 	for (i = 0; i < NUM_VREFS; i++) {
2772 		if (vref_caps & (1 << i)) {
2773 			if (n == item_idx)
2774 				return i;
2775 			n++;
2776 		}
2777 	}
2778 	return 0;
2779 }
2780 
2781 /* convert back from the vref ctl index to the enum item index */
2782 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2783 {
2784 	unsigned int i, n = 0;
2785 
2786 	for (i = 0; i < NUM_VREFS; i++) {
2787 		if (i == idx)
2788 			return n;
2789 		if (vref_caps & (1 << i))
2790 			n++;
2791 	}
2792 	return 0;
2793 }
2794 
2795 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2796 			     struct snd_ctl_elem_info *uinfo)
2797 {
2798 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2799 	hda_nid_t nid = kcontrol->private_value;
2800 	unsigned int vref_caps = get_vref_caps(codec, nid);
2801 
2802 	snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2803 				 vref_texts);
2804 	/* set the right text */
2805 	strscpy(uinfo->value.enumerated.name,
2806 	       vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2807 	return 0;
2808 }
2809 
2810 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2811 			    struct snd_ctl_elem_value *ucontrol)
2812 {
2813 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2814 	hda_nid_t nid = kcontrol->private_value;
2815 	unsigned int vref_caps = get_vref_caps(codec, nid);
2816 	unsigned int idx;
2817 
2818 	idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2819 	ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2820 	return 0;
2821 }
2822 
2823 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2824 			    struct snd_ctl_elem_value *ucontrol)
2825 {
2826 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2827 	hda_nid_t nid = kcontrol->private_value;
2828 	unsigned int vref_caps = get_vref_caps(codec, nid);
2829 	unsigned int val, idx;
2830 
2831 	val = snd_hda_codec_get_pin_target(codec, nid);
2832 	idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2833 	if (idx == ucontrol->value.enumerated.item[0])
2834 		return 0;
2835 
2836 	val &= ~AC_PINCTL_VREFEN;
2837 	val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2838 	snd_hda_set_pin_ctl_cache(codec, nid, val);
2839 	return 1;
2840 }
2841 
2842 static const struct snd_kcontrol_new in_jack_mode_enum = {
2843 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2844 	.info = in_jack_mode_info,
2845 	.get = in_jack_mode_get,
2846 	.put = in_jack_mode_put,
2847 };
2848 
2849 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2850 {
2851 	struct hda_gen_spec *spec = codec->spec;
2852 	int nitems = 0;
2853 	if (spec->add_jack_modes)
2854 		nitems = hweight32(get_vref_caps(codec, pin));
2855 	return nitems ? nitems : 1;
2856 }
2857 
2858 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2859 {
2860 	struct hda_gen_spec *spec = codec->spec;
2861 	struct snd_kcontrol_new *knew;
2862 	char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2863 	unsigned int defcfg;
2864 
2865 	if (pin == spec->hp_mic_pin)
2866 		return 0; /* already done in create_out_jack_mode() */
2867 
2868 	/* no jack mode for fixed pins */
2869 	defcfg = snd_hda_codec_get_pincfg(codec, pin);
2870 	if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2871 		return 0;
2872 
2873 	/* no multiple vref caps? */
2874 	if (get_in_jack_num_items(codec, pin) <= 1)
2875 		return 0;
2876 
2877 	get_jack_mode_name(codec, pin, name, sizeof(name));
2878 	knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2879 	if (!knew)
2880 		return -ENOMEM;
2881 	knew->private_value = pin;
2882 	return 0;
2883 }
2884 
2885 /*
2886  * HP/mic shared jack mode
2887  */
2888 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2889 				 struct snd_ctl_elem_info *uinfo)
2890 {
2891 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2892 	hda_nid_t nid = kcontrol->private_value;
2893 	int out_jacks = get_out_jack_num_items(codec, nid);
2894 	int in_jacks = get_in_jack_num_items(codec, nid);
2895 	const char *text = NULL;
2896 	int idx;
2897 
2898 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2899 	uinfo->count = 1;
2900 	uinfo->value.enumerated.items = out_jacks + in_jacks;
2901 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2902 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2903 	idx = uinfo->value.enumerated.item;
2904 	if (idx < out_jacks) {
2905 		if (out_jacks > 1)
2906 			text = out_jack_texts[idx];
2907 		else
2908 			text = "Headphone Out";
2909 	} else {
2910 		idx -= out_jacks;
2911 		if (in_jacks > 1) {
2912 			unsigned int vref_caps = get_vref_caps(codec, nid);
2913 			text = vref_texts[get_vref_idx(vref_caps, idx)];
2914 		} else
2915 			text = "Mic In";
2916 	}
2917 
2918 	strscpy(uinfo->value.enumerated.name, text);
2919 	return 0;
2920 }
2921 
2922 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2923 {
2924 	int out_jacks = get_out_jack_num_items(codec, nid);
2925 	int in_jacks = get_in_jack_num_items(codec, nid);
2926 	unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2927 	int idx = 0;
2928 
2929 	if (val & PIN_OUT) {
2930 		if (out_jacks > 1 && val == PIN_HP)
2931 			idx = 1;
2932 	} else if (val & PIN_IN) {
2933 		idx = out_jacks;
2934 		if (in_jacks > 1) {
2935 			unsigned int vref_caps = get_vref_caps(codec, nid);
2936 			val &= AC_PINCTL_VREFEN;
2937 			idx += cvt_from_vref_idx(vref_caps, val);
2938 		}
2939 	}
2940 	return idx;
2941 }
2942 
2943 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2944 				struct snd_ctl_elem_value *ucontrol)
2945 {
2946 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2947 	hda_nid_t nid = kcontrol->private_value;
2948 	ucontrol->value.enumerated.item[0] =
2949 		get_cur_hp_mic_jack_mode(codec, nid);
2950 	return 0;
2951 }
2952 
2953 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2954 				struct snd_ctl_elem_value *ucontrol)
2955 {
2956 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2957 	hda_nid_t nid = kcontrol->private_value;
2958 	int out_jacks = get_out_jack_num_items(codec, nid);
2959 	int in_jacks = get_in_jack_num_items(codec, nid);
2960 	unsigned int val, oldval, idx;
2961 
2962 	oldval = get_cur_hp_mic_jack_mode(codec, nid);
2963 	idx = ucontrol->value.enumerated.item[0];
2964 	if (oldval == idx)
2965 		return 0;
2966 
2967 	if (idx < out_jacks) {
2968 		if (out_jacks > 1)
2969 			val = idx ? PIN_HP : PIN_OUT;
2970 		else
2971 			val = PIN_HP;
2972 	} else {
2973 		idx -= out_jacks;
2974 		if (in_jacks > 1) {
2975 			unsigned int vref_caps = get_vref_caps(codec, nid);
2976 			val = snd_hda_codec_get_pin_target(codec, nid);
2977 			val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2978 			val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2979 		} else
2980 			val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
2981 	}
2982 	snd_hda_set_pin_ctl_cache(codec, nid, val);
2983 	call_hp_automute(codec, NULL);
2984 
2985 	return 1;
2986 }
2987 
2988 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2989 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2990 	.info = hp_mic_jack_mode_info,
2991 	.get = hp_mic_jack_mode_get,
2992 	.put = hp_mic_jack_mode_put,
2993 };
2994 
2995 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2996 {
2997 	struct hda_gen_spec *spec = codec->spec;
2998 	struct snd_kcontrol_new *knew;
2999 
3000 	knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
3001 				    &hp_mic_jack_mode_enum);
3002 	if (!knew)
3003 		return -ENOMEM;
3004 	knew->private_value = pin;
3005 	spec->hp_mic_jack_modes = 1;
3006 	return 0;
3007 }
3008 
3009 /*
3010  * Parse input paths
3011  */
3012 
3013 /* add the powersave loopback-list entry */
3014 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
3015 {
3016 	struct hda_amp_list *list;
3017 
3018 	list = snd_array_new(&spec->loopback_list);
3019 	if (!list)
3020 		return -ENOMEM;
3021 	list->nid = mix;
3022 	list->dir = HDA_INPUT;
3023 	list->idx = idx;
3024 	spec->loopback.amplist = spec->loopback_list.list;
3025 	return 0;
3026 }
3027 
3028 /* return true if either a volume or a mute amp is found for the given
3029  * aamix path; the amp has to be either in the mixer node or its direct leaf
3030  */
3031 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3032 				   hda_nid_t pin, unsigned int *mix_val,
3033 				   unsigned int *mute_val)
3034 {
3035 	int idx, num_conns;
3036 	const hda_nid_t *list;
3037 	hda_nid_t nid;
3038 
3039 	idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3040 	if (idx < 0)
3041 		return false;
3042 
3043 	*mix_val = *mute_val = 0;
3044 	if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3045 		*mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3046 	if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3047 		*mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3048 	if (*mix_val && *mute_val)
3049 		return true;
3050 
3051 	/* check leaf node */
3052 	num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3053 	if (num_conns < idx)
3054 		return false;
3055 	nid = list[idx];
3056 	if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3057 	    !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
3058 		*mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3059 	if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3060 	    !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
3061 		*mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3062 
3063 	return *mix_val || *mute_val;
3064 }
3065 
3066 /* create input playback/capture controls for the given pin */
3067 static int new_analog_input(struct hda_codec *codec, int input_idx,
3068 			    hda_nid_t pin, const char *ctlname, int ctlidx,
3069 			    hda_nid_t mix_nid)
3070 {
3071 	struct hda_gen_spec *spec = codec->spec;
3072 	struct nid_path *path;
3073 	unsigned int mix_val, mute_val;
3074 	int err, idx;
3075 
3076 	if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3077 		return 0;
3078 
3079 	path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3080 	if (!path)
3081 		return -EINVAL;
3082 	print_nid_path(codec, "loopback", path);
3083 	spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3084 
3085 	idx = path->idx[path->depth - 1];
3086 	if (mix_val) {
3087 		err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3088 		if (err < 0)
3089 			return err;
3090 		path->ctls[NID_PATH_VOL_CTL] = mix_val;
3091 	}
3092 
3093 	if (mute_val) {
3094 		err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3095 		if (err < 0)
3096 			return err;
3097 		path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3098 	}
3099 
3100 	path->active = true;
3101 	path->stream_enabled = true; /* no DAC/ADC involved */
3102 	err = add_loopback_list(spec, mix_nid, idx);
3103 	if (err < 0)
3104 		return err;
3105 
3106 	if (spec->mixer_nid != spec->mixer_merge_nid &&
3107 	    !spec->loopback_merge_path) {
3108 		path = snd_hda_add_new_path(codec, spec->mixer_nid,
3109 					    spec->mixer_merge_nid, 0);
3110 		if (path) {
3111 			print_nid_path(codec, "loopback-merge", path);
3112 			path->active = true;
3113 			path->pin_fixed = true; /* static route */
3114 			path->stream_enabled = true; /* no DAC/ADC involved */
3115 			spec->loopback_merge_path =
3116 				snd_hda_get_path_idx(codec, path);
3117 		}
3118 	}
3119 
3120 	return 0;
3121 }
3122 
3123 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3124 {
3125 	unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3126 	return (pincap & AC_PINCAP_IN) != 0;
3127 }
3128 
3129 /* Parse the codec tree and retrieve ADCs */
3130 static int fill_adc_nids(struct hda_codec *codec)
3131 {
3132 	struct hda_gen_spec *spec = codec->spec;
3133 	hda_nid_t nid;
3134 	hda_nid_t *adc_nids = spec->adc_nids;
3135 	int max_nums = ARRAY_SIZE(spec->adc_nids);
3136 	int nums = 0;
3137 
3138 	for_each_hda_codec_node(nid, codec) {
3139 		unsigned int caps = get_wcaps(codec, nid);
3140 		int type = get_wcaps_type(caps);
3141 
3142 		if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3143 			continue;
3144 		adc_nids[nums] = nid;
3145 		if (++nums >= max_nums)
3146 			break;
3147 	}
3148 	spec->num_adc_nids = nums;
3149 
3150 	/* copy the detected ADCs to all_adcs[] */
3151 	spec->num_all_adcs = nums;
3152 	memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3153 
3154 	return nums;
3155 }
3156 
3157 /* filter out invalid adc_nids that don't give all active input pins;
3158  * if needed, check whether dynamic ADC-switching is available
3159  */
3160 static int check_dyn_adc_switch(struct hda_codec *codec)
3161 {
3162 	struct hda_gen_spec *spec = codec->spec;
3163 	struct hda_input_mux *imux = &spec->input_mux;
3164 	unsigned int ok_bits;
3165 	int i, n, nums;
3166 
3167 	nums = 0;
3168 	ok_bits = 0;
3169 	for (n = 0; n < spec->num_adc_nids; n++) {
3170 		for (i = 0; i < imux->num_items; i++) {
3171 			if (!spec->input_paths[i][n])
3172 				break;
3173 		}
3174 		if (i >= imux->num_items) {
3175 			ok_bits |= (1 << n);
3176 			nums++;
3177 		}
3178 	}
3179 
3180 	if (!ok_bits) {
3181 		/* check whether ADC-switch is possible */
3182 		for (i = 0; i < imux->num_items; i++) {
3183 			for (n = 0; n < spec->num_adc_nids; n++) {
3184 				if (spec->input_paths[i][n]) {
3185 					spec->dyn_adc_idx[i] = n;
3186 					break;
3187 				}
3188 			}
3189 		}
3190 
3191 		codec_dbg(codec, "enabling ADC switching\n");
3192 		spec->dyn_adc_switch = 1;
3193 	} else if (nums != spec->num_adc_nids) {
3194 		/* shrink the invalid adcs and input paths */
3195 		nums = 0;
3196 		for (n = 0; n < spec->num_adc_nids; n++) {
3197 			if (!(ok_bits & (1 << n)))
3198 				continue;
3199 			if (n != nums) {
3200 				spec->adc_nids[nums] = spec->adc_nids[n];
3201 				for (i = 0; i < imux->num_items; i++) {
3202 					invalidate_nid_path(codec,
3203 						spec->input_paths[i][nums]);
3204 					spec->input_paths[i][nums] =
3205 						spec->input_paths[i][n];
3206 					spec->input_paths[i][n] = 0;
3207 				}
3208 			}
3209 			nums++;
3210 		}
3211 		spec->num_adc_nids = nums;
3212 	}
3213 
3214 	if (imux->num_items == 1 ||
3215 	    (imux->num_items == 2 && spec->hp_mic)) {
3216 		codec_dbg(codec, "reducing to a single ADC\n");
3217 		spec->num_adc_nids = 1; /* reduce to a single ADC */
3218 	}
3219 
3220 	/* single index for individual volumes ctls */
3221 	if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3222 		spec->num_adc_nids = 1;
3223 
3224 	return 0;
3225 }
3226 
3227 /* parse capture source paths from the given pin and create imux items */
3228 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3229 				int cfg_idx, int num_adcs,
3230 				const char *label, int anchor)
3231 {
3232 	struct hda_gen_spec *spec = codec->spec;
3233 	struct hda_input_mux *imux = &spec->input_mux;
3234 	int imux_idx = imux->num_items;
3235 	bool imux_added = false;
3236 	int c;
3237 
3238 	for (c = 0; c < num_adcs; c++) {
3239 		struct nid_path *path;
3240 		hda_nid_t adc = spec->adc_nids[c];
3241 
3242 		if (!is_reachable_path(codec, pin, adc))
3243 			continue;
3244 		path = snd_hda_add_new_path(codec, pin, adc, anchor);
3245 		if (!path)
3246 			continue;
3247 		print_nid_path(codec, "input", path);
3248 		spec->input_paths[imux_idx][c] =
3249 			snd_hda_get_path_idx(codec, path);
3250 
3251 		if (!imux_added) {
3252 			if (spec->hp_mic_pin == pin)
3253 				spec->hp_mic_mux_idx = imux->num_items;
3254 			spec->imux_pins[imux->num_items] = pin;
3255 			snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3256 			imux_added = true;
3257 			if (spec->dyn_adc_switch)
3258 				spec->dyn_adc_idx[imux_idx] = c;
3259 		}
3260 	}
3261 
3262 	return 0;
3263 }
3264 
3265 /*
3266  * create playback/capture controls for input pins
3267  */
3268 
3269 /* fill the label for each input at first */
3270 static int fill_input_pin_labels(struct hda_codec *codec)
3271 {
3272 	struct hda_gen_spec *spec = codec->spec;
3273 	const struct auto_pin_cfg *cfg = &spec->autocfg;
3274 	int i;
3275 
3276 	for (i = 0; i < cfg->num_inputs; i++) {
3277 		hda_nid_t pin = cfg->inputs[i].pin;
3278 		const char *label;
3279 		int j, idx;
3280 
3281 		if (!is_input_pin(codec, pin))
3282 			continue;
3283 
3284 		label = hda_get_autocfg_input_label(codec, cfg, i);
3285 		idx = 0;
3286 		for (j = i - 1; j >= 0; j--) {
3287 			if (spec->input_labels[j] &&
3288 			    !strcmp(spec->input_labels[j], label)) {
3289 				idx = spec->input_label_idxs[j] + 1;
3290 				break;
3291 			}
3292 		}
3293 
3294 		spec->input_labels[i] = label;
3295 		spec->input_label_idxs[i] = idx;
3296 	}
3297 
3298 	return 0;
3299 }
3300 
3301 #define CFG_IDX_MIX	99	/* a dummy cfg->input idx for stereo mix */
3302 
3303 static int create_input_ctls(struct hda_codec *codec)
3304 {
3305 	struct hda_gen_spec *spec = codec->spec;
3306 	const struct auto_pin_cfg *cfg = &spec->autocfg;
3307 	hda_nid_t mixer = spec->mixer_nid;
3308 	int num_adcs;
3309 	int i, err;
3310 	unsigned int val;
3311 
3312 	num_adcs = fill_adc_nids(codec);
3313 	if (num_adcs < 0)
3314 		return 0;
3315 
3316 	err = fill_input_pin_labels(codec);
3317 	if (err < 0)
3318 		return err;
3319 
3320 	for (i = 0; i < cfg->num_inputs; i++) {
3321 		hda_nid_t pin;
3322 
3323 		pin = cfg->inputs[i].pin;
3324 		if (!is_input_pin(codec, pin))
3325 			continue;
3326 
3327 		val = PIN_IN;
3328 		if (cfg->inputs[i].type == AUTO_PIN_MIC)
3329 			val |= snd_hda_get_default_vref(codec, pin);
3330 		if (pin != spec->hp_mic_pin &&
3331 		    !snd_hda_codec_get_pin_target(codec, pin))
3332 			set_pin_target(codec, pin, val, false);
3333 
3334 		if (mixer) {
3335 			if (is_reachable_path(codec, pin, mixer)) {
3336 				err = new_analog_input(codec, i, pin,
3337 						       spec->input_labels[i],
3338 						       spec->input_label_idxs[i],
3339 						       mixer);
3340 				if (err < 0)
3341 					return err;
3342 			}
3343 		}
3344 
3345 		err = parse_capture_source(codec, pin, i, num_adcs,
3346 					   spec->input_labels[i], -mixer);
3347 		if (err < 0)
3348 			return err;
3349 
3350 		if (spec->add_jack_modes) {
3351 			err = create_in_jack_mode(codec, pin);
3352 			if (err < 0)
3353 				return err;
3354 		}
3355 	}
3356 
3357 	/* add stereo mix when explicitly enabled via hint */
3358 	if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3359 		err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3360 					   "Stereo Mix", 0);
3361 		if (err < 0)
3362 			return err;
3363 		else
3364 			spec->suppress_auto_mic = 1;
3365 	}
3366 
3367 	return 0;
3368 }
3369 
3370 
3371 /*
3372  * input source mux
3373  */
3374 
3375 /* get the input path specified by the given adc and imux indices */
3376 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3377 {
3378 	struct hda_gen_spec *spec = codec->spec;
3379 	if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3380 		snd_BUG();
3381 		return NULL;
3382 	}
3383 	if (spec->dyn_adc_switch)
3384 		adc_idx = spec->dyn_adc_idx[imux_idx];
3385 	if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3386 		snd_BUG();
3387 		return NULL;
3388 	}
3389 	return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3390 }
3391 
3392 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3393 		      unsigned int idx);
3394 
3395 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3396 			 struct snd_ctl_elem_info *uinfo)
3397 {
3398 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3399 	struct hda_gen_spec *spec = codec->spec;
3400 	return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3401 }
3402 
3403 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3404 			struct snd_ctl_elem_value *ucontrol)
3405 {
3406 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3407 	struct hda_gen_spec *spec = codec->spec;
3408 	/* the ctls are created at once with multiple counts */
3409 	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3410 
3411 	ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3412 	return 0;
3413 }
3414 
3415 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3416 			    struct snd_ctl_elem_value *ucontrol)
3417 {
3418 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3419 	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3420 	return mux_select(codec, adc_idx,
3421 			  ucontrol->value.enumerated.item[0]);
3422 }
3423 
3424 static const struct snd_kcontrol_new cap_src_temp = {
3425 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3426 	.name = "Input Source",
3427 	.info = mux_enum_info,
3428 	.get = mux_enum_get,
3429 	.put = mux_enum_put,
3430 };
3431 
3432 /*
3433  * capture volume and capture switch ctls
3434  */
3435 
3436 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3437 			  struct snd_ctl_elem_value *ucontrol);
3438 
3439 /* call the given amp update function for all amps in the imux list at once */
3440 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3441 			  struct snd_ctl_elem_value *ucontrol,
3442 			  put_call_t func, int type)
3443 {
3444 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3445 	struct hda_gen_spec *spec = codec->spec;
3446 	const struct hda_input_mux *imux;
3447 	struct nid_path *path;
3448 	int i, adc_idx, ret, err = 0;
3449 
3450 	imux = &spec->input_mux;
3451 	adc_idx = kcontrol->id.index;
3452 	scoped_guard(mutex, &codec->control_mutex) {
3453 		for (i = 0; i < imux->num_items; i++) {
3454 			path = get_input_path(codec, adc_idx, i);
3455 			if (!path || !path->ctls[type])
3456 				continue;
3457 			kcontrol->private_value = path->ctls[type];
3458 			ret = func(kcontrol, ucontrol);
3459 			if (ret < 0)
3460 				return ret;
3461 			if (ret > 0)
3462 				err = 1;
3463 		}
3464 	}
3465 	if (spec->cap_sync_hook)
3466 		spec->cap_sync_hook(codec, kcontrol, ucontrol);
3467 	return err;
3468 }
3469 
3470 /* capture volume ctl callbacks */
3471 #define cap_vol_info		snd_hda_mixer_amp_volume_info
3472 #define cap_vol_get		snd_hda_mixer_amp_volume_get
3473 #define cap_vol_tlv		snd_hda_mixer_amp_tlv
3474 
3475 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3476 		       struct snd_ctl_elem_value *ucontrol)
3477 {
3478 	return cap_put_caller(kcontrol, ucontrol,
3479 			      snd_hda_mixer_amp_volume_put,
3480 			      NID_PATH_VOL_CTL);
3481 }
3482 
3483 static const struct snd_kcontrol_new cap_vol_temp = {
3484 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3485 	.name = "Capture Volume",
3486 	.access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3487 		   SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3488 		   SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3489 	.info = cap_vol_info,
3490 	.get = cap_vol_get,
3491 	.put = cap_vol_put,
3492 	.tlv = { .c = cap_vol_tlv },
3493 };
3494 
3495 /* capture switch ctl callbacks */
3496 #define cap_sw_info		snd_ctl_boolean_stereo_info
3497 #define cap_sw_get		snd_hda_mixer_amp_switch_get
3498 
3499 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3500 		      struct snd_ctl_elem_value *ucontrol)
3501 {
3502 	return cap_put_caller(kcontrol, ucontrol,
3503 			      snd_hda_mixer_amp_switch_put,
3504 			      NID_PATH_MUTE_CTL);
3505 }
3506 
3507 static const struct snd_kcontrol_new cap_sw_temp = {
3508 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3509 	.name = "Capture Switch",
3510 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3511 	.info = cap_sw_info,
3512 	.get = cap_sw_get,
3513 	.put = cap_sw_put,
3514 };
3515 
3516 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3517 {
3518 	hda_nid_t nid;
3519 	int i, depth;
3520 
3521 	path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3522 	for (depth = 0; depth < 3; depth++) {
3523 		if (depth >= path->depth)
3524 			return -EINVAL;
3525 		i = path->depth - depth - 1;
3526 		nid = path->path[i];
3527 		if (!path->ctls[NID_PATH_VOL_CTL]) {
3528 			if (nid_has_volume(codec, nid, HDA_OUTPUT))
3529 				path->ctls[NID_PATH_VOL_CTL] =
3530 					HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3531 			else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3532 				int idx = path->idx[i];
3533 				if (!depth && codec->single_adc_amp)
3534 					idx = 0;
3535 				path->ctls[NID_PATH_VOL_CTL] =
3536 					HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3537 			}
3538 		}
3539 		if (!path->ctls[NID_PATH_MUTE_CTL]) {
3540 			if (nid_has_mute(codec, nid, HDA_OUTPUT))
3541 				path->ctls[NID_PATH_MUTE_CTL] =
3542 					HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3543 			else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3544 				int idx = path->idx[i];
3545 				if (!depth && codec->single_adc_amp)
3546 					idx = 0;
3547 				path->ctls[NID_PATH_MUTE_CTL] =
3548 					HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3549 			}
3550 		}
3551 	}
3552 	return 0;
3553 }
3554 
3555 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3556 {
3557 	struct hda_gen_spec *spec = codec->spec;
3558 	struct auto_pin_cfg *cfg = &spec->autocfg;
3559 	unsigned int val;
3560 	int i;
3561 
3562 	if (!spec->inv_dmic_split)
3563 		return false;
3564 	for (i = 0; i < cfg->num_inputs; i++) {
3565 		if (cfg->inputs[i].pin != nid)
3566 			continue;
3567 		if (cfg->inputs[i].type != AUTO_PIN_MIC)
3568 			return false;
3569 		val = snd_hda_codec_get_pincfg(codec, nid);
3570 		return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3571 	}
3572 	return false;
3573 }
3574 
3575 /* capture switch put callback for a single control with hook call */
3576 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3577 			     struct snd_ctl_elem_value *ucontrol)
3578 {
3579 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3580 	struct hda_gen_spec *spec = codec->spec;
3581 	int ret;
3582 
3583 	ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3584 	if (ret < 0)
3585 		return ret;
3586 
3587 	if (spec->cap_sync_hook)
3588 		spec->cap_sync_hook(codec, kcontrol, ucontrol);
3589 
3590 	return ret;
3591 }
3592 
3593 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3594 			      int idx, bool is_switch, unsigned int ctl,
3595 			      bool inv_dmic)
3596 {
3597 	struct hda_gen_spec *spec = codec->spec;
3598 	char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3599 	int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3600 	const char *sfx = is_switch ? "Switch" : "Volume";
3601 	unsigned int chs = inv_dmic ? 1 : 3;
3602 	struct snd_kcontrol_new *knew;
3603 
3604 	if (!ctl)
3605 		return 0;
3606 
3607 	if (label)
3608 		snprintf(tmpname, sizeof(tmpname),
3609 			 "%s Capture %s", label, sfx);
3610 	else
3611 		snprintf(tmpname, sizeof(tmpname),
3612 			 "Capture %s", sfx);
3613 	knew = add_control(spec, type, tmpname, idx,
3614 			   amp_val_replace_channels(ctl, chs));
3615 	if (!knew)
3616 		return -ENOMEM;
3617 	if (is_switch) {
3618 		knew->put = cap_single_sw_put;
3619 		if (spec->mic_mute_led)
3620 			knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3621 	}
3622 	if (!inv_dmic)
3623 		return 0;
3624 
3625 	/* Make independent right kcontrol */
3626 	if (label)
3627 		snprintf(tmpname, sizeof(tmpname),
3628 			 "Inverted %s Capture %s", label, sfx);
3629 	else
3630 		snprintf(tmpname, sizeof(tmpname),
3631 			 "Inverted Capture %s", sfx);
3632 	knew = add_control(spec, type, tmpname, idx,
3633 			   amp_val_replace_channels(ctl, 2));
3634 	if (!knew)
3635 		return -ENOMEM;
3636 	if (is_switch) {
3637 		knew->put = cap_single_sw_put;
3638 		if (spec->mic_mute_led)
3639 			knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3640 	}
3641 	return 0;
3642 }
3643 
3644 /* create single (and simple) capture volume and switch controls */
3645 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3646 				     unsigned int vol_ctl, unsigned int sw_ctl,
3647 				     bool inv_dmic)
3648 {
3649 	int err;
3650 	err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3651 	if (err < 0)
3652 		return err;
3653 	err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3654 	if (err < 0)
3655 		return err;
3656 	return 0;
3657 }
3658 
3659 /* create bound capture volume and switch controls */
3660 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3661 				   unsigned int vol_ctl, unsigned int sw_ctl)
3662 {
3663 	struct hda_gen_spec *spec = codec->spec;
3664 	struct snd_kcontrol_new *knew;
3665 
3666 	if (vol_ctl) {
3667 		knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3668 		if (!knew)
3669 			return -ENOMEM;
3670 		knew->index = idx;
3671 		knew->private_value = vol_ctl;
3672 		knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3673 	}
3674 	if (sw_ctl) {
3675 		knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3676 		if (!knew)
3677 			return -ENOMEM;
3678 		knew->index = idx;
3679 		knew->private_value = sw_ctl;
3680 		knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3681 		if (spec->mic_mute_led)
3682 			knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3683 	}
3684 	return 0;
3685 }
3686 
3687 /* return the vol ctl when used first in the imux list */
3688 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3689 {
3690 	struct nid_path *path;
3691 	unsigned int ctl;
3692 	int i;
3693 
3694 	path = get_input_path(codec, 0, idx);
3695 	if (!path)
3696 		return 0;
3697 	ctl = path->ctls[type];
3698 	if (!ctl)
3699 		return 0;
3700 	for (i = 0; i < idx - 1; i++) {
3701 		path = get_input_path(codec, 0, i);
3702 		if (path && path->ctls[type] == ctl)
3703 			return 0;
3704 	}
3705 	return ctl;
3706 }
3707 
3708 /* create individual capture volume and switch controls per input */
3709 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3710 {
3711 	struct hda_gen_spec *spec = codec->spec;
3712 	struct hda_input_mux *imux = &spec->input_mux;
3713 	int i, err, type;
3714 
3715 	for (i = 0; i < imux->num_items; i++) {
3716 		bool inv_dmic;
3717 		int idx;
3718 
3719 		idx = imux->items[i].index;
3720 		if (idx >= spec->autocfg.num_inputs)
3721 			continue;
3722 		inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3723 
3724 		for (type = 0; type < 2; type++) {
3725 			err = add_single_cap_ctl(codec,
3726 						 spec->input_labels[idx],
3727 						 spec->input_label_idxs[idx],
3728 						 type,
3729 						 get_first_cap_ctl(codec, i, type),
3730 						 inv_dmic);
3731 			if (err < 0)
3732 				return err;
3733 		}
3734 	}
3735 	return 0;
3736 }
3737 
3738 static int create_capture_mixers(struct hda_codec *codec)
3739 {
3740 	struct hda_gen_spec *spec = codec->spec;
3741 	struct hda_input_mux *imux = &spec->input_mux;
3742 	int i, n, nums, err;
3743 
3744 	if (spec->dyn_adc_switch)
3745 		nums = 1;
3746 	else
3747 		nums = spec->num_adc_nids;
3748 
3749 	if (!spec->auto_mic && imux->num_items > 1) {
3750 		struct snd_kcontrol_new *knew;
3751 		const char *name;
3752 		name = nums > 1 ? "Input Source" : "Capture Source";
3753 		knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3754 		if (!knew)
3755 			return -ENOMEM;
3756 		knew->count = nums;
3757 	}
3758 
3759 	for (n = 0; n < nums; n++) {
3760 		bool multi = false;
3761 		bool multi_cap_vol = spec->multi_cap_vol;
3762 		bool inv_dmic = false;
3763 		int vol, sw;
3764 
3765 		vol = sw = 0;
3766 		for (i = 0; i < imux->num_items; i++) {
3767 			struct nid_path *path;
3768 			path = get_input_path(codec, n, i);
3769 			if (!path)
3770 				continue;
3771 			parse_capvol_in_path(codec, path);
3772 			if (!vol)
3773 				vol = path->ctls[NID_PATH_VOL_CTL];
3774 			else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3775 				multi = true;
3776 				if (!same_amp_caps(codec, vol,
3777 				    path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3778 					multi_cap_vol = true;
3779 			}
3780 			if (!sw)
3781 				sw = path->ctls[NID_PATH_MUTE_CTL];
3782 			else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3783 				multi = true;
3784 				if (!same_amp_caps(codec, sw,
3785 				    path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3786 					multi_cap_vol = true;
3787 			}
3788 			if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3789 				inv_dmic = true;
3790 		}
3791 
3792 		if (!multi)
3793 			err = create_single_cap_vol_ctl(codec, n, vol, sw,
3794 							inv_dmic);
3795 		else if (!multi_cap_vol && !inv_dmic)
3796 			err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3797 		else
3798 			err = create_multi_cap_vol_ctl(codec);
3799 		if (err < 0)
3800 			return err;
3801 	}
3802 
3803 	return 0;
3804 }
3805 
3806 /*
3807  * add mic boosts if needed
3808  */
3809 
3810 /* check whether the given amp is feasible as a boost volume */
3811 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3812 			    int dir, int idx)
3813 {
3814 	unsigned int step;
3815 
3816 	if (!nid_has_volume(codec, nid, dir) ||
3817 	    is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3818 	    is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3819 		return false;
3820 
3821 	step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3822 		>> AC_AMPCAP_STEP_SIZE_SHIFT;
3823 	if (step < 0x20)
3824 		return false;
3825 	return true;
3826 }
3827 
3828 /* look for a boost amp in a widget close to the pin */
3829 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3830 				       struct nid_path *path)
3831 {
3832 	unsigned int val = 0;
3833 	hda_nid_t nid;
3834 	int depth;
3835 
3836 	for (depth = 0; depth < 3; depth++) {
3837 		if (depth >= path->depth - 1)
3838 			break;
3839 		nid = path->path[depth];
3840 		if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3841 			val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3842 			break;
3843 		} else if (check_boost_vol(codec, nid, HDA_INPUT,
3844 					   path->idx[depth])) {
3845 			val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3846 						  HDA_INPUT);
3847 			break;
3848 		}
3849 	}
3850 
3851 	return val;
3852 }
3853 
3854 static int parse_mic_boost(struct hda_codec *codec)
3855 {
3856 	struct hda_gen_spec *spec = codec->spec;
3857 	struct auto_pin_cfg *cfg = &spec->autocfg;
3858 	struct hda_input_mux *imux = &spec->input_mux;
3859 	int i;
3860 
3861 	if (!spec->num_adc_nids)
3862 		return 0;
3863 
3864 	for (i = 0; i < imux->num_items; i++) {
3865 		struct nid_path *path;
3866 		unsigned int val;
3867 		int idx;
3868 		char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3869 
3870 		idx = imux->items[i].index;
3871 		if (idx >= imux->num_items)
3872 			continue;
3873 
3874 		/* check only line-in and mic pins */
3875 		if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3876 			continue;
3877 
3878 		path = get_input_path(codec, 0, i);
3879 		if (!path)
3880 			continue;
3881 
3882 		val = look_for_boost_amp(codec, path);
3883 		if (!val)
3884 			continue;
3885 
3886 		/* create a boost control */
3887 		snprintf(boost_label, sizeof(boost_label),
3888 			 "%s Boost Volume", spec->input_labels[idx]);
3889 		if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3890 				 spec->input_label_idxs[idx], val))
3891 			return -ENOMEM;
3892 
3893 		path->ctls[NID_PATH_BOOST_CTL] = val;
3894 	}
3895 	return 0;
3896 }
3897 
3898 #ifdef CONFIG_SND_HDA_GENERIC_LEDS
3899 /*
3900  * vmaster mute LED hook helpers
3901  */
3902 
3903 static int create_mute_led_cdev(struct hda_codec *codec,
3904 				int (*callback)(struct led_classdev *,
3905 						enum led_brightness),
3906 				bool micmute)
3907 {
3908 	struct hda_gen_spec *spec = codec->spec;
3909 	struct led_classdev *cdev;
3910 	int idx = micmute ? LED_AUDIO_MICMUTE : LED_AUDIO_MUTE;
3911 	int err;
3912 
3913 	cdev = devm_kzalloc(&codec->core.dev, sizeof(*cdev), GFP_KERNEL);
3914 	if (!cdev)
3915 		return -ENOMEM;
3916 
3917 	cdev->name = micmute ? "hda::micmute" : "hda::mute";
3918 	cdev->max_brightness = 1;
3919 	cdev->default_trigger = micmute ? "audio-micmute" : "audio-mute";
3920 	cdev->brightness_set_blocking = callback;
3921 	cdev->flags = LED_CORE_SUSPENDRESUME;
3922 
3923 	err = led_classdev_register(&codec->core.dev, cdev);
3924 	if (err < 0)
3925 		return err;
3926 	spec->led_cdevs[idx] = cdev;
3927 	return 0;
3928 }
3929 
3930 /**
3931  * snd_hda_gen_add_mute_led_cdev - Create a LED classdev and enable as vmaster mute LED
3932  * @codec: the HDA codec
3933  * @callback: the callback for LED classdev brightness_set_blocking
3934  */
3935 int snd_hda_gen_add_mute_led_cdev(struct hda_codec *codec,
3936 				  int (*callback)(struct led_classdev *,
3937 						  enum led_brightness))
3938 {
3939 	struct hda_gen_spec *spec = codec->spec;
3940 	int err;
3941 
3942 	if (callback) {
3943 		err = create_mute_led_cdev(codec, callback, false);
3944 		if (err) {
3945 			codec_warn(codec, "failed to create a mute LED cdev\n");
3946 			return err;
3947 		}
3948 	}
3949 
3950 	if (spec->vmaster_mute.hook)
3951 		codec_err(codec, "vmaster hook already present before cdev!\n");
3952 
3953 	spec->vmaster_mute_led = 1;
3954 	return 0;
3955 }
3956 EXPORT_SYMBOL_GPL(snd_hda_gen_add_mute_led_cdev);
3957 
3958 /**
3959  * snd_hda_gen_add_micmute_led_cdev - Create a LED classdev and enable as mic-mute LED
3960  * @codec: the HDA codec
3961  * @callback: the callback for LED classdev brightness_set_blocking
3962  *
3963  * Called from the codec drivers for offering the mic mute LED controls.
3964  * This creates a LED classdev and sets up the cap_sync_hook that is called at
3965  * each time when the capture mixer switch changes.
3966  *
3967  * When NULL is passed to @callback, no classdev is created but only the
3968  * LED-trigger is set up.
3969  *
3970  * Returns 0 or a negative error.
3971  */
3972 int snd_hda_gen_add_micmute_led_cdev(struct hda_codec *codec,
3973 				     int (*callback)(struct led_classdev *,
3974 						     enum led_brightness))
3975 {
3976 	struct hda_gen_spec *spec = codec->spec;
3977 	int err;
3978 
3979 	if (callback) {
3980 		err = create_mute_led_cdev(codec, callback, true);
3981 		if (err) {
3982 			codec_warn(codec, "failed to create a mic-mute LED cdev\n");
3983 			return err;
3984 		}
3985 	}
3986 
3987 	spec->mic_mute_led = 1;
3988 	return 0;
3989 }
3990 EXPORT_SYMBOL_GPL(snd_hda_gen_add_micmute_led_cdev);
3991 #endif /* CONFIG_SND_HDA_GENERIC_LEDS */
3992 
3993 /*
3994  * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3995  */
3996 static void parse_digital(struct hda_codec *codec)
3997 {
3998 	struct hda_gen_spec *spec = codec->spec;
3999 	struct nid_path *path;
4000 	int i, nums;
4001 	hda_nid_t dig_nid, pin;
4002 
4003 	/* support multiple SPDIFs; the secondary is set up as a follower */
4004 	nums = 0;
4005 	for (i = 0; i < spec->autocfg.dig_outs; i++) {
4006 		pin = spec->autocfg.dig_out_pins[i];
4007 		dig_nid = look_for_dac(codec, pin, true);
4008 		if (!dig_nid)
4009 			continue;
4010 		path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
4011 		if (!path)
4012 			continue;
4013 		print_nid_path(codec, "digout", path);
4014 		path->active = true;
4015 		path->pin_fixed = true; /* no jack detection */
4016 		spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
4017 		set_pin_target(codec, pin, PIN_OUT, false);
4018 		if (!nums) {
4019 			spec->multiout.dig_out_nid = dig_nid;
4020 			spec->dig_out_type = spec->autocfg.dig_out_type[0];
4021 		} else {
4022 			spec->multiout.follower_dig_outs = spec->follower_dig_outs;
4023 			if (nums >= ARRAY_SIZE(spec->follower_dig_outs) - 1)
4024 				break;
4025 			spec->follower_dig_outs[nums - 1] = dig_nid;
4026 		}
4027 		nums++;
4028 	}
4029 
4030 	if (spec->autocfg.dig_in_pin) {
4031 		pin = spec->autocfg.dig_in_pin;
4032 		for_each_hda_codec_node(dig_nid, codec) {
4033 			unsigned int wcaps = get_wcaps(codec, dig_nid);
4034 			if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
4035 				continue;
4036 			if (!(wcaps & AC_WCAP_DIGITAL))
4037 				continue;
4038 			path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
4039 			if (path) {
4040 				print_nid_path(codec, "digin", path);
4041 				path->active = true;
4042 				path->pin_fixed = true; /* no jack */
4043 				spec->dig_in_nid = dig_nid;
4044 				spec->digin_path = snd_hda_get_path_idx(codec, path);
4045 				set_pin_target(codec, pin, PIN_IN, false);
4046 				break;
4047 			}
4048 		}
4049 	}
4050 }
4051 
4052 
4053 /*
4054  * input MUX handling
4055  */
4056 
4057 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
4058 
4059 /* select the given imux item; either unmute exclusively or select the route */
4060 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
4061 		      unsigned int idx)
4062 {
4063 	struct hda_gen_spec *spec = codec->spec;
4064 	const struct hda_input_mux *imux;
4065 	struct nid_path *old_path, *path;
4066 
4067 	imux = &spec->input_mux;
4068 	if (!imux->num_items)
4069 		return 0;
4070 
4071 	if (idx >= imux->num_items)
4072 		idx = imux->num_items - 1;
4073 	if (spec->cur_mux[adc_idx] == idx)
4074 		return 0;
4075 
4076 	old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
4077 	if (!old_path)
4078 		return 0;
4079 	if (old_path->active)
4080 		snd_hda_activate_path(codec, old_path, false, false);
4081 
4082 	spec->cur_mux[adc_idx] = idx;
4083 
4084 	if (spec->hp_mic)
4085 		update_hp_mic(codec, adc_idx, false);
4086 
4087 	if (spec->dyn_adc_switch)
4088 		dyn_adc_pcm_resetup(codec, idx);
4089 
4090 	path = get_input_path(codec, adc_idx, idx);
4091 	if (!path)
4092 		return 0;
4093 	if (path->active)
4094 		return 0;
4095 	snd_hda_activate_path(codec, path, true, false);
4096 	if (spec->cap_sync_hook)
4097 		spec->cap_sync_hook(codec, NULL, NULL);
4098 	path_power_down_sync(codec, old_path);
4099 	return 1;
4100 }
4101 
4102 /* power up/down widgets in the all paths that match with the given NID
4103  * as terminals (either start- or endpoint)
4104  *
4105  * returns the last changed NID, or zero if unchanged.
4106  */
4107 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
4108 				int pin_state, int stream_state)
4109 {
4110 	struct hda_gen_spec *spec = codec->spec;
4111 	hda_nid_t last, changed = 0;
4112 	struct nid_path *path;
4113 	int n;
4114 
4115 	snd_array_for_each(&spec->paths, n, path) {
4116 		if (!path->depth)
4117 			continue;
4118 		if (path->path[0] == nid ||
4119 		    path->path[path->depth - 1] == nid) {
4120 			bool pin_old = path->pin_enabled;
4121 			bool stream_old = path->stream_enabled;
4122 
4123 			if (pin_state >= 0)
4124 				path->pin_enabled = pin_state;
4125 			if (stream_state >= 0)
4126 				path->stream_enabled = stream_state;
4127 			if ((!path->pin_fixed && path->pin_enabled != pin_old)
4128 			    || path->stream_enabled != stream_old) {
4129 				last = path_power_update(codec, path, true);
4130 				if (last)
4131 					changed = last;
4132 			}
4133 		}
4134 	}
4135 	return changed;
4136 }
4137 
4138 /* check the jack status for power control */
4139 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4140 {
4141 	if (!is_jack_detectable(codec, pin))
4142 		return true;
4143 	return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4144 }
4145 
4146 /* power up/down the paths of the given pin according to the jack state;
4147  * power = 0/1 : only power up/down if it matches with the jack state,
4148  *       < 0   : force power up/down to follow the jack sate
4149  *
4150  * returns the last changed NID, or zero if unchanged.
4151  */
4152 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4153 				    int power)
4154 {
4155 	bool on;
4156 
4157 	if (!codec->power_save_node)
4158 		return 0;
4159 
4160 	on = detect_pin_state(codec, pin);
4161 
4162 	if (power >= 0 && on != power)
4163 		return 0;
4164 	return set_path_power(codec, pin, on, -1);
4165 }
4166 
4167 static void pin_power_callback(struct hda_codec *codec,
4168 			       struct hda_jack_callback *jack,
4169 			       bool on)
4170 {
4171 	if (jack && jack->nid)
4172 		sync_power_state_change(codec,
4173 					set_pin_power_jack(codec, jack->nid, on));
4174 }
4175 
4176 /* callback only doing power up -- called at first */
4177 static void pin_power_up_callback(struct hda_codec *codec,
4178 				  struct hda_jack_callback *jack)
4179 {
4180 	pin_power_callback(codec, jack, true);
4181 }
4182 
4183 /* callback only doing power down -- called at last */
4184 static void pin_power_down_callback(struct hda_codec *codec,
4185 				    struct hda_jack_callback *jack)
4186 {
4187 	pin_power_callback(codec, jack, false);
4188 }
4189 
4190 /* set up the power up/down callbacks */
4191 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4192 			       const hda_nid_t *pins, bool on)
4193 {
4194 	int i;
4195 	hda_jack_callback_fn cb =
4196 		on ? pin_power_up_callback : pin_power_down_callback;
4197 
4198 	for (i = 0; i < num_pins && pins[i]; i++) {
4199 		if (is_jack_detectable(codec, pins[i]))
4200 			snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4201 		else
4202 			set_path_power(codec, pins[i], true, -1);
4203 	}
4204 }
4205 
4206 /* enabled power callback to each available I/O pin with jack detections;
4207  * the digital I/O pins are excluded because of the unreliable detectsion
4208  */
4209 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4210 {
4211 	struct hda_gen_spec *spec = codec->spec;
4212 	struct auto_pin_cfg *cfg = &spec->autocfg;
4213 	int i;
4214 
4215 	if (!codec->power_save_node)
4216 		return;
4217 	add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4218 	if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4219 		add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4220 	if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4221 		add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4222 	for (i = 0; i < cfg->num_inputs; i++)
4223 		add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4224 }
4225 
4226 /* sync path power up/down with the jack states of given pins */
4227 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4228 				const hda_nid_t *pins)
4229 {
4230 	int i;
4231 
4232 	for (i = 0; i < num_pins && pins[i]; i++)
4233 		if (is_jack_detectable(codec, pins[i]))
4234 			set_pin_power_jack(codec, pins[i], -1);
4235 }
4236 
4237 /* sync path power up/down with pins; called at init and resume */
4238 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4239 {
4240 	struct hda_gen_spec *spec = codec->spec;
4241 	struct auto_pin_cfg *cfg = &spec->autocfg;
4242 	int i;
4243 
4244 	if (!codec->power_save_node)
4245 		return;
4246 	sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4247 	if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4248 		sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4249 	if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4250 		sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4251 	for (i = 0; i < cfg->num_inputs; i++)
4252 		sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4253 }
4254 
4255 /* add fake paths if not present yet */
4256 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4257 			   int num_pins, const hda_nid_t *pins)
4258 {
4259 	struct hda_gen_spec *spec = codec->spec;
4260 	struct nid_path *path;
4261 	int i;
4262 
4263 	for (i = 0; i < num_pins; i++) {
4264 		if (!pins[i])
4265 			break;
4266 		if (get_nid_path(codec, nid, pins[i], 0))
4267 			continue;
4268 		path = snd_array_new(&spec->paths);
4269 		if (!path)
4270 			return -ENOMEM;
4271 		memset(path, 0, sizeof(*path));
4272 		path->depth = 2;
4273 		path->path[0] = nid;
4274 		path->path[1] = pins[i];
4275 		path->active = true;
4276 	}
4277 	return 0;
4278 }
4279 
4280 /* create fake paths to all outputs from beep */
4281 static int add_fake_beep_paths(struct hda_codec *codec)
4282 {
4283 	struct hda_gen_spec *spec = codec->spec;
4284 	struct auto_pin_cfg *cfg = &spec->autocfg;
4285 	hda_nid_t nid = spec->beep_nid;
4286 	int err;
4287 
4288 	if (!codec->power_save_node || !nid)
4289 		return 0;
4290 	err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4291 	if (err < 0)
4292 		return err;
4293 	if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4294 		err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4295 		if (err < 0)
4296 			return err;
4297 	}
4298 	if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4299 		err = add_fake_paths(codec, nid, cfg->speaker_outs,
4300 				     cfg->speaker_pins);
4301 		if (err < 0)
4302 			return err;
4303 	}
4304 	return 0;
4305 }
4306 
4307 /* power up/down beep widget and its output paths */
4308 static void beep_power_hook(struct hda_beep *beep, bool on)
4309 {
4310 	set_path_power(beep->codec, beep->nid, -1, on);
4311 }
4312 
4313 /**
4314  * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4315  * @codec: the HDA codec
4316  * @pin: NID of pin to fix
4317  */
4318 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4319 {
4320 	struct hda_gen_spec *spec = codec->spec;
4321 	struct nid_path *path;
4322 
4323 	path = snd_array_new(&spec->paths);
4324 	if (!path)
4325 		return -ENOMEM;
4326 	memset(path, 0, sizeof(*path));
4327 	path->depth = 1;
4328 	path->path[0] = pin;
4329 	path->active = true;
4330 	path->pin_fixed = true;
4331 	path->stream_enabled = true;
4332 	return 0;
4333 }
4334 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4335 
4336 /*
4337  * Jack detections for HP auto-mute and mic-switch
4338  */
4339 
4340 /* check each pin in the given array; returns true if any of them is plugged */
4341 static bool detect_jacks(struct hda_codec *codec, int num_pins, const hda_nid_t *pins)
4342 {
4343 	int i;
4344 	bool present = false;
4345 
4346 	for (i = 0; i < num_pins; i++) {
4347 		hda_nid_t nid = pins[i];
4348 		if (!nid)
4349 			break;
4350 		/* don't detect pins retasked as inputs */
4351 		if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4352 			continue;
4353 		if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4354 			present = true;
4355 	}
4356 	return present;
4357 }
4358 
4359 /* standard HP/line-out auto-mute helper */
4360 static void do_automute(struct hda_codec *codec, int num_pins, const hda_nid_t *pins,
4361 			int *paths, bool mute)
4362 {
4363 	struct hda_gen_spec *spec = codec->spec;
4364 	int i;
4365 
4366 	for (i = 0; i < num_pins; i++) {
4367 		hda_nid_t nid = pins[i];
4368 		unsigned int val, oldval;
4369 		if (!nid)
4370 			break;
4371 
4372 		oldval = snd_hda_codec_get_pin_target(codec, nid);
4373 		if (oldval & PIN_IN)
4374 			continue; /* no mute for inputs */
4375 
4376 		if (spec->auto_mute_via_amp) {
4377 			struct nid_path *path;
4378 			hda_nid_t mute_nid;
4379 
4380 			path = snd_hda_get_path_from_idx(codec, paths[i]);
4381 			if (!path)
4382 				continue;
4383 			mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4384 			if (!mute_nid)
4385 				continue;
4386 			if (mute)
4387 				spec->mute_bits |= (1ULL << mute_nid);
4388 			else
4389 				spec->mute_bits &= ~(1ULL << mute_nid);
4390 			continue;
4391 		} else {
4392 			/* don't reset VREF value in case it's controlling
4393 			 * the amp (see alc861_fixup_asus_amp_vref_0f())
4394 			 */
4395 			if (spec->keep_vref_in_automute)
4396 				val = oldval & ~PIN_HP;
4397 			else
4398 				val = 0;
4399 			if (!mute)
4400 				val |= oldval;
4401 			/* here we call update_pin_ctl() so that the pinctl is
4402 			 * changed without changing the pinctl target value;
4403 			 * the original target value will be still referred at
4404 			 * the init / resume again
4405 			 */
4406 			update_pin_ctl(codec, nid, val);
4407 		}
4408 
4409 		set_pin_eapd(codec, nid, !mute);
4410 		if (codec->power_save_node) {
4411 			bool on = !mute;
4412 			if (on)
4413 				on = detect_pin_state(codec, nid);
4414 			set_path_power(codec, nid, on, -1);
4415 		}
4416 	}
4417 }
4418 
4419 /**
4420  * snd_hda_gen_update_outputs - Toggle outputs muting
4421  * @codec: the HDA codec
4422  *
4423  * Update the mute status of all outputs based on the current jack states.
4424  */
4425 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4426 {
4427 	struct hda_gen_spec *spec = codec->spec;
4428 	int *paths;
4429 	int on;
4430 
4431 	/* Control HP pins/amps depending on master_mute state;
4432 	 * in general, HP pins/amps control should be enabled in all cases,
4433 	 * but currently set only for master_mute, just to be safe
4434 	 */
4435 	if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4436 		paths = spec->out_paths;
4437 	else
4438 		paths = spec->hp_paths;
4439 	do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4440 		    spec->autocfg.hp_pins, paths, spec->master_mute);
4441 
4442 	if (!spec->automute_speaker)
4443 		on = 0;
4444 	else
4445 		on = spec->hp_jack_present | spec->line_jack_present;
4446 	on |= spec->master_mute;
4447 	spec->speaker_muted = on;
4448 	if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4449 		paths = spec->out_paths;
4450 	else
4451 		paths = spec->speaker_paths;
4452 	do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4453 		    spec->autocfg.speaker_pins, paths, on);
4454 
4455 	/* toggle line-out mutes if needed, too */
4456 	/* if LO is a copy of either HP or Speaker, don't need to handle it */
4457 	if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4458 	    spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4459 		return;
4460 	if (!spec->automute_lo)
4461 		on = 0;
4462 	else
4463 		on = spec->hp_jack_present;
4464 	on |= spec->master_mute;
4465 	spec->line_out_muted = on;
4466 	paths = spec->out_paths;
4467 	do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4468 		    spec->autocfg.line_out_pins, paths, on);
4469 }
4470 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4471 
4472 static void call_update_outputs(struct hda_codec *codec)
4473 {
4474 	struct hda_gen_spec *spec = codec->spec;
4475 	if (spec->automute_hook)
4476 		spec->automute_hook(codec);
4477 	else
4478 		snd_hda_gen_update_outputs(codec);
4479 
4480 	/* sync the whole vmaster followers to reflect the new auto-mute status */
4481 	if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4482 		snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4483 }
4484 
4485 /**
4486  * snd_hda_gen_hp_automute - standard HP-automute helper
4487  * @codec: the HDA codec
4488  * @jack: jack object, NULL for the whole
4489  */
4490 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4491 			     struct hda_jack_callback *jack)
4492 {
4493 	struct hda_gen_spec *spec = codec->spec;
4494 	hda_nid_t *pins = spec->autocfg.hp_pins;
4495 	int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4496 
4497 	/* No detection for the first HP jack during indep-HP mode */
4498 	if (spec->indep_hp_enabled) {
4499 		pins++;
4500 		num_pins--;
4501 	}
4502 
4503 	spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4504 	if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4505 		return;
4506 	call_update_outputs(codec);
4507 }
4508 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4509 
4510 /**
4511  * snd_hda_gen_line_automute - standard line-out-automute helper
4512  * @codec: the HDA codec
4513  * @jack: jack object, NULL for the whole
4514  */
4515 void snd_hda_gen_line_automute(struct hda_codec *codec,
4516 			       struct hda_jack_callback *jack)
4517 {
4518 	struct hda_gen_spec *spec = codec->spec;
4519 
4520 	if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4521 		return;
4522 	/* check LO jack only when it's different from HP */
4523 	if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4524 		return;
4525 
4526 	spec->line_jack_present =
4527 		detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4528 			     spec->autocfg.line_out_pins);
4529 	if (!spec->automute_speaker || !spec->detect_lo)
4530 		return;
4531 	call_update_outputs(codec);
4532 }
4533 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4534 
4535 /**
4536  * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4537  * @codec: the HDA codec
4538  * @jack: jack object, NULL for the whole
4539  */
4540 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4541 				struct hda_jack_callback *jack)
4542 {
4543 	struct hda_gen_spec *spec = codec->spec;
4544 	int i;
4545 
4546 	if (!spec->auto_mic)
4547 		return;
4548 
4549 	for (i = spec->am_num_entries - 1; i > 0; i--) {
4550 		hda_nid_t pin = spec->am_entry[i].pin;
4551 		/* don't detect pins retasked as outputs */
4552 		if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4553 			continue;
4554 		if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4555 			mux_select(codec, 0, spec->am_entry[i].idx);
4556 			return;
4557 		}
4558 	}
4559 	mux_select(codec, 0, spec->am_entry[0].idx);
4560 }
4561 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4562 
4563 /* call appropriate hooks */
4564 static void call_hp_automute(struct hda_codec *codec,
4565 			     struct hda_jack_callback *jack)
4566 {
4567 	struct hda_gen_spec *spec = codec->spec;
4568 	if (spec->hp_automute_hook)
4569 		spec->hp_automute_hook(codec, jack);
4570 	else
4571 		snd_hda_gen_hp_automute(codec, jack);
4572 }
4573 
4574 static void call_line_automute(struct hda_codec *codec,
4575 			       struct hda_jack_callback *jack)
4576 {
4577 	struct hda_gen_spec *spec = codec->spec;
4578 	if (spec->line_automute_hook)
4579 		spec->line_automute_hook(codec, jack);
4580 	else
4581 		snd_hda_gen_line_automute(codec, jack);
4582 }
4583 
4584 static void call_mic_autoswitch(struct hda_codec *codec,
4585 				struct hda_jack_callback *jack)
4586 {
4587 	struct hda_gen_spec *spec = codec->spec;
4588 	if (spec->mic_autoswitch_hook)
4589 		spec->mic_autoswitch_hook(codec, jack);
4590 	else
4591 		snd_hda_gen_mic_autoswitch(codec, jack);
4592 }
4593 
4594 /* update jack retasking */
4595 static void update_automute_all(struct hda_codec *codec)
4596 {
4597 	call_hp_automute(codec, NULL);
4598 	call_line_automute(codec, NULL);
4599 	call_mic_autoswitch(codec, NULL);
4600 }
4601 
4602 /*
4603  * Auto-Mute mode mixer enum support
4604  */
4605 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4606 			      struct snd_ctl_elem_info *uinfo)
4607 {
4608 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4609 	struct hda_gen_spec *spec = codec->spec;
4610 	static const char * const texts3[] = {
4611 		"Disabled", "Speaker Only", "Line Out+Speaker"
4612 	};
4613 
4614 	if (spec->automute_speaker_possible && spec->automute_lo_possible)
4615 		return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4616 	return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4617 }
4618 
4619 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4620 			     struct snd_ctl_elem_value *ucontrol)
4621 {
4622 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4623 	struct hda_gen_spec *spec = codec->spec;
4624 	unsigned int val = 0;
4625 	if (spec->automute_speaker)
4626 		val++;
4627 	if (spec->automute_lo)
4628 		val++;
4629 
4630 	ucontrol->value.enumerated.item[0] = val;
4631 	return 0;
4632 }
4633 
4634 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4635 			     struct snd_ctl_elem_value *ucontrol)
4636 {
4637 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4638 	struct hda_gen_spec *spec = codec->spec;
4639 
4640 	switch (ucontrol->value.enumerated.item[0]) {
4641 	case 0:
4642 		if (!spec->automute_speaker && !spec->automute_lo)
4643 			return 0;
4644 		spec->automute_speaker = 0;
4645 		spec->automute_lo = 0;
4646 		break;
4647 	case 1:
4648 		if (spec->automute_speaker_possible) {
4649 			if (!spec->automute_lo && spec->automute_speaker)
4650 				return 0;
4651 			spec->automute_speaker = 1;
4652 			spec->automute_lo = 0;
4653 		} else if (spec->automute_lo_possible) {
4654 			if (spec->automute_lo)
4655 				return 0;
4656 			spec->automute_lo = 1;
4657 		} else
4658 			return -EINVAL;
4659 		break;
4660 	case 2:
4661 		if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4662 			return -EINVAL;
4663 		if (spec->automute_speaker && spec->automute_lo)
4664 			return 0;
4665 		spec->automute_speaker = 1;
4666 		spec->automute_lo = 1;
4667 		break;
4668 	default:
4669 		return -EINVAL;
4670 	}
4671 	call_update_outputs(codec);
4672 	return 1;
4673 }
4674 
4675 static const struct snd_kcontrol_new automute_mode_enum = {
4676 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4677 	.name = "Auto-Mute Mode",
4678 	.info = automute_mode_info,
4679 	.get = automute_mode_get,
4680 	.put = automute_mode_put,
4681 };
4682 
4683 static int add_automute_mode_enum(struct hda_codec *codec)
4684 {
4685 	struct hda_gen_spec *spec = codec->spec;
4686 
4687 	if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4688 		return -ENOMEM;
4689 	return 0;
4690 }
4691 
4692 /*
4693  * Check the availability of HP/line-out auto-mute;
4694  * Set up appropriately if really supported
4695  */
4696 static int check_auto_mute_availability(struct hda_codec *codec)
4697 {
4698 	struct hda_gen_spec *spec = codec->spec;
4699 	struct auto_pin_cfg *cfg = &spec->autocfg;
4700 	int present = 0;
4701 	int i, err;
4702 
4703 	if (spec->suppress_auto_mute)
4704 		return 0;
4705 
4706 	if (cfg->hp_pins[0])
4707 		present++;
4708 	if (cfg->line_out_pins[0])
4709 		present++;
4710 	if (cfg->speaker_pins[0])
4711 		present++;
4712 	if (present < 2) /* need two different output types */
4713 		return 0;
4714 
4715 	if (!cfg->speaker_pins[0] &&
4716 	    cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4717 		memcpy(cfg->speaker_pins, cfg->line_out_pins,
4718 		       sizeof(cfg->speaker_pins));
4719 		cfg->speaker_outs = cfg->line_outs;
4720 	}
4721 
4722 	if (!cfg->hp_pins[0] &&
4723 	    cfg->line_out_type == AUTO_PIN_HP_OUT) {
4724 		memcpy(cfg->hp_pins, cfg->line_out_pins,
4725 		       sizeof(cfg->hp_pins));
4726 		cfg->hp_outs = cfg->line_outs;
4727 	}
4728 
4729 	for (i = 0; i < cfg->hp_outs; i++) {
4730 		hda_nid_t nid = cfg->hp_pins[i];
4731 		if (!is_jack_detectable(codec, nid))
4732 			continue;
4733 		codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4734 		snd_hda_jack_detect_enable_callback(codec, nid,
4735 						    call_hp_automute);
4736 		spec->detect_hp = 1;
4737 	}
4738 
4739 	if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4740 		if (cfg->speaker_outs)
4741 			for (i = 0; i < cfg->line_outs; i++) {
4742 				hda_nid_t nid = cfg->line_out_pins[i];
4743 				if (!is_jack_detectable(codec, nid))
4744 					continue;
4745 				codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4746 				snd_hda_jack_detect_enable_callback(codec, nid,
4747 								    call_line_automute);
4748 				spec->detect_lo = 1;
4749 			}
4750 		spec->automute_lo_possible = spec->detect_hp;
4751 	}
4752 
4753 	spec->automute_speaker_possible = cfg->speaker_outs &&
4754 		(spec->detect_hp || spec->detect_lo);
4755 
4756 	spec->automute_lo = spec->automute_lo_possible;
4757 	spec->automute_speaker = spec->automute_speaker_possible;
4758 
4759 	if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4760 		/* create a control for automute mode */
4761 		err = add_automute_mode_enum(codec);
4762 		if (err < 0)
4763 			return err;
4764 	}
4765 	return 0;
4766 }
4767 
4768 /* check whether all auto-mic pins are valid; setup indices if OK */
4769 static bool auto_mic_check_imux(struct hda_codec *codec)
4770 {
4771 	struct hda_gen_spec *spec = codec->spec;
4772 	const struct hda_input_mux *imux;
4773 	int i;
4774 
4775 	imux = &spec->input_mux;
4776 	for (i = 0; i < spec->am_num_entries; i++) {
4777 		spec->am_entry[i].idx =
4778 			find_idx_in_nid_list(spec->am_entry[i].pin,
4779 					     spec->imux_pins, imux->num_items);
4780 		if (spec->am_entry[i].idx < 0)
4781 			return false; /* no corresponding imux */
4782 	}
4783 
4784 	/* we don't need the jack detection for the first pin */
4785 	for (i = 1; i < spec->am_num_entries; i++)
4786 		snd_hda_jack_detect_enable_callback(codec,
4787 						    spec->am_entry[i].pin,
4788 						    call_mic_autoswitch);
4789 	return true;
4790 }
4791 
4792 static int compare_attr(const void *ap, const void *bp)
4793 {
4794 	const struct automic_entry *a = ap;
4795 	const struct automic_entry *b = bp;
4796 	return (int)(a->attr - b->attr);
4797 }
4798 
4799 /*
4800  * Check the availability of auto-mic switch;
4801  * Set up if really supported
4802  */
4803 static int check_auto_mic_availability(struct hda_codec *codec)
4804 {
4805 	struct hda_gen_spec *spec = codec->spec;
4806 	struct auto_pin_cfg *cfg = &spec->autocfg;
4807 	unsigned int types;
4808 	int i, num_pins;
4809 
4810 	if (spec->suppress_auto_mic)
4811 		return 0;
4812 
4813 	types = 0;
4814 	num_pins = 0;
4815 	for (i = 0; i < cfg->num_inputs; i++) {
4816 		hda_nid_t nid = cfg->inputs[i].pin;
4817 		unsigned int attr;
4818 		attr = snd_hda_codec_get_pincfg(codec, nid);
4819 		attr = snd_hda_get_input_pin_attr(attr);
4820 		if (types & (1 << attr))
4821 			return 0; /* already occupied */
4822 		switch (attr) {
4823 		case INPUT_PIN_ATTR_INT:
4824 			if (cfg->inputs[i].type != AUTO_PIN_MIC)
4825 				return 0; /* invalid type */
4826 			break;
4827 		case INPUT_PIN_ATTR_UNUSED:
4828 			return 0; /* invalid entry */
4829 		default:
4830 			if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4831 				return 0; /* invalid type */
4832 			if (!spec->line_in_auto_switch &&
4833 			    cfg->inputs[i].type != AUTO_PIN_MIC)
4834 				return 0; /* only mic is allowed */
4835 			if (!is_jack_detectable(codec, nid))
4836 				return 0; /* no unsol support */
4837 			break;
4838 		}
4839 		if (num_pins >= MAX_AUTO_MIC_PINS)
4840 			return 0;
4841 		types |= (1 << attr);
4842 		spec->am_entry[num_pins].pin = nid;
4843 		spec->am_entry[num_pins].attr = attr;
4844 		num_pins++;
4845 	}
4846 
4847 	if (num_pins < 2)
4848 		return 0;
4849 
4850 	spec->am_num_entries = num_pins;
4851 	/* sort the am_entry in the order of attr so that the pin with a
4852 	 * higher attr will be selected when the jack is plugged.
4853 	 */
4854 	sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4855 	     compare_attr, NULL);
4856 
4857 	if (!auto_mic_check_imux(codec))
4858 		return 0;
4859 
4860 	spec->auto_mic = 1;
4861 	spec->num_adc_nids = 1;
4862 	spec->cur_mux[0] = spec->am_entry[0].idx;
4863 	codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4864 		    spec->am_entry[0].pin,
4865 		    spec->am_entry[1].pin,
4866 		    spec->am_entry[2].pin);
4867 
4868 	return 0;
4869 }
4870 
4871 /**
4872  * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4873  * into power down
4874  * @codec: the HDA codec
4875  * @nid: NID to evaluate
4876  * @power_state: target power state
4877  */
4878 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4879 						  hda_nid_t nid,
4880 						  unsigned int power_state)
4881 {
4882 	struct hda_gen_spec *spec = codec->spec;
4883 
4884 	if (!spec->power_down_unused && !codec->power_save_node)
4885 		return power_state;
4886 	if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4887 		return power_state;
4888 	if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4889 		return power_state;
4890 	if (is_active_nid_for_any(codec, nid))
4891 		return power_state;
4892 	return AC_PWRST_D3;
4893 }
4894 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4895 
4896 /* mute all aamix inputs initially; parse up to the first leaves */
4897 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4898 {
4899 	int i, nums;
4900 	const hda_nid_t *conn;
4901 	bool has_amp;
4902 
4903 	nums = snd_hda_get_conn_list(codec, mix, &conn);
4904 	has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4905 	for (i = 0; i < nums; i++) {
4906 		if (has_amp)
4907 			update_amp(codec, mix, HDA_INPUT, i,
4908 				   0xff, HDA_AMP_MUTE);
4909 		else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4910 			update_amp(codec, conn[i], HDA_OUTPUT, 0,
4911 				   0xff, HDA_AMP_MUTE);
4912 	}
4913 }
4914 
4915 /**
4916  * snd_hda_gen_stream_pm - Stream power management callback
4917  * @codec: the HDA codec
4918  * @nid: audio widget
4919  * @on: power on/off flag
4920  *
4921  * Set this in hda_codec_ops.stream_pm.  Only valid with power_save_node flag.
4922  */
4923 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4924 {
4925 	if (codec->power_save_node)
4926 		set_path_power(codec, nid, -1, on);
4927 }
4928 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4929 
4930 /* forcibly mute the speaker output without caching; return true if updated */
4931 static bool force_mute_output_path(struct hda_codec *codec, hda_nid_t nid)
4932 {
4933 	if (!nid)
4934 		return false;
4935 	if (!nid_has_mute(codec, nid, HDA_OUTPUT))
4936 		return false; /* no mute, skip */
4937 	if (snd_hda_codec_amp_read(codec, nid, 0, HDA_OUTPUT, 0) &
4938 	    snd_hda_codec_amp_read(codec, nid, 1, HDA_OUTPUT, 0) &
4939 	    HDA_AMP_MUTE)
4940 		return false; /* both channels already muted, skip */
4941 
4942 	/* direct amp update without caching */
4943 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
4944 			    AC_AMP_SET_OUTPUT | AC_AMP_SET_LEFT |
4945 			    AC_AMP_SET_RIGHT | HDA_AMP_MUTE);
4946 	return true;
4947 }
4948 
4949 /**
4950  * snd_hda_gen_shutup_speakers - Forcibly mute the speaker outputs
4951  * @codec: the HDA codec
4952  *
4953  * Forcibly mute the speaker outputs, to be called at suspend or shutdown.
4954  *
4955  * The mute state done by this function isn't cached, hence the original state
4956  * will be restored at resume.
4957  *
4958  * Return true if the mute state has been changed.
4959  */
4960 bool snd_hda_gen_shutup_speakers(struct hda_codec *codec)
4961 {
4962 	struct hda_gen_spec *spec = codec->spec;
4963 	const int *paths;
4964 	const struct nid_path *path;
4965 	int i, p, num_paths;
4966 	bool updated = false;
4967 
4968 	/* if already powered off, do nothing */
4969 	if (!snd_hdac_is_power_on(&codec->core))
4970 		return false;
4971 
4972 	if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT) {
4973 		paths = spec->out_paths;
4974 		num_paths = spec->autocfg.line_outs;
4975 	} else {
4976 		paths = spec->speaker_paths;
4977 		num_paths = spec->autocfg.speaker_outs;
4978 	}
4979 
4980 	for (i = 0; i < num_paths; i++) {
4981 		path = snd_hda_get_path_from_idx(codec, paths[i]);
4982 		if (!path)
4983 			continue;
4984 		for (p = 0; p < path->depth; p++)
4985 			if (force_mute_output_path(codec, path->path[p]))
4986 				updated = true;
4987 	}
4988 
4989 	return updated;
4990 }
4991 EXPORT_SYMBOL_GPL(snd_hda_gen_shutup_speakers);
4992 
4993 /**
4994  * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4995  * set up the hda_gen_spec
4996  * @codec: the HDA codec
4997  * @cfg: Parsed pin configuration
4998  *
4999  * return 1 if successful, 0 if the proper config is not found,
5000  * or a negative error code
5001  */
5002 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
5003 				  struct auto_pin_cfg *cfg)
5004 {
5005 	struct hda_gen_spec *spec = codec->spec;
5006 	int err;
5007 
5008 	parse_user_hints(codec);
5009 
5010 	if (spec->vmaster_mute_led || spec->mic_mute_led)
5011 		snd_ctl_led_request();
5012 
5013 	if (spec->mixer_nid && !spec->mixer_merge_nid)
5014 		spec->mixer_merge_nid = spec->mixer_nid;
5015 
5016 	if (cfg != &spec->autocfg) {
5017 		spec->autocfg = *cfg;
5018 		cfg = &spec->autocfg;
5019 	}
5020 
5021 	if (!spec->main_out_badness)
5022 		spec->main_out_badness = &hda_main_out_badness;
5023 	if (!spec->extra_out_badness)
5024 		spec->extra_out_badness = &hda_extra_out_badness;
5025 
5026 	fill_all_dac_nids(codec);
5027 
5028 	if (!cfg->line_outs) {
5029 		if (cfg->dig_outs || cfg->dig_in_pin) {
5030 			spec->multiout.max_channels = 2;
5031 			spec->no_analog = 1;
5032 			goto dig_only;
5033 		}
5034 		if (!cfg->num_inputs && !cfg->dig_in_pin)
5035 			return 0; /* can't find valid BIOS pin config */
5036 	}
5037 
5038 	if (!spec->no_primary_hp &&
5039 	    cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
5040 	    cfg->line_outs <= cfg->hp_outs) {
5041 		/* use HP as primary out */
5042 		cfg->speaker_outs = cfg->line_outs;
5043 		memcpy(cfg->speaker_pins, cfg->line_out_pins,
5044 		       sizeof(cfg->speaker_pins));
5045 		cfg->line_outs = cfg->hp_outs;
5046 		memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
5047 		cfg->hp_outs = 0;
5048 		memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5049 		cfg->line_out_type = AUTO_PIN_HP_OUT;
5050 	}
5051 
5052 	err = parse_output_paths(codec);
5053 	if (err < 0)
5054 		return err;
5055 	err = create_multi_channel_mode(codec);
5056 	if (err < 0)
5057 		return err;
5058 	err = create_multi_out_ctls(codec, cfg);
5059 	if (err < 0)
5060 		return err;
5061 	err = create_hp_out_ctls(codec);
5062 	if (err < 0)
5063 		return err;
5064 	err = create_speaker_out_ctls(codec);
5065 	if (err < 0)
5066 		return err;
5067 	err = create_indep_hp_ctls(codec);
5068 	if (err < 0)
5069 		return err;
5070 	err = create_loopback_mixing_ctl(codec);
5071 	if (err < 0)
5072 		return err;
5073 	err = create_hp_mic(codec);
5074 	if (err < 0)
5075 		return err;
5076 	err = create_input_ctls(codec);
5077 	if (err < 0)
5078 		return err;
5079 
5080 	/* add power-down pin callbacks at first */
5081 	add_all_pin_power_ctls(codec, false);
5082 
5083 	spec->const_channel_count = spec->ext_channel_count;
5084 	/* check the multiple speaker and headphone pins */
5085 	if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
5086 		spec->const_channel_count = max(spec->const_channel_count,
5087 						cfg->speaker_outs * 2);
5088 	if (cfg->line_out_type != AUTO_PIN_HP_OUT)
5089 		spec->const_channel_count = max(spec->const_channel_count,
5090 						cfg->hp_outs * 2);
5091 	spec->multiout.max_channels = max(spec->ext_channel_count,
5092 					  spec->const_channel_count);
5093 
5094 	err = check_auto_mute_availability(codec);
5095 	if (err < 0)
5096 		return err;
5097 
5098 	err = check_dyn_adc_switch(codec);
5099 	if (err < 0)
5100 		return err;
5101 
5102 	err = check_auto_mic_availability(codec);
5103 	if (err < 0)
5104 		return err;
5105 
5106 	/* add stereo mix if available and not enabled yet */
5107 	if (!spec->auto_mic && spec->mixer_nid &&
5108 	    spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
5109 	    spec->input_mux.num_items > 1) {
5110 		err = parse_capture_source(codec, spec->mixer_nid,
5111 					   CFG_IDX_MIX, spec->num_all_adcs,
5112 					   "Stereo Mix", 0);
5113 		if (err < 0)
5114 			return err;
5115 	}
5116 
5117 
5118 	err = create_capture_mixers(codec);
5119 	if (err < 0)
5120 		return err;
5121 
5122 	err = parse_mic_boost(codec);
5123 	if (err < 0)
5124 		return err;
5125 
5126 	/* create "Headphone Mic Jack Mode" if no input selection is
5127 	 * available (or user specifies add_jack_modes hint)
5128 	 */
5129 	if (spec->hp_mic_pin &&
5130 	    (spec->auto_mic || spec->input_mux.num_items == 1 ||
5131 	     spec->add_jack_modes)) {
5132 		err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
5133 		if (err < 0)
5134 			return err;
5135 	}
5136 
5137 	if (spec->add_jack_modes) {
5138 		if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
5139 			err = create_out_jack_modes(codec, cfg->line_outs,
5140 						    cfg->line_out_pins);
5141 			if (err < 0)
5142 				return err;
5143 		}
5144 		if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
5145 			err = create_out_jack_modes(codec, cfg->hp_outs,
5146 						    cfg->hp_pins);
5147 			if (err < 0)
5148 				return err;
5149 		}
5150 	}
5151 
5152 	/* add power-up pin callbacks at last */
5153 	add_all_pin_power_ctls(codec, true);
5154 
5155 	/* mute all aamix input initially */
5156 	if (spec->mixer_nid)
5157 		mute_all_mixer_nid(codec, spec->mixer_nid);
5158 
5159  dig_only:
5160 	parse_digital(codec);
5161 
5162 	if (spec->power_down_unused || codec->power_save_node) {
5163 		if (!codec->power_filter)
5164 			codec->power_filter = snd_hda_gen_path_power_filter;
5165 	}
5166 
5167 	if (!spec->no_analog && spec->beep_nid) {
5168 		err = snd_hda_attach_beep_device(codec, spec->beep_nid);
5169 		if (err < 0)
5170 			return err;
5171 		if (codec->beep && codec->power_save_node) {
5172 			err = add_fake_beep_paths(codec);
5173 			if (err < 0)
5174 				return err;
5175 			codec->beep->power_hook = beep_power_hook;
5176 		}
5177 	}
5178 
5179 	return 1;
5180 }
5181 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
5182 
5183 
5184 /*
5185  * Build control elements
5186  */
5187 
5188 /* follower controls for virtual master */
5189 static const char * const follower_pfxs[] = {
5190 	"Front", "Surround", "Center", "LFE", "Side",
5191 	"Headphone", "Speaker", "Mono", "Line Out",
5192 	"CLFE", "Bass Speaker", "PCM",
5193 	"Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5194 	"Headphone Front", "Headphone Surround", "Headphone CLFE",
5195 	"Headphone Side", "Headphone+LO", "Speaker+LO",
5196 	NULL,
5197 };
5198 
5199 /**
5200  * snd_hda_gen_build_controls - Build controls from the parsed results
5201  * @codec: the HDA codec
5202  *
5203  * Pass this to build_controls hda_codec_ops.
5204  */
5205 int snd_hda_gen_build_controls(struct hda_codec *codec)
5206 {
5207 	struct hda_gen_spec *spec = codec->spec;
5208 	int err;
5209 
5210 	if (spec->kctls.used) {
5211 		err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5212 		if (err < 0)
5213 			return err;
5214 	}
5215 
5216 	if (spec->multiout.dig_out_nid) {
5217 		err = snd_hda_create_dig_out_ctls(codec,
5218 						  spec->multiout.dig_out_nid,
5219 						  spec->multiout.dig_out_nid,
5220 						  spec->pcm_rec[1]->pcm_type);
5221 		if (err < 0)
5222 			return err;
5223 		if (!spec->no_analog) {
5224 			err = snd_hda_create_spdif_share_sw(codec,
5225 							    &spec->multiout);
5226 			if (err < 0)
5227 				return err;
5228 			spec->multiout.share_spdif = 1;
5229 		}
5230 	}
5231 	if (spec->dig_in_nid) {
5232 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5233 		if (err < 0)
5234 			return err;
5235 	}
5236 
5237 	/* if we have no master control, let's create it */
5238 	if (!spec->no_analog && !spec->suppress_vmaster &&
5239 	    !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5240 		err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5241 					  spec->vmaster_tlv, follower_pfxs,
5242 					  "Playback Volume", 0);
5243 		if (err < 0)
5244 			return err;
5245 	}
5246 	if (!spec->no_analog && !spec->suppress_vmaster &&
5247 	    !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5248 		err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5249 					    NULL, follower_pfxs,
5250 					    "Playback Switch", true,
5251 					    spec->vmaster_mute_led ?
5252 						SNDRV_CTL_ELEM_ACCESS_SPK_LED : 0,
5253 					    &spec->vmaster_mute.sw_kctl);
5254 		if (err < 0)
5255 			return err;
5256 		if (spec->vmaster_mute.hook) {
5257 			snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute);
5258 			snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5259 		}
5260 	}
5261 
5262 	free_kctls(spec); /* no longer needed */
5263 
5264 	err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5265 	if (err < 0)
5266 		return err;
5267 
5268 	return 0;
5269 }
5270 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5271 
5272 
5273 /*
5274  * PCM definitions
5275  */
5276 
5277 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5278 				   struct hda_codec *codec,
5279 				   struct snd_pcm_substream *substream,
5280 				   int action)
5281 {
5282 	struct hda_gen_spec *spec = codec->spec;
5283 	if (spec->pcm_playback_hook)
5284 		spec->pcm_playback_hook(hinfo, codec, substream, action);
5285 }
5286 
5287 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5288 				  struct hda_codec *codec,
5289 				  struct snd_pcm_substream *substream,
5290 				  int action)
5291 {
5292 	struct hda_gen_spec *spec = codec->spec;
5293 	if (spec->pcm_capture_hook)
5294 		spec->pcm_capture_hook(hinfo, codec, substream, action);
5295 }
5296 
5297 /*
5298  * Analog playback callbacks
5299  */
5300 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5301 			     struct hda_codec *codec,
5302 			     struct snd_pcm_substream *substream)
5303 {
5304 	struct hda_gen_spec *spec = codec->spec;
5305 	int err;
5306 
5307 	guard(mutex)(&spec->pcm_mutex);
5308 	err = snd_hda_multi_out_analog_open(codec,
5309 					    &spec->multiout, substream,
5310 					     hinfo);
5311 	if (err < 0)
5312 		return err;
5313 
5314 	spec->active_streams |= 1 << STREAM_MULTI_OUT;
5315 	call_pcm_playback_hook(hinfo, codec, substream,
5316 			       HDA_GEN_PCM_ACT_OPEN);
5317 	return 0;
5318 }
5319 
5320 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5321 				struct hda_codec *codec,
5322 				unsigned int stream_tag,
5323 				unsigned int format,
5324 				struct snd_pcm_substream *substream)
5325 {
5326 	struct hda_gen_spec *spec = codec->spec;
5327 	int err;
5328 
5329 	err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5330 					       stream_tag, format, substream);
5331 	if (!err)
5332 		call_pcm_playback_hook(hinfo, codec, substream,
5333 				       HDA_GEN_PCM_ACT_PREPARE);
5334 	return err;
5335 }
5336 
5337 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5338 				struct hda_codec *codec,
5339 				struct snd_pcm_substream *substream)
5340 {
5341 	struct hda_gen_spec *spec = codec->spec;
5342 	int err;
5343 
5344 	err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5345 	if (!err)
5346 		call_pcm_playback_hook(hinfo, codec, substream,
5347 				       HDA_GEN_PCM_ACT_CLEANUP);
5348 	return err;
5349 }
5350 
5351 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5352 			      struct hda_codec *codec,
5353 			      struct snd_pcm_substream *substream)
5354 {
5355 	struct hda_gen_spec *spec = codec->spec;
5356 
5357 	guard(mutex)(&spec->pcm_mutex);
5358 	spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5359 	call_pcm_playback_hook(hinfo, codec, substream,
5360 			       HDA_GEN_PCM_ACT_CLOSE);
5361 	return 0;
5362 }
5363 
5364 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5365 			    struct hda_codec *codec,
5366 			    struct snd_pcm_substream *substream)
5367 {
5368 	call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5369 	return 0;
5370 }
5371 
5372 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5373 			       struct hda_codec *codec,
5374 			       unsigned int stream_tag,
5375 			       unsigned int format,
5376 			       struct snd_pcm_substream *substream)
5377 {
5378 	snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5379 	call_pcm_capture_hook(hinfo, codec, substream,
5380 			      HDA_GEN_PCM_ACT_PREPARE);
5381 	return 0;
5382 }
5383 
5384 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5385 			       struct hda_codec *codec,
5386 			       struct snd_pcm_substream *substream)
5387 {
5388 	snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5389 	call_pcm_capture_hook(hinfo, codec, substream,
5390 			      HDA_GEN_PCM_ACT_CLEANUP);
5391 	return 0;
5392 }
5393 
5394 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5395 			     struct hda_codec *codec,
5396 			     struct snd_pcm_substream *substream)
5397 {
5398 	call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5399 	return 0;
5400 }
5401 
5402 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5403 				 struct hda_codec *codec,
5404 				 struct snd_pcm_substream *substream)
5405 {
5406 	struct hda_gen_spec *spec = codec->spec;
5407 	int err = 0;
5408 
5409 	guard(mutex)(&spec->pcm_mutex);
5410 	if (spec->indep_hp && !spec->indep_hp_enabled)
5411 		err = -EBUSY;
5412 	else
5413 		spec->active_streams |= 1 << STREAM_INDEP_HP;
5414 	call_pcm_playback_hook(hinfo, codec, substream,
5415 			       HDA_GEN_PCM_ACT_OPEN);
5416 	return err;
5417 }
5418 
5419 static int alt_playback_pcm_close(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 
5425 	guard(mutex)(&spec->pcm_mutex);
5426 	spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5427 	call_pcm_playback_hook(hinfo, codec, substream,
5428 			       HDA_GEN_PCM_ACT_CLOSE);
5429 	return 0;
5430 }
5431 
5432 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5433 				    struct hda_codec *codec,
5434 				    unsigned int stream_tag,
5435 				    unsigned int format,
5436 				    struct snd_pcm_substream *substream)
5437 {
5438 	snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5439 	call_pcm_playback_hook(hinfo, codec, substream,
5440 			       HDA_GEN_PCM_ACT_PREPARE);
5441 	return 0;
5442 }
5443 
5444 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5445 				    struct hda_codec *codec,
5446 				    struct snd_pcm_substream *substream)
5447 {
5448 	snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5449 	call_pcm_playback_hook(hinfo, codec, substream,
5450 			       HDA_GEN_PCM_ACT_CLEANUP);
5451 	return 0;
5452 }
5453 
5454 /*
5455  * Digital out
5456  */
5457 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5458 				 struct hda_codec *codec,
5459 				 struct snd_pcm_substream *substream)
5460 {
5461 	struct hda_gen_spec *spec = codec->spec;
5462 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5463 }
5464 
5465 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5466 				    struct hda_codec *codec,
5467 				    unsigned int stream_tag,
5468 				    unsigned int format,
5469 				    struct snd_pcm_substream *substream)
5470 {
5471 	struct hda_gen_spec *spec = codec->spec;
5472 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5473 					     stream_tag, format, substream);
5474 }
5475 
5476 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5477 				    struct hda_codec *codec,
5478 				    struct snd_pcm_substream *substream)
5479 {
5480 	struct hda_gen_spec *spec = codec->spec;
5481 	return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5482 }
5483 
5484 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5485 				  struct hda_codec *codec,
5486 				  struct snd_pcm_substream *substream)
5487 {
5488 	struct hda_gen_spec *spec = codec->spec;
5489 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5490 }
5491 
5492 /*
5493  * Analog capture
5494  */
5495 #define alt_capture_pcm_open	capture_pcm_open
5496 #define alt_capture_pcm_close	capture_pcm_close
5497 
5498 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5499 				   struct hda_codec *codec,
5500 				   unsigned int stream_tag,
5501 				   unsigned int format,
5502 				   struct snd_pcm_substream *substream)
5503 {
5504 	struct hda_gen_spec *spec = codec->spec;
5505 
5506 	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5507 				   stream_tag, 0, format);
5508 	call_pcm_capture_hook(hinfo, codec, substream,
5509 			      HDA_GEN_PCM_ACT_PREPARE);
5510 	return 0;
5511 }
5512 
5513 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5514 				   struct hda_codec *codec,
5515 				   struct snd_pcm_substream *substream)
5516 {
5517 	struct hda_gen_spec *spec = codec->spec;
5518 
5519 	snd_hda_codec_cleanup_stream(codec,
5520 				     spec->adc_nids[substream->number + 1]);
5521 	call_pcm_capture_hook(hinfo, codec, substream,
5522 			      HDA_GEN_PCM_ACT_CLEANUP);
5523 	return 0;
5524 }
5525 
5526 /*
5527  */
5528 static const struct hda_pcm_stream pcm_analog_playback = {
5529 	.substreams = 1,
5530 	.channels_min = 2,
5531 	.channels_max = 8,
5532 	/* NID is set in build_pcms */
5533 	.ops = {
5534 		.open = playback_pcm_open,
5535 		.close = playback_pcm_close,
5536 		.prepare = playback_pcm_prepare,
5537 		.cleanup = playback_pcm_cleanup
5538 	},
5539 };
5540 
5541 static const struct hda_pcm_stream pcm_analog_capture = {
5542 	.substreams = 1,
5543 	.channels_min = 2,
5544 	.channels_max = 2,
5545 	/* NID is set in build_pcms */
5546 	.ops = {
5547 		.open = capture_pcm_open,
5548 		.close = capture_pcm_close,
5549 		.prepare = capture_pcm_prepare,
5550 		.cleanup = capture_pcm_cleanup
5551 	},
5552 };
5553 
5554 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5555 	.substreams = 1,
5556 	.channels_min = 2,
5557 	.channels_max = 2,
5558 	/* NID is set in build_pcms */
5559 	.ops = {
5560 		.open = alt_playback_pcm_open,
5561 		.close = alt_playback_pcm_close,
5562 		.prepare = alt_playback_pcm_prepare,
5563 		.cleanup = alt_playback_pcm_cleanup
5564 	},
5565 };
5566 
5567 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5568 	.substreams = 2, /* can be overridden */
5569 	.channels_min = 2,
5570 	.channels_max = 2,
5571 	/* NID is set in build_pcms */
5572 	.ops = {
5573 		.open = alt_capture_pcm_open,
5574 		.close = alt_capture_pcm_close,
5575 		.prepare = alt_capture_pcm_prepare,
5576 		.cleanup = alt_capture_pcm_cleanup
5577 	},
5578 };
5579 
5580 static const struct hda_pcm_stream pcm_digital_playback = {
5581 	.substreams = 1,
5582 	.channels_min = 2,
5583 	.channels_max = 2,
5584 	/* NID is set in build_pcms */
5585 	.ops = {
5586 		.open = dig_playback_pcm_open,
5587 		.close = dig_playback_pcm_close,
5588 		.prepare = dig_playback_pcm_prepare,
5589 		.cleanup = dig_playback_pcm_cleanup
5590 	},
5591 };
5592 
5593 static const struct hda_pcm_stream pcm_digital_capture = {
5594 	.substreams = 1,
5595 	.channels_min = 2,
5596 	.channels_max = 2,
5597 	/* NID is set in build_pcms */
5598 };
5599 
5600 /* Used by build_pcms to flag that a PCM has no playback stream */
5601 static const struct hda_pcm_stream pcm_null_stream = {
5602 	.substreams = 0,
5603 	.channels_min = 0,
5604 	.channels_max = 0,
5605 };
5606 
5607 /*
5608  * dynamic changing ADC PCM streams
5609  */
5610 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5611 {
5612 	struct hda_gen_spec *spec = codec->spec;
5613 	hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5614 
5615 	if (spec->cur_adc && spec->cur_adc != new_adc) {
5616 		/* stream is running, let's swap the current ADC */
5617 		__snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5618 		spec->cur_adc = new_adc;
5619 		snd_hda_codec_setup_stream(codec, new_adc,
5620 					   spec->cur_adc_stream_tag, 0,
5621 					   spec->cur_adc_format);
5622 		return true;
5623 	}
5624 	return false;
5625 }
5626 
5627 /* analog capture with dynamic dual-adc changes */
5628 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5629 				       struct hda_codec *codec,
5630 				       unsigned int stream_tag,
5631 				       unsigned int format,
5632 				       struct snd_pcm_substream *substream)
5633 {
5634 	struct hda_gen_spec *spec = codec->spec;
5635 	spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5636 	spec->cur_adc_stream_tag = stream_tag;
5637 	spec->cur_adc_format = format;
5638 	snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5639 	call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
5640 	return 0;
5641 }
5642 
5643 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5644 				       struct hda_codec *codec,
5645 				       struct snd_pcm_substream *substream)
5646 {
5647 	struct hda_gen_spec *spec = codec->spec;
5648 	snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5649 	spec->cur_adc = 0;
5650 	call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
5651 	return 0;
5652 }
5653 
5654 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5655 	.substreams = 1,
5656 	.channels_min = 2,
5657 	.channels_max = 2,
5658 	.nid = 0, /* fill later */
5659 	.ops = {
5660 		.prepare = dyn_adc_capture_pcm_prepare,
5661 		.cleanup = dyn_adc_capture_pcm_cleanup
5662 	},
5663 };
5664 
5665 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5666 				 const char *chip_name)
5667 {
5668 	char *p;
5669 
5670 	if (*str)
5671 		return;
5672 	strscpy(str, chip_name, len);
5673 
5674 	/* drop non-alnum chars after a space */
5675 	for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5676 		if (!isalnum(p[1])) {
5677 			*p = 0;
5678 			break;
5679 		}
5680 	}
5681 	hda_append_suffix(str, sfx, len);
5682 }
5683 
5684 /* copy PCM stream info from @default_str, and override non-NULL entries
5685  * from @spec_str and @nid
5686  */
5687 static void setup_pcm_stream(struct hda_pcm_stream *str,
5688 			     const struct hda_pcm_stream *default_str,
5689 			     const struct hda_pcm_stream *spec_str,
5690 			     hda_nid_t nid)
5691 {
5692 	*str = *default_str;
5693 	if (nid)
5694 		str->nid = nid;
5695 	if (spec_str) {
5696 		if (spec_str->substreams)
5697 			str->substreams = spec_str->substreams;
5698 		if (spec_str->channels_min)
5699 			str->channels_min = spec_str->channels_min;
5700 		if (spec_str->channels_max)
5701 			str->channels_max = spec_str->channels_max;
5702 		if (spec_str->rates)
5703 			str->rates = spec_str->rates;
5704 		if (spec_str->formats)
5705 			str->formats = spec_str->formats;
5706 		if (spec_str->maxbps)
5707 			str->maxbps = spec_str->maxbps;
5708 	}
5709 }
5710 
5711 /**
5712  * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5713  * @codec: the HDA codec
5714  *
5715  * Pass this to build_pcms hda_codec_ops.
5716  */
5717 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5718 {
5719 	struct hda_gen_spec *spec = codec->spec;
5720 	struct hda_pcm *info;
5721 	bool have_multi_adcs;
5722 
5723 	if (spec->no_analog)
5724 		goto skip_analog;
5725 
5726 	fill_pcm_stream_name(spec->stream_name_analog,
5727 			     sizeof(spec->stream_name_analog),
5728 			     " Analog", codec->core.chip_name);
5729 	info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5730 	if (!info)
5731 		return -ENOMEM;
5732 	spec->pcm_rec[0] = info;
5733 
5734 	if (spec->multiout.num_dacs > 0) {
5735 		setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5736 				 &pcm_analog_playback,
5737 				 spec->stream_analog_playback,
5738 				 spec->multiout.dac_nids[0]);
5739 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5740 			spec->multiout.max_channels;
5741 		if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5742 		    spec->autocfg.line_outs == 2)
5743 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5744 				snd_pcm_2_1_chmaps;
5745 	}
5746 	if (spec->num_adc_nids) {
5747 		setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5748 				 (spec->dyn_adc_switch ?
5749 				  &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5750 				 spec->stream_analog_capture,
5751 				 spec->adc_nids[0]);
5752 	}
5753 
5754  skip_analog:
5755 	/* SPDIF for stream index #1 */
5756 	if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5757 		fill_pcm_stream_name(spec->stream_name_digital,
5758 				     sizeof(spec->stream_name_digital),
5759 				     " Digital", codec->core.chip_name);
5760 		info = snd_hda_codec_pcm_new(codec, "%s",
5761 					     spec->stream_name_digital);
5762 		if (!info)
5763 			return -ENOMEM;
5764 		codec->follower_dig_outs = spec->multiout.follower_dig_outs;
5765 		spec->pcm_rec[1] = info;
5766 		if (spec->dig_out_type)
5767 			info->pcm_type = spec->dig_out_type;
5768 		else
5769 			info->pcm_type = HDA_PCM_TYPE_SPDIF;
5770 		if (spec->multiout.dig_out_nid)
5771 			setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5772 					 &pcm_digital_playback,
5773 					 spec->stream_digital_playback,
5774 					 spec->multiout.dig_out_nid);
5775 		if (spec->dig_in_nid)
5776 			setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5777 					 &pcm_digital_capture,
5778 					 spec->stream_digital_capture,
5779 					 spec->dig_in_nid);
5780 	}
5781 
5782 	if (spec->no_analog)
5783 		return 0;
5784 
5785 	/* If the use of more than one ADC is requested for the current
5786 	 * model, configure a second analog capture-only PCM.
5787 	 */
5788 	have_multi_adcs = (spec->num_adc_nids > 1) &&
5789 		!spec->dyn_adc_switch && !spec->auto_mic;
5790 	/* Additional Analaog capture for index #2 */
5791 	if (spec->alt_dac_nid || have_multi_adcs) {
5792 		fill_pcm_stream_name(spec->stream_name_alt_analog,
5793 				     sizeof(spec->stream_name_alt_analog),
5794 			     " Alt Analog", codec->core.chip_name);
5795 		info = snd_hda_codec_pcm_new(codec, "%s",
5796 					     spec->stream_name_alt_analog);
5797 		if (!info)
5798 			return -ENOMEM;
5799 		spec->pcm_rec[2] = info;
5800 		if (spec->alt_dac_nid)
5801 			setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5802 					 &pcm_analog_alt_playback,
5803 					 spec->stream_analog_alt_playback,
5804 					 spec->alt_dac_nid);
5805 		else
5806 			setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5807 					 &pcm_null_stream, NULL, 0);
5808 		if (have_multi_adcs) {
5809 			setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5810 					 &pcm_analog_alt_capture,
5811 					 spec->stream_analog_alt_capture,
5812 					 spec->adc_nids[1]);
5813 			info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5814 				spec->num_adc_nids - 1;
5815 		} else {
5816 			setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5817 					 &pcm_null_stream, NULL, 0);
5818 		}
5819 	}
5820 
5821 	return 0;
5822 }
5823 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5824 
5825 
5826 /*
5827  * Standard auto-parser initializations
5828  */
5829 
5830 /* configure the given path as a proper output */
5831 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5832 {
5833 	struct nid_path *path;
5834 	hda_nid_t pin;
5835 
5836 	path = snd_hda_get_path_from_idx(codec, path_idx);
5837 	if (!path || !path->depth)
5838 		return;
5839 	pin = path->path[path->depth - 1];
5840 	restore_pin_ctl(codec, pin);
5841 	snd_hda_activate_path(codec, path, path->active,
5842 			      aamix_default(codec->spec));
5843 	set_pin_eapd(codec, pin, path->active);
5844 }
5845 
5846 /* initialize primary output paths */
5847 static void init_multi_out(struct hda_codec *codec)
5848 {
5849 	struct hda_gen_spec *spec = codec->spec;
5850 	int i;
5851 
5852 	for (i = 0; i < spec->autocfg.line_outs; i++)
5853 		set_output_and_unmute(codec, spec->out_paths[i]);
5854 }
5855 
5856 
5857 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5858 {
5859 	int i;
5860 
5861 	for (i = 0; i < num_outs; i++)
5862 		set_output_and_unmute(codec, paths[i]);
5863 }
5864 
5865 /* initialize hp and speaker paths */
5866 static void init_extra_out(struct hda_codec *codec)
5867 {
5868 	struct hda_gen_spec *spec = codec->spec;
5869 
5870 	if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5871 		__init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5872 	if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5873 		__init_extra_out(codec, spec->autocfg.speaker_outs,
5874 				 spec->speaker_paths);
5875 }
5876 
5877 /* initialize multi-io paths */
5878 static void init_multi_io(struct hda_codec *codec)
5879 {
5880 	struct hda_gen_spec *spec = codec->spec;
5881 	int i;
5882 
5883 	for (i = 0; i < spec->multi_ios; i++) {
5884 		hda_nid_t pin = spec->multi_io[i].pin;
5885 		struct nid_path *path;
5886 		path = get_multiio_path(codec, i);
5887 		if (!path)
5888 			continue;
5889 		if (!spec->multi_io[i].ctl_in)
5890 			spec->multi_io[i].ctl_in =
5891 				snd_hda_codec_get_pin_target(codec, pin);
5892 		snd_hda_activate_path(codec, path, path->active,
5893 				      aamix_default(spec));
5894 	}
5895 }
5896 
5897 static void init_aamix_paths(struct hda_codec *codec)
5898 {
5899 	struct hda_gen_spec *spec = codec->spec;
5900 
5901 	if (!spec->have_aamix_ctl)
5902 		return;
5903 	if (!has_aamix_out_paths(spec))
5904 		return;
5905 	update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5906 			   spec->aamix_out_paths[0],
5907 			   spec->autocfg.line_out_type);
5908 	update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5909 			   spec->aamix_out_paths[1],
5910 			   AUTO_PIN_HP_OUT);
5911 	update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5912 			   spec->aamix_out_paths[2],
5913 			   AUTO_PIN_SPEAKER_OUT);
5914 }
5915 
5916 /* set up input pins and loopback paths */
5917 static void init_analog_input(struct hda_codec *codec)
5918 {
5919 	struct hda_gen_spec *spec = codec->spec;
5920 	struct auto_pin_cfg *cfg = &spec->autocfg;
5921 	int i;
5922 
5923 	for (i = 0; i < cfg->num_inputs; i++) {
5924 		hda_nid_t nid = cfg->inputs[i].pin;
5925 		if (is_input_pin(codec, nid))
5926 			restore_pin_ctl(codec, nid);
5927 
5928 		/* init loopback inputs */
5929 		if (spec->mixer_nid) {
5930 			resume_path_from_idx(codec, spec->loopback_paths[i]);
5931 			resume_path_from_idx(codec, spec->loopback_merge_path);
5932 		}
5933 	}
5934 }
5935 
5936 /* initialize ADC paths */
5937 static void init_input_src(struct hda_codec *codec)
5938 {
5939 	struct hda_gen_spec *spec = codec->spec;
5940 	struct hda_input_mux *imux = &spec->input_mux;
5941 	struct nid_path *path;
5942 	int i, c, nums;
5943 
5944 	if (spec->dyn_adc_switch)
5945 		nums = 1;
5946 	else
5947 		nums = spec->num_adc_nids;
5948 
5949 	for (c = 0; c < nums; c++) {
5950 		for (i = 0; i < imux->num_items; i++) {
5951 			path = get_input_path(codec, c, i);
5952 			if (path) {
5953 				bool active = path->active;
5954 				if (i == spec->cur_mux[c])
5955 					active = true;
5956 				snd_hda_activate_path(codec, path, active, false);
5957 			}
5958 		}
5959 		if (spec->hp_mic)
5960 			update_hp_mic(codec, c, true);
5961 	}
5962 
5963 	if (spec->cap_sync_hook)
5964 		spec->cap_sync_hook(codec, NULL, NULL);
5965 }
5966 
5967 /* set right pin controls for digital I/O */
5968 static void init_digital(struct hda_codec *codec)
5969 {
5970 	struct hda_gen_spec *spec = codec->spec;
5971 	int i;
5972 	hda_nid_t pin;
5973 
5974 	for (i = 0; i < spec->autocfg.dig_outs; i++)
5975 		set_output_and_unmute(codec, spec->digout_paths[i]);
5976 	pin = spec->autocfg.dig_in_pin;
5977 	if (pin) {
5978 		restore_pin_ctl(codec, pin);
5979 		resume_path_from_idx(codec, spec->digin_path);
5980 	}
5981 }
5982 
5983 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5984  * invalid unsol tags by some reason
5985  */
5986 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5987 {
5988 	const struct hda_pincfg *pin;
5989 	int i;
5990 
5991 	snd_array_for_each(&codec->init_pins, i, pin) {
5992 		hda_nid_t nid = pin->nid;
5993 		if (is_jack_detectable(codec, nid) &&
5994 		    !snd_hda_jack_tbl_get(codec, nid))
5995 			snd_hda_codec_write_cache(codec, nid, 0,
5996 					AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5997 	}
5998 }
5999 
6000 /**
6001  * snd_hda_gen_init - initialize the generic spec
6002  * @codec: the HDA codec
6003  *
6004  * This can be put as hda_codec_ops init function.
6005  */
6006 int snd_hda_gen_init(struct hda_codec *codec)
6007 {
6008 	struct hda_gen_spec *spec = codec->spec;
6009 
6010 	if (spec->init_hook)
6011 		spec->init_hook(codec);
6012 
6013 	if (!spec->skip_verbs)
6014 		snd_hda_apply_verbs(codec);
6015 
6016 	init_multi_out(codec);
6017 	init_extra_out(codec);
6018 	init_multi_io(codec);
6019 	init_aamix_paths(codec);
6020 	init_analog_input(codec);
6021 	init_input_src(codec);
6022 	init_digital(codec);
6023 
6024 	clear_unsol_on_unused_pins(codec);
6025 
6026 	sync_all_pin_power_ctls(codec);
6027 
6028 	/* call init functions of standard auto-mute helpers */
6029 	update_automute_all(codec);
6030 
6031 	snd_hda_regmap_sync(codec);
6032 
6033 	if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
6034 		snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
6035 
6036 	hda_call_check_power_status(codec, 0x01);
6037 	return 0;
6038 }
6039 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
6040 
6041 /**
6042  * snd_hda_gen_remove - free the generic spec
6043  * @codec: the HDA codec
6044  *
6045  * This can be put as hda_codec_ops remove function.
6046  */
6047 void snd_hda_gen_remove(struct hda_codec *codec)
6048 {
6049 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
6050 	snd_hda_gen_spec_free(codec->spec);
6051 	kfree(codec->spec);
6052 	codec->spec = NULL;
6053 }
6054 EXPORT_SYMBOL_GPL(snd_hda_gen_remove);
6055 
6056 /**
6057  * snd_hda_gen_check_power_status - check the loopback power save state
6058  * @codec: the HDA codec
6059  * @nid: NID to inspect
6060  *
6061  * This can be put as hda_codec_ops check_power_status function.
6062  */
6063 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
6064 {
6065 	struct hda_gen_spec *spec = codec->spec;
6066 	return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
6067 }
6068 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
6069 
6070 
6071 /*
6072  * the generic codec support
6073  */
6074 
6075 static int snd_hda_gen_probe(struct hda_codec *codec,
6076 			     const struct hda_device_id *id)
6077 {
6078 	struct hda_gen_spec *spec;
6079 	int err;
6080 
6081 	spec = kzalloc_obj(*spec);
6082 	if (!spec)
6083 		return -ENOMEM;
6084 	snd_hda_gen_spec_init(spec);
6085 	codec->spec = spec;
6086 
6087 	err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
6088 	if (err < 0)
6089 		goto error;
6090 
6091 	err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
6092 	if (err < 0)
6093 		goto error;
6094 
6095 	return 0;
6096 
6097 error:
6098 	snd_hda_gen_remove(codec);
6099 	return err;
6100 }
6101 
6102 static const struct hda_codec_ops generic_codec_ops = {
6103 	.probe = snd_hda_gen_probe,
6104 	.remove = snd_hda_gen_remove,
6105 	.build_controls = snd_hda_gen_build_controls,
6106 	.build_pcms = snd_hda_gen_build_pcms,
6107 	.init = snd_hda_gen_init,
6108 	.unsol_event = snd_hda_jack_unsol_event,
6109 	.check_power_status = snd_hda_gen_check_power_status,
6110 	.stream_pm = snd_hda_gen_stream_pm,
6111 };
6112 
6113 static const struct hda_device_id snd_hda_id_generic[] = {
6114 	HDA_CODEC_ID(0x1af40021, "Generic"), /* QEMU */
6115 	HDA_CODEC_ID(HDA_CODEC_ID_GENERIC, "Generic"),
6116 	{} /* terminator */
6117 };
6118 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
6119 
6120 static struct hda_codec_driver generic_driver = {
6121 	.id = snd_hda_id_generic,
6122 	.ops = &generic_codec_ops,
6123 };
6124 
6125 module_hda_codec_driver(generic_driver);
6126 
6127 MODULE_LICENSE("GPL");
6128 MODULE_DESCRIPTION("Generic HD-audio codec parser");
6129