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