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