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