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