blob: 3507448c8b0d88061202c4e8c6cd0d72426eeead [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) ||
Takashi Iwai985803c2013-01-03 16:30:04 +0100466 (!enable && is_active_nid(codec, nid, dir, idx)))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100467 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 Iwaie1284af2013-01-03 16:33:02 +0100876 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100877 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100878 path->active = true;
879 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100880 if (dac)
881 badness += assign_out_path_ctls(codec, pin, dac);
882 }
883
884 return badness;
885}
886
887/* return NID if the given pin has only a single connection to a certain DAC */
888static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
889{
890 struct hda_gen_spec *spec = codec->spec;
891 int i;
892 hda_nid_t nid_found = 0;
893
894 for (i = 0; i < spec->num_all_dacs; i++) {
895 hda_nid_t nid = spec->all_dacs[i];
896 if (!nid || is_dac_already_used(codec, nid))
897 continue;
898 if (is_reachable_path(codec, nid, pin)) {
899 if (nid_found)
900 return 0;
901 nid_found = nid;
902 }
903 }
904 return nid_found;
905}
906
907/* check whether the given pin can be a multi-io pin */
908static bool can_be_multiio_pin(struct hda_codec *codec,
909 unsigned int location, hda_nid_t nid)
910{
911 unsigned int defcfg, caps;
912
913 defcfg = snd_hda_codec_get_pincfg(codec, nid);
914 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
915 return false;
916 if (location && get_defcfg_location(defcfg) != location)
917 return false;
918 caps = snd_hda_query_pin_caps(codec, nid);
919 if (!(caps & AC_PINCAP_OUT))
920 return false;
921 return true;
922}
923
924/*
925 * multi-io helper
926 *
927 * When hardwired is set, try to fill ony hardwired pins, and returns
928 * zero if any pins are filled, non-zero if nothing found.
929 * When hardwired is off, try to fill possible input pins, and returns
930 * the badness value.
931 */
932static int fill_multi_ios(struct hda_codec *codec,
933 hda_nid_t reference_pin,
934 bool hardwired, int offset)
935{
936 struct hda_gen_spec *spec = codec->spec;
937 struct auto_pin_cfg *cfg = &spec->autocfg;
938 int type, i, j, dacs, num_pins, old_pins;
939 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
940 unsigned int location = get_defcfg_location(defcfg);
941 int badness = 0;
942
943 old_pins = spec->multi_ios;
944 if (old_pins >= 2)
945 goto end_fill;
946
947 num_pins = 0;
948 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
949 for (i = 0; i < cfg->num_inputs; i++) {
950 if (cfg->inputs[i].type != type)
951 continue;
952 if (can_be_multiio_pin(codec, location,
953 cfg->inputs[i].pin))
954 num_pins++;
955 }
956 }
957 if (num_pins < 2)
958 goto end_fill;
959
960 dacs = spec->multiout.num_dacs;
961 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
962 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100963 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100964 hda_nid_t nid = cfg->inputs[i].pin;
965 hda_nid_t dac = 0;
966
967 if (cfg->inputs[i].type != type)
968 continue;
969 if (!can_be_multiio_pin(codec, location, nid))
970 continue;
971 for (j = 0; j < spec->multi_ios; j++) {
972 if (nid == spec->multi_io[j].pin)
973 break;
974 }
975 if (j < spec->multi_ios)
976 continue;
977
978 if (offset && offset + spec->multi_ios < dacs) {
979 dac = spec->private_dac_nids[offset + spec->multi_ios];
980 if (!is_reachable_path(codec, dac, nid))
981 dac = 0;
982 }
983 if (hardwired)
984 dac = get_dac_if_single(codec, nid);
985 else if (!dac)
986 dac = look_for_dac(codec, nid, false);
987 if (!dac) {
988 badness++;
989 continue;
990 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100991 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100992 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100993 badness++;
994 continue;
995 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100996 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100997 spec->multi_io[spec->multi_ios].pin = nid;
998 spec->multi_io[spec->multi_ios].dac = dac;
999 spec->multi_ios++;
1000 if (spec->multi_ios >= 2)
1001 break;
1002 }
1003 }
1004 end_fill:
1005 if (badness)
1006 badness = BAD_MULTI_IO;
1007 if (old_pins == spec->multi_ios) {
1008 if (hardwired)
1009 return 1; /* nothing found */
1010 else
1011 return badness; /* no badness if nothing found */
1012 }
1013 if (!hardwired && spec->multi_ios < 2) {
1014 /* cancel newly assigned paths */
1015 spec->paths.used -= spec->multi_ios - old_pins;
1016 spec->multi_ios = old_pins;
1017 return badness;
1018 }
1019
1020 /* assign volume and mute controls */
1021 for (i = old_pins; i < spec->multi_ios; i++)
1022 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1023 spec->multi_io[i].dac);
1024
1025 return badness;
1026}
1027
1028/* map DACs for all pins in the list if they are single connections */
1029static bool map_singles(struct hda_codec *codec, int outs,
1030 const hda_nid_t *pins, hda_nid_t *dacs)
1031{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001032 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001033 int i;
1034 bool found = false;
1035 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001036 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001037 hda_nid_t dac;
1038 if (dacs[i])
1039 continue;
1040 dac = get_dac_if_single(codec, pins[i]);
1041 if (!dac)
1042 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001043 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001044 if (!path && i > 0 && spec->mixer_nid)
1045 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001046 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001047 dacs[i] = dac;
1048 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001049 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001050 path->active = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001051 }
1052 }
1053 return found;
1054}
1055
1056/* fill in the dac_nids table from the parsed pin configuration */
1057static int fill_and_eval_dacs(struct hda_codec *codec,
1058 bool fill_hardwired,
1059 bool fill_mio_first)
1060{
1061 struct hda_gen_spec *spec = codec->spec;
1062 struct auto_pin_cfg *cfg = &spec->autocfg;
1063 int i, err, badness;
1064
1065 /* set num_dacs once to full for look_for_dac() */
1066 spec->multiout.num_dacs = cfg->line_outs;
1067 spec->multiout.dac_nids = spec->private_dac_nids;
1068 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1069 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1070 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1071 spec->multi_ios = 0;
1072 snd_array_free(&spec->paths);
1073 badness = 0;
1074
1075 /* fill hard-wired DACs first */
1076 if (fill_hardwired) {
1077 bool mapped;
1078 do {
1079 mapped = map_singles(codec, cfg->line_outs,
1080 cfg->line_out_pins,
1081 spec->private_dac_nids);
1082 mapped |= map_singles(codec, cfg->hp_outs,
1083 cfg->hp_pins,
1084 spec->multiout.hp_out_nid);
1085 mapped |= map_singles(codec, cfg->speaker_outs,
1086 cfg->speaker_pins,
1087 spec->multiout.extra_out_nid);
1088 if (fill_mio_first && cfg->line_outs == 1 &&
1089 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1090 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1091 if (!err)
1092 mapped = true;
1093 }
1094 } while (mapped);
1095 }
1096
1097 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1098 spec->private_dac_nids,
1099 &main_out_badness);
1100
1101 /* re-count num_dacs and squash invalid entries */
1102 spec->multiout.num_dacs = 0;
1103 for (i = 0; i < cfg->line_outs; i++) {
1104 if (spec->private_dac_nids[i])
1105 spec->multiout.num_dacs++;
1106 else {
1107 memmove(spec->private_dac_nids + i,
1108 spec->private_dac_nids + i + 1,
1109 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1110 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1111 }
1112 }
1113
1114 if (fill_mio_first &&
1115 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1116 /* try to fill multi-io first */
1117 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1118 if (err < 0)
1119 return err;
1120 /* we don't count badness at this stage yet */
1121 }
1122
1123 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1124 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1125 spec->multiout.hp_out_nid,
1126 &extra_out_badness);
1127 if (err < 0)
1128 return err;
1129 badness += err;
1130 }
1131 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1132 err = try_assign_dacs(codec, cfg->speaker_outs,
1133 cfg->speaker_pins,
1134 spec->multiout.extra_out_nid,
1135 &extra_out_badness);
1136 if (err < 0)
1137 return err;
1138 badness += err;
1139 }
1140 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1141 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1142 if (err < 0)
1143 return err;
1144 badness += err;
1145 }
1146 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1147 /* try multi-ios with HP + inputs */
1148 int offset = 0;
1149 if (cfg->line_outs >= 3)
1150 offset = 1;
1151 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1152 if (err < 0)
1153 return err;
1154 badness += err;
1155 }
1156
1157 if (spec->multi_ios == 2) {
1158 for (i = 0; i < 2; i++)
1159 spec->private_dac_nids[spec->multiout.num_dacs++] =
1160 spec->multi_io[i].dac;
1161 spec->ext_channel_count = 2;
1162 } else if (spec->multi_ios) {
1163 spec->multi_ios = 0;
1164 badness += BAD_MULTI_IO;
1165 }
1166
1167 return badness;
1168}
1169
1170#define DEBUG_BADNESS
1171
1172#ifdef DEBUG_BADNESS
1173#define debug_badness snd_printdd
1174#else
1175#define debug_badness(...)
1176#endif
1177
1178static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1179{
1180 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1181 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001182 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001183 spec->multiout.dac_nids[0],
1184 spec->multiout.dac_nids[1],
1185 spec->multiout.dac_nids[2],
1186 spec->multiout.dac_nids[3]);
1187 if (spec->multi_ios > 0)
1188 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1189 spec->multi_ios,
1190 spec->multi_io[0].pin, spec->multi_io[1].pin,
1191 spec->multi_io[0].dac, spec->multi_io[1].dac);
1192 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1193 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001194 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001195 spec->multiout.hp_out_nid[0],
1196 spec->multiout.hp_out_nid[1],
1197 spec->multiout.hp_out_nid[2],
1198 spec->multiout.hp_out_nid[3]);
1199 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1200 cfg->speaker_pins[0], cfg->speaker_pins[1],
1201 cfg->speaker_pins[2], cfg->speaker_pins[3],
1202 spec->multiout.extra_out_nid[0],
1203 spec->multiout.extra_out_nid[1],
1204 spec->multiout.extra_out_nid[2],
1205 spec->multiout.extra_out_nid[3]);
1206}
1207
1208/* find all available DACs of the codec */
1209static void fill_all_dac_nids(struct hda_codec *codec)
1210{
1211 struct hda_gen_spec *spec = codec->spec;
1212 int i;
1213 hda_nid_t nid = codec->start_nid;
1214
1215 spec->num_all_dacs = 0;
1216 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1217 for (i = 0; i < codec->num_nodes; i++, nid++) {
1218 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1219 continue;
1220 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1221 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1222 break;
1223 }
1224 spec->all_dacs[spec->num_all_dacs++] = nid;
1225 }
1226}
1227
1228static int parse_output_paths(struct hda_codec *codec)
1229{
1230 struct hda_gen_spec *spec = codec->spec;
1231 struct auto_pin_cfg *cfg = &spec->autocfg;
1232 struct auto_pin_cfg *best_cfg;
1233 int best_badness = INT_MAX;
1234 int badness;
1235 bool fill_hardwired = true, fill_mio_first = true;
1236 bool best_wired = true, best_mio = true;
1237 bool hp_spk_swapped = false;
1238
1239 fill_all_dac_nids(codec);
1240
1241 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1242 if (!best_cfg)
1243 return -ENOMEM;
1244 *best_cfg = *cfg;
1245
1246 for (;;) {
1247 badness = fill_and_eval_dacs(codec, fill_hardwired,
1248 fill_mio_first);
1249 if (badness < 0) {
1250 kfree(best_cfg);
1251 return badness;
1252 }
1253 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1254 cfg->line_out_type, fill_hardwired, fill_mio_first,
1255 badness);
1256 debug_show_configs(spec, cfg);
1257 if (badness < best_badness) {
1258 best_badness = badness;
1259 *best_cfg = *cfg;
1260 best_wired = fill_hardwired;
1261 best_mio = fill_mio_first;
1262 }
1263 if (!badness)
1264 break;
1265 fill_mio_first = !fill_mio_first;
1266 if (!fill_mio_first)
1267 continue;
1268 fill_hardwired = !fill_hardwired;
1269 if (!fill_hardwired)
1270 continue;
1271 if (hp_spk_swapped)
1272 break;
1273 hp_spk_swapped = true;
1274 if (cfg->speaker_outs > 0 &&
1275 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1276 cfg->hp_outs = cfg->line_outs;
1277 memcpy(cfg->hp_pins, cfg->line_out_pins,
1278 sizeof(cfg->hp_pins));
1279 cfg->line_outs = cfg->speaker_outs;
1280 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1281 sizeof(cfg->speaker_pins));
1282 cfg->speaker_outs = 0;
1283 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1284 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1285 fill_hardwired = true;
1286 continue;
1287 }
1288 if (cfg->hp_outs > 0 &&
1289 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1290 cfg->speaker_outs = cfg->line_outs;
1291 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1292 sizeof(cfg->speaker_pins));
1293 cfg->line_outs = cfg->hp_outs;
1294 memcpy(cfg->line_out_pins, cfg->hp_pins,
1295 sizeof(cfg->hp_pins));
1296 cfg->hp_outs = 0;
1297 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1298 cfg->line_out_type = AUTO_PIN_HP_OUT;
1299 fill_hardwired = true;
1300 continue;
1301 }
1302 break;
1303 }
1304
1305 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001306 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001307 *cfg = *best_cfg;
1308 fill_and_eval_dacs(codec, best_wired, best_mio);
1309 }
1310 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1311 cfg->line_out_type, best_wired, best_mio);
1312 debug_show_configs(spec, cfg);
1313
1314 if (cfg->line_out_pins[0]) {
1315 struct nid_path *path;
1316 path = snd_hda_get_nid_path(codec,
1317 spec->multiout.dac_nids[0],
1318 cfg->line_out_pins[0]);
1319 if (path)
1320 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1321 }
1322
1323 kfree(best_cfg);
1324 return 0;
1325}
1326
1327/* add playback controls from the parsed DAC table */
1328static int create_multi_out_ctls(struct hda_codec *codec,
1329 const struct auto_pin_cfg *cfg)
1330{
1331 struct hda_gen_spec *spec = codec->spec;
1332 int i, err, noutputs;
1333
1334 noutputs = cfg->line_outs;
1335 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1336 noutputs += spec->multi_ios;
1337
1338 for (i = 0; i < noutputs; i++) {
1339 const char *name;
1340 int index;
1341 hda_nid_t dac, pin;
1342 struct nid_path *path;
1343
1344 dac = spec->multiout.dac_nids[i];
1345 if (!dac)
1346 continue;
1347 if (i >= cfg->line_outs) {
1348 pin = spec->multi_io[i - 1].pin;
1349 index = 0;
1350 name = channel_name[i];
1351 } else {
1352 pin = cfg->line_out_pins[i];
1353 name = get_line_out_pfx(spec, i, true, &index);
1354 }
1355
1356 path = snd_hda_get_nid_path(codec, dac, pin);
1357 if (!path)
1358 continue;
1359 if (!name || !strcmp(name, "CLFE")) {
1360 /* Center/LFE */
1361 err = add_vol_ctl(codec, "Center", 0, 1, path);
1362 if (err < 0)
1363 return err;
1364 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1365 if (err < 0)
1366 return err;
1367 err = add_sw_ctl(codec, "Center", 0, 1, path);
1368 if (err < 0)
1369 return err;
1370 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1371 if (err < 0)
1372 return err;
1373 } else {
1374 err = add_stereo_vol(codec, name, index, path);
1375 if (err < 0)
1376 return err;
1377 err = add_stereo_sw(codec, name, index, path);
1378 if (err < 0)
1379 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381 }
1382 return 0;
1383}
1384
Takashi Iwai352f7f92012-12-19 12:52:06 +01001385static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1386 hda_nid_t dac, const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001388 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 int err;
1390
Takashi Iwai352f7f92012-12-19 12:52:06 +01001391 path = snd_hda_get_nid_path(codec, dac, pin);
1392 if (!path)
1393 return 0;
1394 /* bind volume control will be created in the case of dac = 0 */
1395 if (dac) {
1396 err = add_stereo_vol(codec, pfx, cidx, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (err < 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001398 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001400 err = add_stereo_sw(codec, pfx, cidx, path);
1401 if (err < 0)
1402 return err;
1403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Takashi Iwai352f7f92012-12-19 12:52:06 +01001406/* add playback controls for speaker and HP outputs */
1407static int create_extra_outs(struct hda_codec *codec, int num_pins,
1408 const hda_nid_t *pins, const hda_nid_t *dacs,
1409 const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001411 struct hda_gen_spec *spec = codec->spec;
1412 struct hda_bind_ctls *ctl;
1413 char name[32];
1414 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Takashi Iwai352f7f92012-12-19 12:52:06 +01001416 if (!num_pins || !pins[0])
1417 return 0;
1418
1419 if (num_pins == 1) {
1420 hda_nid_t dac = *dacs;
1421 if (!dac)
1422 dac = spec->multiout.dac_nids[0];
1423 return create_extra_out(codec, *pins, dac, pfx, 0);
Takashi Iwai97ec5582006-03-21 11:29:07 +01001424 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001425
1426 for (i = 0; i < num_pins; i++) {
1427 hda_nid_t dac;
1428 if (dacs[num_pins - 1])
1429 dac = dacs[i]; /* with individual volumes */
Takashi Iwai97ec5582006-03-21 11:29:07 +01001430 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001431 dac = 0;
1432 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1433 err = create_extra_out(codec, pins[i], dac,
1434 "Bass Speaker", 0);
1435 } else if (num_pins >= 3) {
1436 snprintf(name, sizeof(name), "%s %s",
1437 pfx, channel_name[i]);
1438 err = create_extra_out(codec, pins[i], dac, name, 0);
1439 } else {
1440 err = create_extra_out(codec, pins[i], dac, pfx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001442 if (err < 0)
1443 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001445 if (dacs[num_pins - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return 0;
1447
Takashi Iwai352f7f92012-12-19 12:52:06 +01001448 /* Let's create a bind-controls for volumes */
1449 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1450 if (!ctl)
1451 return -ENOMEM;
1452 n = 0;
1453 for (i = 0; i < num_pins; i++) {
1454 hda_nid_t vol;
1455 struct nid_path *path;
1456 if (!pins[i] || !dacs[i])
1457 continue;
1458 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1459 if (!path)
1460 continue;
1461 vol = look_for_out_vol_nid(codec, path);
1462 if (vol)
1463 ctl->values[n++] =
1464 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1465 }
1466 if (n) {
1467 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1468 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1469 if (err < 0)
1470 return err;
1471 }
1472 return 0;
1473}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001474
Takashi Iwai352f7f92012-12-19 12:52:06 +01001475static int create_hp_out_ctls(struct hda_codec *codec)
1476{
1477 struct hda_gen_spec *spec = codec->spec;
1478 return create_extra_outs(codec, spec->autocfg.hp_outs,
1479 spec->autocfg.hp_pins,
1480 spec->multiout.hp_out_nid,
1481 "Headphone");
1482}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Takashi Iwai352f7f92012-12-19 12:52:06 +01001484static int create_speaker_out_ctls(struct hda_codec *codec)
1485{
1486 struct hda_gen_spec *spec = codec->spec;
1487 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1488 spec->autocfg.speaker_pins,
1489 spec->multiout.extra_out_nid,
1490 "Speaker");
1491}
1492
1493/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001494 * independent HP controls
1495 */
1496
1497static int indep_hp_info(struct snd_kcontrol *kcontrol,
1498 struct snd_ctl_elem_info *uinfo)
1499{
1500 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1501}
1502
1503static int indep_hp_get(struct snd_kcontrol *kcontrol,
1504 struct snd_ctl_elem_value *ucontrol)
1505{
1506 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1507 struct hda_gen_spec *spec = codec->spec;
1508 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1509 return 0;
1510}
1511
1512static int indep_hp_put(struct snd_kcontrol *kcontrol,
1513 struct snd_ctl_elem_value *ucontrol)
1514{
1515 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1516 struct hda_gen_spec *spec = codec->spec;
1517 unsigned int select = ucontrol->value.enumerated.item[0];
1518 int ret = 0;
1519
1520 mutex_lock(&spec->pcm_mutex);
1521 if (spec->active_streams) {
1522 ret = -EBUSY;
1523 goto unlock;
1524 }
1525
1526 if (spec->indep_hp_enabled != select) {
1527 spec->indep_hp_enabled = select;
1528 if (spec->indep_hp_enabled)
1529 spec->multiout.hp_out_nid[0] = 0;
1530 else
1531 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1532 ret = 1;
1533 }
1534 unlock:
1535 mutex_unlock(&spec->pcm_mutex);
1536 return ret;
1537}
1538
1539static const struct snd_kcontrol_new indep_hp_ctl = {
1540 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1541 .name = "Independent HP",
1542 .info = indep_hp_info,
1543 .get = indep_hp_get,
1544 .put = indep_hp_put,
1545};
1546
1547
1548static int create_indep_hp_ctls(struct hda_codec *codec)
1549{
1550 struct hda_gen_spec *spec = codec->spec;
1551
1552 if (!spec->indep_hp)
1553 return 0;
1554 if (!spec->multiout.hp_out_nid[0]) {
1555 spec->indep_hp = 0;
1556 return 0;
1557 }
1558
1559 spec->indep_hp_enabled = false;
1560 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1561 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1562 return -ENOMEM;
1563 return 0;
1564}
1565
1566/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001567 * channel mode enum control
1568 */
1569
1570static int ch_mode_info(struct snd_kcontrol *kcontrol,
1571 struct snd_ctl_elem_info *uinfo)
1572{
1573 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1574 struct hda_gen_spec *spec = codec->spec;
1575
1576 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1577 uinfo->count = 1;
1578 uinfo->value.enumerated.items = spec->multi_ios + 1;
1579 if (uinfo->value.enumerated.item > spec->multi_ios)
1580 uinfo->value.enumerated.item = spec->multi_ios;
1581 sprintf(uinfo->value.enumerated.name, "%dch",
1582 (uinfo->value.enumerated.item + 1) * 2);
1583 return 0;
1584}
1585
1586static int ch_mode_get(struct snd_kcontrol *kcontrol,
1587 struct snd_ctl_elem_value *ucontrol)
1588{
1589 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1590 struct hda_gen_spec *spec = codec->spec;
1591 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1592 return 0;
1593}
1594
1595static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1596{
1597 struct hda_gen_spec *spec = codec->spec;
1598 hda_nid_t nid = spec->multi_io[idx].pin;
1599 struct nid_path *path;
1600
1601 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1602 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001604
1605 if (path->active == output)
1606 return 0;
1607
1608 if (output) {
1609 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1610 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001611 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001612 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001613 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001614 snd_hda_activate_path(codec, path, false, true);
1615 snd_hda_set_pin_ctl_cache(codec, nid,
1616 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
Takashi Iwai352f7f92012-12-19 12:52:06 +01001621static int ch_mode_put(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001624 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1625 struct hda_gen_spec *spec = codec->spec;
1626 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Takashi Iwai352f7f92012-12-19 12:52:06 +01001628 ch = ucontrol->value.enumerated.item[0];
1629 if (ch < 0 || ch > spec->multi_ios)
1630 return -EINVAL;
1631 if (ch == (spec->ext_channel_count - 1) / 2)
1632 return 0;
1633 spec->ext_channel_count = (ch + 1) * 2;
1634 for (i = 0; i < spec->multi_ios; i++)
1635 set_multi_io(codec, i, i < ch);
1636 spec->multiout.max_channels = max(spec->ext_channel_count,
1637 spec->const_channel_count);
1638 if (spec->need_dac_fix)
1639 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return 1;
1641}
1642
Takashi Iwai352f7f92012-12-19 12:52:06 +01001643static const struct snd_kcontrol_new channel_mode_enum = {
1644 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1645 .name = "Channel Mode",
1646 .info = ch_mode_info,
1647 .get = ch_mode_get,
1648 .put = ch_mode_put,
1649};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Takashi Iwai352f7f92012-12-19 12:52:06 +01001651static int create_multi_channel_mode(struct hda_codec *codec)
1652{
1653 struct hda_gen_spec *spec = codec->spec;
1654
1655 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001656 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001657 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return 0;
1660}
1661
Takashi Iwai352f7f92012-12-19 12:52:06 +01001662/*
1663 * shared headphone/mic handling
1664 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001665
Takashi Iwai352f7f92012-12-19 12:52:06 +01001666static void call_update_outputs(struct hda_codec *codec);
1667
1668/* for shared I/O, change the pin-control accordingly */
1669static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1670{
1671 struct hda_gen_spec *spec = codec->spec;
1672 unsigned int val;
1673 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1674 /* NOTE: this assumes that there are only two inputs, the
1675 * first is the real internal mic and the second is HP/mic jack.
1676 */
1677
1678 val = snd_hda_get_default_vref(codec, pin);
1679
1680 /* This pin does not have vref caps - let's enable vref on pin 0x18
1681 instead, as suggested by Realtek */
1682 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1683 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1684 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1685 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001686 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1687 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001688 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001689
1690 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001691 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001692
1693 spec->automute_speaker = !set_as_mic;
1694 call_update_outputs(codec);
1695}
1696
1697/* create a shared input with the headphone out */
1698static int create_shared_input(struct hda_codec *codec)
1699{
1700 struct hda_gen_spec *spec = codec->spec;
1701 struct auto_pin_cfg *cfg = &spec->autocfg;
1702 unsigned int defcfg;
1703 hda_nid_t nid;
1704
1705 /* only one internal input pin? */
1706 if (cfg->num_inputs != 1)
1707 return 0;
1708 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1709 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1710 return 0;
1711
1712 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1713 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1714 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1715 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1716 else
1717 return 0; /* both not available */
1718
1719 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1720 return 0; /* no input */
1721
1722 cfg->inputs[1].pin = nid;
1723 cfg->inputs[1].type = AUTO_PIN_MIC;
1724 cfg->num_inputs = 2;
1725 spec->shared_mic_hp = 1;
1726 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1727 return 0;
1728}
1729
1730
1731/*
1732 * Parse input paths
1733 */
1734
1735#ifdef CONFIG_PM
1736/* add the powersave loopback-list entry */
1737static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1738{
1739 struct hda_amp_list *list;
1740
1741 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1742 return;
1743 list = spec->loopback_list + spec->num_loopbacks;
1744 list->nid = mix;
1745 list->dir = HDA_INPUT;
1746 list->idx = idx;
1747 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001748 spec->loopback.amplist = spec->loopback_list;
1749}
1750#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001751#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001752#endif
1753
Takashi Iwai352f7f92012-12-19 12:52:06 +01001754/* create input playback/capture controls for the given pin */
1755static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1756 const char *ctlname, int ctlidx,
1757 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001759 struct hda_gen_spec *spec = codec->spec;
1760 struct nid_path *path;
1761 unsigned int val;
1762 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Takashi Iwai352f7f92012-12-19 12:52:06 +01001764 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1765 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1766 return 0; /* no need for analog loopback */
1767
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001768 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001769 if (!path)
1770 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001771 print_nid_path("loopback", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001772
1773 idx = path->idx[path->depth - 1];
1774 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1775 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1776 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001777 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001779 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781
Takashi Iwai352f7f92012-12-19 12:52:06 +01001782 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1783 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1784 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001785 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001787 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
1789
Takashi Iwai352f7f92012-12-19 12:52:06 +01001790 path->active = true;
1791 add_loopback_list(spec, mix_nid, idx);
1792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
Takashi Iwai352f7f92012-12-19 12:52:06 +01001795static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001797 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1798 return (pincap & AC_PINCAP_IN) != 0;
1799}
1800
1801/* Parse the codec tree and retrieve ADCs */
1802static int fill_adc_nids(struct hda_codec *codec)
1803{
1804 struct hda_gen_spec *spec = codec->spec;
1805 hda_nid_t nid;
1806 hda_nid_t *adc_nids = spec->adc_nids;
1807 int max_nums = ARRAY_SIZE(spec->adc_nids);
1808 int i, nums = 0;
1809
1810 nid = codec->start_nid;
1811 for (i = 0; i < codec->num_nodes; i++, nid++) {
1812 unsigned int caps = get_wcaps(codec, nid);
1813 int type = get_wcaps_type(caps);
1814
1815 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1816 continue;
1817 adc_nids[nums] = nid;
1818 if (++nums >= max_nums)
1819 break;
1820 }
1821 spec->num_adc_nids = nums;
1822 return nums;
1823}
1824
1825/* filter out invalid adc_nids that don't give all active input pins;
1826 * if needed, check whether dynamic ADC-switching is available
1827 */
1828static int check_dyn_adc_switch(struct hda_codec *codec)
1829{
1830 struct hda_gen_spec *spec = codec->spec;
1831 struct hda_input_mux *imux = &spec->input_mux;
1832 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1833 int i, n, nums;
1834 hda_nid_t pin, adc;
1835
1836 again:
1837 nums = 0;
1838 for (n = 0; n < spec->num_adc_nids; n++) {
1839 adc = spec->adc_nids[n];
1840 for (i = 0; i < imux->num_items; i++) {
1841 pin = spec->imux_pins[i];
1842 if (!is_reachable_path(codec, pin, adc))
1843 break;
1844 }
1845 if (i >= imux->num_items)
1846 adc_nids[nums++] = adc;
1847 }
1848
1849 if (!nums) {
1850 if (spec->shared_mic_hp) {
1851 spec->shared_mic_hp = 0;
1852 imux->num_items = 1;
1853 goto again;
1854 }
1855
1856 /* check whether ADC-switch is possible */
1857 for (i = 0; i < imux->num_items; i++) {
1858 pin = spec->imux_pins[i];
1859 for (n = 0; n < spec->num_adc_nids; n++) {
1860 adc = spec->adc_nids[n];
1861 if (is_reachable_path(codec, pin, adc)) {
1862 spec->dyn_adc_idx[i] = n;
1863 break;
1864 }
1865 }
1866 }
1867
1868 snd_printdd("hda-codec: enabling ADC switching\n");
1869 spec->dyn_adc_switch = 1;
1870 } else if (nums != spec->num_adc_nids) {
1871 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1872 spec->num_adc_nids = nums;
1873 }
1874
1875 if (imux->num_items == 1 || spec->shared_mic_hp) {
1876 snd_printdd("hda-codec: reducing to a single ADC\n");
1877 spec->num_adc_nids = 1; /* reduce to a single ADC */
1878 }
1879
1880 /* single index for individual volumes ctls */
1881 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1882 spec->num_adc_nids = 1;
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return 0;
1885}
1886
1887/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001888 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001890static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001891{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001892 struct hda_gen_spec *spec = codec->spec;
1893 const struct auto_pin_cfg *cfg = &spec->autocfg;
1894 hda_nid_t mixer = spec->mixer_nid;
1895 struct hda_input_mux *imux = &spec->input_mux;
1896 int num_adcs;
1897 int i, c, err, type_idx = 0;
1898 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001899
Takashi Iwai352f7f92012-12-19 12:52:06 +01001900 num_adcs = fill_adc_nids(codec);
1901 if (num_adcs < 0)
1902 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001903
Takashi Iwai352f7f92012-12-19 12:52:06 +01001904 for (i = 0; i < cfg->num_inputs; i++) {
1905 hda_nid_t pin;
1906 const char *label;
1907 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Takashi Iwai352f7f92012-12-19 12:52:06 +01001909 pin = cfg->inputs[i].pin;
1910 if (!is_input_pin(codec, pin))
1911 continue;
1912
1913 label = hda_get_autocfg_input_label(codec, cfg, i);
1914 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1915 label = "Headphone Mic";
1916 if (prev_label && !strcmp(label, prev_label))
1917 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001918 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001919 type_idx = 0;
1920 prev_label = label;
1921
1922 if (mixer) {
1923 if (is_reachable_path(codec, pin, mixer)) {
1924 err = new_analog_input(codec, pin,
1925 label, type_idx, mixer);
1926 if (err < 0)
1927 return err;
1928 }
1929 }
1930
1931 imux_added = false;
1932 for (c = 0; c < num_adcs; c++) {
1933 struct nid_path *path;
1934 hda_nid_t adc = spec->adc_nids[c];
1935
1936 if (!is_reachable_path(codec, pin, adc))
1937 continue;
1938 path = snd_array_new(&spec->paths);
1939 if (!path)
1940 return -ENOMEM;
1941 memset(path, 0, sizeof(*path));
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001942 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001943 snd_printd(KERN_ERR
1944 "invalid input path 0x%x -> 0x%x\n",
1945 pin, adc);
1946 spec->paths.used--;
1947 continue;
1948 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001949 print_nid_path("input", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001950
1951 if (!imux_added) {
1952 spec->imux_pins[imux->num_items] = pin;
1953 snd_hda_add_imux_item(imux, label,
1954 imux->num_items, NULL);
1955 imux_added = true;
1956 }
1957 }
1958 }
1959
1960 return 0;
1961}
1962
1963
1964/*
1965 * input source mux
1966 */
1967
1968/* get the ADC NID corresponding to the given index */
1969static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1970{
1971 struct hda_gen_spec *spec = codec->spec;
1972 if (spec->dyn_adc_switch)
1973 adc_idx = spec->dyn_adc_idx[imux_idx];
1974 return spec->adc_nids[adc_idx];
1975}
1976
1977static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1978 unsigned int idx);
1979
1980static int mux_enum_info(struct snd_kcontrol *kcontrol,
1981 struct snd_ctl_elem_info *uinfo)
1982{
1983 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1984 struct hda_gen_spec *spec = codec->spec;
1985 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1986}
1987
1988static int mux_enum_get(struct snd_kcontrol *kcontrol,
1989 struct snd_ctl_elem_value *ucontrol)
1990{
1991 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1992 struct hda_gen_spec *spec = codec->spec;
1993 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1994
1995 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1996 return 0;
1997}
1998
1999static int mux_enum_put(struct snd_kcontrol *kcontrol,
2000 struct snd_ctl_elem_value *ucontrol)
2001{
2002 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2003 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2004 return mux_select(codec, adc_idx,
2005 ucontrol->value.enumerated.item[0]);
2006}
2007
Takashi Iwai352f7f92012-12-19 12:52:06 +01002008static const struct snd_kcontrol_new cap_src_temp = {
2009 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2010 .name = "Input Source",
2011 .info = mux_enum_info,
2012 .get = mux_enum_get,
2013 .put = mux_enum_put,
2014};
2015
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002016/*
2017 * capture volume and capture switch ctls
2018 */
2019
Takashi Iwai352f7f92012-12-19 12:52:06 +01002020typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2021 struct snd_ctl_elem_value *ucontrol);
2022
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002023/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002024static int cap_put_caller(struct snd_kcontrol *kcontrol,
2025 struct snd_ctl_elem_value *ucontrol,
2026 put_call_t func, int type)
2027{
2028 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2029 struct hda_gen_spec *spec = codec->spec;
2030 const struct hda_input_mux *imux;
2031 struct nid_path *path;
2032 int i, adc_idx, err = 0;
2033
2034 imux = &spec->input_mux;
2035 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2036 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002037 /* we use the cache-only update at first since multiple input paths
2038 * may shared the same amp; by updating only caches, the redundant
2039 * writes to hardware can be reduced.
2040 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002041 codec->cached_write = 1;
2042 for (i = 0; i < imux->num_items; i++) {
2043 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2044 get_adc_nid(codec, adc_idx, i));
2045 if (!path->ctls[type])
2046 continue;
2047 kcontrol->private_value = path->ctls[type];
2048 err = func(kcontrol, ucontrol);
2049 if (err < 0)
2050 goto error;
2051 }
2052 error:
2053 codec->cached_write = 0;
2054 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002055 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002056 if (err >= 0 && spec->cap_sync_hook)
2057 spec->cap_sync_hook(codec);
2058 return err;
2059}
2060
2061/* capture volume ctl callbacks */
2062#define cap_vol_info snd_hda_mixer_amp_volume_info
2063#define cap_vol_get snd_hda_mixer_amp_volume_get
2064#define cap_vol_tlv snd_hda_mixer_amp_tlv
2065
2066static int cap_vol_put(struct snd_kcontrol *kcontrol,
2067 struct snd_ctl_elem_value *ucontrol)
2068{
2069 return cap_put_caller(kcontrol, ucontrol,
2070 snd_hda_mixer_amp_volume_put,
2071 NID_PATH_VOL_CTL);
2072}
2073
2074static const struct snd_kcontrol_new cap_vol_temp = {
2075 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2076 .name = "Capture Volume",
2077 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2078 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2079 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2080 .info = cap_vol_info,
2081 .get = cap_vol_get,
2082 .put = cap_vol_put,
2083 .tlv = { .c = cap_vol_tlv },
2084};
2085
2086/* capture switch ctl callbacks */
2087#define cap_sw_info snd_ctl_boolean_stereo_info
2088#define cap_sw_get snd_hda_mixer_amp_switch_get
2089
2090static int cap_sw_put(struct snd_kcontrol *kcontrol,
2091 struct snd_ctl_elem_value *ucontrol)
2092{
2093 return cap_put_caller(kcontrol, ucontrol,
2094 snd_hda_mixer_amp_switch_put,
2095 NID_PATH_MUTE_CTL);
2096}
2097
2098static const struct snd_kcontrol_new cap_sw_temp = {
2099 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2100 .name = "Capture Switch",
2101 .info = cap_sw_info,
2102 .get = cap_sw_get,
2103 .put = cap_sw_put,
2104};
2105
2106static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2107{
2108 hda_nid_t nid;
2109 int i, depth;
2110
2111 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2112 for (depth = 0; depth < 3; depth++) {
2113 if (depth >= path->depth)
2114 return -EINVAL;
2115 i = path->depth - depth - 1;
2116 nid = path->path[i];
2117 if (!path->ctls[NID_PATH_VOL_CTL]) {
2118 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2119 path->ctls[NID_PATH_VOL_CTL] =
2120 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2121 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2122 int idx = path->idx[i];
2123 if (!depth && codec->single_adc_amp)
2124 idx = 0;
2125 path->ctls[NID_PATH_VOL_CTL] =
2126 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2127 }
2128 }
2129 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2130 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2131 path->ctls[NID_PATH_MUTE_CTL] =
2132 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2133 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2134 int idx = path->idx[i];
2135 if (!depth && codec->single_adc_amp)
2136 idx = 0;
2137 path->ctls[NID_PATH_MUTE_CTL] =
2138 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2139 }
2140 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return 0;
2143}
2144
Takashi Iwai352f7f92012-12-19 12:52:06 +01002145static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002147 struct hda_gen_spec *spec = codec->spec;
2148 struct auto_pin_cfg *cfg = &spec->autocfg;
2149 unsigned int val;
2150 int i;
2151
2152 if (!spec->inv_dmic_split)
2153 return false;
2154 for (i = 0; i < cfg->num_inputs; i++) {
2155 if (cfg->inputs[i].pin != nid)
2156 continue;
2157 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2158 return false;
2159 val = snd_hda_codec_get_pincfg(codec, nid);
2160 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2161 }
2162 return false;
2163}
2164
2165static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2166 int idx, bool is_switch, unsigned int ctl,
2167 bool inv_dmic)
2168{
2169 struct hda_gen_spec *spec = codec->spec;
2170 char tmpname[44];
2171 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2172 const char *sfx = is_switch ? "Switch" : "Volume";
2173 unsigned int chs = inv_dmic ? 1 : 3;
2174 int err;
2175
2176 if (!ctl)
2177 return 0;
2178
2179 if (label)
2180 snprintf(tmpname, sizeof(tmpname),
2181 "%s Capture %s", label, sfx);
2182 else
2183 snprintf(tmpname, sizeof(tmpname),
2184 "Capture %s", sfx);
2185 err = add_control(spec, type, tmpname, idx,
2186 amp_val_replace_channels(ctl, chs));
2187 if (err < 0 || !inv_dmic)
2188 return err;
2189
2190 /* Make independent right kcontrol */
2191 if (label)
2192 snprintf(tmpname, sizeof(tmpname),
2193 "Inverted %s Capture %s", label, sfx);
2194 else
2195 snprintf(tmpname, sizeof(tmpname),
2196 "Inverted Capture %s", sfx);
2197 return add_control(spec, type, tmpname, idx,
2198 amp_val_replace_channels(ctl, 2));
2199}
2200
2201/* create single (and simple) capture volume and switch controls */
2202static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2203 unsigned int vol_ctl, unsigned int sw_ctl,
2204 bool inv_dmic)
2205{
2206 int err;
2207 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2208 if (err < 0)
2209 return err;
2210 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2211 if (err < 0)
2212 return err;
2213 return 0;
2214}
2215
2216/* create bound capture volume and switch controls */
2217static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2218 unsigned int vol_ctl, unsigned int sw_ctl)
2219{
2220 struct hda_gen_spec *spec = codec->spec;
2221 struct snd_kcontrol_new *knew;
2222
2223 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002224 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002225 if (!knew)
2226 return -ENOMEM;
2227 knew->index = idx;
2228 knew->private_value = vol_ctl;
2229 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2230 }
2231 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002232 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002233 if (!knew)
2234 return -ENOMEM;
2235 knew->index = idx;
2236 knew->private_value = sw_ctl;
2237 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2238 }
2239 return 0;
2240}
2241
2242/* return the vol ctl when used first in the imux list */
2243static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2244{
2245 struct hda_gen_spec *spec = codec->spec;
2246 struct nid_path *path;
2247 unsigned int ctl;
2248 int i;
2249
2250 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2251 get_adc_nid(codec, 0, idx));
2252 if (!path)
2253 return 0;
2254 ctl = path->ctls[type];
2255 if (!ctl)
2256 return 0;
2257 for (i = 0; i < idx - 1; i++) {
2258 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2259 get_adc_nid(codec, 0, i));
2260 if (path && path->ctls[type] == ctl)
2261 return 0;
2262 }
2263 return ctl;
2264}
2265
2266/* create individual capture volume and switch controls per input */
2267static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2268{
2269 struct hda_gen_spec *spec = codec->spec;
2270 struct hda_input_mux *imux = &spec->input_mux;
2271 int i, err, type, type_idx = 0;
2272 const char *prev_label = NULL;
2273
2274 for (i = 0; i < imux->num_items; i++) {
2275 const char *label;
2276 bool inv_dmic;
2277 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2278 if (prev_label && !strcmp(label, prev_label))
2279 type_idx++;
2280 else
2281 type_idx = 0;
2282 prev_label = label;
2283 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2284
2285 for (type = 0; type < 2; type++) {
2286 err = add_single_cap_ctl(codec, label, type_idx, type,
2287 get_first_cap_ctl(codec, i, type),
2288 inv_dmic);
2289 if (err < 0)
2290 return err;
2291 }
2292 }
2293 return 0;
2294}
2295
2296static int create_capture_mixers(struct hda_codec *codec)
2297{
2298 struct hda_gen_spec *spec = codec->spec;
2299 struct hda_input_mux *imux = &spec->input_mux;
2300 int i, n, nums, err;
2301
2302 if (spec->dyn_adc_switch)
2303 nums = 1;
2304 else
2305 nums = spec->num_adc_nids;
2306
2307 if (!spec->auto_mic && imux->num_items > 1) {
2308 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002309 const char *name;
2310 name = nums > 1 ? "Input Source" : "Capture Source";
2311 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002312 if (!knew)
2313 return -ENOMEM;
2314 knew->count = nums;
2315 }
2316
2317 for (n = 0; n < nums; n++) {
2318 bool multi = false;
2319 bool inv_dmic = false;
2320 int vol, sw;
2321
2322 vol = sw = 0;
2323 for (i = 0; i < imux->num_items; i++) {
2324 struct nid_path *path;
2325 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2326 get_adc_nid(codec, n, i));
2327 if (!path)
2328 continue;
2329 parse_capvol_in_path(codec, path);
2330 if (!vol)
2331 vol = path->ctls[NID_PATH_VOL_CTL];
2332 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2333 multi = true;
2334 if (!sw)
2335 sw = path->ctls[NID_PATH_MUTE_CTL];
2336 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2337 multi = true;
2338 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2339 inv_dmic = true;
2340 }
2341
2342 if (!multi)
2343 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2344 inv_dmic);
2345 else if (!spec->multi_cap_vol)
2346 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2347 else
2348 err = create_multi_cap_vol_ctl(codec);
2349 if (err < 0)
2350 return err;
2351 }
2352
2353 return 0;
2354}
2355
2356/*
2357 * add mic boosts if needed
2358 */
2359static int parse_mic_boost(struct hda_codec *codec)
2360{
2361 struct hda_gen_spec *spec = codec->spec;
2362 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002363 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002364 int type_idx = 0;
2365 hda_nid_t nid;
2366 const char *prev_label = NULL;
2367
2368 for (i = 0; i < cfg->num_inputs; i++) {
2369 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2370 break;
2371 nid = cfg->inputs[i].pin;
2372 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2373 const char *label;
2374 char boost_label[32];
2375 struct nid_path *path;
2376 unsigned int val;
2377
2378 label = hda_get_autocfg_input_label(codec, cfg, i);
2379 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2380 label = "Headphone Mic";
2381 if (prev_label && !strcmp(label, prev_label))
2382 type_idx++;
2383 else
2384 type_idx = 0;
2385 prev_label = label;
2386
2387 snprintf(boost_label, sizeof(boost_label),
2388 "%s Boost Volume", label);
2389 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2390 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2391 boost_label, type_idx, val);
2392 if (err < 0)
2393 return err;
2394
2395 path = snd_hda_get_nid_path(codec, nid, 0);
2396 if (path)
2397 path->ctls[NID_PATH_BOOST_CTL] = val;
2398 }
2399 }
2400 return 0;
2401}
2402
2403/*
2404 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2405 */
2406static void parse_digital(struct hda_codec *codec)
2407{
2408 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002409 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002410 int i, nums;
2411 hda_nid_t dig_nid;
2412
2413 /* support multiple SPDIFs; the secondary is set up as a slave */
2414 nums = 0;
2415 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2416 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2417 dig_nid = look_for_dac(codec, pin, true);
2418 if (!dig_nid)
2419 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002420 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002421 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002422 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002423 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002424 path->active = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002425 if (!nums) {
2426 spec->multiout.dig_out_nid = dig_nid;
2427 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2428 } else {
2429 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2430 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2431 break;
2432 spec->slave_dig_outs[nums - 1] = dig_nid;
2433 }
2434 nums++;
2435 }
2436
2437 if (spec->autocfg.dig_in_pin) {
2438 dig_nid = codec->start_nid;
2439 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002440 unsigned int wcaps = get_wcaps(codec, dig_nid);
2441 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2442 continue;
2443 if (!(wcaps & AC_WCAP_DIGITAL))
2444 continue;
2445 path = snd_hda_add_new_path(codec,
2446 spec->autocfg.dig_in_pin,
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002447 dig_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002448 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002449 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002450 path->active = true;
2451 spec->dig_in_nid = dig_nid;
2452 break;
2453 }
2454 }
2455 }
2456}
2457
2458
2459/*
2460 * input MUX handling
2461 */
2462
2463static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2464
2465/* select the given imux item; either unmute exclusively or select the route */
2466static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2467 unsigned int idx)
2468{
2469 struct hda_gen_spec *spec = codec->spec;
2470 const struct hda_input_mux *imux;
2471 struct nid_path *path;
2472
2473 imux = &spec->input_mux;
2474 if (!imux->num_items)
2475 return 0;
2476
2477 if (idx >= imux->num_items)
2478 idx = imux->num_items - 1;
2479 if (spec->cur_mux[adc_idx] == idx)
2480 return 0;
2481
2482 path = snd_hda_get_nid_path(codec,
2483 spec->imux_pins[spec->cur_mux[adc_idx]],
2484 spec->adc_nids[adc_idx]);
2485 if (!path)
2486 return 0;
2487 if (path->active)
2488 snd_hda_activate_path(codec, path, false, false);
2489
2490 spec->cur_mux[adc_idx] = idx;
2491
2492 if (spec->shared_mic_hp)
2493 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2494
2495 if (spec->dyn_adc_switch)
2496 dyn_adc_pcm_resetup(codec, idx);
2497
2498 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2499 get_adc_nid(codec, adc_idx, idx));
2500 if (!path)
2501 return 0;
2502 if (path->active)
2503 return 0;
2504 snd_hda_activate_path(codec, path, true, false);
2505 if (spec->cap_sync_hook)
2506 spec->cap_sync_hook(codec);
2507 return 1;
2508}
2509
2510
2511/*
2512 * Jack detections for HP auto-mute and mic-switch
2513 */
2514
2515/* check each pin in the given array; returns true if any of them is plugged */
2516static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2517{
2518 int i, present = 0;
2519
2520 for (i = 0; i < num_pins; i++) {
2521 hda_nid_t nid = pins[i];
2522 if (!nid)
2523 break;
2524 present |= snd_hda_jack_detect(codec, nid);
2525 }
2526 return present;
2527}
2528
2529/* standard HP/line-out auto-mute helper */
2530static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2531 bool mute, bool hp_out)
2532{
2533 struct hda_gen_spec *spec = codec->spec;
2534 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2535 int i;
2536
2537 for (i = 0; i < num_pins; i++) {
2538 hda_nid_t nid = pins[i];
2539 unsigned int val;
2540 if (!nid)
2541 break;
2542 /* don't reset VREF value in case it's controlling
2543 * the amp (see alc861_fixup_asus_amp_vref_0f())
2544 */
2545 if (spec->keep_vref_in_automute) {
2546 val = snd_hda_codec_read(codec, nid, 0,
2547 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2548 val &= ~PIN_HP;
2549 } else
2550 val = 0;
2551 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002552 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002553 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002554 }
2555}
2556
2557/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002558void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002559{
2560 struct hda_gen_spec *spec = codec->spec;
2561 int on;
2562
2563 /* Control HP pins/amps depending on master_mute state;
2564 * in general, HP pins/amps control should be enabled in all cases,
2565 * but currently set only for master_mute, just to be safe
2566 */
2567 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2568 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2569 spec->autocfg.hp_pins, spec->master_mute, true);
2570
2571 if (!spec->automute_speaker)
2572 on = 0;
2573 else
2574 on = spec->hp_jack_present | spec->line_jack_present;
2575 on |= spec->master_mute;
2576 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2577 spec->autocfg.speaker_pins, on, false);
2578
2579 /* toggle line-out mutes if needed, too */
2580 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2581 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2582 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2583 return;
2584 if (!spec->automute_lo)
2585 on = 0;
2586 else
2587 on = spec->hp_jack_present;
2588 on |= spec->master_mute;
2589 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2590 spec->autocfg.line_out_pins, on, false);
2591}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002592EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002593
2594static void call_update_outputs(struct hda_codec *codec)
2595{
2596 struct hda_gen_spec *spec = codec->spec;
2597 if (spec->automute_hook)
2598 spec->automute_hook(codec);
2599 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002600 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002601}
2602
2603/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002604void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002605{
2606 struct hda_gen_spec *spec = codec->spec;
2607
2608 spec->hp_jack_present =
2609 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2610 spec->autocfg.hp_pins);
2611 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2612 return;
2613 call_update_outputs(codec);
2614}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002615EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002616
2617/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002618void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002619{
2620 struct hda_gen_spec *spec = codec->spec;
2621
2622 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2623 return;
2624 /* check LO jack only when it's different from HP */
2625 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2626 return;
2627
2628 spec->line_jack_present =
2629 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2630 spec->autocfg.line_out_pins);
2631 if (!spec->automute_speaker || !spec->detect_lo)
2632 return;
2633 call_update_outputs(codec);
2634}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002635EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002636
2637/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002638void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002639{
2640 struct hda_gen_spec *spec = codec->spec;
2641 int i;
2642
2643 if (!spec->auto_mic)
2644 return;
2645
2646 for (i = spec->am_num_entries - 1; i > 0; i--) {
2647 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2648 mux_select(codec, 0, spec->am_entry[i].idx);
2649 return;
2650 }
2651 }
2652 mux_select(codec, 0, spec->am_entry[0].idx);
2653}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002654EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002655
2656/*
2657 * Auto-Mute mode mixer enum support
2658 */
2659static int automute_mode_info(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_info *uinfo)
2661{
2662 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2663 struct hda_gen_spec *spec = codec->spec;
2664 static const char * const texts3[] = {
2665 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002666 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Takashi Iwai352f7f92012-12-19 12:52:06 +01002668 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2669 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2670 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2671}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Takashi Iwai352f7f92012-12-19 12:52:06 +01002673static int automute_mode_get(struct snd_kcontrol *kcontrol,
2674 struct snd_ctl_elem_value *ucontrol)
2675{
2676 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2677 struct hda_gen_spec *spec = codec->spec;
2678 unsigned int val = 0;
2679 if (spec->automute_speaker)
2680 val++;
2681 if (spec->automute_lo)
2682 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002683
Takashi Iwai352f7f92012-12-19 12:52:06 +01002684 ucontrol->value.enumerated.item[0] = val;
2685 return 0;
2686}
2687
2688static int automute_mode_put(struct snd_kcontrol *kcontrol,
2689 struct snd_ctl_elem_value *ucontrol)
2690{
2691 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2692 struct hda_gen_spec *spec = codec->spec;
2693
2694 switch (ucontrol->value.enumerated.item[0]) {
2695 case 0:
2696 if (!spec->automute_speaker && !spec->automute_lo)
2697 return 0;
2698 spec->automute_speaker = 0;
2699 spec->automute_lo = 0;
2700 break;
2701 case 1:
2702 if (spec->automute_speaker_possible) {
2703 if (!spec->automute_lo && spec->automute_speaker)
2704 return 0;
2705 spec->automute_speaker = 1;
2706 spec->automute_lo = 0;
2707 } else if (spec->automute_lo_possible) {
2708 if (spec->automute_lo)
2709 return 0;
2710 spec->automute_lo = 1;
2711 } else
2712 return -EINVAL;
2713 break;
2714 case 2:
2715 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2716 return -EINVAL;
2717 if (spec->automute_speaker && spec->automute_lo)
2718 return 0;
2719 spec->automute_speaker = 1;
2720 spec->automute_lo = 1;
2721 break;
2722 default:
2723 return -EINVAL;
2724 }
2725 call_update_outputs(codec);
2726 return 1;
2727}
2728
2729static const struct snd_kcontrol_new automute_mode_enum = {
2730 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2731 .name = "Auto-Mute Mode",
2732 .info = automute_mode_info,
2733 .get = automute_mode_get,
2734 .put = automute_mode_put,
2735};
2736
2737static int add_automute_mode_enum(struct hda_codec *codec)
2738{
2739 struct hda_gen_spec *spec = codec->spec;
2740
Takashi Iwai12c93df2012-12-19 14:38:33 +01002741 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002742 return -ENOMEM;
2743 return 0;
2744}
2745
2746/*
2747 * Check the availability of HP/line-out auto-mute;
2748 * Set up appropriately if really supported
2749 */
2750static int check_auto_mute_availability(struct hda_codec *codec)
2751{
2752 struct hda_gen_spec *spec = codec->spec;
2753 struct auto_pin_cfg *cfg = &spec->autocfg;
2754 int present = 0;
2755 int i, err;
2756
2757 if (cfg->hp_pins[0])
2758 present++;
2759 if (cfg->line_out_pins[0])
2760 present++;
2761 if (cfg->speaker_pins[0])
2762 present++;
2763 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002764 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002765
2766 if (!cfg->speaker_pins[0] &&
2767 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2768 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2769 sizeof(cfg->speaker_pins));
2770 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Takashi Iwai352f7f92012-12-19 12:52:06 +01002773 if (!cfg->hp_pins[0] &&
2774 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2775 memcpy(cfg->hp_pins, cfg->line_out_pins,
2776 sizeof(cfg->hp_pins));
2777 cfg->hp_outs = cfg->line_outs;
2778 }
2779
2780 for (i = 0; i < cfg->hp_outs; i++) {
2781 hda_nid_t nid = cfg->hp_pins[i];
2782 if (!is_jack_detectable(codec, nid))
2783 continue;
2784 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2785 nid);
2786 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002787 spec->hp_automute_hook ?
2788 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002789 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002790 spec->detect_hp = 1;
2791 }
2792
2793 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2794 if (cfg->speaker_outs)
2795 for (i = 0; i < cfg->line_outs; i++) {
2796 hda_nid_t nid = cfg->line_out_pins[i];
2797 if (!is_jack_detectable(codec, nid))
2798 continue;
2799 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2800 snd_hda_jack_detect_enable_callback(codec, nid,
2801 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002802 spec->line_automute_hook ?
2803 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002804 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002805 spec->detect_lo = 1;
2806 }
2807 spec->automute_lo_possible = spec->detect_hp;
2808 }
2809
2810 spec->automute_speaker_possible = cfg->speaker_outs &&
2811 (spec->detect_hp || spec->detect_lo);
2812
2813 spec->automute_lo = spec->automute_lo_possible;
2814 spec->automute_speaker = spec->automute_speaker_possible;
2815
2816 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2817 /* create a control for automute mode */
2818 err = add_automute_mode_enum(codec);
2819 if (err < 0)
2820 return err;
2821 }
2822 return 0;
2823}
2824
2825/* return the position of NID in the list, or -1 if not found */
2826static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2827{
2828 int i;
2829 for (i = 0; i < nums; i++)
2830 if (list[i] == nid)
2831 return i;
2832 return -1;
2833}
2834
2835/* check whether all auto-mic pins are valid; setup indices if OK */
2836static bool auto_mic_check_imux(struct hda_codec *codec)
2837{
2838 struct hda_gen_spec *spec = codec->spec;
2839 const struct hda_input_mux *imux;
2840 int i;
2841
2842 imux = &spec->input_mux;
2843 for (i = 0; i < spec->am_num_entries; i++) {
2844 spec->am_entry[i].idx =
2845 find_idx_in_nid_list(spec->am_entry[i].pin,
2846 spec->imux_pins, imux->num_items);
2847 if (spec->am_entry[i].idx < 0)
2848 return false; /* no corresponding imux */
2849 }
2850
2851 /* we don't need the jack detection for the first pin */
2852 for (i = 1; i < spec->am_num_entries; i++)
2853 snd_hda_jack_detect_enable_callback(codec,
2854 spec->am_entry[i].pin,
2855 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002856 spec->mic_autoswitch_hook ?
2857 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002858 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002859 return true;
2860}
2861
2862static int compare_attr(const void *ap, const void *bp)
2863{
2864 const struct automic_entry *a = ap;
2865 const struct automic_entry *b = bp;
2866 return (int)(a->attr - b->attr);
2867}
2868
2869/*
2870 * Check the availability of auto-mic switch;
2871 * Set up if really supported
2872 */
2873static int check_auto_mic_availability(struct hda_codec *codec)
2874{
2875 struct hda_gen_spec *spec = codec->spec;
2876 struct auto_pin_cfg *cfg = &spec->autocfg;
2877 unsigned int types;
2878 int i, num_pins;
2879
2880 types = 0;
2881 num_pins = 0;
2882 for (i = 0; i < cfg->num_inputs; i++) {
2883 hda_nid_t nid = cfg->inputs[i].pin;
2884 unsigned int attr;
2885 attr = snd_hda_codec_get_pincfg(codec, nid);
2886 attr = snd_hda_get_input_pin_attr(attr);
2887 if (types & (1 << attr))
2888 return 0; /* already occupied */
2889 switch (attr) {
2890 case INPUT_PIN_ATTR_INT:
2891 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2892 return 0; /* invalid type */
2893 break;
2894 case INPUT_PIN_ATTR_UNUSED:
2895 return 0; /* invalid entry */
2896 default:
2897 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2898 return 0; /* invalid type */
2899 if (!spec->line_in_auto_switch &&
2900 cfg->inputs[i].type != AUTO_PIN_MIC)
2901 return 0; /* only mic is allowed */
2902 if (!is_jack_detectable(codec, nid))
2903 return 0; /* no unsol support */
2904 break;
2905 }
2906 if (num_pins >= MAX_AUTO_MIC_PINS)
2907 return 0;
2908 types |= (1 << attr);
2909 spec->am_entry[num_pins].pin = nid;
2910 spec->am_entry[num_pins].attr = attr;
2911 num_pins++;
2912 }
2913
2914 if (num_pins < 2)
2915 return 0;
2916
2917 spec->am_num_entries = num_pins;
2918 /* sort the am_entry in the order of attr so that the pin with a
2919 * higher attr will be selected when the jack is plugged.
2920 */
2921 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2922 compare_attr, NULL);
2923
2924 if (!auto_mic_check_imux(codec))
2925 return 0;
2926
2927 spec->auto_mic = 1;
2928 spec->num_adc_nids = 1;
2929 spec->cur_mux[0] = spec->am_entry[0].idx;
2930 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2931 spec->am_entry[0].pin,
2932 spec->am_entry[1].pin,
2933 spec->am_entry[2].pin);
2934
2935 return 0;
2936}
2937
2938
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002939/*
2940 * Parse the given BIOS configuration and set up the hda_gen_spec
2941 *
2942 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002943 * or a negative error code
2944 */
2945int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002946 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002947{
2948 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002949 int err;
2950
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002951 if (cfg != &spec->autocfg) {
2952 spec->autocfg = *cfg;
2953 cfg = &spec->autocfg;
2954 }
2955
Takashi Iwai352f7f92012-12-19 12:52:06 +01002956 if (!cfg->line_outs) {
2957 if (cfg->dig_outs || cfg->dig_in_pin) {
2958 spec->multiout.max_channels = 2;
2959 spec->no_analog = 1;
2960 goto dig_only;
2961 }
2962 return 0; /* can't find valid BIOS pin config */
2963 }
2964
2965 if (!spec->no_primary_hp &&
2966 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2967 cfg->line_outs <= cfg->hp_outs) {
2968 /* use HP as primary out */
2969 cfg->speaker_outs = cfg->line_outs;
2970 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2971 sizeof(cfg->speaker_pins));
2972 cfg->line_outs = cfg->hp_outs;
2973 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2974 cfg->hp_outs = 0;
2975 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2976 cfg->line_out_type = AUTO_PIN_HP_OUT;
2977 }
2978
2979 err = parse_output_paths(codec);
2980 if (err < 0)
2981 return err;
2982 err = create_multi_channel_mode(codec);
2983 if (err < 0)
2984 return err;
2985 err = create_multi_out_ctls(codec, cfg);
2986 if (err < 0)
2987 return err;
2988 err = create_hp_out_ctls(codec);
2989 if (err < 0)
2990 return err;
2991 err = create_speaker_out_ctls(codec);
2992 if (err < 0)
2993 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002994 err = create_indep_hp_ctls(codec);
2995 if (err < 0)
2996 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002997 err = create_shared_input(codec);
2998 if (err < 0)
2999 return err;
3000 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003001 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003002 return err;
3003
Takashi Iwai352f7f92012-12-19 12:52:06 +01003004 /* check the multiple speaker pins */
3005 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
3006 spec->const_channel_count = cfg->line_outs * 2;
3007 else
3008 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003009
Takashi Iwai352f7f92012-12-19 12:52:06 +01003010 if (spec->multi_ios > 0)
3011 spec->multiout.max_channels = max(spec->ext_channel_count,
3012 spec->const_channel_count);
3013 else
3014 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3015
3016 err = check_auto_mute_availability(codec);
3017 if (err < 0)
3018 return err;
3019
3020 err = check_dyn_adc_switch(codec);
3021 if (err < 0)
3022 return err;
3023
3024 if (!spec->shared_mic_hp) {
3025 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003026 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003029
Takashi Iwai352f7f92012-12-19 12:52:06 +01003030 err = create_capture_mixers(codec);
3031 if (err < 0)
3032 return err;
3033
3034 err = parse_mic_boost(codec);
3035 if (err < 0)
3036 return err;
3037
3038 dig_only:
3039 parse_digital(codec);
3040
3041 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003043EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045
3046/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003047 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003049
3050/* slave controls for virtual master */
3051static const char * const slave_pfxs[] = {
3052 "Front", "Surround", "Center", "LFE", "Side",
3053 "Headphone", "Speaker", "Mono", "Line Out",
3054 "CLFE", "Bass Speaker", "PCM",
3055 NULL,
3056};
3057
3058int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003060 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Takashi Iwai36502d02012-12-19 15:15:10 +01003063 if (spec->kctls.used) {
3064 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3065 if (err < 0)
3066 return err;
3067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Takashi Iwai352f7f92012-12-19 12:52:06 +01003069 if (spec->multiout.dig_out_nid) {
3070 err = snd_hda_create_dig_out_ctls(codec,
3071 spec->multiout.dig_out_nid,
3072 spec->multiout.dig_out_nid,
3073 spec->pcm_rec[1].pcm_type);
3074 if (err < 0)
3075 return err;
3076 if (!spec->no_analog) {
3077 err = snd_hda_create_spdif_share_sw(codec,
3078 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 if (err < 0)
3080 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003081 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
3083 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003084 if (spec->dig_in_nid) {
3085 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3086 if (err < 0)
3087 return err;
3088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Takashi Iwai352f7f92012-12-19 12:52:06 +01003090 /* if we have no master control, let's create it */
3091 if (!spec->no_analog &&
3092 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3093 unsigned int vmaster_tlv[4];
3094 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3095 HDA_OUTPUT, vmaster_tlv);
3096 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3097 vmaster_tlv, slave_pfxs,
3098 "Playback Volume");
3099 if (err < 0)
3100 return err;
3101 }
3102 if (!spec->no_analog &&
3103 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3104 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3105 NULL, slave_pfxs,
3106 "Playback Switch",
3107 true, &spec->vmaster_mute.sw_kctl);
3108 if (err < 0)
3109 return err;
3110 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003111 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3112 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Takashi Iwai352f7f92012-12-19 12:52:06 +01003115 free_kctls(spec); /* no longer needed */
3116
3117 if (spec->shared_mic_hp) {
3118 int err;
3119 int nid = spec->autocfg.inputs[1].pin;
3120 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3121 if (err < 0)
3122 return err;
3123 err = snd_hda_jack_detect_enable(codec, nid, 0);
3124 if (err < 0)
3125 return err;
3126 }
3127
3128 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3129 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 return err;
3131
3132 return 0;
3133}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003134EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003138 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Takashi Iwai352f7f92012-12-19 12:52:06 +01003141/*
3142 * Analog playback callbacks
3143 */
3144static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3145 struct hda_codec *codec,
3146 struct snd_pcm_substream *substream)
3147{
3148 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003149 int err;
3150
3151 mutex_lock(&spec->pcm_mutex);
3152 err = snd_hda_multi_out_analog_open(codec,
3153 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003154 hinfo);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003155 if (!err)
3156 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3157 mutex_unlock(&spec->pcm_mutex);
3158 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003159}
3160
3161static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003162 struct hda_codec *codec,
3163 unsigned int stream_tag,
3164 unsigned int format,
3165 struct snd_pcm_substream *substream)
3166{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003167 struct hda_gen_spec *spec = codec->spec;
3168 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3169 stream_tag, format, substream);
3170}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003171
Takashi Iwai352f7f92012-12-19 12:52:06 +01003172static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3173 struct hda_codec *codec,
3174 struct snd_pcm_substream *substream)
3175{
3176 struct hda_gen_spec *spec = codec->spec;
3177 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3178}
3179
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003180static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3181 struct hda_codec *codec,
3182 struct snd_pcm_substream *substream)
3183{
3184 struct hda_gen_spec *spec = codec->spec;
3185 mutex_lock(&spec->pcm_mutex);
3186 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3187 mutex_unlock(&spec->pcm_mutex);
3188 return 0;
3189}
3190
3191static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3192 struct hda_codec *codec,
3193 struct snd_pcm_substream *substream)
3194{
3195 struct hda_gen_spec *spec = codec->spec;
3196 int err = 0;
3197
3198 mutex_lock(&spec->pcm_mutex);
3199 if (!spec->indep_hp_enabled)
3200 err = -EBUSY;
3201 else
3202 spec->active_streams |= 1 << STREAM_INDEP_HP;
3203 mutex_unlock(&spec->pcm_mutex);
3204 return err;
3205}
3206
3207static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3208 struct hda_codec *codec,
3209 struct snd_pcm_substream *substream)
3210{
3211 struct hda_gen_spec *spec = codec->spec;
3212 mutex_lock(&spec->pcm_mutex);
3213 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3214 mutex_unlock(&spec->pcm_mutex);
3215 return 0;
3216}
3217
Takashi Iwai352f7f92012-12-19 12:52:06 +01003218/*
3219 * Digital out
3220 */
3221static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3222 struct hda_codec *codec,
3223 struct snd_pcm_substream *substream)
3224{
3225 struct hda_gen_spec *spec = codec->spec;
3226 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3227}
3228
3229static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3230 struct hda_codec *codec,
3231 unsigned int stream_tag,
3232 unsigned int format,
3233 struct snd_pcm_substream *substream)
3234{
3235 struct hda_gen_spec *spec = codec->spec;
3236 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3237 stream_tag, format, substream);
3238}
3239
3240static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3241 struct hda_codec *codec,
3242 struct snd_pcm_substream *substream)
3243{
3244 struct hda_gen_spec *spec = codec->spec;
3245 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3246}
3247
3248static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3249 struct hda_codec *codec,
3250 struct snd_pcm_substream *substream)
3251{
3252 struct hda_gen_spec *spec = codec->spec;
3253 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3254}
3255
3256/*
3257 * Analog capture
3258 */
3259static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3260 struct hda_codec *codec,
3261 unsigned int stream_tag,
3262 unsigned int format,
3263 struct snd_pcm_substream *substream)
3264{
3265 struct hda_gen_spec *spec = codec->spec;
3266
3267 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003268 stream_tag, 0, format);
3269 return 0;
3270}
3271
Takashi Iwai352f7f92012-12-19 12:52:06 +01003272static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3273 struct hda_codec *codec,
3274 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003275{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003276 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003277
Takashi Iwai352f7f92012-12-19 12:52:06 +01003278 snd_hda_codec_cleanup_stream(codec,
3279 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003280 return 0;
3281}
3282
Takashi Iwai352f7f92012-12-19 12:52:06 +01003283/*
3284 */
3285static const struct hda_pcm_stream pcm_analog_playback = {
3286 .substreams = 1,
3287 .channels_min = 2,
3288 .channels_max = 8,
3289 /* NID is set in build_pcms */
3290 .ops = {
3291 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003292 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003293 .prepare = playback_pcm_prepare,
3294 .cleanup = playback_pcm_cleanup
3295 },
3296};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
Takashi Iwai352f7f92012-12-19 12:52:06 +01003298static const struct hda_pcm_stream pcm_analog_capture = {
3299 .substreams = 1,
3300 .channels_min = 2,
3301 .channels_max = 2,
3302 /* NID is set in build_pcms */
3303};
3304
3305static const struct hda_pcm_stream pcm_analog_alt_playback = {
3306 .substreams = 1,
3307 .channels_min = 2,
3308 .channels_max = 2,
3309 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003310 .ops = {
3311 .open = alt_playback_pcm_open,
3312 .close = alt_playback_pcm_close
3313 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003314};
3315
3316static const struct hda_pcm_stream pcm_analog_alt_capture = {
3317 .substreams = 2, /* can be overridden */
3318 .channels_min = 2,
3319 .channels_max = 2,
3320 /* NID is set in build_pcms */
3321 .ops = {
3322 .prepare = alt_capture_pcm_prepare,
3323 .cleanup = alt_capture_pcm_cleanup
3324 },
3325};
3326
3327static const struct hda_pcm_stream pcm_digital_playback = {
3328 .substreams = 1,
3329 .channels_min = 2,
3330 .channels_max = 2,
3331 /* NID is set in build_pcms */
3332 .ops = {
3333 .open = dig_playback_pcm_open,
3334 .close = dig_playback_pcm_close,
3335 .prepare = dig_playback_pcm_prepare,
3336 .cleanup = dig_playback_pcm_cleanup
3337 },
3338};
3339
3340static const struct hda_pcm_stream pcm_digital_capture = {
3341 .substreams = 1,
3342 .channels_min = 2,
3343 .channels_max = 2,
3344 /* NID is set in build_pcms */
3345};
3346
3347/* Used by build_pcms to flag that a PCM has no playback stream */
3348static const struct hda_pcm_stream pcm_null_stream = {
3349 .substreams = 0,
3350 .channels_min = 0,
3351 .channels_max = 0,
3352};
3353
3354/*
3355 * dynamic changing ADC PCM streams
3356 */
3357static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3358{
3359 struct hda_gen_spec *spec = codec->spec;
3360 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3361
3362 if (spec->cur_adc && spec->cur_adc != new_adc) {
3363 /* stream is running, let's swap the current ADC */
3364 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3365 spec->cur_adc = new_adc;
3366 snd_hda_codec_setup_stream(codec, new_adc,
3367 spec->cur_adc_stream_tag, 0,
3368 spec->cur_adc_format);
3369 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003371 return false;
3372}
3373
3374/* analog capture with dynamic dual-adc changes */
3375static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3376 struct hda_codec *codec,
3377 unsigned int stream_tag,
3378 unsigned int format,
3379 struct snd_pcm_substream *substream)
3380{
3381 struct hda_gen_spec *spec = codec->spec;
3382 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3383 spec->cur_adc_stream_tag = stream_tag;
3384 spec->cur_adc_format = format;
3385 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3386 return 0;
3387}
3388
3389static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3390 struct hda_codec *codec,
3391 struct snd_pcm_substream *substream)
3392{
3393 struct hda_gen_spec *spec = codec->spec;
3394 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3395 spec->cur_adc = 0;
3396 return 0;
3397}
3398
3399static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3400 .substreams = 1,
3401 .channels_min = 2,
3402 .channels_max = 2,
3403 .nid = 0, /* fill later */
3404 .ops = {
3405 .prepare = dyn_adc_capture_pcm_prepare,
3406 .cleanup = dyn_adc_capture_pcm_cleanup
3407 },
3408};
3409
Takashi Iwaif873e532012-12-20 16:58:39 +01003410static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3411 const char *chip_name)
3412{
3413 char *p;
3414
3415 if (*str)
3416 return;
3417 strlcpy(str, chip_name, len);
3418
3419 /* drop non-alnum chars after a space */
3420 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3421 if (!isalnum(p[1])) {
3422 *p = 0;
3423 break;
3424 }
3425 }
3426 strlcat(str, sfx, len);
3427}
3428
Takashi Iwai352f7f92012-12-19 12:52:06 +01003429/* build PCM streams based on the parsed results */
3430int snd_hda_gen_build_pcms(struct hda_codec *codec)
3431{
3432 struct hda_gen_spec *spec = codec->spec;
3433 struct hda_pcm *info = spec->pcm_rec;
3434 const struct hda_pcm_stream *p;
3435 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
3437 codec->num_pcms = 1;
3438 codec->pcm_info = info;
3439
Takashi Iwai352f7f92012-12-19 12:52:06 +01003440 if (spec->no_analog)
3441 goto skip_analog;
3442
Takashi Iwaif873e532012-12-20 16:58:39 +01003443 fill_pcm_stream_name(spec->stream_name_analog,
3444 sizeof(spec->stream_name_analog),
3445 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003446 info->name = spec->stream_name_analog;
3447
3448 if (spec->multiout.num_dacs > 0) {
3449 p = spec->stream_analog_playback;
3450 if (!p)
3451 p = &pcm_analog_playback;
3452 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3453 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3454 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3455 spec->multiout.max_channels;
3456 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3457 spec->autocfg.line_outs == 2)
3458 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3459 snd_pcm_2_1_chmaps;
3460 }
3461 if (spec->num_adc_nids) {
3462 p = spec->stream_analog_capture;
3463 if (!p) {
3464 if (spec->dyn_adc_switch)
3465 p = &dyn_adc_pcm_analog_capture;
3466 else
3467 p = &pcm_analog_capture;
3468 }
3469 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3470 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3471 }
3472
Takashi Iwai352f7f92012-12-19 12:52:06 +01003473 skip_analog:
3474 /* SPDIF for stream index #1 */
3475 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003476 fill_pcm_stream_name(spec->stream_name_digital,
3477 sizeof(spec->stream_name_digital),
3478 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003479 codec->num_pcms = 2;
3480 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3481 info = spec->pcm_rec + 1;
3482 info->name = spec->stream_name_digital;
3483 if (spec->dig_out_type)
3484 info->pcm_type = spec->dig_out_type;
3485 else
3486 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3487 if (spec->multiout.dig_out_nid) {
3488 p = spec->stream_digital_playback;
3489 if (!p)
3490 p = &pcm_digital_playback;
3491 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3492 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3493 }
3494 if (spec->dig_in_nid) {
3495 p = spec->stream_digital_capture;
3496 if (!p)
3497 p = &pcm_digital_capture;
3498 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3499 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3500 }
3501 }
3502
3503 if (spec->no_analog)
3504 return 0;
3505
3506 /* If the use of more than one ADC is requested for the current
3507 * model, configure a second analog capture-only PCM.
3508 */
3509 have_multi_adcs = (spec->num_adc_nids > 1) &&
3510 !spec->dyn_adc_switch && !spec->auto_mic;
3511 /* Additional Analaog capture for index #2 */
3512 if (spec->alt_dac_nid || have_multi_adcs) {
3513 codec->num_pcms = 3;
3514 info = spec->pcm_rec + 2;
3515 info->name = spec->stream_name_analog;
3516 if (spec->alt_dac_nid) {
3517 p = spec->stream_analog_alt_playback;
3518 if (!p)
3519 p = &pcm_analog_alt_playback;
3520 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3521 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3522 spec->alt_dac_nid;
3523 } else {
3524 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3525 pcm_null_stream;
3526 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3527 }
3528 if (have_multi_adcs) {
3529 p = spec->stream_analog_alt_capture;
3530 if (!p)
3531 p = &pcm_analog_alt_capture;
3532 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3533 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3534 spec->adc_nids[1];
3535 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3536 spec->num_adc_nids - 1;
3537 } else {
3538 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3539 pcm_null_stream;
3540 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 }
3543
3544 return 0;
3545}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003546EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3547
3548
3549/*
3550 * Standard auto-parser initializations
3551 */
3552
3553/* configure the path from the given dac to the pin as the proper output */
3554static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3555 int pin_type, hda_nid_t dac)
3556{
3557 struct nid_path *path;
3558
3559 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3560 path = snd_hda_get_nid_path(codec, dac, pin);
3561 if (!path)
3562 return;
Takashi Iwaie1284af2013-01-03 16:33:02 +01003563 snd_hda_activate_path(codec, path, path->active, true);
3564 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003565}
3566
3567/* initialize primary output paths */
3568static void init_multi_out(struct hda_codec *codec)
3569{
3570 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai64049c82012-12-20 15:29:21 +01003571 hda_nid_t nid, dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003572 int pin_type;
3573 int i;
3574
3575 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3576 pin_type = PIN_HP;
3577 else
3578 pin_type = PIN_OUT;
3579
Takashi Iwai64049c82012-12-20 15:29:21 +01003580 for (i = 0; i < spec->autocfg.line_outs; i++) {
3581 nid = spec->autocfg.line_out_pins[i];
3582 if (nid) {
3583 dac = spec->multiout.dac_nids[i];
3584 if (!dac)
3585 dac = spec->multiout.dac_nids[0];
3586 set_output_and_unmute(codec, nid, pin_type, dac);
3587 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003588 }
3589}
3590
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003591
3592static void __init_extra_out(struct hda_codec *codec, int num_outs,
3593 hda_nid_t *pins, hda_nid_t *dacs, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003594{
3595 struct hda_gen_spec *spec = codec->spec;
3596 int i;
3597 hda_nid_t pin, dac;
3598
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003599 for (i = 0; i < num_outs; i++) {
3600 pin = pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003601 if (!pin)
3602 break;
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003603 dac = dacs[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003604 if (!dac) {
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003605 if (i > 0 && dacs[0])
3606 dac = dacs[0];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003607 else
3608 dac = spec->multiout.dac_nids[0];
3609 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003610 set_output_and_unmute(codec, pin, type, dac);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003611 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003612}
3613
3614/* initialize hp and speaker paths */
3615static void init_extra_out(struct hda_codec *codec)
3616{
3617 struct hda_gen_spec *spec = codec->spec;
3618
3619 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3620 __init_extra_out(codec, spec->autocfg.hp_outs,
3621 spec->autocfg.hp_pins,
3622 spec->multiout.hp_out_nid, PIN_HP);
3623 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3624 __init_extra_out(codec, spec->autocfg.speaker_outs,
3625 spec->autocfg.speaker_pins,
3626 spec->multiout.extra_out_nid, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003627}
3628
3629/* initialize multi-io paths */
3630static void init_multi_io(struct hda_codec *codec)
3631{
3632 struct hda_gen_spec *spec = codec->spec;
3633 int i;
3634
3635 for (i = 0; i < spec->multi_ios; i++) {
3636 hda_nid_t pin = spec->multi_io[i].pin;
3637 struct nid_path *path;
3638 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3639 if (!path)
3640 continue;
3641 if (!spec->multi_io[i].ctl_in)
3642 spec->multi_io[i].ctl_in =
3643 snd_hda_codec_update_cache(codec, pin, 0,
3644 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3645 snd_hda_activate_path(codec, path, path->active, true);
3646 }
3647}
3648
3649/* set up the input pin config, depending on the given auto-pin type */
3650static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3651 int auto_pin_type)
3652{
3653 unsigned int val = PIN_IN;
3654 if (auto_pin_type == AUTO_PIN_MIC)
3655 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003656 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003657}
3658
3659/* set up input pins and loopback paths */
3660static void init_analog_input(struct hda_codec *codec)
3661{
3662 struct hda_gen_spec *spec = codec->spec;
3663 struct auto_pin_cfg *cfg = &spec->autocfg;
3664 int i;
3665
3666 for (i = 0; i < cfg->num_inputs; i++) {
3667 hda_nid_t nid = cfg->inputs[i].pin;
3668 if (is_input_pin(codec, nid))
3669 set_input_pin(codec, nid, cfg->inputs[i].type);
3670
3671 /* init loopback inputs */
3672 if (spec->mixer_nid) {
3673 struct nid_path *path;
3674 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3675 if (path)
3676 snd_hda_activate_path(codec, path,
3677 path->active, false);
3678 }
3679 }
3680}
3681
3682/* initialize ADC paths */
3683static void init_input_src(struct hda_codec *codec)
3684{
3685 struct hda_gen_spec *spec = codec->spec;
3686 struct hda_input_mux *imux = &spec->input_mux;
3687 struct nid_path *path;
3688 int i, c, nums;
3689
3690 if (spec->dyn_adc_switch)
3691 nums = 1;
3692 else
3693 nums = spec->num_adc_nids;
3694
3695 for (c = 0; c < nums; c++) {
3696 for (i = 0; i < imux->num_items; i++) {
3697 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3698 get_adc_nid(codec, c, i));
3699 if (path) {
3700 bool active = path->active;
3701 if (i == spec->cur_mux[c])
3702 active = true;
3703 snd_hda_activate_path(codec, path, active, false);
3704 }
3705 }
3706 }
3707
3708 if (spec->shared_mic_hp)
3709 update_shared_mic_hp(codec, spec->cur_mux[0]);
3710
3711 if (spec->cap_sync_hook)
3712 spec->cap_sync_hook(codec);
3713}
3714
3715/* set right pin controls for digital I/O */
3716static void init_digital(struct hda_codec *codec)
3717{
3718 struct hda_gen_spec *spec = codec->spec;
3719 int i;
3720 hda_nid_t pin;
3721
3722 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3723 pin = spec->autocfg.dig_out_pins[i];
3724 if (!pin)
3725 continue;
3726 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3727 }
3728 pin = spec->autocfg.dig_in_pin;
3729 if (pin)
Takashi Iwai7594aa32012-12-20 15:38:40 +01003730 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003731}
3732
Takashi Iwai973e4972012-12-20 15:16:09 +01003733/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3734 * invalid unsol tags by some reason
3735 */
3736static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3737{
3738 int i;
3739
3740 for (i = 0; i < codec->init_pins.used; i++) {
3741 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3742 hda_nid_t nid = pin->nid;
3743 if (is_jack_detectable(codec, nid) &&
3744 !snd_hda_jack_tbl_get(codec, nid))
3745 snd_hda_codec_update_cache(codec, nid, 0,
3746 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3747 }
3748}
3749
Takashi Iwai352f7f92012-12-19 12:52:06 +01003750int snd_hda_gen_init(struct hda_codec *codec)
3751{
3752 struct hda_gen_spec *spec = codec->spec;
3753
3754 if (spec->init_hook)
3755 spec->init_hook(codec);
3756
3757 snd_hda_apply_verbs(codec);
3758
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003759 codec->cached_write = 1;
3760
Takashi Iwai352f7f92012-12-19 12:52:06 +01003761 init_multi_out(codec);
3762 init_extra_out(codec);
3763 init_multi_io(codec);
3764 init_analog_input(codec);
3765 init_input_src(codec);
3766 init_digital(codec);
3767
Takashi Iwai973e4972012-12-20 15:16:09 +01003768 clear_unsol_on_unused_pins(codec);
3769
Takashi Iwai352f7f92012-12-19 12:52:06 +01003770 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003771 snd_hda_gen_hp_automute(codec, NULL);
3772 snd_hda_gen_line_automute(codec, NULL);
3773 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003774
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003775 snd_hda_codec_flush_amp_cache(codec);
3776 snd_hda_codec_flush_cmd_cache(codec);
3777
Takashi Iwai352f7f92012-12-19 12:52:06 +01003778 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3779 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3780
3781 hda_call_check_power_status(codec, 0x01);
3782 return 0;
3783}
3784EXPORT_SYMBOL(snd_hda_gen_init);
3785
3786
3787/*
3788 * the generic codec support
3789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
Takashi Iwai83012a72012-08-24 18:38:08 +02003791#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003792static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3793{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003794 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003795 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3796}
3797#endif
3798
Takashi Iwai352f7f92012-12-19 12:52:06 +01003799static void generic_free(struct hda_codec *codec)
3800{
3801 snd_hda_gen_spec_free(codec->spec);
3802 kfree(codec->spec);
3803 codec->spec = NULL;
3804}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Takashi Iwai352f7f92012-12-19 12:52:06 +01003806static const struct hda_codec_ops generic_patch_ops = {
3807 .build_controls = snd_hda_gen_build_controls,
3808 .build_pcms = snd_hda_gen_build_pcms,
3809 .init = snd_hda_gen_init,
3810 .free = generic_free,
3811 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02003812#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003813 .check_power_status = generic_check_power_status,
3814#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815};
3816
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817int snd_hda_parse_generic_codec(struct hda_codec *codec)
3818{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003819 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 int err;
3821
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003822 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003823 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003825 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003828 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3829 if (err < 0)
3830 return err;
3831
3832 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003833 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 goto error;
3835
3836 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 return 0;
3838
Takashi Iwai352f7f92012-12-19 12:52:06 +01003839error:
3840 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 return err;
3842}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003843EXPORT_SYMBOL(snd_hda_parse_generic_codec);