blob: f3c6ace2c87fd505f41890824e0ecb3b4dab71a3 [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 Iwaib3a8c742012-12-20 18:29:16 +0100867 if (!path && i > 0 && spec->mixer_nid) {
868 /* try with aamix */
869 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
870 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100871 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100872 dac = dacs[i] = 0;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100873 else
874 print_nid_path("output", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100875 if (dac)
876 badness += assign_out_path_ctls(codec, pin, dac);
877 }
878
879 return badness;
880}
881
882/* return NID if the given pin has only a single connection to a certain DAC */
883static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
884{
885 struct hda_gen_spec *spec = codec->spec;
886 int i;
887 hda_nid_t nid_found = 0;
888
889 for (i = 0; i < spec->num_all_dacs; i++) {
890 hda_nid_t nid = spec->all_dacs[i];
891 if (!nid || is_dac_already_used(codec, nid))
892 continue;
893 if (is_reachable_path(codec, nid, pin)) {
894 if (nid_found)
895 return 0;
896 nid_found = nid;
897 }
898 }
899 return nid_found;
900}
901
902/* check whether the given pin can be a multi-io pin */
903static bool can_be_multiio_pin(struct hda_codec *codec,
904 unsigned int location, hda_nid_t nid)
905{
906 unsigned int defcfg, caps;
907
908 defcfg = snd_hda_codec_get_pincfg(codec, nid);
909 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
910 return false;
911 if (location && get_defcfg_location(defcfg) != location)
912 return false;
913 caps = snd_hda_query_pin_caps(codec, nid);
914 if (!(caps & AC_PINCAP_OUT))
915 return false;
916 return true;
917}
918
919/*
920 * multi-io helper
921 *
922 * When hardwired is set, try to fill ony hardwired pins, and returns
923 * zero if any pins are filled, non-zero if nothing found.
924 * When hardwired is off, try to fill possible input pins, and returns
925 * the badness value.
926 */
927static int fill_multi_ios(struct hda_codec *codec,
928 hda_nid_t reference_pin,
929 bool hardwired, int offset)
930{
931 struct hda_gen_spec *spec = codec->spec;
932 struct auto_pin_cfg *cfg = &spec->autocfg;
933 int type, i, j, dacs, num_pins, old_pins;
934 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
935 unsigned int location = get_defcfg_location(defcfg);
936 int badness = 0;
937
938 old_pins = spec->multi_ios;
939 if (old_pins >= 2)
940 goto end_fill;
941
942 num_pins = 0;
943 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
944 for (i = 0; i < cfg->num_inputs; i++) {
945 if (cfg->inputs[i].type != type)
946 continue;
947 if (can_be_multiio_pin(codec, location,
948 cfg->inputs[i].pin))
949 num_pins++;
950 }
951 }
952 if (num_pins < 2)
953 goto end_fill;
954
955 dacs = spec->multiout.num_dacs;
956 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
957 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100958 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100959 hda_nid_t nid = cfg->inputs[i].pin;
960 hda_nid_t dac = 0;
961
962 if (cfg->inputs[i].type != type)
963 continue;
964 if (!can_be_multiio_pin(codec, location, nid))
965 continue;
966 for (j = 0; j < spec->multi_ios; j++) {
967 if (nid == spec->multi_io[j].pin)
968 break;
969 }
970 if (j < spec->multi_ios)
971 continue;
972
973 if (offset && offset + spec->multi_ios < dacs) {
974 dac = spec->private_dac_nids[offset + spec->multi_ios];
975 if (!is_reachable_path(codec, dac, nid))
976 dac = 0;
977 }
978 if (hardwired)
979 dac = get_dac_if_single(codec, nid);
980 else if (!dac)
981 dac = look_for_dac(codec, nid, false);
982 if (!dac) {
983 badness++;
984 continue;
985 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100986 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100987 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100988 badness++;
989 continue;
990 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100991 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100992 spec->multi_io[spec->multi_ios].pin = nid;
993 spec->multi_io[spec->multi_ios].dac = dac;
994 spec->multi_ios++;
995 if (spec->multi_ios >= 2)
996 break;
997 }
998 }
999 end_fill:
1000 if (badness)
1001 badness = BAD_MULTI_IO;
1002 if (old_pins == spec->multi_ios) {
1003 if (hardwired)
1004 return 1; /* nothing found */
1005 else
1006 return badness; /* no badness if nothing found */
1007 }
1008 if (!hardwired && spec->multi_ios < 2) {
1009 /* cancel newly assigned paths */
1010 spec->paths.used -= spec->multi_ios - old_pins;
1011 spec->multi_ios = old_pins;
1012 return badness;
1013 }
1014
1015 /* assign volume and mute controls */
1016 for (i = old_pins; i < spec->multi_ios; i++)
1017 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1018 spec->multi_io[i].dac);
1019
1020 return badness;
1021}
1022
1023/* map DACs for all pins in the list if they are single connections */
1024static bool map_singles(struct hda_codec *codec, int outs,
1025 const hda_nid_t *pins, hda_nid_t *dacs)
1026{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001027 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001028 int i;
1029 bool found = false;
1030 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001031 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001032 hda_nid_t dac;
1033 if (dacs[i])
1034 continue;
1035 dac = get_dac_if_single(codec, pins[i]);
1036 if (!dac)
1037 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001038 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001039 if (!path && i > 0 && spec->mixer_nid)
1040 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001041 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001042 dacs[i] = dac;
1043 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001044 print_nid_path("output", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001045 }
1046 }
1047 return found;
1048}
1049
1050/* fill in the dac_nids table from the parsed pin configuration */
1051static int fill_and_eval_dacs(struct hda_codec *codec,
1052 bool fill_hardwired,
1053 bool fill_mio_first)
1054{
1055 struct hda_gen_spec *spec = codec->spec;
1056 struct auto_pin_cfg *cfg = &spec->autocfg;
1057 int i, err, badness;
1058
1059 /* set num_dacs once to full for look_for_dac() */
1060 spec->multiout.num_dacs = cfg->line_outs;
1061 spec->multiout.dac_nids = spec->private_dac_nids;
1062 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1063 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1064 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1065 spec->multi_ios = 0;
1066 snd_array_free(&spec->paths);
1067 badness = 0;
1068
1069 /* fill hard-wired DACs first */
1070 if (fill_hardwired) {
1071 bool mapped;
1072 do {
1073 mapped = map_singles(codec, cfg->line_outs,
1074 cfg->line_out_pins,
1075 spec->private_dac_nids);
1076 mapped |= map_singles(codec, cfg->hp_outs,
1077 cfg->hp_pins,
1078 spec->multiout.hp_out_nid);
1079 mapped |= map_singles(codec, cfg->speaker_outs,
1080 cfg->speaker_pins,
1081 spec->multiout.extra_out_nid);
1082 if (fill_mio_first && cfg->line_outs == 1 &&
1083 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1084 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1085 if (!err)
1086 mapped = true;
1087 }
1088 } while (mapped);
1089 }
1090
1091 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1092 spec->private_dac_nids,
1093 &main_out_badness);
1094
1095 /* re-count num_dacs and squash invalid entries */
1096 spec->multiout.num_dacs = 0;
1097 for (i = 0; i < cfg->line_outs; i++) {
1098 if (spec->private_dac_nids[i])
1099 spec->multiout.num_dacs++;
1100 else {
1101 memmove(spec->private_dac_nids + i,
1102 spec->private_dac_nids + i + 1,
1103 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1104 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1105 }
1106 }
1107
1108 if (fill_mio_first &&
1109 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1110 /* try to fill multi-io first */
1111 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1112 if (err < 0)
1113 return err;
1114 /* we don't count badness at this stage yet */
1115 }
1116
1117 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1118 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1119 spec->multiout.hp_out_nid,
1120 &extra_out_badness);
1121 if (err < 0)
1122 return err;
1123 badness += err;
1124 }
1125 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1126 err = try_assign_dacs(codec, cfg->speaker_outs,
1127 cfg->speaker_pins,
1128 spec->multiout.extra_out_nid,
1129 &extra_out_badness);
1130 if (err < 0)
1131 return err;
1132 badness += err;
1133 }
1134 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1135 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1136 if (err < 0)
1137 return err;
1138 badness += err;
1139 }
1140 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1141 /* try multi-ios with HP + inputs */
1142 int offset = 0;
1143 if (cfg->line_outs >= 3)
1144 offset = 1;
1145 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1146 if (err < 0)
1147 return err;
1148 badness += err;
1149 }
1150
1151 if (spec->multi_ios == 2) {
1152 for (i = 0; i < 2; i++)
1153 spec->private_dac_nids[spec->multiout.num_dacs++] =
1154 spec->multi_io[i].dac;
1155 spec->ext_channel_count = 2;
1156 } else if (spec->multi_ios) {
1157 spec->multi_ios = 0;
1158 badness += BAD_MULTI_IO;
1159 }
1160
1161 return badness;
1162}
1163
1164#define DEBUG_BADNESS
1165
1166#ifdef DEBUG_BADNESS
1167#define debug_badness snd_printdd
1168#else
1169#define debug_badness(...)
1170#endif
1171
1172static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1173{
1174 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1175 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001176 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001177 spec->multiout.dac_nids[0],
1178 spec->multiout.dac_nids[1],
1179 spec->multiout.dac_nids[2],
1180 spec->multiout.dac_nids[3]);
1181 if (spec->multi_ios > 0)
1182 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1183 spec->multi_ios,
1184 spec->multi_io[0].pin, spec->multi_io[1].pin,
1185 spec->multi_io[0].dac, spec->multi_io[1].dac);
1186 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1187 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001188 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001189 spec->multiout.hp_out_nid[0],
1190 spec->multiout.hp_out_nid[1],
1191 spec->multiout.hp_out_nid[2],
1192 spec->multiout.hp_out_nid[3]);
1193 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1194 cfg->speaker_pins[0], cfg->speaker_pins[1],
1195 cfg->speaker_pins[2], cfg->speaker_pins[3],
1196 spec->multiout.extra_out_nid[0],
1197 spec->multiout.extra_out_nid[1],
1198 spec->multiout.extra_out_nid[2],
1199 spec->multiout.extra_out_nid[3]);
1200}
1201
1202/* find all available DACs of the codec */
1203static void fill_all_dac_nids(struct hda_codec *codec)
1204{
1205 struct hda_gen_spec *spec = codec->spec;
1206 int i;
1207 hda_nid_t nid = codec->start_nid;
1208
1209 spec->num_all_dacs = 0;
1210 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1211 for (i = 0; i < codec->num_nodes; i++, nid++) {
1212 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1213 continue;
1214 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1215 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1216 break;
1217 }
1218 spec->all_dacs[spec->num_all_dacs++] = nid;
1219 }
1220}
1221
1222static int parse_output_paths(struct hda_codec *codec)
1223{
1224 struct hda_gen_spec *spec = codec->spec;
1225 struct auto_pin_cfg *cfg = &spec->autocfg;
1226 struct auto_pin_cfg *best_cfg;
1227 int best_badness = INT_MAX;
1228 int badness;
1229 bool fill_hardwired = true, fill_mio_first = true;
1230 bool best_wired = true, best_mio = true;
1231 bool hp_spk_swapped = false;
1232
1233 fill_all_dac_nids(codec);
1234
1235 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1236 if (!best_cfg)
1237 return -ENOMEM;
1238 *best_cfg = *cfg;
1239
1240 for (;;) {
1241 badness = fill_and_eval_dacs(codec, fill_hardwired,
1242 fill_mio_first);
1243 if (badness < 0) {
1244 kfree(best_cfg);
1245 return badness;
1246 }
1247 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1248 cfg->line_out_type, fill_hardwired, fill_mio_first,
1249 badness);
1250 debug_show_configs(spec, cfg);
1251 if (badness < best_badness) {
1252 best_badness = badness;
1253 *best_cfg = *cfg;
1254 best_wired = fill_hardwired;
1255 best_mio = fill_mio_first;
1256 }
1257 if (!badness)
1258 break;
1259 fill_mio_first = !fill_mio_first;
1260 if (!fill_mio_first)
1261 continue;
1262 fill_hardwired = !fill_hardwired;
1263 if (!fill_hardwired)
1264 continue;
1265 if (hp_spk_swapped)
1266 break;
1267 hp_spk_swapped = true;
1268 if (cfg->speaker_outs > 0 &&
1269 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1270 cfg->hp_outs = cfg->line_outs;
1271 memcpy(cfg->hp_pins, cfg->line_out_pins,
1272 sizeof(cfg->hp_pins));
1273 cfg->line_outs = cfg->speaker_outs;
1274 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1275 sizeof(cfg->speaker_pins));
1276 cfg->speaker_outs = 0;
1277 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1278 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1279 fill_hardwired = true;
1280 continue;
1281 }
1282 if (cfg->hp_outs > 0 &&
1283 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1284 cfg->speaker_outs = cfg->line_outs;
1285 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1286 sizeof(cfg->speaker_pins));
1287 cfg->line_outs = cfg->hp_outs;
1288 memcpy(cfg->line_out_pins, cfg->hp_pins,
1289 sizeof(cfg->hp_pins));
1290 cfg->hp_outs = 0;
1291 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1292 cfg->line_out_type = AUTO_PIN_HP_OUT;
1293 fill_hardwired = true;
1294 continue;
1295 }
1296 break;
1297 }
1298
1299 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001300 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001301 *cfg = *best_cfg;
1302 fill_and_eval_dacs(codec, best_wired, best_mio);
1303 }
1304 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1305 cfg->line_out_type, best_wired, best_mio);
1306 debug_show_configs(spec, cfg);
1307
1308 if (cfg->line_out_pins[0]) {
1309 struct nid_path *path;
1310 path = snd_hda_get_nid_path(codec,
1311 spec->multiout.dac_nids[0],
1312 cfg->line_out_pins[0]);
1313 if (path)
1314 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1315 }
1316
1317 kfree(best_cfg);
1318 return 0;
1319}
1320
1321/* add playback controls from the parsed DAC table */
1322static int create_multi_out_ctls(struct hda_codec *codec,
1323 const struct auto_pin_cfg *cfg)
1324{
1325 struct hda_gen_spec *spec = codec->spec;
1326 int i, err, noutputs;
1327
1328 noutputs = cfg->line_outs;
1329 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1330 noutputs += spec->multi_ios;
1331
1332 for (i = 0; i < noutputs; i++) {
1333 const char *name;
1334 int index;
1335 hda_nid_t dac, pin;
1336 struct nid_path *path;
1337
1338 dac = spec->multiout.dac_nids[i];
1339 if (!dac)
1340 continue;
1341 if (i >= cfg->line_outs) {
1342 pin = spec->multi_io[i - 1].pin;
1343 index = 0;
1344 name = channel_name[i];
1345 } else {
1346 pin = cfg->line_out_pins[i];
1347 name = get_line_out_pfx(spec, i, true, &index);
1348 }
1349
1350 path = snd_hda_get_nid_path(codec, dac, pin);
1351 if (!path)
1352 continue;
1353 if (!name || !strcmp(name, "CLFE")) {
1354 /* Center/LFE */
1355 err = add_vol_ctl(codec, "Center", 0, 1, path);
1356 if (err < 0)
1357 return err;
1358 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1359 if (err < 0)
1360 return err;
1361 err = add_sw_ctl(codec, "Center", 0, 1, path);
1362 if (err < 0)
1363 return err;
1364 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1365 if (err < 0)
1366 return err;
1367 } else {
1368 err = add_stereo_vol(codec, name, index, path);
1369 if (err < 0)
1370 return err;
1371 err = add_stereo_sw(codec, name, index, path);
1372 if (err < 0)
1373 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375 }
1376 return 0;
1377}
1378
Takashi Iwai352f7f92012-12-19 12:52:06 +01001379static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1380 hda_nid_t dac, const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001382 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 int err;
1384
Takashi Iwai352f7f92012-12-19 12:52:06 +01001385 path = snd_hda_get_nid_path(codec, dac, pin);
1386 if (!path)
1387 return 0;
1388 /* bind volume control will be created in the case of dac = 0 */
1389 if (dac) {
1390 err = add_stereo_vol(codec, pfx, cidx, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (err < 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001392 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001394 err = add_stereo_sw(codec, pfx, cidx, path);
1395 if (err < 0)
1396 return err;
1397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Takashi Iwai352f7f92012-12-19 12:52:06 +01001400/* add playback controls for speaker and HP outputs */
1401static int create_extra_outs(struct hda_codec *codec, int num_pins,
1402 const hda_nid_t *pins, const hda_nid_t *dacs,
1403 const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001405 struct hda_gen_spec *spec = codec->spec;
1406 struct hda_bind_ctls *ctl;
1407 char name[32];
1408 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Takashi Iwai352f7f92012-12-19 12:52:06 +01001410 if (!num_pins || !pins[0])
1411 return 0;
1412
1413 if (num_pins == 1) {
1414 hda_nid_t dac = *dacs;
1415 if (!dac)
1416 dac = spec->multiout.dac_nids[0];
1417 return create_extra_out(codec, *pins, dac, pfx, 0);
Takashi Iwai97ec5582006-03-21 11:29:07 +01001418 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001419
1420 for (i = 0; i < num_pins; i++) {
1421 hda_nid_t dac;
1422 if (dacs[num_pins - 1])
1423 dac = dacs[i]; /* with individual volumes */
Takashi Iwai97ec5582006-03-21 11:29:07 +01001424 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001425 dac = 0;
1426 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1427 err = create_extra_out(codec, pins[i], dac,
1428 "Bass Speaker", 0);
1429 } else if (num_pins >= 3) {
1430 snprintf(name, sizeof(name), "%s %s",
1431 pfx, channel_name[i]);
1432 err = create_extra_out(codec, pins[i], dac, name, 0);
1433 } else {
1434 err = create_extra_out(codec, pins[i], dac, pfx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001436 if (err < 0)
1437 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001439 if (dacs[num_pins - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return 0;
1441
Takashi Iwai352f7f92012-12-19 12:52:06 +01001442 /* Let's create a bind-controls for volumes */
1443 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1444 if (!ctl)
1445 return -ENOMEM;
1446 n = 0;
1447 for (i = 0; i < num_pins; i++) {
1448 hda_nid_t vol;
1449 struct nid_path *path;
1450 if (!pins[i] || !dacs[i])
1451 continue;
1452 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1453 if (!path)
1454 continue;
1455 vol = look_for_out_vol_nid(codec, path);
1456 if (vol)
1457 ctl->values[n++] =
1458 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1459 }
1460 if (n) {
1461 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1462 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1463 if (err < 0)
1464 return err;
1465 }
1466 return 0;
1467}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001468
Takashi Iwai352f7f92012-12-19 12:52:06 +01001469static int create_hp_out_ctls(struct hda_codec *codec)
1470{
1471 struct hda_gen_spec *spec = codec->spec;
1472 return create_extra_outs(codec, spec->autocfg.hp_outs,
1473 spec->autocfg.hp_pins,
1474 spec->multiout.hp_out_nid,
1475 "Headphone");
1476}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Takashi Iwai352f7f92012-12-19 12:52:06 +01001478static int create_speaker_out_ctls(struct hda_codec *codec)
1479{
1480 struct hda_gen_spec *spec = codec->spec;
1481 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1482 spec->autocfg.speaker_pins,
1483 spec->multiout.extra_out_nid,
1484 "Speaker");
1485}
1486
1487/*
1488 * channel mode enum control
1489 */
1490
1491static int ch_mode_info(struct snd_kcontrol *kcontrol,
1492 struct snd_ctl_elem_info *uinfo)
1493{
1494 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1495 struct hda_gen_spec *spec = codec->spec;
1496
1497 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1498 uinfo->count = 1;
1499 uinfo->value.enumerated.items = spec->multi_ios + 1;
1500 if (uinfo->value.enumerated.item > spec->multi_ios)
1501 uinfo->value.enumerated.item = spec->multi_ios;
1502 sprintf(uinfo->value.enumerated.name, "%dch",
1503 (uinfo->value.enumerated.item + 1) * 2);
1504 return 0;
1505}
1506
1507static int ch_mode_get(struct snd_kcontrol *kcontrol,
1508 struct snd_ctl_elem_value *ucontrol)
1509{
1510 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1511 struct hda_gen_spec *spec = codec->spec;
1512 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1513 return 0;
1514}
1515
1516static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1517{
1518 struct hda_gen_spec *spec = codec->spec;
1519 hda_nid_t nid = spec->multi_io[idx].pin;
1520 struct nid_path *path;
1521
1522 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1523 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001525
1526 if (path->active == output)
1527 return 0;
1528
1529 if (output) {
1530 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1531 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001532 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001533 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001534 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001535 snd_hda_activate_path(codec, path, false, true);
1536 snd_hda_set_pin_ctl_cache(codec, nid,
1537 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Takashi Iwai352f7f92012-12-19 12:52:06 +01001542static int ch_mode_put(struct snd_kcontrol *kcontrol,
1543 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001545 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1546 struct hda_gen_spec *spec = codec->spec;
1547 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Takashi Iwai352f7f92012-12-19 12:52:06 +01001549 ch = ucontrol->value.enumerated.item[0];
1550 if (ch < 0 || ch > spec->multi_ios)
1551 return -EINVAL;
1552 if (ch == (spec->ext_channel_count - 1) / 2)
1553 return 0;
1554 spec->ext_channel_count = (ch + 1) * 2;
1555 for (i = 0; i < spec->multi_ios; i++)
1556 set_multi_io(codec, i, i < ch);
1557 spec->multiout.max_channels = max(spec->ext_channel_count,
1558 spec->const_channel_count);
1559 if (spec->need_dac_fix)
1560 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return 1;
1562}
1563
Takashi Iwai352f7f92012-12-19 12:52:06 +01001564static const struct snd_kcontrol_new channel_mode_enum = {
1565 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1566 .name = "Channel Mode",
1567 .info = ch_mode_info,
1568 .get = ch_mode_get,
1569 .put = ch_mode_put,
1570};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Takashi Iwai352f7f92012-12-19 12:52:06 +01001572static int create_multi_channel_mode(struct hda_codec *codec)
1573{
1574 struct hda_gen_spec *spec = codec->spec;
1575
1576 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001577 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001578 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 return 0;
1581}
1582
Takashi Iwai352f7f92012-12-19 12:52:06 +01001583/*
1584 * shared headphone/mic handling
1585 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001586
Takashi Iwai352f7f92012-12-19 12:52:06 +01001587static void call_update_outputs(struct hda_codec *codec);
1588
1589/* for shared I/O, change the pin-control accordingly */
1590static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1591{
1592 struct hda_gen_spec *spec = codec->spec;
1593 unsigned int val;
1594 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1595 /* NOTE: this assumes that there are only two inputs, the
1596 * first is the real internal mic and the second is HP/mic jack.
1597 */
1598
1599 val = snd_hda_get_default_vref(codec, pin);
1600
1601 /* This pin does not have vref caps - let's enable vref on pin 0x18
1602 instead, as suggested by Realtek */
1603 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1604 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1605 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1606 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001607 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1608 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001609 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001610
1611 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001612 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001613
1614 spec->automute_speaker = !set_as_mic;
1615 call_update_outputs(codec);
1616}
1617
1618/* create a shared input with the headphone out */
1619static int create_shared_input(struct hda_codec *codec)
1620{
1621 struct hda_gen_spec *spec = codec->spec;
1622 struct auto_pin_cfg *cfg = &spec->autocfg;
1623 unsigned int defcfg;
1624 hda_nid_t nid;
1625
1626 /* only one internal input pin? */
1627 if (cfg->num_inputs != 1)
1628 return 0;
1629 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1630 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1631 return 0;
1632
1633 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1634 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1635 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1636 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1637 else
1638 return 0; /* both not available */
1639
1640 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1641 return 0; /* no input */
1642
1643 cfg->inputs[1].pin = nid;
1644 cfg->inputs[1].type = AUTO_PIN_MIC;
1645 cfg->num_inputs = 2;
1646 spec->shared_mic_hp = 1;
1647 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1648 return 0;
1649}
1650
1651
1652/*
1653 * Parse input paths
1654 */
1655
1656#ifdef CONFIG_PM
1657/* add the powersave loopback-list entry */
1658static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1659{
1660 struct hda_amp_list *list;
1661
1662 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1663 return;
1664 list = spec->loopback_list + spec->num_loopbacks;
1665 list->nid = mix;
1666 list->dir = HDA_INPUT;
1667 list->idx = idx;
1668 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001669 spec->loopback.amplist = spec->loopback_list;
1670}
1671#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001672#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001673#endif
1674
Takashi Iwai352f7f92012-12-19 12:52:06 +01001675/* create input playback/capture controls for the given pin */
1676static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1677 const char *ctlname, int ctlidx,
1678 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001680 struct hda_gen_spec *spec = codec->spec;
1681 struct nid_path *path;
1682 unsigned int val;
1683 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Takashi Iwai352f7f92012-12-19 12:52:06 +01001685 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1686 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1687 return 0; /* no need for analog loopback */
1688
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001689 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001690 if (!path)
1691 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001692 print_nid_path("loopback", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001693
1694 idx = path->idx[path->depth - 1];
1695 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1696 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1697 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001698 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001700 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
1702
Takashi Iwai352f7f92012-12-19 12:52:06 +01001703 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1704 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1705 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001706 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001708 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
Takashi Iwai352f7f92012-12-19 12:52:06 +01001711 path->active = true;
1712 add_loopback_list(spec, mix_nid, idx);
1713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
Takashi Iwai352f7f92012-12-19 12:52:06 +01001716static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001718 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1719 return (pincap & AC_PINCAP_IN) != 0;
1720}
1721
1722/* Parse the codec tree and retrieve ADCs */
1723static int fill_adc_nids(struct hda_codec *codec)
1724{
1725 struct hda_gen_spec *spec = codec->spec;
1726 hda_nid_t nid;
1727 hda_nid_t *adc_nids = spec->adc_nids;
1728 int max_nums = ARRAY_SIZE(spec->adc_nids);
1729 int i, nums = 0;
1730
1731 nid = codec->start_nid;
1732 for (i = 0; i < codec->num_nodes; i++, nid++) {
1733 unsigned int caps = get_wcaps(codec, nid);
1734 int type = get_wcaps_type(caps);
1735
1736 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1737 continue;
1738 adc_nids[nums] = nid;
1739 if (++nums >= max_nums)
1740 break;
1741 }
1742 spec->num_adc_nids = nums;
1743 return nums;
1744}
1745
1746/* filter out invalid adc_nids that don't give all active input pins;
1747 * if needed, check whether dynamic ADC-switching is available
1748 */
1749static int check_dyn_adc_switch(struct hda_codec *codec)
1750{
1751 struct hda_gen_spec *spec = codec->spec;
1752 struct hda_input_mux *imux = &spec->input_mux;
1753 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1754 int i, n, nums;
1755 hda_nid_t pin, adc;
1756
1757 again:
1758 nums = 0;
1759 for (n = 0; n < spec->num_adc_nids; n++) {
1760 adc = spec->adc_nids[n];
1761 for (i = 0; i < imux->num_items; i++) {
1762 pin = spec->imux_pins[i];
1763 if (!is_reachable_path(codec, pin, adc))
1764 break;
1765 }
1766 if (i >= imux->num_items)
1767 adc_nids[nums++] = adc;
1768 }
1769
1770 if (!nums) {
1771 if (spec->shared_mic_hp) {
1772 spec->shared_mic_hp = 0;
1773 imux->num_items = 1;
1774 goto again;
1775 }
1776
1777 /* check whether ADC-switch is possible */
1778 for (i = 0; i < imux->num_items; i++) {
1779 pin = spec->imux_pins[i];
1780 for (n = 0; n < spec->num_adc_nids; n++) {
1781 adc = spec->adc_nids[n];
1782 if (is_reachable_path(codec, pin, adc)) {
1783 spec->dyn_adc_idx[i] = n;
1784 break;
1785 }
1786 }
1787 }
1788
1789 snd_printdd("hda-codec: enabling ADC switching\n");
1790 spec->dyn_adc_switch = 1;
1791 } else if (nums != spec->num_adc_nids) {
1792 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1793 spec->num_adc_nids = nums;
1794 }
1795
1796 if (imux->num_items == 1 || spec->shared_mic_hp) {
1797 snd_printdd("hda-codec: reducing to a single ADC\n");
1798 spec->num_adc_nids = 1; /* reduce to a single ADC */
1799 }
1800
1801 /* single index for individual volumes ctls */
1802 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1803 spec->num_adc_nids = 1;
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return 0;
1806}
1807
1808/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001809 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001811static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001812{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001813 struct hda_gen_spec *spec = codec->spec;
1814 const struct auto_pin_cfg *cfg = &spec->autocfg;
1815 hda_nid_t mixer = spec->mixer_nid;
1816 struct hda_input_mux *imux = &spec->input_mux;
1817 int num_adcs;
1818 int i, c, err, type_idx = 0;
1819 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001820
Takashi Iwai352f7f92012-12-19 12:52:06 +01001821 num_adcs = fill_adc_nids(codec);
1822 if (num_adcs < 0)
1823 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001824
Takashi Iwai352f7f92012-12-19 12:52:06 +01001825 for (i = 0; i < cfg->num_inputs; i++) {
1826 hda_nid_t pin;
1827 const char *label;
1828 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Takashi Iwai352f7f92012-12-19 12:52:06 +01001830 pin = cfg->inputs[i].pin;
1831 if (!is_input_pin(codec, pin))
1832 continue;
1833
1834 label = hda_get_autocfg_input_label(codec, cfg, i);
1835 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1836 label = "Headphone Mic";
1837 if (prev_label && !strcmp(label, prev_label))
1838 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001839 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001840 type_idx = 0;
1841 prev_label = label;
1842
1843 if (mixer) {
1844 if (is_reachable_path(codec, pin, mixer)) {
1845 err = new_analog_input(codec, pin,
1846 label, type_idx, mixer);
1847 if (err < 0)
1848 return err;
1849 }
1850 }
1851
1852 imux_added = false;
1853 for (c = 0; c < num_adcs; c++) {
1854 struct nid_path *path;
1855 hda_nid_t adc = spec->adc_nids[c];
1856
1857 if (!is_reachable_path(codec, pin, adc))
1858 continue;
1859 path = snd_array_new(&spec->paths);
1860 if (!path)
1861 return -ENOMEM;
1862 memset(path, 0, sizeof(*path));
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001863 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001864 snd_printd(KERN_ERR
1865 "invalid input path 0x%x -> 0x%x\n",
1866 pin, adc);
1867 spec->paths.used--;
1868 continue;
1869 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001870 print_nid_path("input", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001871
1872 if (!imux_added) {
1873 spec->imux_pins[imux->num_items] = pin;
1874 snd_hda_add_imux_item(imux, label,
1875 imux->num_items, NULL);
1876 imux_added = true;
1877 }
1878 }
1879 }
1880
1881 return 0;
1882}
1883
1884
1885/*
1886 * input source mux
1887 */
1888
1889/* get the ADC NID corresponding to the given index */
1890static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1891{
1892 struct hda_gen_spec *spec = codec->spec;
1893 if (spec->dyn_adc_switch)
1894 adc_idx = spec->dyn_adc_idx[imux_idx];
1895 return spec->adc_nids[adc_idx];
1896}
1897
1898static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1899 unsigned int idx);
1900
1901static int mux_enum_info(struct snd_kcontrol *kcontrol,
1902 struct snd_ctl_elem_info *uinfo)
1903{
1904 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1905 struct hda_gen_spec *spec = codec->spec;
1906 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1907}
1908
1909static int mux_enum_get(struct snd_kcontrol *kcontrol,
1910 struct snd_ctl_elem_value *ucontrol)
1911{
1912 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1913 struct hda_gen_spec *spec = codec->spec;
1914 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1915
1916 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1917 return 0;
1918}
1919
1920static int mux_enum_put(struct snd_kcontrol *kcontrol,
1921 struct snd_ctl_elem_value *ucontrol)
1922{
1923 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1924 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1925 return mux_select(codec, adc_idx,
1926 ucontrol->value.enumerated.item[0]);
1927}
1928
Takashi Iwai352f7f92012-12-19 12:52:06 +01001929static const struct snd_kcontrol_new cap_src_temp = {
1930 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1931 .name = "Input Source",
1932 .info = mux_enum_info,
1933 .get = mux_enum_get,
1934 .put = mux_enum_put,
1935};
1936
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001937/*
1938 * capture volume and capture switch ctls
1939 */
1940
Takashi Iwai352f7f92012-12-19 12:52:06 +01001941typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
1942 struct snd_ctl_elem_value *ucontrol);
1943
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001944/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001945static int cap_put_caller(struct snd_kcontrol *kcontrol,
1946 struct snd_ctl_elem_value *ucontrol,
1947 put_call_t func, int type)
1948{
1949 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1950 struct hda_gen_spec *spec = codec->spec;
1951 const struct hda_input_mux *imux;
1952 struct nid_path *path;
1953 int i, adc_idx, err = 0;
1954
1955 imux = &spec->input_mux;
1956 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1957 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001958 /* we use the cache-only update at first since multiple input paths
1959 * may shared the same amp; by updating only caches, the redundant
1960 * writes to hardware can be reduced.
1961 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001962 codec->cached_write = 1;
1963 for (i = 0; i < imux->num_items; i++) {
1964 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
1965 get_adc_nid(codec, adc_idx, i));
1966 if (!path->ctls[type])
1967 continue;
1968 kcontrol->private_value = path->ctls[type];
1969 err = func(kcontrol, ucontrol);
1970 if (err < 0)
1971 goto error;
1972 }
1973 error:
1974 codec->cached_write = 0;
1975 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001976 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001977 if (err >= 0 && spec->cap_sync_hook)
1978 spec->cap_sync_hook(codec);
1979 return err;
1980}
1981
1982/* capture volume ctl callbacks */
1983#define cap_vol_info snd_hda_mixer_amp_volume_info
1984#define cap_vol_get snd_hda_mixer_amp_volume_get
1985#define cap_vol_tlv snd_hda_mixer_amp_tlv
1986
1987static int cap_vol_put(struct snd_kcontrol *kcontrol,
1988 struct snd_ctl_elem_value *ucontrol)
1989{
1990 return cap_put_caller(kcontrol, ucontrol,
1991 snd_hda_mixer_amp_volume_put,
1992 NID_PATH_VOL_CTL);
1993}
1994
1995static const struct snd_kcontrol_new cap_vol_temp = {
1996 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1997 .name = "Capture Volume",
1998 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1999 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2000 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2001 .info = cap_vol_info,
2002 .get = cap_vol_get,
2003 .put = cap_vol_put,
2004 .tlv = { .c = cap_vol_tlv },
2005};
2006
2007/* capture switch ctl callbacks */
2008#define cap_sw_info snd_ctl_boolean_stereo_info
2009#define cap_sw_get snd_hda_mixer_amp_switch_get
2010
2011static int cap_sw_put(struct snd_kcontrol *kcontrol,
2012 struct snd_ctl_elem_value *ucontrol)
2013{
2014 return cap_put_caller(kcontrol, ucontrol,
2015 snd_hda_mixer_amp_switch_put,
2016 NID_PATH_MUTE_CTL);
2017}
2018
2019static const struct snd_kcontrol_new cap_sw_temp = {
2020 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2021 .name = "Capture Switch",
2022 .info = cap_sw_info,
2023 .get = cap_sw_get,
2024 .put = cap_sw_put,
2025};
2026
2027static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2028{
2029 hda_nid_t nid;
2030 int i, depth;
2031
2032 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2033 for (depth = 0; depth < 3; depth++) {
2034 if (depth >= path->depth)
2035 return -EINVAL;
2036 i = path->depth - depth - 1;
2037 nid = path->path[i];
2038 if (!path->ctls[NID_PATH_VOL_CTL]) {
2039 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2040 path->ctls[NID_PATH_VOL_CTL] =
2041 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2042 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2043 int idx = path->idx[i];
2044 if (!depth && codec->single_adc_amp)
2045 idx = 0;
2046 path->ctls[NID_PATH_VOL_CTL] =
2047 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2048 }
2049 }
2050 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2051 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2052 path->ctls[NID_PATH_MUTE_CTL] =
2053 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2054 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2055 int idx = path->idx[i];
2056 if (!depth && codec->single_adc_amp)
2057 idx = 0;
2058 path->ctls[NID_PATH_MUTE_CTL] =
2059 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2060 }
2061 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return 0;
2064}
2065
Takashi Iwai352f7f92012-12-19 12:52:06 +01002066static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002068 struct hda_gen_spec *spec = codec->spec;
2069 struct auto_pin_cfg *cfg = &spec->autocfg;
2070 unsigned int val;
2071 int i;
2072
2073 if (!spec->inv_dmic_split)
2074 return false;
2075 for (i = 0; i < cfg->num_inputs; i++) {
2076 if (cfg->inputs[i].pin != nid)
2077 continue;
2078 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2079 return false;
2080 val = snd_hda_codec_get_pincfg(codec, nid);
2081 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2082 }
2083 return false;
2084}
2085
2086static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2087 int idx, bool is_switch, unsigned int ctl,
2088 bool inv_dmic)
2089{
2090 struct hda_gen_spec *spec = codec->spec;
2091 char tmpname[44];
2092 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2093 const char *sfx = is_switch ? "Switch" : "Volume";
2094 unsigned int chs = inv_dmic ? 1 : 3;
2095 int err;
2096
2097 if (!ctl)
2098 return 0;
2099
2100 if (label)
2101 snprintf(tmpname, sizeof(tmpname),
2102 "%s Capture %s", label, sfx);
2103 else
2104 snprintf(tmpname, sizeof(tmpname),
2105 "Capture %s", sfx);
2106 err = add_control(spec, type, tmpname, idx,
2107 amp_val_replace_channels(ctl, chs));
2108 if (err < 0 || !inv_dmic)
2109 return err;
2110
2111 /* Make independent right kcontrol */
2112 if (label)
2113 snprintf(tmpname, sizeof(tmpname),
2114 "Inverted %s Capture %s", label, sfx);
2115 else
2116 snprintf(tmpname, sizeof(tmpname),
2117 "Inverted Capture %s", sfx);
2118 return add_control(spec, type, tmpname, idx,
2119 amp_val_replace_channels(ctl, 2));
2120}
2121
2122/* create single (and simple) capture volume and switch controls */
2123static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2124 unsigned int vol_ctl, unsigned int sw_ctl,
2125 bool inv_dmic)
2126{
2127 int err;
2128 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2129 if (err < 0)
2130 return err;
2131 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2132 if (err < 0)
2133 return err;
2134 return 0;
2135}
2136
2137/* create bound capture volume and switch controls */
2138static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2139 unsigned int vol_ctl, unsigned int sw_ctl)
2140{
2141 struct hda_gen_spec *spec = codec->spec;
2142 struct snd_kcontrol_new *knew;
2143
2144 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002145 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002146 if (!knew)
2147 return -ENOMEM;
2148 knew->index = idx;
2149 knew->private_value = vol_ctl;
2150 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2151 }
2152 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002153 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002154 if (!knew)
2155 return -ENOMEM;
2156 knew->index = idx;
2157 knew->private_value = sw_ctl;
2158 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2159 }
2160 return 0;
2161}
2162
2163/* return the vol ctl when used first in the imux list */
2164static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2165{
2166 struct hda_gen_spec *spec = codec->spec;
2167 struct nid_path *path;
2168 unsigned int ctl;
2169 int i;
2170
2171 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2172 get_adc_nid(codec, 0, idx));
2173 if (!path)
2174 return 0;
2175 ctl = path->ctls[type];
2176 if (!ctl)
2177 return 0;
2178 for (i = 0; i < idx - 1; i++) {
2179 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2180 get_adc_nid(codec, 0, i));
2181 if (path && path->ctls[type] == ctl)
2182 return 0;
2183 }
2184 return ctl;
2185}
2186
2187/* create individual capture volume and switch controls per input */
2188static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2189{
2190 struct hda_gen_spec *spec = codec->spec;
2191 struct hda_input_mux *imux = &spec->input_mux;
2192 int i, err, type, type_idx = 0;
2193 const char *prev_label = NULL;
2194
2195 for (i = 0; i < imux->num_items; i++) {
2196 const char *label;
2197 bool inv_dmic;
2198 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2199 if (prev_label && !strcmp(label, prev_label))
2200 type_idx++;
2201 else
2202 type_idx = 0;
2203 prev_label = label;
2204 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2205
2206 for (type = 0; type < 2; type++) {
2207 err = add_single_cap_ctl(codec, label, type_idx, type,
2208 get_first_cap_ctl(codec, i, type),
2209 inv_dmic);
2210 if (err < 0)
2211 return err;
2212 }
2213 }
2214 return 0;
2215}
2216
2217static int create_capture_mixers(struct hda_codec *codec)
2218{
2219 struct hda_gen_spec *spec = codec->spec;
2220 struct hda_input_mux *imux = &spec->input_mux;
2221 int i, n, nums, err;
2222
2223 if (spec->dyn_adc_switch)
2224 nums = 1;
2225 else
2226 nums = spec->num_adc_nids;
2227
2228 if (!spec->auto_mic && imux->num_items > 1) {
2229 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002230 const char *name;
2231 name = nums > 1 ? "Input Source" : "Capture Source";
2232 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002233 if (!knew)
2234 return -ENOMEM;
2235 knew->count = nums;
2236 }
2237
2238 for (n = 0; n < nums; n++) {
2239 bool multi = false;
2240 bool inv_dmic = false;
2241 int vol, sw;
2242
2243 vol = sw = 0;
2244 for (i = 0; i < imux->num_items; i++) {
2245 struct nid_path *path;
2246 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2247 get_adc_nid(codec, n, i));
2248 if (!path)
2249 continue;
2250 parse_capvol_in_path(codec, path);
2251 if (!vol)
2252 vol = path->ctls[NID_PATH_VOL_CTL];
2253 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2254 multi = true;
2255 if (!sw)
2256 sw = path->ctls[NID_PATH_MUTE_CTL];
2257 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2258 multi = true;
2259 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2260 inv_dmic = true;
2261 }
2262
2263 if (!multi)
2264 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2265 inv_dmic);
2266 else if (!spec->multi_cap_vol)
2267 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2268 else
2269 err = create_multi_cap_vol_ctl(codec);
2270 if (err < 0)
2271 return err;
2272 }
2273
2274 return 0;
2275}
2276
2277/*
2278 * add mic boosts if needed
2279 */
2280static int parse_mic_boost(struct hda_codec *codec)
2281{
2282 struct hda_gen_spec *spec = codec->spec;
2283 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002284 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002285 int type_idx = 0;
2286 hda_nid_t nid;
2287 const char *prev_label = NULL;
2288
2289 for (i = 0; i < cfg->num_inputs; i++) {
2290 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2291 break;
2292 nid = cfg->inputs[i].pin;
2293 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2294 const char *label;
2295 char boost_label[32];
2296 struct nid_path *path;
2297 unsigned int val;
2298
2299 label = hda_get_autocfg_input_label(codec, cfg, i);
2300 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2301 label = "Headphone Mic";
2302 if (prev_label && !strcmp(label, prev_label))
2303 type_idx++;
2304 else
2305 type_idx = 0;
2306 prev_label = label;
2307
2308 snprintf(boost_label, sizeof(boost_label),
2309 "%s Boost Volume", label);
2310 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2311 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2312 boost_label, type_idx, val);
2313 if (err < 0)
2314 return err;
2315
2316 path = snd_hda_get_nid_path(codec, nid, 0);
2317 if (path)
2318 path->ctls[NID_PATH_BOOST_CTL] = val;
2319 }
2320 }
2321 return 0;
2322}
2323
2324/*
2325 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2326 */
2327static void parse_digital(struct hda_codec *codec)
2328{
2329 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002330 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002331 int i, nums;
2332 hda_nid_t dig_nid;
2333
2334 /* support multiple SPDIFs; the secondary is set up as a slave */
2335 nums = 0;
2336 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2337 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2338 dig_nid = look_for_dac(codec, pin, true);
2339 if (!dig_nid)
2340 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002341 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002342 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002343 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002344 print_nid_path("digout", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002345 if (!nums) {
2346 spec->multiout.dig_out_nid = dig_nid;
2347 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2348 } else {
2349 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2350 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2351 break;
2352 spec->slave_dig_outs[nums - 1] = dig_nid;
2353 }
2354 nums++;
2355 }
2356
2357 if (spec->autocfg.dig_in_pin) {
2358 dig_nid = codec->start_nid;
2359 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002360 unsigned int wcaps = get_wcaps(codec, dig_nid);
2361 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2362 continue;
2363 if (!(wcaps & AC_WCAP_DIGITAL))
2364 continue;
2365 path = snd_hda_add_new_path(codec,
2366 spec->autocfg.dig_in_pin,
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002367 dig_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002368 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002369 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002370 path->active = true;
2371 spec->dig_in_nid = dig_nid;
2372 break;
2373 }
2374 }
2375 }
2376}
2377
2378
2379/*
2380 * input MUX handling
2381 */
2382
2383static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2384
2385/* select the given imux item; either unmute exclusively or select the route */
2386static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2387 unsigned int idx)
2388{
2389 struct hda_gen_spec *spec = codec->spec;
2390 const struct hda_input_mux *imux;
2391 struct nid_path *path;
2392
2393 imux = &spec->input_mux;
2394 if (!imux->num_items)
2395 return 0;
2396
2397 if (idx >= imux->num_items)
2398 idx = imux->num_items - 1;
2399 if (spec->cur_mux[adc_idx] == idx)
2400 return 0;
2401
2402 path = snd_hda_get_nid_path(codec,
2403 spec->imux_pins[spec->cur_mux[adc_idx]],
2404 spec->adc_nids[adc_idx]);
2405 if (!path)
2406 return 0;
2407 if (path->active)
2408 snd_hda_activate_path(codec, path, false, false);
2409
2410 spec->cur_mux[adc_idx] = idx;
2411
2412 if (spec->shared_mic_hp)
2413 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2414
2415 if (spec->dyn_adc_switch)
2416 dyn_adc_pcm_resetup(codec, idx);
2417
2418 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2419 get_adc_nid(codec, adc_idx, idx));
2420 if (!path)
2421 return 0;
2422 if (path->active)
2423 return 0;
2424 snd_hda_activate_path(codec, path, true, false);
2425 if (spec->cap_sync_hook)
2426 spec->cap_sync_hook(codec);
2427 return 1;
2428}
2429
2430
2431/*
2432 * Jack detections for HP auto-mute and mic-switch
2433 */
2434
2435/* check each pin in the given array; returns true if any of them is plugged */
2436static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2437{
2438 int i, present = 0;
2439
2440 for (i = 0; i < num_pins; i++) {
2441 hda_nid_t nid = pins[i];
2442 if (!nid)
2443 break;
2444 present |= snd_hda_jack_detect(codec, nid);
2445 }
2446 return present;
2447}
2448
2449/* standard HP/line-out auto-mute helper */
2450static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2451 bool mute, bool hp_out)
2452{
2453 struct hda_gen_spec *spec = codec->spec;
2454 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2455 int i;
2456
2457 for (i = 0; i < num_pins; i++) {
2458 hda_nid_t nid = pins[i];
2459 unsigned int val;
2460 if (!nid)
2461 break;
2462 /* don't reset VREF value in case it's controlling
2463 * the amp (see alc861_fixup_asus_amp_vref_0f())
2464 */
2465 if (spec->keep_vref_in_automute) {
2466 val = snd_hda_codec_read(codec, nid, 0,
2467 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2468 val &= ~PIN_HP;
2469 } else
2470 val = 0;
2471 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002472 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002473 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002474 }
2475}
2476
2477/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002478void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002479{
2480 struct hda_gen_spec *spec = codec->spec;
2481 int on;
2482
2483 /* Control HP pins/amps depending on master_mute state;
2484 * in general, HP pins/amps control should be enabled in all cases,
2485 * but currently set only for master_mute, just to be safe
2486 */
2487 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2488 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2489 spec->autocfg.hp_pins, spec->master_mute, true);
2490
2491 if (!spec->automute_speaker)
2492 on = 0;
2493 else
2494 on = spec->hp_jack_present | spec->line_jack_present;
2495 on |= spec->master_mute;
2496 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2497 spec->autocfg.speaker_pins, on, false);
2498
2499 /* toggle line-out mutes if needed, too */
2500 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2501 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2502 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2503 return;
2504 if (!spec->automute_lo)
2505 on = 0;
2506 else
2507 on = spec->hp_jack_present;
2508 on |= spec->master_mute;
2509 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2510 spec->autocfg.line_out_pins, on, false);
2511}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002512EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002513
2514static void call_update_outputs(struct hda_codec *codec)
2515{
2516 struct hda_gen_spec *spec = codec->spec;
2517 if (spec->automute_hook)
2518 spec->automute_hook(codec);
2519 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002520 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002521}
2522
2523/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002524void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002525{
2526 struct hda_gen_spec *spec = codec->spec;
2527
2528 spec->hp_jack_present =
2529 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2530 spec->autocfg.hp_pins);
2531 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2532 return;
2533 call_update_outputs(codec);
2534}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002535EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002536
2537/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002538void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002539{
2540 struct hda_gen_spec *spec = codec->spec;
2541
2542 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2543 return;
2544 /* check LO jack only when it's different from HP */
2545 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2546 return;
2547
2548 spec->line_jack_present =
2549 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2550 spec->autocfg.line_out_pins);
2551 if (!spec->automute_speaker || !spec->detect_lo)
2552 return;
2553 call_update_outputs(codec);
2554}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002555EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002556
2557/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002558void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002559{
2560 struct hda_gen_spec *spec = codec->spec;
2561 int i;
2562
2563 if (!spec->auto_mic)
2564 return;
2565
2566 for (i = spec->am_num_entries - 1; i > 0; i--) {
2567 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2568 mux_select(codec, 0, spec->am_entry[i].idx);
2569 return;
2570 }
2571 }
2572 mux_select(codec, 0, spec->am_entry[0].idx);
2573}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002574EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002575
2576/*
2577 * Auto-Mute mode mixer enum support
2578 */
2579static int automute_mode_info(struct snd_kcontrol *kcontrol,
2580 struct snd_ctl_elem_info *uinfo)
2581{
2582 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2583 struct hda_gen_spec *spec = codec->spec;
2584 static const char * const texts3[] = {
2585 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002586 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Takashi Iwai352f7f92012-12-19 12:52:06 +01002588 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2589 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2590 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2591}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Takashi Iwai352f7f92012-12-19 12:52:06 +01002593static int automute_mode_get(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_value *ucontrol)
2595{
2596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2597 struct hda_gen_spec *spec = codec->spec;
2598 unsigned int val = 0;
2599 if (spec->automute_speaker)
2600 val++;
2601 if (spec->automute_lo)
2602 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002603
Takashi Iwai352f7f92012-12-19 12:52:06 +01002604 ucontrol->value.enumerated.item[0] = val;
2605 return 0;
2606}
2607
2608static int automute_mode_put(struct snd_kcontrol *kcontrol,
2609 struct snd_ctl_elem_value *ucontrol)
2610{
2611 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2612 struct hda_gen_spec *spec = codec->spec;
2613
2614 switch (ucontrol->value.enumerated.item[0]) {
2615 case 0:
2616 if (!spec->automute_speaker && !spec->automute_lo)
2617 return 0;
2618 spec->automute_speaker = 0;
2619 spec->automute_lo = 0;
2620 break;
2621 case 1:
2622 if (spec->automute_speaker_possible) {
2623 if (!spec->automute_lo && spec->automute_speaker)
2624 return 0;
2625 spec->automute_speaker = 1;
2626 spec->automute_lo = 0;
2627 } else if (spec->automute_lo_possible) {
2628 if (spec->automute_lo)
2629 return 0;
2630 spec->automute_lo = 1;
2631 } else
2632 return -EINVAL;
2633 break;
2634 case 2:
2635 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2636 return -EINVAL;
2637 if (spec->automute_speaker && spec->automute_lo)
2638 return 0;
2639 spec->automute_speaker = 1;
2640 spec->automute_lo = 1;
2641 break;
2642 default:
2643 return -EINVAL;
2644 }
2645 call_update_outputs(codec);
2646 return 1;
2647}
2648
2649static const struct snd_kcontrol_new automute_mode_enum = {
2650 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2651 .name = "Auto-Mute Mode",
2652 .info = automute_mode_info,
2653 .get = automute_mode_get,
2654 .put = automute_mode_put,
2655};
2656
2657static int add_automute_mode_enum(struct hda_codec *codec)
2658{
2659 struct hda_gen_spec *spec = codec->spec;
2660
Takashi Iwai12c93df2012-12-19 14:38:33 +01002661 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002662 return -ENOMEM;
2663 return 0;
2664}
2665
2666/*
2667 * Check the availability of HP/line-out auto-mute;
2668 * Set up appropriately if really supported
2669 */
2670static int check_auto_mute_availability(struct hda_codec *codec)
2671{
2672 struct hda_gen_spec *spec = codec->spec;
2673 struct auto_pin_cfg *cfg = &spec->autocfg;
2674 int present = 0;
2675 int i, err;
2676
2677 if (cfg->hp_pins[0])
2678 present++;
2679 if (cfg->line_out_pins[0])
2680 present++;
2681 if (cfg->speaker_pins[0])
2682 present++;
2683 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002684 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002685
2686 if (!cfg->speaker_pins[0] &&
2687 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2688 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2689 sizeof(cfg->speaker_pins));
2690 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Takashi Iwai352f7f92012-12-19 12:52:06 +01002693 if (!cfg->hp_pins[0] &&
2694 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2695 memcpy(cfg->hp_pins, cfg->line_out_pins,
2696 sizeof(cfg->hp_pins));
2697 cfg->hp_outs = cfg->line_outs;
2698 }
2699
2700 for (i = 0; i < cfg->hp_outs; i++) {
2701 hda_nid_t nid = cfg->hp_pins[i];
2702 if (!is_jack_detectable(codec, nid))
2703 continue;
2704 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2705 nid);
2706 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002707 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002708 spec->detect_hp = 1;
2709 }
2710
2711 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2712 if (cfg->speaker_outs)
2713 for (i = 0; i < cfg->line_outs; i++) {
2714 hda_nid_t nid = cfg->line_out_pins[i];
2715 if (!is_jack_detectable(codec, nid))
2716 continue;
2717 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2718 snd_hda_jack_detect_enable_callback(codec, nid,
2719 HDA_GEN_FRONT_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002720 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002721 spec->detect_lo = 1;
2722 }
2723 spec->automute_lo_possible = spec->detect_hp;
2724 }
2725
2726 spec->automute_speaker_possible = cfg->speaker_outs &&
2727 (spec->detect_hp || spec->detect_lo);
2728
2729 spec->automute_lo = spec->automute_lo_possible;
2730 spec->automute_speaker = spec->automute_speaker_possible;
2731
2732 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2733 /* create a control for automute mode */
2734 err = add_automute_mode_enum(codec);
2735 if (err < 0)
2736 return err;
2737 }
2738 return 0;
2739}
2740
2741/* return the position of NID in the list, or -1 if not found */
2742static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2743{
2744 int i;
2745 for (i = 0; i < nums; i++)
2746 if (list[i] == nid)
2747 return i;
2748 return -1;
2749}
2750
2751/* check whether all auto-mic pins are valid; setup indices if OK */
2752static bool auto_mic_check_imux(struct hda_codec *codec)
2753{
2754 struct hda_gen_spec *spec = codec->spec;
2755 const struct hda_input_mux *imux;
2756 int i;
2757
2758 imux = &spec->input_mux;
2759 for (i = 0; i < spec->am_num_entries; i++) {
2760 spec->am_entry[i].idx =
2761 find_idx_in_nid_list(spec->am_entry[i].pin,
2762 spec->imux_pins, imux->num_items);
2763 if (spec->am_entry[i].idx < 0)
2764 return false; /* no corresponding imux */
2765 }
2766
2767 /* we don't need the jack detection for the first pin */
2768 for (i = 1; i < spec->am_num_entries; i++)
2769 snd_hda_jack_detect_enable_callback(codec,
2770 spec->am_entry[i].pin,
2771 HDA_GEN_MIC_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002772 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002773 return true;
2774}
2775
2776static int compare_attr(const void *ap, const void *bp)
2777{
2778 const struct automic_entry *a = ap;
2779 const struct automic_entry *b = bp;
2780 return (int)(a->attr - b->attr);
2781}
2782
2783/*
2784 * Check the availability of auto-mic switch;
2785 * Set up if really supported
2786 */
2787static int check_auto_mic_availability(struct hda_codec *codec)
2788{
2789 struct hda_gen_spec *spec = codec->spec;
2790 struct auto_pin_cfg *cfg = &spec->autocfg;
2791 unsigned int types;
2792 int i, num_pins;
2793
2794 types = 0;
2795 num_pins = 0;
2796 for (i = 0; i < cfg->num_inputs; i++) {
2797 hda_nid_t nid = cfg->inputs[i].pin;
2798 unsigned int attr;
2799 attr = snd_hda_codec_get_pincfg(codec, nid);
2800 attr = snd_hda_get_input_pin_attr(attr);
2801 if (types & (1 << attr))
2802 return 0; /* already occupied */
2803 switch (attr) {
2804 case INPUT_PIN_ATTR_INT:
2805 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2806 return 0; /* invalid type */
2807 break;
2808 case INPUT_PIN_ATTR_UNUSED:
2809 return 0; /* invalid entry */
2810 default:
2811 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2812 return 0; /* invalid type */
2813 if (!spec->line_in_auto_switch &&
2814 cfg->inputs[i].type != AUTO_PIN_MIC)
2815 return 0; /* only mic is allowed */
2816 if (!is_jack_detectable(codec, nid))
2817 return 0; /* no unsol support */
2818 break;
2819 }
2820 if (num_pins >= MAX_AUTO_MIC_PINS)
2821 return 0;
2822 types |= (1 << attr);
2823 spec->am_entry[num_pins].pin = nid;
2824 spec->am_entry[num_pins].attr = attr;
2825 num_pins++;
2826 }
2827
2828 if (num_pins < 2)
2829 return 0;
2830
2831 spec->am_num_entries = num_pins;
2832 /* sort the am_entry in the order of attr so that the pin with a
2833 * higher attr will be selected when the jack is plugged.
2834 */
2835 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2836 compare_attr, NULL);
2837
2838 if (!auto_mic_check_imux(codec))
2839 return 0;
2840
2841 spec->auto_mic = 1;
2842 spec->num_adc_nids = 1;
2843 spec->cur_mux[0] = spec->am_entry[0].idx;
2844 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2845 spec->am_entry[0].pin,
2846 spec->am_entry[1].pin,
2847 spec->am_entry[2].pin);
2848
2849 return 0;
2850}
2851
2852
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002853/*
2854 * Parse the given BIOS configuration and set up the hda_gen_spec
2855 *
2856 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002857 * or a negative error code
2858 */
2859int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002860 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002861{
2862 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002863 int err;
2864
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002865 if (cfg != &spec->autocfg) {
2866 spec->autocfg = *cfg;
2867 cfg = &spec->autocfg;
2868 }
2869
Takashi Iwai352f7f92012-12-19 12:52:06 +01002870 if (!cfg->line_outs) {
2871 if (cfg->dig_outs || cfg->dig_in_pin) {
2872 spec->multiout.max_channels = 2;
2873 spec->no_analog = 1;
2874 goto dig_only;
2875 }
2876 return 0; /* can't find valid BIOS pin config */
2877 }
2878
2879 if (!spec->no_primary_hp &&
2880 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2881 cfg->line_outs <= cfg->hp_outs) {
2882 /* use HP as primary out */
2883 cfg->speaker_outs = cfg->line_outs;
2884 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2885 sizeof(cfg->speaker_pins));
2886 cfg->line_outs = cfg->hp_outs;
2887 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2888 cfg->hp_outs = 0;
2889 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2890 cfg->line_out_type = AUTO_PIN_HP_OUT;
2891 }
2892
2893 err = parse_output_paths(codec);
2894 if (err < 0)
2895 return err;
2896 err = create_multi_channel_mode(codec);
2897 if (err < 0)
2898 return err;
2899 err = create_multi_out_ctls(codec, cfg);
2900 if (err < 0)
2901 return err;
2902 err = create_hp_out_ctls(codec);
2903 if (err < 0)
2904 return err;
2905 err = create_speaker_out_ctls(codec);
2906 if (err < 0)
2907 return err;
2908 err = create_shared_input(codec);
2909 if (err < 0)
2910 return err;
2911 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002912 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02002913 return err;
2914
Takashi Iwai352f7f92012-12-19 12:52:06 +01002915 /* check the multiple speaker pins */
2916 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2917 spec->const_channel_count = cfg->line_outs * 2;
2918 else
2919 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002920
Takashi Iwai352f7f92012-12-19 12:52:06 +01002921 if (spec->multi_ios > 0)
2922 spec->multiout.max_channels = max(spec->ext_channel_count,
2923 spec->const_channel_count);
2924 else
2925 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2926
2927 err = check_auto_mute_availability(codec);
2928 if (err < 0)
2929 return err;
2930
2931 err = check_dyn_adc_switch(codec);
2932 if (err < 0)
2933 return err;
2934
2935 if (!spec->shared_mic_hp) {
2936 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002937 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02002940
Takashi Iwai352f7f92012-12-19 12:52:06 +01002941 err = create_capture_mixers(codec);
2942 if (err < 0)
2943 return err;
2944
2945 err = parse_mic_boost(codec);
2946 if (err < 0)
2947 return err;
2948
2949 dig_only:
2950 parse_digital(codec);
2951
2952 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002954EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956
2957/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002958 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002960
2961/* slave controls for virtual master */
2962static const char * const slave_pfxs[] = {
2963 "Front", "Surround", "Center", "LFE", "Side",
2964 "Headphone", "Speaker", "Mono", "Line Out",
2965 "CLFE", "Bass Speaker", "PCM",
2966 NULL,
2967};
2968
2969int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002971 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Takashi Iwai36502d02012-12-19 15:15:10 +01002974 if (spec->kctls.used) {
2975 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
2976 if (err < 0)
2977 return err;
2978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Takashi Iwai352f7f92012-12-19 12:52:06 +01002980 if (spec->multiout.dig_out_nid) {
2981 err = snd_hda_create_dig_out_ctls(codec,
2982 spec->multiout.dig_out_nid,
2983 spec->multiout.dig_out_nid,
2984 spec->pcm_rec[1].pcm_type);
2985 if (err < 0)
2986 return err;
2987 if (!spec->no_analog) {
2988 err = snd_hda_create_spdif_share_sw(codec,
2989 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 if (err < 0)
2991 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002992 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 }
2994 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002995 if (spec->dig_in_nid) {
2996 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
2997 if (err < 0)
2998 return err;
2999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Takashi Iwai352f7f92012-12-19 12:52:06 +01003001 /* if we have no master control, let's create it */
3002 if (!spec->no_analog &&
3003 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3004 unsigned int vmaster_tlv[4];
3005 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3006 HDA_OUTPUT, vmaster_tlv);
3007 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3008 vmaster_tlv, slave_pfxs,
3009 "Playback Volume");
3010 if (err < 0)
3011 return err;
3012 }
3013 if (!spec->no_analog &&
3014 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3015 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3016 NULL, slave_pfxs,
3017 "Playback Switch",
3018 true, &spec->vmaster_mute.sw_kctl);
3019 if (err < 0)
3020 return err;
3021 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003022 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3023 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Takashi Iwai352f7f92012-12-19 12:52:06 +01003026 free_kctls(spec); /* no longer needed */
3027
3028 if (spec->shared_mic_hp) {
3029 int err;
3030 int nid = spec->autocfg.inputs[1].pin;
3031 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3032 if (err < 0)
3033 return err;
3034 err = snd_hda_jack_detect_enable(codec, nid, 0);
3035 if (err < 0)
3036 return err;
3037 }
3038
3039 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3040 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return err;
3042
3043 return 0;
3044}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003045EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
3048/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003049 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Takashi Iwai352f7f92012-12-19 12:52:06 +01003052/*
3053 * Analog playback callbacks
3054 */
3055static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3056 struct hda_codec *codec,
3057 struct snd_pcm_substream *substream)
3058{
3059 struct hda_gen_spec *spec = codec->spec;
3060 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
3061 hinfo);
3062}
3063
3064static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003065 struct hda_codec *codec,
3066 unsigned int stream_tag,
3067 unsigned int format,
3068 struct snd_pcm_substream *substream)
3069{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003070 struct hda_gen_spec *spec = codec->spec;
3071 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3072 stream_tag, format, substream);
3073}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003074
Takashi Iwai352f7f92012-12-19 12:52:06 +01003075static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3076 struct hda_codec *codec,
3077 struct snd_pcm_substream *substream)
3078{
3079 struct hda_gen_spec *spec = codec->spec;
3080 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3081}
3082
3083/*
3084 * Digital out
3085 */
3086static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3087 struct hda_codec *codec,
3088 struct snd_pcm_substream *substream)
3089{
3090 struct hda_gen_spec *spec = codec->spec;
3091 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3092}
3093
3094static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3095 struct hda_codec *codec,
3096 unsigned int stream_tag,
3097 unsigned int format,
3098 struct snd_pcm_substream *substream)
3099{
3100 struct hda_gen_spec *spec = codec->spec;
3101 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3102 stream_tag, format, substream);
3103}
3104
3105static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3106 struct hda_codec *codec,
3107 struct snd_pcm_substream *substream)
3108{
3109 struct hda_gen_spec *spec = codec->spec;
3110 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3111}
3112
3113static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3114 struct hda_codec *codec,
3115 struct snd_pcm_substream *substream)
3116{
3117 struct hda_gen_spec *spec = codec->spec;
3118 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3119}
3120
3121/*
3122 * Analog capture
3123 */
3124static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3125 struct hda_codec *codec,
3126 unsigned int stream_tag,
3127 unsigned int format,
3128 struct snd_pcm_substream *substream)
3129{
3130 struct hda_gen_spec *spec = codec->spec;
3131
3132 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003133 stream_tag, 0, format);
3134 return 0;
3135}
3136
Takashi Iwai352f7f92012-12-19 12:52:06 +01003137static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3138 struct hda_codec *codec,
3139 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003140{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003141 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003142
Takashi Iwai352f7f92012-12-19 12:52:06 +01003143 snd_hda_codec_cleanup_stream(codec,
3144 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003145 return 0;
3146}
3147
Takashi Iwai352f7f92012-12-19 12:52:06 +01003148/*
3149 */
3150static const struct hda_pcm_stream pcm_analog_playback = {
3151 .substreams = 1,
3152 .channels_min = 2,
3153 .channels_max = 8,
3154 /* NID is set in build_pcms */
3155 .ops = {
3156 .open = playback_pcm_open,
3157 .prepare = playback_pcm_prepare,
3158 .cleanup = playback_pcm_cleanup
3159 },
3160};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Takashi Iwai352f7f92012-12-19 12:52:06 +01003162static const struct hda_pcm_stream pcm_analog_capture = {
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_playback = {
3170 .substreams = 1,
3171 .channels_min = 2,
3172 .channels_max = 2,
3173 /* NID is set in build_pcms */
3174};
3175
3176static const struct hda_pcm_stream pcm_analog_alt_capture = {
3177 .substreams = 2, /* can be overridden */
3178 .channels_min = 2,
3179 .channels_max = 2,
3180 /* NID is set in build_pcms */
3181 .ops = {
3182 .prepare = alt_capture_pcm_prepare,
3183 .cleanup = alt_capture_pcm_cleanup
3184 },
3185};
3186
3187static const struct hda_pcm_stream pcm_digital_playback = {
3188 .substreams = 1,
3189 .channels_min = 2,
3190 .channels_max = 2,
3191 /* NID is set in build_pcms */
3192 .ops = {
3193 .open = dig_playback_pcm_open,
3194 .close = dig_playback_pcm_close,
3195 .prepare = dig_playback_pcm_prepare,
3196 .cleanup = dig_playback_pcm_cleanup
3197 },
3198};
3199
3200static const struct hda_pcm_stream pcm_digital_capture = {
3201 .substreams = 1,
3202 .channels_min = 2,
3203 .channels_max = 2,
3204 /* NID is set in build_pcms */
3205};
3206
3207/* Used by build_pcms to flag that a PCM has no playback stream */
3208static const struct hda_pcm_stream pcm_null_stream = {
3209 .substreams = 0,
3210 .channels_min = 0,
3211 .channels_max = 0,
3212};
3213
3214/*
3215 * dynamic changing ADC PCM streams
3216 */
3217static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3218{
3219 struct hda_gen_spec *spec = codec->spec;
3220 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3221
3222 if (spec->cur_adc && spec->cur_adc != new_adc) {
3223 /* stream is running, let's swap the current ADC */
3224 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3225 spec->cur_adc = new_adc;
3226 snd_hda_codec_setup_stream(codec, new_adc,
3227 spec->cur_adc_stream_tag, 0,
3228 spec->cur_adc_format);
3229 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003231 return false;
3232}
3233
3234/* analog capture with dynamic dual-adc changes */
3235static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3236 struct hda_codec *codec,
3237 unsigned int stream_tag,
3238 unsigned int format,
3239 struct snd_pcm_substream *substream)
3240{
3241 struct hda_gen_spec *spec = codec->spec;
3242 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3243 spec->cur_adc_stream_tag = stream_tag;
3244 spec->cur_adc_format = format;
3245 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3246 return 0;
3247}
3248
3249static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3250 struct hda_codec *codec,
3251 struct snd_pcm_substream *substream)
3252{
3253 struct hda_gen_spec *spec = codec->spec;
3254 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3255 spec->cur_adc = 0;
3256 return 0;
3257}
3258
3259static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3260 .substreams = 1,
3261 .channels_min = 2,
3262 .channels_max = 2,
3263 .nid = 0, /* fill later */
3264 .ops = {
3265 .prepare = dyn_adc_capture_pcm_prepare,
3266 .cleanup = dyn_adc_capture_pcm_cleanup
3267 },
3268};
3269
Takashi Iwaif873e532012-12-20 16:58:39 +01003270static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3271 const char *chip_name)
3272{
3273 char *p;
3274
3275 if (*str)
3276 return;
3277 strlcpy(str, chip_name, len);
3278
3279 /* drop non-alnum chars after a space */
3280 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3281 if (!isalnum(p[1])) {
3282 *p = 0;
3283 break;
3284 }
3285 }
3286 strlcat(str, sfx, len);
3287}
3288
Takashi Iwai352f7f92012-12-19 12:52:06 +01003289/* build PCM streams based on the parsed results */
3290int snd_hda_gen_build_pcms(struct hda_codec *codec)
3291{
3292 struct hda_gen_spec *spec = codec->spec;
3293 struct hda_pcm *info = spec->pcm_rec;
3294 const struct hda_pcm_stream *p;
3295 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
3297 codec->num_pcms = 1;
3298 codec->pcm_info = info;
3299
Takashi Iwai352f7f92012-12-19 12:52:06 +01003300 if (spec->no_analog)
3301 goto skip_analog;
3302
Takashi Iwaif873e532012-12-20 16:58:39 +01003303 fill_pcm_stream_name(spec->stream_name_analog,
3304 sizeof(spec->stream_name_analog),
3305 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003306 info->name = spec->stream_name_analog;
3307
3308 if (spec->multiout.num_dacs > 0) {
3309 p = spec->stream_analog_playback;
3310 if (!p)
3311 p = &pcm_analog_playback;
3312 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3313 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3314 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3315 spec->multiout.max_channels;
3316 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3317 spec->autocfg.line_outs == 2)
3318 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3319 snd_pcm_2_1_chmaps;
3320 }
3321 if (spec->num_adc_nids) {
3322 p = spec->stream_analog_capture;
3323 if (!p) {
3324 if (spec->dyn_adc_switch)
3325 p = &dyn_adc_pcm_analog_capture;
3326 else
3327 p = &pcm_analog_capture;
3328 }
3329 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3330 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3331 }
3332
Takashi Iwai352f7f92012-12-19 12:52:06 +01003333 skip_analog:
3334 /* SPDIF for stream index #1 */
3335 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003336 fill_pcm_stream_name(spec->stream_name_digital,
3337 sizeof(spec->stream_name_digital),
3338 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003339 codec->num_pcms = 2;
3340 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3341 info = spec->pcm_rec + 1;
3342 info->name = spec->stream_name_digital;
3343 if (spec->dig_out_type)
3344 info->pcm_type = spec->dig_out_type;
3345 else
3346 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3347 if (spec->multiout.dig_out_nid) {
3348 p = spec->stream_digital_playback;
3349 if (!p)
3350 p = &pcm_digital_playback;
3351 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3352 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3353 }
3354 if (spec->dig_in_nid) {
3355 p = spec->stream_digital_capture;
3356 if (!p)
3357 p = &pcm_digital_capture;
3358 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3359 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3360 }
3361 }
3362
3363 if (spec->no_analog)
3364 return 0;
3365
3366 /* If the use of more than one ADC is requested for the current
3367 * model, configure a second analog capture-only PCM.
3368 */
3369 have_multi_adcs = (spec->num_adc_nids > 1) &&
3370 !spec->dyn_adc_switch && !spec->auto_mic;
3371 /* Additional Analaog capture for index #2 */
3372 if (spec->alt_dac_nid || have_multi_adcs) {
3373 codec->num_pcms = 3;
3374 info = spec->pcm_rec + 2;
3375 info->name = spec->stream_name_analog;
3376 if (spec->alt_dac_nid) {
3377 p = spec->stream_analog_alt_playback;
3378 if (!p)
3379 p = &pcm_analog_alt_playback;
3380 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3381 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3382 spec->alt_dac_nid;
3383 } else {
3384 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3385 pcm_null_stream;
3386 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3387 }
3388 if (have_multi_adcs) {
3389 p = spec->stream_analog_alt_capture;
3390 if (!p)
3391 p = &pcm_analog_alt_capture;
3392 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3393 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3394 spec->adc_nids[1];
3395 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3396 spec->num_adc_nids - 1;
3397 } else {
3398 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3399 pcm_null_stream;
3400 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 }
3403
3404 return 0;
3405}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003406EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3407
3408
3409/*
3410 * Standard auto-parser initializations
3411 */
3412
3413/* configure the path from the given dac to the pin as the proper output */
3414static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3415 int pin_type, hda_nid_t dac)
3416{
3417 struct nid_path *path;
3418
3419 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3420 path = snd_hda_get_nid_path(codec, dac, pin);
3421 if (!path)
3422 return;
3423 if (path->active)
3424 return;
3425 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003426 set_pin_eapd(codec, pin, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003427}
3428
3429/* initialize primary output paths */
3430static void init_multi_out(struct hda_codec *codec)
3431{
3432 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai64049c82012-12-20 15:29:21 +01003433 hda_nid_t nid, dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003434 int pin_type;
3435 int i;
3436
3437 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3438 pin_type = PIN_HP;
3439 else
3440 pin_type = PIN_OUT;
3441
Takashi Iwai64049c82012-12-20 15:29:21 +01003442 for (i = 0; i < spec->autocfg.line_outs; i++) {
3443 nid = spec->autocfg.line_out_pins[i];
3444 if (nid) {
3445 dac = spec->multiout.dac_nids[i];
3446 if (!dac)
3447 dac = spec->multiout.dac_nids[0];
3448 set_output_and_unmute(codec, nid, pin_type, dac);
3449 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003450 }
3451}
3452
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003453
3454static void __init_extra_out(struct hda_codec *codec, int num_outs,
3455 hda_nid_t *pins, hda_nid_t *dacs, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003456{
3457 struct hda_gen_spec *spec = codec->spec;
3458 int i;
3459 hda_nid_t pin, dac;
3460
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003461 for (i = 0; i < num_outs; i++) {
3462 pin = pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003463 if (!pin)
3464 break;
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003465 dac = dacs[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003466 if (!dac) {
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003467 if (i > 0 && dacs[0])
3468 dac = dacs[0];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003469 else
3470 dac = spec->multiout.dac_nids[0];
3471 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003472 set_output_and_unmute(codec, pin, type, dac);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003473 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003474}
3475
3476/* initialize hp and speaker paths */
3477static void init_extra_out(struct hda_codec *codec)
3478{
3479 struct hda_gen_spec *spec = codec->spec;
3480
3481 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3482 __init_extra_out(codec, spec->autocfg.hp_outs,
3483 spec->autocfg.hp_pins,
3484 spec->multiout.hp_out_nid, PIN_HP);
3485 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3486 __init_extra_out(codec, spec->autocfg.speaker_outs,
3487 spec->autocfg.speaker_pins,
3488 spec->multiout.extra_out_nid, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003489}
3490
3491/* initialize multi-io paths */
3492static void init_multi_io(struct hda_codec *codec)
3493{
3494 struct hda_gen_spec *spec = codec->spec;
3495 int i;
3496
3497 for (i = 0; i < spec->multi_ios; i++) {
3498 hda_nid_t pin = spec->multi_io[i].pin;
3499 struct nid_path *path;
3500 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3501 if (!path)
3502 continue;
3503 if (!spec->multi_io[i].ctl_in)
3504 spec->multi_io[i].ctl_in =
3505 snd_hda_codec_update_cache(codec, pin, 0,
3506 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3507 snd_hda_activate_path(codec, path, path->active, true);
3508 }
3509}
3510
3511/* set up the input pin config, depending on the given auto-pin type */
3512static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3513 int auto_pin_type)
3514{
3515 unsigned int val = PIN_IN;
3516 if (auto_pin_type == AUTO_PIN_MIC)
3517 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003518 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003519}
3520
3521/* set up input pins and loopback paths */
3522static void init_analog_input(struct hda_codec *codec)
3523{
3524 struct hda_gen_spec *spec = codec->spec;
3525 struct auto_pin_cfg *cfg = &spec->autocfg;
3526 int i;
3527
3528 for (i = 0; i < cfg->num_inputs; i++) {
3529 hda_nid_t nid = cfg->inputs[i].pin;
3530 if (is_input_pin(codec, nid))
3531 set_input_pin(codec, nid, cfg->inputs[i].type);
3532
3533 /* init loopback inputs */
3534 if (spec->mixer_nid) {
3535 struct nid_path *path;
3536 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3537 if (path)
3538 snd_hda_activate_path(codec, path,
3539 path->active, false);
3540 }
3541 }
3542}
3543
3544/* initialize ADC paths */
3545static void init_input_src(struct hda_codec *codec)
3546{
3547 struct hda_gen_spec *spec = codec->spec;
3548 struct hda_input_mux *imux = &spec->input_mux;
3549 struct nid_path *path;
3550 int i, c, nums;
3551
3552 if (spec->dyn_adc_switch)
3553 nums = 1;
3554 else
3555 nums = spec->num_adc_nids;
3556
3557 for (c = 0; c < nums; c++) {
3558 for (i = 0; i < imux->num_items; i++) {
3559 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3560 get_adc_nid(codec, c, i));
3561 if (path) {
3562 bool active = path->active;
3563 if (i == spec->cur_mux[c])
3564 active = true;
3565 snd_hda_activate_path(codec, path, active, false);
3566 }
3567 }
3568 }
3569
3570 if (spec->shared_mic_hp)
3571 update_shared_mic_hp(codec, spec->cur_mux[0]);
3572
3573 if (spec->cap_sync_hook)
3574 spec->cap_sync_hook(codec);
3575}
3576
3577/* set right pin controls for digital I/O */
3578static void init_digital(struct hda_codec *codec)
3579{
3580 struct hda_gen_spec *spec = codec->spec;
3581 int i;
3582 hda_nid_t pin;
3583
3584 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3585 pin = spec->autocfg.dig_out_pins[i];
3586 if (!pin)
3587 continue;
3588 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3589 }
3590 pin = spec->autocfg.dig_in_pin;
3591 if (pin)
Takashi Iwai7594aa32012-12-20 15:38:40 +01003592 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003593}
3594
Takashi Iwai973e4972012-12-20 15:16:09 +01003595/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3596 * invalid unsol tags by some reason
3597 */
3598static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3599{
3600 int i;
3601
3602 for (i = 0; i < codec->init_pins.used; i++) {
3603 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3604 hda_nid_t nid = pin->nid;
3605 if (is_jack_detectable(codec, nid) &&
3606 !snd_hda_jack_tbl_get(codec, nid))
3607 snd_hda_codec_update_cache(codec, nid, 0,
3608 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3609 }
3610}
3611
Takashi Iwai352f7f92012-12-19 12:52:06 +01003612int snd_hda_gen_init(struct hda_codec *codec)
3613{
3614 struct hda_gen_spec *spec = codec->spec;
3615
3616 if (spec->init_hook)
3617 spec->init_hook(codec);
3618
3619 snd_hda_apply_verbs(codec);
3620
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003621 codec->cached_write = 1;
3622
Takashi Iwai352f7f92012-12-19 12:52:06 +01003623 init_multi_out(codec);
3624 init_extra_out(codec);
3625 init_multi_io(codec);
3626 init_analog_input(codec);
3627 init_input_src(codec);
3628 init_digital(codec);
3629
Takashi Iwai973e4972012-12-20 15:16:09 +01003630 clear_unsol_on_unused_pins(codec);
3631
Takashi Iwai352f7f92012-12-19 12:52:06 +01003632 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003633 snd_hda_gen_hp_automute(codec, NULL);
3634 snd_hda_gen_line_automute(codec, NULL);
3635 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003636
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003637 snd_hda_codec_flush_amp_cache(codec);
3638 snd_hda_codec_flush_cmd_cache(codec);
3639
Takashi Iwai352f7f92012-12-19 12:52:06 +01003640 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3641 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3642
3643 hda_call_check_power_status(codec, 0x01);
3644 return 0;
3645}
3646EXPORT_SYMBOL(snd_hda_gen_init);
3647
3648
3649/*
3650 * the generic codec support
3651 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
Takashi Iwai83012a72012-08-24 18:38:08 +02003653#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003654static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3655{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003656 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003657 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3658}
3659#endif
3660
Takashi Iwai352f7f92012-12-19 12:52:06 +01003661static void generic_free(struct hda_codec *codec)
3662{
3663 snd_hda_gen_spec_free(codec->spec);
3664 kfree(codec->spec);
3665 codec->spec = NULL;
3666}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Takashi Iwai352f7f92012-12-19 12:52:06 +01003668static const struct hda_codec_ops generic_patch_ops = {
3669 .build_controls = snd_hda_gen_build_controls,
3670 .build_pcms = snd_hda_gen_build_pcms,
3671 .init = snd_hda_gen_init,
3672 .free = generic_free,
3673 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02003674#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003675 .check_power_status = generic_check_power_status,
3676#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677};
3678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679int snd_hda_parse_generic_codec(struct hda_codec *codec)
3680{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003681 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 int err;
3683
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003684 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003685 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003687 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003690 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3691 if (err < 0)
3692 return err;
3693
3694 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003695 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 goto error;
3697
3698 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 return 0;
3700
Takashi Iwai352f7f92012-12-19 12:52:06 +01003701error:
3702 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 return err;
3704}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003705EXPORT_SYMBOL(snd_hda_parse_generic_codec);