blob: fb232c118e91e3e142ebf61d384f5cd0ef40810e [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);
2970 set_pin_target(codec, pin, val, false);
2971
Takashi Iwai352f7f92012-12-19 12:52:06 +01002972 if (mixer) {
2973 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002974 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002975 spec->input_labels[i],
2976 spec->input_label_idxs[i],
2977 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002978 if (err < 0)
2979 return err;
2980 }
2981 }
2982
Takashi Iwaic9700422013-01-18 10:17:30 +01002983 err = parse_capture_source(codec, pin, i, num_adcs,
2984 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002985 if (err < 0)
2986 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002987
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002988 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01002989 err = create_in_jack_mode(codec, pin);
2990 if (err < 0)
2991 return err;
2992 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002993 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002994
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002995 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002996 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002997 "Stereo Mix", 0);
2998 if (err < 0)
2999 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003000 }
3001
3002 return 0;
3003}
3004
3005
3006/*
3007 * input source mux
3008 */
3009
Takashi Iwaic697b712013-01-07 17:09:26 +01003010/* get the input path specified by the given adc and imux indices */
3011static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003012{
3013 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003014 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3015 snd_BUG();
3016 return NULL;
3017 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003018 if (spec->dyn_adc_switch)
3019 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003020 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003021 snd_BUG();
3022 return NULL;
3023 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003024 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003025}
3026
3027static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3028 unsigned int idx);
3029
3030static int mux_enum_info(struct snd_kcontrol *kcontrol,
3031 struct snd_ctl_elem_info *uinfo)
3032{
3033 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3034 struct hda_gen_spec *spec = codec->spec;
3035 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3036}
3037
3038static int mux_enum_get(struct snd_kcontrol *kcontrol,
3039 struct snd_ctl_elem_value *ucontrol)
3040{
3041 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3042 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003043 /* the ctls are created at once with multiple counts */
3044 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003045
3046 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3047 return 0;
3048}
3049
3050static int mux_enum_put(struct snd_kcontrol *kcontrol,
3051 struct snd_ctl_elem_value *ucontrol)
3052{
3053 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003054 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003055 return mux_select(codec, adc_idx,
3056 ucontrol->value.enumerated.item[0]);
3057}
3058
Takashi Iwai352f7f92012-12-19 12:52:06 +01003059static const struct snd_kcontrol_new cap_src_temp = {
3060 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3061 .name = "Input Source",
3062 .info = mux_enum_info,
3063 .get = mux_enum_get,
3064 .put = mux_enum_put,
3065};
3066
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003067/*
3068 * capture volume and capture switch ctls
3069 */
3070
Takashi Iwai352f7f92012-12-19 12:52:06 +01003071typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3072 struct snd_ctl_elem_value *ucontrol);
3073
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003074/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003075static int cap_put_caller(struct snd_kcontrol *kcontrol,
3076 struct snd_ctl_elem_value *ucontrol,
3077 put_call_t func, int type)
3078{
3079 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3080 struct hda_gen_spec *spec = codec->spec;
3081 const struct hda_input_mux *imux;
3082 struct nid_path *path;
3083 int i, adc_idx, err = 0;
3084
3085 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003086 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003087 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003088 /* we use the cache-only update at first since multiple input paths
3089 * may shared the same amp; by updating only caches, the redundant
3090 * writes to hardware can be reduced.
3091 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003092 codec->cached_write = 1;
3093 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003094 path = get_input_path(codec, adc_idx, i);
3095 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003096 continue;
3097 kcontrol->private_value = path->ctls[type];
3098 err = func(kcontrol, ucontrol);
3099 if (err < 0)
3100 goto error;
3101 }
3102 error:
3103 codec->cached_write = 0;
3104 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003105 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003106 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003107 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003108 return err;
3109}
3110
3111/* capture volume ctl callbacks */
3112#define cap_vol_info snd_hda_mixer_amp_volume_info
3113#define cap_vol_get snd_hda_mixer_amp_volume_get
3114#define cap_vol_tlv snd_hda_mixer_amp_tlv
3115
3116static int cap_vol_put(struct snd_kcontrol *kcontrol,
3117 struct snd_ctl_elem_value *ucontrol)
3118{
3119 return cap_put_caller(kcontrol, ucontrol,
3120 snd_hda_mixer_amp_volume_put,
3121 NID_PATH_VOL_CTL);
3122}
3123
3124static const struct snd_kcontrol_new cap_vol_temp = {
3125 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3126 .name = "Capture Volume",
3127 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3128 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3129 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3130 .info = cap_vol_info,
3131 .get = cap_vol_get,
3132 .put = cap_vol_put,
3133 .tlv = { .c = cap_vol_tlv },
3134};
3135
3136/* capture switch ctl callbacks */
3137#define cap_sw_info snd_ctl_boolean_stereo_info
3138#define cap_sw_get snd_hda_mixer_amp_switch_get
3139
3140static int cap_sw_put(struct snd_kcontrol *kcontrol,
3141 struct snd_ctl_elem_value *ucontrol)
3142{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003143 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003144 snd_hda_mixer_amp_switch_put,
3145 NID_PATH_MUTE_CTL);
3146}
3147
3148static const struct snd_kcontrol_new cap_sw_temp = {
3149 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3150 .name = "Capture Switch",
3151 .info = cap_sw_info,
3152 .get = cap_sw_get,
3153 .put = cap_sw_put,
3154};
3155
3156static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3157{
3158 hda_nid_t nid;
3159 int i, depth;
3160
3161 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3162 for (depth = 0; depth < 3; depth++) {
3163 if (depth >= path->depth)
3164 return -EINVAL;
3165 i = path->depth - depth - 1;
3166 nid = path->path[i];
3167 if (!path->ctls[NID_PATH_VOL_CTL]) {
3168 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3169 path->ctls[NID_PATH_VOL_CTL] =
3170 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3171 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3172 int idx = path->idx[i];
3173 if (!depth && codec->single_adc_amp)
3174 idx = 0;
3175 path->ctls[NID_PATH_VOL_CTL] =
3176 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3177 }
3178 }
3179 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3180 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3181 path->ctls[NID_PATH_MUTE_CTL] =
3182 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3183 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3184 int idx = path->idx[i];
3185 if (!depth && codec->single_adc_amp)
3186 idx = 0;
3187 path->ctls[NID_PATH_MUTE_CTL] =
3188 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3189 }
3190 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 return 0;
3193}
3194
Takashi Iwai352f7f92012-12-19 12:52:06 +01003195static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003197 struct hda_gen_spec *spec = codec->spec;
3198 struct auto_pin_cfg *cfg = &spec->autocfg;
3199 unsigned int val;
3200 int i;
3201
3202 if (!spec->inv_dmic_split)
3203 return false;
3204 for (i = 0; i < cfg->num_inputs; i++) {
3205 if (cfg->inputs[i].pin != nid)
3206 continue;
3207 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3208 return false;
3209 val = snd_hda_codec_get_pincfg(codec, nid);
3210 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3211 }
3212 return false;
3213}
3214
Takashi Iwaia90229e2013-01-18 14:10:00 +01003215/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003216static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3217 struct snd_ctl_elem_value *ucontrol)
3218{
3219 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3220 struct hda_gen_spec *spec = codec->spec;
3221 int ret;
3222
3223 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3224 if (ret < 0)
3225 return ret;
3226
Takashi Iwaia90229e2013-01-18 14:10:00 +01003227 if (spec->cap_sync_hook)
3228 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003229
3230 return ret;
3231}
3232
Takashi Iwai352f7f92012-12-19 12:52:06 +01003233static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3234 int idx, bool is_switch, unsigned int ctl,
3235 bool inv_dmic)
3236{
3237 struct hda_gen_spec *spec = codec->spec;
3238 char tmpname[44];
3239 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3240 const char *sfx = is_switch ? "Switch" : "Volume";
3241 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003242 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003243
3244 if (!ctl)
3245 return 0;
3246
3247 if (label)
3248 snprintf(tmpname, sizeof(tmpname),
3249 "%s Capture %s", label, sfx);
3250 else
3251 snprintf(tmpname, sizeof(tmpname),
3252 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003253 knew = add_control(spec, type, tmpname, idx,
3254 amp_val_replace_channels(ctl, chs));
3255 if (!knew)
3256 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003257 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003258 knew->put = cap_single_sw_put;
3259 if (!inv_dmic)
3260 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003261
3262 /* Make independent right kcontrol */
3263 if (label)
3264 snprintf(tmpname, sizeof(tmpname),
3265 "Inverted %s Capture %s", label, sfx);
3266 else
3267 snprintf(tmpname, sizeof(tmpname),
3268 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003269 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003270 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003271 if (!knew)
3272 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003273 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003274 knew->put = cap_single_sw_put;
3275 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003276}
3277
3278/* create single (and simple) capture volume and switch controls */
3279static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3280 unsigned int vol_ctl, unsigned int sw_ctl,
3281 bool inv_dmic)
3282{
3283 int err;
3284 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3285 if (err < 0)
3286 return err;
3287 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3288 if (err < 0)
3289 return err;
3290 return 0;
3291}
3292
3293/* create bound capture volume and switch controls */
3294static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3295 unsigned int vol_ctl, unsigned int sw_ctl)
3296{
3297 struct hda_gen_spec *spec = codec->spec;
3298 struct snd_kcontrol_new *knew;
3299
3300 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003301 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003302 if (!knew)
3303 return -ENOMEM;
3304 knew->index = idx;
3305 knew->private_value = vol_ctl;
3306 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3307 }
3308 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003309 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003310 if (!knew)
3311 return -ENOMEM;
3312 knew->index = idx;
3313 knew->private_value = sw_ctl;
3314 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3315 }
3316 return 0;
3317}
3318
3319/* return the vol ctl when used first in the imux list */
3320static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3321{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003322 struct nid_path *path;
3323 unsigned int ctl;
3324 int i;
3325
Takashi Iwaic697b712013-01-07 17:09:26 +01003326 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003327 if (!path)
3328 return 0;
3329 ctl = path->ctls[type];
3330 if (!ctl)
3331 return 0;
3332 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003333 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003334 if (path && path->ctls[type] == ctl)
3335 return 0;
3336 }
3337 return ctl;
3338}
3339
3340/* create individual capture volume and switch controls per input */
3341static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3342{
3343 struct hda_gen_spec *spec = codec->spec;
3344 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003345 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003346
3347 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003348 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003349 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003350
Takashi Iwaic9700422013-01-18 10:17:30 +01003351 idx = imux->items[i].index;
3352 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003353 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003354 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3355
3356 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003357 err = add_single_cap_ctl(codec,
3358 spec->input_labels[idx],
3359 spec->input_label_idxs[idx],
3360 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003361 get_first_cap_ctl(codec, i, type),
3362 inv_dmic);
3363 if (err < 0)
3364 return err;
3365 }
3366 }
3367 return 0;
3368}
3369
3370static int create_capture_mixers(struct hda_codec *codec)
3371{
3372 struct hda_gen_spec *spec = codec->spec;
3373 struct hda_input_mux *imux = &spec->input_mux;
3374 int i, n, nums, err;
3375
3376 if (spec->dyn_adc_switch)
3377 nums = 1;
3378 else
3379 nums = spec->num_adc_nids;
3380
3381 if (!spec->auto_mic && imux->num_items > 1) {
3382 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003383 const char *name;
3384 name = nums > 1 ? "Input Source" : "Capture Source";
3385 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003386 if (!knew)
3387 return -ENOMEM;
3388 knew->count = nums;
3389 }
3390
3391 for (n = 0; n < nums; n++) {
3392 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003393 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003394 bool inv_dmic = false;
3395 int vol, sw;
3396
3397 vol = sw = 0;
3398 for (i = 0; i < imux->num_items; i++) {
3399 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003400 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003401 if (!path)
3402 continue;
3403 parse_capvol_in_path(codec, path);
3404 if (!vol)
3405 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003406 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003407 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003408 if (!same_amp_caps(codec, vol,
3409 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3410 multi_cap_vol = true;
3411 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003412 if (!sw)
3413 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003414 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003415 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003416 if (!same_amp_caps(codec, sw,
3417 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3418 multi_cap_vol = true;
3419 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003420 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3421 inv_dmic = true;
3422 }
3423
3424 if (!multi)
3425 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3426 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003427 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003428 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3429 else
3430 err = create_multi_cap_vol_ctl(codec);
3431 if (err < 0)
3432 return err;
3433 }
3434
3435 return 0;
3436}
3437
3438/*
3439 * add mic boosts if needed
3440 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003441
3442/* check whether the given amp is feasible as a boost volume */
3443static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3444 int dir, int idx)
3445{
3446 unsigned int step;
3447
3448 if (!nid_has_volume(codec, nid, dir) ||
3449 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3450 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3451 return false;
3452
3453 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3454 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3455 if (step < 0x20)
3456 return false;
3457 return true;
3458}
3459
3460/* look for a boost amp in a widget close to the pin */
3461static unsigned int look_for_boost_amp(struct hda_codec *codec,
3462 struct nid_path *path)
3463{
3464 unsigned int val = 0;
3465 hda_nid_t nid;
3466 int depth;
3467
3468 for (depth = 0; depth < 3; depth++) {
3469 if (depth >= path->depth - 1)
3470 break;
3471 nid = path->path[depth];
3472 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3473 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3474 break;
3475 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3476 path->idx[depth])) {
3477 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3478 HDA_INPUT);
3479 break;
3480 }
3481 }
3482
3483 return val;
3484}
3485
Takashi Iwai352f7f92012-12-19 12:52:06 +01003486static int parse_mic_boost(struct hda_codec *codec)
3487{
3488 struct hda_gen_spec *spec = codec->spec;
3489 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003490 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003491 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003492
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003493 if (!spec->num_adc_nids)
3494 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003495
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003496 for (i = 0; i < imux->num_items; i++) {
3497 struct nid_path *path;
3498 unsigned int val;
3499 int idx;
3500 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003501
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003502 idx = imux->items[i].index;
3503 if (idx >= imux->num_items)
3504 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003505
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003506 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003507 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003508 continue;
3509
3510 path = get_input_path(codec, 0, i);
3511 if (!path)
3512 continue;
3513
3514 val = look_for_boost_amp(codec, path);
3515 if (!val)
3516 continue;
3517
3518 /* create a boost control */
3519 snprintf(boost_label, sizeof(boost_label),
3520 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003521 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3522 spec->input_label_idxs[idx], val))
3523 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003524
3525 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003526 }
3527 return 0;
3528}
3529
3530/*
3531 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3532 */
3533static void parse_digital(struct hda_codec *codec)
3534{
3535 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003536 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003537 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003538 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003539
3540 /* support multiple SPDIFs; the secondary is set up as a slave */
3541 nums = 0;
3542 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003543 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003544 dig_nid = look_for_dac(codec, pin, true);
3545 if (!dig_nid)
3546 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003547 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003548 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003549 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003550 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003551 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003552 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003553 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003554 if (!nums) {
3555 spec->multiout.dig_out_nid = dig_nid;
3556 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3557 } else {
3558 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3559 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3560 break;
3561 spec->slave_dig_outs[nums - 1] = dig_nid;
3562 }
3563 nums++;
3564 }
3565
3566 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003567 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003568 dig_nid = codec->start_nid;
3569 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003570 unsigned int wcaps = get_wcaps(codec, dig_nid);
3571 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3572 continue;
3573 if (!(wcaps & AC_WCAP_DIGITAL))
3574 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003575 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003576 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003577 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003578 path->active = true;
3579 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003580 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003581 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003582 break;
3583 }
3584 }
3585 }
3586}
3587
3588
3589/*
3590 * input MUX handling
3591 */
3592
3593static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3594
3595/* select the given imux item; either unmute exclusively or select the route */
3596static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3597 unsigned int idx)
3598{
3599 struct hda_gen_spec *spec = codec->spec;
3600 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003601 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003602
3603 imux = &spec->input_mux;
3604 if (!imux->num_items)
3605 return 0;
3606
3607 if (idx >= imux->num_items)
3608 idx = imux->num_items - 1;
3609 if (spec->cur_mux[adc_idx] == idx)
3610 return 0;
3611
Takashi Iwai55196ff2013-01-24 17:32:56 +01003612 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3613 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003614 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003615 if (old_path->active)
3616 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003617
3618 spec->cur_mux[adc_idx] = idx;
3619
Takashi Iwai967303d2013-02-19 17:12:42 +01003620 if (spec->hp_mic)
3621 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003622
3623 if (spec->dyn_adc_switch)
3624 dyn_adc_pcm_resetup(codec, idx);
3625
Takashi Iwaic697b712013-01-07 17:09:26 +01003626 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003627 if (!path)
3628 return 0;
3629 if (path->active)
3630 return 0;
3631 snd_hda_activate_path(codec, path, true, false);
3632 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003633 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003634 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003635 return 1;
3636}
3637
3638
3639/*
3640 * Jack detections for HP auto-mute and mic-switch
3641 */
3642
3643/* check each pin in the given array; returns true if any of them is plugged */
3644static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3645{
3646 int i, present = 0;
3647
3648 for (i = 0; i < num_pins; i++) {
3649 hda_nid_t nid = pins[i];
3650 if (!nid)
3651 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003652 /* don't detect pins retasked as inputs */
3653 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3654 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003655 present |= snd_hda_jack_detect(codec, nid);
3656 }
3657 return present;
3658}
3659
3660/* standard HP/line-out auto-mute helper */
3661static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003662 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003663{
3664 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003665 int i;
3666
3667 for (i = 0; i < num_pins; i++) {
3668 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003669 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003670 if (!nid)
3671 break;
Takashi Iwai967303d2013-02-19 17:12:42 +01003672 oldval = snd_hda_codec_get_pin_target(codec, nid);
3673 if (oldval & PIN_IN)
3674 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003675 /* don't reset VREF value in case it's controlling
3676 * the amp (see alc861_fixup_asus_amp_vref_0f())
3677 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003678 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003679 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003680 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003681 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003682 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003683 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003684 /* here we call update_pin_ctl() so that the pinctl is changed
3685 * without changing the pinctl target value;
3686 * the original target value will be still referred at the
3687 * init / resume again
3688 */
3689 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003690 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003691 }
3692}
3693
3694/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003695void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003696{
3697 struct hda_gen_spec *spec = codec->spec;
3698 int on;
3699
3700 /* Control HP pins/amps depending on master_mute state;
3701 * in general, HP pins/amps control should be enabled in all cases,
3702 * but currently set only for master_mute, just to be safe
3703 */
Takashi Iwai967303d2013-02-19 17:12:42 +01003704 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003705 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003706
3707 if (!spec->automute_speaker)
3708 on = 0;
3709 else
3710 on = spec->hp_jack_present | spec->line_jack_present;
3711 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003712 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003713 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003714 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003715
3716 /* toggle line-out mutes if needed, too */
3717 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3718 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3719 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3720 return;
3721 if (!spec->automute_lo)
3722 on = 0;
3723 else
3724 on = spec->hp_jack_present;
3725 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003726 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003727 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003728 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003729}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003730EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003731
3732static void call_update_outputs(struct hda_codec *codec)
3733{
3734 struct hda_gen_spec *spec = codec->spec;
3735 if (spec->automute_hook)
3736 spec->automute_hook(codec);
3737 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003738 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003739}
3740
3741/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003742void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003743{
3744 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003745 hda_nid_t *pins = spec->autocfg.hp_pins;
3746 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003747
Takashi Iwai92603c52013-01-22 07:46:31 +01003748 /* No detection for the first HP jack during indep-HP mode */
3749 if (spec->indep_hp_enabled) {
3750 pins++;
3751 num_pins--;
3752 }
3753
3754 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003755 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3756 return;
3757 call_update_outputs(codec);
3758}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003759EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003760
3761/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003762void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003763{
3764 struct hda_gen_spec *spec = codec->spec;
3765
3766 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3767 return;
3768 /* check LO jack only when it's different from HP */
3769 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3770 return;
3771
3772 spec->line_jack_present =
3773 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3774 spec->autocfg.line_out_pins);
3775 if (!spec->automute_speaker || !spec->detect_lo)
3776 return;
3777 call_update_outputs(codec);
3778}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003779EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003780
3781/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003782void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003783{
3784 struct hda_gen_spec *spec = codec->spec;
3785 int i;
3786
3787 if (!spec->auto_mic)
3788 return;
3789
3790 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003791 hda_nid_t pin = spec->am_entry[i].pin;
3792 /* don't detect pins retasked as outputs */
3793 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3794 continue;
3795 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003796 mux_select(codec, 0, spec->am_entry[i].idx);
3797 return;
3798 }
3799 }
3800 mux_select(codec, 0, spec->am_entry[0].idx);
3801}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003802EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003803
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003804/* update jack retasking */
3805static void update_automute_all(struct hda_codec *codec)
3806{
3807 struct hda_gen_spec *spec = codec->spec;
3808
Takashi Iwai8ba955c2013-03-07 18:40:58 +01003809 update_hp_automute_hook(codec);
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003810 if (spec->line_automute_hook)
3811 spec->line_automute_hook(codec, NULL);
3812 else
3813 snd_hda_gen_line_automute(codec, NULL);
3814 if (spec->mic_autoswitch_hook)
3815 spec->mic_autoswitch_hook(codec, NULL);
3816 else
3817 snd_hda_gen_mic_autoswitch(codec, NULL);
3818}
3819
Takashi Iwai352f7f92012-12-19 12:52:06 +01003820/*
3821 * Auto-Mute mode mixer enum support
3822 */
3823static int automute_mode_info(struct snd_kcontrol *kcontrol,
3824 struct snd_ctl_elem_info *uinfo)
3825{
3826 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3827 struct hda_gen_spec *spec = codec->spec;
3828 static const char * const texts3[] = {
3829 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003830 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
Takashi Iwai352f7f92012-12-19 12:52:06 +01003832 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3833 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3834 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3835}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
Takashi Iwai352f7f92012-12-19 12:52:06 +01003837static int automute_mode_get(struct snd_kcontrol *kcontrol,
3838 struct snd_ctl_elem_value *ucontrol)
3839{
3840 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3841 struct hda_gen_spec *spec = codec->spec;
3842 unsigned int val = 0;
3843 if (spec->automute_speaker)
3844 val++;
3845 if (spec->automute_lo)
3846 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003847
Takashi Iwai352f7f92012-12-19 12:52:06 +01003848 ucontrol->value.enumerated.item[0] = val;
3849 return 0;
3850}
3851
3852static int automute_mode_put(struct snd_kcontrol *kcontrol,
3853 struct snd_ctl_elem_value *ucontrol)
3854{
3855 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3856 struct hda_gen_spec *spec = codec->spec;
3857
3858 switch (ucontrol->value.enumerated.item[0]) {
3859 case 0:
3860 if (!spec->automute_speaker && !spec->automute_lo)
3861 return 0;
3862 spec->automute_speaker = 0;
3863 spec->automute_lo = 0;
3864 break;
3865 case 1:
3866 if (spec->automute_speaker_possible) {
3867 if (!spec->automute_lo && spec->automute_speaker)
3868 return 0;
3869 spec->automute_speaker = 1;
3870 spec->automute_lo = 0;
3871 } else if (spec->automute_lo_possible) {
3872 if (spec->automute_lo)
3873 return 0;
3874 spec->automute_lo = 1;
3875 } else
3876 return -EINVAL;
3877 break;
3878 case 2:
3879 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3880 return -EINVAL;
3881 if (spec->automute_speaker && spec->automute_lo)
3882 return 0;
3883 spec->automute_speaker = 1;
3884 spec->automute_lo = 1;
3885 break;
3886 default:
3887 return -EINVAL;
3888 }
3889 call_update_outputs(codec);
3890 return 1;
3891}
3892
3893static const struct snd_kcontrol_new automute_mode_enum = {
3894 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3895 .name = "Auto-Mute Mode",
3896 .info = automute_mode_info,
3897 .get = automute_mode_get,
3898 .put = automute_mode_put,
3899};
3900
3901static int add_automute_mode_enum(struct hda_codec *codec)
3902{
3903 struct hda_gen_spec *spec = codec->spec;
3904
Takashi Iwai12c93df2012-12-19 14:38:33 +01003905 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003906 return -ENOMEM;
3907 return 0;
3908}
3909
3910/*
3911 * Check the availability of HP/line-out auto-mute;
3912 * Set up appropriately if really supported
3913 */
3914static int check_auto_mute_availability(struct hda_codec *codec)
3915{
3916 struct hda_gen_spec *spec = codec->spec;
3917 struct auto_pin_cfg *cfg = &spec->autocfg;
3918 int present = 0;
3919 int i, err;
3920
Takashi Iwaif72706b2013-01-16 18:20:07 +01003921 if (spec->suppress_auto_mute)
3922 return 0;
3923
Takashi Iwai352f7f92012-12-19 12:52:06 +01003924 if (cfg->hp_pins[0])
3925 present++;
3926 if (cfg->line_out_pins[0])
3927 present++;
3928 if (cfg->speaker_pins[0])
3929 present++;
3930 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003931 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003932
3933 if (!cfg->speaker_pins[0] &&
3934 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3935 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3936 sizeof(cfg->speaker_pins));
3937 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Takashi Iwai352f7f92012-12-19 12:52:06 +01003940 if (!cfg->hp_pins[0] &&
3941 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3942 memcpy(cfg->hp_pins, cfg->line_out_pins,
3943 sizeof(cfg->hp_pins));
3944 cfg->hp_outs = cfg->line_outs;
3945 }
3946
3947 for (i = 0; i < cfg->hp_outs; i++) {
3948 hda_nid_t nid = cfg->hp_pins[i];
3949 if (!is_jack_detectable(codec, nid))
3950 continue;
3951 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3952 nid);
3953 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003954 spec->hp_automute_hook ?
3955 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003956 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003957 spec->detect_hp = 1;
3958 }
3959
3960 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3961 if (cfg->speaker_outs)
3962 for (i = 0; i < cfg->line_outs; i++) {
3963 hda_nid_t nid = cfg->line_out_pins[i];
3964 if (!is_jack_detectable(codec, nid))
3965 continue;
3966 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3967 snd_hda_jack_detect_enable_callback(codec, nid,
3968 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003969 spec->line_automute_hook ?
3970 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003971 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003972 spec->detect_lo = 1;
3973 }
3974 spec->automute_lo_possible = spec->detect_hp;
3975 }
3976
3977 spec->automute_speaker_possible = cfg->speaker_outs &&
3978 (spec->detect_hp || spec->detect_lo);
3979
3980 spec->automute_lo = spec->automute_lo_possible;
3981 spec->automute_speaker = spec->automute_speaker_possible;
3982
3983 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3984 /* create a control for automute mode */
3985 err = add_automute_mode_enum(codec);
3986 if (err < 0)
3987 return err;
3988 }
3989 return 0;
3990}
3991
Takashi Iwai352f7f92012-12-19 12:52:06 +01003992/* check whether all auto-mic pins are valid; setup indices if OK */
3993static bool auto_mic_check_imux(struct hda_codec *codec)
3994{
3995 struct hda_gen_spec *spec = codec->spec;
3996 const struct hda_input_mux *imux;
3997 int i;
3998
3999 imux = &spec->input_mux;
4000 for (i = 0; i < spec->am_num_entries; i++) {
4001 spec->am_entry[i].idx =
4002 find_idx_in_nid_list(spec->am_entry[i].pin,
4003 spec->imux_pins, imux->num_items);
4004 if (spec->am_entry[i].idx < 0)
4005 return false; /* no corresponding imux */
4006 }
4007
4008 /* we don't need the jack detection for the first pin */
4009 for (i = 1; i < spec->am_num_entries; i++)
4010 snd_hda_jack_detect_enable_callback(codec,
4011 spec->am_entry[i].pin,
4012 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004013 spec->mic_autoswitch_hook ?
4014 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004015 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004016 return true;
4017}
4018
4019static int compare_attr(const void *ap, const void *bp)
4020{
4021 const struct automic_entry *a = ap;
4022 const struct automic_entry *b = bp;
4023 return (int)(a->attr - b->attr);
4024}
4025
4026/*
4027 * Check the availability of auto-mic switch;
4028 * Set up if really supported
4029 */
4030static int check_auto_mic_availability(struct hda_codec *codec)
4031{
4032 struct hda_gen_spec *spec = codec->spec;
4033 struct auto_pin_cfg *cfg = &spec->autocfg;
4034 unsigned int types;
4035 int i, num_pins;
4036
Takashi Iwaid12daf62013-01-07 16:32:11 +01004037 if (spec->suppress_auto_mic)
4038 return 0;
4039
Takashi Iwai352f7f92012-12-19 12:52:06 +01004040 types = 0;
4041 num_pins = 0;
4042 for (i = 0; i < cfg->num_inputs; i++) {
4043 hda_nid_t nid = cfg->inputs[i].pin;
4044 unsigned int attr;
4045 attr = snd_hda_codec_get_pincfg(codec, nid);
4046 attr = snd_hda_get_input_pin_attr(attr);
4047 if (types & (1 << attr))
4048 return 0; /* already occupied */
4049 switch (attr) {
4050 case INPUT_PIN_ATTR_INT:
4051 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4052 return 0; /* invalid type */
4053 break;
4054 case INPUT_PIN_ATTR_UNUSED:
4055 return 0; /* invalid entry */
4056 default:
4057 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4058 return 0; /* invalid type */
4059 if (!spec->line_in_auto_switch &&
4060 cfg->inputs[i].type != AUTO_PIN_MIC)
4061 return 0; /* only mic is allowed */
4062 if (!is_jack_detectable(codec, nid))
4063 return 0; /* no unsol support */
4064 break;
4065 }
4066 if (num_pins >= MAX_AUTO_MIC_PINS)
4067 return 0;
4068 types |= (1 << attr);
4069 spec->am_entry[num_pins].pin = nid;
4070 spec->am_entry[num_pins].attr = attr;
4071 num_pins++;
4072 }
4073
4074 if (num_pins < 2)
4075 return 0;
4076
4077 spec->am_num_entries = num_pins;
4078 /* sort the am_entry in the order of attr so that the pin with a
4079 * higher attr will be selected when the jack is plugged.
4080 */
4081 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4082 compare_attr, NULL);
4083
4084 if (!auto_mic_check_imux(codec))
4085 return 0;
4086
4087 spec->auto_mic = 1;
4088 spec->num_adc_nids = 1;
4089 spec->cur_mux[0] = spec->am_entry[0].idx;
4090 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4091 spec->am_entry[0].pin,
4092 spec->am_entry[1].pin,
4093 spec->am_entry[2].pin);
4094
4095 return 0;
4096}
4097
Takashi Iwai55196ff2013-01-24 17:32:56 +01004098/* power_filter hook; make inactive widgets into power down */
4099static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4100 hda_nid_t nid,
4101 unsigned int power_state)
4102{
4103 if (power_state != AC_PWRST_D0)
4104 return power_state;
4105 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4106 return power_state;
4107 if (is_active_nid(codec, nid, HDA_OUTPUT, 0))
4108 return power_state;
4109 return AC_PWRST_D3;
4110}
4111
Takashi Iwai352f7f92012-12-19 12:52:06 +01004112
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004113/*
4114 * Parse the given BIOS configuration and set up the hda_gen_spec
4115 *
4116 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004117 * or a negative error code
4118 */
4119int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004120 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004121{
4122 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004123 int err;
4124
Takashi Iwai1c70a582013-01-11 17:48:22 +01004125 parse_user_hints(codec);
4126
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004127 if (spec->mixer_nid && !spec->mixer_merge_nid)
4128 spec->mixer_merge_nid = spec->mixer_nid;
4129
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004130 if (cfg != &spec->autocfg) {
4131 spec->autocfg = *cfg;
4132 cfg = &spec->autocfg;
4133 }
4134
David Henningsson6fc4cb92013-01-16 15:58:45 +01004135 fill_all_dac_nids(codec);
4136
Takashi Iwai352f7f92012-12-19 12:52:06 +01004137 if (!cfg->line_outs) {
4138 if (cfg->dig_outs || cfg->dig_in_pin) {
4139 spec->multiout.max_channels = 2;
4140 spec->no_analog = 1;
4141 goto dig_only;
4142 }
4143 return 0; /* can't find valid BIOS pin config */
4144 }
4145
4146 if (!spec->no_primary_hp &&
4147 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4148 cfg->line_outs <= cfg->hp_outs) {
4149 /* use HP as primary out */
4150 cfg->speaker_outs = cfg->line_outs;
4151 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4152 sizeof(cfg->speaker_pins));
4153 cfg->line_outs = cfg->hp_outs;
4154 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4155 cfg->hp_outs = 0;
4156 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4157 cfg->line_out_type = AUTO_PIN_HP_OUT;
4158 }
4159
4160 err = parse_output_paths(codec);
4161 if (err < 0)
4162 return err;
4163 err = create_multi_channel_mode(codec);
4164 if (err < 0)
4165 return err;
4166 err = create_multi_out_ctls(codec, cfg);
4167 if (err < 0)
4168 return err;
4169 err = create_hp_out_ctls(codec);
4170 if (err < 0)
4171 return err;
4172 err = create_speaker_out_ctls(codec);
4173 if (err < 0)
4174 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004175 err = create_indep_hp_ctls(codec);
4176 if (err < 0)
4177 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004178 err = create_loopback_mixing_ctl(codec);
4179 if (err < 0)
4180 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004181 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004182 if (err < 0)
4183 return err;
4184 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004185 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004186 return err;
4187
Takashi Iwaia07a9492013-01-07 16:44:06 +01004188 spec->const_channel_count = spec->ext_channel_count;
4189 /* check the multiple speaker and headphone pins */
4190 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4191 spec->const_channel_count = max(spec->const_channel_count,
4192 cfg->speaker_outs * 2);
4193 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4194 spec->const_channel_count = max(spec->const_channel_count,
4195 cfg->hp_outs * 2);
4196 spec->multiout.max_channels = max(spec->ext_channel_count,
4197 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004198
4199 err = check_auto_mute_availability(codec);
4200 if (err < 0)
4201 return err;
4202
4203 err = check_dyn_adc_switch(codec);
4204 if (err < 0)
4205 return err;
4206
Takashi Iwai967303d2013-02-19 17:12:42 +01004207 err = check_auto_mic_availability(codec);
4208 if (err < 0)
4209 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004210
Takashi Iwai352f7f92012-12-19 12:52:06 +01004211 err = create_capture_mixers(codec);
4212 if (err < 0)
4213 return err;
4214
4215 err = parse_mic_boost(codec);
4216 if (err < 0)
4217 return err;
4218
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004219 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004220 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4221 err = create_out_jack_modes(codec, cfg->line_outs,
4222 cfg->line_out_pins);
4223 if (err < 0)
4224 return err;
4225 }
4226 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4227 err = create_out_jack_modes(codec, cfg->hp_outs,
4228 cfg->hp_pins);
4229 if (err < 0)
4230 return err;
4231 }
4232 }
4233
Takashi Iwai352f7f92012-12-19 12:52:06 +01004234 dig_only:
4235 parse_digital(codec);
4236
Takashi Iwai55196ff2013-01-24 17:32:56 +01004237 if (spec->power_down_unused)
4238 codec->power_filter = snd_hda_gen_path_power_filter;
4239
Takashi Iwai352f7f92012-12-19 12:52:06 +01004240 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004242EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
4244
4245/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004246 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004248
4249/* slave controls for virtual master */
4250static const char * const slave_pfxs[] = {
4251 "Front", "Surround", "Center", "LFE", "Side",
4252 "Headphone", "Speaker", "Mono", "Line Out",
4253 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004254 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4255 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4256 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004257 NULL,
4258};
4259
4260int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004262 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Takashi Iwai36502d02012-12-19 15:15:10 +01004265 if (spec->kctls.used) {
4266 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4267 if (err < 0)
4268 return err;
4269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
Takashi Iwai352f7f92012-12-19 12:52:06 +01004271 if (spec->multiout.dig_out_nid) {
4272 err = snd_hda_create_dig_out_ctls(codec,
4273 spec->multiout.dig_out_nid,
4274 spec->multiout.dig_out_nid,
4275 spec->pcm_rec[1].pcm_type);
4276 if (err < 0)
4277 return err;
4278 if (!spec->no_analog) {
4279 err = snd_hda_create_spdif_share_sw(codec,
4280 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 if (err < 0)
4282 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004283 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 }
4285 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004286 if (spec->dig_in_nid) {
4287 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4288 if (err < 0)
4289 return err;
4290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
Takashi Iwai352f7f92012-12-19 12:52:06 +01004292 /* if we have no master control, let's create it */
4293 if (!spec->no_analog &&
4294 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004295 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004296 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004297 "Playback Volume");
4298 if (err < 0)
4299 return err;
4300 }
4301 if (!spec->no_analog &&
4302 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4303 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4304 NULL, slave_pfxs,
4305 "Playback Switch",
4306 true, &spec->vmaster_mute.sw_kctl);
4307 if (err < 0)
4308 return err;
4309 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004310 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4311 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Takashi Iwai352f7f92012-12-19 12:52:06 +01004314 free_kctls(spec); /* no longer needed */
4315
Takashi Iwai967303d2013-02-19 17:12:42 +01004316 if (spec->hp_mic_pin) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004317 int err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004318 int nid = spec->hp_mic_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004319 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4320 if (err < 0)
4321 return err;
4322 err = snd_hda_jack_detect_enable(codec, nid, 0);
4323 if (err < 0)
4324 return err;
4325 }
4326
4327 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4328 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 return err;
4330
4331 return 0;
4332}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004333EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4334
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
4336/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004337 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004340static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4341 struct hda_codec *codec,
4342 struct snd_pcm_substream *substream,
4343 int action)
4344{
4345 struct hda_gen_spec *spec = codec->spec;
4346 if (spec->pcm_playback_hook)
4347 spec->pcm_playback_hook(hinfo, codec, substream, action);
4348}
4349
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004350static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4351 struct hda_codec *codec,
4352 struct snd_pcm_substream *substream,
4353 int action)
4354{
4355 struct hda_gen_spec *spec = codec->spec;
4356 if (spec->pcm_capture_hook)
4357 spec->pcm_capture_hook(hinfo, codec, substream, action);
4358}
4359
Takashi Iwai352f7f92012-12-19 12:52:06 +01004360/*
4361 * Analog playback callbacks
4362 */
4363static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4364 struct hda_codec *codec,
4365 struct snd_pcm_substream *substream)
4366{
4367 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004368 int err;
4369
4370 mutex_lock(&spec->pcm_mutex);
4371 err = snd_hda_multi_out_analog_open(codec,
4372 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004373 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004374 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004375 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004376 call_pcm_playback_hook(hinfo, codec, substream,
4377 HDA_GEN_PCM_ACT_OPEN);
4378 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004379 mutex_unlock(&spec->pcm_mutex);
4380 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004381}
4382
4383static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004384 struct hda_codec *codec,
4385 unsigned int stream_tag,
4386 unsigned int format,
4387 struct snd_pcm_substream *substream)
4388{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004389 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004390 int err;
4391
4392 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4393 stream_tag, format, substream);
4394 if (!err)
4395 call_pcm_playback_hook(hinfo, codec, substream,
4396 HDA_GEN_PCM_ACT_PREPARE);
4397 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004398}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004399
Takashi Iwai352f7f92012-12-19 12:52:06 +01004400static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4401 struct hda_codec *codec,
4402 struct snd_pcm_substream *substream)
4403{
4404 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004405 int err;
4406
4407 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4408 if (!err)
4409 call_pcm_playback_hook(hinfo, codec, substream,
4410 HDA_GEN_PCM_ACT_CLEANUP);
4411 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004412}
4413
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004414static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4415 struct hda_codec *codec,
4416 struct snd_pcm_substream *substream)
4417{
4418 struct hda_gen_spec *spec = codec->spec;
4419 mutex_lock(&spec->pcm_mutex);
4420 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004421 call_pcm_playback_hook(hinfo, codec, substream,
4422 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004423 mutex_unlock(&spec->pcm_mutex);
4424 return 0;
4425}
4426
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004427static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4428 struct hda_codec *codec,
4429 struct snd_pcm_substream *substream)
4430{
4431 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4432 return 0;
4433}
4434
4435static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4436 struct hda_codec *codec,
4437 unsigned int stream_tag,
4438 unsigned int format,
4439 struct snd_pcm_substream *substream)
4440{
4441 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4442 call_pcm_capture_hook(hinfo, codec, substream,
4443 HDA_GEN_PCM_ACT_PREPARE);
4444 return 0;
4445}
4446
4447static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4448 struct hda_codec *codec,
4449 struct snd_pcm_substream *substream)
4450{
4451 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4452 call_pcm_capture_hook(hinfo, codec, substream,
4453 HDA_GEN_PCM_ACT_CLEANUP);
4454 return 0;
4455}
4456
4457static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4458 struct hda_codec *codec,
4459 struct snd_pcm_substream *substream)
4460{
4461 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4462 return 0;
4463}
4464
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004465static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4466 struct hda_codec *codec,
4467 struct snd_pcm_substream *substream)
4468{
4469 struct hda_gen_spec *spec = codec->spec;
4470 int err = 0;
4471
4472 mutex_lock(&spec->pcm_mutex);
4473 if (!spec->indep_hp_enabled)
4474 err = -EBUSY;
4475 else
4476 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004477 call_pcm_playback_hook(hinfo, codec, substream,
4478 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004479 mutex_unlock(&spec->pcm_mutex);
4480 return err;
4481}
4482
4483static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4484 struct hda_codec *codec,
4485 struct snd_pcm_substream *substream)
4486{
4487 struct hda_gen_spec *spec = codec->spec;
4488 mutex_lock(&spec->pcm_mutex);
4489 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004490 call_pcm_playback_hook(hinfo, codec, substream,
4491 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004492 mutex_unlock(&spec->pcm_mutex);
4493 return 0;
4494}
4495
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004496static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4497 struct hda_codec *codec,
4498 unsigned int stream_tag,
4499 unsigned int format,
4500 struct snd_pcm_substream *substream)
4501{
4502 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4503 call_pcm_playback_hook(hinfo, codec, substream,
4504 HDA_GEN_PCM_ACT_PREPARE);
4505 return 0;
4506}
4507
4508static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4509 struct hda_codec *codec,
4510 struct snd_pcm_substream *substream)
4511{
4512 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4513 call_pcm_playback_hook(hinfo, codec, substream,
4514 HDA_GEN_PCM_ACT_CLEANUP);
4515 return 0;
4516}
4517
Takashi Iwai352f7f92012-12-19 12:52:06 +01004518/*
4519 * Digital out
4520 */
4521static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4522 struct hda_codec *codec,
4523 struct snd_pcm_substream *substream)
4524{
4525 struct hda_gen_spec *spec = codec->spec;
4526 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4527}
4528
4529static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4530 struct hda_codec *codec,
4531 unsigned int stream_tag,
4532 unsigned int format,
4533 struct snd_pcm_substream *substream)
4534{
4535 struct hda_gen_spec *spec = codec->spec;
4536 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4537 stream_tag, format, substream);
4538}
4539
4540static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4541 struct hda_codec *codec,
4542 struct snd_pcm_substream *substream)
4543{
4544 struct hda_gen_spec *spec = codec->spec;
4545 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4546}
4547
4548static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4549 struct hda_codec *codec,
4550 struct snd_pcm_substream *substream)
4551{
4552 struct hda_gen_spec *spec = codec->spec;
4553 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4554}
4555
4556/*
4557 * Analog capture
4558 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004559#define alt_capture_pcm_open capture_pcm_open
4560#define alt_capture_pcm_close capture_pcm_close
4561
Takashi Iwai352f7f92012-12-19 12:52:06 +01004562static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4563 struct hda_codec *codec,
4564 unsigned int stream_tag,
4565 unsigned int format,
4566 struct snd_pcm_substream *substream)
4567{
4568 struct hda_gen_spec *spec = codec->spec;
4569
4570 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004571 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004572 call_pcm_capture_hook(hinfo, codec, substream,
4573 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004574 return 0;
4575}
4576
Takashi Iwai352f7f92012-12-19 12:52:06 +01004577static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4578 struct hda_codec *codec,
4579 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004580{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004581 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004582
Takashi Iwai352f7f92012-12-19 12:52:06 +01004583 snd_hda_codec_cleanup_stream(codec,
4584 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004585 call_pcm_capture_hook(hinfo, codec, substream,
4586 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004587 return 0;
4588}
4589
Takashi Iwai352f7f92012-12-19 12:52:06 +01004590/*
4591 */
4592static const struct hda_pcm_stream pcm_analog_playback = {
4593 .substreams = 1,
4594 .channels_min = 2,
4595 .channels_max = 8,
4596 /* NID is set in build_pcms */
4597 .ops = {
4598 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004599 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004600 .prepare = playback_pcm_prepare,
4601 .cleanup = playback_pcm_cleanup
4602 },
4603};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Takashi Iwai352f7f92012-12-19 12:52:06 +01004605static const struct hda_pcm_stream pcm_analog_capture = {
4606 .substreams = 1,
4607 .channels_min = 2,
4608 .channels_max = 2,
4609 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004610 .ops = {
4611 .open = capture_pcm_open,
4612 .close = capture_pcm_close,
4613 .prepare = capture_pcm_prepare,
4614 .cleanup = capture_pcm_cleanup
4615 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004616};
4617
4618static const struct hda_pcm_stream pcm_analog_alt_playback = {
4619 .substreams = 1,
4620 .channels_min = 2,
4621 .channels_max = 2,
4622 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004623 .ops = {
4624 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004625 .close = alt_playback_pcm_close,
4626 .prepare = alt_playback_pcm_prepare,
4627 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004628 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004629};
4630
4631static const struct hda_pcm_stream pcm_analog_alt_capture = {
4632 .substreams = 2, /* can be overridden */
4633 .channels_min = 2,
4634 .channels_max = 2,
4635 /* NID is set in build_pcms */
4636 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004637 .open = alt_capture_pcm_open,
4638 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004639 .prepare = alt_capture_pcm_prepare,
4640 .cleanup = alt_capture_pcm_cleanup
4641 },
4642};
4643
4644static const struct hda_pcm_stream pcm_digital_playback = {
4645 .substreams = 1,
4646 .channels_min = 2,
4647 .channels_max = 2,
4648 /* NID is set in build_pcms */
4649 .ops = {
4650 .open = dig_playback_pcm_open,
4651 .close = dig_playback_pcm_close,
4652 .prepare = dig_playback_pcm_prepare,
4653 .cleanup = dig_playback_pcm_cleanup
4654 },
4655};
4656
4657static const struct hda_pcm_stream pcm_digital_capture = {
4658 .substreams = 1,
4659 .channels_min = 2,
4660 .channels_max = 2,
4661 /* NID is set in build_pcms */
4662};
4663
4664/* Used by build_pcms to flag that a PCM has no playback stream */
4665static const struct hda_pcm_stream pcm_null_stream = {
4666 .substreams = 0,
4667 .channels_min = 0,
4668 .channels_max = 0,
4669};
4670
4671/*
4672 * dynamic changing ADC PCM streams
4673 */
4674static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4675{
4676 struct hda_gen_spec *spec = codec->spec;
4677 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4678
4679 if (spec->cur_adc && spec->cur_adc != new_adc) {
4680 /* stream is running, let's swap the current ADC */
4681 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4682 spec->cur_adc = new_adc;
4683 snd_hda_codec_setup_stream(codec, new_adc,
4684 spec->cur_adc_stream_tag, 0,
4685 spec->cur_adc_format);
4686 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004688 return false;
4689}
4690
4691/* analog capture with dynamic dual-adc changes */
4692static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4693 struct hda_codec *codec,
4694 unsigned int stream_tag,
4695 unsigned int format,
4696 struct snd_pcm_substream *substream)
4697{
4698 struct hda_gen_spec *spec = codec->spec;
4699 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4700 spec->cur_adc_stream_tag = stream_tag;
4701 spec->cur_adc_format = format;
4702 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4703 return 0;
4704}
4705
4706static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4707 struct hda_codec *codec,
4708 struct snd_pcm_substream *substream)
4709{
4710 struct hda_gen_spec *spec = codec->spec;
4711 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4712 spec->cur_adc = 0;
4713 return 0;
4714}
4715
4716static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4717 .substreams = 1,
4718 .channels_min = 2,
4719 .channels_max = 2,
4720 .nid = 0, /* fill later */
4721 .ops = {
4722 .prepare = dyn_adc_capture_pcm_prepare,
4723 .cleanup = dyn_adc_capture_pcm_cleanup
4724 },
4725};
4726
Takashi Iwaif873e532012-12-20 16:58:39 +01004727static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4728 const char *chip_name)
4729{
4730 char *p;
4731
4732 if (*str)
4733 return;
4734 strlcpy(str, chip_name, len);
4735
4736 /* drop non-alnum chars after a space */
4737 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4738 if (!isalnum(p[1])) {
4739 *p = 0;
4740 break;
4741 }
4742 }
4743 strlcat(str, sfx, len);
4744}
4745
Takashi Iwai352f7f92012-12-19 12:52:06 +01004746/* build PCM streams based on the parsed results */
4747int snd_hda_gen_build_pcms(struct hda_codec *codec)
4748{
4749 struct hda_gen_spec *spec = codec->spec;
4750 struct hda_pcm *info = spec->pcm_rec;
4751 const struct hda_pcm_stream *p;
4752 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
4754 codec->num_pcms = 1;
4755 codec->pcm_info = info;
4756
Takashi Iwai352f7f92012-12-19 12:52:06 +01004757 if (spec->no_analog)
4758 goto skip_analog;
4759
Takashi Iwaif873e532012-12-20 16:58:39 +01004760 fill_pcm_stream_name(spec->stream_name_analog,
4761 sizeof(spec->stream_name_analog),
4762 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004763 info->name = spec->stream_name_analog;
4764
4765 if (spec->multiout.num_dacs > 0) {
4766 p = spec->stream_analog_playback;
4767 if (!p)
4768 p = &pcm_analog_playback;
4769 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4770 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4771 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4772 spec->multiout.max_channels;
4773 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4774 spec->autocfg.line_outs == 2)
4775 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4776 snd_pcm_2_1_chmaps;
4777 }
4778 if (spec->num_adc_nids) {
4779 p = spec->stream_analog_capture;
4780 if (!p) {
4781 if (spec->dyn_adc_switch)
4782 p = &dyn_adc_pcm_analog_capture;
4783 else
4784 p = &pcm_analog_capture;
4785 }
4786 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4787 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4788 }
4789
Takashi Iwai352f7f92012-12-19 12:52:06 +01004790 skip_analog:
4791 /* SPDIF for stream index #1 */
4792 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004793 fill_pcm_stream_name(spec->stream_name_digital,
4794 sizeof(spec->stream_name_digital),
4795 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004796 codec->num_pcms = 2;
4797 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4798 info = spec->pcm_rec + 1;
4799 info->name = spec->stream_name_digital;
4800 if (spec->dig_out_type)
4801 info->pcm_type = spec->dig_out_type;
4802 else
4803 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4804 if (spec->multiout.dig_out_nid) {
4805 p = spec->stream_digital_playback;
4806 if (!p)
4807 p = &pcm_digital_playback;
4808 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4809 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4810 }
4811 if (spec->dig_in_nid) {
4812 p = spec->stream_digital_capture;
4813 if (!p)
4814 p = &pcm_digital_capture;
4815 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4816 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4817 }
4818 }
4819
4820 if (spec->no_analog)
4821 return 0;
4822
4823 /* If the use of more than one ADC is requested for the current
4824 * model, configure a second analog capture-only PCM.
4825 */
4826 have_multi_adcs = (spec->num_adc_nids > 1) &&
4827 !spec->dyn_adc_switch && !spec->auto_mic;
4828 /* Additional Analaog capture for index #2 */
4829 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004830 fill_pcm_stream_name(spec->stream_name_alt_analog,
4831 sizeof(spec->stream_name_alt_analog),
4832 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004833 codec->num_pcms = 3;
4834 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004835 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004836 if (spec->alt_dac_nid) {
4837 p = spec->stream_analog_alt_playback;
4838 if (!p)
4839 p = &pcm_analog_alt_playback;
4840 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4841 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4842 spec->alt_dac_nid;
4843 } else {
4844 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4845 pcm_null_stream;
4846 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4847 }
4848 if (have_multi_adcs) {
4849 p = spec->stream_analog_alt_capture;
4850 if (!p)
4851 p = &pcm_analog_alt_capture;
4852 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4853 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4854 spec->adc_nids[1];
4855 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4856 spec->num_adc_nids - 1;
4857 } else {
4858 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4859 pcm_null_stream;
4860 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 }
4863
4864 return 0;
4865}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004866EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4867
4868
4869/*
4870 * Standard auto-parser initializations
4871 */
4872
Takashi Iwaid4156932013-01-07 10:08:02 +01004873/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004874static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004875{
4876 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004877 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004878
Takashi Iwai196c17662013-01-04 15:01:40 +01004879 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004880 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004881 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004882 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004883 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004884 snd_hda_activate_path(codec, path, path->active, true);
4885 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004886}
4887
4888/* initialize primary output paths */
4889static void init_multi_out(struct hda_codec *codec)
4890{
4891 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004892 int i;
4893
Takashi Iwaid4156932013-01-07 10:08:02 +01004894 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004895 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004896}
4897
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004898
Takashi Iwai2c12c302013-01-10 09:33:29 +01004899static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004900{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004901 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004902
Takashi Iwaid4156932013-01-07 10:08:02 +01004903 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004904 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004905}
4906
4907/* initialize hp and speaker paths */
4908static void init_extra_out(struct hda_codec *codec)
4909{
4910 struct hda_gen_spec *spec = codec->spec;
4911
4912 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004913 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004914 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4915 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004916 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004917}
4918
4919/* initialize multi-io paths */
4920static void init_multi_io(struct hda_codec *codec)
4921{
4922 struct hda_gen_spec *spec = codec->spec;
4923 int i;
4924
4925 for (i = 0; i < spec->multi_ios; i++) {
4926 hda_nid_t pin = spec->multi_io[i].pin;
4927 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004928 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004929 if (!path)
4930 continue;
4931 if (!spec->multi_io[i].ctl_in)
4932 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004933 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004934 snd_hda_activate_path(codec, path, path->active, true);
4935 }
4936}
4937
Takashi Iwai352f7f92012-12-19 12:52:06 +01004938/* set up input pins and loopback paths */
4939static void init_analog_input(struct hda_codec *codec)
4940{
4941 struct hda_gen_spec *spec = codec->spec;
4942 struct auto_pin_cfg *cfg = &spec->autocfg;
4943 int i;
4944
4945 for (i = 0; i < cfg->num_inputs; i++) {
4946 hda_nid_t nid = cfg->inputs[i].pin;
4947 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004948 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004949
4950 /* init loopback inputs */
4951 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01004952 resume_path_from_idx(codec, spec->loopback_paths[i]);
4953 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004954 }
4955 }
4956}
4957
4958/* initialize ADC paths */
4959static void init_input_src(struct hda_codec *codec)
4960{
4961 struct hda_gen_spec *spec = codec->spec;
4962 struct hda_input_mux *imux = &spec->input_mux;
4963 struct nid_path *path;
4964 int i, c, nums;
4965
4966 if (spec->dyn_adc_switch)
4967 nums = 1;
4968 else
4969 nums = spec->num_adc_nids;
4970
4971 for (c = 0; c < nums; c++) {
4972 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004973 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004974 if (path) {
4975 bool active = path->active;
4976 if (i == spec->cur_mux[c])
4977 active = true;
4978 snd_hda_activate_path(codec, path, active, false);
4979 }
4980 }
Takashi Iwai967303d2013-02-19 17:12:42 +01004981 if (spec->hp_mic)
4982 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004983 }
4984
Takashi Iwai352f7f92012-12-19 12:52:06 +01004985 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004986 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004987}
4988
4989/* set right pin controls for digital I/O */
4990static void init_digital(struct hda_codec *codec)
4991{
4992 struct hda_gen_spec *spec = codec->spec;
4993 int i;
4994 hda_nid_t pin;
4995
Takashi Iwaid4156932013-01-07 10:08:02 +01004996 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004997 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004998 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004999 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005000 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005001 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005002 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005003}
5004
Takashi Iwai973e4972012-12-20 15:16:09 +01005005/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5006 * invalid unsol tags by some reason
5007 */
5008static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5009{
5010 int i;
5011
5012 for (i = 0; i < codec->init_pins.used; i++) {
5013 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5014 hda_nid_t nid = pin->nid;
5015 if (is_jack_detectable(codec, nid) &&
5016 !snd_hda_jack_tbl_get(codec, nid))
5017 snd_hda_codec_update_cache(codec, nid, 0,
5018 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5019 }
5020}
5021
Takashi Iwai5187ac12013-01-07 12:52:16 +01005022/*
5023 * initialize the generic spec;
5024 * this can be put as patch_ops.init function
5025 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005026int snd_hda_gen_init(struct hda_codec *codec)
5027{
5028 struct hda_gen_spec *spec = codec->spec;
5029
5030 if (spec->init_hook)
5031 spec->init_hook(codec);
5032
5033 snd_hda_apply_verbs(codec);
5034
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005035 codec->cached_write = 1;
5036
Takashi Iwai352f7f92012-12-19 12:52:06 +01005037 init_multi_out(codec);
5038 init_extra_out(codec);
5039 init_multi_io(codec);
5040 init_analog_input(codec);
5041 init_input_src(codec);
5042 init_digital(codec);
5043
Takashi Iwai973e4972012-12-20 15:16:09 +01005044 clear_unsol_on_unused_pins(codec);
5045
Takashi Iwai352f7f92012-12-19 12:52:06 +01005046 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005047 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005048
Takashi Iwaidc870f32013-01-22 15:24:30 +01005049 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005050
Takashi Iwai352f7f92012-12-19 12:52:06 +01005051 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5052 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5053
5054 hda_call_check_power_status(codec, 0x01);
5055 return 0;
5056}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005057EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5058
Takashi Iwai5187ac12013-01-07 12:52:16 +01005059/*
5060 * free the generic spec;
5061 * this can be put as patch_ops.free function
5062 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005063void snd_hda_gen_free(struct hda_codec *codec)
5064{
5065 snd_hda_gen_spec_free(codec->spec);
5066 kfree(codec->spec);
5067 codec->spec = NULL;
5068}
5069EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5070
5071#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005072/*
5073 * check the loopback power save state;
5074 * this can be put as patch_ops.check_power_status function
5075 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005076int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5077{
5078 struct hda_gen_spec *spec = codec->spec;
5079 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5080}
5081EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5082#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005083
5084
5085/*
5086 * the generic codec support
5087 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
Takashi Iwai352f7f92012-12-19 12:52:06 +01005089static const struct hda_codec_ops generic_patch_ops = {
5090 .build_controls = snd_hda_gen_build_controls,
5091 .build_pcms = snd_hda_gen_build_pcms,
5092 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005093 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005094 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005095#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005096 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005097#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098};
5099
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100int snd_hda_parse_generic_codec(struct hda_codec *codec)
5101{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005102 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 int err;
5104
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005105 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005106 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005108 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005111 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5112 if (err < 0)
5113 return err;
5114
5115 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005116 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 goto error;
5118
5119 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 return 0;
5121
Takashi Iwai352f7f92012-12-19 12:52:06 +01005122error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005123 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 return err;
5125}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005126EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);