blob: f946714c7d3c18690488934493a21324d7cadf6b [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>
Takashi Iwai294765582013-01-17 09:52:11 +010029#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010031#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "hda_codec.h"
33#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010034#include "hda_auto_parser.h"
35#include "hda_jack.h"
36#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Takashi Iwai352f7f92012-12-19 12:52:06 +010039/* initialize hda_gen_spec struct */
40int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Takashi Iwai352f7f92012-12-19 12:52:06 +010042 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010043 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
Takashi Iwai352f7f92012-12-19 12:52:06 +010078void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
79{
80 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010082 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010083 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
Takashi Iwai352f7f92012-12-19 12:52:06 +010085EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010088 * store user hints
89 */
90static void parse_user_hints(struct hda_codec *codec)
91{
92 struct hda_gen_spec *spec = codec->spec;
93 int val;
94
95 val = snd_hda_get_bool_hint(codec, "jack_detect");
96 if (val >= 0)
97 codec->no_jack_detect = !val;
98 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
99 if (val >= 0)
100 codec->inv_jack_detect = !!val;
101 val = snd_hda_get_bool_hint(codec, "trigger_sense");
102 if (val >= 0)
103 codec->no_trigger_sense = !val;
104 val = snd_hda_get_bool_hint(codec, "inv_eapd");
105 if (val >= 0)
106 codec->inv_eapd = !!val;
107 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
108 if (val >= 0)
109 codec->pcm_format_first = !!val;
110 val = snd_hda_get_bool_hint(codec, "sticky_stream");
111 if (val >= 0)
112 codec->no_sticky_stream = !val;
113 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
114 if (val >= 0)
115 codec->spdif_status_reset = !!val;
116 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
117 if (val >= 0)
118 codec->pin_amp_workaround = !!val;
119 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
120 if (val >= 0)
121 codec->single_adc_amp = !!val;
122
Takashi Iwaif72706b2013-01-16 18:20:07 +0100123 val = snd_hda_get_bool_hint(codec, "auto_mute");
124 if (val >= 0)
125 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100126 val = snd_hda_get_bool_hint(codec, "auto_mic");
127 if (val >= 0)
128 spec->suppress_auto_mic = !val;
129 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
130 if (val >= 0)
131 spec->line_in_auto_switch = !!val;
132 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
133 if (val >= 0)
134 spec->need_dac_fix = !!val;
135 val = snd_hda_get_bool_hint(codec, "primary_hp");
136 if (val >= 0)
137 spec->no_primary_hp = !val;
138 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
139 if (val >= 0)
140 spec->multi_cap_vol = !!val;
141 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
142 if (val >= 0)
143 spec->inv_dmic_split = !!val;
144 val = snd_hda_get_bool_hint(codec, "indep_hp");
145 if (val >= 0)
146 spec->indep_hp = !!val;
147 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
148 if (val >= 0)
149 spec->add_stereo_mix_input = !!val;
150 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
151 if (val >= 0)
152 spec->add_out_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100153 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
154 if (val >= 0)
155 spec->add_in_jack_modes = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100156
157 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
158 spec->mixer_nid = val;
159}
160
161/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100162 * pin control value accesses
163 */
164
165#define update_pin_ctl(codec, pin, val) \
166 snd_hda_codec_update_cache(codec, pin, 0, \
167 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
168
169/* restore the pinctl based on the cached value */
170static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
171{
172 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
173}
174
175/* set the pinctl target value and write it if requested */
176static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
177 unsigned int val, bool do_write)
178{
179 if (!pin)
180 return;
181 val = snd_hda_correct_pin_ctl(codec, pin, val);
182 snd_hda_codec_set_pin_target(codec, pin, val);
183 if (do_write)
184 update_pin_ctl(codec, pin, val);
185}
186
187/* set pinctl target values for all given pins */
188static void set_pin_targets(struct hda_codec *codec, int num_pins,
189 hda_nid_t *pins, unsigned int val)
190{
191 int i;
192 for (i = 0; i < num_pins; i++)
193 set_pin_target(codec, pins[i], val, false);
194}
195
196/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100197 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100200/* return the position of NID in the list, or -1 if not found */
201static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
202{
203 int i;
204 for (i = 0; i < nums; i++)
205 if (list[i] == nid)
206 return i;
207 return -1;
208}
209
210/* return true if the given NID is contained in the path */
211static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
212{
213 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
214}
215
Takashi Iwaif5172a72013-01-04 13:19:55 +0100216static struct nid_path *get_nid_path(struct hda_codec *codec,
217 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100218 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100220 struct hda_gen_spec *spec = codec->spec;
221 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Takashi Iwai352f7f92012-12-19 12:52:06 +0100223 for (i = 0; i < spec->paths.used; i++) {
224 struct nid_path *path = snd_array_elem(&spec->paths, i);
225 if (path->depth <= 0)
226 continue;
227 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100228 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100229 if (!anchor_nid ||
230 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
231 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100232 return path;
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235 return NULL;
236}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100237
238/* get the path between the given NIDs;
239 * passing 0 to either @pin or @dac behaves as a wildcard
240 */
241struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
242 hda_nid_t from_nid, hda_nid_t to_nid)
243{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100244 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100245}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100246EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
247
Takashi Iwai196c17662013-01-04 15:01:40 +0100248/* get the index number corresponding to the path instance;
249 * the index starts from 1, for easier checking the invalid value
250 */
251int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
252{
253 struct hda_gen_spec *spec = codec->spec;
254 struct nid_path *array = spec->paths.list;
255 ssize_t idx;
256
257 if (!spec->paths.used)
258 return 0;
259 idx = path - array;
260 if (idx < 0 || idx >= spec->paths.used)
261 return 0;
262 return idx + 1;
263}
264
265/* get the path instance corresponding to the given index number */
266struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
267{
268 struct hda_gen_spec *spec = codec->spec;
269
270 if (idx <= 0 || idx > spec->paths.used)
271 return NULL;
272 return snd_array_elem(&spec->paths, idx - 1);
273}
274
Takashi Iwai352f7f92012-12-19 12:52:06 +0100275/* check whether the given DAC is already found in any existing paths */
276static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
277{
278 struct hda_gen_spec *spec = codec->spec;
279 int i;
280
281 for (i = 0; i < spec->paths.used; i++) {
282 struct nid_path *path = snd_array_elem(&spec->paths, i);
283 if (path->path[0] == nid)
284 return true;
285 }
286 return false;
287}
288
289/* check whether the given two widgets can be connected */
290static bool is_reachable_path(struct hda_codec *codec,
291 hda_nid_t from_nid, hda_nid_t to_nid)
292{
293 if (!from_nid || !to_nid)
294 return false;
295 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
296}
297
298/* nid, dir and idx */
299#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
300
301/* check whether the given ctl is already assigned in any path elements */
302static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
303{
304 struct hda_gen_spec *spec = codec->spec;
305 int i;
306
307 val &= AMP_VAL_COMPARE_MASK;
308 for (i = 0; i < spec->paths.used; i++) {
309 struct nid_path *path = snd_array_elem(&spec->paths, i);
310 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
311 return true;
312 }
313 return false;
314}
315
316/* check whether a control with the given (nid, dir, idx) was assigned */
317static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
318 int dir, int idx)
319{
320 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
321 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
322 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
323}
324
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100325static void print_nid_path(const char *pfx, struct nid_path *path)
326{
327 char buf[40];
328 int i;
329
330
331 buf[0] = 0;
332 for (i = 0; i < path->depth; i++) {
333 char tmp[4];
334 sprintf(tmp, ":%02x", path->path[i]);
335 strlcat(buf, tmp, sizeof(buf));
336 }
337 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
338}
339
Takashi Iwai352f7f92012-12-19 12:52:06 +0100340/* called recursively */
341static bool __parse_nid_path(struct hda_codec *codec,
342 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100343 int anchor_nid, struct nid_path *path,
344 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100345{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100346 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100347 int i, nums;
348
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100349 if (to_nid == anchor_nid)
350 anchor_nid = 0; /* anchor passed */
351 else if (to_nid == (hda_nid_t)(-anchor_nid))
352 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100353
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100354 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100355 for (i = 0; i < nums; i++) {
356 if (conn[i] != from_nid) {
357 /* special case: when from_nid is 0,
358 * try to find an empty DAC
359 */
360 if (from_nid ||
361 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
362 is_dac_already_used(codec, conn[i]))
363 continue;
364 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100365 /* anchor is not requested or already passed? */
366 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100367 goto found;
368 }
369 if (depth >= MAX_NID_PATH_DEPTH)
370 return false;
371 for (i = 0; i < nums; i++) {
372 unsigned int type;
373 type = get_wcaps_type(get_wcaps(codec, conn[i]));
374 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
375 type == AC_WID_PIN)
376 continue;
377 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100378 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100379 goto found;
380 }
381 return false;
382
383 found:
384 path->path[path->depth] = conn[i];
385 path->idx[path->depth + 1] = i;
386 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
387 path->multi[path->depth + 1] = 1;
388 path->depth++;
389 return true;
390}
391
392/* parse the widget path from the given nid to the target nid;
393 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100394 * when @anchor_nid is set to a positive value, only paths through the widget
395 * with the given value are evaluated.
396 * when @anchor_nid is set to a negative value, paths through the widget
397 * with the negative of given value are excluded, only other paths are chosen.
398 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100399 */
400bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100401 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100402 struct nid_path *path)
403{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100404 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100405 path->path[path->depth] = to_nid;
406 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100407 return true;
408 }
409 return false;
410}
411EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100414 * parse the path between the given NIDs and add to the path list.
415 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100417struct nid_path *
418snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100419 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100421 struct hda_gen_spec *spec = codec->spec;
422 struct nid_path *path;
423
424 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
425 return NULL;
426
Takashi Iwaif5172a72013-01-04 13:19:55 +0100427 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100428 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100429 if (path)
430 return path;
431
Takashi Iwai352f7f92012-12-19 12:52:06 +0100432 path = snd_array_new(&spec->paths);
433 if (!path)
434 return NULL;
435 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100436 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100437 return path;
438 /* push back */
439 spec->paths.used--;
440 return NULL;
441}
442EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
443
Takashi Iwai980428c2013-01-09 09:28:20 +0100444/* clear the given path as invalid so that it won't be picked up later */
445static void invalidate_nid_path(struct hda_codec *codec, int idx)
446{
447 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
448 if (!path)
449 return;
450 memset(path, 0, sizeof(*path));
451}
452
Takashi Iwai352f7f92012-12-19 12:52:06 +0100453/* look for an empty DAC slot */
454static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
455 bool is_digital)
456{
457 struct hda_gen_spec *spec = codec->spec;
458 bool cap_digital;
459 int i;
460
461 for (i = 0; i < spec->num_all_dacs; i++) {
462 hda_nid_t nid = spec->all_dacs[i];
463 if (!nid || is_dac_already_used(codec, nid))
464 continue;
465 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
466 if (is_digital != cap_digital)
467 continue;
468 if (is_reachable_path(codec, nid, pin))
469 return nid;
470 }
471 return 0;
472}
473
474/* replace the channels in the composed amp value with the given number */
475static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
476{
477 val &= ~(0x3U << 16);
478 val |= chs << 16;
479 return val;
480}
481
482/* check whether the widget has the given amp capability for the direction */
483static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
484 int dir, unsigned int bits)
485{
486 if (!nid)
487 return false;
488 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
489 if (query_amp_caps(codec, nid, dir) & bits)
490 return true;
491 return false;
492}
493
David Henningsson99a55922013-01-16 15:58:44 +0100494static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
495 hda_nid_t nid2, int dir)
496{
497 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
498 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
499 return (query_amp_caps(codec, nid1, dir) ==
500 query_amp_caps(codec, nid2, dir));
501}
502
Takashi Iwai352f7f92012-12-19 12:52:06 +0100503#define nid_has_mute(codec, nid, dir) \
504 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
505#define nid_has_volume(codec, nid, dir) \
506 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
507
508/* look for a widget suitable for assigning a mute switch in the path */
509static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
510 struct nid_path *path)
511{
512 int i;
513
514 for (i = path->depth - 1; i >= 0; i--) {
515 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
516 return path->path[i];
517 if (i != path->depth - 1 && i != 0 &&
518 nid_has_mute(codec, path->path[i], HDA_INPUT))
519 return path->path[i];
520 }
521 return 0;
522}
523
524/* look for a widget suitable for assigning a volume ctl in the path */
525static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
526 struct nid_path *path)
527{
528 int i;
529
530 for (i = path->depth - 1; i >= 0; i--) {
531 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
532 return path->path[i];
533 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100538 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100540
541/* can have the amp-in capability? */
542static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100544 hda_nid_t nid = path->path[idx];
545 unsigned int caps = get_wcaps(codec, nid);
546 unsigned int type = get_wcaps_type(caps);
547
548 if (!(caps & AC_WCAP_IN_AMP))
549 return false;
550 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
551 return false;
552 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Takashi Iwai352f7f92012-12-19 12:52:06 +0100555/* can have the amp-out capability? */
556static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100558 hda_nid_t nid = path->path[idx];
559 unsigned int caps = get_wcaps(codec, nid);
560 unsigned int type = get_wcaps_type(caps);
561
562 if (!(caps & AC_WCAP_OUT_AMP))
563 return false;
564 if (type == AC_WID_PIN && !idx) /* only for output pins */
565 return false;
566 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Takashi Iwai352f7f92012-12-19 12:52:06 +0100569/* check whether the given (nid,dir,idx) is active */
570static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
571 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100573 struct hda_gen_spec *spec = codec->spec;
574 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Takashi Iwai352f7f92012-12-19 12:52:06 +0100576 for (n = 0; n < spec->paths.used; n++) {
577 struct nid_path *path = snd_array_elem(&spec->paths, n);
578 if (!path->active)
579 continue;
580 for (i = 0; i < path->depth; i++) {
581 if (path->path[i] == nid) {
582 if (dir == HDA_OUTPUT || path->idx[i] == idx)
583 return true;
584 break;
585 }
586 }
587 }
588 return false;
589}
590
591/* get the default amp value for the target state */
592static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
593 int dir, bool enable)
594{
595 unsigned int caps;
596 unsigned int val = 0;
597
598 caps = query_amp_caps(codec, nid, dir);
599 if (caps & AC_AMPCAP_NUM_STEPS) {
600 /* set to 0dB */
601 if (enable)
602 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
603 }
604 if (caps & AC_AMPCAP_MUTE) {
605 if (!enable)
606 val |= HDA_AMP_MUTE;
607 }
608 return val;
609}
610
611/* initialize the amp value (only at the first time) */
612static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
613{
614 int val = get_amp_val_to_activate(codec, nid, dir, false);
615 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
616}
617
618static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
619 int idx, bool enable)
620{
621 int val;
622 if (is_ctl_associated(codec, nid, dir, idx) ||
Takashi Iwai985803c2013-01-03 16:30:04 +0100623 (!enable && is_active_nid(codec, nid, dir, idx)))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100624 return;
625 val = get_amp_val_to_activate(codec, nid, dir, enable);
626 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
627}
628
629static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
630 int i, bool enable)
631{
632 hda_nid_t nid = path->path[i];
633 init_amp(codec, nid, HDA_OUTPUT, 0);
634 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
635}
636
637static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
638 int i, bool enable, bool add_aamix)
639{
640 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100641 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100642 int n, nums, idx;
643 int type;
644 hda_nid_t nid = path->path[i];
645
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100646 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100647 type = get_wcaps_type(get_wcaps(codec, nid));
648 if (type == AC_WID_PIN ||
649 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
650 nums = 1;
651 idx = 0;
652 } else
653 idx = path->idx[i];
654
655 for (n = 0; n < nums; n++)
656 init_amp(codec, nid, HDA_INPUT, n);
657
658 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
659 return;
660
661 /* here is a little bit tricky in comparison with activate_amp_out();
662 * when aa-mixer is available, we need to enable the path as well
663 */
664 for (n = 0; n < nums; n++) {
665 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
666 continue;
667 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669}
670
Takashi Iwai352f7f92012-12-19 12:52:06 +0100671/* activate or deactivate the given path
672 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100674void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
675 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100677 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Takashi Iwai352f7f92012-12-19 12:52:06 +0100679 if (!enable)
680 path->active = false;
681
682 for (i = path->depth - 1; i >= 0; i--) {
683 if (enable && path->multi[i])
684 snd_hda_codec_write_cache(codec, path->path[i], 0,
685 AC_VERB_SET_CONNECT_SEL,
686 path->idx[i]);
687 if (has_amp_in(codec, path, i))
688 activate_amp_in(codec, path, i, enable, add_aamix);
689 if (has_amp_out(codec, path, i))
690 activate_amp_out(codec, path, i, enable);
691 }
692
693 if (enable)
694 path->active = true;
695}
696EXPORT_SYMBOL_HDA(snd_hda_activate_path);
697
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100698/* turn on/off EAPD on the given pin */
699static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
700{
701 struct hda_gen_spec *spec = codec->spec;
702 if (spec->own_eapd_ctl ||
703 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
704 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100705 if (codec->inv_eapd)
706 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100707 snd_hda_codec_update_cache(codec, pin, 0,
708 AC_VERB_SET_EAPD_BTLENABLE,
709 enable ? 0x02 : 0x00);
710}
711
Takashi Iwai352f7f92012-12-19 12:52:06 +0100712
713/*
714 * Helper functions for creating mixer ctl elements
715 */
716
717enum {
718 HDA_CTL_WIDGET_VOL,
719 HDA_CTL_WIDGET_MUTE,
720 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100721};
722static const struct snd_kcontrol_new control_templates[] = {
723 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
724 HDA_CODEC_MUTE(NULL, 0, 0, 0),
725 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100726};
727
728/* add dynamic controls from template */
729static int add_control(struct hda_gen_spec *spec, int type, const char *name,
730 int cidx, unsigned long val)
731{
732 struct snd_kcontrol_new *knew;
733
Takashi Iwai12c93df2012-12-19 14:38:33 +0100734 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100735 if (!knew)
736 return -ENOMEM;
737 knew->index = cidx;
738 if (get_amp_nid_(val))
739 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
740 knew->private_value = val;
741 return 0;
742}
743
744static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
745 const char *pfx, const char *dir,
746 const char *sfx, int cidx, unsigned long val)
747{
748 char name[32];
749 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
750 return add_control(spec, type, name, cidx, val);
751}
752
753#define add_pb_vol_ctrl(spec, type, pfx, val) \
754 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
755#define add_pb_sw_ctrl(spec, type, pfx, val) \
756 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
757#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
758 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
759#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
760 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
761
762static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
763 unsigned int chs, struct nid_path *path)
764{
765 unsigned int val;
766 if (!path)
767 return 0;
768 val = path->ctls[NID_PATH_VOL_CTL];
769 if (!val)
770 return 0;
771 val = amp_val_replace_channels(val, chs);
772 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
773}
774
775/* return the channel bits suitable for the given path->ctls[] */
776static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
777 int type)
778{
779 int chs = 1; /* mono (left only) */
780 if (path) {
781 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
782 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
783 chs = 3; /* stereo */
784 }
785 return chs;
786}
787
788static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
789 struct nid_path *path)
790{
791 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
792 return add_vol_ctl(codec, pfx, cidx, chs, path);
793}
794
795/* create a mute-switch for the given mixer widget;
796 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
797 */
798static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
799 unsigned int chs, struct nid_path *path)
800{
801 unsigned int val;
802 int type = HDA_CTL_WIDGET_MUTE;
803
804 if (!path)
805 return 0;
806 val = path->ctls[NID_PATH_MUTE_CTL];
807 if (!val)
808 return 0;
809 val = amp_val_replace_channels(val, chs);
810 if (get_amp_direction_(val) == HDA_INPUT) {
811 hda_nid_t nid = get_amp_nid_(val);
812 int nums = snd_hda_get_num_conns(codec, nid);
813 if (nums > 1) {
814 type = HDA_CTL_BIND_MUTE;
815 val |= nums << 19;
816 }
817 }
818 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
819}
820
821static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
822 int cidx, struct nid_path *path)
823{
824 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
825 return add_sw_ctl(codec, pfx, cidx, chs, path);
826}
827
828static const char * const channel_name[4] = {
829 "Front", "Surround", "CLFE", "Side"
830};
831
832/* give some appropriate ctl name prefix for the given line out channel */
833static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
834 bool can_be_master, int *index)
835{
836 struct auto_pin_cfg *cfg = &spec->autocfg;
837
838 *index = 0;
839 if (cfg->line_outs == 1 && !spec->multi_ios &&
840 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
841 return spec->vmaster_mute.hook ? "PCM" : "Master";
842
843 /* if there is really a single DAC used in the whole output paths,
844 * use it master (or "PCM" if a vmaster hook is present)
845 */
846 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
847 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
848 return spec->vmaster_mute.hook ? "PCM" : "Master";
849
850 switch (cfg->line_out_type) {
851 case AUTO_PIN_SPEAKER_OUT:
852 if (cfg->line_outs == 1)
853 return "Speaker";
854 if (cfg->line_outs == 2)
855 return ch ? "Bass Speaker" : "Speaker";
856 break;
857 case AUTO_PIN_HP_OUT:
858 /* for multi-io case, only the primary out */
859 if (ch && spec->multi_ios)
860 break;
861 *index = ch;
862 return "Headphone";
863 default:
864 if (cfg->line_outs == 1 && !spec->multi_ios)
865 return "PCM";
866 break;
867 }
868 if (ch >= ARRAY_SIZE(channel_name)) {
869 snd_BUG();
870 return "PCM";
871 }
872
873 return channel_name[ch];
874}
875
876/*
877 * Parse output paths
878 */
879
880/* badness definition */
881enum {
882 /* No primary DAC is found for the main output */
883 BAD_NO_PRIMARY_DAC = 0x10000,
884 /* No DAC is found for the extra output */
885 BAD_NO_DAC = 0x4000,
886 /* No possible multi-ios */
887 BAD_MULTI_IO = 0x103,
888 /* No individual DAC for extra output */
889 BAD_NO_EXTRA_DAC = 0x102,
890 /* No individual DAC for extra surrounds */
891 BAD_NO_EXTRA_SURR_DAC = 0x101,
892 /* Primary DAC shared with main surrounds */
893 BAD_SHARED_SURROUND = 0x100,
894 /* Primary DAC shared with main CLFE */
895 BAD_SHARED_CLFE = 0x10,
896 /* Primary DAC shared with extra surrounds */
897 BAD_SHARED_EXTRA_SURROUND = 0x10,
898 /* Volume widget is shared */
899 BAD_SHARED_VOL = 0x10,
900};
901
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100902/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +0100903 * volume and mute controls, and assign the values to ctls[].
904 *
905 * When no appropriate widget is found in the path, the badness value
906 * is incremented depending on the situation. The function returns the
907 * total badness for both volume and mute controls.
908 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100909static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100910{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100911 hda_nid_t nid;
912 unsigned int val;
913 int badness = 0;
914
915 if (!path)
916 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100917
918 if (path->ctls[NID_PATH_VOL_CTL] ||
919 path->ctls[NID_PATH_MUTE_CTL])
920 return 0; /* already evaluated */
921
Takashi Iwai352f7f92012-12-19 12:52:06 +0100922 nid = look_for_out_vol_nid(codec, path);
923 if (nid) {
924 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
925 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
926 badness += BAD_SHARED_VOL;
927 else
928 path->ctls[NID_PATH_VOL_CTL] = val;
929 } else
930 badness += BAD_SHARED_VOL;
931 nid = look_for_out_mute_nid(codec, path);
932 if (nid) {
933 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
934 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
935 nid_has_mute(codec, nid, HDA_OUTPUT))
936 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
937 else
938 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
939 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
940 badness += BAD_SHARED_VOL;
941 else
942 path->ctls[NID_PATH_MUTE_CTL] = val;
943 } else
944 badness += BAD_SHARED_VOL;
945 return badness;
946}
947
948struct badness_table {
949 int no_primary_dac; /* no primary DAC */
950 int no_dac; /* no secondary DACs */
951 int shared_primary; /* primary DAC is shared with main output */
952 int shared_surr; /* secondary DAC shared with main or primary */
953 int shared_clfe; /* third DAC shared with main or primary */
954 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
955};
956
957static struct badness_table main_out_badness = {
958 .no_primary_dac = BAD_NO_PRIMARY_DAC,
959 .no_dac = BAD_NO_DAC,
960 .shared_primary = BAD_NO_PRIMARY_DAC,
961 .shared_surr = BAD_SHARED_SURROUND,
962 .shared_clfe = BAD_SHARED_CLFE,
963 .shared_surr_main = BAD_SHARED_SURROUND,
964};
965
966static struct badness_table extra_out_badness = {
967 .no_primary_dac = BAD_NO_DAC,
968 .no_dac = BAD_NO_DAC,
969 .shared_primary = BAD_NO_EXTRA_DAC,
970 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
971 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
972 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
973};
974
Takashi Iwai7385df62013-01-07 09:50:52 +0100975/* get the DAC of the primary output corresponding to the given array index */
976static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
977{
978 struct hda_gen_spec *spec = codec->spec;
979 struct auto_pin_cfg *cfg = &spec->autocfg;
980
981 if (cfg->line_outs > idx)
982 return spec->private_dac_nids[idx];
983 idx -= cfg->line_outs;
984 if (spec->multi_ios > idx)
985 return spec->multi_io[idx].dac;
986 return 0;
987}
988
989/* return the DAC if it's reachable, otherwise zero */
990static inline hda_nid_t try_dac(struct hda_codec *codec,
991 hda_nid_t dac, hda_nid_t pin)
992{
993 return is_reachable_path(codec, dac, pin) ? dac : 0;
994}
995
Takashi Iwai352f7f92012-12-19 12:52:06 +0100996/* try to assign DACs to pins and return the resultant badness */
997static int try_assign_dacs(struct hda_codec *codec, int num_outs,
998 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +0100999 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001000 const struct badness_table *bad)
1001{
1002 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001003 int i, j;
1004 int badness = 0;
1005 hda_nid_t dac;
1006
1007 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return 0;
1009
Takashi Iwai352f7f92012-12-19 12:52:06 +01001010 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001011 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001012 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001013
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001014 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1015 if (path) {
1016 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001017 continue;
1018 }
1019
1020 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001021 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001022 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001023 for (j = 1; j < num_outs; j++) {
1024 if (is_reachable_path(codec, dacs[j], pin)) {
1025 dacs[0] = dacs[j];
1026 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001027 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001028 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001029 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001032 }
1033 dac = dacs[i];
1034 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001035 if (num_outs > 2)
1036 dac = try_dac(codec, get_primary_out(codec, i), pin);
1037 if (!dac)
1038 dac = try_dac(codec, dacs[0], pin);
1039 if (!dac)
1040 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001041 if (dac) {
1042 if (!i)
1043 badness += bad->shared_primary;
1044 else if (i == 1)
1045 badness += bad->shared_surr;
1046 else
1047 badness += bad->shared_clfe;
1048 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1049 dac = spec->private_dac_nids[0];
1050 badness += bad->shared_surr_main;
1051 } else if (!i)
1052 badness += bad->no_primary_dac;
1053 else
1054 badness += bad->no_dac;
1055 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001056 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001057 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001058 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001059 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001060 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001061 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001062 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +01001063 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001064 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001065 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001066 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001067 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001068 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001069 }
1070
1071 return badness;
1072}
1073
1074/* return NID if the given pin has only a single connection to a certain DAC */
1075static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1076{
1077 struct hda_gen_spec *spec = codec->spec;
1078 int i;
1079 hda_nid_t nid_found = 0;
1080
1081 for (i = 0; i < spec->num_all_dacs; i++) {
1082 hda_nid_t nid = spec->all_dacs[i];
1083 if (!nid || is_dac_already_used(codec, nid))
1084 continue;
1085 if (is_reachable_path(codec, nid, pin)) {
1086 if (nid_found)
1087 return 0;
1088 nid_found = nid;
1089 }
1090 }
1091 return nid_found;
1092}
1093
1094/* check whether the given pin can be a multi-io pin */
1095static bool can_be_multiio_pin(struct hda_codec *codec,
1096 unsigned int location, hda_nid_t nid)
1097{
1098 unsigned int defcfg, caps;
1099
1100 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1101 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1102 return false;
1103 if (location && get_defcfg_location(defcfg) != location)
1104 return false;
1105 caps = snd_hda_query_pin_caps(codec, nid);
1106 if (!(caps & AC_PINCAP_OUT))
1107 return false;
1108 return true;
1109}
1110
Takashi Iwaie22aab72013-01-04 14:50:04 +01001111/* count the number of input pins that are capable to be multi-io */
1112static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1113{
1114 struct hda_gen_spec *spec = codec->spec;
1115 struct auto_pin_cfg *cfg = &spec->autocfg;
1116 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1117 unsigned int location = get_defcfg_location(defcfg);
1118 int type, i;
1119 int num_pins = 0;
1120
1121 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1122 for (i = 0; i < cfg->num_inputs; i++) {
1123 if (cfg->inputs[i].type != type)
1124 continue;
1125 if (can_be_multiio_pin(codec, location,
1126 cfg->inputs[i].pin))
1127 num_pins++;
1128 }
1129 }
1130 return num_pins;
1131}
1132
Takashi Iwai352f7f92012-12-19 12:52:06 +01001133/*
1134 * multi-io helper
1135 *
1136 * When hardwired is set, try to fill ony hardwired pins, and returns
1137 * zero if any pins are filled, non-zero if nothing found.
1138 * When hardwired is off, try to fill possible input pins, and returns
1139 * the badness value.
1140 */
1141static int fill_multi_ios(struct hda_codec *codec,
1142 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001143 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001144{
1145 struct hda_gen_spec *spec = codec->spec;
1146 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001147 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001148 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1149 unsigned int location = get_defcfg_location(defcfg);
1150 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001151 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001152
1153 old_pins = spec->multi_ios;
1154 if (old_pins >= 2)
1155 goto end_fill;
1156
Takashi Iwaie22aab72013-01-04 14:50:04 +01001157 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001158 if (num_pins < 2)
1159 goto end_fill;
1160
Takashi Iwai352f7f92012-12-19 12:52:06 +01001161 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1162 for (i = 0; i < cfg->num_inputs; i++) {
1163 hda_nid_t nid = cfg->inputs[i].pin;
1164 hda_nid_t dac = 0;
1165
1166 if (cfg->inputs[i].type != type)
1167 continue;
1168 if (!can_be_multiio_pin(codec, location, nid))
1169 continue;
1170 for (j = 0; j < spec->multi_ios; j++) {
1171 if (nid == spec->multi_io[j].pin)
1172 break;
1173 }
1174 if (j < spec->multi_ios)
1175 continue;
1176
Takashi Iwai352f7f92012-12-19 12:52:06 +01001177 if (hardwired)
1178 dac = get_dac_if_single(codec, nid);
1179 else if (!dac)
1180 dac = look_for_dac(codec, nid, false);
1181 if (!dac) {
1182 badness++;
1183 continue;
1184 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001185 path = snd_hda_add_new_path(codec, dac, nid,
1186 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001187 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001188 badness++;
1189 continue;
1190 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001191 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001192 spec->multi_io[spec->multi_ios].pin = nid;
1193 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001194 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1195 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001196 spec->multi_ios++;
1197 if (spec->multi_ios >= 2)
1198 break;
1199 }
1200 }
1201 end_fill:
1202 if (badness)
1203 badness = BAD_MULTI_IO;
1204 if (old_pins == spec->multi_ios) {
1205 if (hardwired)
1206 return 1; /* nothing found */
1207 else
1208 return badness; /* no badness if nothing found */
1209 }
1210 if (!hardwired && spec->multi_ios < 2) {
1211 /* cancel newly assigned paths */
1212 spec->paths.used -= spec->multi_ios - old_pins;
1213 spec->multi_ios = old_pins;
1214 return badness;
1215 }
1216
1217 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001218 for (i = old_pins; i < spec->multi_ios; i++) {
1219 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1220 badness += assign_out_path_ctls(codec, path);
1221 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001222
1223 return badness;
1224}
1225
1226/* map DACs for all pins in the list if they are single connections */
1227static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001228 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001229{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001230 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001231 int i;
1232 bool found = false;
1233 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001234 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001235 hda_nid_t dac;
1236 if (dacs[i])
1237 continue;
1238 dac = get_dac_if_single(codec, pins[i]);
1239 if (!dac)
1240 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001241 path = snd_hda_add_new_path(codec, dac, pins[i],
1242 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001243 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001244 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001245 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001246 dacs[i] = dac;
1247 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001248 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001249 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001250 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001251 }
1252 }
1253 return found;
1254}
1255
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001256/* create a new path including aamix if available, and return its index */
1257static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1258{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001259 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001260 struct nid_path *path;
1261
1262 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001263 if (!path || !path->depth ||
1264 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001265 return 0;
1266 path = snd_hda_add_new_path(codec, path->path[0],
1267 path->path[path->depth - 1],
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001268 spec->mixer_nid);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001269 if (!path)
1270 return 0;
1271 print_nid_path("output-aamix", path);
1272 path->active = false; /* unused as default */
1273 return snd_hda_get_path_idx(codec, path);
1274}
1275
Takashi Iwaia07a9492013-01-07 16:44:06 +01001276/* fill the empty entries in the dac array for speaker/hp with the
1277 * shared dac pointed by the paths
1278 */
1279static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1280 hda_nid_t *dacs, int *path_idx)
1281{
1282 struct nid_path *path;
1283 int i;
1284
1285 for (i = 0; i < num_outs; i++) {
1286 if (dacs[i])
1287 continue;
1288 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1289 if (!path)
1290 continue;
1291 dacs[i] = path->path[0];
1292 }
1293}
1294
Takashi Iwai352f7f92012-12-19 12:52:06 +01001295/* fill in the dac_nids table from the parsed pin configuration */
1296static int fill_and_eval_dacs(struct hda_codec *codec,
1297 bool fill_hardwired,
1298 bool fill_mio_first)
1299{
1300 struct hda_gen_spec *spec = codec->spec;
1301 struct auto_pin_cfg *cfg = &spec->autocfg;
1302 int i, err, badness;
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001303 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001304
1305 /* set num_dacs once to full for look_for_dac() */
1306 spec->multiout.num_dacs = cfg->line_outs;
1307 spec->multiout.dac_nids = spec->private_dac_nids;
1308 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1309 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1310 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1311 spec->multi_ios = 0;
1312 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001313
1314 /* clear path indices */
1315 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1316 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1317 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1318 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1319 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001320 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001321 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1322 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1323
Takashi Iwai352f7f92012-12-19 12:52:06 +01001324 badness = 0;
1325
1326 /* fill hard-wired DACs first */
1327 if (fill_hardwired) {
1328 bool mapped;
1329 do {
1330 mapped = map_singles(codec, cfg->line_outs,
1331 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001332 spec->private_dac_nids,
1333 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001334 mapped |= map_singles(codec, cfg->hp_outs,
1335 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001336 spec->multiout.hp_out_nid,
1337 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001338 mapped |= map_singles(codec, cfg->speaker_outs,
1339 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001340 spec->multiout.extra_out_nid,
1341 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001342 if (fill_mio_first && cfg->line_outs == 1 &&
1343 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001344 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001345 if (!err)
1346 mapped = true;
1347 }
1348 } while (mapped);
1349 }
1350
1351 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001352 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001353 &main_out_badness);
1354
Takashi Iwai352f7f92012-12-19 12:52:06 +01001355 if (fill_mio_first &&
1356 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1357 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001358 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001359 if (err < 0)
1360 return err;
1361 /* we don't count badness at this stage yet */
1362 }
1363
1364 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1365 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1366 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001367 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001368 &extra_out_badness);
1369 if (err < 0)
1370 return err;
1371 badness += err;
1372 }
1373 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1374 err = try_assign_dacs(codec, cfg->speaker_outs,
1375 cfg->speaker_pins,
1376 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001377 spec->speaker_paths,
1378 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001379 if (err < 0)
1380 return err;
1381 badness += err;
1382 }
1383 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001384 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001385 if (err < 0)
1386 return err;
1387 badness += err;
1388 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001389
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001390 if (spec->mixer_nid) {
1391 spec->aamix_out_paths[0] =
1392 check_aamix_out_path(codec, spec->out_paths[0]);
1393 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1394 spec->aamix_out_paths[1] =
1395 check_aamix_out_path(codec, spec->hp_paths[0]);
1396 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1397 spec->aamix_out_paths[2] =
1398 check_aamix_out_path(codec, spec->speaker_paths[0]);
1399 }
1400
Takashi Iwaie22aab72013-01-04 14:50:04 +01001401 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1402 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1403 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001404
Takashi Iwaia07a9492013-01-07 16:44:06 +01001405 /* re-count num_dacs and squash invalid entries */
1406 spec->multiout.num_dacs = 0;
1407 for (i = 0; i < cfg->line_outs; i++) {
1408 if (spec->private_dac_nids[i])
1409 spec->multiout.num_dacs++;
1410 else {
1411 memmove(spec->private_dac_nids + i,
1412 spec->private_dac_nids + i + 1,
1413 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1414 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1415 }
1416 }
1417
1418 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001419 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001420
Takashi Iwai352f7f92012-12-19 12:52:06 +01001421 if (spec->multi_ios == 2) {
1422 for (i = 0; i < 2; i++)
1423 spec->private_dac_nids[spec->multiout.num_dacs++] =
1424 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001425 } else if (spec->multi_ios) {
1426 spec->multi_ios = 0;
1427 badness += BAD_MULTI_IO;
1428 }
1429
Takashi Iwaia07a9492013-01-07 16:44:06 +01001430 /* re-fill the shared DAC for speaker / headphone */
1431 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1432 refill_shared_dacs(codec, cfg->hp_outs,
1433 spec->multiout.hp_out_nid,
1434 spec->hp_paths);
1435 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1436 refill_shared_dacs(codec, cfg->speaker_outs,
1437 spec->multiout.extra_out_nid,
1438 spec->speaker_paths);
1439
Takashi Iwai2c12c302013-01-10 09:33:29 +01001440 /* set initial pinctl targets */
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001441 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1442 val = PIN_HP;
1443 else
1444 val = PIN_OUT;
1445 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001446 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1447 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001448 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1449 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
Takashi Iwai2c12c302013-01-10 09:33:29 +01001450 set_pin_targets(codec, cfg->speaker_outs,
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001451 cfg->speaker_pins, val);
1452 }
Takashi Iwai2c12c302013-01-10 09:33:29 +01001453
Takashi Iwai352f7f92012-12-19 12:52:06 +01001454 return badness;
1455}
1456
1457#define DEBUG_BADNESS
1458
1459#ifdef DEBUG_BADNESS
1460#define debug_badness snd_printdd
1461#else
1462#define debug_badness(...)
1463#endif
1464
1465static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1466{
1467 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1468 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001469 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001470 spec->multiout.dac_nids[0],
1471 spec->multiout.dac_nids[1],
1472 spec->multiout.dac_nids[2],
1473 spec->multiout.dac_nids[3]);
1474 if (spec->multi_ios > 0)
1475 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1476 spec->multi_ios,
1477 spec->multi_io[0].pin, spec->multi_io[1].pin,
1478 spec->multi_io[0].dac, spec->multi_io[1].dac);
1479 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1480 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001481 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001482 spec->multiout.hp_out_nid[0],
1483 spec->multiout.hp_out_nid[1],
1484 spec->multiout.hp_out_nid[2],
1485 spec->multiout.hp_out_nid[3]);
1486 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1487 cfg->speaker_pins[0], cfg->speaker_pins[1],
1488 cfg->speaker_pins[2], cfg->speaker_pins[3],
1489 spec->multiout.extra_out_nid[0],
1490 spec->multiout.extra_out_nid[1],
1491 spec->multiout.extra_out_nid[2],
1492 spec->multiout.extra_out_nid[3]);
1493}
1494
1495/* find all available DACs of the codec */
1496static void fill_all_dac_nids(struct hda_codec *codec)
1497{
1498 struct hda_gen_spec *spec = codec->spec;
1499 int i;
1500 hda_nid_t nid = codec->start_nid;
1501
1502 spec->num_all_dacs = 0;
1503 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1504 for (i = 0; i < codec->num_nodes; i++, nid++) {
1505 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1506 continue;
1507 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1508 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1509 break;
1510 }
1511 spec->all_dacs[spec->num_all_dacs++] = nid;
1512 }
1513}
1514
1515static int parse_output_paths(struct hda_codec *codec)
1516{
1517 struct hda_gen_spec *spec = codec->spec;
1518 struct auto_pin_cfg *cfg = &spec->autocfg;
1519 struct auto_pin_cfg *best_cfg;
1520 int best_badness = INT_MAX;
1521 int badness;
1522 bool fill_hardwired = true, fill_mio_first = true;
1523 bool best_wired = true, best_mio = true;
1524 bool hp_spk_swapped = false;
1525
Takashi Iwai352f7f92012-12-19 12:52:06 +01001526 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1527 if (!best_cfg)
1528 return -ENOMEM;
1529 *best_cfg = *cfg;
1530
1531 for (;;) {
1532 badness = fill_and_eval_dacs(codec, fill_hardwired,
1533 fill_mio_first);
1534 if (badness < 0) {
1535 kfree(best_cfg);
1536 return badness;
1537 }
1538 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1539 cfg->line_out_type, fill_hardwired, fill_mio_first,
1540 badness);
1541 debug_show_configs(spec, cfg);
1542 if (badness < best_badness) {
1543 best_badness = badness;
1544 *best_cfg = *cfg;
1545 best_wired = fill_hardwired;
1546 best_mio = fill_mio_first;
1547 }
1548 if (!badness)
1549 break;
1550 fill_mio_first = !fill_mio_first;
1551 if (!fill_mio_first)
1552 continue;
1553 fill_hardwired = !fill_hardwired;
1554 if (!fill_hardwired)
1555 continue;
1556 if (hp_spk_swapped)
1557 break;
1558 hp_spk_swapped = true;
1559 if (cfg->speaker_outs > 0 &&
1560 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1561 cfg->hp_outs = cfg->line_outs;
1562 memcpy(cfg->hp_pins, cfg->line_out_pins,
1563 sizeof(cfg->hp_pins));
1564 cfg->line_outs = cfg->speaker_outs;
1565 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1566 sizeof(cfg->speaker_pins));
1567 cfg->speaker_outs = 0;
1568 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1569 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1570 fill_hardwired = true;
1571 continue;
1572 }
1573 if (cfg->hp_outs > 0 &&
1574 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1575 cfg->speaker_outs = cfg->line_outs;
1576 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1577 sizeof(cfg->speaker_pins));
1578 cfg->line_outs = cfg->hp_outs;
1579 memcpy(cfg->line_out_pins, cfg->hp_pins,
1580 sizeof(cfg->hp_pins));
1581 cfg->hp_outs = 0;
1582 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1583 cfg->line_out_type = AUTO_PIN_HP_OUT;
1584 fill_hardwired = true;
1585 continue;
1586 }
1587 break;
1588 }
1589
1590 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001591 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001592 *cfg = *best_cfg;
1593 fill_and_eval_dacs(codec, best_wired, best_mio);
1594 }
1595 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1596 cfg->line_out_type, best_wired, best_mio);
1597 debug_show_configs(spec, cfg);
1598
1599 if (cfg->line_out_pins[0]) {
1600 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001601 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001602 if (path)
1603 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001604 if (spec->vmaster_nid)
1605 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1606 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001607 }
1608
1609 kfree(best_cfg);
1610 return 0;
1611}
1612
1613/* add playback controls from the parsed DAC table */
1614static int create_multi_out_ctls(struct hda_codec *codec,
1615 const struct auto_pin_cfg *cfg)
1616{
1617 struct hda_gen_spec *spec = codec->spec;
1618 int i, err, noutputs;
1619
1620 noutputs = cfg->line_outs;
1621 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1622 noutputs += spec->multi_ios;
1623
1624 for (i = 0; i < noutputs; i++) {
1625 const char *name;
1626 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001627 struct nid_path *path;
1628
Takashi Iwai352f7f92012-12-19 12:52:06 +01001629 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001630 index = 0;
1631 name = channel_name[i];
1632 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001633 name = get_line_out_pfx(spec, i, true, &index);
1634 }
1635
Takashi Iwai196c17662013-01-04 15:01:40 +01001636 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001637 if (!path)
1638 continue;
1639 if (!name || !strcmp(name, "CLFE")) {
1640 /* Center/LFE */
1641 err = add_vol_ctl(codec, "Center", 0, 1, path);
1642 if (err < 0)
1643 return err;
1644 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1645 if (err < 0)
1646 return err;
1647 err = add_sw_ctl(codec, "Center", 0, 1, path);
1648 if (err < 0)
1649 return err;
1650 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1651 if (err < 0)
1652 return err;
1653 } else {
1654 err = add_stereo_vol(codec, name, index, path);
1655 if (err < 0)
1656 return err;
1657 err = add_stereo_sw(codec, name, index, path);
1658 if (err < 0)
1659 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
1661 }
1662 return 0;
1663}
1664
Takashi Iwaic2c80382013-01-07 10:33:57 +01001665static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001666 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001668 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 int err;
1670
Takashi Iwai196c17662013-01-04 15:01:40 +01001671 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001672 if (!path)
1673 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001674 err = add_stereo_vol(codec, pfx, cidx, path);
1675 if (err < 0)
1676 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001677 err = add_stereo_sw(codec, pfx, cidx, path);
1678 if (err < 0)
1679 return err;
1680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Takashi Iwai352f7f92012-12-19 12:52:06 +01001683/* add playback controls for speaker and HP outputs */
1684static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001685 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001687 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001688
1689 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001690 const char *name;
1691 char tmp[44];
1692 int err, idx = 0;
1693
1694 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1695 name = "Bass Speaker";
1696 else if (num_pins >= 3) {
1697 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001698 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001699 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001700 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001701 name = pfx;
1702 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001704 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001705 if (err < 0)
1706 return err;
1707 }
1708 return 0;
1709}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001710
Takashi Iwai352f7f92012-12-19 12:52:06 +01001711static int create_hp_out_ctls(struct hda_codec *codec)
1712{
1713 struct hda_gen_spec *spec = codec->spec;
1714 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001715 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001716 "Headphone");
1717}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Takashi Iwai352f7f92012-12-19 12:52:06 +01001719static int create_speaker_out_ctls(struct hda_codec *codec)
1720{
1721 struct hda_gen_spec *spec = codec->spec;
1722 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001723 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001724 "Speaker");
1725}
1726
1727/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001728 * independent HP controls
1729 */
1730
1731static int indep_hp_info(struct snd_kcontrol *kcontrol,
1732 struct snd_ctl_elem_info *uinfo)
1733{
1734 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1735}
1736
1737static int indep_hp_get(struct snd_kcontrol *kcontrol,
1738 struct snd_ctl_elem_value *ucontrol)
1739{
1740 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1741 struct hda_gen_spec *spec = codec->spec;
1742 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1743 return 0;
1744}
1745
1746static int indep_hp_put(struct snd_kcontrol *kcontrol,
1747 struct snd_ctl_elem_value *ucontrol)
1748{
1749 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1750 struct hda_gen_spec *spec = codec->spec;
1751 unsigned int select = ucontrol->value.enumerated.item[0];
1752 int ret = 0;
1753
1754 mutex_lock(&spec->pcm_mutex);
1755 if (spec->active_streams) {
1756 ret = -EBUSY;
1757 goto unlock;
1758 }
1759
1760 if (spec->indep_hp_enabled != select) {
1761 spec->indep_hp_enabled = select;
1762 if (spec->indep_hp_enabled)
1763 spec->multiout.hp_out_nid[0] = 0;
1764 else
1765 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1766 ret = 1;
1767 }
1768 unlock:
1769 mutex_unlock(&spec->pcm_mutex);
1770 return ret;
1771}
1772
1773static const struct snd_kcontrol_new indep_hp_ctl = {
1774 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1775 .name = "Independent HP",
1776 .info = indep_hp_info,
1777 .get = indep_hp_get,
1778 .put = indep_hp_put,
1779};
1780
1781
1782static int create_indep_hp_ctls(struct hda_codec *codec)
1783{
1784 struct hda_gen_spec *spec = codec->spec;
1785
1786 if (!spec->indep_hp)
1787 return 0;
1788 if (!spec->multiout.hp_out_nid[0]) {
1789 spec->indep_hp = 0;
1790 return 0;
1791 }
1792
1793 spec->indep_hp_enabled = false;
1794 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1795 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1796 return -ENOMEM;
1797 return 0;
1798}
1799
1800/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001801 * channel mode enum control
1802 */
1803
1804static int ch_mode_info(struct snd_kcontrol *kcontrol,
1805 struct snd_ctl_elem_info *uinfo)
1806{
1807 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1808 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001809 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001810
1811 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1812 uinfo->count = 1;
1813 uinfo->value.enumerated.items = spec->multi_ios + 1;
1814 if (uinfo->value.enumerated.item > spec->multi_ios)
1815 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001816 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1817 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001818 return 0;
1819}
1820
1821static int ch_mode_get(struct snd_kcontrol *kcontrol,
1822 struct snd_ctl_elem_value *ucontrol)
1823{
1824 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1825 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001826 ucontrol->value.enumerated.item[0] =
1827 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001828 return 0;
1829}
1830
Takashi Iwai196c17662013-01-04 15:01:40 +01001831static inline struct nid_path *
1832get_multiio_path(struct hda_codec *codec, int idx)
1833{
1834 struct hda_gen_spec *spec = codec->spec;
1835 return snd_hda_get_path_from_idx(codec,
1836 spec->out_paths[spec->autocfg.line_outs + idx]);
1837}
1838
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001839static void update_automute_all(struct hda_codec *codec);
1840
Takashi Iwai352f7f92012-12-19 12:52:06 +01001841static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1842{
1843 struct hda_gen_spec *spec = codec->spec;
1844 hda_nid_t nid = spec->multi_io[idx].pin;
1845 struct nid_path *path;
1846
Takashi Iwai196c17662013-01-04 15:01:40 +01001847 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001848 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001850
1851 if (path->active == output)
1852 return 0;
1853
1854 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01001855 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001856 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001857 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001858 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001859 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001860 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001861 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01001863
1864 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001865 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01001866
Takashi Iwai352f7f92012-12-19 12:52:06 +01001867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
Takashi Iwai352f7f92012-12-19 12:52:06 +01001870static int ch_mode_put(struct snd_kcontrol *kcontrol,
1871 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001873 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1874 struct hda_gen_spec *spec = codec->spec;
1875 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Takashi Iwai352f7f92012-12-19 12:52:06 +01001877 ch = ucontrol->value.enumerated.item[0];
1878 if (ch < 0 || ch > spec->multi_ios)
1879 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001880 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001881 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001882 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883 for (i = 0; i < spec->multi_ios; i++)
1884 set_multi_io(codec, i, i < ch);
1885 spec->multiout.max_channels = max(spec->ext_channel_count,
1886 spec->const_channel_count);
1887 if (spec->need_dac_fix)
1888 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return 1;
1890}
1891
Takashi Iwai352f7f92012-12-19 12:52:06 +01001892static const struct snd_kcontrol_new channel_mode_enum = {
1893 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1894 .name = "Channel Mode",
1895 .info = ch_mode_info,
1896 .get = ch_mode_get,
1897 .put = ch_mode_put,
1898};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Takashi Iwai352f7f92012-12-19 12:52:06 +01001900static int create_multi_channel_mode(struct hda_codec *codec)
1901{
1902 struct hda_gen_spec *spec = codec->spec;
1903
1904 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001905 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001906 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return 0;
1909}
1910
Takashi Iwai352f7f92012-12-19 12:52:06 +01001911/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001912 * aamix loopback enable/disable switch
1913 */
1914
1915#define loopback_mixing_info indep_hp_info
1916
1917static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1918 struct snd_ctl_elem_value *ucontrol)
1919{
1920 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1921 struct hda_gen_spec *spec = codec->spec;
1922 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1923 return 0;
1924}
1925
1926static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1927 int nomix_path_idx, int mix_path_idx)
1928{
1929 struct nid_path *nomix_path, *mix_path;
1930
1931 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1932 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1933 if (!nomix_path || !mix_path)
1934 return;
1935 if (do_mix) {
1936 snd_hda_activate_path(codec, nomix_path, false, true);
1937 snd_hda_activate_path(codec, mix_path, true, true);
1938 } else {
1939 snd_hda_activate_path(codec, mix_path, false, true);
1940 snd_hda_activate_path(codec, nomix_path, true, true);
1941 }
1942}
1943
1944static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1945 struct snd_ctl_elem_value *ucontrol)
1946{
1947 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1948 struct hda_gen_spec *spec = codec->spec;
1949 unsigned int val = ucontrol->value.enumerated.item[0];
1950
1951 if (val == spec->aamix_mode)
1952 return 0;
1953 spec->aamix_mode = val;
1954 update_aamix_paths(codec, val, spec->out_paths[0],
1955 spec->aamix_out_paths[0]);
1956 update_aamix_paths(codec, val, spec->hp_paths[0],
1957 spec->aamix_out_paths[1]);
1958 update_aamix_paths(codec, val, spec->speaker_paths[0],
1959 spec->aamix_out_paths[2]);
1960 return 1;
1961}
1962
1963static const struct snd_kcontrol_new loopback_mixing_enum = {
1964 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1965 .name = "Loopback Mixing",
1966 .info = loopback_mixing_info,
1967 .get = loopback_mixing_get,
1968 .put = loopback_mixing_put,
1969};
1970
1971static int create_loopback_mixing_ctl(struct hda_codec *codec)
1972{
1973 struct hda_gen_spec *spec = codec->spec;
1974
1975 if (!spec->mixer_nid)
1976 return 0;
1977 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1978 spec->aamix_out_paths[2]))
1979 return 0;
1980 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1981 return -ENOMEM;
1982 return 0;
1983}
1984
1985/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001986 * shared headphone/mic handling
1987 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001988
Takashi Iwai352f7f92012-12-19 12:52:06 +01001989static void call_update_outputs(struct hda_codec *codec);
1990
1991/* for shared I/O, change the pin-control accordingly */
1992static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1993{
1994 struct hda_gen_spec *spec = codec->spec;
1995 unsigned int val;
1996 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1997 /* NOTE: this assumes that there are only two inputs, the
1998 * first is the real internal mic and the second is HP/mic jack.
1999 */
2000
2001 val = snd_hda_get_default_vref(codec, pin);
2002
2003 /* This pin does not have vref caps - let's enable vref on pin 0x18
2004 instead, as suggested by Realtek */
2005 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2006 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2007 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2008 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002009 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2010 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002011 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002012
2013 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002014 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002015
2016 spec->automute_speaker = !set_as_mic;
2017 call_update_outputs(codec);
2018}
2019
2020/* create a shared input with the headphone out */
2021static int create_shared_input(struct hda_codec *codec)
2022{
2023 struct hda_gen_spec *spec = codec->spec;
2024 struct auto_pin_cfg *cfg = &spec->autocfg;
2025 unsigned int defcfg;
2026 hda_nid_t nid;
2027
2028 /* only one internal input pin? */
2029 if (cfg->num_inputs != 1)
2030 return 0;
2031 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2032 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2033 return 0;
2034
2035 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2036 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2037 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2038 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2039 else
2040 return 0; /* both not available */
2041
2042 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2043 return 0; /* no input */
2044
2045 cfg->inputs[1].pin = nid;
2046 cfg->inputs[1].type = AUTO_PIN_MIC;
2047 cfg->num_inputs = 2;
2048 spec->shared_mic_hp = 1;
2049 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2050 return 0;
2051}
2052
Takashi Iwai978e77e2013-01-10 16:57:58 +01002053/*
2054 * output jack mode
2055 */
2056static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2057 struct snd_ctl_elem_info *uinfo)
2058{
2059 static const char * const texts[] = {
2060 "Line Out", "Headphone Out",
2061 };
2062 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2063}
2064
2065static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2066 struct snd_ctl_elem_value *ucontrol)
2067{
2068 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2069 hda_nid_t nid = kcontrol->private_value;
2070 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2071 ucontrol->value.enumerated.item[0] = 1;
2072 else
2073 ucontrol->value.enumerated.item[0] = 0;
2074 return 0;
2075}
2076
2077static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2078 struct snd_ctl_elem_value *ucontrol)
2079{
2080 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2081 hda_nid_t nid = kcontrol->private_value;
2082 unsigned int val;
2083
2084 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2085 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2086 return 0;
2087 snd_hda_set_pin_ctl_cache(codec, nid, val);
2088 return 1;
2089}
2090
2091static const struct snd_kcontrol_new out_jack_mode_enum = {
2092 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2093 .info = out_jack_mode_info,
2094 .get = out_jack_mode_get,
2095 .put = out_jack_mode_put,
2096};
2097
2098static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2099{
2100 struct hda_gen_spec *spec = codec->spec;
2101 int i;
2102
2103 for (i = 0; i < spec->kctls.used; i++) {
2104 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2105 if (!strcmp(kctl->name, name) && kctl->index == idx)
2106 return true;
2107 }
2108 return false;
2109}
2110
2111static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2112 char *name, size_t name_len)
2113{
2114 struct hda_gen_spec *spec = codec->spec;
2115 int idx = 0;
2116
2117 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2118 strlcat(name, " Jack Mode", name_len);
2119
2120 for (; find_kctl_name(codec, name, idx); idx++)
2121 ;
2122}
2123
2124static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2125 hda_nid_t *pins)
2126{
2127 struct hda_gen_spec *spec = codec->spec;
2128 int i;
2129
2130 for (i = 0; i < num_pins; i++) {
2131 hda_nid_t pin = pins[i];
2132 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2133 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2134 struct snd_kcontrol_new *knew;
2135 char name[44];
2136 get_jack_mode_name(codec, pin, name, sizeof(name));
2137 knew = snd_hda_gen_add_kctl(spec, name,
2138 &out_jack_mode_enum);
2139 if (!knew)
2140 return -ENOMEM;
2141 knew->private_value = pin;
2142 }
2143 }
2144
2145 return 0;
2146}
2147
Takashi Iwai294765582013-01-17 09:52:11 +01002148/*
2149 * input jack mode
2150 */
2151
2152/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2153#define NUM_VREFS 6
2154
2155static const char * const vref_texts[NUM_VREFS] = {
2156 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2157 "", "Mic 80pc Bias", "Mic 100pc Bias"
2158};
2159
2160static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2161{
2162 unsigned int pincap;
2163
2164 pincap = snd_hda_query_pin_caps(codec, pin);
2165 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2166 /* filter out unusual vrefs */
2167 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2168 return pincap;
2169}
2170
2171/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2172static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2173{
2174 unsigned int i, n = 0;
2175
2176 for (i = 0; i < NUM_VREFS; i++) {
2177 if (vref_caps & (1 << i)) {
2178 if (n == item_idx)
2179 return i;
2180 n++;
2181 }
2182 }
2183 return 0;
2184}
2185
2186/* convert back from the vref ctl index to the enum item index */
2187static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2188{
2189 unsigned int i, n = 0;
2190
2191 for (i = 0; i < NUM_VREFS; i++) {
2192 if (i == idx)
2193 return n;
2194 if (vref_caps & (1 << i))
2195 n++;
2196 }
2197 return 0;
2198}
2199
2200static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2201 struct snd_ctl_elem_info *uinfo)
2202{
2203 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2204 hda_nid_t nid = kcontrol->private_value;
2205 unsigned int vref_caps = get_vref_caps(codec, nid);
2206
2207 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2208 vref_texts);
2209 /* set the right text */
2210 strcpy(uinfo->value.enumerated.name,
2211 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2212 return 0;
2213}
2214
2215static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2216 struct snd_ctl_elem_value *ucontrol)
2217{
2218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2219 hda_nid_t nid = kcontrol->private_value;
2220 unsigned int vref_caps = get_vref_caps(codec, nid);
2221 unsigned int idx;
2222
2223 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2224 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2225 return 0;
2226}
2227
2228static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2229 struct snd_ctl_elem_value *ucontrol)
2230{
2231 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2232 hda_nid_t nid = kcontrol->private_value;
2233 unsigned int vref_caps = get_vref_caps(codec, nid);
2234 unsigned int val, idx;
2235
2236 val = snd_hda_codec_get_pin_target(codec, nid);
2237 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2238 if (idx == ucontrol->value.enumerated.item[0])
2239 return 0;
2240
2241 val &= ~AC_PINCTL_VREFEN;
2242 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2243 snd_hda_set_pin_ctl_cache(codec, nid, val);
2244 return 1;
2245}
2246
2247static const struct snd_kcontrol_new in_jack_mode_enum = {
2248 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2249 .info = in_jack_mode_info,
2250 .get = in_jack_mode_get,
2251 .put = in_jack_mode_put,
2252};
2253
2254static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2255{
2256 struct hda_gen_spec *spec = codec->spec;
2257 unsigned int defcfg;
2258 struct snd_kcontrol_new *knew;
2259 char name[44];
2260
2261 /* no jack mode for fixed pins */
2262 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2263 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2264 return 0;
2265
2266 /* no multiple vref caps? */
2267 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2268 return 0;
2269
2270 get_jack_mode_name(codec, pin, name, sizeof(name));
2271 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2272 if (!knew)
2273 return -ENOMEM;
2274 knew->private_value = pin;
2275 return 0;
2276}
2277
Takashi Iwai352f7f92012-12-19 12:52:06 +01002278
2279/*
2280 * Parse input paths
2281 */
2282
2283#ifdef CONFIG_PM
2284/* add the powersave loopback-list entry */
2285static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2286{
2287 struct hda_amp_list *list;
2288
2289 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2290 return;
2291 list = spec->loopback_list + spec->num_loopbacks;
2292 list->nid = mix;
2293 list->dir = HDA_INPUT;
2294 list->idx = idx;
2295 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002296 spec->loopback.amplist = spec->loopback_list;
2297}
2298#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002299#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002300#endif
2301
Takashi Iwai352f7f92012-12-19 12:52:06 +01002302/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002303static int new_analog_input(struct hda_codec *codec, int input_idx,
2304 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002305 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002307 struct hda_gen_spec *spec = codec->spec;
2308 struct nid_path *path;
2309 unsigned int val;
2310 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Takashi Iwai352f7f92012-12-19 12:52:06 +01002312 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2313 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2314 return 0; /* no need for analog loopback */
2315
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002316 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002317 if (!path)
2318 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002319 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002320 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002321
2322 idx = path->idx[path->depth - 1];
2323 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2324 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2325 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002326 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002328 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330
Takashi Iwai352f7f92012-12-19 12:52:06 +01002331 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2332 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2333 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002334 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002336 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338
Takashi Iwai352f7f92012-12-19 12:52:06 +01002339 path->active = true;
2340 add_loopback_list(spec, mix_nid, idx);
2341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
Takashi Iwai352f7f92012-12-19 12:52:06 +01002344static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002346 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2347 return (pincap & AC_PINCAP_IN) != 0;
2348}
2349
2350/* Parse the codec tree and retrieve ADCs */
2351static int fill_adc_nids(struct hda_codec *codec)
2352{
2353 struct hda_gen_spec *spec = codec->spec;
2354 hda_nid_t nid;
2355 hda_nid_t *adc_nids = spec->adc_nids;
2356 int max_nums = ARRAY_SIZE(spec->adc_nids);
2357 int i, nums = 0;
2358
2359 nid = codec->start_nid;
2360 for (i = 0; i < codec->num_nodes; i++, nid++) {
2361 unsigned int caps = get_wcaps(codec, nid);
2362 int type = get_wcaps_type(caps);
2363
2364 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2365 continue;
2366 adc_nids[nums] = nid;
2367 if (++nums >= max_nums)
2368 break;
2369 }
2370 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002371
2372 /* copy the detected ADCs to all_adcs[] */
2373 spec->num_all_adcs = nums;
2374 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2375
Takashi Iwai352f7f92012-12-19 12:52:06 +01002376 return nums;
2377}
2378
2379/* filter out invalid adc_nids that don't give all active input pins;
2380 * if needed, check whether dynamic ADC-switching is available
2381 */
2382static int check_dyn_adc_switch(struct hda_codec *codec)
2383{
2384 struct hda_gen_spec *spec = codec->spec;
2385 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002386 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002387 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002388
2389 again:
2390 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002391 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002392 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002393 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002394 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002395 break;
2396 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002397 if (i >= imux->num_items) {
2398 ok_bits |= (1 << n);
2399 nums++;
2400 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002401 }
2402
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002403 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002404 if (spec->shared_mic_hp) {
2405 spec->shared_mic_hp = 0;
2406 imux->num_items = 1;
2407 goto again;
2408 }
2409
2410 /* check whether ADC-switch is possible */
2411 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002412 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002413 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002414 spec->dyn_adc_idx[i] = n;
2415 break;
2416 }
2417 }
2418 }
2419
2420 snd_printdd("hda-codec: enabling ADC switching\n");
2421 spec->dyn_adc_switch = 1;
2422 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002423 /* shrink the invalid adcs and input paths */
2424 nums = 0;
2425 for (n = 0; n < spec->num_adc_nids; n++) {
2426 if (!(ok_bits & (1 << n)))
2427 continue;
2428 if (n != nums) {
2429 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002430 for (i = 0; i < imux->num_items; i++) {
2431 invalidate_nid_path(codec,
2432 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002433 spec->input_paths[i][nums] =
2434 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002435 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002436 }
2437 nums++;
2438 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002439 spec->num_adc_nids = nums;
2440 }
2441
2442 if (imux->num_items == 1 || spec->shared_mic_hp) {
2443 snd_printdd("hda-codec: reducing to a single ADC\n");
2444 spec->num_adc_nids = 1; /* reduce to a single ADC */
2445 }
2446
2447 /* single index for individual volumes ctls */
2448 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2449 spec->num_adc_nids = 1;
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return 0;
2452}
2453
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002454/* parse capture source paths from the given pin and create imux items */
2455static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2456 int num_adcs, const char *label, int anchor)
2457{
2458 struct hda_gen_spec *spec = codec->spec;
2459 struct hda_input_mux *imux = &spec->input_mux;
2460 int imux_idx = imux->num_items;
2461 bool imux_added = false;
2462 int c;
2463
2464 for (c = 0; c < num_adcs; c++) {
2465 struct nid_path *path;
2466 hda_nid_t adc = spec->adc_nids[c];
2467
2468 if (!is_reachable_path(codec, pin, adc))
2469 continue;
2470 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2471 if (!path)
2472 continue;
2473 print_nid_path("input", path);
2474 spec->input_paths[imux_idx][c] =
2475 snd_hda_get_path_idx(codec, path);
2476
2477 if (!imux_added) {
2478 spec->imux_pins[imux->num_items] = pin;
2479 snd_hda_add_imux_item(imux, label,
2480 imux->num_items, NULL);
2481 imux_added = true;
2482 }
2483 }
2484
2485 return 0;
2486}
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002489 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002491static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002492{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002493 struct hda_gen_spec *spec = codec->spec;
2494 const struct auto_pin_cfg *cfg = &spec->autocfg;
2495 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002496 int num_adcs;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002497 int i, err, type_idx = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002498 const char *prev_label = NULL;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002499 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002500
Takashi Iwai352f7f92012-12-19 12:52:06 +01002501 num_adcs = fill_adc_nids(codec);
2502 if (num_adcs < 0)
2503 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002504
Takashi Iwai352f7f92012-12-19 12:52:06 +01002505 for (i = 0; i < cfg->num_inputs; i++) {
2506 hda_nid_t pin;
2507 const char *label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Takashi Iwai352f7f92012-12-19 12:52:06 +01002509 pin = cfg->inputs[i].pin;
2510 if (!is_input_pin(codec, pin))
2511 continue;
2512
2513 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002514 if (prev_label && !strcmp(label, prev_label))
2515 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002516 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002517 type_idx = 0;
2518 prev_label = label;
2519
Takashi Iwai2c12c302013-01-10 09:33:29 +01002520 val = PIN_IN;
2521 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2522 val |= snd_hda_get_default_vref(codec, pin);
2523 set_pin_target(codec, pin, val, false);
2524
Takashi Iwai352f7f92012-12-19 12:52:06 +01002525 if (mixer) {
2526 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002527 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002528 label, type_idx, mixer);
2529 if (err < 0)
2530 return err;
2531 }
2532 }
2533
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002534 err = parse_capture_source(codec, pin, num_adcs, label, -mixer);
2535 if (err < 0)
2536 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002537
2538 if (spec->add_in_jack_modes) {
2539 err = create_in_jack_mode(codec, pin);
2540 if (err < 0)
2541 return err;
2542 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002543 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002544
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002545 if (mixer && spec->add_stereo_mix_input) {
2546 err = parse_capture_source(codec, mixer, num_adcs,
2547 "Stereo Mix", 0);
2548 if (err < 0)
2549 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002550 }
2551
2552 return 0;
2553}
2554
2555
2556/*
2557 * input source mux
2558 */
2559
Takashi Iwaic697b712013-01-07 17:09:26 +01002560/* get the input path specified by the given adc and imux indices */
2561static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002562{
2563 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002564 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2565 snd_BUG();
2566 return NULL;
2567 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002568 if (spec->dyn_adc_switch)
2569 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssonb56fa1e2013-01-16 11:45:35 +01002570 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_OUTS) {
2571 snd_BUG();
2572 return NULL;
2573 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002574 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002575}
2576
2577static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2578 unsigned int idx);
2579
2580static int mux_enum_info(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_info *uinfo)
2582{
2583 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2584 struct hda_gen_spec *spec = codec->spec;
2585 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2586}
2587
2588static int mux_enum_get(struct snd_kcontrol *kcontrol,
2589 struct snd_ctl_elem_value *ucontrol)
2590{
2591 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2592 struct hda_gen_spec *spec = codec->spec;
David Henningssona053d1e2013-01-16 11:45:36 +01002593 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002594
2595 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2596 return 0;
2597}
2598
2599static int mux_enum_put(struct snd_kcontrol *kcontrol,
2600 struct snd_ctl_elem_value *ucontrol)
2601{
2602 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningssona053d1e2013-01-16 11:45:36 +01002603 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002604 return mux_select(codec, adc_idx,
2605 ucontrol->value.enumerated.item[0]);
2606}
2607
Takashi Iwai352f7f92012-12-19 12:52:06 +01002608static const struct snd_kcontrol_new cap_src_temp = {
2609 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2610 .name = "Input Source",
2611 .info = mux_enum_info,
2612 .get = mux_enum_get,
2613 .put = mux_enum_put,
2614};
2615
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002616/*
2617 * capture volume and capture switch ctls
2618 */
2619
Takashi Iwai352f7f92012-12-19 12:52:06 +01002620typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2621 struct snd_ctl_elem_value *ucontrol);
2622
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002623/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002624static int cap_put_caller(struct snd_kcontrol *kcontrol,
2625 struct snd_ctl_elem_value *ucontrol,
2626 put_call_t func, int type)
2627{
2628 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2629 struct hda_gen_spec *spec = codec->spec;
2630 const struct hda_input_mux *imux;
2631 struct nid_path *path;
2632 int i, adc_idx, err = 0;
2633
2634 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002635 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002636 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002637 /* we use the cache-only update at first since multiple input paths
2638 * may shared the same amp; by updating only caches, the redundant
2639 * writes to hardware can be reduced.
2640 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002641 codec->cached_write = 1;
2642 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002643 path = get_input_path(codec, adc_idx, i);
2644 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002645 continue;
2646 kcontrol->private_value = path->ctls[type];
2647 err = func(kcontrol, ucontrol);
2648 if (err < 0)
2649 goto error;
2650 }
2651 error:
2652 codec->cached_write = 0;
2653 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002654 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002655 if (err >= 0 && spec->cap_sync_hook)
2656 spec->cap_sync_hook(codec);
2657 return err;
2658}
2659
2660/* capture volume ctl callbacks */
2661#define cap_vol_info snd_hda_mixer_amp_volume_info
2662#define cap_vol_get snd_hda_mixer_amp_volume_get
2663#define cap_vol_tlv snd_hda_mixer_amp_tlv
2664
2665static int cap_vol_put(struct snd_kcontrol *kcontrol,
2666 struct snd_ctl_elem_value *ucontrol)
2667{
2668 return cap_put_caller(kcontrol, ucontrol,
2669 snd_hda_mixer_amp_volume_put,
2670 NID_PATH_VOL_CTL);
2671}
2672
2673static const struct snd_kcontrol_new cap_vol_temp = {
2674 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2675 .name = "Capture Volume",
2676 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2677 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2678 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2679 .info = cap_vol_info,
2680 .get = cap_vol_get,
2681 .put = cap_vol_put,
2682 .tlv = { .c = cap_vol_tlv },
2683};
2684
2685/* capture switch ctl callbacks */
2686#define cap_sw_info snd_ctl_boolean_stereo_info
2687#define cap_sw_get snd_hda_mixer_amp_switch_get
2688
2689static int cap_sw_put(struct snd_kcontrol *kcontrol,
2690 struct snd_ctl_elem_value *ucontrol)
2691{
Takashi Iwaiae177c32013-01-14 12:13:06 +01002692 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2693 struct hda_gen_spec *spec = codec->spec;
2694 int ret;
2695
2696 ret = cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002697 snd_hda_mixer_amp_switch_put,
2698 NID_PATH_MUTE_CTL);
Takashi Iwaiae177c32013-01-14 12:13:06 +01002699 if (ret < 0)
2700 return ret;
2701
2702 if (spec->capture_switch_hook) {
2703 bool enable = (ucontrol->value.integer.value[0] ||
2704 ucontrol->value.integer.value[1]);
2705 spec->capture_switch_hook(codec, enable);
2706 }
2707
2708 return ret;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002709}
2710
2711static const struct snd_kcontrol_new cap_sw_temp = {
2712 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2713 .name = "Capture Switch",
2714 .info = cap_sw_info,
2715 .get = cap_sw_get,
2716 .put = cap_sw_put,
2717};
2718
2719static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2720{
2721 hda_nid_t nid;
2722 int i, depth;
2723
2724 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2725 for (depth = 0; depth < 3; depth++) {
2726 if (depth >= path->depth)
2727 return -EINVAL;
2728 i = path->depth - depth - 1;
2729 nid = path->path[i];
2730 if (!path->ctls[NID_PATH_VOL_CTL]) {
2731 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2732 path->ctls[NID_PATH_VOL_CTL] =
2733 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2734 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2735 int idx = path->idx[i];
2736 if (!depth && codec->single_adc_amp)
2737 idx = 0;
2738 path->ctls[NID_PATH_VOL_CTL] =
2739 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2740 }
2741 }
2742 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2743 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2744 path->ctls[NID_PATH_MUTE_CTL] =
2745 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2746 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2747 int idx = path->idx[i];
2748 if (!depth && codec->single_adc_amp)
2749 idx = 0;
2750 path->ctls[NID_PATH_MUTE_CTL] =
2751 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2752 }
2753 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return 0;
2756}
2757
Takashi Iwai352f7f92012-12-19 12:52:06 +01002758static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002760 struct hda_gen_spec *spec = codec->spec;
2761 struct auto_pin_cfg *cfg = &spec->autocfg;
2762 unsigned int val;
2763 int i;
2764
2765 if (!spec->inv_dmic_split)
2766 return false;
2767 for (i = 0; i < cfg->num_inputs; i++) {
2768 if (cfg->inputs[i].pin != nid)
2769 continue;
2770 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2771 return false;
2772 val = snd_hda_codec_get_pincfg(codec, nid);
2773 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2774 }
2775 return false;
2776}
2777
2778static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2779 int idx, bool is_switch, unsigned int ctl,
2780 bool inv_dmic)
2781{
2782 struct hda_gen_spec *spec = codec->spec;
2783 char tmpname[44];
2784 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2785 const char *sfx = is_switch ? "Switch" : "Volume";
2786 unsigned int chs = inv_dmic ? 1 : 3;
2787 int err;
2788
2789 if (!ctl)
2790 return 0;
2791
2792 if (label)
2793 snprintf(tmpname, sizeof(tmpname),
2794 "%s Capture %s", label, sfx);
2795 else
2796 snprintf(tmpname, sizeof(tmpname),
2797 "Capture %s", sfx);
2798 err = add_control(spec, type, tmpname, idx,
2799 amp_val_replace_channels(ctl, chs));
2800 if (err < 0 || !inv_dmic)
2801 return err;
2802
2803 /* Make independent right kcontrol */
2804 if (label)
2805 snprintf(tmpname, sizeof(tmpname),
2806 "Inverted %s Capture %s", label, sfx);
2807 else
2808 snprintf(tmpname, sizeof(tmpname),
2809 "Inverted Capture %s", sfx);
2810 return add_control(spec, type, tmpname, idx,
2811 amp_val_replace_channels(ctl, 2));
2812}
2813
2814/* create single (and simple) capture volume and switch controls */
2815static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2816 unsigned int vol_ctl, unsigned int sw_ctl,
2817 bool inv_dmic)
2818{
2819 int err;
2820 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2821 if (err < 0)
2822 return err;
2823 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2824 if (err < 0)
2825 return err;
2826 return 0;
2827}
2828
2829/* create bound capture volume and switch controls */
2830static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2831 unsigned int vol_ctl, unsigned int sw_ctl)
2832{
2833 struct hda_gen_spec *spec = codec->spec;
2834 struct snd_kcontrol_new *knew;
2835
2836 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002837 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002838 if (!knew)
2839 return -ENOMEM;
2840 knew->index = idx;
2841 knew->private_value = vol_ctl;
2842 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2843 }
2844 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002845 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002846 if (!knew)
2847 return -ENOMEM;
2848 knew->index = idx;
2849 knew->private_value = sw_ctl;
2850 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2851 }
2852 return 0;
2853}
2854
2855/* return the vol ctl when used first in the imux list */
2856static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2857{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002858 struct nid_path *path;
2859 unsigned int ctl;
2860 int i;
2861
Takashi Iwaic697b712013-01-07 17:09:26 +01002862 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002863 if (!path)
2864 return 0;
2865 ctl = path->ctls[type];
2866 if (!ctl)
2867 return 0;
2868 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002869 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002870 if (path && path->ctls[type] == ctl)
2871 return 0;
2872 }
2873 return ctl;
2874}
2875
2876/* create individual capture volume and switch controls per input */
2877static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2878{
2879 struct hda_gen_spec *spec = codec->spec;
2880 struct hda_input_mux *imux = &spec->input_mux;
2881 int i, err, type, type_idx = 0;
2882 const char *prev_label = NULL;
2883
2884 for (i = 0; i < imux->num_items; i++) {
2885 const char *label;
2886 bool inv_dmic;
2887 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2888 if (prev_label && !strcmp(label, prev_label))
2889 type_idx++;
2890 else
2891 type_idx = 0;
2892 prev_label = label;
2893 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2894
2895 for (type = 0; type < 2; type++) {
2896 err = add_single_cap_ctl(codec, label, type_idx, type,
2897 get_first_cap_ctl(codec, i, type),
2898 inv_dmic);
2899 if (err < 0)
2900 return err;
2901 }
2902 }
2903 return 0;
2904}
2905
2906static int create_capture_mixers(struct hda_codec *codec)
2907{
2908 struct hda_gen_spec *spec = codec->spec;
2909 struct hda_input_mux *imux = &spec->input_mux;
2910 int i, n, nums, err;
2911
2912 if (spec->dyn_adc_switch)
2913 nums = 1;
2914 else
2915 nums = spec->num_adc_nids;
2916
2917 if (!spec->auto_mic && imux->num_items > 1) {
2918 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002919 const char *name;
2920 name = nums > 1 ? "Input Source" : "Capture Source";
2921 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002922 if (!knew)
2923 return -ENOMEM;
2924 knew->count = nums;
2925 }
2926
2927 for (n = 0; n < nums; n++) {
2928 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01002929 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002930 bool inv_dmic = false;
2931 int vol, sw;
2932
2933 vol = sw = 0;
2934 for (i = 0; i < imux->num_items; i++) {
2935 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01002936 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002937 if (!path)
2938 continue;
2939 parse_capvol_in_path(codec, path);
2940 if (!vol)
2941 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01002942 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002943 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01002944 if (!same_amp_caps(codec, vol,
2945 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
2946 multi_cap_vol = true;
2947 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002948 if (!sw)
2949 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01002950 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002951 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01002952 if (!same_amp_caps(codec, sw,
2953 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
2954 multi_cap_vol = true;
2955 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002956 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2957 inv_dmic = true;
2958 }
2959
2960 if (!multi)
2961 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2962 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01002963 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002964 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2965 else
2966 err = create_multi_cap_vol_ctl(codec);
2967 if (err < 0)
2968 return err;
2969 }
2970
2971 return 0;
2972}
2973
2974/*
2975 * add mic boosts if needed
2976 */
2977static int parse_mic_boost(struct hda_codec *codec)
2978{
2979 struct hda_gen_spec *spec = codec->spec;
2980 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002981 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002982 int type_idx = 0;
2983 hda_nid_t nid;
2984 const char *prev_label = NULL;
2985
2986 for (i = 0; i < cfg->num_inputs; i++) {
2987 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2988 break;
2989 nid = cfg->inputs[i].pin;
2990 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2991 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01002992 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002993 struct nid_path *path;
2994 unsigned int val;
2995
David Henningsson02aba552013-01-16 15:58:43 +01002996 if (!nid_has_volume(codec, nid, HDA_INPUT))
2997 continue;
2998
Takashi Iwai352f7f92012-12-19 12:52:06 +01002999 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003000 if (prev_label && !strcmp(label, prev_label))
3001 type_idx++;
3002 else
3003 type_idx = 0;
3004 prev_label = label;
3005
3006 snprintf(boost_label, sizeof(boost_label),
3007 "%s Boost Volume", label);
3008 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
3009 err = add_control(spec, HDA_CTL_WIDGET_VOL,
3010 boost_label, type_idx, val);
3011 if (err < 0)
3012 return err;
3013
3014 path = snd_hda_get_nid_path(codec, nid, 0);
3015 if (path)
3016 path->ctls[NID_PATH_BOOST_CTL] = val;
3017 }
3018 }
3019 return 0;
3020}
3021
3022/*
3023 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3024 */
3025static void parse_digital(struct hda_codec *codec)
3026{
3027 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003028 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003029 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003030 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003031
3032 /* support multiple SPDIFs; the secondary is set up as a slave */
3033 nums = 0;
3034 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003035 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003036 dig_nid = look_for_dac(codec, pin, true);
3037 if (!dig_nid)
3038 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003039 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003040 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003041 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003042 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003043 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003044 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003045 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003046 if (!nums) {
3047 spec->multiout.dig_out_nid = dig_nid;
3048 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3049 } else {
3050 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3051 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3052 break;
3053 spec->slave_dig_outs[nums - 1] = dig_nid;
3054 }
3055 nums++;
3056 }
3057
3058 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003059 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003060 dig_nid = codec->start_nid;
3061 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003062 unsigned int wcaps = get_wcaps(codec, dig_nid);
3063 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3064 continue;
3065 if (!(wcaps & AC_WCAP_DIGITAL))
3066 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003067 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003068 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003069 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003070 path->active = true;
3071 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003072 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003073 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003074 break;
3075 }
3076 }
3077 }
3078}
3079
3080
3081/*
3082 * input MUX handling
3083 */
3084
3085static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3086
3087/* select the given imux item; either unmute exclusively or select the route */
3088static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3089 unsigned int idx)
3090{
3091 struct hda_gen_spec *spec = codec->spec;
3092 const struct hda_input_mux *imux;
3093 struct nid_path *path;
3094
3095 imux = &spec->input_mux;
3096 if (!imux->num_items)
3097 return 0;
3098
3099 if (idx >= imux->num_items)
3100 idx = imux->num_items - 1;
3101 if (spec->cur_mux[adc_idx] == idx)
3102 return 0;
3103
Takashi Iwaic697b712013-01-07 17:09:26 +01003104 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003105 if (!path)
3106 return 0;
3107 if (path->active)
3108 snd_hda_activate_path(codec, path, false, false);
3109
3110 spec->cur_mux[adc_idx] = idx;
3111
3112 if (spec->shared_mic_hp)
3113 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3114
3115 if (spec->dyn_adc_switch)
3116 dyn_adc_pcm_resetup(codec, idx);
3117
Takashi Iwaic697b712013-01-07 17:09:26 +01003118 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003119 if (!path)
3120 return 0;
3121 if (path->active)
3122 return 0;
3123 snd_hda_activate_path(codec, path, true, false);
3124 if (spec->cap_sync_hook)
3125 spec->cap_sync_hook(codec);
3126 return 1;
3127}
3128
3129
3130/*
3131 * Jack detections for HP auto-mute and mic-switch
3132 */
3133
3134/* check each pin in the given array; returns true if any of them is plugged */
3135static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3136{
3137 int i, present = 0;
3138
3139 for (i = 0; i < num_pins; i++) {
3140 hda_nid_t nid = pins[i];
3141 if (!nid)
3142 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003143 /* don't detect pins retasked as inputs */
3144 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3145 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003146 present |= snd_hda_jack_detect(codec, nid);
3147 }
3148 return present;
3149}
3150
3151/* standard HP/line-out auto-mute helper */
3152static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003153 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003154{
3155 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003156 int i;
3157
3158 for (i = 0; i < num_pins; i++) {
3159 hda_nid_t nid = pins[i];
3160 unsigned int val;
3161 if (!nid)
3162 break;
3163 /* don't reset VREF value in case it's controlling
3164 * the amp (see alc861_fixup_asus_amp_vref_0f())
3165 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003166 if (spec->keep_vref_in_automute)
3167 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3168 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003169 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003170 if (!mute)
3171 val |= snd_hda_codec_get_pin_target(codec, nid);
3172 /* here we call update_pin_ctl() so that the pinctl is changed
3173 * without changing the pinctl target value;
3174 * the original target value will be still referred at the
3175 * init / resume again
3176 */
3177 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003178 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003179 }
3180}
3181
3182/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003183void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003184{
3185 struct hda_gen_spec *spec = codec->spec;
3186 int on;
3187
3188 /* Control HP pins/amps depending on master_mute state;
3189 * in general, HP pins/amps control should be enabled in all cases,
3190 * but currently set only for master_mute, just to be safe
3191 */
3192 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3193 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003194 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003195
3196 if (!spec->automute_speaker)
3197 on = 0;
3198 else
3199 on = spec->hp_jack_present | spec->line_jack_present;
3200 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003201 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003202 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003203 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003204
3205 /* toggle line-out mutes if needed, too */
3206 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3207 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3208 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3209 return;
3210 if (!spec->automute_lo)
3211 on = 0;
3212 else
3213 on = spec->hp_jack_present;
3214 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003215 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003216 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003217 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003218}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003219EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003220
3221static void call_update_outputs(struct hda_codec *codec)
3222{
3223 struct hda_gen_spec *spec = codec->spec;
3224 if (spec->automute_hook)
3225 spec->automute_hook(codec);
3226 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003227 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003228}
3229
3230/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003231void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003232{
3233 struct hda_gen_spec *spec = codec->spec;
3234
3235 spec->hp_jack_present =
3236 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3237 spec->autocfg.hp_pins);
3238 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3239 return;
3240 call_update_outputs(codec);
3241}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003242EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003243
3244/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003245void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003246{
3247 struct hda_gen_spec *spec = codec->spec;
3248
3249 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3250 return;
3251 /* check LO jack only when it's different from HP */
3252 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3253 return;
3254
3255 spec->line_jack_present =
3256 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3257 spec->autocfg.line_out_pins);
3258 if (!spec->automute_speaker || !spec->detect_lo)
3259 return;
3260 call_update_outputs(codec);
3261}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003262EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003263
3264/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003265void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003266{
3267 struct hda_gen_spec *spec = codec->spec;
3268 int i;
3269
3270 if (!spec->auto_mic)
3271 return;
3272
3273 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003274 hda_nid_t pin = spec->am_entry[i].pin;
3275 /* don't detect pins retasked as outputs */
3276 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3277 continue;
3278 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003279 mux_select(codec, 0, spec->am_entry[i].idx);
3280 return;
3281 }
3282 }
3283 mux_select(codec, 0, spec->am_entry[0].idx);
3284}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003285EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003286
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003287/* update jack retasking */
3288static void update_automute_all(struct hda_codec *codec)
3289{
3290 struct hda_gen_spec *spec = codec->spec;
3291
3292 if (spec->hp_automute_hook)
3293 spec->hp_automute_hook(codec, NULL);
3294 else
3295 snd_hda_gen_hp_automute(codec, NULL);
3296 if (spec->line_automute_hook)
3297 spec->line_automute_hook(codec, NULL);
3298 else
3299 snd_hda_gen_line_automute(codec, NULL);
3300 if (spec->mic_autoswitch_hook)
3301 spec->mic_autoswitch_hook(codec, NULL);
3302 else
3303 snd_hda_gen_mic_autoswitch(codec, NULL);
3304}
3305
Takashi Iwai352f7f92012-12-19 12:52:06 +01003306/*
3307 * Auto-Mute mode mixer enum support
3308 */
3309static int automute_mode_info(struct snd_kcontrol *kcontrol,
3310 struct snd_ctl_elem_info *uinfo)
3311{
3312 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3313 struct hda_gen_spec *spec = codec->spec;
3314 static const char * const texts3[] = {
3315 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003316 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Takashi Iwai352f7f92012-12-19 12:52:06 +01003318 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3319 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3320 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3321}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Takashi Iwai352f7f92012-12-19 12:52:06 +01003323static int automute_mode_get(struct snd_kcontrol *kcontrol,
3324 struct snd_ctl_elem_value *ucontrol)
3325{
3326 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3327 struct hda_gen_spec *spec = codec->spec;
3328 unsigned int val = 0;
3329 if (spec->automute_speaker)
3330 val++;
3331 if (spec->automute_lo)
3332 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003333
Takashi Iwai352f7f92012-12-19 12:52:06 +01003334 ucontrol->value.enumerated.item[0] = val;
3335 return 0;
3336}
3337
3338static int automute_mode_put(struct snd_kcontrol *kcontrol,
3339 struct snd_ctl_elem_value *ucontrol)
3340{
3341 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3342 struct hda_gen_spec *spec = codec->spec;
3343
3344 switch (ucontrol->value.enumerated.item[0]) {
3345 case 0:
3346 if (!spec->automute_speaker && !spec->automute_lo)
3347 return 0;
3348 spec->automute_speaker = 0;
3349 spec->automute_lo = 0;
3350 break;
3351 case 1:
3352 if (spec->automute_speaker_possible) {
3353 if (!spec->automute_lo && spec->automute_speaker)
3354 return 0;
3355 spec->automute_speaker = 1;
3356 spec->automute_lo = 0;
3357 } else if (spec->automute_lo_possible) {
3358 if (spec->automute_lo)
3359 return 0;
3360 spec->automute_lo = 1;
3361 } else
3362 return -EINVAL;
3363 break;
3364 case 2:
3365 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3366 return -EINVAL;
3367 if (spec->automute_speaker && spec->automute_lo)
3368 return 0;
3369 spec->automute_speaker = 1;
3370 spec->automute_lo = 1;
3371 break;
3372 default:
3373 return -EINVAL;
3374 }
3375 call_update_outputs(codec);
3376 return 1;
3377}
3378
3379static const struct snd_kcontrol_new automute_mode_enum = {
3380 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3381 .name = "Auto-Mute Mode",
3382 .info = automute_mode_info,
3383 .get = automute_mode_get,
3384 .put = automute_mode_put,
3385};
3386
3387static int add_automute_mode_enum(struct hda_codec *codec)
3388{
3389 struct hda_gen_spec *spec = codec->spec;
3390
Takashi Iwai12c93df2012-12-19 14:38:33 +01003391 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003392 return -ENOMEM;
3393 return 0;
3394}
3395
3396/*
3397 * Check the availability of HP/line-out auto-mute;
3398 * Set up appropriately if really supported
3399 */
3400static int check_auto_mute_availability(struct hda_codec *codec)
3401{
3402 struct hda_gen_spec *spec = codec->spec;
3403 struct auto_pin_cfg *cfg = &spec->autocfg;
3404 int present = 0;
3405 int i, err;
3406
Takashi Iwaif72706b2013-01-16 18:20:07 +01003407 if (spec->suppress_auto_mute)
3408 return 0;
3409
Takashi Iwai352f7f92012-12-19 12:52:06 +01003410 if (cfg->hp_pins[0])
3411 present++;
3412 if (cfg->line_out_pins[0])
3413 present++;
3414 if (cfg->speaker_pins[0])
3415 present++;
3416 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003417 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003418
3419 if (!cfg->speaker_pins[0] &&
3420 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3421 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3422 sizeof(cfg->speaker_pins));
3423 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Takashi Iwai352f7f92012-12-19 12:52:06 +01003426 if (!cfg->hp_pins[0] &&
3427 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3428 memcpy(cfg->hp_pins, cfg->line_out_pins,
3429 sizeof(cfg->hp_pins));
3430 cfg->hp_outs = cfg->line_outs;
3431 }
3432
3433 for (i = 0; i < cfg->hp_outs; i++) {
3434 hda_nid_t nid = cfg->hp_pins[i];
3435 if (!is_jack_detectable(codec, nid))
3436 continue;
3437 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3438 nid);
3439 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003440 spec->hp_automute_hook ?
3441 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003442 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003443 spec->detect_hp = 1;
3444 }
3445
3446 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3447 if (cfg->speaker_outs)
3448 for (i = 0; i < cfg->line_outs; i++) {
3449 hda_nid_t nid = cfg->line_out_pins[i];
3450 if (!is_jack_detectable(codec, nid))
3451 continue;
3452 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3453 snd_hda_jack_detect_enable_callback(codec, nid,
3454 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003455 spec->line_automute_hook ?
3456 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003457 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003458 spec->detect_lo = 1;
3459 }
3460 spec->automute_lo_possible = spec->detect_hp;
3461 }
3462
3463 spec->automute_speaker_possible = cfg->speaker_outs &&
3464 (spec->detect_hp || spec->detect_lo);
3465
3466 spec->automute_lo = spec->automute_lo_possible;
3467 spec->automute_speaker = spec->automute_speaker_possible;
3468
3469 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3470 /* create a control for automute mode */
3471 err = add_automute_mode_enum(codec);
3472 if (err < 0)
3473 return err;
3474 }
3475 return 0;
3476}
3477
Takashi Iwai352f7f92012-12-19 12:52:06 +01003478/* check whether all auto-mic pins are valid; setup indices if OK */
3479static bool auto_mic_check_imux(struct hda_codec *codec)
3480{
3481 struct hda_gen_spec *spec = codec->spec;
3482 const struct hda_input_mux *imux;
3483 int i;
3484
3485 imux = &spec->input_mux;
3486 for (i = 0; i < spec->am_num_entries; i++) {
3487 spec->am_entry[i].idx =
3488 find_idx_in_nid_list(spec->am_entry[i].pin,
3489 spec->imux_pins, imux->num_items);
3490 if (spec->am_entry[i].idx < 0)
3491 return false; /* no corresponding imux */
3492 }
3493
3494 /* we don't need the jack detection for the first pin */
3495 for (i = 1; i < spec->am_num_entries; i++)
3496 snd_hda_jack_detect_enable_callback(codec,
3497 spec->am_entry[i].pin,
3498 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003499 spec->mic_autoswitch_hook ?
3500 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003501 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003502 return true;
3503}
3504
3505static int compare_attr(const void *ap, const void *bp)
3506{
3507 const struct automic_entry *a = ap;
3508 const struct automic_entry *b = bp;
3509 return (int)(a->attr - b->attr);
3510}
3511
3512/*
3513 * Check the availability of auto-mic switch;
3514 * Set up if really supported
3515 */
3516static int check_auto_mic_availability(struct hda_codec *codec)
3517{
3518 struct hda_gen_spec *spec = codec->spec;
3519 struct auto_pin_cfg *cfg = &spec->autocfg;
3520 unsigned int types;
3521 int i, num_pins;
3522
Takashi Iwaid12daf62013-01-07 16:32:11 +01003523 if (spec->suppress_auto_mic)
3524 return 0;
3525
Takashi Iwai352f7f92012-12-19 12:52:06 +01003526 types = 0;
3527 num_pins = 0;
3528 for (i = 0; i < cfg->num_inputs; i++) {
3529 hda_nid_t nid = cfg->inputs[i].pin;
3530 unsigned int attr;
3531 attr = snd_hda_codec_get_pincfg(codec, nid);
3532 attr = snd_hda_get_input_pin_attr(attr);
3533 if (types & (1 << attr))
3534 return 0; /* already occupied */
3535 switch (attr) {
3536 case INPUT_PIN_ATTR_INT:
3537 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3538 return 0; /* invalid type */
3539 break;
3540 case INPUT_PIN_ATTR_UNUSED:
3541 return 0; /* invalid entry */
3542 default:
3543 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3544 return 0; /* invalid type */
3545 if (!spec->line_in_auto_switch &&
3546 cfg->inputs[i].type != AUTO_PIN_MIC)
3547 return 0; /* only mic is allowed */
3548 if (!is_jack_detectable(codec, nid))
3549 return 0; /* no unsol support */
3550 break;
3551 }
3552 if (num_pins >= MAX_AUTO_MIC_PINS)
3553 return 0;
3554 types |= (1 << attr);
3555 spec->am_entry[num_pins].pin = nid;
3556 spec->am_entry[num_pins].attr = attr;
3557 num_pins++;
3558 }
3559
3560 if (num_pins < 2)
3561 return 0;
3562
3563 spec->am_num_entries = num_pins;
3564 /* sort the am_entry in the order of attr so that the pin with a
3565 * higher attr will be selected when the jack is plugged.
3566 */
3567 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3568 compare_attr, NULL);
3569
3570 if (!auto_mic_check_imux(codec))
3571 return 0;
3572
3573 spec->auto_mic = 1;
3574 spec->num_adc_nids = 1;
3575 spec->cur_mux[0] = spec->am_entry[0].idx;
3576 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3577 spec->am_entry[0].pin,
3578 spec->am_entry[1].pin,
3579 spec->am_entry[2].pin);
3580
3581 return 0;
3582}
3583
3584
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003585/*
3586 * Parse the given BIOS configuration and set up the hda_gen_spec
3587 *
3588 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003589 * or a negative error code
3590 */
3591int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003592 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003593{
3594 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003595 int err;
3596
Takashi Iwai1c70a582013-01-11 17:48:22 +01003597 parse_user_hints(codec);
3598
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003599 if (cfg != &spec->autocfg) {
3600 spec->autocfg = *cfg;
3601 cfg = &spec->autocfg;
3602 }
3603
David Henningsson6fc4cb92013-01-16 15:58:45 +01003604 fill_all_dac_nids(codec);
3605
Takashi Iwai352f7f92012-12-19 12:52:06 +01003606 if (!cfg->line_outs) {
3607 if (cfg->dig_outs || cfg->dig_in_pin) {
3608 spec->multiout.max_channels = 2;
3609 spec->no_analog = 1;
3610 goto dig_only;
3611 }
3612 return 0; /* can't find valid BIOS pin config */
3613 }
3614
3615 if (!spec->no_primary_hp &&
3616 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3617 cfg->line_outs <= cfg->hp_outs) {
3618 /* use HP as primary out */
3619 cfg->speaker_outs = cfg->line_outs;
3620 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3621 sizeof(cfg->speaker_pins));
3622 cfg->line_outs = cfg->hp_outs;
3623 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3624 cfg->hp_outs = 0;
3625 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3626 cfg->line_out_type = AUTO_PIN_HP_OUT;
3627 }
3628
3629 err = parse_output_paths(codec);
3630 if (err < 0)
3631 return err;
3632 err = create_multi_channel_mode(codec);
3633 if (err < 0)
3634 return err;
3635 err = create_multi_out_ctls(codec, cfg);
3636 if (err < 0)
3637 return err;
3638 err = create_hp_out_ctls(codec);
3639 if (err < 0)
3640 return err;
3641 err = create_speaker_out_ctls(codec);
3642 if (err < 0)
3643 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003644 err = create_indep_hp_ctls(codec);
3645 if (err < 0)
3646 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003647 err = create_loopback_mixing_ctl(codec);
3648 if (err < 0)
3649 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003650 err = create_shared_input(codec);
3651 if (err < 0)
3652 return err;
3653 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003654 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003655 return err;
3656
Takashi Iwaia07a9492013-01-07 16:44:06 +01003657 spec->const_channel_count = spec->ext_channel_count;
3658 /* check the multiple speaker and headphone pins */
3659 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3660 spec->const_channel_count = max(spec->const_channel_count,
3661 cfg->speaker_outs * 2);
3662 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3663 spec->const_channel_count = max(spec->const_channel_count,
3664 cfg->hp_outs * 2);
3665 spec->multiout.max_channels = max(spec->ext_channel_count,
3666 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003667
3668 err = check_auto_mute_availability(codec);
3669 if (err < 0)
3670 return err;
3671
3672 err = check_dyn_adc_switch(codec);
3673 if (err < 0)
3674 return err;
3675
3676 if (!spec->shared_mic_hp) {
3677 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003678 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003681
Takashi Iwai352f7f92012-12-19 12:52:06 +01003682 err = create_capture_mixers(codec);
3683 if (err < 0)
3684 return err;
3685
3686 err = parse_mic_boost(codec);
3687 if (err < 0)
3688 return err;
3689
Takashi Iwai978e77e2013-01-10 16:57:58 +01003690 if (spec->add_out_jack_modes) {
3691 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3692 err = create_out_jack_modes(codec, cfg->line_outs,
3693 cfg->line_out_pins);
3694 if (err < 0)
3695 return err;
3696 }
3697 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3698 err = create_out_jack_modes(codec, cfg->hp_outs,
3699 cfg->hp_pins);
3700 if (err < 0)
3701 return err;
3702 }
3703 }
3704
Takashi Iwai352f7f92012-12-19 12:52:06 +01003705 dig_only:
3706 parse_digital(codec);
3707
3708 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003710EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
3712
3713/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003714 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003716
3717/* slave controls for virtual master */
3718static const char * const slave_pfxs[] = {
3719 "Front", "Surround", "Center", "LFE", "Side",
3720 "Headphone", "Speaker", "Mono", "Line Out",
3721 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003722 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3723 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3724 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003725 NULL,
3726};
3727
3728int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003730 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Takashi Iwai36502d02012-12-19 15:15:10 +01003733 if (spec->kctls.used) {
3734 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3735 if (err < 0)
3736 return err;
3737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Takashi Iwai352f7f92012-12-19 12:52:06 +01003739 if (spec->multiout.dig_out_nid) {
3740 err = snd_hda_create_dig_out_ctls(codec,
3741 spec->multiout.dig_out_nid,
3742 spec->multiout.dig_out_nid,
3743 spec->pcm_rec[1].pcm_type);
3744 if (err < 0)
3745 return err;
3746 if (!spec->no_analog) {
3747 err = snd_hda_create_spdif_share_sw(codec,
3748 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 if (err < 0)
3750 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003751 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 }
3753 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003754 if (spec->dig_in_nid) {
3755 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3756 if (err < 0)
3757 return err;
3758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Takashi Iwai352f7f92012-12-19 12:52:06 +01003760 /* if we have no master control, let's create it */
3761 if (!spec->no_analog &&
3762 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003763 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01003764 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003765 "Playback Volume");
3766 if (err < 0)
3767 return err;
3768 }
3769 if (!spec->no_analog &&
3770 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3771 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3772 NULL, slave_pfxs,
3773 "Playback Switch",
3774 true, &spec->vmaster_mute.sw_kctl);
3775 if (err < 0)
3776 return err;
3777 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003778 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3779 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Takashi Iwai352f7f92012-12-19 12:52:06 +01003782 free_kctls(spec); /* no longer needed */
3783
3784 if (spec->shared_mic_hp) {
3785 int err;
3786 int nid = spec->autocfg.inputs[1].pin;
3787 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3788 if (err < 0)
3789 return err;
3790 err = snd_hda_jack_detect_enable(codec, nid, 0);
3791 if (err < 0)
3792 return err;
3793 }
3794
3795 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3796 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 return err;
3798
3799 return 0;
3800}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003801EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3802
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
3804/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003805 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003808static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3809 struct hda_codec *codec,
3810 struct snd_pcm_substream *substream,
3811 int action)
3812{
3813 struct hda_gen_spec *spec = codec->spec;
3814 if (spec->pcm_playback_hook)
3815 spec->pcm_playback_hook(hinfo, codec, substream, action);
3816}
3817
Takashi Iwaiac2e8732013-01-17 15:57:10 +01003818static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
3819 struct hda_codec *codec,
3820 struct snd_pcm_substream *substream,
3821 int action)
3822{
3823 struct hda_gen_spec *spec = codec->spec;
3824 if (spec->pcm_capture_hook)
3825 spec->pcm_capture_hook(hinfo, codec, substream, action);
3826}
3827
Takashi Iwai352f7f92012-12-19 12:52:06 +01003828/*
3829 * Analog playback callbacks
3830 */
3831static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3832 struct hda_codec *codec,
3833 struct snd_pcm_substream *substream)
3834{
3835 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003836 int err;
3837
3838 mutex_lock(&spec->pcm_mutex);
3839 err = snd_hda_multi_out_analog_open(codec,
3840 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003841 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003842 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003843 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003844 call_pcm_playback_hook(hinfo, codec, substream,
3845 HDA_GEN_PCM_ACT_OPEN);
3846 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003847 mutex_unlock(&spec->pcm_mutex);
3848 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003849}
3850
3851static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003852 struct hda_codec *codec,
3853 unsigned int stream_tag,
3854 unsigned int format,
3855 struct snd_pcm_substream *substream)
3856{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003857 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003858 int err;
3859
3860 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3861 stream_tag, format, substream);
3862 if (!err)
3863 call_pcm_playback_hook(hinfo, codec, substream,
3864 HDA_GEN_PCM_ACT_PREPARE);
3865 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003866}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003867
Takashi Iwai352f7f92012-12-19 12:52:06 +01003868static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3869 struct hda_codec *codec,
3870 struct snd_pcm_substream *substream)
3871{
3872 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003873 int err;
3874
3875 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3876 if (!err)
3877 call_pcm_playback_hook(hinfo, codec, substream,
3878 HDA_GEN_PCM_ACT_CLEANUP);
3879 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003880}
3881
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003882static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3883 struct hda_codec *codec,
3884 struct snd_pcm_substream *substream)
3885{
3886 struct hda_gen_spec *spec = codec->spec;
3887 mutex_lock(&spec->pcm_mutex);
3888 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003889 call_pcm_playback_hook(hinfo, codec, substream,
3890 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003891 mutex_unlock(&spec->pcm_mutex);
3892 return 0;
3893}
3894
Takashi Iwaiac2e8732013-01-17 15:57:10 +01003895static int capture_pcm_open(struct hda_pcm_stream *hinfo,
3896 struct hda_codec *codec,
3897 struct snd_pcm_substream *substream)
3898{
3899 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
3900 return 0;
3901}
3902
3903static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3904 struct hda_codec *codec,
3905 unsigned int stream_tag,
3906 unsigned int format,
3907 struct snd_pcm_substream *substream)
3908{
3909 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3910 call_pcm_capture_hook(hinfo, codec, substream,
3911 HDA_GEN_PCM_ACT_PREPARE);
3912 return 0;
3913}
3914
3915static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3916 struct hda_codec *codec,
3917 struct snd_pcm_substream *substream)
3918{
3919 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3920 call_pcm_capture_hook(hinfo, codec, substream,
3921 HDA_GEN_PCM_ACT_CLEANUP);
3922 return 0;
3923}
3924
3925static int capture_pcm_close(struct hda_pcm_stream *hinfo,
3926 struct hda_codec *codec,
3927 struct snd_pcm_substream *substream)
3928{
3929 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
3930 return 0;
3931}
3932
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003933static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3934 struct hda_codec *codec,
3935 struct snd_pcm_substream *substream)
3936{
3937 struct hda_gen_spec *spec = codec->spec;
3938 int err = 0;
3939
3940 mutex_lock(&spec->pcm_mutex);
3941 if (!spec->indep_hp_enabled)
3942 err = -EBUSY;
3943 else
3944 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003945 call_pcm_playback_hook(hinfo, codec, substream,
3946 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003947 mutex_unlock(&spec->pcm_mutex);
3948 return err;
3949}
3950
3951static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3952 struct hda_codec *codec,
3953 struct snd_pcm_substream *substream)
3954{
3955 struct hda_gen_spec *spec = codec->spec;
3956 mutex_lock(&spec->pcm_mutex);
3957 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003958 call_pcm_playback_hook(hinfo, codec, substream,
3959 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003960 mutex_unlock(&spec->pcm_mutex);
3961 return 0;
3962}
3963
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003964static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3965 struct hda_codec *codec,
3966 unsigned int stream_tag,
3967 unsigned int format,
3968 struct snd_pcm_substream *substream)
3969{
3970 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3971 call_pcm_playback_hook(hinfo, codec, substream,
3972 HDA_GEN_PCM_ACT_PREPARE);
3973 return 0;
3974}
3975
3976static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3977 struct hda_codec *codec,
3978 struct snd_pcm_substream *substream)
3979{
3980 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3981 call_pcm_playback_hook(hinfo, codec, substream,
3982 HDA_GEN_PCM_ACT_CLEANUP);
3983 return 0;
3984}
3985
Takashi Iwai352f7f92012-12-19 12:52:06 +01003986/*
3987 * Digital out
3988 */
3989static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3990 struct hda_codec *codec,
3991 struct snd_pcm_substream *substream)
3992{
3993 struct hda_gen_spec *spec = codec->spec;
3994 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3995}
3996
3997static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3998 struct hda_codec *codec,
3999 unsigned int stream_tag,
4000 unsigned int format,
4001 struct snd_pcm_substream *substream)
4002{
4003 struct hda_gen_spec *spec = codec->spec;
4004 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4005 stream_tag, format, substream);
4006}
4007
4008static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4009 struct hda_codec *codec,
4010 struct snd_pcm_substream *substream)
4011{
4012 struct hda_gen_spec *spec = codec->spec;
4013 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4014}
4015
4016static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4017 struct hda_codec *codec,
4018 struct snd_pcm_substream *substream)
4019{
4020 struct hda_gen_spec *spec = codec->spec;
4021 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4022}
4023
4024/*
4025 * Analog capture
4026 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004027#define alt_capture_pcm_open capture_pcm_open
4028#define alt_capture_pcm_close capture_pcm_close
4029
Takashi Iwai352f7f92012-12-19 12:52:06 +01004030static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4031 struct hda_codec *codec,
4032 unsigned int stream_tag,
4033 unsigned int format,
4034 struct snd_pcm_substream *substream)
4035{
4036 struct hda_gen_spec *spec = codec->spec;
4037
4038 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004039 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004040 call_pcm_capture_hook(hinfo, codec, substream,
4041 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004042 return 0;
4043}
4044
Takashi Iwai352f7f92012-12-19 12:52:06 +01004045static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4046 struct hda_codec *codec,
4047 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004048{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004049 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004050
Takashi Iwai352f7f92012-12-19 12:52:06 +01004051 snd_hda_codec_cleanup_stream(codec,
4052 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004053 call_pcm_capture_hook(hinfo, codec, substream,
4054 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004055 return 0;
4056}
4057
Takashi Iwai352f7f92012-12-19 12:52:06 +01004058/*
4059 */
4060static const struct hda_pcm_stream pcm_analog_playback = {
4061 .substreams = 1,
4062 .channels_min = 2,
4063 .channels_max = 8,
4064 /* NID is set in build_pcms */
4065 .ops = {
4066 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004067 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004068 .prepare = playback_pcm_prepare,
4069 .cleanup = playback_pcm_cleanup
4070 },
4071};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
Takashi Iwai352f7f92012-12-19 12:52:06 +01004073static const struct hda_pcm_stream pcm_analog_capture = {
4074 .substreams = 1,
4075 .channels_min = 2,
4076 .channels_max = 2,
4077 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004078 .ops = {
4079 .open = capture_pcm_open,
4080 .close = capture_pcm_close,
4081 .prepare = capture_pcm_prepare,
4082 .cleanup = capture_pcm_cleanup
4083 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004084};
4085
4086static const struct hda_pcm_stream pcm_analog_alt_playback = {
4087 .substreams = 1,
4088 .channels_min = 2,
4089 .channels_max = 2,
4090 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004091 .ops = {
4092 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004093 .close = alt_playback_pcm_close,
4094 .prepare = alt_playback_pcm_prepare,
4095 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004096 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004097};
4098
4099static const struct hda_pcm_stream pcm_analog_alt_capture = {
4100 .substreams = 2, /* can be overridden */
4101 .channels_min = 2,
4102 .channels_max = 2,
4103 /* NID is set in build_pcms */
4104 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004105 .open = alt_capture_pcm_open,
4106 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004107 .prepare = alt_capture_pcm_prepare,
4108 .cleanup = alt_capture_pcm_cleanup
4109 },
4110};
4111
4112static const struct hda_pcm_stream pcm_digital_playback = {
4113 .substreams = 1,
4114 .channels_min = 2,
4115 .channels_max = 2,
4116 /* NID is set in build_pcms */
4117 .ops = {
4118 .open = dig_playback_pcm_open,
4119 .close = dig_playback_pcm_close,
4120 .prepare = dig_playback_pcm_prepare,
4121 .cleanup = dig_playback_pcm_cleanup
4122 },
4123};
4124
4125static const struct hda_pcm_stream pcm_digital_capture = {
4126 .substreams = 1,
4127 .channels_min = 2,
4128 .channels_max = 2,
4129 /* NID is set in build_pcms */
4130};
4131
4132/* Used by build_pcms to flag that a PCM has no playback stream */
4133static const struct hda_pcm_stream pcm_null_stream = {
4134 .substreams = 0,
4135 .channels_min = 0,
4136 .channels_max = 0,
4137};
4138
4139/*
4140 * dynamic changing ADC PCM streams
4141 */
4142static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4143{
4144 struct hda_gen_spec *spec = codec->spec;
4145 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4146
4147 if (spec->cur_adc && spec->cur_adc != new_adc) {
4148 /* stream is running, let's swap the current ADC */
4149 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4150 spec->cur_adc = new_adc;
4151 snd_hda_codec_setup_stream(codec, new_adc,
4152 spec->cur_adc_stream_tag, 0,
4153 spec->cur_adc_format);
4154 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004156 return false;
4157}
4158
4159/* analog capture with dynamic dual-adc changes */
4160static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4161 struct hda_codec *codec,
4162 unsigned int stream_tag,
4163 unsigned int format,
4164 struct snd_pcm_substream *substream)
4165{
4166 struct hda_gen_spec *spec = codec->spec;
4167 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4168 spec->cur_adc_stream_tag = stream_tag;
4169 spec->cur_adc_format = format;
4170 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4171 return 0;
4172}
4173
4174static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4175 struct hda_codec *codec,
4176 struct snd_pcm_substream *substream)
4177{
4178 struct hda_gen_spec *spec = codec->spec;
4179 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4180 spec->cur_adc = 0;
4181 return 0;
4182}
4183
4184static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4185 .substreams = 1,
4186 .channels_min = 2,
4187 .channels_max = 2,
4188 .nid = 0, /* fill later */
4189 .ops = {
4190 .prepare = dyn_adc_capture_pcm_prepare,
4191 .cleanup = dyn_adc_capture_pcm_cleanup
4192 },
4193};
4194
Takashi Iwaif873e532012-12-20 16:58:39 +01004195static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4196 const char *chip_name)
4197{
4198 char *p;
4199
4200 if (*str)
4201 return;
4202 strlcpy(str, chip_name, len);
4203
4204 /* drop non-alnum chars after a space */
4205 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4206 if (!isalnum(p[1])) {
4207 *p = 0;
4208 break;
4209 }
4210 }
4211 strlcat(str, sfx, len);
4212}
4213
Takashi Iwai352f7f92012-12-19 12:52:06 +01004214/* build PCM streams based on the parsed results */
4215int snd_hda_gen_build_pcms(struct hda_codec *codec)
4216{
4217 struct hda_gen_spec *spec = codec->spec;
4218 struct hda_pcm *info = spec->pcm_rec;
4219 const struct hda_pcm_stream *p;
4220 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
4222 codec->num_pcms = 1;
4223 codec->pcm_info = info;
4224
Takashi Iwai352f7f92012-12-19 12:52:06 +01004225 if (spec->no_analog)
4226 goto skip_analog;
4227
Takashi Iwaif873e532012-12-20 16:58:39 +01004228 fill_pcm_stream_name(spec->stream_name_analog,
4229 sizeof(spec->stream_name_analog),
4230 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004231 info->name = spec->stream_name_analog;
4232
4233 if (spec->multiout.num_dacs > 0) {
4234 p = spec->stream_analog_playback;
4235 if (!p)
4236 p = &pcm_analog_playback;
4237 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4238 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4239 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4240 spec->multiout.max_channels;
4241 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4242 spec->autocfg.line_outs == 2)
4243 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4244 snd_pcm_2_1_chmaps;
4245 }
4246 if (spec->num_adc_nids) {
4247 p = spec->stream_analog_capture;
4248 if (!p) {
4249 if (spec->dyn_adc_switch)
4250 p = &dyn_adc_pcm_analog_capture;
4251 else
4252 p = &pcm_analog_capture;
4253 }
4254 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4255 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4256 }
4257
Takashi Iwai352f7f92012-12-19 12:52:06 +01004258 skip_analog:
4259 /* SPDIF for stream index #1 */
4260 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004261 fill_pcm_stream_name(spec->stream_name_digital,
4262 sizeof(spec->stream_name_digital),
4263 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004264 codec->num_pcms = 2;
4265 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4266 info = spec->pcm_rec + 1;
4267 info->name = spec->stream_name_digital;
4268 if (spec->dig_out_type)
4269 info->pcm_type = spec->dig_out_type;
4270 else
4271 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4272 if (spec->multiout.dig_out_nid) {
4273 p = spec->stream_digital_playback;
4274 if (!p)
4275 p = &pcm_digital_playback;
4276 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4277 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4278 }
4279 if (spec->dig_in_nid) {
4280 p = spec->stream_digital_capture;
4281 if (!p)
4282 p = &pcm_digital_capture;
4283 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4284 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4285 }
4286 }
4287
4288 if (spec->no_analog)
4289 return 0;
4290
4291 /* If the use of more than one ADC is requested for the current
4292 * model, configure a second analog capture-only PCM.
4293 */
4294 have_multi_adcs = (spec->num_adc_nids > 1) &&
4295 !spec->dyn_adc_switch && !spec->auto_mic;
4296 /* Additional Analaog capture for index #2 */
4297 if (spec->alt_dac_nid || have_multi_adcs) {
4298 codec->num_pcms = 3;
4299 info = spec->pcm_rec + 2;
4300 info->name = spec->stream_name_analog;
4301 if (spec->alt_dac_nid) {
4302 p = spec->stream_analog_alt_playback;
4303 if (!p)
4304 p = &pcm_analog_alt_playback;
4305 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4306 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4307 spec->alt_dac_nid;
4308 } else {
4309 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4310 pcm_null_stream;
4311 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4312 }
4313 if (have_multi_adcs) {
4314 p = spec->stream_analog_alt_capture;
4315 if (!p)
4316 p = &pcm_analog_alt_capture;
4317 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4318 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4319 spec->adc_nids[1];
4320 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4321 spec->num_adc_nids - 1;
4322 } else {
4323 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4324 pcm_null_stream;
4325 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 }
4328
4329 return 0;
4330}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004331EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4332
4333
4334/*
4335 * Standard auto-parser initializations
4336 */
4337
Takashi Iwaid4156932013-01-07 10:08:02 +01004338/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004339static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004340{
4341 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004342 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004343
Takashi Iwai196c17662013-01-04 15:01:40 +01004344 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004345 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004346 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004347 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004348 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004349 snd_hda_activate_path(codec, path, path->active, true);
4350 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004351}
4352
4353/* initialize primary output paths */
4354static void init_multi_out(struct hda_codec *codec)
4355{
4356 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004357 int i;
4358
Takashi Iwaid4156932013-01-07 10:08:02 +01004359 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004360 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004361}
4362
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004363
Takashi Iwai2c12c302013-01-10 09:33:29 +01004364static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004365{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004366 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004367
Takashi Iwaid4156932013-01-07 10:08:02 +01004368 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004369 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004370}
4371
4372/* initialize hp and speaker paths */
4373static void init_extra_out(struct hda_codec *codec)
4374{
4375 struct hda_gen_spec *spec = codec->spec;
4376
4377 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004378 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004379 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4380 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004381 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004382}
4383
4384/* initialize multi-io paths */
4385static void init_multi_io(struct hda_codec *codec)
4386{
4387 struct hda_gen_spec *spec = codec->spec;
4388 int i;
4389
4390 for (i = 0; i < spec->multi_ios; i++) {
4391 hda_nid_t pin = spec->multi_io[i].pin;
4392 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004393 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004394 if (!path)
4395 continue;
4396 if (!spec->multi_io[i].ctl_in)
4397 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004398 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004399 snd_hda_activate_path(codec, path, path->active, true);
4400 }
4401}
4402
Takashi Iwai352f7f92012-12-19 12:52:06 +01004403/* set up input pins and loopback paths */
4404static void init_analog_input(struct hda_codec *codec)
4405{
4406 struct hda_gen_spec *spec = codec->spec;
4407 struct auto_pin_cfg *cfg = &spec->autocfg;
4408 int i;
4409
4410 for (i = 0; i < cfg->num_inputs; i++) {
4411 hda_nid_t nid = cfg->inputs[i].pin;
4412 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004413 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004414
4415 /* init loopback inputs */
4416 if (spec->mixer_nid) {
4417 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004418 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004419 if (path)
4420 snd_hda_activate_path(codec, path,
4421 path->active, false);
4422 }
4423 }
4424}
4425
4426/* initialize ADC paths */
4427static void init_input_src(struct hda_codec *codec)
4428{
4429 struct hda_gen_spec *spec = codec->spec;
4430 struct hda_input_mux *imux = &spec->input_mux;
4431 struct nid_path *path;
4432 int i, c, nums;
4433
4434 if (spec->dyn_adc_switch)
4435 nums = 1;
4436 else
4437 nums = spec->num_adc_nids;
4438
4439 for (c = 0; c < nums; c++) {
4440 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004441 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004442 if (path) {
4443 bool active = path->active;
4444 if (i == spec->cur_mux[c])
4445 active = true;
4446 snd_hda_activate_path(codec, path, active, false);
4447 }
4448 }
4449 }
4450
4451 if (spec->shared_mic_hp)
4452 update_shared_mic_hp(codec, spec->cur_mux[0]);
4453
4454 if (spec->cap_sync_hook)
4455 spec->cap_sync_hook(codec);
4456}
4457
4458/* set right pin controls for digital I/O */
4459static void init_digital(struct hda_codec *codec)
4460{
4461 struct hda_gen_spec *spec = codec->spec;
4462 int i;
4463 hda_nid_t pin;
4464
Takashi Iwaid4156932013-01-07 10:08:02 +01004465 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004466 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004467 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004468 if (pin) {
4469 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004470 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004471 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4472 if (path)
4473 snd_hda_activate_path(codec, path, path->active, false);
4474 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004475}
4476
Takashi Iwai973e4972012-12-20 15:16:09 +01004477/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4478 * invalid unsol tags by some reason
4479 */
4480static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4481{
4482 int i;
4483
4484 for (i = 0; i < codec->init_pins.used; i++) {
4485 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4486 hda_nid_t nid = pin->nid;
4487 if (is_jack_detectable(codec, nid) &&
4488 !snd_hda_jack_tbl_get(codec, nid))
4489 snd_hda_codec_update_cache(codec, nid, 0,
4490 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4491 }
4492}
4493
Takashi Iwai5187ac12013-01-07 12:52:16 +01004494/*
4495 * initialize the generic spec;
4496 * this can be put as patch_ops.init function
4497 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004498int snd_hda_gen_init(struct hda_codec *codec)
4499{
4500 struct hda_gen_spec *spec = codec->spec;
4501
4502 if (spec->init_hook)
4503 spec->init_hook(codec);
4504
4505 snd_hda_apply_verbs(codec);
4506
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004507 codec->cached_write = 1;
4508
Takashi Iwai352f7f92012-12-19 12:52:06 +01004509 init_multi_out(codec);
4510 init_extra_out(codec);
4511 init_multi_io(codec);
4512 init_analog_input(codec);
4513 init_input_src(codec);
4514 init_digital(codec);
4515
Takashi Iwai973e4972012-12-20 15:16:09 +01004516 clear_unsol_on_unused_pins(codec);
4517
Takashi Iwai352f7f92012-12-19 12:52:06 +01004518 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004519 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004520
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004521 snd_hda_codec_flush_amp_cache(codec);
4522 snd_hda_codec_flush_cmd_cache(codec);
4523
Takashi Iwai352f7f92012-12-19 12:52:06 +01004524 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4525 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4526
4527 hda_call_check_power_status(codec, 0x01);
4528 return 0;
4529}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004530EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4531
Takashi Iwai5187ac12013-01-07 12:52:16 +01004532/*
4533 * free the generic spec;
4534 * this can be put as patch_ops.free function
4535 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004536void snd_hda_gen_free(struct hda_codec *codec)
4537{
4538 snd_hda_gen_spec_free(codec->spec);
4539 kfree(codec->spec);
4540 codec->spec = NULL;
4541}
4542EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4543
4544#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004545/*
4546 * check the loopback power save state;
4547 * this can be put as patch_ops.check_power_status function
4548 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004549int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4550{
4551 struct hda_gen_spec *spec = codec->spec;
4552 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4553}
4554EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4555#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004556
4557
4558/*
4559 * the generic codec support
4560 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
Takashi Iwai352f7f92012-12-19 12:52:06 +01004562static const struct hda_codec_ops generic_patch_ops = {
4563 .build_controls = snd_hda_gen_build_controls,
4564 .build_pcms = snd_hda_gen_build_pcms,
4565 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004566 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004567 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004568#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004569 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004570#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571};
4572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573int snd_hda_parse_generic_codec(struct hda_codec *codec)
4574{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004575 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 int err;
4577
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004578 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004579 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004581 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004584 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4585 if (err < 0)
4586 return err;
4587
4588 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004589 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 goto error;
4591
4592 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 return 0;
4594
Takashi Iwai352f7f92012-12-19 12:52:06 +01004595error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004596 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 return err;
4598}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004599EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);