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