~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/pci/hda/hda_generic.c

Version: ~ [ linux-4.14 ] ~ [ linux-4.13.12 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.61 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.97 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.46 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.80 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.50 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.95 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.27.62 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp