ALSA: hda - Allow aamix in the primary output path
[pandora-kernel.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/ctype.h>
28 #include <linux/string.h>
29 #include <sound/core.h>
30 #include <sound/jack.h>
31 #include "hda_codec.h"
32 #include "hda_local.h"
33 #include "hda_auto_parser.h"
34 #include "hda_jack.h"
35 #include "hda_generic.h"
36
37
38 /* initialize hda_gen_spec struct */
39 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
40 {
41         snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42         snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
43         snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
44         return 0;
45 }
46 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
47
48 struct snd_kcontrol_new *
49 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
50                      const struct snd_kcontrol_new *temp)
51 {
52         struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
53         if (!knew)
54                 return NULL;
55         *knew = *temp;
56         if (name)
57                 knew->name = kstrdup(name, GFP_KERNEL);
58         else if (knew->name)
59                 knew->name = kstrdup(knew->name, GFP_KERNEL);
60         if (!knew->name)
61                 return NULL;
62         return knew;
63 }
64 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
65
66 static void free_kctls(struct hda_gen_spec *spec)
67 {
68         if (spec->kctls.list) {
69                 struct snd_kcontrol_new *kctl = spec->kctls.list;
70                 int i;
71                 for (i = 0; i < spec->kctls.used; i++)
72                         kfree(kctl[i].name);
73         }
74         snd_array_free(&spec->kctls);
75 }
76
77 static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
78                                           unsigned int nums,
79                                           struct hda_ctl_ops *ops)
80 {
81         struct hda_gen_spec *spec = codec->spec;
82         struct hda_bind_ctls **ctlp, *ctl;
83         ctlp = snd_array_new(&spec->bind_ctls);
84         if (!ctlp)
85                 return NULL;
86         ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
87         *ctlp = ctl;
88         if (ctl)
89                 ctl->ops = ops;
90         return ctl;
91 }
92
93 static void free_bind_ctls(struct hda_gen_spec *spec)
94 {
95         if (spec->bind_ctls.list) {
96                 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
97                 int i;
98                 for (i = 0; i < spec->bind_ctls.used; i++)
99                         kfree(ctl[i]);
100         }
101         snd_array_free(&spec->bind_ctls);
102 }
103
104 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
105 {
106         if (!spec)
107                 return;
108         free_kctls(spec);
109         free_bind_ctls(spec);
110         snd_array_free(&spec->paths);
111 }
112 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
113
114 /*
115  * parsing paths
116  */
117
118 /* get the path between the given NIDs;
119  * passing 0 to either @pin or @dac behaves as a wildcard
120  */
121 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
122                                       hda_nid_t from_nid, hda_nid_t to_nid)
123 {
124         struct hda_gen_spec *spec = codec->spec;
125         int i;
126
127         for (i = 0; i < spec->paths.used; i++) {
128                 struct nid_path *path = snd_array_elem(&spec->paths, i);
129                 if (path->depth <= 0)
130                         continue;
131                 if ((!from_nid || path->path[0] == from_nid) &&
132                     (!to_nid || path->path[path->depth - 1] == to_nid))
133                         return path;
134         }
135         return NULL;
136 }
137 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
138
139 /* check whether the given DAC is already found in any existing paths */
140 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
141 {
142         struct hda_gen_spec *spec = codec->spec;
143         int i;
144
145         for (i = 0; i < spec->paths.used; i++) {
146                 struct nid_path *path = snd_array_elem(&spec->paths, i);
147                 if (path->path[0] == nid)
148                         return true;
149         }
150         return false;
151 }
152
153 /* check whether the given two widgets can be connected */
154 static bool is_reachable_path(struct hda_codec *codec,
155                               hda_nid_t from_nid, hda_nid_t to_nid)
156 {
157         if (!from_nid || !to_nid)
158                 return false;
159         return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
160 }
161
162 /* nid, dir and idx */
163 #define AMP_VAL_COMPARE_MASK    (0xffff | (1U << 18) | (0x0f << 19))
164
165 /* check whether the given ctl is already assigned in any path elements */
166 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
167 {
168         struct hda_gen_spec *spec = codec->spec;
169         int i;
170
171         val &= AMP_VAL_COMPARE_MASK;
172         for (i = 0; i < spec->paths.used; i++) {
173                 struct nid_path *path = snd_array_elem(&spec->paths, i);
174                 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
175                         return true;
176         }
177         return false;
178 }
179
180 /* check whether a control with the given (nid, dir, idx) was assigned */
181 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
182                               int dir, int idx)
183 {
184         unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
185         return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
186                 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
187 }
188
189 static void print_nid_path(const char *pfx, struct nid_path *path)
190 {
191         char buf[40];
192         int i;
193
194
195         buf[0] = 0;
196         for (i = 0; i < path->depth; i++) {
197                 char tmp[4];
198                 sprintf(tmp, ":%02x", path->path[i]);
199                 strlcat(buf, tmp, sizeof(buf));
200         }
201         snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
202 }
203
204 /* called recursively */
205 static bool __parse_nid_path(struct hda_codec *codec,
206                              hda_nid_t from_nid, hda_nid_t to_nid,
207                              int with_aa_mix, struct nid_path *path, int depth)
208 {
209         struct hda_gen_spec *spec = codec->spec;
210         hda_nid_t conn[16];
211         int i, nums;
212
213         if (to_nid == spec->mixer_nid) {
214                 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
215                         return false;
216                 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
217         }
218
219         nums = snd_hda_get_connections(codec, to_nid, conn, ARRAY_SIZE(conn));
220         for (i = 0; i < nums; i++) {
221                 if (conn[i] != from_nid) {
222                         /* special case: when from_nid is 0,
223                          * try to find an empty DAC
224                          */
225                         if (from_nid ||
226                             get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
227                             is_dac_already_used(codec, conn[i]))
228                                 continue;
229                 }
230                 /* aa-mix is requested but not included? */
231                 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
232                         goto found;
233         }
234         if (depth >= MAX_NID_PATH_DEPTH)
235                 return false;
236         for (i = 0; i < nums; i++) {
237                 unsigned int type;
238                 type = get_wcaps_type(get_wcaps(codec, conn[i]));
239                 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
240                     type == AC_WID_PIN)
241                         continue;
242                 if (__parse_nid_path(codec, from_nid, conn[i],
243                                      with_aa_mix, path, depth + 1))
244                         goto found;
245         }
246         return false;
247
248  found:
249         path->path[path->depth] = conn[i];
250         path->idx[path->depth + 1] = i;
251         if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
252                 path->multi[path->depth + 1] = 1;
253         path->depth++;
254         return true;
255 }
256
257 /* parse the widget path from the given nid to the target nid;
258  * when @from_nid is 0, try to find an empty DAC;
259  * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
260  * excluded, only the paths that don't go through the mixer will be chosen.
261  * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
262  * spec->mixer_nid will be chosen.
263  * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
264  */
265 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
266                             hda_nid_t to_nid, int with_aa_mix,
267                             struct nid_path *path)
268 {
269         if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
270                 path->path[path->depth] = to_nid;
271                 path->depth++;
272                 return true;
273         }
274         return false;
275 }
276 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
277
278 /*
279  * parse the path between the given NIDs and add to the path list.
280  * if no valid path is found, return NULL
281  */
282 struct nid_path *
283 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
284                      hda_nid_t to_nid, int with_aa_mix)
285 {
286         struct hda_gen_spec *spec = codec->spec;
287         struct nid_path *path;
288
289         if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
290                 return NULL;
291
292         path = snd_array_new(&spec->paths);
293         if (!path)
294                 return NULL;
295         memset(path, 0, sizeof(*path));
296         if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
297                 return path;
298         /* push back */
299         spec->paths.used--;
300         return NULL;
301 }
302 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
303
304 /* look for an empty DAC slot */
305 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
306                               bool is_digital)
307 {
308         struct hda_gen_spec *spec = codec->spec;
309         bool cap_digital;
310         int i;
311
312         for (i = 0; i < spec->num_all_dacs; i++) {
313                 hda_nid_t nid = spec->all_dacs[i];
314                 if (!nid || is_dac_already_used(codec, nid))
315                         continue;
316                 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
317                 if (is_digital != cap_digital)
318                         continue;
319                 if (is_reachable_path(codec, nid, pin))
320                         return nid;
321         }
322         return 0;
323 }
324
325 /* replace the channels in the composed amp value with the given number */
326 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
327 {
328         val &= ~(0x3U << 16);
329         val |= chs << 16;
330         return val;
331 }
332
333 /* check whether the widget has the given amp capability for the direction */
334 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
335                            int dir, unsigned int bits)
336 {
337         if (!nid)
338                 return false;
339         if (get_wcaps(codec, nid) & (1 << (dir + 1)))
340                 if (query_amp_caps(codec, nid, dir) & bits)
341                         return true;
342         return false;
343 }
344
345 #define nid_has_mute(codec, nid, dir) \
346         check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
347 #define nid_has_volume(codec, nid, dir) \
348         check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
349
350 /* look for a widget suitable for assigning a mute switch in the path */
351 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
352                                        struct nid_path *path)
353 {
354         int i;
355
356         for (i = path->depth - 1; i >= 0; i--) {
357                 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
358                         return path->path[i];
359                 if (i != path->depth - 1 && i != 0 &&
360                     nid_has_mute(codec, path->path[i], HDA_INPUT))
361                         return path->path[i];
362         }
363         return 0;
364 }
365
366 /* look for a widget suitable for assigning a volume ctl in the path */
367 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
368                                       struct nid_path *path)
369 {
370         int i;
371
372         for (i = path->depth - 1; i >= 0; i--) {
373                 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
374                         return path->path[i];
375         }
376         return 0;
377 }
378
379 /*
380  * path activation / deactivation
381  */
382
383 /* can have the amp-in capability? */
384 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
385 {
386         hda_nid_t nid = path->path[idx];
387         unsigned int caps = get_wcaps(codec, nid);
388         unsigned int type = get_wcaps_type(caps);
389
390         if (!(caps & AC_WCAP_IN_AMP))
391                 return false;
392         if (type == AC_WID_PIN && idx > 0) /* only for input pins */
393                 return false;
394         return true;
395 }
396
397 /* can have the amp-out capability? */
398 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
399 {
400         hda_nid_t nid = path->path[idx];
401         unsigned int caps = get_wcaps(codec, nid);
402         unsigned int type = get_wcaps_type(caps);
403
404         if (!(caps & AC_WCAP_OUT_AMP))
405                 return false;
406         if (type == AC_WID_PIN && !idx) /* only for output pins */
407                 return false;
408         return true;
409 }
410
411 /* check whether the given (nid,dir,idx) is active */
412 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
413                           unsigned int idx, unsigned int dir)
414 {
415         struct hda_gen_spec *spec = codec->spec;
416         int i, n;
417
418         for (n = 0; n < spec->paths.used; n++) {
419                 struct nid_path *path = snd_array_elem(&spec->paths, n);
420                 if (!path->active)
421                         continue;
422                 for (i = 0; i < path->depth; i++) {
423                         if (path->path[i] == nid) {
424                                 if (dir == HDA_OUTPUT || path->idx[i] == idx)
425                                         return true;
426                                 break;
427                         }
428                 }
429         }
430         return false;
431 }
432
433 /* get the default amp value for the target state */
434 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
435                                    int dir, bool enable)
436 {
437         unsigned int caps;
438         unsigned int val = 0;
439
440         caps = query_amp_caps(codec, nid, dir);
441         if (caps & AC_AMPCAP_NUM_STEPS) {
442                 /* set to 0dB */
443                 if (enable)
444                         val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
445         }
446         if (caps & AC_AMPCAP_MUTE) {
447                 if (!enable)
448                         val |= HDA_AMP_MUTE;
449         }
450         return val;
451 }
452
453 /* initialize the amp value (only at the first time) */
454 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
455 {
456         int val = get_amp_val_to_activate(codec, nid, dir, false);
457         snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
458 }
459
460 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
461                          int idx, bool enable)
462 {
463         int val;
464         if (is_ctl_associated(codec, nid, dir, idx) ||
465             is_active_nid(codec, nid, dir, idx))
466                 return;
467         val = get_amp_val_to_activate(codec, nid, dir, enable);
468         snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
469 }
470
471 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
472                              int i, bool enable)
473 {
474         hda_nid_t nid = path->path[i];
475         init_amp(codec, nid, HDA_OUTPUT, 0);
476         activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
477 }
478
479 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
480                             int i, bool enable, bool add_aamix)
481 {
482         struct hda_gen_spec *spec = codec->spec;
483         hda_nid_t conn[16];
484         int n, nums, idx;
485         int type;
486         hda_nid_t nid = path->path[i];
487
488         nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
489         type = get_wcaps_type(get_wcaps(codec, nid));
490         if (type == AC_WID_PIN ||
491             (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
492                 nums = 1;
493                 idx = 0;
494         } else
495                 idx = path->idx[i];
496
497         for (n = 0; n < nums; n++)
498                 init_amp(codec, nid, HDA_INPUT, n);
499
500         if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
501                 return;
502
503         /* here is a little bit tricky in comparison with activate_amp_out();
504          * when aa-mixer is available, we need to enable the path as well
505          */
506         for (n = 0; n < nums; n++) {
507                 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
508                         continue;
509                 activate_amp(codec, nid, HDA_INPUT, n, enable);
510         }
511 }
512
513 /* activate or deactivate the given path
514  * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
515  */
516 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
517                            bool enable, bool add_aamix)
518 {
519         int i;
520
521         if (!enable)
522                 path->active = false;
523
524         for (i = path->depth - 1; i >= 0; i--) {
525                 if (enable && path->multi[i])
526                         snd_hda_codec_write_cache(codec, path->path[i], 0,
527                                             AC_VERB_SET_CONNECT_SEL,
528                                             path->idx[i]);
529                 if (has_amp_in(codec, path, i))
530                         activate_amp_in(codec, path, i, enable, add_aamix);
531                 if (has_amp_out(codec, path, i))
532                         activate_amp_out(codec, path, i, enable);
533         }
534
535         if (enable)
536                 path->active = true;
537 }
538 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
539
540 /* turn on/off EAPD on the given pin */
541 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
542 {
543         struct hda_gen_spec *spec = codec->spec;
544         if (spec->own_eapd_ctl ||
545             !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
546                 return;
547         snd_hda_codec_update_cache(codec, pin, 0,
548                                    AC_VERB_SET_EAPD_BTLENABLE,
549                                    enable ? 0x02 : 0x00);
550 }
551
552
553 /*
554  * Helper functions for creating mixer ctl elements
555  */
556
557 enum {
558         HDA_CTL_WIDGET_VOL,
559         HDA_CTL_WIDGET_MUTE,
560         HDA_CTL_BIND_MUTE,
561         HDA_CTL_BIND_VOL,
562         HDA_CTL_BIND_SW,
563 };
564 static const struct snd_kcontrol_new control_templates[] = {
565         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
566         HDA_CODEC_MUTE(NULL, 0, 0, 0),
567         HDA_BIND_MUTE(NULL, 0, 0, 0),
568         HDA_BIND_VOL(NULL, 0),
569         HDA_BIND_SW(NULL, 0),
570 };
571
572 /* add dynamic controls from template */
573 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
574                        int cidx, unsigned long val)
575 {
576         struct snd_kcontrol_new *knew;
577
578         knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
579         if (!knew)
580                 return -ENOMEM;
581         knew->index = cidx;
582         if (get_amp_nid_(val))
583                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
584         knew->private_value = val;
585         return 0;
586 }
587
588 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
589                                 const char *pfx, const char *dir,
590                                 const char *sfx, int cidx, unsigned long val)
591 {
592         char name[32];
593         snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
594         return add_control(spec, type, name, cidx, val);
595 }
596
597 #define add_pb_vol_ctrl(spec, type, pfx, val)                   \
598         add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
599 #define add_pb_sw_ctrl(spec, type, pfx, val)                    \
600         add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
601 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val)                   \
602         add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
603 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val)                    \
604         add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
605
606 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
607                        unsigned int chs, struct nid_path *path)
608 {
609         unsigned int val;
610         if (!path)
611                 return 0;
612         val = path->ctls[NID_PATH_VOL_CTL];
613         if (!val)
614                 return 0;
615         val = amp_val_replace_channels(val, chs);
616         return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
617 }
618
619 /* return the channel bits suitable for the given path->ctls[] */
620 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
621                                int type)
622 {
623         int chs = 1; /* mono (left only) */
624         if (path) {
625                 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
626                 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
627                         chs = 3; /* stereo */
628         }
629         return chs;
630 }
631
632 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
633                           struct nid_path *path)
634 {
635         int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
636         return add_vol_ctl(codec, pfx, cidx, chs, path);
637 }
638
639 /* create a mute-switch for the given mixer widget;
640  * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
641  */
642 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
643                       unsigned int chs, struct nid_path *path)
644 {
645         unsigned int val;
646         int type = HDA_CTL_WIDGET_MUTE;
647
648         if (!path)
649                 return 0;
650         val = path->ctls[NID_PATH_MUTE_CTL];
651         if (!val)
652                 return 0;
653         val = amp_val_replace_channels(val, chs);
654         if (get_amp_direction_(val) == HDA_INPUT) {
655                 hda_nid_t nid = get_amp_nid_(val);
656                 int nums = snd_hda_get_num_conns(codec, nid);
657                 if (nums > 1) {
658                         type = HDA_CTL_BIND_MUTE;
659                         val |= nums << 19;
660                 }
661         }
662         return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
663 }
664
665 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
666                                   int cidx, struct nid_path *path)
667 {
668         int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
669         return add_sw_ctl(codec, pfx, cidx, chs, path);
670 }
671
672 static const char * const channel_name[4] = {
673         "Front", "Surround", "CLFE", "Side"
674 };
675
676 /* give some appropriate ctl name prefix for the given line out channel */
677 static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
678                                     bool can_be_master, int *index)
679 {
680         struct auto_pin_cfg *cfg = &spec->autocfg;
681
682         *index = 0;
683         if (cfg->line_outs == 1 && !spec->multi_ios &&
684             !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
685                 return spec->vmaster_mute.hook ? "PCM" : "Master";
686
687         /* if there is really a single DAC used in the whole output paths,
688          * use it master (or "PCM" if a vmaster hook is present)
689          */
690         if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
691             !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
692                 return spec->vmaster_mute.hook ? "PCM" : "Master";
693
694         switch (cfg->line_out_type) {
695         case AUTO_PIN_SPEAKER_OUT:
696                 if (cfg->line_outs == 1)
697                         return "Speaker";
698                 if (cfg->line_outs == 2)
699                         return ch ? "Bass Speaker" : "Speaker";
700                 break;
701         case AUTO_PIN_HP_OUT:
702                 /* for multi-io case, only the primary out */
703                 if (ch && spec->multi_ios)
704                         break;
705                 *index = ch;
706                 return "Headphone";
707         default:
708                 if (cfg->line_outs == 1 && !spec->multi_ios)
709                         return "PCM";
710                 break;
711         }
712         if (ch >= ARRAY_SIZE(channel_name)) {
713                 snd_BUG();
714                 return "PCM";
715         }
716
717         return channel_name[ch];
718 }
719
720 /*
721  * Parse output paths
722  */
723
724 /* badness definition */
725 enum {
726         /* No primary DAC is found for the main output */
727         BAD_NO_PRIMARY_DAC = 0x10000,
728         /* No DAC is found for the extra output */
729         BAD_NO_DAC = 0x4000,
730         /* No possible multi-ios */
731         BAD_MULTI_IO = 0x103,
732         /* No individual DAC for extra output */
733         BAD_NO_EXTRA_DAC = 0x102,
734         /* No individual DAC for extra surrounds */
735         BAD_NO_EXTRA_SURR_DAC = 0x101,
736         /* Primary DAC shared with main surrounds */
737         BAD_SHARED_SURROUND = 0x100,
738         /* Primary DAC shared with main CLFE */
739         BAD_SHARED_CLFE = 0x10,
740         /* Primary DAC shared with extra surrounds */
741         BAD_SHARED_EXTRA_SURROUND = 0x10,
742         /* Volume widget is shared */
743         BAD_SHARED_VOL = 0x10,
744 };
745
746 /* look for widgets in the path between the given NIDs appropriate for
747  * volume and mute controls, and assign the values to ctls[].
748  *
749  * When no appropriate widget is found in the path, the badness value
750  * is incremented depending on the situation.  The function returns the
751  * total badness for both volume and mute controls.
752  */
753 static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
754                                 hda_nid_t dac)
755 {
756         struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
757         hda_nid_t nid;
758         unsigned int val;
759         int badness = 0;
760
761         if (!path)
762                 return BAD_SHARED_VOL * 2;
763         nid = look_for_out_vol_nid(codec, path);
764         if (nid) {
765                 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
766                 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
767                         badness += BAD_SHARED_VOL;
768                 else
769                         path->ctls[NID_PATH_VOL_CTL] = val;
770         } else
771                 badness += BAD_SHARED_VOL;
772         nid = look_for_out_mute_nid(codec, path);
773         if (nid) {
774                 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
775                 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
776                     nid_has_mute(codec, nid, HDA_OUTPUT))
777                         val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
778                 else
779                         val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
780                 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
781                         badness += BAD_SHARED_VOL;
782                 else
783                         path->ctls[NID_PATH_MUTE_CTL] = val;
784         } else
785                 badness += BAD_SHARED_VOL;
786         return badness;
787 }
788
789 struct badness_table {
790         int no_primary_dac;     /* no primary DAC */
791         int no_dac;             /* no secondary DACs */
792         int shared_primary;     /* primary DAC is shared with main output */
793         int shared_surr;        /* secondary DAC shared with main or primary */
794         int shared_clfe;        /* third DAC shared with main or primary */
795         int shared_surr_main;   /* secondary DAC sahred with main/DAC0 */
796 };
797
798 static struct badness_table main_out_badness = {
799         .no_primary_dac = BAD_NO_PRIMARY_DAC,
800         .no_dac = BAD_NO_DAC,
801         .shared_primary = BAD_NO_PRIMARY_DAC,
802         .shared_surr = BAD_SHARED_SURROUND,
803         .shared_clfe = BAD_SHARED_CLFE,
804         .shared_surr_main = BAD_SHARED_SURROUND,
805 };
806
807 static struct badness_table extra_out_badness = {
808         .no_primary_dac = BAD_NO_DAC,
809         .no_dac = BAD_NO_DAC,
810         .shared_primary = BAD_NO_EXTRA_DAC,
811         .shared_surr = BAD_SHARED_EXTRA_SURROUND,
812         .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
813         .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
814 };
815
816 /* try to assign DACs to pins and return the resultant badness */
817 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
818                            const hda_nid_t *pins, hda_nid_t *dacs,
819                            const struct badness_table *bad)
820 {
821         struct hda_gen_spec *spec = codec->spec;
822         struct auto_pin_cfg *cfg = &spec->autocfg;
823         int i, j;
824         int badness = 0;
825         hda_nid_t dac;
826
827         if (!num_outs)
828                 return 0;
829
830         for (i = 0; i < num_outs; i++) {
831                 struct nid_path *path;
832                 hda_nid_t pin = pins[i];
833                 if (!dacs[i])
834                         dacs[i] = look_for_dac(codec, pin, false);
835                 if (!dacs[i] && !i) {
836                         for (j = 1; j < num_outs; j++) {
837                                 if (is_reachable_path(codec, dacs[j], pin)) {
838                                         dacs[0] = dacs[j];
839                                         dacs[j] = 0;
840                                         break;
841                                 }
842                         }
843                 }
844                 dac = dacs[i];
845                 if (!dac) {
846                         if (is_reachable_path(codec, dacs[0], pin))
847                                 dac = dacs[0];
848                         else if (cfg->line_outs > i &&
849                                  is_reachable_path(codec, spec->private_dac_nids[i], pin))
850                                 dac = spec->private_dac_nids[i];
851                         if (dac) {
852                                 if (!i)
853                                         badness += bad->shared_primary;
854                                 else if (i == 1)
855                                         badness += bad->shared_surr;
856                                 else
857                                         badness += bad->shared_clfe;
858                         } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
859                                 dac = spec->private_dac_nids[0];
860                                 badness += bad->shared_surr_main;
861                         } else if (!i)
862                                 badness += bad->no_primary_dac;
863                         else
864                                 badness += bad->no_dac;
865                 }
866                 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
867                 if (!path && i > 0 && spec->mixer_nid) {
868                         /* try with aamix */
869                         path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
870                 }
871                 if (!path)
872                         dac = dacs[i] = 0;
873                 else
874                         print_nid_path("output", path);
875                 if (dac)
876                         badness += assign_out_path_ctls(codec, pin, dac);
877         }
878
879         return badness;
880 }
881
882 /* return NID if the given pin has only a single connection to a certain DAC */
883 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
884 {
885         struct hda_gen_spec *spec = codec->spec;
886         int i;
887         hda_nid_t nid_found = 0;
888
889         for (i = 0; i < spec->num_all_dacs; i++) {
890                 hda_nid_t nid = spec->all_dacs[i];
891                 if (!nid || is_dac_already_used(codec, nid))
892                         continue;
893                 if (is_reachable_path(codec, nid, pin)) {
894                         if (nid_found)
895                                 return 0;
896                         nid_found = nid;
897                 }
898         }
899         return nid_found;
900 }
901
902 /* check whether the given pin can be a multi-io pin */
903 static bool can_be_multiio_pin(struct hda_codec *codec,
904                                unsigned int location, hda_nid_t nid)
905 {
906         unsigned int defcfg, caps;
907
908         defcfg = snd_hda_codec_get_pincfg(codec, nid);
909         if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
910                 return false;
911         if (location && get_defcfg_location(defcfg) != location)
912                 return false;
913         caps = snd_hda_query_pin_caps(codec, nid);
914         if (!(caps & AC_PINCAP_OUT))
915                 return false;
916         return true;
917 }
918
919 /*
920  * multi-io helper
921  *
922  * When hardwired is set, try to fill ony hardwired pins, and returns
923  * zero if any pins are filled, non-zero if nothing found.
924  * When hardwired is off, try to fill possible input pins, and returns
925  * the badness value.
926  */
927 static int fill_multi_ios(struct hda_codec *codec,
928                           hda_nid_t reference_pin,
929                           bool hardwired, int offset)
930 {
931         struct hda_gen_spec *spec = codec->spec;
932         struct auto_pin_cfg *cfg = &spec->autocfg;
933         int type, i, j, dacs, num_pins, old_pins;
934         unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
935         unsigned int location = get_defcfg_location(defcfg);
936         int badness = 0;
937
938         old_pins = spec->multi_ios;
939         if (old_pins >= 2)
940                 goto end_fill;
941
942         num_pins = 0;
943         for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
944                 for (i = 0; i < cfg->num_inputs; i++) {
945                         if (cfg->inputs[i].type != type)
946                                 continue;
947                         if (can_be_multiio_pin(codec, location,
948                                                cfg->inputs[i].pin))
949                                 num_pins++;
950                 }
951         }
952         if (num_pins < 2)
953                 goto end_fill;
954
955         dacs = spec->multiout.num_dacs;
956         for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
957                 for (i = 0; i < cfg->num_inputs; i++) {
958                         struct nid_path *path;
959                         hda_nid_t nid = cfg->inputs[i].pin;
960                         hda_nid_t dac = 0;
961
962                         if (cfg->inputs[i].type != type)
963                                 continue;
964                         if (!can_be_multiio_pin(codec, location, nid))
965                                 continue;
966                         for (j = 0; j < spec->multi_ios; j++) {
967                                 if (nid == spec->multi_io[j].pin)
968                                         break;
969                         }
970                         if (j < spec->multi_ios)
971                                 continue;
972
973                         if (offset && offset + spec->multi_ios < dacs) {
974                                 dac = spec->private_dac_nids[offset + spec->multi_ios];
975                                 if (!is_reachable_path(codec, dac, nid))
976                                         dac = 0;
977                         }
978                         if (hardwired)
979                                 dac = get_dac_if_single(codec, nid);
980                         else if (!dac)
981                                 dac = look_for_dac(codec, nid, false);
982                         if (!dac) {
983                                 badness++;
984                                 continue;
985                         }
986                         path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
987                         if (!path) {
988                                 badness++;
989                                 continue;
990                         }
991                         print_nid_path("multiio", path);
992                         spec->multi_io[spec->multi_ios].pin = nid;
993                         spec->multi_io[spec->multi_ios].dac = dac;
994                         spec->multi_ios++;
995                         if (spec->multi_ios >= 2)
996                                 break;
997                 }
998         }
999  end_fill:
1000         if (badness)
1001                 badness = BAD_MULTI_IO;
1002         if (old_pins == spec->multi_ios) {
1003                 if (hardwired)
1004                         return 1; /* nothing found */
1005                 else
1006                         return badness; /* no badness if nothing found */
1007         }
1008         if (!hardwired && spec->multi_ios < 2) {
1009                 /* cancel newly assigned paths */
1010                 spec->paths.used -= spec->multi_ios - old_pins;
1011                 spec->multi_ios = old_pins;
1012                 return badness;
1013         }
1014
1015         /* assign volume and mute controls */
1016         for (i = old_pins; i < spec->multi_ios; i++)
1017                 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1018                                                 spec->multi_io[i].dac);
1019
1020         return badness;
1021 }
1022
1023 /* map DACs for all pins in the list if they are single connections */
1024 static bool map_singles(struct hda_codec *codec, int outs,
1025                         const hda_nid_t *pins, hda_nid_t *dacs)
1026 {
1027         struct hda_gen_spec *spec = codec->spec;
1028         int i;
1029         bool found = false;
1030         for (i = 0; i < outs; i++) {
1031                 struct nid_path *path;
1032                 hda_nid_t dac;
1033                 if (dacs[i])
1034                         continue;
1035                 dac = get_dac_if_single(codec, pins[i]);
1036                 if (!dac)
1037                         continue;
1038                 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
1039                 if (!path && i > 0 && spec->mixer_nid)
1040                         path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
1041                 if (path) {
1042                         dacs[i] = dac;
1043                         found = true;
1044                         print_nid_path("output", path);
1045                 }
1046         }
1047         return found;
1048 }
1049
1050 /* fill in the dac_nids table from the parsed pin configuration */
1051 static int fill_and_eval_dacs(struct hda_codec *codec,
1052                               bool fill_hardwired,
1053                               bool fill_mio_first)
1054 {
1055         struct hda_gen_spec *spec = codec->spec;
1056         struct auto_pin_cfg *cfg = &spec->autocfg;
1057         int i, err, badness;
1058
1059         /* set num_dacs once to full for look_for_dac() */
1060         spec->multiout.num_dacs = cfg->line_outs;
1061         spec->multiout.dac_nids = spec->private_dac_nids;
1062         memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1063         memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1064         memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1065         spec->multi_ios = 0;
1066         snd_array_free(&spec->paths);
1067         badness = 0;
1068
1069         /* fill hard-wired DACs first */
1070         if (fill_hardwired) {
1071                 bool mapped;
1072                 do {
1073                         mapped = map_singles(codec, cfg->line_outs,
1074                                              cfg->line_out_pins,
1075                                              spec->private_dac_nids);
1076                         mapped |= map_singles(codec, cfg->hp_outs,
1077                                               cfg->hp_pins,
1078                                               spec->multiout.hp_out_nid);
1079                         mapped |= map_singles(codec, cfg->speaker_outs,
1080                                               cfg->speaker_pins,
1081                                               spec->multiout.extra_out_nid);
1082                         if (fill_mio_first && cfg->line_outs == 1 &&
1083                             cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1084                                 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1085                                 if (!err)
1086                                         mapped = true;
1087                         }
1088                 } while (mapped);
1089         }
1090
1091         badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1092                                    spec->private_dac_nids,
1093                                    &main_out_badness);
1094
1095         /* re-count num_dacs and squash invalid entries */
1096         spec->multiout.num_dacs = 0;
1097         for (i = 0; i < cfg->line_outs; i++) {
1098                 if (spec->private_dac_nids[i])
1099                         spec->multiout.num_dacs++;
1100                 else {
1101                         memmove(spec->private_dac_nids + i,
1102                                 spec->private_dac_nids + i + 1,
1103                                 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1104                         spec->private_dac_nids[cfg->line_outs - 1] = 0;
1105                 }
1106         }
1107
1108         if (fill_mio_first &&
1109             cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1110                 /* try to fill multi-io first */
1111                 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1112                 if (err < 0)
1113                         return err;
1114                 /* we don't count badness at this stage yet */
1115         }
1116
1117         if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1118                 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1119                                       spec->multiout.hp_out_nid,
1120                                       &extra_out_badness);
1121                 if (err < 0)
1122                         return err;
1123                 badness += err;
1124         }
1125         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1126                 err = try_assign_dacs(codec, cfg->speaker_outs,
1127                                       cfg->speaker_pins,
1128                                       spec->multiout.extra_out_nid,
1129                                          &extra_out_badness);
1130                 if (err < 0)
1131                         return err;
1132                 badness += err;
1133         }
1134         if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1135                 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1136                 if (err < 0)
1137                         return err;
1138                 badness += err;
1139         }
1140         if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1141                 /* try multi-ios with HP + inputs */
1142                 int offset = 0;
1143                 if (cfg->line_outs >= 3)
1144                         offset = 1;
1145                 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1146                 if (err < 0)
1147                         return err;
1148                 badness += err;
1149         }
1150
1151         if (spec->multi_ios == 2) {
1152                 for (i = 0; i < 2; i++)
1153                         spec->private_dac_nids[spec->multiout.num_dacs++] =
1154                                 spec->multi_io[i].dac;
1155                 spec->ext_channel_count = 2;
1156         } else if (spec->multi_ios) {
1157                 spec->multi_ios = 0;
1158                 badness += BAD_MULTI_IO;
1159         }
1160
1161         return badness;
1162 }
1163
1164 #define DEBUG_BADNESS
1165
1166 #ifdef DEBUG_BADNESS
1167 #define debug_badness   snd_printdd
1168 #else
1169 #define debug_badness(...)
1170 #endif
1171
1172 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1173 {
1174         debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1175                       cfg->line_out_pins[0], cfg->line_out_pins[1],
1176                       cfg->line_out_pins[2], cfg->line_out_pins[3],
1177                       spec->multiout.dac_nids[0],
1178                       spec->multiout.dac_nids[1],
1179                       spec->multiout.dac_nids[2],
1180                       spec->multiout.dac_nids[3]);
1181         if (spec->multi_ios > 0)
1182                 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1183                               spec->multi_ios,
1184                               spec->multi_io[0].pin, spec->multi_io[1].pin,
1185                               spec->multi_io[0].dac, spec->multi_io[1].dac);
1186         debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1187                       cfg->hp_pins[0], cfg->hp_pins[1],
1188                       cfg->hp_pins[2], cfg->hp_pins[3],
1189                       spec->multiout.hp_out_nid[0],
1190                       spec->multiout.hp_out_nid[1],
1191                       spec->multiout.hp_out_nid[2],
1192                       spec->multiout.hp_out_nid[3]);
1193         debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1194                       cfg->speaker_pins[0], cfg->speaker_pins[1],
1195                       cfg->speaker_pins[2], cfg->speaker_pins[3],
1196                       spec->multiout.extra_out_nid[0],
1197                       spec->multiout.extra_out_nid[1],
1198                       spec->multiout.extra_out_nid[2],
1199                       spec->multiout.extra_out_nid[3]);
1200 }
1201
1202 /* find all available DACs of the codec */
1203 static void fill_all_dac_nids(struct hda_codec *codec)
1204 {
1205         struct hda_gen_spec *spec = codec->spec;
1206         int i;
1207         hda_nid_t nid = codec->start_nid;
1208
1209         spec->num_all_dacs = 0;
1210         memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1211         for (i = 0; i < codec->num_nodes; i++, nid++) {
1212                 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1213                         continue;
1214                 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1215                         snd_printk(KERN_ERR "hda: Too many DACs!\n");
1216                         break;
1217                 }
1218                 spec->all_dacs[spec->num_all_dacs++] = nid;
1219         }
1220 }
1221
1222 static int parse_output_paths(struct hda_codec *codec)
1223 {
1224         struct hda_gen_spec *spec = codec->spec;
1225         struct auto_pin_cfg *cfg = &spec->autocfg;
1226         struct auto_pin_cfg *best_cfg;
1227         int best_badness = INT_MAX;
1228         int badness;
1229         bool fill_hardwired = true, fill_mio_first = true;
1230         bool best_wired = true, best_mio = true;
1231         bool hp_spk_swapped = false;
1232
1233         fill_all_dac_nids(codec);
1234
1235         best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1236         if (!best_cfg)
1237                 return -ENOMEM;
1238         *best_cfg = *cfg;
1239
1240         for (;;) {
1241                 badness = fill_and_eval_dacs(codec, fill_hardwired,
1242                                              fill_mio_first);
1243                 if (badness < 0) {
1244                         kfree(best_cfg);
1245                         return badness;
1246                 }
1247                 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1248                               cfg->line_out_type, fill_hardwired, fill_mio_first,
1249                               badness);
1250                 debug_show_configs(spec, cfg);
1251                 if (badness < best_badness) {
1252                         best_badness = badness;
1253                         *best_cfg = *cfg;
1254                         best_wired = fill_hardwired;
1255                         best_mio = fill_mio_first;
1256                 }
1257                 if (!badness)
1258                         break;
1259                 fill_mio_first = !fill_mio_first;
1260                 if (!fill_mio_first)
1261                         continue;
1262                 fill_hardwired = !fill_hardwired;
1263                 if (!fill_hardwired)
1264                         continue;
1265                 if (hp_spk_swapped)
1266                         break;
1267                 hp_spk_swapped = true;
1268                 if (cfg->speaker_outs > 0 &&
1269                     cfg->line_out_type == AUTO_PIN_HP_OUT) {
1270                         cfg->hp_outs = cfg->line_outs;
1271                         memcpy(cfg->hp_pins, cfg->line_out_pins,
1272                                sizeof(cfg->hp_pins));
1273                         cfg->line_outs = cfg->speaker_outs;
1274                         memcpy(cfg->line_out_pins, cfg->speaker_pins,
1275                                sizeof(cfg->speaker_pins));
1276                         cfg->speaker_outs = 0;
1277                         memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1278                         cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1279                         fill_hardwired = true;
1280                         continue;
1281                 }
1282                 if (cfg->hp_outs > 0 &&
1283                     cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1284                         cfg->speaker_outs = cfg->line_outs;
1285                         memcpy(cfg->speaker_pins, cfg->line_out_pins,
1286                                sizeof(cfg->speaker_pins));
1287                         cfg->line_outs = cfg->hp_outs;
1288                         memcpy(cfg->line_out_pins, cfg->hp_pins,
1289                                sizeof(cfg->hp_pins));
1290                         cfg->hp_outs = 0;
1291                         memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1292                         cfg->line_out_type = AUTO_PIN_HP_OUT;
1293                         fill_hardwired = true;
1294                         continue;
1295                 }
1296                 break;
1297         }
1298
1299         if (badness) {
1300                 debug_badness("==> restoring best_cfg\n");
1301                 *cfg = *best_cfg;
1302                 fill_and_eval_dacs(codec, best_wired, best_mio);
1303         }
1304         debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1305                       cfg->line_out_type, best_wired, best_mio);
1306         debug_show_configs(spec, cfg);
1307
1308         if (cfg->line_out_pins[0]) {
1309                 struct nid_path *path;
1310                 path = snd_hda_get_nid_path(codec,
1311                                             spec->multiout.dac_nids[0],
1312                                             cfg->line_out_pins[0]);
1313                 if (path)
1314                         spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1315         }
1316
1317         kfree(best_cfg);
1318         return 0;
1319 }
1320
1321 /* add playback controls from the parsed DAC table */
1322 static int create_multi_out_ctls(struct hda_codec *codec,
1323                                  const struct auto_pin_cfg *cfg)
1324 {
1325         struct hda_gen_spec *spec = codec->spec;
1326         int i, err, noutputs;
1327
1328         noutputs = cfg->line_outs;
1329         if (spec->multi_ios > 0 && cfg->line_outs < 3)
1330                 noutputs += spec->multi_ios;
1331
1332         for (i = 0; i < noutputs; i++) {
1333                 const char *name;
1334                 int index;
1335                 hda_nid_t dac, pin;
1336                 struct nid_path *path;
1337
1338                 dac = spec->multiout.dac_nids[i];
1339                 if (!dac)
1340                         continue;
1341                 if (i >= cfg->line_outs) {
1342                         pin = spec->multi_io[i - 1].pin;
1343                         index = 0;
1344                         name = channel_name[i];
1345                 } else {
1346                         pin = cfg->line_out_pins[i];
1347                         name = get_line_out_pfx(spec, i, true, &index);
1348                 }
1349
1350                 path = snd_hda_get_nid_path(codec, dac, pin);
1351                 if (!path)
1352                         continue;
1353                 if (!name || !strcmp(name, "CLFE")) {
1354                         /* Center/LFE */
1355                         err = add_vol_ctl(codec, "Center", 0, 1, path);
1356                         if (err < 0)
1357                                 return err;
1358                         err = add_vol_ctl(codec, "LFE", 0, 2, path);
1359                         if (err < 0)
1360                                 return err;
1361                         err = add_sw_ctl(codec, "Center", 0, 1, path);
1362                         if (err < 0)
1363                                 return err;
1364                         err = add_sw_ctl(codec, "LFE", 0, 2, path);
1365                         if (err < 0)
1366                                 return err;
1367                 } else {
1368                         err = add_stereo_vol(codec, name, index, path);
1369                         if (err < 0)
1370                                 return err;
1371                         err = add_stereo_sw(codec, name, index, path);
1372                         if (err < 0)
1373                                 return err;
1374                 }
1375         }
1376         return 0;
1377 }
1378
1379 static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1380                             hda_nid_t dac, const char *pfx, int cidx)
1381 {
1382         struct nid_path *path;
1383         int err;
1384
1385         path = snd_hda_get_nid_path(codec, dac, pin);
1386         if (!path)
1387                 return 0;
1388         /* bind volume control will be created in the case of dac = 0 */
1389         if (dac) {
1390                 err = add_stereo_vol(codec, pfx, cidx, path);
1391                 if (err < 0)
1392                         return err;
1393         }
1394         err = add_stereo_sw(codec, pfx, cidx, path);
1395         if (err < 0)
1396                 return err;
1397         return 0;
1398 }
1399
1400 /* add playback controls for speaker and HP outputs */
1401 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1402                              const hda_nid_t *pins, const hda_nid_t *dacs,
1403                              const char *pfx)
1404 {
1405         struct hda_gen_spec *spec = codec->spec;
1406         struct hda_bind_ctls *ctl;
1407         char name[32];
1408         int i, n, err;
1409
1410         if (!num_pins || !pins[0])
1411                 return 0;
1412
1413         if (num_pins == 1) {
1414                 hda_nid_t dac = *dacs;
1415                 if (!dac)
1416                         dac = spec->multiout.dac_nids[0];
1417                 return create_extra_out(codec, *pins, dac, pfx, 0);
1418         }
1419
1420         for (i = 0; i < num_pins; i++) {
1421                 hda_nid_t dac;
1422                 if (dacs[num_pins - 1])
1423                         dac = dacs[i]; /* with individual volumes */
1424                 else
1425                         dac = 0;
1426                 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1427                         err = create_extra_out(codec, pins[i], dac,
1428                                                "Bass Speaker", 0);
1429                 } else if (num_pins >= 3) {
1430                         snprintf(name, sizeof(name), "%s %s",
1431                                  pfx, channel_name[i]);
1432                         err = create_extra_out(codec, pins[i], dac, name, 0);
1433                 } else {
1434                         err = create_extra_out(codec, pins[i], dac, pfx, i);
1435                 }
1436                 if (err < 0)
1437                         return err;
1438         }
1439         if (dacs[num_pins - 1])
1440                 return 0;
1441
1442         /* Let's create a bind-controls for volumes */
1443         ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1444         if (!ctl)
1445                 return -ENOMEM;
1446         n = 0;
1447         for (i = 0; i < num_pins; i++) {
1448                 hda_nid_t vol;
1449                 struct nid_path *path;
1450                 if (!pins[i] || !dacs[i])
1451                         continue;
1452                 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1453                 if (!path)
1454                         continue;
1455                 vol = look_for_out_vol_nid(codec, path);
1456                 if (vol)
1457                         ctl->values[n++] =
1458                                 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1459         }
1460         if (n) {
1461                 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1462                 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1463                 if (err < 0)
1464                         return err;
1465         }
1466         return 0;
1467 }
1468
1469 static int create_hp_out_ctls(struct hda_codec *codec)
1470 {
1471         struct hda_gen_spec *spec = codec->spec;
1472         return create_extra_outs(codec, spec->autocfg.hp_outs,
1473                                  spec->autocfg.hp_pins,
1474                                  spec->multiout.hp_out_nid,
1475                                  "Headphone");
1476 }
1477
1478 static int create_speaker_out_ctls(struct hda_codec *codec)
1479 {
1480         struct hda_gen_spec *spec = codec->spec;
1481         return create_extra_outs(codec, spec->autocfg.speaker_outs,
1482                                  spec->autocfg.speaker_pins,
1483                                  spec->multiout.extra_out_nid,
1484                                  "Speaker");
1485 }
1486
1487 /*
1488  * channel mode enum control
1489  */
1490
1491 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1492                         struct snd_ctl_elem_info *uinfo)
1493 {
1494         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1495         struct hda_gen_spec *spec = codec->spec;
1496
1497         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1498         uinfo->count = 1;
1499         uinfo->value.enumerated.items = spec->multi_ios + 1;
1500         if (uinfo->value.enumerated.item > spec->multi_ios)
1501                 uinfo->value.enumerated.item = spec->multi_ios;
1502         sprintf(uinfo->value.enumerated.name, "%dch",
1503                 (uinfo->value.enumerated.item + 1) * 2);
1504         return 0;
1505 }
1506
1507 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1508                        struct snd_ctl_elem_value *ucontrol)
1509 {
1510         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1511         struct hda_gen_spec *spec = codec->spec;
1512         ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1513         return 0;
1514 }
1515
1516 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1517 {
1518         struct hda_gen_spec *spec = codec->spec;
1519         hda_nid_t nid = spec->multi_io[idx].pin;
1520         struct nid_path *path;
1521
1522         path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1523         if (!path)
1524                 return -EINVAL;
1525
1526         if (path->active == output)
1527                 return 0;
1528
1529         if (output) {
1530                 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1531                 snd_hda_activate_path(codec, path, true, true);
1532                 set_pin_eapd(codec, nid, true);
1533         } else {
1534                 set_pin_eapd(codec, nid, false);
1535                 snd_hda_activate_path(codec, path, false, true);
1536                 snd_hda_set_pin_ctl_cache(codec, nid,
1537                                           spec->multi_io[idx].ctl_in);
1538         }
1539         return 0;
1540 }
1541
1542 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1543                        struct snd_ctl_elem_value *ucontrol)
1544 {
1545         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1546         struct hda_gen_spec *spec = codec->spec;
1547         int i, ch;
1548
1549         ch = ucontrol->value.enumerated.item[0];
1550         if (ch < 0 || ch > spec->multi_ios)
1551                 return -EINVAL;
1552         if (ch == (spec->ext_channel_count - 1) / 2)
1553                 return 0;
1554         spec->ext_channel_count = (ch + 1) * 2;
1555         for (i = 0; i < spec->multi_ios; i++)
1556                 set_multi_io(codec, i, i < ch);
1557         spec->multiout.max_channels = max(spec->ext_channel_count,
1558                                           spec->const_channel_count);
1559         if (spec->need_dac_fix)
1560                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1561         return 1;
1562 }
1563
1564 static const struct snd_kcontrol_new channel_mode_enum = {
1565         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1566         .name = "Channel Mode",
1567         .info = ch_mode_info,
1568         .get = ch_mode_get,
1569         .put = ch_mode_put,
1570 };
1571
1572 static int create_multi_channel_mode(struct hda_codec *codec)
1573 {
1574         struct hda_gen_spec *spec = codec->spec;
1575
1576         if (spec->multi_ios > 0) {
1577                 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1578                         return -ENOMEM;
1579         }
1580         return 0;
1581 }
1582
1583 /*
1584  * shared headphone/mic handling
1585  */
1586
1587 static void call_update_outputs(struct hda_codec *codec);
1588
1589 /* for shared I/O, change the pin-control accordingly */
1590 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1591 {
1592         struct hda_gen_spec *spec = codec->spec;
1593         unsigned int val;
1594         hda_nid_t pin = spec->autocfg.inputs[1].pin;
1595         /* NOTE: this assumes that there are only two inputs, the
1596          * first is the real internal mic and the second is HP/mic jack.
1597          */
1598
1599         val = snd_hda_get_default_vref(codec, pin);
1600
1601         /* This pin does not have vref caps - let's enable vref on pin 0x18
1602            instead, as suggested by Realtek */
1603         if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1604                 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1605                 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1606                 if (vref_val != AC_PINCTL_VREF_HIZ)
1607                         snd_hda_set_pin_ctl_cache(codec, vref_pin,
1608                                         PIN_IN | (set_as_mic ? vref_val : 0));
1609         }
1610
1611         val = set_as_mic ? val | PIN_IN : PIN_HP;
1612         snd_hda_set_pin_ctl_cache(codec, pin, val);
1613
1614         spec->automute_speaker = !set_as_mic;
1615         call_update_outputs(codec);
1616 }
1617
1618 /* create a shared input with the headphone out */
1619 static int create_shared_input(struct hda_codec *codec)
1620 {
1621         struct hda_gen_spec *spec = codec->spec;
1622         struct auto_pin_cfg *cfg = &spec->autocfg;
1623         unsigned int defcfg;
1624         hda_nid_t nid;
1625
1626         /* only one internal input pin? */
1627         if (cfg->num_inputs != 1)
1628                 return 0;
1629         defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1630         if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1631                 return 0;
1632
1633         if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1634                 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1635         else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1636                 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1637         else
1638                 return 0; /* both not available */
1639
1640         if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1641                 return 0; /* no input */
1642
1643         cfg->inputs[1].pin = nid;
1644         cfg->inputs[1].type = AUTO_PIN_MIC;
1645         cfg->num_inputs = 2;
1646         spec->shared_mic_hp = 1;
1647         snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1648         return 0;
1649 }
1650
1651
1652 /*
1653  * Parse input paths
1654  */
1655
1656 #ifdef CONFIG_PM
1657 /* add the powersave loopback-list entry */
1658 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1659 {
1660         struct hda_amp_list *list;
1661
1662         if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1663                 return;
1664         list = spec->loopback_list + spec->num_loopbacks;
1665         list->nid = mix;
1666         list->dir = HDA_INPUT;
1667         list->idx = idx;
1668         spec->num_loopbacks++;
1669         spec->loopback.amplist = spec->loopback_list;
1670 }
1671 #else
1672 #define add_loopback_list(spec, mix, idx) /* NOP */
1673 #endif
1674
1675 /* create input playback/capture controls for the given pin */
1676 static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1677                             const char *ctlname, int ctlidx,
1678                             hda_nid_t mix_nid)
1679 {
1680         struct hda_gen_spec *spec = codec->spec;
1681         struct nid_path *path;
1682         unsigned int val;
1683         int err, idx;
1684
1685         if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1686             !nid_has_mute(codec, mix_nid, HDA_INPUT))
1687                 return 0; /* no need for analog loopback */
1688
1689         path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
1690         if (!path)
1691                 return -EINVAL;
1692         print_nid_path("loopback", path);
1693
1694         idx = path->idx[path->depth - 1];
1695         if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1696                 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1697                 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1698                 if (err < 0)
1699                         return err;
1700                 path->ctls[NID_PATH_VOL_CTL] = val;
1701         }
1702
1703         if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1704                 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1705                 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
1706                 if (err < 0)
1707                         return err;
1708                 path->ctls[NID_PATH_MUTE_CTL] = val;
1709         }
1710
1711         path->active = true;
1712         add_loopback_list(spec, mix_nid, idx);
1713         return 0;
1714 }
1715
1716 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1717 {
1718         unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1719         return (pincap & AC_PINCAP_IN) != 0;
1720 }
1721
1722 /* Parse the codec tree and retrieve ADCs */
1723 static int fill_adc_nids(struct hda_codec *codec)
1724 {
1725         struct hda_gen_spec *spec = codec->spec;
1726         hda_nid_t nid;
1727         hda_nid_t *adc_nids = spec->adc_nids;
1728         int max_nums = ARRAY_SIZE(spec->adc_nids);
1729         int i, nums = 0;
1730
1731         nid = codec->start_nid;
1732         for (i = 0; i < codec->num_nodes; i++, nid++) {
1733                 unsigned int caps = get_wcaps(codec, nid);
1734                 int type = get_wcaps_type(caps);
1735
1736                 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1737                         continue;
1738                 adc_nids[nums] = nid;
1739                 if (++nums >= max_nums)
1740                         break;
1741         }
1742         spec->num_adc_nids = nums;
1743         return nums;
1744 }
1745
1746 /* filter out invalid adc_nids that don't give all active input pins;
1747  * if needed, check whether dynamic ADC-switching is available
1748  */
1749 static int check_dyn_adc_switch(struct hda_codec *codec)
1750 {
1751         struct hda_gen_spec *spec = codec->spec;
1752         struct hda_input_mux *imux = &spec->input_mux;
1753         hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1754         int i, n, nums;
1755         hda_nid_t pin, adc;
1756
1757  again:
1758         nums = 0;
1759         for (n = 0; n < spec->num_adc_nids; n++) {
1760                 adc = spec->adc_nids[n];
1761                 for (i = 0; i < imux->num_items; i++) {
1762                         pin = spec->imux_pins[i];
1763                         if (!is_reachable_path(codec, pin, adc))
1764                                 break;
1765                 }
1766                 if (i >= imux->num_items)
1767                         adc_nids[nums++] = adc;
1768         }
1769
1770         if (!nums) {
1771                 if (spec->shared_mic_hp) {
1772                         spec->shared_mic_hp = 0;
1773                         imux->num_items = 1;
1774                         goto again;
1775                 }
1776
1777                 /* check whether ADC-switch is possible */
1778                 for (i = 0; i < imux->num_items; i++) {
1779                         pin = spec->imux_pins[i];
1780                         for (n = 0; n < spec->num_adc_nids; n++) {
1781                                 adc = spec->adc_nids[n];
1782                                 if (is_reachable_path(codec, pin, adc)) {
1783                                         spec->dyn_adc_idx[i] = n;
1784                                         break;
1785                                 }
1786                         }
1787                 }
1788
1789                 snd_printdd("hda-codec: enabling ADC switching\n");
1790                 spec->dyn_adc_switch = 1;
1791         } else if (nums != spec->num_adc_nids) {
1792                 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1793                 spec->num_adc_nids = nums;
1794         }
1795
1796         if (imux->num_items == 1 || spec->shared_mic_hp) {
1797                 snd_printdd("hda-codec: reducing to a single ADC\n");
1798                 spec->num_adc_nids = 1; /* reduce to a single ADC */
1799         }
1800
1801         /* single index for individual volumes ctls */
1802         if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1803                 spec->num_adc_nids = 1;
1804
1805         return 0;
1806 }
1807
1808 /*
1809  * create playback/capture controls for input pins
1810  */
1811 static int create_input_ctls(struct hda_codec *codec)
1812 {
1813         struct hda_gen_spec *spec = codec->spec;
1814         const struct auto_pin_cfg *cfg = &spec->autocfg;
1815         hda_nid_t mixer = spec->mixer_nid;
1816         struct hda_input_mux *imux = &spec->input_mux;
1817         int num_adcs;
1818         int i, c, err, type_idx = 0;
1819         const char *prev_label = NULL;
1820
1821         num_adcs = fill_adc_nids(codec);
1822         if (num_adcs < 0)
1823                 return 0;
1824
1825         for (i = 0; i < cfg->num_inputs; i++) {
1826                 hda_nid_t pin;
1827                 const char *label;
1828                 bool imux_added;
1829
1830                 pin = cfg->inputs[i].pin;
1831                 if (!is_input_pin(codec, pin))
1832                         continue;
1833
1834                 label = hda_get_autocfg_input_label(codec, cfg, i);
1835                 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1836                         label = "Headphone Mic";
1837                 if (prev_label && !strcmp(label, prev_label))
1838                         type_idx++;
1839                 else
1840                         type_idx = 0;
1841                 prev_label = label;
1842
1843                 if (mixer) {
1844                         if (is_reachable_path(codec, pin, mixer)) {
1845                                 err = new_analog_input(codec, pin,
1846                                                        label, type_idx, mixer);
1847                                 if (err < 0)
1848                                         return err;
1849                         }
1850                 }
1851
1852                 imux_added = false;
1853                 for (c = 0; c < num_adcs; c++) {
1854                         struct nid_path *path;
1855                         hda_nid_t adc = spec->adc_nids[c];
1856
1857                         if (!is_reachable_path(codec, pin, adc))
1858                                 continue;
1859                         path = snd_array_new(&spec->paths);
1860                         if (!path)
1861                                 return -ENOMEM;
1862                         memset(path, 0, sizeof(*path));
1863                         if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
1864                                 snd_printd(KERN_ERR
1865                                            "invalid input path 0x%x -> 0x%x\n",
1866                                            pin, adc);
1867                                 spec->paths.used--;
1868                                 continue;
1869                         }
1870                         print_nid_path("input", path);
1871
1872                         if (!imux_added) {
1873                                 spec->imux_pins[imux->num_items] = pin;
1874                                 snd_hda_add_imux_item(imux, label,
1875                                                       imux->num_items, NULL);
1876                                 imux_added = true;
1877                         }
1878                 }
1879         }
1880
1881         return 0;
1882 }
1883
1884
1885 /*
1886  * input source mux
1887  */
1888
1889 /* get the ADC NID corresponding to the given index */
1890 static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1891 {
1892         struct hda_gen_spec *spec = codec->spec;
1893         if (spec->dyn_adc_switch)
1894                 adc_idx = spec->dyn_adc_idx[imux_idx];
1895         return spec->adc_nids[adc_idx];
1896 }
1897
1898 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1899                       unsigned int idx);
1900
1901 static int mux_enum_info(struct snd_kcontrol *kcontrol,
1902                          struct snd_ctl_elem_info *uinfo)
1903 {
1904         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1905         struct hda_gen_spec *spec = codec->spec;
1906         return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1907 }
1908
1909 static int mux_enum_get(struct snd_kcontrol *kcontrol,
1910                         struct snd_ctl_elem_value *ucontrol)
1911 {
1912         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1913         struct hda_gen_spec *spec = codec->spec;
1914         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1915
1916         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1917         return 0;
1918 }
1919
1920 static int mux_enum_put(struct snd_kcontrol *kcontrol,
1921                             struct snd_ctl_elem_value *ucontrol)
1922 {
1923         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1924         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1925         return mux_select(codec, adc_idx,
1926                           ucontrol->value.enumerated.item[0]);
1927 }
1928
1929 static const struct snd_kcontrol_new cap_src_temp = {
1930         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1931         .name = "Input Source",
1932         .info = mux_enum_info,
1933         .get = mux_enum_get,
1934         .put = mux_enum_put,
1935 };
1936
1937 /*
1938  * capture volume and capture switch ctls
1939  */
1940
1941 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
1942                           struct snd_ctl_elem_value *ucontrol);
1943
1944 /* call the given amp update function for all amps in the imux list at once */
1945 static int cap_put_caller(struct snd_kcontrol *kcontrol,
1946                           struct snd_ctl_elem_value *ucontrol,
1947                           put_call_t func, int type)
1948 {
1949         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1950         struct hda_gen_spec *spec = codec->spec;
1951         const struct hda_input_mux *imux;
1952         struct nid_path *path;
1953         int i, adc_idx, err = 0;
1954
1955         imux = &spec->input_mux;
1956         adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1957         mutex_lock(&codec->control_mutex);
1958         /* we use the cache-only update at first since multiple input paths
1959          * may shared the same amp; by updating only caches, the redundant
1960          * writes to hardware can be reduced.
1961          */
1962         codec->cached_write = 1;
1963         for (i = 0; i < imux->num_items; i++) {
1964                 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
1965                                             get_adc_nid(codec, adc_idx, i));
1966                 if (!path->ctls[type])
1967                         continue;
1968                 kcontrol->private_value = path->ctls[type];
1969                 err = func(kcontrol, ucontrol);
1970                 if (err < 0)
1971                         goto error;
1972         }
1973  error:
1974         codec->cached_write = 0;
1975         mutex_unlock(&codec->control_mutex);
1976         snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
1977         if (err >= 0 && spec->cap_sync_hook)
1978                 spec->cap_sync_hook(codec);
1979         return err;
1980 }
1981
1982 /* capture volume ctl callbacks */
1983 #define cap_vol_info            snd_hda_mixer_amp_volume_info
1984 #define cap_vol_get             snd_hda_mixer_amp_volume_get
1985 #define cap_vol_tlv             snd_hda_mixer_amp_tlv
1986
1987 static int cap_vol_put(struct snd_kcontrol *kcontrol,
1988                        struct snd_ctl_elem_value *ucontrol)
1989 {
1990         return cap_put_caller(kcontrol, ucontrol,
1991                               snd_hda_mixer_amp_volume_put,
1992                               NID_PATH_VOL_CTL);
1993 }
1994
1995 static const struct snd_kcontrol_new cap_vol_temp = {
1996         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1997         .name = "Capture Volume",
1998         .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1999                    SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2000                    SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2001         .info = cap_vol_info,
2002         .get = cap_vol_get,
2003         .put = cap_vol_put,
2004         .tlv = { .c = cap_vol_tlv },
2005 };
2006
2007 /* capture switch ctl callbacks */
2008 #define cap_sw_info             snd_ctl_boolean_stereo_info
2009 #define cap_sw_get              snd_hda_mixer_amp_switch_get
2010
2011 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2012                       struct snd_ctl_elem_value *ucontrol)
2013 {
2014         return cap_put_caller(kcontrol, ucontrol,
2015                               snd_hda_mixer_amp_switch_put,
2016                               NID_PATH_MUTE_CTL);
2017 }
2018
2019 static const struct snd_kcontrol_new cap_sw_temp = {
2020         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2021         .name = "Capture Switch",
2022         .info = cap_sw_info,
2023         .get = cap_sw_get,
2024         .put = cap_sw_put,
2025 };
2026
2027 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2028 {
2029         hda_nid_t nid;
2030         int i, depth;
2031
2032         path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2033         for (depth = 0; depth < 3; depth++) {
2034                 if (depth >= path->depth)
2035                         return -EINVAL;
2036                 i = path->depth - depth - 1;
2037                 nid = path->path[i];
2038                 if (!path->ctls[NID_PATH_VOL_CTL]) {
2039                         if (nid_has_volume(codec, nid, HDA_OUTPUT))
2040                                 path->ctls[NID_PATH_VOL_CTL] =
2041                                         HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2042                         else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2043                                 int idx = path->idx[i];
2044                                 if (!depth && codec->single_adc_amp)
2045                                         idx = 0;
2046                                 path->ctls[NID_PATH_VOL_CTL] =
2047                                         HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2048                         }
2049                 }
2050                 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2051                         if (nid_has_mute(codec, nid, HDA_OUTPUT))
2052                                 path->ctls[NID_PATH_MUTE_CTL] =
2053                                         HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2054                         else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2055                                 int idx = path->idx[i];
2056                                 if (!depth && codec->single_adc_amp)
2057                                         idx = 0;
2058                                 path->ctls[NID_PATH_MUTE_CTL] =
2059                                         HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2060                         }
2061                 }
2062         }
2063         return 0;
2064 }
2065
2066 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2067 {
2068         struct hda_gen_spec *spec = codec->spec;
2069         struct auto_pin_cfg *cfg = &spec->autocfg;
2070         unsigned int val;
2071         int i;
2072
2073         if (!spec->inv_dmic_split)
2074                 return false;
2075         for (i = 0; i < cfg->num_inputs; i++) {
2076                 if (cfg->inputs[i].pin != nid)
2077                         continue;
2078                 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2079                         return false;
2080                 val = snd_hda_codec_get_pincfg(codec, nid);
2081                 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2082         }
2083         return false;
2084 }
2085
2086 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2087                               int idx, bool is_switch, unsigned int ctl,
2088                               bool inv_dmic)
2089 {
2090         struct hda_gen_spec *spec = codec->spec;
2091         char tmpname[44];
2092         int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2093         const char *sfx = is_switch ? "Switch" : "Volume";
2094         unsigned int chs = inv_dmic ? 1 : 3;
2095         int err;
2096
2097         if (!ctl)
2098                 return 0;
2099
2100         if (label)
2101                 snprintf(tmpname, sizeof(tmpname),
2102                          "%s Capture %s", label, sfx);
2103         else
2104                 snprintf(tmpname, sizeof(tmpname),
2105                          "Capture %s", sfx);
2106         err = add_control(spec, type, tmpname, idx,
2107                           amp_val_replace_channels(ctl, chs));
2108         if (err < 0 || !inv_dmic)
2109                 return err;
2110
2111         /* Make independent right kcontrol */
2112         if (label)
2113                 snprintf(tmpname, sizeof(tmpname),
2114                          "Inverted %s Capture %s", label, sfx);
2115         else
2116                 snprintf(tmpname, sizeof(tmpname),
2117                          "Inverted Capture %s", sfx);
2118         return add_control(spec, type, tmpname, idx,
2119                            amp_val_replace_channels(ctl, 2));
2120 }
2121
2122 /* create single (and simple) capture volume and switch controls */
2123 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2124                                      unsigned int vol_ctl, unsigned int sw_ctl,
2125                                      bool inv_dmic)
2126 {
2127         int err;
2128         err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2129         if (err < 0)
2130                 return err;
2131         err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2132         if (err < 0)
2133                 return err;
2134         return 0;
2135 }
2136
2137 /* create bound capture volume and switch controls */
2138 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2139                                    unsigned int vol_ctl, unsigned int sw_ctl)
2140 {
2141         struct hda_gen_spec *spec = codec->spec;
2142         struct snd_kcontrol_new *knew;
2143
2144         if (vol_ctl) {
2145                 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2146                 if (!knew)
2147                         return -ENOMEM;
2148                 knew->index = idx;
2149                 knew->private_value = vol_ctl;
2150                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2151         }
2152         if (sw_ctl) {
2153                 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2154                 if (!knew)
2155                         return -ENOMEM;
2156                 knew->index = idx;
2157                 knew->private_value = sw_ctl;
2158                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2159         }
2160         return 0;
2161 }
2162
2163 /* return the vol ctl when used first in the imux list */
2164 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2165 {
2166         struct hda_gen_spec *spec = codec->spec;
2167         struct nid_path *path;
2168         unsigned int ctl;
2169         int i;
2170
2171         path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2172                                     get_adc_nid(codec, 0, idx));
2173         if (!path)
2174                 return 0;
2175         ctl = path->ctls[type];
2176         if (!ctl)
2177                 return 0;
2178         for (i = 0; i < idx - 1; i++) {
2179                 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2180                                             get_adc_nid(codec, 0, i));
2181                 if (path && path->ctls[type] == ctl)
2182                         return 0;
2183         }
2184         return ctl;
2185 }
2186
2187 /* create individual capture volume and switch controls per input */
2188 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2189 {
2190         struct hda_gen_spec *spec = codec->spec;
2191         struct hda_input_mux *imux = &spec->input_mux;
2192         int i, err, type, type_idx = 0;
2193         const char *prev_label = NULL;
2194
2195         for (i = 0; i < imux->num_items; i++) {
2196                 const char *label;
2197                 bool inv_dmic;
2198                 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2199                 if (prev_label && !strcmp(label, prev_label))
2200                         type_idx++;
2201                 else
2202                         type_idx = 0;
2203                 prev_label = label;
2204                 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2205
2206                 for (type = 0; type < 2; type++) {
2207                         err = add_single_cap_ctl(codec, label, type_idx, type,
2208                                                  get_first_cap_ctl(codec, i, type),
2209                                                  inv_dmic);
2210                         if (err < 0)
2211                                 return err;
2212                 }
2213         }
2214         return 0;
2215 }
2216
2217 static int create_capture_mixers(struct hda_codec *codec)
2218 {
2219         struct hda_gen_spec *spec = codec->spec;
2220         struct hda_input_mux *imux = &spec->input_mux;
2221         int i, n, nums, err;
2222
2223         if (spec->dyn_adc_switch)
2224                 nums = 1;
2225         else
2226                 nums = spec->num_adc_nids;
2227
2228         if (!spec->auto_mic && imux->num_items > 1) {
2229                 struct snd_kcontrol_new *knew;
2230                 const char *name;
2231                 name = nums > 1 ? "Input Source" : "Capture Source";
2232                 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
2233                 if (!knew)
2234                         return -ENOMEM;
2235                 knew->count = nums;
2236         }
2237
2238         for (n = 0; n < nums; n++) {
2239                 bool multi = false;
2240                 bool inv_dmic = false;
2241                 int vol, sw;
2242
2243                 vol = sw = 0;
2244                 for (i = 0; i < imux->num_items; i++) {
2245                         struct nid_path *path;
2246                         path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2247                                                     get_adc_nid(codec, n, i));
2248                         if (!path)
2249                                 continue;
2250                         parse_capvol_in_path(codec, path);
2251                         if (!vol)
2252                                 vol = path->ctls[NID_PATH_VOL_CTL];
2253                         else if (vol != path->ctls[NID_PATH_VOL_CTL])
2254                                 multi = true;
2255                         if (!sw)
2256                                 sw = path->ctls[NID_PATH_MUTE_CTL];
2257                         else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2258                                 multi = true;
2259                         if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2260                                 inv_dmic = true;
2261                 }
2262
2263                 if (!multi)
2264                         err = create_single_cap_vol_ctl(codec, n, vol, sw,
2265                                                         inv_dmic);
2266                 else if (!spec->multi_cap_vol)
2267                         err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2268                 else
2269                         err = create_multi_cap_vol_ctl(codec);
2270                 if (err < 0)
2271                         return err;
2272         }
2273
2274         return 0;
2275 }
2276
2277 /*
2278  * add mic boosts if needed
2279  */
2280 static int parse_mic_boost(struct hda_codec *codec)
2281 {
2282         struct hda_gen_spec *spec = codec->spec;
2283         struct auto_pin_cfg *cfg = &spec->autocfg;
2284         int i, err;
2285         int type_idx = 0;
2286         hda_nid_t nid;
2287         const char *prev_label = NULL;
2288
2289         for (i = 0; i < cfg->num_inputs; i++) {
2290                 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2291                         break;
2292                 nid = cfg->inputs[i].pin;
2293                 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2294                         const char *label;
2295                         char boost_label[32];
2296                         struct nid_path *path;
2297                         unsigned int val;
2298
2299                         label = hda_get_autocfg_input_label(codec, cfg, i);
2300                         if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2301                                 label = "Headphone Mic";
2302                         if (prev_label && !strcmp(label, prev_label))
2303                                 type_idx++;
2304                         else
2305                                 type_idx = 0;
2306                         prev_label = label;
2307
2308                         snprintf(boost_label, sizeof(boost_label),
2309                                  "%s Boost Volume", label);
2310                         val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2311                         err = add_control(spec, HDA_CTL_WIDGET_VOL,
2312                                           boost_label, type_idx, val);
2313                         if (err < 0)
2314                                 return err;
2315
2316                         path = snd_hda_get_nid_path(codec, nid, 0);
2317                         if (path)
2318                                 path->ctls[NID_PATH_BOOST_CTL] = val;
2319                 }
2320         }
2321         return 0;
2322 }
2323
2324 /*
2325  * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2326  */
2327 static void parse_digital(struct hda_codec *codec)
2328 {
2329         struct hda_gen_spec *spec = codec->spec;
2330         struct nid_path *path;
2331         int i, nums;
2332         hda_nid_t dig_nid;
2333
2334         /* support multiple SPDIFs; the secondary is set up as a slave */
2335         nums = 0;
2336         for (i = 0; i < spec->autocfg.dig_outs; i++) {
2337                 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2338                 dig_nid = look_for_dac(codec, pin, true);
2339                 if (!dig_nid)
2340                         continue;
2341                 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
2342                 if (!path)
2343                         continue;
2344                 print_nid_path("digout", path);
2345                 if (!nums) {
2346                         spec->multiout.dig_out_nid = dig_nid;
2347                         spec->dig_out_type = spec->autocfg.dig_out_type[0];
2348                 } else {
2349                         spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2350                         if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2351                         break;
2352                         spec->slave_dig_outs[nums - 1] = dig_nid;
2353                 }
2354                 nums++;
2355         }
2356
2357         if (spec->autocfg.dig_in_pin) {
2358                 dig_nid = codec->start_nid;
2359                 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2360                         unsigned int wcaps = get_wcaps(codec, dig_nid);
2361                         if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2362                                 continue;
2363                         if (!(wcaps & AC_WCAP_DIGITAL))
2364                                 continue;
2365                         path = snd_hda_add_new_path(codec,
2366                                                     spec->autocfg.dig_in_pin,
2367                                                     dig_nid, HDA_PARSE_ALL);
2368                         if (path) {
2369                                 print_nid_path("digin", path);
2370                                 path->active = true;
2371                                 spec->dig_in_nid = dig_nid;
2372                                 break;
2373                         }
2374                 }
2375         }
2376 }
2377
2378
2379 /*
2380  * input MUX handling
2381  */
2382
2383 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2384
2385 /* select the given imux item; either unmute exclusively or select the route */
2386 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2387                       unsigned int idx)
2388 {
2389         struct hda_gen_spec *spec = codec->spec;
2390         const struct hda_input_mux *imux;
2391         struct nid_path *path;
2392
2393         imux = &spec->input_mux;
2394         if (!imux->num_items)
2395                 return 0;
2396
2397         if (idx >= imux->num_items)
2398                 idx = imux->num_items - 1;
2399         if (spec->cur_mux[adc_idx] == idx)
2400                 return 0;
2401
2402         path = snd_hda_get_nid_path(codec,
2403                                     spec->imux_pins[spec->cur_mux[adc_idx]],
2404                                     spec->adc_nids[adc_idx]);
2405         if (!path)
2406                 return 0;
2407         if (path->active)
2408                 snd_hda_activate_path(codec, path, false, false);
2409
2410         spec->cur_mux[adc_idx] = idx;
2411
2412         if (spec->shared_mic_hp)
2413                 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2414
2415         if (spec->dyn_adc_switch)
2416                 dyn_adc_pcm_resetup(codec, idx);
2417
2418         path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2419                                     get_adc_nid(codec, adc_idx, idx));
2420         if (!path)
2421                 return 0;
2422         if (path->active)
2423                 return 0;
2424         snd_hda_activate_path(codec, path, true, false);
2425         if (spec->cap_sync_hook)
2426                 spec->cap_sync_hook(codec);
2427         return 1;
2428 }
2429
2430
2431 /*
2432  * Jack detections for HP auto-mute and mic-switch
2433  */
2434
2435 /* check each pin in the given array; returns true if any of them is plugged */
2436 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2437 {
2438         int i, present = 0;
2439
2440         for (i = 0; i < num_pins; i++) {
2441                 hda_nid_t nid = pins[i];
2442                 if (!nid)
2443                         break;
2444                 present |= snd_hda_jack_detect(codec, nid);
2445         }
2446         return present;
2447 }
2448
2449 /* standard HP/line-out auto-mute helper */
2450 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2451                         bool mute, bool hp_out)
2452 {
2453         struct hda_gen_spec *spec = codec->spec;
2454         unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2455         int i;
2456
2457         for (i = 0; i < num_pins; i++) {
2458                 hda_nid_t nid = pins[i];
2459                 unsigned int val;
2460                 if (!nid)
2461                         break;
2462                 /* don't reset VREF value in case it's controlling
2463                  * the amp (see alc861_fixup_asus_amp_vref_0f())
2464                  */
2465                 if (spec->keep_vref_in_automute) {
2466                         val = snd_hda_codec_read(codec, nid, 0,
2467                                         AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2468                         val &= ~PIN_HP;
2469                 } else
2470                         val = 0;
2471                 val |= pin_bits;
2472                 snd_hda_set_pin_ctl_cache(codec, nid, val);
2473                 set_pin_eapd(codec, nid, !mute);
2474         }
2475 }
2476
2477 /* Toggle outputs muting */
2478 void snd_hda_gen_update_outputs(struct hda_codec *codec)
2479 {
2480         struct hda_gen_spec *spec = codec->spec;
2481         int on;
2482
2483         /* Control HP pins/amps depending on master_mute state;
2484          * in general, HP pins/amps control should be enabled in all cases,
2485          * but currently set only for master_mute, just to be safe
2486          */
2487         if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2488                 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2489                     spec->autocfg.hp_pins, spec->master_mute, true);
2490
2491         if (!spec->automute_speaker)
2492                 on = 0;
2493         else
2494                 on = spec->hp_jack_present | spec->line_jack_present;
2495         on |= spec->master_mute;
2496         do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2497                     spec->autocfg.speaker_pins, on, false);
2498
2499         /* toggle line-out mutes if needed, too */
2500         /* if LO is a copy of either HP or Speaker, don't need to handle it */
2501         if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2502             spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2503                 return;
2504         if (!spec->automute_lo)
2505                 on = 0;
2506         else
2507                 on = spec->hp_jack_present;
2508         on |= spec->master_mute;
2509         do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2510                     spec->autocfg.line_out_pins, on, false);
2511 }
2512 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
2513
2514 static void call_update_outputs(struct hda_codec *codec)
2515 {
2516         struct hda_gen_spec *spec = codec->spec;
2517         if (spec->automute_hook)
2518                 spec->automute_hook(codec);
2519         else
2520                 snd_hda_gen_update_outputs(codec);
2521 }
2522
2523 /* standard HP-automute helper */
2524 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2525 {
2526         struct hda_gen_spec *spec = codec->spec;
2527
2528         spec->hp_jack_present =
2529                 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2530                              spec->autocfg.hp_pins);
2531         if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2532                 return;
2533         call_update_outputs(codec);
2534 }
2535 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
2536
2537 /* standard line-out-automute helper */
2538 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2539 {
2540         struct hda_gen_spec *spec = codec->spec;
2541
2542         if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2543                 return;
2544         /* check LO jack only when it's different from HP */
2545         if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2546                 return;
2547
2548         spec->line_jack_present =
2549                 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2550                              spec->autocfg.line_out_pins);
2551         if (!spec->automute_speaker || !spec->detect_lo)
2552                 return;
2553         call_update_outputs(codec);
2554 }
2555 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
2556
2557 /* standard mic auto-switch helper */
2558 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2559 {
2560         struct hda_gen_spec *spec = codec->spec;
2561         int i;
2562
2563         if (!spec->auto_mic)
2564                 return;
2565
2566         for (i = spec->am_num_entries - 1; i > 0; i--) {
2567                 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2568                         mux_select(codec, 0, spec->am_entry[i].idx);
2569                         return;
2570                 }
2571         }
2572         mux_select(codec, 0, spec->am_entry[0].idx);
2573 }
2574 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
2575
2576 /*
2577  * Auto-Mute mode mixer enum support
2578  */
2579 static int automute_mode_info(struct snd_kcontrol *kcontrol,
2580                               struct snd_ctl_elem_info *uinfo)
2581 {
2582         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2583         struct hda_gen_spec *spec = codec->spec;
2584         static const char * const texts3[] = {
2585                 "Disabled", "Speaker Only", "Line Out+Speaker"
2586         };
2587
2588         if (spec->automute_speaker_possible && spec->automute_lo_possible)
2589                 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2590         return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2591 }
2592
2593 static int automute_mode_get(struct snd_kcontrol *kcontrol,
2594                              struct snd_ctl_elem_value *ucontrol)
2595 {
2596         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2597         struct hda_gen_spec *spec = codec->spec;
2598         unsigned int val = 0;
2599         if (spec->automute_speaker)
2600                 val++;
2601         if (spec->automute_lo)
2602                 val++;
2603
2604         ucontrol->value.enumerated.item[0] = val;
2605         return 0;
2606 }
2607
2608 static int automute_mode_put(struct snd_kcontrol *kcontrol,
2609                              struct snd_ctl_elem_value *ucontrol)
2610 {
2611         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2612         struct hda_gen_spec *spec = codec->spec;
2613
2614         switch (ucontrol->value.enumerated.item[0]) {
2615         case 0:
2616                 if (!spec->automute_speaker && !spec->automute_lo)
2617                         return 0;
2618                 spec->automute_speaker = 0;
2619                 spec->automute_lo = 0;
2620                 break;
2621         case 1:
2622                 if (spec->automute_speaker_possible) {
2623                         if (!spec->automute_lo && spec->automute_speaker)
2624                                 return 0;
2625                         spec->automute_speaker = 1;
2626                         spec->automute_lo = 0;
2627                 } else if (spec->automute_lo_possible) {
2628                         if (spec->automute_lo)
2629                                 return 0;
2630                         spec->automute_lo = 1;
2631                 } else
2632                         return -EINVAL;
2633                 break;
2634         case 2:
2635                 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2636                         return -EINVAL;
2637                 if (spec->automute_speaker && spec->automute_lo)
2638                         return 0;
2639                 spec->automute_speaker = 1;
2640                 spec->automute_lo = 1;
2641                 break;
2642         default:
2643                 return -EINVAL;
2644         }
2645         call_update_outputs(codec);
2646         return 1;
2647 }
2648
2649 static const struct snd_kcontrol_new automute_mode_enum = {
2650         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2651         .name = "Auto-Mute Mode",
2652         .info = automute_mode_info,
2653         .get = automute_mode_get,
2654         .put = automute_mode_put,
2655 };
2656
2657 static int add_automute_mode_enum(struct hda_codec *codec)
2658 {
2659         struct hda_gen_spec *spec = codec->spec;
2660
2661         if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
2662                 return -ENOMEM;
2663         return 0;
2664 }
2665
2666 /*
2667  * Check the availability of HP/line-out auto-mute;
2668  * Set up appropriately if really supported
2669  */
2670 static int check_auto_mute_availability(struct hda_codec *codec)
2671 {
2672         struct hda_gen_spec *spec = codec->spec;
2673         struct auto_pin_cfg *cfg = &spec->autocfg;
2674         int present = 0;
2675         int i, err;
2676
2677         if (cfg->hp_pins[0])
2678                 present++;
2679         if (cfg->line_out_pins[0])
2680                 present++;
2681         if (cfg->speaker_pins[0])
2682                 present++;
2683         if (present < 2) /* need two different output types */
2684                 return 0;
2685
2686         if (!cfg->speaker_pins[0] &&
2687             cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2688                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2689                        sizeof(cfg->speaker_pins));
2690                 cfg->speaker_outs = cfg->line_outs;
2691         }
2692
2693         if (!cfg->hp_pins[0] &&
2694             cfg->line_out_type == AUTO_PIN_HP_OUT) {
2695                 memcpy(cfg->hp_pins, cfg->line_out_pins,
2696                        sizeof(cfg->hp_pins));
2697                 cfg->hp_outs = cfg->line_outs;
2698         }
2699
2700         for (i = 0; i < cfg->hp_outs; i++) {
2701                 hda_nid_t nid = cfg->hp_pins[i];
2702                 if (!is_jack_detectable(codec, nid))
2703                         continue;
2704                 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2705                             nid);
2706                 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
2707                                                     snd_hda_gen_hp_automute);
2708                 spec->detect_hp = 1;
2709         }
2710
2711         if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2712                 if (cfg->speaker_outs)
2713                         for (i = 0; i < cfg->line_outs; i++) {
2714                                 hda_nid_t nid = cfg->line_out_pins[i];
2715                                 if (!is_jack_detectable(codec, nid))
2716                                         continue;
2717                                 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2718                                 snd_hda_jack_detect_enable_callback(codec, nid,
2719                                                                     HDA_GEN_FRONT_EVENT,
2720                                                                     snd_hda_gen_line_automute);
2721                                 spec->detect_lo = 1;
2722                         }
2723                 spec->automute_lo_possible = spec->detect_hp;
2724         }
2725
2726         spec->automute_speaker_possible = cfg->speaker_outs &&
2727                 (spec->detect_hp || spec->detect_lo);
2728
2729         spec->automute_lo = spec->automute_lo_possible;
2730         spec->automute_speaker = spec->automute_speaker_possible;
2731
2732         if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2733                 /* create a control for automute mode */
2734                 err = add_automute_mode_enum(codec);
2735                 if (err < 0)
2736                         return err;
2737         }
2738         return 0;
2739 }
2740
2741 /* return the position of NID in the list, or -1 if not found */
2742 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2743 {
2744         int i;
2745         for (i = 0; i < nums; i++)
2746                 if (list[i] == nid)
2747                         return i;
2748         return -1;
2749 }
2750
2751 /* check whether all auto-mic pins are valid; setup indices if OK */
2752 static bool auto_mic_check_imux(struct hda_codec *codec)
2753 {
2754         struct hda_gen_spec *spec = codec->spec;
2755         const struct hda_input_mux *imux;
2756         int i;
2757
2758         imux = &spec->input_mux;
2759         for (i = 0; i < spec->am_num_entries; i++) {
2760                 spec->am_entry[i].idx =
2761                         find_idx_in_nid_list(spec->am_entry[i].pin,
2762                                              spec->imux_pins, imux->num_items);
2763                 if (spec->am_entry[i].idx < 0)
2764                         return false; /* no corresponding imux */
2765         }
2766
2767         /* we don't need the jack detection for the first pin */
2768         for (i = 1; i < spec->am_num_entries; i++)
2769                 snd_hda_jack_detect_enable_callback(codec,
2770                                                     spec->am_entry[i].pin,
2771                                                     HDA_GEN_MIC_EVENT,
2772                                                     snd_hda_gen_mic_autoswitch);
2773         return true;
2774 }
2775
2776 static int compare_attr(const void *ap, const void *bp)
2777 {
2778         const struct automic_entry *a = ap;
2779         const struct automic_entry *b = bp;
2780         return (int)(a->attr - b->attr);
2781 }
2782
2783 /*
2784  * Check the availability of auto-mic switch;
2785  * Set up if really supported
2786  */
2787 static int check_auto_mic_availability(struct hda_codec *codec)
2788 {
2789         struct hda_gen_spec *spec = codec->spec;
2790         struct auto_pin_cfg *cfg = &spec->autocfg;
2791         unsigned int types;
2792         int i, num_pins;
2793
2794         types = 0;
2795         num_pins = 0;
2796         for (i = 0; i < cfg->num_inputs; i++) {
2797                 hda_nid_t nid = cfg->inputs[i].pin;
2798                 unsigned int attr;
2799                 attr = snd_hda_codec_get_pincfg(codec, nid);
2800                 attr = snd_hda_get_input_pin_attr(attr);
2801                 if (types & (1 << attr))
2802                         return 0; /* already occupied */
2803                 switch (attr) {
2804                 case INPUT_PIN_ATTR_INT:
2805                         if (cfg->inputs[i].type != AUTO_PIN_MIC)
2806                                 return 0; /* invalid type */
2807                         break;
2808                 case INPUT_PIN_ATTR_UNUSED:
2809                         return 0; /* invalid entry */
2810                 default:
2811                         if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2812                                 return 0; /* invalid type */
2813                         if (!spec->line_in_auto_switch &&
2814                             cfg->inputs[i].type != AUTO_PIN_MIC)
2815                                 return 0; /* only mic is allowed */
2816                         if (!is_jack_detectable(codec, nid))
2817                                 return 0; /* no unsol support */
2818                         break;
2819                 }
2820                 if (num_pins >= MAX_AUTO_MIC_PINS)
2821                         return 0;
2822                 types |= (1 << attr);
2823                 spec->am_entry[num_pins].pin = nid;
2824                 spec->am_entry[num_pins].attr = attr;
2825                 num_pins++;
2826         }
2827
2828         if (num_pins < 2)
2829                 return 0;
2830
2831         spec->am_num_entries = num_pins;
2832         /* sort the am_entry in the order of attr so that the pin with a
2833          * higher attr will be selected when the jack is plugged.
2834          */
2835         sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2836              compare_attr, NULL);
2837
2838         if (!auto_mic_check_imux(codec))
2839                 return 0;
2840
2841         spec->auto_mic = 1;
2842         spec->num_adc_nids = 1;
2843         spec->cur_mux[0] = spec->am_entry[0].idx;
2844         snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2845                     spec->am_entry[0].pin,
2846                     spec->am_entry[1].pin,
2847                     spec->am_entry[2].pin);
2848
2849         return 0;
2850 }
2851
2852
2853 /*
2854  * Parse the given BIOS configuration and set up the hda_gen_spec
2855  *
2856  * return 1 if successful, 0 if the proper config is not found,
2857  * or a negative error code
2858  */
2859 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
2860                                   struct auto_pin_cfg *cfg)
2861 {
2862         struct hda_gen_spec *spec = codec->spec;
2863         int err;
2864
2865         if (cfg != &spec->autocfg) {
2866                 spec->autocfg = *cfg;
2867                 cfg = &spec->autocfg;
2868         }
2869
2870         if (!cfg->line_outs) {
2871                 if (cfg->dig_outs || cfg->dig_in_pin) {
2872                         spec->multiout.max_channels = 2;
2873                         spec->no_analog = 1;
2874                         goto dig_only;
2875                 }
2876                 return 0; /* can't find valid BIOS pin config */
2877         }
2878
2879         if (!spec->no_primary_hp &&
2880             cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2881             cfg->line_outs <= cfg->hp_outs) {
2882                 /* use HP as primary out */
2883                 cfg->speaker_outs = cfg->line_outs;
2884                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2885                        sizeof(cfg->speaker_pins));
2886                 cfg->line_outs = cfg->hp_outs;
2887                 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2888                 cfg->hp_outs = 0;
2889                 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2890                 cfg->line_out_type = AUTO_PIN_HP_OUT;
2891         }
2892
2893         err = parse_output_paths(codec);
2894         if (err < 0)
2895                 return err;
2896         err = create_multi_channel_mode(codec);
2897         if (err < 0)
2898                 return err;
2899         err = create_multi_out_ctls(codec, cfg);
2900         if (err < 0)
2901                 return err;
2902         err = create_hp_out_ctls(codec);
2903         if (err < 0)
2904                 return err;
2905         err = create_speaker_out_ctls(codec);
2906         if (err < 0)
2907                 return err;
2908         err = create_shared_input(codec);
2909         if (err < 0)
2910                 return err;
2911         err = create_input_ctls(codec);
2912         if (err < 0)
2913                 return err;
2914
2915         /* check the multiple speaker pins */
2916         if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2917                 spec->const_channel_count = cfg->line_outs * 2;
2918         else
2919                 spec->const_channel_count = cfg->speaker_outs * 2;
2920
2921         if (spec->multi_ios > 0)
2922                 spec->multiout.max_channels = max(spec->ext_channel_count,
2923                                                   spec->const_channel_count);
2924         else
2925                 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2926
2927         err = check_auto_mute_availability(codec);
2928         if (err < 0)
2929                 return err;
2930
2931         err = check_dyn_adc_switch(codec);
2932         if (err < 0)
2933                 return err;
2934
2935         if (!spec->shared_mic_hp) {
2936                 err = check_auto_mic_availability(codec);
2937                 if (err < 0)
2938                         return err;
2939         }
2940
2941         err = create_capture_mixers(codec);
2942         if (err < 0)
2943                 return err;
2944
2945         err = parse_mic_boost(codec);
2946         if (err < 0)
2947                 return err;
2948
2949  dig_only:
2950         parse_digital(codec);
2951
2952         return 1;
2953 }
2954 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
2955
2956
2957 /*
2958  * Build control elements
2959  */
2960
2961 /* slave controls for virtual master */
2962 static const char * const slave_pfxs[] = {
2963         "Front", "Surround", "Center", "LFE", "Side",
2964         "Headphone", "Speaker", "Mono", "Line Out",
2965         "CLFE", "Bass Speaker", "PCM",
2966         NULL,
2967 };
2968
2969 int snd_hda_gen_build_controls(struct hda_codec *codec)
2970 {
2971         struct hda_gen_spec *spec = codec->spec;
2972         int err;
2973
2974         if (spec->kctls.used) {
2975                 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
2976                 if (err < 0)
2977                         return err;
2978         }
2979
2980         if (spec->multiout.dig_out_nid) {
2981                 err = snd_hda_create_dig_out_ctls(codec,
2982                                                   spec->multiout.dig_out_nid,
2983                                                   spec->multiout.dig_out_nid,
2984                                                   spec->pcm_rec[1].pcm_type);
2985                 if (err < 0)
2986                         return err;
2987                 if (!spec->no_analog) {
2988                         err = snd_hda_create_spdif_share_sw(codec,
2989                                                             &spec->multiout);
2990                         if (err < 0)
2991                                 return err;
2992                         spec->multiout.share_spdif = 1;
2993                 }
2994         }
2995         if (spec->dig_in_nid) {
2996                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
2997                 if (err < 0)
2998                         return err;
2999         }
3000
3001         /* if we have no master control, let's create it */
3002         if (!spec->no_analog &&
3003             !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3004                 unsigned int vmaster_tlv[4];
3005                 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3006                                         HDA_OUTPUT, vmaster_tlv);
3007                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3008                                           vmaster_tlv, slave_pfxs,
3009                                           "Playback Volume");
3010                 if (err < 0)
3011                         return err;
3012         }
3013         if (!spec->no_analog &&
3014             !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3015                 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3016                                             NULL, slave_pfxs,
3017                                             "Playback Switch",
3018                                             true, &spec->vmaster_mute.sw_kctl);
3019                 if (err < 0)
3020                         return err;
3021                 if (spec->vmaster_mute.hook)
3022                         snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3023                                                  spec->vmaster_mute_enum);
3024         }
3025
3026         free_kctls(spec); /* no longer needed */
3027
3028         if (spec->shared_mic_hp) {
3029                 int err;
3030                 int nid = spec->autocfg.inputs[1].pin;
3031                 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3032                 if (err < 0)
3033                         return err;
3034                 err = snd_hda_jack_detect_enable(codec, nid, 0);
3035                 if (err < 0)
3036                         return err;
3037         }
3038
3039         err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3040         if (err < 0)
3041                 return err;
3042
3043         return 0;
3044 }
3045 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3046
3047
3048 /*
3049  * PCM definitions
3050  */
3051
3052 /*
3053  * Analog playback callbacks
3054  */
3055 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3056                              struct hda_codec *codec,
3057                              struct snd_pcm_substream *substream)
3058 {
3059         struct hda_gen_spec *spec = codec->spec;
3060         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
3061                                              hinfo);
3062 }
3063
3064 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3065                                 struct hda_codec *codec,
3066                                 unsigned int stream_tag,
3067                                 unsigned int format,
3068                                 struct snd_pcm_substream *substream)
3069 {
3070         struct hda_gen_spec *spec = codec->spec;
3071         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3072                                                 stream_tag, format, substream);
3073 }
3074
3075 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3076                                 struct hda_codec *codec,
3077                                 struct snd_pcm_substream *substream)
3078 {
3079         struct hda_gen_spec *spec = codec->spec;
3080         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3081 }
3082
3083 /*
3084  * Digital out
3085  */
3086 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3087                                  struct hda_codec *codec,
3088                                  struct snd_pcm_substream *substream)
3089 {
3090         struct hda_gen_spec *spec = codec->spec;
3091         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3092 }
3093
3094 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3095                                     struct hda_codec *codec,
3096                                     unsigned int stream_tag,
3097                                     unsigned int format,
3098                                     struct snd_pcm_substream *substream)
3099 {
3100         struct hda_gen_spec *spec = codec->spec;
3101         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3102                                              stream_tag, format, substream);
3103 }
3104
3105 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3106                                     struct hda_codec *codec,
3107                                     struct snd_pcm_substream *substream)
3108 {
3109         struct hda_gen_spec *spec = codec->spec;
3110         return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3111 }
3112
3113 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3114                                   struct hda_codec *codec,
3115                                   struct snd_pcm_substream *substream)
3116 {
3117         struct hda_gen_spec *spec = codec->spec;
3118         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3119 }
3120
3121 /*
3122  * Analog capture
3123  */
3124 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3125                                    struct hda_codec *codec,
3126                                    unsigned int stream_tag,
3127                                    unsigned int format,
3128                                    struct snd_pcm_substream *substream)
3129 {
3130         struct hda_gen_spec *spec = codec->spec;
3131
3132         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3133                                    stream_tag, 0, format);
3134         return 0;
3135 }
3136
3137 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3138                                    struct hda_codec *codec,
3139                                    struct snd_pcm_substream *substream)
3140 {
3141         struct hda_gen_spec *spec = codec->spec;
3142
3143         snd_hda_codec_cleanup_stream(codec,
3144                                      spec->adc_nids[substream->number + 1]);
3145         return 0;
3146 }
3147
3148 /*
3149  */
3150 static const struct hda_pcm_stream pcm_analog_playback = {
3151         .substreams = 1,
3152         .channels_min = 2,
3153         .channels_max = 8,
3154         /* NID is set in build_pcms */
3155         .ops = {
3156                 .open = playback_pcm_open,
3157                 .prepare = playback_pcm_prepare,
3158                 .cleanup = playback_pcm_cleanup
3159         },
3160 };
3161
3162 static const struct hda_pcm_stream pcm_analog_capture = {
3163         .substreams = 1,
3164         .channels_min = 2,
3165         .channels_max = 2,
3166         /* NID is set in build_pcms */
3167 };
3168
3169 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3170         .substreams = 1,
3171         .channels_min = 2,
3172         .channels_max = 2,
3173         /* NID is set in build_pcms */
3174 };
3175
3176 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3177         .substreams = 2, /* can be overridden */
3178         .channels_min = 2,
3179         .channels_max = 2,
3180         /* NID is set in build_pcms */
3181         .ops = {
3182                 .prepare = alt_capture_pcm_prepare,
3183                 .cleanup = alt_capture_pcm_cleanup
3184         },
3185 };
3186
3187 static const struct hda_pcm_stream pcm_digital_playback = {
3188         .substreams = 1,
3189         .channels_min = 2,
3190         .channels_max = 2,
3191         /* NID is set in build_pcms */
3192         .ops = {
3193                 .open = dig_playback_pcm_open,
3194                 .close = dig_playback_pcm_close,
3195                 .prepare = dig_playback_pcm_prepare,
3196                 .cleanup = dig_playback_pcm_cleanup
3197         },
3198 };
3199
3200 static const struct hda_pcm_stream pcm_digital_capture = {
3201         .substreams = 1,
3202         .channels_min = 2,
3203         .channels_max = 2,
3204         /* NID is set in build_pcms */
3205 };
3206
3207 /* Used by build_pcms to flag that a PCM has no playback stream */
3208 static const struct hda_pcm_stream pcm_null_stream = {
3209         .substreams = 0,
3210         .channels_min = 0,
3211         .channels_max = 0,
3212 };
3213
3214 /*
3215  * dynamic changing ADC PCM streams
3216  */
3217 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3218 {
3219         struct hda_gen_spec *spec = codec->spec;
3220         hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3221
3222         if (spec->cur_adc && spec->cur_adc != new_adc) {
3223                 /* stream is running, let's swap the current ADC */
3224                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3225                 spec->cur_adc = new_adc;
3226                 snd_hda_codec_setup_stream(codec, new_adc,
3227                                            spec->cur_adc_stream_tag, 0,
3228                                            spec->cur_adc_format);
3229                 return true;
3230         }
3231         return false;
3232 }
3233
3234 /* analog capture with dynamic dual-adc changes */
3235 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3236                                        struct hda_codec *codec,
3237                                        unsigned int stream_tag,
3238                                        unsigned int format,
3239                                        struct snd_pcm_substream *substream)
3240 {
3241         struct hda_gen_spec *spec = codec->spec;
3242         spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3243         spec->cur_adc_stream_tag = stream_tag;
3244         spec->cur_adc_format = format;
3245         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3246         return 0;
3247 }
3248
3249 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3250                                        struct hda_codec *codec,
3251                                        struct snd_pcm_substream *substream)
3252 {
3253         struct hda_gen_spec *spec = codec->spec;
3254         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3255         spec->cur_adc = 0;
3256         return 0;
3257 }
3258
3259 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3260         .substreams = 1,
3261         .channels_min = 2,
3262         .channels_max = 2,
3263         .nid = 0, /* fill later */
3264         .ops = {
3265                 .prepare = dyn_adc_capture_pcm_prepare,
3266                 .cleanup = dyn_adc_capture_pcm_cleanup
3267         },
3268 };
3269
3270 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3271                                  const char *chip_name)
3272 {
3273         char *p;
3274
3275         if (*str)
3276                 return;
3277         strlcpy(str, chip_name, len);
3278
3279         /* drop non-alnum chars after a space */
3280         for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3281                 if (!isalnum(p[1])) {
3282                         *p = 0;
3283                         break;
3284                 }
3285         }
3286         strlcat(str, sfx, len);
3287 }
3288
3289 /* build PCM streams based on the parsed results */
3290 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3291 {
3292         struct hda_gen_spec *spec = codec->spec;
3293         struct hda_pcm *info = spec->pcm_rec;
3294         const struct hda_pcm_stream *p;
3295         bool have_multi_adcs;
3296
3297         codec->num_pcms = 1;
3298         codec->pcm_info = info;
3299
3300         if (spec->no_analog)
3301                 goto skip_analog;
3302
3303         fill_pcm_stream_name(spec->stream_name_analog,
3304                              sizeof(spec->stream_name_analog),
3305                              " Analog", codec->chip_name);
3306         info->name = spec->stream_name_analog;
3307
3308         if (spec->multiout.num_dacs > 0) {
3309                 p = spec->stream_analog_playback;
3310                 if (!p)
3311                         p = &pcm_analog_playback;
3312                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3313                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3314                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3315                         spec->multiout.max_channels;
3316                 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3317                     spec->autocfg.line_outs == 2)
3318                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3319                                 snd_pcm_2_1_chmaps;
3320         }
3321         if (spec->num_adc_nids) {
3322                 p = spec->stream_analog_capture;
3323                 if (!p) {
3324                         if (spec->dyn_adc_switch)
3325                                 p = &dyn_adc_pcm_analog_capture;
3326                         else
3327                                 p = &pcm_analog_capture;
3328                 }
3329                 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3330                 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3331         }
3332
3333  skip_analog:
3334         /* SPDIF for stream index #1 */
3335         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3336                 fill_pcm_stream_name(spec->stream_name_digital,
3337                                      sizeof(spec->stream_name_digital),
3338                                      " Digital", codec->chip_name);
3339                 codec->num_pcms = 2;
3340                 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3341                 info = spec->pcm_rec + 1;
3342                 info->name = spec->stream_name_digital;
3343                 if (spec->dig_out_type)
3344                         info->pcm_type = spec->dig_out_type;
3345                 else
3346                         info->pcm_type = HDA_PCM_TYPE_SPDIF;
3347                 if (spec->multiout.dig_out_nid) {
3348                         p = spec->stream_digital_playback;
3349                         if (!p)
3350                                 p = &pcm_digital_playback;
3351                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3352                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3353                 }
3354                 if (spec->dig_in_nid) {
3355                         p = spec->stream_digital_capture;
3356                         if (!p)
3357                                 p = &pcm_digital_capture;
3358                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3359                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3360                 }
3361         }
3362
3363         if (spec->no_analog)
3364                 return 0;
3365
3366         /* If the use of more than one ADC is requested for the current
3367          * model, configure a second analog capture-only PCM.
3368          */
3369         have_multi_adcs = (spec->num_adc_nids > 1) &&
3370                 !spec->dyn_adc_switch && !spec->auto_mic;
3371         /* Additional Analaog capture for index #2 */
3372         if (spec->alt_dac_nid || have_multi_adcs) {
3373                 codec->num_pcms = 3;
3374                 info = spec->pcm_rec + 2;
3375                 info->name = spec->stream_name_analog;
3376                 if (spec->alt_dac_nid) {
3377                         p = spec->stream_analog_alt_playback;
3378                         if (!p)
3379                                 p = &pcm_analog_alt_playback;
3380                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3381                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3382                                 spec->alt_dac_nid;
3383                 } else {
3384                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3385                                 pcm_null_stream;
3386                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3387                 }
3388                 if (have_multi_adcs) {
3389                         p = spec->stream_analog_alt_capture;
3390                         if (!p)
3391                                 p = &pcm_analog_alt_capture;
3392                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3393                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3394                                 spec->adc_nids[1];
3395                         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3396                                 spec->num_adc_nids - 1;
3397                 } else {
3398                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3399                                 pcm_null_stream;
3400                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3401                 }
3402         }
3403
3404         return 0;
3405 }
3406 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3407
3408
3409 /*
3410  * Standard auto-parser initializations
3411  */
3412
3413 /* configure the path from the given dac to the pin as the proper output */
3414 static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3415                                   int pin_type, hda_nid_t dac)
3416 {
3417         struct nid_path *path;
3418
3419         snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3420         path = snd_hda_get_nid_path(codec, dac, pin);
3421         if (!path)
3422                 return;
3423         if (path->active)
3424                 return;
3425         snd_hda_activate_path(codec, path, true, true);
3426         set_pin_eapd(codec, pin, true);
3427 }
3428
3429 /* initialize primary output paths */
3430 static void init_multi_out(struct hda_codec *codec)
3431 {
3432         struct hda_gen_spec *spec = codec->spec;
3433         hda_nid_t nid, dac;
3434         int pin_type;
3435         int i;
3436
3437         if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3438                 pin_type = PIN_HP;
3439         else
3440                 pin_type = PIN_OUT;
3441
3442         for (i = 0; i < spec->autocfg.line_outs; i++) {
3443                 nid = spec->autocfg.line_out_pins[i];
3444                 if (nid) {
3445                         dac = spec->multiout.dac_nids[i];
3446                         if (!dac)
3447                                 dac = spec->multiout.dac_nids[0];
3448                         set_output_and_unmute(codec, nid, pin_type, dac);
3449                 }
3450         }
3451 }
3452
3453
3454 static void __init_extra_out(struct hda_codec *codec, int num_outs,
3455                              hda_nid_t *pins, hda_nid_t *dacs, int type)
3456 {
3457         struct hda_gen_spec *spec = codec->spec;
3458         int i;
3459         hda_nid_t pin, dac;
3460
3461         for (i = 0; i < num_outs; i++) {
3462                 pin = pins[i];
3463                 if (!pin)
3464                         break;
3465                 dac = dacs[i];
3466                 if (!dac) {
3467                         if (i > 0 && dacs[0])
3468                                 dac = dacs[0];
3469                         else
3470                                 dac = spec->multiout.dac_nids[0];
3471                 }
3472                 set_output_and_unmute(codec, pin, type, dac);
3473         }
3474 }
3475
3476 /* initialize hp and speaker paths */
3477 static void init_extra_out(struct hda_codec *codec)
3478 {
3479         struct hda_gen_spec *spec = codec->spec;
3480
3481         if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3482                 __init_extra_out(codec, spec->autocfg.hp_outs,
3483                                  spec->autocfg.hp_pins,
3484                                  spec->multiout.hp_out_nid, PIN_HP);
3485         if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3486                 __init_extra_out(codec, spec->autocfg.speaker_outs,
3487                                  spec->autocfg.speaker_pins,
3488                                  spec->multiout.extra_out_nid, PIN_OUT);
3489 }
3490
3491 /* initialize multi-io paths */
3492 static void init_multi_io(struct hda_codec *codec)
3493 {
3494         struct hda_gen_spec *spec = codec->spec;
3495         int i;
3496
3497         for (i = 0; i < spec->multi_ios; i++) {
3498                 hda_nid_t pin = spec->multi_io[i].pin;
3499                 struct nid_path *path;
3500                 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3501                 if (!path)
3502                         continue;
3503                 if (!spec->multi_io[i].ctl_in)
3504                         spec->multi_io[i].ctl_in =
3505                                 snd_hda_codec_update_cache(codec, pin, 0,
3506                                            AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3507                 snd_hda_activate_path(codec, path, path->active, true);
3508         }
3509 }
3510
3511 /* set up the input pin config, depending on the given auto-pin type */
3512 static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3513                           int auto_pin_type)
3514 {
3515         unsigned int val = PIN_IN;
3516         if (auto_pin_type == AUTO_PIN_MIC)
3517                 val |= snd_hda_get_default_vref(codec, nid);
3518         snd_hda_set_pin_ctl_cache(codec, nid, val);
3519 }
3520
3521 /* set up input pins and loopback paths */
3522 static void init_analog_input(struct hda_codec *codec)
3523 {
3524         struct hda_gen_spec *spec = codec->spec;
3525         struct auto_pin_cfg *cfg = &spec->autocfg;
3526         int i;
3527
3528         for (i = 0; i < cfg->num_inputs; i++) {
3529                 hda_nid_t nid = cfg->inputs[i].pin;
3530                 if (is_input_pin(codec, nid))
3531                         set_input_pin(codec, nid, cfg->inputs[i].type);
3532
3533                 /* init loopback inputs */
3534                 if (spec->mixer_nid) {
3535                         struct nid_path *path;
3536                         path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3537                         if (path)
3538                                 snd_hda_activate_path(codec, path,
3539                                                       path->active, false);
3540                 }
3541         }
3542 }
3543
3544 /* initialize ADC paths */
3545 static void init_input_src(struct hda_codec *codec)
3546 {
3547         struct hda_gen_spec *spec = codec->spec;
3548         struct hda_input_mux *imux = &spec->input_mux;
3549         struct nid_path *path;
3550         int i, c, nums;
3551
3552         if (spec->dyn_adc_switch)
3553                 nums = 1;
3554         else
3555                 nums = spec->num_adc_nids;
3556
3557         for (c = 0; c < nums; c++) {
3558                 for (i = 0; i < imux->num_items; i++) {
3559                         path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3560                                                     get_adc_nid(codec, c, i));
3561                         if (path) {
3562                                 bool active = path->active;
3563                                 if (i == spec->cur_mux[c])
3564                                         active = true;
3565                                 snd_hda_activate_path(codec, path, active, false);
3566                         }
3567                 }
3568         }
3569
3570         if (spec->shared_mic_hp)
3571                 update_shared_mic_hp(codec, spec->cur_mux[0]);
3572
3573         if (spec->cap_sync_hook)
3574                 spec->cap_sync_hook(codec);
3575 }
3576
3577 /* set right pin controls for digital I/O */
3578 static void init_digital(struct hda_codec *codec)
3579 {
3580         struct hda_gen_spec *spec = codec->spec;
3581         int i;
3582         hda_nid_t pin;
3583
3584         for (i = 0; i < spec->autocfg.dig_outs; i++) {
3585                 pin = spec->autocfg.dig_out_pins[i];
3586                 if (!pin)
3587                         continue;
3588                 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3589         }
3590         pin = spec->autocfg.dig_in_pin;
3591         if (pin)
3592                 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
3593 }
3594
3595 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3596  * invalid unsol tags by some reason
3597  */
3598 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3599 {
3600         int i;
3601
3602         for (i = 0; i < codec->init_pins.used; i++) {
3603                 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3604                 hda_nid_t nid = pin->nid;
3605                 if (is_jack_detectable(codec, nid) &&
3606                     !snd_hda_jack_tbl_get(codec, nid))
3607                         snd_hda_codec_update_cache(codec, nid, 0,
3608                                         AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3609         }
3610 }
3611
3612 int snd_hda_gen_init(struct hda_codec *codec)
3613 {
3614         struct hda_gen_spec *spec = codec->spec;
3615
3616         if (spec->init_hook)
3617                 spec->init_hook(codec);
3618
3619         snd_hda_apply_verbs(codec);
3620
3621         codec->cached_write = 1;
3622
3623         init_multi_out(codec);
3624         init_extra_out(codec);
3625         init_multi_io(codec);
3626         init_analog_input(codec);
3627         init_input_src(codec);
3628         init_digital(codec);
3629
3630         clear_unsol_on_unused_pins(codec);
3631
3632         /* call init functions of standard auto-mute helpers */
3633         snd_hda_gen_hp_automute(codec, NULL);
3634         snd_hda_gen_line_automute(codec, NULL);
3635         snd_hda_gen_mic_autoswitch(codec, NULL);
3636
3637         snd_hda_codec_flush_amp_cache(codec);
3638         snd_hda_codec_flush_cmd_cache(codec);
3639
3640         if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3641                 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3642
3643         hda_call_check_power_status(codec, 0x01);
3644         return 0;
3645 }
3646 EXPORT_SYMBOL(snd_hda_gen_init);
3647
3648
3649 /*
3650  * the generic codec support
3651  */
3652
3653 #ifdef CONFIG_PM
3654 static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3655 {
3656         struct hda_gen_spec *spec = codec->spec;
3657         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3658 }
3659 #endif
3660
3661 static void generic_free(struct hda_codec *codec)
3662 {
3663         snd_hda_gen_spec_free(codec->spec);
3664         kfree(codec->spec);
3665         codec->spec = NULL;
3666 }
3667
3668 static const struct hda_codec_ops generic_patch_ops = {
3669         .build_controls = snd_hda_gen_build_controls,
3670         .build_pcms = snd_hda_gen_build_pcms,
3671         .init = snd_hda_gen_init,
3672         .free = generic_free,
3673         .unsol_event = snd_hda_jack_unsol_event,
3674 #ifdef CONFIG_PM
3675         .check_power_status = generic_check_power_status,
3676 #endif
3677 };
3678
3679 int snd_hda_parse_generic_codec(struct hda_codec *codec)
3680 {
3681         struct hda_gen_spec *spec;
3682         int err;
3683
3684         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3685         if (!spec)
3686                 return -ENOMEM;
3687         snd_hda_gen_spec_init(spec);
3688         codec->spec = spec;
3689
3690         err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3691         if (err < 0)
3692                 return err;
3693
3694         err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
3695         if (err < 0)
3696                 goto error;
3697
3698         codec->patch_ops = generic_patch_ops;
3699         return 0;
3700
3701 error:
3702         generic_free(codec);
3703         return err;
3704 }
3705 EXPORT_SYMBOL(snd_hda_parse_generic_codec);