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