ALSA: hda - Move some codes up to hdac_bus struct
[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
5ccf835c
TI
657 if (nid == codec->afg)
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];
5ccf835c
TI
835 if (nid == codec->afg)
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;
1900 int i;
1901 hda_nid_t nid = codec->start_nid;
1902
1903 spec->num_all_dacs = 0;
1904 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1905 for (i = 0; i < codec->num_nodes; i++, nid++) {
1906 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1907 continue;
1908 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
4e76a883 1909 codec_err(codec, "Too many DACs!\n");
352f7f91
TI
1910 break;
1911 }
1912 spec->all_dacs[spec->num_all_dacs++] = nid;
1913 }
1914}
1915
1916static int parse_output_paths(struct hda_codec *codec)
1917{
1918 struct hda_gen_spec *spec = codec->spec;
1919 struct auto_pin_cfg *cfg = &spec->autocfg;
1920 struct auto_pin_cfg *best_cfg;
9314a581 1921 unsigned int val;
352f7f91
TI
1922 int best_badness = INT_MAX;
1923 int badness;
1924 bool fill_hardwired = true, fill_mio_first = true;
1925 bool best_wired = true, best_mio = true;
1926 bool hp_spk_swapped = false;
1927
352f7f91
TI
1928 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1929 if (!best_cfg)
1930 return -ENOMEM;
1931 *best_cfg = *cfg;
1932
1933 for (;;) {
1934 badness = fill_and_eval_dacs(codec, fill_hardwired,
1935 fill_mio_first);
1936 if (badness < 0) {
1937 kfree(best_cfg);
1938 return badness;
1939 }
1940 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1941 cfg->line_out_type, fill_hardwired, fill_mio_first,
1942 badness);
a769409c 1943 debug_show_configs(codec, cfg);
352f7f91
TI
1944 if (badness < best_badness) {
1945 best_badness = badness;
1946 *best_cfg = *cfg;
1947 best_wired = fill_hardwired;
1948 best_mio = fill_mio_first;
1949 }
1950 if (!badness)
1951 break;
1952 fill_mio_first = !fill_mio_first;
1953 if (!fill_mio_first)
1954 continue;
1955 fill_hardwired = !fill_hardwired;
1956 if (!fill_hardwired)
1957 continue;
1958 if (hp_spk_swapped)
1959 break;
1960 hp_spk_swapped = true;
1961 if (cfg->speaker_outs > 0 &&
1962 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1963 cfg->hp_outs = cfg->line_outs;
1964 memcpy(cfg->hp_pins, cfg->line_out_pins,
1965 sizeof(cfg->hp_pins));
1966 cfg->line_outs = cfg->speaker_outs;
1967 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1968 sizeof(cfg->speaker_pins));
1969 cfg->speaker_outs = 0;
1970 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1971 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1972 fill_hardwired = true;
1973 continue;
1974 }
1975 if (cfg->hp_outs > 0 &&
1976 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1977 cfg->speaker_outs = cfg->line_outs;
1978 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1979 sizeof(cfg->speaker_pins));
1980 cfg->line_outs = cfg->hp_outs;
1981 memcpy(cfg->line_out_pins, cfg->hp_pins,
1982 sizeof(cfg->hp_pins));
1983 cfg->hp_outs = 0;
1984 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1985 cfg->line_out_type = AUTO_PIN_HP_OUT;
1986 fill_hardwired = true;
1987 continue;
1988 }
1989 break;
1990 }
1991
1992 if (badness) {
0c8c0f56 1993 debug_badness("==> restoring best_cfg\n");
352f7f91
TI
1994 *cfg = *best_cfg;
1995 fill_and_eval_dacs(codec, best_wired, best_mio);
1996 }
1997 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1998 cfg->line_out_type, best_wired, best_mio);
a769409c 1999 debug_show_configs(codec, cfg);
352f7f91
TI
2000
2001 if (cfg->line_out_pins[0]) {
2002 struct nid_path *path;
196c1766 2003 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
352f7f91
TI
2004 if (path)
2005 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
d89c6c0c 2006 if (spec->vmaster_nid) {
7a71bbf3
TI
2007 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2008 HDA_OUTPUT, spec->vmaster_tlv);
d89c6c0c
TI
2009 if (spec->dac_min_mute)
2010 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2011 }
352f7f91
TI
2012 }
2013
9314a581
TI
2014 /* set initial pinctl targets */
2015 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2016 val = PIN_HP;
2017 else
2018 val = PIN_OUT;
2019 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2020 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2021 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2022 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2023 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2024 set_pin_targets(codec, cfg->speaker_outs,
2025 cfg->speaker_pins, val);
2026 }
2027
55a63d4d
TI
2028 /* clear indep_hp flag if not available */
2029 if (spec->indep_hp && !indep_hp_possible(codec))
2030 spec->indep_hp = 0;
2031
352f7f91
TI
2032 kfree(best_cfg);
2033 return 0;
2034}
2035
2036/* add playback controls from the parsed DAC table */
2037static int create_multi_out_ctls(struct hda_codec *codec,
2038 const struct auto_pin_cfg *cfg)
2039{
2040 struct hda_gen_spec *spec = codec->spec;
2041 int i, err, noutputs;
2042
2043 noutputs = cfg->line_outs;
2044 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2045 noutputs += spec->multi_ios;
2046
2047 for (i = 0; i < noutputs; i++) {
2048 const char *name;
2049 int index;
352f7f91
TI
2050 struct nid_path *path;
2051
196c1766 2052 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
352f7f91
TI
2053 if (!path)
2054 continue;
247d85ee
TI
2055
2056 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
352f7f91
TI
2057 if (!name || !strcmp(name, "CLFE")) {
2058 /* Center/LFE */
2059 err = add_vol_ctl(codec, "Center", 0, 1, path);
2060 if (err < 0)
2061 return err;
2062 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2063 if (err < 0)
2064 return err;
247d85ee
TI
2065 } else {
2066 err = add_stereo_vol(codec, name, index, path);
2067 if (err < 0)
2068 return err;
2069 }
2070
2071 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2072 if (!name || !strcmp(name, "CLFE")) {
352f7f91
TI
2073 err = add_sw_ctl(codec, "Center", 0, 1, path);
2074 if (err < 0)
2075 return err;
2076 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2077 if (err < 0)
2078 return err;
2079 } else {
352f7f91
TI
2080 err = add_stereo_sw(codec, name, index, path);
2081 if (err < 0)
2082 return err;
2083 }
2084 }
2085 return 0;
2086}
2087
c2c80383 2088static int create_extra_out(struct hda_codec *codec, int path_idx,
196c1766 2089 const char *pfx, int cidx)
352f7f91
TI
2090{
2091 struct nid_path *path;
2092 int err;
2093
196c1766 2094 path = snd_hda_get_path_from_idx(codec, path_idx);
352f7f91
TI
2095 if (!path)
2096 return 0;
c2c80383
TI
2097 err = add_stereo_vol(codec, pfx, cidx, path);
2098 if (err < 0)
2099 return err;
352f7f91
TI
2100 err = add_stereo_sw(codec, pfx, cidx, path);
2101 if (err < 0)
2102 return err;
2103 return 0;
2104}
2105
2106/* add playback controls for speaker and HP outputs */
2107static int create_extra_outs(struct hda_codec *codec, int num_pins,
196c1766 2108 const int *paths, const char *pfx)
352f7f91 2109{
c2c80383 2110 int i;
352f7f91
TI
2111
2112 for (i = 0; i < num_pins; i++) {
c2c80383 2113 const char *name;
975cc02a 2114 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
c2c80383
TI
2115 int err, idx = 0;
2116
2117 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2118 name = "Bass Speaker";
2119 else if (num_pins >= 3) {
2120 snprintf(tmp, sizeof(tmp), "%s %s",
352f7f91 2121 pfx, channel_name[i]);
c2c80383 2122 name = tmp;
352f7f91 2123 } else {
c2c80383
TI
2124 name = pfx;
2125 idx = i;
352f7f91 2126 }
c2c80383 2127 err = create_extra_out(codec, paths[i], name, idx);
352f7f91
TI
2128 if (err < 0)
2129 return err;
2130 }
2131 return 0;
2132}
2133
2134static int create_hp_out_ctls(struct hda_codec *codec)
2135{
2136 struct hda_gen_spec *spec = codec->spec;
2137 return create_extra_outs(codec, spec->autocfg.hp_outs,
196c1766 2138 spec->hp_paths,
352f7f91
TI
2139 "Headphone");
2140}
2141
2142static int create_speaker_out_ctls(struct hda_codec *codec)
2143{
2144 struct hda_gen_spec *spec = codec->spec;
2145 return create_extra_outs(codec, spec->autocfg.speaker_outs,
196c1766 2146 spec->speaker_paths,
352f7f91
TI
2147 "Speaker");
2148}
2149
38cf6f1a
TI
2150/*
2151 * independent HP controls
2152 */
2153
1a4f69d5
TI
2154static void call_hp_automute(struct hda_codec *codec,
2155 struct hda_jack_callback *jack);
38cf6f1a
TI
2156static int indep_hp_info(struct snd_kcontrol *kcontrol,
2157 struct snd_ctl_elem_info *uinfo)
2158{
2159 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2160}
2161
2162static int indep_hp_get(struct snd_kcontrol *kcontrol,
2163 struct snd_ctl_elem_value *ucontrol)
2164{
2165 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2166 struct hda_gen_spec *spec = codec->spec;
2167 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2168 return 0;
2169}
2170
a1e908ed
TI
2171static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2172 int nomix_path_idx, int mix_path_idx,
2173 int out_type);
2174
38cf6f1a
TI
2175static int indep_hp_put(struct snd_kcontrol *kcontrol,
2176 struct snd_ctl_elem_value *ucontrol)
2177{
2178 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2179 struct hda_gen_spec *spec = codec->spec;
2180 unsigned int select = ucontrol->value.enumerated.item[0];
2181 int ret = 0;
2182
2183 mutex_lock(&spec->pcm_mutex);
2184 if (spec->active_streams) {
2185 ret = -EBUSY;
2186 goto unlock;
2187 }
2188
2189 if (spec->indep_hp_enabled != select) {
a1e908ed
TI
2190 hda_nid_t *dacp;
2191 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2192 dacp = &spec->private_dac_nids[0];
2193 else
2194 dacp = &spec->multiout.hp_out_nid[0];
2195
2196 /* update HP aamix paths in case it conflicts with indep HP */
2197 if (spec->have_aamix_ctl) {
2198 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2199 update_aamix_paths(codec, spec->aamix_mode,
2200 spec->out_paths[0],
2201 spec->aamix_out_paths[0],
2202 spec->autocfg.line_out_type);
2203 else
2204 update_aamix_paths(codec, spec->aamix_mode,
2205 spec->hp_paths[0],
2206 spec->aamix_out_paths[1],
2207 AUTO_PIN_HP_OUT);
2208 }
2209
38cf6f1a
TI
2210 spec->indep_hp_enabled = select;
2211 if (spec->indep_hp_enabled)
a1e908ed 2212 *dacp = 0;
38cf6f1a 2213 else
a1e908ed 2214 *dacp = spec->alt_dac_nid;
92603c59 2215
963afde9 2216 call_hp_automute(codec, NULL);
38cf6f1a
TI
2217 ret = 1;
2218 }
2219 unlock:
2220 mutex_unlock(&spec->pcm_mutex);
2221 return ret;
2222}
2223
2224static const struct snd_kcontrol_new indep_hp_ctl = {
2225 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2226 .name = "Independent HP",
2227 .info = indep_hp_info,
2228 .get = indep_hp_get,
2229 .put = indep_hp_put,
2230};
2231
2232
2233static int create_indep_hp_ctls(struct hda_codec *codec)
2234{
2235 struct hda_gen_spec *spec = codec->spec;
a1e908ed 2236 hda_nid_t dac;
38cf6f1a
TI
2237
2238 if (!spec->indep_hp)
2239 return 0;
a1e908ed
TI
2240 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2241 dac = spec->multiout.dac_nids[0];
2242 else
2243 dac = spec->multiout.hp_out_nid[0];
2244 if (!dac) {
38cf6f1a
TI
2245 spec->indep_hp = 0;
2246 return 0;
2247 }
2248
2249 spec->indep_hp_enabled = false;
a1e908ed 2250 spec->alt_dac_nid = dac;
38cf6f1a
TI
2251 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2252 return -ENOMEM;
2253 return 0;
2254}
2255
352f7f91
TI
2256/*
2257 * channel mode enum control
2258 */
2259
2260static int ch_mode_info(struct snd_kcontrol *kcontrol,
2261 struct snd_ctl_elem_info *uinfo)
2262{
2263 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2264 struct hda_gen_spec *spec = codec->spec;
a07a949b 2265 int chs;
352f7f91
TI
2266
2267 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2268 uinfo->count = 1;
2269 uinfo->value.enumerated.items = spec->multi_ios + 1;
2270 if (uinfo->value.enumerated.item > spec->multi_ios)
2271 uinfo->value.enumerated.item = spec->multi_ios;
a07a949b
TI
2272 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2273 sprintf(uinfo->value.enumerated.name, "%dch", chs);
352f7f91
TI
2274 return 0;
2275}
2276
2277static int ch_mode_get(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_value *ucontrol)
2279{
2280 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2281 struct hda_gen_spec *spec = codec->spec;
a07a949b
TI
2282 ucontrol->value.enumerated.item[0] =
2283 (spec->ext_channel_count - spec->min_channel_count) / 2;
352f7f91
TI
2284 return 0;
2285}
2286
196c1766
TI
2287static inline struct nid_path *
2288get_multiio_path(struct hda_codec *codec, int idx)
2289{
2290 struct hda_gen_spec *spec = codec->spec;
2291 return snd_hda_get_path_from_idx(codec,
2292 spec->out_paths[spec->autocfg.line_outs + idx]);
2293}
2294
a5cc2509
TI
2295static void update_automute_all(struct hda_codec *codec);
2296
65033cc8
TI
2297/* Default value to be passed as aamix argument for snd_hda_activate_path();
2298 * used for output paths
2299 */
2300static bool aamix_default(struct hda_gen_spec *spec)
2301{
2302 return !spec->have_aamix_ctl || spec->aamix_mode;
2303}
2304
352f7f91
TI
2305static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2306{
2307 struct hda_gen_spec *spec = codec->spec;
2308 hda_nid_t nid = spec->multi_io[idx].pin;
2309 struct nid_path *path;
2310
196c1766 2311 path = get_multiio_path(codec, idx);
352f7f91
TI
2312 if (!path)
2313 return -EINVAL;
2314
2315 if (path->active == output)
2316 return 0;
2317
2318 if (output) {
2c12c30d 2319 set_pin_target(codec, nid, PIN_OUT, true);
65033cc8 2320 snd_hda_activate_path(codec, path, true, aamix_default(spec));
d5a9f1bb 2321 set_pin_eapd(codec, nid, true);
352f7f91 2322 } else {
d5a9f1bb 2323 set_pin_eapd(codec, nid, false);
65033cc8 2324 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2c12c30d 2325 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
55196fff 2326 path_power_down_sync(codec, path);
352f7f91 2327 }
a365fed9
TI
2328
2329 /* update jack retasking in case it modifies any of them */
a5cc2509 2330 update_automute_all(codec);
a365fed9 2331
352f7f91
TI
2332 return 0;
2333}
2334
2335static int ch_mode_put(struct snd_kcontrol *kcontrol,
2336 struct snd_ctl_elem_value *ucontrol)
2337{
2338 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2339 struct hda_gen_spec *spec = codec->spec;
2340 int i, ch;
2341
2342 ch = ucontrol->value.enumerated.item[0];
2343 if (ch < 0 || ch > spec->multi_ios)
2344 return -EINVAL;
a07a949b 2345 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
352f7f91 2346 return 0;
a07a949b 2347 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
352f7f91
TI
2348 for (i = 0; i < spec->multi_ios; i++)
2349 set_multi_io(codec, i, i < ch);
2350 spec->multiout.max_channels = max(spec->ext_channel_count,
2351 spec->const_channel_count);
2352 if (spec->need_dac_fix)
2353 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2354 return 1;
2355}
2356
2357static const struct snd_kcontrol_new channel_mode_enum = {
2358 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2359 .name = "Channel Mode",
2360 .info = ch_mode_info,
2361 .get = ch_mode_get,
2362 .put = ch_mode_put,
2363};
2364
2365static int create_multi_channel_mode(struct hda_codec *codec)
2366{
2367 struct hda_gen_spec *spec = codec->spec;
2368
2369 if (spec->multi_ios > 0) {
12c93df6 2370 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
352f7f91
TI
2371 return -ENOMEM;
2372 }
2373 return 0;
2374}
2375
c30aa7b2
TI
2376/*
2377 * aamix loopback enable/disable switch
2378 */
2379
2380#define loopback_mixing_info indep_hp_info
2381
2382static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2383 struct snd_ctl_elem_value *ucontrol)
2384{
2385 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2386 struct hda_gen_spec *spec = codec->spec;
2387 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2388 return 0;
2389}
2390
2391static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
a1e908ed
TI
2392 int nomix_path_idx, int mix_path_idx,
2393 int out_type)
c30aa7b2 2394{
a1e908ed 2395 struct hda_gen_spec *spec = codec->spec;
c30aa7b2
TI
2396 struct nid_path *nomix_path, *mix_path;
2397
2398 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2399 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2400 if (!nomix_path || !mix_path)
2401 return;
a1e908ed
TI
2402
2403 /* if HP aamix path is driven from a different DAC and the
2404 * independent HP mode is ON, can't turn on aamix path
2405 */
2406 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2407 mix_path->path[0] != spec->alt_dac_nid)
2408 do_mix = false;
2409
c30aa7b2
TI
2410 if (do_mix) {
2411 snd_hda_activate_path(codec, nomix_path, false, true);
2412 snd_hda_activate_path(codec, mix_path, true, true);
55196fff 2413 path_power_down_sync(codec, nomix_path);
c30aa7b2 2414 } else {
65033cc8
TI
2415 snd_hda_activate_path(codec, mix_path, false, false);
2416 snd_hda_activate_path(codec, nomix_path, true, false);
55196fff 2417 path_power_down_sync(codec, mix_path);
c30aa7b2
TI
2418 }
2419}
2420
2421static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2422 struct snd_ctl_elem_value *ucontrol)
2423{
2424 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2425 struct hda_gen_spec *spec = codec->spec;
2426 unsigned int val = ucontrol->value.enumerated.item[0];
2427
2428 if (val == spec->aamix_mode)
2429 return 0;
2430 spec->aamix_mode = val;
2431 update_aamix_paths(codec, val, spec->out_paths[0],
a1e908ed
TI
2432 spec->aamix_out_paths[0],
2433 spec->autocfg.line_out_type);
c30aa7b2 2434 update_aamix_paths(codec, val, spec->hp_paths[0],
a1e908ed
TI
2435 spec->aamix_out_paths[1],
2436 AUTO_PIN_HP_OUT);
c30aa7b2 2437 update_aamix_paths(codec, val, spec->speaker_paths[0],
a1e908ed
TI
2438 spec->aamix_out_paths[2],
2439 AUTO_PIN_SPEAKER_OUT);
c30aa7b2
TI
2440 return 1;
2441}
2442
2443static const struct snd_kcontrol_new loopback_mixing_enum = {
2444 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2445 .name = "Loopback Mixing",
2446 .info = loopback_mixing_info,
2447 .get = loopback_mixing_get,
2448 .put = loopback_mixing_put,
2449};
2450
2451static int create_loopback_mixing_ctl(struct hda_codec *codec)
2452{
2453 struct hda_gen_spec *spec = codec->spec;
2454
2455 if (!spec->mixer_nid)
2456 return 0;
2457 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2458 spec->aamix_out_paths[2]))
2459 return 0;
2460 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2461 return -ENOMEM;
a1e908ed 2462 spec->have_aamix_ctl = 1;
c30aa7b2
TI
2463 return 0;
2464}
2465
352f7f91
TI
2466/*
2467 * shared headphone/mic handling
2468 */
2469
2470static void call_update_outputs(struct hda_codec *codec);
2471
2472/* for shared I/O, change the pin-control accordingly */
967303da 2473static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
352f7f91
TI
2474{
2475 struct hda_gen_spec *spec = codec->spec;
967303da 2476 bool as_mic;
352f7f91 2477 unsigned int val;
967303da 2478 hda_nid_t pin;
352f7f91 2479
967303da
TI
2480 pin = spec->hp_mic_pin;
2481 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
352f7f91 2482
967303da
TI
2483 if (!force) {
2484 val = snd_hda_codec_get_pin_target(codec, pin);
2485 if (as_mic) {
2486 if (val & PIN_IN)
2487 return;
2488 } else {
2489 if (val & PIN_OUT)
2490 return;
2491 }
2492 }
2493
2494 val = snd_hda_get_default_vref(codec, pin);
2495 /* if the HP pin doesn't support VREF and the codec driver gives an
2496 * alternative pin, set up the VREF on that pin instead
2497 */
352f7f91
TI
2498 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2499 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2500 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2501 if (vref_val != AC_PINCTL_VREF_HIZ)
7594aa33 2502 snd_hda_set_pin_ctl_cache(codec, vref_pin,
967303da 2503 PIN_IN | (as_mic ? vref_val : 0));
352f7f91
TI
2504 }
2505
8ba955ce
TI
2506 if (!spec->hp_mic_jack_modes) {
2507 if (as_mic)
2508 val |= PIN_IN;
2509 else
2510 val = PIN_HP;
2511 set_pin_target(codec, pin, val, true);
963afde9 2512 call_hp_automute(codec, NULL);
8ba955ce 2513 }
352f7f91
TI
2514}
2515
2516/* create a shared input with the headphone out */
967303da 2517static int create_hp_mic(struct hda_codec *codec)
352f7f91
TI
2518{
2519 struct hda_gen_spec *spec = codec->spec;
2520 struct auto_pin_cfg *cfg = &spec->autocfg;
2521 unsigned int defcfg;
2522 hda_nid_t nid;
2523
967303da
TI
2524 if (!spec->hp_mic) {
2525 if (spec->suppress_hp_mic_detect)
2526 return 0;
2527 /* automatic detection: only if no input or a single internal
2528 * input pin is found, try to detect the shared hp/mic
2529 */
2530 if (cfg->num_inputs > 1)
2531 return 0;
2532 else if (cfg->num_inputs == 1) {
2533 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2534 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2535 return 0;
2536 }
2537 }
2538
2539 spec->hp_mic = 0; /* clear once */
2540 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
352f7f91
TI
2541 return 0;
2542
967303da
TI
2543 nid = 0;
2544 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2545 nid = cfg->line_out_pins[0];
2546 else if (cfg->hp_outs > 0)
2547 nid = cfg->hp_pins[0];
2548 if (!nid)
2549 return 0;
352f7f91
TI
2550
2551 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2552 return 0; /* no input */
2553
967303da
TI
2554 cfg->inputs[cfg->num_inputs].pin = nid;
2555 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
cb420b11 2556 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
967303da
TI
2557 cfg->num_inputs++;
2558 spec->hp_mic = 1;
2559 spec->hp_mic_pin = nid;
2560 /* we can't handle auto-mic together with HP-mic */
2561 spec->suppress_auto_mic = 1;
4e76a883 2562 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
352f7f91
TI
2563 return 0;
2564}
2565
978e77e7
TI
2566/*
2567 * output jack mode
2568 */
5f171baa
TI
2569
2570static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2571
2572static const char * const out_jack_texts[] = {
2573 "Line Out", "Headphone Out",
2574};
2575
978e77e7
TI
2576static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2577 struct snd_ctl_elem_info *uinfo)
2578{
5f171baa 2579 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
978e77e7
TI
2580}
2581
2582static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2583 struct snd_ctl_elem_value *ucontrol)
2584{
2585 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2586 hda_nid_t nid = kcontrol->private_value;
2587 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2588 ucontrol->value.enumerated.item[0] = 1;
2589 else
2590 ucontrol->value.enumerated.item[0] = 0;
2591 return 0;
2592}
2593
2594static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2595 struct snd_ctl_elem_value *ucontrol)
2596{
2597 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2598 hda_nid_t nid = kcontrol->private_value;
2599 unsigned int val;
2600
2601 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2602 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2603 return 0;
2604 snd_hda_set_pin_ctl_cache(codec, nid, val);
2605 return 1;
2606}
2607
2608static const struct snd_kcontrol_new out_jack_mode_enum = {
2609 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2610 .info = out_jack_mode_info,
2611 .get = out_jack_mode_get,
2612 .put = out_jack_mode_put,
2613};
2614
2615static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2616{
2617 struct hda_gen_spec *spec = codec->spec;
2618 int i;
2619
2620 for (i = 0; i < spec->kctls.used; i++) {
2621 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2622 if (!strcmp(kctl->name, name) && kctl->index == idx)
2623 return true;
2624 }
2625 return false;
2626}
2627
2628static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2629 char *name, size_t name_len)
2630{
2631 struct hda_gen_spec *spec = codec->spec;
2632 int idx = 0;
2633
2634 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2635 strlcat(name, " Jack Mode", name_len);
2636
2637 for (; find_kctl_name(codec, name, idx); idx++)
2638 ;
2639}
2640
5f171baa
TI
2641static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2642{
2643 struct hda_gen_spec *spec = codec->spec;
f811c3cf 2644 if (spec->add_jack_modes) {
5f171baa
TI
2645 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2646 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2647 return 2;
2648 }
2649 return 1;
2650}
2651
978e77e7
TI
2652static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2653 hda_nid_t *pins)
2654{
2655 struct hda_gen_spec *spec = codec->spec;
2656 int i;
2657
2658 for (i = 0; i < num_pins; i++) {
2659 hda_nid_t pin = pins[i];
ced4cefc 2660 if (pin == spec->hp_mic_pin)
5f171baa 2661 continue;
5f171baa 2662 if (get_out_jack_num_items(codec, pin) > 1) {
978e77e7 2663 struct snd_kcontrol_new *knew;
975cc02a 2664 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
978e77e7
TI
2665 get_jack_mode_name(codec, pin, name, sizeof(name));
2666 knew = snd_hda_gen_add_kctl(spec, name,
2667 &out_jack_mode_enum);
2668 if (!knew)
2669 return -ENOMEM;
2670 knew->private_value = pin;
2671 }
2672 }
2673
2674 return 0;
2675}
2676
29476558
TI
2677/*
2678 * input jack mode
2679 */
2680
2681/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2682#define NUM_VREFS 6
2683
2684static const char * const vref_texts[NUM_VREFS] = {
2685 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2686 "", "Mic 80pc Bias", "Mic 100pc Bias"
2687};
2688
2689static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2690{
2691 unsigned int pincap;
2692
2693 pincap = snd_hda_query_pin_caps(codec, pin);
2694 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2695 /* filter out unusual vrefs */
2696 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2697 return pincap;
2698}
2699
2700/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2701static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2702{
2703 unsigned int i, n = 0;
2704
2705 for (i = 0; i < NUM_VREFS; i++) {
2706 if (vref_caps & (1 << i)) {
2707 if (n == item_idx)
2708 return i;
2709 n++;
2710 }
2711 }
2712 return 0;
2713}
2714
2715/* convert back from the vref ctl index to the enum item index */
2716static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2717{
2718 unsigned int i, n = 0;
2719
2720 for (i = 0; i < NUM_VREFS; i++) {
2721 if (i == idx)
2722 return n;
2723 if (vref_caps & (1 << i))
2724 n++;
2725 }
2726 return 0;
2727}
2728
2729static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2730 struct snd_ctl_elem_info *uinfo)
2731{
2732 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2733 hda_nid_t nid = kcontrol->private_value;
2734 unsigned int vref_caps = get_vref_caps(codec, nid);
2735
2736 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2737 vref_texts);
2738 /* set the right text */
2739 strcpy(uinfo->value.enumerated.name,
2740 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2741 return 0;
2742}
2743
2744static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2745 struct snd_ctl_elem_value *ucontrol)
2746{
2747 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2748 hda_nid_t nid = kcontrol->private_value;
2749 unsigned int vref_caps = get_vref_caps(codec, nid);
2750 unsigned int idx;
2751
2752 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2753 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2754 return 0;
2755}
2756
2757static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2758 struct snd_ctl_elem_value *ucontrol)
2759{
2760 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2761 hda_nid_t nid = kcontrol->private_value;
2762 unsigned int vref_caps = get_vref_caps(codec, nid);
2763 unsigned int val, idx;
2764
2765 val = snd_hda_codec_get_pin_target(codec, nid);
2766 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2767 if (idx == ucontrol->value.enumerated.item[0])
2768 return 0;
2769
2770 val &= ~AC_PINCTL_VREFEN;
2771 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2772 snd_hda_set_pin_ctl_cache(codec, nid, val);
2773 return 1;
2774}
2775
2776static const struct snd_kcontrol_new in_jack_mode_enum = {
2777 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2778 .info = in_jack_mode_info,
2779 .get = in_jack_mode_get,
2780 .put = in_jack_mode_put,
2781};
2782
5f171baa
TI
2783static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2784{
2785 struct hda_gen_spec *spec = codec->spec;
2786 int nitems = 0;
f811c3cf 2787 if (spec->add_jack_modes)
5f171baa
TI
2788 nitems = hweight32(get_vref_caps(codec, pin));
2789 return nitems ? nitems : 1;
2790}
2791
29476558
TI
2792static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2793{
2794 struct hda_gen_spec *spec = codec->spec;
29476558 2795 struct snd_kcontrol_new *knew;
975cc02a 2796 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5f171baa
TI
2797 unsigned int defcfg;
2798
f811c3cf
TI
2799 if (pin == spec->hp_mic_pin)
2800 return 0; /* already done in create_out_jack_mode() */
29476558
TI
2801
2802 /* no jack mode for fixed pins */
2803 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2804 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2805 return 0;
2806
2807 /* no multiple vref caps? */
5f171baa 2808 if (get_in_jack_num_items(codec, pin) <= 1)
29476558
TI
2809 return 0;
2810
2811 get_jack_mode_name(codec, pin, name, sizeof(name));
2812 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2813 if (!knew)
2814 return -ENOMEM;
2815 knew->private_value = pin;
2816 return 0;
2817}
2818
5f171baa
TI
2819/*
2820 * HP/mic shared jack mode
2821 */
2822static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2823 struct snd_ctl_elem_info *uinfo)
2824{
2825 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2826 hda_nid_t nid = kcontrol->private_value;
2827 int out_jacks = get_out_jack_num_items(codec, nid);
2828 int in_jacks = get_in_jack_num_items(codec, nid);
2829 const char *text = NULL;
2830 int idx;
2831
2832 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2833 uinfo->count = 1;
2834 uinfo->value.enumerated.items = out_jacks + in_jacks;
2835 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2836 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2837 idx = uinfo->value.enumerated.item;
2838 if (idx < out_jacks) {
2839 if (out_jacks > 1)
2840 text = out_jack_texts[idx];
2841 else
2842 text = "Headphone Out";
2843 } else {
2844 idx -= out_jacks;
2845 if (in_jacks > 1) {
2846 unsigned int vref_caps = get_vref_caps(codec, nid);
2847 text = vref_texts[get_vref_idx(vref_caps, idx)];
2848 } else
2849 text = "Mic In";
2850 }
2851
2852 strcpy(uinfo->value.enumerated.name, text);
2853 return 0;
2854}
2855
2856static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2857{
2858 int out_jacks = get_out_jack_num_items(codec, nid);
2859 int in_jacks = get_in_jack_num_items(codec, nid);
2860 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2861 int idx = 0;
2862
2863 if (val & PIN_OUT) {
2864 if (out_jacks > 1 && val == PIN_HP)
2865 idx = 1;
2866 } else if (val & PIN_IN) {
2867 idx = out_jacks;
2868 if (in_jacks > 1) {
2869 unsigned int vref_caps = get_vref_caps(codec, nid);
2870 val &= AC_PINCTL_VREFEN;
2871 idx += cvt_from_vref_idx(vref_caps, val);
2872 }
2873 }
2874 return idx;
2875}
2876
2877static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2878 struct snd_ctl_elem_value *ucontrol)
2879{
2880 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2881 hda_nid_t nid = kcontrol->private_value;
2882 ucontrol->value.enumerated.item[0] =
2883 get_cur_hp_mic_jack_mode(codec, nid);
2884 return 0;
2885}
2886
2887static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2888 struct snd_ctl_elem_value *ucontrol)
2889{
2890 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2891 hda_nid_t nid = kcontrol->private_value;
2892 int out_jacks = get_out_jack_num_items(codec, nid);
2893 int in_jacks = get_in_jack_num_items(codec, nid);
2894 unsigned int val, oldval, idx;
2895
2896 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2897 idx = ucontrol->value.enumerated.item[0];
2898 if (oldval == idx)
2899 return 0;
2900
2901 if (idx < out_jacks) {
2902 if (out_jacks > 1)
2903 val = idx ? PIN_HP : PIN_OUT;
2904 else
2905 val = PIN_HP;
2906 } else {
2907 idx -= out_jacks;
2908 if (in_jacks > 1) {
2909 unsigned int vref_caps = get_vref_caps(codec, nid);
2910 val = snd_hda_codec_get_pin_target(codec, nid);
3f550e32
TI
2911 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2912 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
5f171baa 2913 } else
16c0cefe 2914 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
5f171baa
TI
2915 }
2916 snd_hda_set_pin_ctl_cache(codec, nid, val);
963afde9 2917 call_hp_automute(codec, NULL);
8ba955ce 2918
5f171baa
TI
2919 return 1;
2920}
2921
2922static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2923 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2924 .info = hp_mic_jack_mode_info,
2925 .get = hp_mic_jack_mode_get,
2926 .put = hp_mic_jack_mode_put,
2927};
2928
2929static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2930{
2931 struct hda_gen_spec *spec = codec->spec;
2932 struct snd_kcontrol_new *knew;
2933
5f171baa
TI
2934 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2935 &hp_mic_jack_mode_enum);
2936 if (!knew)
2937 return -ENOMEM;
2938 knew->private_value = pin;
8ba955ce 2939 spec->hp_mic_jack_modes = 1;
5f171baa
TI
2940 return 0;
2941}
352f7f91
TI
2942
2943/*
2944 * Parse input paths
2945 */
2946
352f7f91 2947/* add the powersave loopback-list entry */
0186f4f4 2948static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
352f7f91
TI
2949{
2950 struct hda_amp_list *list;
2951
0186f4f4
TI
2952 list = snd_array_new(&spec->loopback_list);
2953 if (!list)
2954 return -ENOMEM;
352f7f91
TI
2955 list->nid = mix;
2956 list->dir = HDA_INPUT;
2957 list->idx = idx;
0186f4f4
TI
2958 spec->loopback.amplist = spec->loopback_list.list;
2959 return 0;
352f7f91 2960}
352f7f91 2961
2ded3e5b
TI
2962/* return true if either a volume or a mute amp is found for the given
2963 * aamix path; the amp has to be either in the mixer node or its direct leaf
2964 */
2965static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2966 hda_nid_t pin, unsigned int *mix_val,
2967 unsigned int *mute_val)
2968{
2969 int idx, num_conns;
2970 const hda_nid_t *list;
2971 hda_nid_t nid;
2972
2973 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2974 if (idx < 0)
2975 return false;
2976
2977 *mix_val = *mute_val = 0;
2978 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2979 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2980 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2981 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2982 if (*mix_val && *mute_val)
2983 return true;
2984
2985 /* check leaf node */
2986 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2987 if (num_conns < idx)
2988 return false;
2989 nid = list[idx];
43a8e50a
TI
2990 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2991 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
2ded3e5b 2992 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
43a8e50a
TI
2993 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2994 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
2ded3e5b
TI
2995 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2996
2997 return *mix_val || *mute_val;
2998}
2999
352f7f91 3000/* create input playback/capture controls for the given pin */
196c1766
TI
3001static int new_analog_input(struct hda_codec *codec, int input_idx,
3002 hda_nid_t pin, const char *ctlname, int ctlidx,
352f7f91
TI
3003 hda_nid_t mix_nid)
3004{
3005 struct hda_gen_spec *spec = codec->spec;
3006 struct nid_path *path;
2ded3e5b 3007 unsigned int mix_val, mute_val;
352f7f91
TI
3008 int err, idx;
3009
2ded3e5b
TI
3010 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3011 return 0;
352f7f91 3012
3ca529d3 3013 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
352f7f91
TI
3014 if (!path)
3015 return -EINVAL;
4e76a883 3016 print_nid_path(codec, "loopback", path);
196c1766 3017 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
352f7f91
TI
3018
3019 idx = path->idx[path->depth - 1];
2ded3e5b
TI
3020 if (mix_val) {
3021 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
352f7f91
TI
3022 if (err < 0)
3023 return err;
2ded3e5b 3024 path->ctls[NID_PATH_VOL_CTL] = mix_val;
352f7f91
TI
3025 }
3026
2ded3e5b
TI
3027 if (mute_val) {
3028 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
352f7f91
TI
3029 if (err < 0)
3030 return err;
2ded3e5b 3031 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
352f7f91
TI
3032 }
3033
3034 path->active = true;
e6feb5d0 3035 path->stream_enabled = true; /* no DAC/ADC involved */
0186f4f4
TI
3036 err = add_loopback_list(spec, mix_nid, idx);
3037 if (err < 0)
3038 return err;
e4a395e7
TI
3039
3040 if (spec->mixer_nid != spec->mixer_merge_nid &&
3041 !spec->loopback_merge_path) {
3042 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3043 spec->mixer_merge_nid, 0);
3044 if (path) {
4e76a883 3045 print_nid_path(codec, "loopback-merge", path);
e4a395e7 3046 path->active = true;
6b275b14 3047 path->pin_fixed = true; /* static route */
e6feb5d0 3048 path->stream_enabled = true; /* no DAC/ADC involved */
e4a395e7
TI
3049 spec->loopback_merge_path =
3050 snd_hda_get_path_idx(codec, path);
3051 }
3052 }
3053
352f7f91
TI
3054 return 0;
3055}
3056
3057static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3058{
3059 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3060 return (pincap & AC_PINCAP_IN) != 0;
3061}
3062
3063/* Parse the codec tree and retrieve ADCs */
3064static int fill_adc_nids(struct hda_codec *codec)
3065{
3066 struct hda_gen_spec *spec = codec->spec;
3067 hda_nid_t nid;
3068 hda_nid_t *adc_nids = spec->adc_nids;
3069 int max_nums = ARRAY_SIZE(spec->adc_nids);
3070 int i, nums = 0;
3071
3072 nid = codec->start_nid;
3073 for (i = 0; i < codec->num_nodes; i++, nid++) {
3074 unsigned int caps = get_wcaps(codec, nid);
3075 int type = get_wcaps_type(caps);
3076
3077 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3078 continue;
3079 adc_nids[nums] = nid;
3080 if (++nums >= max_nums)
3081 break;
3082 }
3083 spec->num_adc_nids = nums;
0ffd534e
TI
3084
3085 /* copy the detected ADCs to all_adcs[] */
3086 spec->num_all_adcs = nums;
3087 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3088
352f7f91
TI
3089 return nums;
3090}
3091
3092/* filter out invalid adc_nids that don't give all active input pins;
3093 * if needed, check whether dynamic ADC-switching is available
3094 */
3095static int check_dyn_adc_switch(struct hda_codec *codec)
3096{
3097 struct hda_gen_spec *spec = codec->spec;
3098 struct hda_input_mux *imux = &spec->input_mux;
3a65bcdc 3099 unsigned int ok_bits;
352f7f91 3100 int i, n, nums;
352f7f91 3101
352f7f91 3102 nums = 0;
3a65bcdc 3103 ok_bits = 0;
352f7f91 3104 for (n = 0; n < spec->num_adc_nids; n++) {
352f7f91 3105 for (i = 0; i < imux->num_items; i++) {
3a65bcdc 3106 if (!spec->input_paths[i][n])
352f7f91
TI
3107 break;
3108 }
3a65bcdc
TI
3109 if (i >= imux->num_items) {
3110 ok_bits |= (1 << n);
3111 nums++;
3112 }
352f7f91
TI
3113 }
3114
3a65bcdc 3115 if (!ok_bits) {
352f7f91
TI
3116 /* check whether ADC-switch is possible */
3117 for (i = 0; i < imux->num_items; i++) {
352f7f91 3118 for (n = 0; n < spec->num_adc_nids; n++) {
3a65bcdc 3119 if (spec->input_paths[i][n]) {
352f7f91
TI
3120 spec->dyn_adc_idx[i] = n;
3121 break;
3122 }
3123 }
3124 }
3125
4e76a883 3126 codec_dbg(codec, "enabling ADC switching\n");
352f7f91
TI
3127 spec->dyn_adc_switch = 1;
3128 } else if (nums != spec->num_adc_nids) {
3a65bcdc
TI
3129 /* shrink the invalid adcs and input paths */
3130 nums = 0;
3131 for (n = 0; n < spec->num_adc_nids; n++) {
3132 if (!(ok_bits & (1 << n)))
3133 continue;
3134 if (n != nums) {
3135 spec->adc_nids[nums] = spec->adc_nids[n];
980428ce
TI
3136 for (i = 0; i < imux->num_items; i++) {
3137 invalidate_nid_path(codec,
3138 spec->input_paths[i][nums]);
3a65bcdc
TI
3139 spec->input_paths[i][nums] =
3140 spec->input_paths[i][n];
980428ce 3141 }
3a65bcdc
TI
3142 }
3143 nums++;
3144 }
352f7f91
TI
3145 spec->num_adc_nids = nums;
3146 }
3147
967303da
TI
3148 if (imux->num_items == 1 ||
3149 (imux->num_items == 2 && spec->hp_mic)) {
4e76a883 3150 codec_dbg(codec, "reducing to a single ADC\n");
352f7f91
TI
3151 spec->num_adc_nids = 1; /* reduce to a single ADC */
3152 }
3153
3154 /* single index for individual volumes ctls */
3155 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3156 spec->num_adc_nids = 1;
3157
3158 return 0;
3159}
3160
f3fc0b0b
TI
3161/* parse capture source paths from the given pin and create imux items */
3162static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
9dba205b
TI
3163 int cfg_idx, int num_adcs,
3164 const char *label, int anchor)
f3fc0b0b
TI
3165{
3166 struct hda_gen_spec *spec = codec->spec;
3167 struct hda_input_mux *imux = &spec->input_mux;
3168 int imux_idx = imux->num_items;
3169 bool imux_added = false;
3170 int c;
3171
3172 for (c = 0; c < num_adcs; c++) {
3173 struct nid_path *path;
3174 hda_nid_t adc = spec->adc_nids[c];
3175
3176 if (!is_reachable_path(codec, pin, adc))
3177 continue;
3178 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3179 if (!path)
3180 continue;
4e76a883 3181 print_nid_path(codec, "input", path);
f3fc0b0b
TI
3182 spec->input_paths[imux_idx][c] =
3183 snd_hda_get_path_idx(codec, path);
3184
3185 if (!imux_added) {
967303da
TI
3186 if (spec->hp_mic_pin == pin)
3187 spec->hp_mic_mux_idx = imux->num_items;
f3fc0b0b 3188 spec->imux_pins[imux->num_items] = pin;
6194b99d 3189 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
f3fc0b0b 3190 imux_added = true;
f1e762dd
TI
3191 if (spec->dyn_adc_switch)
3192 spec->dyn_adc_idx[imux_idx] = c;
f3fc0b0b
TI
3193 }
3194 }
3195
3196 return 0;
3197}
3198
352f7f91
TI
3199/*
3200 * create playback/capture controls for input pins
3201 */
9dba205b 3202
c970042c
TI
3203/* fill the label for each input at first */
3204static int fill_input_pin_labels(struct hda_codec *codec)
3205{
3206 struct hda_gen_spec *spec = codec->spec;
3207 const struct auto_pin_cfg *cfg = &spec->autocfg;
3208 int i;
3209
3210 for (i = 0; i < cfg->num_inputs; i++) {
3211 hda_nid_t pin = cfg->inputs[i].pin;
3212 const char *label;
3213 int j, idx;
3214
3215 if (!is_input_pin(codec, pin))
3216 continue;
3217
3218 label = hda_get_autocfg_input_label(codec, cfg, i);
3219 idx = 0;
8e8db7f1 3220 for (j = i - 1; j >= 0; j--) {
c970042c
TI
3221 if (spec->input_labels[j] &&
3222 !strcmp(spec->input_labels[j], label)) {
3223 idx = spec->input_label_idxs[j] + 1;
3224 break;
3225 }
3226 }
3227
3228 spec->input_labels[i] = label;
3229 spec->input_label_idxs[i] = idx;
3230 }
3231
3232 return 0;
3233}
3234
9dba205b
TI
3235#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3236
352f7f91
TI
3237static int create_input_ctls(struct hda_codec *codec)
3238{
3239 struct hda_gen_spec *spec = codec->spec;
3240 const struct auto_pin_cfg *cfg = &spec->autocfg;
3241 hda_nid_t mixer = spec->mixer_nid;
352f7f91 3242 int num_adcs;
c970042c 3243 int i, err;
2c12c30d 3244 unsigned int val;
352f7f91
TI
3245
3246 num_adcs = fill_adc_nids(codec);
3247 if (num_adcs < 0)
3248 return 0;
3249
c970042c
TI
3250 err = fill_input_pin_labels(codec);
3251 if (err < 0)
3252 return err;
3253
352f7f91
TI
3254 for (i = 0; i < cfg->num_inputs; i++) {
3255 hda_nid_t pin;
352f7f91
TI
3256
3257 pin = cfg->inputs[i].pin;
3258 if (!is_input_pin(codec, pin))
3259 continue;
3260
2c12c30d
TI
3261 val = PIN_IN;
3262 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3263 val |= snd_hda_get_default_vref(codec, pin);
93c9d8ae
TI
3264 if (pin != spec->hp_mic_pin)
3265 set_pin_target(codec, pin, val, false);
2c12c30d 3266
352f7f91
TI
3267 if (mixer) {
3268 if (is_reachable_path(codec, pin, mixer)) {
196c1766 3269 err = new_analog_input(codec, i, pin,
c970042c
TI
3270 spec->input_labels[i],
3271 spec->input_label_idxs[i],
3272 mixer);
352f7f91
TI
3273 if (err < 0)
3274 return err;
3275 }
3276 }
3277
c970042c
TI
3278 err = parse_capture_source(codec, pin, i, num_adcs,
3279 spec->input_labels[i], -mixer);
f3fc0b0b
TI
3280 if (err < 0)
3281 return err;
29476558 3282
f811c3cf 3283 if (spec->add_jack_modes) {
29476558
TI
3284 err = create_in_jack_mode(codec, pin);
3285 if (err < 0)
3286 return err;
3287 }
f3fc0b0b 3288 }
352f7f91 3289
f1e762dd 3290 /* add stereo mix when explicitly enabled via hint */
74f14b36 3291 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
9dba205b 3292 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
f3fc0b0b
TI
3293 "Stereo Mix", 0);
3294 if (err < 0)
3295 return err;
82d04e10
TI
3296 else
3297 spec->suppress_auto_mic = 1;
352f7f91
TI
3298 }
3299
3300 return 0;
3301}
3302
3303
3304/*
3305 * input source mux
3306 */
3307
c697b716
TI
3308/* get the input path specified by the given adc and imux indices */
3309static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
352f7f91
TI
3310{
3311 struct hda_gen_spec *spec = codec->spec;
b56fa1ed
DH
3312 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3313 snd_BUG();
3314 return NULL;
3315 }
352f7f91
TI
3316 if (spec->dyn_adc_switch)
3317 adc_idx = spec->dyn_adc_idx[imux_idx];
d3d982f7 3318 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
b56fa1ed
DH
3319 snd_BUG();
3320 return NULL;
3321 }
c697b716 3322 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
352f7f91
TI
3323}
3324
3325static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3326 unsigned int idx);
3327
3328static int mux_enum_info(struct snd_kcontrol *kcontrol,
3329 struct snd_ctl_elem_info *uinfo)
3330{
3331 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3332 struct hda_gen_spec *spec = codec->spec;
3333 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3334}
3335
3336static int mux_enum_get(struct snd_kcontrol *kcontrol,
3337 struct snd_ctl_elem_value *ucontrol)
3338{
3339 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3340 struct hda_gen_spec *spec = codec->spec;
2a8d5391
TI
3341 /* the ctls are created at once with multiple counts */
3342 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
352f7f91
TI
3343
3344 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3345 return 0;
3346}
3347
3348static int mux_enum_put(struct snd_kcontrol *kcontrol,
3349 struct snd_ctl_elem_value *ucontrol)
3350{
3351 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2a8d5391 3352 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
352f7f91
TI
3353 return mux_select(codec, adc_idx,
3354 ucontrol->value.enumerated.item[0]);
3355}
3356
352f7f91
TI
3357static const struct snd_kcontrol_new cap_src_temp = {
3358 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3359 .name = "Input Source",
3360 .info = mux_enum_info,
3361 .get = mux_enum_get,
3362 .put = mux_enum_put,
3363};
3364
47d46abb
TI
3365/*
3366 * capture volume and capture switch ctls
3367 */
3368
352f7f91
TI
3369typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3370 struct snd_ctl_elem_value *ucontrol);
3371
47d46abb 3372/* call the given amp update function for all amps in the imux list at once */
352f7f91
TI
3373static int cap_put_caller(struct snd_kcontrol *kcontrol,
3374 struct snd_ctl_elem_value *ucontrol,
3375 put_call_t func, int type)
3376{
3377 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3378 struct hda_gen_spec *spec = codec->spec;
3379 const struct hda_input_mux *imux;
3380 struct nid_path *path;
3381 int i, adc_idx, err = 0;
3382
3383 imux = &spec->input_mux;
a053d1e3 3384 adc_idx = kcontrol->id.index;
352f7f91 3385 mutex_lock(&codec->control_mutex);
47d46abb
TI
3386 /* we use the cache-only update at first since multiple input paths
3387 * may shared the same amp; by updating only caches, the redundant
3388 * writes to hardware can be reduced.
3389 */
352f7f91
TI
3390 codec->cached_write = 1;
3391 for (i = 0; i < imux->num_items; i++) {
c697b716
TI
3392 path = get_input_path(codec, adc_idx, i);
3393 if (!path || !path->ctls[type])
352f7f91
TI
3394 continue;
3395 kcontrol->private_value = path->ctls[type];
3396 err = func(kcontrol, ucontrol);
3397 if (err < 0)
3398 goto error;
3399 }
3400 error:
3401 codec->cached_write = 0;
3402 mutex_unlock(&codec->control_mutex);
dc870f38 3403 snd_hda_codec_flush_cache(codec); /* flush the updates */
352f7f91 3404 if (err >= 0 && spec->cap_sync_hook)
7fe30711 3405 spec->cap_sync_hook(codec, kcontrol, ucontrol);
352f7f91
TI
3406 return err;
3407}
3408
3409/* capture volume ctl callbacks */
3410#define cap_vol_info snd_hda_mixer_amp_volume_info
3411#define cap_vol_get snd_hda_mixer_amp_volume_get
3412#define cap_vol_tlv snd_hda_mixer_amp_tlv
3413
3414static int cap_vol_put(struct snd_kcontrol *kcontrol,
3415 struct snd_ctl_elem_value *ucontrol)
3416{
3417 return cap_put_caller(kcontrol, ucontrol,
3418 snd_hda_mixer_amp_volume_put,
3419 NID_PATH_VOL_CTL);
3420}
3421
3422static const struct snd_kcontrol_new cap_vol_temp = {
3423 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3424 .name = "Capture Volume",
3425 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3426 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3427 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3428 .info = cap_vol_info,
3429 .get = cap_vol_get,
3430 .put = cap_vol_put,
3431 .tlv = { .c = cap_vol_tlv },
3432};
3433
3434/* capture switch ctl callbacks */
3435#define cap_sw_info snd_ctl_boolean_stereo_info
3436#define cap_sw_get snd_hda_mixer_amp_switch_get
3437
3438static int cap_sw_put(struct snd_kcontrol *kcontrol,
3439 struct snd_ctl_elem_value *ucontrol)
3440{
a90229e0 3441 return cap_put_caller(kcontrol, ucontrol,
352f7f91
TI
3442 snd_hda_mixer_amp_switch_put,
3443 NID_PATH_MUTE_CTL);
3444}
3445
3446static const struct snd_kcontrol_new cap_sw_temp = {
3447 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3448 .name = "Capture Switch",
3449 .info = cap_sw_info,
3450 .get = cap_sw_get,
3451 .put = cap_sw_put,
3452};
3453
3454static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3455{
3456 hda_nid_t nid;
3457 int i, depth;
3458
3459 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3460 for (depth = 0; depth < 3; depth++) {
3461 if (depth >= path->depth)
3462 return -EINVAL;
3463 i = path->depth - depth - 1;
3464 nid = path->path[i];
3465 if (!path->ctls[NID_PATH_VOL_CTL]) {
3466 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3467 path->ctls[NID_PATH_VOL_CTL] =
3468 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3469 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3470 int idx = path->idx[i];
3471 if (!depth && codec->single_adc_amp)
3472 idx = 0;
3473 path->ctls[NID_PATH_VOL_CTL] =
3474 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3475 }
3476 }
3477 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3478 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3479 path->ctls[NID_PATH_MUTE_CTL] =
3480 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3481 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3482 int idx = path->idx[i];
3483 if (!depth && codec->single_adc_amp)
3484 idx = 0;
3485 path->ctls[NID_PATH_MUTE_CTL] =
3486 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3487 }
3488 }
3489 }
3490 return 0;
3491}
3492
3493static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3494{
3495 struct hda_gen_spec *spec = codec->spec;
3496 struct auto_pin_cfg *cfg = &spec->autocfg;
3497 unsigned int val;
3498 int i;
3499
3500 if (!spec->inv_dmic_split)
3501 return false;
3502 for (i = 0; i < cfg->num_inputs; i++) {
3503 if (cfg->inputs[i].pin != nid)
3504 continue;
3505 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3506 return false;
3507 val = snd_hda_codec_get_pincfg(codec, nid);
3508 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3509 }
3510 return false;
3511}
3512
a90229e0 3513/* capture switch put callback for a single control with hook call */
a35bd1e3
TI
3514static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3515 struct snd_ctl_elem_value *ucontrol)
3516{
3517 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3518 struct hda_gen_spec *spec = codec->spec;
3519 int ret;
3520
3521 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3522 if (ret < 0)
3523 return ret;
3524
a90229e0 3525 if (spec->cap_sync_hook)
7fe30711 3526 spec->cap_sync_hook(codec, kcontrol, ucontrol);
a35bd1e3
TI
3527
3528 return ret;
3529}
3530
352f7f91
TI
3531static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3532 int idx, bool is_switch, unsigned int ctl,
3533 bool inv_dmic)
3534{
3535 struct hda_gen_spec *spec = codec->spec;
975cc02a 3536 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
352f7f91
TI
3537 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3538 const char *sfx = is_switch ? "Switch" : "Volume";
3539 unsigned int chs = inv_dmic ? 1 : 3;
a35bd1e3 3540 struct snd_kcontrol_new *knew;
352f7f91
TI
3541
3542 if (!ctl)
3543 return 0;
3544
3545 if (label)
3546 snprintf(tmpname, sizeof(tmpname),
3547 "%s Capture %s", label, sfx);
3548 else
3549 snprintf(tmpname, sizeof(tmpname),
3550 "Capture %s", sfx);
a35bd1e3
TI
3551 knew = add_control(spec, type, tmpname, idx,
3552 amp_val_replace_channels(ctl, chs));
3553 if (!knew)
3554 return -ENOMEM;
a90229e0 3555 if (is_switch)
a35bd1e3
TI
3556 knew->put = cap_single_sw_put;
3557 if (!inv_dmic)
3558 return 0;
352f7f91
TI
3559
3560 /* Make independent right kcontrol */
3561 if (label)
3562 snprintf(tmpname, sizeof(tmpname),
3563 "Inverted %s Capture %s", label, sfx);
3564 else
3565 snprintf(tmpname, sizeof(tmpname),
3566 "Inverted Capture %s", sfx);
a35bd1e3 3567 knew = add_control(spec, type, tmpname, idx,
352f7f91 3568 amp_val_replace_channels(ctl, 2));
a35bd1e3
TI
3569 if (!knew)
3570 return -ENOMEM;
a90229e0 3571 if (is_switch)
a35bd1e3
TI
3572 knew->put = cap_single_sw_put;
3573 return 0;
352f7f91
TI
3574}
3575
3576/* create single (and simple) capture volume and switch controls */
3577static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3578 unsigned int vol_ctl, unsigned int sw_ctl,
3579 bool inv_dmic)
3580{
3581 int err;
3582 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3583 if (err < 0)
3584 return err;
3585 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3586 if (err < 0)
3587 return err;
3588 return 0;
3589}
3590
3591/* create bound capture volume and switch controls */
3592static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3593 unsigned int vol_ctl, unsigned int sw_ctl)
3594{
3595 struct hda_gen_spec *spec = codec->spec;
3596 struct snd_kcontrol_new *knew;
3597
3598 if (vol_ctl) {
12c93df6 3599 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
352f7f91
TI
3600 if (!knew)
3601 return -ENOMEM;
3602 knew->index = idx;
3603 knew->private_value = vol_ctl;
3604 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3605 }
3606 if (sw_ctl) {
12c93df6 3607 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
352f7f91
TI
3608 if (!knew)
3609 return -ENOMEM;
3610 knew->index = idx;
3611 knew->private_value = sw_ctl;
3612 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3613 }
3614 return 0;
3615}
3616
3617/* return the vol ctl when used first in the imux list */
3618static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3619{
352f7f91
TI
3620 struct nid_path *path;
3621 unsigned int ctl;
3622 int i;
3623
c697b716 3624 path = get_input_path(codec, 0, idx);
352f7f91
TI
3625 if (!path)
3626 return 0;
3627 ctl = path->ctls[type];
3628 if (!ctl)
3629 return 0;
3630 for (i = 0; i < idx - 1; i++) {
c697b716 3631 path = get_input_path(codec, 0, i);
352f7f91
TI
3632 if (path && path->ctls[type] == ctl)
3633 return 0;
3634 }
3635 return ctl;
3636}
3637
3638/* create individual capture volume and switch controls per input */
3639static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3640{
3641 struct hda_gen_spec *spec = codec->spec;
3642 struct hda_input_mux *imux = &spec->input_mux;
c970042c 3643 int i, err, type;
352f7f91
TI
3644
3645 for (i = 0; i < imux->num_items; i++) {
352f7f91 3646 bool inv_dmic;
c970042c 3647 int idx;
9dba205b 3648
c970042c
TI
3649 idx = imux->items[i].index;
3650 if (idx >= spec->autocfg.num_inputs)
9dba205b 3651 continue;
352f7f91
TI
3652 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3653
3654 for (type = 0; type < 2; type++) {
c970042c
TI
3655 err = add_single_cap_ctl(codec,
3656 spec->input_labels[idx],
3657 spec->input_label_idxs[idx],
3658 type,
352f7f91
TI
3659 get_first_cap_ctl(codec, i, type),
3660 inv_dmic);
3661 if (err < 0)
3662 return err;
3663 }
3664 }
3665 return 0;
3666}
3667
3668static int create_capture_mixers(struct hda_codec *codec)
3669{
3670 struct hda_gen_spec *spec = codec->spec;
3671 struct hda_input_mux *imux = &spec->input_mux;
3672 int i, n, nums, err;
3673
3674 if (spec->dyn_adc_switch)
3675 nums = 1;
3676 else
3677 nums = spec->num_adc_nids;
3678
3679 if (!spec->auto_mic && imux->num_items > 1) {
3680 struct snd_kcontrol_new *knew;
624d914d
TI
3681 const char *name;
3682 name = nums > 1 ? "Input Source" : "Capture Source";
3683 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
352f7f91
TI
3684 if (!knew)
3685 return -ENOMEM;
3686 knew->count = nums;
3687 }
3688
3689 for (n = 0; n < nums; n++) {
3690 bool multi = false;
99a5592d 3691 bool multi_cap_vol = spec->multi_cap_vol;
352f7f91
TI
3692 bool inv_dmic = false;
3693 int vol, sw;
3694
3695 vol = sw = 0;
3696 for (i = 0; i < imux->num_items; i++) {
3697 struct nid_path *path;
c697b716 3698 path = get_input_path(codec, n, i);
352f7f91
TI
3699 if (!path)
3700 continue;
3701 parse_capvol_in_path(codec, path);
3702 if (!vol)
3703 vol = path->ctls[NID_PATH_VOL_CTL];
99a5592d 3704 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
352f7f91 3705 multi = true;
99a5592d
DH
3706 if (!same_amp_caps(codec, vol,
3707 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3708 multi_cap_vol = true;
3709 }
352f7f91
TI
3710 if (!sw)
3711 sw = path->ctls[NID_PATH_MUTE_CTL];
99a5592d 3712 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
352f7f91 3713 multi = true;
99a5592d
DH
3714 if (!same_amp_caps(codec, sw,
3715 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3716 multi_cap_vol = true;
3717 }
352f7f91
TI
3718 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3719 inv_dmic = true;
3720 }
3721
3722 if (!multi)
3723 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3724 inv_dmic);
ccb04157 3725 else if (!multi_cap_vol && !inv_dmic)
352f7f91
TI
3726 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3727 else
3728 err = create_multi_cap_vol_ctl(codec);
3729 if (err < 0)
3730 return err;
3731 }
3732
3733 return 0;
3734}
3735
3736/*
3737 * add mic boosts if needed
3738 */
6f7c83af
TI
3739
3740/* check whether the given amp is feasible as a boost volume */
3741static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3742 int dir, int idx)
3743{
3744 unsigned int step;
3745
3746 if (!nid_has_volume(codec, nid, dir) ||
3747 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3748 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3749 return false;
3750
3751 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3752 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3753 if (step < 0x20)
3754 return false;
3755 return true;
3756}
3757
3758/* look for a boost amp in a widget close to the pin */
3759static unsigned int look_for_boost_amp(struct hda_codec *codec,
3760 struct nid_path *path)
3761{
3762 unsigned int val = 0;
3763 hda_nid_t nid;
3764 int depth;
3765
3766 for (depth = 0; depth < 3; depth++) {
3767 if (depth >= path->depth - 1)
3768 break;
3769 nid = path->path[depth];
3770 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3771 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3772 break;
3773 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3774 path->idx[depth])) {
3775 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3776 HDA_INPUT);
3777 break;
3778 }
3779 }
3780
3781 return val;
3782}
3783
352f7f91
TI
3784static int parse_mic_boost(struct hda_codec *codec)
3785{
3786 struct hda_gen_spec *spec = codec->spec;
3787 struct auto_pin_cfg *cfg = &spec->autocfg;
6f7c83af 3788 struct hda_input_mux *imux = &spec->input_mux;
a35bd1e3 3789 int i;
352f7f91 3790
6f7c83af
TI
3791 if (!spec->num_adc_nids)
3792 return 0;
352f7f91 3793
6f7c83af
TI
3794 for (i = 0; i < imux->num_items; i++) {
3795 struct nid_path *path;
3796 unsigned int val;
3797 int idx;
975cc02a 3798 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
02aba550 3799
6f7c83af
TI
3800 idx = imux->items[i].index;
3801 if (idx >= imux->num_items)
3802 continue;
352f7f91 3803
6f7c83af 3804 /* check only line-in and mic pins */
1799cdd5 3805 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
6f7c83af
TI
3806 continue;
3807
3808 path = get_input_path(codec, 0, i);
3809 if (!path)
3810 continue;
3811
3812 val = look_for_boost_amp(codec, path);
3813 if (!val)
3814 continue;
3815
3816 /* create a boost control */
3817 snprintf(boost_label, sizeof(boost_label),
3818 "%s Boost Volume", spec->input_labels[idx]);
a35bd1e3
TI
3819 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3820 spec->input_label_idxs[idx], val))
3821 return -ENOMEM;
6f7c83af
TI
3822
3823 path->ctls[NID_PATH_BOOST_CTL] = val;
352f7f91
TI
3824 }
3825 return 0;
3826}
3827
3828/*
3829 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3830 */
3831static void parse_digital(struct hda_codec *codec)
3832{
3833 struct hda_gen_spec *spec = codec->spec;
0c8c0f56 3834 struct nid_path *path;
352f7f91 3835 int i, nums;
2c12c30d 3836 hda_nid_t dig_nid, pin;
352f7f91
TI
3837
3838 /* support multiple SPDIFs; the secondary is set up as a slave */
3839 nums = 0;
3840 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2c12c30d 3841 pin = spec->autocfg.dig_out_pins[i];
352f7f91
TI
3842 dig_nid = look_for_dac(codec, pin, true);
3843 if (!dig_nid)
3844 continue;
3ca529d3 3845 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
0c8c0f56 3846 if (!path)
352f7f91 3847 continue;
4e76a883 3848 print_nid_path(codec, "digout", path);
e1284af7 3849 path->active = true;
6b275b14 3850 path->pin_fixed = true; /* no jack detection */
196c1766 3851 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
2c12c30d 3852 set_pin_target(codec, pin, PIN_OUT, false);
352f7f91
TI
3853 if (!nums) {
3854 spec->multiout.dig_out_nid = dig_nid;
3855 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3856 } else {
3857 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3858 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
d576422e 3859 break;
352f7f91
TI
3860 spec->slave_dig_outs[nums - 1] = dig_nid;
3861 }
3862 nums++;
3863 }
3864
3865 if (spec->autocfg.dig_in_pin) {
2c12c30d 3866 pin = spec->autocfg.dig_in_pin;
352f7f91
TI
3867 dig_nid = codec->start_nid;
3868 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
352f7f91
TI
3869 unsigned int wcaps = get_wcaps(codec, dig_nid);
3870 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3871 continue;
3872 if (!(wcaps & AC_WCAP_DIGITAL))
3873 continue;
2c12c30d 3874 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
352f7f91 3875 if (path) {
4e76a883 3876 print_nid_path(codec, "digin", path);
352f7f91 3877 path->active = true;
6b275b14 3878 path->pin_fixed = true; /* no jack */
352f7f91 3879 spec->dig_in_nid = dig_nid;
2430d7b7 3880 spec->digin_path = snd_hda_get_path_idx(codec, path);
2c12c30d 3881 set_pin_target(codec, pin, PIN_IN, false);
352f7f91
TI
3882 break;
3883 }
3884 }
3885 }
3886}
3887
3888
3889/*
3890 * input MUX handling
3891 */
3892
3893static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3894
3895/* select the given imux item; either unmute exclusively or select the route */
3896static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3897 unsigned int idx)
3898{
3899 struct hda_gen_spec *spec = codec->spec;
3900 const struct hda_input_mux *imux;
55196fff 3901 struct nid_path *old_path, *path;
352f7f91
TI
3902
3903 imux = &spec->input_mux;
3904 if (!imux->num_items)
3905 return 0;
3906
3907 if (idx >= imux->num_items)
3908 idx = imux->num_items - 1;
3909 if (spec->cur_mux[adc_idx] == idx)
3910 return 0;
3911
55196fff
TI
3912 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3913 if (!old_path)
352f7f91 3914 return 0;
55196fff
TI
3915 if (old_path->active)
3916 snd_hda_activate_path(codec, old_path, false, false);
352f7f91
TI
3917
3918 spec->cur_mux[adc_idx] = idx;
3919
967303da
TI
3920 if (spec->hp_mic)
3921 update_hp_mic(codec, adc_idx, false);
352f7f91
TI
3922
3923 if (spec->dyn_adc_switch)
3924 dyn_adc_pcm_resetup(codec, idx);
3925
c697b716 3926 path = get_input_path(codec, adc_idx, idx);
352f7f91
TI
3927 if (!path)
3928 return 0;
3929 if (path->active)
3930 return 0;
3931 snd_hda_activate_path(codec, path, true, false);
3932 if (spec->cap_sync_hook)
7fe30711 3933 spec->cap_sync_hook(codec, NULL, NULL);
55196fff 3934 path_power_down_sync(codec, old_path);
352f7f91
TI
3935 return 1;
3936}
3937
e6feb5d0
TI
3938/* power up/down widgets in the all paths that match with the given NID
3939 * as terminals (either start- or endpoint)
3940 *
3941 * returns the last changed NID, or zero if unchanged.
3942 */
3943static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3944 int pin_state, int stream_state)
3945{
3946 struct hda_gen_spec *spec = codec->spec;
3947 hda_nid_t last, changed = 0;
3948 struct nid_path *path;
3949 int n;
3950
3951 for (n = 0; n < spec->paths.used; n++) {
3952 path = snd_array_elem(&spec->paths, n);
3953 if (path->path[0] == nid ||
3954 path->path[path->depth - 1] == nid) {
3955 bool pin_old = path->pin_enabled;
3956 bool stream_old = path->stream_enabled;
3957
3958 if (pin_state >= 0)
3959 path->pin_enabled = pin_state;
3960 if (stream_state >= 0)
3961 path->stream_enabled = stream_state;
6b275b14
TI
3962 if ((!path->pin_fixed && path->pin_enabled != pin_old)
3963 || path->stream_enabled != stream_old) {
e6feb5d0
TI
3964 last = path_power_update(codec, path, true);
3965 if (last)
3966 changed = last;
3967 }
3968 }
3969 }
3970 return changed;
3971}
3972
3973/* power up/down the paths of the given pin according to the jack state;
3974 * power = 0/1 : only power up/down if it matches with the jack state,
3975 * < 0 : force power up/down to follow the jack sate
3976 *
3977 * returns the last changed NID, or zero if unchanged.
3978 */
3979static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
3980 int power)
3981{
3982 bool on;
3983
967b1307 3984 if (!codec->power_save_node)
e6feb5d0
TI
3985 return 0;
3986
3987 on = snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
3988 if (power >= 0 && on != power)
3989 return 0;
3990 return set_path_power(codec, pin, on, -1);
3991}
3992
3993static void pin_power_callback(struct hda_codec *codec,
3994 struct hda_jack_callback *jack,
3995 bool on)
3996{
3997 if (jack && jack->tbl->nid)
3998 sync_power_state_change(codec,
3999 set_pin_power_jack(codec, jack->tbl->nid, on));
4000}
4001
4002/* callback only doing power up -- called at first */
4003static void pin_power_up_callback(struct hda_codec *codec,
4004 struct hda_jack_callback *jack)
4005{
4006 pin_power_callback(codec, jack, true);
4007}
4008
4009/* callback only doing power down -- called at last */
4010static void pin_power_down_callback(struct hda_codec *codec,
4011 struct hda_jack_callback *jack)
4012{
4013 pin_power_callback(codec, jack, false);
4014}
4015
4016/* set up the power up/down callbacks */
4017static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4018 const hda_nid_t *pins, bool on)
4019{
4020 int i;
4021 hda_jack_callback_fn cb =
4022 on ? pin_power_up_callback : pin_power_down_callback;
4023
4024 for (i = 0; i < num_pins && pins[i]; i++) {
4025 if (is_jack_detectable(codec, pins[i]))
4026 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4027 else
4028 set_path_power(codec, pins[i], true, -1);
4029 }
4030}
4031
4032/* enabled power callback to each available I/O pin with jack detections;
4033 * the digital I/O pins are excluded because of the unreliable detectsion
4034 */
4035static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4036{
4037 struct hda_gen_spec *spec = codec->spec;
4038 struct auto_pin_cfg *cfg = &spec->autocfg;
4039 int i;
4040
967b1307 4041 if (!codec->power_save_node)
e6feb5d0
TI
4042 return;
4043 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4044 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4045 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4046 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4047 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4048 for (i = 0; i < cfg->num_inputs; i++)
4049 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4050}
4051
4052/* sync path power up/down with the jack states of given pins */
4053static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4054 const hda_nid_t *pins)
4055{
4056 int i;
4057
4058 for (i = 0; i < num_pins && pins[i]; i++)
4059 if (is_jack_detectable(codec, pins[i]))
4060 set_pin_power_jack(codec, pins[i], -1);
4061}
4062
4063/* sync path power up/down with pins; called at init and resume */
4064static void sync_all_pin_power_ctls(struct hda_codec *codec)
4065{
4066 struct hda_gen_spec *spec = codec->spec;
4067 struct auto_pin_cfg *cfg = &spec->autocfg;
4068 int i;
4069
967b1307 4070 if (!codec->power_save_node)
e6feb5d0
TI
4071 return;
4072 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4073 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4074 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4075 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4076 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4077 for (i = 0; i < cfg->num_inputs; i++)
4078 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4079}
352f7f91 4080
5ccf835c
TI
4081/* add fake paths if not present yet */
4082static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4083 int num_pins, const hda_nid_t *pins)
4084{
4085 struct hda_gen_spec *spec = codec->spec;
4086 struct nid_path *path;
4087 int i;
4088
4089 for (i = 0; i < num_pins; i++) {
4090 if (!pins[i])
4091 break;
4092 if (get_nid_path(codec, nid, pins[i], 0))
4093 continue;
4094 path = snd_array_new(&spec->paths);
4095 if (!path)
4096 return -ENOMEM;
4097 memset(path, 0, sizeof(*path));
4098 path->depth = 2;
4099 path->path[0] = nid;
4100 path->path[1] = pins[i];
4101 path->active = true;
4102 }
4103 return 0;
4104}
4105
4106/* create fake paths to all outputs from beep */
4107static int add_fake_beep_paths(struct hda_codec *codec)
4108{
4109 struct hda_gen_spec *spec = codec->spec;
4110 struct auto_pin_cfg *cfg = &spec->autocfg;
4111 hda_nid_t nid = spec->beep_nid;
4112 int err;
4113
967b1307 4114 if (!codec->power_save_node || !nid)
5ccf835c
TI
4115 return 0;
4116 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4117 if (err < 0)
4118 return err;
4119 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4120 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4121 if (err < 0)
4122 return err;
4123 }
4124 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4125 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4126 cfg->speaker_pins);
4127 if (err < 0)
4128 return err;
4129 }
4130 return 0;
4131}
4132
4133/* power up/down beep widget and its output paths */
4134static void beep_power_hook(struct hda_beep *beep, bool on)
4135{
4136 set_path_power(beep->codec, beep->nid, -1, on);
4137}
4138
6b275b14
TI
4139/**
4140 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4141 * @codec: the HDA codec
4142 * @pin: NID of pin to fix
4143 */
4144int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4145{
4146 struct hda_gen_spec *spec = codec->spec;
4147 struct nid_path *path;
4148
4149 path = snd_array_new(&spec->paths);
4150 if (!path)
4151 return -ENOMEM;
4152 memset(path, 0, sizeof(*path));
4153 path->depth = 1;
4154 path->path[0] = pin;
4155 path->active = true;
4156 path->pin_fixed = true;
4157 path->stream_enabled = true;
4158 return 0;
4159}
4160EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4161
352f7f91
TI
4162/*
4163 * Jack detections for HP auto-mute and mic-switch
4164 */
4165
4166/* check each pin in the given array; returns true if any of them is plugged */
4167static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4168{
60ea8ca2
TI
4169 int i;
4170 bool present = false;
352f7f91
TI
4171
4172 for (i = 0; i < num_pins; i++) {
4173 hda_nid_t nid = pins[i];
4174 if (!nid)
4175 break;
0b4df931
TI
4176 /* don't detect pins retasked as inputs */
4177 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4178 continue;
60ea8ca2
TI
4179 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4180 present = true;
352f7f91
TI
4181 }
4182 return present;
4183}
4184
4185/* standard HP/line-out auto-mute helper */
4186static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
e80c60f3 4187 int *paths, bool mute)
352f7f91
TI
4188{
4189 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
4190 int i;
4191
4192 for (i = 0; i < num_pins; i++) {
4193 hda_nid_t nid = pins[i];
967303da 4194 unsigned int val, oldval;
352f7f91
TI
4195 if (!nid)
4196 break;
7eebffd3 4197
e6feb5d0
TI
4198 oldval = snd_hda_codec_get_pin_target(codec, nid);
4199 if (oldval & PIN_IN)
4200 continue; /* no mute for inputs */
4201
7eebffd3 4202 if (spec->auto_mute_via_amp) {
e80c60f3
TI
4203 struct nid_path *path;
4204 hda_nid_t mute_nid;
4205
4206 path = snd_hda_get_path_from_idx(codec, paths[i]);
4207 if (!path)
4208 continue;
4209 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4210 if (!mute_nid)
4211 continue;
7eebffd3 4212 if (mute)
e80c60f3 4213 spec->mute_bits |= (1ULL << mute_nid);
7eebffd3 4214 else
e80c60f3 4215 spec->mute_bits &= ~(1ULL << mute_nid);
7eebffd3 4216 continue;
e6feb5d0
TI
4217 } else {
4218 /* don't reset VREF value in case it's controlling
4219 * the amp (see alc861_fixup_asus_amp_vref_0f())
4220 */
4221 if (spec->keep_vref_in_automute)
4222 val = oldval & ~PIN_HP;
4223 else
4224 val = 0;
4225 if (!mute)
4226 val |= oldval;
4227 /* here we call update_pin_ctl() so that the pinctl is
4228 * changed without changing the pinctl target value;
4229 * the original target value will be still referred at
4230 * the init / resume again
4231 */
4232 update_pin_ctl(codec, nid, val);
7eebffd3
TI
4233 }
4234
d5a9f1bb 4235 set_pin_eapd(codec, nid, !mute);
967b1307 4236 if (codec->power_save_node) {
e6feb5d0
TI
4237 bool on = !mute;
4238 if (on)
4239 on = snd_hda_jack_detect_state(codec, nid)
4240 != HDA_JACK_NOT_PRESENT;
4241 set_path_power(codec, nid, on, -1);
4242 }
352f7f91
TI
4243 }
4244}
4245
dda42bd0
TI
4246/**
4247 * snd_hda_gen_update_outputs - Toggle outputs muting
4248 * @codec: the HDA codec
4249 *
4250 * Update the mute status of all outputs based on the current jack states.
4251 */
5d550e15 4252void snd_hda_gen_update_outputs(struct hda_codec *codec)
352f7f91
TI
4253{
4254 struct hda_gen_spec *spec = codec->spec;
e80c60f3 4255 int *paths;
352f7f91
TI
4256 int on;
4257
4258 /* Control HP pins/amps depending on master_mute state;
4259 * in general, HP pins/amps control should be enabled in all cases,
4260 * but currently set only for master_mute, just to be safe
4261 */
e80c60f3
TI
4262 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4263 paths = spec->out_paths;
4264 else
4265 paths = spec->hp_paths;
967303da 4266 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
e80c60f3 4267 spec->autocfg.hp_pins, paths, spec->master_mute);
352f7f91
TI
4268
4269 if (!spec->automute_speaker)
4270 on = 0;
4271 else
4272 on = spec->hp_jack_present | spec->line_jack_present;
4273 on |= spec->master_mute;
47b9ddb8 4274 spec->speaker_muted = on;
e80c60f3
TI
4275 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4276 paths = spec->out_paths;
4277 else
4278 paths = spec->speaker_paths;
352f7f91 4279 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
e80c60f3 4280 spec->autocfg.speaker_pins, paths, on);
352f7f91
TI
4281
4282 /* toggle line-out mutes if needed, too */
4283 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4284 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4285 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4286 return;
4287 if (!spec->automute_lo)
4288 on = 0;
4289 else
4290 on = spec->hp_jack_present;
4291 on |= spec->master_mute;
47b9ddb8 4292 spec->line_out_muted = on;
e80c60f3 4293 paths = spec->out_paths;
352f7f91 4294 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
e80c60f3 4295 spec->autocfg.line_out_pins, paths, on);
352f7f91 4296}
2698ea98 4297EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
352f7f91
TI
4298
4299static void call_update_outputs(struct hda_codec *codec)
4300{
4301 struct hda_gen_spec *spec = codec->spec;
4302 if (spec->automute_hook)
4303 spec->automute_hook(codec);
4304 else
5d550e15 4305 snd_hda_gen_update_outputs(codec);
7eebffd3
TI
4306
4307 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4308 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4309 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
352f7f91
TI
4310}
4311
dda42bd0
TI
4312/**
4313 * snd_hda_gen_hp_automute - standard HP-automute helper
4314 * @codec: the HDA codec
4315 * @jack: jack object, NULL for the whole
4316 */
1a4f69d5
TI
4317void snd_hda_gen_hp_automute(struct hda_codec *codec,
4318 struct hda_jack_callback *jack)
352f7f91
TI
4319{
4320 struct hda_gen_spec *spec = codec->spec;
92603c59
TI
4321 hda_nid_t *pins = spec->autocfg.hp_pins;
4322 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4323
4324 /* No detection for the first HP jack during indep-HP mode */
4325 if (spec->indep_hp_enabled) {
4326 pins++;
4327 num_pins--;
4328 }
352f7f91 4329
92603c59 4330 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
352f7f91
TI
4331 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4332 return;
4333 call_update_outputs(codec);
4334}
2698ea98 4335EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
352f7f91 4336
dda42bd0
TI
4337/**
4338 * snd_hda_gen_line_automute - standard line-out-automute helper
4339 * @codec: the HDA codec
4340 * @jack: jack object, NULL for the whole
4341 */
1a4f69d5
TI
4342void snd_hda_gen_line_automute(struct hda_codec *codec,
4343 struct hda_jack_callback *jack)
352f7f91
TI
4344{
4345 struct hda_gen_spec *spec = codec->spec;
4346
4347 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4348 return;
4349 /* check LO jack only when it's different from HP */
4350 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4351 return;
4352
4353 spec->line_jack_present =
4354 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4355 spec->autocfg.line_out_pins);
4356 if (!spec->automute_speaker || !spec->detect_lo)
4357 return;
4358 call_update_outputs(codec);
4359}
2698ea98 4360EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
352f7f91 4361
dda42bd0
TI
4362/**
4363 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4364 * @codec: the HDA codec
4365 * @jack: jack object, NULL for the whole
4366 */
1a4f69d5
TI
4367void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4368 struct hda_jack_callback *jack)
352f7f91
TI
4369{
4370 struct hda_gen_spec *spec = codec->spec;
4371 int i;
4372
4373 if (!spec->auto_mic)
4374 return;
4375
4376 for (i = spec->am_num_entries - 1; i > 0; i--) {
0b4df931
TI
4377 hda_nid_t pin = spec->am_entry[i].pin;
4378 /* don't detect pins retasked as outputs */
4379 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4380 continue;
60ea8ca2 4381 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
352f7f91
TI
4382 mux_select(codec, 0, spec->am_entry[i].idx);
4383 return;
4384 }
4385 }
4386 mux_select(codec, 0, spec->am_entry[0].idx);
4387}
2698ea98 4388EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
352f7f91 4389
77afe0e9 4390/* call appropriate hooks */
1a4f69d5
TI
4391static void call_hp_automute(struct hda_codec *codec,
4392 struct hda_jack_callback *jack)
77afe0e9
TI
4393{
4394 struct hda_gen_spec *spec = codec->spec;
4395 if (spec->hp_automute_hook)
4396 spec->hp_automute_hook(codec, jack);
4397 else
4398 snd_hda_gen_hp_automute(codec, jack);
4399}
4400
4401static void call_line_automute(struct hda_codec *codec,
1a4f69d5 4402 struct hda_jack_callback *jack)
77afe0e9
TI
4403{
4404 struct hda_gen_spec *spec = codec->spec;
4405 if (spec->line_automute_hook)
4406 spec->line_automute_hook(codec, jack);
4407 else
4408 snd_hda_gen_line_automute(codec, jack);
4409}
4410
4411static void call_mic_autoswitch(struct hda_codec *codec,
1a4f69d5 4412 struct hda_jack_callback *jack)
77afe0e9
TI
4413{
4414 struct hda_gen_spec *spec = codec->spec;
4415 if (spec->mic_autoswitch_hook)
4416 spec->mic_autoswitch_hook(codec, jack);
4417 else
4418 snd_hda_gen_mic_autoswitch(codec, jack);
4419}
4420
963afde9
TI
4421/* update jack retasking */
4422static void update_automute_all(struct hda_codec *codec)
4423{
4424 call_hp_automute(codec, NULL);
4425 call_line_automute(codec, NULL);
4426 call_mic_autoswitch(codec, NULL);
4427}
4428
352f7f91
TI
4429/*
4430 * Auto-Mute mode mixer enum support
4431 */
4432static int automute_mode_info(struct snd_kcontrol *kcontrol,
4433 struct snd_ctl_elem_info *uinfo)
4434{
4435 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4436 struct hda_gen_spec *spec = codec->spec;
4437 static const char * const texts3[] = {
4438 "Disabled", "Speaker Only", "Line Out+Speaker"
4439 };
4440
4441 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4442 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4443 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4444}
4445
4446static int automute_mode_get(struct snd_kcontrol *kcontrol,
4447 struct snd_ctl_elem_value *ucontrol)
4448{
4449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4450 struct hda_gen_spec *spec = codec->spec;
4451 unsigned int val = 0;
4452 if (spec->automute_speaker)
4453 val++;
4454 if (spec->automute_lo)
4455 val++;
4456
4457 ucontrol->value.enumerated.item[0] = val;
4458 return 0;
4459}
4460
4461static int automute_mode_put(struct snd_kcontrol *kcontrol,
4462 struct snd_ctl_elem_value *ucontrol)
4463{
4464 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4465 struct hda_gen_spec *spec = codec->spec;
4466
4467 switch (ucontrol->value.enumerated.item[0]) {
4468 case 0:
4469 if (!spec->automute_speaker && !spec->automute_lo)
4470 return 0;
4471 spec->automute_speaker = 0;
4472 spec->automute_lo = 0;
4473 break;
4474 case 1:
4475 if (spec->automute_speaker_possible) {
4476 if (!spec->automute_lo && spec->automute_speaker)
4477 return 0;
4478 spec->automute_speaker = 1;
4479 spec->automute_lo = 0;
4480 } else if (spec->automute_lo_possible) {
4481 if (spec->automute_lo)
4482 return 0;
4483 spec->automute_lo = 1;
4484 } else
4485 return -EINVAL;
4486 break;
4487 case 2:
4488 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4489 return -EINVAL;
4490 if (spec->automute_speaker && spec->automute_lo)
4491 return 0;
4492 spec->automute_speaker = 1;
4493 spec->automute_lo = 1;
4494 break;
4495 default:
4496 return -EINVAL;
4497 }
4498 call_update_outputs(codec);
4499 return 1;
4500}
4501
4502static const struct snd_kcontrol_new automute_mode_enum = {
4503 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4504 .name = "Auto-Mute Mode",
4505 .info = automute_mode_info,
4506 .get = automute_mode_get,
4507 .put = automute_mode_put,
4508};
4509
4510static int add_automute_mode_enum(struct hda_codec *codec)
4511{
4512 struct hda_gen_spec *spec = codec->spec;
4513
12c93df6 4514 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
352f7f91
TI
4515 return -ENOMEM;
4516 return 0;
4517}
4518
4519/*
4520 * Check the availability of HP/line-out auto-mute;
4521 * Set up appropriately if really supported
4522 */
4523static int check_auto_mute_availability(struct hda_codec *codec)
4524{
4525 struct hda_gen_spec *spec = codec->spec;
4526 struct auto_pin_cfg *cfg = &spec->autocfg;
4527 int present = 0;
4528 int i, err;
4529
f72706be
TI
4530 if (spec->suppress_auto_mute)
4531 return 0;
4532
352f7f91
TI
4533 if (cfg->hp_pins[0])
4534 present++;
4535 if (cfg->line_out_pins[0])
4536 present++;
4537 if (cfg->speaker_pins[0])
4538 present++;
4539 if (present < 2) /* need two different output types */
4540 return 0;
4541
4542 if (!cfg->speaker_pins[0] &&
4543 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4544 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4545 sizeof(cfg->speaker_pins));
4546 cfg->speaker_outs = cfg->line_outs;
4547 }
4548
4549 if (!cfg->hp_pins[0] &&
4550 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4551 memcpy(cfg->hp_pins, cfg->line_out_pins,
4552 sizeof(cfg->hp_pins));
4553 cfg->hp_outs = cfg->line_outs;
4554 }
4555
4556 for (i = 0; i < cfg->hp_outs; i++) {
4557 hda_nid_t nid = cfg->hp_pins[i];
4558 if (!is_jack_detectable(codec, nid))
4559 continue;
4e76a883 4560 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
62f949bf 4561 snd_hda_jack_detect_enable_callback(codec, nid,
77afe0e9 4562 call_hp_automute);
352f7f91
TI
4563 spec->detect_hp = 1;
4564 }
4565
4566 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4567 if (cfg->speaker_outs)
4568 for (i = 0; i < cfg->line_outs; i++) {
4569 hda_nid_t nid = cfg->line_out_pins[i];
4570 if (!is_jack_detectable(codec, nid))
4571 continue;
4e76a883 4572 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
352f7f91 4573 snd_hda_jack_detect_enable_callback(codec, nid,
77afe0e9 4574 call_line_automute);
352f7f91
TI
4575 spec->detect_lo = 1;
4576 }
4577 spec->automute_lo_possible = spec->detect_hp;
4578 }
4579
4580 spec->automute_speaker_possible = cfg->speaker_outs &&
4581 (spec->detect_hp || spec->detect_lo);
4582
4583 spec->automute_lo = spec->automute_lo_possible;
4584 spec->automute_speaker = spec->automute_speaker_possible;
4585
4586 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4587 /* create a control for automute mode */
4588 err = add_automute_mode_enum(codec);
4589 if (err < 0)
4590 return err;
4591 }
4592 return 0;
4593}
352f7f91
TI
4594
4595/* check whether all auto-mic pins are valid; setup indices if OK */
4596static bool auto_mic_check_imux(struct hda_codec *codec)
4597{
4598 struct hda_gen_spec *spec = codec->spec;
4599 const struct hda_input_mux *imux;
4600 int i;
4601
4602 imux = &spec->input_mux;
4603 for (i = 0; i < spec->am_num_entries; i++) {
4604 spec->am_entry[i].idx =
4605 find_idx_in_nid_list(spec->am_entry[i].pin,
4606 spec->imux_pins, imux->num_items);
4607 if (spec->am_entry[i].idx < 0)
4608 return false; /* no corresponding imux */
4609 }
4610
4611 /* we don't need the jack detection for the first pin */
4612 for (i = 1; i < spec->am_num_entries; i++)
4613 snd_hda_jack_detect_enable_callback(codec,
4614 spec->am_entry[i].pin,
77afe0e9 4615 call_mic_autoswitch);
352f7f91
TI
4616 return true;
4617}
4618
4619static int compare_attr(const void *ap, const void *bp)
4620{
4621 const struct automic_entry *a = ap;
4622 const struct automic_entry *b = bp;
4623 return (int)(a->attr - b->attr);
4624}
1da177e4
LT
4625
4626/*
352f7f91
TI
4627 * Check the availability of auto-mic switch;
4628 * Set up if really supported
1da177e4 4629 */
352f7f91
TI
4630static int check_auto_mic_availability(struct hda_codec *codec)
4631{
4632 struct hda_gen_spec *spec = codec->spec;
4633 struct auto_pin_cfg *cfg = &spec->autocfg;
4634 unsigned int types;
4635 int i, num_pins;
4636
d12daf6f
TI
4637 if (spec->suppress_auto_mic)
4638 return 0;
4639
352f7f91
TI
4640 types = 0;
4641 num_pins = 0;
4642 for (i = 0; i < cfg->num_inputs; i++) {
4643 hda_nid_t nid = cfg->inputs[i].pin;
4644 unsigned int attr;
4645 attr = snd_hda_codec_get_pincfg(codec, nid);
4646 attr = snd_hda_get_input_pin_attr(attr);
4647 if (types & (1 << attr))
4648 return 0; /* already occupied */
4649 switch (attr) {
4650 case INPUT_PIN_ATTR_INT:
4651 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4652 return 0; /* invalid type */
4653 break;
4654 case INPUT_PIN_ATTR_UNUSED:
4655 return 0; /* invalid entry */
4656 default:
4657 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4658 return 0; /* invalid type */
4659 if (!spec->line_in_auto_switch &&
4660 cfg->inputs[i].type != AUTO_PIN_MIC)
4661 return 0; /* only mic is allowed */
4662 if (!is_jack_detectable(codec, nid))
4663 return 0; /* no unsol support */
4664 break;
4665 }
4666 if (num_pins >= MAX_AUTO_MIC_PINS)
4667 return 0;
4668 types |= (1 << attr);
4669 spec->am_entry[num_pins].pin = nid;
4670 spec->am_entry[num_pins].attr = attr;
4671 num_pins++;
4672 }
4673
4674 if (num_pins < 2)
4675 return 0;
4676
4677 spec->am_num_entries = num_pins;
4678 /* sort the am_entry in the order of attr so that the pin with a
4679 * higher attr will be selected when the jack is plugged.
4680 */
4681 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4682 compare_attr, NULL);
4683
4684 if (!auto_mic_check_imux(codec))
4685 return 0;
4686
4687 spec->auto_mic = 1;
4688 spec->num_adc_nids = 1;
4689 spec->cur_mux[0] = spec->am_entry[0].idx;
4e76a883 4690 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
352f7f91
TI
4691 spec->am_entry[0].pin,
4692 spec->am_entry[1].pin,
4693 spec->am_entry[2].pin);
4694
1da177e4
LT
4695 return 0;
4696}
4697
dda42bd0
TI
4698/**
4699 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4700 * into power down
4701 * @codec: the HDA codec
4702 * @nid: NID to evalute
4703 * @power_state: target power state
4704 */
dfc6e469 4705unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
55196fff
TI
4706 hda_nid_t nid,
4707 unsigned int power_state)
4708{
dfc6e469 4709 if (power_state != AC_PWRST_D0 || nid == codec->afg)
55196fff
TI
4710 return power_state;
4711 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4712 return power_state;
b1b9fbd0 4713 if (is_active_nid_for_any(codec, nid))
55196fff
TI
4714 return power_state;
4715 return AC_PWRST_D3;
4716}
dfc6e469 4717EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
55196fff 4718
ebb93c05
TI
4719/* mute all aamix inputs initially; parse up to the first leaves */
4720static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4721{
4722 int i, nums;
4723 const hda_nid_t *conn;
4724 bool has_amp;
4725
4726 nums = snd_hda_get_conn_list(codec, mix, &conn);
4727 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4728 for (i = 0; i < nums; i++) {
4729 if (has_amp)
ef403edb
TI
4730 update_amp(codec, mix, HDA_INPUT, i,
4731 0xff, HDA_AMP_MUTE);
ebb93c05 4732 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
ef403edb
TI
4733 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4734 0xff, HDA_AMP_MUTE);
ebb93c05
TI
4735 }
4736}
1da177e4 4737
e6feb5d0
TI
4738/**
4739 * snd_hda_gen_stream_pm - Stream power management callback
4740 * @codec: the HDA codec
4741 * @nid: audio widget
4742 * @on: power on/off flag
4743 *
967b1307 4744 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
e6feb5d0
TI
4745 */
4746void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4747{
967b1307 4748 if (codec->power_save_node)
e6feb5d0
TI
4749 set_path_power(codec, nid, -1, on);
4750}
4751EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4752
dda42bd0
TI
4753/**
4754 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4755 * set up the hda_gen_spec
4756 * @codec: the HDA codec
4757 * @cfg: Parsed pin configuration
9eb413e5
TI
4758 *
4759 * return 1 if successful, 0 if the proper config is not found,
352f7f91
TI
4760 * or a negative error code
4761 */
4762int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
9eb413e5 4763 struct auto_pin_cfg *cfg)
352f7f91
TI
4764{
4765 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
4766 int err;
4767
1c70a583
TI
4768 parse_user_hints(codec);
4769
e4a395e7
TI
4770 if (spec->mixer_nid && !spec->mixer_merge_nid)
4771 spec->mixer_merge_nid = spec->mixer_nid;
4772
9eb413e5
TI
4773 if (cfg != &spec->autocfg) {
4774 spec->autocfg = *cfg;
4775 cfg = &spec->autocfg;
4776 }
4777
98bd1115
TI
4778 if (!spec->main_out_badness)
4779 spec->main_out_badness = &hda_main_out_badness;
4780 if (!spec->extra_out_badness)
4781 spec->extra_out_badness = &hda_extra_out_badness;
4782
6fc4cb97
DH
4783 fill_all_dac_nids(codec);
4784
352f7f91
TI
4785 if (!cfg->line_outs) {
4786 if (cfg->dig_outs || cfg->dig_in_pin) {
4787 spec->multiout.max_channels = 2;
4788 spec->no_analog = 1;
4789 goto dig_only;
4790 }
c9e4bdb7
TI
4791 if (!cfg->num_inputs && !cfg->dig_in_pin)
4792 return 0; /* can't find valid BIOS pin config */
352f7f91
TI
4793 }
4794
4795 if (!spec->no_primary_hp &&
4796 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4797 cfg->line_outs <= cfg->hp_outs) {
4798 /* use HP as primary out */
4799 cfg->speaker_outs = cfg->line_outs;
4800 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4801 sizeof(cfg->speaker_pins));
4802 cfg->line_outs = cfg->hp_outs;
4803 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4804 cfg->hp_outs = 0;
4805 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4806 cfg->line_out_type = AUTO_PIN_HP_OUT;
4807 }
4808
4809 err = parse_output_paths(codec);
4810 if (err < 0)
4811 return err;
4812 err = create_multi_channel_mode(codec);
4813 if (err < 0)
4814 return err;
4815 err = create_multi_out_ctls(codec, cfg);
4816 if (err < 0)
4817 return err;
4818 err = create_hp_out_ctls(codec);
4819 if (err < 0)
4820 return err;
4821 err = create_speaker_out_ctls(codec);
38cf6f1a
TI
4822 if (err < 0)
4823 return err;
4824 err = create_indep_hp_ctls(codec);
c30aa7b2
TI
4825 if (err < 0)
4826 return err;
4827 err = create_loopback_mixing_ctl(codec);
352f7f91
TI
4828 if (err < 0)
4829 return err;
967303da 4830 err = create_hp_mic(codec);
352f7f91
TI
4831 if (err < 0)
4832 return err;
4833 err = create_input_ctls(codec);
4834 if (err < 0)
4835 return err;
4836
e6feb5d0
TI
4837 /* add power-down pin callbacks at first */
4838 add_all_pin_power_ctls(codec, false);
4839
a07a949b
TI
4840 spec->const_channel_count = spec->ext_channel_count;
4841 /* check the multiple speaker and headphone pins */
4842 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4843 spec->const_channel_count = max(spec->const_channel_count,
4844 cfg->speaker_outs * 2);
4845 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4846 spec->const_channel_count = max(spec->const_channel_count,
4847 cfg->hp_outs * 2);
4848 spec->multiout.max_channels = max(spec->ext_channel_count,
4849 spec->const_channel_count);
352f7f91
TI
4850
4851 err = check_auto_mute_availability(codec);
4852 if (err < 0)
4853 return err;
4854
4855 err = check_dyn_adc_switch(codec);
4856 if (err < 0)
4857 return err;
4858
967303da
TI
4859 err = check_auto_mic_availability(codec);
4860 if (err < 0)
4861 return err;
1da177e4 4862
f1e762dd
TI
4863 /* add stereo mix if available and not enabled yet */
4864 if (!spec->auto_mic && spec->mixer_nid &&
74f14b36
TI
4865 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4866 spec->input_mux.num_items > 1) {
f1e762dd
TI
4867 err = parse_capture_source(codec, spec->mixer_nid,
4868 CFG_IDX_MIX, spec->num_all_adcs,
4869 "Stereo Mix", 0);
4870 if (err < 0)
4871 return err;
4872 }
4873
4874
352f7f91
TI
4875 err = create_capture_mixers(codec);
4876 if (err < 0)
4877 return err;
a7da6ce5 4878
352f7f91
TI
4879 err = parse_mic_boost(codec);
4880 if (err < 0)
4881 return err;
4882
ced4cefc
TI
4883 /* create "Headphone Mic Jack Mode" if no input selection is
4884 * available (or user specifies add_jack_modes hint)
4885 */
4886 if (spec->hp_mic_pin &&
4887 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4888 spec->add_jack_modes)) {
4889 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4890 if (err < 0)
4891 return err;
4892 }
4893
f811c3cf 4894 if (spec->add_jack_modes) {
978e77e7
TI
4895 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4896 err = create_out_jack_modes(codec, cfg->line_outs,
4897 cfg->line_out_pins);
4898 if (err < 0)
4899 return err;
4900 }
4901 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4902 err = create_out_jack_modes(codec, cfg->hp_outs,
4903 cfg->hp_pins);
4904 if (err < 0)
4905 return err;
4906 }
4907 }
4908
e6feb5d0
TI
4909 /* add power-up pin callbacks at last */
4910 add_all_pin_power_ctls(codec, true);
4911
ebb93c05
TI
4912 /* mute all aamix input initially */
4913 if (spec->mixer_nid)
4914 mute_all_mixer_nid(codec, spec->mixer_nid);
4915
352f7f91
TI
4916 dig_only:
4917 parse_digital(codec);
4918
967b1307 4919 if (spec->power_down_unused || codec->power_save_node)
55196fff
TI
4920 codec->power_filter = snd_hda_gen_path_power_filter;
4921
7504b6cd
TI
4922 if (!spec->no_analog && spec->beep_nid) {
4923 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4924 if (err < 0)
4925 return err;
967b1307 4926 if (codec->beep && codec->power_save_node) {
5ccf835c
TI
4927 err = add_fake_beep_paths(codec);
4928 if (err < 0)
4929 return err;
4930 codec->beep->power_hook = beep_power_hook;
4931 }
7504b6cd
TI
4932 }
4933
352f7f91 4934 return 1;
a7da6ce5 4935}
2698ea98 4936EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
a7da6ce5 4937
071c73ad 4938
352f7f91
TI
4939/*
4940 * Build control elements
4941 */
4942
4943/* slave controls for virtual master */
4944static const char * const slave_pfxs[] = {
4945 "Front", "Surround", "Center", "LFE", "Side",
4946 "Headphone", "Speaker", "Mono", "Line Out",
4947 "CLFE", "Bass Speaker", "PCM",
ee79c69a
TI
4948 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4949 "Headphone Front", "Headphone Surround", "Headphone CLFE",
03ad6a8c 4950 "Headphone Side", "Headphone+LO", "Speaker+LO",
352f7f91
TI
4951 NULL,
4952};
4953
dda42bd0
TI
4954/**
4955 * snd_hda_gen_build_controls - Build controls from the parsed results
4956 * @codec: the HDA codec
4957 *
4958 * Pass this to build_controls patch_ops.
4959 */
352f7f91
TI
4960int snd_hda_gen_build_controls(struct hda_codec *codec)
4961{
4962 struct hda_gen_spec *spec = codec->spec;
4963 int err;
1da177e4 4964
36502d02
TI
4965 if (spec->kctls.used) {
4966 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4967 if (err < 0)
4968 return err;
4969 }
071c73ad 4970
352f7f91
TI
4971 if (spec->multiout.dig_out_nid) {
4972 err = snd_hda_create_dig_out_ctls(codec,
4973 spec->multiout.dig_out_nid,
4974 spec->multiout.dig_out_nid,
bbbc7e85 4975 spec->pcm_rec[1]->pcm_type);
352f7f91
TI
4976 if (err < 0)
4977 return err;
4978 if (!spec->no_analog) {
4979 err = snd_hda_create_spdif_share_sw(codec,
4980 &spec->multiout);
4981 if (err < 0)
4982 return err;
4983 spec->multiout.share_spdif = 1;
4984 }
4985 }
4986 if (spec->dig_in_nid) {
4987 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
071c73ad
TI
4988 if (err < 0)
4989 return err;
071c73ad 4990 }
1da177e4 4991
352f7f91
TI
4992 /* if we have no master control, let's create it */
4993 if (!spec->no_analog &&
4994 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
352f7f91 4995 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
7a71bbf3 4996 spec->vmaster_tlv, slave_pfxs,
352f7f91
TI
4997 "Playback Volume");
4998 if (err < 0)
4999 return err;
5000 }
5001 if (!spec->no_analog &&
5002 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5003 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5004 NULL, slave_pfxs,
5005 "Playback Switch",
5006 true, &spec->vmaster_mute.sw_kctl);
5007 if (err < 0)
5008 return err;
b63eae0a 5009 if (spec->vmaster_mute.hook) {
fd25a97a
TI
5010 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5011 spec->vmaster_mute_enum);
b63eae0a
TI
5012 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5013 }
352f7f91 5014 }
071c73ad 5015
352f7f91 5016 free_kctls(spec); /* no longer needed */
071c73ad 5017
352f7f91
TI
5018 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5019 if (err < 0)
5020 return err;
5021
1da177e4
LT
5022 return 0;
5023}
2698ea98 5024EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
1da177e4
LT
5025
5026
5027/*
352f7f91 5028 * PCM definitions
1da177e4 5029 */
1da177e4 5030
e6b85f3c
TI
5031static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5032 struct hda_codec *codec,
5033 struct snd_pcm_substream *substream,
5034 int action)
5035{
5036 struct hda_gen_spec *spec = codec->spec;
5037 if (spec->pcm_playback_hook)
5038 spec->pcm_playback_hook(hinfo, codec, substream, action);
5039}
5040
ac2e8736
TI
5041static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5042 struct hda_codec *codec,
5043 struct snd_pcm_substream *substream,
5044 int action)
5045{
5046 struct hda_gen_spec *spec = codec->spec;
5047 if (spec->pcm_capture_hook)
5048 spec->pcm_capture_hook(hinfo, codec, substream, action);
5049}
5050
352f7f91
TI
5051/*
5052 * Analog playback callbacks
5053 */
5054static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5055 struct hda_codec *codec,
5056 struct snd_pcm_substream *substream)
5057{
5058 struct hda_gen_spec *spec = codec->spec;
38cf6f1a
TI
5059 int err;
5060
5061 mutex_lock(&spec->pcm_mutex);
5062 err = snd_hda_multi_out_analog_open(codec,
5063 &spec->multiout, substream,
352f7f91 5064 hinfo);
e6b85f3c 5065 if (!err) {
38cf6f1a 5066 spec->active_streams |= 1 << STREAM_MULTI_OUT;
e6b85f3c
TI
5067 call_pcm_playback_hook(hinfo, codec, substream,
5068 HDA_GEN_PCM_ACT_OPEN);
5069 }
38cf6f1a
TI
5070 mutex_unlock(&spec->pcm_mutex);
5071 return err;
352f7f91 5072}
1da177e4 5073
352f7f91
TI
5074static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5075 struct hda_codec *codec,
5076 unsigned int stream_tag,
5077 unsigned int format,
5078 struct snd_pcm_substream *substream)
5079{
5080 struct hda_gen_spec *spec = codec->spec;
e6b85f3c
TI
5081 int err;
5082
5083 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5084 stream_tag, format, substream);
5085 if (!err)
5086 call_pcm_playback_hook(hinfo, codec, substream,
5087 HDA_GEN_PCM_ACT_PREPARE);
5088 return err;
352f7f91 5089}
1da177e4 5090
352f7f91
TI
5091static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5092 struct hda_codec *codec,
5093 struct snd_pcm_substream *substream)
5094{
5095 struct hda_gen_spec *spec = codec->spec;
e6b85f3c
TI
5096 int err;
5097
5098 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5099 if (!err)
5100 call_pcm_playback_hook(hinfo, codec, substream,
5101 HDA_GEN_PCM_ACT_CLEANUP);
5102 return err;
1da177e4
LT
5103}
5104
38cf6f1a
TI
5105static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5106 struct hda_codec *codec,
5107 struct snd_pcm_substream *substream)
5108{
5109 struct hda_gen_spec *spec = codec->spec;
5110 mutex_lock(&spec->pcm_mutex);
5111 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
e6b85f3c
TI
5112 call_pcm_playback_hook(hinfo, codec, substream,
5113 HDA_GEN_PCM_ACT_CLOSE);
38cf6f1a
TI
5114 mutex_unlock(&spec->pcm_mutex);
5115 return 0;
5116}
5117
ac2e8736
TI
5118static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5119 struct hda_codec *codec,
5120 struct snd_pcm_substream *substream)
5121{
5122 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5123 return 0;
5124}
5125
5126static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5127 struct hda_codec *codec,
5128 unsigned int stream_tag,
5129 unsigned int format,
5130 struct snd_pcm_substream *substream)
5131{
5132 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5133 call_pcm_capture_hook(hinfo, codec, substream,
5134 HDA_GEN_PCM_ACT_PREPARE);
5135 return 0;
5136}
5137
5138static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5139 struct hda_codec *codec,
5140 struct snd_pcm_substream *substream)
5141{
5142 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5143 call_pcm_capture_hook(hinfo, codec, substream,
5144 HDA_GEN_PCM_ACT_CLEANUP);
5145 return 0;
5146}
5147
5148static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5149 struct hda_codec *codec,
5150 struct snd_pcm_substream *substream)
5151{
5152 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5153 return 0;
5154}
5155
38cf6f1a
TI
5156static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5157 struct hda_codec *codec,
5158 struct snd_pcm_substream *substream)
5159{
5160 struct hda_gen_spec *spec = codec->spec;
5161 int err = 0;
5162
5163 mutex_lock(&spec->pcm_mutex);
5164 if (!spec->indep_hp_enabled)
5165 err = -EBUSY;
5166 else
5167 spec->active_streams |= 1 << STREAM_INDEP_HP;
e6b85f3c
TI
5168 call_pcm_playback_hook(hinfo, codec, substream,
5169 HDA_GEN_PCM_ACT_OPEN);
38cf6f1a
TI
5170 mutex_unlock(&spec->pcm_mutex);
5171 return err;
5172}
5173
5174static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5175 struct hda_codec *codec,
5176 struct snd_pcm_substream *substream)
5177{
5178 struct hda_gen_spec *spec = codec->spec;
5179 mutex_lock(&spec->pcm_mutex);
5180 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
e6b85f3c
TI
5181 call_pcm_playback_hook(hinfo, codec, substream,
5182 HDA_GEN_PCM_ACT_CLOSE);
38cf6f1a
TI
5183 mutex_unlock(&spec->pcm_mutex);
5184 return 0;
5185}
5186
e6b85f3c
TI
5187static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5188 struct hda_codec *codec,
5189 unsigned int stream_tag,
5190 unsigned int format,
5191 struct snd_pcm_substream *substream)
5192{
5193 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5194 call_pcm_playback_hook(hinfo, codec, substream,
5195 HDA_GEN_PCM_ACT_PREPARE);
5196 return 0;
5197}
5198
5199static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5200 struct hda_codec *codec,
5201 struct snd_pcm_substream *substream)
5202{
5203 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5204 call_pcm_playback_hook(hinfo, codec, substream,
5205 HDA_GEN_PCM_ACT_CLEANUP);
5206 return 0;
5207}
5208
1da177e4 5209/*
352f7f91 5210 * Digital out
1da177e4 5211 */
352f7f91
TI
5212static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5213 struct hda_codec *codec,
5214 struct snd_pcm_substream *substream)
1da177e4 5215{
352f7f91
TI
5216 struct hda_gen_spec *spec = codec->spec;
5217 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5218}
1da177e4 5219
352f7f91
TI
5220static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5221 struct hda_codec *codec,
5222 unsigned int stream_tag,
5223 unsigned int format,
5224 struct snd_pcm_substream *substream)
5225{
5226 struct hda_gen_spec *spec = codec->spec;
5227 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5228 stream_tag, format, substream);
5229}
1da177e4 5230
352f7f91
TI
5231static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5232 struct hda_codec *codec,
5233 struct snd_pcm_substream *substream)
5234{
5235 struct hda_gen_spec *spec = codec->spec;
5236 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5237}
5238
5239static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5240 struct hda_codec *codec,
5241 struct snd_pcm_substream *substream)
5242{
5243 struct hda_gen_spec *spec = codec->spec;
5244 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1da177e4
LT
5245}
5246
5247/*
352f7f91 5248 * Analog capture
1da177e4 5249 */
ac2e8736
TI
5250#define alt_capture_pcm_open capture_pcm_open
5251#define alt_capture_pcm_close capture_pcm_close
5252
352f7f91
TI
5253static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5254 struct hda_codec *codec,
5255 unsigned int stream_tag,
5256 unsigned int format,
5257 struct snd_pcm_substream *substream)
1da177e4 5258{
352f7f91 5259 struct hda_gen_spec *spec = codec->spec;
1da177e4 5260
352f7f91
TI
5261 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5262 stream_tag, 0, format);
ac2e8736
TI
5263 call_pcm_capture_hook(hinfo, codec, substream,
5264 HDA_GEN_PCM_ACT_PREPARE);
352f7f91
TI
5265 return 0;
5266}
5267
5268static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5269 struct hda_codec *codec,
5270 struct snd_pcm_substream *substream)
5271{
5272 struct hda_gen_spec *spec = codec->spec;
1da177e4 5273
352f7f91
TI
5274 snd_hda_codec_cleanup_stream(codec,
5275 spec->adc_nids[substream->number + 1]);
ac2e8736
TI
5276 call_pcm_capture_hook(hinfo, codec, substream,
5277 HDA_GEN_PCM_ACT_CLEANUP);
1da177e4
LT
5278 return 0;
5279}
5280
5281/*
1da177e4 5282 */
352f7f91
TI
5283static const struct hda_pcm_stream pcm_analog_playback = {
5284 .substreams = 1,
5285 .channels_min = 2,
5286 .channels_max = 8,
5287 /* NID is set in build_pcms */
5288 .ops = {
5289 .open = playback_pcm_open,
38cf6f1a 5290 .close = playback_pcm_close,
352f7f91
TI
5291 .prepare = playback_pcm_prepare,
5292 .cleanup = playback_pcm_cleanup
5293 },
5294};
5295
5296static const struct hda_pcm_stream pcm_analog_capture = {
1da177e4
LT
5297 .substreams = 1,
5298 .channels_min = 2,
5299 .channels_max = 2,
352f7f91 5300 /* NID is set in build_pcms */
ac2e8736
TI
5301 .ops = {
5302 .open = capture_pcm_open,
5303 .close = capture_pcm_close,
5304 .prepare = capture_pcm_prepare,
5305 .cleanup = capture_pcm_cleanup
5306 },
1da177e4
LT
5307};
5308
352f7f91
TI
5309static const struct hda_pcm_stream pcm_analog_alt_playback = {
5310 .substreams = 1,
5311 .channels_min = 2,
5312 .channels_max = 2,
5313 /* NID is set in build_pcms */
38cf6f1a
TI
5314 .ops = {
5315 .open = alt_playback_pcm_open,
e6b85f3c
TI
5316 .close = alt_playback_pcm_close,
5317 .prepare = alt_playback_pcm_prepare,
5318 .cleanup = alt_playback_pcm_cleanup
38cf6f1a 5319 },
352f7f91
TI
5320};
5321
5322static const struct hda_pcm_stream pcm_analog_alt_capture = {
5323 .substreams = 2, /* can be overridden */
5324 .channels_min = 2,
5325 .channels_max = 2,
5326 /* NID is set in build_pcms */
5327 .ops = {
ac2e8736
TI
5328 .open = alt_capture_pcm_open,
5329 .close = alt_capture_pcm_close,
352f7f91
TI
5330 .prepare = alt_capture_pcm_prepare,
5331 .cleanup = alt_capture_pcm_cleanup
5332 },
5333};
5334
5335static const struct hda_pcm_stream pcm_digital_playback = {
5336 .substreams = 1,
5337 .channels_min = 2,
5338 .channels_max = 2,
5339 /* NID is set in build_pcms */
5340 .ops = {
5341 .open = dig_playback_pcm_open,
5342 .close = dig_playback_pcm_close,
5343 .prepare = dig_playback_pcm_prepare,
5344 .cleanup = dig_playback_pcm_cleanup
5345 },
5346};
5347
5348static const struct hda_pcm_stream pcm_digital_capture = {
5349 .substreams = 1,
5350 .channels_min = 2,
5351 .channels_max = 2,
5352 /* NID is set in build_pcms */
5353};
5354
5355/* Used by build_pcms to flag that a PCM has no playback stream */
5356static const struct hda_pcm_stream pcm_null_stream = {
5357 .substreams = 0,
5358 .channels_min = 0,
5359 .channels_max = 0,
5360};
5361
5362/*
5363 * dynamic changing ADC PCM streams
5364 */
5365static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
97ec558a 5366{
352f7f91
TI
5367 struct hda_gen_spec *spec = codec->spec;
5368 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5369
5370 if (spec->cur_adc && spec->cur_adc != new_adc) {
5371 /* stream is running, let's swap the current ADC */
5372 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5373 spec->cur_adc = new_adc;
5374 snd_hda_codec_setup_stream(codec, new_adc,
5375 spec->cur_adc_stream_tag, 0,
5376 spec->cur_adc_format);
5377 return true;
5378 }
5379 return false;
5380}
97ec558a 5381
352f7f91
TI
5382/* analog capture with dynamic dual-adc changes */
5383static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5384 struct hda_codec *codec,
5385 unsigned int stream_tag,
5386 unsigned int format,
5387 struct snd_pcm_substream *substream)
5388{
5389 struct hda_gen_spec *spec = codec->spec;
5390 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5391 spec->cur_adc_stream_tag = stream_tag;
5392 spec->cur_adc_format = format;
5393 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
97ec558a
TI
5394 return 0;
5395}
5396
352f7f91
TI
5397static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5398 struct hda_codec *codec,
5399 struct snd_pcm_substream *substream)
97ec558a 5400{
352f7f91
TI
5401 struct hda_gen_spec *spec = codec->spec;
5402 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5403 spec->cur_adc = 0;
97ec558a
TI
5404 return 0;
5405}
5406
352f7f91
TI
5407static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5408 .substreams = 1,
5409 .channels_min = 2,
5410 .channels_max = 2,
5411 .nid = 0, /* fill later */
5412 .ops = {
5413 .prepare = dyn_adc_capture_pcm_prepare,
5414 .cleanup = dyn_adc_capture_pcm_cleanup
5415 },
5416};
5417
f873e536
TI
5418static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5419 const char *chip_name)
5420{
5421 char *p;
5422
5423 if (*str)
5424 return;
5425 strlcpy(str, chip_name, len);
5426
5427 /* drop non-alnum chars after a space */
5428 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5429 if (!isalnum(p[1])) {
5430 *p = 0;
5431 break;
5432 }
5433 }
5434 strlcat(str, sfx, len);
5435}
5436
fb83b635
TI
5437/* copy PCM stream info from @default_str, and override non-NULL entries
5438 * from @spec_str and @nid
5439 */
5440static void setup_pcm_stream(struct hda_pcm_stream *str,
5441 const struct hda_pcm_stream *default_str,
5442 const struct hda_pcm_stream *spec_str,
5443 hda_nid_t nid)
5444{
5445 *str = *default_str;
5446 if (nid)
5447 str->nid = nid;
5448 if (spec_str) {
5449 if (spec_str->substreams)
5450 str->substreams = spec_str->substreams;
5451 if (spec_str->channels_min)
5452 str->channels_min = spec_str->channels_min;
5453 if (spec_str->channels_max)
5454 str->channels_max = spec_str->channels_max;
5455 if (spec_str->rates)
5456 str->rates = spec_str->rates;
5457 if (spec_str->formats)
5458 str->formats = spec_str->formats;
5459 if (spec_str->maxbps)
5460 str->maxbps = spec_str->maxbps;
5461 }
5462}
5463
dda42bd0
TI
5464/**
5465 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5466 * @codec: the HDA codec
5467 *
5468 * Pass this to build_pcms patch_ops.
5469 */
352f7f91 5470int snd_hda_gen_build_pcms(struct hda_codec *codec)
1da177e4 5471{
352f7f91 5472 struct hda_gen_spec *spec = codec->spec;
bbbc7e85 5473 struct hda_pcm *info;
352f7f91 5474 bool have_multi_adcs;
352f7f91 5475
352f7f91
TI
5476 if (spec->no_analog)
5477 goto skip_analog;
5478
f873e536
TI
5479 fill_pcm_stream_name(spec->stream_name_analog,
5480 sizeof(spec->stream_name_analog),
5481 " Analog", codec->chip_name);
bbbc7e85
TI
5482 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5483 if (!info)
5484 return -ENOMEM;
5485 spec->pcm_rec[0] = info;
352f7f91
TI
5486
5487 if (spec->multiout.num_dacs > 0) {
fb83b635
TI
5488 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5489 &pcm_analog_playback,
5490 spec->stream_analog_playback,
5491 spec->multiout.dac_nids[0]);
352f7f91
TI
5492 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5493 spec->multiout.max_channels;
5494 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5495 spec->autocfg.line_outs == 2)
5496 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5497 snd_pcm_2_1_chmaps;
5498 }
5499 if (spec->num_adc_nids) {
fb83b635
TI
5500 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5501 (spec->dyn_adc_switch ?
5502 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5503 spec->stream_analog_capture,
5504 spec->adc_nids[0]);
352f7f91
TI
5505 }
5506
352f7f91
TI
5507 skip_analog:
5508 /* SPDIF for stream index #1 */
5509 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
f873e536
TI
5510 fill_pcm_stream_name(spec->stream_name_digital,
5511 sizeof(spec->stream_name_digital),
5512 " Digital", codec->chip_name);
bbbc7e85
TI
5513 info = snd_hda_codec_pcm_new(codec, "%s",
5514 spec->stream_name_digital);
5515 if (!info)
5516 return -ENOMEM;
352f7f91 5517 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
bbbc7e85 5518 spec->pcm_rec[1] = info;
352f7f91
TI
5519 if (spec->dig_out_type)
5520 info->pcm_type = spec->dig_out_type;
5521 else
5522 info->pcm_type = HDA_PCM_TYPE_SPDIF;
fb83b635
TI
5523 if (spec->multiout.dig_out_nid)
5524 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5525 &pcm_digital_playback,
5526 spec->stream_digital_playback,
5527 spec->multiout.dig_out_nid);
5528 if (spec->dig_in_nid)
5529 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5530 &pcm_digital_capture,
5531 spec->stream_digital_capture,
5532 spec->dig_in_nid);
352f7f91 5533 }
1da177e4 5534
352f7f91 5535 if (spec->no_analog)
1da177e4 5536 return 0;
352f7f91
TI
5537
5538 /* If the use of more than one ADC is requested for the current
5539 * model, configure a second analog capture-only PCM.
5540 */
5541 have_multi_adcs = (spec->num_adc_nids > 1) &&
5542 !spec->dyn_adc_switch && !spec->auto_mic;
5543 /* Additional Analaog capture for index #2 */
5544 if (spec->alt_dac_nid || have_multi_adcs) {
a607148f
TI
5545 fill_pcm_stream_name(spec->stream_name_alt_analog,
5546 sizeof(spec->stream_name_alt_analog),
5547 " Alt Analog", codec->chip_name);
bbbc7e85
TI
5548 info = snd_hda_codec_pcm_new(codec, "%s",
5549 spec->stream_name_alt_analog);
5550 if (!info)
5551 return -ENOMEM;
5552 spec->pcm_rec[2] = info;
fb83b635
TI
5553 if (spec->alt_dac_nid)
5554 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5555 &pcm_analog_alt_playback,
5556 spec->stream_analog_alt_playback,
5557 spec->alt_dac_nid);
5558 else
5559 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5560 &pcm_null_stream, NULL, 0);
352f7f91 5561 if (have_multi_adcs) {
fb83b635
TI
5562 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5563 &pcm_analog_alt_capture,
5564 spec->stream_analog_alt_capture,
5565 spec->adc_nids[1]);
352f7f91
TI
5566 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5567 spec->num_adc_nids - 1;
5568 } else {
fb83b635
TI
5569 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5570 &pcm_null_stream, NULL, 0);
352f7f91 5571 }
1da177e4
LT
5572 }
5573
352f7f91
TI
5574 return 0;
5575}
2698ea98 5576EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
352f7f91
TI
5577
5578
5579/*
5580 * Standard auto-parser initializations
5581 */
5582
d4156930 5583/* configure the given path as a proper output */
2c12c30d 5584static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
352f7f91
TI
5585{
5586 struct nid_path *path;
d4156930 5587 hda_nid_t pin;
352f7f91 5588
196c1766 5589 path = snd_hda_get_path_from_idx(codec, path_idx);
d4156930 5590 if (!path || !path->depth)
352f7f91 5591 return;
d4156930 5592 pin = path->path[path->depth - 1];
2c12c30d 5593 restore_pin_ctl(codec, pin);
65033cc8
TI
5594 snd_hda_activate_path(codec, path, path->active,
5595 aamix_default(codec->spec));
e1284af7 5596 set_pin_eapd(codec, pin, path->active);
352f7f91
TI
5597}
5598
5599/* initialize primary output paths */
5600static void init_multi_out(struct hda_codec *codec)
5601{
5602 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
5603 int i;
5604
d4156930 5605 for (i = 0; i < spec->autocfg.line_outs; i++)
2c12c30d 5606 set_output_and_unmute(codec, spec->out_paths[i]);
352f7f91
TI
5607}
5608
db23fd19 5609
2c12c30d 5610static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
352f7f91 5611{
352f7f91 5612 int i;
352f7f91 5613
d4156930 5614 for (i = 0; i < num_outs; i++)
2c12c30d 5615 set_output_and_unmute(codec, paths[i]);
352f7f91
TI
5616}
5617
db23fd19
TI
5618/* initialize hp and speaker paths */
5619static void init_extra_out(struct hda_codec *codec)
5620{
5621 struct hda_gen_spec *spec = codec->spec;
5622
5623 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
2c12c30d 5624 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
db23fd19
TI
5625 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5626 __init_extra_out(codec, spec->autocfg.speaker_outs,
2c12c30d 5627 spec->speaker_paths);
db23fd19
TI
5628}
5629
352f7f91
TI
5630/* initialize multi-io paths */
5631static void init_multi_io(struct hda_codec *codec)
5632{
5633 struct hda_gen_spec *spec = codec->spec;
5634 int i;
5635
5636 for (i = 0; i < spec->multi_ios; i++) {
5637 hda_nid_t pin = spec->multi_io[i].pin;
5638 struct nid_path *path;
196c1766 5639 path = get_multiio_path(codec, i);
352f7f91
TI
5640 if (!path)
5641 continue;
5642 if (!spec->multi_io[i].ctl_in)
5643 spec->multi_io[i].ctl_in =
2c12c30d 5644 snd_hda_codec_get_pin_target(codec, pin);
65033cc8
TI
5645 snd_hda_activate_path(codec, path, path->active,
5646 aamix_default(spec));
352f7f91
TI
5647 }
5648}
5649
4f7f67fb
TI
5650static void init_aamix_paths(struct hda_codec *codec)
5651{
5652 struct hda_gen_spec *spec = codec->spec;
5653
5654 if (!spec->have_aamix_ctl)
5655 return;
5656 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5657 spec->aamix_out_paths[0],
5658 spec->autocfg.line_out_type);
5659 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5660 spec->aamix_out_paths[1],
5661 AUTO_PIN_HP_OUT);
5662 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5663 spec->aamix_out_paths[2],
5664 AUTO_PIN_SPEAKER_OUT);
5665}
5666
352f7f91
TI
5667/* set up input pins and loopback paths */
5668static void init_analog_input(struct hda_codec *codec)
5669{
5670 struct hda_gen_spec *spec = codec->spec;
5671 struct auto_pin_cfg *cfg = &spec->autocfg;
5672 int i;
5673
5674 for (i = 0; i < cfg->num_inputs; i++) {
5675 hda_nid_t nid = cfg->inputs[i].pin;
5676 if (is_input_pin(codec, nid))
2c12c30d 5677 restore_pin_ctl(codec, nid);
352f7f91
TI
5678
5679 /* init loopback inputs */
5680 if (spec->mixer_nid) {
3e367f15
TI
5681 resume_path_from_idx(codec, spec->loopback_paths[i]);
5682 resume_path_from_idx(codec, spec->loopback_merge_path);
352f7f91
TI
5683 }
5684 }
5685}
5686
5687/* initialize ADC paths */
5688static void init_input_src(struct hda_codec *codec)
5689{
5690 struct hda_gen_spec *spec = codec->spec;
5691 struct hda_input_mux *imux = &spec->input_mux;
5692 struct nid_path *path;
5693 int i, c, nums;
1da177e4 5694
352f7f91
TI
5695 if (spec->dyn_adc_switch)
5696 nums = 1;
5697 else
5698 nums = spec->num_adc_nids;
5699
5700 for (c = 0; c < nums; c++) {
5701 for (i = 0; i < imux->num_items; i++) {
c697b716 5702 path = get_input_path(codec, c, i);
352f7f91
TI
5703 if (path) {
5704 bool active = path->active;
5705 if (i == spec->cur_mux[c])
5706 active = true;
5707 snd_hda_activate_path(codec, path, active, false);
5708 }
97ec558a 5709 }
967303da
TI
5710 if (spec->hp_mic)
5711 update_hp_mic(codec, c, true);
1da177e4 5712 }
352f7f91 5713
352f7f91 5714 if (spec->cap_sync_hook)
7fe30711 5715 spec->cap_sync_hook(codec, NULL, NULL);
352f7f91
TI
5716}
5717
5718/* set right pin controls for digital I/O */
5719static void init_digital(struct hda_codec *codec)
5720{
5721 struct hda_gen_spec *spec = codec->spec;
5722 int i;
5723 hda_nid_t pin;
5724
d4156930 5725 for (i = 0; i < spec->autocfg.dig_outs; i++)
2c12c30d 5726 set_output_and_unmute(codec, spec->digout_paths[i]);
352f7f91 5727 pin = spec->autocfg.dig_in_pin;
2430d7b7 5728 if (pin) {
2c12c30d 5729 restore_pin_ctl(codec, pin);
3e367f15 5730 resume_path_from_idx(codec, spec->digin_path);
2430d7b7 5731 }
352f7f91
TI
5732}
5733
973e4972
TI
5734/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5735 * invalid unsol tags by some reason
5736 */
5737static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5738{
5739 int i;
5740
5741 for (i = 0; i < codec->init_pins.used; i++) {
5742 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5743 hda_nid_t nid = pin->nid;
5744 if (is_jack_detectable(codec, nid) &&
5745 !snd_hda_jack_tbl_get(codec, nid))
5746 snd_hda_codec_update_cache(codec, nid, 0,
5747 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5748 }
5749}
5750
dda42bd0
TI
5751/**
5752 * snd_hda_gen_init - initialize the generic spec
5753 * @codec: the HDA codec
5754 *
5755 * This can be put as patch_ops init function.
5187ac16 5756 */
352f7f91
TI
5757int snd_hda_gen_init(struct hda_codec *codec)
5758{
5759 struct hda_gen_spec *spec = codec->spec;
5760
5761 if (spec->init_hook)
5762 spec->init_hook(codec);
5763
5764 snd_hda_apply_verbs(codec);
5765
3bbcd274
TI
5766 codec->cached_write = 1;
5767
352f7f91
TI
5768 init_multi_out(codec);
5769 init_extra_out(codec);
5770 init_multi_io(codec);
4f7f67fb 5771 init_aamix_paths(codec);
352f7f91
TI
5772 init_analog_input(codec);
5773 init_input_src(codec);
5774 init_digital(codec);
1da177e4 5775
973e4972
TI
5776 clear_unsol_on_unused_pins(codec);
5777
e6feb5d0
TI
5778 sync_all_pin_power_ctls(codec);
5779
352f7f91 5780 /* call init functions of standard auto-mute helpers */
a5cc2509 5781 update_automute_all(codec);
352f7f91 5782
dc870f38 5783 snd_hda_codec_flush_cache(codec);
3bbcd274 5784
352f7f91
TI
5785 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5786 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5787
5788 hda_call_check_power_status(codec, 0x01);
1da177e4
LT
5789 return 0;
5790}
2698ea98 5791EXPORT_SYMBOL_GPL(snd_hda_gen_init);
352f7f91 5792
dda42bd0
TI
5793/**
5794 * snd_hda_gen_free - free the generic spec
5795 * @codec: the HDA codec
5796 *
5797 * This can be put as patch_ops free function.
5187ac16 5798 */
fce52a3b
TI
5799void snd_hda_gen_free(struct hda_codec *codec)
5800{
8a02c0cc 5801 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
fce52a3b
TI
5802 snd_hda_gen_spec_free(codec->spec);
5803 kfree(codec->spec);
5804 codec->spec = NULL;
5805}
2698ea98 5806EXPORT_SYMBOL_GPL(snd_hda_gen_free);
1da177e4 5807
83012a7c 5808#ifdef CONFIG_PM
dda42bd0
TI
5809/**
5810 * snd_hda_gen_check_power_status - check the loopback power save state
5811 * @codec: the HDA codec
5812 * @nid: NID to inspect
5813 *
5814 * This can be put as patch_ops check_power_status function.
5187ac16 5815 */
fce52a3b 5816int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
cb53c626 5817{
352f7f91 5818 struct hda_gen_spec *spec = codec->spec;
cb53c626
TI
5819 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5820}
2698ea98 5821EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
cb53c626
TI
5822#endif
5823
fce52a3b
TI
5824
5825/*
5826 * the generic codec support
5827 */
1da177e4 5828
352f7f91
TI
5829static const struct hda_codec_ops generic_patch_ops = {
5830 .build_controls = snd_hda_gen_build_controls,
5831 .build_pcms = snd_hda_gen_build_pcms,
5832 .init = snd_hda_gen_init,
fce52a3b 5833 .free = snd_hda_gen_free,
352f7f91 5834 .unsol_event = snd_hda_jack_unsol_event,
83012a7c 5835#ifdef CONFIG_PM
fce52a3b 5836 .check_power_status = snd_hda_gen_check_power_status,
cb53c626 5837#endif
1da177e4
LT
5838};
5839
d8a766a1 5840/*
dda42bd0
TI
5841 * snd_hda_parse_generic_codec - Generic codec parser
5842 * @codec: the HDA codec
dda42bd0 5843 */
d8a766a1 5844static int snd_hda_parse_generic_codec(struct hda_codec *codec)
1da177e4 5845{
352f7f91 5846 struct hda_gen_spec *spec;
1da177e4
LT
5847 int err;
5848
e560d8d8 5849 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
352f7f91 5850 if (!spec)
1da177e4 5851 return -ENOMEM;
352f7f91 5852 snd_hda_gen_spec_init(spec);
1da177e4 5853 codec->spec = spec;
1da177e4 5854
9eb413e5
TI
5855 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5856 if (err < 0)
5857 return err;
5858
5859 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
352f7f91 5860 if (err < 0)
1da177e4
LT
5861 goto error;
5862
5863 codec->patch_ops = generic_patch_ops;
1da177e4
LT
5864 return 0;
5865
352f7f91 5866error:
fce52a3b 5867 snd_hda_gen_free(codec);
1da177e4
LT
5868 return err;
5869}
d8a766a1
TI
5870
5871static const struct hda_codec_preset snd_hda_preset_generic[] = {
5872 { .id = HDA_CODEC_ID_GENERIC, .patch = snd_hda_parse_generic_codec },
5873 {} /* terminator */
5874};
5875
5876static struct hda_codec_driver generic_driver = {
5877 .preset = snd_hda_preset_generic,
5878};
5879
5880module_hda_codec_driver(generic_driver);
b21bdd0d
TI
5881
5882MODULE_LICENSE("GPL");
5883MODULE_DESCRIPTION("Generic HD-audio codec parser");