blob: f4fa60481978caa5a39b377966a08df29dcb76bf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040025#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010026#include <linux/sort.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010027#include <linux/ctype.h>
28#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010030#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "hda_codec.h"
32#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include "hda_auto_parser.h"
34#include "hda_jack.h"
35#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwai352f7f92012-12-19 12:52:06 +010038/* initialize hda_gen_spec struct */
39int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Takashi Iwai352f7f92012-12-19 12:52:06 +010041 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42 snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
43 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010044 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010045 return 0;
46}
47EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Takashi Iwai12c93df2012-12-19 14:38:33 +010049struct snd_kcontrol_new *
50snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
51 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010052{
53 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
54 if (!knew)
55 return NULL;
56 *knew = *temp;
57 if (name)
58 knew->name = kstrdup(name, GFP_KERNEL);
59 else if (knew->name)
60 knew->name = kstrdup(knew->name, GFP_KERNEL);
61 if (!knew->name)
62 return NULL;
63 return knew;
64}
Takashi Iwai12c93df2012-12-19 14:38:33 +010065EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010066
67static void free_kctls(struct hda_gen_spec *spec)
68{
69 if (spec->kctls.list) {
70 struct snd_kcontrol_new *kctl = spec->kctls.list;
71 int i;
72 for (i = 0; i < spec->kctls.used; i++)
73 kfree(kctl[i].name);
74 }
75 snd_array_free(&spec->kctls);
76}
77
78static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
79 unsigned int nums,
80 struct hda_ctl_ops *ops)
81{
82 struct hda_gen_spec *spec = codec->spec;
83 struct hda_bind_ctls **ctlp, *ctl;
84 ctlp = snd_array_new(&spec->bind_ctls);
85 if (!ctlp)
86 return NULL;
87 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
88 *ctlp = ctl;
89 if (ctl)
90 ctl->ops = ops;
91 return ctl;
92}
93
94static void free_bind_ctls(struct hda_gen_spec *spec)
95{
96 if (spec->bind_ctls.list) {
97 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
98 int i;
99 for (i = 0; i < spec->bind_ctls.used; i++)
100 kfree(ctl[i]);
101 }
102 snd_array_free(&spec->bind_ctls);
103}
104
105void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
106{
107 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100109 free_kctls(spec);
110 free_bind_ctls(spec);
111 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100113EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100116 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Takashi Iwaif5172a72013-01-04 13:19:55 +0100119static struct nid_path *get_nid_path(struct hda_codec *codec,
120 hda_nid_t from_nid, hda_nid_t to_nid,
121 int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100123 struct hda_gen_spec *spec = codec->spec;
124 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Takashi Iwai352f7f92012-12-19 12:52:06 +0100126 for (i = 0; i < spec->paths.used; i++) {
127 struct nid_path *path = snd_array_elem(&spec->paths, i);
128 if (path->depth <= 0)
129 continue;
130 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100131 (!to_nid || path->path[path->depth - 1] == to_nid)) {
132 if (with_aa_mix == HDA_PARSE_ALL ||
133 path->with_aa_mix == with_aa_mix)
134 return path;
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137 return NULL;
138}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100139
140/* get the path between the given NIDs;
141 * passing 0 to either @pin or @dac behaves as a wildcard
142 */
143struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
144 hda_nid_t from_nid, hda_nid_t to_nid)
145{
146 return get_nid_path(codec, from_nid, to_nid, HDA_PARSE_ALL);
147}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100148EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
149
Takashi Iwai196c17662013-01-04 15:01:40 +0100150/* get the index number corresponding to the path instance;
151 * the index starts from 1, for easier checking the invalid value
152 */
153int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
154{
155 struct hda_gen_spec *spec = codec->spec;
156 struct nid_path *array = spec->paths.list;
157 ssize_t idx;
158
159 if (!spec->paths.used)
160 return 0;
161 idx = path - array;
162 if (idx < 0 || idx >= spec->paths.used)
163 return 0;
164 return idx + 1;
165}
166
167/* get the path instance corresponding to the given index number */
168struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
169{
170 struct hda_gen_spec *spec = codec->spec;
171
172 if (idx <= 0 || idx > spec->paths.used)
173 return NULL;
174 return snd_array_elem(&spec->paths, idx - 1);
175}
176
Takashi Iwai352f7f92012-12-19 12:52:06 +0100177/* check whether the given DAC is already found in any existing paths */
178static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
179{
180 struct hda_gen_spec *spec = codec->spec;
181 int i;
182
183 for (i = 0; i < spec->paths.used; i++) {
184 struct nid_path *path = snd_array_elem(&spec->paths, i);
185 if (path->path[0] == nid)
186 return true;
187 }
188 return false;
189}
190
191/* check whether the given two widgets can be connected */
192static bool is_reachable_path(struct hda_codec *codec,
193 hda_nid_t from_nid, hda_nid_t to_nid)
194{
195 if (!from_nid || !to_nid)
196 return false;
197 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
198}
199
200/* nid, dir and idx */
201#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
202
203/* check whether the given ctl is already assigned in any path elements */
204static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
205{
206 struct hda_gen_spec *spec = codec->spec;
207 int i;
208
209 val &= AMP_VAL_COMPARE_MASK;
210 for (i = 0; i < spec->paths.used; i++) {
211 struct nid_path *path = snd_array_elem(&spec->paths, i);
212 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
213 return true;
214 }
215 return false;
216}
217
218/* check whether a control with the given (nid, dir, idx) was assigned */
219static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
220 int dir, int idx)
221{
222 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
223 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
224 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
225}
226
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100227static void print_nid_path(const char *pfx, struct nid_path *path)
228{
229 char buf[40];
230 int i;
231
232
233 buf[0] = 0;
234 for (i = 0; i < path->depth; i++) {
235 char tmp[4];
236 sprintf(tmp, ":%02x", path->path[i]);
237 strlcat(buf, tmp, sizeof(buf));
238 }
239 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
240}
241
Takashi Iwai352f7f92012-12-19 12:52:06 +0100242/* called recursively */
243static bool __parse_nid_path(struct hda_codec *codec,
244 hda_nid_t from_nid, hda_nid_t to_nid,
245 int with_aa_mix, struct nid_path *path, int depth)
246{
247 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100248 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100249 int i, nums;
250
251 if (to_nid == spec->mixer_nid) {
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100252 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100253 return false;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100254 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100255 }
256
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100257 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100258 for (i = 0; i < nums; i++) {
259 if (conn[i] != from_nid) {
260 /* special case: when from_nid is 0,
261 * try to find an empty DAC
262 */
263 if (from_nid ||
264 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
265 is_dac_already_used(codec, conn[i]))
266 continue;
267 }
268 /* aa-mix is requested but not included? */
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100269 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100270 goto found;
271 }
272 if (depth >= MAX_NID_PATH_DEPTH)
273 return false;
274 for (i = 0; i < nums; i++) {
275 unsigned int type;
276 type = get_wcaps_type(get_wcaps(codec, conn[i]));
277 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
278 type == AC_WID_PIN)
279 continue;
280 if (__parse_nid_path(codec, from_nid, conn[i],
281 with_aa_mix, path, depth + 1))
282 goto found;
283 }
284 return false;
285
286 found:
287 path->path[path->depth] = conn[i];
Takashi Iwaif5172a72013-01-04 13:19:55 +0100288 if (conn[i] == spec->mixer_nid)
289 path->with_aa_mix = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100290 path->idx[path->depth + 1] = i;
291 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
292 path->multi[path->depth + 1] = 1;
293 path->depth++;
294 return true;
295}
296
297/* parse the widget path from the given nid to the target nid;
298 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100299 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
300 * excluded, only the paths that don't go through the mixer will be chosen.
301 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
302 * spec->mixer_nid will be chosen.
303 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100304 */
305bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
306 hda_nid_t to_nid, int with_aa_mix,
307 struct nid_path *path)
308{
309 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
310 path->path[path->depth] = to_nid;
311 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100312 return true;
313 }
314 return false;
315}
316EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100319 * parse the path between the given NIDs and add to the path list.
320 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100322struct nid_path *
323snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
324 hda_nid_t to_nid, int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100326 struct hda_gen_spec *spec = codec->spec;
327 struct nid_path *path;
328
329 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
330 return NULL;
331
Takashi Iwaif5172a72013-01-04 13:19:55 +0100332 /* check whether the path has been already added */
333 path = get_nid_path(codec, from_nid, to_nid, with_aa_mix);
334 if (path)
335 return path;
336
Takashi Iwai352f7f92012-12-19 12:52:06 +0100337 path = snd_array_new(&spec->paths);
338 if (!path)
339 return NULL;
340 memset(path, 0, sizeof(*path));
341 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
342 return path;
343 /* push back */
344 spec->paths.used--;
345 return NULL;
346}
347EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
348
349/* look for an empty DAC slot */
350static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
351 bool is_digital)
352{
353 struct hda_gen_spec *spec = codec->spec;
354 bool cap_digital;
355 int i;
356
357 for (i = 0; i < spec->num_all_dacs; i++) {
358 hda_nid_t nid = spec->all_dacs[i];
359 if (!nid || is_dac_already_used(codec, nid))
360 continue;
361 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
362 if (is_digital != cap_digital)
363 continue;
364 if (is_reachable_path(codec, nid, pin))
365 return nid;
366 }
367 return 0;
368}
369
370/* replace the channels in the composed amp value with the given number */
371static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
372{
373 val &= ~(0x3U << 16);
374 val |= chs << 16;
375 return val;
376}
377
378/* check whether the widget has the given amp capability for the direction */
379static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
380 int dir, unsigned int bits)
381{
382 if (!nid)
383 return false;
384 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
385 if (query_amp_caps(codec, nid, dir) & bits)
386 return true;
387 return false;
388}
389
390#define nid_has_mute(codec, nid, dir) \
391 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
392#define nid_has_volume(codec, nid, dir) \
393 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
394
395/* look for a widget suitable for assigning a mute switch in the path */
396static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
397 struct nid_path *path)
398{
399 int i;
400
401 for (i = path->depth - 1; i >= 0; i--) {
402 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
403 return path->path[i];
404 if (i != path->depth - 1 && i != 0 &&
405 nid_has_mute(codec, path->path[i], HDA_INPUT))
406 return path->path[i];
407 }
408 return 0;
409}
410
411/* look for a widget suitable for assigning a volume ctl in the path */
412static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
413 struct nid_path *path)
414{
415 int i;
416
417 for (i = path->depth - 1; i >= 0; i--) {
418 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
419 return path->path[i];
420 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100425 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100427
428/* can have the amp-in capability? */
429static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100431 hda_nid_t nid = path->path[idx];
432 unsigned int caps = get_wcaps(codec, nid);
433 unsigned int type = get_wcaps_type(caps);
434
435 if (!(caps & AC_WCAP_IN_AMP))
436 return false;
437 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
438 return false;
439 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Takashi Iwai352f7f92012-12-19 12:52:06 +0100442/* can have the amp-out capability? */
443static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100445 hda_nid_t nid = path->path[idx];
446 unsigned int caps = get_wcaps(codec, nid);
447 unsigned int type = get_wcaps_type(caps);
448
449 if (!(caps & AC_WCAP_OUT_AMP))
450 return false;
451 if (type == AC_WID_PIN && !idx) /* only for output pins */
452 return false;
453 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Takashi Iwai352f7f92012-12-19 12:52:06 +0100456/* check whether the given (nid,dir,idx) is active */
457static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
458 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100460 struct hda_gen_spec *spec = codec->spec;
461 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Takashi Iwai352f7f92012-12-19 12:52:06 +0100463 for (n = 0; n < spec->paths.used; n++) {
464 struct nid_path *path = snd_array_elem(&spec->paths, n);
465 if (!path->active)
466 continue;
467 for (i = 0; i < path->depth; i++) {
468 if (path->path[i] == nid) {
469 if (dir == HDA_OUTPUT || path->idx[i] == idx)
470 return true;
471 break;
472 }
473 }
474 }
475 return false;
476}
477
478/* get the default amp value for the target state */
479static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
480 int dir, bool enable)
481{
482 unsigned int caps;
483 unsigned int val = 0;
484
485 caps = query_amp_caps(codec, nid, dir);
486 if (caps & AC_AMPCAP_NUM_STEPS) {
487 /* set to 0dB */
488 if (enable)
489 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
490 }
491 if (caps & AC_AMPCAP_MUTE) {
492 if (!enable)
493 val |= HDA_AMP_MUTE;
494 }
495 return val;
496}
497
498/* initialize the amp value (only at the first time) */
499static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
500{
501 int val = get_amp_val_to_activate(codec, nid, dir, false);
502 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
503}
504
505static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
506 int idx, bool enable)
507{
508 int val;
509 if (is_ctl_associated(codec, nid, dir, idx) ||
Takashi Iwai985803c2013-01-03 16:30:04 +0100510 (!enable && is_active_nid(codec, nid, dir, idx)))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100511 return;
512 val = get_amp_val_to_activate(codec, nid, dir, enable);
513 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
514}
515
516static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
517 int i, bool enable)
518{
519 hda_nid_t nid = path->path[i];
520 init_amp(codec, nid, HDA_OUTPUT, 0);
521 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
522}
523
524static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
525 int i, bool enable, bool add_aamix)
526{
527 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100528 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100529 int n, nums, idx;
530 int type;
531 hda_nid_t nid = path->path[i];
532
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100533 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100534 type = get_wcaps_type(get_wcaps(codec, nid));
535 if (type == AC_WID_PIN ||
536 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
537 nums = 1;
538 idx = 0;
539 } else
540 idx = path->idx[i];
541
542 for (n = 0; n < nums; n++)
543 init_amp(codec, nid, HDA_INPUT, n);
544
545 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
546 return;
547
548 /* here is a little bit tricky in comparison with activate_amp_out();
549 * when aa-mixer is available, we need to enable the path as well
550 */
551 for (n = 0; n < nums; n++) {
552 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
553 continue;
554 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556}
557
Takashi Iwai352f7f92012-12-19 12:52:06 +0100558/* activate or deactivate the given path
559 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100561void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
562 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100564 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Takashi Iwai352f7f92012-12-19 12:52:06 +0100566 if (!enable)
567 path->active = false;
568
569 for (i = path->depth - 1; i >= 0; i--) {
570 if (enable && path->multi[i])
571 snd_hda_codec_write_cache(codec, path->path[i], 0,
572 AC_VERB_SET_CONNECT_SEL,
573 path->idx[i]);
574 if (has_amp_in(codec, path, i))
575 activate_amp_in(codec, path, i, enable, add_aamix);
576 if (has_amp_out(codec, path, i))
577 activate_amp_out(codec, path, i, enable);
578 }
579
580 if (enable)
581 path->active = true;
582}
583EXPORT_SYMBOL_HDA(snd_hda_activate_path);
584
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100585/* turn on/off EAPD on the given pin */
586static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
587{
588 struct hda_gen_spec *spec = codec->spec;
589 if (spec->own_eapd_ctl ||
590 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
591 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100592 if (codec->inv_eapd)
593 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100594 snd_hda_codec_update_cache(codec, pin, 0,
595 AC_VERB_SET_EAPD_BTLENABLE,
596 enable ? 0x02 : 0x00);
597}
598
Takashi Iwai352f7f92012-12-19 12:52:06 +0100599
600/*
601 * Helper functions for creating mixer ctl elements
602 */
603
604enum {
605 HDA_CTL_WIDGET_VOL,
606 HDA_CTL_WIDGET_MUTE,
607 HDA_CTL_BIND_MUTE,
608 HDA_CTL_BIND_VOL,
609 HDA_CTL_BIND_SW,
610};
611static const struct snd_kcontrol_new control_templates[] = {
612 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
613 HDA_CODEC_MUTE(NULL, 0, 0, 0),
614 HDA_BIND_MUTE(NULL, 0, 0, 0),
615 HDA_BIND_VOL(NULL, 0),
616 HDA_BIND_SW(NULL, 0),
617};
618
619/* add dynamic controls from template */
620static int add_control(struct hda_gen_spec *spec, int type, const char *name,
621 int cidx, unsigned long val)
622{
623 struct snd_kcontrol_new *knew;
624
Takashi Iwai12c93df2012-12-19 14:38:33 +0100625 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100626 if (!knew)
627 return -ENOMEM;
628 knew->index = cidx;
629 if (get_amp_nid_(val))
630 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
631 knew->private_value = val;
632 return 0;
633}
634
635static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
636 const char *pfx, const char *dir,
637 const char *sfx, int cidx, unsigned long val)
638{
639 char name[32];
640 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
641 return add_control(spec, type, name, cidx, val);
642}
643
644#define add_pb_vol_ctrl(spec, type, pfx, val) \
645 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
646#define add_pb_sw_ctrl(spec, type, pfx, val) \
647 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
648#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
649 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
650#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
651 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
652
653static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
654 unsigned int chs, struct nid_path *path)
655{
656 unsigned int val;
657 if (!path)
658 return 0;
659 val = path->ctls[NID_PATH_VOL_CTL];
660 if (!val)
661 return 0;
662 val = amp_val_replace_channels(val, chs);
663 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
664}
665
666/* return the channel bits suitable for the given path->ctls[] */
667static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
668 int type)
669{
670 int chs = 1; /* mono (left only) */
671 if (path) {
672 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
673 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
674 chs = 3; /* stereo */
675 }
676 return chs;
677}
678
679static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
680 struct nid_path *path)
681{
682 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
683 return add_vol_ctl(codec, pfx, cidx, chs, path);
684}
685
686/* create a mute-switch for the given mixer widget;
687 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
688 */
689static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
690 unsigned int chs, struct nid_path *path)
691{
692 unsigned int val;
693 int type = HDA_CTL_WIDGET_MUTE;
694
695 if (!path)
696 return 0;
697 val = path->ctls[NID_PATH_MUTE_CTL];
698 if (!val)
699 return 0;
700 val = amp_val_replace_channels(val, chs);
701 if (get_amp_direction_(val) == HDA_INPUT) {
702 hda_nid_t nid = get_amp_nid_(val);
703 int nums = snd_hda_get_num_conns(codec, nid);
704 if (nums > 1) {
705 type = HDA_CTL_BIND_MUTE;
706 val |= nums << 19;
707 }
708 }
709 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
710}
711
712static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
713 int cidx, struct nid_path *path)
714{
715 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
716 return add_sw_ctl(codec, pfx, cidx, chs, path);
717}
718
719static const char * const channel_name[4] = {
720 "Front", "Surround", "CLFE", "Side"
721};
722
723/* give some appropriate ctl name prefix for the given line out channel */
724static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
725 bool can_be_master, int *index)
726{
727 struct auto_pin_cfg *cfg = &spec->autocfg;
728
729 *index = 0;
730 if (cfg->line_outs == 1 && !spec->multi_ios &&
731 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
732 return spec->vmaster_mute.hook ? "PCM" : "Master";
733
734 /* if there is really a single DAC used in the whole output paths,
735 * use it master (or "PCM" if a vmaster hook is present)
736 */
737 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
738 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
739 return spec->vmaster_mute.hook ? "PCM" : "Master";
740
741 switch (cfg->line_out_type) {
742 case AUTO_PIN_SPEAKER_OUT:
743 if (cfg->line_outs == 1)
744 return "Speaker";
745 if (cfg->line_outs == 2)
746 return ch ? "Bass Speaker" : "Speaker";
747 break;
748 case AUTO_PIN_HP_OUT:
749 /* for multi-io case, only the primary out */
750 if (ch && spec->multi_ios)
751 break;
752 *index = ch;
753 return "Headphone";
754 default:
755 if (cfg->line_outs == 1 && !spec->multi_ios)
756 return "PCM";
757 break;
758 }
759 if (ch >= ARRAY_SIZE(channel_name)) {
760 snd_BUG();
761 return "PCM";
762 }
763
764 return channel_name[ch];
765}
766
767/*
768 * Parse output paths
769 */
770
771/* badness definition */
772enum {
773 /* No primary DAC is found for the main output */
774 BAD_NO_PRIMARY_DAC = 0x10000,
775 /* No DAC is found for the extra output */
776 BAD_NO_DAC = 0x4000,
777 /* No possible multi-ios */
778 BAD_MULTI_IO = 0x103,
779 /* No individual DAC for extra output */
780 BAD_NO_EXTRA_DAC = 0x102,
781 /* No individual DAC for extra surrounds */
782 BAD_NO_EXTRA_SURR_DAC = 0x101,
783 /* Primary DAC shared with main surrounds */
784 BAD_SHARED_SURROUND = 0x100,
785 /* Primary DAC shared with main CLFE */
786 BAD_SHARED_CLFE = 0x10,
787 /* Primary DAC shared with extra surrounds */
788 BAD_SHARED_EXTRA_SURROUND = 0x10,
789 /* Volume widget is shared */
790 BAD_SHARED_VOL = 0x10,
791};
792
793/* look for widgets in the path between the given NIDs appropriate for
794 * volume and mute controls, and assign the values to ctls[].
795 *
796 * When no appropriate widget is found in the path, the badness value
797 * is incremented depending on the situation. The function returns the
798 * total badness for both volume and mute controls.
799 */
800static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
801 hda_nid_t dac)
802{
803 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
804 hda_nid_t nid;
805 unsigned int val;
806 int badness = 0;
807
808 if (!path)
809 return BAD_SHARED_VOL * 2;
810 nid = look_for_out_vol_nid(codec, path);
811 if (nid) {
812 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
813 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
814 badness += BAD_SHARED_VOL;
815 else
816 path->ctls[NID_PATH_VOL_CTL] = val;
817 } else
818 badness += BAD_SHARED_VOL;
819 nid = look_for_out_mute_nid(codec, path);
820 if (nid) {
821 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
822 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
823 nid_has_mute(codec, nid, HDA_OUTPUT))
824 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
825 else
826 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
827 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
828 badness += BAD_SHARED_VOL;
829 else
830 path->ctls[NID_PATH_MUTE_CTL] = val;
831 } else
832 badness += BAD_SHARED_VOL;
833 return badness;
834}
835
836struct badness_table {
837 int no_primary_dac; /* no primary DAC */
838 int no_dac; /* no secondary DACs */
839 int shared_primary; /* primary DAC is shared with main output */
840 int shared_surr; /* secondary DAC shared with main or primary */
841 int shared_clfe; /* third DAC shared with main or primary */
842 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
843};
844
845static struct badness_table main_out_badness = {
846 .no_primary_dac = BAD_NO_PRIMARY_DAC,
847 .no_dac = BAD_NO_DAC,
848 .shared_primary = BAD_NO_PRIMARY_DAC,
849 .shared_surr = BAD_SHARED_SURROUND,
850 .shared_clfe = BAD_SHARED_CLFE,
851 .shared_surr_main = BAD_SHARED_SURROUND,
852};
853
854static struct badness_table extra_out_badness = {
855 .no_primary_dac = BAD_NO_DAC,
856 .no_dac = BAD_NO_DAC,
857 .shared_primary = BAD_NO_EXTRA_DAC,
858 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
859 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
860 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
861};
862
Takashi Iwai7385df62013-01-07 09:50:52 +0100863/* get the DAC of the primary output corresponding to the given array index */
864static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
865{
866 struct hda_gen_spec *spec = codec->spec;
867 struct auto_pin_cfg *cfg = &spec->autocfg;
868
869 if (cfg->line_outs > idx)
870 return spec->private_dac_nids[idx];
871 idx -= cfg->line_outs;
872 if (spec->multi_ios > idx)
873 return spec->multi_io[idx].dac;
874 return 0;
875}
876
877/* return the DAC if it's reachable, otherwise zero */
878static inline hda_nid_t try_dac(struct hda_codec *codec,
879 hda_nid_t dac, hda_nid_t pin)
880{
881 return is_reachable_path(codec, dac, pin) ? dac : 0;
882}
883
Takashi Iwai352f7f92012-12-19 12:52:06 +0100884/* try to assign DACs to pins and return the resultant badness */
885static int try_assign_dacs(struct hda_codec *codec, int num_outs,
886 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +0100887 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100888 const struct badness_table *bad)
889{
890 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100891 int i, j;
892 int badness = 0;
893 hda_nid_t dac;
894
895 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return 0;
897
Takashi Iwai352f7f92012-12-19 12:52:06 +0100898 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100899 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100900 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +0100901
902 if (dacs[i]) {
903 badness += assign_out_path_ctls(codec, pin, dacs[i]);
904 continue;
905 }
906
907 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100908 if (!dacs[i] && !i) {
909 for (j = 1; j < num_outs; j++) {
910 if (is_reachable_path(codec, dacs[j], pin)) {
911 dacs[0] = dacs[j];
912 dacs[j] = 0;
Takashi Iwai196c17662013-01-04 15:01:40 +0100913 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100914 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100917 }
918 dac = dacs[i];
919 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +0100920 if (num_outs > 2)
921 dac = try_dac(codec, get_primary_out(codec, i), pin);
922 if (!dac)
923 dac = try_dac(codec, dacs[0], pin);
924 if (!dac)
925 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100926 if (dac) {
927 if (!i)
928 badness += bad->shared_primary;
929 else if (i == 1)
930 badness += bad->shared_surr;
931 else
932 badness += bad->shared_clfe;
933 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
934 dac = spec->private_dac_nids[0];
935 badness += bad->shared_surr_main;
936 } else if (!i)
937 badness += bad->no_primary_dac;
938 else
939 badness += bad->no_dac;
940 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100941 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
Takashi Iwai117688a2013-01-04 15:41:41 +0100942 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +0100943 /* try with aamix */
944 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
945 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100946 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100947 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +0100948 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100949 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100950 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +0100951 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100952 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100953 if (dac)
954 badness += assign_out_path_ctls(codec, pin, dac);
955 }
956
957 return badness;
958}
959
960/* return NID if the given pin has only a single connection to a certain DAC */
961static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
962{
963 struct hda_gen_spec *spec = codec->spec;
964 int i;
965 hda_nid_t nid_found = 0;
966
967 for (i = 0; i < spec->num_all_dacs; i++) {
968 hda_nid_t nid = spec->all_dacs[i];
969 if (!nid || is_dac_already_used(codec, nid))
970 continue;
971 if (is_reachable_path(codec, nid, pin)) {
972 if (nid_found)
973 return 0;
974 nid_found = nid;
975 }
976 }
977 return nid_found;
978}
979
980/* check whether the given pin can be a multi-io pin */
981static bool can_be_multiio_pin(struct hda_codec *codec,
982 unsigned int location, hda_nid_t nid)
983{
984 unsigned int defcfg, caps;
985
986 defcfg = snd_hda_codec_get_pincfg(codec, nid);
987 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
988 return false;
989 if (location && get_defcfg_location(defcfg) != location)
990 return false;
991 caps = snd_hda_query_pin_caps(codec, nid);
992 if (!(caps & AC_PINCAP_OUT))
993 return false;
994 return true;
995}
996
Takashi Iwaie22aab72013-01-04 14:50:04 +0100997/* count the number of input pins that are capable to be multi-io */
998static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
999{
1000 struct hda_gen_spec *spec = codec->spec;
1001 struct auto_pin_cfg *cfg = &spec->autocfg;
1002 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1003 unsigned int location = get_defcfg_location(defcfg);
1004 int type, i;
1005 int num_pins = 0;
1006
1007 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1008 for (i = 0; i < cfg->num_inputs; i++) {
1009 if (cfg->inputs[i].type != type)
1010 continue;
1011 if (can_be_multiio_pin(codec, location,
1012 cfg->inputs[i].pin))
1013 num_pins++;
1014 }
1015 }
1016 return num_pins;
1017}
1018
Takashi Iwai352f7f92012-12-19 12:52:06 +01001019/*
1020 * multi-io helper
1021 *
1022 * When hardwired is set, try to fill ony hardwired pins, and returns
1023 * zero if any pins are filled, non-zero if nothing found.
1024 * When hardwired is off, try to fill possible input pins, and returns
1025 * the badness value.
1026 */
1027static int fill_multi_ios(struct hda_codec *codec,
1028 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001029 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001030{
1031 struct hda_gen_spec *spec = codec->spec;
1032 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001033 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001034 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1035 unsigned int location = get_defcfg_location(defcfg);
1036 int badness = 0;
1037
1038 old_pins = spec->multi_ios;
1039 if (old_pins >= 2)
1040 goto end_fill;
1041
Takashi Iwaie22aab72013-01-04 14:50:04 +01001042 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001043 if (num_pins < 2)
1044 goto end_fill;
1045
Takashi Iwai352f7f92012-12-19 12:52:06 +01001046 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1047 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001048 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001049 hda_nid_t nid = cfg->inputs[i].pin;
1050 hda_nid_t dac = 0;
1051
1052 if (cfg->inputs[i].type != type)
1053 continue;
1054 if (!can_be_multiio_pin(codec, location, nid))
1055 continue;
1056 for (j = 0; j < spec->multi_ios; j++) {
1057 if (nid == spec->multi_io[j].pin)
1058 break;
1059 }
1060 if (j < spec->multi_ios)
1061 continue;
1062
Takashi Iwai352f7f92012-12-19 12:52:06 +01001063 if (hardwired)
1064 dac = get_dac_if_single(codec, nid);
1065 else if (!dac)
1066 dac = look_for_dac(codec, nid, false);
1067 if (!dac) {
1068 badness++;
1069 continue;
1070 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001071 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001072 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001073 badness++;
1074 continue;
1075 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001076 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001077 spec->multi_io[spec->multi_ios].pin = nid;
1078 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001079 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1080 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001081 spec->multi_ios++;
1082 if (spec->multi_ios >= 2)
1083 break;
1084 }
1085 }
1086 end_fill:
1087 if (badness)
1088 badness = BAD_MULTI_IO;
1089 if (old_pins == spec->multi_ios) {
1090 if (hardwired)
1091 return 1; /* nothing found */
1092 else
1093 return badness; /* no badness if nothing found */
1094 }
1095 if (!hardwired && spec->multi_ios < 2) {
1096 /* cancel newly assigned paths */
1097 spec->paths.used -= spec->multi_ios - old_pins;
1098 spec->multi_ios = old_pins;
1099 return badness;
1100 }
1101
1102 /* assign volume and mute controls */
1103 for (i = old_pins; i < spec->multi_ios; i++)
1104 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1105 spec->multi_io[i].dac);
1106
1107 return badness;
1108}
1109
1110/* map DACs for all pins in the list if they are single connections */
1111static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001112 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001113{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001114 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001115 int i;
1116 bool found = false;
1117 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001118 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001119 hda_nid_t dac;
1120 if (dacs[i])
1121 continue;
1122 dac = get_dac_if_single(codec, pins[i]);
1123 if (!dac)
1124 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001125 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
Takashi Iwai117688a2013-01-04 15:41:41 +01001126 if (!path && !i && spec->mixer_nid)
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001127 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001128 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001129 dacs[i] = dac;
1130 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001131 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001132 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001133 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001134 }
1135 }
1136 return found;
1137}
1138
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001139/* create a new path including aamix if available, and return its index */
1140static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1141{
1142 struct nid_path *path;
1143
1144 path = snd_hda_get_path_from_idx(codec, path_idx);
1145 if (!path || !path->depth || path->with_aa_mix)
1146 return 0;
1147 path = snd_hda_add_new_path(codec, path->path[0],
1148 path->path[path->depth - 1],
1149 HDA_PARSE_ONLY_AAMIX);
1150 if (!path)
1151 return 0;
1152 print_nid_path("output-aamix", path);
1153 path->active = false; /* unused as default */
1154 return snd_hda_get_path_idx(codec, path);
1155}
1156
Takashi Iwai352f7f92012-12-19 12:52:06 +01001157/* fill in the dac_nids table from the parsed pin configuration */
1158static int fill_and_eval_dacs(struct hda_codec *codec,
1159 bool fill_hardwired,
1160 bool fill_mio_first)
1161{
1162 struct hda_gen_spec *spec = codec->spec;
1163 struct auto_pin_cfg *cfg = &spec->autocfg;
1164 int i, err, badness;
1165
1166 /* set num_dacs once to full for look_for_dac() */
1167 spec->multiout.num_dacs = cfg->line_outs;
1168 spec->multiout.dac_nids = spec->private_dac_nids;
1169 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1170 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1171 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1172 spec->multi_ios = 0;
1173 snd_array_free(&spec->paths);
1174 badness = 0;
1175
1176 /* fill hard-wired DACs first */
1177 if (fill_hardwired) {
1178 bool mapped;
1179 do {
1180 mapped = map_singles(codec, cfg->line_outs,
1181 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001182 spec->private_dac_nids,
1183 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001184 mapped |= map_singles(codec, cfg->hp_outs,
1185 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001186 spec->multiout.hp_out_nid,
1187 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001188 mapped |= map_singles(codec, cfg->speaker_outs,
1189 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001190 spec->multiout.extra_out_nid,
1191 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001192 if (fill_mio_first && cfg->line_outs == 1 &&
1193 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001194 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001195 if (!err)
1196 mapped = true;
1197 }
1198 } while (mapped);
1199 }
1200
1201 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001202 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001203 &main_out_badness);
1204
1205 /* re-count num_dacs and squash invalid entries */
1206 spec->multiout.num_dacs = 0;
1207 for (i = 0; i < cfg->line_outs; i++) {
1208 if (spec->private_dac_nids[i])
1209 spec->multiout.num_dacs++;
1210 else {
1211 memmove(spec->private_dac_nids + i,
1212 spec->private_dac_nids + i + 1,
1213 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1214 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1215 }
1216 }
1217
1218 if (fill_mio_first &&
1219 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1220 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001221 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001222 if (err < 0)
1223 return err;
1224 /* we don't count badness at this stage yet */
1225 }
1226
1227 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1228 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1229 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001230 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001231 &extra_out_badness);
1232 if (err < 0)
1233 return err;
1234 badness += err;
1235 }
1236 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1237 err = try_assign_dacs(codec, cfg->speaker_outs,
1238 cfg->speaker_pins,
1239 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001240 spec->speaker_paths,
1241 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001242 if (err < 0)
1243 return err;
1244 badness += err;
1245 }
1246 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001247 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001248 if (err < 0)
1249 return err;
1250 badness += err;
1251 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001252
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001253 if (spec->mixer_nid) {
1254 spec->aamix_out_paths[0] =
1255 check_aamix_out_path(codec, spec->out_paths[0]);
1256 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1257 spec->aamix_out_paths[1] =
1258 check_aamix_out_path(codec, spec->hp_paths[0]);
1259 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1260 spec->aamix_out_paths[2] =
1261 check_aamix_out_path(codec, spec->speaker_paths[0]);
1262 }
1263
Takashi Iwaie22aab72013-01-04 14:50:04 +01001264 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1265 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1266 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001267
1268 if (spec->multi_ios == 2) {
1269 for (i = 0; i < 2; i++)
1270 spec->private_dac_nids[spec->multiout.num_dacs++] =
1271 spec->multi_io[i].dac;
1272 spec->ext_channel_count = 2;
1273 } else if (spec->multi_ios) {
1274 spec->multi_ios = 0;
1275 badness += BAD_MULTI_IO;
1276 }
1277
1278 return badness;
1279}
1280
1281#define DEBUG_BADNESS
1282
1283#ifdef DEBUG_BADNESS
1284#define debug_badness snd_printdd
1285#else
1286#define debug_badness(...)
1287#endif
1288
1289static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1290{
1291 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1292 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001293 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001294 spec->multiout.dac_nids[0],
1295 spec->multiout.dac_nids[1],
1296 spec->multiout.dac_nids[2],
1297 spec->multiout.dac_nids[3]);
1298 if (spec->multi_ios > 0)
1299 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1300 spec->multi_ios,
1301 spec->multi_io[0].pin, spec->multi_io[1].pin,
1302 spec->multi_io[0].dac, spec->multi_io[1].dac);
1303 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1304 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001305 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001306 spec->multiout.hp_out_nid[0],
1307 spec->multiout.hp_out_nid[1],
1308 spec->multiout.hp_out_nid[2],
1309 spec->multiout.hp_out_nid[3]);
1310 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1311 cfg->speaker_pins[0], cfg->speaker_pins[1],
1312 cfg->speaker_pins[2], cfg->speaker_pins[3],
1313 spec->multiout.extra_out_nid[0],
1314 spec->multiout.extra_out_nid[1],
1315 spec->multiout.extra_out_nid[2],
1316 spec->multiout.extra_out_nid[3]);
1317}
1318
1319/* find all available DACs of the codec */
1320static void fill_all_dac_nids(struct hda_codec *codec)
1321{
1322 struct hda_gen_spec *spec = codec->spec;
1323 int i;
1324 hda_nid_t nid = codec->start_nid;
1325
1326 spec->num_all_dacs = 0;
1327 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1328 for (i = 0; i < codec->num_nodes; i++, nid++) {
1329 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1330 continue;
1331 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1332 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1333 break;
1334 }
1335 spec->all_dacs[spec->num_all_dacs++] = nid;
1336 }
1337}
1338
1339static int parse_output_paths(struct hda_codec *codec)
1340{
1341 struct hda_gen_spec *spec = codec->spec;
1342 struct auto_pin_cfg *cfg = &spec->autocfg;
1343 struct auto_pin_cfg *best_cfg;
1344 int best_badness = INT_MAX;
1345 int badness;
1346 bool fill_hardwired = true, fill_mio_first = true;
1347 bool best_wired = true, best_mio = true;
1348 bool hp_spk_swapped = false;
1349
1350 fill_all_dac_nids(codec);
1351
1352 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1353 if (!best_cfg)
1354 return -ENOMEM;
1355 *best_cfg = *cfg;
1356
1357 for (;;) {
1358 badness = fill_and_eval_dacs(codec, fill_hardwired,
1359 fill_mio_first);
1360 if (badness < 0) {
1361 kfree(best_cfg);
1362 return badness;
1363 }
1364 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1365 cfg->line_out_type, fill_hardwired, fill_mio_first,
1366 badness);
1367 debug_show_configs(spec, cfg);
1368 if (badness < best_badness) {
1369 best_badness = badness;
1370 *best_cfg = *cfg;
1371 best_wired = fill_hardwired;
1372 best_mio = fill_mio_first;
1373 }
1374 if (!badness)
1375 break;
1376 fill_mio_first = !fill_mio_first;
1377 if (!fill_mio_first)
1378 continue;
1379 fill_hardwired = !fill_hardwired;
1380 if (!fill_hardwired)
1381 continue;
1382 if (hp_spk_swapped)
1383 break;
1384 hp_spk_swapped = true;
1385 if (cfg->speaker_outs > 0 &&
1386 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1387 cfg->hp_outs = cfg->line_outs;
1388 memcpy(cfg->hp_pins, cfg->line_out_pins,
1389 sizeof(cfg->hp_pins));
1390 cfg->line_outs = cfg->speaker_outs;
1391 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1392 sizeof(cfg->speaker_pins));
1393 cfg->speaker_outs = 0;
1394 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1395 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1396 fill_hardwired = true;
1397 continue;
1398 }
1399 if (cfg->hp_outs > 0 &&
1400 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1401 cfg->speaker_outs = cfg->line_outs;
1402 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1403 sizeof(cfg->speaker_pins));
1404 cfg->line_outs = cfg->hp_outs;
1405 memcpy(cfg->line_out_pins, cfg->hp_pins,
1406 sizeof(cfg->hp_pins));
1407 cfg->hp_outs = 0;
1408 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1409 cfg->line_out_type = AUTO_PIN_HP_OUT;
1410 fill_hardwired = true;
1411 continue;
1412 }
1413 break;
1414 }
1415
1416 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001417 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001418 *cfg = *best_cfg;
1419 fill_and_eval_dacs(codec, best_wired, best_mio);
1420 }
1421 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1422 cfg->line_out_type, best_wired, best_mio);
1423 debug_show_configs(spec, cfg);
1424
1425 if (cfg->line_out_pins[0]) {
1426 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001427 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001428 if (path)
1429 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1430 }
1431
1432 kfree(best_cfg);
1433 return 0;
1434}
1435
1436/* add playback controls from the parsed DAC table */
1437static int create_multi_out_ctls(struct hda_codec *codec,
1438 const struct auto_pin_cfg *cfg)
1439{
1440 struct hda_gen_spec *spec = codec->spec;
1441 int i, err, noutputs;
1442
1443 noutputs = cfg->line_outs;
1444 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1445 noutputs += spec->multi_ios;
1446
1447 for (i = 0; i < noutputs; i++) {
1448 const char *name;
1449 int index;
Takashi Iwai196c17662013-01-04 15:01:40 +01001450 hda_nid_t dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001451 struct nid_path *path;
1452
1453 dac = spec->multiout.dac_nids[i];
1454 if (!dac)
1455 continue;
1456 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001457 index = 0;
1458 name = channel_name[i];
1459 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001460 name = get_line_out_pfx(spec, i, true, &index);
1461 }
1462
Takashi Iwai196c17662013-01-04 15:01:40 +01001463 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001464 if (!path)
1465 continue;
1466 if (!name || !strcmp(name, "CLFE")) {
1467 /* Center/LFE */
1468 err = add_vol_ctl(codec, "Center", 0, 1, path);
1469 if (err < 0)
1470 return err;
1471 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1472 if (err < 0)
1473 return err;
1474 err = add_sw_ctl(codec, "Center", 0, 1, path);
1475 if (err < 0)
1476 return err;
1477 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1478 if (err < 0)
1479 return err;
1480 } else {
1481 err = add_stereo_vol(codec, name, index, path);
1482 if (err < 0)
1483 return err;
1484 err = add_stereo_sw(codec, name, index, path);
1485 if (err < 0)
1486 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488 }
1489 return 0;
1490}
1491
Takashi Iwai352f7f92012-12-19 12:52:06 +01001492static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai196c17662013-01-04 15:01:40 +01001493 hda_nid_t dac, int path_idx,
1494 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001496 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 int err;
1498
Takashi Iwai196c17662013-01-04 15:01:40 +01001499 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001500 if (!path)
1501 return 0;
1502 /* bind volume control will be created in the case of dac = 0 */
1503 if (dac) {
1504 err = add_stereo_vol(codec, pfx, cidx, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (err < 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001506 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001508 err = add_stereo_sw(codec, pfx, cidx, path);
1509 if (err < 0)
1510 return err;
1511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
Takashi Iwai352f7f92012-12-19 12:52:06 +01001514/* add playback controls for speaker and HP outputs */
1515static int create_extra_outs(struct hda_codec *codec, int num_pins,
1516 const hda_nid_t *pins, const hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001517 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001519 struct hda_gen_spec *spec = codec->spec;
1520 struct hda_bind_ctls *ctl;
Takashi Iwai5abd4882013-01-07 09:43:18 +01001521 char name[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01001522 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Takashi Iwai352f7f92012-12-19 12:52:06 +01001524 if (!num_pins || !pins[0])
1525 return 0;
1526
1527 if (num_pins == 1) {
1528 hda_nid_t dac = *dacs;
1529 if (!dac)
1530 dac = spec->multiout.dac_nids[0];
Takashi Iwai196c17662013-01-04 15:01:40 +01001531 return create_extra_out(codec, *pins, dac, paths[0], pfx, 0);
Takashi Iwai97ec5582006-03-21 11:29:07 +01001532 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001533
1534 for (i = 0; i < num_pins; i++) {
1535 hda_nid_t dac;
1536 if (dacs[num_pins - 1])
1537 dac = dacs[i]; /* with individual volumes */
Takashi Iwai97ec5582006-03-21 11:29:07 +01001538 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001539 dac = 0;
1540 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
Takashi Iwai196c17662013-01-04 15:01:40 +01001541 err = create_extra_out(codec, pins[i], dac, paths[i],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001542 "Bass Speaker", 0);
1543 } else if (num_pins >= 3) {
1544 snprintf(name, sizeof(name), "%s %s",
1545 pfx, channel_name[i]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001546 err = create_extra_out(codec, pins[i], dac, paths[i],
1547 name, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001548 } else {
Takashi Iwai196c17662013-01-04 15:01:40 +01001549 err = create_extra_out(codec, pins[i], dac, paths[i],
1550 pfx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001552 if (err < 0)
1553 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001555 if (dacs[num_pins - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return 0;
1557
Takashi Iwai352f7f92012-12-19 12:52:06 +01001558 /* Let's create a bind-controls for volumes */
1559 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1560 if (!ctl)
1561 return -ENOMEM;
1562 n = 0;
1563 for (i = 0; i < num_pins; i++) {
1564 hda_nid_t vol;
1565 struct nid_path *path;
1566 if (!pins[i] || !dacs[i])
1567 continue;
Takashi Iwai196c17662013-01-04 15:01:40 +01001568 path = snd_hda_get_path_from_idx(codec, paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001569 if (!path)
1570 continue;
1571 vol = look_for_out_vol_nid(codec, path);
1572 if (vol)
1573 ctl->values[n++] =
1574 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1575 }
1576 if (n) {
1577 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1578 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1579 if (err < 0)
1580 return err;
1581 }
1582 return 0;
1583}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001584
Takashi Iwai352f7f92012-12-19 12:52:06 +01001585static int create_hp_out_ctls(struct hda_codec *codec)
1586{
1587 struct hda_gen_spec *spec = codec->spec;
1588 return create_extra_outs(codec, spec->autocfg.hp_outs,
1589 spec->autocfg.hp_pins,
1590 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001591 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001592 "Headphone");
1593}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Takashi Iwai352f7f92012-12-19 12:52:06 +01001595static int create_speaker_out_ctls(struct hda_codec *codec)
1596{
1597 struct hda_gen_spec *spec = codec->spec;
1598 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1599 spec->autocfg.speaker_pins,
1600 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001601 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001602 "Speaker");
1603}
1604
1605/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001606 * independent HP controls
1607 */
1608
1609static int indep_hp_info(struct snd_kcontrol *kcontrol,
1610 struct snd_ctl_elem_info *uinfo)
1611{
1612 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1613}
1614
1615static int indep_hp_get(struct snd_kcontrol *kcontrol,
1616 struct snd_ctl_elem_value *ucontrol)
1617{
1618 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1619 struct hda_gen_spec *spec = codec->spec;
1620 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1621 return 0;
1622}
1623
1624static int indep_hp_put(struct snd_kcontrol *kcontrol,
1625 struct snd_ctl_elem_value *ucontrol)
1626{
1627 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1628 struct hda_gen_spec *spec = codec->spec;
1629 unsigned int select = ucontrol->value.enumerated.item[0];
1630 int ret = 0;
1631
1632 mutex_lock(&spec->pcm_mutex);
1633 if (spec->active_streams) {
1634 ret = -EBUSY;
1635 goto unlock;
1636 }
1637
1638 if (spec->indep_hp_enabled != select) {
1639 spec->indep_hp_enabled = select;
1640 if (spec->indep_hp_enabled)
1641 spec->multiout.hp_out_nid[0] = 0;
1642 else
1643 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1644 ret = 1;
1645 }
1646 unlock:
1647 mutex_unlock(&spec->pcm_mutex);
1648 return ret;
1649}
1650
1651static const struct snd_kcontrol_new indep_hp_ctl = {
1652 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1653 .name = "Independent HP",
1654 .info = indep_hp_info,
1655 .get = indep_hp_get,
1656 .put = indep_hp_put,
1657};
1658
1659
1660static int create_indep_hp_ctls(struct hda_codec *codec)
1661{
1662 struct hda_gen_spec *spec = codec->spec;
1663
1664 if (!spec->indep_hp)
1665 return 0;
1666 if (!spec->multiout.hp_out_nid[0]) {
1667 spec->indep_hp = 0;
1668 return 0;
1669 }
1670
1671 spec->indep_hp_enabled = false;
1672 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1673 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1674 return -ENOMEM;
1675 return 0;
1676}
1677
1678/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001679 * channel mode enum control
1680 */
1681
1682static int ch_mode_info(struct snd_kcontrol *kcontrol,
1683 struct snd_ctl_elem_info *uinfo)
1684{
1685 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1686 struct hda_gen_spec *spec = codec->spec;
1687
1688 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1689 uinfo->count = 1;
1690 uinfo->value.enumerated.items = spec->multi_ios + 1;
1691 if (uinfo->value.enumerated.item > spec->multi_ios)
1692 uinfo->value.enumerated.item = spec->multi_ios;
1693 sprintf(uinfo->value.enumerated.name, "%dch",
1694 (uinfo->value.enumerated.item + 1) * 2);
1695 return 0;
1696}
1697
1698static int ch_mode_get(struct snd_kcontrol *kcontrol,
1699 struct snd_ctl_elem_value *ucontrol)
1700{
1701 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1702 struct hda_gen_spec *spec = codec->spec;
1703 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1704 return 0;
1705}
1706
Takashi Iwai196c17662013-01-04 15:01:40 +01001707static inline struct nid_path *
1708get_multiio_path(struct hda_codec *codec, int idx)
1709{
1710 struct hda_gen_spec *spec = codec->spec;
1711 return snd_hda_get_path_from_idx(codec,
1712 spec->out_paths[spec->autocfg.line_outs + idx]);
1713}
1714
Takashi Iwai352f7f92012-12-19 12:52:06 +01001715static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1716{
1717 struct hda_gen_spec *spec = codec->spec;
1718 hda_nid_t nid = spec->multi_io[idx].pin;
1719 struct nid_path *path;
1720
Takashi Iwai196c17662013-01-04 15:01:40 +01001721 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001722 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001724
1725 if (path->active == output)
1726 return 0;
1727
1728 if (output) {
1729 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1730 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001731 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001732 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001733 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001734 snd_hda_activate_path(codec, path, false, true);
1735 snd_hda_set_pin_ctl_cache(codec, nid,
1736 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
Takashi Iwai352f7f92012-12-19 12:52:06 +01001741static int ch_mode_put(struct snd_kcontrol *kcontrol,
1742 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001744 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1745 struct hda_gen_spec *spec = codec->spec;
1746 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Takashi Iwai352f7f92012-12-19 12:52:06 +01001748 ch = ucontrol->value.enumerated.item[0];
1749 if (ch < 0 || ch > spec->multi_ios)
1750 return -EINVAL;
1751 if (ch == (spec->ext_channel_count - 1) / 2)
1752 return 0;
1753 spec->ext_channel_count = (ch + 1) * 2;
1754 for (i = 0; i < spec->multi_ios; i++)
1755 set_multi_io(codec, i, i < ch);
1756 spec->multiout.max_channels = max(spec->ext_channel_count,
1757 spec->const_channel_count);
1758 if (spec->need_dac_fix)
1759 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return 1;
1761}
1762
Takashi Iwai352f7f92012-12-19 12:52:06 +01001763static const struct snd_kcontrol_new channel_mode_enum = {
1764 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1765 .name = "Channel Mode",
1766 .info = ch_mode_info,
1767 .get = ch_mode_get,
1768 .put = ch_mode_put,
1769};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Takashi Iwai352f7f92012-12-19 12:52:06 +01001771static int create_multi_channel_mode(struct hda_codec *codec)
1772{
1773 struct hda_gen_spec *spec = codec->spec;
1774
1775 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001776 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001777 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return 0;
1780}
1781
Takashi Iwai352f7f92012-12-19 12:52:06 +01001782/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001783 * aamix loopback enable/disable switch
1784 */
1785
1786#define loopback_mixing_info indep_hp_info
1787
1788static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1789 struct snd_ctl_elem_value *ucontrol)
1790{
1791 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1792 struct hda_gen_spec *spec = codec->spec;
1793 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1794 return 0;
1795}
1796
1797static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1798 int nomix_path_idx, int mix_path_idx)
1799{
1800 struct nid_path *nomix_path, *mix_path;
1801
1802 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1803 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1804 if (!nomix_path || !mix_path)
1805 return;
1806 if (do_mix) {
1807 snd_hda_activate_path(codec, nomix_path, false, true);
1808 snd_hda_activate_path(codec, mix_path, true, true);
1809 } else {
1810 snd_hda_activate_path(codec, mix_path, false, true);
1811 snd_hda_activate_path(codec, nomix_path, true, true);
1812 }
1813}
1814
1815static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1816 struct snd_ctl_elem_value *ucontrol)
1817{
1818 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1819 struct hda_gen_spec *spec = codec->spec;
1820 unsigned int val = ucontrol->value.enumerated.item[0];
1821
1822 if (val == spec->aamix_mode)
1823 return 0;
1824 spec->aamix_mode = val;
1825 update_aamix_paths(codec, val, spec->out_paths[0],
1826 spec->aamix_out_paths[0]);
1827 update_aamix_paths(codec, val, spec->hp_paths[0],
1828 spec->aamix_out_paths[1]);
1829 update_aamix_paths(codec, val, spec->speaker_paths[0],
1830 spec->aamix_out_paths[2]);
1831 return 1;
1832}
1833
1834static const struct snd_kcontrol_new loopback_mixing_enum = {
1835 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1836 .name = "Loopback Mixing",
1837 .info = loopback_mixing_info,
1838 .get = loopback_mixing_get,
1839 .put = loopback_mixing_put,
1840};
1841
1842static int create_loopback_mixing_ctl(struct hda_codec *codec)
1843{
1844 struct hda_gen_spec *spec = codec->spec;
1845
1846 if (!spec->mixer_nid)
1847 return 0;
1848 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1849 spec->aamix_out_paths[2]))
1850 return 0;
1851 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1852 return -ENOMEM;
1853 return 0;
1854}
1855
1856/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001857 * shared headphone/mic handling
1858 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001859
Takashi Iwai352f7f92012-12-19 12:52:06 +01001860static void call_update_outputs(struct hda_codec *codec);
1861
1862/* for shared I/O, change the pin-control accordingly */
1863static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1864{
1865 struct hda_gen_spec *spec = codec->spec;
1866 unsigned int val;
1867 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1868 /* NOTE: this assumes that there are only two inputs, the
1869 * first is the real internal mic and the second is HP/mic jack.
1870 */
1871
1872 val = snd_hda_get_default_vref(codec, pin);
1873
1874 /* This pin does not have vref caps - let's enable vref on pin 0x18
1875 instead, as suggested by Realtek */
1876 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1877 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1878 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1879 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001880 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1881 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001882 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883
1884 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001885 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001886
1887 spec->automute_speaker = !set_as_mic;
1888 call_update_outputs(codec);
1889}
1890
1891/* create a shared input with the headphone out */
1892static int create_shared_input(struct hda_codec *codec)
1893{
1894 struct hda_gen_spec *spec = codec->spec;
1895 struct auto_pin_cfg *cfg = &spec->autocfg;
1896 unsigned int defcfg;
1897 hda_nid_t nid;
1898
1899 /* only one internal input pin? */
1900 if (cfg->num_inputs != 1)
1901 return 0;
1902 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1903 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1904 return 0;
1905
1906 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1907 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1908 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1909 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1910 else
1911 return 0; /* both not available */
1912
1913 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1914 return 0; /* no input */
1915
1916 cfg->inputs[1].pin = nid;
1917 cfg->inputs[1].type = AUTO_PIN_MIC;
1918 cfg->num_inputs = 2;
1919 spec->shared_mic_hp = 1;
1920 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1921 return 0;
1922}
1923
1924
1925/*
1926 * Parse input paths
1927 */
1928
1929#ifdef CONFIG_PM
1930/* add the powersave loopback-list entry */
1931static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1932{
1933 struct hda_amp_list *list;
1934
1935 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1936 return;
1937 list = spec->loopback_list + spec->num_loopbacks;
1938 list->nid = mix;
1939 list->dir = HDA_INPUT;
1940 list->idx = idx;
1941 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001942 spec->loopback.amplist = spec->loopback_list;
1943}
1944#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001945#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001946#endif
1947
Takashi Iwai352f7f92012-12-19 12:52:06 +01001948/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01001949static int new_analog_input(struct hda_codec *codec, int input_idx,
1950 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001951 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001953 struct hda_gen_spec *spec = codec->spec;
1954 struct nid_path *path;
1955 unsigned int val;
1956 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Takashi Iwai352f7f92012-12-19 12:52:06 +01001958 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1959 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1960 return 0; /* no need for analog loopback */
1961
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001962 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001963 if (!path)
1964 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001965 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01001966 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001967
1968 idx = path->idx[path->depth - 1];
1969 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1970 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1971 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001972 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001974 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
1976
Takashi Iwai352f7f92012-12-19 12:52:06 +01001977 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1978 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1979 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001980 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001982 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984
Takashi Iwai352f7f92012-12-19 12:52:06 +01001985 path->active = true;
1986 add_loopback_list(spec, mix_nid, idx);
1987 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
Takashi Iwai352f7f92012-12-19 12:52:06 +01001990static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001992 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1993 return (pincap & AC_PINCAP_IN) != 0;
1994}
1995
1996/* Parse the codec tree and retrieve ADCs */
1997static int fill_adc_nids(struct hda_codec *codec)
1998{
1999 struct hda_gen_spec *spec = codec->spec;
2000 hda_nid_t nid;
2001 hda_nid_t *adc_nids = spec->adc_nids;
2002 int max_nums = ARRAY_SIZE(spec->adc_nids);
2003 int i, nums = 0;
2004
2005 nid = codec->start_nid;
2006 for (i = 0; i < codec->num_nodes; i++, nid++) {
2007 unsigned int caps = get_wcaps(codec, nid);
2008 int type = get_wcaps_type(caps);
2009
2010 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2011 continue;
2012 adc_nids[nums] = nid;
2013 if (++nums >= max_nums)
2014 break;
2015 }
2016 spec->num_adc_nids = nums;
2017 return nums;
2018}
2019
2020/* filter out invalid adc_nids that don't give all active input pins;
2021 * if needed, check whether dynamic ADC-switching is available
2022 */
2023static int check_dyn_adc_switch(struct hda_codec *codec)
2024{
2025 struct hda_gen_spec *spec = codec->spec;
2026 struct hda_input_mux *imux = &spec->input_mux;
2027 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
2028 int i, n, nums;
2029 hda_nid_t pin, adc;
2030
2031 again:
2032 nums = 0;
2033 for (n = 0; n < spec->num_adc_nids; n++) {
2034 adc = spec->adc_nids[n];
2035 for (i = 0; i < imux->num_items; i++) {
2036 pin = spec->imux_pins[i];
2037 if (!is_reachable_path(codec, pin, adc))
2038 break;
2039 }
2040 if (i >= imux->num_items)
2041 adc_nids[nums++] = adc;
2042 }
2043
2044 if (!nums) {
2045 if (spec->shared_mic_hp) {
2046 spec->shared_mic_hp = 0;
2047 imux->num_items = 1;
2048 goto again;
2049 }
2050
2051 /* check whether ADC-switch is possible */
2052 for (i = 0; i < imux->num_items; i++) {
2053 pin = spec->imux_pins[i];
2054 for (n = 0; n < spec->num_adc_nids; n++) {
2055 adc = spec->adc_nids[n];
2056 if (is_reachable_path(codec, pin, adc)) {
2057 spec->dyn_adc_idx[i] = n;
2058 break;
2059 }
2060 }
2061 }
2062
2063 snd_printdd("hda-codec: enabling ADC switching\n");
2064 spec->dyn_adc_switch = 1;
2065 } else if (nums != spec->num_adc_nids) {
2066 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
2067 spec->num_adc_nids = nums;
2068 }
2069
2070 if (imux->num_items == 1 || spec->shared_mic_hp) {
2071 snd_printdd("hda-codec: reducing to a single ADC\n");
2072 spec->num_adc_nids = 1; /* reduce to a single ADC */
2073 }
2074
2075 /* single index for individual volumes ctls */
2076 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2077 spec->num_adc_nids = 1;
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return 0;
2080}
2081
2082/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002083 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002085static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002086{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002087 struct hda_gen_spec *spec = codec->spec;
2088 const struct auto_pin_cfg *cfg = &spec->autocfg;
2089 hda_nid_t mixer = spec->mixer_nid;
2090 struct hda_input_mux *imux = &spec->input_mux;
2091 int num_adcs;
2092 int i, c, err, type_idx = 0;
2093 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002094
Takashi Iwai352f7f92012-12-19 12:52:06 +01002095 num_adcs = fill_adc_nids(codec);
2096 if (num_adcs < 0)
2097 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002098
Takashi Iwai352f7f92012-12-19 12:52:06 +01002099 for (i = 0; i < cfg->num_inputs; i++) {
2100 hda_nid_t pin;
2101 const char *label;
2102 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Takashi Iwai352f7f92012-12-19 12:52:06 +01002104 pin = cfg->inputs[i].pin;
2105 if (!is_input_pin(codec, pin))
2106 continue;
2107
2108 label = hda_get_autocfg_input_label(codec, cfg, i);
2109 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2110 label = "Headphone Mic";
2111 if (prev_label && !strcmp(label, prev_label))
2112 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002113 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002114 type_idx = 0;
2115 prev_label = label;
2116
2117 if (mixer) {
2118 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002119 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002120 label, type_idx, mixer);
2121 if (err < 0)
2122 return err;
2123 }
2124 }
2125
2126 imux_added = false;
2127 for (c = 0; c < num_adcs; c++) {
2128 struct nid_path *path;
2129 hda_nid_t adc = spec->adc_nids[c];
2130
2131 if (!is_reachable_path(codec, pin, adc))
2132 continue;
2133 path = snd_array_new(&spec->paths);
2134 if (!path)
2135 return -ENOMEM;
2136 memset(path, 0, sizeof(*path));
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002137 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002138 snd_printd(KERN_ERR
2139 "invalid input path 0x%x -> 0x%x\n",
2140 pin, adc);
2141 spec->paths.used--;
2142 continue;
2143 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002144 print_nid_path("input", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002145
2146 if (!imux_added) {
2147 spec->imux_pins[imux->num_items] = pin;
2148 snd_hda_add_imux_item(imux, label,
2149 imux->num_items, NULL);
2150 imux_added = true;
2151 }
2152 }
2153 }
2154
2155 return 0;
2156}
2157
2158
2159/*
2160 * input source mux
2161 */
2162
2163/* get the ADC NID corresponding to the given index */
2164static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
2165{
2166 struct hda_gen_spec *spec = codec->spec;
2167 if (spec->dyn_adc_switch)
2168 adc_idx = spec->dyn_adc_idx[imux_idx];
2169 return spec->adc_nids[adc_idx];
2170}
2171
2172static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2173 unsigned int idx);
2174
2175static int mux_enum_info(struct snd_kcontrol *kcontrol,
2176 struct snd_ctl_elem_info *uinfo)
2177{
2178 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2179 struct hda_gen_spec *spec = codec->spec;
2180 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2181}
2182
2183static int mux_enum_get(struct snd_kcontrol *kcontrol,
2184 struct snd_ctl_elem_value *ucontrol)
2185{
2186 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2187 struct hda_gen_spec *spec = codec->spec;
2188 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2189
2190 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2191 return 0;
2192}
2193
2194static int mux_enum_put(struct snd_kcontrol *kcontrol,
2195 struct snd_ctl_elem_value *ucontrol)
2196{
2197 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2198 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2199 return mux_select(codec, adc_idx,
2200 ucontrol->value.enumerated.item[0]);
2201}
2202
Takashi Iwai352f7f92012-12-19 12:52:06 +01002203static const struct snd_kcontrol_new cap_src_temp = {
2204 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2205 .name = "Input Source",
2206 .info = mux_enum_info,
2207 .get = mux_enum_get,
2208 .put = mux_enum_put,
2209};
2210
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002211/*
2212 * capture volume and capture switch ctls
2213 */
2214
Takashi Iwai352f7f92012-12-19 12:52:06 +01002215typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2216 struct snd_ctl_elem_value *ucontrol);
2217
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002218/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002219static int cap_put_caller(struct snd_kcontrol *kcontrol,
2220 struct snd_ctl_elem_value *ucontrol,
2221 put_call_t func, int type)
2222{
2223 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2224 struct hda_gen_spec *spec = codec->spec;
2225 const struct hda_input_mux *imux;
2226 struct nid_path *path;
2227 int i, adc_idx, err = 0;
2228
2229 imux = &spec->input_mux;
2230 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2231 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002232 /* we use the cache-only update at first since multiple input paths
2233 * may shared the same amp; by updating only caches, the redundant
2234 * writes to hardware can be reduced.
2235 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002236 codec->cached_write = 1;
2237 for (i = 0; i < imux->num_items; i++) {
2238 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2239 get_adc_nid(codec, adc_idx, i));
2240 if (!path->ctls[type])
2241 continue;
2242 kcontrol->private_value = path->ctls[type];
2243 err = func(kcontrol, ucontrol);
2244 if (err < 0)
2245 goto error;
2246 }
2247 error:
2248 codec->cached_write = 0;
2249 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002250 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002251 if (err >= 0 && spec->cap_sync_hook)
2252 spec->cap_sync_hook(codec);
2253 return err;
2254}
2255
2256/* capture volume ctl callbacks */
2257#define cap_vol_info snd_hda_mixer_amp_volume_info
2258#define cap_vol_get snd_hda_mixer_amp_volume_get
2259#define cap_vol_tlv snd_hda_mixer_amp_tlv
2260
2261static int cap_vol_put(struct snd_kcontrol *kcontrol,
2262 struct snd_ctl_elem_value *ucontrol)
2263{
2264 return cap_put_caller(kcontrol, ucontrol,
2265 snd_hda_mixer_amp_volume_put,
2266 NID_PATH_VOL_CTL);
2267}
2268
2269static const struct snd_kcontrol_new cap_vol_temp = {
2270 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2271 .name = "Capture Volume",
2272 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2273 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2274 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2275 .info = cap_vol_info,
2276 .get = cap_vol_get,
2277 .put = cap_vol_put,
2278 .tlv = { .c = cap_vol_tlv },
2279};
2280
2281/* capture switch ctl callbacks */
2282#define cap_sw_info snd_ctl_boolean_stereo_info
2283#define cap_sw_get snd_hda_mixer_amp_switch_get
2284
2285static int cap_sw_put(struct snd_kcontrol *kcontrol,
2286 struct snd_ctl_elem_value *ucontrol)
2287{
2288 return cap_put_caller(kcontrol, ucontrol,
2289 snd_hda_mixer_amp_switch_put,
2290 NID_PATH_MUTE_CTL);
2291}
2292
2293static const struct snd_kcontrol_new cap_sw_temp = {
2294 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2295 .name = "Capture Switch",
2296 .info = cap_sw_info,
2297 .get = cap_sw_get,
2298 .put = cap_sw_put,
2299};
2300
2301static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2302{
2303 hda_nid_t nid;
2304 int i, depth;
2305
2306 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2307 for (depth = 0; depth < 3; depth++) {
2308 if (depth >= path->depth)
2309 return -EINVAL;
2310 i = path->depth - depth - 1;
2311 nid = path->path[i];
2312 if (!path->ctls[NID_PATH_VOL_CTL]) {
2313 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2314 path->ctls[NID_PATH_VOL_CTL] =
2315 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2316 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2317 int idx = path->idx[i];
2318 if (!depth && codec->single_adc_amp)
2319 idx = 0;
2320 path->ctls[NID_PATH_VOL_CTL] =
2321 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2322 }
2323 }
2324 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2325 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2326 path->ctls[NID_PATH_MUTE_CTL] =
2327 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2328 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2329 int idx = path->idx[i];
2330 if (!depth && codec->single_adc_amp)
2331 idx = 0;
2332 path->ctls[NID_PATH_MUTE_CTL] =
2333 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2334 }
2335 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return 0;
2338}
2339
Takashi Iwai352f7f92012-12-19 12:52:06 +01002340static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002342 struct hda_gen_spec *spec = codec->spec;
2343 struct auto_pin_cfg *cfg = &spec->autocfg;
2344 unsigned int val;
2345 int i;
2346
2347 if (!spec->inv_dmic_split)
2348 return false;
2349 for (i = 0; i < cfg->num_inputs; i++) {
2350 if (cfg->inputs[i].pin != nid)
2351 continue;
2352 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2353 return false;
2354 val = snd_hda_codec_get_pincfg(codec, nid);
2355 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2356 }
2357 return false;
2358}
2359
2360static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2361 int idx, bool is_switch, unsigned int ctl,
2362 bool inv_dmic)
2363{
2364 struct hda_gen_spec *spec = codec->spec;
2365 char tmpname[44];
2366 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2367 const char *sfx = is_switch ? "Switch" : "Volume";
2368 unsigned int chs = inv_dmic ? 1 : 3;
2369 int err;
2370
2371 if (!ctl)
2372 return 0;
2373
2374 if (label)
2375 snprintf(tmpname, sizeof(tmpname),
2376 "%s Capture %s", label, sfx);
2377 else
2378 snprintf(tmpname, sizeof(tmpname),
2379 "Capture %s", sfx);
2380 err = add_control(spec, type, tmpname, idx,
2381 amp_val_replace_channels(ctl, chs));
2382 if (err < 0 || !inv_dmic)
2383 return err;
2384
2385 /* Make independent right kcontrol */
2386 if (label)
2387 snprintf(tmpname, sizeof(tmpname),
2388 "Inverted %s Capture %s", label, sfx);
2389 else
2390 snprintf(tmpname, sizeof(tmpname),
2391 "Inverted Capture %s", sfx);
2392 return add_control(spec, type, tmpname, idx,
2393 amp_val_replace_channels(ctl, 2));
2394}
2395
2396/* create single (and simple) capture volume and switch controls */
2397static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2398 unsigned int vol_ctl, unsigned int sw_ctl,
2399 bool inv_dmic)
2400{
2401 int err;
2402 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2403 if (err < 0)
2404 return err;
2405 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2406 if (err < 0)
2407 return err;
2408 return 0;
2409}
2410
2411/* create bound capture volume and switch controls */
2412static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2413 unsigned int vol_ctl, unsigned int sw_ctl)
2414{
2415 struct hda_gen_spec *spec = codec->spec;
2416 struct snd_kcontrol_new *knew;
2417
2418 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002419 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002420 if (!knew)
2421 return -ENOMEM;
2422 knew->index = idx;
2423 knew->private_value = vol_ctl;
2424 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2425 }
2426 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002427 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002428 if (!knew)
2429 return -ENOMEM;
2430 knew->index = idx;
2431 knew->private_value = sw_ctl;
2432 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2433 }
2434 return 0;
2435}
2436
2437/* return the vol ctl when used first in the imux list */
2438static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2439{
2440 struct hda_gen_spec *spec = codec->spec;
2441 struct nid_path *path;
2442 unsigned int ctl;
2443 int i;
2444
2445 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2446 get_adc_nid(codec, 0, idx));
2447 if (!path)
2448 return 0;
2449 ctl = path->ctls[type];
2450 if (!ctl)
2451 return 0;
2452 for (i = 0; i < idx - 1; i++) {
2453 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2454 get_adc_nid(codec, 0, i));
2455 if (path && path->ctls[type] == ctl)
2456 return 0;
2457 }
2458 return ctl;
2459}
2460
2461/* create individual capture volume and switch controls per input */
2462static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2463{
2464 struct hda_gen_spec *spec = codec->spec;
2465 struct hda_input_mux *imux = &spec->input_mux;
2466 int i, err, type, type_idx = 0;
2467 const char *prev_label = NULL;
2468
2469 for (i = 0; i < imux->num_items; i++) {
2470 const char *label;
2471 bool inv_dmic;
2472 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2473 if (prev_label && !strcmp(label, prev_label))
2474 type_idx++;
2475 else
2476 type_idx = 0;
2477 prev_label = label;
2478 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2479
2480 for (type = 0; type < 2; type++) {
2481 err = add_single_cap_ctl(codec, label, type_idx, type,
2482 get_first_cap_ctl(codec, i, type),
2483 inv_dmic);
2484 if (err < 0)
2485 return err;
2486 }
2487 }
2488 return 0;
2489}
2490
2491static int create_capture_mixers(struct hda_codec *codec)
2492{
2493 struct hda_gen_spec *spec = codec->spec;
2494 struct hda_input_mux *imux = &spec->input_mux;
2495 int i, n, nums, err;
2496
2497 if (spec->dyn_adc_switch)
2498 nums = 1;
2499 else
2500 nums = spec->num_adc_nids;
2501
2502 if (!spec->auto_mic && imux->num_items > 1) {
2503 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002504 const char *name;
2505 name = nums > 1 ? "Input Source" : "Capture Source";
2506 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002507 if (!knew)
2508 return -ENOMEM;
2509 knew->count = nums;
2510 }
2511
2512 for (n = 0; n < nums; n++) {
2513 bool multi = false;
2514 bool inv_dmic = false;
2515 int vol, sw;
2516
2517 vol = sw = 0;
2518 for (i = 0; i < imux->num_items; i++) {
2519 struct nid_path *path;
2520 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2521 get_adc_nid(codec, n, i));
2522 if (!path)
2523 continue;
2524 parse_capvol_in_path(codec, path);
2525 if (!vol)
2526 vol = path->ctls[NID_PATH_VOL_CTL];
2527 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2528 multi = true;
2529 if (!sw)
2530 sw = path->ctls[NID_PATH_MUTE_CTL];
2531 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2532 multi = true;
2533 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2534 inv_dmic = true;
2535 }
2536
2537 if (!multi)
2538 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2539 inv_dmic);
2540 else if (!spec->multi_cap_vol)
2541 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2542 else
2543 err = create_multi_cap_vol_ctl(codec);
2544 if (err < 0)
2545 return err;
2546 }
2547
2548 return 0;
2549}
2550
2551/*
2552 * add mic boosts if needed
2553 */
2554static int parse_mic_boost(struct hda_codec *codec)
2555{
2556 struct hda_gen_spec *spec = codec->spec;
2557 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002558 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002559 int type_idx = 0;
2560 hda_nid_t nid;
2561 const char *prev_label = NULL;
2562
2563 for (i = 0; i < cfg->num_inputs; i++) {
2564 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2565 break;
2566 nid = cfg->inputs[i].pin;
2567 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2568 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01002569 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002570 struct nid_path *path;
2571 unsigned int val;
2572
2573 label = hda_get_autocfg_input_label(codec, cfg, i);
2574 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2575 label = "Headphone Mic";
2576 if (prev_label && !strcmp(label, prev_label))
2577 type_idx++;
2578 else
2579 type_idx = 0;
2580 prev_label = label;
2581
2582 snprintf(boost_label, sizeof(boost_label),
2583 "%s Boost Volume", label);
2584 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2585 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2586 boost_label, type_idx, val);
2587 if (err < 0)
2588 return err;
2589
2590 path = snd_hda_get_nid_path(codec, nid, 0);
2591 if (path)
2592 path->ctls[NID_PATH_BOOST_CTL] = val;
2593 }
2594 }
2595 return 0;
2596}
2597
2598/*
2599 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2600 */
2601static void parse_digital(struct hda_codec *codec)
2602{
2603 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002604 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002605 int i, nums;
2606 hda_nid_t dig_nid;
2607
2608 /* support multiple SPDIFs; the secondary is set up as a slave */
2609 nums = 0;
2610 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2611 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2612 dig_nid = look_for_dac(codec, pin, true);
2613 if (!dig_nid)
2614 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002615 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002616 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002617 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002618 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002619 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01002620 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002621 if (!nums) {
2622 spec->multiout.dig_out_nid = dig_nid;
2623 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2624 } else {
2625 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2626 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2627 break;
2628 spec->slave_dig_outs[nums - 1] = dig_nid;
2629 }
2630 nums++;
2631 }
2632
2633 if (spec->autocfg.dig_in_pin) {
2634 dig_nid = codec->start_nid;
2635 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002636 unsigned int wcaps = get_wcaps(codec, dig_nid);
2637 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2638 continue;
2639 if (!(wcaps & AC_WCAP_DIGITAL))
2640 continue;
2641 path = snd_hda_add_new_path(codec,
2642 spec->autocfg.dig_in_pin,
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002643 dig_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002644 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002645 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002646 path->active = true;
2647 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01002648 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002649 break;
2650 }
2651 }
2652 }
2653}
2654
2655
2656/*
2657 * input MUX handling
2658 */
2659
2660static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2661
2662/* select the given imux item; either unmute exclusively or select the route */
2663static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2664 unsigned int idx)
2665{
2666 struct hda_gen_spec *spec = codec->spec;
2667 const struct hda_input_mux *imux;
2668 struct nid_path *path;
2669
2670 imux = &spec->input_mux;
2671 if (!imux->num_items)
2672 return 0;
2673
2674 if (idx >= imux->num_items)
2675 idx = imux->num_items - 1;
2676 if (spec->cur_mux[adc_idx] == idx)
2677 return 0;
2678
2679 path = snd_hda_get_nid_path(codec,
2680 spec->imux_pins[spec->cur_mux[adc_idx]],
2681 spec->adc_nids[adc_idx]);
2682 if (!path)
2683 return 0;
2684 if (path->active)
2685 snd_hda_activate_path(codec, path, false, false);
2686
2687 spec->cur_mux[adc_idx] = idx;
2688
2689 if (spec->shared_mic_hp)
2690 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2691
2692 if (spec->dyn_adc_switch)
2693 dyn_adc_pcm_resetup(codec, idx);
2694
2695 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2696 get_adc_nid(codec, adc_idx, idx));
2697 if (!path)
2698 return 0;
2699 if (path->active)
2700 return 0;
2701 snd_hda_activate_path(codec, path, true, false);
2702 if (spec->cap_sync_hook)
2703 spec->cap_sync_hook(codec);
2704 return 1;
2705}
2706
2707
2708/*
2709 * Jack detections for HP auto-mute and mic-switch
2710 */
2711
2712/* check each pin in the given array; returns true if any of them is plugged */
2713static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2714{
2715 int i, present = 0;
2716
2717 for (i = 0; i < num_pins; i++) {
2718 hda_nid_t nid = pins[i];
2719 if (!nid)
2720 break;
2721 present |= snd_hda_jack_detect(codec, nid);
2722 }
2723 return present;
2724}
2725
2726/* standard HP/line-out auto-mute helper */
2727static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2728 bool mute, bool hp_out)
2729{
2730 struct hda_gen_spec *spec = codec->spec;
2731 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2732 int i;
2733
2734 for (i = 0; i < num_pins; i++) {
2735 hda_nid_t nid = pins[i];
2736 unsigned int val;
2737 if (!nid)
2738 break;
2739 /* don't reset VREF value in case it's controlling
2740 * the amp (see alc861_fixup_asus_amp_vref_0f())
2741 */
2742 if (spec->keep_vref_in_automute) {
2743 val = snd_hda_codec_read(codec, nid, 0,
2744 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2745 val &= ~PIN_HP;
2746 } else
2747 val = 0;
2748 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002749 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002750 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002751 }
2752}
2753
2754/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002755void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002756{
2757 struct hda_gen_spec *spec = codec->spec;
2758 int on;
2759
2760 /* Control HP pins/amps depending on master_mute state;
2761 * in general, HP pins/amps control should be enabled in all cases,
2762 * but currently set only for master_mute, just to be safe
2763 */
2764 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2765 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2766 spec->autocfg.hp_pins, spec->master_mute, true);
2767
2768 if (!spec->automute_speaker)
2769 on = 0;
2770 else
2771 on = spec->hp_jack_present | spec->line_jack_present;
2772 on |= spec->master_mute;
2773 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2774 spec->autocfg.speaker_pins, on, false);
2775
2776 /* toggle line-out mutes if needed, too */
2777 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2778 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2779 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2780 return;
2781 if (!spec->automute_lo)
2782 on = 0;
2783 else
2784 on = spec->hp_jack_present;
2785 on |= spec->master_mute;
2786 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2787 spec->autocfg.line_out_pins, on, false);
2788}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002789EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002790
2791static void call_update_outputs(struct hda_codec *codec)
2792{
2793 struct hda_gen_spec *spec = codec->spec;
2794 if (spec->automute_hook)
2795 spec->automute_hook(codec);
2796 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002797 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002798}
2799
2800/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002801void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002802{
2803 struct hda_gen_spec *spec = codec->spec;
2804
2805 spec->hp_jack_present =
2806 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2807 spec->autocfg.hp_pins);
2808 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2809 return;
2810 call_update_outputs(codec);
2811}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002812EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002813
2814/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002815void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002816{
2817 struct hda_gen_spec *spec = codec->spec;
2818
2819 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2820 return;
2821 /* check LO jack only when it's different from HP */
2822 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2823 return;
2824
2825 spec->line_jack_present =
2826 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2827 spec->autocfg.line_out_pins);
2828 if (!spec->automute_speaker || !spec->detect_lo)
2829 return;
2830 call_update_outputs(codec);
2831}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002832EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002833
2834/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002835void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002836{
2837 struct hda_gen_spec *spec = codec->spec;
2838 int i;
2839
2840 if (!spec->auto_mic)
2841 return;
2842
2843 for (i = spec->am_num_entries - 1; i > 0; i--) {
2844 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2845 mux_select(codec, 0, spec->am_entry[i].idx);
2846 return;
2847 }
2848 }
2849 mux_select(codec, 0, spec->am_entry[0].idx);
2850}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002851EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002852
2853/*
2854 * Auto-Mute mode mixer enum support
2855 */
2856static int automute_mode_info(struct snd_kcontrol *kcontrol,
2857 struct snd_ctl_elem_info *uinfo)
2858{
2859 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2860 struct hda_gen_spec *spec = codec->spec;
2861 static const char * const texts3[] = {
2862 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002863 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Takashi Iwai352f7f92012-12-19 12:52:06 +01002865 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2866 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2867 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2868}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Takashi Iwai352f7f92012-12-19 12:52:06 +01002870static int automute_mode_get(struct snd_kcontrol *kcontrol,
2871 struct snd_ctl_elem_value *ucontrol)
2872{
2873 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2874 struct hda_gen_spec *spec = codec->spec;
2875 unsigned int val = 0;
2876 if (spec->automute_speaker)
2877 val++;
2878 if (spec->automute_lo)
2879 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002880
Takashi Iwai352f7f92012-12-19 12:52:06 +01002881 ucontrol->value.enumerated.item[0] = val;
2882 return 0;
2883}
2884
2885static int automute_mode_put(struct snd_kcontrol *kcontrol,
2886 struct snd_ctl_elem_value *ucontrol)
2887{
2888 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2889 struct hda_gen_spec *spec = codec->spec;
2890
2891 switch (ucontrol->value.enumerated.item[0]) {
2892 case 0:
2893 if (!spec->automute_speaker && !spec->automute_lo)
2894 return 0;
2895 spec->automute_speaker = 0;
2896 spec->automute_lo = 0;
2897 break;
2898 case 1:
2899 if (spec->automute_speaker_possible) {
2900 if (!spec->automute_lo && spec->automute_speaker)
2901 return 0;
2902 spec->automute_speaker = 1;
2903 spec->automute_lo = 0;
2904 } else if (spec->automute_lo_possible) {
2905 if (spec->automute_lo)
2906 return 0;
2907 spec->automute_lo = 1;
2908 } else
2909 return -EINVAL;
2910 break;
2911 case 2:
2912 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2913 return -EINVAL;
2914 if (spec->automute_speaker && spec->automute_lo)
2915 return 0;
2916 spec->automute_speaker = 1;
2917 spec->automute_lo = 1;
2918 break;
2919 default:
2920 return -EINVAL;
2921 }
2922 call_update_outputs(codec);
2923 return 1;
2924}
2925
2926static const struct snd_kcontrol_new automute_mode_enum = {
2927 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2928 .name = "Auto-Mute Mode",
2929 .info = automute_mode_info,
2930 .get = automute_mode_get,
2931 .put = automute_mode_put,
2932};
2933
2934static int add_automute_mode_enum(struct hda_codec *codec)
2935{
2936 struct hda_gen_spec *spec = codec->spec;
2937
Takashi Iwai12c93df2012-12-19 14:38:33 +01002938 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002939 return -ENOMEM;
2940 return 0;
2941}
2942
2943/*
2944 * Check the availability of HP/line-out auto-mute;
2945 * Set up appropriately if really supported
2946 */
2947static int check_auto_mute_availability(struct hda_codec *codec)
2948{
2949 struct hda_gen_spec *spec = codec->spec;
2950 struct auto_pin_cfg *cfg = &spec->autocfg;
2951 int present = 0;
2952 int i, err;
2953
2954 if (cfg->hp_pins[0])
2955 present++;
2956 if (cfg->line_out_pins[0])
2957 present++;
2958 if (cfg->speaker_pins[0])
2959 present++;
2960 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002961 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002962
2963 if (!cfg->speaker_pins[0] &&
2964 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2965 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2966 sizeof(cfg->speaker_pins));
2967 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Takashi Iwai352f7f92012-12-19 12:52:06 +01002970 if (!cfg->hp_pins[0] &&
2971 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2972 memcpy(cfg->hp_pins, cfg->line_out_pins,
2973 sizeof(cfg->hp_pins));
2974 cfg->hp_outs = cfg->line_outs;
2975 }
2976
2977 for (i = 0; i < cfg->hp_outs; i++) {
2978 hda_nid_t nid = cfg->hp_pins[i];
2979 if (!is_jack_detectable(codec, nid))
2980 continue;
2981 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2982 nid);
2983 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002984 spec->hp_automute_hook ?
2985 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002986 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002987 spec->detect_hp = 1;
2988 }
2989
2990 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2991 if (cfg->speaker_outs)
2992 for (i = 0; i < cfg->line_outs; i++) {
2993 hda_nid_t nid = cfg->line_out_pins[i];
2994 if (!is_jack_detectable(codec, nid))
2995 continue;
2996 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2997 snd_hda_jack_detect_enable_callback(codec, nid,
2998 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002999 spec->line_automute_hook ?
3000 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003001 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003002 spec->detect_lo = 1;
3003 }
3004 spec->automute_lo_possible = spec->detect_hp;
3005 }
3006
3007 spec->automute_speaker_possible = cfg->speaker_outs &&
3008 (spec->detect_hp || spec->detect_lo);
3009
3010 spec->automute_lo = spec->automute_lo_possible;
3011 spec->automute_speaker = spec->automute_speaker_possible;
3012
3013 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3014 /* create a control for automute mode */
3015 err = add_automute_mode_enum(codec);
3016 if (err < 0)
3017 return err;
3018 }
3019 return 0;
3020}
3021
3022/* return the position of NID in the list, or -1 if not found */
3023static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
3024{
3025 int i;
3026 for (i = 0; i < nums; i++)
3027 if (list[i] == nid)
3028 return i;
3029 return -1;
3030}
3031
3032/* check whether all auto-mic pins are valid; setup indices if OK */
3033static bool auto_mic_check_imux(struct hda_codec *codec)
3034{
3035 struct hda_gen_spec *spec = codec->spec;
3036 const struct hda_input_mux *imux;
3037 int i;
3038
3039 imux = &spec->input_mux;
3040 for (i = 0; i < spec->am_num_entries; i++) {
3041 spec->am_entry[i].idx =
3042 find_idx_in_nid_list(spec->am_entry[i].pin,
3043 spec->imux_pins, imux->num_items);
3044 if (spec->am_entry[i].idx < 0)
3045 return false; /* no corresponding imux */
3046 }
3047
3048 /* we don't need the jack detection for the first pin */
3049 for (i = 1; i < spec->am_num_entries; i++)
3050 snd_hda_jack_detect_enable_callback(codec,
3051 spec->am_entry[i].pin,
3052 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003053 spec->mic_autoswitch_hook ?
3054 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003055 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003056 return true;
3057}
3058
3059static int compare_attr(const void *ap, const void *bp)
3060{
3061 const struct automic_entry *a = ap;
3062 const struct automic_entry *b = bp;
3063 return (int)(a->attr - b->attr);
3064}
3065
3066/*
3067 * Check the availability of auto-mic switch;
3068 * Set up if really supported
3069 */
3070static int check_auto_mic_availability(struct hda_codec *codec)
3071{
3072 struct hda_gen_spec *spec = codec->spec;
3073 struct auto_pin_cfg *cfg = &spec->autocfg;
3074 unsigned int types;
3075 int i, num_pins;
3076
3077 types = 0;
3078 num_pins = 0;
3079 for (i = 0; i < cfg->num_inputs; i++) {
3080 hda_nid_t nid = cfg->inputs[i].pin;
3081 unsigned int attr;
3082 attr = snd_hda_codec_get_pincfg(codec, nid);
3083 attr = snd_hda_get_input_pin_attr(attr);
3084 if (types & (1 << attr))
3085 return 0; /* already occupied */
3086 switch (attr) {
3087 case INPUT_PIN_ATTR_INT:
3088 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3089 return 0; /* invalid type */
3090 break;
3091 case INPUT_PIN_ATTR_UNUSED:
3092 return 0; /* invalid entry */
3093 default:
3094 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3095 return 0; /* invalid type */
3096 if (!spec->line_in_auto_switch &&
3097 cfg->inputs[i].type != AUTO_PIN_MIC)
3098 return 0; /* only mic is allowed */
3099 if (!is_jack_detectable(codec, nid))
3100 return 0; /* no unsol support */
3101 break;
3102 }
3103 if (num_pins >= MAX_AUTO_MIC_PINS)
3104 return 0;
3105 types |= (1 << attr);
3106 spec->am_entry[num_pins].pin = nid;
3107 spec->am_entry[num_pins].attr = attr;
3108 num_pins++;
3109 }
3110
3111 if (num_pins < 2)
3112 return 0;
3113
3114 spec->am_num_entries = num_pins;
3115 /* sort the am_entry in the order of attr so that the pin with a
3116 * higher attr will be selected when the jack is plugged.
3117 */
3118 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3119 compare_attr, NULL);
3120
3121 if (!auto_mic_check_imux(codec))
3122 return 0;
3123
3124 spec->auto_mic = 1;
3125 spec->num_adc_nids = 1;
3126 spec->cur_mux[0] = spec->am_entry[0].idx;
3127 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3128 spec->am_entry[0].pin,
3129 spec->am_entry[1].pin,
3130 spec->am_entry[2].pin);
3131
3132 return 0;
3133}
3134
3135
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003136/*
3137 * Parse the given BIOS configuration and set up the hda_gen_spec
3138 *
3139 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003140 * or a negative error code
3141 */
3142int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003143 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003144{
3145 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003146 int err;
3147
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003148 if (cfg != &spec->autocfg) {
3149 spec->autocfg = *cfg;
3150 cfg = &spec->autocfg;
3151 }
3152
Takashi Iwai352f7f92012-12-19 12:52:06 +01003153 if (!cfg->line_outs) {
3154 if (cfg->dig_outs || cfg->dig_in_pin) {
3155 spec->multiout.max_channels = 2;
3156 spec->no_analog = 1;
3157 goto dig_only;
3158 }
3159 return 0; /* can't find valid BIOS pin config */
3160 }
3161
3162 if (!spec->no_primary_hp &&
3163 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3164 cfg->line_outs <= cfg->hp_outs) {
3165 /* use HP as primary out */
3166 cfg->speaker_outs = cfg->line_outs;
3167 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3168 sizeof(cfg->speaker_pins));
3169 cfg->line_outs = cfg->hp_outs;
3170 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3171 cfg->hp_outs = 0;
3172 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3173 cfg->line_out_type = AUTO_PIN_HP_OUT;
3174 }
3175
3176 err = parse_output_paths(codec);
3177 if (err < 0)
3178 return err;
3179 err = create_multi_channel_mode(codec);
3180 if (err < 0)
3181 return err;
3182 err = create_multi_out_ctls(codec, cfg);
3183 if (err < 0)
3184 return err;
3185 err = create_hp_out_ctls(codec);
3186 if (err < 0)
3187 return err;
3188 err = create_speaker_out_ctls(codec);
3189 if (err < 0)
3190 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003191 err = create_indep_hp_ctls(codec);
3192 if (err < 0)
3193 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003194 err = create_loopback_mixing_ctl(codec);
3195 if (err < 0)
3196 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003197 err = create_shared_input(codec);
3198 if (err < 0)
3199 return err;
3200 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003201 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003202 return err;
3203
Takashi Iwai352f7f92012-12-19 12:52:06 +01003204 /* check the multiple speaker pins */
3205 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
3206 spec->const_channel_count = cfg->line_outs * 2;
3207 else
3208 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003209
Takashi Iwai352f7f92012-12-19 12:52:06 +01003210 if (spec->multi_ios > 0)
3211 spec->multiout.max_channels = max(spec->ext_channel_count,
3212 spec->const_channel_count);
3213 else
3214 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3215
3216 err = check_auto_mute_availability(codec);
3217 if (err < 0)
3218 return err;
3219
3220 err = check_dyn_adc_switch(codec);
3221 if (err < 0)
3222 return err;
3223
3224 if (!spec->shared_mic_hp) {
3225 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003226 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003229
Takashi Iwai352f7f92012-12-19 12:52:06 +01003230 err = create_capture_mixers(codec);
3231 if (err < 0)
3232 return err;
3233
3234 err = parse_mic_boost(codec);
3235 if (err < 0)
3236 return err;
3237
3238 dig_only:
3239 parse_digital(codec);
3240
3241 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003243EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245
3246/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003247 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003249
3250/* slave controls for virtual master */
3251static const char * const slave_pfxs[] = {
3252 "Front", "Surround", "Center", "LFE", "Side",
3253 "Headphone", "Speaker", "Mono", "Line Out",
3254 "CLFE", "Bass Speaker", "PCM",
3255 NULL,
3256};
3257
3258int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003260 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Takashi Iwai36502d02012-12-19 15:15:10 +01003263 if (spec->kctls.used) {
3264 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3265 if (err < 0)
3266 return err;
3267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Takashi Iwai352f7f92012-12-19 12:52:06 +01003269 if (spec->multiout.dig_out_nid) {
3270 err = snd_hda_create_dig_out_ctls(codec,
3271 spec->multiout.dig_out_nid,
3272 spec->multiout.dig_out_nid,
3273 spec->pcm_rec[1].pcm_type);
3274 if (err < 0)
3275 return err;
3276 if (!spec->no_analog) {
3277 err = snd_hda_create_spdif_share_sw(codec,
3278 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 if (err < 0)
3280 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003281 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
3283 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003284 if (spec->dig_in_nid) {
3285 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3286 if (err < 0)
3287 return err;
3288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Takashi Iwai352f7f92012-12-19 12:52:06 +01003290 /* if we have no master control, let's create it */
3291 if (!spec->no_analog &&
3292 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3293 unsigned int vmaster_tlv[4];
3294 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3295 HDA_OUTPUT, vmaster_tlv);
3296 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3297 vmaster_tlv, slave_pfxs,
3298 "Playback Volume");
3299 if (err < 0)
3300 return err;
3301 }
3302 if (!spec->no_analog &&
3303 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3304 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3305 NULL, slave_pfxs,
3306 "Playback Switch",
3307 true, &spec->vmaster_mute.sw_kctl);
3308 if (err < 0)
3309 return err;
3310 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003311 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3312 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Takashi Iwai352f7f92012-12-19 12:52:06 +01003315 free_kctls(spec); /* no longer needed */
3316
3317 if (spec->shared_mic_hp) {
3318 int err;
3319 int nid = spec->autocfg.inputs[1].pin;
3320 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3321 if (err < 0)
3322 return err;
3323 err = snd_hda_jack_detect_enable(codec, nid, 0);
3324 if (err < 0)
3325 return err;
3326 }
3327
3328 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3329 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return err;
3331
3332 return 0;
3333}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003334EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
3337/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003338 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
Takashi Iwai352f7f92012-12-19 12:52:06 +01003341/*
3342 * Analog playback callbacks
3343 */
3344static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3345 struct hda_codec *codec,
3346 struct snd_pcm_substream *substream)
3347{
3348 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003349 int err;
3350
3351 mutex_lock(&spec->pcm_mutex);
3352 err = snd_hda_multi_out_analog_open(codec,
3353 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003354 hinfo);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003355 if (!err)
3356 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3357 mutex_unlock(&spec->pcm_mutex);
3358 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003359}
3360
3361static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003362 struct hda_codec *codec,
3363 unsigned int stream_tag,
3364 unsigned int format,
3365 struct snd_pcm_substream *substream)
3366{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003367 struct hda_gen_spec *spec = codec->spec;
3368 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3369 stream_tag, format, substream);
3370}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003371
Takashi Iwai352f7f92012-12-19 12:52:06 +01003372static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3373 struct hda_codec *codec,
3374 struct snd_pcm_substream *substream)
3375{
3376 struct hda_gen_spec *spec = codec->spec;
3377 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3378}
3379
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003380static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3381 struct hda_codec *codec,
3382 struct snd_pcm_substream *substream)
3383{
3384 struct hda_gen_spec *spec = codec->spec;
3385 mutex_lock(&spec->pcm_mutex);
3386 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3387 mutex_unlock(&spec->pcm_mutex);
3388 return 0;
3389}
3390
3391static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3392 struct hda_codec *codec,
3393 struct snd_pcm_substream *substream)
3394{
3395 struct hda_gen_spec *spec = codec->spec;
3396 int err = 0;
3397
3398 mutex_lock(&spec->pcm_mutex);
3399 if (!spec->indep_hp_enabled)
3400 err = -EBUSY;
3401 else
3402 spec->active_streams |= 1 << STREAM_INDEP_HP;
3403 mutex_unlock(&spec->pcm_mutex);
3404 return err;
3405}
3406
3407static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3408 struct hda_codec *codec,
3409 struct snd_pcm_substream *substream)
3410{
3411 struct hda_gen_spec *spec = codec->spec;
3412 mutex_lock(&spec->pcm_mutex);
3413 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3414 mutex_unlock(&spec->pcm_mutex);
3415 return 0;
3416}
3417
Takashi Iwai352f7f92012-12-19 12:52:06 +01003418/*
3419 * Digital out
3420 */
3421static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3422 struct hda_codec *codec,
3423 struct snd_pcm_substream *substream)
3424{
3425 struct hda_gen_spec *spec = codec->spec;
3426 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3427}
3428
3429static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3430 struct hda_codec *codec,
3431 unsigned int stream_tag,
3432 unsigned int format,
3433 struct snd_pcm_substream *substream)
3434{
3435 struct hda_gen_spec *spec = codec->spec;
3436 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3437 stream_tag, format, substream);
3438}
3439
3440static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3441 struct hda_codec *codec,
3442 struct snd_pcm_substream *substream)
3443{
3444 struct hda_gen_spec *spec = codec->spec;
3445 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3446}
3447
3448static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3449 struct hda_codec *codec,
3450 struct snd_pcm_substream *substream)
3451{
3452 struct hda_gen_spec *spec = codec->spec;
3453 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3454}
3455
3456/*
3457 * Analog capture
3458 */
3459static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3460 struct hda_codec *codec,
3461 unsigned int stream_tag,
3462 unsigned int format,
3463 struct snd_pcm_substream *substream)
3464{
3465 struct hda_gen_spec *spec = codec->spec;
3466
3467 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003468 stream_tag, 0, format);
3469 return 0;
3470}
3471
Takashi Iwai352f7f92012-12-19 12:52:06 +01003472static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3473 struct hda_codec *codec,
3474 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003475{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003476 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003477
Takashi Iwai352f7f92012-12-19 12:52:06 +01003478 snd_hda_codec_cleanup_stream(codec,
3479 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003480 return 0;
3481}
3482
Takashi Iwai352f7f92012-12-19 12:52:06 +01003483/*
3484 */
3485static const struct hda_pcm_stream pcm_analog_playback = {
3486 .substreams = 1,
3487 .channels_min = 2,
3488 .channels_max = 8,
3489 /* NID is set in build_pcms */
3490 .ops = {
3491 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003492 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003493 .prepare = playback_pcm_prepare,
3494 .cleanup = playback_pcm_cleanup
3495 },
3496};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Takashi Iwai352f7f92012-12-19 12:52:06 +01003498static const struct hda_pcm_stream pcm_analog_capture = {
3499 .substreams = 1,
3500 .channels_min = 2,
3501 .channels_max = 2,
3502 /* NID is set in build_pcms */
3503};
3504
3505static const struct hda_pcm_stream pcm_analog_alt_playback = {
3506 .substreams = 1,
3507 .channels_min = 2,
3508 .channels_max = 2,
3509 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003510 .ops = {
3511 .open = alt_playback_pcm_open,
3512 .close = alt_playback_pcm_close
3513 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003514};
3515
3516static const struct hda_pcm_stream pcm_analog_alt_capture = {
3517 .substreams = 2, /* can be overridden */
3518 .channels_min = 2,
3519 .channels_max = 2,
3520 /* NID is set in build_pcms */
3521 .ops = {
3522 .prepare = alt_capture_pcm_prepare,
3523 .cleanup = alt_capture_pcm_cleanup
3524 },
3525};
3526
3527static const struct hda_pcm_stream pcm_digital_playback = {
3528 .substreams = 1,
3529 .channels_min = 2,
3530 .channels_max = 2,
3531 /* NID is set in build_pcms */
3532 .ops = {
3533 .open = dig_playback_pcm_open,
3534 .close = dig_playback_pcm_close,
3535 .prepare = dig_playback_pcm_prepare,
3536 .cleanup = dig_playback_pcm_cleanup
3537 },
3538};
3539
3540static const struct hda_pcm_stream pcm_digital_capture = {
3541 .substreams = 1,
3542 .channels_min = 2,
3543 .channels_max = 2,
3544 /* NID is set in build_pcms */
3545};
3546
3547/* Used by build_pcms to flag that a PCM has no playback stream */
3548static const struct hda_pcm_stream pcm_null_stream = {
3549 .substreams = 0,
3550 .channels_min = 0,
3551 .channels_max = 0,
3552};
3553
3554/*
3555 * dynamic changing ADC PCM streams
3556 */
3557static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3558{
3559 struct hda_gen_spec *spec = codec->spec;
3560 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3561
3562 if (spec->cur_adc && spec->cur_adc != new_adc) {
3563 /* stream is running, let's swap the current ADC */
3564 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3565 spec->cur_adc = new_adc;
3566 snd_hda_codec_setup_stream(codec, new_adc,
3567 spec->cur_adc_stream_tag, 0,
3568 spec->cur_adc_format);
3569 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003571 return false;
3572}
3573
3574/* analog capture with dynamic dual-adc changes */
3575static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3576 struct hda_codec *codec,
3577 unsigned int stream_tag,
3578 unsigned int format,
3579 struct snd_pcm_substream *substream)
3580{
3581 struct hda_gen_spec *spec = codec->spec;
3582 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3583 spec->cur_adc_stream_tag = stream_tag;
3584 spec->cur_adc_format = format;
3585 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3586 return 0;
3587}
3588
3589static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3590 struct hda_codec *codec,
3591 struct snd_pcm_substream *substream)
3592{
3593 struct hda_gen_spec *spec = codec->spec;
3594 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3595 spec->cur_adc = 0;
3596 return 0;
3597}
3598
3599static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3600 .substreams = 1,
3601 .channels_min = 2,
3602 .channels_max = 2,
3603 .nid = 0, /* fill later */
3604 .ops = {
3605 .prepare = dyn_adc_capture_pcm_prepare,
3606 .cleanup = dyn_adc_capture_pcm_cleanup
3607 },
3608};
3609
Takashi Iwaif873e532012-12-20 16:58:39 +01003610static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3611 const char *chip_name)
3612{
3613 char *p;
3614
3615 if (*str)
3616 return;
3617 strlcpy(str, chip_name, len);
3618
3619 /* drop non-alnum chars after a space */
3620 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3621 if (!isalnum(p[1])) {
3622 *p = 0;
3623 break;
3624 }
3625 }
3626 strlcat(str, sfx, len);
3627}
3628
Takashi Iwai352f7f92012-12-19 12:52:06 +01003629/* build PCM streams based on the parsed results */
3630int snd_hda_gen_build_pcms(struct hda_codec *codec)
3631{
3632 struct hda_gen_spec *spec = codec->spec;
3633 struct hda_pcm *info = spec->pcm_rec;
3634 const struct hda_pcm_stream *p;
3635 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
3637 codec->num_pcms = 1;
3638 codec->pcm_info = info;
3639
Takashi Iwai352f7f92012-12-19 12:52:06 +01003640 if (spec->no_analog)
3641 goto skip_analog;
3642
Takashi Iwaif873e532012-12-20 16:58:39 +01003643 fill_pcm_stream_name(spec->stream_name_analog,
3644 sizeof(spec->stream_name_analog),
3645 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003646 info->name = spec->stream_name_analog;
3647
3648 if (spec->multiout.num_dacs > 0) {
3649 p = spec->stream_analog_playback;
3650 if (!p)
3651 p = &pcm_analog_playback;
3652 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3653 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3654 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3655 spec->multiout.max_channels;
3656 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3657 spec->autocfg.line_outs == 2)
3658 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3659 snd_pcm_2_1_chmaps;
3660 }
3661 if (spec->num_adc_nids) {
3662 p = spec->stream_analog_capture;
3663 if (!p) {
3664 if (spec->dyn_adc_switch)
3665 p = &dyn_adc_pcm_analog_capture;
3666 else
3667 p = &pcm_analog_capture;
3668 }
3669 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3670 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3671 }
3672
Takashi Iwai352f7f92012-12-19 12:52:06 +01003673 skip_analog:
3674 /* SPDIF for stream index #1 */
3675 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003676 fill_pcm_stream_name(spec->stream_name_digital,
3677 sizeof(spec->stream_name_digital),
3678 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003679 codec->num_pcms = 2;
3680 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3681 info = spec->pcm_rec + 1;
3682 info->name = spec->stream_name_digital;
3683 if (spec->dig_out_type)
3684 info->pcm_type = spec->dig_out_type;
3685 else
3686 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3687 if (spec->multiout.dig_out_nid) {
3688 p = spec->stream_digital_playback;
3689 if (!p)
3690 p = &pcm_digital_playback;
3691 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3692 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3693 }
3694 if (spec->dig_in_nid) {
3695 p = spec->stream_digital_capture;
3696 if (!p)
3697 p = &pcm_digital_capture;
3698 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3699 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3700 }
3701 }
3702
3703 if (spec->no_analog)
3704 return 0;
3705
3706 /* If the use of more than one ADC is requested for the current
3707 * model, configure a second analog capture-only PCM.
3708 */
3709 have_multi_adcs = (spec->num_adc_nids > 1) &&
3710 !spec->dyn_adc_switch && !spec->auto_mic;
3711 /* Additional Analaog capture for index #2 */
3712 if (spec->alt_dac_nid || have_multi_adcs) {
3713 codec->num_pcms = 3;
3714 info = spec->pcm_rec + 2;
3715 info->name = spec->stream_name_analog;
3716 if (spec->alt_dac_nid) {
3717 p = spec->stream_analog_alt_playback;
3718 if (!p)
3719 p = &pcm_analog_alt_playback;
3720 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3721 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3722 spec->alt_dac_nid;
3723 } else {
3724 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3725 pcm_null_stream;
3726 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3727 }
3728 if (have_multi_adcs) {
3729 p = spec->stream_analog_alt_capture;
3730 if (!p)
3731 p = &pcm_analog_alt_capture;
3732 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3733 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3734 spec->adc_nids[1];
3735 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3736 spec->num_adc_nids - 1;
3737 } else {
3738 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3739 pcm_null_stream;
3740 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 }
3743
3744 return 0;
3745}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003746EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3747
3748
3749/*
3750 * Standard auto-parser initializations
3751 */
3752
3753/* configure the path from the given dac to the pin as the proper output */
3754static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai196c17662013-01-04 15:01:40 +01003755 int pin_type, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003756{
3757 struct nid_path *path;
3758
3759 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
Takashi Iwai196c17662013-01-04 15:01:40 +01003760 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003761 if (!path)
3762 return;
Takashi Iwaie1284af2013-01-03 16:33:02 +01003763 snd_hda_activate_path(codec, path, path->active, true);
3764 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003765}
3766
3767/* initialize primary output paths */
3768static void init_multi_out(struct hda_codec *codec)
3769{
3770 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai196c17662013-01-04 15:01:40 +01003771 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003772 int pin_type;
3773 int i;
3774
3775 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3776 pin_type = PIN_HP;
3777 else
3778 pin_type = PIN_OUT;
3779
Takashi Iwai64049c82012-12-20 15:29:21 +01003780 for (i = 0; i < spec->autocfg.line_outs; i++) {
3781 nid = spec->autocfg.line_out_pins[i];
Takashi Iwai196c17662013-01-04 15:01:40 +01003782 if (nid)
3783 set_output_and_unmute(codec, nid, pin_type,
3784 spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003785 }
3786}
3787
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003788
3789static void __init_extra_out(struct hda_codec *codec, int num_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01003790 hda_nid_t *pins, int *paths, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003791{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003792 int i;
Takashi Iwai196c17662013-01-04 15:01:40 +01003793 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003794
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003795 for (i = 0; i < num_outs; i++) {
3796 pin = pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003797 if (!pin)
3798 break;
Takashi Iwai196c17662013-01-04 15:01:40 +01003799 set_output_and_unmute(codec, pin, type, paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003800 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003801}
3802
3803/* initialize hp and speaker paths */
3804static void init_extra_out(struct hda_codec *codec)
3805{
3806 struct hda_gen_spec *spec = codec->spec;
3807
3808 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3809 __init_extra_out(codec, spec->autocfg.hp_outs,
3810 spec->autocfg.hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01003811 spec->hp_paths, PIN_HP);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003812 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3813 __init_extra_out(codec, spec->autocfg.speaker_outs,
3814 spec->autocfg.speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01003815 spec->speaker_paths, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003816}
3817
3818/* initialize multi-io paths */
3819static void init_multi_io(struct hda_codec *codec)
3820{
3821 struct hda_gen_spec *spec = codec->spec;
3822 int i;
3823
3824 for (i = 0; i < spec->multi_ios; i++) {
3825 hda_nid_t pin = spec->multi_io[i].pin;
3826 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01003827 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003828 if (!path)
3829 continue;
3830 if (!spec->multi_io[i].ctl_in)
3831 spec->multi_io[i].ctl_in =
3832 snd_hda_codec_update_cache(codec, pin, 0,
3833 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3834 snd_hda_activate_path(codec, path, path->active, true);
3835 }
3836}
3837
3838/* set up the input pin config, depending on the given auto-pin type */
3839static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3840 int auto_pin_type)
3841{
3842 unsigned int val = PIN_IN;
3843 if (auto_pin_type == AUTO_PIN_MIC)
3844 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003845 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003846}
3847
3848/* set up input pins and loopback paths */
3849static void init_analog_input(struct hda_codec *codec)
3850{
3851 struct hda_gen_spec *spec = codec->spec;
3852 struct auto_pin_cfg *cfg = &spec->autocfg;
3853 int i;
3854
3855 for (i = 0; i < cfg->num_inputs; i++) {
3856 hda_nid_t nid = cfg->inputs[i].pin;
3857 if (is_input_pin(codec, nid))
3858 set_input_pin(codec, nid, cfg->inputs[i].type);
3859
3860 /* init loopback inputs */
3861 if (spec->mixer_nid) {
3862 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01003863 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003864 if (path)
3865 snd_hda_activate_path(codec, path,
3866 path->active, false);
3867 }
3868 }
3869}
3870
3871/* initialize ADC paths */
3872static void init_input_src(struct hda_codec *codec)
3873{
3874 struct hda_gen_spec *spec = codec->spec;
3875 struct hda_input_mux *imux = &spec->input_mux;
3876 struct nid_path *path;
3877 int i, c, nums;
3878
3879 if (spec->dyn_adc_switch)
3880 nums = 1;
3881 else
3882 nums = spec->num_adc_nids;
3883
3884 for (c = 0; c < nums; c++) {
3885 for (i = 0; i < imux->num_items; i++) {
3886 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3887 get_adc_nid(codec, c, i));
3888 if (path) {
3889 bool active = path->active;
3890 if (i == spec->cur_mux[c])
3891 active = true;
3892 snd_hda_activate_path(codec, path, active, false);
3893 }
3894 }
3895 }
3896
3897 if (spec->shared_mic_hp)
3898 update_shared_mic_hp(codec, spec->cur_mux[0]);
3899
3900 if (spec->cap_sync_hook)
3901 spec->cap_sync_hook(codec);
3902}
3903
3904/* set right pin controls for digital I/O */
3905static void init_digital(struct hda_codec *codec)
3906{
3907 struct hda_gen_spec *spec = codec->spec;
3908 int i;
3909 hda_nid_t pin;
3910
3911 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3912 pin = spec->autocfg.dig_out_pins[i];
3913 if (!pin)
3914 continue;
Takashi Iwai196c17662013-01-04 15:01:40 +01003915 set_output_and_unmute(codec, pin, PIN_OUT,
3916 spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003917 }
3918 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003919 if (pin) {
3920 struct nid_path *path;
Takashi Iwai7594aa32012-12-20 15:38:40 +01003921 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003922 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
3923 if (path)
3924 snd_hda_activate_path(codec, path, path->active, false);
3925 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003926}
3927
Takashi Iwai973e4972012-12-20 15:16:09 +01003928/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3929 * invalid unsol tags by some reason
3930 */
3931static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3932{
3933 int i;
3934
3935 for (i = 0; i < codec->init_pins.used; i++) {
3936 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3937 hda_nid_t nid = pin->nid;
3938 if (is_jack_detectable(codec, nid) &&
3939 !snd_hda_jack_tbl_get(codec, nid))
3940 snd_hda_codec_update_cache(codec, nid, 0,
3941 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3942 }
3943}
3944
Takashi Iwai352f7f92012-12-19 12:52:06 +01003945int snd_hda_gen_init(struct hda_codec *codec)
3946{
3947 struct hda_gen_spec *spec = codec->spec;
3948
3949 if (spec->init_hook)
3950 spec->init_hook(codec);
3951
3952 snd_hda_apply_verbs(codec);
3953
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003954 codec->cached_write = 1;
3955
Takashi Iwai352f7f92012-12-19 12:52:06 +01003956 init_multi_out(codec);
3957 init_extra_out(codec);
3958 init_multi_io(codec);
3959 init_analog_input(codec);
3960 init_input_src(codec);
3961 init_digital(codec);
3962
Takashi Iwai973e4972012-12-20 15:16:09 +01003963 clear_unsol_on_unused_pins(codec);
3964
Takashi Iwai352f7f92012-12-19 12:52:06 +01003965 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003966 snd_hda_gen_hp_automute(codec, NULL);
3967 snd_hda_gen_line_automute(codec, NULL);
3968 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003969
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003970 snd_hda_codec_flush_amp_cache(codec);
3971 snd_hda_codec_flush_cmd_cache(codec);
3972
Takashi Iwai352f7f92012-12-19 12:52:06 +01003973 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3974 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3975
3976 hda_call_check_power_status(codec, 0x01);
3977 return 0;
3978}
3979EXPORT_SYMBOL(snd_hda_gen_init);
3980
3981
3982/*
3983 * the generic codec support
3984 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Takashi Iwai83012a72012-08-24 18:38:08 +02003986#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003987static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3988{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003989 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003990 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3991}
3992#endif
3993
Takashi Iwai352f7f92012-12-19 12:52:06 +01003994static void generic_free(struct hda_codec *codec)
3995{
3996 snd_hda_gen_spec_free(codec->spec);
3997 kfree(codec->spec);
3998 codec->spec = NULL;
3999}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
Takashi Iwai352f7f92012-12-19 12:52:06 +01004001static const struct hda_codec_ops generic_patch_ops = {
4002 .build_controls = snd_hda_gen_build_controls,
4003 .build_pcms = snd_hda_gen_build_pcms,
4004 .init = snd_hda_gen_init,
4005 .free = generic_free,
4006 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004007#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004008 .check_power_status = generic_check_power_status,
4009#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010};
4011
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012int snd_hda_parse_generic_codec(struct hda_codec *codec)
4013{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004014 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 int err;
4016
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004017 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004018 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004020 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004023 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4024 if (err < 0)
4025 return err;
4026
4027 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004028 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 goto error;
4030
4031 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 return 0;
4033
Takashi Iwai352f7f92012-12-19 12:52:06 +01004034error:
4035 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 return err;
4037}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004038EXPORT_SYMBOL(snd_hda_parse_generic_codec);