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