blob: 18b5fae546d0b8ba84e58e0de5835ca1bb3ae885 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040025#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010026#include <linux/sort.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010027#include <linux/ctype.h>
28#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010030#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "hda_codec.h"
32#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include "hda_auto_parser.h"
34#include "hda_jack.h"
35#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwai352f7f92012-12-19 12:52:06 +010038/* initialize hda_gen_spec struct */
39int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Takashi Iwai352f7f92012-12-19 12:52:06 +010041 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42 snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
43 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
44 return 0;
45}
46EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Takashi Iwai12c93df2012-12-19 14:38:33 +010048struct snd_kcontrol_new *
49snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
50 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010051{
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}
Takashi Iwai12c93df2012-12-19 14:38:33 +010064EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010065
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}
76
77static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
78 unsigned int nums,
79 struct hda_ctl_ops *ops)
80{
81 struct hda_gen_spec *spec = codec->spec;
82 struct hda_bind_ctls **ctlp, *ctl;
83 ctlp = snd_array_new(&spec->bind_ctls);
84 if (!ctlp)
85 return NULL;
86 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
87 *ctlp = ctl;
88 if (ctl)
89 ctl->ops = ops;
90 return ctl;
91}
92
93static void free_bind_ctls(struct hda_gen_spec *spec)
94{
95 if (spec->bind_ctls.list) {
96 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
97 int i;
98 for (i = 0; i < spec->bind_ctls.used; i++)
99 kfree(ctl[i]);
100 }
101 snd_array_free(&spec->bind_ctls);
102}
103
104void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
105{
106 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100108 free_kctls(spec);
109 free_bind_ctls(spec);
110 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100112EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100115 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Takashi Iwai352f7f92012-12-19 12:52:06 +0100118/* get the path between the given NIDs;
119 * passing 0 to either @pin or @dac behaves as a wildcard
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100121struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
122 hda_nid_t from_nid, hda_nid_t to_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100124 struct hda_gen_spec *spec = codec->spec;
125 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Takashi Iwai352f7f92012-12-19 12:52:06 +0100127 for (i = 0; i < spec->paths.used; i++) {
128 struct nid_path *path = snd_array_elem(&spec->paths, i);
129 if (path->depth <= 0)
130 continue;
131 if ((!from_nid || path->path[0] == from_nid) &&
132 (!to_nid || path->path[path->depth - 1] == to_nid))
133 return path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135 return NULL;
136}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100137EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
138
139/* check whether the given DAC is already found in any existing paths */
140static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
141{
142 struct hda_gen_spec *spec = codec->spec;
143 int i;
144
145 for (i = 0; i < spec->paths.used; i++) {
146 struct nid_path *path = snd_array_elem(&spec->paths, i);
147 if (path->path[0] == nid)
148 return true;
149 }
150 return false;
151}
152
153/* check whether the given two widgets can be connected */
154static bool is_reachable_path(struct hda_codec *codec,
155 hda_nid_t from_nid, hda_nid_t to_nid)
156{
157 if (!from_nid || !to_nid)
158 return false;
159 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
160}
161
162/* nid, dir and idx */
163#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
164
165/* check whether the given ctl is already assigned in any path elements */
166static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
167{
168 struct hda_gen_spec *spec = codec->spec;
169 int i;
170
171 val &= AMP_VAL_COMPARE_MASK;
172 for (i = 0; i < spec->paths.used; i++) {
173 struct nid_path *path = snd_array_elem(&spec->paths, i);
174 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
175 return true;
176 }
177 return false;
178}
179
180/* check whether a control with the given (nid, dir, idx) was assigned */
181static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
182 int dir, int idx)
183{
184 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
185 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
186 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
187}
188
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100189static void print_nid_path(const char *pfx, struct nid_path *path)
190{
191 char buf[40];
192 int i;
193
194
195 buf[0] = 0;
196 for (i = 0; i < path->depth; i++) {
197 char tmp[4];
198 sprintf(tmp, ":%02x", path->path[i]);
199 strlcat(buf, tmp, sizeof(buf));
200 }
201 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
202}
203
Takashi Iwai352f7f92012-12-19 12:52:06 +0100204/* called recursively */
205static bool __parse_nid_path(struct hda_codec *codec,
206 hda_nid_t from_nid, hda_nid_t to_nid,
207 int with_aa_mix, struct nid_path *path, int depth)
208{
209 struct hda_gen_spec *spec = codec->spec;
210 hda_nid_t conn[16];
211 int i, nums;
212
213 if (to_nid == spec->mixer_nid) {
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100214 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100215 return false;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100216 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100217 }
218
219 nums = snd_hda_get_connections(codec, to_nid, conn, ARRAY_SIZE(conn));
220 for (i = 0; i < nums; i++) {
221 if (conn[i] != from_nid) {
222 /* special case: when from_nid is 0,
223 * try to find an empty DAC
224 */
225 if (from_nid ||
226 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
227 is_dac_already_used(codec, conn[i]))
228 continue;
229 }
230 /* aa-mix is requested but not included? */
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100231 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100232 goto found;
233 }
234 if (depth >= MAX_NID_PATH_DEPTH)
235 return false;
236 for (i = 0; i < nums; i++) {
237 unsigned int type;
238 type = get_wcaps_type(get_wcaps(codec, conn[i]));
239 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
240 type == AC_WID_PIN)
241 continue;
242 if (__parse_nid_path(codec, from_nid, conn[i],
243 with_aa_mix, path, depth + 1))
244 goto found;
245 }
246 return false;
247
248 found:
249 path->path[path->depth] = conn[i];
250 path->idx[path->depth + 1] = i;
251 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
252 path->multi[path->depth + 1] = 1;
253 path->depth++;
254 return true;
255}
256
257/* parse the widget path from the given nid to the target nid;
258 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100259 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
260 * excluded, only the paths that don't go through the mixer will be chosen.
261 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
262 * spec->mixer_nid will be chosen.
263 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100264 */
265bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
266 hda_nid_t to_nid, int with_aa_mix,
267 struct nid_path *path)
268{
269 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
270 path->path[path->depth] = to_nid;
271 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100272 return true;
273 }
274 return false;
275}
276EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100279 * parse the path between the given NIDs and add to the path list.
280 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100282struct nid_path *
283snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
284 hda_nid_t to_nid, int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100286 struct hda_gen_spec *spec = codec->spec;
287 struct nid_path *path;
288
289 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
290 return NULL;
291
292 path = snd_array_new(&spec->paths);
293 if (!path)
294 return NULL;
295 memset(path, 0, sizeof(*path));
296 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
297 return path;
298 /* push back */
299 spec->paths.used--;
300 return NULL;
301}
302EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
303
304/* look for an empty DAC slot */
305static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
306 bool is_digital)
307{
308 struct hda_gen_spec *spec = codec->spec;
309 bool cap_digital;
310 int i;
311
312 for (i = 0; i < spec->num_all_dacs; i++) {
313 hda_nid_t nid = spec->all_dacs[i];
314 if (!nid || is_dac_already_used(codec, nid))
315 continue;
316 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
317 if (is_digital != cap_digital)
318 continue;
319 if (is_reachable_path(codec, nid, pin))
320 return nid;
321 }
322 return 0;
323}
324
325/* replace the channels in the composed amp value with the given number */
326static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
327{
328 val &= ~(0x3U << 16);
329 val |= chs << 16;
330 return val;
331}
332
333/* check whether the widget has the given amp capability for the direction */
334static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
335 int dir, unsigned int bits)
336{
337 if (!nid)
338 return false;
339 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
340 if (query_amp_caps(codec, nid, dir) & bits)
341 return true;
342 return false;
343}
344
345#define nid_has_mute(codec, nid, dir) \
346 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
347#define nid_has_volume(codec, nid, dir) \
348 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
349
350/* look for a widget suitable for assigning a mute switch in the path */
351static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
352 struct nid_path *path)
353{
354 int i;
355
356 for (i = path->depth - 1; i >= 0; i--) {
357 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
358 return path->path[i];
359 if (i != path->depth - 1 && i != 0 &&
360 nid_has_mute(codec, path->path[i], HDA_INPUT))
361 return path->path[i];
362 }
363 return 0;
364}
365
366/* look for a widget suitable for assigning a volume ctl in the path */
367static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
368 struct nid_path *path)
369{
370 int i;
371
372 for (i = path->depth - 1; i >= 0; i--) {
373 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
374 return path->path[i];
375 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100380 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100382
383/* can have the amp-in capability? */
384static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100386 hda_nid_t nid = path->path[idx];
387 unsigned int caps = get_wcaps(codec, nid);
388 unsigned int type = get_wcaps_type(caps);
389
390 if (!(caps & AC_WCAP_IN_AMP))
391 return false;
392 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
393 return false;
394 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Takashi Iwai352f7f92012-12-19 12:52:06 +0100397/* can have the amp-out capability? */
398static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100400 hda_nid_t nid = path->path[idx];
401 unsigned int caps = get_wcaps(codec, nid);
402 unsigned int type = get_wcaps_type(caps);
403
404 if (!(caps & AC_WCAP_OUT_AMP))
405 return false;
406 if (type == AC_WID_PIN && !idx) /* only for output pins */
407 return false;
408 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Takashi Iwai352f7f92012-12-19 12:52:06 +0100411/* check whether the given (nid,dir,idx) is active */
412static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
413 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100415 struct hda_gen_spec *spec = codec->spec;
416 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Takashi Iwai352f7f92012-12-19 12:52:06 +0100418 for (n = 0; n < spec->paths.used; n++) {
419 struct nid_path *path = snd_array_elem(&spec->paths, n);
420 if (!path->active)
421 continue;
422 for (i = 0; i < path->depth; i++) {
423 if (path->path[i] == nid) {
424 if (dir == HDA_OUTPUT || path->idx[i] == idx)
425 return true;
426 break;
427 }
428 }
429 }
430 return false;
431}
432
433/* get the default amp value for the target state */
434static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
435 int dir, bool enable)
436{
437 unsigned int caps;
438 unsigned int val = 0;
439
440 caps = query_amp_caps(codec, nid, dir);
441 if (caps & AC_AMPCAP_NUM_STEPS) {
442 /* set to 0dB */
443 if (enable)
444 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
445 }
446 if (caps & AC_AMPCAP_MUTE) {
447 if (!enable)
448 val |= HDA_AMP_MUTE;
449 }
450 return val;
451}
452
453/* initialize the amp value (only at the first time) */
454static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
455{
456 int val = get_amp_val_to_activate(codec, nid, dir, false);
457 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
458}
459
460static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
461 int idx, bool enable)
462{
463 int val;
464 if (is_ctl_associated(codec, nid, dir, idx) ||
465 is_active_nid(codec, nid, dir, idx))
466 return;
467 val = get_amp_val_to_activate(codec, nid, dir, enable);
468 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
469}
470
471static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
472 int i, bool enable)
473{
474 hda_nid_t nid = path->path[i];
475 init_amp(codec, nid, HDA_OUTPUT, 0);
476 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
477}
478
479static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
480 int i, bool enable, bool add_aamix)
481{
482 struct hda_gen_spec *spec = codec->spec;
483 hda_nid_t conn[16];
484 int n, nums, idx;
485 int type;
486 hda_nid_t nid = path->path[i];
487
488 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
489 type = get_wcaps_type(get_wcaps(codec, nid));
490 if (type == AC_WID_PIN ||
491 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
492 nums = 1;
493 idx = 0;
494 } else
495 idx = path->idx[i];
496
497 for (n = 0; n < nums; n++)
498 init_amp(codec, nid, HDA_INPUT, n);
499
500 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
501 return;
502
503 /* here is a little bit tricky in comparison with activate_amp_out();
504 * when aa-mixer is available, we need to enable the path as well
505 */
506 for (n = 0; n < nums; n++) {
507 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
508 continue;
509 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511}
512
Takashi Iwai352f7f92012-12-19 12:52:06 +0100513/* activate or deactivate the given path
514 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100516void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
517 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100519 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Takashi Iwai352f7f92012-12-19 12:52:06 +0100521 if (!enable)
522 path->active = false;
523
524 for (i = path->depth - 1; i >= 0; i--) {
525 if (enable && path->multi[i])
526 snd_hda_codec_write_cache(codec, path->path[i], 0,
527 AC_VERB_SET_CONNECT_SEL,
528 path->idx[i]);
529 if (has_amp_in(codec, path, i))
530 activate_amp_in(codec, path, i, enable, add_aamix);
531 if (has_amp_out(codec, path, i))
532 activate_amp_out(codec, path, i, enable);
533 }
534
535 if (enable)
536 path->active = true;
537}
538EXPORT_SYMBOL_HDA(snd_hda_activate_path);
539
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100540/* turn on/off EAPD on the given pin */
541static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
542{
543 struct hda_gen_spec *spec = codec->spec;
544 if (spec->own_eapd_ctl ||
545 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
546 return;
547 snd_hda_codec_update_cache(codec, pin, 0,
548 AC_VERB_SET_EAPD_BTLENABLE,
549 enable ? 0x02 : 0x00);
550}
551
Takashi Iwai352f7f92012-12-19 12:52:06 +0100552
553/*
554 * Helper functions for creating mixer ctl elements
555 */
556
557enum {
558 HDA_CTL_WIDGET_VOL,
559 HDA_CTL_WIDGET_MUTE,
560 HDA_CTL_BIND_MUTE,
561 HDA_CTL_BIND_VOL,
562 HDA_CTL_BIND_SW,
563};
564static const struct snd_kcontrol_new control_templates[] = {
565 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
566 HDA_CODEC_MUTE(NULL, 0, 0, 0),
567 HDA_BIND_MUTE(NULL, 0, 0, 0),
568 HDA_BIND_VOL(NULL, 0),
569 HDA_BIND_SW(NULL, 0),
570};
571
572/* add dynamic controls from template */
573static int add_control(struct hda_gen_spec *spec, int type, const char *name,
574 int cidx, unsigned long val)
575{
576 struct snd_kcontrol_new *knew;
577
Takashi Iwai12c93df2012-12-19 14:38:33 +0100578 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100579 if (!knew)
580 return -ENOMEM;
581 knew->index = cidx;
582 if (get_amp_nid_(val))
583 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
584 knew->private_value = val;
585 return 0;
586}
587
588static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
589 const char *pfx, const char *dir,
590 const char *sfx, int cidx, unsigned long val)
591{
592 char name[32];
593 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
594 return add_control(spec, type, name, cidx, val);
595}
596
597#define add_pb_vol_ctrl(spec, type, pfx, val) \
598 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
599#define add_pb_sw_ctrl(spec, type, pfx, val) \
600 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
601#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
602 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
603#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
604 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
605
606static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
607 unsigned int chs, struct nid_path *path)
608{
609 unsigned int val;
610 if (!path)
611 return 0;
612 val = path->ctls[NID_PATH_VOL_CTL];
613 if (!val)
614 return 0;
615 val = amp_val_replace_channels(val, chs);
616 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
617}
618
619/* return the channel bits suitable for the given path->ctls[] */
620static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
621 int type)
622{
623 int chs = 1; /* mono (left only) */
624 if (path) {
625 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
626 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
627 chs = 3; /* stereo */
628 }
629 return chs;
630}
631
632static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
633 struct nid_path *path)
634{
635 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
636 return add_vol_ctl(codec, pfx, cidx, chs, path);
637}
638
639/* create a mute-switch for the given mixer widget;
640 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
641 */
642static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
643 unsigned int chs, struct nid_path *path)
644{
645 unsigned int val;
646 int type = HDA_CTL_WIDGET_MUTE;
647
648 if (!path)
649 return 0;
650 val = path->ctls[NID_PATH_MUTE_CTL];
651 if (!val)
652 return 0;
653 val = amp_val_replace_channels(val, chs);
654 if (get_amp_direction_(val) == HDA_INPUT) {
655 hda_nid_t nid = get_amp_nid_(val);
656 int nums = snd_hda_get_num_conns(codec, nid);
657 if (nums > 1) {
658 type = HDA_CTL_BIND_MUTE;
659 val |= nums << 19;
660 }
661 }
662 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
663}
664
665static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
666 int cidx, struct nid_path *path)
667{
668 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
669 return add_sw_ctl(codec, pfx, cidx, chs, path);
670}
671
672static const char * const channel_name[4] = {
673 "Front", "Surround", "CLFE", "Side"
674};
675
676/* give some appropriate ctl name prefix for the given line out channel */
677static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
678 bool can_be_master, int *index)
679{
680 struct auto_pin_cfg *cfg = &spec->autocfg;
681
682 *index = 0;
683 if (cfg->line_outs == 1 && !spec->multi_ios &&
684 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
685 return spec->vmaster_mute.hook ? "PCM" : "Master";
686
687 /* if there is really a single DAC used in the whole output paths,
688 * use it master (or "PCM" if a vmaster hook is present)
689 */
690 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
691 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
692 return spec->vmaster_mute.hook ? "PCM" : "Master";
693
694 switch (cfg->line_out_type) {
695 case AUTO_PIN_SPEAKER_OUT:
696 if (cfg->line_outs == 1)
697 return "Speaker";
698 if (cfg->line_outs == 2)
699 return ch ? "Bass Speaker" : "Speaker";
700 break;
701 case AUTO_PIN_HP_OUT:
702 /* for multi-io case, only the primary out */
703 if (ch && spec->multi_ios)
704 break;
705 *index = ch;
706 return "Headphone";
707 default:
708 if (cfg->line_outs == 1 && !spec->multi_ios)
709 return "PCM";
710 break;
711 }
712 if (ch >= ARRAY_SIZE(channel_name)) {
713 snd_BUG();
714 return "PCM";
715 }
716
717 return channel_name[ch];
718}
719
720/*
721 * Parse output paths
722 */
723
724/* badness definition */
725enum {
726 /* No primary DAC is found for the main output */
727 BAD_NO_PRIMARY_DAC = 0x10000,
728 /* No DAC is found for the extra output */
729 BAD_NO_DAC = 0x4000,
730 /* No possible multi-ios */
731 BAD_MULTI_IO = 0x103,
732 /* No individual DAC for extra output */
733 BAD_NO_EXTRA_DAC = 0x102,
734 /* No individual DAC for extra surrounds */
735 BAD_NO_EXTRA_SURR_DAC = 0x101,
736 /* Primary DAC shared with main surrounds */
737 BAD_SHARED_SURROUND = 0x100,
738 /* Primary DAC shared with main CLFE */
739 BAD_SHARED_CLFE = 0x10,
740 /* Primary DAC shared with extra surrounds */
741 BAD_SHARED_EXTRA_SURROUND = 0x10,
742 /* Volume widget is shared */
743 BAD_SHARED_VOL = 0x10,
744};
745
746/* look for widgets in the path between the given NIDs appropriate for
747 * volume and mute controls, and assign the values to ctls[].
748 *
749 * When no appropriate widget is found in the path, the badness value
750 * is incremented depending on the situation. The function returns the
751 * total badness for both volume and mute controls.
752 */
753static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
754 hda_nid_t dac)
755{
756 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
757 hda_nid_t nid;
758 unsigned int val;
759 int badness = 0;
760
761 if (!path)
762 return BAD_SHARED_VOL * 2;
763 nid = look_for_out_vol_nid(codec, path);
764 if (nid) {
765 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
766 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
767 badness += BAD_SHARED_VOL;
768 else
769 path->ctls[NID_PATH_VOL_CTL] = val;
770 } else
771 badness += BAD_SHARED_VOL;
772 nid = look_for_out_mute_nid(codec, path);
773 if (nid) {
774 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
775 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
776 nid_has_mute(codec, nid, HDA_OUTPUT))
777 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
778 else
779 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
780 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
781 badness += BAD_SHARED_VOL;
782 else
783 path->ctls[NID_PATH_MUTE_CTL] = val;
784 } else
785 badness += BAD_SHARED_VOL;
786 return badness;
787}
788
789struct badness_table {
790 int no_primary_dac; /* no primary DAC */
791 int no_dac; /* no secondary DACs */
792 int shared_primary; /* primary DAC is shared with main output */
793 int shared_surr; /* secondary DAC shared with main or primary */
794 int shared_clfe; /* third DAC shared with main or primary */
795 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
796};
797
798static struct badness_table main_out_badness = {
799 .no_primary_dac = BAD_NO_PRIMARY_DAC,
800 .no_dac = BAD_NO_DAC,
801 .shared_primary = BAD_NO_PRIMARY_DAC,
802 .shared_surr = BAD_SHARED_SURROUND,
803 .shared_clfe = BAD_SHARED_CLFE,
804 .shared_surr_main = BAD_SHARED_SURROUND,
805};
806
807static struct badness_table extra_out_badness = {
808 .no_primary_dac = BAD_NO_DAC,
809 .no_dac = BAD_NO_DAC,
810 .shared_primary = BAD_NO_EXTRA_DAC,
811 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
812 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
813 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
814};
815
816/* try to assign DACs to pins and return the resultant badness */
817static int try_assign_dacs(struct hda_codec *codec, int num_outs,
818 const hda_nid_t *pins, hda_nid_t *dacs,
819 const struct badness_table *bad)
820{
821 struct hda_gen_spec *spec = codec->spec;
822 struct auto_pin_cfg *cfg = &spec->autocfg;
823 int i, j;
824 int badness = 0;
825 hda_nid_t dac;
826
827 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return 0;
829
Takashi Iwai352f7f92012-12-19 12:52:06 +0100830 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100831 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100832 hda_nid_t pin = pins[i];
833 if (!dacs[i])
834 dacs[i] = look_for_dac(codec, pin, false);
835 if (!dacs[i] && !i) {
836 for (j = 1; j < num_outs; j++) {
837 if (is_reachable_path(codec, dacs[j], pin)) {
838 dacs[0] = dacs[j];
839 dacs[j] = 0;
840 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100843 }
844 dac = dacs[i];
845 if (!dac) {
846 if (is_reachable_path(codec, dacs[0], pin))
847 dac = dacs[0];
848 else if (cfg->line_outs > i &&
849 is_reachable_path(codec, spec->private_dac_nids[i], pin))
850 dac = spec->private_dac_nids[i];
851 if (dac) {
852 if (!i)
853 badness += bad->shared_primary;
854 else if (i == 1)
855 badness += bad->shared_surr;
856 else
857 badness += bad->shared_clfe;
858 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
859 dac = spec->private_dac_nids[0];
860 badness += bad->shared_surr_main;
861 } else if (!i)
862 badness += bad->no_primary_dac;
863 else
864 badness += bad->no_dac;
865 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100866 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100867 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100868 dac = dacs[i] = 0;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100869 else
870 print_nid_path("output", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100871 if (dac)
872 badness += assign_out_path_ctls(codec, pin, dac);
873 }
874
875 return badness;
876}
877
878/* return NID if the given pin has only a single connection to a certain DAC */
879static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
880{
881 struct hda_gen_spec *spec = codec->spec;
882 int i;
883 hda_nid_t nid_found = 0;
884
885 for (i = 0; i < spec->num_all_dacs; i++) {
886 hda_nid_t nid = spec->all_dacs[i];
887 if (!nid || is_dac_already_used(codec, nid))
888 continue;
889 if (is_reachable_path(codec, nid, pin)) {
890 if (nid_found)
891 return 0;
892 nid_found = nid;
893 }
894 }
895 return nid_found;
896}
897
898/* check whether the given pin can be a multi-io pin */
899static bool can_be_multiio_pin(struct hda_codec *codec,
900 unsigned int location, hda_nid_t nid)
901{
902 unsigned int defcfg, caps;
903
904 defcfg = snd_hda_codec_get_pincfg(codec, nid);
905 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
906 return false;
907 if (location && get_defcfg_location(defcfg) != location)
908 return false;
909 caps = snd_hda_query_pin_caps(codec, nid);
910 if (!(caps & AC_PINCAP_OUT))
911 return false;
912 return true;
913}
914
915/*
916 * multi-io helper
917 *
918 * When hardwired is set, try to fill ony hardwired pins, and returns
919 * zero if any pins are filled, non-zero if nothing found.
920 * When hardwired is off, try to fill possible input pins, and returns
921 * the badness value.
922 */
923static int fill_multi_ios(struct hda_codec *codec,
924 hda_nid_t reference_pin,
925 bool hardwired, int offset)
926{
927 struct hda_gen_spec *spec = codec->spec;
928 struct auto_pin_cfg *cfg = &spec->autocfg;
929 int type, i, j, dacs, num_pins, old_pins;
930 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
931 unsigned int location = get_defcfg_location(defcfg);
932 int badness = 0;
933
934 old_pins = spec->multi_ios;
935 if (old_pins >= 2)
936 goto end_fill;
937
938 num_pins = 0;
939 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
940 for (i = 0; i < cfg->num_inputs; i++) {
941 if (cfg->inputs[i].type != type)
942 continue;
943 if (can_be_multiio_pin(codec, location,
944 cfg->inputs[i].pin))
945 num_pins++;
946 }
947 }
948 if (num_pins < 2)
949 goto end_fill;
950
951 dacs = spec->multiout.num_dacs;
952 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
953 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100954 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100955 hda_nid_t nid = cfg->inputs[i].pin;
956 hda_nid_t dac = 0;
957
958 if (cfg->inputs[i].type != type)
959 continue;
960 if (!can_be_multiio_pin(codec, location, nid))
961 continue;
962 for (j = 0; j < spec->multi_ios; j++) {
963 if (nid == spec->multi_io[j].pin)
964 break;
965 }
966 if (j < spec->multi_ios)
967 continue;
968
969 if (offset && offset + spec->multi_ios < dacs) {
970 dac = spec->private_dac_nids[offset + spec->multi_ios];
971 if (!is_reachable_path(codec, dac, nid))
972 dac = 0;
973 }
974 if (hardwired)
975 dac = get_dac_if_single(codec, nid);
976 else if (!dac)
977 dac = look_for_dac(codec, nid, false);
978 if (!dac) {
979 badness++;
980 continue;
981 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100982 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100983 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100984 badness++;
985 continue;
986 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100987 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100988 spec->multi_io[spec->multi_ios].pin = nid;
989 spec->multi_io[spec->multi_ios].dac = dac;
990 spec->multi_ios++;
991 if (spec->multi_ios >= 2)
992 break;
993 }
994 }
995 end_fill:
996 if (badness)
997 badness = BAD_MULTI_IO;
998 if (old_pins == spec->multi_ios) {
999 if (hardwired)
1000 return 1; /* nothing found */
1001 else
1002 return badness; /* no badness if nothing found */
1003 }
1004 if (!hardwired && spec->multi_ios < 2) {
1005 /* cancel newly assigned paths */
1006 spec->paths.used -= spec->multi_ios - old_pins;
1007 spec->multi_ios = old_pins;
1008 return badness;
1009 }
1010
1011 /* assign volume and mute controls */
1012 for (i = old_pins; i < spec->multi_ios; i++)
1013 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1014 spec->multi_io[i].dac);
1015
1016 return badness;
1017}
1018
1019/* map DACs for all pins in the list if they are single connections */
1020static bool map_singles(struct hda_codec *codec, int outs,
1021 const hda_nid_t *pins, hda_nid_t *dacs)
1022{
1023 int i;
1024 bool found = false;
1025 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001026 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001027 hda_nid_t dac;
1028 if (dacs[i])
1029 continue;
1030 dac = get_dac_if_single(codec, pins[i]);
1031 if (!dac)
1032 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001033 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001034 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001035 dacs[i] = dac;
1036 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001037 print_nid_path("output", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001038 }
1039 }
1040 return found;
1041}
1042
1043/* fill in the dac_nids table from the parsed pin configuration */
1044static int fill_and_eval_dacs(struct hda_codec *codec,
1045 bool fill_hardwired,
1046 bool fill_mio_first)
1047{
1048 struct hda_gen_spec *spec = codec->spec;
1049 struct auto_pin_cfg *cfg = &spec->autocfg;
1050 int i, err, badness;
1051
1052 /* set num_dacs once to full for look_for_dac() */
1053 spec->multiout.num_dacs = cfg->line_outs;
1054 spec->multiout.dac_nids = spec->private_dac_nids;
1055 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1056 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1057 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1058 spec->multi_ios = 0;
1059 snd_array_free(&spec->paths);
1060 badness = 0;
1061
1062 /* fill hard-wired DACs first */
1063 if (fill_hardwired) {
1064 bool mapped;
1065 do {
1066 mapped = map_singles(codec, cfg->line_outs,
1067 cfg->line_out_pins,
1068 spec->private_dac_nids);
1069 mapped |= map_singles(codec, cfg->hp_outs,
1070 cfg->hp_pins,
1071 spec->multiout.hp_out_nid);
1072 mapped |= map_singles(codec, cfg->speaker_outs,
1073 cfg->speaker_pins,
1074 spec->multiout.extra_out_nid);
1075 if (fill_mio_first && cfg->line_outs == 1 &&
1076 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1077 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1078 if (!err)
1079 mapped = true;
1080 }
1081 } while (mapped);
1082 }
1083
1084 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1085 spec->private_dac_nids,
1086 &main_out_badness);
1087
1088 /* re-count num_dacs and squash invalid entries */
1089 spec->multiout.num_dacs = 0;
1090 for (i = 0; i < cfg->line_outs; i++) {
1091 if (spec->private_dac_nids[i])
1092 spec->multiout.num_dacs++;
1093 else {
1094 memmove(spec->private_dac_nids + i,
1095 spec->private_dac_nids + i + 1,
1096 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1097 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1098 }
1099 }
1100
1101 if (fill_mio_first &&
1102 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1103 /* try to fill multi-io first */
1104 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1105 if (err < 0)
1106 return err;
1107 /* we don't count badness at this stage yet */
1108 }
1109
1110 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1111 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1112 spec->multiout.hp_out_nid,
1113 &extra_out_badness);
1114 if (err < 0)
1115 return err;
1116 badness += err;
1117 }
1118 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1119 err = try_assign_dacs(codec, cfg->speaker_outs,
1120 cfg->speaker_pins,
1121 spec->multiout.extra_out_nid,
1122 &extra_out_badness);
1123 if (err < 0)
1124 return err;
1125 badness += err;
1126 }
1127 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1128 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1129 if (err < 0)
1130 return err;
1131 badness += err;
1132 }
1133 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1134 /* try multi-ios with HP + inputs */
1135 int offset = 0;
1136 if (cfg->line_outs >= 3)
1137 offset = 1;
1138 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1139 if (err < 0)
1140 return err;
1141 badness += err;
1142 }
1143
1144 if (spec->multi_ios == 2) {
1145 for (i = 0; i < 2; i++)
1146 spec->private_dac_nids[spec->multiout.num_dacs++] =
1147 spec->multi_io[i].dac;
1148 spec->ext_channel_count = 2;
1149 } else if (spec->multi_ios) {
1150 spec->multi_ios = 0;
1151 badness += BAD_MULTI_IO;
1152 }
1153
1154 return badness;
1155}
1156
1157#define DEBUG_BADNESS
1158
1159#ifdef DEBUG_BADNESS
1160#define debug_badness snd_printdd
1161#else
1162#define debug_badness(...)
1163#endif
1164
1165static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1166{
1167 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1168 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001169 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001170 spec->multiout.dac_nids[0],
1171 spec->multiout.dac_nids[1],
1172 spec->multiout.dac_nids[2],
1173 spec->multiout.dac_nids[3]);
1174 if (spec->multi_ios > 0)
1175 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1176 spec->multi_ios,
1177 spec->multi_io[0].pin, spec->multi_io[1].pin,
1178 spec->multi_io[0].dac, spec->multi_io[1].dac);
1179 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1180 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001181 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001182 spec->multiout.hp_out_nid[0],
1183 spec->multiout.hp_out_nid[1],
1184 spec->multiout.hp_out_nid[2],
1185 spec->multiout.hp_out_nid[3]);
1186 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1187 cfg->speaker_pins[0], cfg->speaker_pins[1],
1188 cfg->speaker_pins[2], cfg->speaker_pins[3],
1189 spec->multiout.extra_out_nid[0],
1190 spec->multiout.extra_out_nid[1],
1191 spec->multiout.extra_out_nid[2],
1192 spec->multiout.extra_out_nid[3]);
1193}
1194
1195/* find all available DACs of the codec */
1196static void fill_all_dac_nids(struct hda_codec *codec)
1197{
1198 struct hda_gen_spec *spec = codec->spec;
1199 int i;
1200 hda_nid_t nid = codec->start_nid;
1201
1202 spec->num_all_dacs = 0;
1203 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1204 for (i = 0; i < codec->num_nodes; i++, nid++) {
1205 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1206 continue;
1207 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1208 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1209 break;
1210 }
1211 spec->all_dacs[spec->num_all_dacs++] = nid;
1212 }
1213}
1214
1215static int parse_output_paths(struct hda_codec *codec)
1216{
1217 struct hda_gen_spec *spec = codec->spec;
1218 struct auto_pin_cfg *cfg = &spec->autocfg;
1219 struct auto_pin_cfg *best_cfg;
1220 int best_badness = INT_MAX;
1221 int badness;
1222 bool fill_hardwired = true, fill_mio_first = true;
1223 bool best_wired = true, best_mio = true;
1224 bool hp_spk_swapped = false;
1225
1226 fill_all_dac_nids(codec);
1227
1228 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1229 if (!best_cfg)
1230 return -ENOMEM;
1231 *best_cfg = *cfg;
1232
1233 for (;;) {
1234 badness = fill_and_eval_dacs(codec, fill_hardwired,
1235 fill_mio_first);
1236 if (badness < 0) {
1237 kfree(best_cfg);
1238 return badness;
1239 }
1240 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1241 cfg->line_out_type, fill_hardwired, fill_mio_first,
1242 badness);
1243 debug_show_configs(spec, cfg);
1244 if (badness < best_badness) {
1245 best_badness = badness;
1246 *best_cfg = *cfg;
1247 best_wired = fill_hardwired;
1248 best_mio = fill_mio_first;
1249 }
1250 if (!badness)
1251 break;
1252 fill_mio_first = !fill_mio_first;
1253 if (!fill_mio_first)
1254 continue;
1255 fill_hardwired = !fill_hardwired;
1256 if (!fill_hardwired)
1257 continue;
1258 if (hp_spk_swapped)
1259 break;
1260 hp_spk_swapped = true;
1261 if (cfg->speaker_outs > 0 &&
1262 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1263 cfg->hp_outs = cfg->line_outs;
1264 memcpy(cfg->hp_pins, cfg->line_out_pins,
1265 sizeof(cfg->hp_pins));
1266 cfg->line_outs = cfg->speaker_outs;
1267 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1268 sizeof(cfg->speaker_pins));
1269 cfg->speaker_outs = 0;
1270 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1271 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1272 fill_hardwired = true;
1273 continue;
1274 }
1275 if (cfg->hp_outs > 0 &&
1276 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1277 cfg->speaker_outs = cfg->line_outs;
1278 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1279 sizeof(cfg->speaker_pins));
1280 cfg->line_outs = cfg->hp_outs;
1281 memcpy(cfg->line_out_pins, cfg->hp_pins,
1282 sizeof(cfg->hp_pins));
1283 cfg->hp_outs = 0;
1284 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1285 cfg->line_out_type = AUTO_PIN_HP_OUT;
1286 fill_hardwired = true;
1287 continue;
1288 }
1289 break;
1290 }
1291
1292 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001293 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001294 *cfg = *best_cfg;
1295 fill_and_eval_dacs(codec, best_wired, best_mio);
1296 }
1297 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1298 cfg->line_out_type, best_wired, best_mio);
1299 debug_show_configs(spec, cfg);
1300
1301 if (cfg->line_out_pins[0]) {
1302 struct nid_path *path;
1303 path = snd_hda_get_nid_path(codec,
1304 spec->multiout.dac_nids[0],
1305 cfg->line_out_pins[0]);
1306 if (path)
1307 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1308 }
1309
1310 kfree(best_cfg);
1311 return 0;
1312}
1313
1314/* add playback controls from the parsed DAC table */
1315static int create_multi_out_ctls(struct hda_codec *codec,
1316 const struct auto_pin_cfg *cfg)
1317{
1318 struct hda_gen_spec *spec = codec->spec;
1319 int i, err, noutputs;
1320
1321 noutputs = cfg->line_outs;
1322 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1323 noutputs += spec->multi_ios;
1324
1325 for (i = 0; i < noutputs; i++) {
1326 const char *name;
1327 int index;
1328 hda_nid_t dac, pin;
1329 struct nid_path *path;
1330
1331 dac = spec->multiout.dac_nids[i];
1332 if (!dac)
1333 continue;
1334 if (i >= cfg->line_outs) {
1335 pin = spec->multi_io[i - 1].pin;
1336 index = 0;
1337 name = channel_name[i];
1338 } else {
1339 pin = cfg->line_out_pins[i];
1340 name = get_line_out_pfx(spec, i, true, &index);
1341 }
1342
1343 path = snd_hda_get_nid_path(codec, dac, pin);
1344 if (!path)
1345 continue;
1346 if (!name || !strcmp(name, "CLFE")) {
1347 /* Center/LFE */
1348 err = add_vol_ctl(codec, "Center", 0, 1, path);
1349 if (err < 0)
1350 return err;
1351 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1352 if (err < 0)
1353 return err;
1354 err = add_sw_ctl(codec, "Center", 0, 1, path);
1355 if (err < 0)
1356 return err;
1357 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1358 if (err < 0)
1359 return err;
1360 } else {
1361 err = add_stereo_vol(codec, name, index, path);
1362 if (err < 0)
1363 return err;
1364 err = add_stereo_sw(codec, name, index, path);
1365 if (err < 0)
1366 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368 }
1369 return 0;
1370}
1371
Takashi Iwai352f7f92012-12-19 12:52:06 +01001372static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1373 hda_nid_t dac, const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001375 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 int err;
1377
Takashi Iwai352f7f92012-12-19 12:52:06 +01001378 path = snd_hda_get_nid_path(codec, dac, pin);
1379 if (!path)
1380 return 0;
1381 /* bind volume control will be created in the case of dac = 0 */
1382 if (dac) {
1383 err = add_stereo_vol(codec, pfx, cidx, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (err < 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001387 err = add_stereo_sw(codec, pfx, cidx, path);
1388 if (err < 0)
1389 return err;
1390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
Takashi Iwai352f7f92012-12-19 12:52:06 +01001393/* add playback controls for speaker and HP outputs */
1394static int create_extra_outs(struct hda_codec *codec, int num_pins,
1395 const hda_nid_t *pins, const hda_nid_t *dacs,
1396 const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001398 struct hda_gen_spec *spec = codec->spec;
1399 struct hda_bind_ctls *ctl;
1400 char name[32];
1401 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Takashi Iwai352f7f92012-12-19 12:52:06 +01001403 if (!num_pins || !pins[0])
1404 return 0;
1405
1406 if (num_pins == 1) {
1407 hda_nid_t dac = *dacs;
1408 if (!dac)
1409 dac = spec->multiout.dac_nids[0];
1410 return create_extra_out(codec, *pins, dac, pfx, 0);
Takashi Iwai97ec5582006-03-21 11:29:07 +01001411 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001412
1413 for (i = 0; i < num_pins; i++) {
1414 hda_nid_t dac;
1415 if (dacs[num_pins - 1])
1416 dac = dacs[i]; /* with individual volumes */
Takashi Iwai97ec5582006-03-21 11:29:07 +01001417 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001418 dac = 0;
1419 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1420 err = create_extra_out(codec, pins[i], dac,
1421 "Bass Speaker", 0);
1422 } else if (num_pins >= 3) {
1423 snprintf(name, sizeof(name), "%s %s",
1424 pfx, channel_name[i]);
1425 err = create_extra_out(codec, pins[i], dac, name, 0);
1426 } else {
1427 err = create_extra_out(codec, pins[i], dac, pfx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001429 if (err < 0)
1430 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001432 if (dacs[num_pins - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return 0;
1434
Takashi Iwai352f7f92012-12-19 12:52:06 +01001435 /* Let's create a bind-controls for volumes */
1436 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1437 if (!ctl)
1438 return -ENOMEM;
1439 n = 0;
1440 for (i = 0; i < num_pins; i++) {
1441 hda_nid_t vol;
1442 struct nid_path *path;
1443 if (!pins[i] || !dacs[i])
1444 continue;
1445 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1446 if (!path)
1447 continue;
1448 vol = look_for_out_vol_nid(codec, path);
1449 if (vol)
1450 ctl->values[n++] =
1451 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1452 }
1453 if (n) {
1454 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1455 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1456 if (err < 0)
1457 return err;
1458 }
1459 return 0;
1460}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001461
Takashi Iwai352f7f92012-12-19 12:52:06 +01001462static int create_hp_out_ctls(struct hda_codec *codec)
1463{
1464 struct hda_gen_spec *spec = codec->spec;
1465 return create_extra_outs(codec, spec->autocfg.hp_outs,
1466 spec->autocfg.hp_pins,
1467 spec->multiout.hp_out_nid,
1468 "Headphone");
1469}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Takashi Iwai352f7f92012-12-19 12:52:06 +01001471static int create_speaker_out_ctls(struct hda_codec *codec)
1472{
1473 struct hda_gen_spec *spec = codec->spec;
1474 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1475 spec->autocfg.speaker_pins,
1476 spec->multiout.extra_out_nid,
1477 "Speaker");
1478}
1479
1480/*
1481 * channel mode enum control
1482 */
1483
1484static int ch_mode_info(struct snd_kcontrol *kcontrol,
1485 struct snd_ctl_elem_info *uinfo)
1486{
1487 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1488 struct hda_gen_spec *spec = codec->spec;
1489
1490 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1491 uinfo->count = 1;
1492 uinfo->value.enumerated.items = spec->multi_ios + 1;
1493 if (uinfo->value.enumerated.item > spec->multi_ios)
1494 uinfo->value.enumerated.item = spec->multi_ios;
1495 sprintf(uinfo->value.enumerated.name, "%dch",
1496 (uinfo->value.enumerated.item + 1) * 2);
1497 return 0;
1498}
1499
1500static int ch_mode_get(struct snd_kcontrol *kcontrol,
1501 struct snd_ctl_elem_value *ucontrol)
1502{
1503 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1504 struct hda_gen_spec *spec = codec->spec;
1505 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1506 return 0;
1507}
1508
1509static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1510{
1511 struct hda_gen_spec *spec = codec->spec;
1512 hda_nid_t nid = spec->multi_io[idx].pin;
1513 struct nid_path *path;
1514
1515 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1516 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001518
1519 if (path->active == output)
1520 return 0;
1521
1522 if (output) {
1523 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1524 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001525 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001526 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001527 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001528 snd_hda_activate_path(codec, path, false, true);
1529 snd_hda_set_pin_ctl_cache(codec, nid,
1530 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
Takashi Iwai352f7f92012-12-19 12:52:06 +01001535static int ch_mode_put(struct snd_kcontrol *kcontrol,
1536 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001538 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1539 struct hda_gen_spec *spec = codec->spec;
1540 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Takashi Iwai352f7f92012-12-19 12:52:06 +01001542 ch = ucontrol->value.enumerated.item[0];
1543 if (ch < 0 || ch > spec->multi_ios)
1544 return -EINVAL;
1545 if (ch == (spec->ext_channel_count - 1) / 2)
1546 return 0;
1547 spec->ext_channel_count = (ch + 1) * 2;
1548 for (i = 0; i < spec->multi_ios; i++)
1549 set_multi_io(codec, i, i < ch);
1550 spec->multiout.max_channels = max(spec->ext_channel_count,
1551 spec->const_channel_count);
1552 if (spec->need_dac_fix)
1553 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return 1;
1555}
1556
Takashi Iwai352f7f92012-12-19 12:52:06 +01001557static const struct snd_kcontrol_new channel_mode_enum = {
1558 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1559 .name = "Channel Mode",
1560 .info = ch_mode_info,
1561 .get = ch_mode_get,
1562 .put = ch_mode_put,
1563};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Takashi Iwai352f7f92012-12-19 12:52:06 +01001565static int create_multi_channel_mode(struct hda_codec *codec)
1566{
1567 struct hda_gen_spec *spec = codec->spec;
1568
1569 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001570 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001571 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return 0;
1574}
1575
Takashi Iwai352f7f92012-12-19 12:52:06 +01001576/*
1577 * shared headphone/mic handling
1578 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001579
Takashi Iwai352f7f92012-12-19 12:52:06 +01001580static void call_update_outputs(struct hda_codec *codec);
1581
1582/* for shared I/O, change the pin-control accordingly */
1583static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1584{
1585 struct hda_gen_spec *spec = codec->spec;
1586 unsigned int val;
1587 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1588 /* NOTE: this assumes that there are only two inputs, the
1589 * first is the real internal mic and the second is HP/mic jack.
1590 */
1591
1592 val = snd_hda_get_default_vref(codec, pin);
1593
1594 /* This pin does not have vref caps - let's enable vref on pin 0x18
1595 instead, as suggested by Realtek */
1596 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1597 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1598 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1599 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001600 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1601 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001602 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001603
1604 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001605 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001606
1607 spec->automute_speaker = !set_as_mic;
1608 call_update_outputs(codec);
1609}
1610
1611/* create a shared input with the headphone out */
1612static int create_shared_input(struct hda_codec *codec)
1613{
1614 struct hda_gen_spec *spec = codec->spec;
1615 struct auto_pin_cfg *cfg = &spec->autocfg;
1616 unsigned int defcfg;
1617 hda_nid_t nid;
1618
1619 /* only one internal input pin? */
1620 if (cfg->num_inputs != 1)
1621 return 0;
1622 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1623 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1624 return 0;
1625
1626 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1627 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1628 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1629 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1630 else
1631 return 0; /* both not available */
1632
1633 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1634 return 0; /* no input */
1635
1636 cfg->inputs[1].pin = nid;
1637 cfg->inputs[1].type = AUTO_PIN_MIC;
1638 cfg->num_inputs = 2;
1639 spec->shared_mic_hp = 1;
1640 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1641 return 0;
1642}
1643
1644
1645/*
1646 * Parse input paths
1647 */
1648
1649#ifdef CONFIG_PM
1650/* add the powersave loopback-list entry */
1651static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1652{
1653 struct hda_amp_list *list;
1654
1655 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1656 return;
1657 list = spec->loopback_list + spec->num_loopbacks;
1658 list->nid = mix;
1659 list->dir = HDA_INPUT;
1660 list->idx = idx;
1661 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001662 spec->loopback.amplist = spec->loopback_list;
1663}
1664#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001665#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001666#endif
1667
Takashi Iwai352f7f92012-12-19 12:52:06 +01001668/* create input playback/capture controls for the given pin */
1669static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1670 const char *ctlname, int ctlidx,
1671 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001673 struct hda_gen_spec *spec = codec->spec;
1674 struct nid_path *path;
1675 unsigned int val;
1676 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Takashi Iwai352f7f92012-12-19 12:52:06 +01001678 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1679 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1680 return 0; /* no need for analog loopback */
1681
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001682 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001683 if (!path)
1684 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001685 print_nid_path("loopback", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001686
1687 idx = path->idx[path->depth - 1];
1688 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1689 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1690 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001691 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001693 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695
Takashi Iwai352f7f92012-12-19 12:52:06 +01001696 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1697 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1698 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001699 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001701 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703
Takashi Iwai352f7f92012-12-19 12:52:06 +01001704 path->active = true;
1705 add_loopback_list(spec, mix_nid, idx);
1706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
Takashi Iwai352f7f92012-12-19 12:52:06 +01001709static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001711 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1712 return (pincap & AC_PINCAP_IN) != 0;
1713}
1714
1715/* Parse the codec tree and retrieve ADCs */
1716static int fill_adc_nids(struct hda_codec *codec)
1717{
1718 struct hda_gen_spec *spec = codec->spec;
1719 hda_nid_t nid;
1720 hda_nid_t *adc_nids = spec->adc_nids;
1721 int max_nums = ARRAY_SIZE(spec->adc_nids);
1722 int i, nums = 0;
1723
1724 nid = codec->start_nid;
1725 for (i = 0; i < codec->num_nodes; i++, nid++) {
1726 unsigned int caps = get_wcaps(codec, nid);
1727 int type = get_wcaps_type(caps);
1728
1729 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1730 continue;
1731 adc_nids[nums] = nid;
1732 if (++nums >= max_nums)
1733 break;
1734 }
1735 spec->num_adc_nids = nums;
1736 return nums;
1737}
1738
1739/* filter out invalid adc_nids that don't give all active input pins;
1740 * if needed, check whether dynamic ADC-switching is available
1741 */
1742static int check_dyn_adc_switch(struct hda_codec *codec)
1743{
1744 struct hda_gen_spec *spec = codec->spec;
1745 struct hda_input_mux *imux = &spec->input_mux;
1746 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1747 int i, n, nums;
1748 hda_nid_t pin, adc;
1749
1750 again:
1751 nums = 0;
1752 for (n = 0; n < spec->num_adc_nids; n++) {
1753 adc = spec->adc_nids[n];
1754 for (i = 0; i < imux->num_items; i++) {
1755 pin = spec->imux_pins[i];
1756 if (!is_reachable_path(codec, pin, adc))
1757 break;
1758 }
1759 if (i >= imux->num_items)
1760 adc_nids[nums++] = adc;
1761 }
1762
1763 if (!nums) {
1764 if (spec->shared_mic_hp) {
1765 spec->shared_mic_hp = 0;
1766 imux->num_items = 1;
1767 goto again;
1768 }
1769
1770 /* check whether ADC-switch is possible */
1771 for (i = 0; i < imux->num_items; i++) {
1772 pin = spec->imux_pins[i];
1773 for (n = 0; n < spec->num_adc_nids; n++) {
1774 adc = spec->adc_nids[n];
1775 if (is_reachable_path(codec, pin, adc)) {
1776 spec->dyn_adc_idx[i] = n;
1777 break;
1778 }
1779 }
1780 }
1781
1782 snd_printdd("hda-codec: enabling ADC switching\n");
1783 spec->dyn_adc_switch = 1;
1784 } else if (nums != spec->num_adc_nids) {
1785 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1786 spec->num_adc_nids = nums;
1787 }
1788
1789 if (imux->num_items == 1 || spec->shared_mic_hp) {
1790 snd_printdd("hda-codec: reducing to a single ADC\n");
1791 spec->num_adc_nids = 1; /* reduce to a single ADC */
1792 }
1793
1794 /* single index for individual volumes ctls */
1795 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1796 spec->num_adc_nids = 1;
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return 0;
1799}
1800
1801/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001802 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001804static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001805{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001806 struct hda_gen_spec *spec = codec->spec;
1807 const struct auto_pin_cfg *cfg = &spec->autocfg;
1808 hda_nid_t mixer = spec->mixer_nid;
1809 struct hda_input_mux *imux = &spec->input_mux;
1810 int num_adcs;
1811 int i, c, err, type_idx = 0;
1812 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001813
Takashi Iwai352f7f92012-12-19 12:52:06 +01001814 num_adcs = fill_adc_nids(codec);
1815 if (num_adcs < 0)
1816 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001817
Takashi Iwai352f7f92012-12-19 12:52:06 +01001818 for (i = 0; i < cfg->num_inputs; i++) {
1819 hda_nid_t pin;
1820 const char *label;
1821 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Takashi Iwai352f7f92012-12-19 12:52:06 +01001823 pin = cfg->inputs[i].pin;
1824 if (!is_input_pin(codec, pin))
1825 continue;
1826
1827 label = hda_get_autocfg_input_label(codec, cfg, i);
1828 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1829 label = "Headphone Mic";
1830 if (prev_label && !strcmp(label, prev_label))
1831 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001832 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001833 type_idx = 0;
1834 prev_label = label;
1835
1836 if (mixer) {
1837 if (is_reachable_path(codec, pin, mixer)) {
1838 err = new_analog_input(codec, pin,
1839 label, type_idx, mixer);
1840 if (err < 0)
1841 return err;
1842 }
1843 }
1844
1845 imux_added = false;
1846 for (c = 0; c < num_adcs; c++) {
1847 struct nid_path *path;
1848 hda_nid_t adc = spec->adc_nids[c];
1849
1850 if (!is_reachable_path(codec, pin, adc))
1851 continue;
1852 path = snd_array_new(&spec->paths);
1853 if (!path)
1854 return -ENOMEM;
1855 memset(path, 0, sizeof(*path));
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001856 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001857 snd_printd(KERN_ERR
1858 "invalid input path 0x%x -> 0x%x\n",
1859 pin, adc);
1860 spec->paths.used--;
1861 continue;
1862 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001863 print_nid_path("input", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001864
1865 if (!imux_added) {
1866 spec->imux_pins[imux->num_items] = pin;
1867 snd_hda_add_imux_item(imux, label,
1868 imux->num_items, NULL);
1869 imux_added = true;
1870 }
1871 }
1872 }
1873
1874 return 0;
1875}
1876
1877
1878/*
1879 * input source mux
1880 */
1881
1882/* get the ADC NID corresponding to the given index */
1883static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1884{
1885 struct hda_gen_spec *spec = codec->spec;
1886 if (spec->dyn_adc_switch)
1887 adc_idx = spec->dyn_adc_idx[imux_idx];
1888 return spec->adc_nids[adc_idx];
1889}
1890
1891static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1892 unsigned int idx);
1893
1894static int mux_enum_info(struct snd_kcontrol *kcontrol,
1895 struct snd_ctl_elem_info *uinfo)
1896{
1897 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1898 struct hda_gen_spec *spec = codec->spec;
1899 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1900}
1901
1902static int mux_enum_get(struct snd_kcontrol *kcontrol,
1903 struct snd_ctl_elem_value *ucontrol)
1904{
1905 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1906 struct hda_gen_spec *spec = codec->spec;
1907 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1908
1909 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1910 return 0;
1911}
1912
1913static int mux_enum_put(struct snd_kcontrol *kcontrol,
1914 struct snd_ctl_elem_value *ucontrol)
1915{
1916 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1917 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1918 return mux_select(codec, adc_idx,
1919 ucontrol->value.enumerated.item[0]);
1920}
1921
Takashi Iwai352f7f92012-12-19 12:52:06 +01001922static const struct snd_kcontrol_new cap_src_temp = {
1923 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1924 .name = "Input Source",
1925 .info = mux_enum_info,
1926 .get = mux_enum_get,
1927 .put = mux_enum_put,
1928};
1929
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001930/*
1931 * capture volume and capture switch ctls
1932 */
1933
Takashi Iwai352f7f92012-12-19 12:52:06 +01001934typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
1935 struct snd_ctl_elem_value *ucontrol);
1936
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001937/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001938static int cap_put_caller(struct snd_kcontrol *kcontrol,
1939 struct snd_ctl_elem_value *ucontrol,
1940 put_call_t func, int type)
1941{
1942 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1943 struct hda_gen_spec *spec = codec->spec;
1944 const struct hda_input_mux *imux;
1945 struct nid_path *path;
1946 int i, adc_idx, err = 0;
1947
1948 imux = &spec->input_mux;
1949 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1950 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001951 /* we use the cache-only update at first since multiple input paths
1952 * may shared the same amp; by updating only caches, the redundant
1953 * writes to hardware can be reduced.
1954 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001955 codec->cached_write = 1;
1956 for (i = 0; i < imux->num_items; i++) {
1957 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
1958 get_adc_nid(codec, adc_idx, i));
1959 if (!path->ctls[type])
1960 continue;
1961 kcontrol->private_value = path->ctls[type];
1962 err = func(kcontrol, ucontrol);
1963 if (err < 0)
1964 goto error;
1965 }
1966 error:
1967 codec->cached_write = 0;
1968 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001969 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001970 if (err >= 0 && spec->cap_sync_hook)
1971 spec->cap_sync_hook(codec);
1972 return err;
1973}
1974
1975/* capture volume ctl callbacks */
1976#define cap_vol_info snd_hda_mixer_amp_volume_info
1977#define cap_vol_get snd_hda_mixer_amp_volume_get
1978#define cap_vol_tlv snd_hda_mixer_amp_tlv
1979
1980static int cap_vol_put(struct snd_kcontrol *kcontrol,
1981 struct snd_ctl_elem_value *ucontrol)
1982{
1983 return cap_put_caller(kcontrol, ucontrol,
1984 snd_hda_mixer_amp_volume_put,
1985 NID_PATH_VOL_CTL);
1986}
1987
1988static const struct snd_kcontrol_new cap_vol_temp = {
1989 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1990 .name = "Capture Volume",
1991 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1992 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1993 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
1994 .info = cap_vol_info,
1995 .get = cap_vol_get,
1996 .put = cap_vol_put,
1997 .tlv = { .c = cap_vol_tlv },
1998};
1999
2000/* capture switch ctl callbacks */
2001#define cap_sw_info snd_ctl_boolean_stereo_info
2002#define cap_sw_get snd_hda_mixer_amp_switch_get
2003
2004static int cap_sw_put(struct snd_kcontrol *kcontrol,
2005 struct snd_ctl_elem_value *ucontrol)
2006{
2007 return cap_put_caller(kcontrol, ucontrol,
2008 snd_hda_mixer_amp_switch_put,
2009 NID_PATH_MUTE_CTL);
2010}
2011
2012static const struct snd_kcontrol_new cap_sw_temp = {
2013 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2014 .name = "Capture Switch",
2015 .info = cap_sw_info,
2016 .get = cap_sw_get,
2017 .put = cap_sw_put,
2018};
2019
2020static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2021{
2022 hda_nid_t nid;
2023 int i, depth;
2024
2025 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2026 for (depth = 0; depth < 3; depth++) {
2027 if (depth >= path->depth)
2028 return -EINVAL;
2029 i = path->depth - depth - 1;
2030 nid = path->path[i];
2031 if (!path->ctls[NID_PATH_VOL_CTL]) {
2032 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2033 path->ctls[NID_PATH_VOL_CTL] =
2034 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2035 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2036 int idx = path->idx[i];
2037 if (!depth && codec->single_adc_amp)
2038 idx = 0;
2039 path->ctls[NID_PATH_VOL_CTL] =
2040 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2041 }
2042 }
2043 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2044 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2045 path->ctls[NID_PATH_MUTE_CTL] =
2046 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2047 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2048 int idx = path->idx[i];
2049 if (!depth && codec->single_adc_amp)
2050 idx = 0;
2051 path->ctls[NID_PATH_MUTE_CTL] =
2052 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2053 }
2054 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return 0;
2057}
2058
Takashi Iwai352f7f92012-12-19 12:52:06 +01002059static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002061 struct hda_gen_spec *spec = codec->spec;
2062 struct auto_pin_cfg *cfg = &spec->autocfg;
2063 unsigned int val;
2064 int i;
2065
2066 if (!spec->inv_dmic_split)
2067 return false;
2068 for (i = 0; i < cfg->num_inputs; i++) {
2069 if (cfg->inputs[i].pin != nid)
2070 continue;
2071 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2072 return false;
2073 val = snd_hda_codec_get_pincfg(codec, nid);
2074 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2075 }
2076 return false;
2077}
2078
2079static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2080 int idx, bool is_switch, unsigned int ctl,
2081 bool inv_dmic)
2082{
2083 struct hda_gen_spec *spec = codec->spec;
2084 char tmpname[44];
2085 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2086 const char *sfx = is_switch ? "Switch" : "Volume";
2087 unsigned int chs = inv_dmic ? 1 : 3;
2088 int err;
2089
2090 if (!ctl)
2091 return 0;
2092
2093 if (label)
2094 snprintf(tmpname, sizeof(tmpname),
2095 "%s Capture %s", label, sfx);
2096 else
2097 snprintf(tmpname, sizeof(tmpname),
2098 "Capture %s", sfx);
2099 err = add_control(spec, type, tmpname, idx,
2100 amp_val_replace_channels(ctl, chs));
2101 if (err < 0 || !inv_dmic)
2102 return err;
2103
2104 /* Make independent right kcontrol */
2105 if (label)
2106 snprintf(tmpname, sizeof(tmpname),
2107 "Inverted %s Capture %s", label, sfx);
2108 else
2109 snprintf(tmpname, sizeof(tmpname),
2110 "Inverted Capture %s", sfx);
2111 return add_control(spec, type, tmpname, idx,
2112 amp_val_replace_channels(ctl, 2));
2113}
2114
2115/* create single (and simple) capture volume and switch controls */
2116static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2117 unsigned int vol_ctl, unsigned int sw_ctl,
2118 bool inv_dmic)
2119{
2120 int err;
2121 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2122 if (err < 0)
2123 return err;
2124 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2125 if (err < 0)
2126 return err;
2127 return 0;
2128}
2129
2130/* create bound capture volume and switch controls */
2131static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2132 unsigned int vol_ctl, unsigned int sw_ctl)
2133{
2134 struct hda_gen_spec *spec = codec->spec;
2135 struct snd_kcontrol_new *knew;
2136
2137 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002138 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002139 if (!knew)
2140 return -ENOMEM;
2141 knew->index = idx;
2142 knew->private_value = vol_ctl;
2143 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2144 }
2145 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002146 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002147 if (!knew)
2148 return -ENOMEM;
2149 knew->index = idx;
2150 knew->private_value = sw_ctl;
2151 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2152 }
2153 return 0;
2154}
2155
2156/* return the vol ctl when used first in the imux list */
2157static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2158{
2159 struct hda_gen_spec *spec = codec->spec;
2160 struct nid_path *path;
2161 unsigned int ctl;
2162 int i;
2163
2164 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2165 get_adc_nid(codec, 0, idx));
2166 if (!path)
2167 return 0;
2168 ctl = path->ctls[type];
2169 if (!ctl)
2170 return 0;
2171 for (i = 0; i < idx - 1; i++) {
2172 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2173 get_adc_nid(codec, 0, i));
2174 if (path && path->ctls[type] == ctl)
2175 return 0;
2176 }
2177 return ctl;
2178}
2179
2180/* create individual capture volume and switch controls per input */
2181static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2182{
2183 struct hda_gen_spec *spec = codec->spec;
2184 struct hda_input_mux *imux = &spec->input_mux;
2185 int i, err, type, type_idx = 0;
2186 const char *prev_label = NULL;
2187
2188 for (i = 0; i < imux->num_items; i++) {
2189 const char *label;
2190 bool inv_dmic;
2191 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2192 if (prev_label && !strcmp(label, prev_label))
2193 type_idx++;
2194 else
2195 type_idx = 0;
2196 prev_label = label;
2197 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2198
2199 for (type = 0; type < 2; type++) {
2200 err = add_single_cap_ctl(codec, label, type_idx, type,
2201 get_first_cap_ctl(codec, i, type),
2202 inv_dmic);
2203 if (err < 0)
2204 return err;
2205 }
2206 }
2207 return 0;
2208}
2209
2210static int create_capture_mixers(struct hda_codec *codec)
2211{
2212 struct hda_gen_spec *spec = codec->spec;
2213 struct hda_input_mux *imux = &spec->input_mux;
2214 int i, n, nums, err;
2215
2216 if (spec->dyn_adc_switch)
2217 nums = 1;
2218 else
2219 nums = spec->num_adc_nids;
2220
2221 if (!spec->auto_mic && imux->num_items > 1) {
2222 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002223 const char *name;
2224 name = nums > 1 ? "Input Source" : "Capture Source";
2225 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002226 if (!knew)
2227 return -ENOMEM;
2228 knew->count = nums;
2229 }
2230
2231 for (n = 0; n < nums; n++) {
2232 bool multi = false;
2233 bool inv_dmic = false;
2234 int vol, sw;
2235
2236 vol = sw = 0;
2237 for (i = 0; i < imux->num_items; i++) {
2238 struct nid_path *path;
2239 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2240 get_adc_nid(codec, n, i));
2241 if (!path)
2242 continue;
2243 parse_capvol_in_path(codec, path);
2244 if (!vol)
2245 vol = path->ctls[NID_PATH_VOL_CTL];
2246 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2247 multi = true;
2248 if (!sw)
2249 sw = path->ctls[NID_PATH_MUTE_CTL];
2250 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2251 multi = true;
2252 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2253 inv_dmic = true;
2254 }
2255
2256 if (!multi)
2257 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2258 inv_dmic);
2259 else if (!spec->multi_cap_vol)
2260 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2261 else
2262 err = create_multi_cap_vol_ctl(codec);
2263 if (err < 0)
2264 return err;
2265 }
2266
2267 return 0;
2268}
2269
2270/*
2271 * add mic boosts if needed
2272 */
2273static int parse_mic_boost(struct hda_codec *codec)
2274{
2275 struct hda_gen_spec *spec = codec->spec;
2276 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002277 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002278 int type_idx = 0;
2279 hda_nid_t nid;
2280 const char *prev_label = NULL;
2281
2282 for (i = 0; i < cfg->num_inputs; i++) {
2283 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2284 break;
2285 nid = cfg->inputs[i].pin;
2286 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2287 const char *label;
2288 char boost_label[32];
2289 struct nid_path *path;
2290 unsigned int val;
2291
2292 label = hda_get_autocfg_input_label(codec, cfg, i);
2293 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2294 label = "Headphone Mic";
2295 if (prev_label && !strcmp(label, prev_label))
2296 type_idx++;
2297 else
2298 type_idx = 0;
2299 prev_label = label;
2300
2301 snprintf(boost_label, sizeof(boost_label),
2302 "%s Boost Volume", label);
2303 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2304 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2305 boost_label, type_idx, val);
2306 if (err < 0)
2307 return err;
2308
2309 path = snd_hda_get_nid_path(codec, nid, 0);
2310 if (path)
2311 path->ctls[NID_PATH_BOOST_CTL] = val;
2312 }
2313 }
2314 return 0;
2315}
2316
2317/*
2318 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2319 */
2320static void parse_digital(struct hda_codec *codec)
2321{
2322 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002323 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002324 int i, nums;
2325 hda_nid_t dig_nid;
2326
2327 /* support multiple SPDIFs; the secondary is set up as a slave */
2328 nums = 0;
2329 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2330 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2331 dig_nid = look_for_dac(codec, pin, true);
2332 if (!dig_nid)
2333 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002334 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002335 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002336 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002337 print_nid_path("digout", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002338 if (!nums) {
2339 spec->multiout.dig_out_nid = dig_nid;
2340 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2341 } else {
2342 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2343 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2344 break;
2345 spec->slave_dig_outs[nums - 1] = dig_nid;
2346 }
2347 nums++;
2348 }
2349
2350 if (spec->autocfg.dig_in_pin) {
2351 dig_nid = codec->start_nid;
2352 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002353 unsigned int wcaps = get_wcaps(codec, dig_nid);
2354 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2355 continue;
2356 if (!(wcaps & AC_WCAP_DIGITAL))
2357 continue;
2358 path = snd_hda_add_new_path(codec,
2359 spec->autocfg.dig_in_pin,
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002360 dig_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002361 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002362 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002363 path->active = true;
2364 spec->dig_in_nid = dig_nid;
2365 break;
2366 }
2367 }
2368 }
2369}
2370
2371
2372/*
2373 * input MUX handling
2374 */
2375
2376static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2377
2378/* select the given imux item; either unmute exclusively or select the route */
2379static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2380 unsigned int idx)
2381{
2382 struct hda_gen_spec *spec = codec->spec;
2383 const struct hda_input_mux *imux;
2384 struct nid_path *path;
2385
2386 imux = &spec->input_mux;
2387 if (!imux->num_items)
2388 return 0;
2389
2390 if (idx >= imux->num_items)
2391 idx = imux->num_items - 1;
2392 if (spec->cur_mux[adc_idx] == idx)
2393 return 0;
2394
2395 path = snd_hda_get_nid_path(codec,
2396 spec->imux_pins[spec->cur_mux[adc_idx]],
2397 spec->adc_nids[adc_idx]);
2398 if (!path)
2399 return 0;
2400 if (path->active)
2401 snd_hda_activate_path(codec, path, false, false);
2402
2403 spec->cur_mux[adc_idx] = idx;
2404
2405 if (spec->shared_mic_hp)
2406 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2407
2408 if (spec->dyn_adc_switch)
2409 dyn_adc_pcm_resetup(codec, idx);
2410
2411 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2412 get_adc_nid(codec, adc_idx, idx));
2413 if (!path)
2414 return 0;
2415 if (path->active)
2416 return 0;
2417 snd_hda_activate_path(codec, path, true, false);
2418 if (spec->cap_sync_hook)
2419 spec->cap_sync_hook(codec);
2420 return 1;
2421}
2422
2423
2424/*
2425 * Jack detections for HP auto-mute and mic-switch
2426 */
2427
2428/* check each pin in the given array; returns true if any of them is plugged */
2429static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2430{
2431 int i, present = 0;
2432
2433 for (i = 0; i < num_pins; i++) {
2434 hda_nid_t nid = pins[i];
2435 if (!nid)
2436 break;
2437 present |= snd_hda_jack_detect(codec, nid);
2438 }
2439 return present;
2440}
2441
2442/* standard HP/line-out auto-mute helper */
2443static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2444 bool mute, bool hp_out)
2445{
2446 struct hda_gen_spec *spec = codec->spec;
2447 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2448 int i;
2449
2450 for (i = 0; i < num_pins; i++) {
2451 hda_nid_t nid = pins[i];
2452 unsigned int val;
2453 if (!nid)
2454 break;
2455 /* don't reset VREF value in case it's controlling
2456 * the amp (see alc861_fixup_asus_amp_vref_0f())
2457 */
2458 if (spec->keep_vref_in_automute) {
2459 val = snd_hda_codec_read(codec, nid, 0,
2460 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2461 val &= ~PIN_HP;
2462 } else
2463 val = 0;
2464 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002465 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002466 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002467 }
2468}
2469
2470/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002471void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002472{
2473 struct hda_gen_spec *spec = codec->spec;
2474 int on;
2475
2476 /* Control HP pins/amps depending on master_mute state;
2477 * in general, HP pins/amps control should be enabled in all cases,
2478 * but currently set only for master_mute, just to be safe
2479 */
2480 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2481 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2482 spec->autocfg.hp_pins, spec->master_mute, true);
2483
2484 if (!spec->automute_speaker)
2485 on = 0;
2486 else
2487 on = spec->hp_jack_present | spec->line_jack_present;
2488 on |= spec->master_mute;
2489 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2490 spec->autocfg.speaker_pins, on, false);
2491
2492 /* toggle line-out mutes if needed, too */
2493 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2494 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2495 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2496 return;
2497 if (!spec->automute_lo)
2498 on = 0;
2499 else
2500 on = spec->hp_jack_present;
2501 on |= spec->master_mute;
2502 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2503 spec->autocfg.line_out_pins, on, false);
2504}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002505EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002506
2507static void call_update_outputs(struct hda_codec *codec)
2508{
2509 struct hda_gen_spec *spec = codec->spec;
2510 if (spec->automute_hook)
2511 spec->automute_hook(codec);
2512 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002513 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002514}
2515
2516/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002517void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002518{
2519 struct hda_gen_spec *spec = codec->spec;
2520
2521 spec->hp_jack_present =
2522 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2523 spec->autocfg.hp_pins);
2524 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2525 return;
2526 call_update_outputs(codec);
2527}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002528EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002529
2530/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002531void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002532{
2533 struct hda_gen_spec *spec = codec->spec;
2534
2535 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2536 return;
2537 /* check LO jack only when it's different from HP */
2538 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2539 return;
2540
2541 spec->line_jack_present =
2542 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2543 spec->autocfg.line_out_pins);
2544 if (!spec->automute_speaker || !spec->detect_lo)
2545 return;
2546 call_update_outputs(codec);
2547}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002548EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002549
2550/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002551void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002552{
2553 struct hda_gen_spec *spec = codec->spec;
2554 int i;
2555
2556 if (!spec->auto_mic)
2557 return;
2558
2559 for (i = spec->am_num_entries - 1; i > 0; i--) {
2560 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2561 mux_select(codec, 0, spec->am_entry[i].idx);
2562 return;
2563 }
2564 }
2565 mux_select(codec, 0, spec->am_entry[0].idx);
2566}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002567EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002568
2569/*
2570 * Auto-Mute mode mixer enum support
2571 */
2572static int automute_mode_info(struct snd_kcontrol *kcontrol,
2573 struct snd_ctl_elem_info *uinfo)
2574{
2575 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2576 struct hda_gen_spec *spec = codec->spec;
2577 static const char * const texts3[] = {
2578 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002579 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Takashi Iwai352f7f92012-12-19 12:52:06 +01002581 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2582 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2583 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2584}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Takashi Iwai352f7f92012-12-19 12:52:06 +01002586static int automute_mode_get(struct snd_kcontrol *kcontrol,
2587 struct snd_ctl_elem_value *ucontrol)
2588{
2589 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2590 struct hda_gen_spec *spec = codec->spec;
2591 unsigned int val = 0;
2592 if (spec->automute_speaker)
2593 val++;
2594 if (spec->automute_lo)
2595 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002596
Takashi Iwai352f7f92012-12-19 12:52:06 +01002597 ucontrol->value.enumerated.item[0] = val;
2598 return 0;
2599}
2600
2601static int automute_mode_put(struct snd_kcontrol *kcontrol,
2602 struct snd_ctl_elem_value *ucontrol)
2603{
2604 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2605 struct hda_gen_spec *spec = codec->spec;
2606
2607 switch (ucontrol->value.enumerated.item[0]) {
2608 case 0:
2609 if (!spec->automute_speaker && !spec->automute_lo)
2610 return 0;
2611 spec->automute_speaker = 0;
2612 spec->automute_lo = 0;
2613 break;
2614 case 1:
2615 if (spec->automute_speaker_possible) {
2616 if (!spec->automute_lo && spec->automute_speaker)
2617 return 0;
2618 spec->automute_speaker = 1;
2619 spec->automute_lo = 0;
2620 } else if (spec->automute_lo_possible) {
2621 if (spec->automute_lo)
2622 return 0;
2623 spec->automute_lo = 1;
2624 } else
2625 return -EINVAL;
2626 break;
2627 case 2:
2628 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2629 return -EINVAL;
2630 if (spec->automute_speaker && spec->automute_lo)
2631 return 0;
2632 spec->automute_speaker = 1;
2633 spec->automute_lo = 1;
2634 break;
2635 default:
2636 return -EINVAL;
2637 }
2638 call_update_outputs(codec);
2639 return 1;
2640}
2641
2642static const struct snd_kcontrol_new automute_mode_enum = {
2643 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2644 .name = "Auto-Mute Mode",
2645 .info = automute_mode_info,
2646 .get = automute_mode_get,
2647 .put = automute_mode_put,
2648};
2649
2650static int add_automute_mode_enum(struct hda_codec *codec)
2651{
2652 struct hda_gen_spec *spec = codec->spec;
2653
Takashi Iwai12c93df2012-12-19 14:38:33 +01002654 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002655 return -ENOMEM;
2656 return 0;
2657}
2658
2659/*
2660 * Check the availability of HP/line-out auto-mute;
2661 * Set up appropriately if really supported
2662 */
2663static int check_auto_mute_availability(struct hda_codec *codec)
2664{
2665 struct hda_gen_spec *spec = codec->spec;
2666 struct auto_pin_cfg *cfg = &spec->autocfg;
2667 int present = 0;
2668 int i, err;
2669
2670 if (cfg->hp_pins[0])
2671 present++;
2672 if (cfg->line_out_pins[0])
2673 present++;
2674 if (cfg->speaker_pins[0])
2675 present++;
2676 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002677 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002678
2679 if (!cfg->speaker_pins[0] &&
2680 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2681 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2682 sizeof(cfg->speaker_pins));
2683 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Takashi Iwai352f7f92012-12-19 12:52:06 +01002686 if (!cfg->hp_pins[0] &&
2687 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2688 memcpy(cfg->hp_pins, cfg->line_out_pins,
2689 sizeof(cfg->hp_pins));
2690 cfg->hp_outs = cfg->line_outs;
2691 }
2692
2693 for (i = 0; i < cfg->hp_outs; i++) {
2694 hda_nid_t nid = cfg->hp_pins[i];
2695 if (!is_jack_detectable(codec, nid))
2696 continue;
2697 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2698 nid);
2699 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002700 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002701 spec->detect_hp = 1;
2702 }
2703
2704 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2705 if (cfg->speaker_outs)
2706 for (i = 0; i < cfg->line_outs; i++) {
2707 hda_nid_t nid = cfg->line_out_pins[i];
2708 if (!is_jack_detectable(codec, nid))
2709 continue;
2710 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2711 snd_hda_jack_detect_enable_callback(codec, nid,
2712 HDA_GEN_FRONT_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002713 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002714 spec->detect_lo = 1;
2715 }
2716 spec->automute_lo_possible = spec->detect_hp;
2717 }
2718
2719 spec->automute_speaker_possible = cfg->speaker_outs &&
2720 (spec->detect_hp || spec->detect_lo);
2721
2722 spec->automute_lo = spec->automute_lo_possible;
2723 spec->automute_speaker = spec->automute_speaker_possible;
2724
2725 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2726 /* create a control for automute mode */
2727 err = add_automute_mode_enum(codec);
2728 if (err < 0)
2729 return err;
2730 }
2731 return 0;
2732}
2733
2734/* return the position of NID in the list, or -1 if not found */
2735static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2736{
2737 int i;
2738 for (i = 0; i < nums; i++)
2739 if (list[i] == nid)
2740 return i;
2741 return -1;
2742}
2743
2744/* check whether all auto-mic pins are valid; setup indices if OK */
2745static bool auto_mic_check_imux(struct hda_codec *codec)
2746{
2747 struct hda_gen_spec *spec = codec->spec;
2748 const struct hda_input_mux *imux;
2749 int i;
2750
2751 imux = &spec->input_mux;
2752 for (i = 0; i < spec->am_num_entries; i++) {
2753 spec->am_entry[i].idx =
2754 find_idx_in_nid_list(spec->am_entry[i].pin,
2755 spec->imux_pins, imux->num_items);
2756 if (spec->am_entry[i].idx < 0)
2757 return false; /* no corresponding imux */
2758 }
2759
2760 /* we don't need the jack detection for the first pin */
2761 for (i = 1; i < spec->am_num_entries; i++)
2762 snd_hda_jack_detect_enable_callback(codec,
2763 spec->am_entry[i].pin,
2764 HDA_GEN_MIC_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002765 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002766 return true;
2767}
2768
2769static int compare_attr(const void *ap, const void *bp)
2770{
2771 const struct automic_entry *a = ap;
2772 const struct automic_entry *b = bp;
2773 return (int)(a->attr - b->attr);
2774}
2775
2776/*
2777 * Check the availability of auto-mic switch;
2778 * Set up if really supported
2779 */
2780static int check_auto_mic_availability(struct hda_codec *codec)
2781{
2782 struct hda_gen_spec *spec = codec->spec;
2783 struct auto_pin_cfg *cfg = &spec->autocfg;
2784 unsigned int types;
2785 int i, num_pins;
2786
2787 types = 0;
2788 num_pins = 0;
2789 for (i = 0; i < cfg->num_inputs; i++) {
2790 hda_nid_t nid = cfg->inputs[i].pin;
2791 unsigned int attr;
2792 attr = snd_hda_codec_get_pincfg(codec, nid);
2793 attr = snd_hda_get_input_pin_attr(attr);
2794 if (types & (1 << attr))
2795 return 0; /* already occupied */
2796 switch (attr) {
2797 case INPUT_PIN_ATTR_INT:
2798 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2799 return 0; /* invalid type */
2800 break;
2801 case INPUT_PIN_ATTR_UNUSED:
2802 return 0; /* invalid entry */
2803 default:
2804 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2805 return 0; /* invalid type */
2806 if (!spec->line_in_auto_switch &&
2807 cfg->inputs[i].type != AUTO_PIN_MIC)
2808 return 0; /* only mic is allowed */
2809 if (!is_jack_detectable(codec, nid))
2810 return 0; /* no unsol support */
2811 break;
2812 }
2813 if (num_pins >= MAX_AUTO_MIC_PINS)
2814 return 0;
2815 types |= (1 << attr);
2816 spec->am_entry[num_pins].pin = nid;
2817 spec->am_entry[num_pins].attr = attr;
2818 num_pins++;
2819 }
2820
2821 if (num_pins < 2)
2822 return 0;
2823
2824 spec->am_num_entries = num_pins;
2825 /* sort the am_entry in the order of attr so that the pin with a
2826 * higher attr will be selected when the jack is plugged.
2827 */
2828 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2829 compare_attr, NULL);
2830
2831 if (!auto_mic_check_imux(codec))
2832 return 0;
2833
2834 spec->auto_mic = 1;
2835 spec->num_adc_nids = 1;
2836 spec->cur_mux[0] = spec->am_entry[0].idx;
2837 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2838 spec->am_entry[0].pin,
2839 spec->am_entry[1].pin,
2840 spec->am_entry[2].pin);
2841
2842 return 0;
2843}
2844
2845
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002846/*
2847 * Parse the given BIOS configuration and set up the hda_gen_spec
2848 *
2849 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002850 * or a negative error code
2851 */
2852int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002853 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002854{
2855 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002856 int err;
2857
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002858 if (cfg != &spec->autocfg) {
2859 spec->autocfg = *cfg;
2860 cfg = &spec->autocfg;
2861 }
2862
Takashi Iwai352f7f92012-12-19 12:52:06 +01002863 if (!cfg->line_outs) {
2864 if (cfg->dig_outs || cfg->dig_in_pin) {
2865 spec->multiout.max_channels = 2;
2866 spec->no_analog = 1;
2867 goto dig_only;
2868 }
2869 return 0; /* can't find valid BIOS pin config */
2870 }
2871
2872 if (!spec->no_primary_hp &&
2873 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2874 cfg->line_outs <= cfg->hp_outs) {
2875 /* use HP as primary out */
2876 cfg->speaker_outs = cfg->line_outs;
2877 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2878 sizeof(cfg->speaker_pins));
2879 cfg->line_outs = cfg->hp_outs;
2880 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2881 cfg->hp_outs = 0;
2882 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2883 cfg->line_out_type = AUTO_PIN_HP_OUT;
2884 }
2885
2886 err = parse_output_paths(codec);
2887 if (err < 0)
2888 return err;
2889 err = create_multi_channel_mode(codec);
2890 if (err < 0)
2891 return err;
2892 err = create_multi_out_ctls(codec, cfg);
2893 if (err < 0)
2894 return err;
2895 err = create_hp_out_ctls(codec);
2896 if (err < 0)
2897 return err;
2898 err = create_speaker_out_ctls(codec);
2899 if (err < 0)
2900 return err;
2901 err = create_shared_input(codec);
2902 if (err < 0)
2903 return err;
2904 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002905 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02002906 return err;
2907
Takashi Iwai352f7f92012-12-19 12:52:06 +01002908 /* check the multiple speaker pins */
2909 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2910 spec->const_channel_count = cfg->line_outs * 2;
2911 else
2912 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002913
Takashi Iwai352f7f92012-12-19 12:52:06 +01002914 if (spec->multi_ios > 0)
2915 spec->multiout.max_channels = max(spec->ext_channel_count,
2916 spec->const_channel_count);
2917 else
2918 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2919
2920 err = check_auto_mute_availability(codec);
2921 if (err < 0)
2922 return err;
2923
2924 err = check_dyn_adc_switch(codec);
2925 if (err < 0)
2926 return err;
2927
2928 if (!spec->shared_mic_hp) {
2929 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002930 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02002933
Takashi Iwai352f7f92012-12-19 12:52:06 +01002934 err = create_capture_mixers(codec);
2935 if (err < 0)
2936 return err;
2937
2938 err = parse_mic_boost(codec);
2939 if (err < 0)
2940 return err;
2941
2942 dig_only:
2943 parse_digital(codec);
2944
2945 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002947EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949
2950/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002951 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002953
2954/* slave controls for virtual master */
2955static const char * const slave_pfxs[] = {
2956 "Front", "Surround", "Center", "LFE", "Side",
2957 "Headphone", "Speaker", "Mono", "Line Out",
2958 "CLFE", "Bass Speaker", "PCM",
2959 NULL,
2960};
2961
2962int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002964 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Takashi Iwai36502d02012-12-19 15:15:10 +01002967 if (spec->kctls.used) {
2968 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
2969 if (err < 0)
2970 return err;
2971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Takashi Iwai352f7f92012-12-19 12:52:06 +01002973 if (spec->multiout.dig_out_nid) {
2974 err = snd_hda_create_dig_out_ctls(codec,
2975 spec->multiout.dig_out_nid,
2976 spec->multiout.dig_out_nid,
2977 spec->pcm_rec[1].pcm_type);
2978 if (err < 0)
2979 return err;
2980 if (!spec->no_analog) {
2981 err = snd_hda_create_spdif_share_sw(codec,
2982 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 if (err < 0)
2984 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002985 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
2987 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002988 if (spec->dig_in_nid) {
2989 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
2990 if (err < 0)
2991 return err;
2992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Takashi Iwai352f7f92012-12-19 12:52:06 +01002994 /* if we have no master control, let's create it */
2995 if (!spec->no_analog &&
2996 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
2997 unsigned int vmaster_tlv[4];
2998 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2999 HDA_OUTPUT, vmaster_tlv);
3000 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3001 vmaster_tlv, slave_pfxs,
3002 "Playback Volume");
3003 if (err < 0)
3004 return err;
3005 }
3006 if (!spec->no_analog &&
3007 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3008 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3009 NULL, slave_pfxs,
3010 "Playback Switch",
3011 true, &spec->vmaster_mute.sw_kctl);
3012 if (err < 0)
3013 return err;
3014 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003015 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3016 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Takashi Iwai352f7f92012-12-19 12:52:06 +01003019 free_kctls(spec); /* no longer needed */
3020
3021 if (spec->shared_mic_hp) {
3022 int err;
3023 int nid = spec->autocfg.inputs[1].pin;
3024 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3025 if (err < 0)
3026 return err;
3027 err = snd_hda_jack_detect_enable(codec, nid, 0);
3028 if (err < 0)
3029 return err;
3030 }
3031
3032 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3033 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return err;
3035
3036 return 0;
3037}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003038EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003042 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Takashi Iwai352f7f92012-12-19 12:52:06 +01003045/*
3046 * Analog playback callbacks
3047 */
3048static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3049 struct hda_codec *codec,
3050 struct snd_pcm_substream *substream)
3051{
3052 struct hda_gen_spec *spec = codec->spec;
3053 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
3054 hinfo);
3055}
3056
3057static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003058 struct hda_codec *codec,
3059 unsigned int stream_tag,
3060 unsigned int format,
3061 struct snd_pcm_substream *substream)
3062{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003063 struct hda_gen_spec *spec = codec->spec;
3064 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3065 stream_tag, format, substream);
3066}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003067
Takashi Iwai352f7f92012-12-19 12:52:06 +01003068static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3069 struct hda_codec *codec,
3070 struct snd_pcm_substream *substream)
3071{
3072 struct hda_gen_spec *spec = codec->spec;
3073 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3074}
3075
3076/*
3077 * Digital out
3078 */
3079static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3080 struct hda_codec *codec,
3081 struct snd_pcm_substream *substream)
3082{
3083 struct hda_gen_spec *spec = codec->spec;
3084 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3085}
3086
3087static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3088 struct hda_codec *codec,
3089 unsigned int stream_tag,
3090 unsigned int format,
3091 struct snd_pcm_substream *substream)
3092{
3093 struct hda_gen_spec *spec = codec->spec;
3094 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3095 stream_tag, format, substream);
3096}
3097
3098static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3099 struct hda_codec *codec,
3100 struct snd_pcm_substream *substream)
3101{
3102 struct hda_gen_spec *spec = codec->spec;
3103 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3104}
3105
3106static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3107 struct hda_codec *codec,
3108 struct snd_pcm_substream *substream)
3109{
3110 struct hda_gen_spec *spec = codec->spec;
3111 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3112}
3113
3114/*
3115 * Analog capture
3116 */
3117static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3118 struct hda_codec *codec,
3119 unsigned int stream_tag,
3120 unsigned int format,
3121 struct snd_pcm_substream *substream)
3122{
3123 struct hda_gen_spec *spec = codec->spec;
3124
3125 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003126 stream_tag, 0, format);
3127 return 0;
3128}
3129
Takashi Iwai352f7f92012-12-19 12:52:06 +01003130static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3131 struct hda_codec *codec,
3132 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003133{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003134 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003135
Takashi Iwai352f7f92012-12-19 12:52:06 +01003136 snd_hda_codec_cleanup_stream(codec,
3137 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003138 return 0;
3139}
3140
Takashi Iwai352f7f92012-12-19 12:52:06 +01003141/*
3142 */
3143static const struct hda_pcm_stream pcm_analog_playback = {
3144 .substreams = 1,
3145 .channels_min = 2,
3146 .channels_max = 8,
3147 /* NID is set in build_pcms */
3148 .ops = {
3149 .open = playback_pcm_open,
3150 .prepare = playback_pcm_prepare,
3151 .cleanup = playback_pcm_cleanup
3152 },
3153};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Takashi Iwai352f7f92012-12-19 12:52:06 +01003155static const struct hda_pcm_stream pcm_analog_capture = {
3156 .substreams = 1,
3157 .channels_min = 2,
3158 .channels_max = 2,
3159 /* NID is set in build_pcms */
3160};
3161
3162static const struct hda_pcm_stream pcm_analog_alt_playback = {
3163 .substreams = 1,
3164 .channels_min = 2,
3165 .channels_max = 2,
3166 /* NID is set in build_pcms */
3167};
3168
3169static const struct hda_pcm_stream pcm_analog_alt_capture = {
3170 .substreams = 2, /* can be overridden */
3171 .channels_min = 2,
3172 .channels_max = 2,
3173 /* NID is set in build_pcms */
3174 .ops = {
3175 .prepare = alt_capture_pcm_prepare,
3176 .cleanup = alt_capture_pcm_cleanup
3177 },
3178};
3179
3180static const struct hda_pcm_stream pcm_digital_playback = {
3181 .substreams = 1,
3182 .channels_min = 2,
3183 .channels_max = 2,
3184 /* NID is set in build_pcms */
3185 .ops = {
3186 .open = dig_playback_pcm_open,
3187 .close = dig_playback_pcm_close,
3188 .prepare = dig_playback_pcm_prepare,
3189 .cleanup = dig_playback_pcm_cleanup
3190 },
3191};
3192
3193static const struct hda_pcm_stream pcm_digital_capture = {
3194 .substreams = 1,
3195 .channels_min = 2,
3196 .channels_max = 2,
3197 /* NID is set in build_pcms */
3198};
3199
3200/* Used by build_pcms to flag that a PCM has no playback stream */
3201static const struct hda_pcm_stream pcm_null_stream = {
3202 .substreams = 0,
3203 .channels_min = 0,
3204 .channels_max = 0,
3205};
3206
3207/*
3208 * dynamic changing ADC PCM streams
3209 */
3210static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3211{
3212 struct hda_gen_spec *spec = codec->spec;
3213 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3214
3215 if (spec->cur_adc && spec->cur_adc != new_adc) {
3216 /* stream is running, let's swap the current ADC */
3217 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3218 spec->cur_adc = new_adc;
3219 snd_hda_codec_setup_stream(codec, new_adc,
3220 spec->cur_adc_stream_tag, 0,
3221 spec->cur_adc_format);
3222 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003224 return false;
3225}
3226
3227/* analog capture with dynamic dual-adc changes */
3228static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3229 struct hda_codec *codec,
3230 unsigned int stream_tag,
3231 unsigned int format,
3232 struct snd_pcm_substream *substream)
3233{
3234 struct hda_gen_spec *spec = codec->spec;
3235 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3236 spec->cur_adc_stream_tag = stream_tag;
3237 spec->cur_adc_format = format;
3238 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3239 return 0;
3240}
3241
3242static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3243 struct hda_codec *codec,
3244 struct snd_pcm_substream *substream)
3245{
3246 struct hda_gen_spec *spec = codec->spec;
3247 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3248 spec->cur_adc = 0;
3249 return 0;
3250}
3251
3252static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3253 .substreams = 1,
3254 .channels_min = 2,
3255 .channels_max = 2,
3256 .nid = 0, /* fill later */
3257 .ops = {
3258 .prepare = dyn_adc_capture_pcm_prepare,
3259 .cleanup = dyn_adc_capture_pcm_cleanup
3260 },
3261};
3262
Takashi Iwaif873e532012-12-20 16:58:39 +01003263static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3264 const char *chip_name)
3265{
3266 char *p;
3267
3268 if (*str)
3269 return;
3270 strlcpy(str, chip_name, len);
3271
3272 /* drop non-alnum chars after a space */
3273 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3274 if (!isalnum(p[1])) {
3275 *p = 0;
3276 break;
3277 }
3278 }
3279 strlcat(str, sfx, len);
3280}
3281
Takashi Iwai352f7f92012-12-19 12:52:06 +01003282/* build PCM streams based on the parsed results */
3283int snd_hda_gen_build_pcms(struct hda_codec *codec)
3284{
3285 struct hda_gen_spec *spec = codec->spec;
3286 struct hda_pcm *info = spec->pcm_rec;
3287 const struct hda_pcm_stream *p;
3288 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 codec->num_pcms = 1;
3291 codec->pcm_info = info;
3292
Takashi Iwai352f7f92012-12-19 12:52:06 +01003293 if (spec->no_analog)
3294 goto skip_analog;
3295
Takashi Iwaif873e532012-12-20 16:58:39 +01003296 fill_pcm_stream_name(spec->stream_name_analog,
3297 sizeof(spec->stream_name_analog),
3298 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003299 info->name = spec->stream_name_analog;
3300
3301 if (spec->multiout.num_dacs > 0) {
3302 p = spec->stream_analog_playback;
3303 if (!p)
3304 p = &pcm_analog_playback;
3305 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3306 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3307 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3308 spec->multiout.max_channels;
3309 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3310 spec->autocfg.line_outs == 2)
3311 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3312 snd_pcm_2_1_chmaps;
3313 }
3314 if (spec->num_adc_nids) {
3315 p = spec->stream_analog_capture;
3316 if (!p) {
3317 if (spec->dyn_adc_switch)
3318 p = &dyn_adc_pcm_analog_capture;
3319 else
3320 p = &pcm_analog_capture;
3321 }
3322 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3323 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3324 }
3325
Takashi Iwai352f7f92012-12-19 12:52:06 +01003326 skip_analog:
3327 /* SPDIF for stream index #1 */
3328 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003329 fill_pcm_stream_name(spec->stream_name_digital,
3330 sizeof(spec->stream_name_digital),
3331 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003332 codec->num_pcms = 2;
3333 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3334 info = spec->pcm_rec + 1;
3335 info->name = spec->stream_name_digital;
3336 if (spec->dig_out_type)
3337 info->pcm_type = spec->dig_out_type;
3338 else
3339 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3340 if (spec->multiout.dig_out_nid) {
3341 p = spec->stream_digital_playback;
3342 if (!p)
3343 p = &pcm_digital_playback;
3344 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3345 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3346 }
3347 if (spec->dig_in_nid) {
3348 p = spec->stream_digital_capture;
3349 if (!p)
3350 p = &pcm_digital_capture;
3351 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3352 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3353 }
3354 }
3355
3356 if (spec->no_analog)
3357 return 0;
3358
3359 /* If the use of more than one ADC is requested for the current
3360 * model, configure a second analog capture-only PCM.
3361 */
3362 have_multi_adcs = (spec->num_adc_nids > 1) &&
3363 !spec->dyn_adc_switch && !spec->auto_mic;
3364 /* Additional Analaog capture for index #2 */
3365 if (spec->alt_dac_nid || have_multi_adcs) {
3366 codec->num_pcms = 3;
3367 info = spec->pcm_rec + 2;
3368 info->name = spec->stream_name_analog;
3369 if (spec->alt_dac_nid) {
3370 p = spec->stream_analog_alt_playback;
3371 if (!p)
3372 p = &pcm_analog_alt_playback;
3373 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3374 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3375 spec->alt_dac_nid;
3376 } else {
3377 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3378 pcm_null_stream;
3379 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3380 }
3381 if (have_multi_adcs) {
3382 p = spec->stream_analog_alt_capture;
3383 if (!p)
3384 p = &pcm_analog_alt_capture;
3385 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3386 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3387 spec->adc_nids[1];
3388 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3389 spec->num_adc_nids - 1;
3390 } else {
3391 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3392 pcm_null_stream;
3393 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
3396
3397 return 0;
3398}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003399EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3400
3401
3402/*
3403 * Standard auto-parser initializations
3404 */
3405
3406/* configure the path from the given dac to the pin as the proper output */
3407static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3408 int pin_type, hda_nid_t dac)
3409{
3410 struct nid_path *path;
3411
3412 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3413 path = snd_hda_get_nid_path(codec, dac, pin);
3414 if (!path)
3415 return;
3416 if (path->active)
3417 return;
3418 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003419 set_pin_eapd(codec, pin, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003420}
3421
3422/* initialize primary output paths */
3423static void init_multi_out(struct hda_codec *codec)
3424{
3425 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai64049c82012-12-20 15:29:21 +01003426 hda_nid_t nid, dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003427 int pin_type;
3428 int i;
3429
3430 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3431 pin_type = PIN_HP;
3432 else
3433 pin_type = PIN_OUT;
3434
Takashi Iwai64049c82012-12-20 15:29:21 +01003435 for (i = 0; i < spec->autocfg.line_outs; i++) {
3436 nid = spec->autocfg.line_out_pins[i];
3437 if (nid) {
3438 dac = spec->multiout.dac_nids[i];
3439 if (!dac)
3440 dac = spec->multiout.dac_nids[0];
3441 set_output_and_unmute(codec, nid, pin_type, dac);
3442 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003443 }
3444}
3445
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003446
3447static void __init_extra_out(struct hda_codec *codec, int num_outs,
3448 hda_nid_t *pins, hda_nid_t *dacs, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003449{
3450 struct hda_gen_spec *spec = codec->spec;
3451 int i;
3452 hda_nid_t pin, dac;
3453
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003454 for (i = 0; i < num_outs; i++) {
3455 pin = pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003456 if (!pin)
3457 break;
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003458 dac = dacs[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003459 if (!dac) {
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003460 if (i > 0 && dacs[0])
3461 dac = dacs[0];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003462 else
3463 dac = spec->multiout.dac_nids[0];
3464 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003465 set_output_and_unmute(codec, pin, type, dac);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003466 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003467}
3468
3469/* initialize hp and speaker paths */
3470static void init_extra_out(struct hda_codec *codec)
3471{
3472 struct hda_gen_spec *spec = codec->spec;
3473
3474 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3475 __init_extra_out(codec, spec->autocfg.hp_outs,
3476 spec->autocfg.hp_pins,
3477 spec->multiout.hp_out_nid, PIN_HP);
3478 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3479 __init_extra_out(codec, spec->autocfg.speaker_outs,
3480 spec->autocfg.speaker_pins,
3481 spec->multiout.extra_out_nid, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003482}
3483
3484/* initialize multi-io paths */
3485static void init_multi_io(struct hda_codec *codec)
3486{
3487 struct hda_gen_spec *spec = codec->spec;
3488 int i;
3489
3490 for (i = 0; i < spec->multi_ios; i++) {
3491 hda_nid_t pin = spec->multi_io[i].pin;
3492 struct nid_path *path;
3493 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3494 if (!path)
3495 continue;
3496 if (!spec->multi_io[i].ctl_in)
3497 spec->multi_io[i].ctl_in =
3498 snd_hda_codec_update_cache(codec, pin, 0,
3499 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3500 snd_hda_activate_path(codec, path, path->active, true);
3501 }
3502}
3503
3504/* set up the input pin config, depending on the given auto-pin type */
3505static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3506 int auto_pin_type)
3507{
3508 unsigned int val = PIN_IN;
3509 if (auto_pin_type == AUTO_PIN_MIC)
3510 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003511 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003512}
3513
3514/* set up input pins and loopback paths */
3515static void init_analog_input(struct hda_codec *codec)
3516{
3517 struct hda_gen_spec *spec = codec->spec;
3518 struct auto_pin_cfg *cfg = &spec->autocfg;
3519 int i;
3520
3521 for (i = 0; i < cfg->num_inputs; i++) {
3522 hda_nid_t nid = cfg->inputs[i].pin;
3523 if (is_input_pin(codec, nid))
3524 set_input_pin(codec, nid, cfg->inputs[i].type);
3525
3526 /* init loopback inputs */
3527 if (spec->mixer_nid) {
3528 struct nid_path *path;
3529 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3530 if (path)
3531 snd_hda_activate_path(codec, path,
3532 path->active, false);
3533 }
3534 }
3535}
3536
3537/* initialize ADC paths */
3538static void init_input_src(struct hda_codec *codec)
3539{
3540 struct hda_gen_spec *spec = codec->spec;
3541 struct hda_input_mux *imux = &spec->input_mux;
3542 struct nid_path *path;
3543 int i, c, nums;
3544
3545 if (spec->dyn_adc_switch)
3546 nums = 1;
3547 else
3548 nums = spec->num_adc_nids;
3549
3550 for (c = 0; c < nums; c++) {
3551 for (i = 0; i < imux->num_items; i++) {
3552 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3553 get_adc_nid(codec, c, i));
3554 if (path) {
3555 bool active = path->active;
3556 if (i == spec->cur_mux[c])
3557 active = true;
3558 snd_hda_activate_path(codec, path, active, false);
3559 }
3560 }
3561 }
3562
3563 if (spec->shared_mic_hp)
3564 update_shared_mic_hp(codec, spec->cur_mux[0]);
3565
3566 if (spec->cap_sync_hook)
3567 spec->cap_sync_hook(codec);
3568}
3569
3570/* set right pin controls for digital I/O */
3571static void init_digital(struct hda_codec *codec)
3572{
3573 struct hda_gen_spec *spec = codec->spec;
3574 int i;
3575 hda_nid_t pin;
3576
3577 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3578 pin = spec->autocfg.dig_out_pins[i];
3579 if (!pin)
3580 continue;
3581 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3582 }
3583 pin = spec->autocfg.dig_in_pin;
3584 if (pin)
Takashi Iwai7594aa32012-12-20 15:38:40 +01003585 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003586}
3587
Takashi Iwai973e4972012-12-20 15:16:09 +01003588/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3589 * invalid unsol tags by some reason
3590 */
3591static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3592{
3593 int i;
3594
3595 for (i = 0; i < codec->init_pins.used; i++) {
3596 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3597 hda_nid_t nid = pin->nid;
3598 if (is_jack_detectable(codec, nid) &&
3599 !snd_hda_jack_tbl_get(codec, nid))
3600 snd_hda_codec_update_cache(codec, nid, 0,
3601 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3602 }
3603}
3604
Takashi Iwai352f7f92012-12-19 12:52:06 +01003605int snd_hda_gen_init(struct hda_codec *codec)
3606{
3607 struct hda_gen_spec *spec = codec->spec;
3608
3609 if (spec->init_hook)
3610 spec->init_hook(codec);
3611
3612 snd_hda_apply_verbs(codec);
3613
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003614 codec->cached_write = 1;
3615
Takashi Iwai352f7f92012-12-19 12:52:06 +01003616 init_multi_out(codec);
3617 init_extra_out(codec);
3618 init_multi_io(codec);
3619 init_analog_input(codec);
3620 init_input_src(codec);
3621 init_digital(codec);
3622
Takashi Iwai973e4972012-12-20 15:16:09 +01003623 clear_unsol_on_unused_pins(codec);
3624
Takashi Iwai352f7f92012-12-19 12:52:06 +01003625 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003626 snd_hda_gen_hp_automute(codec, NULL);
3627 snd_hda_gen_line_automute(codec, NULL);
3628 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003629
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003630 snd_hda_codec_flush_amp_cache(codec);
3631 snd_hda_codec_flush_cmd_cache(codec);
3632
Takashi Iwai352f7f92012-12-19 12:52:06 +01003633 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3634 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3635
3636 hda_call_check_power_status(codec, 0x01);
3637 return 0;
3638}
3639EXPORT_SYMBOL(snd_hda_gen_init);
3640
3641
3642/*
3643 * the generic codec support
3644 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
Takashi Iwai83012a72012-08-24 18:38:08 +02003646#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003647static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3648{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003649 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003650 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3651}
3652#endif
3653
Takashi Iwai352f7f92012-12-19 12:52:06 +01003654static void generic_free(struct hda_codec *codec)
3655{
3656 snd_hda_gen_spec_free(codec->spec);
3657 kfree(codec->spec);
3658 codec->spec = NULL;
3659}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Takashi Iwai352f7f92012-12-19 12:52:06 +01003661static const struct hda_codec_ops generic_patch_ops = {
3662 .build_controls = snd_hda_gen_build_controls,
3663 .build_pcms = snd_hda_gen_build_pcms,
3664 .init = snd_hda_gen_init,
3665 .free = generic_free,
3666 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02003667#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003668 .check_power_status = generic_check_power_status,
3669#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670};
3671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672int snd_hda_parse_generic_codec(struct hda_codec *codec)
3673{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003674 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 int err;
3676
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003677 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003678 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003680 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003683 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3684 if (err < 0)
3685 return err;
3686
3687 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003688 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 goto error;
3690
3691 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 return 0;
3693
Takashi Iwai352f7f92012-12-19 12:52:06 +01003694error:
3695 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 return err;
3697}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003698EXPORT_SYMBOL(snd_hda_parse_generic_codec);