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