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