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