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