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