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