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