blob: c8bf812302064c0bffd9c62e65b1ed8e4c5c29e4 [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
863/* try to assign DACs to pins and return the resultant badness */
864static int try_assign_dacs(struct hda_codec *codec, int num_outs,
865 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +0100866 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100867 const struct badness_table *bad)
868{
869 struct hda_gen_spec *spec = codec->spec;
870 struct auto_pin_cfg *cfg = &spec->autocfg;
871 int i, j;
872 int badness = 0;
873 hda_nid_t dac;
874
875 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return 0;
877
Takashi Iwai352f7f92012-12-19 12:52:06 +0100878 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100879 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100880 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +0100881
882 if (dacs[i]) {
883 badness += assign_out_path_ctls(codec, pin, dacs[i]);
884 continue;
885 }
886
887 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100888 if (!dacs[i] && !i) {
889 for (j = 1; j < num_outs; j++) {
890 if (is_reachable_path(codec, dacs[j], pin)) {
891 dacs[0] = dacs[j];
892 dacs[j] = 0;
Takashi Iwai196c17662013-01-04 15:01:40 +0100893 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100894 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100897 }
898 dac = dacs[i];
899 if (!dac) {
900 if (is_reachable_path(codec, dacs[0], pin))
901 dac = dacs[0];
902 else if (cfg->line_outs > i &&
903 is_reachable_path(codec, spec->private_dac_nids[i], pin))
904 dac = spec->private_dac_nids[i];
905 if (dac) {
906 if (!i)
907 badness += bad->shared_primary;
908 else if (i == 1)
909 badness += bad->shared_surr;
910 else
911 badness += bad->shared_clfe;
912 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
913 dac = spec->private_dac_nids[0];
914 badness += bad->shared_surr_main;
915 } else if (!i)
916 badness += bad->no_primary_dac;
917 else
918 badness += bad->no_dac;
919 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100920 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
Takashi Iwaib3a8c742012-12-20 18:29:16 +0100921 if (!path && i > 0 && spec->mixer_nid) {
922 /* try with aamix */
923 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
924 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100925 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100926 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +0100927 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100928 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100929 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +0100930 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100931 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100932 if (dac)
933 badness += assign_out_path_ctls(codec, pin, dac);
934 }
935
936 return badness;
937}
938
939/* return NID if the given pin has only a single connection to a certain DAC */
940static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
941{
942 struct hda_gen_spec *spec = codec->spec;
943 int i;
944 hda_nid_t nid_found = 0;
945
946 for (i = 0; i < spec->num_all_dacs; i++) {
947 hda_nid_t nid = spec->all_dacs[i];
948 if (!nid || is_dac_already_used(codec, nid))
949 continue;
950 if (is_reachable_path(codec, nid, pin)) {
951 if (nid_found)
952 return 0;
953 nid_found = nid;
954 }
955 }
956 return nid_found;
957}
958
959/* check whether the given pin can be a multi-io pin */
960static bool can_be_multiio_pin(struct hda_codec *codec,
961 unsigned int location, hda_nid_t nid)
962{
963 unsigned int defcfg, caps;
964
965 defcfg = snd_hda_codec_get_pincfg(codec, nid);
966 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
967 return false;
968 if (location && get_defcfg_location(defcfg) != location)
969 return false;
970 caps = snd_hda_query_pin_caps(codec, nid);
971 if (!(caps & AC_PINCAP_OUT))
972 return false;
973 return true;
974}
975
Takashi Iwaie22aab72013-01-04 14:50:04 +0100976/* count the number of input pins that are capable to be multi-io */
977static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
978{
979 struct hda_gen_spec *spec = codec->spec;
980 struct auto_pin_cfg *cfg = &spec->autocfg;
981 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
982 unsigned int location = get_defcfg_location(defcfg);
983 int type, i;
984 int num_pins = 0;
985
986 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
987 for (i = 0; i < cfg->num_inputs; i++) {
988 if (cfg->inputs[i].type != type)
989 continue;
990 if (can_be_multiio_pin(codec, location,
991 cfg->inputs[i].pin))
992 num_pins++;
993 }
994 }
995 return num_pins;
996}
997
Takashi Iwai352f7f92012-12-19 12:52:06 +0100998/*
999 * multi-io helper
1000 *
1001 * When hardwired is set, try to fill ony hardwired pins, and returns
1002 * zero if any pins are filled, non-zero if nothing found.
1003 * When hardwired is off, try to fill possible input pins, and returns
1004 * the badness value.
1005 */
1006static int fill_multi_ios(struct hda_codec *codec,
1007 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001008 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001009{
1010 struct hda_gen_spec *spec = codec->spec;
1011 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001012 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001013 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1014 unsigned int location = get_defcfg_location(defcfg);
1015 int badness = 0;
1016
1017 old_pins = spec->multi_ios;
1018 if (old_pins >= 2)
1019 goto end_fill;
1020
Takashi Iwaie22aab72013-01-04 14:50:04 +01001021 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001022 if (num_pins < 2)
1023 goto end_fill;
1024
Takashi Iwai352f7f92012-12-19 12:52:06 +01001025 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1026 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001027 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001028 hda_nid_t nid = cfg->inputs[i].pin;
1029 hda_nid_t dac = 0;
1030
1031 if (cfg->inputs[i].type != type)
1032 continue;
1033 if (!can_be_multiio_pin(codec, location, nid))
1034 continue;
1035 for (j = 0; j < spec->multi_ios; j++) {
1036 if (nid == spec->multi_io[j].pin)
1037 break;
1038 }
1039 if (j < spec->multi_ios)
1040 continue;
1041
Takashi Iwai352f7f92012-12-19 12:52:06 +01001042 if (hardwired)
1043 dac = get_dac_if_single(codec, nid);
1044 else if (!dac)
1045 dac = look_for_dac(codec, nid, false);
1046 if (!dac) {
1047 badness++;
1048 continue;
1049 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001050 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001051 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001052 badness++;
1053 continue;
1054 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001055 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001056 spec->multi_io[spec->multi_ios].pin = nid;
1057 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001058 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1059 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001060 spec->multi_ios++;
1061 if (spec->multi_ios >= 2)
1062 break;
1063 }
1064 }
1065 end_fill:
1066 if (badness)
1067 badness = BAD_MULTI_IO;
1068 if (old_pins == spec->multi_ios) {
1069 if (hardwired)
1070 return 1; /* nothing found */
1071 else
1072 return badness; /* no badness if nothing found */
1073 }
1074 if (!hardwired && spec->multi_ios < 2) {
1075 /* cancel newly assigned paths */
1076 spec->paths.used -= spec->multi_ios - old_pins;
1077 spec->multi_ios = old_pins;
1078 return badness;
1079 }
1080
1081 /* assign volume and mute controls */
1082 for (i = old_pins; i < spec->multi_ios; i++)
1083 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1084 spec->multi_io[i].dac);
1085
1086 return badness;
1087}
1088
1089/* map DACs for all pins in the list if they are single connections */
1090static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001091 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001092{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001093 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001094 int i;
1095 bool found = false;
1096 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001097 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001098 hda_nid_t dac;
1099 if (dacs[i])
1100 continue;
1101 dac = get_dac_if_single(codec, pins[i]);
1102 if (!dac)
1103 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001104 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001105 if (!path && i > 0 && spec->mixer_nid)
1106 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001107 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001108 dacs[i] = dac;
1109 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001110 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001111 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001112 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001113 }
1114 }
1115 return found;
1116}
1117
1118/* fill in the dac_nids table from the parsed pin configuration */
1119static int fill_and_eval_dacs(struct hda_codec *codec,
1120 bool fill_hardwired,
1121 bool fill_mio_first)
1122{
1123 struct hda_gen_spec *spec = codec->spec;
1124 struct auto_pin_cfg *cfg = &spec->autocfg;
1125 int i, err, badness;
1126
1127 /* set num_dacs once to full for look_for_dac() */
1128 spec->multiout.num_dacs = cfg->line_outs;
1129 spec->multiout.dac_nids = spec->private_dac_nids;
1130 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1131 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1132 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1133 spec->multi_ios = 0;
1134 snd_array_free(&spec->paths);
1135 badness = 0;
1136
1137 /* fill hard-wired DACs first */
1138 if (fill_hardwired) {
1139 bool mapped;
1140 do {
1141 mapped = map_singles(codec, cfg->line_outs,
1142 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001143 spec->private_dac_nids,
1144 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001145 mapped |= map_singles(codec, cfg->hp_outs,
1146 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001147 spec->multiout.hp_out_nid,
1148 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001149 mapped |= map_singles(codec, cfg->speaker_outs,
1150 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001151 spec->multiout.extra_out_nid,
1152 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001153 if (fill_mio_first && cfg->line_outs == 1 &&
1154 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001155 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001156 if (!err)
1157 mapped = true;
1158 }
1159 } while (mapped);
1160 }
1161
1162 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001163 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001164 &main_out_badness);
1165
1166 /* re-count num_dacs and squash invalid entries */
1167 spec->multiout.num_dacs = 0;
1168 for (i = 0; i < cfg->line_outs; i++) {
1169 if (spec->private_dac_nids[i])
1170 spec->multiout.num_dacs++;
1171 else {
1172 memmove(spec->private_dac_nids + i,
1173 spec->private_dac_nids + i + 1,
1174 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1175 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1176 }
1177 }
1178
1179 if (fill_mio_first &&
1180 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1181 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001182 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001183 if (err < 0)
1184 return err;
1185 /* we don't count badness at this stage yet */
1186 }
1187
1188 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1189 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1190 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001191 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001192 &extra_out_badness);
1193 if (err < 0)
1194 return err;
1195 badness += err;
1196 }
1197 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1198 err = try_assign_dacs(codec, cfg->speaker_outs,
1199 cfg->speaker_pins,
1200 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001201 spec->speaker_paths,
1202 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001203 if (err < 0)
1204 return err;
1205 badness += err;
1206 }
1207 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001208 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001209 if (err < 0)
1210 return err;
1211 badness += err;
1212 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001213
1214 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1215 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1216 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001217
1218 if (spec->multi_ios == 2) {
1219 for (i = 0; i < 2; i++)
1220 spec->private_dac_nids[spec->multiout.num_dacs++] =
1221 spec->multi_io[i].dac;
1222 spec->ext_channel_count = 2;
1223 } else if (spec->multi_ios) {
1224 spec->multi_ios = 0;
1225 badness += BAD_MULTI_IO;
1226 }
1227
1228 return badness;
1229}
1230
1231#define DEBUG_BADNESS
1232
1233#ifdef DEBUG_BADNESS
1234#define debug_badness snd_printdd
1235#else
1236#define debug_badness(...)
1237#endif
1238
1239static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1240{
1241 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1242 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001243 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001244 spec->multiout.dac_nids[0],
1245 spec->multiout.dac_nids[1],
1246 spec->multiout.dac_nids[2],
1247 spec->multiout.dac_nids[3]);
1248 if (spec->multi_ios > 0)
1249 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1250 spec->multi_ios,
1251 spec->multi_io[0].pin, spec->multi_io[1].pin,
1252 spec->multi_io[0].dac, spec->multi_io[1].dac);
1253 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1254 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001255 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001256 spec->multiout.hp_out_nid[0],
1257 spec->multiout.hp_out_nid[1],
1258 spec->multiout.hp_out_nid[2],
1259 spec->multiout.hp_out_nid[3]);
1260 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1261 cfg->speaker_pins[0], cfg->speaker_pins[1],
1262 cfg->speaker_pins[2], cfg->speaker_pins[3],
1263 spec->multiout.extra_out_nid[0],
1264 spec->multiout.extra_out_nid[1],
1265 spec->multiout.extra_out_nid[2],
1266 spec->multiout.extra_out_nid[3]);
1267}
1268
1269/* find all available DACs of the codec */
1270static void fill_all_dac_nids(struct hda_codec *codec)
1271{
1272 struct hda_gen_spec *spec = codec->spec;
1273 int i;
1274 hda_nid_t nid = codec->start_nid;
1275
1276 spec->num_all_dacs = 0;
1277 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1278 for (i = 0; i < codec->num_nodes; i++, nid++) {
1279 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1280 continue;
1281 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1282 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1283 break;
1284 }
1285 spec->all_dacs[spec->num_all_dacs++] = nid;
1286 }
1287}
1288
1289static int parse_output_paths(struct hda_codec *codec)
1290{
1291 struct hda_gen_spec *spec = codec->spec;
1292 struct auto_pin_cfg *cfg = &spec->autocfg;
1293 struct auto_pin_cfg *best_cfg;
1294 int best_badness = INT_MAX;
1295 int badness;
1296 bool fill_hardwired = true, fill_mio_first = true;
1297 bool best_wired = true, best_mio = true;
1298 bool hp_spk_swapped = false;
1299
1300 fill_all_dac_nids(codec);
1301
1302 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1303 if (!best_cfg)
1304 return -ENOMEM;
1305 *best_cfg = *cfg;
1306
1307 for (;;) {
1308 badness = fill_and_eval_dacs(codec, fill_hardwired,
1309 fill_mio_first);
1310 if (badness < 0) {
1311 kfree(best_cfg);
1312 return badness;
1313 }
1314 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1315 cfg->line_out_type, fill_hardwired, fill_mio_first,
1316 badness);
1317 debug_show_configs(spec, cfg);
1318 if (badness < best_badness) {
1319 best_badness = badness;
1320 *best_cfg = *cfg;
1321 best_wired = fill_hardwired;
1322 best_mio = fill_mio_first;
1323 }
1324 if (!badness)
1325 break;
1326 fill_mio_first = !fill_mio_first;
1327 if (!fill_mio_first)
1328 continue;
1329 fill_hardwired = !fill_hardwired;
1330 if (!fill_hardwired)
1331 continue;
1332 if (hp_spk_swapped)
1333 break;
1334 hp_spk_swapped = true;
1335 if (cfg->speaker_outs > 0 &&
1336 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1337 cfg->hp_outs = cfg->line_outs;
1338 memcpy(cfg->hp_pins, cfg->line_out_pins,
1339 sizeof(cfg->hp_pins));
1340 cfg->line_outs = cfg->speaker_outs;
1341 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1342 sizeof(cfg->speaker_pins));
1343 cfg->speaker_outs = 0;
1344 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1345 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1346 fill_hardwired = true;
1347 continue;
1348 }
1349 if (cfg->hp_outs > 0 &&
1350 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1351 cfg->speaker_outs = cfg->line_outs;
1352 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1353 sizeof(cfg->speaker_pins));
1354 cfg->line_outs = cfg->hp_outs;
1355 memcpy(cfg->line_out_pins, cfg->hp_pins,
1356 sizeof(cfg->hp_pins));
1357 cfg->hp_outs = 0;
1358 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1359 cfg->line_out_type = AUTO_PIN_HP_OUT;
1360 fill_hardwired = true;
1361 continue;
1362 }
1363 break;
1364 }
1365
1366 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001367 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001368 *cfg = *best_cfg;
1369 fill_and_eval_dacs(codec, best_wired, best_mio);
1370 }
1371 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1372 cfg->line_out_type, best_wired, best_mio);
1373 debug_show_configs(spec, cfg);
1374
1375 if (cfg->line_out_pins[0]) {
1376 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001377 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001378 if (path)
1379 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1380 }
1381
1382 kfree(best_cfg);
1383 return 0;
1384}
1385
1386/* add playback controls from the parsed DAC table */
1387static int create_multi_out_ctls(struct hda_codec *codec,
1388 const struct auto_pin_cfg *cfg)
1389{
1390 struct hda_gen_spec *spec = codec->spec;
1391 int i, err, noutputs;
1392
1393 noutputs = cfg->line_outs;
1394 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1395 noutputs += spec->multi_ios;
1396
1397 for (i = 0; i < noutputs; i++) {
1398 const char *name;
1399 int index;
Takashi Iwai196c17662013-01-04 15:01:40 +01001400 hda_nid_t dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001401 struct nid_path *path;
1402
1403 dac = spec->multiout.dac_nids[i];
1404 if (!dac)
1405 continue;
1406 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001407 index = 0;
1408 name = channel_name[i];
1409 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001410 name = get_line_out_pfx(spec, i, true, &index);
1411 }
1412
Takashi Iwai196c17662013-01-04 15:01:40 +01001413 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001414 if (!path)
1415 continue;
1416 if (!name || !strcmp(name, "CLFE")) {
1417 /* Center/LFE */
1418 err = add_vol_ctl(codec, "Center", 0, 1, path);
1419 if (err < 0)
1420 return err;
1421 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1422 if (err < 0)
1423 return err;
1424 err = add_sw_ctl(codec, "Center", 0, 1, path);
1425 if (err < 0)
1426 return err;
1427 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1428 if (err < 0)
1429 return err;
1430 } else {
1431 err = add_stereo_vol(codec, name, index, path);
1432 if (err < 0)
1433 return err;
1434 err = add_stereo_sw(codec, name, index, path);
1435 if (err < 0)
1436 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438 }
1439 return 0;
1440}
1441
Takashi Iwai352f7f92012-12-19 12:52:06 +01001442static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai196c17662013-01-04 15:01:40 +01001443 hda_nid_t dac, int path_idx,
1444 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001446 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int err;
1448
Takashi Iwai196c17662013-01-04 15:01:40 +01001449 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001450 if (!path)
1451 return 0;
1452 /* bind volume control will be created in the case of dac = 0 */
1453 if (dac) {
1454 err = add_stereo_vol(codec, pfx, cidx, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (err < 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001456 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001458 err = add_stereo_sw(codec, pfx, cidx, path);
1459 if (err < 0)
1460 return err;
1461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Takashi Iwai352f7f92012-12-19 12:52:06 +01001464/* add playback controls for speaker and HP outputs */
1465static int create_extra_outs(struct hda_codec *codec, int num_pins,
1466 const hda_nid_t *pins, const hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001467 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001469 struct hda_gen_spec *spec = codec->spec;
1470 struct hda_bind_ctls *ctl;
1471 char name[32];
1472 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Takashi Iwai352f7f92012-12-19 12:52:06 +01001474 if (!num_pins || !pins[0])
1475 return 0;
1476
1477 if (num_pins == 1) {
1478 hda_nid_t dac = *dacs;
1479 if (!dac)
1480 dac = spec->multiout.dac_nids[0];
Takashi Iwai196c17662013-01-04 15:01:40 +01001481 return create_extra_out(codec, *pins, dac, paths[0], pfx, 0);
Takashi Iwai97ec5582006-03-21 11:29:07 +01001482 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001483
1484 for (i = 0; i < num_pins; i++) {
1485 hda_nid_t dac;
1486 if (dacs[num_pins - 1])
1487 dac = dacs[i]; /* with individual volumes */
Takashi Iwai97ec5582006-03-21 11:29:07 +01001488 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001489 dac = 0;
1490 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
Takashi Iwai196c17662013-01-04 15:01:40 +01001491 err = create_extra_out(codec, pins[i], dac, paths[i],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001492 "Bass Speaker", 0);
1493 } else if (num_pins >= 3) {
1494 snprintf(name, sizeof(name), "%s %s",
1495 pfx, channel_name[i]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001496 err = create_extra_out(codec, pins[i], dac, paths[i],
1497 name, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001498 } else {
Takashi Iwai196c17662013-01-04 15:01:40 +01001499 err = create_extra_out(codec, pins[i], dac, paths[i],
1500 pfx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001502 if (err < 0)
1503 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001505 if (dacs[num_pins - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return 0;
1507
Takashi Iwai352f7f92012-12-19 12:52:06 +01001508 /* Let's create a bind-controls for volumes */
1509 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1510 if (!ctl)
1511 return -ENOMEM;
1512 n = 0;
1513 for (i = 0; i < num_pins; i++) {
1514 hda_nid_t vol;
1515 struct nid_path *path;
1516 if (!pins[i] || !dacs[i])
1517 continue;
Takashi Iwai196c17662013-01-04 15:01:40 +01001518 path = snd_hda_get_path_from_idx(codec, paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001519 if (!path)
1520 continue;
1521 vol = look_for_out_vol_nid(codec, path);
1522 if (vol)
1523 ctl->values[n++] =
1524 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1525 }
1526 if (n) {
1527 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1528 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1529 if (err < 0)
1530 return err;
1531 }
1532 return 0;
1533}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001534
Takashi Iwai352f7f92012-12-19 12:52:06 +01001535static int create_hp_out_ctls(struct hda_codec *codec)
1536{
1537 struct hda_gen_spec *spec = codec->spec;
1538 return create_extra_outs(codec, spec->autocfg.hp_outs,
1539 spec->autocfg.hp_pins,
1540 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001541 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001542 "Headphone");
1543}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Takashi Iwai352f7f92012-12-19 12:52:06 +01001545static int create_speaker_out_ctls(struct hda_codec *codec)
1546{
1547 struct hda_gen_spec *spec = codec->spec;
1548 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1549 spec->autocfg.speaker_pins,
1550 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001551 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001552 "Speaker");
1553}
1554
1555/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001556 * independent HP controls
1557 */
1558
1559static int indep_hp_info(struct snd_kcontrol *kcontrol,
1560 struct snd_ctl_elem_info *uinfo)
1561{
1562 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1563}
1564
1565static int indep_hp_get(struct snd_kcontrol *kcontrol,
1566 struct snd_ctl_elem_value *ucontrol)
1567{
1568 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1569 struct hda_gen_spec *spec = codec->spec;
1570 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1571 return 0;
1572}
1573
1574static int indep_hp_put(struct snd_kcontrol *kcontrol,
1575 struct snd_ctl_elem_value *ucontrol)
1576{
1577 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1578 struct hda_gen_spec *spec = codec->spec;
1579 unsigned int select = ucontrol->value.enumerated.item[0];
1580 int ret = 0;
1581
1582 mutex_lock(&spec->pcm_mutex);
1583 if (spec->active_streams) {
1584 ret = -EBUSY;
1585 goto unlock;
1586 }
1587
1588 if (spec->indep_hp_enabled != select) {
1589 spec->indep_hp_enabled = select;
1590 if (spec->indep_hp_enabled)
1591 spec->multiout.hp_out_nid[0] = 0;
1592 else
1593 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1594 ret = 1;
1595 }
1596 unlock:
1597 mutex_unlock(&spec->pcm_mutex);
1598 return ret;
1599}
1600
1601static const struct snd_kcontrol_new indep_hp_ctl = {
1602 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1603 .name = "Independent HP",
1604 .info = indep_hp_info,
1605 .get = indep_hp_get,
1606 .put = indep_hp_put,
1607};
1608
1609
1610static int create_indep_hp_ctls(struct hda_codec *codec)
1611{
1612 struct hda_gen_spec *spec = codec->spec;
1613
1614 if (!spec->indep_hp)
1615 return 0;
1616 if (!spec->multiout.hp_out_nid[0]) {
1617 spec->indep_hp = 0;
1618 return 0;
1619 }
1620
1621 spec->indep_hp_enabled = false;
1622 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1623 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1624 return -ENOMEM;
1625 return 0;
1626}
1627
1628/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001629 * channel mode enum control
1630 */
1631
1632static int ch_mode_info(struct snd_kcontrol *kcontrol,
1633 struct snd_ctl_elem_info *uinfo)
1634{
1635 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1636 struct hda_gen_spec *spec = codec->spec;
1637
1638 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1639 uinfo->count = 1;
1640 uinfo->value.enumerated.items = spec->multi_ios + 1;
1641 if (uinfo->value.enumerated.item > spec->multi_ios)
1642 uinfo->value.enumerated.item = spec->multi_ios;
1643 sprintf(uinfo->value.enumerated.name, "%dch",
1644 (uinfo->value.enumerated.item + 1) * 2);
1645 return 0;
1646}
1647
1648static int ch_mode_get(struct snd_kcontrol *kcontrol,
1649 struct snd_ctl_elem_value *ucontrol)
1650{
1651 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1652 struct hda_gen_spec *spec = codec->spec;
1653 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1654 return 0;
1655}
1656
Takashi Iwai196c17662013-01-04 15:01:40 +01001657static inline struct nid_path *
1658get_multiio_path(struct hda_codec *codec, int idx)
1659{
1660 struct hda_gen_spec *spec = codec->spec;
1661 return snd_hda_get_path_from_idx(codec,
1662 spec->out_paths[spec->autocfg.line_outs + idx]);
1663}
1664
Takashi Iwai352f7f92012-12-19 12:52:06 +01001665static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1666{
1667 struct hda_gen_spec *spec = codec->spec;
1668 hda_nid_t nid = spec->multi_io[idx].pin;
1669 struct nid_path *path;
1670
Takashi Iwai196c17662013-01-04 15:01:40 +01001671 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001672 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001674
1675 if (path->active == output)
1676 return 0;
1677
1678 if (output) {
1679 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1680 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001681 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001682 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001683 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001684 snd_hda_activate_path(codec, path, false, true);
1685 snd_hda_set_pin_ctl_cache(codec, nid,
1686 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Takashi Iwai352f7f92012-12-19 12:52:06 +01001691static int ch_mode_put(struct snd_kcontrol *kcontrol,
1692 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001694 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1695 struct hda_gen_spec *spec = codec->spec;
1696 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Takashi Iwai352f7f92012-12-19 12:52:06 +01001698 ch = ucontrol->value.enumerated.item[0];
1699 if (ch < 0 || ch > spec->multi_ios)
1700 return -EINVAL;
1701 if (ch == (spec->ext_channel_count - 1) / 2)
1702 return 0;
1703 spec->ext_channel_count = (ch + 1) * 2;
1704 for (i = 0; i < spec->multi_ios; i++)
1705 set_multi_io(codec, i, i < ch);
1706 spec->multiout.max_channels = max(spec->ext_channel_count,
1707 spec->const_channel_count);
1708 if (spec->need_dac_fix)
1709 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return 1;
1711}
1712
Takashi Iwai352f7f92012-12-19 12:52:06 +01001713static const struct snd_kcontrol_new channel_mode_enum = {
1714 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1715 .name = "Channel Mode",
1716 .info = ch_mode_info,
1717 .get = ch_mode_get,
1718 .put = ch_mode_put,
1719};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Takashi Iwai352f7f92012-12-19 12:52:06 +01001721static int create_multi_channel_mode(struct hda_codec *codec)
1722{
1723 struct hda_gen_spec *spec = codec->spec;
1724
1725 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001726 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001727 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return 0;
1730}
1731
Takashi Iwai352f7f92012-12-19 12:52:06 +01001732/*
1733 * shared headphone/mic handling
1734 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001735
Takashi Iwai352f7f92012-12-19 12:52:06 +01001736static void call_update_outputs(struct hda_codec *codec);
1737
1738/* for shared I/O, change the pin-control accordingly */
1739static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1740{
1741 struct hda_gen_spec *spec = codec->spec;
1742 unsigned int val;
1743 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1744 /* NOTE: this assumes that there are only two inputs, the
1745 * first is the real internal mic and the second is HP/mic jack.
1746 */
1747
1748 val = snd_hda_get_default_vref(codec, pin);
1749
1750 /* This pin does not have vref caps - let's enable vref on pin 0x18
1751 instead, as suggested by Realtek */
1752 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1753 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1754 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1755 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001756 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1757 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001758 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001759
1760 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001761 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001762
1763 spec->automute_speaker = !set_as_mic;
1764 call_update_outputs(codec);
1765}
1766
1767/* create a shared input with the headphone out */
1768static int create_shared_input(struct hda_codec *codec)
1769{
1770 struct hda_gen_spec *spec = codec->spec;
1771 struct auto_pin_cfg *cfg = &spec->autocfg;
1772 unsigned int defcfg;
1773 hda_nid_t nid;
1774
1775 /* only one internal input pin? */
1776 if (cfg->num_inputs != 1)
1777 return 0;
1778 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1779 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1780 return 0;
1781
1782 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1783 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1784 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1785 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1786 else
1787 return 0; /* both not available */
1788
1789 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1790 return 0; /* no input */
1791
1792 cfg->inputs[1].pin = nid;
1793 cfg->inputs[1].type = AUTO_PIN_MIC;
1794 cfg->num_inputs = 2;
1795 spec->shared_mic_hp = 1;
1796 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1797 return 0;
1798}
1799
1800
1801/*
1802 * Parse input paths
1803 */
1804
1805#ifdef CONFIG_PM
1806/* add the powersave loopback-list entry */
1807static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1808{
1809 struct hda_amp_list *list;
1810
1811 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1812 return;
1813 list = spec->loopback_list + spec->num_loopbacks;
1814 list->nid = mix;
1815 list->dir = HDA_INPUT;
1816 list->idx = idx;
1817 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001818 spec->loopback.amplist = spec->loopback_list;
1819}
1820#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001821#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001822#endif
1823
Takashi Iwai352f7f92012-12-19 12:52:06 +01001824/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01001825static int new_analog_input(struct hda_codec *codec, int input_idx,
1826 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001827 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001829 struct hda_gen_spec *spec = codec->spec;
1830 struct nid_path *path;
1831 unsigned int val;
1832 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Takashi Iwai352f7f92012-12-19 12:52:06 +01001834 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1835 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1836 return 0; /* no need for analog loopback */
1837
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001838 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001839 if (!path)
1840 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001841 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01001842 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001843
1844 idx = path->idx[path->depth - 1];
1845 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1846 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1847 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001848 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001850 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852
Takashi Iwai352f7f92012-12-19 12:52:06 +01001853 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1854 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1855 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001856 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001858 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860
Takashi Iwai352f7f92012-12-19 12:52:06 +01001861 path->active = true;
1862 add_loopback_list(spec, mix_nid, idx);
1863 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
Takashi Iwai352f7f92012-12-19 12:52:06 +01001866static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001868 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1869 return (pincap & AC_PINCAP_IN) != 0;
1870}
1871
1872/* Parse the codec tree and retrieve ADCs */
1873static int fill_adc_nids(struct hda_codec *codec)
1874{
1875 struct hda_gen_spec *spec = codec->spec;
1876 hda_nid_t nid;
1877 hda_nid_t *adc_nids = spec->adc_nids;
1878 int max_nums = ARRAY_SIZE(spec->adc_nids);
1879 int i, nums = 0;
1880
1881 nid = codec->start_nid;
1882 for (i = 0; i < codec->num_nodes; i++, nid++) {
1883 unsigned int caps = get_wcaps(codec, nid);
1884 int type = get_wcaps_type(caps);
1885
1886 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1887 continue;
1888 adc_nids[nums] = nid;
1889 if (++nums >= max_nums)
1890 break;
1891 }
1892 spec->num_adc_nids = nums;
1893 return nums;
1894}
1895
1896/* filter out invalid adc_nids that don't give all active input pins;
1897 * if needed, check whether dynamic ADC-switching is available
1898 */
1899static int check_dyn_adc_switch(struct hda_codec *codec)
1900{
1901 struct hda_gen_spec *spec = codec->spec;
1902 struct hda_input_mux *imux = &spec->input_mux;
1903 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1904 int i, n, nums;
1905 hda_nid_t pin, adc;
1906
1907 again:
1908 nums = 0;
1909 for (n = 0; n < spec->num_adc_nids; n++) {
1910 adc = spec->adc_nids[n];
1911 for (i = 0; i < imux->num_items; i++) {
1912 pin = spec->imux_pins[i];
1913 if (!is_reachable_path(codec, pin, adc))
1914 break;
1915 }
1916 if (i >= imux->num_items)
1917 adc_nids[nums++] = adc;
1918 }
1919
1920 if (!nums) {
1921 if (spec->shared_mic_hp) {
1922 spec->shared_mic_hp = 0;
1923 imux->num_items = 1;
1924 goto again;
1925 }
1926
1927 /* check whether ADC-switch is possible */
1928 for (i = 0; i < imux->num_items; i++) {
1929 pin = spec->imux_pins[i];
1930 for (n = 0; n < spec->num_adc_nids; n++) {
1931 adc = spec->adc_nids[n];
1932 if (is_reachable_path(codec, pin, adc)) {
1933 spec->dyn_adc_idx[i] = n;
1934 break;
1935 }
1936 }
1937 }
1938
1939 snd_printdd("hda-codec: enabling ADC switching\n");
1940 spec->dyn_adc_switch = 1;
1941 } else if (nums != spec->num_adc_nids) {
1942 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1943 spec->num_adc_nids = nums;
1944 }
1945
1946 if (imux->num_items == 1 || spec->shared_mic_hp) {
1947 snd_printdd("hda-codec: reducing to a single ADC\n");
1948 spec->num_adc_nids = 1; /* reduce to a single ADC */
1949 }
1950
1951 /* single index for individual volumes ctls */
1952 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1953 spec->num_adc_nids = 1;
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return 0;
1956}
1957
1958/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001959 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001961static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001962{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001963 struct hda_gen_spec *spec = codec->spec;
1964 const struct auto_pin_cfg *cfg = &spec->autocfg;
1965 hda_nid_t mixer = spec->mixer_nid;
1966 struct hda_input_mux *imux = &spec->input_mux;
1967 int num_adcs;
1968 int i, c, err, type_idx = 0;
1969 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001970
Takashi Iwai352f7f92012-12-19 12:52:06 +01001971 num_adcs = fill_adc_nids(codec);
1972 if (num_adcs < 0)
1973 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001974
Takashi Iwai352f7f92012-12-19 12:52:06 +01001975 for (i = 0; i < cfg->num_inputs; i++) {
1976 hda_nid_t pin;
1977 const char *label;
1978 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Takashi Iwai352f7f92012-12-19 12:52:06 +01001980 pin = cfg->inputs[i].pin;
1981 if (!is_input_pin(codec, pin))
1982 continue;
1983
1984 label = hda_get_autocfg_input_label(codec, cfg, i);
1985 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1986 label = "Headphone Mic";
1987 if (prev_label && !strcmp(label, prev_label))
1988 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001989 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001990 type_idx = 0;
1991 prev_label = label;
1992
1993 if (mixer) {
1994 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01001995 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001996 label, type_idx, mixer);
1997 if (err < 0)
1998 return err;
1999 }
2000 }
2001
2002 imux_added = false;
2003 for (c = 0; c < num_adcs; c++) {
2004 struct nid_path *path;
2005 hda_nid_t adc = spec->adc_nids[c];
2006
2007 if (!is_reachable_path(codec, pin, adc))
2008 continue;
2009 path = snd_array_new(&spec->paths);
2010 if (!path)
2011 return -ENOMEM;
2012 memset(path, 0, sizeof(*path));
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002013 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002014 snd_printd(KERN_ERR
2015 "invalid input path 0x%x -> 0x%x\n",
2016 pin, adc);
2017 spec->paths.used--;
2018 continue;
2019 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002020 print_nid_path("input", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002021
2022 if (!imux_added) {
2023 spec->imux_pins[imux->num_items] = pin;
2024 snd_hda_add_imux_item(imux, label,
2025 imux->num_items, NULL);
2026 imux_added = true;
2027 }
2028 }
2029 }
2030
2031 return 0;
2032}
2033
2034
2035/*
2036 * input source mux
2037 */
2038
2039/* get the ADC NID corresponding to the given index */
2040static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
2041{
2042 struct hda_gen_spec *spec = codec->spec;
2043 if (spec->dyn_adc_switch)
2044 adc_idx = spec->dyn_adc_idx[imux_idx];
2045 return spec->adc_nids[adc_idx];
2046}
2047
2048static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2049 unsigned int idx);
2050
2051static int mux_enum_info(struct snd_kcontrol *kcontrol,
2052 struct snd_ctl_elem_info *uinfo)
2053{
2054 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2055 struct hda_gen_spec *spec = codec->spec;
2056 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2057}
2058
2059static int mux_enum_get(struct snd_kcontrol *kcontrol,
2060 struct snd_ctl_elem_value *ucontrol)
2061{
2062 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2063 struct hda_gen_spec *spec = codec->spec;
2064 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2065
2066 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2067 return 0;
2068}
2069
2070static int mux_enum_put(struct snd_kcontrol *kcontrol,
2071 struct snd_ctl_elem_value *ucontrol)
2072{
2073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2074 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2075 return mux_select(codec, adc_idx,
2076 ucontrol->value.enumerated.item[0]);
2077}
2078
Takashi Iwai352f7f92012-12-19 12:52:06 +01002079static const struct snd_kcontrol_new cap_src_temp = {
2080 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2081 .name = "Input Source",
2082 .info = mux_enum_info,
2083 .get = mux_enum_get,
2084 .put = mux_enum_put,
2085};
2086
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002087/*
2088 * capture volume and capture switch ctls
2089 */
2090
Takashi Iwai352f7f92012-12-19 12:52:06 +01002091typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2092 struct snd_ctl_elem_value *ucontrol);
2093
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002094/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002095static int cap_put_caller(struct snd_kcontrol *kcontrol,
2096 struct snd_ctl_elem_value *ucontrol,
2097 put_call_t func, int type)
2098{
2099 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2100 struct hda_gen_spec *spec = codec->spec;
2101 const struct hda_input_mux *imux;
2102 struct nid_path *path;
2103 int i, adc_idx, err = 0;
2104
2105 imux = &spec->input_mux;
2106 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2107 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002108 /* we use the cache-only update at first since multiple input paths
2109 * may shared the same amp; by updating only caches, the redundant
2110 * writes to hardware can be reduced.
2111 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002112 codec->cached_write = 1;
2113 for (i = 0; i < imux->num_items; i++) {
2114 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2115 get_adc_nid(codec, adc_idx, i));
2116 if (!path->ctls[type])
2117 continue;
2118 kcontrol->private_value = path->ctls[type];
2119 err = func(kcontrol, ucontrol);
2120 if (err < 0)
2121 goto error;
2122 }
2123 error:
2124 codec->cached_write = 0;
2125 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002126 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002127 if (err >= 0 && spec->cap_sync_hook)
2128 spec->cap_sync_hook(codec);
2129 return err;
2130}
2131
2132/* capture volume ctl callbacks */
2133#define cap_vol_info snd_hda_mixer_amp_volume_info
2134#define cap_vol_get snd_hda_mixer_amp_volume_get
2135#define cap_vol_tlv snd_hda_mixer_amp_tlv
2136
2137static int cap_vol_put(struct snd_kcontrol *kcontrol,
2138 struct snd_ctl_elem_value *ucontrol)
2139{
2140 return cap_put_caller(kcontrol, ucontrol,
2141 snd_hda_mixer_amp_volume_put,
2142 NID_PATH_VOL_CTL);
2143}
2144
2145static const struct snd_kcontrol_new cap_vol_temp = {
2146 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2147 .name = "Capture Volume",
2148 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2149 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2150 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2151 .info = cap_vol_info,
2152 .get = cap_vol_get,
2153 .put = cap_vol_put,
2154 .tlv = { .c = cap_vol_tlv },
2155};
2156
2157/* capture switch ctl callbacks */
2158#define cap_sw_info snd_ctl_boolean_stereo_info
2159#define cap_sw_get snd_hda_mixer_amp_switch_get
2160
2161static int cap_sw_put(struct snd_kcontrol *kcontrol,
2162 struct snd_ctl_elem_value *ucontrol)
2163{
2164 return cap_put_caller(kcontrol, ucontrol,
2165 snd_hda_mixer_amp_switch_put,
2166 NID_PATH_MUTE_CTL);
2167}
2168
2169static const struct snd_kcontrol_new cap_sw_temp = {
2170 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2171 .name = "Capture Switch",
2172 .info = cap_sw_info,
2173 .get = cap_sw_get,
2174 .put = cap_sw_put,
2175};
2176
2177static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2178{
2179 hda_nid_t nid;
2180 int i, depth;
2181
2182 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2183 for (depth = 0; depth < 3; depth++) {
2184 if (depth >= path->depth)
2185 return -EINVAL;
2186 i = path->depth - depth - 1;
2187 nid = path->path[i];
2188 if (!path->ctls[NID_PATH_VOL_CTL]) {
2189 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2190 path->ctls[NID_PATH_VOL_CTL] =
2191 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2192 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2193 int idx = path->idx[i];
2194 if (!depth && codec->single_adc_amp)
2195 idx = 0;
2196 path->ctls[NID_PATH_VOL_CTL] =
2197 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2198 }
2199 }
2200 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2201 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2202 path->ctls[NID_PATH_MUTE_CTL] =
2203 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2204 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2205 int idx = path->idx[i];
2206 if (!depth && codec->single_adc_amp)
2207 idx = 0;
2208 path->ctls[NID_PATH_MUTE_CTL] =
2209 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2210 }
2211 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return 0;
2214}
2215
Takashi Iwai352f7f92012-12-19 12:52:06 +01002216static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002218 struct hda_gen_spec *spec = codec->spec;
2219 struct auto_pin_cfg *cfg = &spec->autocfg;
2220 unsigned int val;
2221 int i;
2222
2223 if (!spec->inv_dmic_split)
2224 return false;
2225 for (i = 0; i < cfg->num_inputs; i++) {
2226 if (cfg->inputs[i].pin != nid)
2227 continue;
2228 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2229 return false;
2230 val = snd_hda_codec_get_pincfg(codec, nid);
2231 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2232 }
2233 return false;
2234}
2235
2236static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2237 int idx, bool is_switch, unsigned int ctl,
2238 bool inv_dmic)
2239{
2240 struct hda_gen_spec *spec = codec->spec;
2241 char tmpname[44];
2242 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2243 const char *sfx = is_switch ? "Switch" : "Volume";
2244 unsigned int chs = inv_dmic ? 1 : 3;
2245 int err;
2246
2247 if (!ctl)
2248 return 0;
2249
2250 if (label)
2251 snprintf(tmpname, sizeof(tmpname),
2252 "%s Capture %s", label, sfx);
2253 else
2254 snprintf(tmpname, sizeof(tmpname),
2255 "Capture %s", sfx);
2256 err = add_control(spec, type, tmpname, idx,
2257 amp_val_replace_channels(ctl, chs));
2258 if (err < 0 || !inv_dmic)
2259 return err;
2260
2261 /* Make independent right kcontrol */
2262 if (label)
2263 snprintf(tmpname, sizeof(tmpname),
2264 "Inverted %s Capture %s", label, sfx);
2265 else
2266 snprintf(tmpname, sizeof(tmpname),
2267 "Inverted Capture %s", sfx);
2268 return add_control(spec, type, tmpname, idx,
2269 amp_val_replace_channels(ctl, 2));
2270}
2271
2272/* create single (and simple) capture volume and switch controls */
2273static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2274 unsigned int vol_ctl, unsigned int sw_ctl,
2275 bool inv_dmic)
2276{
2277 int err;
2278 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2279 if (err < 0)
2280 return err;
2281 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2282 if (err < 0)
2283 return err;
2284 return 0;
2285}
2286
2287/* create bound capture volume and switch controls */
2288static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2289 unsigned int vol_ctl, unsigned int sw_ctl)
2290{
2291 struct hda_gen_spec *spec = codec->spec;
2292 struct snd_kcontrol_new *knew;
2293
2294 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002295 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002296 if (!knew)
2297 return -ENOMEM;
2298 knew->index = idx;
2299 knew->private_value = vol_ctl;
2300 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2301 }
2302 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002303 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002304 if (!knew)
2305 return -ENOMEM;
2306 knew->index = idx;
2307 knew->private_value = sw_ctl;
2308 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2309 }
2310 return 0;
2311}
2312
2313/* return the vol ctl when used first in the imux list */
2314static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2315{
2316 struct hda_gen_spec *spec = codec->spec;
2317 struct nid_path *path;
2318 unsigned int ctl;
2319 int i;
2320
2321 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2322 get_adc_nid(codec, 0, idx));
2323 if (!path)
2324 return 0;
2325 ctl = path->ctls[type];
2326 if (!ctl)
2327 return 0;
2328 for (i = 0; i < idx - 1; i++) {
2329 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2330 get_adc_nid(codec, 0, i));
2331 if (path && path->ctls[type] == ctl)
2332 return 0;
2333 }
2334 return ctl;
2335}
2336
2337/* create individual capture volume and switch controls per input */
2338static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2339{
2340 struct hda_gen_spec *spec = codec->spec;
2341 struct hda_input_mux *imux = &spec->input_mux;
2342 int i, err, type, type_idx = 0;
2343 const char *prev_label = NULL;
2344
2345 for (i = 0; i < imux->num_items; i++) {
2346 const char *label;
2347 bool inv_dmic;
2348 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2349 if (prev_label && !strcmp(label, prev_label))
2350 type_idx++;
2351 else
2352 type_idx = 0;
2353 prev_label = label;
2354 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2355
2356 for (type = 0; type < 2; type++) {
2357 err = add_single_cap_ctl(codec, label, type_idx, type,
2358 get_first_cap_ctl(codec, i, type),
2359 inv_dmic);
2360 if (err < 0)
2361 return err;
2362 }
2363 }
2364 return 0;
2365}
2366
2367static int create_capture_mixers(struct hda_codec *codec)
2368{
2369 struct hda_gen_spec *spec = codec->spec;
2370 struct hda_input_mux *imux = &spec->input_mux;
2371 int i, n, nums, err;
2372
2373 if (spec->dyn_adc_switch)
2374 nums = 1;
2375 else
2376 nums = spec->num_adc_nids;
2377
2378 if (!spec->auto_mic && imux->num_items > 1) {
2379 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002380 const char *name;
2381 name = nums > 1 ? "Input Source" : "Capture Source";
2382 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002383 if (!knew)
2384 return -ENOMEM;
2385 knew->count = nums;
2386 }
2387
2388 for (n = 0; n < nums; n++) {
2389 bool multi = false;
2390 bool inv_dmic = false;
2391 int vol, sw;
2392
2393 vol = sw = 0;
2394 for (i = 0; i < imux->num_items; i++) {
2395 struct nid_path *path;
2396 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2397 get_adc_nid(codec, n, i));
2398 if (!path)
2399 continue;
2400 parse_capvol_in_path(codec, path);
2401 if (!vol)
2402 vol = path->ctls[NID_PATH_VOL_CTL];
2403 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2404 multi = true;
2405 if (!sw)
2406 sw = path->ctls[NID_PATH_MUTE_CTL];
2407 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2408 multi = true;
2409 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2410 inv_dmic = true;
2411 }
2412
2413 if (!multi)
2414 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2415 inv_dmic);
2416 else if (!spec->multi_cap_vol)
2417 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2418 else
2419 err = create_multi_cap_vol_ctl(codec);
2420 if (err < 0)
2421 return err;
2422 }
2423
2424 return 0;
2425}
2426
2427/*
2428 * add mic boosts if needed
2429 */
2430static int parse_mic_boost(struct hda_codec *codec)
2431{
2432 struct hda_gen_spec *spec = codec->spec;
2433 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002434 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002435 int type_idx = 0;
2436 hda_nid_t nid;
2437 const char *prev_label = NULL;
2438
2439 for (i = 0; i < cfg->num_inputs; i++) {
2440 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2441 break;
2442 nid = cfg->inputs[i].pin;
2443 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2444 const char *label;
2445 char boost_label[32];
2446 struct nid_path *path;
2447 unsigned int val;
2448
2449 label = hda_get_autocfg_input_label(codec, cfg, i);
2450 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2451 label = "Headphone Mic";
2452 if (prev_label && !strcmp(label, prev_label))
2453 type_idx++;
2454 else
2455 type_idx = 0;
2456 prev_label = label;
2457
2458 snprintf(boost_label, sizeof(boost_label),
2459 "%s Boost Volume", label);
2460 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2461 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2462 boost_label, type_idx, val);
2463 if (err < 0)
2464 return err;
2465
2466 path = snd_hda_get_nid_path(codec, nid, 0);
2467 if (path)
2468 path->ctls[NID_PATH_BOOST_CTL] = val;
2469 }
2470 }
2471 return 0;
2472}
2473
2474/*
2475 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2476 */
2477static void parse_digital(struct hda_codec *codec)
2478{
2479 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002480 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002481 int i, nums;
2482 hda_nid_t dig_nid;
2483
2484 /* support multiple SPDIFs; the secondary is set up as a slave */
2485 nums = 0;
2486 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2487 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2488 dig_nid = look_for_dac(codec, pin, true);
2489 if (!dig_nid)
2490 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002491 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002492 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002493 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002494 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002495 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01002496 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002497 if (!nums) {
2498 spec->multiout.dig_out_nid = dig_nid;
2499 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2500 } else {
2501 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2502 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2503 break;
2504 spec->slave_dig_outs[nums - 1] = dig_nid;
2505 }
2506 nums++;
2507 }
2508
2509 if (spec->autocfg.dig_in_pin) {
2510 dig_nid = codec->start_nid;
2511 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002512 unsigned int wcaps = get_wcaps(codec, dig_nid);
2513 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2514 continue;
2515 if (!(wcaps & AC_WCAP_DIGITAL))
2516 continue;
2517 path = snd_hda_add_new_path(codec,
2518 spec->autocfg.dig_in_pin,
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002519 dig_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002520 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002521 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002522 path->active = true;
2523 spec->dig_in_nid = dig_nid;
2524 break;
2525 }
2526 }
2527 }
2528}
2529
2530
2531/*
2532 * input MUX handling
2533 */
2534
2535static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2536
2537/* select the given imux item; either unmute exclusively or select the route */
2538static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2539 unsigned int idx)
2540{
2541 struct hda_gen_spec *spec = codec->spec;
2542 const struct hda_input_mux *imux;
2543 struct nid_path *path;
2544
2545 imux = &spec->input_mux;
2546 if (!imux->num_items)
2547 return 0;
2548
2549 if (idx >= imux->num_items)
2550 idx = imux->num_items - 1;
2551 if (spec->cur_mux[adc_idx] == idx)
2552 return 0;
2553
2554 path = snd_hda_get_nid_path(codec,
2555 spec->imux_pins[spec->cur_mux[adc_idx]],
2556 spec->adc_nids[adc_idx]);
2557 if (!path)
2558 return 0;
2559 if (path->active)
2560 snd_hda_activate_path(codec, path, false, false);
2561
2562 spec->cur_mux[adc_idx] = idx;
2563
2564 if (spec->shared_mic_hp)
2565 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2566
2567 if (spec->dyn_adc_switch)
2568 dyn_adc_pcm_resetup(codec, idx);
2569
2570 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2571 get_adc_nid(codec, adc_idx, idx));
2572 if (!path)
2573 return 0;
2574 if (path->active)
2575 return 0;
2576 snd_hda_activate_path(codec, path, true, false);
2577 if (spec->cap_sync_hook)
2578 spec->cap_sync_hook(codec);
2579 return 1;
2580}
2581
2582
2583/*
2584 * Jack detections for HP auto-mute and mic-switch
2585 */
2586
2587/* check each pin in the given array; returns true if any of them is plugged */
2588static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2589{
2590 int i, present = 0;
2591
2592 for (i = 0; i < num_pins; i++) {
2593 hda_nid_t nid = pins[i];
2594 if (!nid)
2595 break;
2596 present |= snd_hda_jack_detect(codec, nid);
2597 }
2598 return present;
2599}
2600
2601/* standard HP/line-out auto-mute helper */
2602static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2603 bool mute, bool hp_out)
2604{
2605 struct hda_gen_spec *spec = codec->spec;
2606 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2607 int i;
2608
2609 for (i = 0; i < num_pins; i++) {
2610 hda_nid_t nid = pins[i];
2611 unsigned int val;
2612 if (!nid)
2613 break;
2614 /* don't reset VREF value in case it's controlling
2615 * the amp (see alc861_fixup_asus_amp_vref_0f())
2616 */
2617 if (spec->keep_vref_in_automute) {
2618 val = snd_hda_codec_read(codec, nid, 0,
2619 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2620 val &= ~PIN_HP;
2621 } else
2622 val = 0;
2623 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002624 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002625 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002626 }
2627}
2628
2629/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002630void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002631{
2632 struct hda_gen_spec *spec = codec->spec;
2633 int on;
2634
2635 /* Control HP pins/amps depending on master_mute state;
2636 * in general, HP pins/amps control should be enabled in all cases,
2637 * but currently set only for master_mute, just to be safe
2638 */
2639 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2640 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2641 spec->autocfg.hp_pins, spec->master_mute, true);
2642
2643 if (!spec->automute_speaker)
2644 on = 0;
2645 else
2646 on = spec->hp_jack_present | spec->line_jack_present;
2647 on |= spec->master_mute;
2648 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2649 spec->autocfg.speaker_pins, on, false);
2650
2651 /* toggle line-out mutes if needed, too */
2652 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2653 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2654 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2655 return;
2656 if (!spec->automute_lo)
2657 on = 0;
2658 else
2659 on = spec->hp_jack_present;
2660 on |= spec->master_mute;
2661 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2662 spec->autocfg.line_out_pins, on, false);
2663}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002664EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002665
2666static void call_update_outputs(struct hda_codec *codec)
2667{
2668 struct hda_gen_spec *spec = codec->spec;
2669 if (spec->automute_hook)
2670 spec->automute_hook(codec);
2671 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002672 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002673}
2674
2675/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002676void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002677{
2678 struct hda_gen_spec *spec = codec->spec;
2679
2680 spec->hp_jack_present =
2681 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2682 spec->autocfg.hp_pins);
2683 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2684 return;
2685 call_update_outputs(codec);
2686}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002687EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002688
2689/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002690void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002691{
2692 struct hda_gen_spec *spec = codec->spec;
2693
2694 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2695 return;
2696 /* check LO jack only when it's different from HP */
2697 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2698 return;
2699
2700 spec->line_jack_present =
2701 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2702 spec->autocfg.line_out_pins);
2703 if (!spec->automute_speaker || !spec->detect_lo)
2704 return;
2705 call_update_outputs(codec);
2706}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002707EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002708
2709/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002710void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002711{
2712 struct hda_gen_spec *spec = codec->spec;
2713 int i;
2714
2715 if (!spec->auto_mic)
2716 return;
2717
2718 for (i = spec->am_num_entries - 1; i > 0; i--) {
2719 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2720 mux_select(codec, 0, spec->am_entry[i].idx);
2721 return;
2722 }
2723 }
2724 mux_select(codec, 0, spec->am_entry[0].idx);
2725}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002726EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002727
2728/*
2729 * Auto-Mute mode mixer enum support
2730 */
2731static int automute_mode_info(struct snd_kcontrol *kcontrol,
2732 struct snd_ctl_elem_info *uinfo)
2733{
2734 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2735 struct hda_gen_spec *spec = codec->spec;
2736 static const char * const texts3[] = {
2737 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002738 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Takashi Iwai352f7f92012-12-19 12:52:06 +01002740 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2741 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2742 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2743}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Takashi Iwai352f7f92012-12-19 12:52:06 +01002745static int automute_mode_get(struct snd_kcontrol *kcontrol,
2746 struct snd_ctl_elem_value *ucontrol)
2747{
2748 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2749 struct hda_gen_spec *spec = codec->spec;
2750 unsigned int val = 0;
2751 if (spec->automute_speaker)
2752 val++;
2753 if (spec->automute_lo)
2754 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002755
Takashi Iwai352f7f92012-12-19 12:52:06 +01002756 ucontrol->value.enumerated.item[0] = val;
2757 return 0;
2758}
2759
2760static int automute_mode_put(struct snd_kcontrol *kcontrol,
2761 struct snd_ctl_elem_value *ucontrol)
2762{
2763 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2764 struct hda_gen_spec *spec = codec->spec;
2765
2766 switch (ucontrol->value.enumerated.item[0]) {
2767 case 0:
2768 if (!spec->automute_speaker && !spec->automute_lo)
2769 return 0;
2770 spec->automute_speaker = 0;
2771 spec->automute_lo = 0;
2772 break;
2773 case 1:
2774 if (spec->automute_speaker_possible) {
2775 if (!spec->automute_lo && spec->automute_speaker)
2776 return 0;
2777 spec->automute_speaker = 1;
2778 spec->automute_lo = 0;
2779 } else if (spec->automute_lo_possible) {
2780 if (spec->automute_lo)
2781 return 0;
2782 spec->automute_lo = 1;
2783 } else
2784 return -EINVAL;
2785 break;
2786 case 2:
2787 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2788 return -EINVAL;
2789 if (spec->automute_speaker && spec->automute_lo)
2790 return 0;
2791 spec->automute_speaker = 1;
2792 spec->automute_lo = 1;
2793 break;
2794 default:
2795 return -EINVAL;
2796 }
2797 call_update_outputs(codec);
2798 return 1;
2799}
2800
2801static const struct snd_kcontrol_new automute_mode_enum = {
2802 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2803 .name = "Auto-Mute Mode",
2804 .info = automute_mode_info,
2805 .get = automute_mode_get,
2806 .put = automute_mode_put,
2807};
2808
2809static int add_automute_mode_enum(struct hda_codec *codec)
2810{
2811 struct hda_gen_spec *spec = codec->spec;
2812
Takashi Iwai12c93df2012-12-19 14:38:33 +01002813 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002814 return -ENOMEM;
2815 return 0;
2816}
2817
2818/*
2819 * Check the availability of HP/line-out auto-mute;
2820 * Set up appropriately if really supported
2821 */
2822static int check_auto_mute_availability(struct hda_codec *codec)
2823{
2824 struct hda_gen_spec *spec = codec->spec;
2825 struct auto_pin_cfg *cfg = &spec->autocfg;
2826 int present = 0;
2827 int i, err;
2828
2829 if (cfg->hp_pins[0])
2830 present++;
2831 if (cfg->line_out_pins[0])
2832 present++;
2833 if (cfg->speaker_pins[0])
2834 present++;
2835 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002836 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002837
2838 if (!cfg->speaker_pins[0] &&
2839 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2840 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2841 sizeof(cfg->speaker_pins));
2842 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Takashi Iwai352f7f92012-12-19 12:52:06 +01002845 if (!cfg->hp_pins[0] &&
2846 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2847 memcpy(cfg->hp_pins, cfg->line_out_pins,
2848 sizeof(cfg->hp_pins));
2849 cfg->hp_outs = cfg->line_outs;
2850 }
2851
2852 for (i = 0; i < cfg->hp_outs; i++) {
2853 hda_nid_t nid = cfg->hp_pins[i];
2854 if (!is_jack_detectable(codec, nid))
2855 continue;
2856 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2857 nid);
2858 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002859 spec->hp_automute_hook ?
2860 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002861 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002862 spec->detect_hp = 1;
2863 }
2864
2865 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2866 if (cfg->speaker_outs)
2867 for (i = 0; i < cfg->line_outs; i++) {
2868 hda_nid_t nid = cfg->line_out_pins[i];
2869 if (!is_jack_detectable(codec, nid))
2870 continue;
2871 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2872 snd_hda_jack_detect_enable_callback(codec, nid,
2873 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002874 spec->line_automute_hook ?
2875 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002876 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002877 spec->detect_lo = 1;
2878 }
2879 spec->automute_lo_possible = spec->detect_hp;
2880 }
2881
2882 spec->automute_speaker_possible = cfg->speaker_outs &&
2883 (spec->detect_hp || spec->detect_lo);
2884
2885 spec->automute_lo = spec->automute_lo_possible;
2886 spec->automute_speaker = spec->automute_speaker_possible;
2887
2888 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2889 /* create a control for automute mode */
2890 err = add_automute_mode_enum(codec);
2891 if (err < 0)
2892 return err;
2893 }
2894 return 0;
2895}
2896
2897/* return the position of NID in the list, or -1 if not found */
2898static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2899{
2900 int i;
2901 for (i = 0; i < nums; i++)
2902 if (list[i] == nid)
2903 return i;
2904 return -1;
2905}
2906
2907/* check whether all auto-mic pins are valid; setup indices if OK */
2908static bool auto_mic_check_imux(struct hda_codec *codec)
2909{
2910 struct hda_gen_spec *spec = codec->spec;
2911 const struct hda_input_mux *imux;
2912 int i;
2913
2914 imux = &spec->input_mux;
2915 for (i = 0; i < spec->am_num_entries; i++) {
2916 spec->am_entry[i].idx =
2917 find_idx_in_nid_list(spec->am_entry[i].pin,
2918 spec->imux_pins, imux->num_items);
2919 if (spec->am_entry[i].idx < 0)
2920 return false; /* no corresponding imux */
2921 }
2922
2923 /* we don't need the jack detection for the first pin */
2924 for (i = 1; i < spec->am_num_entries; i++)
2925 snd_hda_jack_detect_enable_callback(codec,
2926 spec->am_entry[i].pin,
2927 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002928 spec->mic_autoswitch_hook ?
2929 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002930 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002931 return true;
2932}
2933
2934static int compare_attr(const void *ap, const void *bp)
2935{
2936 const struct automic_entry *a = ap;
2937 const struct automic_entry *b = bp;
2938 return (int)(a->attr - b->attr);
2939}
2940
2941/*
2942 * Check the availability of auto-mic switch;
2943 * Set up if really supported
2944 */
2945static int check_auto_mic_availability(struct hda_codec *codec)
2946{
2947 struct hda_gen_spec *spec = codec->spec;
2948 struct auto_pin_cfg *cfg = &spec->autocfg;
2949 unsigned int types;
2950 int i, num_pins;
2951
2952 types = 0;
2953 num_pins = 0;
2954 for (i = 0; i < cfg->num_inputs; i++) {
2955 hda_nid_t nid = cfg->inputs[i].pin;
2956 unsigned int attr;
2957 attr = snd_hda_codec_get_pincfg(codec, nid);
2958 attr = snd_hda_get_input_pin_attr(attr);
2959 if (types & (1 << attr))
2960 return 0; /* already occupied */
2961 switch (attr) {
2962 case INPUT_PIN_ATTR_INT:
2963 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2964 return 0; /* invalid type */
2965 break;
2966 case INPUT_PIN_ATTR_UNUSED:
2967 return 0; /* invalid entry */
2968 default:
2969 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2970 return 0; /* invalid type */
2971 if (!spec->line_in_auto_switch &&
2972 cfg->inputs[i].type != AUTO_PIN_MIC)
2973 return 0; /* only mic is allowed */
2974 if (!is_jack_detectable(codec, nid))
2975 return 0; /* no unsol support */
2976 break;
2977 }
2978 if (num_pins >= MAX_AUTO_MIC_PINS)
2979 return 0;
2980 types |= (1 << attr);
2981 spec->am_entry[num_pins].pin = nid;
2982 spec->am_entry[num_pins].attr = attr;
2983 num_pins++;
2984 }
2985
2986 if (num_pins < 2)
2987 return 0;
2988
2989 spec->am_num_entries = num_pins;
2990 /* sort the am_entry in the order of attr so that the pin with a
2991 * higher attr will be selected when the jack is plugged.
2992 */
2993 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2994 compare_attr, NULL);
2995
2996 if (!auto_mic_check_imux(codec))
2997 return 0;
2998
2999 spec->auto_mic = 1;
3000 spec->num_adc_nids = 1;
3001 spec->cur_mux[0] = spec->am_entry[0].idx;
3002 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3003 spec->am_entry[0].pin,
3004 spec->am_entry[1].pin,
3005 spec->am_entry[2].pin);
3006
3007 return 0;
3008}
3009
3010
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003011/*
3012 * Parse the given BIOS configuration and set up the hda_gen_spec
3013 *
3014 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003015 * or a negative error code
3016 */
3017int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003018 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003019{
3020 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003021 int err;
3022
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003023 if (cfg != &spec->autocfg) {
3024 spec->autocfg = *cfg;
3025 cfg = &spec->autocfg;
3026 }
3027
Takashi Iwai352f7f92012-12-19 12:52:06 +01003028 if (!cfg->line_outs) {
3029 if (cfg->dig_outs || cfg->dig_in_pin) {
3030 spec->multiout.max_channels = 2;
3031 spec->no_analog = 1;
3032 goto dig_only;
3033 }
3034 return 0; /* can't find valid BIOS pin config */
3035 }
3036
3037 if (!spec->no_primary_hp &&
3038 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3039 cfg->line_outs <= cfg->hp_outs) {
3040 /* use HP as primary out */
3041 cfg->speaker_outs = cfg->line_outs;
3042 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3043 sizeof(cfg->speaker_pins));
3044 cfg->line_outs = cfg->hp_outs;
3045 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3046 cfg->hp_outs = 0;
3047 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3048 cfg->line_out_type = AUTO_PIN_HP_OUT;
3049 }
3050
3051 err = parse_output_paths(codec);
3052 if (err < 0)
3053 return err;
3054 err = create_multi_channel_mode(codec);
3055 if (err < 0)
3056 return err;
3057 err = create_multi_out_ctls(codec, cfg);
3058 if (err < 0)
3059 return err;
3060 err = create_hp_out_ctls(codec);
3061 if (err < 0)
3062 return err;
3063 err = create_speaker_out_ctls(codec);
3064 if (err < 0)
3065 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003066 err = create_indep_hp_ctls(codec);
3067 if (err < 0)
3068 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003069 err = create_shared_input(codec);
3070 if (err < 0)
3071 return err;
3072 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003073 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003074 return err;
3075
Takashi Iwai352f7f92012-12-19 12:52:06 +01003076 /* check the multiple speaker pins */
3077 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
3078 spec->const_channel_count = cfg->line_outs * 2;
3079 else
3080 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003081
Takashi Iwai352f7f92012-12-19 12:52:06 +01003082 if (spec->multi_ios > 0)
3083 spec->multiout.max_channels = max(spec->ext_channel_count,
3084 spec->const_channel_count);
3085 else
3086 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3087
3088 err = check_auto_mute_availability(codec);
3089 if (err < 0)
3090 return err;
3091
3092 err = check_dyn_adc_switch(codec);
3093 if (err < 0)
3094 return err;
3095
3096 if (!spec->shared_mic_hp) {
3097 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003098 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003101
Takashi Iwai352f7f92012-12-19 12:52:06 +01003102 err = create_capture_mixers(codec);
3103 if (err < 0)
3104 return err;
3105
3106 err = parse_mic_boost(codec);
3107 if (err < 0)
3108 return err;
3109
3110 dig_only:
3111 parse_digital(codec);
3112
3113 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003115EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117
3118/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003119 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003121
3122/* slave controls for virtual master */
3123static const char * const slave_pfxs[] = {
3124 "Front", "Surround", "Center", "LFE", "Side",
3125 "Headphone", "Speaker", "Mono", "Line Out",
3126 "CLFE", "Bass Speaker", "PCM",
3127 NULL,
3128};
3129
3130int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003132 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Takashi Iwai36502d02012-12-19 15:15:10 +01003135 if (spec->kctls.used) {
3136 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3137 if (err < 0)
3138 return err;
3139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Takashi Iwai352f7f92012-12-19 12:52:06 +01003141 if (spec->multiout.dig_out_nid) {
3142 err = snd_hda_create_dig_out_ctls(codec,
3143 spec->multiout.dig_out_nid,
3144 spec->multiout.dig_out_nid,
3145 spec->pcm_rec[1].pcm_type);
3146 if (err < 0)
3147 return err;
3148 if (!spec->no_analog) {
3149 err = snd_hda_create_spdif_share_sw(codec,
3150 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 if (err < 0)
3152 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003153 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 }
3155 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003156 if (spec->dig_in_nid) {
3157 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3158 if (err < 0)
3159 return err;
3160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Takashi Iwai352f7f92012-12-19 12:52:06 +01003162 /* if we have no master control, let's create it */
3163 if (!spec->no_analog &&
3164 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3165 unsigned int vmaster_tlv[4];
3166 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3167 HDA_OUTPUT, vmaster_tlv);
3168 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3169 vmaster_tlv, slave_pfxs,
3170 "Playback Volume");
3171 if (err < 0)
3172 return err;
3173 }
3174 if (!spec->no_analog &&
3175 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3176 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3177 NULL, slave_pfxs,
3178 "Playback Switch",
3179 true, &spec->vmaster_mute.sw_kctl);
3180 if (err < 0)
3181 return err;
3182 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003183 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3184 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Takashi Iwai352f7f92012-12-19 12:52:06 +01003187 free_kctls(spec); /* no longer needed */
3188
3189 if (spec->shared_mic_hp) {
3190 int err;
3191 int nid = spec->autocfg.inputs[1].pin;
3192 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3193 if (err < 0)
3194 return err;
3195 err = snd_hda_jack_detect_enable(codec, nid, 0);
3196 if (err < 0)
3197 return err;
3198 }
3199
3200 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3201 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 return err;
3203
3204 return 0;
3205}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003206EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
3209/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003210 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Takashi Iwai352f7f92012-12-19 12:52:06 +01003213/*
3214 * Analog playback callbacks
3215 */
3216static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3217 struct hda_codec *codec,
3218 struct snd_pcm_substream *substream)
3219{
3220 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003221 int err;
3222
3223 mutex_lock(&spec->pcm_mutex);
3224 err = snd_hda_multi_out_analog_open(codec,
3225 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003226 hinfo);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003227 if (!err)
3228 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3229 mutex_unlock(&spec->pcm_mutex);
3230 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003231}
3232
3233static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003234 struct hda_codec *codec,
3235 unsigned int stream_tag,
3236 unsigned int format,
3237 struct snd_pcm_substream *substream)
3238{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003239 struct hda_gen_spec *spec = codec->spec;
3240 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3241 stream_tag, format, substream);
3242}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003243
Takashi Iwai352f7f92012-12-19 12:52:06 +01003244static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3245 struct hda_codec *codec,
3246 struct snd_pcm_substream *substream)
3247{
3248 struct hda_gen_spec *spec = codec->spec;
3249 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3250}
3251
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003252static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3253 struct hda_codec *codec,
3254 struct snd_pcm_substream *substream)
3255{
3256 struct hda_gen_spec *spec = codec->spec;
3257 mutex_lock(&spec->pcm_mutex);
3258 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3259 mutex_unlock(&spec->pcm_mutex);
3260 return 0;
3261}
3262
3263static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3264 struct hda_codec *codec,
3265 struct snd_pcm_substream *substream)
3266{
3267 struct hda_gen_spec *spec = codec->spec;
3268 int err = 0;
3269
3270 mutex_lock(&spec->pcm_mutex);
3271 if (!spec->indep_hp_enabled)
3272 err = -EBUSY;
3273 else
3274 spec->active_streams |= 1 << STREAM_INDEP_HP;
3275 mutex_unlock(&spec->pcm_mutex);
3276 return err;
3277}
3278
3279static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3280 struct hda_codec *codec,
3281 struct snd_pcm_substream *substream)
3282{
3283 struct hda_gen_spec *spec = codec->spec;
3284 mutex_lock(&spec->pcm_mutex);
3285 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3286 mutex_unlock(&spec->pcm_mutex);
3287 return 0;
3288}
3289
Takashi Iwai352f7f92012-12-19 12:52:06 +01003290/*
3291 * Digital out
3292 */
3293static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3294 struct hda_codec *codec,
3295 struct snd_pcm_substream *substream)
3296{
3297 struct hda_gen_spec *spec = codec->spec;
3298 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3299}
3300
3301static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3302 struct hda_codec *codec,
3303 unsigned int stream_tag,
3304 unsigned int format,
3305 struct snd_pcm_substream *substream)
3306{
3307 struct hda_gen_spec *spec = codec->spec;
3308 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3309 stream_tag, format, substream);
3310}
3311
3312static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3313 struct hda_codec *codec,
3314 struct snd_pcm_substream *substream)
3315{
3316 struct hda_gen_spec *spec = codec->spec;
3317 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3318}
3319
3320static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3321 struct hda_codec *codec,
3322 struct snd_pcm_substream *substream)
3323{
3324 struct hda_gen_spec *spec = codec->spec;
3325 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3326}
3327
3328/*
3329 * Analog capture
3330 */
3331static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3332 struct hda_codec *codec,
3333 unsigned int stream_tag,
3334 unsigned int format,
3335 struct snd_pcm_substream *substream)
3336{
3337 struct hda_gen_spec *spec = codec->spec;
3338
3339 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003340 stream_tag, 0, format);
3341 return 0;
3342}
3343
Takashi Iwai352f7f92012-12-19 12:52:06 +01003344static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3345 struct hda_codec *codec,
3346 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003347{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003348 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003349
Takashi Iwai352f7f92012-12-19 12:52:06 +01003350 snd_hda_codec_cleanup_stream(codec,
3351 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003352 return 0;
3353}
3354
Takashi Iwai352f7f92012-12-19 12:52:06 +01003355/*
3356 */
3357static const struct hda_pcm_stream pcm_analog_playback = {
3358 .substreams = 1,
3359 .channels_min = 2,
3360 .channels_max = 8,
3361 /* NID is set in build_pcms */
3362 .ops = {
3363 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003364 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003365 .prepare = playback_pcm_prepare,
3366 .cleanup = playback_pcm_cleanup
3367 },
3368};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Takashi Iwai352f7f92012-12-19 12:52:06 +01003370static const struct hda_pcm_stream pcm_analog_capture = {
3371 .substreams = 1,
3372 .channels_min = 2,
3373 .channels_max = 2,
3374 /* NID is set in build_pcms */
3375};
3376
3377static const struct hda_pcm_stream pcm_analog_alt_playback = {
3378 .substreams = 1,
3379 .channels_min = 2,
3380 .channels_max = 2,
3381 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003382 .ops = {
3383 .open = alt_playback_pcm_open,
3384 .close = alt_playback_pcm_close
3385 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003386};
3387
3388static const struct hda_pcm_stream pcm_analog_alt_capture = {
3389 .substreams = 2, /* can be overridden */
3390 .channels_min = 2,
3391 .channels_max = 2,
3392 /* NID is set in build_pcms */
3393 .ops = {
3394 .prepare = alt_capture_pcm_prepare,
3395 .cleanup = alt_capture_pcm_cleanup
3396 },
3397};
3398
3399static const struct hda_pcm_stream pcm_digital_playback = {
3400 .substreams = 1,
3401 .channels_min = 2,
3402 .channels_max = 2,
3403 /* NID is set in build_pcms */
3404 .ops = {
3405 .open = dig_playback_pcm_open,
3406 .close = dig_playback_pcm_close,
3407 .prepare = dig_playback_pcm_prepare,
3408 .cleanup = dig_playback_pcm_cleanup
3409 },
3410};
3411
3412static const struct hda_pcm_stream pcm_digital_capture = {
3413 .substreams = 1,
3414 .channels_min = 2,
3415 .channels_max = 2,
3416 /* NID is set in build_pcms */
3417};
3418
3419/* Used by build_pcms to flag that a PCM has no playback stream */
3420static const struct hda_pcm_stream pcm_null_stream = {
3421 .substreams = 0,
3422 .channels_min = 0,
3423 .channels_max = 0,
3424};
3425
3426/*
3427 * dynamic changing ADC PCM streams
3428 */
3429static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3430{
3431 struct hda_gen_spec *spec = codec->spec;
3432 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3433
3434 if (spec->cur_adc && spec->cur_adc != new_adc) {
3435 /* stream is running, let's swap the current ADC */
3436 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3437 spec->cur_adc = new_adc;
3438 snd_hda_codec_setup_stream(codec, new_adc,
3439 spec->cur_adc_stream_tag, 0,
3440 spec->cur_adc_format);
3441 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003443 return false;
3444}
3445
3446/* analog capture with dynamic dual-adc changes */
3447static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3448 struct hda_codec *codec,
3449 unsigned int stream_tag,
3450 unsigned int format,
3451 struct snd_pcm_substream *substream)
3452{
3453 struct hda_gen_spec *spec = codec->spec;
3454 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3455 spec->cur_adc_stream_tag = stream_tag;
3456 spec->cur_adc_format = format;
3457 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3458 return 0;
3459}
3460
3461static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3462 struct hda_codec *codec,
3463 struct snd_pcm_substream *substream)
3464{
3465 struct hda_gen_spec *spec = codec->spec;
3466 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3467 spec->cur_adc = 0;
3468 return 0;
3469}
3470
3471static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3472 .substreams = 1,
3473 .channels_min = 2,
3474 .channels_max = 2,
3475 .nid = 0, /* fill later */
3476 .ops = {
3477 .prepare = dyn_adc_capture_pcm_prepare,
3478 .cleanup = dyn_adc_capture_pcm_cleanup
3479 },
3480};
3481
Takashi Iwaif873e532012-12-20 16:58:39 +01003482static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3483 const char *chip_name)
3484{
3485 char *p;
3486
3487 if (*str)
3488 return;
3489 strlcpy(str, chip_name, len);
3490
3491 /* drop non-alnum chars after a space */
3492 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3493 if (!isalnum(p[1])) {
3494 *p = 0;
3495 break;
3496 }
3497 }
3498 strlcat(str, sfx, len);
3499}
3500
Takashi Iwai352f7f92012-12-19 12:52:06 +01003501/* build PCM streams based on the parsed results */
3502int snd_hda_gen_build_pcms(struct hda_codec *codec)
3503{
3504 struct hda_gen_spec *spec = codec->spec;
3505 struct hda_pcm *info = spec->pcm_rec;
3506 const struct hda_pcm_stream *p;
3507 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
3509 codec->num_pcms = 1;
3510 codec->pcm_info = info;
3511
Takashi Iwai352f7f92012-12-19 12:52:06 +01003512 if (spec->no_analog)
3513 goto skip_analog;
3514
Takashi Iwaif873e532012-12-20 16:58:39 +01003515 fill_pcm_stream_name(spec->stream_name_analog,
3516 sizeof(spec->stream_name_analog),
3517 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003518 info->name = spec->stream_name_analog;
3519
3520 if (spec->multiout.num_dacs > 0) {
3521 p = spec->stream_analog_playback;
3522 if (!p)
3523 p = &pcm_analog_playback;
3524 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3525 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3526 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3527 spec->multiout.max_channels;
3528 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3529 spec->autocfg.line_outs == 2)
3530 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3531 snd_pcm_2_1_chmaps;
3532 }
3533 if (spec->num_adc_nids) {
3534 p = spec->stream_analog_capture;
3535 if (!p) {
3536 if (spec->dyn_adc_switch)
3537 p = &dyn_adc_pcm_analog_capture;
3538 else
3539 p = &pcm_analog_capture;
3540 }
3541 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3542 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3543 }
3544
Takashi Iwai352f7f92012-12-19 12:52:06 +01003545 skip_analog:
3546 /* SPDIF for stream index #1 */
3547 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003548 fill_pcm_stream_name(spec->stream_name_digital,
3549 sizeof(spec->stream_name_digital),
3550 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003551 codec->num_pcms = 2;
3552 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3553 info = spec->pcm_rec + 1;
3554 info->name = spec->stream_name_digital;
3555 if (spec->dig_out_type)
3556 info->pcm_type = spec->dig_out_type;
3557 else
3558 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3559 if (spec->multiout.dig_out_nid) {
3560 p = spec->stream_digital_playback;
3561 if (!p)
3562 p = &pcm_digital_playback;
3563 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3564 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3565 }
3566 if (spec->dig_in_nid) {
3567 p = spec->stream_digital_capture;
3568 if (!p)
3569 p = &pcm_digital_capture;
3570 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3571 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3572 }
3573 }
3574
3575 if (spec->no_analog)
3576 return 0;
3577
3578 /* If the use of more than one ADC is requested for the current
3579 * model, configure a second analog capture-only PCM.
3580 */
3581 have_multi_adcs = (spec->num_adc_nids > 1) &&
3582 !spec->dyn_adc_switch && !spec->auto_mic;
3583 /* Additional Analaog capture for index #2 */
3584 if (spec->alt_dac_nid || have_multi_adcs) {
3585 codec->num_pcms = 3;
3586 info = spec->pcm_rec + 2;
3587 info->name = spec->stream_name_analog;
3588 if (spec->alt_dac_nid) {
3589 p = spec->stream_analog_alt_playback;
3590 if (!p)
3591 p = &pcm_analog_alt_playback;
3592 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3593 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3594 spec->alt_dac_nid;
3595 } else {
3596 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3597 pcm_null_stream;
3598 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3599 }
3600 if (have_multi_adcs) {
3601 p = spec->stream_analog_alt_capture;
3602 if (!p)
3603 p = &pcm_analog_alt_capture;
3604 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3605 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3606 spec->adc_nids[1];
3607 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3608 spec->num_adc_nids - 1;
3609 } else {
3610 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3611 pcm_null_stream;
3612 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 }
3615
3616 return 0;
3617}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003618EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3619
3620
3621/*
3622 * Standard auto-parser initializations
3623 */
3624
3625/* configure the path from the given dac to the pin as the proper output */
3626static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai196c17662013-01-04 15:01:40 +01003627 int pin_type, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003628{
3629 struct nid_path *path;
3630
3631 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
Takashi Iwai196c17662013-01-04 15:01:40 +01003632 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003633 if (!path)
3634 return;
Takashi Iwaie1284af2013-01-03 16:33:02 +01003635 snd_hda_activate_path(codec, path, path->active, true);
3636 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003637}
3638
3639/* initialize primary output paths */
3640static void init_multi_out(struct hda_codec *codec)
3641{
3642 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai196c17662013-01-04 15:01:40 +01003643 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003644 int pin_type;
3645 int i;
3646
3647 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3648 pin_type = PIN_HP;
3649 else
3650 pin_type = PIN_OUT;
3651
Takashi Iwai64049c82012-12-20 15:29:21 +01003652 for (i = 0; i < spec->autocfg.line_outs; i++) {
3653 nid = spec->autocfg.line_out_pins[i];
Takashi Iwai196c17662013-01-04 15:01:40 +01003654 if (nid)
3655 set_output_and_unmute(codec, nid, pin_type,
3656 spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003657 }
3658}
3659
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003660
3661static void __init_extra_out(struct hda_codec *codec, int num_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01003662 hda_nid_t *pins, int *paths, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003663{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003664 int i;
Takashi Iwai196c17662013-01-04 15:01:40 +01003665 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003666
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003667 for (i = 0; i < num_outs; i++) {
3668 pin = pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003669 if (!pin)
3670 break;
Takashi Iwai196c17662013-01-04 15:01:40 +01003671 set_output_and_unmute(codec, pin, type, paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003672 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003673}
3674
3675/* initialize hp and speaker paths */
3676static void init_extra_out(struct hda_codec *codec)
3677{
3678 struct hda_gen_spec *spec = codec->spec;
3679
3680 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3681 __init_extra_out(codec, spec->autocfg.hp_outs,
3682 spec->autocfg.hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01003683 spec->hp_paths, PIN_HP);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003684 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3685 __init_extra_out(codec, spec->autocfg.speaker_outs,
3686 spec->autocfg.speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01003687 spec->speaker_paths, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003688}
3689
3690/* initialize multi-io paths */
3691static void init_multi_io(struct hda_codec *codec)
3692{
3693 struct hda_gen_spec *spec = codec->spec;
3694 int i;
3695
3696 for (i = 0; i < spec->multi_ios; i++) {
3697 hda_nid_t pin = spec->multi_io[i].pin;
3698 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01003699 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003700 if (!path)
3701 continue;
3702 if (!spec->multi_io[i].ctl_in)
3703 spec->multi_io[i].ctl_in =
3704 snd_hda_codec_update_cache(codec, pin, 0,
3705 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3706 snd_hda_activate_path(codec, path, path->active, true);
3707 }
3708}
3709
3710/* set up the input pin config, depending on the given auto-pin type */
3711static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3712 int auto_pin_type)
3713{
3714 unsigned int val = PIN_IN;
3715 if (auto_pin_type == AUTO_PIN_MIC)
3716 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003717 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003718}
3719
3720/* set up input pins and loopback paths */
3721static void init_analog_input(struct hda_codec *codec)
3722{
3723 struct hda_gen_spec *spec = codec->spec;
3724 struct auto_pin_cfg *cfg = &spec->autocfg;
3725 int i;
3726
3727 for (i = 0; i < cfg->num_inputs; i++) {
3728 hda_nid_t nid = cfg->inputs[i].pin;
3729 if (is_input_pin(codec, nid))
3730 set_input_pin(codec, nid, cfg->inputs[i].type);
3731
3732 /* init loopback inputs */
3733 if (spec->mixer_nid) {
3734 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01003735 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003736 if (path)
3737 snd_hda_activate_path(codec, path,
3738 path->active, false);
3739 }
3740 }
3741}
3742
3743/* initialize ADC paths */
3744static void init_input_src(struct hda_codec *codec)
3745{
3746 struct hda_gen_spec *spec = codec->spec;
3747 struct hda_input_mux *imux = &spec->input_mux;
3748 struct nid_path *path;
3749 int i, c, nums;
3750
3751 if (spec->dyn_adc_switch)
3752 nums = 1;
3753 else
3754 nums = spec->num_adc_nids;
3755
3756 for (c = 0; c < nums; c++) {
3757 for (i = 0; i < imux->num_items; i++) {
3758 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3759 get_adc_nid(codec, c, i));
3760 if (path) {
3761 bool active = path->active;
3762 if (i == spec->cur_mux[c])
3763 active = true;
3764 snd_hda_activate_path(codec, path, active, false);
3765 }
3766 }
3767 }
3768
3769 if (spec->shared_mic_hp)
3770 update_shared_mic_hp(codec, spec->cur_mux[0]);
3771
3772 if (spec->cap_sync_hook)
3773 spec->cap_sync_hook(codec);
3774}
3775
3776/* set right pin controls for digital I/O */
3777static void init_digital(struct hda_codec *codec)
3778{
3779 struct hda_gen_spec *spec = codec->spec;
3780 int i;
3781 hda_nid_t pin;
3782
3783 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3784 pin = spec->autocfg.dig_out_pins[i];
3785 if (!pin)
3786 continue;
Takashi Iwai196c17662013-01-04 15:01:40 +01003787 set_output_and_unmute(codec, pin, PIN_OUT,
3788 spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003789 }
3790 pin = spec->autocfg.dig_in_pin;
3791 if (pin)
Takashi Iwai7594aa32012-12-20 15:38:40 +01003792 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003793}
3794
Takashi Iwai973e4972012-12-20 15:16:09 +01003795/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3796 * invalid unsol tags by some reason
3797 */
3798static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3799{
3800 int i;
3801
3802 for (i = 0; i < codec->init_pins.used; i++) {
3803 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3804 hda_nid_t nid = pin->nid;
3805 if (is_jack_detectable(codec, nid) &&
3806 !snd_hda_jack_tbl_get(codec, nid))
3807 snd_hda_codec_update_cache(codec, nid, 0,
3808 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3809 }
3810}
3811
Takashi Iwai352f7f92012-12-19 12:52:06 +01003812int snd_hda_gen_init(struct hda_codec *codec)
3813{
3814 struct hda_gen_spec *spec = codec->spec;
3815
3816 if (spec->init_hook)
3817 spec->init_hook(codec);
3818
3819 snd_hda_apply_verbs(codec);
3820
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003821 codec->cached_write = 1;
3822
Takashi Iwai352f7f92012-12-19 12:52:06 +01003823 init_multi_out(codec);
3824 init_extra_out(codec);
3825 init_multi_io(codec);
3826 init_analog_input(codec);
3827 init_input_src(codec);
3828 init_digital(codec);
3829
Takashi Iwai973e4972012-12-20 15:16:09 +01003830 clear_unsol_on_unused_pins(codec);
3831
Takashi Iwai352f7f92012-12-19 12:52:06 +01003832 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003833 snd_hda_gen_hp_automute(codec, NULL);
3834 snd_hda_gen_line_automute(codec, NULL);
3835 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003836
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003837 snd_hda_codec_flush_amp_cache(codec);
3838 snd_hda_codec_flush_cmd_cache(codec);
3839
Takashi Iwai352f7f92012-12-19 12:52:06 +01003840 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3841 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3842
3843 hda_call_check_power_status(codec, 0x01);
3844 return 0;
3845}
3846EXPORT_SYMBOL(snd_hda_gen_init);
3847
3848
3849/*
3850 * the generic codec support
3851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
Takashi Iwai83012a72012-08-24 18:38:08 +02003853#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003854static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3855{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003856 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003857 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3858}
3859#endif
3860
Takashi Iwai352f7f92012-12-19 12:52:06 +01003861static void generic_free(struct hda_codec *codec)
3862{
3863 snd_hda_gen_spec_free(codec->spec);
3864 kfree(codec->spec);
3865 codec->spec = NULL;
3866}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Takashi Iwai352f7f92012-12-19 12:52:06 +01003868static const struct hda_codec_ops generic_patch_ops = {
3869 .build_controls = snd_hda_gen_build_controls,
3870 .build_pcms = snd_hda_gen_build_pcms,
3871 .init = snd_hda_gen_init,
3872 .free = generic_free,
3873 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02003874#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003875 .check_power_status = generic_check_power_status,
3876#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877};
3878
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879int snd_hda_parse_generic_codec(struct hda_codec *codec)
3880{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003881 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 int err;
3883
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003884 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003885 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003887 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003890 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3891 if (err < 0)
3892 return err;
3893
3894 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003895 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 goto error;
3897
3898 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return 0;
3900
Takashi Iwai352f7f92012-12-19 12:52:06 +01003901error:
3902 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 return err;
3904}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003905EXPORT_SYMBOL(snd_hda_parse_generic_codec);