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