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