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