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