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