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