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