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