blob: 88f166e65752da191a1da1c8d1048f94a1ce93d5 [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 Iwai352f7f92012-12-19 12:52:06 +0100119/* get the path between the given NIDs;
120 * passing 0 to either @pin or @dac behaves as a wildcard
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100122struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
123 hda_nid_t from_nid, hda_nid_t to_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100125 struct hda_gen_spec *spec = codec->spec;
126 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Takashi Iwai352f7f92012-12-19 12:52:06 +0100128 for (i = 0; i < spec->paths.used; i++) {
129 struct nid_path *path = snd_array_elem(&spec->paths, i);
130 if (path->depth <= 0)
131 continue;
132 if ((!from_nid || path->path[0] == from_nid) &&
133 (!to_nid || path->path[path->depth - 1] == to_nid))
134 return path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136 return NULL;
137}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100138EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
139
140/* check whether the given DAC is already found in any existing paths */
141static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
142{
143 struct hda_gen_spec *spec = codec->spec;
144 int i;
145
146 for (i = 0; i < spec->paths.used; i++) {
147 struct nid_path *path = snd_array_elem(&spec->paths, i);
148 if (path->path[0] == nid)
149 return true;
150 }
151 return false;
152}
153
154/* check whether the given two widgets can be connected */
155static bool is_reachable_path(struct hda_codec *codec,
156 hda_nid_t from_nid, hda_nid_t to_nid)
157{
158 if (!from_nid || !to_nid)
159 return false;
160 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
161}
162
163/* nid, dir and idx */
164#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
165
166/* check whether the given ctl is already assigned in any path elements */
167static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
168{
169 struct hda_gen_spec *spec = codec->spec;
170 int i;
171
172 val &= AMP_VAL_COMPARE_MASK;
173 for (i = 0; i < spec->paths.used; i++) {
174 struct nid_path *path = snd_array_elem(&spec->paths, i);
175 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
176 return true;
177 }
178 return false;
179}
180
181/* check whether a control with the given (nid, dir, idx) was assigned */
182static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
183 int dir, int idx)
184{
185 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
186 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
187 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
188}
189
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100190static void print_nid_path(const char *pfx, struct nid_path *path)
191{
192 char buf[40];
193 int i;
194
195
196 buf[0] = 0;
197 for (i = 0; i < path->depth; i++) {
198 char tmp[4];
199 sprintf(tmp, ":%02x", path->path[i]);
200 strlcat(buf, tmp, sizeof(buf));
201 }
202 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
203}
204
Takashi Iwai352f7f92012-12-19 12:52:06 +0100205/* called recursively */
206static bool __parse_nid_path(struct hda_codec *codec,
207 hda_nid_t from_nid, hda_nid_t to_nid,
208 int with_aa_mix, struct nid_path *path, int depth)
209{
210 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100211 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100212 int i, nums;
213
214 if (to_nid == spec->mixer_nid) {
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100215 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100216 return false;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100217 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100218 }
219
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100220 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100221 for (i = 0; i < nums; i++) {
222 if (conn[i] != from_nid) {
223 /* special case: when from_nid is 0,
224 * try to find an empty DAC
225 */
226 if (from_nid ||
227 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
228 is_dac_already_used(codec, conn[i]))
229 continue;
230 }
231 /* aa-mix is requested but not included? */
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100232 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100233 goto found;
234 }
235 if (depth >= MAX_NID_PATH_DEPTH)
236 return false;
237 for (i = 0; i < nums; i++) {
238 unsigned int type;
239 type = get_wcaps_type(get_wcaps(codec, conn[i]));
240 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
241 type == AC_WID_PIN)
242 continue;
243 if (__parse_nid_path(codec, from_nid, conn[i],
244 with_aa_mix, path, depth + 1))
245 goto found;
246 }
247 return false;
248
249 found:
250 path->path[path->depth] = conn[i];
251 path->idx[path->depth + 1] = i;
252 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
253 path->multi[path->depth + 1] = 1;
254 path->depth++;
255 return true;
256}
257
258/* parse the widget path from the given nid to the target nid;
259 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100260 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
261 * excluded, only the paths that don't go through the mixer will be chosen.
262 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
263 * spec->mixer_nid will be chosen.
264 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100265 */
266bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
267 hda_nid_t to_nid, int with_aa_mix,
268 struct nid_path *path)
269{
270 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
271 path->path[path->depth] = to_nid;
272 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100273 return true;
274 }
275 return false;
276}
277EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100280 * parse the path between the given NIDs and add to the path list.
281 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100283struct nid_path *
284snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
285 hda_nid_t to_nid, int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100287 struct hda_gen_spec *spec = codec->spec;
288 struct nid_path *path;
289
290 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
291 return NULL;
292
293 path = snd_array_new(&spec->paths);
294 if (!path)
295 return NULL;
296 memset(path, 0, sizeof(*path));
297 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
298 return path;
299 /* push back */
300 spec->paths.used--;
301 return NULL;
302}
303EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
304
305/* look for an empty DAC slot */
306static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
307 bool is_digital)
308{
309 struct hda_gen_spec *spec = codec->spec;
310 bool cap_digital;
311 int i;
312
313 for (i = 0; i < spec->num_all_dacs; i++) {
314 hda_nid_t nid = spec->all_dacs[i];
315 if (!nid || is_dac_already_used(codec, nid))
316 continue;
317 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
318 if (is_digital != cap_digital)
319 continue;
320 if (is_reachable_path(codec, nid, pin))
321 return nid;
322 }
323 return 0;
324}
325
326/* replace the channels in the composed amp value with the given number */
327static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
328{
329 val &= ~(0x3U << 16);
330 val |= chs << 16;
331 return val;
332}
333
334/* check whether the widget has the given amp capability for the direction */
335static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
336 int dir, unsigned int bits)
337{
338 if (!nid)
339 return false;
340 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
341 if (query_amp_caps(codec, nid, dir) & bits)
342 return true;
343 return false;
344}
345
346#define nid_has_mute(codec, nid, dir) \
347 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
348#define nid_has_volume(codec, nid, dir) \
349 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
350
351/* look for a widget suitable for assigning a mute switch in the path */
352static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
353 struct nid_path *path)
354{
355 int i;
356
357 for (i = path->depth - 1; i >= 0; i--) {
358 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
359 return path->path[i];
360 if (i != path->depth - 1 && i != 0 &&
361 nid_has_mute(codec, path->path[i], HDA_INPUT))
362 return path->path[i];
363 }
364 return 0;
365}
366
367/* look for a widget suitable for assigning a volume ctl in the path */
368static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
369 struct nid_path *path)
370{
371 int i;
372
373 for (i = path->depth - 1; i >= 0; i--) {
374 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
375 return path->path[i];
376 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100381 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100383
384/* can have the amp-in capability? */
385static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100387 hda_nid_t nid = path->path[idx];
388 unsigned int caps = get_wcaps(codec, nid);
389 unsigned int type = get_wcaps_type(caps);
390
391 if (!(caps & AC_WCAP_IN_AMP))
392 return false;
393 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
394 return false;
395 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Takashi Iwai352f7f92012-12-19 12:52:06 +0100398/* can have the amp-out capability? */
399static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100401 hda_nid_t nid = path->path[idx];
402 unsigned int caps = get_wcaps(codec, nid);
403 unsigned int type = get_wcaps_type(caps);
404
405 if (!(caps & AC_WCAP_OUT_AMP))
406 return false;
407 if (type == AC_WID_PIN && !idx) /* only for output pins */
408 return false;
409 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Takashi Iwai352f7f92012-12-19 12:52:06 +0100412/* check whether the given (nid,dir,idx) is active */
413static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
414 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100416 struct hda_gen_spec *spec = codec->spec;
417 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Takashi Iwai352f7f92012-12-19 12:52:06 +0100419 for (n = 0; n < spec->paths.used; n++) {
420 struct nid_path *path = snd_array_elem(&spec->paths, n);
421 if (!path->active)
422 continue;
423 for (i = 0; i < path->depth; i++) {
424 if (path->path[i] == nid) {
425 if (dir == HDA_OUTPUT || path->idx[i] == idx)
426 return true;
427 break;
428 }
429 }
430 }
431 return false;
432}
433
434/* get the default amp value for the target state */
435static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
436 int dir, bool enable)
437{
438 unsigned int caps;
439 unsigned int val = 0;
440
441 caps = query_amp_caps(codec, nid, dir);
442 if (caps & AC_AMPCAP_NUM_STEPS) {
443 /* set to 0dB */
444 if (enable)
445 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
446 }
447 if (caps & AC_AMPCAP_MUTE) {
448 if (!enable)
449 val |= HDA_AMP_MUTE;
450 }
451 return val;
452}
453
454/* initialize the amp value (only at the first time) */
455static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
456{
457 int val = get_amp_val_to_activate(codec, nid, dir, false);
458 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
459}
460
461static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
462 int idx, bool enable)
463{
464 int val;
465 if (is_ctl_associated(codec, nid, dir, idx) ||
466 is_active_nid(codec, nid, dir, idx))
467 return;
468 val = get_amp_val_to_activate(codec, nid, dir, enable);
469 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
470}
471
472static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
473 int i, bool enable)
474{
475 hda_nid_t nid = path->path[i];
476 init_amp(codec, nid, HDA_OUTPUT, 0);
477 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
478}
479
480static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
481 int i, bool enable, bool add_aamix)
482{
483 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100484 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100485 int n, nums, idx;
486 int type;
487 hda_nid_t nid = path->path[i];
488
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100489 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100490 type = get_wcaps_type(get_wcaps(codec, nid));
491 if (type == AC_WID_PIN ||
492 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
493 nums = 1;
494 idx = 0;
495 } else
496 idx = path->idx[i];
497
498 for (n = 0; n < nums; n++)
499 init_amp(codec, nid, HDA_INPUT, n);
500
501 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
502 return;
503
504 /* here is a little bit tricky in comparison with activate_amp_out();
505 * when aa-mixer is available, we need to enable the path as well
506 */
507 for (n = 0; n < nums; n++) {
508 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
509 continue;
510 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512}
513
Takashi Iwai352f7f92012-12-19 12:52:06 +0100514/* activate or deactivate the given path
515 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100517void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
518 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100520 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Takashi Iwai352f7f92012-12-19 12:52:06 +0100522 if (!enable)
523 path->active = false;
524
525 for (i = path->depth - 1; i >= 0; i--) {
526 if (enable && path->multi[i])
527 snd_hda_codec_write_cache(codec, path->path[i], 0,
528 AC_VERB_SET_CONNECT_SEL,
529 path->idx[i]);
530 if (has_amp_in(codec, path, i))
531 activate_amp_in(codec, path, i, enable, add_aamix);
532 if (has_amp_out(codec, path, i))
533 activate_amp_out(codec, path, i, enable);
534 }
535
536 if (enable)
537 path->active = true;
538}
539EXPORT_SYMBOL_HDA(snd_hda_activate_path);
540
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100541/* turn on/off EAPD on the given pin */
542static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
543{
544 struct hda_gen_spec *spec = codec->spec;
545 if (spec->own_eapd_ctl ||
546 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
547 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100548 if (codec->inv_eapd)
549 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100550 snd_hda_codec_update_cache(codec, pin, 0,
551 AC_VERB_SET_EAPD_BTLENABLE,
552 enable ? 0x02 : 0x00);
553}
554
Takashi Iwai352f7f92012-12-19 12:52:06 +0100555
556/*
557 * Helper functions for creating mixer ctl elements
558 */
559
560enum {
561 HDA_CTL_WIDGET_VOL,
562 HDA_CTL_WIDGET_MUTE,
563 HDA_CTL_BIND_MUTE,
564 HDA_CTL_BIND_VOL,
565 HDA_CTL_BIND_SW,
566};
567static const struct snd_kcontrol_new control_templates[] = {
568 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
569 HDA_CODEC_MUTE(NULL, 0, 0, 0),
570 HDA_BIND_MUTE(NULL, 0, 0, 0),
571 HDA_BIND_VOL(NULL, 0),
572 HDA_BIND_SW(NULL, 0),
573};
574
575/* add dynamic controls from template */
576static int add_control(struct hda_gen_spec *spec, int type, const char *name,
577 int cidx, unsigned long val)
578{
579 struct snd_kcontrol_new *knew;
580
Takashi Iwai12c93df2012-12-19 14:38:33 +0100581 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100582 if (!knew)
583 return -ENOMEM;
584 knew->index = cidx;
585 if (get_amp_nid_(val))
586 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
587 knew->private_value = val;
588 return 0;
589}
590
591static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
592 const char *pfx, const char *dir,
593 const char *sfx, int cidx, unsigned long val)
594{
595 char name[32];
596 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
597 return add_control(spec, type, name, cidx, val);
598}
599
600#define add_pb_vol_ctrl(spec, type, pfx, val) \
601 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
602#define add_pb_sw_ctrl(spec, type, pfx, val) \
603 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
604#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
605 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
606#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
607 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
608
609static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
610 unsigned int chs, struct nid_path *path)
611{
612 unsigned int val;
613 if (!path)
614 return 0;
615 val = path->ctls[NID_PATH_VOL_CTL];
616 if (!val)
617 return 0;
618 val = amp_val_replace_channels(val, chs);
619 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
620}
621
622/* return the channel bits suitable for the given path->ctls[] */
623static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
624 int type)
625{
626 int chs = 1; /* mono (left only) */
627 if (path) {
628 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
629 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
630 chs = 3; /* stereo */
631 }
632 return chs;
633}
634
635static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
636 struct nid_path *path)
637{
638 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
639 return add_vol_ctl(codec, pfx, cidx, chs, path);
640}
641
642/* create a mute-switch for the given mixer widget;
643 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
644 */
645static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
646 unsigned int chs, struct nid_path *path)
647{
648 unsigned int val;
649 int type = HDA_CTL_WIDGET_MUTE;
650
651 if (!path)
652 return 0;
653 val = path->ctls[NID_PATH_MUTE_CTL];
654 if (!val)
655 return 0;
656 val = amp_val_replace_channels(val, chs);
657 if (get_amp_direction_(val) == HDA_INPUT) {
658 hda_nid_t nid = get_amp_nid_(val);
659 int nums = snd_hda_get_num_conns(codec, nid);
660 if (nums > 1) {
661 type = HDA_CTL_BIND_MUTE;
662 val |= nums << 19;
663 }
664 }
665 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
666}
667
668static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
669 int cidx, struct nid_path *path)
670{
671 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
672 return add_sw_ctl(codec, pfx, cidx, chs, path);
673}
674
675static const char * const channel_name[4] = {
676 "Front", "Surround", "CLFE", "Side"
677};
678
679/* give some appropriate ctl name prefix for the given line out channel */
680static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
681 bool can_be_master, int *index)
682{
683 struct auto_pin_cfg *cfg = &spec->autocfg;
684
685 *index = 0;
686 if (cfg->line_outs == 1 && !spec->multi_ios &&
687 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
688 return spec->vmaster_mute.hook ? "PCM" : "Master";
689
690 /* if there is really a single DAC used in the whole output paths,
691 * use it master (or "PCM" if a vmaster hook is present)
692 */
693 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
694 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
695 return spec->vmaster_mute.hook ? "PCM" : "Master";
696
697 switch (cfg->line_out_type) {
698 case AUTO_PIN_SPEAKER_OUT:
699 if (cfg->line_outs == 1)
700 return "Speaker";
701 if (cfg->line_outs == 2)
702 return ch ? "Bass Speaker" : "Speaker";
703 break;
704 case AUTO_PIN_HP_OUT:
705 /* for multi-io case, only the primary out */
706 if (ch && spec->multi_ios)
707 break;
708 *index = ch;
709 return "Headphone";
710 default:
711 if (cfg->line_outs == 1 && !spec->multi_ios)
712 return "PCM";
713 break;
714 }
715 if (ch >= ARRAY_SIZE(channel_name)) {
716 snd_BUG();
717 return "PCM";
718 }
719
720 return channel_name[ch];
721}
722
723/*
724 * Parse output paths
725 */
726
727/* badness definition */
728enum {
729 /* No primary DAC is found for the main output */
730 BAD_NO_PRIMARY_DAC = 0x10000,
731 /* No DAC is found for the extra output */
732 BAD_NO_DAC = 0x4000,
733 /* No possible multi-ios */
734 BAD_MULTI_IO = 0x103,
735 /* No individual DAC for extra output */
736 BAD_NO_EXTRA_DAC = 0x102,
737 /* No individual DAC for extra surrounds */
738 BAD_NO_EXTRA_SURR_DAC = 0x101,
739 /* Primary DAC shared with main surrounds */
740 BAD_SHARED_SURROUND = 0x100,
741 /* Primary DAC shared with main CLFE */
742 BAD_SHARED_CLFE = 0x10,
743 /* Primary DAC shared with extra surrounds */
744 BAD_SHARED_EXTRA_SURROUND = 0x10,
745 /* Volume widget is shared */
746 BAD_SHARED_VOL = 0x10,
747};
748
749/* look for widgets in the path between the given NIDs appropriate for
750 * volume and mute controls, and assign the values to ctls[].
751 *
752 * When no appropriate widget is found in the path, the badness value
753 * is incremented depending on the situation. The function returns the
754 * total badness for both volume and mute controls.
755 */
756static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
757 hda_nid_t dac)
758{
759 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
760 hda_nid_t nid;
761 unsigned int val;
762 int badness = 0;
763
764 if (!path)
765 return BAD_SHARED_VOL * 2;
766 nid = look_for_out_vol_nid(codec, path);
767 if (nid) {
768 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
769 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
770 badness += BAD_SHARED_VOL;
771 else
772 path->ctls[NID_PATH_VOL_CTL] = val;
773 } else
774 badness += BAD_SHARED_VOL;
775 nid = look_for_out_mute_nid(codec, path);
776 if (nid) {
777 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
778 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
779 nid_has_mute(codec, nid, HDA_OUTPUT))
780 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
781 else
782 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
783 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
784 badness += BAD_SHARED_VOL;
785 else
786 path->ctls[NID_PATH_MUTE_CTL] = val;
787 } else
788 badness += BAD_SHARED_VOL;
789 return badness;
790}
791
792struct badness_table {
793 int no_primary_dac; /* no primary DAC */
794 int no_dac; /* no secondary DACs */
795 int shared_primary; /* primary DAC is shared with main output */
796 int shared_surr; /* secondary DAC shared with main or primary */
797 int shared_clfe; /* third DAC shared with main or primary */
798 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
799};
800
801static struct badness_table main_out_badness = {
802 .no_primary_dac = BAD_NO_PRIMARY_DAC,
803 .no_dac = BAD_NO_DAC,
804 .shared_primary = BAD_NO_PRIMARY_DAC,
805 .shared_surr = BAD_SHARED_SURROUND,
806 .shared_clfe = BAD_SHARED_CLFE,
807 .shared_surr_main = BAD_SHARED_SURROUND,
808};
809
810static struct badness_table extra_out_badness = {
811 .no_primary_dac = BAD_NO_DAC,
812 .no_dac = BAD_NO_DAC,
813 .shared_primary = BAD_NO_EXTRA_DAC,
814 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
815 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
816 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
817};
818
819/* try to assign DACs to pins and return the resultant badness */
820static int try_assign_dacs(struct hda_codec *codec, int num_outs,
821 const hda_nid_t *pins, hda_nid_t *dacs,
822 const struct badness_table *bad)
823{
824 struct hda_gen_spec *spec = codec->spec;
825 struct auto_pin_cfg *cfg = &spec->autocfg;
826 int i, j;
827 int badness = 0;
828 hda_nid_t dac;
829
830 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return 0;
832
Takashi Iwai352f7f92012-12-19 12:52:06 +0100833 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100834 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100835 hda_nid_t pin = pins[i];
836 if (!dacs[i])
837 dacs[i] = look_for_dac(codec, pin, false);
838 if (!dacs[i] && !i) {
839 for (j = 1; j < num_outs; j++) {
840 if (is_reachable_path(codec, dacs[j], pin)) {
841 dacs[0] = dacs[j];
842 dacs[j] = 0;
843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100846 }
847 dac = dacs[i];
848 if (!dac) {
849 if (is_reachable_path(codec, dacs[0], pin))
850 dac = dacs[0];
851 else if (cfg->line_outs > i &&
852 is_reachable_path(codec, spec->private_dac_nids[i], pin))
853 dac = spec->private_dac_nids[i];
854 if (dac) {
855 if (!i)
856 badness += bad->shared_primary;
857 else if (i == 1)
858 badness += bad->shared_surr;
859 else
860 badness += bad->shared_clfe;
861 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
862 dac = spec->private_dac_nids[0];
863 badness += bad->shared_surr_main;
864 } else if (!i)
865 badness += bad->no_primary_dac;
866 else
867 badness += bad->no_dac;
868 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100869 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
Takashi Iwaib3a8c742012-12-20 18:29:16 +0100870 if (!path && i > 0 && spec->mixer_nid) {
871 /* try with aamix */
872 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
873 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100874 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100875 dac = dacs[i] = 0;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100876 else
877 print_nid_path("output", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100878 if (dac)
879 badness += assign_out_path_ctls(codec, pin, dac);
880 }
881
882 return badness;
883}
884
885/* return NID if the given pin has only a single connection to a certain DAC */
886static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
887{
888 struct hda_gen_spec *spec = codec->spec;
889 int i;
890 hda_nid_t nid_found = 0;
891
892 for (i = 0; i < spec->num_all_dacs; i++) {
893 hda_nid_t nid = spec->all_dacs[i];
894 if (!nid || is_dac_already_used(codec, nid))
895 continue;
896 if (is_reachable_path(codec, nid, pin)) {
897 if (nid_found)
898 return 0;
899 nid_found = nid;
900 }
901 }
902 return nid_found;
903}
904
905/* check whether the given pin can be a multi-io pin */
906static bool can_be_multiio_pin(struct hda_codec *codec,
907 unsigned int location, hda_nid_t nid)
908{
909 unsigned int defcfg, caps;
910
911 defcfg = snd_hda_codec_get_pincfg(codec, nid);
912 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
913 return false;
914 if (location && get_defcfg_location(defcfg) != location)
915 return false;
916 caps = snd_hda_query_pin_caps(codec, nid);
917 if (!(caps & AC_PINCAP_OUT))
918 return false;
919 return true;
920}
921
922/*
923 * multi-io helper
924 *
925 * When hardwired is set, try to fill ony hardwired pins, and returns
926 * zero if any pins are filled, non-zero if nothing found.
927 * When hardwired is off, try to fill possible input pins, and returns
928 * the badness value.
929 */
930static int fill_multi_ios(struct hda_codec *codec,
931 hda_nid_t reference_pin,
932 bool hardwired, int offset)
933{
934 struct hda_gen_spec *spec = codec->spec;
935 struct auto_pin_cfg *cfg = &spec->autocfg;
936 int type, i, j, dacs, num_pins, old_pins;
937 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
938 unsigned int location = get_defcfg_location(defcfg);
939 int badness = 0;
940
941 old_pins = spec->multi_ios;
942 if (old_pins >= 2)
943 goto end_fill;
944
945 num_pins = 0;
946 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
947 for (i = 0; i < cfg->num_inputs; i++) {
948 if (cfg->inputs[i].type != type)
949 continue;
950 if (can_be_multiio_pin(codec, location,
951 cfg->inputs[i].pin))
952 num_pins++;
953 }
954 }
955 if (num_pins < 2)
956 goto end_fill;
957
958 dacs = spec->multiout.num_dacs;
959 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
960 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100961 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100962 hda_nid_t nid = cfg->inputs[i].pin;
963 hda_nid_t dac = 0;
964
965 if (cfg->inputs[i].type != type)
966 continue;
967 if (!can_be_multiio_pin(codec, location, nid))
968 continue;
969 for (j = 0; j < spec->multi_ios; j++) {
970 if (nid == spec->multi_io[j].pin)
971 break;
972 }
973 if (j < spec->multi_ios)
974 continue;
975
976 if (offset && offset + spec->multi_ios < dacs) {
977 dac = spec->private_dac_nids[offset + spec->multi_ios];
978 if (!is_reachable_path(codec, dac, nid))
979 dac = 0;
980 }
981 if (hardwired)
982 dac = get_dac_if_single(codec, nid);
983 else if (!dac)
984 dac = look_for_dac(codec, nid, false);
985 if (!dac) {
986 badness++;
987 continue;
988 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100989 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100990 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100991 badness++;
992 continue;
993 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100994 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100995 spec->multi_io[spec->multi_ios].pin = nid;
996 spec->multi_io[spec->multi_ios].dac = dac;
997 spec->multi_ios++;
998 if (spec->multi_ios >= 2)
999 break;
1000 }
1001 }
1002 end_fill:
1003 if (badness)
1004 badness = BAD_MULTI_IO;
1005 if (old_pins == spec->multi_ios) {
1006 if (hardwired)
1007 return 1; /* nothing found */
1008 else
1009 return badness; /* no badness if nothing found */
1010 }
1011 if (!hardwired && spec->multi_ios < 2) {
1012 /* cancel newly assigned paths */
1013 spec->paths.used -= spec->multi_ios - old_pins;
1014 spec->multi_ios = old_pins;
1015 return badness;
1016 }
1017
1018 /* assign volume and mute controls */
1019 for (i = old_pins; i < spec->multi_ios; i++)
1020 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1021 spec->multi_io[i].dac);
1022
1023 return badness;
1024}
1025
1026/* map DACs for all pins in the list if they are single connections */
1027static bool map_singles(struct hda_codec *codec, int outs,
1028 const hda_nid_t *pins, hda_nid_t *dacs)
1029{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001030 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001031 int i;
1032 bool found = false;
1033 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001034 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001035 hda_nid_t dac;
1036 if (dacs[i])
1037 continue;
1038 dac = get_dac_if_single(codec, pins[i]);
1039 if (!dac)
1040 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001041 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001042 if (!path && i > 0 && spec->mixer_nid)
1043 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001044 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001045 dacs[i] = dac;
1046 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001047 print_nid_path("output", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001048 }
1049 }
1050 return found;
1051}
1052
1053/* fill in the dac_nids table from the parsed pin configuration */
1054static int fill_and_eval_dacs(struct hda_codec *codec,
1055 bool fill_hardwired,
1056 bool fill_mio_first)
1057{
1058 struct hda_gen_spec *spec = codec->spec;
1059 struct auto_pin_cfg *cfg = &spec->autocfg;
1060 int i, err, badness;
1061
1062 /* set num_dacs once to full for look_for_dac() */
1063 spec->multiout.num_dacs = cfg->line_outs;
1064 spec->multiout.dac_nids = spec->private_dac_nids;
1065 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1066 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1067 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1068 spec->multi_ios = 0;
1069 snd_array_free(&spec->paths);
1070 badness = 0;
1071
1072 /* fill hard-wired DACs first */
1073 if (fill_hardwired) {
1074 bool mapped;
1075 do {
1076 mapped = map_singles(codec, cfg->line_outs,
1077 cfg->line_out_pins,
1078 spec->private_dac_nids);
1079 mapped |= map_singles(codec, cfg->hp_outs,
1080 cfg->hp_pins,
1081 spec->multiout.hp_out_nid);
1082 mapped |= map_singles(codec, cfg->speaker_outs,
1083 cfg->speaker_pins,
1084 spec->multiout.extra_out_nid);
1085 if (fill_mio_first && cfg->line_outs == 1 &&
1086 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1087 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1088 if (!err)
1089 mapped = true;
1090 }
1091 } while (mapped);
1092 }
1093
1094 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1095 spec->private_dac_nids,
1096 &main_out_badness);
1097
1098 /* re-count num_dacs and squash invalid entries */
1099 spec->multiout.num_dacs = 0;
1100 for (i = 0; i < cfg->line_outs; i++) {
1101 if (spec->private_dac_nids[i])
1102 spec->multiout.num_dacs++;
1103 else {
1104 memmove(spec->private_dac_nids + i,
1105 spec->private_dac_nids + i + 1,
1106 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1107 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1108 }
1109 }
1110
1111 if (fill_mio_first &&
1112 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1113 /* try to fill multi-io first */
1114 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1115 if (err < 0)
1116 return err;
1117 /* we don't count badness at this stage yet */
1118 }
1119
1120 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1121 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1122 spec->multiout.hp_out_nid,
1123 &extra_out_badness);
1124 if (err < 0)
1125 return err;
1126 badness += err;
1127 }
1128 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1129 err = try_assign_dacs(codec, cfg->speaker_outs,
1130 cfg->speaker_pins,
1131 spec->multiout.extra_out_nid,
1132 &extra_out_badness);
1133 if (err < 0)
1134 return err;
1135 badness += err;
1136 }
1137 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1138 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1139 if (err < 0)
1140 return err;
1141 badness += err;
1142 }
1143 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1144 /* try multi-ios with HP + inputs */
1145 int offset = 0;
1146 if (cfg->line_outs >= 3)
1147 offset = 1;
1148 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1149 if (err < 0)
1150 return err;
1151 badness += err;
1152 }
1153
1154 if (spec->multi_ios == 2) {
1155 for (i = 0; i < 2; i++)
1156 spec->private_dac_nids[spec->multiout.num_dacs++] =
1157 spec->multi_io[i].dac;
1158 spec->ext_channel_count = 2;
1159 } else if (spec->multi_ios) {
1160 spec->multi_ios = 0;
1161 badness += BAD_MULTI_IO;
1162 }
1163
1164 return badness;
1165}
1166
1167#define DEBUG_BADNESS
1168
1169#ifdef DEBUG_BADNESS
1170#define debug_badness snd_printdd
1171#else
1172#define debug_badness(...)
1173#endif
1174
1175static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1176{
1177 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1178 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001179 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001180 spec->multiout.dac_nids[0],
1181 spec->multiout.dac_nids[1],
1182 spec->multiout.dac_nids[2],
1183 spec->multiout.dac_nids[3]);
1184 if (spec->multi_ios > 0)
1185 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1186 spec->multi_ios,
1187 spec->multi_io[0].pin, spec->multi_io[1].pin,
1188 spec->multi_io[0].dac, spec->multi_io[1].dac);
1189 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1190 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001191 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001192 spec->multiout.hp_out_nid[0],
1193 spec->multiout.hp_out_nid[1],
1194 spec->multiout.hp_out_nid[2],
1195 spec->multiout.hp_out_nid[3]);
1196 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1197 cfg->speaker_pins[0], cfg->speaker_pins[1],
1198 cfg->speaker_pins[2], cfg->speaker_pins[3],
1199 spec->multiout.extra_out_nid[0],
1200 spec->multiout.extra_out_nid[1],
1201 spec->multiout.extra_out_nid[2],
1202 spec->multiout.extra_out_nid[3]);
1203}
1204
1205/* find all available DACs of the codec */
1206static void fill_all_dac_nids(struct hda_codec *codec)
1207{
1208 struct hda_gen_spec *spec = codec->spec;
1209 int i;
1210 hda_nid_t nid = codec->start_nid;
1211
1212 spec->num_all_dacs = 0;
1213 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1214 for (i = 0; i < codec->num_nodes; i++, nid++) {
1215 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1216 continue;
1217 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1218 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1219 break;
1220 }
1221 spec->all_dacs[spec->num_all_dacs++] = nid;
1222 }
1223}
1224
1225static int parse_output_paths(struct hda_codec *codec)
1226{
1227 struct hda_gen_spec *spec = codec->spec;
1228 struct auto_pin_cfg *cfg = &spec->autocfg;
1229 struct auto_pin_cfg *best_cfg;
1230 int best_badness = INT_MAX;
1231 int badness;
1232 bool fill_hardwired = true, fill_mio_first = true;
1233 bool best_wired = true, best_mio = true;
1234 bool hp_spk_swapped = false;
1235
1236 fill_all_dac_nids(codec);
1237
1238 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1239 if (!best_cfg)
1240 return -ENOMEM;
1241 *best_cfg = *cfg;
1242
1243 for (;;) {
1244 badness = fill_and_eval_dacs(codec, fill_hardwired,
1245 fill_mio_first);
1246 if (badness < 0) {
1247 kfree(best_cfg);
1248 return badness;
1249 }
1250 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1251 cfg->line_out_type, fill_hardwired, fill_mio_first,
1252 badness);
1253 debug_show_configs(spec, cfg);
1254 if (badness < best_badness) {
1255 best_badness = badness;
1256 *best_cfg = *cfg;
1257 best_wired = fill_hardwired;
1258 best_mio = fill_mio_first;
1259 }
1260 if (!badness)
1261 break;
1262 fill_mio_first = !fill_mio_first;
1263 if (!fill_mio_first)
1264 continue;
1265 fill_hardwired = !fill_hardwired;
1266 if (!fill_hardwired)
1267 continue;
1268 if (hp_spk_swapped)
1269 break;
1270 hp_spk_swapped = true;
1271 if (cfg->speaker_outs > 0 &&
1272 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1273 cfg->hp_outs = cfg->line_outs;
1274 memcpy(cfg->hp_pins, cfg->line_out_pins,
1275 sizeof(cfg->hp_pins));
1276 cfg->line_outs = cfg->speaker_outs;
1277 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1278 sizeof(cfg->speaker_pins));
1279 cfg->speaker_outs = 0;
1280 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1281 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1282 fill_hardwired = true;
1283 continue;
1284 }
1285 if (cfg->hp_outs > 0 &&
1286 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1287 cfg->speaker_outs = cfg->line_outs;
1288 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1289 sizeof(cfg->speaker_pins));
1290 cfg->line_outs = cfg->hp_outs;
1291 memcpy(cfg->line_out_pins, cfg->hp_pins,
1292 sizeof(cfg->hp_pins));
1293 cfg->hp_outs = 0;
1294 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1295 cfg->line_out_type = AUTO_PIN_HP_OUT;
1296 fill_hardwired = true;
1297 continue;
1298 }
1299 break;
1300 }
1301
1302 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001303 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001304 *cfg = *best_cfg;
1305 fill_and_eval_dacs(codec, best_wired, best_mio);
1306 }
1307 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1308 cfg->line_out_type, best_wired, best_mio);
1309 debug_show_configs(spec, cfg);
1310
1311 if (cfg->line_out_pins[0]) {
1312 struct nid_path *path;
1313 path = snd_hda_get_nid_path(codec,
1314 spec->multiout.dac_nids[0],
1315 cfg->line_out_pins[0]);
1316 if (path)
1317 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1318 }
1319
1320 kfree(best_cfg);
1321 return 0;
1322}
1323
1324/* add playback controls from the parsed DAC table */
1325static int create_multi_out_ctls(struct hda_codec *codec,
1326 const struct auto_pin_cfg *cfg)
1327{
1328 struct hda_gen_spec *spec = codec->spec;
1329 int i, err, noutputs;
1330
1331 noutputs = cfg->line_outs;
1332 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1333 noutputs += spec->multi_ios;
1334
1335 for (i = 0; i < noutputs; i++) {
1336 const char *name;
1337 int index;
1338 hda_nid_t dac, pin;
1339 struct nid_path *path;
1340
1341 dac = spec->multiout.dac_nids[i];
1342 if (!dac)
1343 continue;
1344 if (i >= cfg->line_outs) {
1345 pin = spec->multi_io[i - 1].pin;
1346 index = 0;
1347 name = channel_name[i];
1348 } else {
1349 pin = cfg->line_out_pins[i];
1350 name = get_line_out_pfx(spec, i, true, &index);
1351 }
1352
1353 path = snd_hda_get_nid_path(codec, dac, pin);
1354 if (!path)
1355 continue;
1356 if (!name || !strcmp(name, "CLFE")) {
1357 /* Center/LFE */
1358 err = add_vol_ctl(codec, "Center", 0, 1, path);
1359 if (err < 0)
1360 return err;
1361 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1362 if (err < 0)
1363 return err;
1364 err = add_sw_ctl(codec, "Center", 0, 1, path);
1365 if (err < 0)
1366 return err;
1367 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1368 if (err < 0)
1369 return err;
1370 } else {
1371 err = add_stereo_vol(codec, name, index, path);
1372 if (err < 0)
1373 return err;
1374 err = add_stereo_sw(codec, name, index, path);
1375 if (err < 0)
1376 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378 }
1379 return 0;
1380}
1381
Takashi Iwai352f7f92012-12-19 12:52:06 +01001382static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1383 hda_nid_t dac, const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001385 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 int err;
1387
Takashi Iwai352f7f92012-12-19 12:52:06 +01001388 path = snd_hda_get_nid_path(codec, dac, pin);
1389 if (!path)
1390 return 0;
1391 /* bind volume control will be created in the case of dac = 0 */
1392 if (dac) {
1393 err = add_stereo_vol(codec, pfx, cidx, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (err < 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001395 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001397 err = add_stereo_sw(codec, pfx, cidx, path);
1398 if (err < 0)
1399 return err;
1400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Takashi Iwai352f7f92012-12-19 12:52:06 +01001403/* add playback controls for speaker and HP outputs */
1404static int create_extra_outs(struct hda_codec *codec, int num_pins,
1405 const hda_nid_t *pins, const hda_nid_t *dacs,
1406 const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001408 struct hda_gen_spec *spec = codec->spec;
1409 struct hda_bind_ctls *ctl;
1410 char name[32];
1411 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Takashi Iwai352f7f92012-12-19 12:52:06 +01001413 if (!num_pins || !pins[0])
1414 return 0;
1415
1416 if (num_pins == 1) {
1417 hda_nid_t dac = *dacs;
1418 if (!dac)
1419 dac = spec->multiout.dac_nids[0];
1420 return create_extra_out(codec, *pins, dac, pfx, 0);
Takashi Iwai97ec5582006-03-21 11:29:07 +01001421 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001422
1423 for (i = 0; i < num_pins; i++) {
1424 hda_nid_t dac;
1425 if (dacs[num_pins - 1])
1426 dac = dacs[i]; /* with individual volumes */
Takashi Iwai97ec5582006-03-21 11:29:07 +01001427 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001428 dac = 0;
1429 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1430 err = create_extra_out(codec, pins[i], dac,
1431 "Bass Speaker", 0);
1432 } else if (num_pins >= 3) {
1433 snprintf(name, sizeof(name), "%s %s",
1434 pfx, channel_name[i]);
1435 err = create_extra_out(codec, pins[i], dac, name, 0);
1436 } else {
1437 err = create_extra_out(codec, pins[i], dac, pfx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001439 if (err < 0)
1440 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001442 if (dacs[num_pins - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return 0;
1444
Takashi Iwai352f7f92012-12-19 12:52:06 +01001445 /* Let's create a bind-controls for volumes */
1446 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1447 if (!ctl)
1448 return -ENOMEM;
1449 n = 0;
1450 for (i = 0; i < num_pins; i++) {
1451 hda_nid_t vol;
1452 struct nid_path *path;
1453 if (!pins[i] || !dacs[i])
1454 continue;
1455 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1456 if (!path)
1457 continue;
1458 vol = look_for_out_vol_nid(codec, path);
1459 if (vol)
1460 ctl->values[n++] =
1461 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1462 }
1463 if (n) {
1464 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1465 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1466 if (err < 0)
1467 return err;
1468 }
1469 return 0;
1470}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001471
Takashi Iwai352f7f92012-12-19 12:52:06 +01001472static int create_hp_out_ctls(struct hda_codec *codec)
1473{
1474 struct hda_gen_spec *spec = codec->spec;
1475 return create_extra_outs(codec, spec->autocfg.hp_outs,
1476 spec->autocfg.hp_pins,
1477 spec->multiout.hp_out_nid,
1478 "Headphone");
1479}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Takashi Iwai352f7f92012-12-19 12:52:06 +01001481static int create_speaker_out_ctls(struct hda_codec *codec)
1482{
1483 struct hda_gen_spec *spec = codec->spec;
1484 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1485 spec->autocfg.speaker_pins,
1486 spec->multiout.extra_out_nid,
1487 "Speaker");
1488}
1489
1490/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001491 * independent HP controls
1492 */
1493
1494static int indep_hp_info(struct snd_kcontrol *kcontrol,
1495 struct snd_ctl_elem_info *uinfo)
1496{
1497 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1498}
1499
1500static int indep_hp_get(struct snd_kcontrol *kcontrol,
1501 struct snd_ctl_elem_value *ucontrol)
1502{
1503 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1504 struct hda_gen_spec *spec = codec->spec;
1505 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1506 return 0;
1507}
1508
1509static int indep_hp_put(struct snd_kcontrol *kcontrol,
1510 struct snd_ctl_elem_value *ucontrol)
1511{
1512 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1513 struct hda_gen_spec *spec = codec->spec;
1514 unsigned int select = ucontrol->value.enumerated.item[0];
1515 int ret = 0;
1516
1517 mutex_lock(&spec->pcm_mutex);
1518 if (spec->active_streams) {
1519 ret = -EBUSY;
1520 goto unlock;
1521 }
1522
1523 if (spec->indep_hp_enabled != select) {
1524 spec->indep_hp_enabled = select;
1525 if (spec->indep_hp_enabled)
1526 spec->multiout.hp_out_nid[0] = 0;
1527 else
1528 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1529 ret = 1;
1530 }
1531 unlock:
1532 mutex_unlock(&spec->pcm_mutex);
1533 return ret;
1534}
1535
1536static const struct snd_kcontrol_new indep_hp_ctl = {
1537 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1538 .name = "Independent HP",
1539 .info = indep_hp_info,
1540 .get = indep_hp_get,
1541 .put = indep_hp_put,
1542};
1543
1544
1545static int create_indep_hp_ctls(struct hda_codec *codec)
1546{
1547 struct hda_gen_spec *spec = codec->spec;
1548
1549 if (!spec->indep_hp)
1550 return 0;
1551 if (!spec->multiout.hp_out_nid[0]) {
1552 spec->indep_hp = 0;
1553 return 0;
1554 }
1555
1556 spec->indep_hp_enabled = false;
1557 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1558 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1559 return -ENOMEM;
1560 return 0;
1561}
1562
1563/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001564 * channel mode enum control
1565 */
1566
1567static int ch_mode_info(struct snd_kcontrol *kcontrol,
1568 struct snd_ctl_elem_info *uinfo)
1569{
1570 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1571 struct hda_gen_spec *spec = codec->spec;
1572
1573 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1574 uinfo->count = 1;
1575 uinfo->value.enumerated.items = spec->multi_ios + 1;
1576 if (uinfo->value.enumerated.item > spec->multi_ios)
1577 uinfo->value.enumerated.item = spec->multi_ios;
1578 sprintf(uinfo->value.enumerated.name, "%dch",
1579 (uinfo->value.enumerated.item + 1) * 2);
1580 return 0;
1581}
1582
1583static int ch_mode_get(struct snd_kcontrol *kcontrol,
1584 struct snd_ctl_elem_value *ucontrol)
1585{
1586 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1587 struct hda_gen_spec *spec = codec->spec;
1588 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1589 return 0;
1590}
1591
1592static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1593{
1594 struct hda_gen_spec *spec = codec->spec;
1595 hda_nid_t nid = spec->multi_io[idx].pin;
1596 struct nid_path *path;
1597
1598 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1599 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001601
1602 if (path->active == output)
1603 return 0;
1604
1605 if (output) {
1606 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1607 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001608 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001609 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001610 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001611 snd_hda_activate_path(codec, path, false, true);
1612 snd_hda_set_pin_ctl_cache(codec, nid,
1613 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
Takashi Iwai352f7f92012-12-19 12:52:06 +01001618static int ch_mode_put(struct snd_kcontrol *kcontrol,
1619 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001621 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1622 struct hda_gen_spec *spec = codec->spec;
1623 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Takashi Iwai352f7f92012-12-19 12:52:06 +01001625 ch = ucontrol->value.enumerated.item[0];
1626 if (ch < 0 || ch > spec->multi_ios)
1627 return -EINVAL;
1628 if (ch == (spec->ext_channel_count - 1) / 2)
1629 return 0;
1630 spec->ext_channel_count = (ch + 1) * 2;
1631 for (i = 0; i < spec->multi_ios; i++)
1632 set_multi_io(codec, i, i < ch);
1633 spec->multiout.max_channels = max(spec->ext_channel_count,
1634 spec->const_channel_count);
1635 if (spec->need_dac_fix)
1636 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return 1;
1638}
1639
Takashi Iwai352f7f92012-12-19 12:52:06 +01001640static const struct snd_kcontrol_new channel_mode_enum = {
1641 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1642 .name = "Channel Mode",
1643 .info = ch_mode_info,
1644 .get = ch_mode_get,
1645 .put = ch_mode_put,
1646};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Takashi Iwai352f7f92012-12-19 12:52:06 +01001648static int create_multi_channel_mode(struct hda_codec *codec)
1649{
1650 struct hda_gen_spec *spec = codec->spec;
1651
1652 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001653 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001654 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return 0;
1657}
1658
Takashi Iwai352f7f92012-12-19 12:52:06 +01001659/*
1660 * shared headphone/mic handling
1661 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001662
Takashi Iwai352f7f92012-12-19 12:52:06 +01001663static void call_update_outputs(struct hda_codec *codec);
1664
1665/* for shared I/O, change the pin-control accordingly */
1666static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1667{
1668 struct hda_gen_spec *spec = codec->spec;
1669 unsigned int val;
1670 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1671 /* NOTE: this assumes that there are only two inputs, the
1672 * first is the real internal mic and the second is HP/mic jack.
1673 */
1674
1675 val = snd_hda_get_default_vref(codec, pin);
1676
1677 /* This pin does not have vref caps - let's enable vref on pin 0x18
1678 instead, as suggested by Realtek */
1679 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1680 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1681 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1682 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001683 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1684 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001685 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001686
1687 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001688 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001689
1690 spec->automute_speaker = !set_as_mic;
1691 call_update_outputs(codec);
1692}
1693
1694/* create a shared input with the headphone out */
1695static int create_shared_input(struct hda_codec *codec)
1696{
1697 struct hda_gen_spec *spec = codec->spec;
1698 struct auto_pin_cfg *cfg = &spec->autocfg;
1699 unsigned int defcfg;
1700 hda_nid_t nid;
1701
1702 /* only one internal input pin? */
1703 if (cfg->num_inputs != 1)
1704 return 0;
1705 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1706 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1707 return 0;
1708
1709 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1710 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1711 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1712 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1713 else
1714 return 0; /* both not available */
1715
1716 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1717 return 0; /* no input */
1718
1719 cfg->inputs[1].pin = nid;
1720 cfg->inputs[1].type = AUTO_PIN_MIC;
1721 cfg->num_inputs = 2;
1722 spec->shared_mic_hp = 1;
1723 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1724 return 0;
1725}
1726
1727
1728/*
1729 * Parse input paths
1730 */
1731
1732#ifdef CONFIG_PM
1733/* add the powersave loopback-list entry */
1734static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1735{
1736 struct hda_amp_list *list;
1737
1738 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1739 return;
1740 list = spec->loopback_list + spec->num_loopbacks;
1741 list->nid = mix;
1742 list->dir = HDA_INPUT;
1743 list->idx = idx;
1744 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001745 spec->loopback.amplist = spec->loopback_list;
1746}
1747#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001748#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001749#endif
1750
Takashi Iwai352f7f92012-12-19 12:52:06 +01001751/* create input playback/capture controls for the given pin */
1752static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1753 const char *ctlname, int ctlidx,
1754 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001756 struct hda_gen_spec *spec = codec->spec;
1757 struct nid_path *path;
1758 unsigned int val;
1759 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Takashi Iwai352f7f92012-12-19 12:52:06 +01001761 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1762 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1763 return 0; /* no need for analog loopback */
1764
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001765 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001766 if (!path)
1767 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001768 print_nid_path("loopback", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001769
1770 idx = path->idx[path->depth - 1];
1771 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1772 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1773 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001774 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001776 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778
Takashi Iwai352f7f92012-12-19 12:52:06 +01001779 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1780 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1781 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001782 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001784 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786
Takashi Iwai352f7f92012-12-19 12:52:06 +01001787 path->active = true;
1788 add_loopback_list(spec, mix_nid, idx);
1789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
Takashi Iwai352f7f92012-12-19 12:52:06 +01001792static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001794 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1795 return (pincap & AC_PINCAP_IN) != 0;
1796}
1797
1798/* Parse the codec tree and retrieve ADCs */
1799static int fill_adc_nids(struct hda_codec *codec)
1800{
1801 struct hda_gen_spec *spec = codec->spec;
1802 hda_nid_t nid;
1803 hda_nid_t *adc_nids = spec->adc_nids;
1804 int max_nums = ARRAY_SIZE(spec->adc_nids);
1805 int i, nums = 0;
1806
1807 nid = codec->start_nid;
1808 for (i = 0; i < codec->num_nodes; i++, nid++) {
1809 unsigned int caps = get_wcaps(codec, nid);
1810 int type = get_wcaps_type(caps);
1811
1812 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1813 continue;
1814 adc_nids[nums] = nid;
1815 if (++nums >= max_nums)
1816 break;
1817 }
1818 spec->num_adc_nids = nums;
1819 return nums;
1820}
1821
1822/* filter out invalid adc_nids that don't give all active input pins;
1823 * if needed, check whether dynamic ADC-switching is available
1824 */
1825static int check_dyn_adc_switch(struct hda_codec *codec)
1826{
1827 struct hda_gen_spec *spec = codec->spec;
1828 struct hda_input_mux *imux = &spec->input_mux;
1829 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1830 int i, n, nums;
1831 hda_nid_t pin, adc;
1832
1833 again:
1834 nums = 0;
1835 for (n = 0; n < spec->num_adc_nids; n++) {
1836 adc = spec->adc_nids[n];
1837 for (i = 0; i < imux->num_items; i++) {
1838 pin = spec->imux_pins[i];
1839 if (!is_reachable_path(codec, pin, adc))
1840 break;
1841 }
1842 if (i >= imux->num_items)
1843 adc_nids[nums++] = adc;
1844 }
1845
1846 if (!nums) {
1847 if (spec->shared_mic_hp) {
1848 spec->shared_mic_hp = 0;
1849 imux->num_items = 1;
1850 goto again;
1851 }
1852
1853 /* check whether ADC-switch is possible */
1854 for (i = 0; i < imux->num_items; i++) {
1855 pin = spec->imux_pins[i];
1856 for (n = 0; n < spec->num_adc_nids; n++) {
1857 adc = spec->adc_nids[n];
1858 if (is_reachable_path(codec, pin, adc)) {
1859 spec->dyn_adc_idx[i] = n;
1860 break;
1861 }
1862 }
1863 }
1864
1865 snd_printdd("hda-codec: enabling ADC switching\n");
1866 spec->dyn_adc_switch = 1;
1867 } else if (nums != spec->num_adc_nids) {
1868 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1869 spec->num_adc_nids = nums;
1870 }
1871
1872 if (imux->num_items == 1 || spec->shared_mic_hp) {
1873 snd_printdd("hda-codec: reducing to a single ADC\n");
1874 spec->num_adc_nids = 1; /* reduce to a single ADC */
1875 }
1876
1877 /* single index for individual volumes ctls */
1878 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1879 spec->num_adc_nids = 1;
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return 0;
1882}
1883
1884/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001885 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001887static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001888{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001889 struct hda_gen_spec *spec = codec->spec;
1890 const struct auto_pin_cfg *cfg = &spec->autocfg;
1891 hda_nid_t mixer = spec->mixer_nid;
1892 struct hda_input_mux *imux = &spec->input_mux;
1893 int num_adcs;
1894 int i, c, err, type_idx = 0;
1895 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001896
Takashi Iwai352f7f92012-12-19 12:52:06 +01001897 num_adcs = fill_adc_nids(codec);
1898 if (num_adcs < 0)
1899 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001900
Takashi Iwai352f7f92012-12-19 12:52:06 +01001901 for (i = 0; i < cfg->num_inputs; i++) {
1902 hda_nid_t pin;
1903 const char *label;
1904 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Takashi Iwai352f7f92012-12-19 12:52:06 +01001906 pin = cfg->inputs[i].pin;
1907 if (!is_input_pin(codec, pin))
1908 continue;
1909
1910 label = hda_get_autocfg_input_label(codec, cfg, i);
1911 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1912 label = "Headphone Mic";
1913 if (prev_label && !strcmp(label, prev_label))
1914 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001915 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001916 type_idx = 0;
1917 prev_label = label;
1918
1919 if (mixer) {
1920 if (is_reachable_path(codec, pin, mixer)) {
1921 err = new_analog_input(codec, pin,
1922 label, type_idx, mixer);
1923 if (err < 0)
1924 return err;
1925 }
1926 }
1927
1928 imux_added = false;
1929 for (c = 0; c < num_adcs; c++) {
1930 struct nid_path *path;
1931 hda_nid_t adc = spec->adc_nids[c];
1932
1933 if (!is_reachable_path(codec, pin, adc))
1934 continue;
1935 path = snd_array_new(&spec->paths);
1936 if (!path)
1937 return -ENOMEM;
1938 memset(path, 0, sizeof(*path));
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001939 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001940 snd_printd(KERN_ERR
1941 "invalid input path 0x%x -> 0x%x\n",
1942 pin, adc);
1943 spec->paths.used--;
1944 continue;
1945 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001946 print_nid_path("input", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001947
1948 if (!imux_added) {
1949 spec->imux_pins[imux->num_items] = pin;
1950 snd_hda_add_imux_item(imux, label,
1951 imux->num_items, NULL);
1952 imux_added = true;
1953 }
1954 }
1955 }
1956
1957 return 0;
1958}
1959
1960
1961/*
1962 * input source mux
1963 */
1964
1965/* get the ADC NID corresponding to the given index */
1966static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1967{
1968 struct hda_gen_spec *spec = codec->spec;
1969 if (spec->dyn_adc_switch)
1970 adc_idx = spec->dyn_adc_idx[imux_idx];
1971 return spec->adc_nids[adc_idx];
1972}
1973
1974static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1975 unsigned int idx);
1976
1977static int mux_enum_info(struct snd_kcontrol *kcontrol,
1978 struct snd_ctl_elem_info *uinfo)
1979{
1980 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1981 struct hda_gen_spec *spec = codec->spec;
1982 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1983}
1984
1985static int mux_enum_get(struct snd_kcontrol *kcontrol,
1986 struct snd_ctl_elem_value *ucontrol)
1987{
1988 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1989 struct hda_gen_spec *spec = codec->spec;
1990 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1991
1992 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1993 return 0;
1994}
1995
1996static int mux_enum_put(struct snd_kcontrol *kcontrol,
1997 struct snd_ctl_elem_value *ucontrol)
1998{
1999 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2000 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2001 return mux_select(codec, adc_idx,
2002 ucontrol->value.enumerated.item[0]);
2003}
2004
Takashi Iwai352f7f92012-12-19 12:52:06 +01002005static const struct snd_kcontrol_new cap_src_temp = {
2006 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2007 .name = "Input Source",
2008 .info = mux_enum_info,
2009 .get = mux_enum_get,
2010 .put = mux_enum_put,
2011};
2012
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002013/*
2014 * capture volume and capture switch ctls
2015 */
2016
Takashi Iwai352f7f92012-12-19 12:52:06 +01002017typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2018 struct snd_ctl_elem_value *ucontrol);
2019
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002020/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002021static int cap_put_caller(struct snd_kcontrol *kcontrol,
2022 struct snd_ctl_elem_value *ucontrol,
2023 put_call_t func, int type)
2024{
2025 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2026 struct hda_gen_spec *spec = codec->spec;
2027 const struct hda_input_mux *imux;
2028 struct nid_path *path;
2029 int i, adc_idx, err = 0;
2030
2031 imux = &spec->input_mux;
2032 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2033 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002034 /* we use the cache-only update at first since multiple input paths
2035 * may shared the same amp; by updating only caches, the redundant
2036 * writes to hardware can be reduced.
2037 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002038 codec->cached_write = 1;
2039 for (i = 0; i < imux->num_items; i++) {
2040 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2041 get_adc_nid(codec, adc_idx, i));
2042 if (!path->ctls[type])
2043 continue;
2044 kcontrol->private_value = path->ctls[type];
2045 err = func(kcontrol, ucontrol);
2046 if (err < 0)
2047 goto error;
2048 }
2049 error:
2050 codec->cached_write = 0;
2051 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002052 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002053 if (err >= 0 && spec->cap_sync_hook)
2054 spec->cap_sync_hook(codec);
2055 return err;
2056}
2057
2058/* capture volume ctl callbacks */
2059#define cap_vol_info snd_hda_mixer_amp_volume_info
2060#define cap_vol_get snd_hda_mixer_amp_volume_get
2061#define cap_vol_tlv snd_hda_mixer_amp_tlv
2062
2063static int cap_vol_put(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_value *ucontrol)
2065{
2066 return cap_put_caller(kcontrol, ucontrol,
2067 snd_hda_mixer_amp_volume_put,
2068 NID_PATH_VOL_CTL);
2069}
2070
2071static const struct snd_kcontrol_new cap_vol_temp = {
2072 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2073 .name = "Capture Volume",
2074 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2075 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2076 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2077 .info = cap_vol_info,
2078 .get = cap_vol_get,
2079 .put = cap_vol_put,
2080 .tlv = { .c = cap_vol_tlv },
2081};
2082
2083/* capture switch ctl callbacks */
2084#define cap_sw_info snd_ctl_boolean_stereo_info
2085#define cap_sw_get snd_hda_mixer_amp_switch_get
2086
2087static int cap_sw_put(struct snd_kcontrol *kcontrol,
2088 struct snd_ctl_elem_value *ucontrol)
2089{
2090 return cap_put_caller(kcontrol, ucontrol,
2091 snd_hda_mixer_amp_switch_put,
2092 NID_PATH_MUTE_CTL);
2093}
2094
2095static const struct snd_kcontrol_new cap_sw_temp = {
2096 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2097 .name = "Capture Switch",
2098 .info = cap_sw_info,
2099 .get = cap_sw_get,
2100 .put = cap_sw_put,
2101};
2102
2103static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2104{
2105 hda_nid_t nid;
2106 int i, depth;
2107
2108 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2109 for (depth = 0; depth < 3; depth++) {
2110 if (depth >= path->depth)
2111 return -EINVAL;
2112 i = path->depth - depth - 1;
2113 nid = path->path[i];
2114 if (!path->ctls[NID_PATH_VOL_CTL]) {
2115 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2116 path->ctls[NID_PATH_VOL_CTL] =
2117 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2118 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2119 int idx = path->idx[i];
2120 if (!depth && codec->single_adc_amp)
2121 idx = 0;
2122 path->ctls[NID_PATH_VOL_CTL] =
2123 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2124 }
2125 }
2126 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2127 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2128 path->ctls[NID_PATH_MUTE_CTL] =
2129 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2130 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2131 int idx = path->idx[i];
2132 if (!depth && codec->single_adc_amp)
2133 idx = 0;
2134 path->ctls[NID_PATH_MUTE_CTL] =
2135 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2136 }
2137 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return 0;
2140}
2141
Takashi Iwai352f7f92012-12-19 12:52:06 +01002142static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002144 struct hda_gen_spec *spec = codec->spec;
2145 struct auto_pin_cfg *cfg = &spec->autocfg;
2146 unsigned int val;
2147 int i;
2148
2149 if (!spec->inv_dmic_split)
2150 return false;
2151 for (i = 0; i < cfg->num_inputs; i++) {
2152 if (cfg->inputs[i].pin != nid)
2153 continue;
2154 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2155 return false;
2156 val = snd_hda_codec_get_pincfg(codec, nid);
2157 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2158 }
2159 return false;
2160}
2161
2162static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2163 int idx, bool is_switch, unsigned int ctl,
2164 bool inv_dmic)
2165{
2166 struct hda_gen_spec *spec = codec->spec;
2167 char tmpname[44];
2168 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2169 const char *sfx = is_switch ? "Switch" : "Volume";
2170 unsigned int chs = inv_dmic ? 1 : 3;
2171 int err;
2172
2173 if (!ctl)
2174 return 0;
2175
2176 if (label)
2177 snprintf(tmpname, sizeof(tmpname),
2178 "%s Capture %s", label, sfx);
2179 else
2180 snprintf(tmpname, sizeof(tmpname),
2181 "Capture %s", sfx);
2182 err = add_control(spec, type, tmpname, idx,
2183 amp_val_replace_channels(ctl, chs));
2184 if (err < 0 || !inv_dmic)
2185 return err;
2186
2187 /* Make independent right kcontrol */
2188 if (label)
2189 snprintf(tmpname, sizeof(tmpname),
2190 "Inverted %s Capture %s", label, sfx);
2191 else
2192 snprintf(tmpname, sizeof(tmpname),
2193 "Inverted Capture %s", sfx);
2194 return add_control(spec, type, tmpname, idx,
2195 amp_val_replace_channels(ctl, 2));
2196}
2197
2198/* create single (and simple) capture volume and switch controls */
2199static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2200 unsigned int vol_ctl, unsigned int sw_ctl,
2201 bool inv_dmic)
2202{
2203 int err;
2204 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2205 if (err < 0)
2206 return err;
2207 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2208 if (err < 0)
2209 return err;
2210 return 0;
2211}
2212
2213/* create bound capture volume and switch controls */
2214static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2215 unsigned int vol_ctl, unsigned int sw_ctl)
2216{
2217 struct hda_gen_spec *spec = codec->spec;
2218 struct snd_kcontrol_new *knew;
2219
2220 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002221 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002222 if (!knew)
2223 return -ENOMEM;
2224 knew->index = idx;
2225 knew->private_value = vol_ctl;
2226 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2227 }
2228 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002229 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002230 if (!knew)
2231 return -ENOMEM;
2232 knew->index = idx;
2233 knew->private_value = sw_ctl;
2234 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2235 }
2236 return 0;
2237}
2238
2239/* return the vol ctl when used first in the imux list */
2240static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2241{
2242 struct hda_gen_spec *spec = codec->spec;
2243 struct nid_path *path;
2244 unsigned int ctl;
2245 int i;
2246
2247 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2248 get_adc_nid(codec, 0, idx));
2249 if (!path)
2250 return 0;
2251 ctl = path->ctls[type];
2252 if (!ctl)
2253 return 0;
2254 for (i = 0; i < idx - 1; i++) {
2255 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2256 get_adc_nid(codec, 0, i));
2257 if (path && path->ctls[type] == ctl)
2258 return 0;
2259 }
2260 return ctl;
2261}
2262
2263/* create individual capture volume and switch controls per input */
2264static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2265{
2266 struct hda_gen_spec *spec = codec->spec;
2267 struct hda_input_mux *imux = &spec->input_mux;
2268 int i, err, type, type_idx = 0;
2269 const char *prev_label = NULL;
2270
2271 for (i = 0; i < imux->num_items; i++) {
2272 const char *label;
2273 bool inv_dmic;
2274 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2275 if (prev_label && !strcmp(label, prev_label))
2276 type_idx++;
2277 else
2278 type_idx = 0;
2279 prev_label = label;
2280 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2281
2282 for (type = 0; type < 2; type++) {
2283 err = add_single_cap_ctl(codec, label, type_idx, type,
2284 get_first_cap_ctl(codec, i, type),
2285 inv_dmic);
2286 if (err < 0)
2287 return err;
2288 }
2289 }
2290 return 0;
2291}
2292
2293static int create_capture_mixers(struct hda_codec *codec)
2294{
2295 struct hda_gen_spec *spec = codec->spec;
2296 struct hda_input_mux *imux = &spec->input_mux;
2297 int i, n, nums, err;
2298
2299 if (spec->dyn_adc_switch)
2300 nums = 1;
2301 else
2302 nums = spec->num_adc_nids;
2303
2304 if (!spec->auto_mic && imux->num_items > 1) {
2305 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002306 const char *name;
2307 name = nums > 1 ? "Input Source" : "Capture Source";
2308 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002309 if (!knew)
2310 return -ENOMEM;
2311 knew->count = nums;
2312 }
2313
2314 for (n = 0; n < nums; n++) {
2315 bool multi = false;
2316 bool inv_dmic = false;
2317 int vol, sw;
2318
2319 vol = sw = 0;
2320 for (i = 0; i < imux->num_items; i++) {
2321 struct nid_path *path;
2322 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2323 get_adc_nid(codec, n, i));
2324 if (!path)
2325 continue;
2326 parse_capvol_in_path(codec, path);
2327 if (!vol)
2328 vol = path->ctls[NID_PATH_VOL_CTL];
2329 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2330 multi = true;
2331 if (!sw)
2332 sw = path->ctls[NID_PATH_MUTE_CTL];
2333 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2334 multi = true;
2335 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2336 inv_dmic = true;
2337 }
2338
2339 if (!multi)
2340 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2341 inv_dmic);
2342 else if (!spec->multi_cap_vol)
2343 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2344 else
2345 err = create_multi_cap_vol_ctl(codec);
2346 if (err < 0)
2347 return err;
2348 }
2349
2350 return 0;
2351}
2352
2353/*
2354 * add mic boosts if needed
2355 */
2356static int parse_mic_boost(struct hda_codec *codec)
2357{
2358 struct hda_gen_spec *spec = codec->spec;
2359 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002360 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002361 int type_idx = 0;
2362 hda_nid_t nid;
2363 const char *prev_label = NULL;
2364
2365 for (i = 0; i < cfg->num_inputs; i++) {
2366 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2367 break;
2368 nid = cfg->inputs[i].pin;
2369 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2370 const char *label;
2371 char boost_label[32];
2372 struct nid_path *path;
2373 unsigned int val;
2374
2375 label = hda_get_autocfg_input_label(codec, cfg, i);
2376 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2377 label = "Headphone Mic";
2378 if (prev_label && !strcmp(label, prev_label))
2379 type_idx++;
2380 else
2381 type_idx = 0;
2382 prev_label = label;
2383
2384 snprintf(boost_label, sizeof(boost_label),
2385 "%s Boost Volume", label);
2386 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2387 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2388 boost_label, type_idx, val);
2389 if (err < 0)
2390 return err;
2391
2392 path = snd_hda_get_nid_path(codec, nid, 0);
2393 if (path)
2394 path->ctls[NID_PATH_BOOST_CTL] = val;
2395 }
2396 }
2397 return 0;
2398}
2399
2400/*
2401 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2402 */
2403static void parse_digital(struct hda_codec *codec)
2404{
2405 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002406 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002407 int i, nums;
2408 hda_nid_t dig_nid;
2409
2410 /* support multiple SPDIFs; the secondary is set up as a slave */
2411 nums = 0;
2412 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2413 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2414 dig_nid = look_for_dac(codec, pin, true);
2415 if (!dig_nid)
2416 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002417 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002418 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002419 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002420 print_nid_path("digout", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002421 if (!nums) {
2422 spec->multiout.dig_out_nid = dig_nid;
2423 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2424 } else {
2425 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2426 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2427 break;
2428 spec->slave_dig_outs[nums - 1] = dig_nid;
2429 }
2430 nums++;
2431 }
2432
2433 if (spec->autocfg.dig_in_pin) {
2434 dig_nid = codec->start_nid;
2435 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002436 unsigned int wcaps = get_wcaps(codec, dig_nid);
2437 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2438 continue;
2439 if (!(wcaps & AC_WCAP_DIGITAL))
2440 continue;
2441 path = snd_hda_add_new_path(codec,
2442 spec->autocfg.dig_in_pin,
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002443 dig_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002444 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002445 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002446 path->active = true;
2447 spec->dig_in_nid = dig_nid;
2448 break;
2449 }
2450 }
2451 }
2452}
2453
2454
2455/*
2456 * input MUX handling
2457 */
2458
2459static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2460
2461/* select the given imux item; either unmute exclusively or select the route */
2462static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2463 unsigned int idx)
2464{
2465 struct hda_gen_spec *spec = codec->spec;
2466 const struct hda_input_mux *imux;
2467 struct nid_path *path;
2468
2469 imux = &spec->input_mux;
2470 if (!imux->num_items)
2471 return 0;
2472
2473 if (idx >= imux->num_items)
2474 idx = imux->num_items - 1;
2475 if (spec->cur_mux[adc_idx] == idx)
2476 return 0;
2477
2478 path = snd_hda_get_nid_path(codec,
2479 spec->imux_pins[spec->cur_mux[adc_idx]],
2480 spec->adc_nids[adc_idx]);
2481 if (!path)
2482 return 0;
2483 if (path->active)
2484 snd_hda_activate_path(codec, path, false, false);
2485
2486 spec->cur_mux[adc_idx] = idx;
2487
2488 if (spec->shared_mic_hp)
2489 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2490
2491 if (spec->dyn_adc_switch)
2492 dyn_adc_pcm_resetup(codec, idx);
2493
2494 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2495 get_adc_nid(codec, adc_idx, idx));
2496 if (!path)
2497 return 0;
2498 if (path->active)
2499 return 0;
2500 snd_hda_activate_path(codec, path, true, false);
2501 if (spec->cap_sync_hook)
2502 spec->cap_sync_hook(codec);
2503 return 1;
2504}
2505
2506
2507/*
2508 * Jack detections for HP auto-mute and mic-switch
2509 */
2510
2511/* check each pin in the given array; returns true if any of them is plugged */
2512static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2513{
2514 int i, present = 0;
2515
2516 for (i = 0; i < num_pins; i++) {
2517 hda_nid_t nid = pins[i];
2518 if (!nid)
2519 break;
2520 present |= snd_hda_jack_detect(codec, nid);
2521 }
2522 return present;
2523}
2524
2525/* standard HP/line-out auto-mute helper */
2526static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2527 bool mute, bool hp_out)
2528{
2529 struct hda_gen_spec *spec = codec->spec;
2530 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2531 int i;
2532
2533 for (i = 0; i < num_pins; i++) {
2534 hda_nid_t nid = pins[i];
2535 unsigned int val;
2536 if (!nid)
2537 break;
2538 /* don't reset VREF value in case it's controlling
2539 * the amp (see alc861_fixup_asus_amp_vref_0f())
2540 */
2541 if (spec->keep_vref_in_automute) {
2542 val = snd_hda_codec_read(codec, nid, 0,
2543 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2544 val &= ~PIN_HP;
2545 } else
2546 val = 0;
2547 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002548 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002549 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002550 }
2551}
2552
2553/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002554void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002555{
2556 struct hda_gen_spec *spec = codec->spec;
2557 int on;
2558
2559 /* Control HP pins/amps depending on master_mute state;
2560 * in general, HP pins/amps control should be enabled in all cases,
2561 * but currently set only for master_mute, just to be safe
2562 */
2563 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2564 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2565 spec->autocfg.hp_pins, spec->master_mute, true);
2566
2567 if (!spec->automute_speaker)
2568 on = 0;
2569 else
2570 on = spec->hp_jack_present | spec->line_jack_present;
2571 on |= spec->master_mute;
2572 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2573 spec->autocfg.speaker_pins, on, false);
2574
2575 /* toggle line-out mutes if needed, too */
2576 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2577 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2578 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2579 return;
2580 if (!spec->automute_lo)
2581 on = 0;
2582 else
2583 on = spec->hp_jack_present;
2584 on |= spec->master_mute;
2585 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2586 spec->autocfg.line_out_pins, on, false);
2587}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002588EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002589
2590static void call_update_outputs(struct hda_codec *codec)
2591{
2592 struct hda_gen_spec *spec = codec->spec;
2593 if (spec->automute_hook)
2594 spec->automute_hook(codec);
2595 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002596 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002597}
2598
2599/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002600void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002601{
2602 struct hda_gen_spec *spec = codec->spec;
2603
2604 spec->hp_jack_present =
2605 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2606 spec->autocfg.hp_pins);
2607 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2608 return;
2609 call_update_outputs(codec);
2610}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002611EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002612
2613/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002614void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002615{
2616 struct hda_gen_spec *spec = codec->spec;
2617
2618 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2619 return;
2620 /* check LO jack only when it's different from HP */
2621 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2622 return;
2623
2624 spec->line_jack_present =
2625 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2626 spec->autocfg.line_out_pins);
2627 if (!spec->automute_speaker || !spec->detect_lo)
2628 return;
2629 call_update_outputs(codec);
2630}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002631EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002632
2633/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002634void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002635{
2636 struct hda_gen_spec *spec = codec->spec;
2637 int i;
2638
2639 if (!spec->auto_mic)
2640 return;
2641
2642 for (i = spec->am_num_entries - 1; i > 0; i--) {
2643 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2644 mux_select(codec, 0, spec->am_entry[i].idx);
2645 return;
2646 }
2647 }
2648 mux_select(codec, 0, spec->am_entry[0].idx);
2649}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002650EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002651
2652/*
2653 * Auto-Mute mode mixer enum support
2654 */
2655static int automute_mode_info(struct snd_kcontrol *kcontrol,
2656 struct snd_ctl_elem_info *uinfo)
2657{
2658 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2659 struct hda_gen_spec *spec = codec->spec;
2660 static const char * const texts3[] = {
2661 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002662 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Takashi Iwai352f7f92012-12-19 12:52:06 +01002664 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2665 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2666 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2667}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Takashi Iwai352f7f92012-12-19 12:52:06 +01002669static int automute_mode_get(struct snd_kcontrol *kcontrol,
2670 struct snd_ctl_elem_value *ucontrol)
2671{
2672 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2673 struct hda_gen_spec *spec = codec->spec;
2674 unsigned int val = 0;
2675 if (spec->automute_speaker)
2676 val++;
2677 if (spec->automute_lo)
2678 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002679
Takashi Iwai352f7f92012-12-19 12:52:06 +01002680 ucontrol->value.enumerated.item[0] = val;
2681 return 0;
2682}
2683
2684static int automute_mode_put(struct snd_kcontrol *kcontrol,
2685 struct snd_ctl_elem_value *ucontrol)
2686{
2687 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2688 struct hda_gen_spec *spec = codec->spec;
2689
2690 switch (ucontrol->value.enumerated.item[0]) {
2691 case 0:
2692 if (!spec->automute_speaker && !spec->automute_lo)
2693 return 0;
2694 spec->automute_speaker = 0;
2695 spec->automute_lo = 0;
2696 break;
2697 case 1:
2698 if (spec->automute_speaker_possible) {
2699 if (!spec->automute_lo && spec->automute_speaker)
2700 return 0;
2701 spec->automute_speaker = 1;
2702 spec->automute_lo = 0;
2703 } else if (spec->automute_lo_possible) {
2704 if (spec->automute_lo)
2705 return 0;
2706 spec->automute_lo = 1;
2707 } else
2708 return -EINVAL;
2709 break;
2710 case 2:
2711 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2712 return -EINVAL;
2713 if (spec->automute_speaker && spec->automute_lo)
2714 return 0;
2715 spec->automute_speaker = 1;
2716 spec->automute_lo = 1;
2717 break;
2718 default:
2719 return -EINVAL;
2720 }
2721 call_update_outputs(codec);
2722 return 1;
2723}
2724
2725static const struct snd_kcontrol_new automute_mode_enum = {
2726 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2727 .name = "Auto-Mute Mode",
2728 .info = automute_mode_info,
2729 .get = automute_mode_get,
2730 .put = automute_mode_put,
2731};
2732
2733static int add_automute_mode_enum(struct hda_codec *codec)
2734{
2735 struct hda_gen_spec *spec = codec->spec;
2736
Takashi Iwai12c93df2012-12-19 14:38:33 +01002737 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002738 return -ENOMEM;
2739 return 0;
2740}
2741
2742/*
2743 * Check the availability of HP/line-out auto-mute;
2744 * Set up appropriately if really supported
2745 */
2746static int check_auto_mute_availability(struct hda_codec *codec)
2747{
2748 struct hda_gen_spec *spec = codec->spec;
2749 struct auto_pin_cfg *cfg = &spec->autocfg;
2750 int present = 0;
2751 int i, err;
2752
2753 if (cfg->hp_pins[0])
2754 present++;
2755 if (cfg->line_out_pins[0])
2756 present++;
2757 if (cfg->speaker_pins[0])
2758 present++;
2759 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002760 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002761
2762 if (!cfg->speaker_pins[0] &&
2763 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2764 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2765 sizeof(cfg->speaker_pins));
2766 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Takashi Iwai352f7f92012-12-19 12:52:06 +01002769 if (!cfg->hp_pins[0] &&
2770 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2771 memcpy(cfg->hp_pins, cfg->line_out_pins,
2772 sizeof(cfg->hp_pins));
2773 cfg->hp_outs = cfg->line_outs;
2774 }
2775
2776 for (i = 0; i < cfg->hp_outs; i++) {
2777 hda_nid_t nid = cfg->hp_pins[i];
2778 if (!is_jack_detectable(codec, nid))
2779 continue;
2780 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2781 nid);
2782 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002783 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002784 spec->detect_hp = 1;
2785 }
2786
2787 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2788 if (cfg->speaker_outs)
2789 for (i = 0; i < cfg->line_outs; i++) {
2790 hda_nid_t nid = cfg->line_out_pins[i];
2791 if (!is_jack_detectable(codec, nid))
2792 continue;
2793 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2794 snd_hda_jack_detect_enable_callback(codec, nid,
2795 HDA_GEN_FRONT_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002796 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002797 spec->detect_lo = 1;
2798 }
2799 spec->automute_lo_possible = spec->detect_hp;
2800 }
2801
2802 spec->automute_speaker_possible = cfg->speaker_outs &&
2803 (spec->detect_hp || spec->detect_lo);
2804
2805 spec->automute_lo = spec->automute_lo_possible;
2806 spec->automute_speaker = spec->automute_speaker_possible;
2807
2808 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2809 /* create a control for automute mode */
2810 err = add_automute_mode_enum(codec);
2811 if (err < 0)
2812 return err;
2813 }
2814 return 0;
2815}
2816
2817/* return the position of NID in the list, or -1 if not found */
2818static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2819{
2820 int i;
2821 for (i = 0; i < nums; i++)
2822 if (list[i] == nid)
2823 return i;
2824 return -1;
2825}
2826
2827/* check whether all auto-mic pins are valid; setup indices if OK */
2828static bool auto_mic_check_imux(struct hda_codec *codec)
2829{
2830 struct hda_gen_spec *spec = codec->spec;
2831 const struct hda_input_mux *imux;
2832 int i;
2833
2834 imux = &spec->input_mux;
2835 for (i = 0; i < spec->am_num_entries; i++) {
2836 spec->am_entry[i].idx =
2837 find_idx_in_nid_list(spec->am_entry[i].pin,
2838 spec->imux_pins, imux->num_items);
2839 if (spec->am_entry[i].idx < 0)
2840 return false; /* no corresponding imux */
2841 }
2842
2843 /* we don't need the jack detection for the first pin */
2844 for (i = 1; i < spec->am_num_entries; i++)
2845 snd_hda_jack_detect_enable_callback(codec,
2846 spec->am_entry[i].pin,
2847 HDA_GEN_MIC_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002848 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002849 return true;
2850}
2851
2852static int compare_attr(const void *ap, const void *bp)
2853{
2854 const struct automic_entry *a = ap;
2855 const struct automic_entry *b = bp;
2856 return (int)(a->attr - b->attr);
2857}
2858
2859/*
2860 * Check the availability of auto-mic switch;
2861 * Set up if really supported
2862 */
2863static int check_auto_mic_availability(struct hda_codec *codec)
2864{
2865 struct hda_gen_spec *spec = codec->spec;
2866 struct auto_pin_cfg *cfg = &spec->autocfg;
2867 unsigned int types;
2868 int i, num_pins;
2869
2870 types = 0;
2871 num_pins = 0;
2872 for (i = 0; i < cfg->num_inputs; i++) {
2873 hda_nid_t nid = cfg->inputs[i].pin;
2874 unsigned int attr;
2875 attr = snd_hda_codec_get_pincfg(codec, nid);
2876 attr = snd_hda_get_input_pin_attr(attr);
2877 if (types & (1 << attr))
2878 return 0; /* already occupied */
2879 switch (attr) {
2880 case INPUT_PIN_ATTR_INT:
2881 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2882 return 0; /* invalid type */
2883 break;
2884 case INPUT_PIN_ATTR_UNUSED:
2885 return 0; /* invalid entry */
2886 default:
2887 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2888 return 0; /* invalid type */
2889 if (!spec->line_in_auto_switch &&
2890 cfg->inputs[i].type != AUTO_PIN_MIC)
2891 return 0; /* only mic is allowed */
2892 if (!is_jack_detectable(codec, nid))
2893 return 0; /* no unsol support */
2894 break;
2895 }
2896 if (num_pins >= MAX_AUTO_MIC_PINS)
2897 return 0;
2898 types |= (1 << attr);
2899 spec->am_entry[num_pins].pin = nid;
2900 spec->am_entry[num_pins].attr = attr;
2901 num_pins++;
2902 }
2903
2904 if (num_pins < 2)
2905 return 0;
2906
2907 spec->am_num_entries = num_pins;
2908 /* sort the am_entry in the order of attr so that the pin with a
2909 * higher attr will be selected when the jack is plugged.
2910 */
2911 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2912 compare_attr, NULL);
2913
2914 if (!auto_mic_check_imux(codec))
2915 return 0;
2916
2917 spec->auto_mic = 1;
2918 spec->num_adc_nids = 1;
2919 spec->cur_mux[0] = spec->am_entry[0].idx;
2920 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2921 spec->am_entry[0].pin,
2922 spec->am_entry[1].pin,
2923 spec->am_entry[2].pin);
2924
2925 return 0;
2926}
2927
2928
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002929/*
2930 * Parse the given BIOS configuration and set up the hda_gen_spec
2931 *
2932 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002933 * or a negative error code
2934 */
2935int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002936 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002937{
2938 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002939 int err;
2940
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002941 if (cfg != &spec->autocfg) {
2942 spec->autocfg = *cfg;
2943 cfg = &spec->autocfg;
2944 }
2945
Takashi Iwai352f7f92012-12-19 12:52:06 +01002946 if (!cfg->line_outs) {
2947 if (cfg->dig_outs || cfg->dig_in_pin) {
2948 spec->multiout.max_channels = 2;
2949 spec->no_analog = 1;
2950 goto dig_only;
2951 }
2952 return 0; /* can't find valid BIOS pin config */
2953 }
2954
2955 if (!spec->no_primary_hp &&
2956 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2957 cfg->line_outs <= cfg->hp_outs) {
2958 /* use HP as primary out */
2959 cfg->speaker_outs = cfg->line_outs;
2960 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2961 sizeof(cfg->speaker_pins));
2962 cfg->line_outs = cfg->hp_outs;
2963 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2964 cfg->hp_outs = 0;
2965 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2966 cfg->line_out_type = AUTO_PIN_HP_OUT;
2967 }
2968
2969 err = parse_output_paths(codec);
2970 if (err < 0)
2971 return err;
2972 err = create_multi_channel_mode(codec);
2973 if (err < 0)
2974 return err;
2975 err = create_multi_out_ctls(codec, cfg);
2976 if (err < 0)
2977 return err;
2978 err = create_hp_out_ctls(codec);
2979 if (err < 0)
2980 return err;
2981 err = create_speaker_out_ctls(codec);
2982 if (err < 0)
2983 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002984 err = create_indep_hp_ctls(codec);
2985 if (err < 0)
2986 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002987 err = create_shared_input(codec);
2988 if (err < 0)
2989 return err;
2990 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002991 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02002992 return err;
2993
Takashi Iwai352f7f92012-12-19 12:52:06 +01002994 /* check the multiple speaker pins */
2995 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2996 spec->const_channel_count = cfg->line_outs * 2;
2997 else
2998 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002999
Takashi Iwai352f7f92012-12-19 12:52:06 +01003000 if (spec->multi_ios > 0)
3001 spec->multiout.max_channels = max(spec->ext_channel_count,
3002 spec->const_channel_count);
3003 else
3004 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3005
3006 err = check_auto_mute_availability(codec);
3007 if (err < 0)
3008 return err;
3009
3010 err = check_dyn_adc_switch(codec);
3011 if (err < 0)
3012 return err;
3013
3014 if (!spec->shared_mic_hp) {
3015 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003016 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003019
Takashi Iwai352f7f92012-12-19 12:52:06 +01003020 err = create_capture_mixers(codec);
3021 if (err < 0)
3022 return err;
3023
3024 err = parse_mic_boost(codec);
3025 if (err < 0)
3026 return err;
3027
3028 dig_only:
3029 parse_digital(codec);
3030
3031 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003033EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035
3036/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003037 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003039
3040/* slave controls for virtual master */
3041static const char * const slave_pfxs[] = {
3042 "Front", "Surround", "Center", "LFE", "Side",
3043 "Headphone", "Speaker", "Mono", "Line Out",
3044 "CLFE", "Bass Speaker", "PCM",
3045 NULL,
3046};
3047
3048int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003050 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Takashi Iwai36502d02012-12-19 15:15:10 +01003053 if (spec->kctls.used) {
3054 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3055 if (err < 0)
3056 return err;
3057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Takashi Iwai352f7f92012-12-19 12:52:06 +01003059 if (spec->multiout.dig_out_nid) {
3060 err = snd_hda_create_dig_out_ctls(codec,
3061 spec->multiout.dig_out_nid,
3062 spec->multiout.dig_out_nid,
3063 spec->pcm_rec[1].pcm_type);
3064 if (err < 0)
3065 return err;
3066 if (!spec->no_analog) {
3067 err = snd_hda_create_spdif_share_sw(codec,
3068 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 if (err < 0)
3070 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003071 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 }
3073 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003074 if (spec->dig_in_nid) {
3075 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3076 if (err < 0)
3077 return err;
3078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Takashi Iwai352f7f92012-12-19 12:52:06 +01003080 /* if we have no master control, let's create it */
3081 if (!spec->no_analog &&
3082 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3083 unsigned int vmaster_tlv[4];
3084 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3085 HDA_OUTPUT, vmaster_tlv);
3086 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3087 vmaster_tlv, slave_pfxs,
3088 "Playback Volume");
3089 if (err < 0)
3090 return err;
3091 }
3092 if (!spec->no_analog &&
3093 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3094 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3095 NULL, slave_pfxs,
3096 "Playback Switch",
3097 true, &spec->vmaster_mute.sw_kctl);
3098 if (err < 0)
3099 return err;
3100 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003101 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3102 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Takashi Iwai352f7f92012-12-19 12:52:06 +01003105 free_kctls(spec); /* no longer needed */
3106
3107 if (spec->shared_mic_hp) {
3108 int err;
3109 int nid = spec->autocfg.inputs[1].pin;
3110 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3111 if (err < 0)
3112 return err;
3113 err = snd_hda_jack_detect_enable(codec, nid, 0);
3114 if (err < 0)
3115 return err;
3116 }
3117
3118 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3119 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 return err;
3121
3122 return 0;
3123}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003124EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
3127/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003128 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Takashi Iwai352f7f92012-12-19 12:52:06 +01003131/*
3132 * Analog playback callbacks
3133 */
3134static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3135 struct hda_codec *codec,
3136 struct snd_pcm_substream *substream)
3137{
3138 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003139 int err;
3140
3141 mutex_lock(&spec->pcm_mutex);
3142 err = snd_hda_multi_out_analog_open(codec,
3143 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003144 hinfo);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003145 if (!err)
3146 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3147 mutex_unlock(&spec->pcm_mutex);
3148 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003149}
3150
3151static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003152 struct hda_codec *codec,
3153 unsigned int stream_tag,
3154 unsigned int format,
3155 struct snd_pcm_substream *substream)
3156{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003157 struct hda_gen_spec *spec = codec->spec;
3158 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3159 stream_tag, format, substream);
3160}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003161
Takashi Iwai352f7f92012-12-19 12:52:06 +01003162static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3163 struct hda_codec *codec,
3164 struct snd_pcm_substream *substream)
3165{
3166 struct hda_gen_spec *spec = codec->spec;
3167 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3168}
3169
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003170static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3171 struct hda_codec *codec,
3172 struct snd_pcm_substream *substream)
3173{
3174 struct hda_gen_spec *spec = codec->spec;
3175 mutex_lock(&spec->pcm_mutex);
3176 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3177 mutex_unlock(&spec->pcm_mutex);
3178 return 0;
3179}
3180
3181static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3182 struct hda_codec *codec,
3183 struct snd_pcm_substream *substream)
3184{
3185 struct hda_gen_spec *spec = codec->spec;
3186 int err = 0;
3187
3188 mutex_lock(&spec->pcm_mutex);
3189 if (!spec->indep_hp_enabled)
3190 err = -EBUSY;
3191 else
3192 spec->active_streams |= 1 << STREAM_INDEP_HP;
3193 mutex_unlock(&spec->pcm_mutex);
3194 return err;
3195}
3196
3197static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3198 struct hda_codec *codec,
3199 struct snd_pcm_substream *substream)
3200{
3201 struct hda_gen_spec *spec = codec->spec;
3202 mutex_lock(&spec->pcm_mutex);
3203 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3204 mutex_unlock(&spec->pcm_mutex);
3205 return 0;
3206}
3207
Takashi Iwai352f7f92012-12-19 12:52:06 +01003208/*
3209 * Digital out
3210 */
3211static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3212 struct hda_codec *codec,
3213 struct snd_pcm_substream *substream)
3214{
3215 struct hda_gen_spec *spec = codec->spec;
3216 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3217}
3218
3219static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3220 struct hda_codec *codec,
3221 unsigned int stream_tag,
3222 unsigned int format,
3223 struct snd_pcm_substream *substream)
3224{
3225 struct hda_gen_spec *spec = codec->spec;
3226 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3227 stream_tag, format, substream);
3228}
3229
3230static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3231 struct hda_codec *codec,
3232 struct snd_pcm_substream *substream)
3233{
3234 struct hda_gen_spec *spec = codec->spec;
3235 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3236}
3237
3238static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3239 struct hda_codec *codec,
3240 struct snd_pcm_substream *substream)
3241{
3242 struct hda_gen_spec *spec = codec->spec;
3243 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3244}
3245
3246/*
3247 * Analog capture
3248 */
3249static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3250 struct hda_codec *codec,
3251 unsigned int stream_tag,
3252 unsigned int format,
3253 struct snd_pcm_substream *substream)
3254{
3255 struct hda_gen_spec *spec = codec->spec;
3256
3257 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003258 stream_tag, 0, format);
3259 return 0;
3260}
3261
Takashi Iwai352f7f92012-12-19 12:52:06 +01003262static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3263 struct hda_codec *codec,
3264 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003265{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003266 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003267
Takashi Iwai352f7f92012-12-19 12:52:06 +01003268 snd_hda_codec_cleanup_stream(codec,
3269 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003270 return 0;
3271}
3272
Takashi Iwai352f7f92012-12-19 12:52:06 +01003273/*
3274 */
3275static const struct hda_pcm_stream pcm_analog_playback = {
3276 .substreams = 1,
3277 .channels_min = 2,
3278 .channels_max = 8,
3279 /* NID is set in build_pcms */
3280 .ops = {
3281 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003282 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003283 .prepare = playback_pcm_prepare,
3284 .cleanup = playback_pcm_cleanup
3285 },
3286};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Takashi Iwai352f7f92012-12-19 12:52:06 +01003288static const struct hda_pcm_stream pcm_analog_capture = {
3289 .substreams = 1,
3290 .channels_min = 2,
3291 .channels_max = 2,
3292 /* NID is set in build_pcms */
3293};
3294
3295static const struct hda_pcm_stream pcm_analog_alt_playback = {
3296 .substreams = 1,
3297 .channels_min = 2,
3298 .channels_max = 2,
3299 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003300 .ops = {
3301 .open = alt_playback_pcm_open,
3302 .close = alt_playback_pcm_close
3303 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003304};
3305
3306static const struct hda_pcm_stream pcm_analog_alt_capture = {
3307 .substreams = 2, /* can be overridden */
3308 .channels_min = 2,
3309 .channels_max = 2,
3310 /* NID is set in build_pcms */
3311 .ops = {
3312 .prepare = alt_capture_pcm_prepare,
3313 .cleanup = alt_capture_pcm_cleanup
3314 },
3315};
3316
3317static const struct hda_pcm_stream pcm_digital_playback = {
3318 .substreams = 1,
3319 .channels_min = 2,
3320 .channels_max = 2,
3321 /* NID is set in build_pcms */
3322 .ops = {
3323 .open = dig_playback_pcm_open,
3324 .close = dig_playback_pcm_close,
3325 .prepare = dig_playback_pcm_prepare,
3326 .cleanup = dig_playback_pcm_cleanup
3327 },
3328};
3329
3330static const struct hda_pcm_stream pcm_digital_capture = {
3331 .substreams = 1,
3332 .channels_min = 2,
3333 .channels_max = 2,
3334 /* NID is set in build_pcms */
3335};
3336
3337/* Used by build_pcms to flag that a PCM has no playback stream */
3338static const struct hda_pcm_stream pcm_null_stream = {
3339 .substreams = 0,
3340 .channels_min = 0,
3341 .channels_max = 0,
3342};
3343
3344/*
3345 * dynamic changing ADC PCM streams
3346 */
3347static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3348{
3349 struct hda_gen_spec *spec = codec->spec;
3350 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3351
3352 if (spec->cur_adc && spec->cur_adc != new_adc) {
3353 /* stream is running, let's swap the current ADC */
3354 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3355 spec->cur_adc = new_adc;
3356 snd_hda_codec_setup_stream(codec, new_adc,
3357 spec->cur_adc_stream_tag, 0,
3358 spec->cur_adc_format);
3359 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003361 return false;
3362}
3363
3364/* analog capture with dynamic dual-adc changes */
3365static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3366 struct hda_codec *codec,
3367 unsigned int stream_tag,
3368 unsigned int format,
3369 struct snd_pcm_substream *substream)
3370{
3371 struct hda_gen_spec *spec = codec->spec;
3372 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3373 spec->cur_adc_stream_tag = stream_tag;
3374 spec->cur_adc_format = format;
3375 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3376 return 0;
3377}
3378
3379static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3380 struct hda_codec *codec,
3381 struct snd_pcm_substream *substream)
3382{
3383 struct hda_gen_spec *spec = codec->spec;
3384 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3385 spec->cur_adc = 0;
3386 return 0;
3387}
3388
3389static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3390 .substreams = 1,
3391 .channels_min = 2,
3392 .channels_max = 2,
3393 .nid = 0, /* fill later */
3394 .ops = {
3395 .prepare = dyn_adc_capture_pcm_prepare,
3396 .cleanup = dyn_adc_capture_pcm_cleanup
3397 },
3398};
3399
Takashi Iwaif873e532012-12-20 16:58:39 +01003400static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3401 const char *chip_name)
3402{
3403 char *p;
3404
3405 if (*str)
3406 return;
3407 strlcpy(str, chip_name, len);
3408
3409 /* drop non-alnum chars after a space */
3410 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3411 if (!isalnum(p[1])) {
3412 *p = 0;
3413 break;
3414 }
3415 }
3416 strlcat(str, sfx, len);
3417}
3418
Takashi Iwai352f7f92012-12-19 12:52:06 +01003419/* build PCM streams based on the parsed results */
3420int snd_hda_gen_build_pcms(struct hda_codec *codec)
3421{
3422 struct hda_gen_spec *spec = codec->spec;
3423 struct hda_pcm *info = spec->pcm_rec;
3424 const struct hda_pcm_stream *p;
3425 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
3427 codec->num_pcms = 1;
3428 codec->pcm_info = info;
3429
Takashi Iwai352f7f92012-12-19 12:52:06 +01003430 if (spec->no_analog)
3431 goto skip_analog;
3432
Takashi Iwaif873e532012-12-20 16:58:39 +01003433 fill_pcm_stream_name(spec->stream_name_analog,
3434 sizeof(spec->stream_name_analog),
3435 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003436 info->name = spec->stream_name_analog;
3437
3438 if (spec->multiout.num_dacs > 0) {
3439 p = spec->stream_analog_playback;
3440 if (!p)
3441 p = &pcm_analog_playback;
3442 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3443 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3444 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3445 spec->multiout.max_channels;
3446 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3447 spec->autocfg.line_outs == 2)
3448 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3449 snd_pcm_2_1_chmaps;
3450 }
3451 if (spec->num_adc_nids) {
3452 p = spec->stream_analog_capture;
3453 if (!p) {
3454 if (spec->dyn_adc_switch)
3455 p = &dyn_adc_pcm_analog_capture;
3456 else
3457 p = &pcm_analog_capture;
3458 }
3459 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3460 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3461 }
3462
Takashi Iwai352f7f92012-12-19 12:52:06 +01003463 skip_analog:
3464 /* SPDIF for stream index #1 */
3465 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003466 fill_pcm_stream_name(spec->stream_name_digital,
3467 sizeof(spec->stream_name_digital),
3468 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003469 codec->num_pcms = 2;
3470 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3471 info = spec->pcm_rec + 1;
3472 info->name = spec->stream_name_digital;
3473 if (spec->dig_out_type)
3474 info->pcm_type = spec->dig_out_type;
3475 else
3476 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3477 if (spec->multiout.dig_out_nid) {
3478 p = spec->stream_digital_playback;
3479 if (!p)
3480 p = &pcm_digital_playback;
3481 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3482 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3483 }
3484 if (spec->dig_in_nid) {
3485 p = spec->stream_digital_capture;
3486 if (!p)
3487 p = &pcm_digital_capture;
3488 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3489 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3490 }
3491 }
3492
3493 if (spec->no_analog)
3494 return 0;
3495
3496 /* If the use of more than one ADC is requested for the current
3497 * model, configure a second analog capture-only PCM.
3498 */
3499 have_multi_adcs = (spec->num_adc_nids > 1) &&
3500 !spec->dyn_adc_switch && !spec->auto_mic;
3501 /* Additional Analaog capture for index #2 */
3502 if (spec->alt_dac_nid || have_multi_adcs) {
3503 codec->num_pcms = 3;
3504 info = spec->pcm_rec + 2;
3505 info->name = spec->stream_name_analog;
3506 if (spec->alt_dac_nid) {
3507 p = spec->stream_analog_alt_playback;
3508 if (!p)
3509 p = &pcm_analog_alt_playback;
3510 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3511 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3512 spec->alt_dac_nid;
3513 } else {
3514 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3515 pcm_null_stream;
3516 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3517 }
3518 if (have_multi_adcs) {
3519 p = spec->stream_analog_alt_capture;
3520 if (!p)
3521 p = &pcm_analog_alt_capture;
3522 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3523 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3524 spec->adc_nids[1];
3525 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3526 spec->num_adc_nids - 1;
3527 } else {
3528 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3529 pcm_null_stream;
3530 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 }
3533
3534 return 0;
3535}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003536EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3537
3538
3539/*
3540 * Standard auto-parser initializations
3541 */
3542
3543/* configure the path from the given dac to the pin as the proper output */
3544static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3545 int pin_type, hda_nid_t dac)
3546{
3547 struct nid_path *path;
3548
3549 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3550 path = snd_hda_get_nid_path(codec, dac, pin);
3551 if (!path)
3552 return;
3553 if (path->active)
3554 return;
3555 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003556 set_pin_eapd(codec, pin, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003557}
3558
3559/* initialize primary output paths */
3560static void init_multi_out(struct hda_codec *codec)
3561{
3562 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai64049c82012-12-20 15:29:21 +01003563 hda_nid_t nid, dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003564 int pin_type;
3565 int i;
3566
3567 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3568 pin_type = PIN_HP;
3569 else
3570 pin_type = PIN_OUT;
3571
Takashi Iwai64049c82012-12-20 15:29:21 +01003572 for (i = 0; i < spec->autocfg.line_outs; i++) {
3573 nid = spec->autocfg.line_out_pins[i];
3574 if (nid) {
3575 dac = spec->multiout.dac_nids[i];
3576 if (!dac)
3577 dac = spec->multiout.dac_nids[0];
3578 set_output_and_unmute(codec, nid, pin_type, dac);
3579 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003580 }
3581}
3582
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003583
3584static void __init_extra_out(struct hda_codec *codec, int num_outs,
3585 hda_nid_t *pins, hda_nid_t *dacs, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003586{
3587 struct hda_gen_spec *spec = codec->spec;
3588 int i;
3589 hda_nid_t pin, dac;
3590
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003591 for (i = 0; i < num_outs; i++) {
3592 pin = pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003593 if (!pin)
3594 break;
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003595 dac = dacs[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003596 if (!dac) {
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003597 if (i > 0 && dacs[0])
3598 dac = dacs[0];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003599 else
3600 dac = spec->multiout.dac_nids[0];
3601 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003602 set_output_and_unmute(codec, pin, type, dac);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003603 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003604}
3605
3606/* initialize hp and speaker paths */
3607static void init_extra_out(struct hda_codec *codec)
3608{
3609 struct hda_gen_spec *spec = codec->spec;
3610
3611 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3612 __init_extra_out(codec, spec->autocfg.hp_outs,
3613 spec->autocfg.hp_pins,
3614 spec->multiout.hp_out_nid, PIN_HP);
3615 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3616 __init_extra_out(codec, spec->autocfg.speaker_outs,
3617 spec->autocfg.speaker_pins,
3618 spec->multiout.extra_out_nid, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003619}
3620
3621/* initialize multi-io paths */
3622static void init_multi_io(struct hda_codec *codec)
3623{
3624 struct hda_gen_spec *spec = codec->spec;
3625 int i;
3626
3627 for (i = 0; i < spec->multi_ios; i++) {
3628 hda_nid_t pin = spec->multi_io[i].pin;
3629 struct nid_path *path;
3630 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3631 if (!path)
3632 continue;
3633 if (!spec->multi_io[i].ctl_in)
3634 spec->multi_io[i].ctl_in =
3635 snd_hda_codec_update_cache(codec, pin, 0,
3636 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3637 snd_hda_activate_path(codec, path, path->active, true);
3638 }
3639}
3640
3641/* set up the input pin config, depending on the given auto-pin type */
3642static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3643 int auto_pin_type)
3644{
3645 unsigned int val = PIN_IN;
3646 if (auto_pin_type == AUTO_PIN_MIC)
3647 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003648 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003649}
3650
3651/* set up input pins and loopback paths */
3652static void init_analog_input(struct hda_codec *codec)
3653{
3654 struct hda_gen_spec *spec = codec->spec;
3655 struct auto_pin_cfg *cfg = &spec->autocfg;
3656 int i;
3657
3658 for (i = 0; i < cfg->num_inputs; i++) {
3659 hda_nid_t nid = cfg->inputs[i].pin;
3660 if (is_input_pin(codec, nid))
3661 set_input_pin(codec, nid, cfg->inputs[i].type);
3662
3663 /* init loopback inputs */
3664 if (spec->mixer_nid) {
3665 struct nid_path *path;
3666 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3667 if (path)
3668 snd_hda_activate_path(codec, path,
3669 path->active, false);
3670 }
3671 }
3672}
3673
3674/* initialize ADC paths */
3675static void init_input_src(struct hda_codec *codec)
3676{
3677 struct hda_gen_spec *spec = codec->spec;
3678 struct hda_input_mux *imux = &spec->input_mux;
3679 struct nid_path *path;
3680 int i, c, nums;
3681
3682 if (spec->dyn_adc_switch)
3683 nums = 1;
3684 else
3685 nums = spec->num_adc_nids;
3686
3687 for (c = 0; c < nums; c++) {
3688 for (i = 0; i < imux->num_items; i++) {
3689 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3690 get_adc_nid(codec, c, i));
3691 if (path) {
3692 bool active = path->active;
3693 if (i == spec->cur_mux[c])
3694 active = true;
3695 snd_hda_activate_path(codec, path, active, false);
3696 }
3697 }
3698 }
3699
3700 if (spec->shared_mic_hp)
3701 update_shared_mic_hp(codec, spec->cur_mux[0]);
3702
3703 if (spec->cap_sync_hook)
3704 spec->cap_sync_hook(codec);
3705}
3706
3707/* set right pin controls for digital I/O */
3708static void init_digital(struct hda_codec *codec)
3709{
3710 struct hda_gen_spec *spec = codec->spec;
3711 int i;
3712 hda_nid_t pin;
3713
3714 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3715 pin = spec->autocfg.dig_out_pins[i];
3716 if (!pin)
3717 continue;
3718 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3719 }
3720 pin = spec->autocfg.dig_in_pin;
3721 if (pin)
Takashi Iwai7594aa32012-12-20 15:38:40 +01003722 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003723}
3724
Takashi Iwai973e4972012-12-20 15:16:09 +01003725/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3726 * invalid unsol tags by some reason
3727 */
3728static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3729{
3730 int i;
3731
3732 for (i = 0; i < codec->init_pins.used; i++) {
3733 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3734 hda_nid_t nid = pin->nid;
3735 if (is_jack_detectable(codec, nid) &&
3736 !snd_hda_jack_tbl_get(codec, nid))
3737 snd_hda_codec_update_cache(codec, nid, 0,
3738 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3739 }
3740}
3741
Takashi Iwai352f7f92012-12-19 12:52:06 +01003742int snd_hda_gen_init(struct hda_codec *codec)
3743{
3744 struct hda_gen_spec *spec = codec->spec;
3745
3746 if (spec->init_hook)
3747 spec->init_hook(codec);
3748
3749 snd_hda_apply_verbs(codec);
3750
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003751 codec->cached_write = 1;
3752
Takashi Iwai352f7f92012-12-19 12:52:06 +01003753 init_multi_out(codec);
3754 init_extra_out(codec);
3755 init_multi_io(codec);
3756 init_analog_input(codec);
3757 init_input_src(codec);
3758 init_digital(codec);
3759
Takashi Iwai973e4972012-12-20 15:16:09 +01003760 clear_unsol_on_unused_pins(codec);
3761
Takashi Iwai352f7f92012-12-19 12:52:06 +01003762 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003763 snd_hda_gen_hp_automute(codec, NULL);
3764 snd_hda_gen_line_automute(codec, NULL);
3765 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003766
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003767 snd_hda_codec_flush_amp_cache(codec);
3768 snd_hda_codec_flush_cmd_cache(codec);
3769
Takashi Iwai352f7f92012-12-19 12:52:06 +01003770 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3771 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3772
3773 hda_call_check_power_status(codec, 0x01);
3774 return 0;
3775}
3776EXPORT_SYMBOL(snd_hda_gen_init);
3777
3778
3779/*
3780 * the generic codec support
3781 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
Takashi Iwai83012a72012-08-24 18:38:08 +02003783#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003784static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3785{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003786 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003787 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3788}
3789#endif
3790
Takashi Iwai352f7f92012-12-19 12:52:06 +01003791static void generic_free(struct hda_codec *codec)
3792{
3793 snd_hda_gen_spec_free(codec->spec);
3794 kfree(codec->spec);
3795 codec->spec = NULL;
3796}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
Takashi Iwai352f7f92012-12-19 12:52:06 +01003798static const struct hda_codec_ops generic_patch_ops = {
3799 .build_controls = snd_hda_gen_build_controls,
3800 .build_pcms = snd_hda_gen_build_pcms,
3801 .init = snd_hda_gen_init,
3802 .free = generic_free,
3803 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02003804#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003805 .check_power_status = generic_check_power_status,
3806#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807};
3808
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809int snd_hda_parse_generic_codec(struct hda_codec *codec)
3810{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003811 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 int err;
3813
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003814 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003815 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003817 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003820 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3821 if (err < 0)
3822 return err;
3823
3824 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003825 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 goto error;
3827
3828 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 return 0;
3830
Takashi Iwai352f7f92012-12-19 12:52:06 +01003831error:
3832 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 return err;
3834}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003835EXPORT_SYMBOL(snd_hda_parse_generic_codec);