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