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