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