blob: aae6b1023d693c784b09ae498aa58b0dfd9d16bb [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 Iwai55196ff2013-01-24 17:32:56 +010027#include <linux/delay.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010028#include <linux/ctype.h>
29#include <linux/string.h>
Takashi Iwai294765582013-01-17 09:52:11 +010030#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_codec.h"
34#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010035#include "hda_auto_parser.h"
36#include "hda_jack.h"
37#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwai352f7f92012-12-19 12:52:06 +010040/* initialize hda_gen_spec struct */
41int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Takashi Iwai352f7f92012-12-19 12:52:06 +010043 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010044 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010045 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010046 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010047 return 0;
48}
49EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Takashi Iwai12c93df2012-12-19 14:38:33 +010051struct snd_kcontrol_new *
52snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
53 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010054{
55 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
56 if (!knew)
57 return NULL;
58 *knew = *temp;
59 if (name)
60 knew->name = kstrdup(name, GFP_KERNEL);
61 else if (knew->name)
62 knew->name = kstrdup(knew->name, GFP_KERNEL);
63 if (!knew->name)
64 return NULL;
65 return knew;
66}
Takashi Iwai12c93df2012-12-19 14:38:33 +010067EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010068
69static void free_kctls(struct hda_gen_spec *spec)
70{
71 if (spec->kctls.list) {
72 struct snd_kcontrol_new *kctl = spec->kctls.list;
73 int i;
74 for (i = 0; i < spec->kctls.used; i++)
75 kfree(kctl[i].name);
76 }
77 snd_array_free(&spec->kctls);
78}
79
Takashi Iwai352f7f92012-12-19 12:52:06 +010080void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
81{
82 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010084 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010085 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010086 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
Takashi Iwai352f7f92012-12-19 12:52:06 +010088EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010091 * store user hints
92 */
93static void parse_user_hints(struct hda_codec *codec)
94{
95 struct hda_gen_spec *spec = codec->spec;
96 int val;
97
98 val = snd_hda_get_bool_hint(codec, "jack_detect");
99 if (val >= 0)
100 codec->no_jack_detect = !val;
101 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
102 if (val >= 0)
103 codec->inv_jack_detect = !!val;
104 val = snd_hda_get_bool_hint(codec, "trigger_sense");
105 if (val >= 0)
106 codec->no_trigger_sense = !val;
107 val = snd_hda_get_bool_hint(codec, "inv_eapd");
108 if (val >= 0)
109 codec->inv_eapd = !!val;
110 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
111 if (val >= 0)
112 codec->pcm_format_first = !!val;
113 val = snd_hda_get_bool_hint(codec, "sticky_stream");
114 if (val >= 0)
115 codec->no_sticky_stream = !val;
116 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
117 if (val >= 0)
118 codec->spdif_status_reset = !!val;
119 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
120 if (val >= 0)
121 codec->pin_amp_workaround = !!val;
122 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
123 if (val >= 0)
124 codec->single_adc_amp = !!val;
125
Takashi Iwaif72706b2013-01-16 18:20:07 +0100126 val = snd_hda_get_bool_hint(codec, "auto_mute");
127 if (val >= 0)
128 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100129 val = snd_hda_get_bool_hint(codec, "auto_mic");
130 if (val >= 0)
131 spec->suppress_auto_mic = !val;
132 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
133 if (val >= 0)
134 spec->line_in_auto_switch = !!val;
135 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
136 if (val >= 0)
137 spec->need_dac_fix = !!val;
138 val = snd_hda_get_bool_hint(codec, "primary_hp");
139 if (val >= 0)
140 spec->no_primary_hp = !val;
141 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
142 if (val >= 0)
143 spec->multi_cap_vol = !!val;
144 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
145 if (val >= 0)
146 spec->inv_dmic_split = !!val;
147 val = snd_hda_get_bool_hint(codec, "indep_hp");
148 if (val >= 0)
149 spec->indep_hp = !!val;
150 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
151 if (val >= 0)
152 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100153 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100154 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
155 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100156 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100157 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
158 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100159 spec->add_jack_modes = !!val;
160 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
161 if (val >= 0)
162 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100163 val = snd_hda_get_bool_hint(codec, "power_down_unused");
164 if (val >= 0)
165 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100166 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
167 if (val >= 0)
168 spec->hp_mic = !!val;
169 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
170 if (val >= 0)
171 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100172
173 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
174 spec->mixer_nid = val;
175}
176
177/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100178 * pin control value accesses
179 */
180
181#define update_pin_ctl(codec, pin, val) \
182 snd_hda_codec_update_cache(codec, pin, 0, \
183 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
184
185/* restore the pinctl based on the cached value */
186static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
187{
188 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
189}
190
191/* set the pinctl target value and write it if requested */
192static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
193 unsigned int val, bool do_write)
194{
195 if (!pin)
196 return;
197 val = snd_hda_correct_pin_ctl(codec, pin, val);
198 snd_hda_codec_set_pin_target(codec, pin, val);
199 if (do_write)
200 update_pin_ctl(codec, pin, val);
201}
202
203/* set pinctl target values for all given pins */
204static void set_pin_targets(struct hda_codec *codec, int num_pins,
205 hda_nid_t *pins, unsigned int val)
206{
207 int i;
208 for (i = 0; i < num_pins; i++)
209 set_pin_target(codec, pins[i], val, false);
210}
211
212/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100213 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100216/* return the position of NID in the list, or -1 if not found */
217static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
218{
219 int i;
220 for (i = 0; i < nums; i++)
221 if (list[i] == nid)
222 return i;
223 return -1;
224}
225
226/* return true if the given NID is contained in the path */
227static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
228{
229 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
230}
231
Takashi Iwaif5172a72013-01-04 13:19:55 +0100232static struct nid_path *get_nid_path(struct hda_codec *codec,
233 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100234 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100236 struct hda_gen_spec *spec = codec->spec;
237 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Takashi Iwai352f7f92012-12-19 12:52:06 +0100239 for (i = 0; i < spec->paths.used; i++) {
240 struct nid_path *path = snd_array_elem(&spec->paths, i);
241 if (path->depth <= 0)
242 continue;
243 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100244 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100245 if (!anchor_nid ||
246 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
247 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100248 return path;
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251 return NULL;
252}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100253
254/* get the path between the given NIDs;
255 * passing 0 to either @pin or @dac behaves as a wildcard
256 */
257struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
258 hda_nid_t from_nid, hda_nid_t to_nid)
259{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100260 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100261}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100262EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
263
Takashi Iwai196c17662013-01-04 15:01:40 +0100264/* get the index number corresponding to the path instance;
265 * the index starts from 1, for easier checking the invalid value
266 */
267int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
268{
269 struct hda_gen_spec *spec = codec->spec;
270 struct nid_path *array = spec->paths.list;
271 ssize_t idx;
272
273 if (!spec->paths.used)
274 return 0;
275 idx = path - array;
276 if (idx < 0 || idx >= spec->paths.used)
277 return 0;
278 return idx + 1;
279}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100280EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100281
282/* get the path instance corresponding to the given index number */
283struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
284{
285 struct hda_gen_spec *spec = codec->spec;
286
287 if (idx <= 0 || idx > spec->paths.used)
288 return NULL;
289 return snd_array_elem(&spec->paths, idx - 1);
290}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100291EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100292
Takashi Iwai352f7f92012-12-19 12:52:06 +0100293/* check whether the given DAC is already found in any existing paths */
294static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
295{
296 struct hda_gen_spec *spec = codec->spec;
297 int i;
298
299 for (i = 0; i < spec->paths.used; i++) {
300 struct nid_path *path = snd_array_elem(&spec->paths, i);
301 if (path->path[0] == nid)
302 return true;
303 }
304 return false;
305}
306
307/* check whether the given two widgets can be connected */
308static bool is_reachable_path(struct hda_codec *codec,
309 hda_nid_t from_nid, hda_nid_t to_nid)
310{
311 if (!from_nid || !to_nid)
312 return false;
313 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
314}
315
316/* nid, dir and idx */
317#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
318
319/* check whether the given ctl is already assigned in any path elements */
320static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
321{
322 struct hda_gen_spec *spec = codec->spec;
323 int i;
324
325 val &= AMP_VAL_COMPARE_MASK;
326 for (i = 0; i < spec->paths.used; i++) {
327 struct nid_path *path = snd_array_elem(&spec->paths, i);
328 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
329 return true;
330 }
331 return false;
332}
333
334/* check whether a control with the given (nid, dir, idx) was assigned */
335static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100336 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100337{
338 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100339 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100340}
341
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100342static void print_nid_path(const char *pfx, struct nid_path *path)
343{
344 char buf[40];
345 int i;
346
347
348 buf[0] = 0;
349 for (i = 0; i < path->depth; i++) {
350 char tmp[4];
351 sprintf(tmp, ":%02x", path->path[i]);
352 strlcat(buf, tmp, sizeof(buf));
353 }
354 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
355}
356
Takashi Iwai352f7f92012-12-19 12:52:06 +0100357/* called recursively */
358static bool __parse_nid_path(struct hda_codec *codec,
359 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100360 int anchor_nid, struct nid_path *path,
361 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100362{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100363 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100364 int i, nums;
365
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100366 if (to_nid == anchor_nid)
367 anchor_nid = 0; /* anchor passed */
368 else if (to_nid == (hda_nid_t)(-anchor_nid))
369 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100370
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100371 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100372 for (i = 0; i < nums; i++) {
373 if (conn[i] != from_nid) {
374 /* special case: when from_nid is 0,
375 * try to find an empty DAC
376 */
377 if (from_nid ||
378 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
379 is_dac_already_used(codec, conn[i]))
380 continue;
381 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100382 /* anchor is not requested or already passed? */
383 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100384 goto found;
385 }
386 if (depth >= MAX_NID_PATH_DEPTH)
387 return false;
388 for (i = 0; i < nums; i++) {
389 unsigned int type;
390 type = get_wcaps_type(get_wcaps(codec, conn[i]));
391 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
392 type == AC_WID_PIN)
393 continue;
394 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100395 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100396 goto found;
397 }
398 return false;
399
400 found:
401 path->path[path->depth] = conn[i];
402 path->idx[path->depth + 1] = i;
403 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
404 path->multi[path->depth + 1] = 1;
405 path->depth++;
406 return true;
407}
408
409/* parse the widget path from the given nid to the target nid;
410 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100411 * when @anchor_nid is set to a positive value, only paths through the widget
412 * with the given value are evaluated.
413 * when @anchor_nid is set to a negative value, paths through the widget
414 * with the negative of given value are excluded, only other paths are chosen.
415 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100416 */
417bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100418 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100419 struct nid_path *path)
420{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100421 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100422 path->path[path->depth] = to_nid;
423 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100424 return true;
425 }
426 return false;
427}
428EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100431 * parse the path between the given NIDs and add to the path list.
432 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100434struct nid_path *
435snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100436 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100438 struct hda_gen_spec *spec = codec->spec;
439 struct nid_path *path;
440
441 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
442 return NULL;
443
Takashi Iwaif5172a72013-01-04 13:19:55 +0100444 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100445 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100446 if (path)
447 return path;
448
Takashi Iwai352f7f92012-12-19 12:52:06 +0100449 path = snd_array_new(&spec->paths);
450 if (!path)
451 return NULL;
452 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100453 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100454 return path;
455 /* push back */
456 spec->paths.used--;
457 return NULL;
458}
459EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
460
Takashi Iwai980428c2013-01-09 09:28:20 +0100461/* clear the given path as invalid so that it won't be picked up later */
462static void invalidate_nid_path(struct hda_codec *codec, int idx)
463{
464 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
465 if (!path)
466 return;
467 memset(path, 0, sizeof(*path));
468}
469
Takashi Iwai352f7f92012-12-19 12:52:06 +0100470/* look for an empty DAC slot */
471static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
472 bool is_digital)
473{
474 struct hda_gen_spec *spec = codec->spec;
475 bool cap_digital;
476 int i;
477
478 for (i = 0; i < spec->num_all_dacs; i++) {
479 hda_nid_t nid = spec->all_dacs[i];
480 if (!nid || is_dac_already_used(codec, nid))
481 continue;
482 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
483 if (is_digital != cap_digital)
484 continue;
485 if (is_reachable_path(codec, nid, pin))
486 return nid;
487 }
488 return 0;
489}
490
491/* replace the channels in the composed amp value with the given number */
492static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
493{
494 val &= ~(0x3U << 16);
495 val |= chs << 16;
496 return val;
497}
498
499/* check whether the widget has the given amp capability for the direction */
500static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
501 int dir, unsigned int bits)
502{
503 if (!nid)
504 return false;
505 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
506 if (query_amp_caps(codec, nid, dir) & bits)
507 return true;
508 return false;
509}
510
David Henningsson99a55922013-01-16 15:58:44 +0100511static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
512 hda_nid_t nid2, int dir)
513{
514 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
515 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
516 return (query_amp_caps(codec, nid1, dir) ==
517 query_amp_caps(codec, nid2, dir));
518}
519
Takashi Iwai352f7f92012-12-19 12:52:06 +0100520#define nid_has_mute(codec, nid, dir) \
521 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
522#define nid_has_volume(codec, nid, dir) \
523 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
524
525/* look for a widget suitable for assigning a mute switch in the path */
526static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
527 struct nid_path *path)
528{
529 int i;
530
531 for (i = path->depth - 1; i >= 0; i--) {
532 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
533 return path->path[i];
534 if (i != path->depth - 1 && i != 0 &&
535 nid_has_mute(codec, path->path[i], HDA_INPUT))
536 return path->path[i];
537 }
538 return 0;
539}
540
541/* look for a widget suitable for assigning a volume ctl in the path */
542static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
543 struct nid_path *path)
544{
545 int i;
546
547 for (i = path->depth - 1; i >= 0; i--) {
548 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
549 return path->path[i];
550 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100555 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100557
558/* can have the amp-in capability? */
559static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100561 hda_nid_t nid = path->path[idx];
562 unsigned int caps = get_wcaps(codec, nid);
563 unsigned int type = get_wcaps_type(caps);
564
565 if (!(caps & AC_WCAP_IN_AMP))
566 return false;
567 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
568 return false;
569 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Takashi Iwai352f7f92012-12-19 12:52:06 +0100572/* can have the amp-out capability? */
573static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100575 hda_nid_t nid = path->path[idx];
576 unsigned int caps = get_wcaps(codec, nid);
577 unsigned int type = get_wcaps_type(caps);
578
579 if (!(caps & AC_WCAP_OUT_AMP))
580 return false;
581 if (type == AC_WID_PIN && !idx) /* only for output pins */
582 return false;
583 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Takashi Iwai352f7f92012-12-19 12:52:06 +0100586/* check whether the given (nid,dir,idx) is active */
587static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100588 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100590 struct hda_gen_spec *spec = codec->spec;
591 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Takashi Iwai352f7f92012-12-19 12:52:06 +0100593 for (n = 0; n < spec->paths.used; n++) {
594 struct nid_path *path = snd_array_elem(&spec->paths, n);
595 if (!path->active)
596 continue;
597 for (i = 0; i < path->depth; i++) {
598 if (path->path[i] == nid) {
599 if (dir == HDA_OUTPUT || path->idx[i] == idx)
600 return true;
601 break;
602 }
603 }
604 }
605 return false;
606}
607
608/* get the default amp value for the target state */
609static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100610 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100611{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100612 unsigned int val = 0;
613
Takashi Iwai352f7f92012-12-19 12:52:06 +0100614 if (caps & AC_AMPCAP_NUM_STEPS) {
615 /* set to 0dB */
616 if (enable)
617 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
618 }
619 if (caps & AC_AMPCAP_MUTE) {
620 if (!enable)
621 val |= HDA_AMP_MUTE;
622 }
623 return val;
624}
625
626/* initialize the amp value (only at the first time) */
627static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
628{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100629 unsigned int caps = query_amp_caps(codec, nid, dir);
630 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100631 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
632}
633
Takashi Iwai8999bf02013-01-18 11:01:33 +0100634/* calculate amp value mask we can modify;
635 * if the given amp is controlled by mixers, don't touch it
636 */
637static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
638 hda_nid_t nid, int dir, int idx,
639 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100640{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100641 unsigned int mask = 0xff;
642
643 if (caps & AC_AMPCAP_MUTE) {
644 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
645 mask &= ~0x80;
646 }
647 if (caps & AC_AMPCAP_NUM_STEPS) {
648 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
649 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
650 mask &= ~0x7f;
651 }
652 return mask;
653}
654
655static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
656 int idx, int idx_to_check, bool enable)
657{
658 unsigned int caps;
659 unsigned int mask, val;
660
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100661 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100662 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100663
664 caps = query_amp_caps(codec, nid, dir);
665 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
666 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
667 if (!mask)
668 return;
669
670 val &= mask;
671 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100672}
673
674static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
675 int i, bool enable)
676{
677 hda_nid_t nid = path->path[i];
678 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100679 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100680}
681
682static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
683 int i, bool enable, bool add_aamix)
684{
685 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100686 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100687 int n, nums, idx;
688 int type;
689 hda_nid_t nid = path->path[i];
690
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100691 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100692 type = get_wcaps_type(get_wcaps(codec, nid));
693 if (type == AC_WID_PIN ||
694 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
695 nums = 1;
696 idx = 0;
697 } else
698 idx = path->idx[i];
699
700 for (n = 0; n < nums; n++)
701 init_amp(codec, nid, HDA_INPUT, n);
702
Takashi Iwai352f7f92012-12-19 12:52:06 +0100703 /* here is a little bit tricky in comparison with activate_amp_out();
704 * when aa-mixer is available, we need to enable the path as well
705 */
706 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100707 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100708 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100709 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711}
712
Takashi Iwai352f7f92012-12-19 12:52:06 +0100713/* activate or deactivate the given path
714 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100716void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
717 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100719 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100720 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Takashi Iwai352f7f92012-12-19 12:52:06 +0100722 if (!enable)
723 path->active = false;
724
725 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100726 hda_nid_t nid = path->path[i];
727 if (enable && spec->power_down_unused) {
728 /* make sure the widget is powered up */
729 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
730 snd_hda_codec_write(codec, nid, 0,
731 AC_VERB_SET_POWER_STATE,
732 AC_PWRST_D0);
733 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100734 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100735 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100736 AC_VERB_SET_CONNECT_SEL,
737 path->idx[i]);
738 if (has_amp_in(codec, path, i))
739 activate_amp_in(codec, path, i, enable, add_aamix);
740 if (has_amp_out(codec, path, i))
741 activate_amp_out(codec, path, i, enable);
742 }
743
744 if (enable)
745 path->active = true;
746}
747EXPORT_SYMBOL_HDA(snd_hda_activate_path);
748
Takashi Iwai55196ff2013-01-24 17:32:56 +0100749/* if the given path is inactive, put widgets into D3 (only if suitable) */
750static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
751{
752 struct hda_gen_spec *spec = codec->spec;
753 bool changed;
754 int i;
755
756 if (!spec->power_down_unused || path->active)
757 return;
758
759 for (i = 0; i < path->depth; i++) {
760 hda_nid_t nid = path->path[i];
761 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3)) {
762 snd_hda_codec_write(codec, nid, 0,
763 AC_VERB_SET_POWER_STATE,
764 AC_PWRST_D3);
765 changed = true;
766 }
767 }
768
769 if (changed) {
770 msleep(10);
771 snd_hda_codec_read(codec, path->path[0], 0,
772 AC_VERB_GET_POWER_STATE, 0);
773 }
774}
775
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100776/* turn on/off EAPD on the given pin */
777static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
778{
779 struct hda_gen_spec *spec = codec->spec;
780 if (spec->own_eapd_ctl ||
781 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
782 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100783 if (codec->inv_eapd)
784 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100785 snd_hda_codec_update_cache(codec, pin, 0,
786 AC_VERB_SET_EAPD_BTLENABLE,
787 enable ? 0x02 : 0x00);
788}
789
Takashi Iwai3e367f12013-01-23 17:07:23 +0100790/* re-initialize the path specified by the given path index */
791static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
792{
793 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
794 if (path)
795 snd_hda_activate_path(codec, path, path->active, false);
796}
797
Takashi Iwai352f7f92012-12-19 12:52:06 +0100798
799/*
800 * Helper functions for creating mixer ctl elements
801 */
802
803enum {
804 HDA_CTL_WIDGET_VOL,
805 HDA_CTL_WIDGET_MUTE,
806 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100807};
808static const struct snd_kcontrol_new control_templates[] = {
809 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
810 HDA_CODEC_MUTE(NULL, 0, 0, 0),
811 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100812};
813
814/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100815static struct snd_kcontrol_new *
816add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100817 int cidx, unsigned long val)
818{
819 struct snd_kcontrol_new *knew;
820
Takashi Iwai12c93df2012-12-19 14:38:33 +0100821 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100822 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100823 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100824 knew->index = cidx;
825 if (get_amp_nid_(val))
826 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
827 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100828 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100829}
830
831static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
832 const char *pfx, const char *dir,
833 const char *sfx, int cidx, unsigned long val)
834{
835 char name[32];
836 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100837 if (!add_control(spec, type, name, cidx, val))
838 return -ENOMEM;
839 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100840}
841
842#define add_pb_vol_ctrl(spec, type, pfx, val) \
843 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
844#define add_pb_sw_ctrl(spec, type, pfx, val) \
845 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
846#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
847 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
848#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
849 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
850
851static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
852 unsigned int chs, struct nid_path *path)
853{
854 unsigned int val;
855 if (!path)
856 return 0;
857 val = path->ctls[NID_PATH_VOL_CTL];
858 if (!val)
859 return 0;
860 val = amp_val_replace_channels(val, chs);
861 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
862}
863
864/* return the channel bits suitable for the given path->ctls[] */
865static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
866 int type)
867{
868 int chs = 1; /* mono (left only) */
869 if (path) {
870 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
871 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
872 chs = 3; /* stereo */
873 }
874 return chs;
875}
876
877static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
878 struct nid_path *path)
879{
880 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
881 return add_vol_ctl(codec, pfx, cidx, chs, path);
882}
883
884/* create a mute-switch for the given mixer widget;
885 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
886 */
887static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
888 unsigned int chs, struct nid_path *path)
889{
890 unsigned int val;
891 int type = HDA_CTL_WIDGET_MUTE;
892
893 if (!path)
894 return 0;
895 val = path->ctls[NID_PATH_MUTE_CTL];
896 if (!val)
897 return 0;
898 val = amp_val_replace_channels(val, chs);
899 if (get_amp_direction_(val) == HDA_INPUT) {
900 hda_nid_t nid = get_amp_nid_(val);
901 int nums = snd_hda_get_num_conns(codec, nid);
902 if (nums > 1) {
903 type = HDA_CTL_BIND_MUTE;
904 val |= nums << 19;
905 }
906 }
907 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
908}
909
910static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
911 int cidx, struct nid_path *path)
912{
913 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
914 return add_sw_ctl(codec, pfx, cidx, chs, path);
915}
916
Takashi Iwai247d85e2013-01-17 16:18:11 +0100917/* any ctl assigned to the path with the given index? */
918static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
919{
920 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
921 return path && path->ctls[ctl_type];
922}
923
Takashi Iwai352f7f92012-12-19 12:52:06 +0100924static const char * const channel_name[4] = {
925 "Front", "Surround", "CLFE", "Side"
926};
927
928/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100929static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
930 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100931{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100932 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100933 struct auto_pin_cfg *cfg = &spec->autocfg;
934
935 *index = 0;
936 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100937 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100938 return spec->vmaster_mute.hook ? "PCM" : "Master";
939
940 /* if there is really a single DAC used in the whole output paths,
941 * use it master (or "PCM" if a vmaster hook is present)
942 */
943 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
944 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
945 return spec->vmaster_mute.hook ? "PCM" : "Master";
946
Takashi Iwai247d85e2013-01-17 16:18:11 +0100947 /* multi-io channels */
948 if (ch >= cfg->line_outs)
949 return channel_name[ch];
950
Takashi Iwai352f7f92012-12-19 12:52:06 +0100951 switch (cfg->line_out_type) {
952 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100953 /* if the primary channel vol/mute is shared with HP volume,
954 * don't name it as Speaker
955 */
956 if (!ch && cfg->hp_outs &&
957 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
958 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100959 if (cfg->line_outs == 1)
960 return "Speaker";
961 if (cfg->line_outs == 2)
962 return ch ? "Bass Speaker" : "Speaker";
963 break;
964 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100965 /* if the primary channel vol/mute is shared with spk volume,
966 * don't name it as Headphone
967 */
968 if (!ch && cfg->speaker_outs &&
969 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
970 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100971 /* for multi-io case, only the primary out */
972 if (ch && spec->multi_ios)
973 break;
974 *index = ch;
975 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100976 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100977
978 /* for a single channel output, we don't have to name the channel */
979 if (cfg->line_outs == 1 && !spec->multi_ios)
980 return "PCM";
981
Takashi Iwai352f7f92012-12-19 12:52:06 +0100982 if (ch >= ARRAY_SIZE(channel_name)) {
983 snd_BUG();
984 return "PCM";
985 }
986
987 return channel_name[ch];
988}
989
990/*
991 * Parse output paths
992 */
993
994/* badness definition */
995enum {
996 /* No primary DAC is found for the main output */
997 BAD_NO_PRIMARY_DAC = 0x10000,
998 /* No DAC is found for the extra output */
999 BAD_NO_DAC = 0x4000,
1000 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001001 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001002 /* No individual DAC for extra output */
1003 BAD_NO_EXTRA_DAC = 0x102,
1004 /* No individual DAC for extra surrounds */
1005 BAD_NO_EXTRA_SURR_DAC = 0x101,
1006 /* Primary DAC shared with main surrounds */
1007 BAD_SHARED_SURROUND = 0x100,
1008 /* Primary DAC shared with main CLFE */
1009 BAD_SHARED_CLFE = 0x10,
1010 /* Primary DAC shared with extra surrounds */
1011 BAD_SHARED_EXTRA_SURROUND = 0x10,
1012 /* Volume widget is shared */
1013 BAD_SHARED_VOL = 0x10,
1014};
1015
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001016/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001017 * volume and mute controls, and assign the values to ctls[].
1018 *
1019 * When no appropriate widget is found in the path, the badness value
1020 * is incremented depending on the situation. The function returns the
1021 * total badness for both volume and mute controls.
1022 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001023static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001024{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001025 hda_nid_t nid;
1026 unsigned int val;
1027 int badness = 0;
1028
1029 if (!path)
1030 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001031
1032 if (path->ctls[NID_PATH_VOL_CTL] ||
1033 path->ctls[NID_PATH_MUTE_CTL])
1034 return 0; /* already evaluated */
1035
Takashi Iwai352f7f92012-12-19 12:52:06 +01001036 nid = look_for_out_vol_nid(codec, path);
1037 if (nid) {
1038 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1039 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1040 badness += BAD_SHARED_VOL;
1041 else
1042 path->ctls[NID_PATH_VOL_CTL] = val;
1043 } else
1044 badness += BAD_SHARED_VOL;
1045 nid = look_for_out_mute_nid(codec, path);
1046 if (nid) {
1047 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1048 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1049 nid_has_mute(codec, nid, HDA_OUTPUT))
1050 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1051 else
1052 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1053 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1054 badness += BAD_SHARED_VOL;
1055 else
1056 path->ctls[NID_PATH_MUTE_CTL] = val;
1057 } else
1058 badness += BAD_SHARED_VOL;
1059 return badness;
1060}
1061
1062struct badness_table {
1063 int no_primary_dac; /* no primary DAC */
1064 int no_dac; /* no secondary DACs */
1065 int shared_primary; /* primary DAC is shared with main output */
1066 int shared_surr; /* secondary DAC shared with main or primary */
1067 int shared_clfe; /* third DAC shared with main or primary */
1068 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1069};
1070
1071static struct badness_table main_out_badness = {
1072 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1073 .no_dac = BAD_NO_DAC,
1074 .shared_primary = BAD_NO_PRIMARY_DAC,
1075 .shared_surr = BAD_SHARED_SURROUND,
1076 .shared_clfe = BAD_SHARED_CLFE,
1077 .shared_surr_main = BAD_SHARED_SURROUND,
1078};
1079
1080static struct badness_table extra_out_badness = {
1081 .no_primary_dac = BAD_NO_DAC,
1082 .no_dac = BAD_NO_DAC,
1083 .shared_primary = BAD_NO_EXTRA_DAC,
1084 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1085 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1086 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1087};
1088
Takashi Iwai7385df62013-01-07 09:50:52 +01001089/* get the DAC of the primary output corresponding to the given array index */
1090static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1091{
1092 struct hda_gen_spec *spec = codec->spec;
1093 struct auto_pin_cfg *cfg = &spec->autocfg;
1094
1095 if (cfg->line_outs > idx)
1096 return spec->private_dac_nids[idx];
1097 idx -= cfg->line_outs;
1098 if (spec->multi_ios > idx)
1099 return spec->multi_io[idx].dac;
1100 return 0;
1101}
1102
1103/* return the DAC if it's reachable, otherwise zero */
1104static inline hda_nid_t try_dac(struct hda_codec *codec,
1105 hda_nid_t dac, hda_nid_t pin)
1106{
1107 return is_reachable_path(codec, dac, pin) ? dac : 0;
1108}
1109
Takashi Iwai352f7f92012-12-19 12:52:06 +01001110/* try to assign DACs to pins and return the resultant badness */
1111static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1112 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001113 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001114 const struct badness_table *bad)
1115{
1116 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001117 int i, j;
1118 int badness = 0;
1119 hda_nid_t dac;
1120
1121 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return 0;
1123
Takashi Iwai352f7f92012-12-19 12:52:06 +01001124 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001125 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001126 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001127
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001128 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1129 if (path) {
1130 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001131 continue;
1132 }
1133
1134 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001135 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001136 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001137 for (j = 1; j < num_outs; j++) {
1138 if (is_reachable_path(codec, dacs[j], pin)) {
1139 dacs[0] = dacs[j];
1140 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001141 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001142 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001143 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001146 }
1147 dac = dacs[i];
1148 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001149 if (num_outs > 2)
1150 dac = try_dac(codec, get_primary_out(codec, i), pin);
1151 if (!dac)
1152 dac = try_dac(codec, dacs[0], pin);
1153 if (!dac)
1154 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001155 if (dac) {
1156 if (!i)
1157 badness += bad->shared_primary;
1158 else if (i == 1)
1159 badness += bad->shared_surr;
1160 else
1161 badness += bad->shared_clfe;
1162 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1163 dac = spec->private_dac_nids[0];
1164 badness += bad->shared_surr_main;
1165 } else if (!i)
1166 badness += bad->no_primary_dac;
1167 else
1168 badness += bad->no_dac;
1169 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001170 if (!dac)
1171 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001172 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001173 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001174 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001175 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001176 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001177 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001178 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001179 badness += bad->no_dac;
1180 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001181 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001182 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001183 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001184 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001185 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001186 }
1187
1188 return badness;
1189}
1190
1191/* return NID if the given pin has only a single connection to a certain DAC */
1192static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1193{
1194 struct hda_gen_spec *spec = codec->spec;
1195 int i;
1196 hda_nid_t nid_found = 0;
1197
1198 for (i = 0; i < spec->num_all_dacs; i++) {
1199 hda_nid_t nid = spec->all_dacs[i];
1200 if (!nid || is_dac_already_used(codec, nid))
1201 continue;
1202 if (is_reachable_path(codec, nid, pin)) {
1203 if (nid_found)
1204 return 0;
1205 nid_found = nid;
1206 }
1207 }
1208 return nid_found;
1209}
1210
1211/* check whether the given pin can be a multi-io pin */
1212static bool can_be_multiio_pin(struct hda_codec *codec,
1213 unsigned int location, hda_nid_t nid)
1214{
1215 unsigned int defcfg, caps;
1216
1217 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1218 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1219 return false;
1220 if (location && get_defcfg_location(defcfg) != location)
1221 return false;
1222 caps = snd_hda_query_pin_caps(codec, nid);
1223 if (!(caps & AC_PINCAP_OUT))
1224 return false;
1225 return true;
1226}
1227
Takashi Iwaie22aab72013-01-04 14:50:04 +01001228/* count the number of input pins that are capable to be multi-io */
1229static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1230{
1231 struct hda_gen_spec *spec = codec->spec;
1232 struct auto_pin_cfg *cfg = &spec->autocfg;
1233 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1234 unsigned int location = get_defcfg_location(defcfg);
1235 int type, i;
1236 int num_pins = 0;
1237
1238 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1239 for (i = 0; i < cfg->num_inputs; i++) {
1240 if (cfg->inputs[i].type != type)
1241 continue;
1242 if (can_be_multiio_pin(codec, location,
1243 cfg->inputs[i].pin))
1244 num_pins++;
1245 }
1246 }
1247 return num_pins;
1248}
1249
Takashi Iwai352f7f92012-12-19 12:52:06 +01001250/*
1251 * multi-io helper
1252 *
1253 * When hardwired is set, try to fill ony hardwired pins, and returns
1254 * zero if any pins are filled, non-zero if nothing found.
1255 * When hardwired is off, try to fill possible input pins, and returns
1256 * the badness value.
1257 */
1258static int fill_multi_ios(struct hda_codec *codec,
1259 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001260 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001261{
1262 struct hda_gen_spec *spec = codec->spec;
1263 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001264 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001265 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1266 unsigned int location = get_defcfg_location(defcfg);
1267 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001268 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001269
1270 old_pins = spec->multi_ios;
1271 if (old_pins >= 2)
1272 goto end_fill;
1273
Takashi Iwaie22aab72013-01-04 14:50:04 +01001274 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001275 if (num_pins < 2)
1276 goto end_fill;
1277
Takashi Iwai352f7f92012-12-19 12:52:06 +01001278 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1279 for (i = 0; i < cfg->num_inputs; i++) {
1280 hda_nid_t nid = cfg->inputs[i].pin;
1281 hda_nid_t dac = 0;
1282
1283 if (cfg->inputs[i].type != type)
1284 continue;
1285 if (!can_be_multiio_pin(codec, location, nid))
1286 continue;
1287 for (j = 0; j < spec->multi_ios; j++) {
1288 if (nid == spec->multi_io[j].pin)
1289 break;
1290 }
1291 if (j < spec->multi_ios)
1292 continue;
1293
Takashi Iwai352f7f92012-12-19 12:52:06 +01001294 if (hardwired)
1295 dac = get_dac_if_single(codec, nid);
1296 else if (!dac)
1297 dac = look_for_dac(codec, nid, false);
1298 if (!dac) {
1299 badness++;
1300 continue;
1301 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001302 path = snd_hda_add_new_path(codec, dac, nid,
1303 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001304 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001305 badness++;
1306 continue;
1307 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001308 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001309 spec->multi_io[spec->multi_ios].pin = nid;
1310 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001311 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1312 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001313 spec->multi_ios++;
1314 if (spec->multi_ios >= 2)
1315 break;
1316 }
1317 }
1318 end_fill:
1319 if (badness)
1320 badness = BAD_MULTI_IO;
1321 if (old_pins == spec->multi_ios) {
1322 if (hardwired)
1323 return 1; /* nothing found */
1324 else
1325 return badness; /* no badness if nothing found */
1326 }
1327 if (!hardwired && spec->multi_ios < 2) {
1328 /* cancel newly assigned paths */
1329 spec->paths.used -= spec->multi_ios - old_pins;
1330 spec->multi_ios = old_pins;
1331 return badness;
1332 }
1333
1334 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001335 for (i = old_pins; i < spec->multi_ios; i++) {
1336 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1337 badness += assign_out_path_ctls(codec, path);
1338 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001339
1340 return badness;
1341}
1342
1343/* map DACs for all pins in the list if they are single connections */
1344static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001345 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001346{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001347 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 int i;
1349 bool found = false;
1350 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001351 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001352 hda_nid_t dac;
1353 if (dacs[i])
1354 continue;
1355 dac = get_dac_if_single(codec, pins[i]);
1356 if (!dac)
1357 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001358 path = snd_hda_add_new_path(codec, dac, pins[i],
1359 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001360 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001361 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001362 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001363 dacs[i] = dac;
1364 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001365 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001366 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001367 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001368 }
1369 }
1370 return found;
1371}
1372
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001373/* create a new path including aamix if available, and return its index */
1374static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1375{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001376 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001377 struct nid_path *path;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001378 hda_nid_t dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001379
1380 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001381 if (!path || !path->depth ||
1382 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001383 return 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001384 dac = path->path[0];
1385 pin = path->path[path->depth - 1];
1386 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1387 if (!path) {
1388 if (dac != spec->multiout.dac_nids[0])
1389 dac = spec->multiout.dac_nids[0];
1390 else if (spec->multiout.hp_out_nid[0])
1391 dac = spec->multiout.hp_out_nid[0];
1392 else if (spec->multiout.extra_out_nid[0])
1393 dac = spec->multiout.extra_out_nid[0];
1394 if (dac)
1395 path = snd_hda_add_new_path(codec, dac, pin,
1396 spec->mixer_nid);
1397 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001398 if (!path)
1399 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001400 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001401 path->active = false; /* unused as default */
1402 return snd_hda_get_path_idx(codec, path);
1403}
1404
Takashi Iwaia07a9492013-01-07 16:44:06 +01001405/* fill the empty entries in the dac array for speaker/hp with the
1406 * shared dac pointed by the paths
1407 */
1408static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1409 hda_nid_t *dacs, int *path_idx)
1410{
1411 struct nid_path *path;
1412 int i;
1413
1414 for (i = 0; i < num_outs; i++) {
1415 if (dacs[i])
1416 continue;
1417 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1418 if (!path)
1419 continue;
1420 dacs[i] = path->path[0];
1421 }
1422}
1423
Takashi Iwai352f7f92012-12-19 12:52:06 +01001424/* fill in the dac_nids table from the parsed pin configuration */
1425static int fill_and_eval_dacs(struct hda_codec *codec,
1426 bool fill_hardwired,
1427 bool fill_mio_first)
1428{
1429 struct hda_gen_spec *spec = codec->spec;
1430 struct auto_pin_cfg *cfg = &spec->autocfg;
1431 int i, err, badness;
1432
1433 /* set num_dacs once to full for look_for_dac() */
1434 spec->multiout.num_dacs = cfg->line_outs;
1435 spec->multiout.dac_nids = spec->private_dac_nids;
1436 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1437 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1438 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1439 spec->multi_ios = 0;
1440 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001441
1442 /* clear path indices */
1443 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1444 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1445 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1446 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1447 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001448 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001449 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1450 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1451
Takashi Iwai352f7f92012-12-19 12:52:06 +01001452 badness = 0;
1453
1454 /* fill hard-wired DACs first */
1455 if (fill_hardwired) {
1456 bool mapped;
1457 do {
1458 mapped = map_singles(codec, cfg->line_outs,
1459 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001460 spec->private_dac_nids,
1461 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001462 mapped |= map_singles(codec, cfg->hp_outs,
1463 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001464 spec->multiout.hp_out_nid,
1465 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001466 mapped |= map_singles(codec, cfg->speaker_outs,
1467 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001468 spec->multiout.extra_out_nid,
1469 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001470 if (fill_mio_first && cfg->line_outs == 1 &&
1471 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001472 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001473 if (!err)
1474 mapped = true;
1475 }
1476 } while (mapped);
1477 }
1478
1479 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001480 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001481 &main_out_badness);
1482
Takashi Iwai352f7f92012-12-19 12:52:06 +01001483 if (fill_mio_first &&
1484 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1485 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001486 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001487 if (err < 0)
1488 return err;
1489 /* we don't count badness at this stage yet */
1490 }
1491
1492 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1493 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1494 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001495 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001496 &extra_out_badness);
1497 if (err < 0)
1498 return err;
1499 badness += err;
1500 }
1501 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1502 err = try_assign_dacs(codec, cfg->speaker_outs,
1503 cfg->speaker_pins,
1504 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001505 spec->speaker_paths,
1506 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001507 if (err < 0)
1508 return err;
1509 badness += err;
1510 }
1511 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001512 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001513 if (err < 0)
1514 return err;
1515 badness += err;
1516 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001517
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001518 if (spec->mixer_nid) {
1519 spec->aamix_out_paths[0] =
1520 check_aamix_out_path(codec, spec->out_paths[0]);
1521 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1522 spec->aamix_out_paths[1] =
1523 check_aamix_out_path(codec, spec->hp_paths[0]);
1524 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1525 spec->aamix_out_paths[2] =
1526 check_aamix_out_path(codec, spec->speaker_paths[0]);
1527 }
1528
Takashi Iwaie22aab72013-01-04 14:50:04 +01001529 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1530 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1531 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001532
Takashi Iwaia07a9492013-01-07 16:44:06 +01001533 /* re-count num_dacs and squash invalid entries */
1534 spec->multiout.num_dacs = 0;
1535 for (i = 0; i < cfg->line_outs; i++) {
1536 if (spec->private_dac_nids[i])
1537 spec->multiout.num_dacs++;
1538 else {
1539 memmove(spec->private_dac_nids + i,
1540 spec->private_dac_nids + i + 1,
1541 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1542 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1543 }
1544 }
1545
1546 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001547 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001548
Takashi Iwai352f7f92012-12-19 12:52:06 +01001549 if (spec->multi_ios == 2) {
1550 for (i = 0; i < 2; i++)
1551 spec->private_dac_nids[spec->multiout.num_dacs++] =
1552 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001553 } else if (spec->multi_ios) {
1554 spec->multi_ios = 0;
1555 badness += BAD_MULTI_IO;
1556 }
1557
Takashi Iwaia07a9492013-01-07 16:44:06 +01001558 /* re-fill the shared DAC for speaker / headphone */
1559 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1560 refill_shared_dacs(codec, cfg->hp_outs,
1561 spec->multiout.hp_out_nid,
1562 spec->hp_paths);
1563 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1564 refill_shared_dacs(codec, cfg->speaker_outs,
1565 spec->multiout.extra_out_nid,
1566 spec->speaker_paths);
1567
Takashi Iwai352f7f92012-12-19 12:52:06 +01001568 return badness;
1569}
1570
1571#define DEBUG_BADNESS
1572
1573#ifdef DEBUG_BADNESS
1574#define debug_badness snd_printdd
1575#else
1576#define debug_badness(...)
1577#endif
1578
Takashi Iwaia7694092013-01-21 10:43:18 +01001579#ifdef DEBUG_BADNESS
1580static inline void print_nid_path_idx(struct hda_codec *codec,
1581 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001582{
Takashi Iwaia7694092013-01-21 10:43:18 +01001583 struct nid_path *path;
1584
1585 path = snd_hda_get_path_from_idx(codec, idx);
1586 if (path)
1587 print_nid_path(pfx, path);
1588}
1589
1590static void debug_show_configs(struct hda_codec *codec,
1591 struct auto_pin_cfg *cfg)
1592{
1593 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001594 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001595 int i;
1596
1597 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001598 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001599 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001600 spec->multiout.dac_nids[0],
1601 spec->multiout.dac_nids[1],
1602 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001603 spec->multiout.dac_nids[3],
1604 lo_type[cfg->line_out_type]);
1605 for (i = 0; i < cfg->line_outs; i++)
1606 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001607 if (spec->multi_ios > 0)
1608 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1609 spec->multi_ios,
1610 spec->multi_io[0].pin, spec->multi_io[1].pin,
1611 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001612 for (i = 0; i < spec->multi_ios; i++)
1613 print_nid_path_idx(codec, " mio",
1614 spec->out_paths[cfg->line_outs + i]);
1615 if (cfg->hp_outs)
1616 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001617 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001618 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001619 spec->multiout.hp_out_nid[0],
1620 spec->multiout.hp_out_nid[1],
1621 spec->multiout.hp_out_nid[2],
1622 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001623 for (i = 0; i < cfg->hp_outs; i++)
1624 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1625 if (cfg->speaker_outs)
1626 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001627 cfg->speaker_pins[0], cfg->speaker_pins[1],
1628 cfg->speaker_pins[2], cfg->speaker_pins[3],
1629 spec->multiout.extra_out_nid[0],
1630 spec->multiout.extra_out_nid[1],
1631 spec->multiout.extra_out_nid[2],
1632 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001633 for (i = 0; i < cfg->speaker_outs; i++)
1634 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1635 for (i = 0; i < 3; i++)
1636 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001637}
Takashi Iwaia7694092013-01-21 10:43:18 +01001638#else
1639#define debug_show_configs(codec, cfg) /* NOP */
1640#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001641
1642/* find all available DACs of the codec */
1643static void fill_all_dac_nids(struct hda_codec *codec)
1644{
1645 struct hda_gen_spec *spec = codec->spec;
1646 int i;
1647 hda_nid_t nid = codec->start_nid;
1648
1649 spec->num_all_dacs = 0;
1650 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1651 for (i = 0; i < codec->num_nodes; i++, nid++) {
1652 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1653 continue;
1654 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1655 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1656 break;
1657 }
1658 spec->all_dacs[spec->num_all_dacs++] = nid;
1659 }
1660}
1661
1662static int parse_output_paths(struct hda_codec *codec)
1663{
1664 struct hda_gen_spec *spec = codec->spec;
1665 struct auto_pin_cfg *cfg = &spec->autocfg;
1666 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001667 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001668 int best_badness = INT_MAX;
1669 int badness;
1670 bool fill_hardwired = true, fill_mio_first = true;
1671 bool best_wired = true, best_mio = true;
1672 bool hp_spk_swapped = false;
1673
Takashi Iwai352f7f92012-12-19 12:52:06 +01001674 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1675 if (!best_cfg)
1676 return -ENOMEM;
1677 *best_cfg = *cfg;
1678
1679 for (;;) {
1680 badness = fill_and_eval_dacs(codec, fill_hardwired,
1681 fill_mio_first);
1682 if (badness < 0) {
1683 kfree(best_cfg);
1684 return badness;
1685 }
1686 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1687 cfg->line_out_type, fill_hardwired, fill_mio_first,
1688 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001689 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001690 if (badness < best_badness) {
1691 best_badness = badness;
1692 *best_cfg = *cfg;
1693 best_wired = fill_hardwired;
1694 best_mio = fill_mio_first;
1695 }
1696 if (!badness)
1697 break;
1698 fill_mio_first = !fill_mio_first;
1699 if (!fill_mio_first)
1700 continue;
1701 fill_hardwired = !fill_hardwired;
1702 if (!fill_hardwired)
1703 continue;
1704 if (hp_spk_swapped)
1705 break;
1706 hp_spk_swapped = true;
1707 if (cfg->speaker_outs > 0 &&
1708 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1709 cfg->hp_outs = cfg->line_outs;
1710 memcpy(cfg->hp_pins, cfg->line_out_pins,
1711 sizeof(cfg->hp_pins));
1712 cfg->line_outs = cfg->speaker_outs;
1713 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1714 sizeof(cfg->speaker_pins));
1715 cfg->speaker_outs = 0;
1716 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1717 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1718 fill_hardwired = true;
1719 continue;
1720 }
1721 if (cfg->hp_outs > 0 &&
1722 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1723 cfg->speaker_outs = cfg->line_outs;
1724 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1725 sizeof(cfg->speaker_pins));
1726 cfg->line_outs = cfg->hp_outs;
1727 memcpy(cfg->line_out_pins, cfg->hp_pins,
1728 sizeof(cfg->hp_pins));
1729 cfg->hp_outs = 0;
1730 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1731 cfg->line_out_type = AUTO_PIN_HP_OUT;
1732 fill_hardwired = true;
1733 continue;
1734 }
1735 break;
1736 }
1737
1738 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001739 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001740 *cfg = *best_cfg;
1741 fill_and_eval_dacs(codec, best_wired, best_mio);
1742 }
1743 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1744 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001745 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001746
1747 if (cfg->line_out_pins[0]) {
1748 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001749 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001750 if (path)
1751 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001752 if (spec->vmaster_nid)
1753 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1754 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001755 }
1756
Takashi Iwai9314a582013-01-21 10:49:05 +01001757 /* set initial pinctl targets */
1758 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1759 val = PIN_HP;
1760 else
1761 val = PIN_OUT;
1762 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1763 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1764 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1765 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1766 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1767 set_pin_targets(codec, cfg->speaker_outs,
1768 cfg->speaker_pins, val);
1769 }
1770
Takashi Iwai352f7f92012-12-19 12:52:06 +01001771 kfree(best_cfg);
1772 return 0;
1773}
1774
1775/* add playback controls from the parsed DAC table */
1776static int create_multi_out_ctls(struct hda_codec *codec,
1777 const struct auto_pin_cfg *cfg)
1778{
1779 struct hda_gen_spec *spec = codec->spec;
1780 int i, err, noutputs;
1781
1782 noutputs = cfg->line_outs;
1783 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1784 noutputs += spec->multi_ios;
1785
1786 for (i = 0; i < noutputs; i++) {
1787 const char *name;
1788 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001789 struct nid_path *path;
1790
Takashi Iwai196c17662013-01-04 15:01:40 +01001791 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001792 if (!path)
1793 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001794
1795 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001796 if (!name || !strcmp(name, "CLFE")) {
1797 /* Center/LFE */
1798 err = add_vol_ctl(codec, "Center", 0, 1, path);
1799 if (err < 0)
1800 return err;
1801 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1802 if (err < 0)
1803 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001804 } else {
1805 err = add_stereo_vol(codec, name, index, path);
1806 if (err < 0)
1807 return err;
1808 }
1809
1810 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1811 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001812 err = add_sw_ctl(codec, "Center", 0, 1, path);
1813 if (err < 0)
1814 return err;
1815 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1816 if (err < 0)
1817 return err;
1818 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001819 err = add_stereo_sw(codec, name, index, path);
1820 if (err < 0)
1821 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823 }
1824 return 0;
1825}
1826
Takashi Iwaic2c80382013-01-07 10:33:57 +01001827static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001828 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001830 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 int err;
1832
Takashi Iwai196c17662013-01-04 15:01:40 +01001833 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001834 if (!path)
1835 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001836 err = add_stereo_vol(codec, pfx, cidx, path);
1837 if (err < 0)
1838 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001839 err = add_stereo_sw(codec, pfx, cidx, path);
1840 if (err < 0)
1841 return err;
1842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
Takashi Iwai352f7f92012-12-19 12:52:06 +01001845/* add playback controls for speaker and HP outputs */
1846static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001847 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001849 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001850
1851 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001852 const char *name;
1853 char tmp[44];
1854 int err, idx = 0;
1855
1856 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1857 name = "Bass Speaker";
1858 else if (num_pins >= 3) {
1859 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001860 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001861 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001862 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001863 name = pfx;
1864 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001866 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001867 if (err < 0)
1868 return err;
1869 }
1870 return 0;
1871}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001872
Takashi Iwai352f7f92012-12-19 12:52:06 +01001873static int create_hp_out_ctls(struct hda_codec *codec)
1874{
1875 struct hda_gen_spec *spec = codec->spec;
1876 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001877 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001878 "Headphone");
1879}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Takashi Iwai352f7f92012-12-19 12:52:06 +01001881static int create_speaker_out_ctls(struct hda_codec *codec)
1882{
1883 struct hda_gen_spec *spec = codec->spec;
1884 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001885 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001886 "Speaker");
1887}
1888
1889/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001890 * independent HP controls
1891 */
1892
Takashi Iwai8ba955c2013-03-07 18:40:58 +01001893/* update HP auto-mute state too */
1894static void update_hp_automute_hook(struct hda_codec *codec)
1895{
1896 struct hda_gen_spec *spec = codec->spec;
1897
1898 if (spec->hp_automute_hook)
1899 spec->hp_automute_hook(codec, NULL);
1900 else
1901 snd_hda_gen_hp_automute(codec, NULL);
1902}
1903
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001904static int indep_hp_info(struct snd_kcontrol *kcontrol,
1905 struct snd_ctl_elem_info *uinfo)
1906{
1907 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1908}
1909
1910static int indep_hp_get(struct snd_kcontrol *kcontrol,
1911 struct snd_ctl_elem_value *ucontrol)
1912{
1913 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1914 struct hda_gen_spec *spec = codec->spec;
1915 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1916 return 0;
1917}
1918
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001919static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1920 int nomix_path_idx, int mix_path_idx,
1921 int out_type);
1922
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001923static int indep_hp_put(struct snd_kcontrol *kcontrol,
1924 struct snd_ctl_elem_value *ucontrol)
1925{
1926 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1927 struct hda_gen_spec *spec = codec->spec;
1928 unsigned int select = ucontrol->value.enumerated.item[0];
1929 int ret = 0;
1930
1931 mutex_lock(&spec->pcm_mutex);
1932 if (spec->active_streams) {
1933 ret = -EBUSY;
1934 goto unlock;
1935 }
1936
1937 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001938 hda_nid_t *dacp;
1939 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1940 dacp = &spec->private_dac_nids[0];
1941 else
1942 dacp = &spec->multiout.hp_out_nid[0];
1943
1944 /* update HP aamix paths in case it conflicts with indep HP */
1945 if (spec->have_aamix_ctl) {
1946 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1947 update_aamix_paths(codec, spec->aamix_mode,
1948 spec->out_paths[0],
1949 spec->aamix_out_paths[0],
1950 spec->autocfg.line_out_type);
1951 else
1952 update_aamix_paths(codec, spec->aamix_mode,
1953 spec->hp_paths[0],
1954 spec->aamix_out_paths[1],
1955 AUTO_PIN_HP_OUT);
1956 }
1957
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001958 spec->indep_hp_enabled = select;
1959 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001960 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001961 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001962 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01001963
Takashi Iwai8ba955c2013-03-07 18:40:58 +01001964 update_hp_automute_hook(codec);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001965 ret = 1;
1966 }
1967 unlock:
1968 mutex_unlock(&spec->pcm_mutex);
1969 return ret;
1970}
1971
1972static const struct snd_kcontrol_new indep_hp_ctl = {
1973 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1974 .name = "Independent HP",
1975 .info = indep_hp_info,
1976 .get = indep_hp_get,
1977 .put = indep_hp_put,
1978};
1979
1980
1981static int create_indep_hp_ctls(struct hda_codec *codec)
1982{
1983 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001984 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001985
1986 if (!spec->indep_hp)
1987 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001988 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1989 dac = spec->multiout.dac_nids[0];
1990 else
1991 dac = spec->multiout.hp_out_nid[0];
1992 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001993 spec->indep_hp = 0;
1994 return 0;
1995 }
1996
1997 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001998 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001999 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2000 return -ENOMEM;
2001 return 0;
2002}
2003
2004/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002005 * channel mode enum control
2006 */
2007
2008static int ch_mode_info(struct snd_kcontrol *kcontrol,
2009 struct snd_ctl_elem_info *uinfo)
2010{
2011 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2012 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002013 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002014
2015 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2016 uinfo->count = 1;
2017 uinfo->value.enumerated.items = spec->multi_ios + 1;
2018 if (uinfo->value.enumerated.item > spec->multi_ios)
2019 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002020 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2021 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002022 return 0;
2023}
2024
2025static int ch_mode_get(struct snd_kcontrol *kcontrol,
2026 struct snd_ctl_elem_value *ucontrol)
2027{
2028 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2029 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002030 ucontrol->value.enumerated.item[0] =
2031 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002032 return 0;
2033}
2034
Takashi Iwai196c17662013-01-04 15:01:40 +01002035static inline struct nid_path *
2036get_multiio_path(struct hda_codec *codec, int idx)
2037{
2038 struct hda_gen_spec *spec = codec->spec;
2039 return snd_hda_get_path_from_idx(codec,
2040 spec->out_paths[spec->autocfg.line_outs + idx]);
2041}
2042
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002043static void update_automute_all(struct hda_codec *codec);
2044
Takashi Iwai352f7f92012-12-19 12:52:06 +01002045static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2046{
2047 struct hda_gen_spec *spec = codec->spec;
2048 hda_nid_t nid = spec->multi_io[idx].pin;
2049 struct nid_path *path;
2050
Takashi Iwai196c17662013-01-04 15:01:40 +01002051 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002052 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002054
2055 if (path->active == output)
2056 return 0;
2057
2058 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002059 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002060 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002061 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002062 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002063 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002064 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002065 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002066 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002068
2069 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002070 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002071
Takashi Iwai352f7f92012-12-19 12:52:06 +01002072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
Takashi Iwai352f7f92012-12-19 12:52:06 +01002075static int ch_mode_put(struct snd_kcontrol *kcontrol,
2076 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002078 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2079 struct hda_gen_spec *spec = codec->spec;
2080 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Takashi Iwai352f7f92012-12-19 12:52:06 +01002082 ch = ucontrol->value.enumerated.item[0];
2083 if (ch < 0 || ch > spec->multi_ios)
2084 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002085 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002086 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002087 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002088 for (i = 0; i < spec->multi_ios; i++)
2089 set_multi_io(codec, i, i < ch);
2090 spec->multiout.max_channels = max(spec->ext_channel_count,
2091 spec->const_channel_count);
2092 if (spec->need_dac_fix)
2093 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return 1;
2095}
2096
Takashi Iwai352f7f92012-12-19 12:52:06 +01002097static const struct snd_kcontrol_new channel_mode_enum = {
2098 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2099 .name = "Channel Mode",
2100 .info = ch_mode_info,
2101 .get = ch_mode_get,
2102 .put = ch_mode_put,
2103};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Takashi Iwai352f7f92012-12-19 12:52:06 +01002105static int create_multi_channel_mode(struct hda_codec *codec)
2106{
2107 struct hda_gen_spec *spec = codec->spec;
2108
2109 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002110 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002111 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return 0;
2114}
2115
Takashi Iwai352f7f92012-12-19 12:52:06 +01002116/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002117 * aamix loopback enable/disable switch
2118 */
2119
2120#define loopback_mixing_info indep_hp_info
2121
2122static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2123 struct snd_ctl_elem_value *ucontrol)
2124{
2125 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2126 struct hda_gen_spec *spec = codec->spec;
2127 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2128 return 0;
2129}
2130
2131static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002132 int nomix_path_idx, int mix_path_idx,
2133 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002134{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002135 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002136 struct nid_path *nomix_path, *mix_path;
2137
2138 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2139 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2140 if (!nomix_path || !mix_path)
2141 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002142
2143 /* if HP aamix path is driven from a different DAC and the
2144 * independent HP mode is ON, can't turn on aamix path
2145 */
2146 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2147 mix_path->path[0] != spec->alt_dac_nid)
2148 do_mix = false;
2149
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002150 if (do_mix) {
2151 snd_hda_activate_path(codec, nomix_path, false, true);
2152 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002153 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002154 } else {
2155 snd_hda_activate_path(codec, mix_path, false, true);
2156 snd_hda_activate_path(codec, nomix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002157 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002158 }
2159}
2160
2161static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2162 struct snd_ctl_elem_value *ucontrol)
2163{
2164 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2165 struct hda_gen_spec *spec = codec->spec;
2166 unsigned int val = ucontrol->value.enumerated.item[0];
2167
2168 if (val == spec->aamix_mode)
2169 return 0;
2170 spec->aamix_mode = val;
2171 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002172 spec->aamix_out_paths[0],
2173 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002174 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002175 spec->aamix_out_paths[1],
2176 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002177 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002178 spec->aamix_out_paths[2],
2179 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002180 return 1;
2181}
2182
2183static const struct snd_kcontrol_new loopback_mixing_enum = {
2184 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2185 .name = "Loopback Mixing",
2186 .info = loopback_mixing_info,
2187 .get = loopback_mixing_get,
2188 .put = loopback_mixing_put,
2189};
2190
2191static int create_loopback_mixing_ctl(struct hda_codec *codec)
2192{
2193 struct hda_gen_spec *spec = codec->spec;
2194
2195 if (!spec->mixer_nid)
2196 return 0;
2197 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2198 spec->aamix_out_paths[2]))
2199 return 0;
2200 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2201 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002202 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002203 return 0;
2204}
2205
2206/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002207 * shared headphone/mic handling
2208 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002209
Takashi Iwai352f7f92012-12-19 12:52:06 +01002210static void call_update_outputs(struct hda_codec *codec);
2211
2212/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002213static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002214{
2215 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002216 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002217 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002218 hda_nid_t pin;
2219
2220 pin = spec->hp_mic_pin;
2221 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2222
2223 if (!force) {
2224 val = snd_hda_codec_get_pin_target(codec, pin);
2225 if (as_mic) {
2226 if (val & PIN_IN)
2227 return;
2228 } else {
2229 if (val & PIN_OUT)
2230 return;
2231 }
2232 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002233
2234 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002235 /* if the HP pin doesn't support VREF and the codec driver gives an
2236 * alternative pin, set up the VREF on that pin instead
2237 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002238 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2239 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2240 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2241 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002242 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002243 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002244 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002245
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002246 if (!spec->hp_mic_jack_modes) {
2247 if (as_mic)
2248 val |= PIN_IN;
2249 else
2250 val = PIN_HP;
2251 set_pin_target(codec, pin, val, true);
2252 update_hp_automute_hook(codec);
2253 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002254}
2255
2256/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002257static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002258{
2259 struct hda_gen_spec *spec = codec->spec;
2260 struct auto_pin_cfg *cfg = &spec->autocfg;
2261 unsigned int defcfg;
2262 hda_nid_t nid;
2263
Takashi Iwai967303d2013-02-19 17:12:42 +01002264 if (!spec->hp_mic) {
2265 if (spec->suppress_hp_mic_detect)
2266 return 0;
2267 /* automatic detection: only if no input or a single internal
2268 * input pin is found, try to detect the shared hp/mic
2269 */
2270 if (cfg->num_inputs > 1)
2271 return 0;
2272 else if (cfg->num_inputs == 1) {
2273 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2274 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2275 return 0;
2276 }
2277 }
2278
2279 spec->hp_mic = 0; /* clear once */
2280 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002281 return 0;
2282
Takashi Iwai967303d2013-02-19 17:12:42 +01002283 nid = 0;
2284 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2285 nid = cfg->line_out_pins[0];
2286 else if (cfg->hp_outs > 0)
2287 nid = cfg->hp_pins[0];
2288 if (!nid)
2289 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002290
2291 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2292 return 0; /* no input */
2293
Takashi Iwai967303d2013-02-19 17:12:42 +01002294 cfg->inputs[cfg->num_inputs].pin = nid;
2295 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2296 cfg->num_inputs++;
2297 spec->hp_mic = 1;
2298 spec->hp_mic_pin = nid;
2299 /* we can't handle auto-mic together with HP-mic */
2300 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002301 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2302 return 0;
2303}
2304
Takashi Iwai978e77e2013-01-10 16:57:58 +01002305/*
2306 * output jack mode
2307 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002308
2309static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2310
2311static const char * const out_jack_texts[] = {
2312 "Line Out", "Headphone Out",
2313};
2314
Takashi Iwai978e77e2013-01-10 16:57:58 +01002315static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2316 struct snd_ctl_elem_info *uinfo)
2317{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002318 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002319}
2320
2321static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2322 struct snd_ctl_elem_value *ucontrol)
2323{
2324 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2325 hda_nid_t nid = kcontrol->private_value;
2326 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2327 ucontrol->value.enumerated.item[0] = 1;
2328 else
2329 ucontrol->value.enumerated.item[0] = 0;
2330 return 0;
2331}
2332
2333static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_value *ucontrol)
2335{
2336 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2337 hda_nid_t nid = kcontrol->private_value;
2338 unsigned int val;
2339
2340 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2341 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2342 return 0;
2343 snd_hda_set_pin_ctl_cache(codec, nid, val);
2344 return 1;
2345}
2346
2347static const struct snd_kcontrol_new out_jack_mode_enum = {
2348 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2349 .info = out_jack_mode_info,
2350 .get = out_jack_mode_get,
2351 .put = out_jack_mode_put,
2352};
2353
2354static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2355{
2356 struct hda_gen_spec *spec = codec->spec;
2357 int i;
2358
2359 for (i = 0; i < spec->kctls.used; i++) {
2360 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2361 if (!strcmp(kctl->name, name) && kctl->index == idx)
2362 return true;
2363 }
2364 return false;
2365}
2366
2367static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2368 char *name, size_t name_len)
2369{
2370 struct hda_gen_spec *spec = codec->spec;
2371 int idx = 0;
2372
2373 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2374 strlcat(name, " Jack Mode", name_len);
2375
2376 for (; find_kctl_name(codec, name, idx); idx++)
2377 ;
2378}
2379
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002380static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2381{
2382 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002383 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002384 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2385 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2386 return 2;
2387 }
2388 return 1;
2389}
2390
Takashi Iwai978e77e2013-01-10 16:57:58 +01002391static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2392 hda_nid_t *pins)
2393{
2394 struct hda_gen_spec *spec = codec->spec;
2395 int i;
2396
2397 for (i = 0; i < num_pins; i++) {
2398 hda_nid_t pin = pins[i];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002399 if (pin == spec->hp_mic_pin) {
2400 int ret = create_hp_mic_jack_mode(codec, pin);
2401 if (ret < 0)
2402 return ret;
2403 continue;
2404 }
2405 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002406 struct snd_kcontrol_new *knew;
2407 char name[44];
2408 get_jack_mode_name(codec, pin, name, sizeof(name));
2409 knew = snd_hda_gen_add_kctl(spec, name,
2410 &out_jack_mode_enum);
2411 if (!knew)
2412 return -ENOMEM;
2413 knew->private_value = pin;
2414 }
2415 }
2416
2417 return 0;
2418}
2419
Takashi Iwai294765582013-01-17 09:52:11 +01002420/*
2421 * input jack mode
2422 */
2423
2424/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2425#define NUM_VREFS 6
2426
2427static const char * const vref_texts[NUM_VREFS] = {
2428 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2429 "", "Mic 80pc Bias", "Mic 100pc Bias"
2430};
2431
2432static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2433{
2434 unsigned int pincap;
2435
2436 pincap = snd_hda_query_pin_caps(codec, pin);
2437 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2438 /* filter out unusual vrefs */
2439 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2440 return pincap;
2441}
2442
2443/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2444static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2445{
2446 unsigned int i, n = 0;
2447
2448 for (i = 0; i < NUM_VREFS; i++) {
2449 if (vref_caps & (1 << i)) {
2450 if (n == item_idx)
2451 return i;
2452 n++;
2453 }
2454 }
2455 return 0;
2456}
2457
2458/* convert back from the vref ctl index to the enum item index */
2459static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2460{
2461 unsigned int i, n = 0;
2462
2463 for (i = 0; i < NUM_VREFS; i++) {
2464 if (i == idx)
2465 return n;
2466 if (vref_caps & (1 << i))
2467 n++;
2468 }
2469 return 0;
2470}
2471
2472static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2473 struct snd_ctl_elem_info *uinfo)
2474{
2475 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2476 hda_nid_t nid = kcontrol->private_value;
2477 unsigned int vref_caps = get_vref_caps(codec, nid);
2478
2479 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2480 vref_texts);
2481 /* set the right text */
2482 strcpy(uinfo->value.enumerated.name,
2483 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2484 return 0;
2485}
2486
2487static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2488 struct snd_ctl_elem_value *ucontrol)
2489{
2490 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2491 hda_nid_t nid = kcontrol->private_value;
2492 unsigned int vref_caps = get_vref_caps(codec, nid);
2493 unsigned int idx;
2494
2495 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2496 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2497 return 0;
2498}
2499
2500static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2501 struct snd_ctl_elem_value *ucontrol)
2502{
2503 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2504 hda_nid_t nid = kcontrol->private_value;
2505 unsigned int vref_caps = get_vref_caps(codec, nid);
2506 unsigned int val, idx;
2507
2508 val = snd_hda_codec_get_pin_target(codec, nid);
2509 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2510 if (idx == ucontrol->value.enumerated.item[0])
2511 return 0;
2512
2513 val &= ~AC_PINCTL_VREFEN;
2514 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2515 snd_hda_set_pin_ctl_cache(codec, nid, val);
2516 return 1;
2517}
2518
2519static const struct snd_kcontrol_new in_jack_mode_enum = {
2520 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2521 .info = in_jack_mode_info,
2522 .get = in_jack_mode_get,
2523 .put = in_jack_mode_put,
2524};
2525
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002526static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2527{
2528 struct hda_gen_spec *spec = codec->spec;
2529 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002530 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002531 nitems = hweight32(get_vref_caps(codec, pin));
2532 return nitems ? nitems : 1;
2533}
2534
Takashi Iwai294765582013-01-17 09:52:11 +01002535static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2536{
2537 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002538 struct snd_kcontrol_new *knew;
2539 char name[44];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002540 unsigned int defcfg;
2541
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002542 if (pin == spec->hp_mic_pin)
2543 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002544
2545 /* no jack mode for fixed pins */
2546 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2547 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2548 return 0;
2549
2550 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002551 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002552 return 0;
2553
2554 get_jack_mode_name(codec, pin, name, sizeof(name));
2555 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2556 if (!knew)
2557 return -ENOMEM;
2558 knew->private_value = pin;
2559 return 0;
2560}
2561
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002562/*
2563 * HP/mic shared jack mode
2564 */
2565static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2566 struct snd_ctl_elem_info *uinfo)
2567{
2568 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2569 hda_nid_t nid = kcontrol->private_value;
2570 int out_jacks = get_out_jack_num_items(codec, nid);
2571 int in_jacks = get_in_jack_num_items(codec, nid);
2572 const char *text = NULL;
2573 int idx;
2574
2575 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2576 uinfo->count = 1;
2577 uinfo->value.enumerated.items = out_jacks + in_jacks;
2578 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2579 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2580 idx = uinfo->value.enumerated.item;
2581 if (idx < out_jacks) {
2582 if (out_jacks > 1)
2583 text = out_jack_texts[idx];
2584 else
2585 text = "Headphone Out";
2586 } else {
2587 idx -= out_jacks;
2588 if (in_jacks > 1) {
2589 unsigned int vref_caps = get_vref_caps(codec, nid);
2590 text = vref_texts[get_vref_idx(vref_caps, idx)];
2591 } else
2592 text = "Mic In";
2593 }
2594
2595 strcpy(uinfo->value.enumerated.name, text);
2596 return 0;
2597}
2598
2599static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2600{
2601 int out_jacks = get_out_jack_num_items(codec, nid);
2602 int in_jacks = get_in_jack_num_items(codec, nid);
2603 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2604 int idx = 0;
2605
2606 if (val & PIN_OUT) {
2607 if (out_jacks > 1 && val == PIN_HP)
2608 idx = 1;
2609 } else if (val & PIN_IN) {
2610 idx = out_jacks;
2611 if (in_jacks > 1) {
2612 unsigned int vref_caps = get_vref_caps(codec, nid);
2613 val &= AC_PINCTL_VREFEN;
2614 idx += cvt_from_vref_idx(vref_caps, val);
2615 }
2616 }
2617 return idx;
2618}
2619
2620static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2621 struct snd_ctl_elem_value *ucontrol)
2622{
2623 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2624 hda_nid_t nid = kcontrol->private_value;
2625 ucontrol->value.enumerated.item[0] =
2626 get_cur_hp_mic_jack_mode(codec, nid);
2627 return 0;
2628}
2629
2630static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2631 struct snd_ctl_elem_value *ucontrol)
2632{
2633 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2634 hda_nid_t nid = kcontrol->private_value;
2635 int out_jacks = get_out_jack_num_items(codec, nid);
2636 int in_jacks = get_in_jack_num_items(codec, nid);
2637 unsigned int val, oldval, idx;
2638
2639 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2640 idx = ucontrol->value.enumerated.item[0];
2641 if (oldval == idx)
2642 return 0;
2643
2644 if (idx < out_jacks) {
2645 if (out_jacks > 1)
2646 val = idx ? PIN_HP : PIN_OUT;
2647 else
2648 val = PIN_HP;
2649 } else {
2650 idx -= out_jacks;
2651 if (in_jacks > 1) {
2652 unsigned int vref_caps = get_vref_caps(codec, nid);
2653 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002654 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2655 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002656 } else
2657 val = snd_hda_get_default_vref(codec, nid);
2658 }
2659 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002660 update_hp_automute_hook(codec);
2661
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002662 return 1;
2663}
2664
2665static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2666 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2667 .info = hp_mic_jack_mode_info,
2668 .get = hp_mic_jack_mode_get,
2669 .put = hp_mic_jack_mode_put,
2670};
2671
2672static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2673{
2674 struct hda_gen_spec *spec = codec->spec;
2675 struct snd_kcontrol_new *knew;
2676
2677 if (get_out_jack_num_items(codec, pin) <= 1 &&
2678 get_in_jack_num_items(codec, pin) <= 1)
2679 return 0; /* no need */
2680 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2681 &hp_mic_jack_mode_enum);
2682 if (!knew)
2683 return -ENOMEM;
2684 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002685 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002686 return 0;
2687}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002688
2689/*
2690 * Parse input paths
2691 */
2692
Takashi Iwai352f7f92012-12-19 12:52:06 +01002693/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002694static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002695{
2696 struct hda_amp_list *list;
2697
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002698 list = snd_array_new(&spec->loopback_list);
2699 if (!list)
2700 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002701 list->nid = mix;
2702 list->dir = HDA_INPUT;
2703 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002704 spec->loopback.amplist = spec->loopback_list.list;
2705 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002706}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002707
Takashi Iwai352f7f92012-12-19 12:52:06 +01002708/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002709static int new_analog_input(struct hda_codec *codec, int input_idx,
2710 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002711 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002713 struct hda_gen_spec *spec = codec->spec;
2714 struct nid_path *path;
2715 unsigned int val;
2716 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Takashi Iwai352f7f92012-12-19 12:52:06 +01002718 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2719 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2720 return 0; /* no need for analog loopback */
2721
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002722 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002723 if (!path)
2724 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002725 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002726 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002727
2728 idx = path->idx[path->depth - 1];
2729 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2730 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2731 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002732 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002734 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 }
2736
Takashi Iwai352f7f92012-12-19 12:52:06 +01002737 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2738 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2739 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002740 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002742 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 }
2744
Takashi Iwai352f7f92012-12-19 12:52:06 +01002745 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002746 err = add_loopback_list(spec, mix_nid, idx);
2747 if (err < 0)
2748 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002749
2750 if (spec->mixer_nid != spec->mixer_merge_nid &&
2751 !spec->loopback_merge_path) {
2752 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2753 spec->mixer_merge_nid, 0);
2754 if (path) {
2755 print_nid_path("loopback-merge", path);
2756 path->active = true;
2757 spec->loopback_merge_path =
2758 snd_hda_get_path_idx(codec, path);
2759 }
2760 }
2761
Takashi Iwai352f7f92012-12-19 12:52:06 +01002762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
Takashi Iwai352f7f92012-12-19 12:52:06 +01002765static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002767 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2768 return (pincap & AC_PINCAP_IN) != 0;
2769}
2770
2771/* Parse the codec tree and retrieve ADCs */
2772static int fill_adc_nids(struct hda_codec *codec)
2773{
2774 struct hda_gen_spec *spec = codec->spec;
2775 hda_nid_t nid;
2776 hda_nid_t *adc_nids = spec->adc_nids;
2777 int max_nums = ARRAY_SIZE(spec->adc_nids);
2778 int i, nums = 0;
2779
2780 nid = codec->start_nid;
2781 for (i = 0; i < codec->num_nodes; i++, nid++) {
2782 unsigned int caps = get_wcaps(codec, nid);
2783 int type = get_wcaps_type(caps);
2784
2785 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2786 continue;
2787 adc_nids[nums] = nid;
2788 if (++nums >= max_nums)
2789 break;
2790 }
2791 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002792
2793 /* copy the detected ADCs to all_adcs[] */
2794 spec->num_all_adcs = nums;
2795 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2796
Takashi Iwai352f7f92012-12-19 12:52:06 +01002797 return nums;
2798}
2799
2800/* filter out invalid adc_nids that don't give all active input pins;
2801 * if needed, check whether dynamic ADC-switching is available
2802 */
2803static int check_dyn_adc_switch(struct hda_codec *codec)
2804{
2805 struct hda_gen_spec *spec = codec->spec;
2806 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002807 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002808 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002809
Takashi Iwai352f7f92012-12-19 12:52:06 +01002810 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002811 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002812 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002813 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002814 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002815 break;
2816 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002817 if (i >= imux->num_items) {
2818 ok_bits |= (1 << n);
2819 nums++;
2820 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002821 }
2822
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002823 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002824 /* check whether ADC-switch is possible */
2825 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002826 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002827 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002828 spec->dyn_adc_idx[i] = n;
2829 break;
2830 }
2831 }
2832 }
2833
2834 snd_printdd("hda-codec: enabling ADC switching\n");
2835 spec->dyn_adc_switch = 1;
2836 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002837 /* shrink the invalid adcs and input paths */
2838 nums = 0;
2839 for (n = 0; n < spec->num_adc_nids; n++) {
2840 if (!(ok_bits & (1 << n)))
2841 continue;
2842 if (n != nums) {
2843 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002844 for (i = 0; i < imux->num_items; i++) {
2845 invalidate_nid_path(codec,
2846 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002847 spec->input_paths[i][nums] =
2848 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002849 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002850 }
2851 nums++;
2852 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002853 spec->num_adc_nids = nums;
2854 }
2855
Takashi Iwai967303d2013-02-19 17:12:42 +01002856 if (imux->num_items == 1 ||
2857 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002858 snd_printdd("hda-codec: reducing to a single ADC\n");
2859 spec->num_adc_nids = 1; /* reduce to a single ADC */
2860 }
2861
2862 /* single index for individual volumes ctls */
2863 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2864 spec->num_adc_nids = 1;
2865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return 0;
2867}
2868
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002869/* parse capture source paths from the given pin and create imux items */
2870static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002871 int cfg_idx, int num_adcs,
2872 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002873{
2874 struct hda_gen_spec *spec = codec->spec;
2875 struct hda_input_mux *imux = &spec->input_mux;
2876 int imux_idx = imux->num_items;
2877 bool imux_added = false;
2878 int c;
2879
2880 for (c = 0; c < num_adcs; c++) {
2881 struct nid_path *path;
2882 hda_nid_t adc = spec->adc_nids[c];
2883
2884 if (!is_reachable_path(codec, pin, adc))
2885 continue;
2886 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2887 if (!path)
2888 continue;
2889 print_nid_path("input", path);
2890 spec->input_paths[imux_idx][c] =
2891 snd_hda_get_path_idx(codec, path);
2892
2893 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01002894 if (spec->hp_mic_pin == pin)
2895 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002896 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002897 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002898 imux_added = true;
2899 }
2900 }
2901
2902 return 0;
2903}
2904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002906 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002908
Takashi Iwaic9700422013-01-18 10:17:30 +01002909/* fill the label for each input at first */
2910static int fill_input_pin_labels(struct hda_codec *codec)
2911{
2912 struct hda_gen_spec *spec = codec->spec;
2913 const struct auto_pin_cfg *cfg = &spec->autocfg;
2914 int i;
2915
2916 for (i = 0; i < cfg->num_inputs; i++) {
2917 hda_nid_t pin = cfg->inputs[i].pin;
2918 const char *label;
2919 int j, idx;
2920
2921 if (!is_input_pin(codec, pin))
2922 continue;
2923
2924 label = hda_get_autocfg_input_label(codec, cfg, i);
2925 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002926 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002927 if (spec->input_labels[j] &&
2928 !strcmp(spec->input_labels[j], label)) {
2929 idx = spec->input_label_idxs[j] + 1;
2930 break;
2931 }
2932 }
2933
2934 spec->input_labels[i] = label;
2935 spec->input_label_idxs[i] = idx;
2936 }
2937
2938 return 0;
2939}
2940
Takashi Iwai9dba2052013-01-18 10:01:15 +01002941#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2942
Takashi Iwai352f7f92012-12-19 12:52:06 +01002943static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002944{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002945 struct hda_gen_spec *spec = codec->spec;
2946 const struct auto_pin_cfg *cfg = &spec->autocfg;
2947 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002948 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002949 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002950 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002951
Takashi Iwai352f7f92012-12-19 12:52:06 +01002952 num_adcs = fill_adc_nids(codec);
2953 if (num_adcs < 0)
2954 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002955
Takashi Iwaic9700422013-01-18 10:17:30 +01002956 err = fill_input_pin_labels(codec);
2957 if (err < 0)
2958 return err;
2959
Takashi Iwai352f7f92012-12-19 12:52:06 +01002960 for (i = 0; i < cfg->num_inputs; i++) {
2961 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Takashi Iwai352f7f92012-12-19 12:52:06 +01002963 pin = cfg->inputs[i].pin;
2964 if (!is_input_pin(codec, pin))
2965 continue;
2966
Takashi Iwai2c12c302013-01-10 09:33:29 +01002967 val = PIN_IN;
2968 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2969 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01002970 if (pin != spec->hp_mic_pin)
2971 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002972
Takashi Iwai352f7f92012-12-19 12:52:06 +01002973 if (mixer) {
2974 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002975 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002976 spec->input_labels[i],
2977 spec->input_label_idxs[i],
2978 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002979 if (err < 0)
2980 return err;
2981 }
2982 }
2983
Takashi Iwaic9700422013-01-18 10:17:30 +01002984 err = parse_capture_source(codec, pin, i, num_adcs,
2985 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002986 if (err < 0)
2987 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002988
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002989 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01002990 err = create_in_jack_mode(codec, pin);
2991 if (err < 0)
2992 return err;
2993 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002994 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002995
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002996 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002997 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002998 "Stereo Mix", 0);
2999 if (err < 0)
3000 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003001 }
3002
3003 return 0;
3004}
3005
3006
3007/*
3008 * input source mux
3009 */
3010
Takashi Iwaic697b712013-01-07 17:09:26 +01003011/* get the input path specified by the given adc and imux indices */
3012static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003013{
3014 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003015 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3016 snd_BUG();
3017 return NULL;
3018 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003019 if (spec->dyn_adc_switch)
3020 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003021 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003022 snd_BUG();
3023 return NULL;
3024 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003025 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003026}
3027
3028static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3029 unsigned int idx);
3030
3031static int mux_enum_info(struct snd_kcontrol *kcontrol,
3032 struct snd_ctl_elem_info *uinfo)
3033{
3034 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3035 struct hda_gen_spec *spec = codec->spec;
3036 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3037}
3038
3039static int mux_enum_get(struct snd_kcontrol *kcontrol,
3040 struct snd_ctl_elem_value *ucontrol)
3041{
3042 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3043 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003044 /* the ctls are created at once with multiple counts */
3045 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003046
3047 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3048 return 0;
3049}
3050
3051static int mux_enum_put(struct snd_kcontrol *kcontrol,
3052 struct snd_ctl_elem_value *ucontrol)
3053{
3054 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003055 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003056 return mux_select(codec, adc_idx,
3057 ucontrol->value.enumerated.item[0]);
3058}
3059
Takashi Iwai352f7f92012-12-19 12:52:06 +01003060static const struct snd_kcontrol_new cap_src_temp = {
3061 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3062 .name = "Input Source",
3063 .info = mux_enum_info,
3064 .get = mux_enum_get,
3065 .put = mux_enum_put,
3066};
3067
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003068/*
3069 * capture volume and capture switch ctls
3070 */
3071
Takashi Iwai352f7f92012-12-19 12:52:06 +01003072typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3073 struct snd_ctl_elem_value *ucontrol);
3074
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003075/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003076static int cap_put_caller(struct snd_kcontrol *kcontrol,
3077 struct snd_ctl_elem_value *ucontrol,
3078 put_call_t func, int type)
3079{
3080 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3081 struct hda_gen_spec *spec = codec->spec;
3082 const struct hda_input_mux *imux;
3083 struct nid_path *path;
3084 int i, adc_idx, err = 0;
3085
3086 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003087 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003088 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003089 /* we use the cache-only update at first since multiple input paths
3090 * may shared the same amp; by updating only caches, the redundant
3091 * writes to hardware can be reduced.
3092 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003093 codec->cached_write = 1;
3094 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003095 path = get_input_path(codec, adc_idx, i);
3096 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003097 continue;
3098 kcontrol->private_value = path->ctls[type];
3099 err = func(kcontrol, ucontrol);
3100 if (err < 0)
3101 goto error;
3102 }
3103 error:
3104 codec->cached_write = 0;
3105 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003106 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003107 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003108 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003109 return err;
3110}
3111
3112/* capture volume ctl callbacks */
3113#define cap_vol_info snd_hda_mixer_amp_volume_info
3114#define cap_vol_get snd_hda_mixer_amp_volume_get
3115#define cap_vol_tlv snd_hda_mixer_amp_tlv
3116
3117static int cap_vol_put(struct snd_kcontrol *kcontrol,
3118 struct snd_ctl_elem_value *ucontrol)
3119{
3120 return cap_put_caller(kcontrol, ucontrol,
3121 snd_hda_mixer_amp_volume_put,
3122 NID_PATH_VOL_CTL);
3123}
3124
3125static const struct snd_kcontrol_new cap_vol_temp = {
3126 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3127 .name = "Capture Volume",
3128 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3129 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3130 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3131 .info = cap_vol_info,
3132 .get = cap_vol_get,
3133 .put = cap_vol_put,
3134 .tlv = { .c = cap_vol_tlv },
3135};
3136
3137/* capture switch ctl callbacks */
3138#define cap_sw_info snd_ctl_boolean_stereo_info
3139#define cap_sw_get snd_hda_mixer_amp_switch_get
3140
3141static int cap_sw_put(struct snd_kcontrol *kcontrol,
3142 struct snd_ctl_elem_value *ucontrol)
3143{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003144 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003145 snd_hda_mixer_amp_switch_put,
3146 NID_PATH_MUTE_CTL);
3147}
3148
3149static const struct snd_kcontrol_new cap_sw_temp = {
3150 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3151 .name = "Capture Switch",
3152 .info = cap_sw_info,
3153 .get = cap_sw_get,
3154 .put = cap_sw_put,
3155};
3156
3157static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3158{
3159 hda_nid_t nid;
3160 int i, depth;
3161
3162 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3163 for (depth = 0; depth < 3; depth++) {
3164 if (depth >= path->depth)
3165 return -EINVAL;
3166 i = path->depth - depth - 1;
3167 nid = path->path[i];
3168 if (!path->ctls[NID_PATH_VOL_CTL]) {
3169 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3170 path->ctls[NID_PATH_VOL_CTL] =
3171 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3172 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3173 int idx = path->idx[i];
3174 if (!depth && codec->single_adc_amp)
3175 idx = 0;
3176 path->ctls[NID_PATH_VOL_CTL] =
3177 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3178 }
3179 }
3180 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3181 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3182 path->ctls[NID_PATH_MUTE_CTL] =
3183 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3184 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3185 int idx = path->idx[i];
3186 if (!depth && codec->single_adc_amp)
3187 idx = 0;
3188 path->ctls[NID_PATH_MUTE_CTL] =
3189 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3190 }
3191 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return 0;
3194}
3195
Takashi Iwai352f7f92012-12-19 12:52:06 +01003196static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003198 struct hda_gen_spec *spec = codec->spec;
3199 struct auto_pin_cfg *cfg = &spec->autocfg;
3200 unsigned int val;
3201 int i;
3202
3203 if (!spec->inv_dmic_split)
3204 return false;
3205 for (i = 0; i < cfg->num_inputs; i++) {
3206 if (cfg->inputs[i].pin != nid)
3207 continue;
3208 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3209 return false;
3210 val = snd_hda_codec_get_pincfg(codec, nid);
3211 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3212 }
3213 return false;
3214}
3215
Takashi Iwaia90229e2013-01-18 14:10:00 +01003216/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003217static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3218 struct snd_ctl_elem_value *ucontrol)
3219{
3220 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3221 struct hda_gen_spec *spec = codec->spec;
3222 int ret;
3223
3224 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3225 if (ret < 0)
3226 return ret;
3227
Takashi Iwaia90229e2013-01-18 14:10:00 +01003228 if (spec->cap_sync_hook)
3229 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003230
3231 return ret;
3232}
3233
Takashi Iwai352f7f92012-12-19 12:52:06 +01003234static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3235 int idx, bool is_switch, unsigned int ctl,
3236 bool inv_dmic)
3237{
3238 struct hda_gen_spec *spec = codec->spec;
3239 char tmpname[44];
3240 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3241 const char *sfx = is_switch ? "Switch" : "Volume";
3242 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003243 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003244
3245 if (!ctl)
3246 return 0;
3247
3248 if (label)
3249 snprintf(tmpname, sizeof(tmpname),
3250 "%s Capture %s", label, sfx);
3251 else
3252 snprintf(tmpname, sizeof(tmpname),
3253 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003254 knew = add_control(spec, type, tmpname, idx,
3255 amp_val_replace_channels(ctl, chs));
3256 if (!knew)
3257 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003258 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003259 knew->put = cap_single_sw_put;
3260 if (!inv_dmic)
3261 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003262
3263 /* Make independent right kcontrol */
3264 if (label)
3265 snprintf(tmpname, sizeof(tmpname),
3266 "Inverted %s Capture %s", label, sfx);
3267 else
3268 snprintf(tmpname, sizeof(tmpname),
3269 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003270 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003271 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003272 if (!knew)
3273 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003274 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003275 knew->put = cap_single_sw_put;
3276 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003277}
3278
3279/* create single (and simple) capture volume and switch controls */
3280static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3281 unsigned int vol_ctl, unsigned int sw_ctl,
3282 bool inv_dmic)
3283{
3284 int err;
3285 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3286 if (err < 0)
3287 return err;
3288 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3289 if (err < 0)
3290 return err;
3291 return 0;
3292}
3293
3294/* create bound capture volume and switch controls */
3295static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3296 unsigned int vol_ctl, unsigned int sw_ctl)
3297{
3298 struct hda_gen_spec *spec = codec->spec;
3299 struct snd_kcontrol_new *knew;
3300
3301 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003302 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003303 if (!knew)
3304 return -ENOMEM;
3305 knew->index = idx;
3306 knew->private_value = vol_ctl;
3307 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3308 }
3309 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003310 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003311 if (!knew)
3312 return -ENOMEM;
3313 knew->index = idx;
3314 knew->private_value = sw_ctl;
3315 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3316 }
3317 return 0;
3318}
3319
3320/* return the vol ctl when used first in the imux list */
3321static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3322{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003323 struct nid_path *path;
3324 unsigned int ctl;
3325 int i;
3326
Takashi Iwaic697b712013-01-07 17:09:26 +01003327 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003328 if (!path)
3329 return 0;
3330 ctl = path->ctls[type];
3331 if (!ctl)
3332 return 0;
3333 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003334 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003335 if (path && path->ctls[type] == ctl)
3336 return 0;
3337 }
3338 return ctl;
3339}
3340
3341/* create individual capture volume and switch controls per input */
3342static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3343{
3344 struct hda_gen_spec *spec = codec->spec;
3345 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003346 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003347
3348 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003349 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003350 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003351
Takashi Iwaic9700422013-01-18 10:17:30 +01003352 idx = imux->items[i].index;
3353 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003354 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003355 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3356
3357 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003358 err = add_single_cap_ctl(codec,
3359 spec->input_labels[idx],
3360 spec->input_label_idxs[idx],
3361 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003362 get_first_cap_ctl(codec, i, type),
3363 inv_dmic);
3364 if (err < 0)
3365 return err;
3366 }
3367 }
3368 return 0;
3369}
3370
3371static int create_capture_mixers(struct hda_codec *codec)
3372{
3373 struct hda_gen_spec *spec = codec->spec;
3374 struct hda_input_mux *imux = &spec->input_mux;
3375 int i, n, nums, err;
3376
3377 if (spec->dyn_adc_switch)
3378 nums = 1;
3379 else
3380 nums = spec->num_adc_nids;
3381
3382 if (!spec->auto_mic && imux->num_items > 1) {
3383 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003384 const char *name;
3385 name = nums > 1 ? "Input Source" : "Capture Source";
3386 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003387 if (!knew)
3388 return -ENOMEM;
3389 knew->count = nums;
3390 }
3391
3392 for (n = 0; n < nums; n++) {
3393 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003394 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003395 bool inv_dmic = false;
3396 int vol, sw;
3397
3398 vol = sw = 0;
3399 for (i = 0; i < imux->num_items; i++) {
3400 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003401 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003402 if (!path)
3403 continue;
3404 parse_capvol_in_path(codec, path);
3405 if (!vol)
3406 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003407 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003408 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003409 if (!same_amp_caps(codec, vol,
3410 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3411 multi_cap_vol = true;
3412 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003413 if (!sw)
3414 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003415 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003416 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003417 if (!same_amp_caps(codec, sw,
3418 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3419 multi_cap_vol = true;
3420 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003421 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3422 inv_dmic = true;
3423 }
3424
3425 if (!multi)
3426 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3427 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003428 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003429 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3430 else
3431 err = create_multi_cap_vol_ctl(codec);
3432 if (err < 0)
3433 return err;
3434 }
3435
3436 return 0;
3437}
3438
3439/*
3440 * add mic boosts if needed
3441 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003442
3443/* check whether the given amp is feasible as a boost volume */
3444static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3445 int dir, int idx)
3446{
3447 unsigned int step;
3448
3449 if (!nid_has_volume(codec, nid, dir) ||
3450 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3451 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3452 return false;
3453
3454 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3455 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3456 if (step < 0x20)
3457 return false;
3458 return true;
3459}
3460
3461/* look for a boost amp in a widget close to the pin */
3462static unsigned int look_for_boost_amp(struct hda_codec *codec,
3463 struct nid_path *path)
3464{
3465 unsigned int val = 0;
3466 hda_nid_t nid;
3467 int depth;
3468
3469 for (depth = 0; depth < 3; depth++) {
3470 if (depth >= path->depth - 1)
3471 break;
3472 nid = path->path[depth];
3473 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3474 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3475 break;
3476 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3477 path->idx[depth])) {
3478 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3479 HDA_INPUT);
3480 break;
3481 }
3482 }
3483
3484 return val;
3485}
3486
Takashi Iwai352f7f92012-12-19 12:52:06 +01003487static int parse_mic_boost(struct hda_codec *codec)
3488{
3489 struct hda_gen_spec *spec = codec->spec;
3490 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003491 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003492 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003493
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003494 if (!spec->num_adc_nids)
3495 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003496
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003497 for (i = 0; i < imux->num_items; i++) {
3498 struct nid_path *path;
3499 unsigned int val;
3500 int idx;
3501 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003502
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003503 idx = imux->items[i].index;
3504 if (idx >= imux->num_items)
3505 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003506
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003507 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003508 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003509 continue;
3510
3511 path = get_input_path(codec, 0, i);
3512 if (!path)
3513 continue;
3514
3515 val = look_for_boost_amp(codec, path);
3516 if (!val)
3517 continue;
3518
3519 /* create a boost control */
3520 snprintf(boost_label, sizeof(boost_label),
3521 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003522 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3523 spec->input_label_idxs[idx], val))
3524 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003525
3526 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003527 }
3528 return 0;
3529}
3530
3531/*
3532 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3533 */
3534static void parse_digital(struct hda_codec *codec)
3535{
3536 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003537 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003538 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003539 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003540
3541 /* support multiple SPDIFs; the secondary is set up as a slave */
3542 nums = 0;
3543 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003544 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003545 dig_nid = look_for_dac(codec, pin, true);
3546 if (!dig_nid)
3547 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003548 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003549 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003550 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003551 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003552 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003553 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003554 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003555 if (!nums) {
3556 spec->multiout.dig_out_nid = dig_nid;
3557 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3558 } else {
3559 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3560 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3561 break;
3562 spec->slave_dig_outs[nums - 1] = dig_nid;
3563 }
3564 nums++;
3565 }
3566
3567 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003568 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003569 dig_nid = codec->start_nid;
3570 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003571 unsigned int wcaps = get_wcaps(codec, dig_nid);
3572 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3573 continue;
3574 if (!(wcaps & AC_WCAP_DIGITAL))
3575 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003576 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003577 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003578 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003579 path->active = true;
3580 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003581 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003582 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003583 break;
3584 }
3585 }
3586 }
3587}
3588
3589
3590/*
3591 * input MUX handling
3592 */
3593
3594static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3595
3596/* select the given imux item; either unmute exclusively or select the route */
3597static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3598 unsigned int idx)
3599{
3600 struct hda_gen_spec *spec = codec->spec;
3601 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003602 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003603
3604 imux = &spec->input_mux;
3605 if (!imux->num_items)
3606 return 0;
3607
3608 if (idx >= imux->num_items)
3609 idx = imux->num_items - 1;
3610 if (spec->cur_mux[adc_idx] == idx)
3611 return 0;
3612
Takashi Iwai55196ff2013-01-24 17:32:56 +01003613 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3614 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003615 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003616 if (old_path->active)
3617 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003618
3619 spec->cur_mux[adc_idx] = idx;
3620
Takashi Iwai967303d2013-02-19 17:12:42 +01003621 if (spec->hp_mic)
3622 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003623
3624 if (spec->dyn_adc_switch)
3625 dyn_adc_pcm_resetup(codec, idx);
3626
Takashi Iwaic697b712013-01-07 17:09:26 +01003627 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003628 if (!path)
3629 return 0;
3630 if (path->active)
3631 return 0;
3632 snd_hda_activate_path(codec, path, true, false);
3633 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003634 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003635 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003636 return 1;
3637}
3638
3639
3640/*
3641 * Jack detections for HP auto-mute and mic-switch
3642 */
3643
3644/* check each pin in the given array; returns true if any of them is plugged */
3645static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3646{
3647 int i, present = 0;
3648
3649 for (i = 0; i < num_pins; i++) {
3650 hda_nid_t nid = pins[i];
3651 if (!nid)
3652 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003653 /* don't detect pins retasked as inputs */
3654 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3655 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003656 present |= snd_hda_jack_detect(codec, nid);
3657 }
3658 return present;
3659}
3660
3661/* standard HP/line-out auto-mute helper */
3662static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003663 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003664{
3665 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003666 int i;
3667
3668 for (i = 0; i < num_pins; i++) {
3669 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003670 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003671 if (!nid)
3672 break;
Takashi Iwai967303d2013-02-19 17:12:42 +01003673 oldval = snd_hda_codec_get_pin_target(codec, nid);
3674 if (oldval & PIN_IN)
3675 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003676 /* don't reset VREF value in case it's controlling
3677 * the amp (see alc861_fixup_asus_amp_vref_0f())
3678 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003679 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003680 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003681 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003682 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003683 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003684 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003685 /* here we call update_pin_ctl() so that the pinctl is changed
3686 * without changing the pinctl target value;
3687 * the original target value will be still referred at the
3688 * init / resume again
3689 */
3690 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003691 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003692 }
3693}
3694
3695/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003696void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003697{
3698 struct hda_gen_spec *spec = codec->spec;
3699 int on;
3700
3701 /* Control HP pins/amps depending on master_mute state;
3702 * in general, HP pins/amps control should be enabled in all cases,
3703 * but currently set only for master_mute, just to be safe
3704 */
Takashi Iwai967303d2013-02-19 17:12:42 +01003705 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003706 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003707
3708 if (!spec->automute_speaker)
3709 on = 0;
3710 else
3711 on = spec->hp_jack_present | spec->line_jack_present;
3712 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003713 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003714 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003715 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003716
3717 /* toggle line-out mutes if needed, too */
3718 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3719 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3720 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3721 return;
3722 if (!spec->automute_lo)
3723 on = 0;
3724 else
3725 on = spec->hp_jack_present;
3726 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003727 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003728 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003729 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003730}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003731EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003732
3733static void call_update_outputs(struct hda_codec *codec)
3734{
3735 struct hda_gen_spec *spec = codec->spec;
3736 if (spec->automute_hook)
3737 spec->automute_hook(codec);
3738 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003739 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003740}
3741
3742/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003743void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003744{
3745 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003746 hda_nid_t *pins = spec->autocfg.hp_pins;
3747 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003748
Takashi Iwai92603c52013-01-22 07:46:31 +01003749 /* No detection for the first HP jack during indep-HP mode */
3750 if (spec->indep_hp_enabled) {
3751 pins++;
3752 num_pins--;
3753 }
3754
3755 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003756 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3757 return;
3758 call_update_outputs(codec);
3759}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003760EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003761
3762/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003763void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003764{
3765 struct hda_gen_spec *spec = codec->spec;
3766
3767 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3768 return;
3769 /* check LO jack only when it's different from HP */
3770 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3771 return;
3772
3773 spec->line_jack_present =
3774 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3775 spec->autocfg.line_out_pins);
3776 if (!spec->automute_speaker || !spec->detect_lo)
3777 return;
3778 call_update_outputs(codec);
3779}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003780EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003781
3782/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003783void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003784{
3785 struct hda_gen_spec *spec = codec->spec;
3786 int i;
3787
3788 if (!spec->auto_mic)
3789 return;
3790
3791 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003792 hda_nid_t pin = spec->am_entry[i].pin;
3793 /* don't detect pins retasked as outputs */
3794 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3795 continue;
3796 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003797 mux_select(codec, 0, spec->am_entry[i].idx);
3798 return;
3799 }
3800 }
3801 mux_select(codec, 0, spec->am_entry[0].idx);
3802}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003803EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003804
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003805/* update jack retasking */
3806static void update_automute_all(struct hda_codec *codec)
3807{
3808 struct hda_gen_spec *spec = codec->spec;
3809
Takashi Iwai8ba955c2013-03-07 18:40:58 +01003810 update_hp_automute_hook(codec);
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003811 if (spec->line_automute_hook)
3812 spec->line_automute_hook(codec, NULL);
3813 else
3814 snd_hda_gen_line_automute(codec, NULL);
3815 if (spec->mic_autoswitch_hook)
3816 spec->mic_autoswitch_hook(codec, NULL);
3817 else
3818 snd_hda_gen_mic_autoswitch(codec, NULL);
3819}
3820
Takashi Iwai352f7f92012-12-19 12:52:06 +01003821/*
3822 * Auto-Mute mode mixer enum support
3823 */
3824static int automute_mode_info(struct snd_kcontrol *kcontrol,
3825 struct snd_ctl_elem_info *uinfo)
3826{
3827 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3828 struct hda_gen_spec *spec = codec->spec;
3829 static const char * const texts3[] = {
3830 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003831 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
Takashi Iwai352f7f92012-12-19 12:52:06 +01003833 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3834 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3835 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3836}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
Takashi Iwai352f7f92012-12-19 12:52:06 +01003838static int automute_mode_get(struct snd_kcontrol *kcontrol,
3839 struct snd_ctl_elem_value *ucontrol)
3840{
3841 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3842 struct hda_gen_spec *spec = codec->spec;
3843 unsigned int val = 0;
3844 if (spec->automute_speaker)
3845 val++;
3846 if (spec->automute_lo)
3847 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003848
Takashi Iwai352f7f92012-12-19 12:52:06 +01003849 ucontrol->value.enumerated.item[0] = val;
3850 return 0;
3851}
3852
3853static int automute_mode_put(struct snd_kcontrol *kcontrol,
3854 struct snd_ctl_elem_value *ucontrol)
3855{
3856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3857 struct hda_gen_spec *spec = codec->spec;
3858
3859 switch (ucontrol->value.enumerated.item[0]) {
3860 case 0:
3861 if (!spec->automute_speaker && !spec->automute_lo)
3862 return 0;
3863 spec->automute_speaker = 0;
3864 spec->automute_lo = 0;
3865 break;
3866 case 1:
3867 if (spec->automute_speaker_possible) {
3868 if (!spec->automute_lo && spec->automute_speaker)
3869 return 0;
3870 spec->automute_speaker = 1;
3871 spec->automute_lo = 0;
3872 } else if (spec->automute_lo_possible) {
3873 if (spec->automute_lo)
3874 return 0;
3875 spec->automute_lo = 1;
3876 } else
3877 return -EINVAL;
3878 break;
3879 case 2:
3880 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3881 return -EINVAL;
3882 if (spec->automute_speaker && spec->automute_lo)
3883 return 0;
3884 spec->automute_speaker = 1;
3885 spec->automute_lo = 1;
3886 break;
3887 default:
3888 return -EINVAL;
3889 }
3890 call_update_outputs(codec);
3891 return 1;
3892}
3893
3894static const struct snd_kcontrol_new automute_mode_enum = {
3895 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3896 .name = "Auto-Mute Mode",
3897 .info = automute_mode_info,
3898 .get = automute_mode_get,
3899 .put = automute_mode_put,
3900};
3901
3902static int add_automute_mode_enum(struct hda_codec *codec)
3903{
3904 struct hda_gen_spec *spec = codec->spec;
3905
Takashi Iwai12c93df2012-12-19 14:38:33 +01003906 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003907 return -ENOMEM;
3908 return 0;
3909}
3910
3911/*
3912 * Check the availability of HP/line-out auto-mute;
3913 * Set up appropriately if really supported
3914 */
3915static int check_auto_mute_availability(struct hda_codec *codec)
3916{
3917 struct hda_gen_spec *spec = codec->spec;
3918 struct auto_pin_cfg *cfg = &spec->autocfg;
3919 int present = 0;
3920 int i, err;
3921
Takashi Iwaif72706b2013-01-16 18:20:07 +01003922 if (spec->suppress_auto_mute)
3923 return 0;
3924
Takashi Iwai352f7f92012-12-19 12:52:06 +01003925 if (cfg->hp_pins[0])
3926 present++;
3927 if (cfg->line_out_pins[0])
3928 present++;
3929 if (cfg->speaker_pins[0])
3930 present++;
3931 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003932 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003933
3934 if (!cfg->speaker_pins[0] &&
3935 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3936 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3937 sizeof(cfg->speaker_pins));
3938 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Takashi Iwai352f7f92012-12-19 12:52:06 +01003941 if (!cfg->hp_pins[0] &&
3942 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3943 memcpy(cfg->hp_pins, cfg->line_out_pins,
3944 sizeof(cfg->hp_pins));
3945 cfg->hp_outs = cfg->line_outs;
3946 }
3947
3948 for (i = 0; i < cfg->hp_outs; i++) {
3949 hda_nid_t nid = cfg->hp_pins[i];
3950 if (!is_jack_detectable(codec, nid))
3951 continue;
3952 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3953 nid);
3954 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003955 spec->hp_automute_hook ?
3956 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003957 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003958 spec->detect_hp = 1;
3959 }
3960
3961 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3962 if (cfg->speaker_outs)
3963 for (i = 0; i < cfg->line_outs; i++) {
3964 hda_nid_t nid = cfg->line_out_pins[i];
3965 if (!is_jack_detectable(codec, nid))
3966 continue;
3967 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3968 snd_hda_jack_detect_enable_callback(codec, nid,
3969 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003970 spec->line_automute_hook ?
3971 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003972 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003973 spec->detect_lo = 1;
3974 }
3975 spec->automute_lo_possible = spec->detect_hp;
3976 }
3977
3978 spec->automute_speaker_possible = cfg->speaker_outs &&
3979 (spec->detect_hp || spec->detect_lo);
3980
3981 spec->automute_lo = spec->automute_lo_possible;
3982 spec->automute_speaker = spec->automute_speaker_possible;
3983
3984 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3985 /* create a control for automute mode */
3986 err = add_automute_mode_enum(codec);
3987 if (err < 0)
3988 return err;
3989 }
3990 return 0;
3991}
3992
Takashi Iwai352f7f92012-12-19 12:52:06 +01003993/* check whether all auto-mic pins are valid; setup indices if OK */
3994static bool auto_mic_check_imux(struct hda_codec *codec)
3995{
3996 struct hda_gen_spec *spec = codec->spec;
3997 const struct hda_input_mux *imux;
3998 int i;
3999
4000 imux = &spec->input_mux;
4001 for (i = 0; i < spec->am_num_entries; i++) {
4002 spec->am_entry[i].idx =
4003 find_idx_in_nid_list(spec->am_entry[i].pin,
4004 spec->imux_pins, imux->num_items);
4005 if (spec->am_entry[i].idx < 0)
4006 return false; /* no corresponding imux */
4007 }
4008
4009 /* we don't need the jack detection for the first pin */
4010 for (i = 1; i < spec->am_num_entries; i++)
4011 snd_hda_jack_detect_enable_callback(codec,
4012 spec->am_entry[i].pin,
4013 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004014 spec->mic_autoswitch_hook ?
4015 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004016 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004017 return true;
4018}
4019
4020static int compare_attr(const void *ap, const void *bp)
4021{
4022 const struct automic_entry *a = ap;
4023 const struct automic_entry *b = bp;
4024 return (int)(a->attr - b->attr);
4025}
4026
4027/*
4028 * Check the availability of auto-mic switch;
4029 * Set up if really supported
4030 */
4031static int check_auto_mic_availability(struct hda_codec *codec)
4032{
4033 struct hda_gen_spec *spec = codec->spec;
4034 struct auto_pin_cfg *cfg = &spec->autocfg;
4035 unsigned int types;
4036 int i, num_pins;
4037
Takashi Iwaid12daf62013-01-07 16:32:11 +01004038 if (spec->suppress_auto_mic)
4039 return 0;
4040
Takashi Iwai352f7f92012-12-19 12:52:06 +01004041 types = 0;
4042 num_pins = 0;
4043 for (i = 0; i < cfg->num_inputs; i++) {
4044 hda_nid_t nid = cfg->inputs[i].pin;
4045 unsigned int attr;
4046 attr = snd_hda_codec_get_pincfg(codec, nid);
4047 attr = snd_hda_get_input_pin_attr(attr);
4048 if (types & (1 << attr))
4049 return 0; /* already occupied */
4050 switch (attr) {
4051 case INPUT_PIN_ATTR_INT:
4052 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4053 return 0; /* invalid type */
4054 break;
4055 case INPUT_PIN_ATTR_UNUSED:
4056 return 0; /* invalid entry */
4057 default:
4058 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4059 return 0; /* invalid type */
4060 if (!spec->line_in_auto_switch &&
4061 cfg->inputs[i].type != AUTO_PIN_MIC)
4062 return 0; /* only mic is allowed */
4063 if (!is_jack_detectable(codec, nid))
4064 return 0; /* no unsol support */
4065 break;
4066 }
4067 if (num_pins >= MAX_AUTO_MIC_PINS)
4068 return 0;
4069 types |= (1 << attr);
4070 spec->am_entry[num_pins].pin = nid;
4071 spec->am_entry[num_pins].attr = attr;
4072 num_pins++;
4073 }
4074
4075 if (num_pins < 2)
4076 return 0;
4077
4078 spec->am_num_entries = num_pins;
4079 /* sort the am_entry in the order of attr so that the pin with a
4080 * higher attr will be selected when the jack is plugged.
4081 */
4082 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4083 compare_attr, NULL);
4084
4085 if (!auto_mic_check_imux(codec))
4086 return 0;
4087
4088 spec->auto_mic = 1;
4089 spec->num_adc_nids = 1;
4090 spec->cur_mux[0] = spec->am_entry[0].idx;
4091 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4092 spec->am_entry[0].pin,
4093 spec->am_entry[1].pin,
4094 spec->am_entry[2].pin);
4095
4096 return 0;
4097}
4098
Takashi Iwai55196ff2013-01-24 17:32:56 +01004099/* power_filter hook; make inactive widgets into power down */
4100static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4101 hda_nid_t nid,
4102 unsigned int power_state)
4103{
4104 if (power_state != AC_PWRST_D0)
4105 return power_state;
4106 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4107 return power_state;
4108 if (is_active_nid(codec, nid, HDA_OUTPUT, 0))
4109 return power_state;
4110 return AC_PWRST_D3;
4111}
4112
Takashi Iwai352f7f92012-12-19 12:52:06 +01004113
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004114/*
4115 * Parse the given BIOS configuration and set up the hda_gen_spec
4116 *
4117 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004118 * or a negative error code
4119 */
4120int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004121 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004122{
4123 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004124 int err;
4125
Takashi Iwai1c70a582013-01-11 17:48:22 +01004126 parse_user_hints(codec);
4127
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004128 if (spec->mixer_nid && !spec->mixer_merge_nid)
4129 spec->mixer_merge_nid = spec->mixer_nid;
4130
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004131 if (cfg != &spec->autocfg) {
4132 spec->autocfg = *cfg;
4133 cfg = &spec->autocfg;
4134 }
4135
David Henningsson6fc4cb92013-01-16 15:58:45 +01004136 fill_all_dac_nids(codec);
4137
Takashi Iwai352f7f92012-12-19 12:52:06 +01004138 if (!cfg->line_outs) {
4139 if (cfg->dig_outs || cfg->dig_in_pin) {
4140 spec->multiout.max_channels = 2;
4141 spec->no_analog = 1;
4142 goto dig_only;
4143 }
4144 return 0; /* can't find valid BIOS pin config */
4145 }
4146
4147 if (!spec->no_primary_hp &&
4148 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4149 cfg->line_outs <= cfg->hp_outs) {
4150 /* use HP as primary out */
4151 cfg->speaker_outs = cfg->line_outs;
4152 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4153 sizeof(cfg->speaker_pins));
4154 cfg->line_outs = cfg->hp_outs;
4155 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4156 cfg->hp_outs = 0;
4157 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4158 cfg->line_out_type = AUTO_PIN_HP_OUT;
4159 }
4160
4161 err = parse_output_paths(codec);
4162 if (err < 0)
4163 return err;
4164 err = create_multi_channel_mode(codec);
4165 if (err < 0)
4166 return err;
4167 err = create_multi_out_ctls(codec, cfg);
4168 if (err < 0)
4169 return err;
4170 err = create_hp_out_ctls(codec);
4171 if (err < 0)
4172 return err;
4173 err = create_speaker_out_ctls(codec);
4174 if (err < 0)
4175 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004176 err = create_indep_hp_ctls(codec);
4177 if (err < 0)
4178 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004179 err = create_loopback_mixing_ctl(codec);
4180 if (err < 0)
4181 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004182 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004183 if (err < 0)
4184 return err;
4185 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004186 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004187 return err;
4188
Takashi Iwaia07a9492013-01-07 16:44:06 +01004189 spec->const_channel_count = spec->ext_channel_count;
4190 /* check the multiple speaker and headphone pins */
4191 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4192 spec->const_channel_count = max(spec->const_channel_count,
4193 cfg->speaker_outs * 2);
4194 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4195 spec->const_channel_count = max(spec->const_channel_count,
4196 cfg->hp_outs * 2);
4197 spec->multiout.max_channels = max(spec->ext_channel_count,
4198 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004199
4200 err = check_auto_mute_availability(codec);
4201 if (err < 0)
4202 return err;
4203
4204 err = check_dyn_adc_switch(codec);
4205 if (err < 0)
4206 return err;
4207
Takashi Iwai967303d2013-02-19 17:12:42 +01004208 err = check_auto_mic_availability(codec);
4209 if (err < 0)
4210 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004211
Takashi Iwai352f7f92012-12-19 12:52:06 +01004212 err = create_capture_mixers(codec);
4213 if (err < 0)
4214 return err;
4215
4216 err = parse_mic_boost(codec);
4217 if (err < 0)
4218 return err;
4219
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004220 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004221 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4222 err = create_out_jack_modes(codec, cfg->line_outs,
4223 cfg->line_out_pins);
4224 if (err < 0)
4225 return err;
4226 }
4227 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4228 err = create_out_jack_modes(codec, cfg->hp_outs,
4229 cfg->hp_pins);
4230 if (err < 0)
4231 return err;
4232 }
4233 }
4234
Takashi Iwai352f7f92012-12-19 12:52:06 +01004235 dig_only:
4236 parse_digital(codec);
4237
Takashi Iwai55196ff2013-01-24 17:32:56 +01004238 if (spec->power_down_unused)
4239 codec->power_filter = snd_hda_gen_path_power_filter;
4240
Takashi Iwai352f7f92012-12-19 12:52:06 +01004241 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004243EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
4245
4246/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004247 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004249
4250/* slave controls for virtual master */
4251static const char * const slave_pfxs[] = {
4252 "Front", "Surround", "Center", "LFE", "Side",
4253 "Headphone", "Speaker", "Mono", "Line Out",
4254 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004255 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4256 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4257 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004258 NULL,
4259};
4260
4261int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004263 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Takashi Iwai36502d02012-12-19 15:15:10 +01004266 if (spec->kctls.used) {
4267 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4268 if (err < 0)
4269 return err;
4270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Takashi Iwai352f7f92012-12-19 12:52:06 +01004272 if (spec->multiout.dig_out_nid) {
4273 err = snd_hda_create_dig_out_ctls(codec,
4274 spec->multiout.dig_out_nid,
4275 spec->multiout.dig_out_nid,
4276 spec->pcm_rec[1].pcm_type);
4277 if (err < 0)
4278 return err;
4279 if (!spec->no_analog) {
4280 err = snd_hda_create_spdif_share_sw(codec,
4281 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 if (err < 0)
4283 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004284 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 }
4286 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004287 if (spec->dig_in_nid) {
4288 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4289 if (err < 0)
4290 return err;
4291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Takashi Iwai352f7f92012-12-19 12:52:06 +01004293 /* if we have no master control, let's create it */
4294 if (!spec->no_analog &&
4295 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004296 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004297 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004298 "Playback Volume");
4299 if (err < 0)
4300 return err;
4301 }
4302 if (!spec->no_analog &&
4303 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4304 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4305 NULL, slave_pfxs,
4306 "Playback Switch",
4307 true, &spec->vmaster_mute.sw_kctl);
4308 if (err < 0)
4309 return err;
4310 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004311 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4312 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
Takashi Iwai352f7f92012-12-19 12:52:06 +01004315 free_kctls(spec); /* no longer needed */
4316
Takashi Iwai967303d2013-02-19 17:12:42 +01004317 if (spec->hp_mic_pin) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004318 int err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004319 int nid = spec->hp_mic_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004320 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4321 if (err < 0)
4322 return err;
4323 err = snd_hda_jack_detect_enable(codec, nid, 0);
4324 if (err < 0)
4325 return err;
4326 }
4327
4328 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4329 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 return err;
4331
4332 return 0;
4333}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004334EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4335
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
4337/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004338 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004341static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4342 struct hda_codec *codec,
4343 struct snd_pcm_substream *substream,
4344 int action)
4345{
4346 struct hda_gen_spec *spec = codec->spec;
4347 if (spec->pcm_playback_hook)
4348 spec->pcm_playback_hook(hinfo, codec, substream, action);
4349}
4350
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004351static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4352 struct hda_codec *codec,
4353 struct snd_pcm_substream *substream,
4354 int action)
4355{
4356 struct hda_gen_spec *spec = codec->spec;
4357 if (spec->pcm_capture_hook)
4358 spec->pcm_capture_hook(hinfo, codec, substream, action);
4359}
4360
Takashi Iwai352f7f92012-12-19 12:52:06 +01004361/*
4362 * Analog playback callbacks
4363 */
4364static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4365 struct hda_codec *codec,
4366 struct snd_pcm_substream *substream)
4367{
4368 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004369 int err;
4370
4371 mutex_lock(&spec->pcm_mutex);
4372 err = snd_hda_multi_out_analog_open(codec,
4373 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004374 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004375 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004376 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004377 call_pcm_playback_hook(hinfo, codec, substream,
4378 HDA_GEN_PCM_ACT_OPEN);
4379 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004380 mutex_unlock(&spec->pcm_mutex);
4381 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004382}
4383
4384static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004385 struct hda_codec *codec,
4386 unsigned int stream_tag,
4387 unsigned int format,
4388 struct snd_pcm_substream *substream)
4389{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004390 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004391 int err;
4392
4393 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4394 stream_tag, format, substream);
4395 if (!err)
4396 call_pcm_playback_hook(hinfo, codec, substream,
4397 HDA_GEN_PCM_ACT_PREPARE);
4398 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004399}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004400
Takashi Iwai352f7f92012-12-19 12:52:06 +01004401static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4402 struct hda_codec *codec,
4403 struct snd_pcm_substream *substream)
4404{
4405 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004406 int err;
4407
4408 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4409 if (!err)
4410 call_pcm_playback_hook(hinfo, codec, substream,
4411 HDA_GEN_PCM_ACT_CLEANUP);
4412 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004413}
4414
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004415static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4416 struct hda_codec *codec,
4417 struct snd_pcm_substream *substream)
4418{
4419 struct hda_gen_spec *spec = codec->spec;
4420 mutex_lock(&spec->pcm_mutex);
4421 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004422 call_pcm_playback_hook(hinfo, codec, substream,
4423 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004424 mutex_unlock(&spec->pcm_mutex);
4425 return 0;
4426}
4427
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004428static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4429 struct hda_codec *codec,
4430 struct snd_pcm_substream *substream)
4431{
4432 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4433 return 0;
4434}
4435
4436static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4437 struct hda_codec *codec,
4438 unsigned int stream_tag,
4439 unsigned int format,
4440 struct snd_pcm_substream *substream)
4441{
4442 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4443 call_pcm_capture_hook(hinfo, codec, substream,
4444 HDA_GEN_PCM_ACT_PREPARE);
4445 return 0;
4446}
4447
4448static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4449 struct hda_codec *codec,
4450 struct snd_pcm_substream *substream)
4451{
4452 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4453 call_pcm_capture_hook(hinfo, codec, substream,
4454 HDA_GEN_PCM_ACT_CLEANUP);
4455 return 0;
4456}
4457
4458static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4459 struct hda_codec *codec,
4460 struct snd_pcm_substream *substream)
4461{
4462 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4463 return 0;
4464}
4465
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004466static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4467 struct hda_codec *codec,
4468 struct snd_pcm_substream *substream)
4469{
4470 struct hda_gen_spec *spec = codec->spec;
4471 int err = 0;
4472
4473 mutex_lock(&spec->pcm_mutex);
4474 if (!spec->indep_hp_enabled)
4475 err = -EBUSY;
4476 else
4477 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004478 call_pcm_playback_hook(hinfo, codec, substream,
4479 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004480 mutex_unlock(&spec->pcm_mutex);
4481 return err;
4482}
4483
4484static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4485 struct hda_codec *codec,
4486 struct snd_pcm_substream *substream)
4487{
4488 struct hda_gen_spec *spec = codec->spec;
4489 mutex_lock(&spec->pcm_mutex);
4490 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004491 call_pcm_playback_hook(hinfo, codec, substream,
4492 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004493 mutex_unlock(&spec->pcm_mutex);
4494 return 0;
4495}
4496
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004497static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4498 struct hda_codec *codec,
4499 unsigned int stream_tag,
4500 unsigned int format,
4501 struct snd_pcm_substream *substream)
4502{
4503 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4504 call_pcm_playback_hook(hinfo, codec, substream,
4505 HDA_GEN_PCM_ACT_PREPARE);
4506 return 0;
4507}
4508
4509static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4510 struct hda_codec *codec,
4511 struct snd_pcm_substream *substream)
4512{
4513 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4514 call_pcm_playback_hook(hinfo, codec, substream,
4515 HDA_GEN_PCM_ACT_CLEANUP);
4516 return 0;
4517}
4518
Takashi Iwai352f7f92012-12-19 12:52:06 +01004519/*
4520 * Digital out
4521 */
4522static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4523 struct hda_codec *codec,
4524 struct snd_pcm_substream *substream)
4525{
4526 struct hda_gen_spec *spec = codec->spec;
4527 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4528}
4529
4530static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4531 struct hda_codec *codec,
4532 unsigned int stream_tag,
4533 unsigned int format,
4534 struct snd_pcm_substream *substream)
4535{
4536 struct hda_gen_spec *spec = codec->spec;
4537 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4538 stream_tag, format, substream);
4539}
4540
4541static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4542 struct hda_codec *codec,
4543 struct snd_pcm_substream *substream)
4544{
4545 struct hda_gen_spec *spec = codec->spec;
4546 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4547}
4548
4549static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4550 struct hda_codec *codec,
4551 struct snd_pcm_substream *substream)
4552{
4553 struct hda_gen_spec *spec = codec->spec;
4554 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4555}
4556
4557/*
4558 * Analog capture
4559 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004560#define alt_capture_pcm_open capture_pcm_open
4561#define alt_capture_pcm_close capture_pcm_close
4562
Takashi Iwai352f7f92012-12-19 12:52:06 +01004563static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4564 struct hda_codec *codec,
4565 unsigned int stream_tag,
4566 unsigned int format,
4567 struct snd_pcm_substream *substream)
4568{
4569 struct hda_gen_spec *spec = codec->spec;
4570
4571 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004572 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004573 call_pcm_capture_hook(hinfo, codec, substream,
4574 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004575 return 0;
4576}
4577
Takashi Iwai352f7f92012-12-19 12:52:06 +01004578static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4579 struct hda_codec *codec,
4580 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004581{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004582 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004583
Takashi Iwai352f7f92012-12-19 12:52:06 +01004584 snd_hda_codec_cleanup_stream(codec,
4585 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004586 call_pcm_capture_hook(hinfo, codec, substream,
4587 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004588 return 0;
4589}
4590
Takashi Iwai352f7f92012-12-19 12:52:06 +01004591/*
4592 */
4593static const struct hda_pcm_stream pcm_analog_playback = {
4594 .substreams = 1,
4595 .channels_min = 2,
4596 .channels_max = 8,
4597 /* NID is set in build_pcms */
4598 .ops = {
4599 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004600 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004601 .prepare = playback_pcm_prepare,
4602 .cleanup = playback_pcm_cleanup
4603 },
4604};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
Takashi Iwai352f7f92012-12-19 12:52:06 +01004606static const struct hda_pcm_stream pcm_analog_capture = {
4607 .substreams = 1,
4608 .channels_min = 2,
4609 .channels_max = 2,
4610 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004611 .ops = {
4612 .open = capture_pcm_open,
4613 .close = capture_pcm_close,
4614 .prepare = capture_pcm_prepare,
4615 .cleanup = capture_pcm_cleanup
4616 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004617};
4618
4619static const struct hda_pcm_stream pcm_analog_alt_playback = {
4620 .substreams = 1,
4621 .channels_min = 2,
4622 .channels_max = 2,
4623 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004624 .ops = {
4625 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004626 .close = alt_playback_pcm_close,
4627 .prepare = alt_playback_pcm_prepare,
4628 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004629 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004630};
4631
4632static const struct hda_pcm_stream pcm_analog_alt_capture = {
4633 .substreams = 2, /* can be overridden */
4634 .channels_min = 2,
4635 .channels_max = 2,
4636 /* NID is set in build_pcms */
4637 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004638 .open = alt_capture_pcm_open,
4639 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004640 .prepare = alt_capture_pcm_prepare,
4641 .cleanup = alt_capture_pcm_cleanup
4642 },
4643};
4644
4645static const struct hda_pcm_stream pcm_digital_playback = {
4646 .substreams = 1,
4647 .channels_min = 2,
4648 .channels_max = 2,
4649 /* NID is set in build_pcms */
4650 .ops = {
4651 .open = dig_playback_pcm_open,
4652 .close = dig_playback_pcm_close,
4653 .prepare = dig_playback_pcm_prepare,
4654 .cleanup = dig_playback_pcm_cleanup
4655 },
4656};
4657
4658static const struct hda_pcm_stream pcm_digital_capture = {
4659 .substreams = 1,
4660 .channels_min = 2,
4661 .channels_max = 2,
4662 /* NID is set in build_pcms */
4663};
4664
4665/* Used by build_pcms to flag that a PCM has no playback stream */
4666static const struct hda_pcm_stream pcm_null_stream = {
4667 .substreams = 0,
4668 .channels_min = 0,
4669 .channels_max = 0,
4670};
4671
4672/*
4673 * dynamic changing ADC PCM streams
4674 */
4675static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4676{
4677 struct hda_gen_spec *spec = codec->spec;
4678 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4679
4680 if (spec->cur_adc && spec->cur_adc != new_adc) {
4681 /* stream is running, let's swap the current ADC */
4682 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4683 spec->cur_adc = new_adc;
4684 snd_hda_codec_setup_stream(codec, new_adc,
4685 spec->cur_adc_stream_tag, 0,
4686 spec->cur_adc_format);
4687 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004689 return false;
4690}
4691
4692/* analog capture with dynamic dual-adc changes */
4693static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4694 struct hda_codec *codec,
4695 unsigned int stream_tag,
4696 unsigned int format,
4697 struct snd_pcm_substream *substream)
4698{
4699 struct hda_gen_spec *spec = codec->spec;
4700 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4701 spec->cur_adc_stream_tag = stream_tag;
4702 spec->cur_adc_format = format;
4703 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4704 return 0;
4705}
4706
4707static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4708 struct hda_codec *codec,
4709 struct snd_pcm_substream *substream)
4710{
4711 struct hda_gen_spec *spec = codec->spec;
4712 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4713 spec->cur_adc = 0;
4714 return 0;
4715}
4716
4717static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4718 .substreams = 1,
4719 .channels_min = 2,
4720 .channels_max = 2,
4721 .nid = 0, /* fill later */
4722 .ops = {
4723 .prepare = dyn_adc_capture_pcm_prepare,
4724 .cleanup = dyn_adc_capture_pcm_cleanup
4725 },
4726};
4727
Takashi Iwaif873e532012-12-20 16:58:39 +01004728static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4729 const char *chip_name)
4730{
4731 char *p;
4732
4733 if (*str)
4734 return;
4735 strlcpy(str, chip_name, len);
4736
4737 /* drop non-alnum chars after a space */
4738 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4739 if (!isalnum(p[1])) {
4740 *p = 0;
4741 break;
4742 }
4743 }
4744 strlcat(str, sfx, len);
4745}
4746
Takashi Iwai352f7f92012-12-19 12:52:06 +01004747/* build PCM streams based on the parsed results */
4748int snd_hda_gen_build_pcms(struct hda_codec *codec)
4749{
4750 struct hda_gen_spec *spec = codec->spec;
4751 struct hda_pcm *info = spec->pcm_rec;
4752 const struct hda_pcm_stream *p;
4753 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
4755 codec->num_pcms = 1;
4756 codec->pcm_info = info;
4757
Takashi Iwai352f7f92012-12-19 12:52:06 +01004758 if (spec->no_analog)
4759 goto skip_analog;
4760
Takashi Iwaif873e532012-12-20 16:58:39 +01004761 fill_pcm_stream_name(spec->stream_name_analog,
4762 sizeof(spec->stream_name_analog),
4763 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004764 info->name = spec->stream_name_analog;
4765
4766 if (spec->multiout.num_dacs > 0) {
4767 p = spec->stream_analog_playback;
4768 if (!p)
4769 p = &pcm_analog_playback;
4770 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4771 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4772 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4773 spec->multiout.max_channels;
4774 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4775 spec->autocfg.line_outs == 2)
4776 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4777 snd_pcm_2_1_chmaps;
4778 }
4779 if (spec->num_adc_nids) {
4780 p = spec->stream_analog_capture;
4781 if (!p) {
4782 if (spec->dyn_adc_switch)
4783 p = &dyn_adc_pcm_analog_capture;
4784 else
4785 p = &pcm_analog_capture;
4786 }
4787 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4788 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4789 }
4790
Takashi Iwai352f7f92012-12-19 12:52:06 +01004791 skip_analog:
4792 /* SPDIF for stream index #1 */
4793 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004794 fill_pcm_stream_name(spec->stream_name_digital,
4795 sizeof(spec->stream_name_digital),
4796 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004797 codec->num_pcms = 2;
4798 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4799 info = spec->pcm_rec + 1;
4800 info->name = spec->stream_name_digital;
4801 if (spec->dig_out_type)
4802 info->pcm_type = spec->dig_out_type;
4803 else
4804 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4805 if (spec->multiout.dig_out_nid) {
4806 p = spec->stream_digital_playback;
4807 if (!p)
4808 p = &pcm_digital_playback;
4809 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4810 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4811 }
4812 if (spec->dig_in_nid) {
4813 p = spec->stream_digital_capture;
4814 if (!p)
4815 p = &pcm_digital_capture;
4816 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4817 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4818 }
4819 }
4820
4821 if (spec->no_analog)
4822 return 0;
4823
4824 /* If the use of more than one ADC is requested for the current
4825 * model, configure a second analog capture-only PCM.
4826 */
4827 have_multi_adcs = (spec->num_adc_nids > 1) &&
4828 !spec->dyn_adc_switch && !spec->auto_mic;
4829 /* Additional Analaog capture for index #2 */
4830 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004831 fill_pcm_stream_name(spec->stream_name_alt_analog,
4832 sizeof(spec->stream_name_alt_analog),
4833 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004834 codec->num_pcms = 3;
4835 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004836 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004837 if (spec->alt_dac_nid) {
4838 p = spec->stream_analog_alt_playback;
4839 if (!p)
4840 p = &pcm_analog_alt_playback;
4841 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4842 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4843 spec->alt_dac_nid;
4844 } else {
4845 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4846 pcm_null_stream;
4847 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4848 }
4849 if (have_multi_adcs) {
4850 p = spec->stream_analog_alt_capture;
4851 if (!p)
4852 p = &pcm_analog_alt_capture;
4853 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4854 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4855 spec->adc_nids[1];
4856 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4857 spec->num_adc_nids - 1;
4858 } else {
4859 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4860 pcm_null_stream;
4861 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 }
4864
4865 return 0;
4866}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004867EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4868
4869
4870/*
4871 * Standard auto-parser initializations
4872 */
4873
Takashi Iwaid4156932013-01-07 10:08:02 +01004874/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004875static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004876{
4877 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004878 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004879
Takashi Iwai196c17662013-01-04 15:01:40 +01004880 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004881 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004882 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004883 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004884 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004885 snd_hda_activate_path(codec, path, path->active, true);
4886 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004887}
4888
4889/* initialize primary output paths */
4890static void init_multi_out(struct hda_codec *codec)
4891{
4892 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004893 int i;
4894
Takashi Iwaid4156932013-01-07 10:08:02 +01004895 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004896 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004897}
4898
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004899
Takashi Iwai2c12c302013-01-10 09:33:29 +01004900static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004901{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004902 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004903
Takashi Iwaid4156932013-01-07 10:08:02 +01004904 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004905 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004906}
4907
4908/* initialize hp and speaker paths */
4909static void init_extra_out(struct hda_codec *codec)
4910{
4911 struct hda_gen_spec *spec = codec->spec;
4912
4913 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004914 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004915 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4916 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004917 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004918}
4919
4920/* initialize multi-io paths */
4921static void init_multi_io(struct hda_codec *codec)
4922{
4923 struct hda_gen_spec *spec = codec->spec;
4924 int i;
4925
4926 for (i = 0; i < spec->multi_ios; i++) {
4927 hda_nid_t pin = spec->multi_io[i].pin;
4928 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004929 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004930 if (!path)
4931 continue;
4932 if (!spec->multi_io[i].ctl_in)
4933 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004934 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004935 snd_hda_activate_path(codec, path, path->active, true);
4936 }
4937}
4938
Takashi Iwai352f7f92012-12-19 12:52:06 +01004939/* set up input pins and loopback paths */
4940static void init_analog_input(struct hda_codec *codec)
4941{
4942 struct hda_gen_spec *spec = codec->spec;
4943 struct auto_pin_cfg *cfg = &spec->autocfg;
4944 int i;
4945
4946 for (i = 0; i < cfg->num_inputs; i++) {
4947 hda_nid_t nid = cfg->inputs[i].pin;
4948 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004949 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004950
4951 /* init loopback inputs */
4952 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01004953 resume_path_from_idx(codec, spec->loopback_paths[i]);
4954 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004955 }
4956 }
4957}
4958
4959/* initialize ADC paths */
4960static void init_input_src(struct hda_codec *codec)
4961{
4962 struct hda_gen_spec *spec = codec->spec;
4963 struct hda_input_mux *imux = &spec->input_mux;
4964 struct nid_path *path;
4965 int i, c, nums;
4966
4967 if (spec->dyn_adc_switch)
4968 nums = 1;
4969 else
4970 nums = spec->num_adc_nids;
4971
4972 for (c = 0; c < nums; c++) {
4973 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004974 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004975 if (path) {
4976 bool active = path->active;
4977 if (i == spec->cur_mux[c])
4978 active = true;
4979 snd_hda_activate_path(codec, path, active, false);
4980 }
4981 }
Takashi Iwai967303d2013-02-19 17:12:42 +01004982 if (spec->hp_mic)
4983 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004984 }
4985
Takashi Iwai352f7f92012-12-19 12:52:06 +01004986 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004987 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004988}
4989
4990/* set right pin controls for digital I/O */
4991static void init_digital(struct hda_codec *codec)
4992{
4993 struct hda_gen_spec *spec = codec->spec;
4994 int i;
4995 hda_nid_t pin;
4996
Takashi Iwaid4156932013-01-07 10:08:02 +01004997 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004998 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004999 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005000 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005001 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005002 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005003 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005004}
5005
Takashi Iwai973e4972012-12-20 15:16:09 +01005006/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5007 * invalid unsol tags by some reason
5008 */
5009static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5010{
5011 int i;
5012
5013 for (i = 0; i < codec->init_pins.used; i++) {
5014 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5015 hda_nid_t nid = pin->nid;
5016 if (is_jack_detectable(codec, nid) &&
5017 !snd_hda_jack_tbl_get(codec, nid))
5018 snd_hda_codec_update_cache(codec, nid, 0,
5019 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5020 }
5021}
5022
Takashi Iwai5187ac12013-01-07 12:52:16 +01005023/*
5024 * initialize the generic spec;
5025 * this can be put as patch_ops.init function
5026 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005027int snd_hda_gen_init(struct hda_codec *codec)
5028{
5029 struct hda_gen_spec *spec = codec->spec;
5030
5031 if (spec->init_hook)
5032 spec->init_hook(codec);
5033
5034 snd_hda_apply_verbs(codec);
5035
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005036 codec->cached_write = 1;
5037
Takashi Iwai352f7f92012-12-19 12:52:06 +01005038 init_multi_out(codec);
5039 init_extra_out(codec);
5040 init_multi_io(codec);
5041 init_analog_input(codec);
5042 init_input_src(codec);
5043 init_digital(codec);
5044
Takashi Iwai973e4972012-12-20 15:16:09 +01005045 clear_unsol_on_unused_pins(codec);
5046
Takashi Iwai352f7f92012-12-19 12:52:06 +01005047 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005048 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005049
Takashi Iwaidc870f32013-01-22 15:24:30 +01005050 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005051
Takashi Iwai352f7f92012-12-19 12:52:06 +01005052 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5053 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5054
5055 hda_call_check_power_status(codec, 0x01);
5056 return 0;
5057}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005058EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5059
Takashi Iwai5187ac12013-01-07 12:52:16 +01005060/*
5061 * free the generic spec;
5062 * this can be put as patch_ops.free function
5063 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005064void snd_hda_gen_free(struct hda_codec *codec)
5065{
5066 snd_hda_gen_spec_free(codec->spec);
5067 kfree(codec->spec);
5068 codec->spec = NULL;
5069}
5070EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5071
5072#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005073/*
5074 * check the loopback power save state;
5075 * this can be put as patch_ops.check_power_status function
5076 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005077int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5078{
5079 struct hda_gen_spec *spec = codec->spec;
5080 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5081}
5082EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5083#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005084
5085
5086/*
5087 * the generic codec support
5088 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
Takashi Iwai352f7f92012-12-19 12:52:06 +01005090static const struct hda_codec_ops generic_patch_ops = {
5091 .build_controls = snd_hda_gen_build_controls,
5092 .build_pcms = snd_hda_gen_build_pcms,
5093 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005094 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005095 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005096#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005097 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005098#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099};
5100
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101int snd_hda_parse_generic_codec(struct hda_codec *codec)
5102{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005103 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 int err;
5105
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005106 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005107 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005109 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005112 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5113 if (err < 0)
5114 return err;
5115
5116 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005117 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 goto error;
5119
5120 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 return 0;
5122
Takashi Iwai352f7f92012-12-19 12:52:06 +01005123error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005124 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 return err;
5126}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005127EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);