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