blob: c8791225b2ba2ba12e8d7472e018984f215d5fdd [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
1893static int indep_hp_info(struct snd_kcontrol *kcontrol,
1894 struct snd_ctl_elem_info *uinfo)
1895{
1896 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1897}
1898
1899static int indep_hp_get(struct snd_kcontrol *kcontrol,
1900 struct snd_ctl_elem_value *ucontrol)
1901{
1902 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1903 struct hda_gen_spec *spec = codec->spec;
1904 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1905 return 0;
1906}
1907
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001908static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1909 int nomix_path_idx, int mix_path_idx,
1910 int out_type);
1911
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001912static int indep_hp_put(struct snd_kcontrol *kcontrol,
1913 struct snd_ctl_elem_value *ucontrol)
1914{
1915 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1916 struct hda_gen_spec *spec = codec->spec;
1917 unsigned int select = ucontrol->value.enumerated.item[0];
1918 int ret = 0;
1919
1920 mutex_lock(&spec->pcm_mutex);
1921 if (spec->active_streams) {
1922 ret = -EBUSY;
1923 goto unlock;
1924 }
1925
1926 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001927 hda_nid_t *dacp;
1928 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1929 dacp = &spec->private_dac_nids[0];
1930 else
1931 dacp = &spec->multiout.hp_out_nid[0];
1932
1933 /* update HP aamix paths in case it conflicts with indep HP */
1934 if (spec->have_aamix_ctl) {
1935 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1936 update_aamix_paths(codec, spec->aamix_mode,
1937 spec->out_paths[0],
1938 spec->aamix_out_paths[0],
1939 spec->autocfg.line_out_type);
1940 else
1941 update_aamix_paths(codec, spec->aamix_mode,
1942 spec->hp_paths[0],
1943 spec->aamix_out_paths[1],
1944 AUTO_PIN_HP_OUT);
1945 }
1946
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001947 spec->indep_hp_enabled = select;
1948 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001949 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001950 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001951 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01001952
1953 /* update HP auto-mute state too */
1954 if (spec->hp_automute_hook)
1955 spec->hp_automute_hook(codec, NULL);
1956 else
1957 snd_hda_gen_hp_automute(codec, NULL);
1958
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001959 ret = 1;
1960 }
1961 unlock:
1962 mutex_unlock(&spec->pcm_mutex);
1963 return ret;
1964}
1965
1966static const struct snd_kcontrol_new indep_hp_ctl = {
1967 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1968 .name = "Independent HP",
1969 .info = indep_hp_info,
1970 .get = indep_hp_get,
1971 .put = indep_hp_put,
1972};
1973
1974
1975static int create_indep_hp_ctls(struct hda_codec *codec)
1976{
1977 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001978 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001979
1980 if (!spec->indep_hp)
1981 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001982 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1983 dac = spec->multiout.dac_nids[0];
1984 else
1985 dac = spec->multiout.hp_out_nid[0];
1986 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001987 spec->indep_hp = 0;
1988 return 0;
1989 }
1990
1991 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001992 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001993 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1994 return -ENOMEM;
1995 return 0;
1996}
1997
1998/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001999 * channel mode enum control
2000 */
2001
2002static int ch_mode_info(struct snd_kcontrol *kcontrol,
2003 struct snd_ctl_elem_info *uinfo)
2004{
2005 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2006 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002007 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002008
2009 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2010 uinfo->count = 1;
2011 uinfo->value.enumerated.items = spec->multi_ios + 1;
2012 if (uinfo->value.enumerated.item > spec->multi_ios)
2013 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002014 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2015 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002016 return 0;
2017}
2018
2019static int ch_mode_get(struct snd_kcontrol *kcontrol,
2020 struct snd_ctl_elem_value *ucontrol)
2021{
2022 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2023 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002024 ucontrol->value.enumerated.item[0] =
2025 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002026 return 0;
2027}
2028
Takashi Iwai196c17662013-01-04 15:01:40 +01002029static inline struct nid_path *
2030get_multiio_path(struct hda_codec *codec, int idx)
2031{
2032 struct hda_gen_spec *spec = codec->spec;
2033 return snd_hda_get_path_from_idx(codec,
2034 spec->out_paths[spec->autocfg.line_outs + idx]);
2035}
2036
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002037static void update_automute_all(struct hda_codec *codec);
2038
Takashi Iwai352f7f92012-12-19 12:52:06 +01002039static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2040{
2041 struct hda_gen_spec *spec = codec->spec;
2042 hda_nid_t nid = spec->multi_io[idx].pin;
2043 struct nid_path *path;
2044
Takashi Iwai196c17662013-01-04 15:01:40 +01002045 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002046 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002048
2049 if (path->active == output)
2050 return 0;
2051
2052 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002053 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002054 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002055 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002056 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002057 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002058 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002059 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002060 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002062
2063 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002064 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002065
Takashi Iwai352f7f92012-12-19 12:52:06 +01002066 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
Takashi Iwai352f7f92012-12-19 12:52:06 +01002069static int ch_mode_put(struct snd_kcontrol *kcontrol,
2070 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002072 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2073 struct hda_gen_spec *spec = codec->spec;
2074 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Takashi Iwai352f7f92012-12-19 12:52:06 +01002076 ch = ucontrol->value.enumerated.item[0];
2077 if (ch < 0 || ch > spec->multi_ios)
2078 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002079 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002080 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002081 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002082 for (i = 0; i < spec->multi_ios; i++)
2083 set_multi_io(codec, i, i < ch);
2084 spec->multiout.max_channels = max(spec->ext_channel_count,
2085 spec->const_channel_count);
2086 if (spec->need_dac_fix)
2087 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return 1;
2089}
2090
Takashi Iwai352f7f92012-12-19 12:52:06 +01002091static const struct snd_kcontrol_new channel_mode_enum = {
2092 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2093 .name = "Channel Mode",
2094 .info = ch_mode_info,
2095 .get = ch_mode_get,
2096 .put = ch_mode_put,
2097};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Takashi Iwai352f7f92012-12-19 12:52:06 +01002099static int create_multi_channel_mode(struct hda_codec *codec)
2100{
2101 struct hda_gen_spec *spec = codec->spec;
2102
2103 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002104 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002105 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return 0;
2108}
2109
Takashi Iwai352f7f92012-12-19 12:52:06 +01002110/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002111 * aamix loopback enable/disable switch
2112 */
2113
2114#define loopback_mixing_info indep_hp_info
2115
2116static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2117 struct snd_ctl_elem_value *ucontrol)
2118{
2119 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2120 struct hda_gen_spec *spec = codec->spec;
2121 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2122 return 0;
2123}
2124
2125static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002126 int nomix_path_idx, int mix_path_idx,
2127 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002128{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002129 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002130 struct nid_path *nomix_path, *mix_path;
2131
2132 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2133 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2134 if (!nomix_path || !mix_path)
2135 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002136
2137 /* if HP aamix path is driven from a different DAC and the
2138 * independent HP mode is ON, can't turn on aamix path
2139 */
2140 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2141 mix_path->path[0] != spec->alt_dac_nid)
2142 do_mix = false;
2143
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002144 if (do_mix) {
2145 snd_hda_activate_path(codec, nomix_path, false, true);
2146 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002147 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002148 } else {
2149 snd_hda_activate_path(codec, mix_path, false, true);
2150 snd_hda_activate_path(codec, nomix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002151 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002152 }
2153}
2154
2155static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2156 struct snd_ctl_elem_value *ucontrol)
2157{
2158 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2159 struct hda_gen_spec *spec = codec->spec;
2160 unsigned int val = ucontrol->value.enumerated.item[0];
2161
2162 if (val == spec->aamix_mode)
2163 return 0;
2164 spec->aamix_mode = val;
2165 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002166 spec->aamix_out_paths[0],
2167 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002168 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002169 spec->aamix_out_paths[1],
2170 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002171 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002172 spec->aamix_out_paths[2],
2173 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002174 return 1;
2175}
2176
2177static const struct snd_kcontrol_new loopback_mixing_enum = {
2178 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2179 .name = "Loopback Mixing",
2180 .info = loopback_mixing_info,
2181 .get = loopback_mixing_get,
2182 .put = loopback_mixing_put,
2183};
2184
2185static int create_loopback_mixing_ctl(struct hda_codec *codec)
2186{
2187 struct hda_gen_spec *spec = codec->spec;
2188
2189 if (!spec->mixer_nid)
2190 return 0;
2191 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2192 spec->aamix_out_paths[2]))
2193 return 0;
2194 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2195 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002196 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002197 return 0;
2198}
2199
2200/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002201 * shared headphone/mic handling
2202 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002203
Takashi Iwai352f7f92012-12-19 12:52:06 +01002204static void call_update_outputs(struct hda_codec *codec);
2205
2206/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002207static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002208{
2209 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002210 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002211 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002212 hda_nid_t pin;
2213
2214 pin = spec->hp_mic_pin;
2215 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2216
2217 if (!force) {
2218 val = snd_hda_codec_get_pin_target(codec, pin);
2219 if (as_mic) {
2220 if (val & PIN_IN)
2221 return;
2222 } else {
2223 if (val & PIN_OUT)
2224 return;
2225 }
2226 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002227
2228 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002229 /* if the HP pin doesn't support VREF and the codec driver gives an
2230 * alternative pin, set up the VREF on that pin instead
2231 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002232 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2233 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2234 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2235 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002236 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002237 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002238 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002239
Takashi Iwai967303d2013-02-19 17:12:42 +01002240 if (as_mic)
2241 val |= PIN_IN;
2242 else
2243 val = PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002244 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002245
Takashi Iwai967303d2013-02-19 17:12:42 +01002246 /* update HP auto-mute state too */
2247 if (spec->hp_automute_hook)
2248 spec->hp_automute_hook(codec, NULL);
2249 else
2250 snd_hda_gen_hp_automute(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002251}
2252
2253/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002254static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002255{
2256 struct hda_gen_spec *spec = codec->spec;
2257 struct auto_pin_cfg *cfg = &spec->autocfg;
2258 unsigned int defcfg;
2259 hda_nid_t nid;
2260
Takashi Iwai967303d2013-02-19 17:12:42 +01002261 if (!spec->hp_mic) {
2262 if (spec->suppress_hp_mic_detect)
2263 return 0;
2264 /* automatic detection: only if no input or a single internal
2265 * input pin is found, try to detect the shared hp/mic
2266 */
2267 if (cfg->num_inputs > 1)
2268 return 0;
2269 else if (cfg->num_inputs == 1) {
2270 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2271 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2272 return 0;
2273 }
2274 }
2275
2276 spec->hp_mic = 0; /* clear once */
2277 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002278 return 0;
2279
Takashi Iwai967303d2013-02-19 17:12:42 +01002280 nid = 0;
2281 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2282 nid = cfg->line_out_pins[0];
2283 else if (cfg->hp_outs > 0)
2284 nid = cfg->hp_pins[0];
2285 if (!nid)
2286 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002287
2288 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2289 return 0; /* no input */
2290
Takashi Iwai967303d2013-02-19 17:12:42 +01002291 cfg->inputs[cfg->num_inputs].pin = nid;
2292 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2293 cfg->num_inputs++;
2294 spec->hp_mic = 1;
2295 spec->hp_mic_pin = nid;
2296 /* we can't handle auto-mic together with HP-mic */
2297 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002298 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2299 return 0;
2300}
2301
Takashi Iwai978e77e2013-01-10 16:57:58 +01002302/*
2303 * output jack mode
2304 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002305
2306static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2307
2308static const char * const out_jack_texts[] = {
2309 "Line Out", "Headphone Out",
2310};
2311
Takashi Iwai978e77e2013-01-10 16:57:58 +01002312static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2313 struct snd_ctl_elem_info *uinfo)
2314{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002315 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002316}
2317
2318static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2319 struct snd_ctl_elem_value *ucontrol)
2320{
2321 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2322 hda_nid_t nid = kcontrol->private_value;
2323 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2324 ucontrol->value.enumerated.item[0] = 1;
2325 else
2326 ucontrol->value.enumerated.item[0] = 0;
2327 return 0;
2328}
2329
2330static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_value *ucontrol)
2332{
2333 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2334 hda_nid_t nid = kcontrol->private_value;
2335 unsigned int val;
2336
2337 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2338 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2339 return 0;
2340 snd_hda_set_pin_ctl_cache(codec, nid, val);
2341 return 1;
2342}
2343
2344static const struct snd_kcontrol_new out_jack_mode_enum = {
2345 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2346 .info = out_jack_mode_info,
2347 .get = out_jack_mode_get,
2348 .put = out_jack_mode_put,
2349};
2350
2351static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2352{
2353 struct hda_gen_spec *spec = codec->spec;
2354 int i;
2355
2356 for (i = 0; i < spec->kctls.used; i++) {
2357 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2358 if (!strcmp(kctl->name, name) && kctl->index == idx)
2359 return true;
2360 }
2361 return false;
2362}
2363
2364static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2365 char *name, size_t name_len)
2366{
2367 struct hda_gen_spec *spec = codec->spec;
2368 int idx = 0;
2369
2370 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2371 strlcat(name, " Jack Mode", name_len);
2372
2373 for (; find_kctl_name(codec, name, idx); idx++)
2374 ;
2375}
2376
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002377static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2378{
2379 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002380 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002381 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2382 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2383 return 2;
2384 }
2385 return 1;
2386}
2387
Takashi Iwai978e77e2013-01-10 16:57:58 +01002388static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2389 hda_nid_t *pins)
2390{
2391 struct hda_gen_spec *spec = codec->spec;
2392 int i;
2393
2394 for (i = 0; i < num_pins; i++) {
2395 hda_nid_t pin = pins[i];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002396 if (pin == spec->hp_mic_pin) {
2397 int ret = create_hp_mic_jack_mode(codec, pin);
2398 if (ret < 0)
2399 return ret;
2400 continue;
2401 }
2402 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002403 struct snd_kcontrol_new *knew;
2404 char name[44];
2405 get_jack_mode_name(codec, pin, name, sizeof(name));
2406 knew = snd_hda_gen_add_kctl(spec, name,
2407 &out_jack_mode_enum);
2408 if (!knew)
2409 return -ENOMEM;
2410 knew->private_value = pin;
2411 }
2412 }
2413
2414 return 0;
2415}
2416
Takashi Iwai294765582013-01-17 09:52:11 +01002417/*
2418 * input jack mode
2419 */
2420
2421/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2422#define NUM_VREFS 6
2423
2424static const char * const vref_texts[NUM_VREFS] = {
2425 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2426 "", "Mic 80pc Bias", "Mic 100pc Bias"
2427};
2428
2429static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2430{
2431 unsigned int pincap;
2432
2433 pincap = snd_hda_query_pin_caps(codec, pin);
2434 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2435 /* filter out unusual vrefs */
2436 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2437 return pincap;
2438}
2439
2440/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2441static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2442{
2443 unsigned int i, n = 0;
2444
2445 for (i = 0; i < NUM_VREFS; i++) {
2446 if (vref_caps & (1 << i)) {
2447 if (n == item_idx)
2448 return i;
2449 n++;
2450 }
2451 }
2452 return 0;
2453}
2454
2455/* convert back from the vref ctl index to the enum item index */
2456static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2457{
2458 unsigned int i, n = 0;
2459
2460 for (i = 0; i < NUM_VREFS; i++) {
2461 if (i == idx)
2462 return n;
2463 if (vref_caps & (1 << i))
2464 n++;
2465 }
2466 return 0;
2467}
2468
2469static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2470 struct snd_ctl_elem_info *uinfo)
2471{
2472 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2473 hda_nid_t nid = kcontrol->private_value;
2474 unsigned int vref_caps = get_vref_caps(codec, nid);
2475
2476 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2477 vref_texts);
2478 /* set the right text */
2479 strcpy(uinfo->value.enumerated.name,
2480 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2481 return 0;
2482}
2483
2484static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2485 struct snd_ctl_elem_value *ucontrol)
2486{
2487 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2488 hda_nid_t nid = kcontrol->private_value;
2489 unsigned int vref_caps = get_vref_caps(codec, nid);
2490 unsigned int idx;
2491
2492 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2493 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2494 return 0;
2495}
2496
2497static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2498 struct snd_ctl_elem_value *ucontrol)
2499{
2500 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2501 hda_nid_t nid = kcontrol->private_value;
2502 unsigned int vref_caps = get_vref_caps(codec, nid);
2503 unsigned int val, idx;
2504
2505 val = snd_hda_codec_get_pin_target(codec, nid);
2506 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2507 if (idx == ucontrol->value.enumerated.item[0])
2508 return 0;
2509
2510 val &= ~AC_PINCTL_VREFEN;
2511 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2512 snd_hda_set_pin_ctl_cache(codec, nid, val);
2513 return 1;
2514}
2515
2516static const struct snd_kcontrol_new in_jack_mode_enum = {
2517 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2518 .info = in_jack_mode_info,
2519 .get = in_jack_mode_get,
2520 .put = in_jack_mode_put,
2521};
2522
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002523static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2524{
2525 struct hda_gen_spec *spec = codec->spec;
2526 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002527 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002528 nitems = hweight32(get_vref_caps(codec, pin));
2529 return nitems ? nitems : 1;
2530}
2531
Takashi Iwai294765582013-01-17 09:52:11 +01002532static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2533{
2534 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002535 struct snd_kcontrol_new *knew;
2536 char name[44];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002537 unsigned int defcfg;
2538
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002539 if (pin == spec->hp_mic_pin)
2540 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002541
2542 /* no jack mode for fixed pins */
2543 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2544 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2545 return 0;
2546
2547 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002548 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002549 return 0;
2550
2551 get_jack_mode_name(codec, pin, name, sizeof(name));
2552 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2553 if (!knew)
2554 return -ENOMEM;
2555 knew->private_value = pin;
2556 return 0;
2557}
2558
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002559/*
2560 * HP/mic shared jack mode
2561 */
2562static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2563 struct snd_ctl_elem_info *uinfo)
2564{
2565 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2566 hda_nid_t nid = kcontrol->private_value;
2567 int out_jacks = get_out_jack_num_items(codec, nid);
2568 int in_jacks = get_in_jack_num_items(codec, nid);
2569 const char *text = NULL;
2570 int idx;
2571
2572 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2573 uinfo->count = 1;
2574 uinfo->value.enumerated.items = out_jacks + in_jacks;
2575 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2576 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2577 idx = uinfo->value.enumerated.item;
2578 if (idx < out_jacks) {
2579 if (out_jacks > 1)
2580 text = out_jack_texts[idx];
2581 else
2582 text = "Headphone Out";
2583 } else {
2584 idx -= out_jacks;
2585 if (in_jacks > 1) {
2586 unsigned int vref_caps = get_vref_caps(codec, nid);
2587 text = vref_texts[get_vref_idx(vref_caps, idx)];
2588 } else
2589 text = "Mic In";
2590 }
2591
2592 strcpy(uinfo->value.enumerated.name, text);
2593 return 0;
2594}
2595
2596static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2597{
2598 int out_jacks = get_out_jack_num_items(codec, nid);
2599 int in_jacks = get_in_jack_num_items(codec, nid);
2600 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2601 int idx = 0;
2602
2603 if (val & PIN_OUT) {
2604 if (out_jacks > 1 && val == PIN_HP)
2605 idx = 1;
2606 } else if (val & PIN_IN) {
2607 idx = out_jacks;
2608 if (in_jacks > 1) {
2609 unsigned int vref_caps = get_vref_caps(codec, nid);
2610 val &= AC_PINCTL_VREFEN;
2611 idx += cvt_from_vref_idx(vref_caps, val);
2612 }
2613 }
2614 return idx;
2615}
2616
2617static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2618 struct snd_ctl_elem_value *ucontrol)
2619{
2620 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2621 hda_nid_t nid = kcontrol->private_value;
2622 ucontrol->value.enumerated.item[0] =
2623 get_cur_hp_mic_jack_mode(codec, nid);
2624 return 0;
2625}
2626
2627static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2628 struct snd_ctl_elem_value *ucontrol)
2629{
2630 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2631 hda_nid_t nid = kcontrol->private_value;
2632 int out_jacks = get_out_jack_num_items(codec, nid);
2633 int in_jacks = get_in_jack_num_items(codec, nid);
2634 unsigned int val, oldval, idx;
2635
2636 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2637 idx = ucontrol->value.enumerated.item[0];
2638 if (oldval == idx)
2639 return 0;
2640
2641 if (idx < out_jacks) {
2642 if (out_jacks > 1)
2643 val = idx ? PIN_HP : PIN_OUT;
2644 else
2645 val = PIN_HP;
2646 } else {
2647 idx -= out_jacks;
2648 if (in_jacks > 1) {
2649 unsigned int vref_caps = get_vref_caps(codec, nid);
2650 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002651 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2652 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002653 } else
2654 val = snd_hda_get_default_vref(codec, nid);
2655 }
2656 snd_hda_set_pin_ctl_cache(codec, nid, val);
2657 return 1;
2658}
2659
2660static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2661 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2662 .info = hp_mic_jack_mode_info,
2663 .get = hp_mic_jack_mode_get,
2664 .put = hp_mic_jack_mode_put,
2665};
2666
2667static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2668{
2669 struct hda_gen_spec *spec = codec->spec;
2670 struct snd_kcontrol_new *knew;
2671
2672 if (get_out_jack_num_items(codec, pin) <= 1 &&
2673 get_in_jack_num_items(codec, pin) <= 1)
2674 return 0; /* no need */
2675 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2676 &hp_mic_jack_mode_enum);
2677 if (!knew)
2678 return -ENOMEM;
2679 knew->private_value = pin;
2680 return 0;
2681}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002682
2683/*
2684 * Parse input paths
2685 */
2686
Takashi Iwai352f7f92012-12-19 12:52:06 +01002687/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002688static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002689{
2690 struct hda_amp_list *list;
2691
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002692 list = snd_array_new(&spec->loopback_list);
2693 if (!list)
2694 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002695 list->nid = mix;
2696 list->dir = HDA_INPUT;
2697 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002698 spec->loopback.amplist = spec->loopback_list.list;
2699 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002700}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002701
Takashi Iwai352f7f92012-12-19 12:52:06 +01002702/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002703static int new_analog_input(struct hda_codec *codec, int input_idx,
2704 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002705 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002707 struct hda_gen_spec *spec = codec->spec;
2708 struct nid_path *path;
2709 unsigned int val;
2710 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Takashi Iwai352f7f92012-12-19 12:52:06 +01002712 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2713 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2714 return 0; /* no need for analog loopback */
2715
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002716 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002717 if (!path)
2718 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002719 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002720 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002721
2722 idx = path->idx[path->depth - 1];
2723 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2724 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2725 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002726 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002728 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 }
2730
Takashi Iwai352f7f92012-12-19 12:52:06 +01002731 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2732 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2733 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002734 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002736 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 }
2738
Takashi Iwai352f7f92012-12-19 12:52:06 +01002739 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002740 err = add_loopback_list(spec, mix_nid, idx);
2741 if (err < 0)
2742 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002743
2744 if (spec->mixer_nid != spec->mixer_merge_nid &&
2745 !spec->loopback_merge_path) {
2746 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2747 spec->mixer_merge_nid, 0);
2748 if (path) {
2749 print_nid_path("loopback-merge", path);
2750 path->active = true;
2751 spec->loopback_merge_path =
2752 snd_hda_get_path_idx(codec, path);
2753 }
2754 }
2755
Takashi Iwai352f7f92012-12-19 12:52:06 +01002756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
Takashi Iwai352f7f92012-12-19 12:52:06 +01002759static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002761 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2762 return (pincap & AC_PINCAP_IN) != 0;
2763}
2764
2765/* Parse the codec tree and retrieve ADCs */
2766static int fill_adc_nids(struct hda_codec *codec)
2767{
2768 struct hda_gen_spec *spec = codec->spec;
2769 hda_nid_t nid;
2770 hda_nid_t *adc_nids = spec->adc_nids;
2771 int max_nums = ARRAY_SIZE(spec->adc_nids);
2772 int i, nums = 0;
2773
2774 nid = codec->start_nid;
2775 for (i = 0; i < codec->num_nodes; i++, nid++) {
2776 unsigned int caps = get_wcaps(codec, nid);
2777 int type = get_wcaps_type(caps);
2778
2779 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2780 continue;
2781 adc_nids[nums] = nid;
2782 if (++nums >= max_nums)
2783 break;
2784 }
2785 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002786
2787 /* copy the detected ADCs to all_adcs[] */
2788 spec->num_all_adcs = nums;
2789 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2790
Takashi Iwai352f7f92012-12-19 12:52:06 +01002791 return nums;
2792}
2793
2794/* filter out invalid adc_nids that don't give all active input pins;
2795 * if needed, check whether dynamic ADC-switching is available
2796 */
2797static int check_dyn_adc_switch(struct hda_codec *codec)
2798{
2799 struct hda_gen_spec *spec = codec->spec;
2800 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002801 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002802 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002803
Takashi Iwai352f7f92012-12-19 12:52:06 +01002804 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002805 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002806 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002807 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002808 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002809 break;
2810 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002811 if (i >= imux->num_items) {
2812 ok_bits |= (1 << n);
2813 nums++;
2814 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002815 }
2816
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002817 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002818 /* check whether ADC-switch is possible */
2819 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002820 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002821 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002822 spec->dyn_adc_idx[i] = n;
2823 break;
2824 }
2825 }
2826 }
2827
2828 snd_printdd("hda-codec: enabling ADC switching\n");
2829 spec->dyn_adc_switch = 1;
2830 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002831 /* shrink the invalid adcs and input paths */
2832 nums = 0;
2833 for (n = 0; n < spec->num_adc_nids; n++) {
2834 if (!(ok_bits & (1 << n)))
2835 continue;
2836 if (n != nums) {
2837 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002838 for (i = 0; i < imux->num_items; i++) {
2839 invalidate_nid_path(codec,
2840 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002841 spec->input_paths[i][nums] =
2842 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002843 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002844 }
2845 nums++;
2846 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002847 spec->num_adc_nids = nums;
2848 }
2849
Takashi Iwai967303d2013-02-19 17:12:42 +01002850 if (imux->num_items == 1 ||
2851 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002852 snd_printdd("hda-codec: reducing to a single ADC\n");
2853 spec->num_adc_nids = 1; /* reduce to a single ADC */
2854 }
2855
2856 /* single index for individual volumes ctls */
2857 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2858 spec->num_adc_nids = 1;
2859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return 0;
2861}
2862
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002863/* parse capture source paths from the given pin and create imux items */
2864static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002865 int cfg_idx, int num_adcs,
2866 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002867{
2868 struct hda_gen_spec *spec = codec->spec;
2869 struct hda_input_mux *imux = &spec->input_mux;
2870 int imux_idx = imux->num_items;
2871 bool imux_added = false;
2872 int c;
2873
2874 for (c = 0; c < num_adcs; c++) {
2875 struct nid_path *path;
2876 hda_nid_t adc = spec->adc_nids[c];
2877
2878 if (!is_reachable_path(codec, pin, adc))
2879 continue;
2880 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2881 if (!path)
2882 continue;
2883 print_nid_path("input", path);
2884 spec->input_paths[imux_idx][c] =
2885 snd_hda_get_path_idx(codec, path);
2886
2887 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01002888 if (spec->hp_mic_pin == pin)
2889 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002890 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002891 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002892 imux_added = true;
2893 }
2894 }
2895
2896 return 0;
2897}
2898
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002900 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002902
Takashi Iwaic9700422013-01-18 10:17:30 +01002903/* fill the label for each input at first */
2904static int fill_input_pin_labels(struct hda_codec *codec)
2905{
2906 struct hda_gen_spec *spec = codec->spec;
2907 const struct auto_pin_cfg *cfg = &spec->autocfg;
2908 int i;
2909
2910 for (i = 0; i < cfg->num_inputs; i++) {
2911 hda_nid_t pin = cfg->inputs[i].pin;
2912 const char *label;
2913 int j, idx;
2914
2915 if (!is_input_pin(codec, pin))
2916 continue;
2917
2918 label = hda_get_autocfg_input_label(codec, cfg, i);
2919 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002920 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002921 if (spec->input_labels[j] &&
2922 !strcmp(spec->input_labels[j], label)) {
2923 idx = spec->input_label_idxs[j] + 1;
2924 break;
2925 }
2926 }
2927
2928 spec->input_labels[i] = label;
2929 spec->input_label_idxs[i] = idx;
2930 }
2931
2932 return 0;
2933}
2934
Takashi Iwai9dba2052013-01-18 10:01:15 +01002935#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2936
Takashi Iwai352f7f92012-12-19 12:52:06 +01002937static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002938{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002939 struct hda_gen_spec *spec = codec->spec;
2940 const struct auto_pin_cfg *cfg = &spec->autocfg;
2941 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002942 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002943 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002944 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002945
Takashi Iwai352f7f92012-12-19 12:52:06 +01002946 num_adcs = fill_adc_nids(codec);
2947 if (num_adcs < 0)
2948 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002949
Takashi Iwaic9700422013-01-18 10:17:30 +01002950 err = fill_input_pin_labels(codec);
2951 if (err < 0)
2952 return err;
2953
Takashi Iwai352f7f92012-12-19 12:52:06 +01002954 for (i = 0; i < cfg->num_inputs; i++) {
2955 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Takashi Iwai352f7f92012-12-19 12:52:06 +01002957 pin = cfg->inputs[i].pin;
2958 if (!is_input_pin(codec, pin))
2959 continue;
2960
Takashi Iwai2c12c302013-01-10 09:33:29 +01002961 val = PIN_IN;
2962 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2963 val |= snd_hda_get_default_vref(codec, pin);
2964 set_pin_target(codec, pin, val, false);
2965
Takashi Iwai352f7f92012-12-19 12:52:06 +01002966 if (mixer) {
2967 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002968 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002969 spec->input_labels[i],
2970 spec->input_label_idxs[i],
2971 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002972 if (err < 0)
2973 return err;
2974 }
2975 }
2976
Takashi Iwaic9700422013-01-18 10:17:30 +01002977 err = parse_capture_source(codec, pin, i, num_adcs,
2978 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002979 if (err < 0)
2980 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002981
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002982 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01002983 err = create_in_jack_mode(codec, pin);
2984 if (err < 0)
2985 return err;
2986 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002987 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002988
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002989 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002990 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002991 "Stereo Mix", 0);
2992 if (err < 0)
2993 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002994 }
2995
2996 return 0;
2997}
2998
2999
3000/*
3001 * input source mux
3002 */
3003
Takashi Iwaic697b712013-01-07 17:09:26 +01003004/* get the input path specified by the given adc and imux indices */
3005static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003006{
3007 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003008 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3009 snd_BUG();
3010 return NULL;
3011 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003012 if (spec->dyn_adc_switch)
3013 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003014 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003015 snd_BUG();
3016 return NULL;
3017 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003018 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003019}
3020
3021static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3022 unsigned int idx);
3023
3024static int mux_enum_info(struct snd_kcontrol *kcontrol,
3025 struct snd_ctl_elem_info *uinfo)
3026{
3027 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3028 struct hda_gen_spec *spec = codec->spec;
3029 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3030}
3031
3032static int mux_enum_get(struct snd_kcontrol *kcontrol,
3033 struct snd_ctl_elem_value *ucontrol)
3034{
3035 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3036 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003037 /* the ctls are created at once with multiple counts */
3038 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003039
3040 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3041 return 0;
3042}
3043
3044static int mux_enum_put(struct snd_kcontrol *kcontrol,
3045 struct snd_ctl_elem_value *ucontrol)
3046{
3047 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003048 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003049 return mux_select(codec, adc_idx,
3050 ucontrol->value.enumerated.item[0]);
3051}
3052
Takashi Iwai352f7f92012-12-19 12:52:06 +01003053static const struct snd_kcontrol_new cap_src_temp = {
3054 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3055 .name = "Input Source",
3056 .info = mux_enum_info,
3057 .get = mux_enum_get,
3058 .put = mux_enum_put,
3059};
3060
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003061/*
3062 * capture volume and capture switch ctls
3063 */
3064
Takashi Iwai352f7f92012-12-19 12:52:06 +01003065typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3066 struct snd_ctl_elem_value *ucontrol);
3067
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003068/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003069static int cap_put_caller(struct snd_kcontrol *kcontrol,
3070 struct snd_ctl_elem_value *ucontrol,
3071 put_call_t func, int type)
3072{
3073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3074 struct hda_gen_spec *spec = codec->spec;
3075 const struct hda_input_mux *imux;
3076 struct nid_path *path;
3077 int i, adc_idx, err = 0;
3078
3079 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003080 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003081 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003082 /* we use the cache-only update at first since multiple input paths
3083 * may shared the same amp; by updating only caches, the redundant
3084 * writes to hardware can be reduced.
3085 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003086 codec->cached_write = 1;
3087 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003088 path = get_input_path(codec, adc_idx, i);
3089 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003090 continue;
3091 kcontrol->private_value = path->ctls[type];
3092 err = func(kcontrol, ucontrol);
3093 if (err < 0)
3094 goto error;
3095 }
3096 error:
3097 codec->cached_write = 0;
3098 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003099 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003100 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003101 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003102 return err;
3103}
3104
3105/* capture volume ctl callbacks */
3106#define cap_vol_info snd_hda_mixer_amp_volume_info
3107#define cap_vol_get snd_hda_mixer_amp_volume_get
3108#define cap_vol_tlv snd_hda_mixer_amp_tlv
3109
3110static int cap_vol_put(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_value *ucontrol)
3112{
3113 return cap_put_caller(kcontrol, ucontrol,
3114 snd_hda_mixer_amp_volume_put,
3115 NID_PATH_VOL_CTL);
3116}
3117
3118static const struct snd_kcontrol_new cap_vol_temp = {
3119 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3120 .name = "Capture Volume",
3121 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3122 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3123 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3124 .info = cap_vol_info,
3125 .get = cap_vol_get,
3126 .put = cap_vol_put,
3127 .tlv = { .c = cap_vol_tlv },
3128};
3129
3130/* capture switch ctl callbacks */
3131#define cap_sw_info snd_ctl_boolean_stereo_info
3132#define cap_sw_get snd_hda_mixer_amp_switch_get
3133
3134static int cap_sw_put(struct snd_kcontrol *kcontrol,
3135 struct snd_ctl_elem_value *ucontrol)
3136{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003137 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003138 snd_hda_mixer_amp_switch_put,
3139 NID_PATH_MUTE_CTL);
3140}
3141
3142static const struct snd_kcontrol_new cap_sw_temp = {
3143 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3144 .name = "Capture Switch",
3145 .info = cap_sw_info,
3146 .get = cap_sw_get,
3147 .put = cap_sw_put,
3148};
3149
3150static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3151{
3152 hda_nid_t nid;
3153 int i, depth;
3154
3155 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3156 for (depth = 0; depth < 3; depth++) {
3157 if (depth >= path->depth)
3158 return -EINVAL;
3159 i = path->depth - depth - 1;
3160 nid = path->path[i];
3161 if (!path->ctls[NID_PATH_VOL_CTL]) {
3162 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3163 path->ctls[NID_PATH_VOL_CTL] =
3164 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3165 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3166 int idx = path->idx[i];
3167 if (!depth && codec->single_adc_amp)
3168 idx = 0;
3169 path->ctls[NID_PATH_VOL_CTL] =
3170 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3171 }
3172 }
3173 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3174 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3175 path->ctls[NID_PATH_MUTE_CTL] =
3176 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3177 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3178 int idx = path->idx[i];
3179 if (!depth && codec->single_adc_amp)
3180 idx = 0;
3181 path->ctls[NID_PATH_MUTE_CTL] =
3182 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3183 }
3184 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 return 0;
3187}
3188
Takashi Iwai352f7f92012-12-19 12:52:06 +01003189static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003191 struct hda_gen_spec *spec = codec->spec;
3192 struct auto_pin_cfg *cfg = &spec->autocfg;
3193 unsigned int val;
3194 int i;
3195
3196 if (!spec->inv_dmic_split)
3197 return false;
3198 for (i = 0; i < cfg->num_inputs; i++) {
3199 if (cfg->inputs[i].pin != nid)
3200 continue;
3201 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3202 return false;
3203 val = snd_hda_codec_get_pincfg(codec, nid);
3204 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3205 }
3206 return false;
3207}
3208
Takashi Iwaia90229e2013-01-18 14:10:00 +01003209/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003210static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3211 struct snd_ctl_elem_value *ucontrol)
3212{
3213 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3214 struct hda_gen_spec *spec = codec->spec;
3215 int ret;
3216
3217 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3218 if (ret < 0)
3219 return ret;
3220
Takashi Iwaia90229e2013-01-18 14:10:00 +01003221 if (spec->cap_sync_hook)
3222 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003223
3224 return ret;
3225}
3226
Takashi Iwai352f7f92012-12-19 12:52:06 +01003227static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3228 int idx, bool is_switch, unsigned int ctl,
3229 bool inv_dmic)
3230{
3231 struct hda_gen_spec *spec = codec->spec;
3232 char tmpname[44];
3233 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3234 const char *sfx = is_switch ? "Switch" : "Volume";
3235 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003236 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003237
3238 if (!ctl)
3239 return 0;
3240
3241 if (label)
3242 snprintf(tmpname, sizeof(tmpname),
3243 "%s Capture %s", label, sfx);
3244 else
3245 snprintf(tmpname, sizeof(tmpname),
3246 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003247 knew = add_control(spec, type, tmpname, idx,
3248 amp_val_replace_channels(ctl, chs));
3249 if (!knew)
3250 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003251 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003252 knew->put = cap_single_sw_put;
3253 if (!inv_dmic)
3254 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003255
3256 /* Make independent right kcontrol */
3257 if (label)
3258 snprintf(tmpname, sizeof(tmpname),
3259 "Inverted %s Capture %s", label, sfx);
3260 else
3261 snprintf(tmpname, sizeof(tmpname),
3262 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003263 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003264 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003265 if (!knew)
3266 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003267 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003268 knew->put = cap_single_sw_put;
3269 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003270}
3271
3272/* create single (and simple) capture volume and switch controls */
3273static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3274 unsigned int vol_ctl, unsigned int sw_ctl,
3275 bool inv_dmic)
3276{
3277 int err;
3278 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3279 if (err < 0)
3280 return err;
3281 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3282 if (err < 0)
3283 return err;
3284 return 0;
3285}
3286
3287/* create bound capture volume and switch controls */
3288static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3289 unsigned int vol_ctl, unsigned int sw_ctl)
3290{
3291 struct hda_gen_spec *spec = codec->spec;
3292 struct snd_kcontrol_new *knew;
3293
3294 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003295 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003296 if (!knew)
3297 return -ENOMEM;
3298 knew->index = idx;
3299 knew->private_value = vol_ctl;
3300 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3301 }
3302 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003303 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003304 if (!knew)
3305 return -ENOMEM;
3306 knew->index = idx;
3307 knew->private_value = sw_ctl;
3308 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3309 }
3310 return 0;
3311}
3312
3313/* return the vol ctl when used first in the imux list */
3314static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3315{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003316 struct nid_path *path;
3317 unsigned int ctl;
3318 int i;
3319
Takashi Iwaic697b712013-01-07 17:09:26 +01003320 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003321 if (!path)
3322 return 0;
3323 ctl = path->ctls[type];
3324 if (!ctl)
3325 return 0;
3326 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003327 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003328 if (path && path->ctls[type] == ctl)
3329 return 0;
3330 }
3331 return ctl;
3332}
3333
3334/* create individual capture volume and switch controls per input */
3335static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3336{
3337 struct hda_gen_spec *spec = codec->spec;
3338 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003339 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003340
3341 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003342 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003343 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003344
Takashi Iwaic9700422013-01-18 10:17:30 +01003345 idx = imux->items[i].index;
3346 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003347 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003348 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3349
3350 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003351 err = add_single_cap_ctl(codec,
3352 spec->input_labels[idx],
3353 spec->input_label_idxs[idx],
3354 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003355 get_first_cap_ctl(codec, i, type),
3356 inv_dmic);
3357 if (err < 0)
3358 return err;
3359 }
3360 }
3361 return 0;
3362}
3363
3364static int create_capture_mixers(struct hda_codec *codec)
3365{
3366 struct hda_gen_spec *spec = codec->spec;
3367 struct hda_input_mux *imux = &spec->input_mux;
3368 int i, n, nums, err;
3369
3370 if (spec->dyn_adc_switch)
3371 nums = 1;
3372 else
3373 nums = spec->num_adc_nids;
3374
3375 if (!spec->auto_mic && imux->num_items > 1) {
3376 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003377 const char *name;
3378 name = nums > 1 ? "Input Source" : "Capture Source";
3379 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003380 if (!knew)
3381 return -ENOMEM;
3382 knew->count = nums;
3383 }
3384
3385 for (n = 0; n < nums; n++) {
3386 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003387 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003388 bool inv_dmic = false;
3389 int vol, sw;
3390
3391 vol = sw = 0;
3392 for (i = 0; i < imux->num_items; i++) {
3393 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003394 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003395 if (!path)
3396 continue;
3397 parse_capvol_in_path(codec, path);
3398 if (!vol)
3399 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003400 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003401 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003402 if (!same_amp_caps(codec, vol,
3403 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3404 multi_cap_vol = true;
3405 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003406 if (!sw)
3407 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003408 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003409 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003410 if (!same_amp_caps(codec, sw,
3411 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3412 multi_cap_vol = true;
3413 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003414 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3415 inv_dmic = true;
3416 }
3417
3418 if (!multi)
3419 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3420 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003421 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003422 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3423 else
3424 err = create_multi_cap_vol_ctl(codec);
3425 if (err < 0)
3426 return err;
3427 }
3428
3429 return 0;
3430}
3431
3432/*
3433 * add mic boosts if needed
3434 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003435
3436/* check whether the given amp is feasible as a boost volume */
3437static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3438 int dir, int idx)
3439{
3440 unsigned int step;
3441
3442 if (!nid_has_volume(codec, nid, dir) ||
3443 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3444 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3445 return false;
3446
3447 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3448 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3449 if (step < 0x20)
3450 return false;
3451 return true;
3452}
3453
3454/* look for a boost amp in a widget close to the pin */
3455static unsigned int look_for_boost_amp(struct hda_codec *codec,
3456 struct nid_path *path)
3457{
3458 unsigned int val = 0;
3459 hda_nid_t nid;
3460 int depth;
3461
3462 for (depth = 0; depth < 3; depth++) {
3463 if (depth >= path->depth - 1)
3464 break;
3465 nid = path->path[depth];
3466 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3467 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3468 break;
3469 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3470 path->idx[depth])) {
3471 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3472 HDA_INPUT);
3473 break;
3474 }
3475 }
3476
3477 return val;
3478}
3479
Takashi Iwai352f7f92012-12-19 12:52:06 +01003480static int parse_mic_boost(struct hda_codec *codec)
3481{
3482 struct hda_gen_spec *spec = codec->spec;
3483 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003484 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003485 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003486
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003487 if (!spec->num_adc_nids)
3488 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003489
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003490 for (i = 0; i < imux->num_items; i++) {
3491 struct nid_path *path;
3492 unsigned int val;
3493 int idx;
3494 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003495
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003496 idx = imux->items[i].index;
3497 if (idx >= imux->num_items)
3498 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003499
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003500 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003501 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003502 continue;
3503
3504 path = get_input_path(codec, 0, i);
3505 if (!path)
3506 continue;
3507
3508 val = look_for_boost_amp(codec, path);
3509 if (!val)
3510 continue;
3511
3512 /* create a boost control */
3513 snprintf(boost_label, sizeof(boost_label),
3514 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003515 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3516 spec->input_label_idxs[idx], val))
3517 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003518
3519 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003520 }
3521 return 0;
3522}
3523
3524/*
3525 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3526 */
3527static void parse_digital(struct hda_codec *codec)
3528{
3529 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003530 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003531 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003532 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003533
3534 /* support multiple SPDIFs; the secondary is set up as a slave */
3535 nums = 0;
3536 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003537 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003538 dig_nid = look_for_dac(codec, pin, true);
3539 if (!dig_nid)
3540 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003541 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003542 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003543 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003544 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003545 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003546 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003547 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003548 if (!nums) {
3549 spec->multiout.dig_out_nid = dig_nid;
3550 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3551 } else {
3552 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3553 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3554 break;
3555 spec->slave_dig_outs[nums - 1] = dig_nid;
3556 }
3557 nums++;
3558 }
3559
3560 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003561 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003562 dig_nid = codec->start_nid;
3563 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003564 unsigned int wcaps = get_wcaps(codec, dig_nid);
3565 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3566 continue;
3567 if (!(wcaps & AC_WCAP_DIGITAL))
3568 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003569 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003570 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003571 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003572 path->active = true;
3573 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003574 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003575 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003576 break;
3577 }
3578 }
3579 }
3580}
3581
3582
3583/*
3584 * input MUX handling
3585 */
3586
3587static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3588
3589/* select the given imux item; either unmute exclusively or select the route */
3590static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3591 unsigned int idx)
3592{
3593 struct hda_gen_spec *spec = codec->spec;
3594 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003595 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003596
3597 imux = &spec->input_mux;
3598 if (!imux->num_items)
3599 return 0;
3600
3601 if (idx >= imux->num_items)
3602 idx = imux->num_items - 1;
3603 if (spec->cur_mux[adc_idx] == idx)
3604 return 0;
3605
Takashi Iwai55196ff2013-01-24 17:32:56 +01003606 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3607 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003608 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003609 if (old_path->active)
3610 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003611
3612 spec->cur_mux[adc_idx] = idx;
3613
Takashi Iwai967303d2013-02-19 17:12:42 +01003614 if (spec->hp_mic)
3615 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003616
3617 if (spec->dyn_adc_switch)
3618 dyn_adc_pcm_resetup(codec, idx);
3619
Takashi Iwaic697b712013-01-07 17:09:26 +01003620 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003621 if (!path)
3622 return 0;
3623 if (path->active)
3624 return 0;
3625 snd_hda_activate_path(codec, path, true, false);
3626 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003627 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003628 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003629 return 1;
3630}
3631
3632
3633/*
3634 * Jack detections for HP auto-mute and mic-switch
3635 */
3636
3637/* check each pin in the given array; returns true if any of them is plugged */
3638static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3639{
3640 int i, present = 0;
3641
3642 for (i = 0; i < num_pins; i++) {
3643 hda_nid_t nid = pins[i];
3644 if (!nid)
3645 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003646 /* don't detect pins retasked as inputs */
3647 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3648 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003649 present |= snd_hda_jack_detect(codec, nid);
3650 }
3651 return present;
3652}
3653
3654/* standard HP/line-out auto-mute helper */
3655static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003656 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003657{
3658 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003659 int i;
3660
3661 for (i = 0; i < num_pins; i++) {
3662 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003663 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003664 if (!nid)
3665 break;
Takashi Iwai967303d2013-02-19 17:12:42 +01003666 oldval = snd_hda_codec_get_pin_target(codec, nid);
3667 if (oldval & PIN_IN)
3668 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003669 /* don't reset VREF value in case it's controlling
3670 * the amp (see alc861_fixup_asus_amp_vref_0f())
3671 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003672 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003673 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003674 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003675 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003676 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003677 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003678 /* here we call update_pin_ctl() so that the pinctl is changed
3679 * without changing the pinctl target value;
3680 * the original target value will be still referred at the
3681 * init / resume again
3682 */
3683 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003684 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003685 }
3686}
3687
3688/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003689void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003690{
3691 struct hda_gen_spec *spec = codec->spec;
3692 int on;
3693
3694 /* Control HP pins/amps depending on master_mute state;
3695 * in general, HP pins/amps control should be enabled in all cases,
3696 * but currently set only for master_mute, just to be safe
3697 */
Takashi Iwai967303d2013-02-19 17:12:42 +01003698 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003699 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003700
3701 if (!spec->automute_speaker)
3702 on = 0;
3703 else
3704 on = spec->hp_jack_present | spec->line_jack_present;
3705 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003706 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003707 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003708 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003709
3710 /* toggle line-out mutes if needed, too */
3711 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3712 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3713 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3714 return;
3715 if (!spec->automute_lo)
3716 on = 0;
3717 else
3718 on = spec->hp_jack_present;
3719 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003720 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003721 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003722 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003723}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003724EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003725
3726static void call_update_outputs(struct hda_codec *codec)
3727{
3728 struct hda_gen_spec *spec = codec->spec;
3729 if (spec->automute_hook)
3730 spec->automute_hook(codec);
3731 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003732 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003733}
3734
3735/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003736void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003737{
3738 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003739 hda_nid_t *pins = spec->autocfg.hp_pins;
3740 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003741
Takashi Iwai92603c52013-01-22 07:46:31 +01003742 /* No detection for the first HP jack during indep-HP mode */
3743 if (spec->indep_hp_enabled) {
3744 pins++;
3745 num_pins--;
3746 }
3747
3748 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003749 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3750 return;
3751 call_update_outputs(codec);
3752}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003753EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003754
3755/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003756void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003757{
3758 struct hda_gen_spec *spec = codec->spec;
3759
3760 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3761 return;
3762 /* check LO jack only when it's different from HP */
3763 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3764 return;
3765
3766 spec->line_jack_present =
3767 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3768 spec->autocfg.line_out_pins);
3769 if (!spec->automute_speaker || !spec->detect_lo)
3770 return;
3771 call_update_outputs(codec);
3772}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003773EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003774
3775/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003776void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003777{
3778 struct hda_gen_spec *spec = codec->spec;
3779 int i;
3780
3781 if (!spec->auto_mic)
3782 return;
3783
3784 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003785 hda_nid_t pin = spec->am_entry[i].pin;
3786 /* don't detect pins retasked as outputs */
3787 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3788 continue;
3789 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003790 mux_select(codec, 0, spec->am_entry[i].idx);
3791 return;
3792 }
3793 }
3794 mux_select(codec, 0, spec->am_entry[0].idx);
3795}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003796EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003797
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003798/* update jack retasking */
3799static void update_automute_all(struct hda_codec *codec)
3800{
3801 struct hda_gen_spec *spec = codec->spec;
3802
3803 if (spec->hp_automute_hook)
3804 spec->hp_automute_hook(codec, NULL);
3805 else
3806 snd_hda_gen_hp_automute(codec, NULL);
3807 if (spec->line_automute_hook)
3808 spec->line_automute_hook(codec, NULL);
3809 else
3810 snd_hda_gen_line_automute(codec, NULL);
3811 if (spec->mic_autoswitch_hook)
3812 spec->mic_autoswitch_hook(codec, NULL);
3813 else
3814 snd_hda_gen_mic_autoswitch(codec, NULL);
3815}
3816
Takashi Iwai352f7f92012-12-19 12:52:06 +01003817/*
3818 * Auto-Mute mode mixer enum support
3819 */
3820static int automute_mode_info(struct snd_kcontrol *kcontrol,
3821 struct snd_ctl_elem_info *uinfo)
3822{
3823 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3824 struct hda_gen_spec *spec = codec->spec;
3825 static const char * const texts3[] = {
3826 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003827 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
Takashi Iwai352f7f92012-12-19 12:52:06 +01003829 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3830 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3831 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3832}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
Takashi Iwai352f7f92012-12-19 12:52:06 +01003834static int automute_mode_get(struct snd_kcontrol *kcontrol,
3835 struct snd_ctl_elem_value *ucontrol)
3836{
3837 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3838 struct hda_gen_spec *spec = codec->spec;
3839 unsigned int val = 0;
3840 if (spec->automute_speaker)
3841 val++;
3842 if (spec->automute_lo)
3843 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003844
Takashi Iwai352f7f92012-12-19 12:52:06 +01003845 ucontrol->value.enumerated.item[0] = val;
3846 return 0;
3847}
3848
3849static int automute_mode_put(struct snd_kcontrol *kcontrol,
3850 struct snd_ctl_elem_value *ucontrol)
3851{
3852 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3853 struct hda_gen_spec *spec = codec->spec;
3854
3855 switch (ucontrol->value.enumerated.item[0]) {
3856 case 0:
3857 if (!spec->automute_speaker && !spec->automute_lo)
3858 return 0;
3859 spec->automute_speaker = 0;
3860 spec->automute_lo = 0;
3861 break;
3862 case 1:
3863 if (spec->automute_speaker_possible) {
3864 if (!spec->automute_lo && spec->automute_speaker)
3865 return 0;
3866 spec->automute_speaker = 1;
3867 spec->automute_lo = 0;
3868 } else if (spec->automute_lo_possible) {
3869 if (spec->automute_lo)
3870 return 0;
3871 spec->automute_lo = 1;
3872 } else
3873 return -EINVAL;
3874 break;
3875 case 2:
3876 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3877 return -EINVAL;
3878 if (spec->automute_speaker && spec->automute_lo)
3879 return 0;
3880 spec->automute_speaker = 1;
3881 spec->automute_lo = 1;
3882 break;
3883 default:
3884 return -EINVAL;
3885 }
3886 call_update_outputs(codec);
3887 return 1;
3888}
3889
3890static const struct snd_kcontrol_new automute_mode_enum = {
3891 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3892 .name = "Auto-Mute Mode",
3893 .info = automute_mode_info,
3894 .get = automute_mode_get,
3895 .put = automute_mode_put,
3896};
3897
3898static int add_automute_mode_enum(struct hda_codec *codec)
3899{
3900 struct hda_gen_spec *spec = codec->spec;
3901
Takashi Iwai12c93df2012-12-19 14:38:33 +01003902 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003903 return -ENOMEM;
3904 return 0;
3905}
3906
3907/*
3908 * Check the availability of HP/line-out auto-mute;
3909 * Set up appropriately if really supported
3910 */
3911static int check_auto_mute_availability(struct hda_codec *codec)
3912{
3913 struct hda_gen_spec *spec = codec->spec;
3914 struct auto_pin_cfg *cfg = &spec->autocfg;
3915 int present = 0;
3916 int i, err;
3917
Takashi Iwaif72706b2013-01-16 18:20:07 +01003918 if (spec->suppress_auto_mute)
3919 return 0;
3920
Takashi Iwai352f7f92012-12-19 12:52:06 +01003921 if (cfg->hp_pins[0])
3922 present++;
3923 if (cfg->line_out_pins[0])
3924 present++;
3925 if (cfg->speaker_pins[0])
3926 present++;
3927 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003928 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003929
3930 if (!cfg->speaker_pins[0] &&
3931 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3932 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3933 sizeof(cfg->speaker_pins));
3934 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Takashi Iwai352f7f92012-12-19 12:52:06 +01003937 if (!cfg->hp_pins[0] &&
3938 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3939 memcpy(cfg->hp_pins, cfg->line_out_pins,
3940 sizeof(cfg->hp_pins));
3941 cfg->hp_outs = cfg->line_outs;
3942 }
3943
3944 for (i = 0; i < cfg->hp_outs; i++) {
3945 hda_nid_t nid = cfg->hp_pins[i];
3946 if (!is_jack_detectable(codec, nid))
3947 continue;
3948 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3949 nid);
3950 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003951 spec->hp_automute_hook ?
3952 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003953 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003954 spec->detect_hp = 1;
3955 }
3956
3957 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3958 if (cfg->speaker_outs)
3959 for (i = 0; i < cfg->line_outs; i++) {
3960 hda_nid_t nid = cfg->line_out_pins[i];
3961 if (!is_jack_detectable(codec, nid))
3962 continue;
3963 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3964 snd_hda_jack_detect_enable_callback(codec, nid,
3965 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003966 spec->line_automute_hook ?
3967 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003968 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003969 spec->detect_lo = 1;
3970 }
3971 spec->automute_lo_possible = spec->detect_hp;
3972 }
3973
3974 spec->automute_speaker_possible = cfg->speaker_outs &&
3975 (spec->detect_hp || spec->detect_lo);
3976
3977 spec->automute_lo = spec->automute_lo_possible;
3978 spec->automute_speaker = spec->automute_speaker_possible;
3979
3980 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3981 /* create a control for automute mode */
3982 err = add_automute_mode_enum(codec);
3983 if (err < 0)
3984 return err;
3985 }
3986 return 0;
3987}
3988
Takashi Iwai352f7f92012-12-19 12:52:06 +01003989/* check whether all auto-mic pins are valid; setup indices if OK */
3990static bool auto_mic_check_imux(struct hda_codec *codec)
3991{
3992 struct hda_gen_spec *spec = codec->spec;
3993 const struct hda_input_mux *imux;
3994 int i;
3995
3996 imux = &spec->input_mux;
3997 for (i = 0; i < spec->am_num_entries; i++) {
3998 spec->am_entry[i].idx =
3999 find_idx_in_nid_list(spec->am_entry[i].pin,
4000 spec->imux_pins, imux->num_items);
4001 if (spec->am_entry[i].idx < 0)
4002 return false; /* no corresponding imux */
4003 }
4004
4005 /* we don't need the jack detection for the first pin */
4006 for (i = 1; i < spec->am_num_entries; i++)
4007 snd_hda_jack_detect_enable_callback(codec,
4008 spec->am_entry[i].pin,
4009 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004010 spec->mic_autoswitch_hook ?
4011 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004012 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004013 return true;
4014}
4015
4016static int compare_attr(const void *ap, const void *bp)
4017{
4018 const struct automic_entry *a = ap;
4019 const struct automic_entry *b = bp;
4020 return (int)(a->attr - b->attr);
4021}
4022
4023/*
4024 * Check the availability of auto-mic switch;
4025 * Set up if really supported
4026 */
4027static int check_auto_mic_availability(struct hda_codec *codec)
4028{
4029 struct hda_gen_spec *spec = codec->spec;
4030 struct auto_pin_cfg *cfg = &spec->autocfg;
4031 unsigned int types;
4032 int i, num_pins;
4033
Takashi Iwaid12daf62013-01-07 16:32:11 +01004034 if (spec->suppress_auto_mic)
4035 return 0;
4036
Takashi Iwai352f7f92012-12-19 12:52:06 +01004037 types = 0;
4038 num_pins = 0;
4039 for (i = 0; i < cfg->num_inputs; i++) {
4040 hda_nid_t nid = cfg->inputs[i].pin;
4041 unsigned int attr;
4042 attr = snd_hda_codec_get_pincfg(codec, nid);
4043 attr = snd_hda_get_input_pin_attr(attr);
4044 if (types & (1 << attr))
4045 return 0; /* already occupied */
4046 switch (attr) {
4047 case INPUT_PIN_ATTR_INT:
4048 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4049 return 0; /* invalid type */
4050 break;
4051 case INPUT_PIN_ATTR_UNUSED:
4052 return 0; /* invalid entry */
4053 default:
4054 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4055 return 0; /* invalid type */
4056 if (!spec->line_in_auto_switch &&
4057 cfg->inputs[i].type != AUTO_PIN_MIC)
4058 return 0; /* only mic is allowed */
4059 if (!is_jack_detectable(codec, nid))
4060 return 0; /* no unsol support */
4061 break;
4062 }
4063 if (num_pins >= MAX_AUTO_MIC_PINS)
4064 return 0;
4065 types |= (1 << attr);
4066 spec->am_entry[num_pins].pin = nid;
4067 spec->am_entry[num_pins].attr = attr;
4068 num_pins++;
4069 }
4070
4071 if (num_pins < 2)
4072 return 0;
4073
4074 spec->am_num_entries = num_pins;
4075 /* sort the am_entry in the order of attr so that the pin with a
4076 * higher attr will be selected when the jack is plugged.
4077 */
4078 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4079 compare_attr, NULL);
4080
4081 if (!auto_mic_check_imux(codec))
4082 return 0;
4083
4084 spec->auto_mic = 1;
4085 spec->num_adc_nids = 1;
4086 spec->cur_mux[0] = spec->am_entry[0].idx;
4087 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4088 spec->am_entry[0].pin,
4089 spec->am_entry[1].pin,
4090 spec->am_entry[2].pin);
4091
4092 return 0;
4093}
4094
Takashi Iwai55196ff2013-01-24 17:32:56 +01004095/* power_filter hook; make inactive widgets into power down */
4096static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4097 hda_nid_t nid,
4098 unsigned int power_state)
4099{
4100 if (power_state != AC_PWRST_D0)
4101 return power_state;
4102 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4103 return power_state;
4104 if (is_active_nid(codec, nid, HDA_OUTPUT, 0))
4105 return power_state;
4106 return AC_PWRST_D3;
4107}
4108
Takashi Iwai352f7f92012-12-19 12:52:06 +01004109
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004110/*
4111 * Parse the given BIOS configuration and set up the hda_gen_spec
4112 *
4113 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004114 * or a negative error code
4115 */
4116int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004117 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004118{
4119 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004120 int err;
4121
Takashi Iwai1c70a582013-01-11 17:48:22 +01004122 parse_user_hints(codec);
4123
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004124 if (spec->mixer_nid && !spec->mixer_merge_nid)
4125 spec->mixer_merge_nid = spec->mixer_nid;
4126
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004127 if (cfg != &spec->autocfg) {
4128 spec->autocfg = *cfg;
4129 cfg = &spec->autocfg;
4130 }
4131
David Henningsson6fc4cb92013-01-16 15:58:45 +01004132 fill_all_dac_nids(codec);
4133
Takashi Iwai352f7f92012-12-19 12:52:06 +01004134 if (!cfg->line_outs) {
4135 if (cfg->dig_outs || cfg->dig_in_pin) {
4136 spec->multiout.max_channels = 2;
4137 spec->no_analog = 1;
4138 goto dig_only;
4139 }
4140 return 0; /* can't find valid BIOS pin config */
4141 }
4142
4143 if (!spec->no_primary_hp &&
4144 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4145 cfg->line_outs <= cfg->hp_outs) {
4146 /* use HP as primary out */
4147 cfg->speaker_outs = cfg->line_outs;
4148 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4149 sizeof(cfg->speaker_pins));
4150 cfg->line_outs = cfg->hp_outs;
4151 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4152 cfg->hp_outs = 0;
4153 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4154 cfg->line_out_type = AUTO_PIN_HP_OUT;
4155 }
4156
4157 err = parse_output_paths(codec);
4158 if (err < 0)
4159 return err;
4160 err = create_multi_channel_mode(codec);
4161 if (err < 0)
4162 return err;
4163 err = create_multi_out_ctls(codec, cfg);
4164 if (err < 0)
4165 return err;
4166 err = create_hp_out_ctls(codec);
4167 if (err < 0)
4168 return err;
4169 err = create_speaker_out_ctls(codec);
4170 if (err < 0)
4171 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004172 err = create_indep_hp_ctls(codec);
4173 if (err < 0)
4174 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004175 err = create_loopback_mixing_ctl(codec);
4176 if (err < 0)
4177 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004178 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004179 if (err < 0)
4180 return err;
4181 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004182 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004183 return err;
4184
Takashi Iwaia07a9492013-01-07 16:44:06 +01004185 spec->const_channel_count = spec->ext_channel_count;
4186 /* check the multiple speaker and headphone pins */
4187 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4188 spec->const_channel_count = max(spec->const_channel_count,
4189 cfg->speaker_outs * 2);
4190 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4191 spec->const_channel_count = max(spec->const_channel_count,
4192 cfg->hp_outs * 2);
4193 spec->multiout.max_channels = max(spec->ext_channel_count,
4194 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004195
4196 err = check_auto_mute_availability(codec);
4197 if (err < 0)
4198 return err;
4199
4200 err = check_dyn_adc_switch(codec);
4201 if (err < 0)
4202 return err;
4203
Takashi Iwai967303d2013-02-19 17:12:42 +01004204 err = check_auto_mic_availability(codec);
4205 if (err < 0)
4206 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004207
Takashi Iwai352f7f92012-12-19 12:52:06 +01004208 err = create_capture_mixers(codec);
4209 if (err < 0)
4210 return err;
4211
4212 err = parse_mic_boost(codec);
4213 if (err < 0)
4214 return err;
4215
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004216 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004217 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4218 err = create_out_jack_modes(codec, cfg->line_outs,
4219 cfg->line_out_pins);
4220 if (err < 0)
4221 return err;
4222 }
4223 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4224 err = create_out_jack_modes(codec, cfg->hp_outs,
4225 cfg->hp_pins);
4226 if (err < 0)
4227 return err;
4228 }
4229 }
4230
Takashi Iwai352f7f92012-12-19 12:52:06 +01004231 dig_only:
4232 parse_digital(codec);
4233
Takashi Iwai55196ff2013-01-24 17:32:56 +01004234 if (spec->power_down_unused)
4235 codec->power_filter = snd_hda_gen_path_power_filter;
4236
Takashi Iwai352f7f92012-12-19 12:52:06 +01004237 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004239EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
4241
4242/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004243 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004245
4246/* slave controls for virtual master */
4247static const char * const slave_pfxs[] = {
4248 "Front", "Surround", "Center", "LFE", "Side",
4249 "Headphone", "Speaker", "Mono", "Line Out",
4250 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004251 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4252 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4253 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004254 NULL,
4255};
4256
4257int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004259 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Takashi Iwai36502d02012-12-19 15:15:10 +01004262 if (spec->kctls.used) {
4263 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4264 if (err < 0)
4265 return err;
4266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
Takashi Iwai352f7f92012-12-19 12:52:06 +01004268 if (spec->multiout.dig_out_nid) {
4269 err = snd_hda_create_dig_out_ctls(codec,
4270 spec->multiout.dig_out_nid,
4271 spec->multiout.dig_out_nid,
4272 spec->pcm_rec[1].pcm_type);
4273 if (err < 0)
4274 return err;
4275 if (!spec->no_analog) {
4276 err = snd_hda_create_spdif_share_sw(codec,
4277 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 if (err < 0)
4279 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004280 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 }
4282 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004283 if (spec->dig_in_nid) {
4284 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4285 if (err < 0)
4286 return err;
4287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
Takashi Iwai352f7f92012-12-19 12:52:06 +01004289 /* if we have no master control, let's create it */
4290 if (!spec->no_analog &&
4291 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004292 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004293 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004294 "Playback Volume");
4295 if (err < 0)
4296 return err;
4297 }
4298 if (!spec->no_analog &&
4299 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4300 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4301 NULL, slave_pfxs,
4302 "Playback Switch",
4303 true, &spec->vmaster_mute.sw_kctl);
4304 if (err < 0)
4305 return err;
4306 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004307 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4308 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
Takashi Iwai352f7f92012-12-19 12:52:06 +01004311 free_kctls(spec); /* no longer needed */
4312
Takashi Iwai967303d2013-02-19 17:12:42 +01004313 if (spec->hp_mic_pin) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004314 int err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004315 int nid = spec->hp_mic_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004316 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4317 if (err < 0)
4318 return err;
4319 err = snd_hda_jack_detect_enable(codec, nid, 0);
4320 if (err < 0)
4321 return err;
4322 }
4323
4324 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4325 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 return err;
4327
4328 return 0;
4329}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004330EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4331
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
4333/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004334 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004337static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4338 struct hda_codec *codec,
4339 struct snd_pcm_substream *substream,
4340 int action)
4341{
4342 struct hda_gen_spec *spec = codec->spec;
4343 if (spec->pcm_playback_hook)
4344 spec->pcm_playback_hook(hinfo, codec, substream, action);
4345}
4346
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004347static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4348 struct hda_codec *codec,
4349 struct snd_pcm_substream *substream,
4350 int action)
4351{
4352 struct hda_gen_spec *spec = codec->spec;
4353 if (spec->pcm_capture_hook)
4354 spec->pcm_capture_hook(hinfo, codec, substream, action);
4355}
4356
Takashi Iwai352f7f92012-12-19 12:52:06 +01004357/*
4358 * Analog playback callbacks
4359 */
4360static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4361 struct hda_codec *codec,
4362 struct snd_pcm_substream *substream)
4363{
4364 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004365 int err;
4366
4367 mutex_lock(&spec->pcm_mutex);
4368 err = snd_hda_multi_out_analog_open(codec,
4369 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004370 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004371 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004372 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004373 call_pcm_playback_hook(hinfo, codec, substream,
4374 HDA_GEN_PCM_ACT_OPEN);
4375 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004376 mutex_unlock(&spec->pcm_mutex);
4377 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004378}
4379
4380static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004381 struct hda_codec *codec,
4382 unsigned int stream_tag,
4383 unsigned int format,
4384 struct snd_pcm_substream *substream)
4385{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004386 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004387 int err;
4388
4389 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4390 stream_tag, format, substream);
4391 if (!err)
4392 call_pcm_playback_hook(hinfo, codec, substream,
4393 HDA_GEN_PCM_ACT_PREPARE);
4394 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004395}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004396
Takashi Iwai352f7f92012-12-19 12:52:06 +01004397static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4398 struct hda_codec *codec,
4399 struct snd_pcm_substream *substream)
4400{
4401 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004402 int err;
4403
4404 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4405 if (!err)
4406 call_pcm_playback_hook(hinfo, codec, substream,
4407 HDA_GEN_PCM_ACT_CLEANUP);
4408 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004409}
4410
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004411static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4412 struct hda_codec *codec,
4413 struct snd_pcm_substream *substream)
4414{
4415 struct hda_gen_spec *spec = codec->spec;
4416 mutex_lock(&spec->pcm_mutex);
4417 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004418 call_pcm_playback_hook(hinfo, codec, substream,
4419 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004420 mutex_unlock(&spec->pcm_mutex);
4421 return 0;
4422}
4423
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004424static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4425 struct hda_codec *codec,
4426 struct snd_pcm_substream *substream)
4427{
4428 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4429 return 0;
4430}
4431
4432static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4433 struct hda_codec *codec,
4434 unsigned int stream_tag,
4435 unsigned int format,
4436 struct snd_pcm_substream *substream)
4437{
4438 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4439 call_pcm_capture_hook(hinfo, codec, substream,
4440 HDA_GEN_PCM_ACT_PREPARE);
4441 return 0;
4442}
4443
4444static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4445 struct hda_codec *codec,
4446 struct snd_pcm_substream *substream)
4447{
4448 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4449 call_pcm_capture_hook(hinfo, codec, substream,
4450 HDA_GEN_PCM_ACT_CLEANUP);
4451 return 0;
4452}
4453
4454static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4455 struct hda_codec *codec,
4456 struct snd_pcm_substream *substream)
4457{
4458 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4459 return 0;
4460}
4461
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004462static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4463 struct hda_codec *codec,
4464 struct snd_pcm_substream *substream)
4465{
4466 struct hda_gen_spec *spec = codec->spec;
4467 int err = 0;
4468
4469 mutex_lock(&spec->pcm_mutex);
4470 if (!spec->indep_hp_enabled)
4471 err = -EBUSY;
4472 else
4473 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004474 call_pcm_playback_hook(hinfo, codec, substream,
4475 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004476 mutex_unlock(&spec->pcm_mutex);
4477 return err;
4478}
4479
4480static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4481 struct hda_codec *codec,
4482 struct snd_pcm_substream *substream)
4483{
4484 struct hda_gen_spec *spec = codec->spec;
4485 mutex_lock(&spec->pcm_mutex);
4486 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004487 call_pcm_playback_hook(hinfo, codec, substream,
4488 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004489 mutex_unlock(&spec->pcm_mutex);
4490 return 0;
4491}
4492
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004493static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4494 struct hda_codec *codec,
4495 unsigned int stream_tag,
4496 unsigned int format,
4497 struct snd_pcm_substream *substream)
4498{
4499 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4500 call_pcm_playback_hook(hinfo, codec, substream,
4501 HDA_GEN_PCM_ACT_PREPARE);
4502 return 0;
4503}
4504
4505static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4506 struct hda_codec *codec,
4507 struct snd_pcm_substream *substream)
4508{
4509 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4510 call_pcm_playback_hook(hinfo, codec, substream,
4511 HDA_GEN_PCM_ACT_CLEANUP);
4512 return 0;
4513}
4514
Takashi Iwai352f7f92012-12-19 12:52:06 +01004515/*
4516 * Digital out
4517 */
4518static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4519 struct hda_codec *codec,
4520 struct snd_pcm_substream *substream)
4521{
4522 struct hda_gen_spec *spec = codec->spec;
4523 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4524}
4525
4526static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4527 struct hda_codec *codec,
4528 unsigned int stream_tag,
4529 unsigned int format,
4530 struct snd_pcm_substream *substream)
4531{
4532 struct hda_gen_spec *spec = codec->spec;
4533 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4534 stream_tag, format, substream);
4535}
4536
4537static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4538 struct hda_codec *codec,
4539 struct snd_pcm_substream *substream)
4540{
4541 struct hda_gen_spec *spec = codec->spec;
4542 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4543}
4544
4545static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4546 struct hda_codec *codec,
4547 struct snd_pcm_substream *substream)
4548{
4549 struct hda_gen_spec *spec = codec->spec;
4550 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4551}
4552
4553/*
4554 * Analog capture
4555 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004556#define alt_capture_pcm_open capture_pcm_open
4557#define alt_capture_pcm_close capture_pcm_close
4558
Takashi Iwai352f7f92012-12-19 12:52:06 +01004559static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4560 struct hda_codec *codec,
4561 unsigned int stream_tag,
4562 unsigned int format,
4563 struct snd_pcm_substream *substream)
4564{
4565 struct hda_gen_spec *spec = codec->spec;
4566
4567 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004568 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004569 call_pcm_capture_hook(hinfo, codec, substream,
4570 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004571 return 0;
4572}
4573
Takashi Iwai352f7f92012-12-19 12:52:06 +01004574static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4575 struct hda_codec *codec,
4576 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004577{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004578 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004579
Takashi Iwai352f7f92012-12-19 12:52:06 +01004580 snd_hda_codec_cleanup_stream(codec,
4581 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004582 call_pcm_capture_hook(hinfo, codec, substream,
4583 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004584 return 0;
4585}
4586
Takashi Iwai352f7f92012-12-19 12:52:06 +01004587/*
4588 */
4589static const struct hda_pcm_stream pcm_analog_playback = {
4590 .substreams = 1,
4591 .channels_min = 2,
4592 .channels_max = 8,
4593 /* NID is set in build_pcms */
4594 .ops = {
4595 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004596 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004597 .prepare = playback_pcm_prepare,
4598 .cleanup = playback_pcm_cleanup
4599 },
4600};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
Takashi Iwai352f7f92012-12-19 12:52:06 +01004602static const struct hda_pcm_stream pcm_analog_capture = {
4603 .substreams = 1,
4604 .channels_min = 2,
4605 .channels_max = 2,
4606 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004607 .ops = {
4608 .open = capture_pcm_open,
4609 .close = capture_pcm_close,
4610 .prepare = capture_pcm_prepare,
4611 .cleanup = capture_pcm_cleanup
4612 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004613};
4614
4615static const struct hda_pcm_stream pcm_analog_alt_playback = {
4616 .substreams = 1,
4617 .channels_min = 2,
4618 .channels_max = 2,
4619 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004620 .ops = {
4621 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004622 .close = alt_playback_pcm_close,
4623 .prepare = alt_playback_pcm_prepare,
4624 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004625 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004626};
4627
4628static const struct hda_pcm_stream pcm_analog_alt_capture = {
4629 .substreams = 2, /* can be overridden */
4630 .channels_min = 2,
4631 .channels_max = 2,
4632 /* NID is set in build_pcms */
4633 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004634 .open = alt_capture_pcm_open,
4635 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004636 .prepare = alt_capture_pcm_prepare,
4637 .cleanup = alt_capture_pcm_cleanup
4638 },
4639};
4640
4641static const struct hda_pcm_stream pcm_digital_playback = {
4642 .substreams = 1,
4643 .channels_min = 2,
4644 .channels_max = 2,
4645 /* NID is set in build_pcms */
4646 .ops = {
4647 .open = dig_playback_pcm_open,
4648 .close = dig_playback_pcm_close,
4649 .prepare = dig_playback_pcm_prepare,
4650 .cleanup = dig_playback_pcm_cleanup
4651 },
4652};
4653
4654static const struct hda_pcm_stream pcm_digital_capture = {
4655 .substreams = 1,
4656 .channels_min = 2,
4657 .channels_max = 2,
4658 /* NID is set in build_pcms */
4659};
4660
4661/* Used by build_pcms to flag that a PCM has no playback stream */
4662static const struct hda_pcm_stream pcm_null_stream = {
4663 .substreams = 0,
4664 .channels_min = 0,
4665 .channels_max = 0,
4666};
4667
4668/*
4669 * dynamic changing ADC PCM streams
4670 */
4671static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4672{
4673 struct hda_gen_spec *spec = codec->spec;
4674 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4675
4676 if (spec->cur_adc && spec->cur_adc != new_adc) {
4677 /* stream is running, let's swap the current ADC */
4678 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4679 spec->cur_adc = new_adc;
4680 snd_hda_codec_setup_stream(codec, new_adc,
4681 spec->cur_adc_stream_tag, 0,
4682 spec->cur_adc_format);
4683 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004685 return false;
4686}
4687
4688/* analog capture with dynamic dual-adc changes */
4689static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4690 struct hda_codec *codec,
4691 unsigned int stream_tag,
4692 unsigned int format,
4693 struct snd_pcm_substream *substream)
4694{
4695 struct hda_gen_spec *spec = codec->spec;
4696 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4697 spec->cur_adc_stream_tag = stream_tag;
4698 spec->cur_adc_format = format;
4699 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4700 return 0;
4701}
4702
4703static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4704 struct hda_codec *codec,
4705 struct snd_pcm_substream *substream)
4706{
4707 struct hda_gen_spec *spec = codec->spec;
4708 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4709 spec->cur_adc = 0;
4710 return 0;
4711}
4712
4713static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4714 .substreams = 1,
4715 .channels_min = 2,
4716 .channels_max = 2,
4717 .nid = 0, /* fill later */
4718 .ops = {
4719 .prepare = dyn_adc_capture_pcm_prepare,
4720 .cleanup = dyn_adc_capture_pcm_cleanup
4721 },
4722};
4723
Takashi Iwaif873e532012-12-20 16:58:39 +01004724static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4725 const char *chip_name)
4726{
4727 char *p;
4728
4729 if (*str)
4730 return;
4731 strlcpy(str, chip_name, len);
4732
4733 /* drop non-alnum chars after a space */
4734 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4735 if (!isalnum(p[1])) {
4736 *p = 0;
4737 break;
4738 }
4739 }
4740 strlcat(str, sfx, len);
4741}
4742
Takashi Iwai352f7f92012-12-19 12:52:06 +01004743/* build PCM streams based on the parsed results */
4744int snd_hda_gen_build_pcms(struct hda_codec *codec)
4745{
4746 struct hda_gen_spec *spec = codec->spec;
4747 struct hda_pcm *info = spec->pcm_rec;
4748 const struct hda_pcm_stream *p;
4749 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
4751 codec->num_pcms = 1;
4752 codec->pcm_info = info;
4753
Takashi Iwai352f7f92012-12-19 12:52:06 +01004754 if (spec->no_analog)
4755 goto skip_analog;
4756
Takashi Iwaif873e532012-12-20 16:58:39 +01004757 fill_pcm_stream_name(spec->stream_name_analog,
4758 sizeof(spec->stream_name_analog),
4759 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004760 info->name = spec->stream_name_analog;
4761
4762 if (spec->multiout.num_dacs > 0) {
4763 p = spec->stream_analog_playback;
4764 if (!p)
4765 p = &pcm_analog_playback;
4766 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4767 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4768 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4769 spec->multiout.max_channels;
4770 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4771 spec->autocfg.line_outs == 2)
4772 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4773 snd_pcm_2_1_chmaps;
4774 }
4775 if (spec->num_adc_nids) {
4776 p = spec->stream_analog_capture;
4777 if (!p) {
4778 if (spec->dyn_adc_switch)
4779 p = &dyn_adc_pcm_analog_capture;
4780 else
4781 p = &pcm_analog_capture;
4782 }
4783 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4784 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4785 }
4786
Takashi Iwai352f7f92012-12-19 12:52:06 +01004787 skip_analog:
4788 /* SPDIF for stream index #1 */
4789 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004790 fill_pcm_stream_name(spec->stream_name_digital,
4791 sizeof(spec->stream_name_digital),
4792 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004793 codec->num_pcms = 2;
4794 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4795 info = spec->pcm_rec + 1;
4796 info->name = spec->stream_name_digital;
4797 if (spec->dig_out_type)
4798 info->pcm_type = spec->dig_out_type;
4799 else
4800 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4801 if (spec->multiout.dig_out_nid) {
4802 p = spec->stream_digital_playback;
4803 if (!p)
4804 p = &pcm_digital_playback;
4805 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4806 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4807 }
4808 if (spec->dig_in_nid) {
4809 p = spec->stream_digital_capture;
4810 if (!p)
4811 p = &pcm_digital_capture;
4812 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4813 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4814 }
4815 }
4816
4817 if (spec->no_analog)
4818 return 0;
4819
4820 /* If the use of more than one ADC is requested for the current
4821 * model, configure a second analog capture-only PCM.
4822 */
4823 have_multi_adcs = (spec->num_adc_nids > 1) &&
4824 !spec->dyn_adc_switch && !spec->auto_mic;
4825 /* Additional Analaog capture for index #2 */
4826 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004827 fill_pcm_stream_name(spec->stream_name_alt_analog,
4828 sizeof(spec->stream_name_alt_analog),
4829 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004830 codec->num_pcms = 3;
4831 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004832 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004833 if (spec->alt_dac_nid) {
4834 p = spec->stream_analog_alt_playback;
4835 if (!p)
4836 p = &pcm_analog_alt_playback;
4837 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4838 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4839 spec->alt_dac_nid;
4840 } else {
4841 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4842 pcm_null_stream;
4843 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4844 }
4845 if (have_multi_adcs) {
4846 p = spec->stream_analog_alt_capture;
4847 if (!p)
4848 p = &pcm_analog_alt_capture;
4849 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4850 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4851 spec->adc_nids[1];
4852 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4853 spec->num_adc_nids - 1;
4854 } else {
4855 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4856 pcm_null_stream;
4857 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 }
4860
4861 return 0;
4862}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004863EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4864
4865
4866/*
4867 * Standard auto-parser initializations
4868 */
4869
Takashi Iwaid4156932013-01-07 10:08:02 +01004870/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004871static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004872{
4873 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004874 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004875
Takashi Iwai196c17662013-01-04 15:01:40 +01004876 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004877 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004878 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004879 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004880 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004881 snd_hda_activate_path(codec, path, path->active, true);
4882 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004883}
4884
4885/* initialize primary output paths */
4886static void init_multi_out(struct hda_codec *codec)
4887{
4888 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004889 int i;
4890
Takashi Iwaid4156932013-01-07 10:08:02 +01004891 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004892 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004893}
4894
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004895
Takashi Iwai2c12c302013-01-10 09:33:29 +01004896static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004897{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004898 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004899
Takashi Iwaid4156932013-01-07 10:08:02 +01004900 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004901 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004902}
4903
4904/* initialize hp and speaker paths */
4905static void init_extra_out(struct hda_codec *codec)
4906{
4907 struct hda_gen_spec *spec = codec->spec;
4908
4909 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004910 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004911 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4912 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004913 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004914}
4915
4916/* initialize multi-io paths */
4917static void init_multi_io(struct hda_codec *codec)
4918{
4919 struct hda_gen_spec *spec = codec->spec;
4920 int i;
4921
4922 for (i = 0; i < spec->multi_ios; i++) {
4923 hda_nid_t pin = spec->multi_io[i].pin;
4924 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004925 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004926 if (!path)
4927 continue;
4928 if (!spec->multi_io[i].ctl_in)
4929 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004930 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004931 snd_hda_activate_path(codec, path, path->active, true);
4932 }
4933}
4934
Takashi Iwai352f7f92012-12-19 12:52:06 +01004935/* set up input pins and loopback paths */
4936static void init_analog_input(struct hda_codec *codec)
4937{
4938 struct hda_gen_spec *spec = codec->spec;
4939 struct auto_pin_cfg *cfg = &spec->autocfg;
4940 int i;
4941
4942 for (i = 0; i < cfg->num_inputs; i++) {
4943 hda_nid_t nid = cfg->inputs[i].pin;
4944 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004945 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004946
4947 /* init loopback inputs */
4948 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01004949 resume_path_from_idx(codec, spec->loopback_paths[i]);
4950 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004951 }
4952 }
4953}
4954
4955/* initialize ADC paths */
4956static void init_input_src(struct hda_codec *codec)
4957{
4958 struct hda_gen_spec *spec = codec->spec;
4959 struct hda_input_mux *imux = &spec->input_mux;
4960 struct nid_path *path;
4961 int i, c, nums;
4962
4963 if (spec->dyn_adc_switch)
4964 nums = 1;
4965 else
4966 nums = spec->num_adc_nids;
4967
4968 for (c = 0; c < nums; c++) {
4969 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004970 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004971 if (path) {
4972 bool active = path->active;
4973 if (i == spec->cur_mux[c])
4974 active = true;
4975 snd_hda_activate_path(codec, path, active, false);
4976 }
4977 }
Takashi Iwai967303d2013-02-19 17:12:42 +01004978 if (spec->hp_mic)
4979 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004980 }
4981
Takashi Iwai352f7f92012-12-19 12:52:06 +01004982 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004983 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004984}
4985
4986/* set right pin controls for digital I/O */
4987static void init_digital(struct hda_codec *codec)
4988{
4989 struct hda_gen_spec *spec = codec->spec;
4990 int i;
4991 hda_nid_t pin;
4992
Takashi Iwaid4156932013-01-07 10:08:02 +01004993 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004994 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004995 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004996 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01004997 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01004998 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004999 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005000}
5001
Takashi Iwai973e4972012-12-20 15:16:09 +01005002/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5003 * invalid unsol tags by some reason
5004 */
5005static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5006{
5007 int i;
5008
5009 for (i = 0; i < codec->init_pins.used; i++) {
5010 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5011 hda_nid_t nid = pin->nid;
5012 if (is_jack_detectable(codec, nid) &&
5013 !snd_hda_jack_tbl_get(codec, nid))
5014 snd_hda_codec_update_cache(codec, nid, 0,
5015 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5016 }
5017}
5018
Takashi Iwai5187ac12013-01-07 12:52:16 +01005019/*
5020 * initialize the generic spec;
5021 * this can be put as patch_ops.init function
5022 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005023int snd_hda_gen_init(struct hda_codec *codec)
5024{
5025 struct hda_gen_spec *spec = codec->spec;
5026
5027 if (spec->init_hook)
5028 spec->init_hook(codec);
5029
5030 snd_hda_apply_verbs(codec);
5031
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005032 codec->cached_write = 1;
5033
Takashi Iwai352f7f92012-12-19 12:52:06 +01005034 init_multi_out(codec);
5035 init_extra_out(codec);
5036 init_multi_io(codec);
5037 init_analog_input(codec);
5038 init_input_src(codec);
5039 init_digital(codec);
5040
Takashi Iwai973e4972012-12-20 15:16:09 +01005041 clear_unsol_on_unused_pins(codec);
5042
Takashi Iwai352f7f92012-12-19 12:52:06 +01005043 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005044 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005045
Takashi Iwaidc870f32013-01-22 15:24:30 +01005046 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005047
Takashi Iwai352f7f92012-12-19 12:52:06 +01005048 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5049 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5050
5051 hda_call_check_power_status(codec, 0x01);
5052 return 0;
5053}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005054EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5055
Takashi Iwai5187ac12013-01-07 12:52:16 +01005056/*
5057 * free the generic spec;
5058 * this can be put as patch_ops.free function
5059 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005060void snd_hda_gen_free(struct hda_codec *codec)
5061{
5062 snd_hda_gen_spec_free(codec->spec);
5063 kfree(codec->spec);
5064 codec->spec = NULL;
5065}
5066EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5067
5068#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005069/*
5070 * check the loopback power save state;
5071 * this can be put as patch_ops.check_power_status function
5072 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005073int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5074{
5075 struct hda_gen_spec *spec = codec->spec;
5076 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5077}
5078EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5079#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005080
5081
5082/*
5083 * the generic codec support
5084 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
Takashi Iwai352f7f92012-12-19 12:52:06 +01005086static const struct hda_codec_ops generic_patch_ops = {
5087 .build_controls = snd_hda_gen_build_controls,
5088 .build_pcms = snd_hda_gen_build_pcms,
5089 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005090 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005091 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005092#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005093 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005094#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095};
5096
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097int snd_hda_parse_generic_codec(struct hda_codec *codec)
5098{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005099 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 int err;
5101
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005102 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005103 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005105 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005108 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5109 if (err < 0)
5110 return err;
5111
5112 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005113 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 goto error;
5115
5116 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 return 0;
5118
Takashi Iwai352f7f92012-12-19 12:52:06 +01005119error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005120 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 return err;
5122}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005123EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);