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