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