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