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