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