OSDN Git Service

ALSA: hda/realtek - Fix overridden device-specific initialization
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / sound / pci / hda / hda_generic.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * Generic widget tree parser
5  *
6  * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7  *
8  *  This driver is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This driver is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/delay.h>
28 #include <linux/ctype.h>
29 #include <linux/string.h>
30 #include <linux/bitops.h>
31 #include <linux/module.h>
32 #include <sound/core.h>
33 #include <sound/jack.h>
34 #include <sound/tlv.h>
35 #include "hda_codec.h"
36 #include "hda_local.h"
37 #include "hda_auto_parser.h"
38 #include "hda_jack.h"
39 #include "hda_beep.h"
40 #include "hda_generic.h"
41
42
43 /**
44  * snd_hda_gen_spec_init - initialize hda_gen_spec struct
45  * @spec: hda_gen_spec object to initialize
46  *
47  * Initialize the given hda_gen_spec object.
48  */
49 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
50 {
51         snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
52         snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
53         snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
54         mutex_init(&spec->pcm_mutex);
55         return 0;
56 }
57 EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
58
59 /**
60  * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
61  * @spec: hda_gen_spec object
62  * @name: name string to override the template, NULL if unchanged
63  * @temp: template for the new kctl
64  *
65  * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
66  * element based on the given snd_kcontrol_new template @temp and the
67  * name string @name to the list in @spec.
68  * Returns the newly created object or NULL as error.
69  */
70 struct snd_kcontrol_new *
71 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
72                      const struct snd_kcontrol_new *temp)
73 {
74         struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
75         if (!knew)
76                 return NULL;
77         *knew = *temp;
78         if (name)
79                 knew->name = kstrdup(name, GFP_KERNEL);
80         else if (knew->name)
81                 knew->name = kstrdup(knew->name, GFP_KERNEL);
82         if (!knew->name)
83                 return NULL;
84         return knew;
85 }
86 EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
87
88 static void free_kctls(struct hda_gen_spec *spec)
89 {
90         if (spec->kctls.list) {
91                 struct snd_kcontrol_new *kctl = spec->kctls.list;
92                 int i;
93                 for (i = 0; i < spec->kctls.used; i++)
94                         kfree(kctl[i].name);
95         }
96         snd_array_free(&spec->kctls);
97 }
98
99 static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
100 {
101         if (!spec)
102                 return;
103         free_kctls(spec);
104         snd_array_free(&spec->paths);
105         snd_array_free(&spec->loopback_list);
106 }
107
108 /*
109  * store user hints
110  */
111 static void parse_user_hints(struct hda_codec *codec)
112 {
113         struct hda_gen_spec *spec = codec->spec;
114         int val;
115
116         val = snd_hda_get_bool_hint(codec, "jack_detect");
117         if (val >= 0)
118                 codec->no_jack_detect = !val;
119         val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
120         if (val >= 0)
121                 codec->inv_jack_detect = !!val;
122         val = snd_hda_get_bool_hint(codec, "trigger_sense");
123         if (val >= 0)
124                 codec->no_trigger_sense = !val;
125         val = snd_hda_get_bool_hint(codec, "inv_eapd");
126         if (val >= 0)
127                 codec->inv_eapd = !!val;
128         val = snd_hda_get_bool_hint(codec, "pcm_format_first");
129         if (val >= 0)
130                 codec->pcm_format_first = !!val;
131         val = snd_hda_get_bool_hint(codec, "sticky_stream");
132         if (val >= 0)
133                 codec->no_sticky_stream = !val;
134         val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
135         if (val >= 0)
136                 codec->spdif_status_reset = !!val;
137         val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
138         if (val >= 0)
139                 codec->pin_amp_workaround = !!val;
140         val = snd_hda_get_bool_hint(codec, "single_adc_amp");
141         if (val >= 0)
142                 codec->single_adc_amp = !!val;
143         val = snd_hda_get_bool_hint(codec, "power_save_node");
144         if (val >= 0)
145                 codec->power_save_node = !!val;
146
147         val = snd_hda_get_bool_hint(codec, "auto_mute");
148         if (val >= 0)
149                 spec->suppress_auto_mute = !val;
150         val = snd_hda_get_bool_hint(codec, "auto_mic");
151         if (val >= 0)
152                 spec->suppress_auto_mic = !val;
153         val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
154         if (val >= 0)
155                 spec->line_in_auto_switch = !!val;
156         val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
157         if (val >= 0)
158                 spec->auto_mute_via_amp = !!val;
159         val = snd_hda_get_bool_hint(codec, "need_dac_fix");
160         if (val >= 0)
161                 spec->need_dac_fix = !!val;
162         val = snd_hda_get_bool_hint(codec, "primary_hp");
163         if (val >= 0)
164                 spec->no_primary_hp = !val;
165         val = snd_hda_get_bool_hint(codec, "multi_io");
166         if (val >= 0)
167                 spec->no_multi_io = !val;
168         val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
169         if (val >= 0)
170                 spec->multi_cap_vol = !!val;
171         val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
172         if (val >= 0)
173                 spec->inv_dmic_split = !!val;
174         val = snd_hda_get_bool_hint(codec, "indep_hp");
175         if (val >= 0)
176                 spec->indep_hp = !!val;
177         val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
178         if (val >= 0)
179                 spec->add_stereo_mix_input = !!val;
180         /* the following two are just for compatibility */
181         val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
182         if (val >= 0)
183                 spec->add_jack_modes = !!val;
184         val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
185         if (val >= 0)
186                 spec->add_jack_modes = !!val;
187         val = snd_hda_get_bool_hint(codec, "add_jack_modes");
188         if (val >= 0)
189                 spec->add_jack_modes = !!val;
190         val = snd_hda_get_bool_hint(codec, "power_down_unused");
191         if (val >= 0)
192                 spec->power_down_unused = !!val;
193         val = snd_hda_get_bool_hint(codec, "add_hp_mic");
194         if (val >= 0)
195                 spec->hp_mic = !!val;
196         val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
197         if (val >= 0)
198                 spec->suppress_hp_mic_detect = !val;
199
200         if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
201                 spec->mixer_nid = val;
202 }
203
204 /*
205  * pin control value accesses
206  */
207
208 #define update_pin_ctl(codec, pin, val) \
209         snd_hda_codec_update_cache(codec, pin, 0, \
210                                    AC_VERB_SET_PIN_WIDGET_CONTROL, val)
211
212 /* restore the pinctl based on the cached value */
213 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
214 {
215         update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
216 }
217
218 /* set the pinctl target value and write it if requested */
219 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
220                            unsigned int val, bool do_write)
221 {
222         if (!pin)
223                 return;
224         val = snd_hda_correct_pin_ctl(codec, pin, val);
225         snd_hda_codec_set_pin_target(codec, pin, val);
226         if (do_write)
227                 update_pin_ctl(codec, pin, val);
228 }
229
230 /* set pinctl target values for all given pins */
231 static void set_pin_targets(struct hda_codec *codec, int num_pins,
232                             hda_nid_t *pins, unsigned int val)
233 {
234         int i;
235         for (i = 0; i < num_pins; i++)
236                 set_pin_target(codec, pins[i], val, false);
237 }
238
239 /*
240  * parsing paths
241  */
242
243 /* return the position of NID in the list, or -1 if not found */
244 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
245 {
246         int i;
247         for (i = 0; i < nums; i++)
248                 if (list[i] == nid)
249                         return i;
250         return -1;
251 }
252
253 /* return true if the given NID is contained in the path */
254 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
255 {
256         return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
257 }
258
259 static struct nid_path *get_nid_path(struct hda_codec *codec,
260                                      hda_nid_t from_nid, hda_nid_t to_nid,
261                                      int anchor_nid)
262 {
263         struct hda_gen_spec *spec = codec->spec;
264         int i;
265
266         for (i = 0; i < spec->paths.used; i++) {
267                 struct nid_path *path = snd_array_elem(&spec->paths, i);
268                 if (path->depth <= 0)
269                         continue;
270                 if ((!from_nid || path->path[0] == from_nid) &&
271                     (!to_nid || path->path[path->depth - 1] == to_nid)) {
272                         if (!anchor_nid ||
273                             (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
274                             (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
275                                 return path;
276                 }
277         }
278         return NULL;
279 }
280
281 /**
282  * snd_hda_get_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                                         spec->input_paths[i][n] = 0;
3194                                 }
3195                         }
3196                         nums++;
3197                 }
3198                 spec->num_adc_nids = nums;
3199         }
3200
3201         if (imux->num_items == 1 ||
3202             (imux->num_items == 2 && spec->hp_mic)) {
3203                 codec_dbg(codec, "reducing to a single ADC\n");
3204                 spec->num_adc_nids = 1; /* reduce to a single ADC */
3205         }
3206
3207         /* single index for individual volumes ctls */
3208         if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3209                 spec->num_adc_nids = 1;
3210
3211         return 0;
3212 }
3213
3214 /* parse capture source paths from the given pin and create imux items */
3215 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3216                                 int cfg_idx, int num_adcs,
3217                                 const char *label, int anchor)
3218 {
3219         struct hda_gen_spec *spec = codec->spec;
3220         struct hda_input_mux *imux = &spec->input_mux;
3221         int imux_idx = imux->num_items;
3222         bool imux_added = false;
3223         int c;
3224
3225         for (c = 0; c < num_adcs; c++) {
3226                 struct nid_path *path;
3227                 hda_nid_t adc = spec->adc_nids[c];
3228
3229                 if (!is_reachable_path(codec, pin, adc))
3230                         continue;
3231                 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3232                 if (!path)
3233                         continue;
3234                 print_nid_path(codec, "input", path);
3235                 spec->input_paths[imux_idx][c] =
3236                         snd_hda_get_path_idx(codec, path);
3237
3238                 if (!imux_added) {
3239                         if (spec->hp_mic_pin == pin)
3240                                 spec->hp_mic_mux_idx = imux->num_items;
3241                         spec->imux_pins[imux->num_items] = pin;
3242                         snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3243                         imux_added = true;
3244                         if (spec->dyn_adc_switch)
3245                                 spec->dyn_adc_idx[imux_idx] = c;
3246                 }
3247         }
3248
3249         return 0;
3250 }
3251
3252 /*
3253  * create playback/capture controls for input pins
3254  */
3255
3256 /* fill the label for each input at first */
3257 static int fill_input_pin_labels(struct hda_codec *codec)
3258 {
3259         struct hda_gen_spec *spec = codec->spec;
3260         const struct auto_pin_cfg *cfg = &spec->autocfg;
3261         int i;
3262
3263         for (i = 0; i < cfg->num_inputs; i++) {
3264                 hda_nid_t pin = cfg->inputs[i].pin;
3265                 const char *label;
3266                 int j, idx;
3267
3268                 if (!is_input_pin(codec, pin))
3269                         continue;
3270
3271                 label = hda_get_autocfg_input_label(codec, cfg, i);
3272                 idx = 0;
3273                 for (j = i - 1; j >= 0; j--) {
3274                         if (spec->input_labels[j] &&
3275                             !strcmp(spec->input_labels[j], label)) {
3276                                 idx = spec->input_label_idxs[j] + 1;
3277                                 break;
3278                         }
3279                 }
3280
3281                 spec->input_labels[i] = label;
3282                 spec->input_label_idxs[i] = idx;
3283         }
3284
3285         return 0;
3286 }
3287
3288 #define CFG_IDX_MIX     99      /* a dummy cfg->input idx for stereo mix */
3289
3290 static int create_input_ctls(struct hda_codec *codec)
3291 {
3292         struct hda_gen_spec *spec = codec->spec;
3293         const struct auto_pin_cfg *cfg = &spec->autocfg;
3294         hda_nid_t mixer = spec->mixer_nid;
3295         int num_adcs;
3296         int i, err;
3297         unsigned int val;
3298
3299         num_adcs = fill_adc_nids(codec);
3300         if (num_adcs < 0)
3301                 return 0;
3302
3303         err = fill_input_pin_labels(codec);
3304         if (err < 0)
3305                 return err;
3306
3307         for (i = 0; i < cfg->num_inputs; i++) {
3308                 hda_nid_t pin;
3309
3310                 pin = cfg->inputs[i].pin;
3311                 if (!is_input_pin(codec, pin))
3312                         continue;
3313
3314                 val = PIN_IN;
3315                 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3316                         val |= snd_hda_get_default_vref(codec, pin);
3317                 if (pin != spec->hp_mic_pin &&
3318                     !snd_hda_codec_get_pin_target(codec, pin))
3319                         set_pin_target(codec, pin, val, false);
3320
3321                 if (mixer) {
3322                         if (is_reachable_path(codec, pin, mixer)) {
3323                                 err = new_analog_input(codec, i, pin,
3324                                                        spec->input_labels[i],
3325                                                        spec->input_label_idxs[i],
3326                                                        mixer);
3327                                 if (err < 0)
3328                                         return err;
3329                         }
3330                 }
3331
3332                 err = parse_capture_source(codec, pin, i, num_adcs,
3333                                            spec->input_labels[i], -mixer);
3334                 if (err < 0)
3335                         return err;
3336
3337                 if (spec->add_jack_modes) {
3338                         err = create_in_jack_mode(codec, pin);
3339                         if (err < 0)
3340                                 return err;
3341                 }
3342         }
3343
3344         /* add stereo mix when explicitly enabled via hint */
3345         if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3346                 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3347                                            "Stereo Mix", 0);
3348                 if (err < 0)
3349                         return err;
3350                 else
3351                         spec->suppress_auto_mic = 1;
3352         }
3353
3354         return 0;
3355 }
3356
3357
3358 /*
3359  * input source mux
3360  */
3361
3362 /* get the input path specified by the given adc and imux indices */
3363 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3364 {
3365         struct hda_gen_spec *spec = codec->spec;
3366         if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3367                 snd_BUG();
3368                 return NULL;
3369         }
3370         if (spec->dyn_adc_switch)
3371                 adc_idx = spec->dyn_adc_idx[imux_idx];
3372         if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3373                 snd_BUG();
3374                 return NULL;
3375         }
3376         return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3377 }
3378
3379 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3380                       unsigned int idx);
3381
3382 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3383                          struct snd_ctl_elem_info *uinfo)
3384 {
3385         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3386         struct hda_gen_spec *spec = codec->spec;
3387         return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3388 }
3389
3390 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3391                         struct snd_ctl_elem_value *ucontrol)
3392 {
3393         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3394         struct hda_gen_spec *spec = codec->spec;
3395         /* the ctls are created at once with multiple counts */
3396         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3397
3398         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3399         return 0;
3400 }
3401
3402 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3403                             struct snd_ctl_elem_value *ucontrol)
3404 {
3405         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3406         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3407         return mux_select(codec, adc_idx,
3408                           ucontrol->value.enumerated.item[0]);
3409 }
3410
3411 static const struct snd_kcontrol_new cap_src_temp = {
3412         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3413         .name = "Input Source",
3414         .info = mux_enum_info,
3415         .get = mux_enum_get,
3416         .put = mux_enum_put,
3417 };
3418
3419 /*
3420  * capture volume and capture switch ctls
3421  */
3422
3423 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3424                           struct snd_ctl_elem_value *ucontrol);
3425
3426 /* call the given amp update function for all amps in the imux list at once */
3427 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3428                           struct snd_ctl_elem_value *ucontrol,
3429                           put_call_t func, int type)
3430 {
3431         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3432         struct hda_gen_spec *spec = codec->spec;
3433         const struct hda_input_mux *imux;
3434         struct nid_path *path;
3435         int i, adc_idx, err = 0;
3436
3437         imux = &spec->input_mux;
3438         adc_idx = kcontrol->id.index;
3439         mutex_lock(&codec->control_mutex);
3440         for (i = 0; i < imux->num_items; i++) {
3441                 path = get_input_path(codec, adc_idx, i);
3442                 if (!path || !path->ctls[type])
3443                         continue;
3444                 kcontrol->private_value = path->ctls[type];
3445                 err = func(kcontrol, ucontrol);
3446                 if (err < 0)
3447                         break;
3448         }
3449         mutex_unlock(&codec->control_mutex);
3450         if (err >= 0 && spec->cap_sync_hook)
3451                 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3452         return err;
3453 }
3454
3455 /* capture volume ctl callbacks */
3456 #define cap_vol_info            snd_hda_mixer_amp_volume_info
3457 #define cap_vol_get             snd_hda_mixer_amp_volume_get
3458 #define cap_vol_tlv             snd_hda_mixer_amp_tlv
3459
3460 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3461                        struct snd_ctl_elem_value *ucontrol)
3462 {
3463         return cap_put_caller(kcontrol, ucontrol,
3464                               snd_hda_mixer_amp_volume_put,
3465                               NID_PATH_VOL_CTL);
3466 }
3467
3468 static const struct snd_kcontrol_new cap_vol_temp = {
3469         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3470         .name = "Capture Volume",
3471         .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3472                    SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3473                    SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3474         .info = cap_vol_info,
3475         .get = cap_vol_get,
3476         .put = cap_vol_put,
3477         .tlv = { .c = cap_vol_tlv },
3478 };
3479
3480 /* capture switch ctl callbacks */
3481 #define cap_sw_info             snd_ctl_boolean_stereo_info
3482 #define cap_sw_get              snd_hda_mixer_amp_switch_get
3483
3484 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3485                       struct snd_ctl_elem_value *ucontrol)
3486 {
3487         return cap_put_caller(kcontrol, ucontrol,
3488                               snd_hda_mixer_amp_switch_put,
3489                               NID_PATH_MUTE_CTL);
3490 }
3491
3492 static const struct snd_kcontrol_new cap_sw_temp = {
3493         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3494         .name = "Capture Switch",
3495         .info = cap_sw_info,
3496         .get = cap_sw_get,
3497         .put = cap_sw_put,
3498 };
3499
3500 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3501 {
3502         hda_nid_t nid;
3503         int i, depth;
3504
3505         path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3506         for (depth = 0; depth < 3; depth++) {
3507                 if (depth >= path->depth)
3508                         return -EINVAL;
3509                 i = path->depth - depth - 1;
3510                 nid = path->path[i];
3511                 if (!path->ctls[NID_PATH_VOL_CTL]) {
3512                         if (nid_has_volume(codec, nid, HDA_OUTPUT))
3513                                 path->ctls[NID_PATH_VOL_CTL] =
3514                                         HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3515                         else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3516                                 int idx = path->idx[i];
3517                                 if (!depth && codec->single_adc_amp)
3518                                         idx = 0;
3519                                 path->ctls[NID_PATH_VOL_CTL] =
3520                                         HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3521                         }
3522                 }
3523                 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3524                         if (nid_has_mute(codec, nid, HDA_OUTPUT))
3525                                 path->ctls[NID_PATH_MUTE_CTL] =
3526                                         HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3527                         else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3528                                 int idx = path->idx[i];
3529                                 if (!depth && codec->single_adc_amp)
3530                                         idx = 0;
3531                                 path->ctls[NID_PATH_MUTE_CTL] =
3532                                         HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3533                         }
3534                 }
3535         }
3536         return 0;
3537 }
3538
3539 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3540 {
3541         struct hda_gen_spec *spec = codec->spec;
3542         struct auto_pin_cfg *cfg = &spec->autocfg;
3543         unsigned int val;
3544         int i;
3545
3546         if (!spec->inv_dmic_split)
3547                 return false;
3548         for (i = 0; i < cfg->num_inputs; i++) {
3549                 if (cfg->inputs[i].pin != nid)
3550                         continue;
3551                 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3552                         return false;
3553                 val = snd_hda_codec_get_pincfg(codec, nid);
3554                 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3555         }
3556         return false;
3557 }
3558
3559 /* capture switch put callback for a single control with hook call */
3560 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3561                              struct snd_ctl_elem_value *ucontrol)
3562 {
3563         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3564         struct hda_gen_spec *spec = codec->spec;
3565         int ret;
3566
3567         ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3568         if (ret < 0)
3569                 return ret;
3570
3571         if (spec->cap_sync_hook)
3572                 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3573
3574         return ret;
3575 }
3576
3577 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3578                               int idx, bool is_switch, unsigned int ctl,
3579                               bool inv_dmic)
3580 {
3581         struct hda_gen_spec *spec = codec->spec;
3582         char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3583         int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3584         const char *sfx = is_switch ? "Switch" : "Volume";
3585         unsigned int chs = inv_dmic ? 1 : 3;
3586         struct snd_kcontrol_new *knew;
3587
3588         if (!ctl)
3589                 return 0;
3590
3591         if (label)
3592                 snprintf(tmpname, sizeof(tmpname),
3593                          "%s Capture %s", label, sfx);
3594         else
3595                 snprintf(tmpname, sizeof(tmpname),
3596                          "Capture %s", sfx);
3597         knew = add_control(spec, type, tmpname, idx,
3598                            amp_val_replace_channels(ctl, chs));
3599         if (!knew)
3600                 return -ENOMEM;
3601         if (is_switch)
3602                 knew->put = cap_single_sw_put;
3603         if (!inv_dmic)
3604                 return 0;
3605
3606         /* Make independent right kcontrol */
3607         if (label)
3608                 snprintf(tmpname, sizeof(tmpname),
3609                          "Inverted %s Capture %s", label, sfx);
3610         else
3611                 snprintf(tmpname, sizeof(tmpname),
3612                          "Inverted Capture %s", sfx);
3613         knew = add_control(spec, type, tmpname, idx,
3614                            amp_val_replace_channels(ctl, 2));
3615         if (!knew)
3616                 return -ENOMEM;
3617         if (is_switch)
3618                 knew->put = cap_single_sw_put;
3619         return 0;
3620 }
3621
3622 /* create single (and simple) capture volume and switch controls */
3623 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3624                                      unsigned int vol_ctl, unsigned int sw_ctl,
3625                                      bool inv_dmic)
3626 {
3627         int err;
3628         err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3629         if (err < 0)
3630                 return err;
3631         err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3632         if (err < 0)
3633                 return err;
3634         return 0;
3635 }
3636
3637 /* create bound capture volume and switch controls */
3638 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3639                                    unsigned int vol_ctl, unsigned int sw_ctl)
3640 {
3641         struct hda_gen_spec *spec = codec->spec;
3642         struct snd_kcontrol_new *knew;
3643
3644         if (vol_ctl) {
3645                 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3646                 if (!knew)
3647                         return -ENOMEM;
3648                 knew->index = idx;
3649                 knew->private_value = vol_ctl;
3650                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3651         }
3652         if (sw_ctl) {
3653                 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3654                 if (!knew)
3655                         return -ENOMEM;
3656                 knew->index = idx;
3657                 knew->private_value = sw_ctl;
3658                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3659         }
3660         return 0;
3661 }
3662
3663 /* return the vol ctl when used first in the imux list */
3664 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3665 {
3666         struct nid_path *path;
3667         unsigned int ctl;
3668         int i;
3669
3670         path = get_input_path(codec, 0, idx);
3671         if (!path)
3672                 return 0;
3673         ctl = path->ctls[type];
3674         if (!ctl)
3675                 return 0;
3676         for (i = 0; i < idx - 1; i++) {
3677                 path = get_input_path(codec, 0, i);
3678                 if (path && path->ctls[type] == ctl)
3679                         return 0;
3680         }
3681         return ctl;
3682 }
3683
3684 /* create individual capture volume and switch controls per input */
3685 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3686 {
3687         struct hda_gen_spec *spec = codec->spec;
3688         struct hda_input_mux *imux = &spec->input_mux;
3689         int i, err, type;
3690
3691         for (i = 0; i < imux->num_items; i++) {
3692                 bool inv_dmic;
3693                 int idx;
3694
3695                 idx = imux->items[i].index;
3696                 if (idx >= spec->autocfg.num_inputs)
3697                         continue;
3698                 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3699
3700                 for (type = 0; type < 2; type++) {
3701                         err = add_single_cap_ctl(codec,
3702                                                  spec->input_labels[idx],
3703                                                  spec->input_label_idxs[idx],
3704                                                  type,
3705                                                  get_first_cap_ctl(codec, i, type),
3706                                                  inv_dmic);
3707                         if (err < 0)
3708                                 return err;
3709                 }
3710         }
3711         return 0;
3712 }
3713
3714 static int create_capture_mixers(struct hda_codec *codec)
3715 {
3716         struct hda_gen_spec *spec = codec->spec;
3717         struct hda_input_mux *imux = &spec->input_mux;
3718         int i, n, nums, err;
3719
3720         if (spec->dyn_adc_switch)
3721                 nums = 1;
3722         else
3723                 nums = spec->num_adc_nids;
3724
3725         if (!spec->auto_mic && imux->num_items > 1) {
3726                 struct snd_kcontrol_new *knew;
3727                 const char *name;
3728                 name = nums > 1 ? "Input Source" : "Capture Source";
3729                 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3730                 if (!knew)
3731                         return -ENOMEM;
3732                 knew->count = nums;
3733         }
3734
3735         for (n = 0; n < nums; n++) {
3736                 bool multi = false;
3737                 bool multi_cap_vol = spec->multi_cap_vol;
3738                 bool inv_dmic = false;
3739                 int vol, sw;
3740
3741                 vol = sw = 0;
3742                 for (i = 0; i < imux->num_items; i++) {
3743                         struct nid_path *path;
3744                         path = get_input_path(codec, n, i);
3745                         if (!path)
3746                                 continue;
3747                         parse_capvol_in_path(codec, path);
3748                         if (!vol)
3749                                 vol = path->ctls[NID_PATH_VOL_CTL];
3750                         else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3751                                 multi = true;
3752                                 if (!same_amp_caps(codec, vol,
3753                                     path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3754                                         multi_cap_vol = true;
3755                         }
3756                         if (!sw)
3757                                 sw = path->ctls[NID_PATH_MUTE_CTL];
3758                         else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3759                                 multi = true;
3760                                 if (!same_amp_caps(codec, sw,
3761                                     path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3762                                         multi_cap_vol = true;
3763                         }
3764                         if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3765                                 inv_dmic = true;
3766                 }
3767
3768                 if (!multi)
3769                         err = create_single_cap_vol_ctl(codec, n, vol, sw,
3770                                                         inv_dmic);
3771                 else if (!multi_cap_vol && !inv_dmic)
3772                         err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3773                 else
3774                         err = create_multi_cap_vol_ctl(codec);
3775                 if (err < 0)
3776                         return err;
3777         }
3778
3779         return 0;
3780 }
3781
3782 /*
3783  * add mic boosts if needed
3784  */
3785
3786 /* check whether the given amp is feasible as a boost volume */
3787 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3788                             int dir, int idx)
3789 {
3790         unsigned int step;
3791
3792         if (!nid_has_volume(codec, nid, dir) ||
3793             is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3794             is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3795                 return false;
3796
3797         step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3798                 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3799         if (step < 0x20)
3800                 return false;
3801         return true;
3802 }
3803
3804 /* look for a boost amp in a widget close to the pin */
3805 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3806                                        struct nid_path *path)
3807 {
3808         unsigned int val = 0;
3809         hda_nid_t nid;
3810         int depth;
3811
3812         for (depth = 0; depth < 3; depth++) {
3813                 if (depth >= path->depth - 1)
3814                         break;
3815                 nid = path->path[depth];
3816                 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3817                         val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3818                         break;
3819                 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3820                                            path->idx[depth])) {
3821                         val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3822                                                   HDA_INPUT);
3823                         break;
3824                 }
3825         }
3826
3827         return val;
3828 }
3829
3830 static int parse_mic_boost(struct hda_codec *codec)
3831 {
3832         struct hda_gen_spec *spec = codec->spec;
3833         struct auto_pin_cfg *cfg = &spec->autocfg;
3834         struct hda_input_mux *imux = &spec->input_mux;
3835         int i;
3836
3837         if (!spec->num_adc_nids)
3838                 return 0;
3839
3840         for (i = 0; i < imux->num_items; i++) {
3841                 struct nid_path *path;
3842                 unsigned int val;
3843                 int idx;
3844                 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3845
3846                 idx = imux->items[i].index;
3847                 if (idx >= imux->num_items)
3848                         continue;
3849
3850                 /* check only line-in and mic pins */
3851                 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3852                         continue;
3853
3854                 path = get_input_path(codec, 0, i);
3855                 if (!path)
3856                         continue;
3857
3858                 val = look_for_boost_amp(codec, path);
3859                 if (!val)
3860                         continue;
3861
3862                 /* create a boost control */
3863                 snprintf(boost_label, sizeof(boost_label),
3864                          "%s Boost Volume", spec->input_labels[idx]);
3865                 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3866                                  spec->input_label_idxs[idx], val))
3867                         return -ENOMEM;
3868
3869                 path->ctls[NID_PATH_BOOST_CTL] = val;
3870         }
3871         return 0;
3872 }
3873
3874 /*
3875  * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3876  */
3877 static void parse_digital(struct hda_codec *codec)
3878 {
3879         struct hda_gen_spec *spec = codec->spec;
3880         struct nid_path *path;
3881         int i, nums;
3882         hda_nid_t dig_nid, pin;
3883
3884         /* support multiple SPDIFs; the secondary is set up as a slave */
3885         nums = 0;
3886         for (i = 0; i < spec->autocfg.dig_outs; i++) {
3887                 pin = spec->autocfg.dig_out_pins[i];
3888                 dig_nid = look_for_dac(codec, pin, true);
3889                 if (!dig_nid)
3890                         continue;
3891                 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3892                 if (!path)
3893                         continue;
3894                 print_nid_path(codec, "digout", path);
3895                 path->active = true;
3896                 path->pin_fixed = true; /* no jack detection */
3897                 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3898                 set_pin_target(codec, pin, PIN_OUT, false);
3899                 if (!nums) {
3900                         spec->multiout.dig_out_nid = dig_nid;
3901                         spec->dig_out_type = spec->autocfg.dig_out_type[0];
3902                 } else {
3903                         spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3904                         if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3905                                 break;
3906                         spec->slave_dig_outs[nums - 1] = dig_nid;
3907                 }
3908                 nums++;
3909         }
3910
3911         if (spec->autocfg.dig_in_pin) {
3912                 pin = spec->autocfg.dig_in_pin;
3913                 for_each_hda_codec_node(dig_nid, codec) {
3914                         unsigned int wcaps = get_wcaps(codec, dig_nid);
3915                         if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3916                                 continue;
3917                         if (!(wcaps & AC_WCAP_DIGITAL))
3918                                 continue;
3919                         path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3920                         if (path) {
3921                                 print_nid_path(codec, "digin", path);
3922                                 path->active = true;
3923                                 path->pin_fixed = true; /* no jack */
3924                                 spec->dig_in_nid = dig_nid;
3925                                 spec->digin_path = snd_hda_get_path_idx(codec, path);
3926                                 set_pin_target(codec, pin, PIN_IN, false);
3927                                 break;
3928                         }
3929                 }
3930         }
3931 }
3932
3933
3934 /*
3935  * input MUX handling
3936  */
3937
3938 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3939
3940 /* select the given imux item; either unmute exclusively or select the route */
3941 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3942                       unsigned int idx)
3943 {
3944         struct hda_gen_spec *spec = codec->spec;
3945         const struct hda_input_mux *imux;
3946         struct nid_path *old_path, *path;
3947
3948         imux = &spec->input_mux;
3949         if (!imux->num_items)
3950                 return 0;
3951
3952         if (idx >= imux->num_items)
3953                 idx = imux->num_items - 1;
3954         if (spec->cur_mux[adc_idx] == idx)
3955                 return 0;
3956
3957         old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3958         if (!old_path)
3959                 return 0;
3960         if (old_path->active)
3961                 snd_hda_activate_path(codec, old_path, false, false);
3962
3963         spec->cur_mux[adc_idx] = idx;
3964
3965         if (spec->hp_mic)
3966                 update_hp_mic(codec, adc_idx, false);
3967
3968         if (spec->dyn_adc_switch)
3969                 dyn_adc_pcm_resetup(codec, idx);
3970
3971         path = get_input_path(codec, adc_idx, idx);
3972         if (!path)
3973                 return 0;
3974         if (path->active)
3975                 return 0;
3976         snd_hda_activate_path(codec, path, true, false);
3977         if (spec->cap_sync_hook)
3978                 spec->cap_sync_hook(codec, NULL, NULL);
3979         path_power_down_sync(codec, old_path);
3980         return 1;
3981 }
3982
3983 /* power up/down widgets in the all paths that match with the given NID
3984  * as terminals (either start- or endpoint)
3985  *
3986  * returns the last changed NID, or zero if unchanged.
3987  */
3988 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3989                                 int pin_state, int stream_state)
3990 {
3991         struct hda_gen_spec *spec = codec->spec;
3992         hda_nid_t last, changed = 0;
3993         struct nid_path *path;
3994         int n;
3995
3996         for (n = 0; n < spec->paths.used; n++) {
3997                 path = snd_array_elem(&spec->paths, n);
3998                 if (!path->depth)
3999                         continue;
4000                 if (path->path[0] == nid ||
4001                     path->path[path->depth - 1] == nid) {
4002                         bool pin_old = path->pin_enabled;
4003                         bool stream_old = path->stream_enabled;
4004
4005                         if (pin_state >= 0)
4006                                 path->pin_enabled = pin_state;
4007                         if (stream_state >= 0)
4008                                 path->stream_enabled = stream_state;
4009                         if ((!path->pin_fixed && path->pin_enabled != pin_old)
4010                             || path->stream_enabled != stream_old) {
4011                                 last = path_power_update(codec, path, true);
4012                                 if (last)
4013                                         changed = last;
4014                         }
4015                 }
4016         }
4017         return changed;
4018 }
4019
4020 /* check the jack status for power control */
4021 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4022 {
4023         if (!is_jack_detectable(codec, pin))
4024                 return true;
4025         return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4026 }
4027
4028 /* power up/down the paths of the given pin according to the jack state;
4029  * power = 0/1 : only power up/down if it matches with the jack state,
4030  *       < 0   : force power up/down to follow the jack sate
4031  *
4032  * returns the last changed NID, or zero if unchanged.
4033  */
4034 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4035                                     int power)
4036 {
4037         bool on;
4038
4039         if (!codec->power_save_node)
4040                 return 0;
4041
4042         on = detect_pin_state(codec, pin);
4043
4044         if (power >= 0 && on != power)
4045                 return 0;
4046         return set_path_power(codec, pin, on, -1);
4047 }
4048
4049 static void pin_power_callback(struct hda_codec *codec,
4050                                struct hda_jack_callback *jack,
4051                                bool on)
4052 {
4053         if (jack && jack->nid)
4054                 sync_power_state_change(codec,
4055                                         set_pin_power_jack(codec, jack->nid, on));
4056 }
4057
4058 /* callback only doing power up -- called at first */
4059 static void pin_power_up_callback(struct hda_codec *codec,
4060                                   struct hda_jack_callback *jack)
4061 {
4062         pin_power_callback(codec, jack, true);
4063 }
4064
4065 /* callback only doing power down -- called at last */
4066 static void pin_power_down_callback(struct hda_codec *codec,
4067                                     struct hda_jack_callback *jack)
4068 {
4069         pin_power_callback(codec, jack, false);
4070 }
4071
4072 /* set up the power up/down callbacks */
4073 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4074                                const hda_nid_t *pins, bool on)
4075 {
4076         int i;
4077         hda_jack_callback_fn cb =
4078                 on ? pin_power_up_callback : pin_power_down_callback;
4079
4080         for (i = 0; i < num_pins && pins[i]; i++) {
4081                 if (is_jack_detectable(codec, pins[i]))
4082                         snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4083                 else
4084                         set_path_power(codec, pins[i], true, -1);
4085         }
4086 }
4087
4088 /* enabled power callback to each available I/O pin with jack detections;
4089  * the digital I/O pins are excluded because of the unreliable detectsion
4090  */
4091 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4092 {
4093         struct hda_gen_spec *spec = codec->spec;
4094         struct auto_pin_cfg *cfg = &spec->autocfg;
4095         int i;
4096
4097         if (!codec->power_save_node)
4098                 return;
4099         add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4100         if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4101                 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4102         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4103                 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4104         for (i = 0; i < cfg->num_inputs; i++)
4105                 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4106 }
4107
4108 /* sync path power up/down with the jack states of given pins */
4109 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4110                                 const hda_nid_t *pins)
4111 {
4112         int i;
4113
4114         for (i = 0; i < num_pins && pins[i]; i++)
4115                 if (is_jack_detectable(codec, pins[i]))
4116                         set_pin_power_jack(codec, pins[i], -1);
4117 }
4118
4119 /* sync path power up/down with pins; called at init and resume */
4120 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4121 {
4122         struct hda_gen_spec *spec = codec->spec;
4123         struct auto_pin_cfg *cfg = &spec->autocfg;
4124         int i;
4125
4126         if (!codec->power_save_node)
4127                 return;
4128         sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4129         if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4130                 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4131         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4132                 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4133         for (i = 0; i < cfg->num_inputs; i++)
4134                 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4135 }
4136
4137 /* add fake paths if not present yet */
4138 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4139                            int num_pins, const hda_nid_t *pins)
4140 {
4141         struct hda_gen_spec *spec = codec->spec;
4142         struct nid_path *path;
4143         int i;
4144
4145         for (i = 0; i < num_pins; i++) {
4146                 if (!pins[i])
4147                         break;
4148                 if (get_nid_path(codec, nid, pins[i], 0))
4149                         continue;
4150                 path = snd_array_new(&spec->paths);
4151                 if (!path)
4152                         return -ENOMEM;
4153                 memset(path, 0, sizeof(*path));
4154                 path->depth = 2;
4155                 path->path[0] = nid;
4156                 path->path[1] = pins[i];
4157                 path->active = true;
4158         }
4159         return 0;
4160 }
4161
4162 /* create fake paths to all outputs from beep */
4163 static int add_fake_beep_paths(struct hda_codec *codec)
4164 {
4165         struct hda_gen_spec *spec = codec->spec;
4166         struct auto_pin_cfg *cfg = &spec->autocfg;
4167         hda_nid_t nid = spec->beep_nid;
4168         int err;
4169
4170         if (!codec->power_save_node || !nid)
4171                 return 0;
4172         err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4173         if (err < 0)
4174                 return err;
4175         if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4176                 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4177                 if (err < 0)
4178                         return err;
4179         }
4180         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4181                 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4182                                      cfg->speaker_pins);
4183                 if (err < 0)
4184                         return err;
4185         }
4186         return 0;
4187 }
4188
4189 /* power up/down beep widget and its output paths */
4190 static void beep_power_hook(struct hda_beep *beep, bool on)
4191 {
4192         set_path_power(beep->codec, beep->nid, -1, on);
4193 }
4194
4195 /**
4196  * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4197  * @codec: the HDA codec
4198  * @pin: NID of pin to fix
4199  */
4200 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4201 {
4202         struct hda_gen_spec *spec = codec->spec;
4203         struct nid_path *path;
4204
4205         path = snd_array_new(&spec->paths);
4206         if (!path)
4207                 return -ENOMEM;
4208         memset(path, 0, sizeof(*path));
4209         path->depth = 1;
4210         path->path[0] = pin;
4211         path->active = true;
4212         path->pin_fixed = true;
4213         path->stream_enabled = true;
4214         return 0;
4215 }
4216 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4217
4218 /*
4219  * Jack detections for HP auto-mute and mic-switch
4220  */
4221
4222 /* check each pin in the given array; returns true if any of them is plugged */
4223 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4224 {
4225         int i;
4226         bool present = false;
4227
4228         for (i = 0; i < num_pins; i++) {
4229                 hda_nid_t nid = pins[i];
4230                 if (!nid)
4231                         break;
4232                 /* don't detect pins retasked as inputs */
4233                 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4234                         continue;
4235                 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4236                         present = true;
4237         }
4238         return present;
4239 }
4240
4241 /* standard HP/line-out auto-mute helper */
4242 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
4243                         int *paths, bool mute)
4244 {
4245         struct hda_gen_spec *spec = codec->spec;
4246         int i;
4247
4248         for (i = 0; i < num_pins; i++) {
4249                 hda_nid_t nid = pins[i];
4250                 unsigned int val, oldval;
4251                 if (!nid)
4252                         break;
4253
4254                 oldval = snd_hda_codec_get_pin_target(codec, nid);
4255                 if (oldval & PIN_IN)
4256                         continue; /* no mute for inputs */
4257
4258                 if (spec->auto_mute_via_amp) {
4259                         struct nid_path *path;
4260                         hda_nid_t mute_nid;
4261
4262                         path = snd_hda_get_path_from_idx(codec, paths[i]);
4263                         if (!path)
4264                                 continue;
4265                         mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4266                         if (!mute_nid)
4267                                 continue;
4268                         if (mute)
4269                                 spec->mute_bits |= (1ULL << mute_nid);
4270                         else
4271                                 spec->mute_bits &= ~(1ULL << mute_nid);
4272                         continue;
4273                 } else {
4274                         /* don't reset VREF value in case it's controlling
4275                          * the amp (see alc861_fixup_asus_amp_vref_0f())
4276                          */
4277                         if (spec->keep_vref_in_automute)
4278                                 val = oldval & ~PIN_HP;
4279                         else
4280                                 val = 0;
4281                         if (!mute)
4282                                 val |= oldval;
4283                         /* here we call update_pin_ctl() so that the pinctl is
4284                          * changed without changing the pinctl target value;
4285                          * the original target value will be still referred at
4286                          * the init / resume again
4287                          */
4288                         update_pin_ctl(codec, nid, val);
4289                 }
4290
4291                 set_pin_eapd(codec, nid, !mute);
4292                 if (codec->power_save_node) {
4293                         bool on = !mute;
4294                         if (on)
4295                                 on = detect_pin_state(codec, nid);
4296                         set_path_power(codec, nid, on, -1);
4297                 }
4298         }
4299 }
4300
4301 /**
4302  * snd_hda_gen_update_outputs - Toggle outputs muting
4303  * @codec: the HDA codec
4304  *
4305  * Update the mute status of all outputs based on the current jack states.
4306  */
4307 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4308 {
4309         struct hda_gen_spec *spec = codec->spec;
4310         int *paths;
4311         int on;
4312
4313         /* Control HP pins/amps depending on master_mute state;
4314          * in general, HP pins/amps control should be enabled in all cases,
4315          * but currently set only for master_mute, just to be safe
4316          */
4317         if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4318                 paths = spec->out_paths;
4319         else
4320                 paths = spec->hp_paths;
4321         do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4322                     spec->autocfg.hp_pins, paths, spec->master_mute);
4323
4324         if (!spec->automute_speaker)
4325                 on = 0;
4326         else
4327                 on = spec->hp_jack_present | spec->line_jack_present;
4328         on |= spec->master_mute;
4329         spec->speaker_muted = on;
4330         if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4331                 paths = spec->out_paths;
4332         else
4333                 paths = spec->speaker_paths;
4334         do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4335                     spec->autocfg.speaker_pins, paths, on);
4336
4337         /* toggle line-out mutes if needed, too */
4338         /* if LO is a copy of either HP or Speaker, don't need to handle it */
4339         if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4340             spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4341                 return;
4342         if (!spec->automute_lo)
4343                 on = 0;
4344         else
4345                 on = spec->hp_jack_present;
4346         on |= spec->master_mute;
4347         spec->line_out_muted = on;
4348         paths = spec->out_paths;
4349         do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4350                     spec->autocfg.line_out_pins, paths, on);
4351 }
4352 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4353
4354 static void call_update_outputs(struct hda_codec *codec)
4355 {
4356         struct hda_gen_spec *spec = codec->spec;
4357         if (spec->automute_hook)
4358                 spec->automute_hook(codec);
4359         else
4360                 snd_hda_gen_update_outputs(codec);
4361
4362         /* sync the whole vmaster slaves to reflect the new auto-mute status */
4363         if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4364                 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4365 }
4366
4367 /**
4368  * snd_hda_gen_hp_automute - standard HP-automute helper
4369  * @codec: the HDA codec
4370  * @jack: jack object, NULL for the whole
4371  */
4372 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4373                              struct hda_jack_callback *jack)
4374 {
4375         struct hda_gen_spec *spec = codec->spec;
4376         hda_nid_t *pins = spec->autocfg.hp_pins;
4377         int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4378
4379         /* No detection for the first HP jack during indep-HP mode */
4380         if (spec->indep_hp_enabled) {
4381                 pins++;
4382                 num_pins--;
4383         }
4384
4385         spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4386         if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4387                 return;
4388         call_update_outputs(codec);
4389 }
4390 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4391
4392 /**
4393  * snd_hda_gen_line_automute - standard line-out-automute helper
4394  * @codec: the HDA codec
4395  * @jack: jack object, NULL for the whole
4396  */
4397 void snd_hda_gen_line_automute(struct hda_codec *codec,
4398                                struct hda_jack_callback *jack)
4399 {
4400         struct hda_gen_spec *spec = codec->spec;
4401
4402         if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4403                 return;
4404         /* check LO jack only when it's different from HP */
4405         if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4406                 return;
4407
4408         spec->line_jack_present =
4409                 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4410                              spec->autocfg.line_out_pins);
4411         if (!spec->automute_speaker || !spec->detect_lo)
4412                 return;
4413         call_update_outputs(codec);
4414 }
4415 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4416
4417 /**
4418  * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4419  * @codec: the HDA codec
4420  * @jack: jack object, NULL for the whole
4421  */
4422 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4423                                 struct hda_jack_callback *jack)
4424 {
4425         struct hda_gen_spec *spec = codec->spec;
4426         int i;
4427
4428         if (!spec->auto_mic)
4429                 return;
4430
4431         for (i = spec->am_num_entries - 1; i > 0; i--) {
4432                 hda_nid_t pin = spec->am_entry[i].pin;
4433                 /* don't detect pins retasked as outputs */
4434                 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4435                         continue;
4436                 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4437                         mux_select(codec, 0, spec->am_entry[i].idx);
4438                         return;
4439                 }
4440         }
4441         mux_select(codec, 0, spec->am_entry[0].idx);
4442 }
4443 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4444
4445 /* call appropriate hooks */
4446 static void call_hp_automute(struct hda_codec *codec,
4447                              struct hda_jack_callback *jack)
4448 {
4449         struct hda_gen_spec *spec = codec->spec;
4450         if (spec->hp_automute_hook)
4451                 spec->hp_automute_hook(codec, jack);
4452         else
4453                 snd_hda_gen_hp_automute(codec, jack);
4454 }
4455
4456 static void call_line_automute(struct hda_codec *codec,
4457                                struct hda_jack_callback *jack)
4458 {
4459         struct hda_gen_spec *spec = codec->spec;
4460         if (spec->line_automute_hook)
4461                 spec->line_automute_hook(codec, jack);
4462         else
4463                 snd_hda_gen_line_automute(codec, jack);
4464 }
4465
4466 static void call_mic_autoswitch(struct hda_codec *codec,
4467                                 struct hda_jack_callback *jack)
4468 {
4469         struct hda_gen_spec *spec = codec->spec;
4470         if (spec->mic_autoswitch_hook)
4471                 spec->mic_autoswitch_hook(codec, jack);
4472         else
4473                 snd_hda_gen_mic_autoswitch(codec, jack);
4474 }
4475
4476 /* update jack retasking */
4477 static void update_automute_all(struct hda_codec *codec)
4478 {
4479         call_hp_automute(codec, NULL);
4480         call_line_automute(codec, NULL);
4481         call_mic_autoswitch(codec, NULL);
4482 }
4483
4484 /*
4485  * Auto-Mute mode mixer enum support
4486  */
4487 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4488                               struct snd_ctl_elem_info *uinfo)
4489 {
4490         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4491         struct hda_gen_spec *spec = codec->spec;
4492         static const char * const texts3[] = {
4493                 "Disabled", "Speaker Only", "Line Out+Speaker"
4494         };
4495
4496         if (spec->automute_speaker_possible && spec->automute_lo_possible)
4497                 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4498         return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4499 }
4500
4501 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4502                              struct snd_ctl_elem_value *ucontrol)
4503 {
4504         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4505         struct hda_gen_spec *spec = codec->spec;
4506         unsigned int val = 0;
4507         if (spec->automute_speaker)
4508                 val++;
4509         if (spec->automute_lo)
4510                 val++;
4511
4512         ucontrol->value.enumerated.item[0] = val;
4513         return 0;
4514 }
4515
4516 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4517                              struct snd_ctl_elem_value *ucontrol)
4518 {
4519         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4520         struct hda_gen_spec *spec = codec->spec;
4521
4522         switch (ucontrol->value.enumerated.item[0]) {
4523         case 0:
4524                 if (!spec->automute_speaker && !spec->automute_lo)
4525                         return 0;
4526                 spec->automute_speaker = 0;
4527                 spec->automute_lo = 0;
4528                 break;
4529         case 1:
4530                 if (spec->automute_speaker_possible) {
4531                         if (!spec->automute_lo && spec->automute_speaker)
4532                                 return 0;
4533                         spec->automute_speaker = 1;
4534                         spec->automute_lo = 0;
4535                 } else if (spec->automute_lo_possible) {
4536                         if (spec->automute_lo)
4537                                 return 0;
4538                         spec->automute_lo = 1;
4539                 } else
4540                         return -EINVAL;
4541                 break;
4542         case 2:
4543                 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4544                         return -EINVAL;
4545                 if (spec->automute_speaker && spec->automute_lo)
4546                         return 0;
4547                 spec->automute_speaker = 1;
4548                 spec->automute_lo = 1;
4549                 break;
4550         default:
4551                 return -EINVAL;
4552         }
4553         call_update_outputs(codec);
4554         return 1;
4555 }
4556
4557 static const struct snd_kcontrol_new automute_mode_enum = {
4558         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4559         .name = "Auto-Mute Mode",
4560         .info = automute_mode_info,
4561         .get = automute_mode_get,
4562         .put = automute_mode_put,
4563 };
4564
4565 static int add_automute_mode_enum(struct hda_codec *codec)
4566 {
4567         struct hda_gen_spec *spec = codec->spec;
4568
4569         if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4570                 return -ENOMEM;
4571         return 0;
4572 }
4573
4574 /*
4575  * Check the availability of HP/line-out auto-mute;
4576  * Set up appropriately if really supported
4577  */
4578 static int check_auto_mute_availability(struct hda_codec *codec)
4579 {
4580         struct hda_gen_spec *spec = codec->spec;
4581         struct auto_pin_cfg *cfg = &spec->autocfg;
4582         int present = 0;
4583         int i, err;
4584
4585         if (spec->suppress_auto_mute)
4586                 return 0;
4587
4588         if (cfg->hp_pins[0])
4589                 present++;
4590         if (cfg->line_out_pins[0])
4591                 present++;
4592         if (cfg->speaker_pins[0])
4593                 present++;
4594         if (present < 2) /* need two different output types */
4595                 return 0;
4596
4597         if (!cfg->speaker_pins[0] &&
4598             cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4599                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4600                        sizeof(cfg->speaker_pins));
4601                 cfg->speaker_outs = cfg->line_outs;
4602         }
4603
4604         if (!cfg->hp_pins[0] &&
4605             cfg->line_out_type == AUTO_PIN_HP_OUT) {
4606                 memcpy(cfg->hp_pins, cfg->line_out_pins,
4607                        sizeof(cfg->hp_pins));
4608                 cfg->hp_outs = cfg->line_outs;
4609         }
4610
4611         for (i = 0; i < cfg->hp_outs; i++) {
4612                 hda_nid_t nid = cfg->hp_pins[i];
4613                 if (!is_jack_detectable(codec, nid))
4614                         continue;
4615                 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4616                 snd_hda_jack_detect_enable_callback(codec, nid,
4617                                                     call_hp_automute);
4618                 spec->detect_hp = 1;
4619         }
4620
4621         if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4622                 if (cfg->speaker_outs)
4623                         for (i = 0; i < cfg->line_outs; i++) {
4624                                 hda_nid_t nid = cfg->line_out_pins[i];
4625                                 if (!is_jack_detectable(codec, nid))
4626                                         continue;
4627                                 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4628                                 snd_hda_jack_detect_enable_callback(codec, nid,
4629                                                                     call_line_automute);
4630                                 spec->detect_lo = 1;
4631                         }
4632                 spec->automute_lo_possible = spec->detect_hp;
4633         }
4634
4635         spec->automute_speaker_possible = cfg->speaker_outs &&
4636                 (spec->detect_hp || spec->detect_lo);
4637
4638         spec->automute_lo = spec->automute_lo_possible;
4639         spec->automute_speaker = spec->automute_speaker_possible;
4640
4641         if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4642                 /* create a control for automute mode */
4643                 err = add_automute_mode_enum(codec);
4644                 if (err < 0)
4645                         return err;
4646         }
4647         return 0;
4648 }
4649
4650 /* check whether all auto-mic pins are valid; setup indices if OK */
4651 static bool auto_mic_check_imux(struct hda_codec *codec)
4652 {
4653         struct hda_gen_spec *spec = codec->spec;
4654         const struct hda_input_mux *imux;
4655         int i;
4656
4657         imux = &spec->input_mux;
4658         for (i = 0; i < spec->am_num_entries; i++) {
4659                 spec->am_entry[i].idx =
4660                         find_idx_in_nid_list(spec->am_entry[i].pin,
4661                                              spec->imux_pins, imux->num_items);
4662                 if (spec->am_entry[i].idx < 0)
4663                         return false; /* no corresponding imux */
4664         }
4665
4666         /* we don't need the jack detection for the first pin */
4667         for (i = 1; i < spec->am_num_entries; i++)
4668                 snd_hda_jack_detect_enable_callback(codec,
4669                                                     spec->am_entry[i].pin,
4670                                                     call_mic_autoswitch);
4671         return true;
4672 }
4673
4674 static int compare_attr(const void *ap, const void *bp)
4675 {
4676         const struct automic_entry *a = ap;
4677         const struct automic_entry *b = bp;
4678         return (int)(a->attr - b->attr);
4679 }
4680
4681 /*
4682  * Check the availability of auto-mic switch;
4683  * Set up if really supported
4684  */
4685 static int check_auto_mic_availability(struct hda_codec *codec)
4686 {
4687         struct hda_gen_spec *spec = codec->spec;
4688         struct auto_pin_cfg *cfg = &spec->autocfg;
4689         unsigned int types;
4690         int i, num_pins;
4691
4692         if (spec->suppress_auto_mic)
4693                 return 0;
4694
4695         types = 0;
4696         num_pins = 0;
4697         for (i = 0; i < cfg->num_inputs; i++) {
4698                 hda_nid_t nid = cfg->inputs[i].pin;
4699                 unsigned int attr;
4700                 attr = snd_hda_codec_get_pincfg(codec, nid);
4701                 attr = snd_hda_get_input_pin_attr(attr);
4702                 if (types & (1 << attr))
4703                         return 0; /* already occupied */
4704                 switch (attr) {
4705                 case INPUT_PIN_ATTR_INT:
4706                         if (cfg->inputs[i].type != AUTO_PIN_MIC)
4707                                 return 0; /* invalid type */
4708                         break;
4709                 case INPUT_PIN_ATTR_UNUSED:
4710                         return 0; /* invalid entry */
4711                 default:
4712                         if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4713                                 return 0; /* invalid type */
4714                         if (!spec->line_in_auto_switch &&
4715                             cfg->inputs[i].type != AUTO_PIN_MIC)
4716                                 return 0; /* only mic is allowed */
4717                         if (!is_jack_detectable(codec, nid))
4718                                 return 0; /* no unsol support */
4719                         break;
4720                 }
4721                 if (num_pins >= MAX_AUTO_MIC_PINS)
4722                         return 0;
4723                 types |= (1 << attr);
4724                 spec->am_entry[num_pins].pin = nid;
4725                 spec->am_entry[num_pins].attr = attr;
4726                 num_pins++;
4727         }
4728
4729         if (num_pins < 2)
4730                 return 0;
4731
4732         spec->am_num_entries = num_pins;
4733         /* sort the am_entry in the order of attr so that the pin with a
4734          * higher attr will be selected when the jack is plugged.
4735          */
4736         sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4737              compare_attr, NULL);
4738
4739         if (!auto_mic_check_imux(codec))
4740                 return 0;
4741
4742         spec->auto_mic = 1;
4743         spec->num_adc_nids = 1;
4744         spec->cur_mux[0] = spec->am_entry[0].idx;
4745         codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4746                     spec->am_entry[0].pin,
4747                     spec->am_entry[1].pin,
4748                     spec->am_entry[2].pin);
4749
4750         return 0;
4751 }
4752
4753 /**
4754  * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4755  * into power down
4756  * @codec: the HDA codec
4757  * @nid: NID to evalute
4758  * @power_state: target power state
4759  */
4760 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4761                                                   hda_nid_t nid,
4762                                                   unsigned int power_state)
4763 {
4764         struct hda_gen_spec *spec = codec->spec;
4765
4766         if (!spec->power_down_unused && !codec->power_save_node)
4767                 return power_state;
4768         if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4769                 return power_state;
4770         if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4771                 return power_state;
4772         if (is_active_nid_for_any(codec, nid))
4773                 return power_state;
4774         return AC_PWRST_D3;
4775 }
4776 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4777
4778 /* mute all aamix inputs initially; parse up to the first leaves */
4779 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4780 {
4781         int i, nums;
4782         const hda_nid_t *conn;
4783         bool has_amp;
4784
4785         nums = snd_hda_get_conn_list(codec, mix, &conn);
4786         has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4787         for (i = 0; i < nums; i++) {
4788                 if (has_amp)
4789                         update_amp(codec, mix, HDA_INPUT, i,
4790                                    0xff, HDA_AMP_MUTE);
4791                 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4792                         update_amp(codec, conn[i], HDA_OUTPUT, 0,
4793                                    0xff, HDA_AMP_MUTE);
4794         }
4795 }
4796
4797 /**
4798  * snd_hda_gen_stream_pm - Stream power management callback
4799  * @codec: the HDA codec
4800  * @nid: audio widget
4801  * @on: power on/off flag
4802  *
4803  * Set this in patch_ops.stream_pm.  Only valid with power_save_node flag.
4804  */
4805 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4806 {
4807         if (codec->power_save_node)
4808                 set_path_power(codec, nid, -1, on);
4809 }
4810 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4811
4812 /**
4813  * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4814  * set up the hda_gen_spec
4815  * @codec: the HDA codec
4816  * @cfg: Parsed pin configuration
4817  *
4818  * return 1 if successful, 0 if the proper config is not found,
4819  * or a negative error code
4820  */
4821 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4822                                   struct auto_pin_cfg *cfg)
4823 {
4824         struct hda_gen_spec *spec = codec->spec;
4825         int err;
4826
4827         parse_user_hints(codec);
4828
4829         if (spec->mixer_nid && !spec->mixer_merge_nid)
4830                 spec->mixer_merge_nid = spec->mixer_nid;
4831
4832         if (cfg != &spec->autocfg) {
4833                 spec->autocfg = *cfg;
4834                 cfg = &spec->autocfg;
4835         }
4836
4837         if (!spec->main_out_badness)
4838                 spec->main_out_badness = &hda_main_out_badness;
4839         if (!spec->extra_out_badness)
4840                 spec->extra_out_badness = &hda_extra_out_badness;
4841
4842         fill_all_dac_nids(codec);
4843
4844         if (!cfg->line_outs) {
4845                 if (cfg->dig_outs || cfg->dig_in_pin) {
4846                         spec->multiout.max_channels = 2;
4847                         spec->no_analog = 1;
4848                         goto dig_only;
4849                 }
4850                 if (!cfg->num_inputs && !cfg->dig_in_pin)
4851                         return 0; /* can't find valid BIOS pin config */
4852         }
4853
4854         if (!spec->no_primary_hp &&
4855             cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4856             cfg->line_outs <= cfg->hp_outs) {
4857                 /* use HP as primary out */
4858                 cfg->speaker_outs = cfg->line_outs;
4859                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4860                        sizeof(cfg->speaker_pins));
4861                 cfg->line_outs = cfg->hp_outs;
4862                 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4863                 cfg->hp_outs = 0;
4864                 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4865                 cfg->line_out_type = AUTO_PIN_HP_OUT;
4866         }
4867
4868         err = parse_output_paths(codec);
4869         if (err < 0)
4870                 return err;
4871         err = create_multi_channel_mode(codec);
4872         if (err < 0)
4873                 return err;
4874         err = create_multi_out_ctls(codec, cfg);
4875         if (err < 0)
4876                 return err;
4877         err = create_hp_out_ctls(codec);
4878         if (err < 0)
4879                 return err;
4880         err = create_speaker_out_ctls(codec);
4881         if (err < 0)
4882                 return err;
4883         err = create_indep_hp_ctls(codec);
4884         if (err < 0)
4885                 return err;
4886         err = create_loopback_mixing_ctl(codec);
4887         if (err < 0)
4888                 return err;
4889         err = create_hp_mic(codec);
4890         if (err < 0)
4891                 return err;
4892         err = create_input_ctls(codec);
4893         if (err < 0)
4894                 return err;
4895
4896         /* add power-down pin callbacks at first */
4897         add_all_pin_power_ctls(codec, false);
4898
4899         spec->const_channel_count = spec->ext_channel_count;
4900         /* check the multiple speaker and headphone pins */
4901         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4902                 spec->const_channel_count = max(spec->const_channel_count,
4903                                                 cfg->speaker_outs * 2);
4904         if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4905                 spec->const_channel_count = max(spec->const_channel_count,
4906                                                 cfg->hp_outs * 2);
4907         spec->multiout.max_channels = max(spec->ext_channel_count,
4908                                           spec->const_channel_count);
4909
4910         err = check_auto_mute_availability(codec);
4911         if (err < 0)
4912                 return err;
4913
4914         err = check_dyn_adc_switch(codec);
4915         if (err < 0)
4916                 return err;
4917
4918         err = check_auto_mic_availability(codec);
4919         if (err < 0)
4920                 return err;
4921
4922         /* add stereo mix if available and not enabled yet */
4923         if (!spec->auto_mic && spec->mixer_nid &&
4924             spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4925             spec->input_mux.num_items > 1) {
4926                 err = parse_capture_source(codec, spec->mixer_nid,
4927                                            CFG_IDX_MIX, spec->num_all_adcs,
4928                                            "Stereo Mix", 0);
4929                 if (err < 0)
4930                         return err;
4931         }
4932
4933
4934         err = create_capture_mixers(codec);
4935         if (err < 0)
4936                 return err;
4937
4938         err = parse_mic_boost(codec);
4939         if (err < 0)
4940                 return err;
4941
4942         /* create "Headphone Mic Jack Mode" if no input selection is
4943          * available (or user specifies add_jack_modes hint)
4944          */
4945         if (spec->hp_mic_pin &&
4946             (spec->auto_mic || spec->input_mux.num_items == 1 ||
4947              spec->add_jack_modes)) {
4948                 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4949                 if (err < 0)
4950                         return err;
4951         }
4952
4953         if (spec->add_jack_modes) {
4954                 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4955                         err = create_out_jack_modes(codec, cfg->line_outs,
4956                                                     cfg->line_out_pins);
4957                         if (err < 0)
4958                                 return err;
4959                 }
4960                 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4961                         err = create_out_jack_modes(codec, cfg->hp_outs,
4962                                                     cfg->hp_pins);
4963                         if (err < 0)
4964                                 return err;
4965                 }
4966         }
4967
4968         /* add power-up pin callbacks at last */
4969         add_all_pin_power_ctls(codec, true);
4970
4971         /* mute all aamix input initially */
4972         if (spec->mixer_nid)
4973                 mute_all_mixer_nid(codec, spec->mixer_nid);
4974
4975  dig_only:
4976         parse_digital(codec);
4977
4978         if (spec->power_down_unused || codec->power_save_node) {
4979                 if (!codec->power_filter)
4980                         codec->power_filter = snd_hda_gen_path_power_filter;
4981                 if (!codec->patch_ops.stream_pm)
4982                         codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
4983         }
4984
4985         if (!spec->no_analog && spec->beep_nid) {
4986                 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4987                 if (err < 0)
4988                         return err;
4989                 if (codec->beep && codec->power_save_node) {
4990                         err = add_fake_beep_paths(codec);
4991                         if (err < 0)
4992                                 return err;
4993                         codec->beep->power_hook = beep_power_hook;
4994                 }
4995         }
4996
4997         return 1;
4998 }
4999 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
5000
5001
5002 /*
5003  * Build control elements
5004  */
5005
5006 /* slave controls for virtual master */
5007 static const char * const slave_pfxs[] = {
5008         "Front", "Surround", "Center", "LFE", "Side",
5009         "Headphone", "Speaker", "Mono", "Line Out",
5010         "CLFE", "Bass Speaker", "PCM",
5011         "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5012         "Headphone Front", "Headphone Surround", "Headphone CLFE",
5013         "Headphone Side", "Headphone+LO", "Speaker+LO",
5014         NULL,
5015 };
5016
5017 /**
5018  * snd_hda_gen_build_controls - Build controls from the parsed results
5019  * @codec: the HDA codec
5020  *
5021  * Pass this to build_controls patch_ops.
5022  */
5023 int snd_hda_gen_build_controls(struct hda_codec *codec)
5024 {
5025         struct hda_gen_spec *spec = codec->spec;
5026         int err;
5027
5028         if (spec->kctls.used) {
5029                 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5030                 if (err < 0)
5031                         return err;
5032         }
5033
5034         if (spec->multiout.dig_out_nid) {
5035                 err = snd_hda_create_dig_out_ctls(codec,
5036                                                   spec->multiout.dig_out_nid,
5037                                                   spec->multiout.dig_out_nid,
5038                                                   spec->pcm_rec[1]->pcm_type);
5039                 if (err < 0)
5040                         return err;
5041                 if (!spec->no_analog) {
5042                         err = snd_hda_create_spdif_share_sw(codec,
5043                                                             &spec->multiout);
5044                         if (err < 0)
5045                                 return err;
5046                         spec->multiout.share_spdif = 1;
5047                 }
5048         }
5049         if (spec->dig_in_nid) {
5050                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5051                 if (err < 0)
5052                         return err;
5053         }
5054
5055         /* if we have no master control, let's create it */
5056         if (!spec->no_analog &&
5057             !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5058                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5059                                           spec->vmaster_tlv, slave_pfxs,
5060                                           "Playback Volume");
5061                 if (err < 0)
5062                         return err;
5063         }
5064         if (!spec->no_analog &&
5065             !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5066                 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5067                                             NULL, slave_pfxs,
5068                                             "Playback Switch",
5069                                             true, &spec->vmaster_mute.sw_kctl);
5070                 if (err < 0)
5071                         return err;
5072                 if (spec->vmaster_mute.hook) {
5073                         snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5074                                                  spec->vmaster_mute_enum);
5075                         snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5076                 }
5077         }
5078
5079         free_kctls(spec); /* no longer needed */
5080
5081         err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5082         if (err < 0)
5083                 return err;
5084
5085         return 0;
5086 }
5087 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5088
5089
5090 /*
5091  * PCM definitions
5092  */
5093
5094 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5095                                    struct hda_codec *codec,
5096                                    struct snd_pcm_substream *substream,
5097                                    int action)
5098 {
5099         struct hda_gen_spec *spec = codec->spec;
5100         if (spec->pcm_playback_hook)
5101                 spec->pcm_playback_hook(hinfo, codec, substream, action);
5102 }
5103
5104 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5105                                   struct hda_codec *codec,
5106                                   struct snd_pcm_substream *substream,
5107                                   int action)
5108 {
5109         struct hda_gen_spec *spec = codec->spec;
5110         if (spec->pcm_capture_hook)
5111                 spec->pcm_capture_hook(hinfo, codec, substream, action);
5112 }
5113
5114 /*
5115  * Analog playback callbacks
5116  */
5117 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5118                              struct hda_codec *codec,
5119                              struct snd_pcm_substream *substream)
5120 {
5121         struct hda_gen_spec *spec = codec->spec;
5122         int err;
5123
5124         mutex_lock(&spec->pcm_mutex);
5125         err = snd_hda_multi_out_analog_open(codec,
5126                                             &spec->multiout, substream,
5127                                              hinfo);
5128         if (!err) {
5129                 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5130                 call_pcm_playback_hook(hinfo, codec, substream,
5131                                        HDA_GEN_PCM_ACT_OPEN);
5132         }
5133         mutex_unlock(&spec->pcm_mutex);
5134         return err;
5135 }
5136
5137 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5138                                 struct hda_codec *codec,
5139                                 unsigned int stream_tag,
5140                                 unsigned int format,
5141                                 struct snd_pcm_substream *substream)
5142 {
5143         struct hda_gen_spec *spec = codec->spec;
5144         int err;
5145
5146         err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5147                                                stream_tag, format, substream);
5148         if (!err)
5149                 call_pcm_playback_hook(hinfo, codec, substream,
5150                                        HDA_GEN_PCM_ACT_PREPARE);
5151         return err;
5152 }
5153
5154 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5155                                 struct hda_codec *codec,
5156                                 struct snd_pcm_substream *substream)
5157 {
5158         struct hda_gen_spec *spec = codec->spec;
5159         int err;
5160
5161         err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5162         if (!err)
5163                 call_pcm_playback_hook(hinfo, codec, substream,
5164                                        HDA_GEN_PCM_ACT_CLEANUP);
5165         return err;
5166 }
5167
5168 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5169                               struct hda_codec *codec,
5170                               struct snd_pcm_substream *substream)
5171 {
5172         struct hda_gen_spec *spec = codec->spec;
5173         mutex_lock(&spec->pcm_mutex);
5174         spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5175         call_pcm_playback_hook(hinfo, codec, substream,
5176                                HDA_GEN_PCM_ACT_CLOSE);
5177         mutex_unlock(&spec->pcm_mutex);
5178         return 0;
5179 }
5180
5181 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5182                             struct hda_codec *codec,
5183                             struct snd_pcm_substream *substream)
5184 {
5185         call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5186         return 0;
5187 }
5188
5189 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5190                                struct hda_codec *codec,
5191                                unsigned int stream_tag,
5192                                unsigned int format,
5193                                struct snd_pcm_substream *substream)
5194 {
5195         snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5196         call_pcm_capture_hook(hinfo, codec, substream,
5197                               HDA_GEN_PCM_ACT_PREPARE);
5198         return 0;
5199 }
5200
5201 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5202                                struct hda_codec *codec,
5203                                struct snd_pcm_substream *substream)
5204 {
5205         snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5206         call_pcm_capture_hook(hinfo, codec, substream,
5207                               HDA_GEN_PCM_ACT_CLEANUP);
5208         return 0;
5209 }
5210
5211 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5212                              struct hda_codec *codec,
5213                              struct snd_pcm_substream *substream)
5214 {
5215         call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5216         return 0;
5217 }
5218
5219 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5220                                  struct hda_codec *codec,
5221                                  struct snd_pcm_substream *substream)
5222 {
5223         struct hda_gen_spec *spec = codec->spec;
5224         int err = 0;
5225
5226         mutex_lock(&spec->pcm_mutex);
5227         if (spec->indep_hp && !spec->indep_hp_enabled)
5228                 err = -EBUSY;
5229         else
5230                 spec->active_streams |= 1 << STREAM_INDEP_HP;
5231         call_pcm_playback_hook(hinfo, codec, substream,
5232                                HDA_GEN_PCM_ACT_OPEN);
5233         mutex_unlock(&spec->pcm_mutex);
5234         return err;
5235 }
5236
5237 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5238                                   struct hda_codec *codec,
5239                                   struct snd_pcm_substream *substream)
5240 {
5241         struct hda_gen_spec *spec = codec->spec;
5242         mutex_lock(&spec->pcm_mutex);
5243         spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5244         call_pcm_playback_hook(hinfo, codec, substream,
5245                                HDA_GEN_PCM_ACT_CLOSE);
5246         mutex_unlock(&spec->pcm_mutex);
5247         return 0;
5248 }
5249
5250 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5251                                     struct hda_codec *codec,
5252                                     unsigned int stream_tag,
5253                                     unsigned int format,
5254                                     struct snd_pcm_substream *substream)
5255 {
5256         snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5257         call_pcm_playback_hook(hinfo, codec, substream,
5258                                HDA_GEN_PCM_ACT_PREPARE);
5259         return 0;
5260 }
5261
5262 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5263                                     struct hda_codec *codec,
5264                                     struct snd_pcm_substream *substream)
5265 {
5266         snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5267         call_pcm_playback_hook(hinfo, codec, substream,
5268                                HDA_GEN_PCM_ACT_CLEANUP);
5269         return 0;
5270 }
5271
5272 /*
5273  * Digital out
5274  */
5275 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5276                                  struct hda_codec *codec,
5277                                  struct snd_pcm_substream *substream)
5278 {
5279         struct hda_gen_spec *spec = codec->spec;
5280         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5281 }
5282
5283 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5284                                     struct hda_codec *codec,
5285                                     unsigned int stream_tag,
5286                                     unsigned int format,
5287                                     struct snd_pcm_substream *substream)
5288 {
5289         struct hda_gen_spec *spec = codec->spec;
5290         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5291                                              stream_tag, format, substream);
5292 }
5293
5294 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5295                                     struct hda_codec *codec,
5296                                     struct snd_pcm_substream *substream)
5297 {
5298         struct hda_gen_spec *spec = codec->spec;
5299         return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5300 }
5301
5302 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5303                                   struct hda_codec *codec,
5304                                   struct snd_pcm_substream *substream)
5305 {
5306         struct hda_gen_spec *spec = codec->spec;
5307         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5308 }
5309
5310 /*
5311  * Analog capture
5312  */
5313 #define alt_capture_pcm_open    capture_pcm_open
5314 #define alt_capture_pcm_close   capture_pcm_close
5315
5316 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5317                                    struct hda_codec *codec,
5318                                    unsigned int stream_tag,
5319                                    unsigned int format,
5320                                    struct snd_pcm_substream *substream)
5321 {
5322         struct hda_gen_spec *spec = codec->spec;
5323
5324         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5325                                    stream_tag, 0, format);
5326         call_pcm_capture_hook(hinfo, codec, substream,
5327                               HDA_GEN_PCM_ACT_PREPARE);
5328         return 0;
5329 }
5330
5331 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5332                                    struct hda_codec *codec,
5333                                    struct snd_pcm_substream *substream)
5334 {
5335         struct hda_gen_spec *spec = codec->spec;
5336
5337         snd_hda_codec_cleanup_stream(codec,
5338                                      spec->adc_nids[substream->number + 1]);
5339         call_pcm_capture_hook(hinfo, codec, substream,
5340                               HDA_GEN_PCM_ACT_CLEANUP);
5341         return 0;
5342 }
5343
5344 /*
5345  */
5346 static const struct hda_pcm_stream pcm_analog_playback = {
5347         .substreams = 1,
5348         .channels_min = 2,
5349         .channels_max = 8,
5350         /* NID is set in build_pcms */
5351         .ops = {
5352                 .open = playback_pcm_open,
5353                 .close = playback_pcm_close,
5354                 .prepare = playback_pcm_prepare,
5355                 .cleanup = playback_pcm_cleanup
5356         },
5357 };
5358
5359 static const struct hda_pcm_stream pcm_analog_capture = {
5360         .substreams = 1,
5361         .channels_min = 2,
5362         .channels_max = 2,
5363         /* NID is set in build_pcms */
5364         .ops = {
5365                 .open = capture_pcm_open,
5366                 .close = capture_pcm_close,
5367                 .prepare = capture_pcm_prepare,
5368                 .cleanup = capture_pcm_cleanup
5369         },
5370 };
5371
5372 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5373         .substreams = 1,
5374         .channels_min = 2,
5375         .channels_max = 2,
5376         /* NID is set in build_pcms */
5377         .ops = {
5378                 .open = alt_playback_pcm_open,
5379                 .close = alt_playback_pcm_close,
5380                 .prepare = alt_playback_pcm_prepare,
5381                 .cleanup = alt_playback_pcm_cleanup
5382         },
5383 };
5384
5385 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5386         .substreams = 2, /* can be overridden */
5387         .channels_min = 2,
5388         .channels_max = 2,
5389         /* NID is set in build_pcms */
5390         .ops = {
5391                 .open = alt_capture_pcm_open,
5392                 .close = alt_capture_pcm_close,
5393                 .prepare = alt_capture_pcm_prepare,
5394                 .cleanup = alt_capture_pcm_cleanup
5395         },
5396 };
5397
5398 static const struct hda_pcm_stream pcm_digital_playback = {
5399         .substreams = 1,
5400         .channels_min = 2,
5401         .channels_max = 2,
5402         /* NID is set in build_pcms */
5403         .ops = {
5404                 .open = dig_playback_pcm_open,
5405                 .close = dig_playback_pcm_close,
5406                 .prepare = dig_playback_pcm_prepare,
5407                 .cleanup = dig_playback_pcm_cleanup
5408         },
5409 };
5410
5411 static const struct hda_pcm_stream pcm_digital_capture = {
5412         .substreams = 1,
5413         .channels_min = 2,
5414         .channels_max = 2,
5415         /* NID is set in build_pcms */
5416 };
5417
5418 /* Used by build_pcms to flag that a PCM has no playback stream */
5419 static const struct hda_pcm_stream pcm_null_stream = {
5420         .substreams = 0,
5421         .channels_min = 0,
5422         .channels_max = 0,
5423 };
5424
5425 /*
5426  * dynamic changing ADC PCM streams
5427  */
5428 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5429 {
5430         struct hda_gen_spec *spec = codec->spec;
5431         hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5432
5433         if (spec->cur_adc && spec->cur_adc != new_adc) {
5434                 /* stream is running, let's swap the current ADC */
5435                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5436                 spec->cur_adc = new_adc;
5437                 snd_hda_codec_setup_stream(codec, new_adc,
5438                                            spec->cur_adc_stream_tag, 0,
5439                                            spec->cur_adc_format);
5440                 return true;
5441         }
5442         return false;
5443 }
5444
5445 /* analog capture with dynamic dual-adc changes */
5446 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5447                                        struct hda_codec *codec,
5448                                        unsigned int stream_tag,
5449                                        unsigned int format,
5450                                        struct snd_pcm_substream *substream)
5451 {
5452         struct hda_gen_spec *spec = codec->spec;
5453         spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5454         spec->cur_adc_stream_tag = stream_tag;
5455         spec->cur_adc_format = format;
5456         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5457         return 0;
5458 }
5459
5460 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5461                                        struct hda_codec *codec,
5462                                        struct snd_pcm_substream *substream)
5463 {
5464         struct hda_gen_spec *spec = codec->spec;
5465         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5466         spec->cur_adc = 0;
5467         return 0;
5468 }
5469
5470 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5471         .substreams = 1,
5472         .channels_min = 2,
5473         .channels_max = 2,
5474         .nid = 0, /* fill later */
5475         .ops = {
5476                 .prepare = dyn_adc_capture_pcm_prepare,
5477                 .cleanup = dyn_adc_capture_pcm_cleanup
5478         },
5479 };
5480
5481 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5482                                  const char *chip_name)
5483 {
5484         char *p;
5485
5486         if (*str)
5487                 return;
5488         strlcpy(str, chip_name, len);
5489
5490         /* drop non-alnum chars after a space */
5491         for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5492                 if (!isalnum(p[1])) {
5493                         *p = 0;
5494                         break;
5495                 }
5496         }
5497         strlcat(str, sfx, len);
5498 }
5499
5500 /* copy PCM stream info from @default_str, and override non-NULL entries
5501  * from @spec_str and @nid
5502  */
5503 static void setup_pcm_stream(struct hda_pcm_stream *str,
5504                              const struct hda_pcm_stream *default_str,
5505                              const struct hda_pcm_stream *spec_str,
5506                              hda_nid_t nid)
5507 {
5508         *str = *default_str;
5509         if (nid)
5510                 str->nid = nid;
5511         if (spec_str) {
5512                 if (spec_str->substreams)
5513                         str->substreams = spec_str->substreams;
5514                 if (spec_str->channels_min)
5515                         str->channels_min = spec_str->channels_min;
5516                 if (spec_str->channels_max)
5517                         str->channels_max = spec_str->channels_max;
5518                 if (spec_str->rates)
5519                         str->rates = spec_str->rates;
5520                 if (spec_str->formats)
5521                         str->formats = spec_str->formats;
5522                 if (spec_str->maxbps)
5523                         str->maxbps = spec_str->maxbps;
5524         }
5525 }
5526
5527 /**
5528  * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5529  * @codec: the HDA codec
5530  *
5531  * Pass this to build_pcms patch_ops.
5532  */
5533 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5534 {
5535         struct hda_gen_spec *spec = codec->spec;
5536         struct hda_pcm *info;
5537         bool have_multi_adcs;
5538
5539         if (spec->no_analog)
5540                 goto skip_analog;
5541
5542         fill_pcm_stream_name(spec->stream_name_analog,
5543                              sizeof(spec->stream_name_analog),
5544                              " Analog", codec->core.chip_name);
5545         info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5546         if (!info)
5547                 return -ENOMEM;
5548         spec->pcm_rec[0] = info;
5549
5550         if (spec->multiout.num_dacs > 0) {
5551                 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5552                                  &pcm_analog_playback,
5553                                  spec->stream_analog_playback,
5554                                  spec->multiout.dac_nids[0]);
5555                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5556                         spec->multiout.max_channels;
5557                 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5558                     spec->autocfg.line_outs == 2)
5559                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5560                                 snd_pcm_2_1_chmaps;
5561         }
5562         if (spec->num_adc_nids) {
5563                 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5564                                  (spec->dyn_adc_switch ?
5565                                   &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5566                                  spec->stream_analog_capture,
5567                                  spec->adc_nids[0]);
5568         }
5569
5570  skip_analog:
5571         /* SPDIF for stream index #1 */
5572         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5573                 fill_pcm_stream_name(spec->stream_name_digital,
5574                                      sizeof(spec->stream_name_digital),
5575                                      " Digital", codec->core.chip_name);
5576                 info = snd_hda_codec_pcm_new(codec, "%s",
5577                                              spec->stream_name_digital);
5578                 if (!info)
5579                         return -ENOMEM;
5580                 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5581                 spec->pcm_rec[1] = info;
5582                 if (spec->dig_out_type)
5583                         info->pcm_type = spec->dig_out_type;
5584                 else
5585                         info->pcm_type = HDA_PCM_TYPE_SPDIF;
5586                 if (spec->multiout.dig_out_nid)
5587                         setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5588                                          &pcm_digital_playback,
5589                                          spec->stream_digital_playback,
5590                                          spec->multiout.dig_out_nid);
5591                 if (spec->dig_in_nid)
5592                         setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5593                                          &pcm_digital_capture,
5594                                          spec->stream_digital_capture,
5595                                          spec->dig_in_nid);
5596         }
5597
5598         if (spec->no_analog)
5599                 return 0;
5600
5601         /* If the use of more than one ADC is requested for the current
5602          * model, configure a second analog capture-only PCM.
5603          */
5604         have_multi_adcs = (spec->num_adc_nids > 1) &&
5605                 !spec->dyn_adc_switch && !spec->auto_mic;
5606         /* Additional Analaog capture for index #2 */
5607         if (spec->alt_dac_nid || have_multi_adcs) {
5608                 fill_pcm_stream_name(spec->stream_name_alt_analog,
5609                                      sizeof(spec->stream_name_alt_analog),
5610                              " Alt Analog", codec->core.chip_name);
5611                 info = snd_hda_codec_pcm_new(codec, "%s",
5612                                              spec->stream_name_alt_analog);
5613                 if (!info)
5614                         return -ENOMEM;
5615                 spec->pcm_rec[2] = info;
5616                 if (spec->alt_dac_nid)
5617                         setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5618                                          &pcm_analog_alt_playback,
5619                                          spec->stream_analog_alt_playback,
5620                                          spec->alt_dac_nid);
5621                 else
5622                         setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5623                                          &pcm_null_stream, NULL, 0);
5624                 if (have_multi_adcs) {
5625                         setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5626                                          &pcm_analog_alt_capture,
5627                                          spec->stream_analog_alt_capture,
5628                                          spec->adc_nids[1]);
5629                         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5630                                 spec->num_adc_nids - 1;
5631                 } else {
5632                         setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5633                                          &pcm_null_stream, NULL, 0);
5634                 }
5635         }
5636
5637         return 0;
5638 }
5639 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5640
5641
5642 /*
5643  * Standard auto-parser initializations
5644  */
5645
5646 /* configure the given path as a proper output */
5647 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5648 {
5649         struct nid_path *path;
5650         hda_nid_t pin;
5651
5652         path = snd_hda_get_path_from_idx(codec, path_idx);
5653         if (!path || !path->depth)
5654                 return;
5655         pin = path->path[path->depth - 1];
5656         restore_pin_ctl(codec, pin);
5657         snd_hda_activate_path(codec, path, path->active,
5658                               aamix_default(codec->spec));
5659         set_pin_eapd(codec, pin, path->active);
5660 }
5661
5662 /* initialize primary output paths */
5663 static void init_multi_out(struct hda_codec *codec)
5664 {
5665         struct hda_gen_spec *spec = codec->spec;
5666         int i;
5667
5668         for (i = 0; i < spec->autocfg.line_outs; i++)
5669                 set_output_and_unmute(codec, spec->out_paths[i]);
5670 }
5671
5672
5673 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5674 {
5675         int i;
5676
5677         for (i = 0; i < num_outs; i++)
5678                 set_output_and_unmute(codec, paths[i]);
5679 }
5680
5681 /* initialize hp and speaker paths */
5682 static void init_extra_out(struct hda_codec *codec)
5683 {
5684         struct hda_gen_spec *spec = codec->spec;
5685
5686         if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5687                 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5688         if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5689                 __init_extra_out(codec, spec->autocfg.speaker_outs,
5690                                  spec->speaker_paths);
5691 }
5692
5693 /* initialize multi-io paths */
5694 static void init_multi_io(struct hda_codec *codec)
5695 {
5696         struct hda_gen_spec *spec = codec->spec;
5697         int i;
5698
5699         for (i = 0; i < spec->multi_ios; i++) {
5700                 hda_nid_t pin = spec->multi_io[i].pin;
5701                 struct nid_path *path;
5702                 path = get_multiio_path(codec, i);
5703                 if (!path)
5704                         continue;
5705                 if (!spec->multi_io[i].ctl_in)
5706                         spec->multi_io[i].ctl_in =
5707                                 snd_hda_codec_get_pin_target(codec, pin);
5708                 snd_hda_activate_path(codec, path, path->active,
5709                                       aamix_default(spec));
5710         }
5711 }
5712
5713 static void init_aamix_paths(struct hda_codec *codec)
5714 {
5715         struct hda_gen_spec *spec = codec->spec;
5716
5717         if (!spec->have_aamix_ctl)
5718                 return;
5719         if (!has_aamix_out_paths(spec))
5720                 return;
5721         update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5722                            spec->aamix_out_paths[0],
5723                            spec->autocfg.line_out_type);
5724         update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5725                            spec->aamix_out_paths[1],
5726                            AUTO_PIN_HP_OUT);
5727         update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5728                            spec->aamix_out_paths[2],
5729                            AUTO_PIN_SPEAKER_OUT);
5730 }
5731
5732 /* set up input pins and loopback paths */
5733 static void init_analog_input(struct hda_codec *codec)
5734 {
5735         struct hda_gen_spec *spec = codec->spec;
5736         struct auto_pin_cfg *cfg = &spec->autocfg;
5737         int i;
5738
5739         for (i = 0; i < cfg->num_inputs; i++) {
5740                 hda_nid_t nid = cfg->inputs[i].pin;
5741                 if (is_input_pin(codec, nid))
5742                         restore_pin_ctl(codec, nid);
5743
5744                 /* init loopback inputs */
5745                 if (spec->mixer_nid) {
5746                         resume_path_from_idx(codec, spec->loopback_paths[i]);
5747                         resume_path_from_idx(codec, spec->loopback_merge_path);
5748                 }
5749         }
5750 }
5751
5752 /* initialize ADC paths */
5753 static void init_input_src(struct hda_codec *codec)
5754 {
5755         struct hda_gen_spec *spec = codec->spec;
5756         struct hda_input_mux *imux = &spec->input_mux;
5757         struct nid_path *path;
5758         int i, c, nums;
5759
5760         if (spec->dyn_adc_switch)
5761                 nums = 1;
5762         else
5763                 nums = spec->num_adc_nids;
5764
5765         for (c = 0; c < nums; c++) {
5766                 for (i = 0; i < imux->num_items; i++) {
5767                         path = get_input_path(codec, c, i);
5768                         if (path) {
5769                                 bool active = path->active;
5770                                 if (i == spec->cur_mux[c])
5771                                         active = true;
5772                                 snd_hda_activate_path(codec, path, active, false);
5773                         }
5774                 }
5775                 if (spec->hp_mic)
5776                         update_hp_mic(codec, c, true);
5777         }
5778
5779         if (spec->cap_sync_hook)
5780                 spec->cap_sync_hook(codec, NULL, NULL);
5781 }
5782
5783 /* set right pin controls for digital I/O */
5784 static void init_digital(struct hda_codec *codec)
5785 {
5786         struct hda_gen_spec *spec = codec->spec;
5787         int i;
5788         hda_nid_t pin;
5789
5790         for (i = 0; i < spec->autocfg.dig_outs; i++)
5791                 set_output_and_unmute(codec, spec->digout_paths[i]);
5792         pin = spec->autocfg.dig_in_pin;
5793         if (pin) {
5794                 restore_pin_ctl(codec, pin);
5795                 resume_path_from_idx(codec, spec->digin_path);
5796         }
5797 }
5798
5799 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5800  * invalid unsol tags by some reason
5801  */
5802 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5803 {
5804         int i;
5805
5806         for (i = 0; i < codec->init_pins.used; i++) {
5807                 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5808                 hda_nid_t nid = pin->nid;
5809                 if (is_jack_detectable(codec, nid) &&
5810                     !snd_hda_jack_tbl_get(codec, nid))
5811                         snd_hda_codec_update_cache(codec, nid, 0,
5812                                         AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5813         }
5814 }
5815
5816 /**
5817  * snd_hda_gen_init - initialize the generic spec
5818  * @codec: the HDA codec
5819  *
5820  * This can be put as patch_ops init function.
5821  */
5822 int snd_hda_gen_init(struct hda_codec *codec)
5823 {
5824         struct hda_gen_spec *spec = codec->spec;
5825
5826         if (spec->init_hook)
5827                 spec->init_hook(codec);
5828
5829         if (!spec->skip_verbs)
5830                 snd_hda_apply_verbs(codec);
5831
5832         init_multi_out(codec);
5833         init_extra_out(codec);
5834         init_multi_io(codec);
5835         init_aamix_paths(codec);
5836         init_analog_input(codec);
5837         init_input_src(codec);
5838         init_digital(codec);
5839
5840         clear_unsol_on_unused_pins(codec);
5841
5842         sync_all_pin_power_ctls(codec);
5843
5844         /* call init functions of standard auto-mute helpers */
5845         update_automute_all(codec);
5846
5847         regcache_sync(codec->core.regmap);
5848
5849         if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5850                 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5851
5852         hda_call_check_power_status(codec, 0x01);
5853         return 0;
5854 }
5855 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
5856
5857 /**
5858  * snd_hda_gen_free - free the generic spec
5859  * @codec: the HDA codec
5860  *
5861  * This can be put as patch_ops free function.
5862  */
5863 void snd_hda_gen_free(struct hda_codec *codec)
5864 {
5865         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
5866         snd_hda_gen_spec_free(codec->spec);
5867         kfree(codec->spec);
5868         codec->spec = NULL;
5869 }
5870 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
5871
5872 #ifdef CONFIG_PM
5873 /**
5874  * snd_hda_gen_check_power_status - check the loopback power save state
5875  * @codec: the HDA codec
5876  * @nid: NID to inspect
5877  *
5878  * This can be put as patch_ops check_power_status function.
5879  */
5880 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5881 {
5882         struct hda_gen_spec *spec = codec->spec;
5883         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5884 }
5885 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
5886 #endif
5887
5888
5889 /*
5890  * the generic codec support
5891  */
5892
5893 static const struct hda_codec_ops generic_patch_ops = {
5894         .build_controls = snd_hda_gen_build_controls,
5895         .build_pcms = snd_hda_gen_build_pcms,
5896         .init = snd_hda_gen_init,
5897         .free = snd_hda_gen_free,
5898         .unsol_event = snd_hda_jack_unsol_event,
5899 #ifdef CONFIG_PM
5900         .check_power_status = snd_hda_gen_check_power_status,
5901 #endif
5902 };
5903
5904 /*
5905  * snd_hda_parse_generic_codec - Generic codec parser
5906  * @codec: the HDA codec
5907  */
5908 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
5909 {
5910         struct hda_gen_spec *spec;
5911         int err;
5912
5913         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5914         if (!spec)
5915                 return -ENOMEM;
5916         snd_hda_gen_spec_init(spec);
5917         codec->spec = spec;
5918
5919         err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5920         if (err < 0)
5921                 goto error;
5922
5923         err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5924         if (err < 0)
5925                 goto error;
5926
5927         codec->patch_ops = generic_patch_ops;
5928         return 0;
5929
5930 error:
5931         snd_hda_gen_free(codec);
5932         return err;
5933 }
5934
5935 static const struct hda_device_id snd_hda_id_generic[] = {
5936         HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
5937         {} /* terminator */
5938 };
5939 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
5940
5941 static struct hda_codec_driver generic_driver = {
5942         .id = snd_hda_id_generic,
5943 };
5944
5945 module_hda_codec_driver(generic_driver);
5946
5947 MODULE_LICENSE("GPL");
5948 MODULE_DESCRIPTION("Generic HD-audio codec parser");