blob: cb40a0b7ce3ed0cfb618bc5ff07b83bd492dd09f [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;
153 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
154 if (val >= 0)
155 spec->add_out_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100156 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
157 if (val >= 0)
158 spec->add_in_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100159 val = snd_hda_get_bool_hint(codec, "power_down_unused");
160 if (val >= 0)
161 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100162 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
163 if (val >= 0)
164 spec->hp_mic = !!val;
165 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
166 if (val >= 0)
167 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100168
169 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
170 spec->mixer_nid = val;
171}
172
173/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100174 * pin control value accesses
175 */
176
177#define update_pin_ctl(codec, pin, val) \
178 snd_hda_codec_update_cache(codec, pin, 0, \
179 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
180
181/* restore the pinctl based on the cached value */
182static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
183{
184 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
185}
186
187/* set the pinctl target value and write it if requested */
188static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
189 unsigned int val, bool do_write)
190{
191 if (!pin)
192 return;
193 val = snd_hda_correct_pin_ctl(codec, pin, val);
194 snd_hda_codec_set_pin_target(codec, pin, val);
195 if (do_write)
196 update_pin_ctl(codec, pin, val);
197}
198
199/* set pinctl target values for all given pins */
200static void set_pin_targets(struct hda_codec *codec, int num_pins,
201 hda_nid_t *pins, unsigned int val)
202{
203 int i;
204 for (i = 0; i < num_pins; i++)
205 set_pin_target(codec, pins[i], val, false);
206}
207
208/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100209 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100212/* return the position of NID in the list, or -1 if not found */
213static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
214{
215 int i;
216 for (i = 0; i < nums; i++)
217 if (list[i] == nid)
218 return i;
219 return -1;
220}
221
222/* return true if the given NID is contained in the path */
223static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
224{
225 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
226}
227
Takashi Iwaif5172a72013-01-04 13:19:55 +0100228static struct nid_path *get_nid_path(struct hda_codec *codec,
229 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100230 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100232 struct hda_gen_spec *spec = codec->spec;
233 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Takashi Iwai352f7f92012-12-19 12:52:06 +0100235 for (i = 0; i < spec->paths.used; i++) {
236 struct nid_path *path = snd_array_elem(&spec->paths, i);
237 if (path->depth <= 0)
238 continue;
239 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100240 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100241 if (!anchor_nid ||
242 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
243 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100244 return path;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 return NULL;
248}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100249
250/* get the path between the given NIDs;
251 * passing 0 to either @pin or @dac behaves as a wildcard
252 */
253struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
254 hda_nid_t from_nid, hda_nid_t to_nid)
255{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100256 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100257}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100258EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
259
Takashi Iwai196c17662013-01-04 15:01:40 +0100260/* get the index number corresponding to the path instance;
261 * the index starts from 1, for easier checking the invalid value
262 */
263int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
264{
265 struct hda_gen_spec *spec = codec->spec;
266 struct nid_path *array = spec->paths.list;
267 ssize_t idx;
268
269 if (!spec->paths.used)
270 return 0;
271 idx = path - array;
272 if (idx < 0 || idx >= spec->paths.used)
273 return 0;
274 return idx + 1;
275}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100276EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100277
278/* get the path instance corresponding to the given index number */
279struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
280{
281 struct hda_gen_spec *spec = codec->spec;
282
283 if (idx <= 0 || idx > spec->paths.used)
284 return NULL;
285 return snd_array_elem(&spec->paths, idx - 1);
286}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100287EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100288
Takashi Iwai352f7f92012-12-19 12:52:06 +0100289/* check whether the given DAC is already found in any existing paths */
290static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
291{
292 struct hda_gen_spec *spec = codec->spec;
293 int i;
294
295 for (i = 0; i < spec->paths.used; i++) {
296 struct nid_path *path = snd_array_elem(&spec->paths, i);
297 if (path->path[0] == nid)
298 return true;
299 }
300 return false;
301}
302
303/* check whether the given two widgets can be connected */
304static bool is_reachable_path(struct hda_codec *codec,
305 hda_nid_t from_nid, hda_nid_t to_nid)
306{
307 if (!from_nid || !to_nid)
308 return false;
309 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
310}
311
312/* nid, dir and idx */
313#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
314
315/* check whether the given ctl is already assigned in any path elements */
316static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
317{
318 struct hda_gen_spec *spec = codec->spec;
319 int i;
320
321 val &= AMP_VAL_COMPARE_MASK;
322 for (i = 0; i < spec->paths.used; i++) {
323 struct nid_path *path = snd_array_elem(&spec->paths, i);
324 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
325 return true;
326 }
327 return false;
328}
329
330/* check whether a control with the given (nid, dir, idx) was assigned */
331static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100332 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100333{
334 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100335 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100336}
337
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100338static void print_nid_path(const char *pfx, struct nid_path *path)
339{
340 char buf[40];
341 int i;
342
343
344 buf[0] = 0;
345 for (i = 0; i < path->depth; i++) {
346 char tmp[4];
347 sprintf(tmp, ":%02x", path->path[i]);
348 strlcat(buf, tmp, sizeof(buf));
349 }
350 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
351}
352
Takashi Iwai352f7f92012-12-19 12:52:06 +0100353/* called recursively */
354static bool __parse_nid_path(struct hda_codec *codec,
355 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100356 int anchor_nid, struct nid_path *path,
357 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100358{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100359 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100360 int i, nums;
361
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100362 if (to_nid == anchor_nid)
363 anchor_nid = 0; /* anchor passed */
364 else if (to_nid == (hda_nid_t)(-anchor_nid))
365 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100366
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100367 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100368 for (i = 0; i < nums; i++) {
369 if (conn[i] != from_nid) {
370 /* special case: when from_nid is 0,
371 * try to find an empty DAC
372 */
373 if (from_nid ||
374 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
375 is_dac_already_used(codec, conn[i]))
376 continue;
377 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100378 /* anchor is not requested or already passed? */
379 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100380 goto found;
381 }
382 if (depth >= MAX_NID_PATH_DEPTH)
383 return false;
384 for (i = 0; i < nums; i++) {
385 unsigned int type;
386 type = get_wcaps_type(get_wcaps(codec, conn[i]));
387 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
388 type == AC_WID_PIN)
389 continue;
390 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100391 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100392 goto found;
393 }
394 return false;
395
396 found:
397 path->path[path->depth] = conn[i];
398 path->idx[path->depth + 1] = i;
399 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
400 path->multi[path->depth + 1] = 1;
401 path->depth++;
402 return true;
403}
404
405/* parse the widget path from the given nid to the target nid;
406 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100407 * when @anchor_nid is set to a positive value, only paths through the widget
408 * with the given value are evaluated.
409 * when @anchor_nid is set to a negative value, paths through the widget
410 * with the negative of given value are excluded, only other paths are chosen.
411 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100412 */
413bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100414 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100415 struct nid_path *path)
416{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100417 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100418 path->path[path->depth] = to_nid;
419 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100420 return true;
421 }
422 return false;
423}
424EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100427 * parse the path between the given NIDs and add to the path list.
428 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100430struct nid_path *
431snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100432 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100434 struct hda_gen_spec *spec = codec->spec;
435 struct nid_path *path;
436
437 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
438 return NULL;
439
Takashi Iwaif5172a72013-01-04 13:19:55 +0100440 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100441 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100442 if (path)
443 return path;
444
Takashi Iwai352f7f92012-12-19 12:52:06 +0100445 path = snd_array_new(&spec->paths);
446 if (!path)
447 return NULL;
448 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100449 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100450 return path;
451 /* push back */
452 spec->paths.used--;
453 return NULL;
454}
455EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
456
Takashi Iwai980428c2013-01-09 09:28:20 +0100457/* clear the given path as invalid so that it won't be picked up later */
458static void invalidate_nid_path(struct hda_codec *codec, int idx)
459{
460 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
461 if (!path)
462 return;
463 memset(path, 0, sizeof(*path));
464}
465
Takashi Iwai352f7f92012-12-19 12:52:06 +0100466/* look for an empty DAC slot */
467static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
468 bool is_digital)
469{
470 struct hda_gen_spec *spec = codec->spec;
471 bool cap_digital;
472 int i;
473
474 for (i = 0; i < spec->num_all_dacs; i++) {
475 hda_nid_t nid = spec->all_dacs[i];
476 if (!nid || is_dac_already_used(codec, nid))
477 continue;
478 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
479 if (is_digital != cap_digital)
480 continue;
481 if (is_reachable_path(codec, nid, pin))
482 return nid;
483 }
484 return 0;
485}
486
487/* replace the channels in the composed amp value with the given number */
488static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
489{
490 val &= ~(0x3U << 16);
491 val |= chs << 16;
492 return val;
493}
494
495/* check whether the widget has the given amp capability for the direction */
496static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
497 int dir, unsigned int bits)
498{
499 if (!nid)
500 return false;
501 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
502 if (query_amp_caps(codec, nid, dir) & bits)
503 return true;
504 return false;
505}
506
David Henningsson99a55922013-01-16 15:58:44 +0100507static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
508 hda_nid_t nid2, int dir)
509{
510 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
511 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
512 return (query_amp_caps(codec, nid1, dir) ==
513 query_amp_caps(codec, nid2, dir));
514}
515
Takashi Iwai352f7f92012-12-19 12:52:06 +0100516#define nid_has_mute(codec, nid, dir) \
517 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
518#define nid_has_volume(codec, nid, dir) \
519 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
520
521/* look for a widget suitable for assigning a mute switch in the path */
522static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
523 struct nid_path *path)
524{
525 int i;
526
527 for (i = path->depth - 1; i >= 0; i--) {
528 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
529 return path->path[i];
530 if (i != path->depth - 1 && i != 0 &&
531 nid_has_mute(codec, path->path[i], HDA_INPUT))
532 return path->path[i];
533 }
534 return 0;
535}
536
537/* look for a widget suitable for assigning a volume ctl in the path */
538static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
539 struct nid_path *path)
540{
541 int i;
542
543 for (i = path->depth - 1; i >= 0; i--) {
544 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
545 return path->path[i];
546 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100551 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100553
554/* can have the amp-in capability? */
555static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100557 hda_nid_t nid = path->path[idx];
558 unsigned int caps = get_wcaps(codec, nid);
559 unsigned int type = get_wcaps_type(caps);
560
561 if (!(caps & AC_WCAP_IN_AMP))
562 return false;
563 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
564 return false;
565 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Takashi Iwai352f7f92012-12-19 12:52:06 +0100568/* can have the amp-out capability? */
569static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100571 hda_nid_t nid = path->path[idx];
572 unsigned int caps = get_wcaps(codec, nid);
573 unsigned int type = get_wcaps_type(caps);
574
575 if (!(caps & AC_WCAP_OUT_AMP))
576 return false;
577 if (type == AC_WID_PIN && !idx) /* only for output pins */
578 return false;
579 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Takashi Iwai352f7f92012-12-19 12:52:06 +0100582/* check whether the given (nid,dir,idx) is active */
583static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100584 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100586 struct hda_gen_spec *spec = codec->spec;
587 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Takashi Iwai352f7f92012-12-19 12:52:06 +0100589 for (n = 0; n < spec->paths.used; n++) {
590 struct nid_path *path = snd_array_elem(&spec->paths, n);
591 if (!path->active)
592 continue;
593 for (i = 0; i < path->depth; i++) {
594 if (path->path[i] == nid) {
595 if (dir == HDA_OUTPUT || path->idx[i] == idx)
596 return true;
597 break;
598 }
599 }
600 }
601 return false;
602}
603
604/* get the default amp value for the target state */
605static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100606 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100607{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100608 unsigned int val = 0;
609
Takashi Iwai352f7f92012-12-19 12:52:06 +0100610 if (caps & AC_AMPCAP_NUM_STEPS) {
611 /* set to 0dB */
612 if (enable)
613 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
614 }
615 if (caps & AC_AMPCAP_MUTE) {
616 if (!enable)
617 val |= HDA_AMP_MUTE;
618 }
619 return val;
620}
621
622/* initialize the amp value (only at the first time) */
623static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
624{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100625 unsigned int caps = query_amp_caps(codec, nid, dir);
626 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100627 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
628}
629
Takashi Iwai8999bf02013-01-18 11:01:33 +0100630/* calculate amp value mask we can modify;
631 * if the given amp is controlled by mixers, don't touch it
632 */
633static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
634 hda_nid_t nid, int dir, int idx,
635 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100636{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100637 unsigned int mask = 0xff;
638
639 if (caps & AC_AMPCAP_MUTE) {
640 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
641 mask &= ~0x80;
642 }
643 if (caps & AC_AMPCAP_NUM_STEPS) {
644 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
645 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
646 mask &= ~0x7f;
647 }
648 return mask;
649}
650
651static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
652 int idx, int idx_to_check, bool enable)
653{
654 unsigned int caps;
655 unsigned int mask, val;
656
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100657 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100658 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100659
660 caps = query_amp_caps(codec, nid, dir);
661 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
662 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
663 if (!mask)
664 return;
665
666 val &= mask;
667 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100668}
669
670static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
671 int i, bool enable)
672{
673 hda_nid_t nid = path->path[i];
674 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100675 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100676}
677
678static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
679 int i, bool enable, bool add_aamix)
680{
681 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100682 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100683 int n, nums, idx;
684 int type;
685 hda_nid_t nid = path->path[i];
686
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100687 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100688 type = get_wcaps_type(get_wcaps(codec, nid));
689 if (type == AC_WID_PIN ||
690 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
691 nums = 1;
692 idx = 0;
693 } else
694 idx = path->idx[i];
695
696 for (n = 0; n < nums; n++)
697 init_amp(codec, nid, HDA_INPUT, n);
698
Takashi Iwai352f7f92012-12-19 12:52:06 +0100699 /* here is a little bit tricky in comparison with activate_amp_out();
700 * when aa-mixer is available, we need to enable the path as well
701 */
702 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100703 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100704 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100705 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707}
708
Takashi Iwai352f7f92012-12-19 12:52:06 +0100709/* activate or deactivate the given path
710 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100712void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
713 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100715 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100716 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Takashi Iwai352f7f92012-12-19 12:52:06 +0100718 if (!enable)
719 path->active = false;
720
721 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100722 hda_nid_t nid = path->path[i];
723 if (enable && spec->power_down_unused) {
724 /* make sure the widget is powered up */
725 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
726 snd_hda_codec_write(codec, nid, 0,
727 AC_VERB_SET_POWER_STATE,
728 AC_PWRST_D0);
729 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100730 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100731 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100732 AC_VERB_SET_CONNECT_SEL,
733 path->idx[i]);
734 if (has_amp_in(codec, path, i))
735 activate_amp_in(codec, path, i, enable, add_aamix);
736 if (has_amp_out(codec, path, i))
737 activate_amp_out(codec, path, i, enable);
738 }
739
740 if (enable)
741 path->active = true;
742}
743EXPORT_SYMBOL_HDA(snd_hda_activate_path);
744
Takashi Iwai55196ff2013-01-24 17:32:56 +0100745/* if the given path is inactive, put widgets into D3 (only if suitable) */
746static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
747{
748 struct hda_gen_spec *spec = codec->spec;
749 bool changed;
750 int i;
751
752 if (!spec->power_down_unused || path->active)
753 return;
754
755 for (i = 0; i < path->depth; i++) {
756 hda_nid_t nid = path->path[i];
757 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3)) {
758 snd_hda_codec_write(codec, nid, 0,
759 AC_VERB_SET_POWER_STATE,
760 AC_PWRST_D3);
761 changed = true;
762 }
763 }
764
765 if (changed) {
766 msleep(10);
767 snd_hda_codec_read(codec, path->path[0], 0,
768 AC_VERB_GET_POWER_STATE, 0);
769 }
770}
771
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100772/* turn on/off EAPD on the given pin */
773static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
774{
775 struct hda_gen_spec *spec = codec->spec;
776 if (spec->own_eapd_ctl ||
777 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
778 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100779 if (codec->inv_eapd)
780 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100781 snd_hda_codec_update_cache(codec, pin, 0,
782 AC_VERB_SET_EAPD_BTLENABLE,
783 enable ? 0x02 : 0x00);
784}
785
Takashi Iwai3e367f12013-01-23 17:07:23 +0100786/* re-initialize the path specified by the given path index */
787static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
788{
789 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
790 if (path)
791 snd_hda_activate_path(codec, path, path->active, false);
792}
793
Takashi Iwai352f7f92012-12-19 12:52:06 +0100794
795/*
796 * Helper functions for creating mixer ctl elements
797 */
798
799enum {
800 HDA_CTL_WIDGET_VOL,
801 HDA_CTL_WIDGET_MUTE,
802 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100803};
804static const struct snd_kcontrol_new control_templates[] = {
805 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
806 HDA_CODEC_MUTE(NULL, 0, 0, 0),
807 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100808};
809
810/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100811static struct snd_kcontrol_new *
812add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100813 int cidx, unsigned long val)
814{
815 struct snd_kcontrol_new *knew;
816
Takashi Iwai12c93df2012-12-19 14:38:33 +0100817 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100818 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100819 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100820 knew->index = cidx;
821 if (get_amp_nid_(val))
822 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
823 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100824 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100825}
826
827static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
828 const char *pfx, const char *dir,
829 const char *sfx, int cidx, unsigned long val)
830{
831 char name[32];
832 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100833 if (!add_control(spec, type, name, cidx, val))
834 return -ENOMEM;
835 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100836}
837
838#define add_pb_vol_ctrl(spec, type, pfx, val) \
839 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
840#define add_pb_sw_ctrl(spec, type, pfx, val) \
841 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
842#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
843 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
844#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
845 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
846
847static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
848 unsigned int chs, struct nid_path *path)
849{
850 unsigned int val;
851 if (!path)
852 return 0;
853 val = path->ctls[NID_PATH_VOL_CTL];
854 if (!val)
855 return 0;
856 val = amp_val_replace_channels(val, chs);
857 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
858}
859
860/* return the channel bits suitable for the given path->ctls[] */
861static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
862 int type)
863{
864 int chs = 1; /* mono (left only) */
865 if (path) {
866 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
867 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
868 chs = 3; /* stereo */
869 }
870 return chs;
871}
872
873static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
874 struct nid_path *path)
875{
876 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
877 return add_vol_ctl(codec, pfx, cidx, chs, path);
878}
879
880/* create a mute-switch for the given mixer widget;
881 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
882 */
883static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
884 unsigned int chs, struct nid_path *path)
885{
886 unsigned int val;
887 int type = HDA_CTL_WIDGET_MUTE;
888
889 if (!path)
890 return 0;
891 val = path->ctls[NID_PATH_MUTE_CTL];
892 if (!val)
893 return 0;
894 val = amp_val_replace_channels(val, chs);
895 if (get_amp_direction_(val) == HDA_INPUT) {
896 hda_nid_t nid = get_amp_nid_(val);
897 int nums = snd_hda_get_num_conns(codec, nid);
898 if (nums > 1) {
899 type = HDA_CTL_BIND_MUTE;
900 val |= nums << 19;
901 }
902 }
903 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
904}
905
906static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
907 int cidx, struct nid_path *path)
908{
909 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
910 return add_sw_ctl(codec, pfx, cidx, chs, path);
911}
912
Takashi Iwai247d85e2013-01-17 16:18:11 +0100913/* any ctl assigned to the path with the given index? */
914static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
915{
916 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
917 return path && path->ctls[ctl_type];
918}
919
Takashi Iwai352f7f92012-12-19 12:52:06 +0100920static const char * const channel_name[4] = {
921 "Front", "Surround", "CLFE", "Side"
922};
923
924/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100925static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
926 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100927{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100928 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100929 struct auto_pin_cfg *cfg = &spec->autocfg;
930
931 *index = 0;
932 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100933 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100934 return spec->vmaster_mute.hook ? "PCM" : "Master";
935
936 /* if there is really a single DAC used in the whole output paths,
937 * use it master (or "PCM" if a vmaster hook is present)
938 */
939 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
940 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
941 return spec->vmaster_mute.hook ? "PCM" : "Master";
942
Takashi Iwai247d85e2013-01-17 16:18:11 +0100943 /* multi-io channels */
944 if (ch >= cfg->line_outs)
945 return channel_name[ch];
946
Takashi Iwai352f7f92012-12-19 12:52:06 +0100947 switch (cfg->line_out_type) {
948 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100949 /* if the primary channel vol/mute is shared with HP volume,
950 * don't name it as Speaker
951 */
952 if (!ch && cfg->hp_outs &&
953 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
954 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100955 if (cfg->line_outs == 1)
956 return "Speaker";
957 if (cfg->line_outs == 2)
958 return ch ? "Bass Speaker" : "Speaker";
959 break;
960 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100961 /* if the primary channel vol/mute is shared with spk volume,
962 * don't name it as Headphone
963 */
964 if (!ch && cfg->speaker_outs &&
965 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
966 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100967 /* for multi-io case, only the primary out */
968 if (ch && spec->multi_ios)
969 break;
970 *index = ch;
971 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100972 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100973
974 /* for a single channel output, we don't have to name the channel */
975 if (cfg->line_outs == 1 && !spec->multi_ios)
976 return "PCM";
977
Takashi Iwai352f7f92012-12-19 12:52:06 +0100978 if (ch >= ARRAY_SIZE(channel_name)) {
979 snd_BUG();
980 return "PCM";
981 }
982
983 return channel_name[ch];
984}
985
986/*
987 * Parse output paths
988 */
989
990/* badness definition */
991enum {
992 /* No primary DAC is found for the main output */
993 BAD_NO_PRIMARY_DAC = 0x10000,
994 /* No DAC is found for the extra output */
995 BAD_NO_DAC = 0x4000,
996 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +0100997 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100998 /* No individual DAC for extra output */
999 BAD_NO_EXTRA_DAC = 0x102,
1000 /* No individual DAC for extra surrounds */
1001 BAD_NO_EXTRA_SURR_DAC = 0x101,
1002 /* Primary DAC shared with main surrounds */
1003 BAD_SHARED_SURROUND = 0x100,
1004 /* Primary DAC shared with main CLFE */
1005 BAD_SHARED_CLFE = 0x10,
1006 /* Primary DAC shared with extra surrounds */
1007 BAD_SHARED_EXTRA_SURROUND = 0x10,
1008 /* Volume widget is shared */
1009 BAD_SHARED_VOL = 0x10,
1010};
1011
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001012/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001013 * volume and mute controls, and assign the values to ctls[].
1014 *
1015 * When no appropriate widget is found in the path, the badness value
1016 * is incremented depending on the situation. The function returns the
1017 * total badness for both volume and mute controls.
1018 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001019static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001020{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001021 hda_nid_t nid;
1022 unsigned int val;
1023 int badness = 0;
1024
1025 if (!path)
1026 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001027
1028 if (path->ctls[NID_PATH_VOL_CTL] ||
1029 path->ctls[NID_PATH_MUTE_CTL])
1030 return 0; /* already evaluated */
1031
Takashi Iwai352f7f92012-12-19 12:52:06 +01001032 nid = look_for_out_vol_nid(codec, path);
1033 if (nid) {
1034 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1035 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1036 badness += BAD_SHARED_VOL;
1037 else
1038 path->ctls[NID_PATH_VOL_CTL] = val;
1039 } else
1040 badness += BAD_SHARED_VOL;
1041 nid = look_for_out_mute_nid(codec, path);
1042 if (nid) {
1043 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1044 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1045 nid_has_mute(codec, nid, HDA_OUTPUT))
1046 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1047 else
1048 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1049 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1050 badness += BAD_SHARED_VOL;
1051 else
1052 path->ctls[NID_PATH_MUTE_CTL] = val;
1053 } else
1054 badness += BAD_SHARED_VOL;
1055 return badness;
1056}
1057
1058struct badness_table {
1059 int no_primary_dac; /* no primary DAC */
1060 int no_dac; /* no secondary DACs */
1061 int shared_primary; /* primary DAC is shared with main output */
1062 int shared_surr; /* secondary DAC shared with main or primary */
1063 int shared_clfe; /* third DAC shared with main or primary */
1064 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1065};
1066
1067static struct badness_table main_out_badness = {
1068 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1069 .no_dac = BAD_NO_DAC,
1070 .shared_primary = BAD_NO_PRIMARY_DAC,
1071 .shared_surr = BAD_SHARED_SURROUND,
1072 .shared_clfe = BAD_SHARED_CLFE,
1073 .shared_surr_main = BAD_SHARED_SURROUND,
1074};
1075
1076static struct badness_table extra_out_badness = {
1077 .no_primary_dac = BAD_NO_DAC,
1078 .no_dac = BAD_NO_DAC,
1079 .shared_primary = BAD_NO_EXTRA_DAC,
1080 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1081 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1082 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1083};
1084
Takashi Iwai7385df62013-01-07 09:50:52 +01001085/* get the DAC of the primary output corresponding to the given array index */
1086static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1087{
1088 struct hda_gen_spec *spec = codec->spec;
1089 struct auto_pin_cfg *cfg = &spec->autocfg;
1090
1091 if (cfg->line_outs > idx)
1092 return spec->private_dac_nids[idx];
1093 idx -= cfg->line_outs;
1094 if (spec->multi_ios > idx)
1095 return spec->multi_io[idx].dac;
1096 return 0;
1097}
1098
1099/* return the DAC if it's reachable, otherwise zero */
1100static inline hda_nid_t try_dac(struct hda_codec *codec,
1101 hda_nid_t dac, hda_nid_t pin)
1102{
1103 return is_reachable_path(codec, dac, pin) ? dac : 0;
1104}
1105
Takashi Iwai352f7f92012-12-19 12:52:06 +01001106/* try to assign DACs to pins and return the resultant badness */
1107static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1108 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001109 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001110 const struct badness_table *bad)
1111{
1112 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001113 int i, j;
1114 int badness = 0;
1115 hda_nid_t dac;
1116
1117 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return 0;
1119
Takashi Iwai352f7f92012-12-19 12:52:06 +01001120 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001121 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001122 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001123
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001124 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1125 if (path) {
1126 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001127 continue;
1128 }
1129
1130 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001131 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001132 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001133 for (j = 1; j < num_outs; j++) {
1134 if (is_reachable_path(codec, dacs[j], pin)) {
1135 dacs[0] = dacs[j];
1136 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001137 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001138 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001139 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001142 }
1143 dac = dacs[i];
1144 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001145 if (num_outs > 2)
1146 dac = try_dac(codec, get_primary_out(codec, i), pin);
1147 if (!dac)
1148 dac = try_dac(codec, dacs[0], pin);
1149 if (!dac)
1150 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001151 if (dac) {
1152 if (!i)
1153 badness += bad->shared_primary;
1154 else if (i == 1)
1155 badness += bad->shared_surr;
1156 else
1157 badness += bad->shared_clfe;
1158 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1159 dac = spec->private_dac_nids[0];
1160 badness += bad->shared_surr_main;
1161 } else if (!i)
1162 badness += bad->no_primary_dac;
1163 else
1164 badness += bad->no_dac;
1165 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001166 if (!dac)
1167 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001168 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001169 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001170 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001171 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001172 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001173 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001174 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001175 badness += bad->no_dac;
1176 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001177 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001178 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001179 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001180 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001181 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001182 }
1183
1184 return badness;
1185}
1186
1187/* return NID if the given pin has only a single connection to a certain DAC */
1188static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1189{
1190 struct hda_gen_spec *spec = codec->spec;
1191 int i;
1192 hda_nid_t nid_found = 0;
1193
1194 for (i = 0; i < spec->num_all_dacs; i++) {
1195 hda_nid_t nid = spec->all_dacs[i];
1196 if (!nid || is_dac_already_used(codec, nid))
1197 continue;
1198 if (is_reachable_path(codec, nid, pin)) {
1199 if (nid_found)
1200 return 0;
1201 nid_found = nid;
1202 }
1203 }
1204 return nid_found;
1205}
1206
1207/* check whether the given pin can be a multi-io pin */
1208static bool can_be_multiio_pin(struct hda_codec *codec,
1209 unsigned int location, hda_nid_t nid)
1210{
1211 unsigned int defcfg, caps;
1212
1213 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1214 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1215 return false;
1216 if (location && get_defcfg_location(defcfg) != location)
1217 return false;
1218 caps = snd_hda_query_pin_caps(codec, nid);
1219 if (!(caps & AC_PINCAP_OUT))
1220 return false;
1221 return true;
1222}
1223
Takashi Iwaie22aab72013-01-04 14:50:04 +01001224/* count the number of input pins that are capable to be multi-io */
1225static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1226{
1227 struct hda_gen_spec *spec = codec->spec;
1228 struct auto_pin_cfg *cfg = &spec->autocfg;
1229 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1230 unsigned int location = get_defcfg_location(defcfg);
1231 int type, i;
1232 int num_pins = 0;
1233
1234 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1235 for (i = 0; i < cfg->num_inputs; i++) {
1236 if (cfg->inputs[i].type != type)
1237 continue;
1238 if (can_be_multiio_pin(codec, location,
1239 cfg->inputs[i].pin))
1240 num_pins++;
1241 }
1242 }
1243 return num_pins;
1244}
1245
Takashi Iwai352f7f92012-12-19 12:52:06 +01001246/*
1247 * multi-io helper
1248 *
1249 * When hardwired is set, try to fill ony hardwired pins, and returns
1250 * zero if any pins are filled, non-zero if nothing found.
1251 * When hardwired is off, try to fill possible input pins, and returns
1252 * the badness value.
1253 */
1254static int fill_multi_ios(struct hda_codec *codec,
1255 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001256 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001257{
1258 struct hda_gen_spec *spec = codec->spec;
1259 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001260 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001261 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1262 unsigned int location = get_defcfg_location(defcfg);
1263 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001264 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001265
1266 old_pins = spec->multi_ios;
1267 if (old_pins >= 2)
1268 goto end_fill;
1269
Takashi Iwaie22aab72013-01-04 14:50:04 +01001270 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001271 if (num_pins < 2)
1272 goto end_fill;
1273
Takashi Iwai352f7f92012-12-19 12:52:06 +01001274 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1275 for (i = 0; i < cfg->num_inputs; i++) {
1276 hda_nid_t nid = cfg->inputs[i].pin;
1277 hda_nid_t dac = 0;
1278
1279 if (cfg->inputs[i].type != type)
1280 continue;
1281 if (!can_be_multiio_pin(codec, location, nid))
1282 continue;
1283 for (j = 0; j < spec->multi_ios; j++) {
1284 if (nid == spec->multi_io[j].pin)
1285 break;
1286 }
1287 if (j < spec->multi_ios)
1288 continue;
1289
Takashi Iwai352f7f92012-12-19 12:52:06 +01001290 if (hardwired)
1291 dac = get_dac_if_single(codec, nid);
1292 else if (!dac)
1293 dac = look_for_dac(codec, nid, false);
1294 if (!dac) {
1295 badness++;
1296 continue;
1297 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001298 path = snd_hda_add_new_path(codec, dac, nid,
1299 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001300 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001301 badness++;
1302 continue;
1303 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001304 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001305 spec->multi_io[spec->multi_ios].pin = nid;
1306 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001307 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1308 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001309 spec->multi_ios++;
1310 if (spec->multi_ios >= 2)
1311 break;
1312 }
1313 }
1314 end_fill:
1315 if (badness)
1316 badness = BAD_MULTI_IO;
1317 if (old_pins == spec->multi_ios) {
1318 if (hardwired)
1319 return 1; /* nothing found */
1320 else
1321 return badness; /* no badness if nothing found */
1322 }
1323 if (!hardwired && spec->multi_ios < 2) {
1324 /* cancel newly assigned paths */
1325 spec->paths.used -= spec->multi_ios - old_pins;
1326 spec->multi_ios = old_pins;
1327 return badness;
1328 }
1329
1330 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001331 for (i = old_pins; i < spec->multi_ios; i++) {
1332 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1333 badness += assign_out_path_ctls(codec, path);
1334 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001335
1336 return badness;
1337}
1338
1339/* map DACs for all pins in the list if they are single connections */
1340static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001341 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001342{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001343 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001344 int i;
1345 bool found = false;
1346 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001347 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 hda_nid_t dac;
1349 if (dacs[i])
1350 continue;
1351 dac = get_dac_if_single(codec, pins[i]);
1352 if (!dac)
1353 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001354 path = snd_hda_add_new_path(codec, dac, pins[i],
1355 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001356 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001357 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001358 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001359 dacs[i] = dac;
1360 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001361 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001362 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001363 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001364 }
1365 }
1366 return found;
1367}
1368
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001369/* create a new path including aamix if available, and return its index */
1370static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1371{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001372 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001373 struct nid_path *path;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001374 hda_nid_t dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001375
1376 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001377 if (!path || !path->depth ||
1378 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001379 return 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001380 dac = path->path[0];
1381 pin = path->path[path->depth - 1];
1382 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1383 if (!path) {
1384 if (dac != spec->multiout.dac_nids[0])
1385 dac = spec->multiout.dac_nids[0];
1386 else if (spec->multiout.hp_out_nid[0])
1387 dac = spec->multiout.hp_out_nid[0];
1388 else if (spec->multiout.extra_out_nid[0])
1389 dac = spec->multiout.extra_out_nid[0];
1390 if (dac)
1391 path = snd_hda_add_new_path(codec, dac, pin,
1392 spec->mixer_nid);
1393 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001394 if (!path)
1395 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001396 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001397 path->active = false; /* unused as default */
1398 return snd_hda_get_path_idx(codec, path);
1399}
1400
Takashi Iwaia07a9492013-01-07 16:44:06 +01001401/* fill the empty entries in the dac array for speaker/hp with the
1402 * shared dac pointed by the paths
1403 */
1404static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1405 hda_nid_t *dacs, int *path_idx)
1406{
1407 struct nid_path *path;
1408 int i;
1409
1410 for (i = 0; i < num_outs; i++) {
1411 if (dacs[i])
1412 continue;
1413 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1414 if (!path)
1415 continue;
1416 dacs[i] = path->path[0];
1417 }
1418}
1419
Takashi Iwai352f7f92012-12-19 12:52:06 +01001420/* fill in the dac_nids table from the parsed pin configuration */
1421static int fill_and_eval_dacs(struct hda_codec *codec,
1422 bool fill_hardwired,
1423 bool fill_mio_first)
1424{
1425 struct hda_gen_spec *spec = codec->spec;
1426 struct auto_pin_cfg *cfg = &spec->autocfg;
1427 int i, err, badness;
1428
1429 /* set num_dacs once to full for look_for_dac() */
1430 spec->multiout.num_dacs = cfg->line_outs;
1431 spec->multiout.dac_nids = spec->private_dac_nids;
1432 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1433 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1434 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1435 spec->multi_ios = 0;
1436 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001437
1438 /* clear path indices */
1439 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1440 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1441 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1442 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1443 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001444 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001445 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1446 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1447
Takashi Iwai352f7f92012-12-19 12:52:06 +01001448 badness = 0;
1449
1450 /* fill hard-wired DACs first */
1451 if (fill_hardwired) {
1452 bool mapped;
1453 do {
1454 mapped = map_singles(codec, cfg->line_outs,
1455 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001456 spec->private_dac_nids,
1457 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001458 mapped |= map_singles(codec, cfg->hp_outs,
1459 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001460 spec->multiout.hp_out_nid,
1461 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001462 mapped |= map_singles(codec, cfg->speaker_outs,
1463 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001464 spec->multiout.extra_out_nid,
1465 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001466 if (fill_mio_first && cfg->line_outs == 1 &&
1467 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001468 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001469 if (!err)
1470 mapped = true;
1471 }
1472 } while (mapped);
1473 }
1474
1475 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001476 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001477 &main_out_badness);
1478
Takashi Iwai352f7f92012-12-19 12:52:06 +01001479 if (fill_mio_first &&
1480 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1481 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001482 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001483 if (err < 0)
1484 return err;
1485 /* we don't count badness at this stage yet */
1486 }
1487
1488 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1489 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1490 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001491 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001492 &extra_out_badness);
1493 if (err < 0)
1494 return err;
1495 badness += err;
1496 }
1497 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1498 err = try_assign_dacs(codec, cfg->speaker_outs,
1499 cfg->speaker_pins,
1500 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001501 spec->speaker_paths,
1502 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001503 if (err < 0)
1504 return err;
1505 badness += err;
1506 }
1507 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001508 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001509 if (err < 0)
1510 return err;
1511 badness += err;
1512 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001513
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001514 if (spec->mixer_nid) {
1515 spec->aamix_out_paths[0] =
1516 check_aamix_out_path(codec, spec->out_paths[0]);
1517 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1518 spec->aamix_out_paths[1] =
1519 check_aamix_out_path(codec, spec->hp_paths[0]);
1520 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1521 spec->aamix_out_paths[2] =
1522 check_aamix_out_path(codec, spec->speaker_paths[0]);
1523 }
1524
Takashi Iwaie22aab72013-01-04 14:50:04 +01001525 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1526 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1527 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001528
Takashi Iwaia07a9492013-01-07 16:44:06 +01001529 /* re-count num_dacs and squash invalid entries */
1530 spec->multiout.num_dacs = 0;
1531 for (i = 0; i < cfg->line_outs; i++) {
1532 if (spec->private_dac_nids[i])
1533 spec->multiout.num_dacs++;
1534 else {
1535 memmove(spec->private_dac_nids + i,
1536 spec->private_dac_nids + i + 1,
1537 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1538 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1539 }
1540 }
1541
1542 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001543 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001544
Takashi Iwai352f7f92012-12-19 12:52:06 +01001545 if (spec->multi_ios == 2) {
1546 for (i = 0; i < 2; i++)
1547 spec->private_dac_nids[spec->multiout.num_dacs++] =
1548 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001549 } else if (spec->multi_ios) {
1550 spec->multi_ios = 0;
1551 badness += BAD_MULTI_IO;
1552 }
1553
Takashi Iwaia07a9492013-01-07 16:44:06 +01001554 /* re-fill the shared DAC for speaker / headphone */
1555 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1556 refill_shared_dacs(codec, cfg->hp_outs,
1557 spec->multiout.hp_out_nid,
1558 spec->hp_paths);
1559 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1560 refill_shared_dacs(codec, cfg->speaker_outs,
1561 spec->multiout.extra_out_nid,
1562 spec->speaker_paths);
1563
Takashi Iwai352f7f92012-12-19 12:52:06 +01001564 return badness;
1565}
1566
1567#define DEBUG_BADNESS
1568
1569#ifdef DEBUG_BADNESS
1570#define debug_badness snd_printdd
1571#else
1572#define debug_badness(...)
1573#endif
1574
Takashi Iwaia7694092013-01-21 10:43:18 +01001575#ifdef DEBUG_BADNESS
1576static inline void print_nid_path_idx(struct hda_codec *codec,
1577 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001578{
Takashi Iwaia7694092013-01-21 10:43:18 +01001579 struct nid_path *path;
1580
1581 path = snd_hda_get_path_from_idx(codec, idx);
1582 if (path)
1583 print_nid_path(pfx, path);
1584}
1585
1586static void debug_show_configs(struct hda_codec *codec,
1587 struct auto_pin_cfg *cfg)
1588{
1589 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001590 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001591 int i;
1592
1593 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001594 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001595 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001596 spec->multiout.dac_nids[0],
1597 spec->multiout.dac_nids[1],
1598 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001599 spec->multiout.dac_nids[3],
1600 lo_type[cfg->line_out_type]);
1601 for (i = 0; i < cfg->line_outs; i++)
1602 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001603 if (spec->multi_ios > 0)
1604 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1605 spec->multi_ios,
1606 spec->multi_io[0].pin, spec->multi_io[1].pin,
1607 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001608 for (i = 0; i < spec->multi_ios; i++)
1609 print_nid_path_idx(codec, " mio",
1610 spec->out_paths[cfg->line_outs + i]);
1611 if (cfg->hp_outs)
1612 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001613 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001614 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001615 spec->multiout.hp_out_nid[0],
1616 spec->multiout.hp_out_nid[1],
1617 spec->multiout.hp_out_nid[2],
1618 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001619 for (i = 0; i < cfg->hp_outs; i++)
1620 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1621 if (cfg->speaker_outs)
1622 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001623 cfg->speaker_pins[0], cfg->speaker_pins[1],
1624 cfg->speaker_pins[2], cfg->speaker_pins[3],
1625 spec->multiout.extra_out_nid[0],
1626 spec->multiout.extra_out_nid[1],
1627 spec->multiout.extra_out_nid[2],
1628 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001629 for (i = 0; i < cfg->speaker_outs; i++)
1630 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1631 for (i = 0; i < 3; i++)
1632 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001633}
Takashi Iwaia7694092013-01-21 10:43:18 +01001634#else
1635#define debug_show_configs(codec, cfg) /* NOP */
1636#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001637
1638/* find all available DACs of the codec */
1639static void fill_all_dac_nids(struct hda_codec *codec)
1640{
1641 struct hda_gen_spec *spec = codec->spec;
1642 int i;
1643 hda_nid_t nid = codec->start_nid;
1644
1645 spec->num_all_dacs = 0;
1646 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1647 for (i = 0; i < codec->num_nodes; i++, nid++) {
1648 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1649 continue;
1650 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1651 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1652 break;
1653 }
1654 spec->all_dacs[spec->num_all_dacs++] = nid;
1655 }
1656}
1657
1658static int parse_output_paths(struct hda_codec *codec)
1659{
1660 struct hda_gen_spec *spec = codec->spec;
1661 struct auto_pin_cfg *cfg = &spec->autocfg;
1662 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001663 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001664 int best_badness = INT_MAX;
1665 int badness;
1666 bool fill_hardwired = true, fill_mio_first = true;
1667 bool best_wired = true, best_mio = true;
1668 bool hp_spk_swapped = false;
1669
Takashi Iwai352f7f92012-12-19 12:52:06 +01001670 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1671 if (!best_cfg)
1672 return -ENOMEM;
1673 *best_cfg = *cfg;
1674
1675 for (;;) {
1676 badness = fill_and_eval_dacs(codec, fill_hardwired,
1677 fill_mio_first);
1678 if (badness < 0) {
1679 kfree(best_cfg);
1680 return badness;
1681 }
1682 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1683 cfg->line_out_type, fill_hardwired, fill_mio_first,
1684 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001685 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001686 if (badness < best_badness) {
1687 best_badness = badness;
1688 *best_cfg = *cfg;
1689 best_wired = fill_hardwired;
1690 best_mio = fill_mio_first;
1691 }
1692 if (!badness)
1693 break;
1694 fill_mio_first = !fill_mio_first;
1695 if (!fill_mio_first)
1696 continue;
1697 fill_hardwired = !fill_hardwired;
1698 if (!fill_hardwired)
1699 continue;
1700 if (hp_spk_swapped)
1701 break;
1702 hp_spk_swapped = true;
1703 if (cfg->speaker_outs > 0 &&
1704 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1705 cfg->hp_outs = cfg->line_outs;
1706 memcpy(cfg->hp_pins, cfg->line_out_pins,
1707 sizeof(cfg->hp_pins));
1708 cfg->line_outs = cfg->speaker_outs;
1709 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1710 sizeof(cfg->speaker_pins));
1711 cfg->speaker_outs = 0;
1712 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1713 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1714 fill_hardwired = true;
1715 continue;
1716 }
1717 if (cfg->hp_outs > 0 &&
1718 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1719 cfg->speaker_outs = cfg->line_outs;
1720 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1721 sizeof(cfg->speaker_pins));
1722 cfg->line_outs = cfg->hp_outs;
1723 memcpy(cfg->line_out_pins, cfg->hp_pins,
1724 sizeof(cfg->hp_pins));
1725 cfg->hp_outs = 0;
1726 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1727 cfg->line_out_type = AUTO_PIN_HP_OUT;
1728 fill_hardwired = true;
1729 continue;
1730 }
1731 break;
1732 }
1733
1734 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001735 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001736 *cfg = *best_cfg;
1737 fill_and_eval_dacs(codec, best_wired, best_mio);
1738 }
1739 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1740 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001741 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001742
1743 if (cfg->line_out_pins[0]) {
1744 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001745 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001746 if (path)
1747 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001748 if (spec->vmaster_nid)
1749 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1750 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001751 }
1752
Takashi Iwai9314a582013-01-21 10:49:05 +01001753 /* set initial pinctl targets */
1754 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1755 val = PIN_HP;
1756 else
1757 val = PIN_OUT;
1758 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1759 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1760 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1761 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1762 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1763 set_pin_targets(codec, cfg->speaker_outs,
1764 cfg->speaker_pins, val);
1765 }
1766
Takashi Iwai352f7f92012-12-19 12:52:06 +01001767 kfree(best_cfg);
1768 return 0;
1769}
1770
1771/* add playback controls from the parsed DAC table */
1772static int create_multi_out_ctls(struct hda_codec *codec,
1773 const struct auto_pin_cfg *cfg)
1774{
1775 struct hda_gen_spec *spec = codec->spec;
1776 int i, err, noutputs;
1777
1778 noutputs = cfg->line_outs;
1779 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1780 noutputs += spec->multi_ios;
1781
1782 for (i = 0; i < noutputs; i++) {
1783 const char *name;
1784 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001785 struct nid_path *path;
1786
Takashi Iwai196c17662013-01-04 15:01:40 +01001787 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001788 if (!path)
1789 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001790
1791 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001792 if (!name || !strcmp(name, "CLFE")) {
1793 /* Center/LFE */
1794 err = add_vol_ctl(codec, "Center", 0, 1, path);
1795 if (err < 0)
1796 return err;
1797 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1798 if (err < 0)
1799 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001800 } else {
1801 err = add_stereo_vol(codec, name, index, path);
1802 if (err < 0)
1803 return err;
1804 }
1805
1806 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1807 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001808 err = add_sw_ctl(codec, "Center", 0, 1, path);
1809 if (err < 0)
1810 return err;
1811 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1812 if (err < 0)
1813 return err;
1814 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001815 err = add_stereo_sw(codec, name, index, path);
1816 if (err < 0)
1817 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819 }
1820 return 0;
1821}
1822
Takashi Iwaic2c80382013-01-07 10:33:57 +01001823static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001824 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001826 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 int err;
1828
Takashi Iwai196c17662013-01-04 15:01:40 +01001829 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001830 if (!path)
1831 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001832 err = add_stereo_vol(codec, pfx, cidx, path);
1833 if (err < 0)
1834 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001835 err = add_stereo_sw(codec, pfx, cidx, path);
1836 if (err < 0)
1837 return err;
1838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
Takashi Iwai352f7f92012-12-19 12:52:06 +01001841/* add playback controls for speaker and HP outputs */
1842static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001843 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001845 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001846
1847 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001848 const char *name;
1849 char tmp[44];
1850 int err, idx = 0;
1851
1852 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1853 name = "Bass Speaker";
1854 else if (num_pins >= 3) {
1855 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001856 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001857 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001858 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001859 name = pfx;
1860 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001862 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001863 if (err < 0)
1864 return err;
1865 }
1866 return 0;
1867}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001868
Takashi Iwai352f7f92012-12-19 12:52:06 +01001869static int create_hp_out_ctls(struct hda_codec *codec)
1870{
1871 struct hda_gen_spec *spec = codec->spec;
1872 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001873 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001874 "Headphone");
1875}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Takashi Iwai352f7f92012-12-19 12:52:06 +01001877static int create_speaker_out_ctls(struct hda_codec *codec)
1878{
1879 struct hda_gen_spec *spec = codec->spec;
1880 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001881 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001882 "Speaker");
1883}
1884
1885/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001886 * independent HP controls
1887 */
1888
1889static int indep_hp_info(struct snd_kcontrol *kcontrol,
1890 struct snd_ctl_elem_info *uinfo)
1891{
1892 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1893}
1894
1895static int indep_hp_get(struct snd_kcontrol *kcontrol,
1896 struct snd_ctl_elem_value *ucontrol)
1897{
1898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1899 struct hda_gen_spec *spec = codec->spec;
1900 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1901 return 0;
1902}
1903
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001904static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1905 int nomix_path_idx, int mix_path_idx,
1906 int out_type);
1907
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001908static int indep_hp_put(struct snd_kcontrol *kcontrol,
1909 struct snd_ctl_elem_value *ucontrol)
1910{
1911 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1912 struct hda_gen_spec *spec = codec->spec;
1913 unsigned int select = ucontrol->value.enumerated.item[0];
1914 int ret = 0;
1915
1916 mutex_lock(&spec->pcm_mutex);
1917 if (spec->active_streams) {
1918 ret = -EBUSY;
1919 goto unlock;
1920 }
1921
1922 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001923 hda_nid_t *dacp;
1924 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1925 dacp = &spec->private_dac_nids[0];
1926 else
1927 dacp = &spec->multiout.hp_out_nid[0];
1928
1929 /* update HP aamix paths in case it conflicts with indep HP */
1930 if (spec->have_aamix_ctl) {
1931 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1932 update_aamix_paths(codec, spec->aamix_mode,
1933 spec->out_paths[0],
1934 spec->aamix_out_paths[0],
1935 spec->autocfg.line_out_type);
1936 else
1937 update_aamix_paths(codec, spec->aamix_mode,
1938 spec->hp_paths[0],
1939 spec->aamix_out_paths[1],
1940 AUTO_PIN_HP_OUT);
1941 }
1942
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001943 spec->indep_hp_enabled = select;
1944 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001945 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001946 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001947 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01001948
1949 /* update HP auto-mute state too */
1950 if (spec->hp_automute_hook)
1951 spec->hp_automute_hook(codec, NULL);
1952 else
1953 snd_hda_gen_hp_automute(codec, NULL);
1954
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001955 ret = 1;
1956 }
1957 unlock:
1958 mutex_unlock(&spec->pcm_mutex);
1959 return ret;
1960}
1961
1962static const struct snd_kcontrol_new indep_hp_ctl = {
1963 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1964 .name = "Independent HP",
1965 .info = indep_hp_info,
1966 .get = indep_hp_get,
1967 .put = indep_hp_put,
1968};
1969
1970
1971static int create_indep_hp_ctls(struct hda_codec *codec)
1972{
1973 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001974 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001975
1976 if (!spec->indep_hp)
1977 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001978 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1979 dac = spec->multiout.dac_nids[0];
1980 else
1981 dac = spec->multiout.hp_out_nid[0];
1982 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001983 spec->indep_hp = 0;
1984 return 0;
1985 }
1986
1987 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001988 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001989 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1990 return -ENOMEM;
1991 return 0;
1992}
1993
1994/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001995 * channel mode enum control
1996 */
1997
1998static int ch_mode_info(struct snd_kcontrol *kcontrol,
1999 struct snd_ctl_elem_info *uinfo)
2000{
2001 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2002 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002003 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002004
2005 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2006 uinfo->count = 1;
2007 uinfo->value.enumerated.items = spec->multi_ios + 1;
2008 if (uinfo->value.enumerated.item > spec->multi_ios)
2009 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002010 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2011 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002012 return 0;
2013}
2014
2015static int ch_mode_get(struct snd_kcontrol *kcontrol,
2016 struct snd_ctl_elem_value *ucontrol)
2017{
2018 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2019 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002020 ucontrol->value.enumerated.item[0] =
2021 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002022 return 0;
2023}
2024
Takashi Iwai196c17662013-01-04 15:01:40 +01002025static inline struct nid_path *
2026get_multiio_path(struct hda_codec *codec, int idx)
2027{
2028 struct hda_gen_spec *spec = codec->spec;
2029 return snd_hda_get_path_from_idx(codec,
2030 spec->out_paths[spec->autocfg.line_outs + idx]);
2031}
2032
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002033static void update_automute_all(struct hda_codec *codec);
2034
Takashi Iwai352f7f92012-12-19 12:52:06 +01002035static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2036{
2037 struct hda_gen_spec *spec = codec->spec;
2038 hda_nid_t nid = spec->multi_io[idx].pin;
2039 struct nid_path *path;
2040
Takashi Iwai196c17662013-01-04 15:01:40 +01002041 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002042 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002044
2045 if (path->active == output)
2046 return 0;
2047
2048 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002049 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002050 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002051 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002052 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002053 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002054 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002055 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002056 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002058
2059 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002060 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002061
Takashi Iwai352f7f92012-12-19 12:52:06 +01002062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Takashi Iwai352f7f92012-12-19 12:52:06 +01002065static int ch_mode_put(struct snd_kcontrol *kcontrol,
2066 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002068 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2069 struct hda_gen_spec *spec = codec->spec;
2070 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Takashi Iwai352f7f92012-12-19 12:52:06 +01002072 ch = ucontrol->value.enumerated.item[0];
2073 if (ch < 0 || ch > spec->multi_ios)
2074 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002075 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002076 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002077 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002078 for (i = 0; i < spec->multi_ios; i++)
2079 set_multi_io(codec, i, i < ch);
2080 spec->multiout.max_channels = max(spec->ext_channel_count,
2081 spec->const_channel_count);
2082 if (spec->need_dac_fix)
2083 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return 1;
2085}
2086
Takashi Iwai352f7f92012-12-19 12:52:06 +01002087static const struct snd_kcontrol_new channel_mode_enum = {
2088 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2089 .name = "Channel Mode",
2090 .info = ch_mode_info,
2091 .get = ch_mode_get,
2092 .put = ch_mode_put,
2093};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Takashi Iwai352f7f92012-12-19 12:52:06 +01002095static int create_multi_channel_mode(struct hda_codec *codec)
2096{
2097 struct hda_gen_spec *spec = codec->spec;
2098
2099 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002100 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002101 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return 0;
2104}
2105
Takashi Iwai352f7f92012-12-19 12:52:06 +01002106/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002107 * aamix loopback enable/disable switch
2108 */
2109
2110#define loopback_mixing_info indep_hp_info
2111
2112static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2113 struct snd_ctl_elem_value *ucontrol)
2114{
2115 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2116 struct hda_gen_spec *spec = codec->spec;
2117 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2118 return 0;
2119}
2120
2121static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002122 int nomix_path_idx, int mix_path_idx,
2123 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002124{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002125 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002126 struct nid_path *nomix_path, *mix_path;
2127
2128 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2129 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2130 if (!nomix_path || !mix_path)
2131 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002132
2133 /* if HP aamix path is driven from a different DAC and the
2134 * independent HP mode is ON, can't turn on aamix path
2135 */
2136 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2137 mix_path->path[0] != spec->alt_dac_nid)
2138 do_mix = false;
2139
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002140 if (do_mix) {
2141 snd_hda_activate_path(codec, nomix_path, false, true);
2142 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002143 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002144 } else {
2145 snd_hda_activate_path(codec, mix_path, false, true);
2146 snd_hda_activate_path(codec, nomix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002147 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002148 }
2149}
2150
2151static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2152 struct snd_ctl_elem_value *ucontrol)
2153{
2154 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2155 struct hda_gen_spec *spec = codec->spec;
2156 unsigned int val = ucontrol->value.enumerated.item[0];
2157
2158 if (val == spec->aamix_mode)
2159 return 0;
2160 spec->aamix_mode = val;
2161 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002162 spec->aamix_out_paths[0],
2163 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002164 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002165 spec->aamix_out_paths[1],
2166 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002167 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002168 spec->aamix_out_paths[2],
2169 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002170 return 1;
2171}
2172
2173static const struct snd_kcontrol_new loopback_mixing_enum = {
2174 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2175 .name = "Loopback Mixing",
2176 .info = loopback_mixing_info,
2177 .get = loopback_mixing_get,
2178 .put = loopback_mixing_put,
2179};
2180
2181static int create_loopback_mixing_ctl(struct hda_codec *codec)
2182{
2183 struct hda_gen_spec *spec = codec->spec;
2184
2185 if (!spec->mixer_nid)
2186 return 0;
2187 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2188 spec->aamix_out_paths[2]))
2189 return 0;
2190 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2191 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002192 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002193 return 0;
2194}
2195
2196/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002197 * shared headphone/mic handling
2198 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002199
Takashi Iwai352f7f92012-12-19 12:52:06 +01002200static void call_update_outputs(struct hda_codec *codec);
2201
2202/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002203static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002204{
2205 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002206 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002207 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002208 hda_nid_t pin;
2209
2210 pin = spec->hp_mic_pin;
2211 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2212
2213 if (!force) {
2214 val = snd_hda_codec_get_pin_target(codec, pin);
2215 if (as_mic) {
2216 if (val & PIN_IN)
2217 return;
2218 } else {
2219 if (val & PIN_OUT)
2220 return;
2221 }
2222 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002223
2224 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002225 /* if the HP pin doesn't support VREF and the codec driver gives an
2226 * alternative pin, set up the VREF on that pin instead
2227 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002228 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2229 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2230 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2231 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002232 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002233 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002234 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002235
Takashi Iwai967303d2013-02-19 17:12:42 +01002236 if (as_mic)
2237 val |= PIN_IN;
2238 else
2239 val = PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002240 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002241
Takashi Iwai967303d2013-02-19 17:12:42 +01002242 /* update HP auto-mute state too */
2243 if (spec->hp_automute_hook)
2244 spec->hp_automute_hook(codec, NULL);
2245 else
2246 snd_hda_gen_hp_automute(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002247}
2248
2249/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002250static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002251{
2252 struct hda_gen_spec *spec = codec->spec;
2253 struct auto_pin_cfg *cfg = &spec->autocfg;
2254 unsigned int defcfg;
2255 hda_nid_t nid;
2256
Takashi Iwai967303d2013-02-19 17:12:42 +01002257 if (!spec->hp_mic) {
2258 if (spec->suppress_hp_mic_detect)
2259 return 0;
2260 /* automatic detection: only if no input or a single internal
2261 * input pin is found, try to detect the shared hp/mic
2262 */
2263 if (cfg->num_inputs > 1)
2264 return 0;
2265 else if (cfg->num_inputs == 1) {
2266 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2267 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2268 return 0;
2269 }
2270 }
2271
2272 spec->hp_mic = 0; /* clear once */
2273 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002274 return 0;
2275
Takashi Iwai967303d2013-02-19 17:12:42 +01002276 nid = 0;
2277 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2278 nid = cfg->line_out_pins[0];
2279 else if (cfg->hp_outs > 0)
2280 nid = cfg->hp_pins[0];
2281 if (!nid)
2282 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002283
2284 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2285 return 0; /* no input */
2286
Takashi Iwai967303d2013-02-19 17:12:42 +01002287 cfg->inputs[cfg->num_inputs].pin = nid;
2288 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2289 cfg->num_inputs++;
2290 spec->hp_mic = 1;
2291 spec->hp_mic_pin = nid;
2292 /* we can't handle auto-mic together with HP-mic */
2293 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002294 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2295 return 0;
2296}
2297
Takashi Iwai978e77e2013-01-10 16:57:58 +01002298/*
2299 * output jack mode
2300 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002301
2302static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2303
2304static const char * const out_jack_texts[] = {
2305 "Line Out", "Headphone Out",
2306};
2307
Takashi Iwai978e77e2013-01-10 16:57:58 +01002308static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2309 struct snd_ctl_elem_info *uinfo)
2310{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002311 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002312}
2313
2314static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2315 struct snd_ctl_elem_value *ucontrol)
2316{
2317 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2318 hda_nid_t nid = kcontrol->private_value;
2319 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2320 ucontrol->value.enumerated.item[0] = 1;
2321 else
2322 ucontrol->value.enumerated.item[0] = 0;
2323 return 0;
2324}
2325
2326static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2327 struct snd_ctl_elem_value *ucontrol)
2328{
2329 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2330 hda_nid_t nid = kcontrol->private_value;
2331 unsigned int val;
2332
2333 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2334 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2335 return 0;
2336 snd_hda_set_pin_ctl_cache(codec, nid, val);
2337 return 1;
2338}
2339
2340static const struct snd_kcontrol_new out_jack_mode_enum = {
2341 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2342 .info = out_jack_mode_info,
2343 .get = out_jack_mode_get,
2344 .put = out_jack_mode_put,
2345};
2346
2347static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2348{
2349 struct hda_gen_spec *spec = codec->spec;
2350 int i;
2351
2352 for (i = 0; i < spec->kctls.used; i++) {
2353 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2354 if (!strcmp(kctl->name, name) && kctl->index == idx)
2355 return true;
2356 }
2357 return false;
2358}
2359
2360static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2361 char *name, size_t name_len)
2362{
2363 struct hda_gen_spec *spec = codec->spec;
2364 int idx = 0;
2365
2366 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2367 strlcat(name, " Jack Mode", name_len);
2368
2369 for (; find_kctl_name(codec, name, idx); idx++)
2370 ;
2371}
2372
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002373static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2374{
2375 struct hda_gen_spec *spec = codec->spec;
2376 if (spec->add_out_jack_modes) {
2377 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2378 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2379 return 2;
2380 }
2381 return 1;
2382}
2383
Takashi Iwai978e77e2013-01-10 16:57:58 +01002384static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2385 hda_nid_t *pins)
2386{
2387 struct hda_gen_spec *spec = codec->spec;
2388 int i;
2389
2390 for (i = 0; i < num_pins; i++) {
2391 hda_nid_t pin = pins[i];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002392 if (pin == spec->hp_mic_pin) {
2393 int ret = create_hp_mic_jack_mode(codec, pin);
2394 if (ret < 0)
2395 return ret;
2396 continue;
2397 }
2398 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002399 struct snd_kcontrol_new *knew;
2400 char name[44];
2401 get_jack_mode_name(codec, pin, name, sizeof(name));
2402 knew = snd_hda_gen_add_kctl(spec, name,
2403 &out_jack_mode_enum);
2404 if (!knew)
2405 return -ENOMEM;
2406 knew->private_value = pin;
2407 }
2408 }
2409
2410 return 0;
2411}
2412
Takashi Iwai294765582013-01-17 09:52:11 +01002413/*
2414 * input jack mode
2415 */
2416
2417/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2418#define NUM_VREFS 6
2419
2420static const char * const vref_texts[NUM_VREFS] = {
2421 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2422 "", "Mic 80pc Bias", "Mic 100pc Bias"
2423};
2424
2425static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2426{
2427 unsigned int pincap;
2428
2429 pincap = snd_hda_query_pin_caps(codec, pin);
2430 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2431 /* filter out unusual vrefs */
2432 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2433 return pincap;
2434}
2435
2436/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2437static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2438{
2439 unsigned int i, n = 0;
2440
2441 for (i = 0; i < NUM_VREFS; i++) {
2442 if (vref_caps & (1 << i)) {
2443 if (n == item_idx)
2444 return i;
2445 n++;
2446 }
2447 }
2448 return 0;
2449}
2450
2451/* convert back from the vref ctl index to the enum item index */
2452static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2453{
2454 unsigned int i, n = 0;
2455
2456 for (i = 0; i < NUM_VREFS; i++) {
2457 if (i == idx)
2458 return n;
2459 if (vref_caps & (1 << i))
2460 n++;
2461 }
2462 return 0;
2463}
2464
2465static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2466 struct snd_ctl_elem_info *uinfo)
2467{
2468 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2469 hda_nid_t nid = kcontrol->private_value;
2470 unsigned int vref_caps = get_vref_caps(codec, nid);
2471
2472 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2473 vref_texts);
2474 /* set the right text */
2475 strcpy(uinfo->value.enumerated.name,
2476 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2477 return 0;
2478}
2479
2480static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2481 struct snd_ctl_elem_value *ucontrol)
2482{
2483 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2484 hda_nid_t nid = kcontrol->private_value;
2485 unsigned int vref_caps = get_vref_caps(codec, nid);
2486 unsigned int idx;
2487
2488 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2489 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2490 return 0;
2491}
2492
2493static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2494 struct snd_ctl_elem_value *ucontrol)
2495{
2496 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2497 hda_nid_t nid = kcontrol->private_value;
2498 unsigned int vref_caps = get_vref_caps(codec, nid);
2499 unsigned int val, idx;
2500
2501 val = snd_hda_codec_get_pin_target(codec, nid);
2502 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2503 if (idx == ucontrol->value.enumerated.item[0])
2504 return 0;
2505
2506 val &= ~AC_PINCTL_VREFEN;
2507 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2508 snd_hda_set_pin_ctl_cache(codec, nid, val);
2509 return 1;
2510}
2511
2512static const struct snd_kcontrol_new in_jack_mode_enum = {
2513 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2514 .info = in_jack_mode_info,
2515 .get = in_jack_mode_get,
2516 .put = in_jack_mode_put,
2517};
2518
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002519static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2520{
2521 struct hda_gen_spec *spec = codec->spec;
2522 int nitems = 0;
2523 if (spec->add_in_jack_modes)
2524 nitems = hweight32(get_vref_caps(codec, pin));
2525 return nitems ? nitems : 1;
2526}
2527
Takashi Iwai294765582013-01-17 09:52:11 +01002528static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2529{
2530 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002531 struct snd_kcontrol_new *knew;
2532 char name[44];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002533 unsigned int defcfg;
2534
2535 if (pin == spec->hp_mic_pin) {
2536 if (!spec->add_out_jack_modes) {
2537 int ret = create_hp_mic_jack_mode(codec, pin);
2538 if (ret < 0)
2539 return ret;
2540 }
2541 return 0;
2542 }
Takashi Iwai294765582013-01-17 09:52:11 +01002543
2544 /* no jack mode for fixed pins */
2545 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2546 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2547 return 0;
2548
2549 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002550 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002551 return 0;
2552
2553 get_jack_mode_name(codec, pin, name, sizeof(name));
2554 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2555 if (!knew)
2556 return -ENOMEM;
2557 knew->private_value = pin;
2558 return 0;
2559}
2560
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002561/*
2562 * HP/mic shared jack mode
2563 */
2564static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2565 struct snd_ctl_elem_info *uinfo)
2566{
2567 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2568 hda_nid_t nid = kcontrol->private_value;
2569 int out_jacks = get_out_jack_num_items(codec, nid);
2570 int in_jacks = get_in_jack_num_items(codec, nid);
2571 const char *text = NULL;
2572 int idx;
2573
2574 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2575 uinfo->count = 1;
2576 uinfo->value.enumerated.items = out_jacks + in_jacks;
2577 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2578 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2579 idx = uinfo->value.enumerated.item;
2580 if (idx < out_jacks) {
2581 if (out_jacks > 1)
2582 text = out_jack_texts[idx];
2583 else
2584 text = "Headphone Out";
2585 } else {
2586 idx -= out_jacks;
2587 if (in_jacks > 1) {
2588 unsigned int vref_caps = get_vref_caps(codec, nid);
2589 text = vref_texts[get_vref_idx(vref_caps, idx)];
2590 } else
2591 text = "Mic In";
2592 }
2593
2594 strcpy(uinfo->value.enumerated.name, text);
2595 return 0;
2596}
2597
2598static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2599{
2600 int out_jacks = get_out_jack_num_items(codec, nid);
2601 int in_jacks = get_in_jack_num_items(codec, nid);
2602 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2603 int idx = 0;
2604
2605 if (val & PIN_OUT) {
2606 if (out_jacks > 1 && val == PIN_HP)
2607 idx = 1;
2608 } else if (val & PIN_IN) {
2609 idx = out_jacks;
2610 if (in_jacks > 1) {
2611 unsigned int vref_caps = get_vref_caps(codec, nid);
2612 val &= AC_PINCTL_VREFEN;
2613 idx += cvt_from_vref_idx(vref_caps, val);
2614 }
2615 }
2616 return idx;
2617}
2618
2619static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2620 struct snd_ctl_elem_value *ucontrol)
2621{
2622 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2623 hda_nid_t nid = kcontrol->private_value;
2624 ucontrol->value.enumerated.item[0] =
2625 get_cur_hp_mic_jack_mode(codec, nid);
2626 return 0;
2627}
2628
2629static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2630 struct snd_ctl_elem_value *ucontrol)
2631{
2632 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2633 hda_nid_t nid = kcontrol->private_value;
2634 int out_jacks = get_out_jack_num_items(codec, nid);
2635 int in_jacks = get_in_jack_num_items(codec, nid);
2636 unsigned int val, oldval, idx;
2637
2638 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2639 idx = ucontrol->value.enumerated.item[0];
2640 if (oldval == idx)
2641 return 0;
2642
2643 if (idx < out_jacks) {
2644 if (out_jacks > 1)
2645 val = idx ? PIN_HP : PIN_OUT;
2646 else
2647 val = PIN_HP;
2648 } else {
2649 idx -= out_jacks;
2650 if (in_jacks > 1) {
2651 unsigned int vref_caps = get_vref_caps(codec, nid);
2652 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002653 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2654 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002655 } else
2656 val = snd_hda_get_default_vref(codec, nid);
2657 }
2658 snd_hda_set_pin_ctl_cache(codec, nid, val);
2659 return 1;
2660}
2661
2662static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2663 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2664 .info = hp_mic_jack_mode_info,
2665 .get = hp_mic_jack_mode_get,
2666 .put = hp_mic_jack_mode_put,
2667};
2668
2669static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2670{
2671 struct hda_gen_spec *spec = codec->spec;
2672 struct snd_kcontrol_new *knew;
2673
2674 if (get_out_jack_num_items(codec, pin) <= 1 &&
2675 get_in_jack_num_items(codec, pin) <= 1)
2676 return 0; /* no need */
2677 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2678 &hp_mic_jack_mode_enum);
2679 if (!knew)
2680 return -ENOMEM;
2681 knew->private_value = pin;
2682 return 0;
2683}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002684
2685/*
2686 * Parse input paths
2687 */
2688
Takashi Iwai352f7f92012-12-19 12:52:06 +01002689/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002690static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002691{
2692 struct hda_amp_list *list;
2693
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002694 list = snd_array_new(&spec->loopback_list);
2695 if (!list)
2696 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002697 list->nid = mix;
2698 list->dir = HDA_INPUT;
2699 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002700 spec->loopback.amplist = spec->loopback_list.list;
2701 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002702}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002703
Takashi Iwai352f7f92012-12-19 12:52:06 +01002704/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002705static int new_analog_input(struct hda_codec *codec, int input_idx,
2706 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002707 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002709 struct hda_gen_spec *spec = codec->spec;
2710 struct nid_path *path;
2711 unsigned int val;
2712 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Takashi Iwai352f7f92012-12-19 12:52:06 +01002714 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2715 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2716 return 0; /* no need for analog loopback */
2717
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002718 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002719 if (!path)
2720 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002721 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002722 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002723
2724 idx = path->idx[path->depth - 1];
2725 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2726 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2727 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002728 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002730 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
2732
Takashi Iwai352f7f92012-12-19 12:52:06 +01002733 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2734 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2735 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002736 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002738 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 }
2740
Takashi Iwai352f7f92012-12-19 12:52:06 +01002741 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002742 err = add_loopback_list(spec, mix_nid, idx);
2743 if (err < 0)
2744 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002745
2746 if (spec->mixer_nid != spec->mixer_merge_nid &&
2747 !spec->loopback_merge_path) {
2748 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2749 spec->mixer_merge_nid, 0);
2750 if (path) {
2751 print_nid_path("loopback-merge", path);
2752 path->active = true;
2753 spec->loopback_merge_path =
2754 snd_hda_get_path_idx(codec, path);
2755 }
2756 }
2757
Takashi Iwai352f7f92012-12-19 12:52:06 +01002758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
2760
Takashi Iwai352f7f92012-12-19 12:52:06 +01002761static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002763 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2764 return (pincap & AC_PINCAP_IN) != 0;
2765}
2766
2767/* Parse the codec tree and retrieve ADCs */
2768static int fill_adc_nids(struct hda_codec *codec)
2769{
2770 struct hda_gen_spec *spec = codec->spec;
2771 hda_nid_t nid;
2772 hda_nid_t *adc_nids = spec->adc_nids;
2773 int max_nums = ARRAY_SIZE(spec->adc_nids);
2774 int i, nums = 0;
2775
2776 nid = codec->start_nid;
2777 for (i = 0; i < codec->num_nodes; i++, nid++) {
2778 unsigned int caps = get_wcaps(codec, nid);
2779 int type = get_wcaps_type(caps);
2780
2781 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2782 continue;
2783 adc_nids[nums] = nid;
2784 if (++nums >= max_nums)
2785 break;
2786 }
2787 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002788
2789 /* copy the detected ADCs to all_adcs[] */
2790 spec->num_all_adcs = nums;
2791 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2792
Takashi Iwai352f7f92012-12-19 12:52:06 +01002793 return nums;
2794}
2795
2796/* filter out invalid adc_nids that don't give all active input pins;
2797 * if needed, check whether dynamic ADC-switching is available
2798 */
2799static int check_dyn_adc_switch(struct hda_codec *codec)
2800{
2801 struct hda_gen_spec *spec = codec->spec;
2802 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002803 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002804 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002805
Takashi Iwai352f7f92012-12-19 12:52:06 +01002806 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002807 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002808 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002809 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002810 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002811 break;
2812 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002813 if (i >= imux->num_items) {
2814 ok_bits |= (1 << n);
2815 nums++;
2816 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002817 }
2818
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002819 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002820 /* check whether ADC-switch is possible */
2821 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002822 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002823 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002824 spec->dyn_adc_idx[i] = n;
2825 break;
2826 }
2827 }
2828 }
2829
2830 snd_printdd("hda-codec: enabling ADC switching\n");
2831 spec->dyn_adc_switch = 1;
2832 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002833 /* shrink the invalid adcs and input paths */
2834 nums = 0;
2835 for (n = 0; n < spec->num_adc_nids; n++) {
2836 if (!(ok_bits & (1 << n)))
2837 continue;
2838 if (n != nums) {
2839 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002840 for (i = 0; i < imux->num_items; i++) {
2841 invalidate_nid_path(codec,
2842 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002843 spec->input_paths[i][nums] =
2844 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002845 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002846 }
2847 nums++;
2848 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002849 spec->num_adc_nids = nums;
2850 }
2851
Takashi Iwai967303d2013-02-19 17:12:42 +01002852 if (imux->num_items == 1 ||
2853 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002854 snd_printdd("hda-codec: reducing to a single ADC\n");
2855 spec->num_adc_nids = 1; /* reduce to a single ADC */
2856 }
2857
2858 /* single index for individual volumes ctls */
2859 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2860 spec->num_adc_nids = 1;
2861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 return 0;
2863}
2864
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002865/* parse capture source paths from the given pin and create imux items */
2866static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002867 int cfg_idx, int num_adcs,
2868 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002869{
2870 struct hda_gen_spec *spec = codec->spec;
2871 struct hda_input_mux *imux = &spec->input_mux;
2872 int imux_idx = imux->num_items;
2873 bool imux_added = false;
2874 int c;
2875
2876 for (c = 0; c < num_adcs; c++) {
2877 struct nid_path *path;
2878 hda_nid_t adc = spec->adc_nids[c];
2879
2880 if (!is_reachable_path(codec, pin, adc))
2881 continue;
2882 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2883 if (!path)
2884 continue;
2885 print_nid_path("input", path);
2886 spec->input_paths[imux_idx][c] =
2887 snd_hda_get_path_idx(codec, path);
2888
2889 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01002890 if (spec->hp_mic_pin == pin)
2891 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002892 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002893 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002894 imux_added = true;
2895 }
2896 }
2897
2898 return 0;
2899}
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002902 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002904
Takashi Iwaic9700422013-01-18 10:17:30 +01002905/* fill the label for each input at first */
2906static int fill_input_pin_labels(struct hda_codec *codec)
2907{
2908 struct hda_gen_spec *spec = codec->spec;
2909 const struct auto_pin_cfg *cfg = &spec->autocfg;
2910 int i;
2911
2912 for (i = 0; i < cfg->num_inputs; i++) {
2913 hda_nid_t pin = cfg->inputs[i].pin;
2914 const char *label;
2915 int j, idx;
2916
2917 if (!is_input_pin(codec, pin))
2918 continue;
2919
2920 label = hda_get_autocfg_input_label(codec, cfg, i);
2921 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002922 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002923 if (spec->input_labels[j] &&
2924 !strcmp(spec->input_labels[j], label)) {
2925 idx = spec->input_label_idxs[j] + 1;
2926 break;
2927 }
2928 }
2929
2930 spec->input_labels[i] = label;
2931 spec->input_label_idxs[i] = idx;
2932 }
2933
2934 return 0;
2935}
2936
Takashi Iwai9dba2052013-01-18 10:01:15 +01002937#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2938
Takashi Iwai352f7f92012-12-19 12:52:06 +01002939static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002940{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002941 struct hda_gen_spec *spec = codec->spec;
2942 const struct auto_pin_cfg *cfg = &spec->autocfg;
2943 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002944 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002945 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002946 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002947
Takashi Iwai352f7f92012-12-19 12:52:06 +01002948 num_adcs = fill_adc_nids(codec);
2949 if (num_adcs < 0)
2950 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002951
Takashi Iwaic9700422013-01-18 10:17:30 +01002952 err = fill_input_pin_labels(codec);
2953 if (err < 0)
2954 return err;
2955
Takashi Iwai352f7f92012-12-19 12:52:06 +01002956 for (i = 0; i < cfg->num_inputs; i++) {
2957 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Takashi Iwai352f7f92012-12-19 12:52:06 +01002959 pin = cfg->inputs[i].pin;
2960 if (!is_input_pin(codec, pin))
2961 continue;
2962
Takashi Iwai2c12c302013-01-10 09:33:29 +01002963 val = PIN_IN;
2964 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2965 val |= snd_hda_get_default_vref(codec, pin);
2966 set_pin_target(codec, pin, val, false);
2967
Takashi Iwai352f7f92012-12-19 12:52:06 +01002968 if (mixer) {
2969 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002970 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002971 spec->input_labels[i],
2972 spec->input_label_idxs[i],
2973 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002974 if (err < 0)
2975 return err;
2976 }
2977 }
2978
Takashi Iwaic9700422013-01-18 10:17:30 +01002979 err = parse_capture_source(codec, pin, i, num_adcs,
2980 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002981 if (err < 0)
2982 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002983
2984 if (spec->add_in_jack_modes) {
2985 err = create_in_jack_mode(codec, pin);
2986 if (err < 0)
2987 return err;
2988 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002989 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002990
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002991 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002992 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002993 "Stereo Mix", 0);
2994 if (err < 0)
2995 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002996 }
2997
2998 return 0;
2999}
3000
3001
3002/*
3003 * input source mux
3004 */
3005
Takashi Iwaic697b712013-01-07 17:09:26 +01003006/* get the input path specified by the given adc and imux indices */
3007static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003008{
3009 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003010 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3011 snd_BUG();
3012 return NULL;
3013 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003014 if (spec->dyn_adc_switch)
3015 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003016 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003017 snd_BUG();
3018 return NULL;
3019 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003020 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003021}
3022
3023static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3024 unsigned int idx);
3025
3026static int mux_enum_info(struct snd_kcontrol *kcontrol,
3027 struct snd_ctl_elem_info *uinfo)
3028{
3029 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3030 struct hda_gen_spec *spec = codec->spec;
3031 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3032}
3033
3034static int mux_enum_get(struct snd_kcontrol *kcontrol,
3035 struct snd_ctl_elem_value *ucontrol)
3036{
3037 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3038 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003039 /* the ctls are created at once with multiple counts */
3040 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003041
3042 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3043 return 0;
3044}
3045
3046static int mux_enum_put(struct snd_kcontrol *kcontrol,
3047 struct snd_ctl_elem_value *ucontrol)
3048{
3049 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003050 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003051 return mux_select(codec, adc_idx,
3052 ucontrol->value.enumerated.item[0]);
3053}
3054
Takashi Iwai352f7f92012-12-19 12:52:06 +01003055static const struct snd_kcontrol_new cap_src_temp = {
3056 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3057 .name = "Input Source",
3058 .info = mux_enum_info,
3059 .get = mux_enum_get,
3060 .put = mux_enum_put,
3061};
3062
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003063/*
3064 * capture volume and capture switch ctls
3065 */
3066
Takashi Iwai352f7f92012-12-19 12:52:06 +01003067typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3068 struct snd_ctl_elem_value *ucontrol);
3069
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003070/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003071static int cap_put_caller(struct snd_kcontrol *kcontrol,
3072 struct snd_ctl_elem_value *ucontrol,
3073 put_call_t func, int type)
3074{
3075 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3076 struct hda_gen_spec *spec = codec->spec;
3077 const struct hda_input_mux *imux;
3078 struct nid_path *path;
3079 int i, adc_idx, err = 0;
3080
3081 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003082 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003083 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003084 /* we use the cache-only update at first since multiple input paths
3085 * may shared the same amp; by updating only caches, the redundant
3086 * writes to hardware can be reduced.
3087 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003088 codec->cached_write = 1;
3089 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003090 path = get_input_path(codec, adc_idx, i);
3091 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003092 continue;
3093 kcontrol->private_value = path->ctls[type];
3094 err = func(kcontrol, ucontrol);
3095 if (err < 0)
3096 goto error;
3097 }
3098 error:
3099 codec->cached_write = 0;
3100 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003101 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003102 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003103 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003104 return err;
3105}
3106
3107/* capture volume ctl callbacks */
3108#define cap_vol_info snd_hda_mixer_amp_volume_info
3109#define cap_vol_get snd_hda_mixer_amp_volume_get
3110#define cap_vol_tlv snd_hda_mixer_amp_tlv
3111
3112static int cap_vol_put(struct snd_kcontrol *kcontrol,
3113 struct snd_ctl_elem_value *ucontrol)
3114{
3115 return cap_put_caller(kcontrol, ucontrol,
3116 snd_hda_mixer_amp_volume_put,
3117 NID_PATH_VOL_CTL);
3118}
3119
3120static const struct snd_kcontrol_new cap_vol_temp = {
3121 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3122 .name = "Capture Volume",
3123 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3124 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3125 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3126 .info = cap_vol_info,
3127 .get = cap_vol_get,
3128 .put = cap_vol_put,
3129 .tlv = { .c = cap_vol_tlv },
3130};
3131
3132/* capture switch ctl callbacks */
3133#define cap_sw_info snd_ctl_boolean_stereo_info
3134#define cap_sw_get snd_hda_mixer_amp_switch_get
3135
3136static int cap_sw_put(struct snd_kcontrol *kcontrol,
3137 struct snd_ctl_elem_value *ucontrol)
3138{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003139 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003140 snd_hda_mixer_amp_switch_put,
3141 NID_PATH_MUTE_CTL);
3142}
3143
3144static const struct snd_kcontrol_new cap_sw_temp = {
3145 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3146 .name = "Capture Switch",
3147 .info = cap_sw_info,
3148 .get = cap_sw_get,
3149 .put = cap_sw_put,
3150};
3151
3152static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3153{
3154 hda_nid_t nid;
3155 int i, depth;
3156
3157 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3158 for (depth = 0; depth < 3; depth++) {
3159 if (depth >= path->depth)
3160 return -EINVAL;
3161 i = path->depth - depth - 1;
3162 nid = path->path[i];
3163 if (!path->ctls[NID_PATH_VOL_CTL]) {
3164 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3165 path->ctls[NID_PATH_VOL_CTL] =
3166 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3167 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3168 int idx = path->idx[i];
3169 if (!depth && codec->single_adc_amp)
3170 idx = 0;
3171 path->ctls[NID_PATH_VOL_CTL] =
3172 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3173 }
3174 }
3175 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3176 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3177 path->ctls[NID_PATH_MUTE_CTL] =
3178 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3179 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3180 int idx = path->idx[i];
3181 if (!depth && codec->single_adc_amp)
3182 idx = 0;
3183 path->ctls[NID_PATH_MUTE_CTL] =
3184 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3185 }
3186 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 return 0;
3189}
3190
Takashi Iwai352f7f92012-12-19 12:52:06 +01003191static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003193 struct hda_gen_spec *spec = codec->spec;
3194 struct auto_pin_cfg *cfg = &spec->autocfg;
3195 unsigned int val;
3196 int i;
3197
3198 if (!spec->inv_dmic_split)
3199 return false;
3200 for (i = 0; i < cfg->num_inputs; i++) {
3201 if (cfg->inputs[i].pin != nid)
3202 continue;
3203 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3204 return false;
3205 val = snd_hda_codec_get_pincfg(codec, nid);
3206 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3207 }
3208 return false;
3209}
3210
Takashi Iwaia90229e2013-01-18 14:10:00 +01003211/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003212static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3213 struct snd_ctl_elem_value *ucontrol)
3214{
3215 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3216 struct hda_gen_spec *spec = codec->spec;
3217 int ret;
3218
3219 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3220 if (ret < 0)
3221 return ret;
3222
Takashi Iwaia90229e2013-01-18 14:10:00 +01003223 if (spec->cap_sync_hook)
3224 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003225
3226 return ret;
3227}
3228
Takashi Iwai352f7f92012-12-19 12:52:06 +01003229static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3230 int idx, bool is_switch, unsigned int ctl,
3231 bool inv_dmic)
3232{
3233 struct hda_gen_spec *spec = codec->spec;
3234 char tmpname[44];
3235 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3236 const char *sfx = is_switch ? "Switch" : "Volume";
3237 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003238 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003239
3240 if (!ctl)
3241 return 0;
3242
3243 if (label)
3244 snprintf(tmpname, sizeof(tmpname),
3245 "%s Capture %s", label, sfx);
3246 else
3247 snprintf(tmpname, sizeof(tmpname),
3248 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003249 knew = add_control(spec, type, tmpname, idx,
3250 amp_val_replace_channels(ctl, chs));
3251 if (!knew)
3252 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003253 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003254 knew->put = cap_single_sw_put;
3255 if (!inv_dmic)
3256 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003257
3258 /* Make independent right kcontrol */
3259 if (label)
3260 snprintf(tmpname, sizeof(tmpname),
3261 "Inverted %s Capture %s", label, sfx);
3262 else
3263 snprintf(tmpname, sizeof(tmpname),
3264 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003265 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003266 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003267 if (!knew)
3268 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003269 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003270 knew->put = cap_single_sw_put;
3271 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003272}
3273
3274/* create single (and simple) capture volume and switch controls */
3275static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3276 unsigned int vol_ctl, unsigned int sw_ctl,
3277 bool inv_dmic)
3278{
3279 int err;
3280 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3281 if (err < 0)
3282 return err;
3283 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3284 if (err < 0)
3285 return err;
3286 return 0;
3287}
3288
3289/* create bound capture volume and switch controls */
3290static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3291 unsigned int vol_ctl, unsigned int sw_ctl)
3292{
3293 struct hda_gen_spec *spec = codec->spec;
3294 struct snd_kcontrol_new *knew;
3295
3296 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003297 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003298 if (!knew)
3299 return -ENOMEM;
3300 knew->index = idx;
3301 knew->private_value = vol_ctl;
3302 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3303 }
3304 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003305 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003306 if (!knew)
3307 return -ENOMEM;
3308 knew->index = idx;
3309 knew->private_value = sw_ctl;
3310 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3311 }
3312 return 0;
3313}
3314
3315/* return the vol ctl when used first in the imux list */
3316static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3317{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003318 struct nid_path *path;
3319 unsigned int ctl;
3320 int i;
3321
Takashi Iwaic697b712013-01-07 17:09:26 +01003322 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003323 if (!path)
3324 return 0;
3325 ctl = path->ctls[type];
3326 if (!ctl)
3327 return 0;
3328 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003329 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003330 if (path && path->ctls[type] == ctl)
3331 return 0;
3332 }
3333 return ctl;
3334}
3335
3336/* create individual capture volume and switch controls per input */
3337static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3338{
3339 struct hda_gen_spec *spec = codec->spec;
3340 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003341 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003342
3343 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003344 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003345 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003346
Takashi Iwaic9700422013-01-18 10:17:30 +01003347 idx = imux->items[i].index;
3348 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003349 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003350 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3351
3352 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003353 err = add_single_cap_ctl(codec,
3354 spec->input_labels[idx],
3355 spec->input_label_idxs[idx],
3356 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003357 get_first_cap_ctl(codec, i, type),
3358 inv_dmic);
3359 if (err < 0)
3360 return err;
3361 }
3362 }
3363 return 0;
3364}
3365
3366static int create_capture_mixers(struct hda_codec *codec)
3367{
3368 struct hda_gen_spec *spec = codec->spec;
3369 struct hda_input_mux *imux = &spec->input_mux;
3370 int i, n, nums, err;
3371
3372 if (spec->dyn_adc_switch)
3373 nums = 1;
3374 else
3375 nums = spec->num_adc_nids;
3376
3377 if (!spec->auto_mic && imux->num_items > 1) {
3378 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003379 const char *name;
3380 name = nums > 1 ? "Input Source" : "Capture Source";
3381 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003382 if (!knew)
3383 return -ENOMEM;
3384 knew->count = nums;
3385 }
3386
3387 for (n = 0; n < nums; n++) {
3388 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003389 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003390 bool inv_dmic = false;
3391 int vol, sw;
3392
3393 vol = sw = 0;
3394 for (i = 0; i < imux->num_items; i++) {
3395 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003396 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003397 if (!path)
3398 continue;
3399 parse_capvol_in_path(codec, path);
3400 if (!vol)
3401 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003402 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003403 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003404 if (!same_amp_caps(codec, vol,
3405 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3406 multi_cap_vol = true;
3407 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003408 if (!sw)
3409 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003410 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003411 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003412 if (!same_amp_caps(codec, sw,
3413 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3414 multi_cap_vol = true;
3415 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003416 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3417 inv_dmic = true;
3418 }
3419
3420 if (!multi)
3421 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3422 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003423 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003424 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3425 else
3426 err = create_multi_cap_vol_ctl(codec);
3427 if (err < 0)
3428 return err;
3429 }
3430
3431 return 0;
3432}
3433
3434/*
3435 * add mic boosts if needed
3436 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003437
3438/* check whether the given amp is feasible as a boost volume */
3439static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3440 int dir, int idx)
3441{
3442 unsigned int step;
3443
3444 if (!nid_has_volume(codec, nid, dir) ||
3445 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3446 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3447 return false;
3448
3449 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3450 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3451 if (step < 0x20)
3452 return false;
3453 return true;
3454}
3455
3456/* look for a boost amp in a widget close to the pin */
3457static unsigned int look_for_boost_amp(struct hda_codec *codec,
3458 struct nid_path *path)
3459{
3460 unsigned int val = 0;
3461 hda_nid_t nid;
3462 int depth;
3463
3464 for (depth = 0; depth < 3; depth++) {
3465 if (depth >= path->depth - 1)
3466 break;
3467 nid = path->path[depth];
3468 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3469 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3470 break;
3471 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3472 path->idx[depth])) {
3473 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3474 HDA_INPUT);
3475 break;
3476 }
3477 }
3478
3479 return val;
3480}
3481
Takashi Iwai352f7f92012-12-19 12:52:06 +01003482static int parse_mic_boost(struct hda_codec *codec)
3483{
3484 struct hda_gen_spec *spec = codec->spec;
3485 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003486 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003487 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003488
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003489 if (!spec->num_adc_nids)
3490 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003491
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003492 for (i = 0; i < imux->num_items; i++) {
3493 struct nid_path *path;
3494 unsigned int val;
3495 int idx;
3496 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003497
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003498 idx = imux->items[i].index;
3499 if (idx >= imux->num_items)
3500 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003501
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003502 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003503 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003504 continue;
3505
3506 path = get_input_path(codec, 0, i);
3507 if (!path)
3508 continue;
3509
3510 val = look_for_boost_amp(codec, path);
3511 if (!val)
3512 continue;
3513
3514 /* create a boost control */
3515 snprintf(boost_label, sizeof(boost_label),
3516 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003517 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3518 spec->input_label_idxs[idx], val))
3519 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003520
3521 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003522 }
3523 return 0;
3524}
3525
3526/*
3527 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3528 */
3529static void parse_digital(struct hda_codec *codec)
3530{
3531 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003532 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003533 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003534 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003535
3536 /* support multiple SPDIFs; the secondary is set up as a slave */
3537 nums = 0;
3538 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003539 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003540 dig_nid = look_for_dac(codec, pin, true);
3541 if (!dig_nid)
3542 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003543 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003544 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003545 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003546 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003547 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003548 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003549 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003550 if (!nums) {
3551 spec->multiout.dig_out_nid = dig_nid;
3552 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3553 } else {
3554 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3555 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3556 break;
3557 spec->slave_dig_outs[nums - 1] = dig_nid;
3558 }
3559 nums++;
3560 }
3561
3562 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003563 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003564 dig_nid = codec->start_nid;
3565 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003566 unsigned int wcaps = get_wcaps(codec, dig_nid);
3567 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3568 continue;
3569 if (!(wcaps & AC_WCAP_DIGITAL))
3570 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003571 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003572 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003573 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003574 path->active = true;
3575 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003576 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003577 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003578 break;
3579 }
3580 }
3581 }
3582}
3583
3584
3585/*
3586 * input MUX handling
3587 */
3588
3589static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3590
3591/* select the given imux item; either unmute exclusively or select the route */
3592static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3593 unsigned int idx)
3594{
3595 struct hda_gen_spec *spec = codec->spec;
3596 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003597 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003598
3599 imux = &spec->input_mux;
3600 if (!imux->num_items)
3601 return 0;
3602
3603 if (idx >= imux->num_items)
3604 idx = imux->num_items - 1;
3605 if (spec->cur_mux[adc_idx] == idx)
3606 return 0;
3607
Takashi Iwai55196ff2013-01-24 17:32:56 +01003608 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3609 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003610 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003611 if (old_path->active)
3612 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003613
3614 spec->cur_mux[adc_idx] = idx;
3615
Takashi Iwai967303d2013-02-19 17:12:42 +01003616 if (spec->hp_mic)
3617 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003618
3619 if (spec->dyn_adc_switch)
3620 dyn_adc_pcm_resetup(codec, idx);
3621
Takashi Iwaic697b712013-01-07 17:09:26 +01003622 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003623 if (!path)
3624 return 0;
3625 if (path->active)
3626 return 0;
3627 snd_hda_activate_path(codec, path, true, false);
3628 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003629 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003630 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003631 return 1;
3632}
3633
3634
3635/*
3636 * Jack detections for HP auto-mute and mic-switch
3637 */
3638
3639/* check each pin in the given array; returns true if any of them is plugged */
3640static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3641{
3642 int i, present = 0;
3643
3644 for (i = 0; i < num_pins; i++) {
3645 hda_nid_t nid = pins[i];
3646 if (!nid)
3647 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003648 /* don't detect pins retasked as inputs */
3649 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3650 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003651 present |= snd_hda_jack_detect(codec, nid);
3652 }
3653 return present;
3654}
3655
3656/* standard HP/line-out auto-mute helper */
3657static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003658 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003659{
3660 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003661 int i;
3662
3663 for (i = 0; i < num_pins; i++) {
3664 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003665 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003666 if (!nid)
3667 break;
Takashi Iwai967303d2013-02-19 17:12:42 +01003668 oldval = snd_hda_codec_get_pin_target(codec, nid);
3669 if (oldval & PIN_IN)
3670 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003671 /* don't reset VREF value in case it's controlling
3672 * the amp (see alc861_fixup_asus_amp_vref_0f())
3673 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003674 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003675 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003676 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003677 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003678 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003679 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003680 /* here we call update_pin_ctl() so that the pinctl is changed
3681 * without changing the pinctl target value;
3682 * the original target value will be still referred at the
3683 * init / resume again
3684 */
3685 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003686 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003687 }
3688}
3689
3690/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003691void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003692{
3693 struct hda_gen_spec *spec = codec->spec;
3694 int on;
3695
3696 /* Control HP pins/amps depending on master_mute state;
3697 * in general, HP pins/amps control should be enabled in all cases,
3698 * but currently set only for master_mute, just to be safe
3699 */
Takashi Iwai967303d2013-02-19 17:12:42 +01003700 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003701 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003702
3703 if (!spec->automute_speaker)
3704 on = 0;
3705 else
3706 on = spec->hp_jack_present | spec->line_jack_present;
3707 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003708 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003709 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003710 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003711
3712 /* toggle line-out mutes if needed, too */
3713 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3714 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3715 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3716 return;
3717 if (!spec->automute_lo)
3718 on = 0;
3719 else
3720 on = spec->hp_jack_present;
3721 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003722 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003723 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003724 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003725}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003726EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003727
3728static void call_update_outputs(struct hda_codec *codec)
3729{
3730 struct hda_gen_spec *spec = codec->spec;
3731 if (spec->automute_hook)
3732 spec->automute_hook(codec);
3733 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003734 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003735}
3736
3737/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003738void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003739{
3740 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003741 hda_nid_t *pins = spec->autocfg.hp_pins;
3742 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003743
Takashi Iwai92603c52013-01-22 07:46:31 +01003744 /* No detection for the first HP jack during indep-HP mode */
3745 if (spec->indep_hp_enabled) {
3746 pins++;
3747 num_pins--;
3748 }
3749
3750 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003751 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3752 return;
3753 call_update_outputs(codec);
3754}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003755EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003756
3757/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003758void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003759{
3760 struct hda_gen_spec *spec = codec->spec;
3761
3762 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3763 return;
3764 /* check LO jack only when it's different from HP */
3765 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3766 return;
3767
3768 spec->line_jack_present =
3769 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3770 spec->autocfg.line_out_pins);
3771 if (!spec->automute_speaker || !spec->detect_lo)
3772 return;
3773 call_update_outputs(codec);
3774}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003775EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003776
3777/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003778void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003779{
3780 struct hda_gen_spec *spec = codec->spec;
3781 int i;
3782
3783 if (!spec->auto_mic)
3784 return;
3785
3786 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003787 hda_nid_t pin = spec->am_entry[i].pin;
3788 /* don't detect pins retasked as outputs */
3789 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3790 continue;
3791 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003792 mux_select(codec, 0, spec->am_entry[i].idx);
3793 return;
3794 }
3795 }
3796 mux_select(codec, 0, spec->am_entry[0].idx);
3797}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003798EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003799
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003800/* update jack retasking */
3801static void update_automute_all(struct hda_codec *codec)
3802{
3803 struct hda_gen_spec *spec = codec->spec;
3804
3805 if (spec->hp_automute_hook)
3806 spec->hp_automute_hook(codec, NULL);
3807 else
3808 snd_hda_gen_hp_automute(codec, NULL);
3809 if (spec->line_automute_hook)
3810 spec->line_automute_hook(codec, NULL);
3811 else
3812 snd_hda_gen_line_automute(codec, NULL);
3813 if (spec->mic_autoswitch_hook)
3814 spec->mic_autoswitch_hook(codec, NULL);
3815 else
3816 snd_hda_gen_mic_autoswitch(codec, NULL);
3817}
3818
Takashi Iwai352f7f92012-12-19 12:52:06 +01003819/*
3820 * Auto-Mute mode mixer enum support
3821 */
3822static int automute_mode_info(struct snd_kcontrol *kcontrol,
3823 struct snd_ctl_elem_info *uinfo)
3824{
3825 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3826 struct hda_gen_spec *spec = codec->spec;
3827 static const char * const texts3[] = {
3828 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003829 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
Takashi Iwai352f7f92012-12-19 12:52:06 +01003831 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3832 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3833 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3834}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Takashi Iwai352f7f92012-12-19 12:52:06 +01003836static int automute_mode_get(struct snd_kcontrol *kcontrol,
3837 struct snd_ctl_elem_value *ucontrol)
3838{
3839 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3840 struct hda_gen_spec *spec = codec->spec;
3841 unsigned int val = 0;
3842 if (spec->automute_speaker)
3843 val++;
3844 if (spec->automute_lo)
3845 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003846
Takashi Iwai352f7f92012-12-19 12:52:06 +01003847 ucontrol->value.enumerated.item[0] = val;
3848 return 0;
3849}
3850
3851static int automute_mode_put(struct snd_kcontrol *kcontrol,
3852 struct snd_ctl_elem_value *ucontrol)
3853{
3854 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3855 struct hda_gen_spec *spec = codec->spec;
3856
3857 switch (ucontrol->value.enumerated.item[0]) {
3858 case 0:
3859 if (!spec->automute_speaker && !spec->automute_lo)
3860 return 0;
3861 spec->automute_speaker = 0;
3862 spec->automute_lo = 0;
3863 break;
3864 case 1:
3865 if (spec->automute_speaker_possible) {
3866 if (!spec->automute_lo && spec->automute_speaker)
3867 return 0;
3868 spec->automute_speaker = 1;
3869 spec->automute_lo = 0;
3870 } else if (spec->automute_lo_possible) {
3871 if (spec->automute_lo)
3872 return 0;
3873 spec->automute_lo = 1;
3874 } else
3875 return -EINVAL;
3876 break;
3877 case 2:
3878 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3879 return -EINVAL;
3880 if (spec->automute_speaker && spec->automute_lo)
3881 return 0;
3882 spec->automute_speaker = 1;
3883 spec->automute_lo = 1;
3884 break;
3885 default:
3886 return -EINVAL;
3887 }
3888 call_update_outputs(codec);
3889 return 1;
3890}
3891
3892static const struct snd_kcontrol_new automute_mode_enum = {
3893 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3894 .name = "Auto-Mute Mode",
3895 .info = automute_mode_info,
3896 .get = automute_mode_get,
3897 .put = automute_mode_put,
3898};
3899
3900static int add_automute_mode_enum(struct hda_codec *codec)
3901{
3902 struct hda_gen_spec *spec = codec->spec;
3903
Takashi Iwai12c93df2012-12-19 14:38:33 +01003904 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003905 return -ENOMEM;
3906 return 0;
3907}
3908
3909/*
3910 * Check the availability of HP/line-out auto-mute;
3911 * Set up appropriately if really supported
3912 */
3913static int check_auto_mute_availability(struct hda_codec *codec)
3914{
3915 struct hda_gen_spec *spec = codec->spec;
3916 struct auto_pin_cfg *cfg = &spec->autocfg;
3917 int present = 0;
3918 int i, err;
3919
Takashi Iwaif72706b2013-01-16 18:20:07 +01003920 if (spec->suppress_auto_mute)
3921 return 0;
3922
Takashi Iwai352f7f92012-12-19 12:52:06 +01003923 if (cfg->hp_pins[0])
3924 present++;
3925 if (cfg->line_out_pins[0])
3926 present++;
3927 if (cfg->speaker_pins[0])
3928 present++;
3929 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003930 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003931
3932 if (!cfg->speaker_pins[0] &&
3933 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3934 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3935 sizeof(cfg->speaker_pins));
3936 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
Takashi Iwai352f7f92012-12-19 12:52:06 +01003939 if (!cfg->hp_pins[0] &&
3940 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3941 memcpy(cfg->hp_pins, cfg->line_out_pins,
3942 sizeof(cfg->hp_pins));
3943 cfg->hp_outs = cfg->line_outs;
3944 }
3945
3946 for (i = 0; i < cfg->hp_outs; i++) {
3947 hda_nid_t nid = cfg->hp_pins[i];
3948 if (!is_jack_detectable(codec, nid))
3949 continue;
3950 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3951 nid);
3952 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003953 spec->hp_automute_hook ?
3954 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003955 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003956 spec->detect_hp = 1;
3957 }
3958
3959 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3960 if (cfg->speaker_outs)
3961 for (i = 0; i < cfg->line_outs; i++) {
3962 hda_nid_t nid = cfg->line_out_pins[i];
3963 if (!is_jack_detectable(codec, nid))
3964 continue;
3965 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3966 snd_hda_jack_detect_enable_callback(codec, nid,
3967 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003968 spec->line_automute_hook ?
3969 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003970 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003971 spec->detect_lo = 1;
3972 }
3973 spec->automute_lo_possible = spec->detect_hp;
3974 }
3975
3976 spec->automute_speaker_possible = cfg->speaker_outs &&
3977 (spec->detect_hp || spec->detect_lo);
3978
3979 spec->automute_lo = spec->automute_lo_possible;
3980 spec->automute_speaker = spec->automute_speaker_possible;
3981
3982 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3983 /* create a control for automute mode */
3984 err = add_automute_mode_enum(codec);
3985 if (err < 0)
3986 return err;
3987 }
3988 return 0;
3989}
3990
Takashi Iwai352f7f92012-12-19 12:52:06 +01003991/* check whether all auto-mic pins are valid; setup indices if OK */
3992static bool auto_mic_check_imux(struct hda_codec *codec)
3993{
3994 struct hda_gen_spec *spec = codec->spec;
3995 const struct hda_input_mux *imux;
3996 int i;
3997
3998 imux = &spec->input_mux;
3999 for (i = 0; i < spec->am_num_entries; i++) {
4000 spec->am_entry[i].idx =
4001 find_idx_in_nid_list(spec->am_entry[i].pin,
4002 spec->imux_pins, imux->num_items);
4003 if (spec->am_entry[i].idx < 0)
4004 return false; /* no corresponding imux */
4005 }
4006
4007 /* we don't need the jack detection for the first pin */
4008 for (i = 1; i < spec->am_num_entries; i++)
4009 snd_hda_jack_detect_enable_callback(codec,
4010 spec->am_entry[i].pin,
4011 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004012 spec->mic_autoswitch_hook ?
4013 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004014 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004015 return true;
4016}
4017
4018static int compare_attr(const void *ap, const void *bp)
4019{
4020 const struct automic_entry *a = ap;
4021 const struct automic_entry *b = bp;
4022 return (int)(a->attr - b->attr);
4023}
4024
4025/*
4026 * Check the availability of auto-mic switch;
4027 * Set up if really supported
4028 */
4029static int check_auto_mic_availability(struct hda_codec *codec)
4030{
4031 struct hda_gen_spec *spec = codec->spec;
4032 struct auto_pin_cfg *cfg = &spec->autocfg;
4033 unsigned int types;
4034 int i, num_pins;
4035
Takashi Iwaid12daf62013-01-07 16:32:11 +01004036 if (spec->suppress_auto_mic)
4037 return 0;
4038
Takashi Iwai352f7f92012-12-19 12:52:06 +01004039 types = 0;
4040 num_pins = 0;
4041 for (i = 0; i < cfg->num_inputs; i++) {
4042 hda_nid_t nid = cfg->inputs[i].pin;
4043 unsigned int attr;
4044 attr = snd_hda_codec_get_pincfg(codec, nid);
4045 attr = snd_hda_get_input_pin_attr(attr);
4046 if (types & (1 << attr))
4047 return 0; /* already occupied */
4048 switch (attr) {
4049 case INPUT_PIN_ATTR_INT:
4050 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4051 return 0; /* invalid type */
4052 break;
4053 case INPUT_PIN_ATTR_UNUSED:
4054 return 0; /* invalid entry */
4055 default:
4056 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4057 return 0; /* invalid type */
4058 if (!spec->line_in_auto_switch &&
4059 cfg->inputs[i].type != AUTO_PIN_MIC)
4060 return 0; /* only mic is allowed */
4061 if (!is_jack_detectable(codec, nid))
4062 return 0; /* no unsol support */
4063 break;
4064 }
4065 if (num_pins >= MAX_AUTO_MIC_PINS)
4066 return 0;
4067 types |= (1 << attr);
4068 spec->am_entry[num_pins].pin = nid;
4069 spec->am_entry[num_pins].attr = attr;
4070 num_pins++;
4071 }
4072
4073 if (num_pins < 2)
4074 return 0;
4075
4076 spec->am_num_entries = num_pins;
4077 /* sort the am_entry in the order of attr so that the pin with a
4078 * higher attr will be selected when the jack is plugged.
4079 */
4080 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4081 compare_attr, NULL);
4082
4083 if (!auto_mic_check_imux(codec))
4084 return 0;
4085
4086 spec->auto_mic = 1;
4087 spec->num_adc_nids = 1;
4088 spec->cur_mux[0] = spec->am_entry[0].idx;
4089 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4090 spec->am_entry[0].pin,
4091 spec->am_entry[1].pin,
4092 spec->am_entry[2].pin);
4093
4094 return 0;
4095}
4096
Takashi Iwai55196ff2013-01-24 17:32:56 +01004097/* power_filter hook; make inactive widgets into power down */
4098static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4099 hda_nid_t nid,
4100 unsigned int power_state)
4101{
4102 if (power_state != AC_PWRST_D0)
4103 return power_state;
4104 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4105 return power_state;
4106 if (is_active_nid(codec, nid, HDA_OUTPUT, 0))
4107 return power_state;
4108 return AC_PWRST_D3;
4109}
4110
Takashi Iwai352f7f92012-12-19 12:52:06 +01004111
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004112/*
4113 * Parse the given BIOS configuration and set up the hda_gen_spec
4114 *
4115 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004116 * or a negative error code
4117 */
4118int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004119 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004120{
4121 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004122 int err;
4123
Takashi Iwai1c70a582013-01-11 17:48:22 +01004124 parse_user_hints(codec);
4125
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004126 if (spec->mixer_nid && !spec->mixer_merge_nid)
4127 spec->mixer_merge_nid = spec->mixer_nid;
4128
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004129 if (cfg != &spec->autocfg) {
4130 spec->autocfg = *cfg;
4131 cfg = &spec->autocfg;
4132 }
4133
David Henningsson6fc4cb92013-01-16 15:58:45 +01004134 fill_all_dac_nids(codec);
4135
Takashi Iwai352f7f92012-12-19 12:52:06 +01004136 if (!cfg->line_outs) {
4137 if (cfg->dig_outs || cfg->dig_in_pin) {
4138 spec->multiout.max_channels = 2;
4139 spec->no_analog = 1;
4140 goto dig_only;
4141 }
4142 return 0; /* can't find valid BIOS pin config */
4143 }
4144
4145 if (!spec->no_primary_hp &&
4146 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4147 cfg->line_outs <= cfg->hp_outs) {
4148 /* use HP as primary out */
4149 cfg->speaker_outs = cfg->line_outs;
4150 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4151 sizeof(cfg->speaker_pins));
4152 cfg->line_outs = cfg->hp_outs;
4153 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4154 cfg->hp_outs = 0;
4155 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4156 cfg->line_out_type = AUTO_PIN_HP_OUT;
4157 }
4158
4159 err = parse_output_paths(codec);
4160 if (err < 0)
4161 return err;
4162 err = create_multi_channel_mode(codec);
4163 if (err < 0)
4164 return err;
4165 err = create_multi_out_ctls(codec, cfg);
4166 if (err < 0)
4167 return err;
4168 err = create_hp_out_ctls(codec);
4169 if (err < 0)
4170 return err;
4171 err = create_speaker_out_ctls(codec);
4172 if (err < 0)
4173 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004174 err = create_indep_hp_ctls(codec);
4175 if (err < 0)
4176 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004177 err = create_loopback_mixing_ctl(codec);
4178 if (err < 0)
4179 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004180 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004181 if (err < 0)
4182 return err;
4183 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004184 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004185 return err;
4186
Takashi Iwaia07a9492013-01-07 16:44:06 +01004187 spec->const_channel_count = spec->ext_channel_count;
4188 /* check the multiple speaker and headphone pins */
4189 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4190 spec->const_channel_count = max(spec->const_channel_count,
4191 cfg->speaker_outs * 2);
4192 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4193 spec->const_channel_count = max(spec->const_channel_count,
4194 cfg->hp_outs * 2);
4195 spec->multiout.max_channels = max(spec->ext_channel_count,
4196 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004197
4198 err = check_auto_mute_availability(codec);
4199 if (err < 0)
4200 return err;
4201
4202 err = check_dyn_adc_switch(codec);
4203 if (err < 0)
4204 return err;
4205
Takashi Iwai967303d2013-02-19 17:12:42 +01004206 err = check_auto_mic_availability(codec);
4207 if (err < 0)
4208 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004209
Takashi Iwai352f7f92012-12-19 12:52:06 +01004210 err = create_capture_mixers(codec);
4211 if (err < 0)
4212 return err;
4213
4214 err = parse_mic_boost(codec);
4215 if (err < 0)
4216 return err;
4217
Takashi Iwai978e77e2013-01-10 16:57:58 +01004218 if (spec->add_out_jack_modes) {
4219 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4220 err = create_out_jack_modes(codec, cfg->line_outs,
4221 cfg->line_out_pins);
4222 if (err < 0)
4223 return err;
4224 }
4225 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4226 err = create_out_jack_modes(codec, cfg->hp_outs,
4227 cfg->hp_pins);
4228 if (err < 0)
4229 return err;
4230 }
4231 }
4232
Takashi Iwai352f7f92012-12-19 12:52:06 +01004233 dig_only:
4234 parse_digital(codec);
4235
Takashi Iwai55196ff2013-01-24 17:32:56 +01004236 if (spec->power_down_unused)
4237 codec->power_filter = snd_hda_gen_path_power_filter;
4238
Takashi Iwai352f7f92012-12-19 12:52:06 +01004239 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004241EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
4243
4244/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004245 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004247
4248/* slave controls for virtual master */
4249static const char * const slave_pfxs[] = {
4250 "Front", "Surround", "Center", "LFE", "Side",
4251 "Headphone", "Speaker", "Mono", "Line Out",
4252 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004253 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4254 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4255 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004256 NULL,
4257};
4258
4259int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004261 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Takashi Iwai36502d02012-12-19 15:15:10 +01004264 if (spec->kctls.used) {
4265 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4266 if (err < 0)
4267 return err;
4268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
Takashi Iwai352f7f92012-12-19 12:52:06 +01004270 if (spec->multiout.dig_out_nid) {
4271 err = snd_hda_create_dig_out_ctls(codec,
4272 spec->multiout.dig_out_nid,
4273 spec->multiout.dig_out_nid,
4274 spec->pcm_rec[1].pcm_type);
4275 if (err < 0)
4276 return err;
4277 if (!spec->no_analog) {
4278 err = snd_hda_create_spdif_share_sw(codec,
4279 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 if (err < 0)
4281 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004282 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 }
4284 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004285 if (spec->dig_in_nid) {
4286 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4287 if (err < 0)
4288 return err;
4289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Takashi Iwai352f7f92012-12-19 12:52:06 +01004291 /* if we have no master control, let's create it */
4292 if (!spec->no_analog &&
4293 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004294 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004295 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004296 "Playback Volume");
4297 if (err < 0)
4298 return err;
4299 }
4300 if (!spec->no_analog &&
4301 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4302 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4303 NULL, slave_pfxs,
4304 "Playback Switch",
4305 true, &spec->vmaster_mute.sw_kctl);
4306 if (err < 0)
4307 return err;
4308 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004309 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4310 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
Takashi Iwai352f7f92012-12-19 12:52:06 +01004313 free_kctls(spec); /* no longer needed */
4314
Takashi Iwai967303d2013-02-19 17:12:42 +01004315 if (spec->hp_mic_pin) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004316 int err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004317 int nid = spec->hp_mic_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004318 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4319 if (err < 0)
4320 return err;
4321 err = snd_hda_jack_detect_enable(codec, nid, 0);
4322 if (err < 0)
4323 return err;
4324 }
4325
4326 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4327 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 return err;
4329
4330 return 0;
4331}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004332EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
4335/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004336 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004339static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4340 struct hda_codec *codec,
4341 struct snd_pcm_substream *substream,
4342 int action)
4343{
4344 struct hda_gen_spec *spec = codec->spec;
4345 if (spec->pcm_playback_hook)
4346 spec->pcm_playback_hook(hinfo, codec, substream, action);
4347}
4348
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004349static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4350 struct hda_codec *codec,
4351 struct snd_pcm_substream *substream,
4352 int action)
4353{
4354 struct hda_gen_spec *spec = codec->spec;
4355 if (spec->pcm_capture_hook)
4356 spec->pcm_capture_hook(hinfo, codec, substream, action);
4357}
4358
Takashi Iwai352f7f92012-12-19 12:52:06 +01004359/*
4360 * Analog playback callbacks
4361 */
4362static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4363 struct hda_codec *codec,
4364 struct snd_pcm_substream *substream)
4365{
4366 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004367 int err;
4368
4369 mutex_lock(&spec->pcm_mutex);
4370 err = snd_hda_multi_out_analog_open(codec,
4371 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004372 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004373 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004374 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004375 call_pcm_playback_hook(hinfo, codec, substream,
4376 HDA_GEN_PCM_ACT_OPEN);
4377 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004378 mutex_unlock(&spec->pcm_mutex);
4379 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004380}
4381
4382static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004383 struct hda_codec *codec,
4384 unsigned int stream_tag,
4385 unsigned int format,
4386 struct snd_pcm_substream *substream)
4387{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004388 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004389 int err;
4390
4391 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4392 stream_tag, format, substream);
4393 if (!err)
4394 call_pcm_playback_hook(hinfo, codec, substream,
4395 HDA_GEN_PCM_ACT_PREPARE);
4396 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004397}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004398
Takashi Iwai352f7f92012-12-19 12:52:06 +01004399static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4400 struct hda_codec *codec,
4401 struct snd_pcm_substream *substream)
4402{
4403 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004404 int err;
4405
4406 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4407 if (!err)
4408 call_pcm_playback_hook(hinfo, codec, substream,
4409 HDA_GEN_PCM_ACT_CLEANUP);
4410 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004411}
4412
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004413static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4414 struct hda_codec *codec,
4415 struct snd_pcm_substream *substream)
4416{
4417 struct hda_gen_spec *spec = codec->spec;
4418 mutex_lock(&spec->pcm_mutex);
4419 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004420 call_pcm_playback_hook(hinfo, codec, substream,
4421 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004422 mutex_unlock(&spec->pcm_mutex);
4423 return 0;
4424}
4425
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004426static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4427 struct hda_codec *codec,
4428 struct snd_pcm_substream *substream)
4429{
4430 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4431 return 0;
4432}
4433
4434static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4435 struct hda_codec *codec,
4436 unsigned int stream_tag,
4437 unsigned int format,
4438 struct snd_pcm_substream *substream)
4439{
4440 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4441 call_pcm_capture_hook(hinfo, codec, substream,
4442 HDA_GEN_PCM_ACT_PREPARE);
4443 return 0;
4444}
4445
4446static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4447 struct hda_codec *codec,
4448 struct snd_pcm_substream *substream)
4449{
4450 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4451 call_pcm_capture_hook(hinfo, codec, substream,
4452 HDA_GEN_PCM_ACT_CLEANUP);
4453 return 0;
4454}
4455
4456static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4457 struct hda_codec *codec,
4458 struct snd_pcm_substream *substream)
4459{
4460 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4461 return 0;
4462}
4463
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004464static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4465 struct hda_codec *codec,
4466 struct snd_pcm_substream *substream)
4467{
4468 struct hda_gen_spec *spec = codec->spec;
4469 int err = 0;
4470
4471 mutex_lock(&spec->pcm_mutex);
4472 if (!spec->indep_hp_enabled)
4473 err = -EBUSY;
4474 else
4475 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004476 call_pcm_playback_hook(hinfo, codec, substream,
4477 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004478 mutex_unlock(&spec->pcm_mutex);
4479 return err;
4480}
4481
4482static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4483 struct hda_codec *codec,
4484 struct snd_pcm_substream *substream)
4485{
4486 struct hda_gen_spec *spec = codec->spec;
4487 mutex_lock(&spec->pcm_mutex);
4488 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004489 call_pcm_playback_hook(hinfo, codec, substream,
4490 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004491 mutex_unlock(&spec->pcm_mutex);
4492 return 0;
4493}
4494
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004495static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4496 struct hda_codec *codec,
4497 unsigned int stream_tag,
4498 unsigned int format,
4499 struct snd_pcm_substream *substream)
4500{
4501 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4502 call_pcm_playback_hook(hinfo, codec, substream,
4503 HDA_GEN_PCM_ACT_PREPARE);
4504 return 0;
4505}
4506
4507static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4508 struct hda_codec *codec,
4509 struct snd_pcm_substream *substream)
4510{
4511 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4512 call_pcm_playback_hook(hinfo, codec, substream,
4513 HDA_GEN_PCM_ACT_CLEANUP);
4514 return 0;
4515}
4516
Takashi Iwai352f7f92012-12-19 12:52:06 +01004517/*
4518 * Digital out
4519 */
4520static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4521 struct hda_codec *codec,
4522 struct snd_pcm_substream *substream)
4523{
4524 struct hda_gen_spec *spec = codec->spec;
4525 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4526}
4527
4528static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4529 struct hda_codec *codec,
4530 unsigned int stream_tag,
4531 unsigned int format,
4532 struct snd_pcm_substream *substream)
4533{
4534 struct hda_gen_spec *spec = codec->spec;
4535 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4536 stream_tag, format, substream);
4537}
4538
4539static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4540 struct hda_codec *codec,
4541 struct snd_pcm_substream *substream)
4542{
4543 struct hda_gen_spec *spec = codec->spec;
4544 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4545}
4546
4547static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4548 struct hda_codec *codec,
4549 struct snd_pcm_substream *substream)
4550{
4551 struct hda_gen_spec *spec = codec->spec;
4552 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4553}
4554
4555/*
4556 * Analog capture
4557 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004558#define alt_capture_pcm_open capture_pcm_open
4559#define alt_capture_pcm_close capture_pcm_close
4560
Takashi Iwai352f7f92012-12-19 12:52:06 +01004561static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4562 struct hda_codec *codec,
4563 unsigned int stream_tag,
4564 unsigned int format,
4565 struct snd_pcm_substream *substream)
4566{
4567 struct hda_gen_spec *spec = codec->spec;
4568
4569 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004570 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004571 call_pcm_capture_hook(hinfo, codec, substream,
4572 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004573 return 0;
4574}
4575
Takashi Iwai352f7f92012-12-19 12:52:06 +01004576static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4577 struct hda_codec *codec,
4578 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004579{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004580 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004581
Takashi Iwai352f7f92012-12-19 12:52:06 +01004582 snd_hda_codec_cleanup_stream(codec,
4583 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004584 call_pcm_capture_hook(hinfo, codec, substream,
4585 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004586 return 0;
4587}
4588
Takashi Iwai352f7f92012-12-19 12:52:06 +01004589/*
4590 */
4591static const struct hda_pcm_stream pcm_analog_playback = {
4592 .substreams = 1,
4593 .channels_min = 2,
4594 .channels_max = 8,
4595 /* NID is set in build_pcms */
4596 .ops = {
4597 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004598 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004599 .prepare = playback_pcm_prepare,
4600 .cleanup = playback_pcm_cleanup
4601 },
4602};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Takashi Iwai352f7f92012-12-19 12:52:06 +01004604static const struct hda_pcm_stream pcm_analog_capture = {
4605 .substreams = 1,
4606 .channels_min = 2,
4607 .channels_max = 2,
4608 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004609 .ops = {
4610 .open = capture_pcm_open,
4611 .close = capture_pcm_close,
4612 .prepare = capture_pcm_prepare,
4613 .cleanup = capture_pcm_cleanup
4614 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004615};
4616
4617static const struct hda_pcm_stream pcm_analog_alt_playback = {
4618 .substreams = 1,
4619 .channels_min = 2,
4620 .channels_max = 2,
4621 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004622 .ops = {
4623 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004624 .close = alt_playback_pcm_close,
4625 .prepare = alt_playback_pcm_prepare,
4626 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004627 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004628};
4629
4630static const struct hda_pcm_stream pcm_analog_alt_capture = {
4631 .substreams = 2, /* can be overridden */
4632 .channels_min = 2,
4633 .channels_max = 2,
4634 /* NID is set in build_pcms */
4635 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004636 .open = alt_capture_pcm_open,
4637 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004638 .prepare = alt_capture_pcm_prepare,
4639 .cleanup = alt_capture_pcm_cleanup
4640 },
4641};
4642
4643static const struct hda_pcm_stream pcm_digital_playback = {
4644 .substreams = 1,
4645 .channels_min = 2,
4646 .channels_max = 2,
4647 /* NID is set in build_pcms */
4648 .ops = {
4649 .open = dig_playback_pcm_open,
4650 .close = dig_playback_pcm_close,
4651 .prepare = dig_playback_pcm_prepare,
4652 .cleanup = dig_playback_pcm_cleanup
4653 },
4654};
4655
4656static const struct hda_pcm_stream pcm_digital_capture = {
4657 .substreams = 1,
4658 .channels_min = 2,
4659 .channels_max = 2,
4660 /* NID is set in build_pcms */
4661};
4662
4663/* Used by build_pcms to flag that a PCM has no playback stream */
4664static const struct hda_pcm_stream pcm_null_stream = {
4665 .substreams = 0,
4666 .channels_min = 0,
4667 .channels_max = 0,
4668};
4669
4670/*
4671 * dynamic changing ADC PCM streams
4672 */
4673static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4674{
4675 struct hda_gen_spec *spec = codec->spec;
4676 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4677
4678 if (spec->cur_adc && spec->cur_adc != new_adc) {
4679 /* stream is running, let's swap the current ADC */
4680 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4681 spec->cur_adc = new_adc;
4682 snd_hda_codec_setup_stream(codec, new_adc,
4683 spec->cur_adc_stream_tag, 0,
4684 spec->cur_adc_format);
4685 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004687 return false;
4688}
4689
4690/* analog capture with dynamic dual-adc changes */
4691static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4692 struct hda_codec *codec,
4693 unsigned int stream_tag,
4694 unsigned int format,
4695 struct snd_pcm_substream *substream)
4696{
4697 struct hda_gen_spec *spec = codec->spec;
4698 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4699 spec->cur_adc_stream_tag = stream_tag;
4700 spec->cur_adc_format = format;
4701 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4702 return 0;
4703}
4704
4705static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4706 struct hda_codec *codec,
4707 struct snd_pcm_substream *substream)
4708{
4709 struct hda_gen_spec *spec = codec->spec;
4710 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4711 spec->cur_adc = 0;
4712 return 0;
4713}
4714
4715static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4716 .substreams = 1,
4717 .channels_min = 2,
4718 .channels_max = 2,
4719 .nid = 0, /* fill later */
4720 .ops = {
4721 .prepare = dyn_adc_capture_pcm_prepare,
4722 .cleanup = dyn_adc_capture_pcm_cleanup
4723 },
4724};
4725
Takashi Iwaif873e532012-12-20 16:58:39 +01004726static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4727 const char *chip_name)
4728{
4729 char *p;
4730
4731 if (*str)
4732 return;
4733 strlcpy(str, chip_name, len);
4734
4735 /* drop non-alnum chars after a space */
4736 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4737 if (!isalnum(p[1])) {
4738 *p = 0;
4739 break;
4740 }
4741 }
4742 strlcat(str, sfx, len);
4743}
4744
Takashi Iwai352f7f92012-12-19 12:52:06 +01004745/* build PCM streams based on the parsed results */
4746int snd_hda_gen_build_pcms(struct hda_codec *codec)
4747{
4748 struct hda_gen_spec *spec = codec->spec;
4749 struct hda_pcm *info = spec->pcm_rec;
4750 const struct hda_pcm_stream *p;
4751 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
4753 codec->num_pcms = 1;
4754 codec->pcm_info = info;
4755
Takashi Iwai352f7f92012-12-19 12:52:06 +01004756 if (spec->no_analog)
4757 goto skip_analog;
4758
Takashi Iwaif873e532012-12-20 16:58:39 +01004759 fill_pcm_stream_name(spec->stream_name_analog,
4760 sizeof(spec->stream_name_analog),
4761 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004762 info->name = spec->stream_name_analog;
4763
4764 if (spec->multiout.num_dacs > 0) {
4765 p = spec->stream_analog_playback;
4766 if (!p)
4767 p = &pcm_analog_playback;
4768 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4769 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4770 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4771 spec->multiout.max_channels;
4772 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4773 spec->autocfg.line_outs == 2)
4774 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4775 snd_pcm_2_1_chmaps;
4776 }
4777 if (spec->num_adc_nids) {
4778 p = spec->stream_analog_capture;
4779 if (!p) {
4780 if (spec->dyn_adc_switch)
4781 p = &dyn_adc_pcm_analog_capture;
4782 else
4783 p = &pcm_analog_capture;
4784 }
4785 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4786 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4787 }
4788
Takashi Iwai352f7f92012-12-19 12:52:06 +01004789 skip_analog:
4790 /* SPDIF for stream index #1 */
4791 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004792 fill_pcm_stream_name(spec->stream_name_digital,
4793 sizeof(spec->stream_name_digital),
4794 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004795 codec->num_pcms = 2;
4796 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4797 info = spec->pcm_rec + 1;
4798 info->name = spec->stream_name_digital;
4799 if (spec->dig_out_type)
4800 info->pcm_type = spec->dig_out_type;
4801 else
4802 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4803 if (spec->multiout.dig_out_nid) {
4804 p = spec->stream_digital_playback;
4805 if (!p)
4806 p = &pcm_digital_playback;
4807 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4808 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4809 }
4810 if (spec->dig_in_nid) {
4811 p = spec->stream_digital_capture;
4812 if (!p)
4813 p = &pcm_digital_capture;
4814 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4815 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4816 }
4817 }
4818
4819 if (spec->no_analog)
4820 return 0;
4821
4822 /* If the use of more than one ADC is requested for the current
4823 * model, configure a second analog capture-only PCM.
4824 */
4825 have_multi_adcs = (spec->num_adc_nids > 1) &&
4826 !spec->dyn_adc_switch && !spec->auto_mic;
4827 /* Additional Analaog capture for index #2 */
4828 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004829 fill_pcm_stream_name(spec->stream_name_alt_analog,
4830 sizeof(spec->stream_name_alt_analog),
4831 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004832 codec->num_pcms = 3;
4833 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004834 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004835 if (spec->alt_dac_nid) {
4836 p = spec->stream_analog_alt_playback;
4837 if (!p)
4838 p = &pcm_analog_alt_playback;
4839 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4840 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4841 spec->alt_dac_nid;
4842 } else {
4843 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4844 pcm_null_stream;
4845 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4846 }
4847 if (have_multi_adcs) {
4848 p = spec->stream_analog_alt_capture;
4849 if (!p)
4850 p = &pcm_analog_alt_capture;
4851 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4852 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4853 spec->adc_nids[1];
4854 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4855 spec->num_adc_nids - 1;
4856 } else {
4857 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4858 pcm_null_stream;
4859 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 }
4862
4863 return 0;
4864}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004865EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4866
4867
4868/*
4869 * Standard auto-parser initializations
4870 */
4871
Takashi Iwaid4156932013-01-07 10:08:02 +01004872/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004873static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004874{
4875 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004876 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004877
Takashi Iwai196c17662013-01-04 15:01:40 +01004878 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004879 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004880 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004881 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004882 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004883 snd_hda_activate_path(codec, path, path->active, true);
4884 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004885}
4886
4887/* initialize primary output paths */
4888static void init_multi_out(struct hda_codec *codec)
4889{
4890 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004891 int i;
4892
Takashi Iwaid4156932013-01-07 10:08:02 +01004893 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004894 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004895}
4896
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004897
Takashi Iwai2c12c302013-01-10 09:33:29 +01004898static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004899{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004900 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004901
Takashi Iwaid4156932013-01-07 10:08:02 +01004902 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004903 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004904}
4905
4906/* initialize hp and speaker paths */
4907static void init_extra_out(struct hda_codec *codec)
4908{
4909 struct hda_gen_spec *spec = codec->spec;
4910
4911 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004912 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004913 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4914 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004915 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004916}
4917
4918/* initialize multi-io paths */
4919static void init_multi_io(struct hda_codec *codec)
4920{
4921 struct hda_gen_spec *spec = codec->spec;
4922 int i;
4923
4924 for (i = 0; i < spec->multi_ios; i++) {
4925 hda_nid_t pin = spec->multi_io[i].pin;
4926 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004927 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004928 if (!path)
4929 continue;
4930 if (!spec->multi_io[i].ctl_in)
4931 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004932 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004933 snd_hda_activate_path(codec, path, path->active, true);
4934 }
4935}
4936
Takashi Iwai352f7f92012-12-19 12:52:06 +01004937/* set up input pins and loopback paths */
4938static void init_analog_input(struct hda_codec *codec)
4939{
4940 struct hda_gen_spec *spec = codec->spec;
4941 struct auto_pin_cfg *cfg = &spec->autocfg;
4942 int i;
4943
4944 for (i = 0; i < cfg->num_inputs; i++) {
4945 hda_nid_t nid = cfg->inputs[i].pin;
4946 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004947 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004948
4949 /* init loopback inputs */
4950 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01004951 resume_path_from_idx(codec, spec->loopback_paths[i]);
4952 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004953 }
4954 }
4955}
4956
4957/* initialize ADC paths */
4958static void init_input_src(struct hda_codec *codec)
4959{
4960 struct hda_gen_spec *spec = codec->spec;
4961 struct hda_input_mux *imux = &spec->input_mux;
4962 struct nid_path *path;
4963 int i, c, nums;
4964
4965 if (spec->dyn_adc_switch)
4966 nums = 1;
4967 else
4968 nums = spec->num_adc_nids;
4969
4970 for (c = 0; c < nums; c++) {
4971 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004972 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004973 if (path) {
4974 bool active = path->active;
4975 if (i == spec->cur_mux[c])
4976 active = true;
4977 snd_hda_activate_path(codec, path, active, false);
4978 }
4979 }
Takashi Iwai967303d2013-02-19 17:12:42 +01004980 if (spec->hp_mic)
4981 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004982 }
4983
Takashi Iwai352f7f92012-12-19 12:52:06 +01004984 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004985 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004986}
4987
4988/* set right pin controls for digital I/O */
4989static void init_digital(struct hda_codec *codec)
4990{
4991 struct hda_gen_spec *spec = codec->spec;
4992 int i;
4993 hda_nid_t pin;
4994
Takashi Iwaid4156932013-01-07 10:08:02 +01004995 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004996 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004997 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004998 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01004999 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005000 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005001 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005002}
5003
Takashi Iwai973e4972012-12-20 15:16:09 +01005004/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5005 * invalid unsol tags by some reason
5006 */
5007static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5008{
5009 int i;
5010
5011 for (i = 0; i < codec->init_pins.used; i++) {
5012 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5013 hda_nid_t nid = pin->nid;
5014 if (is_jack_detectable(codec, nid) &&
5015 !snd_hda_jack_tbl_get(codec, nid))
5016 snd_hda_codec_update_cache(codec, nid, 0,
5017 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5018 }
5019}
5020
Takashi Iwai5187ac12013-01-07 12:52:16 +01005021/*
5022 * initialize the generic spec;
5023 * this can be put as patch_ops.init function
5024 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005025int snd_hda_gen_init(struct hda_codec *codec)
5026{
5027 struct hda_gen_spec *spec = codec->spec;
5028
5029 if (spec->init_hook)
5030 spec->init_hook(codec);
5031
5032 snd_hda_apply_verbs(codec);
5033
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005034 codec->cached_write = 1;
5035
Takashi Iwai352f7f92012-12-19 12:52:06 +01005036 init_multi_out(codec);
5037 init_extra_out(codec);
5038 init_multi_io(codec);
5039 init_analog_input(codec);
5040 init_input_src(codec);
5041 init_digital(codec);
5042
Takashi Iwai973e4972012-12-20 15:16:09 +01005043 clear_unsol_on_unused_pins(codec);
5044
Takashi Iwai352f7f92012-12-19 12:52:06 +01005045 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005046 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005047
Takashi Iwaidc870f32013-01-22 15:24:30 +01005048 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005049
Takashi Iwai352f7f92012-12-19 12:52:06 +01005050 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5051 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5052
5053 hda_call_check_power_status(codec, 0x01);
5054 return 0;
5055}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005056EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5057
Takashi Iwai5187ac12013-01-07 12:52:16 +01005058/*
5059 * free the generic spec;
5060 * this can be put as patch_ops.free function
5061 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005062void snd_hda_gen_free(struct hda_codec *codec)
5063{
5064 snd_hda_gen_spec_free(codec->spec);
5065 kfree(codec->spec);
5066 codec->spec = NULL;
5067}
5068EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5069
5070#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005071/*
5072 * check the loopback power save state;
5073 * this can be put as patch_ops.check_power_status function
5074 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005075int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5076{
5077 struct hda_gen_spec *spec = codec->spec;
5078 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5079}
5080EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5081#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005082
5083
5084/*
5085 * the generic codec support
5086 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
Takashi Iwai352f7f92012-12-19 12:52:06 +01005088static const struct hda_codec_ops generic_patch_ops = {
5089 .build_controls = snd_hda_gen_build_controls,
5090 .build_pcms = snd_hda_gen_build_pcms,
5091 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005092 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005093 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005094#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005095 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005096#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097};
5098
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099int snd_hda_parse_generic_codec(struct hda_codec *codec)
5100{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005101 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 int err;
5103
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005104 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005105 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005107 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005110 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5111 if (err < 0)
5112 return err;
5113
5114 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005115 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 goto error;
5117
5118 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 return 0;
5120
Takashi Iwai352f7f92012-12-19 12:52:06 +01005121error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005122 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 return err;
5124}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005125EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);