blob: f9ecbe09a52669c82b8c9eb8dd7266ecb702613b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040025#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010026#include <linux/sort.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010027#include <linux/ctype.h>
28#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010030#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "hda_codec.h"
32#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include "hda_auto_parser.h"
34#include "hda_jack.h"
35#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwai352f7f92012-12-19 12:52:06 +010038/* initialize hda_gen_spec struct */
39int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Takashi Iwai352f7f92012-12-19 12:52:06 +010041 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42 snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
43 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010044 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010045 return 0;
46}
47EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Takashi Iwai12c93df2012-12-19 14:38:33 +010049struct snd_kcontrol_new *
50snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
51 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010052{
53 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
54 if (!knew)
55 return NULL;
56 *knew = *temp;
57 if (name)
58 knew->name = kstrdup(name, GFP_KERNEL);
59 else if (knew->name)
60 knew->name = kstrdup(knew->name, GFP_KERNEL);
61 if (!knew->name)
62 return NULL;
63 return knew;
64}
Takashi Iwai12c93df2012-12-19 14:38:33 +010065EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010066
67static void free_kctls(struct hda_gen_spec *spec)
68{
69 if (spec->kctls.list) {
70 struct snd_kcontrol_new *kctl = spec->kctls.list;
71 int i;
72 for (i = 0; i < spec->kctls.used; i++)
73 kfree(kctl[i].name);
74 }
75 snd_array_free(&spec->kctls);
76}
77
78static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
79 unsigned int nums,
80 struct hda_ctl_ops *ops)
81{
82 struct hda_gen_spec *spec = codec->spec;
83 struct hda_bind_ctls **ctlp, *ctl;
84 ctlp = snd_array_new(&spec->bind_ctls);
85 if (!ctlp)
86 return NULL;
87 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
88 *ctlp = ctl;
89 if (ctl)
90 ctl->ops = ops;
91 return ctl;
92}
93
94static void free_bind_ctls(struct hda_gen_spec *spec)
95{
96 if (spec->bind_ctls.list) {
97 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
98 int i;
99 for (i = 0; i < spec->bind_ctls.used; i++)
100 kfree(ctl[i]);
101 }
102 snd_array_free(&spec->bind_ctls);
103}
104
105void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
106{
107 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100109 free_kctls(spec);
110 free_bind_ctls(spec);
111 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100113EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100116 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Takashi Iwaif5172a72013-01-04 13:19:55 +0100119static struct nid_path *get_nid_path(struct hda_codec *codec,
120 hda_nid_t from_nid, hda_nid_t to_nid,
121 int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100123 struct hda_gen_spec *spec = codec->spec;
124 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Takashi Iwai352f7f92012-12-19 12:52:06 +0100126 for (i = 0; i < spec->paths.used; i++) {
127 struct nid_path *path = snd_array_elem(&spec->paths, i);
128 if (path->depth <= 0)
129 continue;
130 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100131 (!to_nid || path->path[path->depth - 1] == to_nid)) {
132 if (with_aa_mix == HDA_PARSE_ALL ||
133 path->with_aa_mix == with_aa_mix)
134 return path;
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137 return NULL;
138}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100139
140/* get the path between the given NIDs;
141 * passing 0 to either @pin or @dac behaves as a wildcard
142 */
143struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
144 hda_nid_t from_nid, hda_nid_t to_nid)
145{
146 return get_nid_path(codec, from_nid, to_nid, HDA_PARSE_ALL);
147}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100148EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
149
150/* check whether the given DAC is already found in any existing paths */
151static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
152{
153 struct hda_gen_spec *spec = codec->spec;
154 int i;
155
156 for (i = 0; i < spec->paths.used; i++) {
157 struct nid_path *path = snd_array_elem(&spec->paths, i);
158 if (path->path[0] == nid)
159 return true;
160 }
161 return false;
162}
163
164/* check whether the given two widgets can be connected */
165static bool is_reachable_path(struct hda_codec *codec,
166 hda_nid_t from_nid, hda_nid_t to_nid)
167{
168 if (!from_nid || !to_nid)
169 return false;
170 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
171}
172
173/* nid, dir and idx */
174#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
175
176/* check whether the given ctl is already assigned in any path elements */
177static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
178{
179 struct hda_gen_spec *spec = codec->spec;
180 int i;
181
182 val &= AMP_VAL_COMPARE_MASK;
183 for (i = 0; i < spec->paths.used; i++) {
184 struct nid_path *path = snd_array_elem(&spec->paths, i);
185 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
186 return true;
187 }
188 return false;
189}
190
191/* check whether a control with the given (nid, dir, idx) was assigned */
192static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
193 int dir, int idx)
194{
195 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
196 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
197 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
198}
199
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100200static void print_nid_path(const char *pfx, struct nid_path *path)
201{
202 char buf[40];
203 int i;
204
205
206 buf[0] = 0;
207 for (i = 0; i < path->depth; i++) {
208 char tmp[4];
209 sprintf(tmp, ":%02x", path->path[i]);
210 strlcat(buf, tmp, sizeof(buf));
211 }
212 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
213}
214
Takashi Iwai352f7f92012-12-19 12:52:06 +0100215/* called recursively */
216static bool __parse_nid_path(struct hda_codec *codec,
217 hda_nid_t from_nid, hda_nid_t to_nid,
218 int with_aa_mix, struct nid_path *path, int depth)
219{
220 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100221 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100222 int i, nums;
223
224 if (to_nid == spec->mixer_nid) {
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100225 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100226 return false;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100227 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100228 }
229
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100230 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100231 for (i = 0; i < nums; i++) {
232 if (conn[i] != from_nid) {
233 /* special case: when from_nid is 0,
234 * try to find an empty DAC
235 */
236 if (from_nid ||
237 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
238 is_dac_already_used(codec, conn[i]))
239 continue;
240 }
241 /* aa-mix is requested but not included? */
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100242 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100243 goto found;
244 }
245 if (depth >= MAX_NID_PATH_DEPTH)
246 return false;
247 for (i = 0; i < nums; i++) {
248 unsigned int type;
249 type = get_wcaps_type(get_wcaps(codec, conn[i]));
250 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
251 type == AC_WID_PIN)
252 continue;
253 if (__parse_nid_path(codec, from_nid, conn[i],
254 with_aa_mix, path, depth + 1))
255 goto found;
256 }
257 return false;
258
259 found:
260 path->path[path->depth] = conn[i];
Takashi Iwaif5172a72013-01-04 13:19:55 +0100261 if (conn[i] == spec->mixer_nid)
262 path->with_aa_mix = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100263 path->idx[path->depth + 1] = i;
264 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
265 path->multi[path->depth + 1] = 1;
266 path->depth++;
267 return true;
268}
269
270/* parse the widget path from the given nid to the target nid;
271 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100272 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
273 * excluded, only the paths that don't go through the mixer will be chosen.
274 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
275 * spec->mixer_nid will be chosen.
276 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100277 */
278bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
279 hda_nid_t to_nid, int with_aa_mix,
280 struct nid_path *path)
281{
282 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
283 path->path[path->depth] = to_nid;
284 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100285 return true;
286 }
287 return false;
288}
289EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100292 * parse the path between the given NIDs and add to the path list.
293 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100295struct nid_path *
296snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
297 hda_nid_t to_nid, int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100299 struct hda_gen_spec *spec = codec->spec;
300 struct nid_path *path;
301
302 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
303 return NULL;
304
Takashi Iwaif5172a72013-01-04 13:19:55 +0100305 /* check whether the path has been already added */
306 path = get_nid_path(codec, from_nid, to_nid, with_aa_mix);
307 if (path)
308 return path;
309
Takashi Iwai352f7f92012-12-19 12:52:06 +0100310 path = snd_array_new(&spec->paths);
311 if (!path)
312 return NULL;
313 memset(path, 0, sizeof(*path));
314 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
315 return path;
316 /* push back */
317 spec->paths.used--;
318 return NULL;
319}
320EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
321
322/* look for an empty DAC slot */
323static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
324 bool is_digital)
325{
326 struct hda_gen_spec *spec = codec->spec;
327 bool cap_digital;
328 int i;
329
330 for (i = 0; i < spec->num_all_dacs; i++) {
331 hda_nid_t nid = spec->all_dacs[i];
332 if (!nid || is_dac_already_used(codec, nid))
333 continue;
334 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
335 if (is_digital != cap_digital)
336 continue;
337 if (is_reachable_path(codec, nid, pin))
338 return nid;
339 }
340 return 0;
341}
342
343/* replace the channels in the composed amp value with the given number */
344static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
345{
346 val &= ~(0x3U << 16);
347 val |= chs << 16;
348 return val;
349}
350
351/* check whether the widget has the given amp capability for the direction */
352static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
353 int dir, unsigned int bits)
354{
355 if (!nid)
356 return false;
357 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
358 if (query_amp_caps(codec, nid, dir) & bits)
359 return true;
360 return false;
361}
362
363#define nid_has_mute(codec, nid, dir) \
364 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
365#define nid_has_volume(codec, nid, dir) \
366 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
367
368/* look for a widget suitable for assigning a mute switch in the path */
369static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
370 struct nid_path *path)
371{
372 int i;
373
374 for (i = path->depth - 1; i >= 0; i--) {
375 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
376 return path->path[i];
377 if (i != path->depth - 1 && i != 0 &&
378 nid_has_mute(codec, path->path[i], HDA_INPUT))
379 return path->path[i];
380 }
381 return 0;
382}
383
384/* look for a widget suitable for assigning a volume ctl in the path */
385static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
386 struct nid_path *path)
387{
388 int i;
389
390 for (i = path->depth - 1; i >= 0; i--) {
391 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
392 return path->path[i];
393 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100398 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100400
401/* can have the amp-in capability? */
402static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100404 hda_nid_t nid = path->path[idx];
405 unsigned int caps = get_wcaps(codec, nid);
406 unsigned int type = get_wcaps_type(caps);
407
408 if (!(caps & AC_WCAP_IN_AMP))
409 return false;
410 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
411 return false;
412 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Takashi Iwai352f7f92012-12-19 12:52:06 +0100415/* can have the amp-out capability? */
416static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100418 hda_nid_t nid = path->path[idx];
419 unsigned int caps = get_wcaps(codec, nid);
420 unsigned int type = get_wcaps_type(caps);
421
422 if (!(caps & AC_WCAP_OUT_AMP))
423 return false;
424 if (type == AC_WID_PIN && !idx) /* only for output pins */
425 return false;
426 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Takashi Iwai352f7f92012-12-19 12:52:06 +0100429/* check whether the given (nid,dir,idx) is active */
430static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
431 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100433 struct hda_gen_spec *spec = codec->spec;
434 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Takashi Iwai352f7f92012-12-19 12:52:06 +0100436 for (n = 0; n < spec->paths.used; n++) {
437 struct nid_path *path = snd_array_elem(&spec->paths, n);
438 if (!path->active)
439 continue;
440 for (i = 0; i < path->depth; i++) {
441 if (path->path[i] == nid) {
442 if (dir == HDA_OUTPUT || path->idx[i] == idx)
443 return true;
444 break;
445 }
446 }
447 }
448 return false;
449}
450
451/* get the default amp value for the target state */
452static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
453 int dir, bool enable)
454{
455 unsigned int caps;
456 unsigned int val = 0;
457
458 caps = query_amp_caps(codec, nid, dir);
459 if (caps & AC_AMPCAP_NUM_STEPS) {
460 /* set to 0dB */
461 if (enable)
462 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
463 }
464 if (caps & AC_AMPCAP_MUTE) {
465 if (!enable)
466 val |= HDA_AMP_MUTE;
467 }
468 return val;
469}
470
471/* initialize the amp value (only at the first time) */
472static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
473{
474 int val = get_amp_val_to_activate(codec, nid, dir, false);
475 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
476}
477
478static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
479 int idx, bool enable)
480{
481 int val;
482 if (is_ctl_associated(codec, nid, dir, idx) ||
Takashi Iwai985803c2013-01-03 16:30:04 +0100483 (!enable && is_active_nid(codec, nid, dir, idx)))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100484 return;
485 val = get_amp_val_to_activate(codec, nid, dir, enable);
486 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
487}
488
489static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
490 int i, bool enable)
491{
492 hda_nid_t nid = path->path[i];
493 init_amp(codec, nid, HDA_OUTPUT, 0);
494 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
495}
496
497static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
498 int i, bool enable, bool add_aamix)
499{
500 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100501 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100502 int n, nums, idx;
503 int type;
504 hda_nid_t nid = path->path[i];
505
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100506 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100507 type = get_wcaps_type(get_wcaps(codec, nid));
508 if (type == AC_WID_PIN ||
509 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
510 nums = 1;
511 idx = 0;
512 } else
513 idx = path->idx[i];
514
515 for (n = 0; n < nums; n++)
516 init_amp(codec, nid, HDA_INPUT, n);
517
518 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
519 return;
520
521 /* here is a little bit tricky in comparison with activate_amp_out();
522 * when aa-mixer is available, we need to enable the path as well
523 */
524 for (n = 0; n < nums; n++) {
525 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
526 continue;
527 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529}
530
Takashi Iwai352f7f92012-12-19 12:52:06 +0100531/* activate or deactivate the given path
532 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100534void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
535 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100537 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Takashi Iwai352f7f92012-12-19 12:52:06 +0100539 if (!enable)
540 path->active = false;
541
542 for (i = path->depth - 1; i >= 0; i--) {
543 if (enable && path->multi[i])
544 snd_hda_codec_write_cache(codec, path->path[i], 0,
545 AC_VERB_SET_CONNECT_SEL,
546 path->idx[i]);
547 if (has_amp_in(codec, path, i))
548 activate_amp_in(codec, path, i, enable, add_aamix);
549 if (has_amp_out(codec, path, i))
550 activate_amp_out(codec, path, i, enable);
551 }
552
553 if (enable)
554 path->active = true;
555}
556EXPORT_SYMBOL_HDA(snd_hda_activate_path);
557
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100558/* turn on/off EAPD on the given pin */
559static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
560{
561 struct hda_gen_spec *spec = codec->spec;
562 if (spec->own_eapd_ctl ||
563 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
564 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100565 if (codec->inv_eapd)
566 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100567 snd_hda_codec_update_cache(codec, pin, 0,
568 AC_VERB_SET_EAPD_BTLENABLE,
569 enable ? 0x02 : 0x00);
570}
571
Takashi Iwai352f7f92012-12-19 12:52:06 +0100572
573/*
574 * Helper functions for creating mixer ctl elements
575 */
576
577enum {
578 HDA_CTL_WIDGET_VOL,
579 HDA_CTL_WIDGET_MUTE,
580 HDA_CTL_BIND_MUTE,
581 HDA_CTL_BIND_VOL,
582 HDA_CTL_BIND_SW,
583};
584static const struct snd_kcontrol_new control_templates[] = {
585 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
586 HDA_CODEC_MUTE(NULL, 0, 0, 0),
587 HDA_BIND_MUTE(NULL, 0, 0, 0),
588 HDA_BIND_VOL(NULL, 0),
589 HDA_BIND_SW(NULL, 0),
590};
591
592/* add dynamic controls from template */
593static int add_control(struct hda_gen_spec *spec, int type, const char *name,
594 int cidx, unsigned long val)
595{
596 struct snd_kcontrol_new *knew;
597
Takashi Iwai12c93df2012-12-19 14:38:33 +0100598 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100599 if (!knew)
600 return -ENOMEM;
601 knew->index = cidx;
602 if (get_amp_nid_(val))
603 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
604 knew->private_value = val;
605 return 0;
606}
607
608static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
609 const char *pfx, const char *dir,
610 const char *sfx, int cidx, unsigned long val)
611{
612 char name[32];
613 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
614 return add_control(spec, type, name, cidx, val);
615}
616
617#define add_pb_vol_ctrl(spec, type, pfx, val) \
618 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
619#define add_pb_sw_ctrl(spec, type, pfx, val) \
620 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
621#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
622 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
623#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
624 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
625
626static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
627 unsigned int chs, struct nid_path *path)
628{
629 unsigned int val;
630 if (!path)
631 return 0;
632 val = path->ctls[NID_PATH_VOL_CTL];
633 if (!val)
634 return 0;
635 val = amp_val_replace_channels(val, chs);
636 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
637}
638
639/* return the channel bits suitable for the given path->ctls[] */
640static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
641 int type)
642{
643 int chs = 1; /* mono (left only) */
644 if (path) {
645 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
646 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
647 chs = 3; /* stereo */
648 }
649 return chs;
650}
651
652static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
653 struct nid_path *path)
654{
655 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
656 return add_vol_ctl(codec, pfx, cidx, chs, path);
657}
658
659/* create a mute-switch for the given mixer widget;
660 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
661 */
662static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
663 unsigned int chs, struct nid_path *path)
664{
665 unsigned int val;
666 int type = HDA_CTL_WIDGET_MUTE;
667
668 if (!path)
669 return 0;
670 val = path->ctls[NID_PATH_MUTE_CTL];
671 if (!val)
672 return 0;
673 val = amp_val_replace_channels(val, chs);
674 if (get_amp_direction_(val) == HDA_INPUT) {
675 hda_nid_t nid = get_amp_nid_(val);
676 int nums = snd_hda_get_num_conns(codec, nid);
677 if (nums > 1) {
678 type = HDA_CTL_BIND_MUTE;
679 val |= nums << 19;
680 }
681 }
682 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
683}
684
685static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
686 int cidx, struct nid_path *path)
687{
688 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
689 return add_sw_ctl(codec, pfx, cidx, chs, path);
690}
691
692static const char * const channel_name[4] = {
693 "Front", "Surround", "CLFE", "Side"
694};
695
696/* give some appropriate ctl name prefix for the given line out channel */
697static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
698 bool can_be_master, int *index)
699{
700 struct auto_pin_cfg *cfg = &spec->autocfg;
701
702 *index = 0;
703 if (cfg->line_outs == 1 && !spec->multi_ios &&
704 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
705 return spec->vmaster_mute.hook ? "PCM" : "Master";
706
707 /* if there is really a single DAC used in the whole output paths,
708 * use it master (or "PCM" if a vmaster hook is present)
709 */
710 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
711 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
712 return spec->vmaster_mute.hook ? "PCM" : "Master";
713
714 switch (cfg->line_out_type) {
715 case AUTO_PIN_SPEAKER_OUT:
716 if (cfg->line_outs == 1)
717 return "Speaker";
718 if (cfg->line_outs == 2)
719 return ch ? "Bass Speaker" : "Speaker";
720 break;
721 case AUTO_PIN_HP_OUT:
722 /* for multi-io case, only the primary out */
723 if (ch && spec->multi_ios)
724 break;
725 *index = ch;
726 return "Headphone";
727 default:
728 if (cfg->line_outs == 1 && !spec->multi_ios)
729 return "PCM";
730 break;
731 }
732 if (ch >= ARRAY_SIZE(channel_name)) {
733 snd_BUG();
734 return "PCM";
735 }
736
737 return channel_name[ch];
738}
739
740/*
741 * Parse output paths
742 */
743
744/* badness definition */
745enum {
746 /* No primary DAC is found for the main output */
747 BAD_NO_PRIMARY_DAC = 0x10000,
748 /* No DAC is found for the extra output */
749 BAD_NO_DAC = 0x4000,
750 /* No possible multi-ios */
751 BAD_MULTI_IO = 0x103,
752 /* No individual DAC for extra output */
753 BAD_NO_EXTRA_DAC = 0x102,
754 /* No individual DAC for extra surrounds */
755 BAD_NO_EXTRA_SURR_DAC = 0x101,
756 /* Primary DAC shared with main surrounds */
757 BAD_SHARED_SURROUND = 0x100,
758 /* Primary DAC shared with main CLFE */
759 BAD_SHARED_CLFE = 0x10,
760 /* Primary DAC shared with extra surrounds */
761 BAD_SHARED_EXTRA_SURROUND = 0x10,
762 /* Volume widget is shared */
763 BAD_SHARED_VOL = 0x10,
764};
765
766/* look for widgets in the path between the given NIDs appropriate for
767 * volume and mute controls, and assign the values to ctls[].
768 *
769 * When no appropriate widget is found in the path, the badness value
770 * is incremented depending on the situation. The function returns the
771 * total badness for both volume and mute controls.
772 */
773static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
774 hda_nid_t dac)
775{
776 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
777 hda_nid_t nid;
778 unsigned int val;
779 int badness = 0;
780
781 if (!path)
782 return BAD_SHARED_VOL * 2;
783 nid = look_for_out_vol_nid(codec, path);
784 if (nid) {
785 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
786 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
787 badness += BAD_SHARED_VOL;
788 else
789 path->ctls[NID_PATH_VOL_CTL] = val;
790 } else
791 badness += BAD_SHARED_VOL;
792 nid = look_for_out_mute_nid(codec, path);
793 if (nid) {
794 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
795 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
796 nid_has_mute(codec, nid, HDA_OUTPUT))
797 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
798 else
799 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
800 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
801 badness += BAD_SHARED_VOL;
802 else
803 path->ctls[NID_PATH_MUTE_CTL] = val;
804 } else
805 badness += BAD_SHARED_VOL;
806 return badness;
807}
808
809struct badness_table {
810 int no_primary_dac; /* no primary DAC */
811 int no_dac; /* no secondary DACs */
812 int shared_primary; /* primary DAC is shared with main output */
813 int shared_surr; /* secondary DAC shared with main or primary */
814 int shared_clfe; /* third DAC shared with main or primary */
815 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
816};
817
818static struct badness_table main_out_badness = {
819 .no_primary_dac = BAD_NO_PRIMARY_DAC,
820 .no_dac = BAD_NO_DAC,
821 .shared_primary = BAD_NO_PRIMARY_DAC,
822 .shared_surr = BAD_SHARED_SURROUND,
823 .shared_clfe = BAD_SHARED_CLFE,
824 .shared_surr_main = BAD_SHARED_SURROUND,
825};
826
827static struct badness_table extra_out_badness = {
828 .no_primary_dac = BAD_NO_DAC,
829 .no_dac = BAD_NO_DAC,
830 .shared_primary = BAD_NO_EXTRA_DAC,
831 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
832 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
833 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
834};
835
836/* try to assign DACs to pins and return the resultant badness */
837static int try_assign_dacs(struct hda_codec *codec, int num_outs,
838 const hda_nid_t *pins, hda_nid_t *dacs,
839 const struct badness_table *bad)
840{
841 struct hda_gen_spec *spec = codec->spec;
842 struct auto_pin_cfg *cfg = &spec->autocfg;
843 int i, j;
844 int badness = 0;
845 hda_nid_t dac;
846
847 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return 0;
849
Takashi Iwai352f7f92012-12-19 12:52:06 +0100850 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100851 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100852 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +0100853
854 if (dacs[i]) {
855 badness += assign_out_path_ctls(codec, pin, dacs[i]);
856 continue;
857 }
858
859 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100860 if (!dacs[i] && !i) {
861 for (j = 1; j < num_outs; j++) {
862 if (is_reachable_path(codec, dacs[j], pin)) {
863 dacs[0] = dacs[j];
864 dacs[j] = 0;
865 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100868 }
869 dac = dacs[i];
870 if (!dac) {
871 if (is_reachable_path(codec, dacs[0], pin))
872 dac = dacs[0];
873 else if (cfg->line_outs > i &&
874 is_reachable_path(codec, spec->private_dac_nids[i], pin))
875 dac = spec->private_dac_nids[i];
876 if (dac) {
877 if (!i)
878 badness += bad->shared_primary;
879 else if (i == 1)
880 badness += bad->shared_surr;
881 else
882 badness += bad->shared_clfe;
883 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
884 dac = spec->private_dac_nids[0];
885 badness += bad->shared_surr_main;
886 } else if (!i)
887 badness += bad->no_primary_dac;
888 else
889 badness += bad->no_dac;
890 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100891 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
Takashi Iwaib3a8c742012-12-20 18:29:16 +0100892 if (!path && i > 0 && spec->mixer_nid) {
893 /* try with aamix */
894 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
895 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100896 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100897 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +0100898 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100899 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100900 path->active = true;
901 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100902 if (dac)
903 badness += assign_out_path_ctls(codec, pin, dac);
904 }
905
906 return badness;
907}
908
909/* return NID if the given pin has only a single connection to a certain DAC */
910static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
911{
912 struct hda_gen_spec *spec = codec->spec;
913 int i;
914 hda_nid_t nid_found = 0;
915
916 for (i = 0; i < spec->num_all_dacs; i++) {
917 hda_nid_t nid = spec->all_dacs[i];
918 if (!nid || is_dac_already_used(codec, nid))
919 continue;
920 if (is_reachable_path(codec, nid, pin)) {
921 if (nid_found)
922 return 0;
923 nid_found = nid;
924 }
925 }
926 return nid_found;
927}
928
929/* check whether the given pin can be a multi-io pin */
930static bool can_be_multiio_pin(struct hda_codec *codec,
931 unsigned int location, hda_nid_t nid)
932{
933 unsigned int defcfg, caps;
934
935 defcfg = snd_hda_codec_get_pincfg(codec, nid);
936 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
937 return false;
938 if (location && get_defcfg_location(defcfg) != location)
939 return false;
940 caps = snd_hda_query_pin_caps(codec, nid);
941 if (!(caps & AC_PINCAP_OUT))
942 return false;
943 return true;
944}
945
946/*
947 * multi-io helper
948 *
949 * When hardwired is set, try to fill ony hardwired pins, and returns
950 * zero if any pins are filled, non-zero if nothing found.
951 * When hardwired is off, try to fill possible input pins, and returns
952 * the badness value.
953 */
954static int fill_multi_ios(struct hda_codec *codec,
955 hda_nid_t reference_pin,
956 bool hardwired, int offset)
957{
958 struct hda_gen_spec *spec = codec->spec;
959 struct auto_pin_cfg *cfg = &spec->autocfg;
960 int type, i, j, dacs, num_pins, old_pins;
961 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
962 unsigned int location = get_defcfg_location(defcfg);
963 int badness = 0;
964
965 old_pins = spec->multi_ios;
966 if (old_pins >= 2)
967 goto end_fill;
968
969 num_pins = 0;
970 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
971 for (i = 0; i < cfg->num_inputs; i++) {
972 if (cfg->inputs[i].type != type)
973 continue;
974 if (can_be_multiio_pin(codec, location,
975 cfg->inputs[i].pin))
976 num_pins++;
977 }
978 }
979 if (num_pins < 2)
980 goto end_fill;
981
982 dacs = spec->multiout.num_dacs;
983 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
984 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100985 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100986 hda_nid_t nid = cfg->inputs[i].pin;
987 hda_nid_t dac = 0;
988
989 if (cfg->inputs[i].type != type)
990 continue;
991 if (!can_be_multiio_pin(codec, location, nid))
992 continue;
993 for (j = 0; j < spec->multi_ios; j++) {
994 if (nid == spec->multi_io[j].pin)
995 break;
996 }
997 if (j < spec->multi_ios)
998 continue;
999
1000 if (offset && offset + spec->multi_ios < dacs) {
1001 dac = spec->private_dac_nids[offset + spec->multi_ios];
1002 if (!is_reachable_path(codec, dac, nid))
1003 dac = 0;
1004 }
1005 if (hardwired)
1006 dac = get_dac_if_single(codec, nid);
1007 else if (!dac)
1008 dac = look_for_dac(codec, nid, false);
1009 if (!dac) {
1010 badness++;
1011 continue;
1012 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001013 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001014 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001015 badness++;
1016 continue;
1017 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001018 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001019 spec->multi_io[spec->multi_ios].pin = nid;
1020 spec->multi_io[spec->multi_ios].dac = dac;
1021 spec->multi_ios++;
1022 if (spec->multi_ios >= 2)
1023 break;
1024 }
1025 }
1026 end_fill:
1027 if (badness)
1028 badness = BAD_MULTI_IO;
1029 if (old_pins == spec->multi_ios) {
1030 if (hardwired)
1031 return 1; /* nothing found */
1032 else
1033 return badness; /* no badness if nothing found */
1034 }
1035 if (!hardwired && spec->multi_ios < 2) {
1036 /* cancel newly assigned paths */
1037 spec->paths.used -= spec->multi_ios - old_pins;
1038 spec->multi_ios = old_pins;
1039 return badness;
1040 }
1041
1042 /* assign volume and mute controls */
1043 for (i = old_pins; i < spec->multi_ios; i++)
1044 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1045 spec->multi_io[i].dac);
1046
1047 return badness;
1048}
1049
1050/* map DACs for all pins in the list if they are single connections */
1051static bool map_singles(struct hda_codec *codec, int outs,
1052 const hda_nid_t *pins, hda_nid_t *dacs)
1053{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001054 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001055 int i;
1056 bool found = false;
1057 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001058 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001059 hda_nid_t dac;
1060 if (dacs[i])
1061 continue;
1062 dac = get_dac_if_single(codec, pins[i]);
1063 if (!dac)
1064 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001065 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001066 if (!path && i > 0 && spec->mixer_nid)
1067 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001068 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001069 dacs[i] = dac;
1070 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001071 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001072 path->active = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001073 }
1074 }
1075 return found;
1076}
1077
1078/* fill in the dac_nids table from the parsed pin configuration */
1079static int fill_and_eval_dacs(struct hda_codec *codec,
1080 bool fill_hardwired,
1081 bool fill_mio_first)
1082{
1083 struct hda_gen_spec *spec = codec->spec;
1084 struct auto_pin_cfg *cfg = &spec->autocfg;
1085 int i, err, badness;
1086
1087 /* set num_dacs once to full for look_for_dac() */
1088 spec->multiout.num_dacs = cfg->line_outs;
1089 spec->multiout.dac_nids = spec->private_dac_nids;
1090 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1091 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1092 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1093 spec->multi_ios = 0;
1094 snd_array_free(&spec->paths);
1095 badness = 0;
1096
1097 /* fill hard-wired DACs first */
1098 if (fill_hardwired) {
1099 bool mapped;
1100 do {
1101 mapped = map_singles(codec, cfg->line_outs,
1102 cfg->line_out_pins,
1103 spec->private_dac_nids);
1104 mapped |= map_singles(codec, cfg->hp_outs,
1105 cfg->hp_pins,
1106 spec->multiout.hp_out_nid);
1107 mapped |= map_singles(codec, cfg->speaker_outs,
1108 cfg->speaker_pins,
1109 spec->multiout.extra_out_nid);
1110 if (fill_mio_first && cfg->line_outs == 1 &&
1111 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1112 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1113 if (!err)
1114 mapped = true;
1115 }
1116 } while (mapped);
1117 }
1118
1119 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1120 spec->private_dac_nids,
1121 &main_out_badness);
1122
1123 /* re-count num_dacs and squash invalid entries */
1124 spec->multiout.num_dacs = 0;
1125 for (i = 0; i < cfg->line_outs; i++) {
1126 if (spec->private_dac_nids[i])
1127 spec->multiout.num_dacs++;
1128 else {
1129 memmove(spec->private_dac_nids + i,
1130 spec->private_dac_nids + i + 1,
1131 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1132 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1133 }
1134 }
1135
1136 if (fill_mio_first &&
1137 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1138 /* try to fill multi-io first */
1139 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1140 if (err < 0)
1141 return err;
1142 /* we don't count badness at this stage yet */
1143 }
1144
1145 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1146 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1147 spec->multiout.hp_out_nid,
1148 &extra_out_badness);
1149 if (err < 0)
1150 return err;
1151 badness += err;
1152 }
1153 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1154 err = try_assign_dacs(codec, cfg->speaker_outs,
1155 cfg->speaker_pins,
1156 spec->multiout.extra_out_nid,
1157 &extra_out_badness);
1158 if (err < 0)
1159 return err;
1160 badness += err;
1161 }
1162 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1163 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1164 if (err < 0)
1165 return err;
1166 badness += err;
1167 }
1168 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1169 /* try multi-ios with HP + inputs */
1170 int offset = 0;
1171 if (cfg->line_outs >= 3)
1172 offset = 1;
1173 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1174 if (err < 0)
1175 return err;
1176 badness += err;
1177 }
1178
1179 if (spec->multi_ios == 2) {
1180 for (i = 0; i < 2; i++)
1181 spec->private_dac_nids[spec->multiout.num_dacs++] =
1182 spec->multi_io[i].dac;
1183 spec->ext_channel_count = 2;
1184 } else if (spec->multi_ios) {
1185 spec->multi_ios = 0;
1186 badness += BAD_MULTI_IO;
1187 }
1188
1189 return badness;
1190}
1191
1192#define DEBUG_BADNESS
1193
1194#ifdef DEBUG_BADNESS
1195#define debug_badness snd_printdd
1196#else
1197#define debug_badness(...)
1198#endif
1199
1200static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1201{
1202 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1203 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001204 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001205 spec->multiout.dac_nids[0],
1206 spec->multiout.dac_nids[1],
1207 spec->multiout.dac_nids[2],
1208 spec->multiout.dac_nids[3]);
1209 if (spec->multi_ios > 0)
1210 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1211 spec->multi_ios,
1212 spec->multi_io[0].pin, spec->multi_io[1].pin,
1213 spec->multi_io[0].dac, spec->multi_io[1].dac);
1214 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1215 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001216 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001217 spec->multiout.hp_out_nid[0],
1218 spec->multiout.hp_out_nid[1],
1219 spec->multiout.hp_out_nid[2],
1220 spec->multiout.hp_out_nid[3]);
1221 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1222 cfg->speaker_pins[0], cfg->speaker_pins[1],
1223 cfg->speaker_pins[2], cfg->speaker_pins[3],
1224 spec->multiout.extra_out_nid[0],
1225 spec->multiout.extra_out_nid[1],
1226 spec->multiout.extra_out_nid[2],
1227 spec->multiout.extra_out_nid[3]);
1228}
1229
1230/* find all available DACs of the codec */
1231static void fill_all_dac_nids(struct hda_codec *codec)
1232{
1233 struct hda_gen_spec *spec = codec->spec;
1234 int i;
1235 hda_nid_t nid = codec->start_nid;
1236
1237 spec->num_all_dacs = 0;
1238 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1239 for (i = 0; i < codec->num_nodes; i++, nid++) {
1240 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1241 continue;
1242 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1243 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1244 break;
1245 }
1246 spec->all_dacs[spec->num_all_dacs++] = nid;
1247 }
1248}
1249
1250static int parse_output_paths(struct hda_codec *codec)
1251{
1252 struct hda_gen_spec *spec = codec->spec;
1253 struct auto_pin_cfg *cfg = &spec->autocfg;
1254 struct auto_pin_cfg *best_cfg;
1255 int best_badness = INT_MAX;
1256 int badness;
1257 bool fill_hardwired = true, fill_mio_first = true;
1258 bool best_wired = true, best_mio = true;
1259 bool hp_spk_swapped = false;
1260
1261 fill_all_dac_nids(codec);
1262
1263 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1264 if (!best_cfg)
1265 return -ENOMEM;
1266 *best_cfg = *cfg;
1267
1268 for (;;) {
1269 badness = fill_and_eval_dacs(codec, fill_hardwired,
1270 fill_mio_first);
1271 if (badness < 0) {
1272 kfree(best_cfg);
1273 return badness;
1274 }
1275 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1276 cfg->line_out_type, fill_hardwired, fill_mio_first,
1277 badness);
1278 debug_show_configs(spec, cfg);
1279 if (badness < best_badness) {
1280 best_badness = badness;
1281 *best_cfg = *cfg;
1282 best_wired = fill_hardwired;
1283 best_mio = fill_mio_first;
1284 }
1285 if (!badness)
1286 break;
1287 fill_mio_first = !fill_mio_first;
1288 if (!fill_mio_first)
1289 continue;
1290 fill_hardwired = !fill_hardwired;
1291 if (!fill_hardwired)
1292 continue;
1293 if (hp_spk_swapped)
1294 break;
1295 hp_spk_swapped = true;
1296 if (cfg->speaker_outs > 0 &&
1297 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1298 cfg->hp_outs = cfg->line_outs;
1299 memcpy(cfg->hp_pins, cfg->line_out_pins,
1300 sizeof(cfg->hp_pins));
1301 cfg->line_outs = cfg->speaker_outs;
1302 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1303 sizeof(cfg->speaker_pins));
1304 cfg->speaker_outs = 0;
1305 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1306 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1307 fill_hardwired = true;
1308 continue;
1309 }
1310 if (cfg->hp_outs > 0 &&
1311 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1312 cfg->speaker_outs = cfg->line_outs;
1313 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1314 sizeof(cfg->speaker_pins));
1315 cfg->line_outs = cfg->hp_outs;
1316 memcpy(cfg->line_out_pins, cfg->hp_pins,
1317 sizeof(cfg->hp_pins));
1318 cfg->hp_outs = 0;
1319 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1320 cfg->line_out_type = AUTO_PIN_HP_OUT;
1321 fill_hardwired = true;
1322 continue;
1323 }
1324 break;
1325 }
1326
1327 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001328 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001329 *cfg = *best_cfg;
1330 fill_and_eval_dacs(codec, best_wired, best_mio);
1331 }
1332 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1333 cfg->line_out_type, best_wired, best_mio);
1334 debug_show_configs(spec, cfg);
1335
1336 if (cfg->line_out_pins[0]) {
1337 struct nid_path *path;
1338 path = snd_hda_get_nid_path(codec,
1339 spec->multiout.dac_nids[0],
1340 cfg->line_out_pins[0]);
1341 if (path)
1342 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1343 }
1344
1345 kfree(best_cfg);
1346 return 0;
1347}
1348
1349/* add playback controls from the parsed DAC table */
1350static int create_multi_out_ctls(struct hda_codec *codec,
1351 const struct auto_pin_cfg *cfg)
1352{
1353 struct hda_gen_spec *spec = codec->spec;
1354 int i, err, noutputs;
1355
1356 noutputs = cfg->line_outs;
1357 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1358 noutputs += spec->multi_ios;
1359
1360 for (i = 0; i < noutputs; i++) {
1361 const char *name;
1362 int index;
1363 hda_nid_t dac, pin;
1364 struct nid_path *path;
1365
1366 dac = spec->multiout.dac_nids[i];
1367 if (!dac)
1368 continue;
1369 if (i >= cfg->line_outs) {
1370 pin = spec->multi_io[i - 1].pin;
1371 index = 0;
1372 name = channel_name[i];
1373 } else {
1374 pin = cfg->line_out_pins[i];
1375 name = get_line_out_pfx(spec, i, true, &index);
1376 }
1377
1378 path = snd_hda_get_nid_path(codec, dac, pin);
1379 if (!path)
1380 continue;
1381 if (!name || !strcmp(name, "CLFE")) {
1382 /* Center/LFE */
1383 err = add_vol_ctl(codec, "Center", 0, 1, path);
1384 if (err < 0)
1385 return err;
1386 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1387 if (err < 0)
1388 return err;
1389 err = add_sw_ctl(codec, "Center", 0, 1, path);
1390 if (err < 0)
1391 return err;
1392 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1393 if (err < 0)
1394 return err;
1395 } else {
1396 err = add_stereo_vol(codec, name, index, path);
1397 if (err < 0)
1398 return err;
1399 err = add_stereo_sw(codec, name, index, path);
1400 if (err < 0)
1401 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 }
1404 return 0;
1405}
1406
Takashi Iwai352f7f92012-12-19 12:52:06 +01001407static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1408 hda_nid_t dac, const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001410 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 int err;
1412
Takashi Iwai352f7f92012-12-19 12:52:06 +01001413 path = snd_hda_get_nid_path(codec, dac, pin);
1414 if (!path)
1415 return 0;
1416 /* bind volume control will be created in the case of dac = 0 */
1417 if (dac) {
1418 err = add_stereo_vol(codec, pfx, cidx, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (err < 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001420 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001422 err = add_stereo_sw(codec, pfx, cidx, path);
1423 if (err < 0)
1424 return err;
1425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Takashi Iwai352f7f92012-12-19 12:52:06 +01001428/* add playback controls for speaker and HP outputs */
1429static int create_extra_outs(struct hda_codec *codec, int num_pins,
1430 const hda_nid_t *pins, const hda_nid_t *dacs,
1431 const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001433 struct hda_gen_spec *spec = codec->spec;
1434 struct hda_bind_ctls *ctl;
1435 char name[32];
1436 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Takashi Iwai352f7f92012-12-19 12:52:06 +01001438 if (!num_pins || !pins[0])
1439 return 0;
1440
1441 if (num_pins == 1) {
1442 hda_nid_t dac = *dacs;
1443 if (!dac)
1444 dac = spec->multiout.dac_nids[0];
1445 return create_extra_out(codec, *pins, dac, pfx, 0);
Takashi Iwai97ec5582006-03-21 11:29:07 +01001446 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001447
1448 for (i = 0; i < num_pins; i++) {
1449 hda_nid_t dac;
1450 if (dacs[num_pins - 1])
1451 dac = dacs[i]; /* with individual volumes */
Takashi Iwai97ec5582006-03-21 11:29:07 +01001452 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001453 dac = 0;
1454 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1455 err = create_extra_out(codec, pins[i], dac,
1456 "Bass Speaker", 0);
1457 } else if (num_pins >= 3) {
1458 snprintf(name, sizeof(name), "%s %s",
1459 pfx, channel_name[i]);
1460 err = create_extra_out(codec, pins[i], dac, name, 0);
1461 } else {
1462 err = create_extra_out(codec, pins[i], dac, pfx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001464 if (err < 0)
1465 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001467 if (dacs[num_pins - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return 0;
1469
Takashi Iwai352f7f92012-12-19 12:52:06 +01001470 /* Let's create a bind-controls for volumes */
1471 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1472 if (!ctl)
1473 return -ENOMEM;
1474 n = 0;
1475 for (i = 0; i < num_pins; i++) {
1476 hda_nid_t vol;
1477 struct nid_path *path;
1478 if (!pins[i] || !dacs[i])
1479 continue;
1480 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1481 if (!path)
1482 continue;
1483 vol = look_for_out_vol_nid(codec, path);
1484 if (vol)
1485 ctl->values[n++] =
1486 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1487 }
1488 if (n) {
1489 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1490 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1491 if (err < 0)
1492 return err;
1493 }
1494 return 0;
1495}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001496
Takashi Iwai352f7f92012-12-19 12:52:06 +01001497static int create_hp_out_ctls(struct hda_codec *codec)
1498{
1499 struct hda_gen_spec *spec = codec->spec;
1500 return create_extra_outs(codec, spec->autocfg.hp_outs,
1501 spec->autocfg.hp_pins,
1502 spec->multiout.hp_out_nid,
1503 "Headphone");
1504}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Takashi Iwai352f7f92012-12-19 12:52:06 +01001506static int create_speaker_out_ctls(struct hda_codec *codec)
1507{
1508 struct hda_gen_spec *spec = codec->spec;
1509 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1510 spec->autocfg.speaker_pins,
1511 spec->multiout.extra_out_nid,
1512 "Speaker");
1513}
1514
1515/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001516 * independent HP controls
1517 */
1518
1519static int indep_hp_info(struct snd_kcontrol *kcontrol,
1520 struct snd_ctl_elem_info *uinfo)
1521{
1522 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1523}
1524
1525static int indep_hp_get(struct snd_kcontrol *kcontrol,
1526 struct snd_ctl_elem_value *ucontrol)
1527{
1528 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1529 struct hda_gen_spec *spec = codec->spec;
1530 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1531 return 0;
1532}
1533
1534static int indep_hp_put(struct snd_kcontrol *kcontrol,
1535 struct snd_ctl_elem_value *ucontrol)
1536{
1537 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1538 struct hda_gen_spec *spec = codec->spec;
1539 unsigned int select = ucontrol->value.enumerated.item[0];
1540 int ret = 0;
1541
1542 mutex_lock(&spec->pcm_mutex);
1543 if (spec->active_streams) {
1544 ret = -EBUSY;
1545 goto unlock;
1546 }
1547
1548 if (spec->indep_hp_enabled != select) {
1549 spec->indep_hp_enabled = select;
1550 if (spec->indep_hp_enabled)
1551 spec->multiout.hp_out_nid[0] = 0;
1552 else
1553 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1554 ret = 1;
1555 }
1556 unlock:
1557 mutex_unlock(&spec->pcm_mutex);
1558 return ret;
1559}
1560
1561static const struct snd_kcontrol_new indep_hp_ctl = {
1562 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1563 .name = "Independent HP",
1564 .info = indep_hp_info,
1565 .get = indep_hp_get,
1566 .put = indep_hp_put,
1567};
1568
1569
1570static int create_indep_hp_ctls(struct hda_codec *codec)
1571{
1572 struct hda_gen_spec *spec = codec->spec;
1573
1574 if (!spec->indep_hp)
1575 return 0;
1576 if (!spec->multiout.hp_out_nid[0]) {
1577 spec->indep_hp = 0;
1578 return 0;
1579 }
1580
1581 spec->indep_hp_enabled = false;
1582 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1583 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1584 return -ENOMEM;
1585 return 0;
1586}
1587
1588/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001589 * channel mode enum control
1590 */
1591
1592static int ch_mode_info(struct snd_kcontrol *kcontrol,
1593 struct snd_ctl_elem_info *uinfo)
1594{
1595 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1596 struct hda_gen_spec *spec = codec->spec;
1597
1598 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1599 uinfo->count = 1;
1600 uinfo->value.enumerated.items = spec->multi_ios + 1;
1601 if (uinfo->value.enumerated.item > spec->multi_ios)
1602 uinfo->value.enumerated.item = spec->multi_ios;
1603 sprintf(uinfo->value.enumerated.name, "%dch",
1604 (uinfo->value.enumerated.item + 1) * 2);
1605 return 0;
1606}
1607
1608static int ch_mode_get(struct snd_kcontrol *kcontrol,
1609 struct snd_ctl_elem_value *ucontrol)
1610{
1611 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1612 struct hda_gen_spec *spec = codec->spec;
1613 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1614 return 0;
1615}
1616
1617static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1618{
1619 struct hda_gen_spec *spec = codec->spec;
1620 hda_nid_t nid = spec->multi_io[idx].pin;
1621 struct nid_path *path;
1622
1623 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1624 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001626
1627 if (path->active == output)
1628 return 0;
1629
1630 if (output) {
1631 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1632 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001633 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001634 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001635 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001636 snd_hda_activate_path(codec, path, false, true);
1637 snd_hda_set_pin_ctl_cache(codec, nid,
1638 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001640 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
Takashi Iwai352f7f92012-12-19 12:52:06 +01001643static int ch_mode_put(struct snd_kcontrol *kcontrol,
1644 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001646 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1647 struct hda_gen_spec *spec = codec->spec;
1648 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Takashi Iwai352f7f92012-12-19 12:52:06 +01001650 ch = ucontrol->value.enumerated.item[0];
1651 if (ch < 0 || ch > spec->multi_ios)
1652 return -EINVAL;
1653 if (ch == (spec->ext_channel_count - 1) / 2)
1654 return 0;
1655 spec->ext_channel_count = (ch + 1) * 2;
1656 for (i = 0; i < spec->multi_ios; i++)
1657 set_multi_io(codec, i, i < ch);
1658 spec->multiout.max_channels = max(spec->ext_channel_count,
1659 spec->const_channel_count);
1660 if (spec->need_dac_fix)
1661 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return 1;
1663}
1664
Takashi Iwai352f7f92012-12-19 12:52:06 +01001665static const struct snd_kcontrol_new channel_mode_enum = {
1666 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1667 .name = "Channel Mode",
1668 .info = ch_mode_info,
1669 .get = ch_mode_get,
1670 .put = ch_mode_put,
1671};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Takashi Iwai352f7f92012-12-19 12:52:06 +01001673static int create_multi_channel_mode(struct hda_codec *codec)
1674{
1675 struct hda_gen_spec *spec = codec->spec;
1676
1677 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001678 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001679 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return 0;
1682}
1683
Takashi Iwai352f7f92012-12-19 12:52:06 +01001684/*
1685 * shared headphone/mic handling
1686 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001687
Takashi Iwai352f7f92012-12-19 12:52:06 +01001688static void call_update_outputs(struct hda_codec *codec);
1689
1690/* for shared I/O, change the pin-control accordingly */
1691static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1692{
1693 struct hda_gen_spec *spec = codec->spec;
1694 unsigned int val;
1695 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1696 /* NOTE: this assumes that there are only two inputs, the
1697 * first is the real internal mic and the second is HP/mic jack.
1698 */
1699
1700 val = snd_hda_get_default_vref(codec, pin);
1701
1702 /* This pin does not have vref caps - let's enable vref on pin 0x18
1703 instead, as suggested by Realtek */
1704 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1705 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1706 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1707 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001708 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1709 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001710 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001711
1712 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001713 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001714
1715 spec->automute_speaker = !set_as_mic;
1716 call_update_outputs(codec);
1717}
1718
1719/* create a shared input with the headphone out */
1720static int create_shared_input(struct hda_codec *codec)
1721{
1722 struct hda_gen_spec *spec = codec->spec;
1723 struct auto_pin_cfg *cfg = &spec->autocfg;
1724 unsigned int defcfg;
1725 hda_nid_t nid;
1726
1727 /* only one internal input pin? */
1728 if (cfg->num_inputs != 1)
1729 return 0;
1730 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1731 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1732 return 0;
1733
1734 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1735 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1736 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1737 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1738 else
1739 return 0; /* both not available */
1740
1741 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1742 return 0; /* no input */
1743
1744 cfg->inputs[1].pin = nid;
1745 cfg->inputs[1].type = AUTO_PIN_MIC;
1746 cfg->num_inputs = 2;
1747 spec->shared_mic_hp = 1;
1748 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1749 return 0;
1750}
1751
1752
1753/*
1754 * Parse input paths
1755 */
1756
1757#ifdef CONFIG_PM
1758/* add the powersave loopback-list entry */
1759static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1760{
1761 struct hda_amp_list *list;
1762
1763 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1764 return;
1765 list = spec->loopback_list + spec->num_loopbacks;
1766 list->nid = mix;
1767 list->dir = HDA_INPUT;
1768 list->idx = idx;
1769 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001770 spec->loopback.amplist = spec->loopback_list;
1771}
1772#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001773#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001774#endif
1775
Takashi Iwai352f7f92012-12-19 12:52:06 +01001776/* create input playback/capture controls for the given pin */
1777static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1778 const char *ctlname, int ctlidx,
1779 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001781 struct hda_gen_spec *spec = codec->spec;
1782 struct nid_path *path;
1783 unsigned int val;
1784 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Takashi Iwai352f7f92012-12-19 12:52:06 +01001786 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1787 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1788 return 0; /* no need for analog loopback */
1789
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001790 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001791 if (!path)
1792 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001793 print_nid_path("loopback", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001794
1795 idx = path->idx[path->depth - 1];
1796 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1797 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1798 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001799 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001801 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
1803
Takashi Iwai352f7f92012-12-19 12:52:06 +01001804 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1805 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1806 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001807 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001809 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811
Takashi Iwai352f7f92012-12-19 12:52:06 +01001812 path->active = true;
1813 add_loopback_list(spec, mix_nid, idx);
1814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
Takashi Iwai352f7f92012-12-19 12:52:06 +01001817static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001819 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1820 return (pincap & AC_PINCAP_IN) != 0;
1821}
1822
1823/* Parse the codec tree and retrieve ADCs */
1824static int fill_adc_nids(struct hda_codec *codec)
1825{
1826 struct hda_gen_spec *spec = codec->spec;
1827 hda_nid_t nid;
1828 hda_nid_t *adc_nids = spec->adc_nids;
1829 int max_nums = ARRAY_SIZE(spec->adc_nids);
1830 int i, nums = 0;
1831
1832 nid = codec->start_nid;
1833 for (i = 0; i < codec->num_nodes; i++, nid++) {
1834 unsigned int caps = get_wcaps(codec, nid);
1835 int type = get_wcaps_type(caps);
1836
1837 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1838 continue;
1839 adc_nids[nums] = nid;
1840 if (++nums >= max_nums)
1841 break;
1842 }
1843 spec->num_adc_nids = nums;
1844 return nums;
1845}
1846
1847/* filter out invalid adc_nids that don't give all active input pins;
1848 * if needed, check whether dynamic ADC-switching is available
1849 */
1850static int check_dyn_adc_switch(struct hda_codec *codec)
1851{
1852 struct hda_gen_spec *spec = codec->spec;
1853 struct hda_input_mux *imux = &spec->input_mux;
1854 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1855 int i, n, nums;
1856 hda_nid_t pin, adc;
1857
1858 again:
1859 nums = 0;
1860 for (n = 0; n < spec->num_adc_nids; n++) {
1861 adc = spec->adc_nids[n];
1862 for (i = 0; i < imux->num_items; i++) {
1863 pin = spec->imux_pins[i];
1864 if (!is_reachable_path(codec, pin, adc))
1865 break;
1866 }
1867 if (i >= imux->num_items)
1868 adc_nids[nums++] = adc;
1869 }
1870
1871 if (!nums) {
1872 if (spec->shared_mic_hp) {
1873 spec->shared_mic_hp = 0;
1874 imux->num_items = 1;
1875 goto again;
1876 }
1877
1878 /* check whether ADC-switch is possible */
1879 for (i = 0; i < imux->num_items; i++) {
1880 pin = spec->imux_pins[i];
1881 for (n = 0; n < spec->num_adc_nids; n++) {
1882 adc = spec->adc_nids[n];
1883 if (is_reachable_path(codec, pin, adc)) {
1884 spec->dyn_adc_idx[i] = n;
1885 break;
1886 }
1887 }
1888 }
1889
1890 snd_printdd("hda-codec: enabling ADC switching\n");
1891 spec->dyn_adc_switch = 1;
1892 } else if (nums != spec->num_adc_nids) {
1893 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1894 spec->num_adc_nids = nums;
1895 }
1896
1897 if (imux->num_items == 1 || spec->shared_mic_hp) {
1898 snd_printdd("hda-codec: reducing to a single ADC\n");
1899 spec->num_adc_nids = 1; /* reduce to a single ADC */
1900 }
1901
1902 /* single index for individual volumes ctls */
1903 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1904 spec->num_adc_nids = 1;
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return 0;
1907}
1908
1909/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001910 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001912static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001913{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001914 struct hda_gen_spec *spec = codec->spec;
1915 const struct auto_pin_cfg *cfg = &spec->autocfg;
1916 hda_nid_t mixer = spec->mixer_nid;
1917 struct hda_input_mux *imux = &spec->input_mux;
1918 int num_adcs;
1919 int i, c, err, type_idx = 0;
1920 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001921
Takashi Iwai352f7f92012-12-19 12:52:06 +01001922 num_adcs = fill_adc_nids(codec);
1923 if (num_adcs < 0)
1924 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001925
Takashi Iwai352f7f92012-12-19 12:52:06 +01001926 for (i = 0; i < cfg->num_inputs; i++) {
1927 hda_nid_t pin;
1928 const char *label;
1929 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Takashi Iwai352f7f92012-12-19 12:52:06 +01001931 pin = cfg->inputs[i].pin;
1932 if (!is_input_pin(codec, pin))
1933 continue;
1934
1935 label = hda_get_autocfg_input_label(codec, cfg, i);
1936 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1937 label = "Headphone Mic";
1938 if (prev_label && !strcmp(label, prev_label))
1939 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001940 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001941 type_idx = 0;
1942 prev_label = label;
1943
1944 if (mixer) {
1945 if (is_reachable_path(codec, pin, mixer)) {
1946 err = new_analog_input(codec, pin,
1947 label, type_idx, mixer);
1948 if (err < 0)
1949 return err;
1950 }
1951 }
1952
1953 imux_added = false;
1954 for (c = 0; c < num_adcs; c++) {
1955 struct nid_path *path;
1956 hda_nid_t adc = spec->adc_nids[c];
1957
1958 if (!is_reachable_path(codec, pin, adc))
1959 continue;
1960 path = snd_array_new(&spec->paths);
1961 if (!path)
1962 return -ENOMEM;
1963 memset(path, 0, sizeof(*path));
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001964 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001965 snd_printd(KERN_ERR
1966 "invalid input path 0x%x -> 0x%x\n",
1967 pin, adc);
1968 spec->paths.used--;
1969 continue;
1970 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001971 print_nid_path("input", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001972
1973 if (!imux_added) {
1974 spec->imux_pins[imux->num_items] = pin;
1975 snd_hda_add_imux_item(imux, label,
1976 imux->num_items, NULL);
1977 imux_added = true;
1978 }
1979 }
1980 }
1981
1982 return 0;
1983}
1984
1985
1986/*
1987 * input source mux
1988 */
1989
1990/* get the ADC NID corresponding to the given index */
1991static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1992{
1993 struct hda_gen_spec *spec = codec->spec;
1994 if (spec->dyn_adc_switch)
1995 adc_idx = spec->dyn_adc_idx[imux_idx];
1996 return spec->adc_nids[adc_idx];
1997}
1998
1999static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2000 unsigned int idx);
2001
2002static int mux_enum_info(struct snd_kcontrol *kcontrol,
2003 struct snd_ctl_elem_info *uinfo)
2004{
2005 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2006 struct hda_gen_spec *spec = codec->spec;
2007 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2008}
2009
2010static int mux_enum_get(struct snd_kcontrol *kcontrol,
2011 struct snd_ctl_elem_value *ucontrol)
2012{
2013 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2014 struct hda_gen_spec *spec = codec->spec;
2015 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2016
2017 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2018 return 0;
2019}
2020
2021static int mux_enum_put(struct snd_kcontrol *kcontrol,
2022 struct snd_ctl_elem_value *ucontrol)
2023{
2024 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2025 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2026 return mux_select(codec, adc_idx,
2027 ucontrol->value.enumerated.item[0]);
2028}
2029
Takashi Iwai352f7f92012-12-19 12:52:06 +01002030static const struct snd_kcontrol_new cap_src_temp = {
2031 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2032 .name = "Input Source",
2033 .info = mux_enum_info,
2034 .get = mux_enum_get,
2035 .put = mux_enum_put,
2036};
2037
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002038/*
2039 * capture volume and capture switch ctls
2040 */
2041
Takashi Iwai352f7f92012-12-19 12:52:06 +01002042typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2043 struct snd_ctl_elem_value *ucontrol);
2044
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002045/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002046static int cap_put_caller(struct snd_kcontrol *kcontrol,
2047 struct snd_ctl_elem_value *ucontrol,
2048 put_call_t func, int type)
2049{
2050 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2051 struct hda_gen_spec *spec = codec->spec;
2052 const struct hda_input_mux *imux;
2053 struct nid_path *path;
2054 int i, adc_idx, err = 0;
2055
2056 imux = &spec->input_mux;
2057 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2058 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002059 /* we use the cache-only update at first since multiple input paths
2060 * may shared the same amp; by updating only caches, the redundant
2061 * writes to hardware can be reduced.
2062 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002063 codec->cached_write = 1;
2064 for (i = 0; i < imux->num_items; i++) {
2065 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2066 get_adc_nid(codec, adc_idx, i));
2067 if (!path->ctls[type])
2068 continue;
2069 kcontrol->private_value = path->ctls[type];
2070 err = func(kcontrol, ucontrol);
2071 if (err < 0)
2072 goto error;
2073 }
2074 error:
2075 codec->cached_write = 0;
2076 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002077 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002078 if (err >= 0 && spec->cap_sync_hook)
2079 spec->cap_sync_hook(codec);
2080 return err;
2081}
2082
2083/* capture volume ctl callbacks */
2084#define cap_vol_info snd_hda_mixer_amp_volume_info
2085#define cap_vol_get snd_hda_mixer_amp_volume_get
2086#define cap_vol_tlv snd_hda_mixer_amp_tlv
2087
2088static int cap_vol_put(struct snd_kcontrol *kcontrol,
2089 struct snd_ctl_elem_value *ucontrol)
2090{
2091 return cap_put_caller(kcontrol, ucontrol,
2092 snd_hda_mixer_amp_volume_put,
2093 NID_PATH_VOL_CTL);
2094}
2095
2096static const struct snd_kcontrol_new cap_vol_temp = {
2097 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2098 .name = "Capture Volume",
2099 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2100 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2101 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2102 .info = cap_vol_info,
2103 .get = cap_vol_get,
2104 .put = cap_vol_put,
2105 .tlv = { .c = cap_vol_tlv },
2106};
2107
2108/* capture switch ctl callbacks */
2109#define cap_sw_info snd_ctl_boolean_stereo_info
2110#define cap_sw_get snd_hda_mixer_amp_switch_get
2111
2112static int cap_sw_put(struct snd_kcontrol *kcontrol,
2113 struct snd_ctl_elem_value *ucontrol)
2114{
2115 return cap_put_caller(kcontrol, ucontrol,
2116 snd_hda_mixer_amp_switch_put,
2117 NID_PATH_MUTE_CTL);
2118}
2119
2120static const struct snd_kcontrol_new cap_sw_temp = {
2121 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2122 .name = "Capture Switch",
2123 .info = cap_sw_info,
2124 .get = cap_sw_get,
2125 .put = cap_sw_put,
2126};
2127
2128static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2129{
2130 hda_nid_t nid;
2131 int i, depth;
2132
2133 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2134 for (depth = 0; depth < 3; depth++) {
2135 if (depth >= path->depth)
2136 return -EINVAL;
2137 i = path->depth - depth - 1;
2138 nid = path->path[i];
2139 if (!path->ctls[NID_PATH_VOL_CTL]) {
2140 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2141 path->ctls[NID_PATH_VOL_CTL] =
2142 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2143 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2144 int idx = path->idx[i];
2145 if (!depth && codec->single_adc_amp)
2146 idx = 0;
2147 path->ctls[NID_PATH_VOL_CTL] =
2148 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2149 }
2150 }
2151 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2152 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2153 path->ctls[NID_PATH_MUTE_CTL] =
2154 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2155 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2156 int idx = path->idx[i];
2157 if (!depth && codec->single_adc_amp)
2158 idx = 0;
2159 path->ctls[NID_PATH_MUTE_CTL] =
2160 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2161 }
2162 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return 0;
2165}
2166
Takashi Iwai352f7f92012-12-19 12:52:06 +01002167static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002169 struct hda_gen_spec *spec = codec->spec;
2170 struct auto_pin_cfg *cfg = &spec->autocfg;
2171 unsigned int val;
2172 int i;
2173
2174 if (!spec->inv_dmic_split)
2175 return false;
2176 for (i = 0; i < cfg->num_inputs; i++) {
2177 if (cfg->inputs[i].pin != nid)
2178 continue;
2179 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2180 return false;
2181 val = snd_hda_codec_get_pincfg(codec, nid);
2182 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2183 }
2184 return false;
2185}
2186
2187static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2188 int idx, bool is_switch, unsigned int ctl,
2189 bool inv_dmic)
2190{
2191 struct hda_gen_spec *spec = codec->spec;
2192 char tmpname[44];
2193 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2194 const char *sfx = is_switch ? "Switch" : "Volume";
2195 unsigned int chs = inv_dmic ? 1 : 3;
2196 int err;
2197
2198 if (!ctl)
2199 return 0;
2200
2201 if (label)
2202 snprintf(tmpname, sizeof(tmpname),
2203 "%s Capture %s", label, sfx);
2204 else
2205 snprintf(tmpname, sizeof(tmpname),
2206 "Capture %s", sfx);
2207 err = add_control(spec, type, tmpname, idx,
2208 amp_val_replace_channels(ctl, chs));
2209 if (err < 0 || !inv_dmic)
2210 return err;
2211
2212 /* Make independent right kcontrol */
2213 if (label)
2214 snprintf(tmpname, sizeof(tmpname),
2215 "Inverted %s Capture %s", label, sfx);
2216 else
2217 snprintf(tmpname, sizeof(tmpname),
2218 "Inverted Capture %s", sfx);
2219 return add_control(spec, type, tmpname, idx,
2220 amp_val_replace_channels(ctl, 2));
2221}
2222
2223/* create single (and simple) capture volume and switch controls */
2224static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2225 unsigned int vol_ctl, unsigned int sw_ctl,
2226 bool inv_dmic)
2227{
2228 int err;
2229 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2230 if (err < 0)
2231 return err;
2232 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2233 if (err < 0)
2234 return err;
2235 return 0;
2236}
2237
2238/* create bound capture volume and switch controls */
2239static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2240 unsigned int vol_ctl, unsigned int sw_ctl)
2241{
2242 struct hda_gen_spec *spec = codec->spec;
2243 struct snd_kcontrol_new *knew;
2244
2245 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002246 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002247 if (!knew)
2248 return -ENOMEM;
2249 knew->index = idx;
2250 knew->private_value = vol_ctl;
2251 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2252 }
2253 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002254 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002255 if (!knew)
2256 return -ENOMEM;
2257 knew->index = idx;
2258 knew->private_value = sw_ctl;
2259 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2260 }
2261 return 0;
2262}
2263
2264/* return the vol ctl when used first in the imux list */
2265static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2266{
2267 struct hda_gen_spec *spec = codec->spec;
2268 struct nid_path *path;
2269 unsigned int ctl;
2270 int i;
2271
2272 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2273 get_adc_nid(codec, 0, idx));
2274 if (!path)
2275 return 0;
2276 ctl = path->ctls[type];
2277 if (!ctl)
2278 return 0;
2279 for (i = 0; i < idx - 1; i++) {
2280 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2281 get_adc_nid(codec, 0, i));
2282 if (path && path->ctls[type] == ctl)
2283 return 0;
2284 }
2285 return ctl;
2286}
2287
2288/* create individual capture volume and switch controls per input */
2289static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2290{
2291 struct hda_gen_spec *spec = codec->spec;
2292 struct hda_input_mux *imux = &spec->input_mux;
2293 int i, err, type, type_idx = 0;
2294 const char *prev_label = NULL;
2295
2296 for (i = 0; i < imux->num_items; i++) {
2297 const char *label;
2298 bool inv_dmic;
2299 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2300 if (prev_label && !strcmp(label, prev_label))
2301 type_idx++;
2302 else
2303 type_idx = 0;
2304 prev_label = label;
2305 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2306
2307 for (type = 0; type < 2; type++) {
2308 err = add_single_cap_ctl(codec, label, type_idx, type,
2309 get_first_cap_ctl(codec, i, type),
2310 inv_dmic);
2311 if (err < 0)
2312 return err;
2313 }
2314 }
2315 return 0;
2316}
2317
2318static int create_capture_mixers(struct hda_codec *codec)
2319{
2320 struct hda_gen_spec *spec = codec->spec;
2321 struct hda_input_mux *imux = &spec->input_mux;
2322 int i, n, nums, err;
2323
2324 if (spec->dyn_adc_switch)
2325 nums = 1;
2326 else
2327 nums = spec->num_adc_nids;
2328
2329 if (!spec->auto_mic && imux->num_items > 1) {
2330 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002331 const char *name;
2332 name = nums > 1 ? "Input Source" : "Capture Source";
2333 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002334 if (!knew)
2335 return -ENOMEM;
2336 knew->count = nums;
2337 }
2338
2339 for (n = 0; n < nums; n++) {
2340 bool multi = false;
2341 bool inv_dmic = false;
2342 int vol, sw;
2343
2344 vol = sw = 0;
2345 for (i = 0; i < imux->num_items; i++) {
2346 struct nid_path *path;
2347 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2348 get_adc_nid(codec, n, i));
2349 if (!path)
2350 continue;
2351 parse_capvol_in_path(codec, path);
2352 if (!vol)
2353 vol = path->ctls[NID_PATH_VOL_CTL];
2354 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2355 multi = true;
2356 if (!sw)
2357 sw = path->ctls[NID_PATH_MUTE_CTL];
2358 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2359 multi = true;
2360 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2361 inv_dmic = true;
2362 }
2363
2364 if (!multi)
2365 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2366 inv_dmic);
2367 else if (!spec->multi_cap_vol)
2368 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2369 else
2370 err = create_multi_cap_vol_ctl(codec);
2371 if (err < 0)
2372 return err;
2373 }
2374
2375 return 0;
2376}
2377
2378/*
2379 * add mic boosts if needed
2380 */
2381static int parse_mic_boost(struct hda_codec *codec)
2382{
2383 struct hda_gen_spec *spec = codec->spec;
2384 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002385 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002386 int type_idx = 0;
2387 hda_nid_t nid;
2388 const char *prev_label = NULL;
2389
2390 for (i = 0; i < cfg->num_inputs; i++) {
2391 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2392 break;
2393 nid = cfg->inputs[i].pin;
2394 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2395 const char *label;
2396 char boost_label[32];
2397 struct nid_path *path;
2398 unsigned int val;
2399
2400 label = hda_get_autocfg_input_label(codec, cfg, i);
2401 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2402 label = "Headphone Mic";
2403 if (prev_label && !strcmp(label, prev_label))
2404 type_idx++;
2405 else
2406 type_idx = 0;
2407 prev_label = label;
2408
2409 snprintf(boost_label, sizeof(boost_label),
2410 "%s Boost Volume", label);
2411 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2412 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2413 boost_label, type_idx, val);
2414 if (err < 0)
2415 return err;
2416
2417 path = snd_hda_get_nid_path(codec, nid, 0);
2418 if (path)
2419 path->ctls[NID_PATH_BOOST_CTL] = val;
2420 }
2421 }
2422 return 0;
2423}
2424
2425/*
2426 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2427 */
2428static void parse_digital(struct hda_codec *codec)
2429{
2430 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002431 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002432 int i, nums;
2433 hda_nid_t dig_nid;
2434
2435 /* support multiple SPDIFs; the secondary is set up as a slave */
2436 nums = 0;
2437 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2438 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2439 dig_nid = look_for_dac(codec, pin, true);
2440 if (!dig_nid)
2441 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002442 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002443 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002444 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002445 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002446 path->active = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002447 if (!nums) {
2448 spec->multiout.dig_out_nid = dig_nid;
2449 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2450 } else {
2451 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2452 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2453 break;
2454 spec->slave_dig_outs[nums - 1] = dig_nid;
2455 }
2456 nums++;
2457 }
2458
2459 if (spec->autocfg.dig_in_pin) {
2460 dig_nid = codec->start_nid;
2461 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002462 unsigned int wcaps = get_wcaps(codec, dig_nid);
2463 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2464 continue;
2465 if (!(wcaps & AC_WCAP_DIGITAL))
2466 continue;
2467 path = snd_hda_add_new_path(codec,
2468 spec->autocfg.dig_in_pin,
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002469 dig_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002470 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002471 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002472 path->active = true;
2473 spec->dig_in_nid = dig_nid;
2474 break;
2475 }
2476 }
2477 }
2478}
2479
2480
2481/*
2482 * input MUX handling
2483 */
2484
2485static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2486
2487/* select the given imux item; either unmute exclusively or select the route */
2488static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2489 unsigned int idx)
2490{
2491 struct hda_gen_spec *spec = codec->spec;
2492 const struct hda_input_mux *imux;
2493 struct nid_path *path;
2494
2495 imux = &spec->input_mux;
2496 if (!imux->num_items)
2497 return 0;
2498
2499 if (idx >= imux->num_items)
2500 idx = imux->num_items - 1;
2501 if (spec->cur_mux[adc_idx] == idx)
2502 return 0;
2503
2504 path = snd_hda_get_nid_path(codec,
2505 spec->imux_pins[spec->cur_mux[adc_idx]],
2506 spec->adc_nids[adc_idx]);
2507 if (!path)
2508 return 0;
2509 if (path->active)
2510 snd_hda_activate_path(codec, path, false, false);
2511
2512 spec->cur_mux[adc_idx] = idx;
2513
2514 if (spec->shared_mic_hp)
2515 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2516
2517 if (spec->dyn_adc_switch)
2518 dyn_adc_pcm_resetup(codec, idx);
2519
2520 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2521 get_adc_nid(codec, adc_idx, idx));
2522 if (!path)
2523 return 0;
2524 if (path->active)
2525 return 0;
2526 snd_hda_activate_path(codec, path, true, false);
2527 if (spec->cap_sync_hook)
2528 spec->cap_sync_hook(codec);
2529 return 1;
2530}
2531
2532
2533/*
2534 * Jack detections for HP auto-mute and mic-switch
2535 */
2536
2537/* check each pin in the given array; returns true if any of them is plugged */
2538static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2539{
2540 int i, present = 0;
2541
2542 for (i = 0; i < num_pins; i++) {
2543 hda_nid_t nid = pins[i];
2544 if (!nid)
2545 break;
2546 present |= snd_hda_jack_detect(codec, nid);
2547 }
2548 return present;
2549}
2550
2551/* standard HP/line-out auto-mute helper */
2552static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2553 bool mute, bool hp_out)
2554{
2555 struct hda_gen_spec *spec = codec->spec;
2556 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2557 int i;
2558
2559 for (i = 0; i < num_pins; i++) {
2560 hda_nid_t nid = pins[i];
2561 unsigned int val;
2562 if (!nid)
2563 break;
2564 /* don't reset VREF value in case it's controlling
2565 * the amp (see alc861_fixup_asus_amp_vref_0f())
2566 */
2567 if (spec->keep_vref_in_automute) {
2568 val = snd_hda_codec_read(codec, nid, 0,
2569 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2570 val &= ~PIN_HP;
2571 } else
2572 val = 0;
2573 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002574 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002575 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002576 }
2577}
2578
2579/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002580void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002581{
2582 struct hda_gen_spec *spec = codec->spec;
2583 int on;
2584
2585 /* Control HP pins/amps depending on master_mute state;
2586 * in general, HP pins/amps control should be enabled in all cases,
2587 * but currently set only for master_mute, just to be safe
2588 */
2589 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2590 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2591 spec->autocfg.hp_pins, spec->master_mute, true);
2592
2593 if (!spec->automute_speaker)
2594 on = 0;
2595 else
2596 on = spec->hp_jack_present | spec->line_jack_present;
2597 on |= spec->master_mute;
2598 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2599 spec->autocfg.speaker_pins, on, false);
2600
2601 /* toggle line-out mutes if needed, too */
2602 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2603 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2604 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2605 return;
2606 if (!spec->automute_lo)
2607 on = 0;
2608 else
2609 on = spec->hp_jack_present;
2610 on |= spec->master_mute;
2611 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2612 spec->autocfg.line_out_pins, on, false);
2613}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002614EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002615
2616static void call_update_outputs(struct hda_codec *codec)
2617{
2618 struct hda_gen_spec *spec = codec->spec;
2619 if (spec->automute_hook)
2620 spec->automute_hook(codec);
2621 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002622 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002623}
2624
2625/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002626void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002627{
2628 struct hda_gen_spec *spec = codec->spec;
2629
2630 spec->hp_jack_present =
2631 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2632 spec->autocfg.hp_pins);
2633 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2634 return;
2635 call_update_outputs(codec);
2636}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002637EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002638
2639/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002640void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002641{
2642 struct hda_gen_spec *spec = codec->spec;
2643
2644 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2645 return;
2646 /* check LO jack only when it's different from HP */
2647 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2648 return;
2649
2650 spec->line_jack_present =
2651 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2652 spec->autocfg.line_out_pins);
2653 if (!spec->automute_speaker || !spec->detect_lo)
2654 return;
2655 call_update_outputs(codec);
2656}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002657EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002658
2659/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002660void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002661{
2662 struct hda_gen_spec *spec = codec->spec;
2663 int i;
2664
2665 if (!spec->auto_mic)
2666 return;
2667
2668 for (i = spec->am_num_entries - 1; i > 0; i--) {
2669 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2670 mux_select(codec, 0, spec->am_entry[i].idx);
2671 return;
2672 }
2673 }
2674 mux_select(codec, 0, spec->am_entry[0].idx);
2675}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002676EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002677
2678/*
2679 * Auto-Mute mode mixer enum support
2680 */
2681static int automute_mode_info(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_info *uinfo)
2683{
2684 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2685 struct hda_gen_spec *spec = codec->spec;
2686 static const char * const texts3[] = {
2687 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002688 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Takashi Iwai352f7f92012-12-19 12:52:06 +01002690 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2691 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2692 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2693}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Takashi Iwai352f7f92012-12-19 12:52:06 +01002695static int automute_mode_get(struct snd_kcontrol *kcontrol,
2696 struct snd_ctl_elem_value *ucontrol)
2697{
2698 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2699 struct hda_gen_spec *spec = codec->spec;
2700 unsigned int val = 0;
2701 if (spec->automute_speaker)
2702 val++;
2703 if (spec->automute_lo)
2704 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002705
Takashi Iwai352f7f92012-12-19 12:52:06 +01002706 ucontrol->value.enumerated.item[0] = val;
2707 return 0;
2708}
2709
2710static int automute_mode_put(struct snd_kcontrol *kcontrol,
2711 struct snd_ctl_elem_value *ucontrol)
2712{
2713 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2714 struct hda_gen_spec *spec = codec->spec;
2715
2716 switch (ucontrol->value.enumerated.item[0]) {
2717 case 0:
2718 if (!spec->automute_speaker && !spec->automute_lo)
2719 return 0;
2720 spec->automute_speaker = 0;
2721 spec->automute_lo = 0;
2722 break;
2723 case 1:
2724 if (spec->automute_speaker_possible) {
2725 if (!spec->automute_lo && spec->automute_speaker)
2726 return 0;
2727 spec->automute_speaker = 1;
2728 spec->automute_lo = 0;
2729 } else if (spec->automute_lo_possible) {
2730 if (spec->automute_lo)
2731 return 0;
2732 spec->automute_lo = 1;
2733 } else
2734 return -EINVAL;
2735 break;
2736 case 2:
2737 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2738 return -EINVAL;
2739 if (spec->automute_speaker && spec->automute_lo)
2740 return 0;
2741 spec->automute_speaker = 1;
2742 spec->automute_lo = 1;
2743 break;
2744 default:
2745 return -EINVAL;
2746 }
2747 call_update_outputs(codec);
2748 return 1;
2749}
2750
2751static const struct snd_kcontrol_new automute_mode_enum = {
2752 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2753 .name = "Auto-Mute Mode",
2754 .info = automute_mode_info,
2755 .get = automute_mode_get,
2756 .put = automute_mode_put,
2757};
2758
2759static int add_automute_mode_enum(struct hda_codec *codec)
2760{
2761 struct hda_gen_spec *spec = codec->spec;
2762
Takashi Iwai12c93df2012-12-19 14:38:33 +01002763 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002764 return -ENOMEM;
2765 return 0;
2766}
2767
2768/*
2769 * Check the availability of HP/line-out auto-mute;
2770 * Set up appropriately if really supported
2771 */
2772static int check_auto_mute_availability(struct hda_codec *codec)
2773{
2774 struct hda_gen_spec *spec = codec->spec;
2775 struct auto_pin_cfg *cfg = &spec->autocfg;
2776 int present = 0;
2777 int i, err;
2778
2779 if (cfg->hp_pins[0])
2780 present++;
2781 if (cfg->line_out_pins[0])
2782 present++;
2783 if (cfg->speaker_pins[0])
2784 present++;
2785 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002786 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002787
2788 if (!cfg->speaker_pins[0] &&
2789 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2790 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2791 sizeof(cfg->speaker_pins));
2792 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Takashi Iwai352f7f92012-12-19 12:52:06 +01002795 if (!cfg->hp_pins[0] &&
2796 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2797 memcpy(cfg->hp_pins, cfg->line_out_pins,
2798 sizeof(cfg->hp_pins));
2799 cfg->hp_outs = cfg->line_outs;
2800 }
2801
2802 for (i = 0; i < cfg->hp_outs; i++) {
2803 hda_nid_t nid = cfg->hp_pins[i];
2804 if (!is_jack_detectable(codec, nid))
2805 continue;
2806 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2807 nid);
2808 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002809 spec->hp_automute_hook ?
2810 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002811 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002812 spec->detect_hp = 1;
2813 }
2814
2815 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2816 if (cfg->speaker_outs)
2817 for (i = 0; i < cfg->line_outs; i++) {
2818 hda_nid_t nid = cfg->line_out_pins[i];
2819 if (!is_jack_detectable(codec, nid))
2820 continue;
2821 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2822 snd_hda_jack_detect_enable_callback(codec, nid,
2823 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002824 spec->line_automute_hook ?
2825 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002826 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002827 spec->detect_lo = 1;
2828 }
2829 spec->automute_lo_possible = spec->detect_hp;
2830 }
2831
2832 spec->automute_speaker_possible = cfg->speaker_outs &&
2833 (spec->detect_hp || spec->detect_lo);
2834
2835 spec->automute_lo = spec->automute_lo_possible;
2836 spec->automute_speaker = spec->automute_speaker_possible;
2837
2838 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2839 /* create a control for automute mode */
2840 err = add_automute_mode_enum(codec);
2841 if (err < 0)
2842 return err;
2843 }
2844 return 0;
2845}
2846
2847/* return the position of NID in the list, or -1 if not found */
2848static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2849{
2850 int i;
2851 for (i = 0; i < nums; i++)
2852 if (list[i] == nid)
2853 return i;
2854 return -1;
2855}
2856
2857/* check whether all auto-mic pins are valid; setup indices if OK */
2858static bool auto_mic_check_imux(struct hda_codec *codec)
2859{
2860 struct hda_gen_spec *spec = codec->spec;
2861 const struct hda_input_mux *imux;
2862 int i;
2863
2864 imux = &spec->input_mux;
2865 for (i = 0; i < spec->am_num_entries; i++) {
2866 spec->am_entry[i].idx =
2867 find_idx_in_nid_list(spec->am_entry[i].pin,
2868 spec->imux_pins, imux->num_items);
2869 if (spec->am_entry[i].idx < 0)
2870 return false; /* no corresponding imux */
2871 }
2872
2873 /* we don't need the jack detection for the first pin */
2874 for (i = 1; i < spec->am_num_entries; i++)
2875 snd_hda_jack_detect_enable_callback(codec,
2876 spec->am_entry[i].pin,
2877 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002878 spec->mic_autoswitch_hook ?
2879 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002880 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002881 return true;
2882}
2883
2884static int compare_attr(const void *ap, const void *bp)
2885{
2886 const struct automic_entry *a = ap;
2887 const struct automic_entry *b = bp;
2888 return (int)(a->attr - b->attr);
2889}
2890
2891/*
2892 * Check the availability of auto-mic switch;
2893 * Set up if really supported
2894 */
2895static int check_auto_mic_availability(struct hda_codec *codec)
2896{
2897 struct hda_gen_spec *spec = codec->spec;
2898 struct auto_pin_cfg *cfg = &spec->autocfg;
2899 unsigned int types;
2900 int i, num_pins;
2901
2902 types = 0;
2903 num_pins = 0;
2904 for (i = 0; i < cfg->num_inputs; i++) {
2905 hda_nid_t nid = cfg->inputs[i].pin;
2906 unsigned int attr;
2907 attr = snd_hda_codec_get_pincfg(codec, nid);
2908 attr = snd_hda_get_input_pin_attr(attr);
2909 if (types & (1 << attr))
2910 return 0; /* already occupied */
2911 switch (attr) {
2912 case INPUT_PIN_ATTR_INT:
2913 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2914 return 0; /* invalid type */
2915 break;
2916 case INPUT_PIN_ATTR_UNUSED:
2917 return 0; /* invalid entry */
2918 default:
2919 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2920 return 0; /* invalid type */
2921 if (!spec->line_in_auto_switch &&
2922 cfg->inputs[i].type != AUTO_PIN_MIC)
2923 return 0; /* only mic is allowed */
2924 if (!is_jack_detectable(codec, nid))
2925 return 0; /* no unsol support */
2926 break;
2927 }
2928 if (num_pins >= MAX_AUTO_MIC_PINS)
2929 return 0;
2930 types |= (1 << attr);
2931 spec->am_entry[num_pins].pin = nid;
2932 spec->am_entry[num_pins].attr = attr;
2933 num_pins++;
2934 }
2935
2936 if (num_pins < 2)
2937 return 0;
2938
2939 spec->am_num_entries = num_pins;
2940 /* sort the am_entry in the order of attr so that the pin with a
2941 * higher attr will be selected when the jack is plugged.
2942 */
2943 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2944 compare_attr, NULL);
2945
2946 if (!auto_mic_check_imux(codec))
2947 return 0;
2948
2949 spec->auto_mic = 1;
2950 spec->num_adc_nids = 1;
2951 spec->cur_mux[0] = spec->am_entry[0].idx;
2952 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2953 spec->am_entry[0].pin,
2954 spec->am_entry[1].pin,
2955 spec->am_entry[2].pin);
2956
2957 return 0;
2958}
2959
2960
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002961/*
2962 * Parse the given BIOS configuration and set up the hda_gen_spec
2963 *
2964 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002965 * or a negative error code
2966 */
2967int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002968 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002969{
2970 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002971 int err;
2972
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002973 if (cfg != &spec->autocfg) {
2974 spec->autocfg = *cfg;
2975 cfg = &spec->autocfg;
2976 }
2977
Takashi Iwai352f7f92012-12-19 12:52:06 +01002978 if (!cfg->line_outs) {
2979 if (cfg->dig_outs || cfg->dig_in_pin) {
2980 spec->multiout.max_channels = 2;
2981 spec->no_analog = 1;
2982 goto dig_only;
2983 }
2984 return 0; /* can't find valid BIOS pin config */
2985 }
2986
2987 if (!spec->no_primary_hp &&
2988 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2989 cfg->line_outs <= cfg->hp_outs) {
2990 /* use HP as primary out */
2991 cfg->speaker_outs = cfg->line_outs;
2992 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2993 sizeof(cfg->speaker_pins));
2994 cfg->line_outs = cfg->hp_outs;
2995 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2996 cfg->hp_outs = 0;
2997 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2998 cfg->line_out_type = AUTO_PIN_HP_OUT;
2999 }
3000
3001 err = parse_output_paths(codec);
3002 if (err < 0)
3003 return err;
3004 err = create_multi_channel_mode(codec);
3005 if (err < 0)
3006 return err;
3007 err = create_multi_out_ctls(codec, cfg);
3008 if (err < 0)
3009 return err;
3010 err = create_hp_out_ctls(codec);
3011 if (err < 0)
3012 return err;
3013 err = create_speaker_out_ctls(codec);
3014 if (err < 0)
3015 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003016 err = create_indep_hp_ctls(codec);
3017 if (err < 0)
3018 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003019 err = create_shared_input(codec);
3020 if (err < 0)
3021 return err;
3022 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003023 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003024 return err;
3025
Takashi Iwai352f7f92012-12-19 12:52:06 +01003026 /* check the multiple speaker pins */
3027 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
3028 spec->const_channel_count = cfg->line_outs * 2;
3029 else
3030 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003031
Takashi Iwai352f7f92012-12-19 12:52:06 +01003032 if (spec->multi_ios > 0)
3033 spec->multiout.max_channels = max(spec->ext_channel_count,
3034 spec->const_channel_count);
3035 else
3036 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3037
3038 err = check_auto_mute_availability(codec);
3039 if (err < 0)
3040 return err;
3041
3042 err = check_dyn_adc_switch(codec);
3043 if (err < 0)
3044 return err;
3045
3046 if (!spec->shared_mic_hp) {
3047 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003048 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003051
Takashi Iwai352f7f92012-12-19 12:52:06 +01003052 err = create_capture_mixers(codec);
3053 if (err < 0)
3054 return err;
3055
3056 err = parse_mic_boost(codec);
3057 if (err < 0)
3058 return err;
3059
3060 dig_only:
3061 parse_digital(codec);
3062
3063 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003065EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
3067
3068/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003069 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003071
3072/* slave controls for virtual master */
3073static const char * const slave_pfxs[] = {
3074 "Front", "Surround", "Center", "LFE", "Side",
3075 "Headphone", "Speaker", "Mono", "Line Out",
3076 "CLFE", "Bass Speaker", "PCM",
3077 NULL,
3078};
3079
3080int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003082 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Takashi Iwai36502d02012-12-19 15:15:10 +01003085 if (spec->kctls.used) {
3086 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3087 if (err < 0)
3088 return err;
3089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Takashi Iwai352f7f92012-12-19 12:52:06 +01003091 if (spec->multiout.dig_out_nid) {
3092 err = snd_hda_create_dig_out_ctls(codec,
3093 spec->multiout.dig_out_nid,
3094 spec->multiout.dig_out_nid,
3095 spec->pcm_rec[1].pcm_type);
3096 if (err < 0)
3097 return err;
3098 if (!spec->no_analog) {
3099 err = snd_hda_create_spdif_share_sw(codec,
3100 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 if (err < 0)
3102 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003103 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 }
3105 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003106 if (spec->dig_in_nid) {
3107 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3108 if (err < 0)
3109 return err;
3110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Takashi Iwai352f7f92012-12-19 12:52:06 +01003112 /* if we have no master control, let's create it */
3113 if (!spec->no_analog &&
3114 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3115 unsigned int vmaster_tlv[4];
3116 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3117 HDA_OUTPUT, vmaster_tlv);
3118 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3119 vmaster_tlv, slave_pfxs,
3120 "Playback Volume");
3121 if (err < 0)
3122 return err;
3123 }
3124 if (!spec->no_analog &&
3125 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3126 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3127 NULL, slave_pfxs,
3128 "Playback Switch",
3129 true, &spec->vmaster_mute.sw_kctl);
3130 if (err < 0)
3131 return err;
3132 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003133 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3134 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Takashi Iwai352f7f92012-12-19 12:52:06 +01003137 free_kctls(spec); /* no longer needed */
3138
3139 if (spec->shared_mic_hp) {
3140 int err;
3141 int nid = spec->autocfg.inputs[1].pin;
3142 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3143 if (err < 0)
3144 return err;
3145 err = snd_hda_jack_detect_enable(codec, nid, 0);
3146 if (err < 0)
3147 return err;
3148 }
3149
3150 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3151 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 return err;
3153
3154 return 0;
3155}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003156EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
3159/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003160 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Takashi Iwai352f7f92012-12-19 12:52:06 +01003163/*
3164 * Analog playback callbacks
3165 */
3166static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3167 struct hda_codec *codec,
3168 struct snd_pcm_substream *substream)
3169{
3170 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003171 int err;
3172
3173 mutex_lock(&spec->pcm_mutex);
3174 err = snd_hda_multi_out_analog_open(codec,
3175 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003176 hinfo);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003177 if (!err)
3178 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3179 mutex_unlock(&spec->pcm_mutex);
3180 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003181}
3182
3183static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003184 struct hda_codec *codec,
3185 unsigned int stream_tag,
3186 unsigned int format,
3187 struct snd_pcm_substream *substream)
3188{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003189 struct hda_gen_spec *spec = codec->spec;
3190 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3191 stream_tag, format, substream);
3192}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003193
Takashi Iwai352f7f92012-12-19 12:52:06 +01003194static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3195 struct hda_codec *codec,
3196 struct snd_pcm_substream *substream)
3197{
3198 struct hda_gen_spec *spec = codec->spec;
3199 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3200}
3201
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003202static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3203 struct hda_codec *codec,
3204 struct snd_pcm_substream *substream)
3205{
3206 struct hda_gen_spec *spec = codec->spec;
3207 mutex_lock(&spec->pcm_mutex);
3208 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3209 mutex_unlock(&spec->pcm_mutex);
3210 return 0;
3211}
3212
3213static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3214 struct hda_codec *codec,
3215 struct snd_pcm_substream *substream)
3216{
3217 struct hda_gen_spec *spec = codec->spec;
3218 int err = 0;
3219
3220 mutex_lock(&spec->pcm_mutex);
3221 if (!spec->indep_hp_enabled)
3222 err = -EBUSY;
3223 else
3224 spec->active_streams |= 1 << STREAM_INDEP_HP;
3225 mutex_unlock(&spec->pcm_mutex);
3226 return err;
3227}
3228
3229static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3230 struct hda_codec *codec,
3231 struct snd_pcm_substream *substream)
3232{
3233 struct hda_gen_spec *spec = codec->spec;
3234 mutex_lock(&spec->pcm_mutex);
3235 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3236 mutex_unlock(&spec->pcm_mutex);
3237 return 0;
3238}
3239
Takashi Iwai352f7f92012-12-19 12:52:06 +01003240/*
3241 * Digital out
3242 */
3243static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3244 struct hda_codec *codec,
3245 struct snd_pcm_substream *substream)
3246{
3247 struct hda_gen_spec *spec = codec->spec;
3248 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3249}
3250
3251static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3252 struct hda_codec *codec,
3253 unsigned int stream_tag,
3254 unsigned int format,
3255 struct snd_pcm_substream *substream)
3256{
3257 struct hda_gen_spec *spec = codec->spec;
3258 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3259 stream_tag, format, substream);
3260}
3261
3262static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3263 struct hda_codec *codec,
3264 struct snd_pcm_substream *substream)
3265{
3266 struct hda_gen_spec *spec = codec->spec;
3267 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3268}
3269
3270static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3271 struct hda_codec *codec,
3272 struct snd_pcm_substream *substream)
3273{
3274 struct hda_gen_spec *spec = codec->spec;
3275 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3276}
3277
3278/*
3279 * Analog capture
3280 */
3281static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3282 struct hda_codec *codec,
3283 unsigned int stream_tag,
3284 unsigned int format,
3285 struct snd_pcm_substream *substream)
3286{
3287 struct hda_gen_spec *spec = codec->spec;
3288
3289 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003290 stream_tag, 0, format);
3291 return 0;
3292}
3293
Takashi Iwai352f7f92012-12-19 12:52:06 +01003294static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3295 struct hda_codec *codec,
3296 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003297{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003298 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003299
Takashi Iwai352f7f92012-12-19 12:52:06 +01003300 snd_hda_codec_cleanup_stream(codec,
3301 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003302 return 0;
3303}
3304
Takashi Iwai352f7f92012-12-19 12:52:06 +01003305/*
3306 */
3307static const struct hda_pcm_stream pcm_analog_playback = {
3308 .substreams = 1,
3309 .channels_min = 2,
3310 .channels_max = 8,
3311 /* NID is set in build_pcms */
3312 .ops = {
3313 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003314 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003315 .prepare = playback_pcm_prepare,
3316 .cleanup = playback_pcm_cleanup
3317 },
3318};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
Takashi Iwai352f7f92012-12-19 12:52:06 +01003320static const struct hda_pcm_stream pcm_analog_capture = {
3321 .substreams = 1,
3322 .channels_min = 2,
3323 .channels_max = 2,
3324 /* NID is set in build_pcms */
3325};
3326
3327static const struct hda_pcm_stream pcm_analog_alt_playback = {
3328 .substreams = 1,
3329 .channels_min = 2,
3330 .channels_max = 2,
3331 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003332 .ops = {
3333 .open = alt_playback_pcm_open,
3334 .close = alt_playback_pcm_close
3335 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003336};
3337
3338static const struct hda_pcm_stream pcm_analog_alt_capture = {
3339 .substreams = 2, /* can be overridden */
3340 .channels_min = 2,
3341 .channels_max = 2,
3342 /* NID is set in build_pcms */
3343 .ops = {
3344 .prepare = alt_capture_pcm_prepare,
3345 .cleanup = alt_capture_pcm_cleanup
3346 },
3347};
3348
3349static const struct hda_pcm_stream pcm_digital_playback = {
3350 .substreams = 1,
3351 .channels_min = 2,
3352 .channels_max = 2,
3353 /* NID is set in build_pcms */
3354 .ops = {
3355 .open = dig_playback_pcm_open,
3356 .close = dig_playback_pcm_close,
3357 .prepare = dig_playback_pcm_prepare,
3358 .cleanup = dig_playback_pcm_cleanup
3359 },
3360};
3361
3362static const struct hda_pcm_stream pcm_digital_capture = {
3363 .substreams = 1,
3364 .channels_min = 2,
3365 .channels_max = 2,
3366 /* NID is set in build_pcms */
3367};
3368
3369/* Used by build_pcms to flag that a PCM has no playback stream */
3370static const struct hda_pcm_stream pcm_null_stream = {
3371 .substreams = 0,
3372 .channels_min = 0,
3373 .channels_max = 0,
3374};
3375
3376/*
3377 * dynamic changing ADC PCM streams
3378 */
3379static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3380{
3381 struct hda_gen_spec *spec = codec->spec;
3382 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3383
3384 if (spec->cur_adc && spec->cur_adc != new_adc) {
3385 /* stream is running, let's swap the current ADC */
3386 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3387 spec->cur_adc = new_adc;
3388 snd_hda_codec_setup_stream(codec, new_adc,
3389 spec->cur_adc_stream_tag, 0,
3390 spec->cur_adc_format);
3391 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003393 return false;
3394}
3395
3396/* analog capture with dynamic dual-adc changes */
3397static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3398 struct hda_codec *codec,
3399 unsigned int stream_tag,
3400 unsigned int format,
3401 struct snd_pcm_substream *substream)
3402{
3403 struct hda_gen_spec *spec = codec->spec;
3404 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3405 spec->cur_adc_stream_tag = stream_tag;
3406 spec->cur_adc_format = format;
3407 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3408 return 0;
3409}
3410
3411static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3412 struct hda_codec *codec,
3413 struct snd_pcm_substream *substream)
3414{
3415 struct hda_gen_spec *spec = codec->spec;
3416 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3417 spec->cur_adc = 0;
3418 return 0;
3419}
3420
3421static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3422 .substreams = 1,
3423 .channels_min = 2,
3424 .channels_max = 2,
3425 .nid = 0, /* fill later */
3426 .ops = {
3427 .prepare = dyn_adc_capture_pcm_prepare,
3428 .cleanup = dyn_adc_capture_pcm_cleanup
3429 },
3430};
3431
Takashi Iwaif873e532012-12-20 16:58:39 +01003432static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3433 const char *chip_name)
3434{
3435 char *p;
3436
3437 if (*str)
3438 return;
3439 strlcpy(str, chip_name, len);
3440
3441 /* drop non-alnum chars after a space */
3442 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3443 if (!isalnum(p[1])) {
3444 *p = 0;
3445 break;
3446 }
3447 }
3448 strlcat(str, sfx, len);
3449}
3450
Takashi Iwai352f7f92012-12-19 12:52:06 +01003451/* build PCM streams based on the parsed results */
3452int snd_hda_gen_build_pcms(struct hda_codec *codec)
3453{
3454 struct hda_gen_spec *spec = codec->spec;
3455 struct hda_pcm *info = spec->pcm_rec;
3456 const struct hda_pcm_stream *p;
3457 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
3459 codec->num_pcms = 1;
3460 codec->pcm_info = info;
3461
Takashi Iwai352f7f92012-12-19 12:52:06 +01003462 if (spec->no_analog)
3463 goto skip_analog;
3464
Takashi Iwaif873e532012-12-20 16:58:39 +01003465 fill_pcm_stream_name(spec->stream_name_analog,
3466 sizeof(spec->stream_name_analog),
3467 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003468 info->name = spec->stream_name_analog;
3469
3470 if (spec->multiout.num_dacs > 0) {
3471 p = spec->stream_analog_playback;
3472 if (!p)
3473 p = &pcm_analog_playback;
3474 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3475 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3476 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3477 spec->multiout.max_channels;
3478 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3479 spec->autocfg.line_outs == 2)
3480 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3481 snd_pcm_2_1_chmaps;
3482 }
3483 if (spec->num_adc_nids) {
3484 p = spec->stream_analog_capture;
3485 if (!p) {
3486 if (spec->dyn_adc_switch)
3487 p = &dyn_adc_pcm_analog_capture;
3488 else
3489 p = &pcm_analog_capture;
3490 }
3491 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3492 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3493 }
3494
Takashi Iwai352f7f92012-12-19 12:52:06 +01003495 skip_analog:
3496 /* SPDIF for stream index #1 */
3497 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003498 fill_pcm_stream_name(spec->stream_name_digital,
3499 sizeof(spec->stream_name_digital),
3500 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003501 codec->num_pcms = 2;
3502 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3503 info = spec->pcm_rec + 1;
3504 info->name = spec->stream_name_digital;
3505 if (spec->dig_out_type)
3506 info->pcm_type = spec->dig_out_type;
3507 else
3508 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3509 if (spec->multiout.dig_out_nid) {
3510 p = spec->stream_digital_playback;
3511 if (!p)
3512 p = &pcm_digital_playback;
3513 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3514 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3515 }
3516 if (spec->dig_in_nid) {
3517 p = spec->stream_digital_capture;
3518 if (!p)
3519 p = &pcm_digital_capture;
3520 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3521 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3522 }
3523 }
3524
3525 if (spec->no_analog)
3526 return 0;
3527
3528 /* If the use of more than one ADC is requested for the current
3529 * model, configure a second analog capture-only PCM.
3530 */
3531 have_multi_adcs = (spec->num_adc_nids > 1) &&
3532 !spec->dyn_adc_switch && !spec->auto_mic;
3533 /* Additional Analaog capture for index #2 */
3534 if (spec->alt_dac_nid || have_multi_adcs) {
3535 codec->num_pcms = 3;
3536 info = spec->pcm_rec + 2;
3537 info->name = spec->stream_name_analog;
3538 if (spec->alt_dac_nid) {
3539 p = spec->stream_analog_alt_playback;
3540 if (!p)
3541 p = &pcm_analog_alt_playback;
3542 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3543 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3544 spec->alt_dac_nid;
3545 } else {
3546 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3547 pcm_null_stream;
3548 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3549 }
3550 if (have_multi_adcs) {
3551 p = spec->stream_analog_alt_capture;
3552 if (!p)
3553 p = &pcm_analog_alt_capture;
3554 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3555 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3556 spec->adc_nids[1];
3557 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3558 spec->num_adc_nids - 1;
3559 } else {
3560 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3561 pcm_null_stream;
3562 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 }
3565
3566 return 0;
3567}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003568EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3569
3570
3571/*
3572 * Standard auto-parser initializations
3573 */
3574
3575/* configure the path from the given dac to the pin as the proper output */
3576static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3577 int pin_type, hda_nid_t dac)
3578{
3579 struct nid_path *path;
3580
3581 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3582 path = snd_hda_get_nid_path(codec, dac, pin);
3583 if (!path)
3584 return;
Takashi Iwaie1284af2013-01-03 16:33:02 +01003585 snd_hda_activate_path(codec, path, path->active, true);
3586 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003587}
3588
3589/* initialize primary output paths */
3590static void init_multi_out(struct hda_codec *codec)
3591{
3592 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai64049c82012-12-20 15:29:21 +01003593 hda_nid_t nid, dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003594 int pin_type;
3595 int i;
3596
3597 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3598 pin_type = PIN_HP;
3599 else
3600 pin_type = PIN_OUT;
3601
Takashi Iwai64049c82012-12-20 15:29:21 +01003602 for (i = 0; i < spec->autocfg.line_outs; i++) {
3603 nid = spec->autocfg.line_out_pins[i];
3604 if (nid) {
3605 dac = spec->multiout.dac_nids[i];
3606 if (!dac)
3607 dac = spec->multiout.dac_nids[0];
3608 set_output_and_unmute(codec, nid, pin_type, dac);
3609 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003610 }
3611}
3612
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003613
3614static void __init_extra_out(struct hda_codec *codec, int num_outs,
3615 hda_nid_t *pins, hda_nid_t *dacs, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003616{
3617 struct hda_gen_spec *spec = codec->spec;
3618 int i;
3619 hda_nid_t pin, dac;
3620
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003621 for (i = 0; i < num_outs; i++) {
3622 pin = pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003623 if (!pin)
3624 break;
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003625 dac = dacs[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003626 if (!dac) {
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003627 if (i > 0 && dacs[0])
3628 dac = dacs[0];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003629 else
3630 dac = spec->multiout.dac_nids[0];
3631 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003632 set_output_and_unmute(codec, pin, type, dac);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003633 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003634}
3635
3636/* initialize hp and speaker paths */
3637static void init_extra_out(struct hda_codec *codec)
3638{
3639 struct hda_gen_spec *spec = codec->spec;
3640
3641 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3642 __init_extra_out(codec, spec->autocfg.hp_outs,
3643 spec->autocfg.hp_pins,
3644 spec->multiout.hp_out_nid, PIN_HP);
3645 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3646 __init_extra_out(codec, spec->autocfg.speaker_outs,
3647 spec->autocfg.speaker_pins,
3648 spec->multiout.extra_out_nid, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003649}
3650
3651/* initialize multi-io paths */
3652static void init_multi_io(struct hda_codec *codec)
3653{
3654 struct hda_gen_spec *spec = codec->spec;
3655 int i;
3656
3657 for (i = 0; i < spec->multi_ios; i++) {
3658 hda_nid_t pin = spec->multi_io[i].pin;
3659 struct nid_path *path;
3660 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3661 if (!path)
3662 continue;
3663 if (!spec->multi_io[i].ctl_in)
3664 spec->multi_io[i].ctl_in =
3665 snd_hda_codec_update_cache(codec, pin, 0,
3666 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3667 snd_hda_activate_path(codec, path, path->active, true);
3668 }
3669}
3670
3671/* set up the input pin config, depending on the given auto-pin type */
3672static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3673 int auto_pin_type)
3674{
3675 unsigned int val = PIN_IN;
3676 if (auto_pin_type == AUTO_PIN_MIC)
3677 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003678 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003679}
3680
3681/* set up input pins and loopback paths */
3682static void init_analog_input(struct hda_codec *codec)
3683{
3684 struct hda_gen_spec *spec = codec->spec;
3685 struct auto_pin_cfg *cfg = &spec->autocfg;
3686 int i;
3687
3688 for (i = 0; i < cfg->num_inputs; i++) {
3689 hda_nid_t nid = cfg->inputs[i].pin;
3690 if (is_input_pin(codec, nid))
3691 set_input_pin(codec, nid, cfg->inputs[i].type);
3692
3693 /* init loopback inputs */
3694 if (spec->mixer_nid) {
3695 struct nid_path *path;
3696 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3697 if (path)
3698 snd_hda_activate_path(codec, path,
3699 path->active, false);
3700 }
3701 }
3702}
3703
3704/* initialize ADC paths */
3705static void init_input_src(struct hda_codec *codec)
3706{
3707 struct hda_gen_spec *spec = codec->spec;
3708 struct hda_input_mux *imux = &spec->input_mux;
3709 struct nid_path *path;
3710 int i, c, nums;
3711
3712 if (spec->dyn_adc_switch)
3713 nums = 1;
3714 else
3715 nums = spec->num_adc_nids;
3716
3717 for (c = 0; c < nums; c++) {
3718 for (i = 0; i < imux->num_items; i++) {
3719 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3720 get_adc_nid(codec, c, i));
3721 if (path) {
3722 bool active = path->active;
3723 if (i == spec->cur_mux[c])
3724 active = true;
3725 snd_hda_activate_path(codec, path, active, false);
3726 }
3727 }
3728 }
3729
3730 if (spec->shared_mic_hp)
3731 update_shared_mic_hp(codec, spec->cur_mux[0]);
3732
3733 if (spec->cap_sync_hook)
3734 spec->cap_sync_hook(codec);
3735}
3736
3737/* set right pin controls for digital I/O */
3738static void init_digital(struct hda_codec *codec)
3739{
3740 struct hda_gen_spec *spec = codec->spec;
3741 int i;
3742 hda_nid_t pin;
3743
3744 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3745 pin = spec->autocfg.dig_out_pins[i];
3746 if (!pin)
3747 continue;
3748 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3749 }
3750 pin = spec->autocfg.dig_in_pin;
3751 if (pin)
Takashi Iwai7594aa32012-12-20 15:38:40 +01003752 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003753}
3754
Takashi Iwai973e4972012-12-20 15:16:09 +01003755/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3756 * invalid unsol tags by some reason
3757 */
3758static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3759{
3760 int i;
3761
3762 for (i = 0; i < codec->init_pins.used; i++) {
3763 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3764 hda_nid_t nid = pin->nid;
3765 if (is_jack_detectable(codec, nid) &&
3766 !snd_hda_jack_tbl_get(codec, nid))
3767 snd_hda_codec_update_cache(codec, nid, 0,
3768 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3769 }
3770}
3771
Takashi Iwai352f7f92012-12-19 12:52:06 +01003772int snd_hda_gen_init(struct hda_codec *codec)
3773{
3774 struct hda_gen_spec *spec = codec->spec;
3775
3776 if (spec->init_hook)
3777 spec->init_hook(codec);
3778
3779 snd_hda_apply_verbs(codec);
3780
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003781 codec->cached_write = 1;
3782
Takashi Iwai352f7f92012-12-19 12:52:06 +01003783 init_multi_out(codec);
3784 init_extra_out(codec);
3785 init_multi_io(codec);
3786 init_analog_input(codec);
3787 init_input_src(codec);
3788 init_digital(codec);
3789
Takashi Iwai973e4972012-12-20 15:16:09 +01003790 clear_unsol_on_unused_pins(codec);
3791
Takashi Iwai352f7f92012-12-19 12:52:06 +01003792 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003793 snd_hda_gen_hp_automute(codec, NULL);
3794 snd_hda_gen_line_automute(codec, NULL);
3795 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003796
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003797 snd_hda_codec_flush_amp_cache(codec);
3798 snd_hda_codec_flush_cmd_cache(codec);
3799
Takashi Iwai352f7f92012-12-19 12:52:06 +01003800 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3801 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3802
3803 hda_call_check_power_status(codec, 0x01);
3804 return 0;
3805}
3806EXPORT_SYMBOL(snd_hda_gen_init);
3807
3808
3809/*
3810 * the generic codec support
3811 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
Takashi Iwai83012a72012-08-24 18:38:08 +02003813#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003814static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3815{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003816 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003817 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3818}
3819#endif
3820
Takashi Iwai352f7f92012-12-19 12:52:06 +01003821static void generic_free(struct hda_codec *codec)
3822{
3823 snd_hda_gen_spec_free(codec->spec);
3824 kfree(codec->spec);
3825 codec->spec = NULL;
3826}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
Takashi Iwai352f7f92012-12-19 12:52:06 +01003828static const struct hda_codec_ops generic_patch_ops = {
3829 .build_controls = snd_hda_gen_build_controls,
3830 .build_pcms = snd_hda_gen_build_pcms,
3831 .init = snd_hda_gen_init,
3832 .free = generic_free,
3833 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02003834#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003835 .check_power_status = generic_check_power_status,
3836#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837};
3838
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839int snd_hda_parse_generic_codec(struct hda_codec *codec)
3840{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003841 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 int err;
3843
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003844 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003845 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003847 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003850 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3851 if (err < 0)
3852 return err;
3853
3854 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003855 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 goto error;
3857
3858 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 return 0;
3860
Takashi Iwai352f7f92012-12-19 12:52:06 +01003861error:
3862 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 return err;
3864}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003865EXPORT_SYMBOL(snd_hda_parse_generic_codec);