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