blob: b7b8d7eee7c6b6b421906dee3d72bdbf26613ecc [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010028#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "hda_codec.h"
30#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010031#include "hda_auto_parser.h"
32#include "hda_jack.h"
33#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Takashi Iwai352f7f92012-12-19 12:52:06 +010036/* initialize hda_gen_spec struct */
37int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Takashi Iwai352f7f92012-12-19 12:52:06 +010039 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
40 snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
41 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
42 return 0;
43}
44EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Takashi Iwai12c93df2012-12-19 14:38:33 +010046struct snd_kcontrol_new *
47snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
48 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010049{
50 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
51 if (!knew)
52 return NULL;
53 *knew = *temp;
54 if (name)
55 knew->name = kstrdup(name, GFP_KERNEL);
56 else if (knew->name)
57 knew->name = kstrdup(knew->name, GFP_KERNEL);
58 if (!knew->name)
59 return NULL;
60 return knew;
61}
Takashi Iwai12c93df2012-12-19 14:38:33 +010062EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010063
64static void free_kctls(struct hda_gen_spec *spec)
65{
66 if (spec->kctls.list) {
67 struct snd_kcontrol_new *kctl = spec->kctls.list;
68 int i;
69 for (i = 0; i < spec->kctls.used; i++)
70 kfree(kctl[i].name);
71 }
72 snd_array_free(&spec->kctls);
73}
74
75static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
76 unsigned int nums,
77 struct hda_ctl_ops *ops)
78{
79 struct hda_gen_spec *spec = codec->spec;
80 struct hda_bind_ctls **ctlp, *ctl;
81 ctlp = snd_array_new(&spec->bind_ctls);
82 if (!ctlp)
83 return NULL;
84 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
85 *ctlp = ctl;
86 if (ctl)
87 ctl->ops = ops;
88 return ctl;
89}
90
91static void free_bind_ctls(struct hda_gen_spec *spec)
92{
93 if (spec->bind_ctls.list) {
94 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
95 int i;
96 for (i = 0; i < spec->bind_ctls.used; i++)
97 kfree(ctl[i]);
98 }
99 snd_array_free(&spec->bind_ctls);
100}
101
102void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
103{
104 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100106 free_kctls(spec);
107 free_bind_ctls(spec);
108 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100110EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100113 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Takashi Iwai352f7f92012-12-19 12:52:06 +0100116/* get the path between the given NIDs;
117 * passing 0 to either @pin or @dac behaves as a wildcard
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100119struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
120 hda_nid_t from_nid, hda_nid_t to_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100122 struct hda_gen_spec *spec = codec->spec;
123 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Takashi Iwai352f7f92012-12-19 12:52:06 +0100125 for (i = 0; i < spec->paths.used; i++) {
126 struct nid_path *path = snd_array_elem(&spec->paths, i);
127 if (path->depth <= 0)
128 continue;
129 if ((!from_nid || path->path[0] == from_nid) &&
130 (!to_nid || path->path[path->depth - 1] == to_nid))
131 return path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133 return NULL;
134}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100135EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
136
137/* check whether the given DAC is already found in any existing paths */
138static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
139{
140 struct hda_gen_spec *spec = codec->spec;
141 int i;
142
143 for (i = 0; i < spec->paths.used; i++) {
144 struct nid_path *path = snd_array_elem(&spec->paths, i);
145 if (path->path[0] == nid)
146 return true;
147 }
148 return false;
149}
150
151/* check whether the given two widgets can be connected */
152static bool is_reachable_path(struct hda_codec *codec,
153 hda_nid_t from_nid, hda_nid_t to_nid)
154{
155 if (!from_nid || !to_nid)
156 return false;
157 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
158}
159
160/* nid, dir and idx */
161#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
162
163/* check whether the given ctl is already assigned in any path elements */
164static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
165{
166 struct hda_gen_spec *spec = codec->spec;
167 int i;
168
169 val &= AMP_VAL_COMPARE_MASK;
170 for (i = 0; i < spec->paths.used; i++) {
171 struct nid_path *path = snd_array_elem(&spec->paths, i);
172 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
173 return true;
174 }
175 return false;
176}
177
178/* check whether a control with the given (nid, dir, idx) was assigned */
179static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
180 int dir, int idx)
181{
182 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
183 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
184 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
185}
186
187/* called recursively */
188static bool __parse_nid_path(struct hda_codec *codec,
189 hda_nid_t from_nid, hda_nid_t to_nid,
190 int with_aa_mix, struct nid_path *path, int depth)
191{
192 struct hda_gen_spec *spec = codec->spec;
193 hda_nid_t conn[16];
194 int i, nums;
195
196 if (to_nid == spec->mixer_nid) {
197 if (!with_aa_mix)
198 return false;
199 with_aa_mix = 2; /* mark aa-mix is included */
200 }
201
202 nums = snd_hda_get_connections(codec, to_nid, conn, ARRAY_SIZE(conn));
203 for (i = 0; i < nums; i++) {
204 if (conn[i] != from_nid) {
205 /* special case: when from_nid is 0,
206 * try to find an empty DAC
207 */
208 if (from_nid ||
209 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
210 is_dac_already_used(codec, conn[i]))
211 continue;
212 }
213 /* aa-mix is requested but not included? */
214 if (!(spec->mixer_nid && with_aa_mix == 1))
215 goto found;
216 }
217 if (depth >= MAX_NID_PATH_DEPTH)
218 return false;
219 for (i = 0; i < nums; i++) {
220 unsigned int type;
221 type = get_wcaps_type(get_wcaps(codec, conn[i]));
222 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
223 type == AC_WID_PIN)
224 continue;
225 if (__parse_nid_path(codec, from_nid, conn[i],
226 with_aa_mix, path, depth + 1))
227 goto found;
228 }
229 return false;
230
231 found:
232 path->path[path->depth] = conn[i];
233 path->idx[path->depth + 1] = i;
234 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
235 path->multi[path->depth + 1] = 1;
236 path->depth++;
237 return true;
238}
239
240/* parse the widget path from the given nid to the target nid;
241 * when @from_nid is 0, try to find an empty DAC;
242 * when @with_aa_mix is 0, paths with spec->mixer_nid are excluded.
243 * when @with_aa_mix is 1, paths without spec->mixer_nid are excluded.
244 * when @with_aa_mix is 2, no special handling about spec->mixer_nid.
245 */
246bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
247 hda_nid_t to_nid, int with_aa_mix,
248 struct nid_path *path)
249{
250 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
251 path->path[path->depth] = to_nid;
252 path->depth++;
253#if 0
254 snd_printdd("path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
255 path->depth, path->path[0], path->path[1],
256 path->path[2], path->path[3], path->path[4]);
257#endif
258 return true;
259 }
260 return false;
261}
262EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100265 * parse the path between the given NIDs and add to the path list.
266 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100268struct nid_path *
269snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
270 hda_nid_t to_nid, int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100272 struct hda_gen_spec *spec = codec->spec;
273 struct nid_path *path;
274
275 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
276 return NULL;
277
278 path = snd_array_new(&spec->paths);
279 if (!path)
280 return NULL;
281 memset(path, 0, sizeof(*path));
282 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
283 return path;
284 /* push back */
285 spec->paths.used--;
286 return NULL;
287}
288EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
289
290/* look for an empty DAC slot */
291static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
292 bool is_digital)
293{
294 struct hda_gen_spec *spec = codec->spec;
295 bool cap_digital;
296 int i;
297
298 for (i = 0; i < spec->num_all_dacs; i++) {
299 hda_nid_t nid = spec->all_dacs[i];
300 if (!nid || is_dac_already_used(codec, nid))
301 continue;
302 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
303 if (is_digital != cap_digital)
304 continue;
305 if (is_reachable_path(codec, nid, pin))
306 return nid;
307 }
308 return 0;
309}
310
311/* replace the channels in the composed amp value with the given number */
312static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
313{
314 val &= ~(0x3U << 16);
315 val |= chs << 16;
316 return val;
317}
318
319/* check whether the widget has the given amp capability for the direction */
320static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
321 int dir, unsigned int bits)
322{
323 if (!nid)
324 return false;
325 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
326 if (query_amp_caps(codec, nid, dir) & bits)
327 return true;
328 return false;
329}
330
331#define nid_has_mute(codec, nid, dir) \
332 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
333#define nid_has_volume(codec, nid, dir) \
334 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
335
336/* look for a widget suitable for assigning a mute switch in the path */
337static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
338 struct nid_path *path)
339{
340 int i;
341
342 for (i = path->depth - 1; i >= 0; i--) {
343 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
344 return path->path[i];
345 if (i != path->depth - 1 && i != 0 &&
346 nid_has_mute(codec, path->path[i], HDA_INPUT))
347 return path->path[i];
348 }
349 return 0;
350}
351
352/* look for a widget suitable for assigning a volume ctl in the path */
353static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
354 struct nid_path *path)
355{
356 int i;
357
358 for (i = path->depth - 1; i >= 0; i--) {
359 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
360 return path->path[i];
361 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100366 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100368
369/* can have the amp-in capability? */
370static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100372 hda_nid_t nid = path->path[idx];
373 unsigned int caps = get_wcaps(codec, nid);
374 unsigned int type = get_wcaps_type(caps);
375
376 if (!(caps & AC_WCAP_IN_AMP))
377 return false;
378 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
379 return false;
380 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Takashi Iwai352f7f92012-12-19 12:52:06 +0100383/* can have the amp-out capability? */
384static bool has_amp_out(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_OUT_AMP))
391 return false;
392 if (type == AC_WID_PIN && !idx) /* only for output pins */
393 return false;
394 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Takashi Iwai352f7f92012-12-19 12:52:06 +0100397/* check whether the given (nid,dir,idx) is active */
398static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
399 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100401 struct hda_gen_spec *spec = codec->spec;
402 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Takashi Iwai352f7f92012-12-19 12:52:06 +0100404 for (n = 0; n < spec->paths.used; n++) {
405 struct nid_path *path = snd_array_elem(&spec->paths, n);
406 if (!path->active)
407 continue;
408 for (i = 0; i < path->depth; i++) {
409 if (path->path[i] == nid) {
410 if (dir == HDA_OUTPUT || path->idx[i] == idx)
411 return true;
412 break;
413 }
414 }
415 }
416 return false;
417}
418
419/* get the default amp value for the target state */
420static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
421 int dir, bool enable)
422{
423 unsigned int caps;
424 unsigned int val = 0;
425
426 caps = query_amp_caps(codec, nid, dir);
427 if (caps & AC_AMPCAP_NUM_STEPS) {
428 /* set to 0dB */
429 if (enable)
430 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
431 }
432 if (caps & AC_AMPCAP_MUTE) {
433 if (!enable)
434 val |= HDA_AMP_MUTE;
435 }
436 return val;
437}
438
439/* initialize the amp value (only at the first time) */
440static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
441{
442 int val = get_amp_val_to_activate(codec, nid, dir, false);
443 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
444}
445
446static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
447 int idx, bool enable)
448{
449 int val;
450 if (is_ctl_associated(codec, nid, dir, idx) ||
451 is_active_nid(codec, nid, dir, idx))
452 return;
453 val = get_amp_val_to_activate(codec, nid, dir, enable);
454 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
455}
456
457static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
458 int i, bool enable)
459{
460 hda_nid_t nid = path->path[i];
461 init_amp(codec, nid, HDA_OUTPUT, 0);
462 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
463}
464
465static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
466 int i, bool enable, bool add_aamix)
467{
468 struct hda_gen_spec *spec = codec->spec;
469 hda_nid_t conn[16];
470 int n, nums, idx;
471 int type;
472 hda_nid_t nid = path->path[i];
473
474 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
475 type = get_wcaps_type(get_wcaps(codec, nid));
476 if (type == AC_WID_PIN ||
477 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
478 nums = 1;
479 idx = 0;
480 } else
481 idx = path->idx[i];
482
483 for (n = 0; n < nums; n++)
484 init_amp(codec, nid, HDA_INPUT, n);
485
486 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
487 return;
488
489 /* here is a little bit tricky in comparison with activate_amp_out();
490 * when aa-mixer is available, we need to enable the path as well
491 */
492 for (n = 0; n < nums; n++) {
493 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
494 continue;
495 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497}
498
Takashi Iwai352f7f92012-12-19 12:52:06 +0100499/* activate or deactivate the given path
500 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100502void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
503 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100505 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Takashi Iwai352f7f92012-12-19 12:52:06 +0100507 if (!enable)
508 path->active = false;
509
510 for (i = path->depth - 1; i >= 0; i--) {
511 if (enable && path->multi[i])
512 snd_hda_codec_write_cache(codec, path->path[i], 0,
513 AC_VERB_SET_CONNECT_SEL,
514 path->idx[i]);
515 if (has_amp_in(codec, path, i))
516 activate_amp_in(codec, path, i, enable, add_aamix);
517 if (has_amp_out(codec, path, i))
518 activate_amp_out(codec, path, i, enable);
519 }
520
521 if (enable)
522 path->active = true;
523}
524EXPORT_SYMBOL_HDA(snd_hda_activate_path);
525
526
527/*
528 * Helper functions for creating mixer ctl elements
529 */
530
531enum {
532 HDA_CTL_WIDGET_VOL,
533 HDA_CTL_WIDGET_MUTE,
534 HDA_CTL_BIND_MUTE,
535 HDA_CTL_BIND_VOL,
536 HDA_CTL_BIND_SW,
537};
538static const struct snd_kcontrol_new control_templates[] = {
539 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
540 HDA_CODEC_MUTE(NULL, 0, 0, 0),
541 HDA_BIND_MUTE(NULL, 0, 0, 0),
542 HDA_BIND_VOL(NULL, 0),
543 HDA_BIND_SW(NULL, 0),
544};
545
546/* add dynamic controls from template */
547static int add_control(struct hda_gen_spec *spec, int type, const char *name,
548 int cidx, unsigned long val)
549{
550 struct snd_kcontrol_new *knew;
551
Takashi Iwai12c93df2012-12-19 14:38:33 +0100552 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100553 if (!knew)
554 return -ENOMEM;
555 knew->index = cidx;
556 if (get_amp_nid_(val))
557 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
558 knew->private_value = val;
559 return 0;
560}
561
562static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
563 const char *pfx, const char *dir,
564 const char *sfx, int cidx, unsigned long val)
565{
566 char name[32];
567 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
568 return add_control(spec, type, name, cidx, val);
569}
570
571#define add_pb_vol_ctrl(spec, type, pfx, val) \
572 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
573#define add_pb_sw_ctrl(spec, type, pfx, val) \
574 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
575#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
576 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
577#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
578 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
579
580static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
581 unsigned int chs, struct nid_path *path)
582{
583 unsigned int val;
584 if (!path)
585 return 0;
586 val = path->ctls[NID_PATH_VOL_CTL];
587 if (!val)
588 return 0;
589 val = amp_val_replace_channels(val, chs);
590 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
591}
592
593/* return the channel bits suitable for the given path->ctls[] */
594static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
595 int type)
596{
597 int chs = 1; /* mono (left only) */
598 if (path) {
599 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
600 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
601 chs = 3; /* stereo */
602 }
603 return chs;
604}
605
606static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
607 struct nid_path *path)
608{
609 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
610 return add_vol_ctl(codec, pfx, cidx, chs, path);
611}
612
613/* create a mute-switch for the given mixer widget;
614 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
615 */
616static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
617 unsigned int chs, struct nid_path *path)
618{
619 unsigned int val;
620 int type = HDA_CTL_WIDGET_MUTE;
621
622 if (!path)
623 return 0;
624 val = path->ctls[NID_PATH_MUTE_CTL];
625 if (!val)
626 return 0;
627 val = amp_val_replace_channels(val, chs);
628 if (get_amp_direction_(val) == HDA_INPUT) {
629 hda_nid_t nid = get_amp_nid_(val);
630 int nums = snd_hda_get_num_conns(codec, nid);
631 if (nums > 1) {
632 type = HDA_CTL_BIND_MUTE;
633 val |= nums << 19;
634 }
635 }
636 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
637}
638
639static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
640 int cidx, struct nid_path *path)
641{
642 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
643 return add_sw_ctl(codec, pfx, cidx, chs, path);
644}
645
646static const char * const channel_name[4] = {
647 "Front", "Surround", "CLFE", "Side"
648};
649
650/* give some appropriate ctl name prefix for the given line out channel */
651static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
652 bool can_be_master, int *index)
653{
654 struct auto_pin_cfg *cfg = &spec->autocfg;
655
656 *index = 0;
657 if (cfg->line_outs == 1 && !spec->multi_ios &&
658 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
659 return spec->vmaster_mute.hook ? "PCM" : "Master";
660
661 /* if there is really a single DAC used in the whole output paths,
662 * use it master (or "PCM" if a vmaster hook is present)
663 */
664 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
665 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
666 return spec->vmaster_mute.hook ? "PCM" : "Master";
667
668 switch (cfg->line_out_type) {
669 case AUTO_PIN_SPEAKER_OUT:
670 if (cfg->line_outs == 1)
671 return "Speaker";
672 if (cfg->line_outs == 2)
673 return ch ? "Bass Speaker" : "Speaker";
674 break;
675 case AUTO_PIN_HP_OUT:
676 /* for multi-io case, only the primary out */
677 if (ch && spec->multi_ios)
678 break;
679 *index = ch;
680 return "Headphone";
681 default:
682 if (cfg->line_outs == 1 && !spec->multi_ios)
683 return "PCM";
684 break;
685 }
686 if (ch >= ARRAY_SIZE(channel_name)) {
687 snd_BUG();
688 return "PCM";
689 }
690
691 return channel_name[ch];
692}
693
694/*
695 * Parse output paths
696 */
697
698/* badness definition */
699enum {
700 /* No primary DAC is found for the main output */
701 BAD_NO_PRIMARY_DAC = 0x10000,
702 /* No DAC is found for the extra output */
703 BAD_NO_DAC = 0x4000,
704 /* No possible multi-ios */
705 BAD_MULTI_IO = 0x103,
706 /* No individual DAC for extra output */
707 BAD_NO_EXTRA_DAC = 0x102,
708 /* No individual DAC for extra surrounds */
709 BAD_NO_EXTRA_SURR_DAC = 0x101,
710 /* Primary DAC shared with main surrounds */
711 BAD_SHARED_SURROUND = 0x100,
712 /* Primary DAC shared with main CLFE */
713 BAD_SHARED_CLFE = 0x10,
714 /* Primary DAC shared with extra surrounds */
715 BAD_SHARED_EXTRA_SURROUND = 0x10,
716 /* Volume widget is shared */
717 BAD_SHARED_VOL = 0x10,
718};
719
720/* look for widgets in the path between the given NIDs appropriate for
721 * volume and mute controls, and assign the values to ctls[].
722 *
723 * When no appropriate widget is found in the path, the badness value
724 * is incremented depending on the situation. The function returns the
725 * total badness for both volume and mute controls.
726 */
727static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
728 hda_nid_t dac)
729{
730 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
731 hda_nid_t nid;
732 unsigned int val;
733 int badness = 0;
734
735 if (!path)
736 return BAD_SHARED_VOL * 2;
737 nid = look_for_out_vol_nid(codec, path);
738 if (nid) {
739 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
740 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
741 badness += BAD_SHARED_VOL;
742 else
743 path->ctls[NID_PATH_VOL_CTL] = val;
744 } else
745 badness += BAD_SHARED_VOL;
746 nid = look_for_out_mute_nid(codec, path);
747 if (nid) {
748 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
749 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
750 nid_has_mute(codec, nid, HDA_OUTPUT))
751 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
752 else
753 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
754 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
755 badness += BAD_SHARED_VOL;
756 else
757 path->ctls[NID_PATH_MUTE_CTL] = val;
758 } else
759 badness += BAD_SHARED_VOL;
760 return badness;
761}
762
763struct badness_table {
764 int no_primary_dac; /* no primary DAC */
765 int no_dac; /* no secondary DACs */
766 int shared_primary; /* primary DAC is shared with main output */
767 int shared_surr; /* secondary DAC shared with main or primary */
768 int shared_clfe; /* third DAC shared with main or primary */
769 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
770};
771
772static struct badness_table main_out_badness = {
773 .no_primary_dac = BAD_NO_PRIMARY_DAC,
774 .no_dac = BAD_NO_DAC,
775 .shared_primary = BAD_NO_PRIMARY_DAC,
776 .shared_surr = BAD_SHARED_SURROUND,
777 .shared_clfe = BAD_SHARED_CLFE,
778 .shared_surr_main = BAD_SHARED_SURROUND,
779};
780
781static struct badness_table extra_out_badness = {
782 .no_primary_dac = BAD_NO_DAC,
783 .no_dac = BAD_NO_DAC,
784 .shared_primary = BAD_NO_EXTRA_DAC,
785 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
786 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
787 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
788};
789
790/* try to assign DACs to pins and return the resultant badness */
791static int try_assign_dacs(struct hda_codec *codec, int num_outs,
792 const hda_nid_t *pins, hda_nid_t *dacs,
793 const struct badness_table *bad)
794{
795 struct hda_gen_spec *spec = codec->spec;
796 struct auto_pin_cfg *cfg = &spec->autocfg;
797 int i, j;
798 int badness = 0;
799 hda_nid_t dac;
800
801 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return 0;
803
Takashi Iwai352f7f92012-12-19 12:52:06 +0100804 for (i = 0; i < num_outs; i++) {
805 hda_nid_t pin = pins[i];
806 if (!dacs[i])
807 dacs[i] = look_for_dac(codec, pin, false);
808 if (!dacs[i] && !i) {
809 for (j = 1; j < num_outs; j++) {
810 if (is_reachable_path(codec, dacs[j], pin)) {
811 dacs[0] = dacs[j];
812 dacs[j] = 0;
813 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100816 }
817 dac = dacs[i];
818 if (!dac) {
819 if (is_reachable_path(codec, dacs[0], pin))
820 dac = dacs[0];
821 else if (cfg->line_outs > i &&
822 is_reachable_path(codec, spec->private_dac_nids[i], pin))
823 dac = spec->private_dac_nids[i];
824 if (dac) {
825 if (!i)
826 badness += bad->shared_primary;
827 else if (i == 1)
828 badness += bad->shared_surr;
829 else
830 badness += bad->shared_clfe;
831 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
832 dac = spec->private_dac_nids[0];
833 badness += bad->shared_surr_main;
834 } else if (!i)
835 badness += bad->no_primary_dac;
836 else
837 badness += bad->no_dac;
838 }
839 if (!snd_hda_add_new_path(codec, dac, pin, 0))
840 dac = dacs[i] = 0;
841 if (dac)
842 badness += assign_out_path_ctls(codec, pin, dac);
843 }
844
845 return badness;
846}
847
848/* return NID if the given pin has only a single connection to a certain DAC */
849static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
850{
851 struct hda_gen_spec *spec = codec->spec;
852 int i;
853 hda_nid_t nid_found = 0;
854
855 for (i = 0; i < spec->num_all_dacs; i++) {
856 hda_nid_t nid = spec->all_dacs[i];
857 if (!nid || is_dac_already_used(codec, nid))
858 continue;
859 if (is_reachable_path(codec, nid, pin)) {
860 if (nid_found)
861 return 0;
862 nid_found = nid;
863 }
864 }
865 return nid_found;
866}
867
868/* check whether the given pin can be a multi-io pin */
869static bool can_be_multiio_pin(struct hda_codec *codec,
870 unsigned int location, hda_nid_t nid)
871{
872 unsigned int defcfg, caps;
873
874 defcfg = snd_hda_codec_get_pincfg(codec, nid);
875 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
876 return false;
877 if (location && get_defcfg_location(defcfg) != location)
878 return false;
879 caps = snd_hda_query_pin_caps(codec, nid);
880 if (!(caps & AC_PINCAP_OUT))
881 return false;
882 return true;
883}
884
885/*
886 * multi-io helper
887 *
888 * When hardwired is set, try to fill ony hardwired pins, and returns
889 * zero if any pins are filled, non-zero if nothing found.
890 * When hardwired is off, try to fill possible input pins, and returns
891 * the badness value.
892 */
893static int fill_multi_ios(struct hda_codec *codec,
894 hda_nid_t reference_pin,
895 bool hardwired, int offset)
896{
897 struct hda_gen_spec *spec = codec->spec;
898 struct auto_pin_cfg *cfg = &spec->autocfg;
899 int type, i, j, dacs, num_pins, old_pins;
900 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
901 unsigned int location = get_defcfg_location(defcfg);
902 int badness = 0;
903
904 old_pins = spec->multi_ios;
905 if (old_pins >= 2)
906 goto end_fill;
907
908 num_pins = 0;
909 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
910 for (i = 0; i < cfg->num_inputs; i++) {
911 if (cfg->inputs[i].type != type)
912 continue;
913 if (can_be_multiio_pin(codec, location,
914 cfg->inputs[i].pin))
915 num_pins++;
916 }
917 }
918 if (num_pins < 2)
919 goto end_fill;
920
921 dacs = spec->multiout.num_dacs;
922 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
923 for (i = 0; i < cfg->num_inputs; i++) {
924 hda_nid_t nid = cfg->inputs[i].pin;
925 hda_nid_t dac = 0;
926
927 if (cfg->inputs[i].type != type)
928 continue;
929 if (!can_be_multiio_pin(codec, location, nid))
930 continue;
931 for (j = 0; j < spec->multi_ios; j++) {
932 if (nid == spec->multi_io[j].pin)
933 break;
934 }
935 if (j < spec->multi_ios)
936 continue;
937
938 if (offset && offset + spec->multi_ios < dacs) {
939 dac = spec->private_dac_nids[offset + spec->multi_ios];
940 if (!is_reachable_path(codec, dac, nid))
941 dac = 0;
942 }
943 if (hardwired)
944 dac = get_dac_if_single(codec, nid);
945 else if (!dac)
946 dac = look_for_dac(codec, nid, false);
947 if (!dac) {
948 badness++;
949 continue;
950 }
951 if (!snd_hda_add_new_path(codec, dac, nid, 0)) {
952 badness++;
953 continue;
954 }
955 spec->multi_io[spec->multi_ios].pin = nid;
956 spec->multi_io[spec->multi_ios].dac = dac;
957 spec->multi_ios++;
958 if (spec->multi_ios >= 2)
959 break;
960 }
961 }
962 end_fill:
963 if (badness)
964 badness = BAD_MULTI_IO;
965 if (old_pins == spec->multi_ios) {
966 if (hardwired)
967 return 1; /* nothing found */
968 else
969 return badness; /* no badness if nothing found */
970 }
971 if (!hardwired && spec->multi_ios < 2) {
972 /* cancel newly assigned paths */
973 spec->paths.used -= spec->multi_ios - old_pins;
974 spec->multi_ios = old_pins;
975 return badness;
976 }
977
978 /* assign volume and mute controls */
979 for (i = old_pins; i < spec->multi_ios; i++)
980 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
981 spec->multi_io[i].dac);
982
983 return badness;
984}
985
986/* map DACs for all pins in the list if they are single connections */
987static bool map_singles(struct hda_codec *codec, int outs,
988 const hda_nid_t *pins, hda_nid_t *dacs)
989{
990 int i;
991 bool found = false;
992 for (i = 0; i < outs; i++) {
993 hda_nid_t dac;
994 if (dacs[i])
995 continue;
996 dac = get_dac_if_single(codec, pins[i]);
997 if (!dac)
998 continue;
999 if (snd_hda_add_new_path(codec, dac, pins[i], 0)) {
1000 dacs[i] = dac;
1001 found = true;
1002 }
1003 }
1004 return found;
1005}
1006
1007/* fill in the dac_nids table from the parsed pin configuration */
1008static int fill_and_eval_dacs(struct hda_codec *codec,
1009 bool fill_hardwired,
1010 bool fill_mio_first)
1011{
1012 struct hda_gen_spec *spec = codec->spec;
1013 struct auto_pin_cfg *cfg = &spec->autocfg;
1014 int i, err, badness;
1015
1016 /* set num_dacs once to full for look_for_dac() */
1017 spec->multiout.num_dacs = cfg->line_outs;
1018 spec->multiout.dac_nids = spec->private_dac_nids;
1019 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1020 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1021 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1022 spec->multi_ios = 0;
1023 snd_array_free(&spec->paths);
1024 badness = 0;
1025
1026 /* fill hard-wired DACs first */
1027 if (fill_hardwired) {
1028 bool mapped;
1029 do {
1030 mapped = map_singles(codec, cfg->line_outs,
1031 cfg->line_out_pins,
1032 spec->private_dac_nids);
1033 mapped |= map_singles(codec, cfg->hp_outs,
1034 cfg->hp_pins,
1035 spec->multiout.hp_out_nid);
1036 mapped |= map_singles(codec, cfg->speaker_outs,
1037 cfg->speaker_pins,
1038 spec->multiout.extra_out_nid);
1039 if (fill_mio_first && cfg->line_outs == 1 &&
1040 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1041 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1042 if (!err)
1043 mapped = true;
1044 }
1045 } while (mapped);
1046 }
1047
1048 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1049 spec->private_dac_nids,
1050 &main_out_badness);
1051
1052 /* re-count num_dacs and squash invalid entries */
1053 spec->multiout.num_dacs = 0;
1054 for (i = 0; i < cfg->line_outs; i++) {
1055 if (spec->private_dac_nids[i])
1056 spec->multiout.num_dacs++;
1057 else {
1058 memmove(spec->private_dac_nids + i,
1059 spec->private_dac_nids + i + 1,
1060 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1061 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1062 }
1063 }
1064
1065 if (fill_mio_first &&
1066 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1067 /* try to fill multi-io first */
1068 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1069 if (err < 0)
1070 return err;
1071 /* we don't count badness at this stage yet */
1072 }
1073
1074 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1075 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1076 spec->multiout.hp_out_nid,
1077 &extra_out_badness);
1078 if (err < 0)
1079 return err;
1080 badness += err;
1081 }
1082 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1083 err = try_assign_dacs(codec, cfg->speaker_outs,
1084 cfg->speaker_pins,
1085 spec->multiout.extra_out_nid,
1086 &extra_out_badness);
1087 if (err < 0)
1088 return err;
1089 badness += err;
1090 }
1091 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1092 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1093 if (err < 0)
1094 return err;
1095 badness += err;
1096 }
1097 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1098 /* try multi-ios with HP + inputs */
1099 int offset = 0;
1100 if (cfg->line_outs >= 3)
1101 offset = 1;
1102 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1103 if (err < 0)
1104 return err;
1105 badness += err;
1106 }
1107
1108 if (spec->multi_ios == 2) {
1109 for (i = 0; i < 2; i++)
1110 spec->private_dac_nids[spec->multiout.num_dacs++] =
1111 spec->multi_io[i].dac;
1112 spec->ext_channel_count = 2;
1113 } else if (spec->multi_ios) {
1114 spec->multi_ios = 0;
1115 badness += BAD_MULTI_IO;
1116 }
1117
1118 return badness;
1119}
1120
1121#define DEBUG_BADNESS
1122
1123#ifdef DEBUG_BADNESS
1124#define debug_badness snd_printdd
1125#else
1126#define debug_badness(...)
1127#endif
1128
1129static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1130{
1131 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1132 cfg->line_out_pins[0], cfg->line_out_pins[1],
1133 cfg->line_out_pins[2], cfg->line_out_pins[2],
1134 spec->multiout.dac_nids[0],
1135 spec->multiout.dac_nids[1],
1136 spec->multiout.dac_nids[2],
1137 spec->multiout.dac_nids[3]);
1138 if (spec->multi_ios > 0)
1139 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1140 spec->multi_ios,
1141 spec->multi_io[0].pin, spec->multi_io[1].pin,
1142 spec->multi_io[0].dac, spec->multi_io[1].dac);
1143 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1144 cfg->hp_pins[0], cfg->hp_pins[1],
1145 cfg->hp_pins[2], cfg->hp_pins[2],
1146 spec->multiout.hp_out_nid[0],
1147 spec->multiout.hp_out_nid[1],
1148 spec->multiout.hp_out_nid[2],
1149 spec->multiout.hp_out_nid[3]);
1150 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1151 cfg->speaker_pins[0], cfg->speaker_pins[1],
1152 cfg->speaker_pins[2], cfg->speaker_pins[3],
1153 spec->multiout.extra_out_nid[0],
1154 spec->multiout.extra_out_nid[1],
1155 spec->multiout.extra_out_nid[2],
1156 spec->multiout.extra_out_nid[3]);
1157}
1158
1159/* find all available DACs of the codec */
1160static void fill_all_dac_nids(struct hda_codec *codec)
1161{
1162 struct hda_gen_spec *spec = codec->spec;
1163 int i;
1164 hda_nid_t nid = codec->start_nid;
1165
1166 spec->num_all_dacs = 0;
1167 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1168 for (i = 0; i < codec->num_nodes; i++, nid++) {
1169 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1170 continue;
1171 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1172 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1173 break;
1174 }
1175 spec->all_dacs[spec->num_all_dacs++] = nid;
1176 }
1177}
1178
1179static int parse_output_paths(struct hda_codec *codec)
1180{
1181 struct hda_gen_spec *spec = codec->spec;
1182 struct auto_pin_cfg *cfg = &spec->autocfg;
1183 struct auto_pin_cfg *best_cfg;
1184 int best_badness = INT_MAX;
1185 int badness;
1186 bool fill_hardwired = true, fill_mio_first = true;
1187 bool best_wired = true, best_mio = true;
1188 bool hp_spk_swapped = false;
1189
1190 fill_all_dac_nids(codec);
1191
1192 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1193 if (!best_cfg)
1194 return -ENOMEM;
1195 *best_cfg = *cfg;
1196
1197 for (;;) {
1198 badness = fill_and_eval_dacs(codec, fill_hardwired,
1199 fill_mio_first);
1200 if (badness < 0) {
1201 kfree(best_cfg);
1202 return badness;
1203 }
1204 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1205 cfg->line_out_type, fill_hardwired, fill_mio_first,
1206 badness);
1207 debug_show_configs(spec, cfg);
1208 if (badness < best_badness) {
1209 best_badness = badness;
1210 *best_cfg = *cfg;
1211 best_wired = fill_hardwired;
1212 best_mio = fill_mio_first;
1213 }
1214 if (!badness)
1215 break;
1216 fill_mio_first = !fill_mio_first;
1217 if (!fill_mio_first)
1218 continue;
1219 fill_hardwired = !fill_hardwired;
1220 if (!fill_hardwired)
1221 continue;
1222 if (hp_spk_swapped)
1223 break;
1224 hp_spk_swapped = true;
1225 if (cfg->speaker_outs > 0 &&
1226 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1227 cfg->hp_outs = cfg->line_outs;
1228 memcpy(cfg->hp_pins, cfg->line_out_pins,
1229 sizeof(cfg->hp_pins));
1230 cfg->line_outs = cfg->speaker_outs;
1231 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1232 sizeof(cfg->speaker_pins));
1233 cfg->speaker_outs = 0;
1234 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1235 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1236 fill_hardwired = true;
1237 continue;
1238 }
1239 if (cfg->hp_outs > 0 &&
1240 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1241 cfg->speaker_outs = cfg->line_outs;
1242 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1243 sizeof(cfg->speaker_pins));
1244 cfg->line_outs = cfg->hp_outs;
1245 memcpy(cfg->line_out_pins, cfg->hp_pins,
1246 sizeof(cfg->hp_pins));
1247 cfg->hp_outs = 0;
1248 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1249 cfg->line_out_type = AUTO_PIN_HP_OUT;
1250 fill_hardwired = true;
1251 continue;
1252 }
1253 break;
1254 }
1255
1256 if (badness) {
1257 *cfg = *best_cfg;
1258 fill_and_eval_dacs(codec, best_wired, best_mio);
1259 }
1260 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1261 cfg->line_out_type, best_wired, best_mio);
1262 debug_show_configs(spec, cfg);
1263
1264 if (cfg->line_out_pins[0]) {
1265 struct nid_path *path;
1266 path = snd_hda_get_nid_path(codec,
1267 spec->multiout.dac_nids[0],
1268 cfg->line_out_pins[0]);
1269 if (path)
1270 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1271 }
1272
1273 kfree(best_cfg);
1274 return 0;
1275}
1276
1277/* add playback controls from the parsed DAC table */
1278static int create_multi_out_ctls(struct hda_codec *codec,
1279 const struct auto_pin_cfg *cfg)
1280{
1281 struct hda_gen_spec *spec = codec->spec;
1282 int i, err, noutputs;
1283
1284 noutputs = cfg->line_outs;
1285 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1286 noutputs += spec->multi_ios;
1287
1288 for (i = 0; i < noutputs; i++) {
1289 const char *name;
1290 int index;
1291 hda_nid_t dac, pin;
1292 struct nid_path *path;
1293
1294 dac = spec->multiout.dac_nids[i];
1295 if (!dac)
1296 continue;
1297 if (i >= cfg->line_outs) {
1298 pin = spec->multi_io[i - 1].pin;
1299 index = 0;
1300 name = channel_name[i];
1301 } else {
1302 pin = cfg->line_out_pins[i];
1303 name = get_line_out_pfx(spec, i, true, &index);
1304 }
1305
1306 path = snd_hda_get_nid_path(codec, dac, pin);
1307 if (!path)
1308 continue;
1309 if (!name || !strcmp(name, "CLFE")) {
1310 /* Center/LFE */
1311 err = add_vol_ctl(codec, "Center", 0, 1, path);
1312 if (err < 0)
1313 return err;
1314 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1315 if (err < 0)
1316 return err;
1317 err = add_sw_ctl(codec, "Center", 0, 1, path);
1318 if (err < 0)
1319 return err;
1320 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1321 if (err < 0)
1322 return err;
1323 } else {
1324 err = add_stereo_vol(codec, name, index, path);
1325 if (err < 0)
1326 return err;
1327 err = add_stereo_sw(codec, name, index, path);
1328 if (err < 0)
1329 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331 }
1332 return 0;
1333}
1334
Takashi Iwai352f7f92012-12-19 12:52:06 +01001335static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1336 hda_nid_t dac, const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001338 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 int err;
1340
Takashi Iwai352f7f92012-12-19 12:52:06 +01001341 path = snd_hda_get_nid_path(codec, dac, pin);
1342 if (!path)
1343 return 0;
1344 /* bind volume control will be created in the case of dac = 0 */
1345 if (dac) {
1346 err = add_stereo_vol(codec, pfx, cidx, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (err < 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001350 err = add_stereo_sw(codec, pfx, cidx, path);
1351 if (err < 0)
1352 return err;
1353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
Takashi Iwai352f7f92012-12-19 12:52:06 +01001356/* add playback controls for speaker and HP outputs */
1357static int create_extra_outs(struct hda_codec *codec, int num_pins,
1358 const hda_nid_t *pins, const hda_nid_t *dacs,
1359 const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001361 struct hda_gen_spec *spec = codec->spec;
1362 struct hda_bind_ctls *ctl;
1363 char name[32];
1364 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Takashi Iwai352f7f92012-12-19 12:52:06 +01001366 if (!num_pins || !pins[0])
1367 return 0;
1368
1369 if (num_pins == 1) {
1370 hda_nid_t dac = *dacs;
1371 if (!dac)
1372 dac = spec->multiout.dac_nids[0];
1373 return create_extra_out(codec, *pins, dac, pfx, 0);
Takashi Iwai97ec5582006-03-21 11:29:07 +01001374 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001375
1376 for (i = 0; i < num_pins; i++) {
1377 hda_nid_t dac;
1378 if (dacs[num_pins - 1])
1379 dac = dacs[i]; /* with individual volumes */
Takashi Iwai97ec5582006-03-21 11:29:07 +01001380 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001381 dac = 0;
1382 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1383 err = create_extra_out(codec, pins[i], dac,
1384 "Bass Speaker", 0);
1385 } else if (num_pins >= 3) {
1386 snprintf(name, sizeof(name), "%s %s",
1387 pfx, channel_name[i]);
1388 err = create_extra_out(codec, pins[i], dac, name, 0);
1389 } else {
1390 err = create_extra_out(codec, pins[i], dac, pfx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001392 if (err < 0)
1393 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001395 if (dacs[num_pins - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return 0;
1397
Takashi Iwai352f7f92012-12-19 12:52:06 +01001398 /* Let's create a bind-controls for volumes */
1399 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1400 if (!ctl)
1401 return -ENOMEM;
1402 n = 0;
1403 for (i = 0; i < num_pins; i++) {
1404 hda_nid_t vol;
1405 struct nid_path *path;
1406 if (!pins[i] || !dacs[i])
1407 continue;
1408 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1409 if (!path)
1410 continue;
1411 vol = look_for_out_vol_nid(codec, path);
1412 if (vol)
1413 ctl->values[n++] =
1414 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1415 }
1416 if (n) {
1417 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1418 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1419 if (err < 0)
1420 return err;
1421 }
1422 return 0;
1423}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001424
Takashi Iwai352f7f92012-12-19 12:52:06 +01001425static int create_hp_out_ctls(struct hda_codec *codec)
1426{
1427 struct hda_gen_spec *spec = codec->spec;
1428 return create_extra_outs(codec, spec->autocfg.hp_outs,
1429 spec->autocfg.hp_pins,
1430 spec->multiout.hp_out_nid,
1431 "Headphone");
1432}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Takashi Iwai352f7f92012-12-19 12:52:06 +01001434static int create_speaker_out_ctls(struct hda_codec *codec)
1435{
1436 struct hda_gen_spec *spec = codec->spec;
1437 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1438 spec->autocfg.speaker_pins,
1439 spec->multiout.extra_out_nid,
1440 "Speaker");
1441}
1442
1443/*
1444 * channel mode enum control
1445 */
1446
1447static int ch_mode_info(struct snd_kcontrol *kcontrol,
1448 struct snd_ctl_elem_info *uinfo)
1449{
1450 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1451 struct hda_gen_spec *spec = codec->spec;
1452
1453 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1454 uinfo->count = 1;
1455 uinfo->value.enumerated.items = spec->multi_ios + 1;
1456 if (uinfo->value.enumerated.item > spec->multi_ios)
1457 uinfo->value.enumerated.item = spec->multi_ios;
1458 sprintf(uinfo->value.enumerated.name, "%dch",
1459 (uinfo->value.enumerated.item + 1) * 2);
1460 return 0;
1461}
1462
1463static int ch_mode_get(struct snd_kcontrol *kcontrol,
1464 struct snd_ctl_elem_value *ucontrol)
1465{
1466 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1467 struct hda_gen_spec *spec = codec->spec;
1468 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1469 return 0;
1470}
1471
1472static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1473{
1474 struct hda_gen_spec *spec = codec->spec;
1475 hda_nid_t nid = spec->multi_io[idx].pin;
1476 struct nid_path *path;
1477
1478 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1479 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001481
1482 if (path->active == output)
1483 return 0;
1484
1485 if (output) {
1486 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1487 snd_hda_activate_path(codec, path, true, true);
1488 } else {
1489 snd_hda_activate_path(codec, path, false, true);
1490 snd_hda_set_pin_ctl_cache(codec, nid,
1491 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
Takashi Iwai352f7f92012-12-19 12:52:06 +01001496static int ch_mode_put(struct snd_kcontrol *kcontrol,
1497 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001499 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1500 struct hda_gen_spec *spec = codec->spec;
1501 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Takashi Iwai352f7f92012-12-19 12:52:06 +01001503 ch = ucontrol->value.enumerated.item[0];
1504 if (ch < 0 || ch > spec->multi_ios)
1505 return -EINVAL;
1506 if (ch == (spec->ext_channel_count - 1) / 2)
1507 return 0;
1508 spec->ext_channel_count = (ch + 1) * 2;
1509 for (i = 0; i < spec->multi_ios; i++)
1510 set_multi_io(codec, i, i < ch);
1511 spec->multiout.max_channels = max(spec->ext_channel_count,
1512 spec->const_channel_count);
1513 if (spec->need_dac_fix)
1514 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return 1;
1516}
1517
Takashi Iwai352f7f92012-12-19 12:52:06 +01001518static const struct snd_kcontrol_new channel_mode_enum = {
1519 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1520 .name = "Channel Mode",
1521 .info = ch_mode_info,
1522 .get = ch_mode_get,
1523 .put = ch_mode_put,
1524};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Takashi Iwai352f7f92012-12-19 12:52:06 +01001526static int create_multi_channel_mode(struct hda_codec *codec)
1527{
1528 struct hda_gen_spec *spec = codec->spec;
1529
1530 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001531 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001532 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return 0;
1535}
1536
Takashi Iwai352f7f92012-12-19 12:52:06 +01001537/*
1538 * shared headphone/mic handling
1539 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001540
Takashi Iwai352f7f92012-12-19 12:52:06 +01001541static void call_update_outputs(struct hda_codec *codec);
1542
1543/* for shared I/O, change the pin-control accordingly */
1544static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1545{
1546 struct hda_gen_spec *spec = codec->spec;
1547 unsigned int val;
1548 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1549 /* NOTE: this assumes that there are only two inputs, the
1550 * first is the real internal mic and the second is HP/mic jack.
1551 */
1552
1553 val = snd_hda_get_default_vref(codec, pin);
1554
1555 /* This pin does not have vref caps - let's enable vref on pin 0x18
1556 instead, as suggested by Realtek */
1557 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1558 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1559 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1560 if (vref_val != AC_PINCTL_VREF_HIZ)
1561 snd_hda_set_pin_ctl(codec, vref_pin, PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001562 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001563
1564 val = set_as_mic ? val | PIN_IN : PIN_HP;
1565 snd_hda_set_pin_ctl(codec, pin, val);
1566
1567 spec->automute_speaker = !set_as_mic;
1568 call_update_outputs(codec);
1569}
1570
1571/* create a shared input with the headphone out */
1572static int create_shared_input(struct hda_codec *codec)
1573{
1574 struct hda_gen_spec *spec = codec->spec;
1575 struct auto_pin_cfg *cfg = &spec->autocfg;
1576 unsigned int defcfg;
1577 hda_nid_t nid;
1578
1579 /* only one internal input pin? */
1580 if (cfg->num_inputs != 1)
1581 return 0;
1582 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1583 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1584 return 0;
1585
1586 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1587 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1588 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1589 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1590 else
1591 return 0; /* both not available */
1592
1593 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1594 return 0; /* no input */
1595
1596 cfg->inputs[1].pin = nid;
1597 cfg->inputs[1].type = AUTO_PIN_MIC;
1598 cfg->num_inputs = 2;
1599 spec->shared_mic_hp = 1;
1600 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1601 return 0;
1602}
1603
1604
1605/*
1606 * Parse input paths
1607 */
1608
1609#ifdef CONFIG_PM
1610/* add the powersave loopback-list entry */
1611static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1612{
1613 struct hda_amp_list *list;
1614
1615 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1616 return;
1617 list = spec->loopback_list + spec->num_loopbacks;
1618 list->nid = mix;
1619 list->dir = HDA_INPUT;
1620 list->idx = idx;
1621 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001622 spec->loopback.amplist = spec->loopback_list;
1623}
1624#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001625#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001626#endif
1627
Takashi Iwai352f7f92012-12-19 12:52:06 +01001628/* create input playback/capture controls for the given pin */
1629static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1630 const char *ctlname, int ctlidx,
1631 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001633 struct hda_gen_spec *spec = codec->spec;
1634 struct nid_path *path;
1635 unsigned int val;
1636 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Takashi Iwai352f7f92012-12-19 12:52:06 +01001638 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1639 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1640 return 0; /* no need for analog loopback */
1641
1642 path = snd_hda_add_new_path(codec, pin, mix_nid, 2);
1643 if (!path)
1644 return -EINVAL;
1645
1646 idx = path->idx[path->depth - 1];
1647 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1648 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1649 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001650 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001652 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654
Takashi Iwai352f7f92012-12-19 12:52:06 +01001655 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1656 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1657 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001658 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001660 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662
Takashi Iwai352f7f92012-12-19 12:52:06 +01001663 path->active = true;
1664 add_loopback_list(spec, mix_nid, idx);
1665 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
Takashi Iwai352f7f92012-12-19 12:52:06 +01001668static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001670 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1671 return (pincap & AC_PINCAP_IN) != 0;
1672}
1673
1674/* Parse the codec tree and retrieve ADCs */
1675static int fill_adc_nids(struct hda_codec *codec)
1676{
1677 struct hda_gen_spec *spec = codec->spec;
1678 hda_nid_t nid;
1679 hda_nid_t *adc_nids = spec->adc_nids;
1680 int max_nums = ARRAY_SIZE(spec->adc_nids);
1681 int i, nums = 0;
1682
1683 nid = codec->start_nid;
1684 for (i = 0; i < codec->num_nodes; i++, nid++) {
1685 unsigned int caps = get_wcaps(codec, nid);
1686 int type = get_wcaps_type(caps);
1687
1688 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1689 continue;
1690 adc_nids[nums] = nid;
1691 if (++nums >= max_nums)
1692 break;
1693 }
1694 spec->num_adc_nids = nums;
1695 return nums;
1696}
1697
1698/* filter out invalid adc_nids that don't give all active input pins;
1699 * if needed, check whether dynamic ADC-switching is available
1700 */
1701static int check_dyn_adc_switch(struct hda_codec *codec)
1702{
1703 struct hda_gen_spec *spec = codec->spec;
1704 struct hda_input_mux *imux = &spec->input_mux;
1705 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1706 int i, n, nums;
1707 hda_nid_t pin, adc;
1708
1709 again:
1710 nums = 0;
1711 for (n = 0; n < spec->num_adc_nids; n++) {
1712 adc = spec->adc_nids[n];
1713 for (i = 0; i < imux->num_items; i++) {
1714 pin = spec->imux_pins[i];
1715 if (!is_reachable_path(codec, pin, adc))
1716 break;
1717 }
1718 if (i >= imux->num_items)
1719 adc_nids[nums++] = adc;
1720 }
1721
1722 if (!nums) {
1723 if (spec->shared_mic_hp) {
1724 spec->shared_mic_hp = 0;
1725 imux->num_items = 1;
1726 goto again;
1727 }
1728
1729 /* check whether ADC-switch is possible */
1730 for (i = 0; i < imux->num_items; i++) {
1731 pin = spec->imux_pins[i];
1732 for (n = 0; n < spec->num_adc_nids; n++) {
1733 adc = spec->adc_nids[n];
1734 if (is_reachable_path(codec, pin, adc)) {
1735 spec->dyn_adc_idx[i] = n;
1736 break;
1737 }
1738 }
1739 }
1740
1741 snd_printdd("hda-codec: enabling ADC switching\n");
1742 spec->dyn_adc_switch = 1;
1743 } else if (nums != spec->num_adc_nids) {
1744 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1745 spec->num_adc_nids = nums;
1746 }
1747
1748 if (imux->num_items == 1 || spec->shared_mic_hp) {
1749 snd_printdd("hda-codec: reducing to a single ADC\n");
1750 spec->num_adc_nids = 1; /* reduce to a single ADC */
1751 }
1752
1753 /* single index for individual volumes ctls */
1754 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1755 spec->num_adc_nids = 1;
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return 0;
1758}
1759
1760/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001761 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001763static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001764{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001765 struct hda_gen_spec *spec = codec->spec;
1766 const struct auto_pin_cfg *cfg = &spec->autocfg;
1767 hda_nid_t mixer = spec->mixer_nid;
1768 struct hda_input_mux *imux = &spec->input_mux;
1769 int num_adcs;
1770 int i, c, err, type_idx = 0;
1771 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001772
Takashi Iwai352f7f92012-12-19 12:52:06 +01001773 num_adcs = fill_adc_nids(codec);
1774 if (num_adcs < 0)
1775 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001776
Takashi Iwai352f7f92012-12-19 12:52:06 +01001777 for (i = 0; i < cfg->num_inputs; i++) {
1778 hda_nid_t pin;
1779 const char *label;
1780 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Takashi Iwai352f7f92012-12-19 12:52:06 +01001782 pin = cfg->inputs[i].pin;
1783 if (!is_input_pin(codec, pin))
1784 continue;
1785
1786 label = hda_get_autocfg_input_label(codec, cfg, i);
1787 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1788 label = "Headphone Mic";
1789 if (prev_label && !strcmp(label, prev_label))
1790 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001791 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001792 type_idx = 0;
1793 prev_label = label;
1794
1795 if (mixer) {
1796 if (is_reachable_path(codec, pin, mixer)) {
1797 err = new_analog_input(codec, pin,
1798 label, type_idx, mixer);
1799 if (err < 0)
1800 return err;
1801 }
1802 }
1803
1804 imux_added = false;
1805 for (c = 0; c < num_adcs; c++) {
1806 struct nid_path *path;
1807 hda_nid_t adc = spec->adc_nids[c];
1808
1809 if (!is_reachable_path(codec, pin, adc))
1810 continue;
1811 path = snd_array_new(&spec->paths);
1812 if (!path)
1813 return -ENOMEM;
1814 memset(path, 0, sizeof(*path));
1815 if (!snd_hda_parse_nid_path(codec, pin, adc, 2, path)) {
1816 snd_printd(KERN_ERR
1817 "invalid input path 0x%x -> 0x%x\n",
1818 pin, adc);
1819 spec->paths.used--;
1820 continue;
1821 }
1822
1823 if (!imux_added) {
1824 spec->imux_pins[imux->num_items] = pin;
1825 snd_hda_add_imux_item(imux, label,
1826 imux->num_items, NULL);
1827 imux_added = true;
1828 }
1829 }
1830 }
1831
1832 return 0;
1833}
1834
1835
1836/*
1837 * input source mux
1838 */
1839
1840/* get the ADC NID corresponding to the given index */
1841static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1842{
1843 struct hda_gen_spec *spec = codec->spec;
1844 if (spec->dyn_adc_switch)
1845 adc_idx = spec->dyn_adc_idx[imux_idx];
1846 return spec->adc_nids[adc_idx];
1847}
1848
1849static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1850 unsigned int idx);
1851
1852static int mux_enum_info(struct snd_kcontrol *kcontrol,
1853 struct snd_ctl_elem_info *uinfo)
1854{
1855 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1856 struct hda_gen_spec *spec = codec->spec;
1857 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1858}
1859
1860static int mux_enum_get(struct snd_kcontrol *kcontrol,
1861 struct snd_ctl_elem_value *ucontrol)
1862{
1863 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1864 struct hda_gen_spec *spec = codec->spec;
1865 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1866
1867 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1868 return 0;
1869}
1870
1871static int mux_enum_put(struct snd_kcontrol *kcontrol,
1872 struct snd_ctl_elem_value *ucontrol)
1873{
1874 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1875 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1876 return mux_select(codec, adc_idx,
1877 ucontrol->value.enumerated.item[0]);
1878}
1879
Takashi Iwai352f7f92012-12-19 12:52:06 +01001880static const struct snd_kcontrol_new cap_src_temp = {
1881 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1882 .name = "Input Source",
1883 .info = mux_enum_info,
1884 .get = mux_enum_get,
1885 .put = mux_enum_put,
1886};
1887
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001888/*
1889 * capture volume and capture switch ctls
1890 */
1891
Takashi Iwai352f7f92012-12-19 12:52:06 +01001892typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
1893 struct snd_ctl_elem_value *ucontrol);
1894
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001895/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001896static int cap_put_caller(struct snd_kcontrol *kcontrol,
1897 struct snd_ctl_elem_value *ucontrol,
1898 put_call_t func, int type)
1899{
1900 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1901 struct hda_gen_spec *spec = codec->spec;
1902 const struct hda_input_mux *imux;
1903 struct nid_path *path;
1904 int i, adc_idx, err = 0;
1905
1906 imux = &spec->input_mux;
1907 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1908 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001909 /* we use the cache-only update at first since multiple input paths
1910 * may shared the same amp; by updating only caches, the redundant
1911 * writes to hardware can be reduced.
1912 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001913 codec->cached_write = 1;
1914 for (i = 0; i < imux->num_items; i++) {
1915 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
1916 get_adc_nid(codec, adc_idx, i));
1917 if (!path->ctls[type])
1918 continue;
1919 kcontrol->private_value = path->ctls[type];
1920 err = func(kcontrol, ucontrol);
1921 if (err < 0)
1922 goto error;
1923 }
1924 error:
1925 codec->cached_write = 0;
1926 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001927 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001928 if (err >= 0 && spec->cap_sync_hook)
1929 spec->cap_sync_hook(codec);
1930 return err;
1931}
1932
1933/* capture volume ctl callbacks */
1934#define cap_vol_info snd_hda_mixer_amp_volume_info
1935#define cap_vol_get snd_hda_mixer_amp_volume_get
1936#define cap_vol_tlv snd_hda_mixer_amp_tlv
1937
1938static int cap_vol_put(struct snd_kcontrol *kcontrol,
1939 struct snd_ctl_elem_value *ucontrol)
1940{
1941 return cap_put_caller(kcontrol, ucontrol,
1942 snd_hda_mixer_amp_volume_put,
1943 NID_PATH_VOL_CTL);
1944}
1945
1946static const struct snd_kcontrol_new cap_vol_temp = {
1947 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1948 .name = "Capture Volume",
1949 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1950 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1951 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
1952 .info = cap_vol_info,
1953 .get = cap_vol_get,
1954 .put = cap_vol_put,
1955 .tlv = { .c = cap_vol_tlv },
1956};
1957
1958/* capture switch ctl callbacks */
1959#define cap_sw_info snd_ctl_boolean_stereo_info
1960#define cap_sw_get snd_hda_mixer_amp_switch_get
1961
1962static int cap_sw_put(struct snd_kcontrol *kcontrol,
1963 struct snd_ctl_elem_value *ucontrol)
1964{
1965 return cap_put_caller(kcontrol, ucontrol,
1966 snd_hda_mixer_amp_switch_put,
1967 NID_PATH_MUTE_CTL);
1968}
1969
1970static const struct snd_kcontrol_new cap_sw_temp = {
1971 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1972 .name = "Capture Switch",
1973 .info = cap_sw_info,
1974 .get = cap_sw_get,
1975 .put = cap_sw_put,
1976};
1977
1978static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
1979{
1980 hda_nid_t nid;
1981 int i, depth;
1982
1983 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
1984 for (depth = 0; depth < 3; depth++) {
1985 if (depth >= path->depth)
1986 return -EINVAL;
1987 i = path->depth - depth - 1;
1988 nid = path->path[i];
1989 if (!path->ctls[NID_PATH_VOL_CTL]) {
1990 if (nid_has_volume(codec, nid, HDA_OUTPUT))
1991 path->ctls[NID_PATH_VOL_CTL] =
1992 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1993 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
1994 int idx = path->idx[i];
1995 if (!depth && codec->single_adc_amp)
1996 idx = 0;
1997 path->ctls[NID_PATH_VOL_CTL] =
1998 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
1999 }
2000 }
2001 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2002 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2003 path->ctls[NID_PATH_MUTE_CTL] =
2004 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2005 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2006 int idx = path->idx[i];
2007 if (!depth && codec->single_adc_amp)
2008 idx = 0;
2009 path->ctls[NID_PATH_MUTE_CTL] =
2010 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2011 }
2012 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return 0;
2015}
2016
Takashi Iwai352f7f92012-12-19 12:52:06 +01002017static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002019 struct hda_gen_spec *spec = codec->spec;
2020 struct auto_pin_cfg *cfg = &spec->autocfg;
2021 unsigned int val;
2022 int i;
2023
2024 if (!spec->inv_dmic_split)
2025 return false;
2026 for (i = 0; i < cfg->num_inputs; i++) {
2027 if (cfg->inputs[i].pin != nid)
2028 continue;
2029 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2030 return false;
2031 val = snd_hda_codec_get_pincfg(codec, nid);
2032 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2033 }
2034 return false;
2035}
2036
2037static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2038 int idx, bool is_switch, unsigned int ctl,
2039 bool inv_dmic)
2040{
2041 struct hda_gen_spec *spec = codec->spec;
2042 char tmpname[44];
2043 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2044 const char *sfx = is_switch ? "Switch" : "Volume";
2045 unsigned int chs = inv_dmic ? 1 : 3;
2046 int err;
2047
2048 if (!ctl)
2049 return 0;
2050
2051 if (label)
2052 snprintf(tmpname, sizeof(tmpname),
2053 "%s Capture %s", label, sfx);
2054 else
2055 snprintf(tmpname, sizeof(tmpname),
2056 "Capture %s", sfx);
2057 err = add_control(spec, type, tmpname, idx,
2058 amp_val_replace_channels(ctl, chs));
2059 if (err < 0 || !inv_dmic)
2060 return err;
2061
2062 /* Make independent right kcontrol */
2063 if (label)
2064 snprintf(tmpname, sizeof(tmpname),
2065 "Inverted %s Capture %s", label, sfx);
2066 else
2067 snprintf(tmpname, sizeof(tmpname),
2068 "Inverted Capture %s", sfx);
2069 return add_control(spec, type, tmpname, idx,
2070 amp_val_replace_channels(ctl, 2));
2071}
2072
2073/* create single (and simple) capture volume and switch controls */
2074static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2075 unsigned int vol_ctl, unsigned int sw_ctl,
2076 bool inv_dmic)
2077{
2078 int err;
2079 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2080 if (err < 0)
2081 return err;
2082 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2083 if (err < 0)
2084 return err;
2085 return 0;
2086}
2087
2088/* create bound capture volume and switch controls */
2089static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2090 unsigned int vol_ctl, unsigned int sw_ctl)
2091{
2092 struct hda_gen_spec *spec = codec->spec;
2093 struct snd_kcontrol_new *knew;
2094
2095 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002096 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002097 if (!knew)
2098 return -ENOMEM;
2099 knew->index = idx;
2100 knew->private_value = vol_ctl;
2101 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2102 }
2103 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002104 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002105 if (!knew)
2106 return -ENOMEM;
2107 knew->index = idx;
2108 knew->private_value = sw_ctl;
2109 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2110 }
2111 return 0;
2112}
2113
2114/* return the vol ctl when used first in the imux list */
2115static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2116{
2117 struct hda_gen_spec *spec = codec->spec;
2118 struct nid_path *path;
2119 unsigned int ctl;
2120 int i;
2121
2122 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2123 get_adc_nid(codec, 0, idx));
2124 if (!path)
2125 return 0;
2126 ctl = path->ctls[type];
2127 if (!ctl)
2128 return 0;
2129 for (i = 0; i < idx - 1; i++) {
2130 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2131 get_adc_nid(codec, 0, i));
2132 if (path && path->ctls[type] == ctl)
2133 return 0;
2134 }
2135 return ctl;
2136}
2137
2138/* create individual capture volume and switch controls per input */
2139static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2140{
2141 struct hda_gen_spec *spec = codec->spec;
2142 struct hda_input_mux *imux = &spec->input_mux;
2143 int i, err, type, type_idx = 0;
2144 const char *prev_label = NULL;
2145
2146 for (i = 0; i < imux->num_items; i++) {
2147 const char *label;
2148 bool inv_dmic;
2149 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2150 if (prev_label && !strcmp(label, prev_label))
2151 type_idx++;
2152 else
2153 type_idx = 0;
2154 prev_label = label;
2155 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2156
2157 for (type = 0; type < 2; type++) {
2158 err = add_single_cap_ctl(codec, label, type_idx, type,
2159 get_first_cap_ctl(codec, i, type),
2160 inv_dmic);
2161 if (err < 0)
2162 return err;
2163 }
2164 }
2165 return 0;
2166}
2167
2168static int create_capture_mixers(struct hda_codec *codec)
2169{
2170 struct hda_gen_spec *spec = codec->spec;
2171 struct hda_input_mux *imux = &spec->input_mux;
2172 int i, n, nums, err;
2173
2174 if (spec->dyn_adc_switch)
2175 nums = 1;
2176 else
2177 nums = spec->num_adc_nids;
2178
2179 if (!spec->auto_mic && imux->num_items > 1) {
2180 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002181 const char *name;
2182 name = nums > 1 ? "Input Source" : "Capture Source";
2183 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002184 if (!knew)
2185 return -ENOMEM;
2186 knew->count = nums;
2187 }
2188
2189 for (n = 0; n < nums; n++) {
2190 bool multi = false;
2191 bool inv_dmic = false;
2192 int vol, sw;
2193
2194 vol = sw = 0;
2195 for (i = 0; i < imux->num_items; i++) {
2196 struct nid_path *path;
2197 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2198 get_adc_nid(codec, n, i));
2199 if (!path)
2200 continue;
2201 parse_capvol_in_path(codec, path);
2202 if (!vol)
2203 vol = path->ctls[NID_PATH_VOL_CTL];
2204 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2205 multi = true;
2206 if (!sw)
2207 sw = path->ctls[NID_PATH_MUTE_CTL];
2208 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2209 multi = true;
2210 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2211 inv_dmic = true;
2212 }
2213
2214 if (!multi)
2215 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2216 inv_dmic);
2217 else if (!spec->multi_cap_vol)
2218 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2219 else
2220 err = create_multi_cap_vol_ctl(codec);
2221 if (err < 0)
2222 return err;
2223 }
2224
2225 return 0;
2226}
2227
2228/*
2229 * add mic boosts if needed
2230 */
2231static int parse_mic_boost(struct hda_codec *codec)
2232{
2233 struct hda_gen_spec *spec = codec->spec;
2234 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002235 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002236 int type_idx = 0;
2237 hda_nid_t nid;
2238 const char *prev_label = NULL;
2239
2240 for (i = 0; i < cfg->num_inputs; i++) {
2241 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2242 break;
2243 nid = cfg->inputs[i].pin;
2244 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2245 const char *label;
2246 char boost_label[32];
2247 struct nid_path *path;
2248 unsigned int val;
2249
2250 label = hda_get_autocfg_input_label(codec, cfg, i);
2251 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2252 label = "Headphone Mic";
2253 if (prev_label && !strcmp(label, prev_label))
2254 type_idx++;
2255 else
2256 type_idx = 0;
2257 prev_label = label;
2258
2259 snprintf(boost_label, sizeof(boost_label),
2260 "%s Boost Volume", label);
2261 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2262 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2263 boost_label, type_idx, val);
2264 if (err < 0)
2265 return err;
2266
2267 path = snd_hda_get_nid_path(codec, nid, 0);
2268 if (path)
2269 path->ctls[NID_PATH_BOOST_CTL] = val;
2270 }
2271 }
2272 return 0;
2273}
2274
2275/*
2276 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2277 */
2278static void parse_digital(struct hda_codec *codec)
2279{
2280 struct hda_gen_spec *spec = codec->spec;
2281 int i, nums;
2282 hda_nid_t dig_nid;
2283
2284 /* support multiple SPDIFs; the secondary is set up as a slave */
2285 nums = 0;
2286 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2287 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2288 dig_nid = look_for_dac(codec, pin, true);
2289 if (!dig_nid)
2290 continue;
2291 if (!snd_hda_add_new_path(codec, dig_nid, pin, 2))
2292 continue;
2293 if (!nums) {
2294 spec->multiout.dig_out_nid = dig_nid;
2295 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2296 } else {
2297 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2298 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2299 break;
2300 spec->slave_dig_outs[nums - 1] = dig_nid;
2301 }
2302 nums++;
2303 }
2304
2305 if (spec->autocfg.dig_in_pin) {
2306 dig_nid = codec->start_nid;
2307 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2308 struct nid_path *path;
2309 unsigned int wcaps = get_wcaps(codec, dig_nid);
2310 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2311 continue;
2312 if (!(wcaps & AC_WCAP_DIGITAL))
2313 continue;
2314 path = snd_hda_add_new_path(codec,
2315 spec->autocfg.dig_in_pin,
2316 dig_nid, 2);
2317 if (path) {
2318 path->active = true;
2319 spec->dig_in_nid = dig_nid;
2320 break;
2321 }
2322 }
2323 }
2324}
2325
2326
2327/*
2328 * input MUX handling
2329 */
2330
2331static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2332
2333/* select the given imux item; either unmute exclusively or select the route */
2334static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2335 unsigned int idx)
2336{
2337 struct hda_gen_spec *spec = codec->spec;
2338 const struct hda_input_mux *imux;
2339 struct nid_path *path;
2340
2341 imux = &spec->input_mux;
2342 if (!imux->num_items)
2343 return 0;
2344
2345 if (idx >= imux->num_items)
2346 idx = imux->num_items - 1;
2347 if (spec->cur_mux[adc_idx] == idx)
2348 return 0;
2349
2350 path = snd_hda_get_nid_path(codec,
2351 spec->imux_pins[spec->cur_mux[adc_idx]],
2352 spec->adc_nids[adc_idx]);
2353 if (!path)
2354 return 0;
2355 if (path->active)
2356 snd_hda_activate_path(codec, path, false, false);
2357
2358 spec->cur_mux[adc_idx] = idx;
2359
2360 if (spec->shared_mic_hp)
2361 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2362
2363 if (spec->dyn_adc_switch)
2364 dyn_adc_pcm_resetup(codec, idx);
2365
2366 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2367 get_adc_nid(codec, adc_idx, idx));
2368 if (!path)
2369 return 0;
2370 if (path->active)
2371 return 0;
2372 snd_hda_activate_path(codec, path, true, false);
2373 if (spec->cap_sync_hook)
2374 spec->cap_sync_hook(codec);
2375 return 1;
2376}
2377
2378
2379/*
2380 * Jack detections for HP auto-mute and mic-switch
2381 */
2382
2383/* check each pin in the given array; returns true if any of them is plugged */
2384static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2385{
2386 int i, present = 0;
2387
2388 for (i = 0; i < num_pins; i++) {
2389 hda_nid_t nid = pins[i];
2390 if (!nid)
2391 break;
2392 present |= snd_hda_jack_detect(codec, nid);
2393 }
2394 return present;
2395}
2396
2397/* standard HP/line-out auto-mute helper */
2398static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2399 bool mute, bool hp_out)
2400{
2401 struct hda_gen_spec *spec = codec->spec;
2402 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2403 int i;
2404
2405 for (i = 0; i < num_pins; i++) {
2406 hda_nid_t nid = pins[i];
2407 unsigned int val;
2408 if (!nid)
2409 break;
2410 /* don't reset VREF value in case it's controlling
2411 * the amp (see alc861_fixup_asus_amp_vref_0f())
2412 */
2413 if (spec->keep_vref_in_automute) {
2414 val = snd_hda_codec_read(codec, nid, 0,
2415 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2416 val &= ~PIN_HP;
2417 } else
2418 val = 0;
2419 val |= pin_bits;
2420 snd_hda_set_pin_ctl(codec, nid, val);
2421 }
2422}
2423
2424/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002425void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002426{
2427 struct hda_gen_spec *spec = codec->spec;
2428 int on;
2429
2430 /* Control HP pins/amps depending on master_mute state;
2431 * in general, HP pins/amps control should be enabled in all cases,
2432 * but currently set only for master_mute, just to be safe
2433 */
2434 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2435 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2436 spec->autocfg.hp_pins, spec->master_mute, true);
2437
2438 if (!spec->automute_speaker)
2439 on = 0;
2440 else
2441 on = spec->hp_jack_present | spec->line_jack_present;
2442 on |= spec->master_mute;
2443 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2444 spec->autocfg.speaker_pins, on, false);
2445
2446 /* toggle line-out mutes if needed, too */
2447 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2448 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2449 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2450 return;
2451 if (!spec->automute_lo)
2452 on = 0;
2453 else
2454 on = spec->hp_jack_present;
2455 on |= spec->master_mute;
2456 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2457 spec->autocfg.line_out_pins, on, false);
2458}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002459EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002460
2461static void call_update_outputs(struct hda_codec *codec)
2462{
2463 struct hda_gen_spec *spec = codec->spec;
2464 if (spec->automute_hook)
2465 spec->automute_hook(codec);
2466 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002467 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002468}
2469
2470/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002471void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002472{
2473 struct hda_gen_spec *spec = codec->spec;
2474
2475 spec->hp_jack_present =
2476 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2477 spec->autocfg.hp_pins);
2478 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2479 return;
2480 call_update_outputs(codec);
2481}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002482EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002483
2484/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002485void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002486{
2487 struct hda_gen_spec *spec = codec->spec;
2488
2489 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2490 return;
2491 /* check LO jack only when it's different from HP */
2492 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2493 return;
2494
2495 spec->line_jack_present =
2496 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2497 spec->autocfg.line_out_pins);
2498 if (!spec->automute_speaker || !spec->detect_lo)
2499 return;
2500 call_update_outputs(codec);
2501}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002502EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002503
2504/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002505void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002506{
2507 struct hda_gen_spec *spec = codec->spec;
2508 int i;
2509
2510 if (!spec->auto_mic)
2511 return;
2512
2513 for (i = spec->am_num_entries - 1; i > 0; i--) {
2514 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2515 mux_select(codec, 0, spec->am_entry[i].idx);
2516 return;
2517 }
2518 }
2519 mux_select(codec, 0, spec->am_entry[0].idx);
2520}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002521EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002522
2523/*
2524 * Auto-Mute mode mixer enum support
2525 */
2526static int automute_mode_info(struct snd_kcontrol *kcontrol,
2527 struct snd_ctl_elem_info *uinfo)
2528{
2529 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2530 struct hda_gen_spec *spec = codec->spec;
2531 static const char * const texts3[] = {
2532 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002533 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Takashi Iwai352f7f92012-12-19 12:52:06 +01002535 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2536 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2537 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2538}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Takashi Iwai352f7f92012-12-19 12:52:06 +01002540static int automute_mode_get(struct snd_kcontrol *kcontrol,
2541 struct snd_ctl_elem_value *ucontrol)
2542{
2543 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2544 struct hda_gen_spec *spec = codec->spec;
2545 unsigned int val = 0;
2546 if (spec->automute_speaker)
2547 val++;
2548 if (spec->automute_lo)
2549 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002550
Takashi Iwai352f7f92012-12-19 12:52:06 +01002551 ucontrol->value.enumerated.item[0] = val;
2552 return 0;
2553}
2554
2555static int automute_mode_put(struct snd_kcontrol *kcontrol,
2556 struct snd_ctl_elem_value *ucontrol)
2557{
2558 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2559 struct hda_gen_spec *spec = codec->spec;
2560
2561 switch (ucontrol->value.enumerated.item[0]) {
2562 case 0:
2563 if (!spec->automute_speaker && !spec->automute_lo)
2564 return 0;
2565 spec->automute_speaker = 0;
2566 spec->automute_lo = 0;
2567 break;
2568 case 1:
2569 if (spec->automute_speaker_possible) {
2570 if (!spec->automute_lo && spec->automute_speaker)
2571 return 0;
2572 spec->automute_speaker = 1;
2573 spec->automute_lo = 0;
2574 } else if (spec->automute_lo_possible) {
2575 if (spec->automute_lo)
2576 return 0;
2577 spec->automute_lo = 1;
2578 } else
2579 return -EINVAL;
2580 break;
2581 case 2:
2582 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2583 return -EINVAL;
2584 if (spec->automute_speaker && spec->automute_lo)
2585 return 0;
2586 spec->automute_speaker = 1;
2587 spec->automute_lo = 1;
2588 break;
2589 default:
2590 return -EINVAL;
2591 }
2592 call_update_outputs(codec);
2593 return 1;
2594}
2595
2596static const struct snd_kcontrol_new automute_mode_enum = {
2597 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2598 .name = "Auto-Mute Mode",
2599 .info = automute_mode_info,
2600 .get = automute_mode_get,
2601 .put = automute_mode_put,
2602};
2603
2604static int add_automute_mode_enum(struct hda_codec *codec)
2605{
2606 struct hda_gen_spec *spec = codec->spec;
2607
Takashi Iwai12c93df2012-12-19 14:38:33 +01002608 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002609 return -ENOMEM;
2610 return 0;
2611}
2612
2613/*
2614 * Check the availability of HP/line-out auto-mute;
2615 * Set up appropriately if really supported
2616 */
2617static int check_auto_mute_availability(struct hda_codec *codec)
2618{
2619 struct hda_gen_spec *spec = codec->spec;
2620 struct auto_pin_cfg *cfg = &spec->autocfg;
2621 int present = 0;
2622 int i, err;
2623
2624 if (cfg->hp_pins[0])
2625 present++;
2626 if (cfg->line_out_pins[0])
2627 present++;
2628 if (cfg->speaker_pins[0])
2629 present++;
2630 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002631 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002632
2633 if (!cfg->speaker_pins[0] &&
2634 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2635 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2636 sizeof(cfg->speaker_pins));
2637 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Takashi Iwai352f7f92012-12-19 12:52:06 +01002640 if (!cfg->hp_pins[0] &&
2641 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2642 memcpy(cfg->hp_pins, cfg->line_out_pins,
2643 sizeof(cfg->hp_pins));
2644 cfg->hp_outs = cfg->line_outs;
2645 }
2646
2647 for (i = 0; i < cfg->hp_outs; i++) {
2648 hda_nid_t nid = cfg->hp_pins[i];
2649 if (!is_jack_detectable(codec, nid))
2650 continue;
2651 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2652 nid);
2653 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002654 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002655 spec->detect_hp = 1;
2656 }
2657
2658 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2659 if (cfg->speaker_outs)
2660 for (i = 0; i < cfg->line_outs; i++) {
2661 hda_nid_t nid = cfg->line_out_pins[i];
2662 if (!is_jack_detectable(codec, nid))
2663 continue;
2664 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2665 snd_hda_jack_detect_enable_callback(codec, nid,
2666 HDA_GEN_FRONT_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002667 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002668 spec->detect_lo = 1;
2669 }
2670 spec->automute_lo_possible = spec->detect_hp;
2671 }
2672
2673 spec->automute_speaker_possible = cfg->speaker_outs &&
2674 (spec->detect_hp || spec->detect_lo);
2675
2676 spec->automute_lo = spec->automute_lo_possible;
2677 spec->automute_speaker = spec->automute_speaker_possible;
2678
2679 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2680 /* create a control for automute mode */
2681 err = add_automute_mode_enum(codec);
2682 if (err < 0)
2683 return err;
2684 }
2685 return 0;
2686}
2687
2688/* return the position of NID in the list, or -1 if not found */
2689static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2690{
2691 int i;
2692 for (i = 0; i < nums; i++)
2693 if (list[i] == nid)
2694 return i;
2695 return -1;
2696}
2697
2698/* check whether all auto-mic pins are valid; setup indices if OK */
2699static bool auto_mic_check_imux(struct hda_codec *codec)
2700{
2701 struct hda_gen_spec *spec = codec->spec;
2702 const struct hda_input_mux *imux;
2703 int i;
2704
2705 imux = &spec->input_mux;
2706 for (i = 0; i < spec->am_num_entries; i++) {
2707 spec->am_entry[i].idx =
2708 find_idx_in_nid_list(spec->am_entry[i].pin,
2709 spec->imux_pins, imux->num_items);
2710 if (spec->am_entry[i].idx < 0)
2711 return false; /* no corresponding imux */
2712 }
2713
2714 /* we don't need the jack detection for the first pin */
2715 for (i = 1; i < spec->am_num_entries; i++)
2716 snd_hda_jack_detect_enable_callback(codec,
2717 spec->am_entry[i].pin,
2718 HDA_GEN_MIC_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002719 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002720 return true;
2721}
2722
2723static int compare_attr(const void *ap, const void *bp)
2724{
2725 const struct automic_entry *a = ap;
2726 const struct automic_entry *b = bp;
2727 return (int)(a->attr - b->attr);
2728}
2729
2730/*
2731 * Check the availability of auto-mic switch;
2732 * Set up if really supported
2733 */
2734static int check_auto_mic_availability(struct hda_codec *codec)
2735{
2736 struct hda_gen_spec *spec = codec->spec;
2737 struct auto_pin_cfg *cfg = &spec->autocfg;
2738 unsigned int types;
2739 int i, num_pins;
2740
2741 types = 0;
2742 num_pins = 0;
2743 for (i = 0; i < cfg->num_inputs; i++) {
2744 hda_nid_t nid = cfg->inputs[i].pin;
2745 unsigned int attr;
2746 attr = snd_hda_codec_get_pincfg(codec, nid);
2747 attr = snd_hda_get_input_pin_attr(attr);
2748 if (types & (1 << attr))
2749 return 0; /* already occupied */
2750 switch (attr) {
2751 case INPUT_PIN_ATTR_INT:
2752 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2753 return 0; /* invalid type */
2754 break;
2755 case INPUT_PIN_ATTR_UNUSED:
2756 return 0; /* invalid entry */
2757 default:
2758 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2759 return 0; /* invalid type */
2760 if (!spec->line_in_auto_switch &&
2761 cfg->inputs[i].type != AUTO_PIN_MIC)
2762 return 0; /* only mic is allowed */
2763 if (!is_jack_detectable(codec, nid))
2764 return 0; /* no unsol support */
2765 break;
2766 }
2767 if (num_pins >= MAX_AUTO_MIC_PINS)
2768 return 0;
2769 types |= (1 << attr);
2770 spec->am_entry[num_pins].pin = nid;
2771 spec->am_entry[num_pins].attr = attr;
2772 num_pins++;
2773 }
2774
2775 if (num_pins < 2)
2776 return 0;
2777
2778 spec->am_num_entries = num_pins;
2779 /* sort the am_entry in the order of attr so that the pin with a
2780 * higher attr will be selected when the jack is plugged.
2781 */
2782 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2783 compare_attr, NULL);
2784
2785 if (!auto_mic_check_imux(codec))
2786 return 0;
2787
2788 spec->auto_mic = 1;
2789 spec->num_adc_nids = 1;
2790 spec->cur_mux[0] = spec->am_entry[0].idx;
2791 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2792 spec->am_entry[0].pin,
2793 spec->am_entry[1].pin,
2794 spec->am_entry[2].pin);
2795
2796 return 0;
2797}
2798
2799
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002800/*
2801 * Parse the given BIOS configuration and set up the hda_gen_spec
2802 *
2803 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002804 * or a negative error code
2805 */
2806int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002807 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002808{
2809 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002810 int err;
2811
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002812 if (cfg != &spec->autocfg) {
2813 spec->autocfg = *cfg;
2814 cfg = &spec->autocfg;
2815 }
2816
Takashi Iwai352f7f92012-12-19 12:52:06 +01002817 if (!cfg->line_outs) {
2818 if (cfg->dig_outs || cfg->dig_in_pin) {
2819 spec->multiout.max_channels = 2;
2820 spec->no_analog = 1;
2821 goto dig_only;
2822 }
2823 return 0; /* can't find valid BIOS pin config */
2824 }
2825
2826 if (!spec->no_primary_hp &&
2827 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2828 cfg->line_outs <= cfg->hp_outs) {
2829 /* use HP as primary out */
2830 cfg->speaker_outs = cfg->line_outs;
2831 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2832 sizeof(cfg->speaker_pins));
2833 cfg->line_outs = cfg->hp_outs;
2834 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2835 cfg->hp_outs = 0;
2836 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2837 cfg->line_out_type = AUTO_PIN_HP_OUT;
2838 }
2839
2840 err = parse_output_paths(codec);
2841 if (err < 0)
2842 return err;
2843 err = create_multi_channel_mode(codec);
2844 if (err < 0)
2845 return err;
2846 err = create_multi_out_ctls(codec, cfg);
2847 if (err < 0)
2848 return err;
2849 err = create_hp_out_ctls(codec);
2850 if (err < 0)
2851 return err;
2852 err = create_speaker_out_ctls(codec);
2853 if (err < 0)
2854 return err;
2855 err = create_shared_input(codec);
2856 if (err < 0)
2857 return err;
2858 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002859 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02002860 return err;
2861
Takashi Iwai352f7f92012-12-19 12:52:06 +01002862 /* check the multiple speaker pins */
2863 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2864 spec->const_channel_count = cfg->line_outs * 2;
2865 else
2866 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002867
Takashi Iwai352f7f92012-12-19 12:52:06 +01002868 if (spec->multi_ios > 0)
2869 spec->multiout.max_channels = max(spec->ext_channel_count,
2870 spec->const_channel_count);
2871 else
2872 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2873
2874 err = check_auto_mute_availability(codec);
2875 if (err < 0)
2876 return err;
2877
2878 err = check_dyn_adc_switch(codec);
2879 if (err < 0)
2880 return err;
2881
2882 if (!spec->shared_mic_hp) {
2883 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002884 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02002887
Takashi Iwai352f7f92012-12-19 12:52:06 +01002888 err = create_capture_mixers(codec);
2889 if (err < 0)
2890 return err;
2891
2892 err = parse_mic_boost(codec);
2893 if (err < 0)
2894 return err;
2895
2896 dig_only:
2897 parse_digital(codec);
2898
2899 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002901EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903
2904/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002905 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002907
2908/* slave controls for virtual master */
2909static const char * const slave_pfxs[] = {
2910 "Front", "Surround", "Center", "LFE", "Side",
2911 "Headphone", "Speaker", "Mono", "Line Out",
2912 "CLFE", "Bass Speaker", "PCM",
2913 NULL,
2914};
2915
2916int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002918 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Takashi Iwai36502d02012-12-19 15:15:10 +01002921 if (spec->kctls.used) {
2922 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
2923 if (err < 0)
2924 return err;
2925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Takashi Iwai352f7f92012-12-19 12:52:06 +01002927 if (spec->multiout.dig_out_nid) {
2928 err = snd_hda_create_dig_out_ctls(codec,
2929 spec->multiout.dig_out_nid,
2930 spec->multiout.dig_out_nid,
2931 spec->pcm_rec[1].pcm_type);
2932 if (err < 0)
2933 return err;
2934 if (!spec->no_analog) {
2935 err = snd_hda_create_spdif_share_sw(codec,
2936 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (err < 0)
2938 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002939 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 }
2941 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002942 if (spec->dig_in_nid) {
2943 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
2944 if (err < 0)
2945 return err;
2946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Takashi Iwai352f7f92012-12-19 12:52:06 +01002948 /* if we have no master control, let's create it */
2949 if (!spec->no_analog &&
2950 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
2951 unsigned int vmaster_tlv[4];
2952 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2953 HDA_OUTPUT, vmaster_tlv);
2954 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
2955 vmaster_tlv, slave_pfxs,
2956 "Playback Volume");
2957 if (err < 0)
2958 return err;
2959 }
2960 if (!spec->no_analog &&
2961 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
2962 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
2963 NULL, slave_pfxs,
2964 "Playback Switch",
2965 true, &spec->vmaster_mute.sw_kctl);
2966 if (err < 0)
2967 return err;
2968 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01002969 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
2970 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Takashi Iwai352f7f92012-12-19 12:52:06 +01002973 free_kctls(spec); /* no longer needed */
2974
2975 if (spec->shared_mic_hp) {
2976 int err;
2977 int nid = spec->autocfg.inputs[1].pin;
2978 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
2979 if (err < 0)
2980 return err;
2981 err = snd_hda_jack_detect_enable(codec, nid, 0);
2982 if (err < 0)
2983 return err;
2984 }
2985
2986 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
2987 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 return err;
2989
2990 return 0;
2991}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002992EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
2993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
2995/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002996 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Takashi Iwai352f7f92012-12-19 12:52:06 +01002999/*
3000 * Analog playback callbacks
3001 */
3002static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3003 struct hda_codec *codec,
3004 struct snd_pcm_substream *substream)
3005{
3006 struct hda_gen_spec *spec = codec->spec;
3007 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
3008 hinfo);
3009}
3010
3011static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003012 struct hda_codec *codec,
3013 unsigned int stream_tag,
3014 unsigned int format,
3015 struct snd_pcm_substream *substream)
3016{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003017 struct hda_gen_spec *spec = codec->spec;
3018 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3019 stream_tag, format, substream);
3020}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003021
Takashi Iwai352f7f92012-12-19 12:52:06 +01003022static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3023 struct hda_codec *codec,
3024 struct snd_pcm_substream *substream)
3025{
3026 struct hda_gen_spec *spec = codec->spec;
3027 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3028}
3029
3030/*
3031 * Digital out
3032 */
3033static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3034 struct hda_codec *codec,
3035 struct snd_pcm_substream *substream)
3036{
3037 struct hda_gen_spec *spec = codec->spec;
3038 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3039}
3040
3041static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3042 struct hda_codec *codec,
3043 unsigned int stream_tag,
3044 unsigned int format,
3045 struct snd_pcm_substream *substream)
3046{
3047 struct hda_gen_spec *spec = codec->spec;
3048 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3049 stream_tag, format, substream);
3050}
3051
3052static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3053 struct hda_codec *codec,
3054 struct snd_pcm_substream *substream)
3055{
3056 struct hda_gen_spec *spec = codec->spec;
3057 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3058}
3059
3060static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3061 struct hda_codec *codec,
3062 struct snd_pcm_substream *substream)
3063{
3064 struct hda_gen_spec *spec = codec->spec;
3065 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3066}
3067
3068/*
3069 * Analog capture
3070 */
3071static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3072 struct hda_codec *codec,
3073 unsigned int stream_tag,
3074 unsigned int format,
3075 struct snd_pcm_substream *substream)
3076{
3077 struct hda_gen_spec *spec = codec->spec;
3078
3079 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003080 stream_tag, 0, format);
3081 return 0;
3082}
3083
Takashi Iwai352f7f92012-12-19 12:52:06 +01003084static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3085 struct hda_codec *codec,
3086 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003087{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003088 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003089
Takashi Iwai352f7f92012-12-19 12:52:06 +01003090 snd_hda_codec_cleanup_stream(codec,
3091 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003092 return 0;
3093}
3094
Takashi Iwai352f7f92012-12-19 12:52:06 +01003095/*
3096 */
3097static const struct hda_pcm_stream pcm_analog_playback = {
3098 .substreams = 1,
3099 .channels_min = 2,
3100 .channels_max = 8,
3101 /* NID is set in build_pcms */
3102 .ops = {
3103 .open = playback_pcm_open,
3104 .prepare = playback_pcm_prepare,
3105 .cleanup = playback_pcm_cleanup
3106 },
3107};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Takashi Iwai352f7f92012-12-19 12:52:06 +01003109static const struct hda_pcm_stream pcm_analog_capture = {
3110 .substreams = 1,
3111 .channels_min = 2,
3112 .channels_max = 2,
3113 /* NID is set in build_pcms */
3114};
3115
3116static const struct hda_pcm_stream pcm_analog_alt_playback = {
3117 .substreams = 1,
3118 .channels_min = 2,
3119 .channels_max = 2,
3120 /* NID is set in build_pcms */
3121};
3122
3123static const struct hda_pcm_stream pcm_analog_alt_capture = {
3124 .substreams = 2, /* can be overridden */
3125 .channels_min = 2,
3126 .channels_max = 2,
3127 /* NID is set in build_pcms */
3128 .ops = {
3129 .prepare = alt_capture_pcm_prepare,
3130 .cleanup = alt_capture_pcm_cleanup
3131 },
3132};
3133
3134static const struct hda_pcm_stream pcm_digital_playback = {
3135 .substreams = 1,
3136 .channels_min = 2,
3137 .channels_max = 2,
3138 /* NID is set in build_pcms */
3139 .ops = {
3140 .open = dig_playback_pcm_open,
3141 .close = dig_playback_pcm_close,
3142 .prepare = dig_playback_pcm_prepare,
3143 .cleanup = dig_playback_pcm_cleanup
3144 },
3145};
3146
3147static const struct hda_pcm_stream pcm_digital_capture = {
3148 .substreams = 1,
3149 .channels_min = 2,
3150 .channels_max = 2,
3151 /* NID is set in build_pcms */
3152};
3153
3154/* Used by build_pcms to flag that a PCM has no playback stream */
3155static const struct hda_pcm_stream pcm_null_stream = {
3156 .substreams = 0,
3157 .channels_min = 0,
3158 .channels_max = 0,
3159};
3160
3161/*
3162 * dynamic changing ADC PCM streams
3163 */
3164static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3165{
3166 struct hda_gen_spec *spec = codec->spec;
3167 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3168
3169 if (spec->cur_adc && spec->cur_adc != new_adc) {
3170 /* stream is running, let's swap the current ADC */
3171 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3172 spec->cur_adc = new_adc;
3173 snd_hda_codec_setup_stream(codec, new_adc,
3174 spec->cur_adc_stream_tag, 0,
3175 spec->cur_adc_format);
3176 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003178 return false;
3179}
3180
3181/* analog capture with dynamic dual-adc changes */
3182static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3183 struct hda_codec *codec,
3184 unsigned int stream_tag,
3185 unsigned int format,
3186 struct snd_pcm_substream *substream)
3187{
3188 struct hda_gen_spec *spec = codec->spec;
3189 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3190 spec->cur_adc_stream_tag = stream_tag;
3191 spec->cur_adc_format = format;
3192 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3193 return 0;
3194}
3195
3196static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3197 struct hda_codec *codec,
3198 struct snd_pcm_substream *substream)
3199{
3200 struct hda_gen_spec *spec = codec->spec;
3201 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3202 spec->cur_adc = 0;
3203 return 0;
3204}
3205
3206static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3207 .substreams = 1,
3208 .channels_min = 2,
3209 .channels_max = 2,
3210 .nid = 0, /* fill later */
3211 .ops = {
3212 .prepare = dyn_adc_capture_pcm_prepare,
3213 .cleanup = dyn_adc_capture_pcm_cleanup
3214 },
3215};
3216
3217/* build PCM streams based on the parsed results */
3218int snd_hda_gen_build_pcms(struct hda_codec *codec)
3219{
3220 struct hda_gen_spec *spec = codec->spec;
3221 struct hda_pcm *info = spec->pcm_rec;
3222 const struct hda_pcm_stream *p;
3223 bool have_multi_adcs;
3224 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
3226 codec->num_pcms = 1;
3227 codec->pcm_info = info;
3228
Takashi Iwai352f7f92012-12-19 12:52:06 +01003229 if (spec->no_analog)
3230 goto skip_analog;
3231
3232 snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
3233 "%s Analog", codec->chip_name);
3234 info->name = spec->stream_name_analog;
3235
3236 if (spec->multiout.num_dacs > 0) {
3237 p = spec->stream_analog_playback;
3238 if (!p)
3239 p = &pcm_analog_playback;
3240 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3241 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3242 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3243 spec->multiout.max_channels;
3244 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3245 spec->autocfg.line_outs == 2)
3246 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3247 snd_pcm_2_1_chmaps;
3248 }
3249 if (spec->num_adc_nids) {
3250 p = spec->stream_analog_capture;
3251 if (!p) {
3252 if (spec->dyn_adc_switch)
3253 p = &dyn_adc_pcm_analog_capture;
3254 else
3255 p = &pcm_analog_capture;
3256 }
3257 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3258 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3259 }
3260
3261 if (spec->channel_mode) {
3262 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
3263 for (i = 0; i < spec->num_channel_mode; i++) {
3264 if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
3265 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
3266 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003269
3270 skip_analog:
3271 /* SPDIF for stream index #1 */
3272 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3273 snprintf(spec->stream_name_digital,
3274 sizeof(spec->stream_name_digital),
3275 "%s Digital", codec->chip_name);
3276 codec->num_pcms = 2;
3277 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3278 info = spec->pcm_rec + 1;
3279 info->name = spec->stream_name_digital;
3280 if (spec->dig_out_type)
3281 info->pcm_type = spec->dig_out_type;
3282 else
3283 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3284 if (spec->multiout.dig_out_nid) {
3285 p = spec->stream_digital_playback;
3286 if (!p)
3287 p = &pcm_digital_playback;
3288 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3289 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3290 }
3291 if (spec->dig_in_nid) {
3292 p = spec->stream_digital_capture;
3293 if (!p)
3294 p = &pcm_digital_capture;
3295 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3296 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3297 }
3298 }
3299
3300 if (spec->no_analog)
3301 return 0;
3302
3303 /* If the use of more than one ADC is requested for the current
3304 * model, configure a second analog capture-only PCM.
3305 */
3306 have_multi_adcs = (spec->num_adc_nids > 1) &&
3307 !spec->dyn_adc_switch && !spec->auto_mic;
3308 /* Additional Analaog capture for index #2 */
3309 if (spec->alt_dac_nid || have_multi_adcs) {
3310 codec->num_pcms = 3;
3311 info = spec->pcm_rec + 2;
3312 info->name = spec->stream_name_analog;
3313 if (spec->alt_dac_nid) {
3314 p = spec->stream_analog_alt_playback;
3315 if (!p)
3316 p = &pcm_analog_alt_playback;
3317 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3318 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3319 spec->alt_dac_nid;
3320 } else {
3321 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3322 pcm_null_stream;
3323 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3324 }
3325 if (have_multi_adcs) {
3326 p = spec->stream_analog_alt_capture;
3327 if (!p)
3328 p = &pcm_analog_alt_capture;
3329 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3330 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3331 spec->adc_nids[1];
3332 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3333 spec->num_adc_nids - 1;
3334 } else {
3335 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3336 pcm_null_stream;
3337 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 }
3340
3341 return 0;
3342}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003343EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3344
3345
3346/*
3347 * Standard auto-parser initializations
3348 */
3349
3350/* configure the path from the given dac to the pin as the proper output */
3351static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3352 int pin_type, hda_nid_t dac)
3353{
Takashi Iwai731dc302012-12-19 13:01:54 +01003354 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003355 struct nid_path *path;
3356
3357 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3358 path = snd_hda_get_nid_path(codec, dac, pin);
3359 if (!path)
3360 return;
3361 if (path->active)
3362 return;
3363 snd_hda_activate_path(codec, path, true, true);
Takashi Iwai731dc302012-12-19 13:01:54 +01003364
3365 if (!spec->own_eapd_ctl &&
3366 (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
3367 snd_hda_codec_update_cache(codec, pin, 0,
3368 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003369}
3370
3371/* initialize primary output paths */
3372static void init_multi_out(struct hda_codec *codec)
3373{
3374 struct hda_gen_spec *spec = codec->spec;
3375 int pin_type;
3376 int i;
3377
3378 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3379 pin_type = PIN_HP;
3380 else
3381 pin_type = PIN_OUT;
3382
3383 for (i = 0; i <= HDA_SIDE; i++) {
3384 hda_nid_t nid = spec->autocfg.line_out_pins[i];
3385 if (nid)
3386 set_output_and_unmute(codec, nid, pin_type,
3387 spec->multiout.dac_nids[i]);
3388
3389 }
3390}
3391
3392/* initialize hp and speaker paths */
3393static void init_extra_out(struct hda_codec *codec)
3394{
3395 struct hda_gen_spec *spec = codec->spec;
3396 int i;
3397 hda_nid_t pin, dac;
3398
3399 for (i = 0; i < spec->autocfg.hp_outs; i++) {
3400 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3401 break;
3402 pin = spec->autocfg.hp_pins[i];
3403 if (!pin)
3404 break;
3405 dac = spec->multiout.hp_out_nid[i];
3406 if (!dac) {
3407 if (i > 0 && spec->multiout.hp_out_nid[0])
3408 dac = spec->multiout.hp_out_nid[0];
3409 else
3410 dac = spec->multiout.dac_nids[0];
3411 }
3412 set_output_and_unmute(codec, pin, PIN_HP, dac);
3413 }
3414 for (i = 0; i < spec->autocfg.speaker_outs; i++) {
3415 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3416 break;
3417 pin = spec->autocfg.speaker_pins[i];
3418 if (!pin)
3419 break;
3420 dac = spec->multiout.extra_out_nid[i];
3421 if (!dac) {
3422 if (i > 0 && spec->multiout.extra_out_nid[0])
3423 dac = spec->multiout.extra_out_nid[0];
3424 else
3425 dac = spec->multiout.dac_nids[0];
3426 }
3427 set_output_and_unmute(codec, pin, PIN_OUT, dac);
3428 }
3429}
3430
3431/* initialize multi-io paths */
3432static void init_multi_io(struct hda_codec *codec)
3433{
3434 struct hda_gen_spec *spec = codec->spec;
3435 int i;
3436
3437 for (i = 0; i < spec->multi_ios; i++) {
3438 hda_nid_t pin = spec->multi_io[i].pin;
3439 struct nid_path *path;
3440 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3441 if (!path)
3442 continue;
3443 if (!spec->multi_io[i].ctl_in)
3444 spec->multi_io[i].ctl_in =
3445 snd_hda_codec_update_cache(codec, pin, 0,
3446 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3447 snd_hda_activate_path(codec, path, path->active, true);
3448 }
3449}
3450
3451/* set up the input pin config, depending on the given auto-pin type */
3452static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3453 int auto_pin_type)
3454{
3455 unsigned int val = PIN_IN;
3456 if (auto_pin_type == AUTO_PIN_MIC)
3457 val |= snd_hda_get_default_vref(codec, nid);
3458 snd_hda_set_pin_ctl(codec, nid, val);
3459}
3460
3461/* set up input pins and loopback paths */
3462static void init_analog_input(struct hda_codec *codec)
3463{
3464 struct hda_gen_spec *spec = codec->spec;
3465 struct auto_pin_cfg *cfg = &spec->autocfg;
3466 int i;
3467
3468 for (i = 0; i < cfg->num_inputs; i++) {
3469 hda_nid_t nid = cfg->inputs[i].pin;
3470 if (is_input_pin(codec, nid))
3471 set_input_pin(codec, nid, cfg->inputs[i].type);
3472
3473 /* init loopback inputs */
3474 if (spec->mixer_nid) {
3475 struct nid_path *path;
3476 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3477 if (path)
3478 snd_hda_activate_path(codec, path,
3479 path->active, false);
3480 }
3481 }
3482}
3483
3484/* initialize ADC paths */
3485static void init_input_src(struct hda_codec *codec)
3486{
3487 struct hda_gen_spec *spec = codec->spec;
3488 struct hda_input_mux *imux = &spec->input_mux;
3489 struct nid_path *path;
3490 int i, c, nums;
3491
3492 if (spec->dyn_adc_switch)
3493 nums = 1;
3494 else
3495 nums = spec->num_adc_nids;
3496
3497 for (c = 0; c < nums; c++) {
3498 for (i = 0; i < imux->num_items; i++) {
3499 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3500 get_adc_nid(codec, c, i));
3501 if (path) {
3502 bool active = path->active;
3503 if (i == spec->cur_mux[c])
3504 active = true;
3505 snd_hda_activate_path(codec, path, active, false);
3506 }
3507 }
3508 }
3509
3510 if (spec->shared_mic_hp)
3511 update_shared_mic_hp(codec, spec->cur_mux[0]);
3512
3513 if (spec->cap_sync_hook)
3514 spec->cap_sync_hook(codec);
3515}
3516
3517/* set right pin controls for digital I/O */
3518static void init_digital(struct hda_codec *codec)
3519{
3520 struct hda_gen_spec *spec = codec->spec;
3521 int i;
3522 hda_nid_t pin;
3523
3524 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3525 pin = spec->autocfg.dig_out_pins[i];
3526 if (!pin)
3527 continue;
3528 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3529 }
3530 pin = spec->autocfg.dig_in_pin;
3531 if (pin)
3532 snd_hda_set_pin_ctl(codec, pin, PIN_IN);
3533}
3534
Takashi Iwai973e4972012-12-20 15:16:09 +01003535/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3536 * invalid unsol tags by some reason
3537 */
3538static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3539{
3540 int i;
3541
3542 for (i = 0; i < codec->init_pins.used; i++) {
3543 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3544 hda_nid_t nid = pin->nid;
3545 if (is_jack_detectable(codec, nid) &&
3546 !snd_hda_jack_tbl_get(codec, nid))
3547 snd_hda_codec_update_cache(codec, nid, 0,
3548 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3549 }
3550}
3551
Takashi Iwai352f7f92012-12-19 12:52:06 +01003552int snd_hda_gen_init(struct hda_codec *codec)
3553{
3554 struct hda_gen_spec *spec = codec->spec;
3555
3556 if (spec->init_hook)
3557 spec->init_hook(codec);
3558
3559 snd_hda_apply_verbs(codec);
3560
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003561 codec->cached_write = 1;
3562
Takashi Iwai352f7f92012-12-19 12:52:06 +01003563 init_multi_out(codec);
3564 init_extra_out(codec);
3565 init_multi_io(codec);
3566 init_analog_input(codec);
3567 init_input_src(codec);
3568 init_digital(codec);
3569
Takashi Iwai973e4972012-12-20 15:16:09 +01003570 clear_unsol_on_unused_pins(codec);
3571
Takashi Iwai352f7f92012-12-19 12:52:06 +01003572 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003573 snd_hda_gen_hp_automute(codec, NULL);
3574 snd_hda_gen_line_automute(codec, NULL);
3575 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003576
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003577 snd_hda_codec_flush_amp_cache(codec);
3578 snd_hda_codec_flush_cmd_cache(codec);
3579
Takashi Iwai352f7f92012-12-19 12:52:06 +01003580 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3581 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3582
3583 hda_call_check_power_status(codec, 0x01);
3584 return 0;
3585}
3586EXPORT_SYMBOL(snd_hda_gen_init);
3587
3588
3589/*
3590 * the generic codec support
3591 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
Takashi Iwai83012a72012-08-24 18:38:08 +02003593#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003594static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3595{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003596 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003597 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3598}
3599#endif
3600
Takashi Iwai352f7f92012-12-19 12:52:06 +01003601static void generic_free(struct hda_codec *codec)
3602{
3603 snd_hda_gen_spec_free(codec->spec);
3604 kfree(codec->spec);
3605 codec->spec = NULL;
3606}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
Takashi Iwai352f7f92012-12-19 12:52:06 +01003608static const struct hda_codec_ops generic_patch_ops = {
3609 .build_controls = snd_hda_gen_build_controls,
3610 .build_pcms = snd_hda_gen_build_pcms,
3611 .init = snd_hda_gen_init,
3612 .free = generic_free,
3613 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02003614#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003615 .check_power_status = generic_check_power_status,
3616#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617};
3618
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619int snd_hda_parse_generic_codec(struct hda_codec *codec)
3620{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003621 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 int err;
3623
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003624 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003625 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003627 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003630 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3631 if (err < 0)
3632 return err;
3633
3634 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003635 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 goto error;
3637
3638 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 return 0;
3640
Takashi Iwai352f7f92012-12-19 12:52:06 +01003641error:
3642 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 return err;
3644}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003645EXPORT_SYMBOL(snd_hda_parse_generic_codec);