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