blob: 98c8627b2afbdf2efc598f74358b2a7fcdac4d58 [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"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010037#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010038#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Takashi Iwai352f7f92012-12-19 12:52:06 +010041/* initialize hda_gen_spec struct */
42int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Takashi Iwai352f7f92012-12-19 12:52:06 +010044 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010045 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010046 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010047 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010048 return 0;
49}
50EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Takashi Iwai12c93df2012-12-19 14:38:33 +010052struct snd_kcontrol_new *
53snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
54 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010055{
56 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
57 if (!knew)
58 return NULL;
59 *knew = *temp;
60 if (name)
61 knew->name = kstrdup(name, GFP_KERNEL);
62 else if (knew->name)
63 knew->name = kstrdup(knew->name, GFP_KERNEL);
64 if (!knew->name)
65 return NULL;
66 return knew;
67}
Takashi Iwai12c93df2012-12-19 14:38:33 +010068EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010069
70static void free_kctls(struct hda_gen_spec *spec)
71{
72 if (spec->kctls.list) {
73 struct snd_kcontrol_new *kctl = spec->kctls.list;
74 int i;
75 for (i = 0; i < spec->kctls.used; i++)
76 kfree(kctl[i].name);
77 }
78 snd_array_free(&spec->kctls);
79}
80
Takashi Iwai352f7f92012-12-19 12:52:06 +010081void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
82{
83 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010085 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010086 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010087 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
Takashi Iwai352f7f92012-12-19 12:52:06 +010089EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010092 * store user hints
93 */
94static void parse_user_hints(struct hda_codec *codec)
95{
96 struct hda_gen_spec *spec = codec->spec;
97 int val;
98
99 val = snd_hda_get_bool_hint(codec, "jack_detect");
100 if (val >= 0)
101 codec->no_jack_detect = !val;
102 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
103 if (val >= 0)
104 codec->inv_jack_detect = !!val;
105 val = snd_hda_get_bool_hint(codec, "trigger_sense");
106 if (val >= 0)
107 codec->no_trigger_sense = !val;
108 val = snd_hda_get_bool_hint(codec, "inv_eapd");
109 if (val >= 0)
110 codec->inv_eapd = !!val;
111 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
112 if (val >= 0)
113 codec->pcm_format_first = !!val;
114 val = snd_hda_get_bool_hint(codec, "sticky_stream");
115 if (val >= 0)
116 codec->no_sticky_stream = !val;
117 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
118 if (val >= 0)
119 codec->spdif_status_reset = !!val;
120 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
121 if (val >= 0)
122 codec->pin_amp_workaround = !!val;
123 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
124 if (val >= 0)
125 codec->single_adc_amp = !!val;
126
Takashi Iwaif72706b2013-01-16 18:20:07 +0100127 val = snd_hda_get_bool_hint(codec, "auto_mute");
128 if (val >= 0)
129 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100130 val = snd_hda_get_bool_hint(codec, "auto_mic");
131 if (val >= 0)
132 spec->suppress_auto_mic = !val;
133 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
134 if (val >= 0)
135 spec->line_in_auto_switch = !!val;
136 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
137 if (val >= 0)
138 spec->need_dac_fix = !!val;
139 val = snd_hda_get_bool_hint(codec, "primary_hp");
140 if (val >= 0)
141 spec->no_primary_hp = !val;
142 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
143 if (val >= 0)
144 spec->multi_cap_vol = !!val;
145 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
146 if (val >= 0)
147 spec->inv_dmic_split = !!val;
148 val = snd_hda_get_bool_hint(codec, "indep_hp");
149 if (val >= 0)
150 spec->indep_hp = !!val;
151 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
152 if (val >= 0)
153 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100154 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100155 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
156 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100157 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100158 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
159 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100160 spec->add_jack_modes = !!val;
161 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
162 if (val >= 0)
163 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100164 val = snd_hda_get_bool_hint(codec, "power_down_unused");
165 if (val >= 0)
166 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100167 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
168 if (val >= 0)
169 spec->hp_mic = !!val;
170 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
171 if (val >= 0)
172 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100173
174 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
175 spec->mixer_nid = val;
176}
177
178/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100179 * pin control value accesses
180 */
181
182#define update_pin_ctl(codec, pin, val) \
183 snd_hda_codec_update_cache(codec, pin, 0, \
184 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
185
186/* restore the pinctl based on the cached value */
187static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
188{
189 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
190}
191
192/* set the pinctl target value and write it if requested */
193static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
194 unsigned int val, bool do_write)
195{
196 if (!pin)
197 return;
198 val = snd_hda_correct_pin_ctl(codec, pin, val);
199 snd_hda_codec_set_pin_target(codec, pin, val);
200 if (do_write)
201 update_pin_ctl(codec, pin, val);
202}
203
204/* set pinctl target values for all given pins */
205static void set_pin_targets(struct hda_codec *codec, int num_pins,
206 hda_nid_t *pins, unsigned int val)
207{
208 int i;
209 for (i = 0; i < num_pins; i++)
210 set_pin_target(codec, pins[i], val, false);
211}
212
213/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100214 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100217/* return the position of NID in the list, or -1 if not found */
218static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
219{
220 int i;
221 for (i = 0; i < nums; i++)
222 if (list[i] == nid)
223 return i;
224 return -1;
225}
226
227/* return true if the given NID is contained in the path */
228static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
229{
230 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
231}
232
Takashi Iwaif5172a72013-01-04 13:19:55 +0100233static struct nid_path *get_nid_path(struct hda_codec *codec,
234 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100235 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100237 struct hda_gen_spec *spec = codec->spec;
238 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Takashi Iwai352f7f92012-12-19 12:52:06 +0100240 for (i = 0; i < spec->paths.used; i++) {
241 struct nid_path *path = snd_array_elem(&spec->paths, i);
242 if (path->depth <= 0)
243 continue;
244 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100245 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100246 if (!anchor_nid ||
247 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
248 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100249 return path;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 return NULL;
253}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100254
255/* get the path between the given NIDs;
256 * passing 0 to either @pin or @dac behaves as a wildcard
257 */
258struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
259 hda_nid_t from_nid, hda_nid_t to_nid)
260{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100261 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100262}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100263EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
264
Takashi Iwai196c17662013-01-04 15:01:40 +0100265/* get the index number corresponding to the path instance;
266 * the index starts from 1, for easier checking the invalid value
267 */
268int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
269{
270 struct hda_gen_spec *spec = codec->spec;
271 struct nid_path *array = spec->paths.list;
272 ssize_t idx;
273
274 if (!spec->paths.used)
275 return 0;
276 idx = path - array;
277 if (idx < 0 || idx >= spec->paths.used)
278 return 0;
279 return idx + 1;
280}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100281EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100282
283/* get the path instance corresponding to the given index number */
284struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
285{
286 struct hda_gen_spec *spec = codec->spec;
287
288 if (idx <= 0 || idx > spec->paths.used)
289 return NULL;
290 return snd_array_elem(&spec->paths, idx - 1);
291}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100292EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100293
Takashi Iwai352f7f92012-12-19 12:52:06 +0100294/* check whether the given DAC is already found in any existing paths */
295static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
296{
297 struct hda_gen_spec *spec = codec->spec;
298 int i;
299
300 for (i = 0; i < spec->paths.used; i++) {
301 struct nid_path *path = snd_array_elem(&spec->paths, i);
302 if (path->path[0] == nid)
303 return true;
304 }
305 return false;
306}
307
308/* check whether the given two widgets can be connected */
309static bool is_reachable_path(struct hda_codec *codec,
310 hda_nid_t from_nid, hda_nid_t to_nid)
311{
312 if (!from_nid || !to_nid)
313 return false;
314 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
315}
316
317/* nid, dir and idx */
318#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
319
320/* check whether the given ctl is already assigned in any path elements */
321static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
322{
323 struct hda_gen_spec *spec = codec->spec;
324 int i;
325
326 val &= AMP_VAL_COMPARE_MASK;
327 for (i = 0; i < spec->paths.used; i++) {
328 struct nid_path *path = snd_array_elem(&spec->paths, i);
329 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
330 return true;
331 }
332 return false;
333}
334
335/* check whether a control with the given (nid, dir, idx) was assigned */
336static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100337 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100338{
339 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100340 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100341}
342
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100343static void print_nid_path(const char *pfx, struct nid_path *path)
344{
345 char buf[40];
346 int i;
347
348
349 buf[0] = 0;
350 for (i = 0; i < path->depth; i++) {
351 char tmp[4];
352 sprintf(tmp, ":%02x", path->path[i]);
353 strlcat(buf, tmp, sizeof(buf));
354 }
355 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
356}
357
Takashi Iwai352f7f92012-12-19 12:52:06 +0100358/* called recursively */
359static bool __parse_nid_path(struct hda_codec *codec,
360 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100361 int anchor_nid, struct nid_path *path,
362 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100363{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100364 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100365 int i, nums;
366
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100367 if (to_nid == anchor_nid)
368 anchor_nid = 0; /* anchor passed */
369 else if (to_nid == (hda_nid_t)(-anchor_nid))
370 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100371
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100372 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100373 for (i = 0; i < nums; i++) {
374 if (conn[i] != from_nid) {
375 /* special case: when from_nid is 0,
376 * try to find an empty DAC
377 */
378 if (from_nid ||
379 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
380 is_dac_already_used(codec, conn[i]))
381 continue;
382 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100383 /* anchor is not requested or already passed? */
384 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100385 goto found;
386 }
387 if (depth >= MAX_NID_PATH_DEPTH)
388 return false;
389 for (i = 0; i < nums; i++) {
390 unsigned int type;
391 type = get_wcaps_type(get_wcaps(codec, conn[i]));
392 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
393 type == AC_WID_PIN)
394 continue;
395 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100396 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100397 goto found;
398 }
399 return false;
400
401 found:
402 path->path[path->depth] = conn[i];
403 path->idx[path->depth + 1] = i;
404 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
405 path->multi[path->depth + 1] = 1;
406 path->depth++;
407 return true;
408}
409
410/* parse the widget path from the given nid to the target nid;
411 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100412 * when @anchor_nid is set to a positive value, only paths through the widget
413 * with the given value are evaluated.
414 * when @anchor_nid is set to a negative value, paths through the widget
415 * with the negative of given value are excluded, only other paths are chosen.
416 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100417 */
418bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100419 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100420 struct nid_path *path)
421{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100422 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100423 path->path[path->depth] = to_nid;
424 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100425 return true;
426 }
427 return false;
428}
429EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100432 * parse the path between the given NIDs and add to the path list.
433 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100435struct nid_path *
436snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100437 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100439 struct hda_gen_spec *spec = codec->spec;
440 struct nid_path *path;
441
442 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
443 return NULL;
444
Takashi Iwaif5172a72013-01-04 13:19:55 +0100445 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100446 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100447 if (path)
448 return path;
449
Takashi Iwai352f7f92012-12-19 12:52:06 +0100450 path = snd_array_new(&spec->paths);
451 if (!path)
452 return NULL;
453 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100454 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100455 return path;
456 /* push back */
457 spec->paths.used--;
458 return NULL;
459}
460EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
461
Takashi Iwai980428c2013-01-09 09:28:20 +0100462/* clear the given path as invalid so that it won't be picked up later */
463static void invalidate_nid_path(struct hda_codec *codec, int idx)
464{
465 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
466 if (!path)
467 return;
468 memset(path, 0, sizeof(*path));
469}
470
Takashi Iwai352f7f92012-12-19 12:52:06 +0100471/* look for an empty DAC slot */
472static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
473 bool is_digital)
474{
475 struct hda_gen_spec *spec = codec->spec;
476 bool cap_digital;
477 int i;
478
479 for (i = 0; i < spec->num_all_dacs; i++) {
480 hda_nid_t nid = spec->all_dacs[i];
481 if (!nid || is_dac_already_used(codec, nid))
482 continue;
483 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
484 if (is_digital != cap_digital)
485 continue;
486 if (is_reachable_path(codec, nid, pin))
487 return nid;
488 }
489 return 0;
490}
491
492/* replace the channels in the composed amp value with the given number */
493static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
494{
495 val &= ~(0x3U << 16);
496 val |= chs << 16;
497 return val;
498}
499
500/* check whether the widget has the given amp capability for the direction */
501static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
502 int dir, unsigned int bits)
503{
504 if (!nid)
505 return false;
506 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
507 if (query_amp_caps(codec, nid, dir) & bits)
508 return true;
509 return false;
510}
511
David Henningsson99a55922013-01-16 15:58:44 +0100512static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
513 hda_nid_t nid2, int dir)
514{
515 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
516 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
517 return (query_amp_caps(codec, nid1, dir) ==
518 query_amp_caps(codec, nid2, dir));
519}
520
Takashi Iwai352f7f92012-12-19 12:52:06 +0100521#define nid_has_mute(codec, nid, dir) \
522 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
523#define nid_has_volume(codec, nid, dir) \
524 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
525
526/* look for a widget suitable for assigning a mute switch in the path */
527static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
528 struct nid_path *path)
529{
530 int i;
531
532 for (i = path->depth - 1; i >= 0; i--) {
533 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
534 return path->path[i];
535 if (i != path->depth - 1 && i != 0 &&
536 nid_has_mute(codec, path->path[i], HDA_INPUT))
537 return path->path[i];
538 }
539 return 0;
540}
541
542/* look for a widget suitable for assigning a volume ctl in the path */
543static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
544 struct nid_path *path)
545{
546 int i;
547
548 for (i = path->depth - 1; i >= 0; i--) {
549 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
550 return path->path[i];
551 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100556 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100558
559/* can have the amp-in capability? */
560static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100562 hda_nid_t nid = path->path[idx];
563 unsigned int caps = get_wcaps(codec, nid);
564 unsigned int type = get_wcaps_type(caps);
565
566 if (!(caps & AC_WCAP_IN_AMP))
567 return false;
568 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
569 return false;
570 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Takashi Iwai352f7f92012-12-19 12:52:06 +0100573/* can have the amp-out capability? */
574static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100576 hda_nid_t nid = path->path[idx];
577 unsigned int caps = get_wcaps(codec, nid);
578 unsigned int type = get_wcaps_type(caps);
579
580 if (!(caps & AC_WCAP_OUT_AMP))
581 return false;
582 if (type == AC_WID_PIN && !idx) /* only for output pins */
583 return false;
584 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Takashi Iwai352f7f92012-12-19 12:52:06 +0100587/* check whether the given (nid,dir,idx) is active */
588static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100589 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100591 struct hda_gen_spec *spec = codec->spec;
592 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Takashi Iwai352f7f92012-12-19 12:52:06 +0100594 for (n = 0; n < spec->paths.used; n++) {
595 struct nid_path *path = snd_array_elem(&spec->paths, n);
596 if (!path->active)
597 continue;
598 for (i = 0; i < path->depth; i++) {
599 if (path->path[i] == nid) {
600 if (dir == HDA_OUTPUT || path->idx[i] == idx)
601 return true;
602 break;
603 }
604 }
605 }
606 return false;
607}
608
609/* get the default amp value for the target state */
610static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100611 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100612{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100613 unsigned int val = 0;
614
Takashi Iwai352f7f92012-12-19 12:52:06 +0100615 if (caps & AC_AMPCAP_NUM_STEPS) {
616 /* set to 0dB */
617 if (enable)
618 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
619 }
620 if (caps & AC_AMPCAP_MUTE) {
621 if (!enable)
622 val |= HDA_AMP_MUTE;
623 }
624 return val;
625}
626
627/* initialize the amp value (only at the first time) */
628static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
629{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100630 unsigned int caps = query_amp_caps(codec, nid, dir);
631 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100632 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
633}
634
Takashi Iwai8999bf02013-01-18 11:01:33 +0100635/* calculate amp value mask we can modify;
636 * if the given amp is controlled by mixers, don't touch it
637 */
638static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
639 hda_nid_t nid, int dir, int idx,
640 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100641{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100642 unsigned int mask = 0xff;
643
644 if (caps & AC_AMPCAP_MUTE) {
645 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
646 mask &= ~0x80;
647 }
648 if (caps & AC_AMPCAP_NUM_STEPS) {
649 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
650 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
651 mask &= ~0x7f;
652 }
653 return mask;
654}
655
656static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
657 int idx, int idx_to_check, bool enable)
658{
659 unsigned int caps;
660 unsigned int mask, val;
661
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100662 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100663 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100664
665 caps = query_amp_caps(codec, nid, dir);
666 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
667 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
668 if (!mask)
669 return;
670
671 val &= mask;
672 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100673}
674
675static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
676 int i, bool enable)
677{
678 hda_nid_t nid = path->path[i];
679 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100680 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100681}
682
683static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
684 int i, bool enable, bool add_aamix)
685{
686 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100687 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100688 int n, nums, idx;
689 int type;
690 hda_nid_t nid = path->path[i];
691
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100692 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100693 type = get_wcaps_type(get_wcaps(codec, nid));
694 if (type == AC_WID_PIN ||
695 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
696 nums = 1;
697 idx = 0;
698 } else
699 idx = path->idx[i];
700
701 for (n = 0; n < nums; n++)
702 init_amp(codec, nid, HDA_INPUT, n);
703
Takashi Iwai352f7f92012-12-19 12:52:06 +0100704 /* here is a little bit tricky in comparison with activate_amp_out();
705 * when aa-mixer is available, we need to enable the path as well
706 */
707 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100708 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100709 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100710 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712}
713
Takashi Iwai352f7f92012-12-19 12:52:06 +0100714/* activate or deactivate the given path
715 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100717void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
718 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100720 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100721 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Takashi Iwai352f7f92012-12-19 12:52:06 +0100723 if (!enable)
724 path->active = false;
725
726 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100727 hda_nid_t nid = path->path[i];
728 if (enable && spec->power_down_unused) {
729 /* make sure the widget is powered up */
730 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
731 snd_hda_codec_write(codec, nid, 0,
732 AC_VERB_SET_POWER_STATE,
733 AC_PWRST_D0);
734 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100735 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100736 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100737 AC_VERB_SET_CONNECT_SEL,
738 path->idx[i]);
739 if (has_amp_in(codec, path, i))
740 activate_amp_in(codec, path, i, enable, add_aamix);
741 if (has_amp_out(codec, path, i))
742 activate_amp_out(codec, path, i, enable);
743 }
744
745 if (enable)
746 path->active = true;
747}
748EXPORT_SYMBOL_HDA(snd_hda_activate_path);
749
Takashi Iwai55196ff2013-01-24 17:32:56 +0100750/* if the given path is inactive, put widgets into D3 (only if suitable) */
751static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
752{
753 struct hda_gen_spec *spec = codec->spec;
754 bool changed;
755 int i;
756
757 if (!spec->power_down_unused || path->active)
758 return;
759
760 for (i = 0; i < path->depth; i++) {
761 hda_nid_t nid = path->path[i];
762 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3)) {
763 snd_hda_codec_write(codec, nid, 0,
764 AC_VERB_SET_POWER_STATE,
765 AC_PWRST_D3);
766 changed = true;
767 }
768 }
769
770 if (changed) {
771 msleep(10);
772 snd_hda_codec_read(codec, path->path[0], 0,
773 AC_VERB_GET_POWER_STATE, 0);
774 }
775}
776
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100777/* turn on/off EAPD on the given pin */
778static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
779{
780 struct hda_gen_spec *spec = codec->spec;
781 if (spec->own_eapd_ctl ||
782 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
783 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100784 if (codec->inv_eapd)
785 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100786 snd_hda_codec_update_cache(codec, pin, 0,
787 AC_VERB_SET_EAPD_BTLENABLE,
788 enable ? 0x02 : 0x00);
789}
790
Takashi Iwai3e367f12013-01-23 17:07:23 +0100791/* re-initialize the path specified by the given path index */
792static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
793{
794 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
795 if (path)
796 snd_hda_activate_path(codec, path, path->active, false);
797}
798
Takashi Iwai352f7f92012-12-19 12:52:06 +0100799
800/*
801 * Helper functions for creating mixer ctl elements
802 */
803
804enum {
805 HDA_CTL_WIDGET_VOL,
806 HDA_CTL_WIDGET_MUTE,
807 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100808};
809static const struct snd_kcontrol_new control_templates[] = {
810 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
811 HDA_CODEC_MUTE(NULL, 0, 0, 0),
812 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100813};
814
815/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100816static struct snd_kcontrol_new *
817add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100818 int cidx, unsigned long val)
819{
820 struct snd_kcontrol_new *knew;
821
Takashi Iwai12c93df2012-12-19 14:38:33 +0100822 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100823 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100824 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100825 knew->index = cidx;
826 if (get_amp_nid_(val))
827 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
828 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100829 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100830}
831
832static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
833 const char *pfx, const char *dir,
834 const char *sfx, int cidx, unsigned long val)
835{
836 char name[32];
837 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100838 if (!add_control(spec, type, name, cidx, val))
839 return -ENOMEM;
840 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100841}
842
843#define add_pb_vol_ctrl(spec, type, pfx, val) \
844 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
845#define add_pb_sw_ctrl(spec, type, pfx, val) \
846 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
847#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
848 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
849#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
850 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
851
852static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
853 unsigned int chs, struct nid_path *path)
854{
855 unsigned int val;
856 if (!path)
857 return 0;
858 val = path->ctls[NID_PATH_VOL_CTL];
859 if (!val)
860 return 0;
861 val = amp_val_replace_channels(val, chs);
862 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
863}
864
865/* return the channel bits suitable for the given path->ctls[] */
866static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
867 int type)
868{
869 int chs = 1; /* mono (left only) */
870 if (path) {
871 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
872 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
873 chs = 3; /* stereo */
874 }
875 return chs;
876}
877
878static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
879 struct nid_path *path)
880{
881 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
882 return add_vol_ctl(codec, pfx, cidx, chs, path);
883}
884
885/* create a mute-switch for the given mixer widget;
886 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
887 */
888static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
889 unsigned int chs, struct nid_path *path)
890{
891 unsigned int val;
892 int type = HDA_CTL_WIDGET_MUTE;
893
894 if (!path)
895 return 0;
896 val = path->ctls[NID_PATH_MUTE_CTL];
897 if (!val)
898 return 0;
899 val = amp_val_replace_channels(val, chs);
900 if (get_amp_direction_(val) == HDA_INPUT) {
901 hda_nid_t nid = get_amp_nid_(val);
902 int nums = snd_hda_get_num_conns(codec, nid);
903 if (nums > 1) {
904 type = HDA_CTL_BIND_MUTE;
905 val |= nums << 19;
906 }
907 }
908 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
909}
910
911static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
912 int cidx, struct nid_path *path)
913{
914 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
915 return add_sw_ctl(codec, pfx, cidx, chs, path);
916}
917
Takashi Iwai247d85e2013-01-17 16:18:11 +0100918/* any ctl assigned to the path with the given index? */
919static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
920{
921 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
922 return path && path->ctls[ctl_type];
923}
924
Takashi Iwai352f7f92012-12-19 12:52:06 +0100925static const char * const channel_name[4] = {
926 "Front", "Surround", "CLFE", "Side"
927};
928
929/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100930static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
931 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100932{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100933 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100934 struct auto_pin_cfg *cfg = &spec->autocfg;
935
936 *index = 0;
937 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100938 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100939 return spec->vmaster_mute.hook ? "PCM" : "Master";
940
941 /* if there is really a single DAC used in the whole output paths,
942 * use it master (or "PCM" if a vmaster hook is present)
943 */
944 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
945 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
946 return spec->vmaster_mute.hook ? "PCM" : "Master";
947
Takashi Iwai247d85e2013-01-17 16:18:11 +0100948 /* multi-io channels */
949 if (ch >= cfg->line_outs)
950 return channel_name[ch];
951
Takashi Iwai352f7f92012-12-19 12:52:06 +0100952 switch (cfg->line_out_type) {
953 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100954 /* if the primary channel vol/mute is shared with HP volume,
955 * don't name it as Speaker
956 */
957 if (!ch && cfg->hp_outs &&
958 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
959 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100960 if (cfg->line_outs == 1)
961 return "Speaker";
962 if (cfg->line_outs == 2)
963 return ch ? "Bass Speaker" : "Speaker";
964 break;
965 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100966 /* if the primary channel vol/mute is shared with spk volume,
967 * don't name it as Headphone
968 */
969 if (!ch && cfg->speaker_outs &&
970 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
971 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100972 /* for multi-io case, only the primary out */
973 if (ch && spec->multi_ios)
974 break;
975 *index = ch;
976 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100977 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100978
979 /* for a single channel output, we don't have to name the channel */
980 if (cfg->line_outs == 1 && !spec->multi_ios)
981 return "PCM";
982
Takashi Iwai352f7f92012-12-19 12:52:06 +0100983 if (ch >= ARRAY_SIZE(channel_name)) {
984 snd_BUG();
985 return "PCM";
986 }
987
988 return channel_name[ch];
989}
990
991/*
992 * Parse output paths
993 */
994
995/* badness definition */
996enum {
997 /* No primary DAC is found for the main output */
998 BAD_NO_PRIMARY_DAC = 0x10000,
999 /* No DAC is found for the extra output */
1000 BAD_NO_DAC = 0x4000,
1001 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001002 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001003 /* No individual DAC for extra output */
1004 BAD_NO_EXTRA_DAC = 0x102,
1005 /* No individual DAC for extra surrounds */
1006 BAD_NO_EXTRA_SURR_DAC = 0x101,
1007 /* Primary DAC shared with main surrounds */
1008 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001009 /* No independent HP possible */
1010 BAD_NO_INDEP_HP = 0x40,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001011 /* Primary DAC shared with main CLFE */
1012 BAD_SHARED_CLFE = 0x10,
1013 /* Primary DAC shared with extra surrounds */
1014 BAD_SHARED_EXTRA_SURROUND = 0x10,
1015 /* Volume widget is shared */
1016 BAD_SHARED_VOL = 0x10,
1017};
1018
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001019/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001020 * volume and mute controls, and assign the values to ctls[].
1021 *
1022 * When no appropriate widget is found in the path, the badness value
1023 * is incremented depending on the situation. The function returns the
1024 * total badness for both volume and mute controls.
1025 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001026static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001027{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001028 hda_nid_t nid;
1029 unsigned int val;
1030 int badness = 0;
1031
1032 if (!path)
1033 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001034
1035 if (path->ctls[NID_PATH_VOL_CTL] ||
1036 path->ctls[NID_PATH_MUTE_CTL])
1037 return 0; /* already evaluated */
1038
Takashi Iwai352f7f92012-12-19 12:52:06 +01001039 nid = look_for_out_vol_nid(codec, path);
1040 if (nid) {
1041 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1042 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1043 badness += BAD_SHARED_VOL;
1044 else
1045 path->ctls[NID_PATH_VOL_CTL] = val;
1046 } else
1047 badness += BAD_SHARED_VOL;
1048 nid = look_for_out_mute_nid(codec, path);
1049 if (nid) {
1050 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1051 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1052 nid_has_mute(codec, nid, HDA_OUTPUT))
1053 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1054 else
1055 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1056 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1057 badness += BAD_SHARED_VOL;
1058 else
1059 path->ctls[NID_PATH_MUTE_CTL] = val;
1060 } else
1061 badness += BAD_SHARED_VOL;
1062 return badness;
1063}
1064
1065struct badness_table {
1066 int no_primary_dac; /* no primary DAC */
1067 int no_dac; /* no secondary DACs */
1068 int shared_primary; /* primary DAC is shared with main output */
1069 int shared_surr; /* secondary DAC shared with main or primary */
1070 int shared_clfe; /* third DAC shared with main or primary */
1071 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1072};
1073
1074static struct badness_table main_out_badness = {
1075 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1076 .no_dac = BAD_NO_DAC,
1077 .shared_primary = BAD_NO_PRIMARY_DAC,
1078 .shared_surr = BAD_SHARED_SURROUND,
1079 .shared_clfe = BAD_SHARED_CLFE,
1080 .shared_surr_main = BAD_SHARED_SURROUND,
1081};
1082
1083static struct badness_table extra_out_badness = {
1084 .no_primary_dac = BAD_NO_DAC,
1085 .no_dac = BAD_NO_DAC,
1086 .shared_primary = BAD_NO_EXTRA_DAC,
1087 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1088 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1089 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1090};
1091
Takashi Iwai7385df62013-01-07 09:50:52 +01001092/* get the DAC of the primary output corresponding to the given array index */
1093static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1094{
1095 struct hda_gen_spec *spec = codec->spec;
1096 struct auto_pin_cfg *cfg = &spec->autocfg;
1097
1098 if (cfg->line_outs > idx)
1099 return spec->private_dac_nids[idx];
1100 idx -= cfg->line_outs;
1101 if (spec->multi_ios > idx)
1102 return spec->multi_io[idx].dac;
1103 return 0;
1104}
1105
1106/* return the DAC if it's reachable, otherwise zero */
1107static inline hda_nid_t try_dac(struct hda_codec *codec,
1108 hda_nid_t dac, hda_nid_t pin)
1109{
1110 return is_reachable_path(codec, dac, pin) ? dac : 0;
1111}
1112
Takashi Iwai352f7f92012-12-19 12:52:06 +01001113/* try to assign DACs to pins and return the resultant badness */
1114static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1115 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001116 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001117 const struct badness_table *bad)
1118{
1119 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001120 int i, j;
1121 int badness = 0;
1122 hda_nid_t dac;
1123
1124 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return 0;
1126
Takashi Iwai352f7f92012-12-19 12:52:06 +01001127 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001128 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001129 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001130
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001131 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1132 if (path) {
1133 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001134 continue;
1135 }
1136
1137 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001138 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001139 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001140 for (j = 1; j < num_outs; j++) {
1141 if (is_reachable_path(codec, dacs[j], pin)) {
1142 dacs[0] = dacs[j];
1143 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001144 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001145 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001146 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001149 }
1150 dac = dacs[i];
1151 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001152 if (num_outs > 2)
1153 dac = try_dac(codec, get_primary_out(codec, i), pin);
1154 if (!dac)
1155 dac = try_dac(codec, dacs[0], pin);
1156 if (!dac)
1157 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001158 if (dac) {
1159 if (!i)
1160 badness += bad->shared_primary;
1161 else if (i == 1)
1162 badness += bad->shared_surr;
1163 else
1164 badness += bad->shared_clfe;
1165 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1166 dac = spec->private_dac_nids[0];
1167 badness += bad->shared_surr_main;
1168 } else if (!i)
1169 badness += bad->no_primary_dac;
1170 else
1171 badness += bad->no_dac;
1172 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001173 if (!dac)
1174 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001175 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001176 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001177 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001178 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001179 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001180 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001181 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001182 badness += bad->no_dac;
1183 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001184 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001185 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001186 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001187 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001188 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001189 }
1190
1191 return badness;
1192}
1193
1194/* return NID if the given pin has only a single connection to a certain DAC */
1195static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1196{
1197 struct hda_gen_spec *spec = codec->spec;
1198 int i;
1199 hda_nid_t nid_found = 0;
1200
1201 for (i = 0; i < spec->num_all_dacs; i++) {
1202 hda_nid_t nid = spec->all_dacs[i];
1203 if (!nid || is_dac_already_used(codec, nid))
1204 continue;
1205 if (is_reachable_path(codec, nid, pin)) {
1206 if (nid_found)
1207 return 0;
1208 nid_found = nid;
1209 }
1210 }
1211 return nid_found;
1212}
1213
1214/* check whether the given pin can be a multi-io pin */
1215static bool can_be_multiio_pin(struct hda_codec *codec,
1216 unsigned int location, hda_nid_t nid)
1217{
1218 unsigned int defcfg, caps;
1219
1220 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1221 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1222 return false;
1223 if (location && get_defcfg_location(defcfg) != location)
1224 return false;
1225 caps = snd_hda_query_pin_caps(codec, nid);
1226 if (!(caps & AC_PINCAP_OUT))
1227 return false;
1228 return true;
1229}
1230
Takashi Iwaie22aab72013-01-04 14:50:04 +01001231/* count the number of input pins that are capable to be multi-io */
1232static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1233{
1234 struct hda_gen_spec *spec = codec->spec;
1235 struct auto_pin_cfg *cfg = &spec->autocfg;
1236 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1237 unsigned int location = get_defcfg_location(defcfg);
1238 int type, i;
1239 int num_pins = 0;
1240
1241 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1242 for (i = 0; i < cfg->num_inputs; i++) {
1243 if (cfg->inputs[i].type != type)
1244 continue;
1245 if (can_be_multiio_pin(codec, location,
1246 cfg->inputs[i].pin))
1247 num_pins++;
1248 }
1249 }
1250 return num_pins;
1251}
1252
Takashi Iwai352f7f92012-12-19 12:52:06 +01001253/*
1254 * multi-io helper
1255 *
1256 * When hardwired is set, try to fill ony hardwired pins, and returns
1257 * zero if any pins are filled, non-zero if nothing found.
1258 * When hardwired is off, try to fill possible input pins, and returns
1259 * the badness value.
1260 */
1261static int fill_multi_ios(struct hda_codec *codec,
1262 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001263 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001264{
1265 struct hda_gen_spec *spec = codec->spec;
1266 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001267 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001268 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1269 unsigned int location = get_defcfg_location(defcfg);
1270 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001271 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001272
1273 old_pins = spec->multi_ios;
1274 if (old_pins >= 2)
1275 goto end_fill;
1276
Takashi Iwaie22aab72013-01-04 14:50:04 +01001277 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001278 if (num_pins < 2)
1279 goto end_fill;
1280
Takashi Iwai352f7f92012-12-19 12:52:06 +01001281 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1282 for (i = 0; i < cfg->num_inputs; i++) {
1283 hda_nid_t nid = cfg->inputs[i].pin;
1284 hda_nid_t dac = 0;
1285
1286 if (cfg->inputs[i].type != type)
1287 continue;
1288 if (!can_be_multiio_pin(codec, location, nid))
1289 continue;
1290 for (j = 0; j < spec->multi_ios; j++) {
1291 if (nid == spec->multi_io[j].pin)
1292 break;
1293 }
1294 if (j < spec->multi_ios)
1295 continue;
1296
Takashi Iwai352f7f92012-12-19 12:52:06 +01001297 if (hardwired)
1298 dac = get_dac_if_single(codec, nid);
1299 else if (!dac)
1300 dac = look_for_dac(codec, nid, false);
1301 if (!dac) {
1302 badness++;
1303 continue;
1304 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001305 path = snd_hda_add_new_path(codec, dac, nid,
1306 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001307 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001308 badness++;
1309 continue;
1310 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001311 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001312 spec->multi_io[spec->multi_ios].pin = nid;
1313 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001314 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1315 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001316 spec->multi_ios++;
1317 if (spec->multi_ios >= 2)
1318 break;
1319 }
1320 }
1321 end_fill:
1322 if (badness)
1323 badness = BAD_MULTI_IO;
1324 if (old_pins == spec->multi_ios) {
1325 if (hardwired)
1326 return 1; /* nothing found */
1327 else
1328 return badness; /* no badness if nothing found */
1329 }
1330 if (!hardwired && spec->multi_ios < 2) {
1331 /* cancel newly assigned paths */
1332 spec->paths.used -= spec->multi_ios - old_pins;
1333 spec->multi_ios = old_pins;
1334 return badness;
1335 }
1336
1337 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001338 for (i = old_pins; i < spec->multi_ios; i++) {
1339 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1340 badness += assign_out_path_ctls(codec, path);
1341 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001342
1343 return badness;
1344}
1345
1346/* map DACs for all pins in the list if they are single connections */
1347static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001348 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001349{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001350 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001351 int i;
1352 bool found = false;
1353 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001354 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001355 hda_nid_t dac;
1356 if (dacs[i])
1357 continue;
1358 dac = get_dac_if_single(codec, pins[i]);
1359 if (!dac)
1360 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001361 path = snd_hda_add_new_path(codec, dac, pins[i],
1362 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001363 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001364 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001365 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001366 dacs[i] = dac;
1367 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001368 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001369 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001370 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001371 }
1372 }
1373 return found;
1374}
1375
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001376/* create a new path including aamix if available, and return its index */
1377static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1378{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001379 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001380 struct nid_path *path;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001381 hda_nid_t dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001382
1383 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001384 if (!path || !path->depth ||
1385 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001386 return 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001387 dac = path->path[0];
1388 pin = path->path[path->depth - 1];
1389 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1390 if (!path) {
1391 if (dac != spec->multiout.dac_nids[0])
1392 dac = spec->multiout.dac_nids[0];
1393 else if (spec->multiout.hp_out_nid[0])
1394 dac = spec->multiout.hp_out_nid[0];
1395 else if (spec->multiout.extra_out_nid[0])
1396 dac = spec->multiout.extra_out_nid[0];
1397 if (dac)
1398 path = snd_hda_add_new_path(codec, dac, pin,
1399 spec->mixer_nid);
1400 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001401 if (!path)
1402 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001403 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001404 path->active = false; /* unused as default */
1405 return snd_hda_get_path_idx(codec, path);
1406}
1407
Takashi Iwai55a63d42013-03-21 17:20:12 +01001408/* check whether the independent HP is available with the current config */
1409static bool indep_hp_possible(struct hda_codec *codec)
1410{
1411 struct hda_gen_spec *spec = codec->spec;
1412 struct auto_pin_cfg *cfg = &spec->autocfg;
1413 struct nid_path *path;
1414 int i, idx;
1415
1416 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1417 idx = spec->out_paths[0];
1418 else
1419 idx = spec->hp_paths[0];
1420 path = snd_hda_get_path_from_idx(codec, idx);
1421 if (!path)
1422 return false;
1423
1424 /* assume no path conflicts unless aamix is involved */
1425 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1426 return true;
1427
1428 /* check whether output paths contain aamix */
1429 for (i = 0; i < cfg->line_outs; i++) {
1430 if (spec->out_paths[i] == idx)
1431 break;
1432 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1433 if (path && is_nid_contained(path, spec->mixer_nid))
1434 return false;
1435 }
1436 for (i = 0; i < cfg->speaker_outs; i++) {
1437 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1438 if (path && is_nid_contained(path, spec->mixer_nid))
1439 return false;
1440 }
1441
1442 return true;
1443}
1444
Takashi Iwaia07a9492013-01-07 16:44:06 +01001445/* fill the empty entries in the dac array for speaker/hp with the
1446 * shared dac pointed by the paths
1447 */
1448static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1449 hda_nid_t *dacs, int *path_idx)
1450{
1451 struct nid_path *path;
1452 int i;
1453
1454 for (i = 0; i < num_outs; i++) {
1455 if (dacs[i])
1456 continue;
1457 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1458 if (!path)
1459 continue;
1460 dacs[i] = path->path[0];
1461 }
1462}
1463
Takashi Iwai352f7f92012-12-19 12:52:06 +01001464/* fill in the dac_nids table from the parsed pin configuration */
1465static int fill_and_eval_dacs(struct hda_codec *codec,
1466 bool fill_hardwired,
1467 bool fill_mio_first)
1468{
1469 struct hda_gen_spec *spec = codec->spec;
1470 struct auto_pin_cfg *cfg = &spec->autocfg;
1471 int i, err, badness;
1472
1473 /* set num_dacs once to full for look_for_dac() */
1474 spec->multiout.num_dacs = cfg->line_outs;
1475 spec->multiout.dac_nids = spec->private_dac_nids;
1476 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1477 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1478 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1479 spec->multi_ios = 0;
1480 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001481
1482 /* clear path indices */
1483 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1484 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1485 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1486 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1487 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001488 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001489 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1490 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1491
Takashi Iwai352f7f92012-12-19 12:52:06 +01001492 badness = 0;
1493
1494 /* fill hard-wired DACs first */
1495 if (fill_hardwired) {
1496 bool mapped;
1497 do {
1498 mapped = map_singles(codec, cfg->line_outs,
1499 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001500 spec->private_dac_nids,
1501 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001502 mapped |= map_singles(codec, cfg->hp_outs,
1503 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001504 spec->multiout.hp_out_nid,
1505 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001506 mapped |= map_singles(codec, cfg->speaker_outs,
1507 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001508 spec->multiout.extra_out_nid,
1509 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001510 if (fill_mio_first && cfg->line_outs == 1 &&
1511 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001512 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001513 if (!err)
1514 mapped = true;
1515 }
1516 } while (mapped);
1517 }
1518
1519 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001520 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001521 &main_out_badness);
1522
Takashi Iwai352f7f92012-12-19 12:52:06 +01001523 if (fill_mio_first &&
1524 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1525 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001526 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001527 if (err < 0)
1528 return err;
1529 /* we don't count badness at this stage yet */
1530 }
1531
1532 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1533 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1534 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001535 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001536 &extra_out_badness);
1537 if (err < 0)
1538 return err;
1539 badness += err;
1540 }
1541 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1542 err = try_assign_dacs(codec, cfg->speaker_outs,
1543 cfg->speaker_pins,
1544 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001545 spec->speaker_paths,
1546 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001547 if (err < 0)
1548 return err;
1549 badness += err;
1550 }
1551 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001552 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001553 if (err < 0)
1554 return err;
1555 badness += err;
1556 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001557
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001558 if (spec->mixer_nid) {
1559 spec->aamix_out_paths[0] =
1560 check_aamix_out_path(codec, spec->out_paths[0]);
1561 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1562 spec->aamix_out_paths[1] =
1563 check_aamix_out_path(codec, spec->hp_paths[0]);
1564 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1565 spec->aamix_out_paths[2] =
1566 check_aamix_out_path(codec, spec->speaker_paths[0]);
1567 }
1568
Takashi Iwaie22aab72013-01-04 14:50:04 +01001569 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1570 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1571 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001572
Takashi Iwaia07a9492013-01-07 16:44:06 +01001573 /* re-count num_dacs and squash invalid entries */
1574 spec->multiout.num_dacs = 0;
1575 for (i = 0; i < cfg->line_outs; i++) {
1576 if (spec->private_dac_nids[i])
1577 spec->multiout.num_dacs++;
1578 else {
1579 memmove(spec->private_dac_nids + i,
1580 spec->private_dac_nids + i + 1,
1581 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1582 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1583 }
1584 }
1585
1586 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001587 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001588
Takashi Iwai352f7f92012-12-19 12:52:06 +01001589 if (spec->multi_ios == 2) {
1590 for (i = 0; i < 2; i++)
1591 spec->private_dac_nids[spec->multiout.num_dacs++] =
1592 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001593 } else if (spec->multi_ios) {
1594 spec->multi_ios = 0;
1595 badness += BAD_MULTI_IO;
1596 }
1597
Takashi Iwai55a63d42013-03-21 17:20:12 +01001598 if (spec->indep_hp && !indep_hp_possible(codec))
1599 badness += BAD_NO_INDEP_HP;
1600
Takashi Iwaia07a9492013-01-07 16:44:06 +01001601 /* re-fill the shared DAC for speaker / headphone */
1602 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1603 refill_shared_dacs(codec, cfg->hp_outs,
1604 spec->multiout.hp_out_nid,
1605 spec->hp_paths);
1606 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1607 refill_shared_dacs(codec, cfg->speaker_outs,
1608 spec->multiout.extra_out_nid,
1609 spec->speaker_paths);
1610
Takashi Iwai352f7f92012-12-19 12:52:06 +01001611 return badness;
1612}
1613
1614#define DEBUG_BADNESS
1615
1616#ifdef DEBUG_BADNESS
1617#define debug_badness snd_printdd
1618#else
1619#define debug_badness(...)
1620#endif
1621
Takashi Iwaia7694092013-01-21 10:43:18 +01001622#ifdef DEBUG_BADNESS
1623static inline void print_nid_path_idx(struct hda_codec *codec,
1624 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001625{
Takashi Iwaia7694092013-01-21 10:43:18 +01001626 struct nid_path *path;
1627
1628 path = snd_hda_get_path_from_idx(codec, idx);
1629 if (path)
1630 print_nid_path(pfx, path);
1631}
1632
1633static void debug_show_configs(struct hda_codec *codec,
1634 struct auto_pin_cfg *cfg)
1635{
1636 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001637 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001638 int i;
1639
1640 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001641 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001642 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001643 spec->multiout.dac_nids[0],
1644 spec->multiout.dac_nids[1],
1645 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001646 spec->multiout.dac_nids[3],
1647 lo_type[cfg->line_out_type]);
1648 for (i = 0; i < cfg->line_outs; i++)
1649 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001650 if (spec->multi_ios > 0)
1651 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1652 spec->multi_ios,
1653 spec->multi_io[0].pin, spec->multi_io[1].pin,
1654 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001655 for (i = 0; i < spec->multi_ios; i++)
1656 print_nid_path_idx(codec, " mio",
1657 spec->out_paths[cfg->line_outs + i]);
1658 if (cfg->hp_outs)
1659 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001660 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001661 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001662 spec->multiout.hp_out_nid[0],
1663 spec->multiout.hp_out_nid[1],
1664 spec->multiout.hp_out_nid[2],
1665 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001666 for (i = 0; i < cfg->hp_outs; i++)
1667 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1668 if (cfg->speaker_outs)
1669 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001670 cfg->speaker_pins[0], cfg->speaker_pins[1],
1671 cfg->speaker_pins[2], cfg->speaker_pins[3],
1672 spec->multiout.extra_out_nid[0],
1673 spec->multiout.extra_out_nid[1],
1674 spec->multiout.extra_out_nid[2],
1675 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001676 for (i = 0; i < cfg->speaker_outs; i++)
1677 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1678 for (i = 0; i < 3; i++)
1679 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001680}
Takashi Iwaia7694092013-01-21 10:43:18 +01001681#else
1682#define debug_show_configs(codec, cfg) /* NOP */
1683#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001684
1685/* find all available DACs of the codec */
1686static void fill_all_dac_nids(struct hda_codec *codec)
1687{
1688 struct hda_gen_spec *spec = codec->spec;
1689 int i;
1690 hda_nid_t nid = codec->start_nid;
1691
1692 spec->num_all_dacs = 0;
1693 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1694 for (i = 0; i < codec->num_nodes; i++, nid++) {
1695 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1696 continue;
1697 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1698 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1699 break;
1700 }
1701 spec->all_dacs[spec->num_all_dacs++] = nid;
1702 }
1703}
1704
1705static int parse_output_paths(struct hda_codec *codec)
1706{
1707 struct hda_gen_spec *spec = codec->spec;
1708 struct auto_pin_cfg *cfg = &spec->autocfg;
1709 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001710 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001711 int best_badness = INT_MAX;
1712 int badness;
1713 bool fill_hardwired = true, fill_mio_first = true;
1714 bool best_wired = true, best_mio = true;
1715 bool hp_spk_swapped = false;
1716
Takashi Iwai352f7f92012-12-19 12:52:06 +01001717 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1718 if (!best_cfg)
1719 return -ENOMEM;
1720 *best_cfg = *cfg;
1721
1722 for (;;) {
1723 badness = fill_and_eval_dacs(codec, fill_hardwired,
1724 fill_mio_first);
1725 if (badness < 0) {
1726 kfree(best_cfg);
1727 return badness;
1728 }
1729 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1730 cfg->line_out_type, fill_hardwired, fill_mio_first,
1731 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001732 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001733 if (badness < best_badness) {
1734 best_badness = badness;
1735 *best_cfg = *cfg;
1736 best_wired = fill_hardwired;
1737 best_mio = fill_mio_first;
1738 }
1739 if (!badness)
1740 break;
1741 fill_mio_first = !fill_mio_first;
1742 if (!fill_mio_first)
1743 continue;
1744 fill_hardwired = !fill_hardwired;
1745 if (!fill_hardwired)
1746 continue;
1747 if (hp_spk_swapped)
1748 break;
1749 hp_spk_swapped = true;
1750 if (cfg->speaker_outs > 0 &&
1751 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1752 cfg->hp_outs = cfg->line_outs;
1753 memcpy(cfg->hp_pins, cfg->line_out_pins,
1754 sizeof(cfg->hp_pins));
1755 cfg->line_outs = cfg->speaker_outs;
1756 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1757 sizeof(cfg->speaker_pins));
1758 cfg->speaker_outs = 0;
1759 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1760 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1761 fill_hardwired = true;
1762 continue;
1763 }
1764 if (cfg->hp_outs > 0 &&
1765 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1766 cfg->speaker_outs = cfg->line_outs;
1767 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1768 sizeof(cfg->speaker_pins));
1769 cfg->line_outs = cfg->hp_outs;
1770 memcpy(cfg->line_out_pins, cfg->hp_pins,
1771 sizeof(cfg->hp_pins));
1772 cfg->hp_outs = 0;
1773 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1774 cfg->line_out_type = AUTO_PIN_HP_OUT;
1775 fill_hardwired = true;
1776 continue;
1777 }
1778 break;
1779 }
1780
1781 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001782 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001783 *cfg = *best_cfg;
1784 fill_and_eval_dacs(codec, best_wired, best_mio);
1785 }
1786 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1787 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001788 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001789
1790 if (cfg->line_out_pins[0]) {
1791 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001792 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001793 if (path)
1794 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001795 if (spec->vmaster_nid)
1796 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1797 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001798 }
1799
Takashi Iwai9314a582013-01-21 10:49:05 +01001800 /* set initial pinctl targets */
1801 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1802 val = PIN_HP;
1803 else
1804 val = PIN_OUT;
1805 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1806 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1807 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1808 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1809 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1810 set_pin_targets(codec, cfg->speaker_outs,
1811 cfg->speaker_pins, val);
1812 }
1813
Takashi Iwai55a63d42013-03-21 17:20:12 +01001814 /* clear indep_hp flag if not available */
1815 if (spec->indep_hp && !indep_hp_possible(codec))
1816 spec->indep_hp = 0;
1817
Takashi Iwai352f7f92012-12-19 12:52:06 +01001818 kfree(best_cfg);
1819 return 0;
1820}
1821
1822/* add playback controls from the parsed DAC table */
1823static int create_multi_out_ctls(struct hda_codec *codec,
1824 const struct auto_pin_cfg *cfg)
1825{
1826 struct hda_gen_spec *spec = codec->spec;
1827 int i, err, noutputs;
1828
1829 noutputs = cfg->line_outs;
1830 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1831 noutputs += spec->multi_ios;
1832
1833 for (i = 0; i < noutputs; i++) {
1834 const char *name;
1835 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001836 struct nid_path *path;
1837
Takashi Iwai196c17662013-01-04 15:01:40 +01001838 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001839 if (!path)
1840 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001841
1842 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001843 if (!name || !strcmp(name, "CLFE")) {
1844 /* Center/LFE */
1845 err = add_vol_ctl(codec, "Center", 0, 1, path);
1846 if (err < 0)
1847 return err;
1848 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1849 if (err < 0)
1850 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001851 } else {
1852 err = add_stereo_vol(codec, name, index, path);
1853 if (err < 0)
1854 return err;
1855 }
1856
1857 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1858 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001859 err = add_sw_ctl(codec, "Center", 0, 1, path);
1860 if (err < 0)
1861 return err;
1862 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1863 if (err < 0)
1864 return err;
1865 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001866 err = add_stereo_sw(codec, name, index, path);
1867 if (err < 0)
1868 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870 }
1871 return 0;
1872}
1873
Takashi Iwaic2c80382013-01-07 10:33:57 +01001874static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001875 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001877 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 int err;
1879
Takashi Iwai196c17662013-01-04 15:01:40 +01001880 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001881 if (!path)
1882 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001883 err = add_stereo_vol(codec, pfx, cidx, path);
1884 if (err < 0)
1885 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001886 err = add_stereo_sw(codec, pfx, cidx, path);
1887 if (err < 0)
1888 return err;
1889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
Takashi Iwai352f7f92012-12-19 12:52:06 +01001892/* add playback controls for speaker and HP outputs */
1893static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001894 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001896 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001897
1898 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001899 const char *name;
1900 char tmp[44];
1901 int err, idx = 0;
1902
1903 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1904 name = "Bass Speaker";
1905 else if (num_pins >= 3) {
1906 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001907 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001908 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001909 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001910 name = pfx;
1911 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001913 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001914 if (err < 0)
1915 return err;
1916 }
1917 return 0;
1918}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001919
Takashi Iwai352f7f92012-12-19 12:52:06 +01001920static int create_hp_out_ctls(struct hda_codec *codec)
1921{
1922 struct hda_gen_spec *spec = codec->spec;
1923 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001924 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001925 "Headphone");
1926}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Takashi Iwai352f7f92012-12-19 12:52:06 +01001928static int create_speaker_out_ctls(struct hda_codec *codec)
1929{
1930 struct hda_gen_spec *spec = codec->spec;
1931 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001932 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001933 "Speaker");
1934}
1935
1936/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001937 * independent HP controls
1938 */
1939
Takashi Iwai8ba955c2013-03-07 18:40:58 +01001940/* update HP auto-mute state too */
1941static void update_hp_automute_hook(struct hda_codec *codec)
1942{
1943 struct hda_gen_spec *spec = codec->spec;
1944
1945 if (spec->hp_automute_hook)
1946 spec->hp_automute_hook(codec, NULL);
1947 else
1948 snd_hda_gen_hp_automute(codec, NULL);
1949}
1950
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001951static int indep_hp_info(struct snd_kcontrol *kcontrol,
1952 struct snd_ctl_elem_info *uinfo)
1953{
1954 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1955}
1956
1957static int indep_hp_get(struct snd_kcontrol *kcontrol,
1958 struct snd_ctl_elem_value *ucontrol)
1959{
1960 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1961 struct hda_gen_spec *spec = codec->spec;
1962 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1963 return 0;
1964}
1965
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001966static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1967 int nomix_path_idx, int mix_path_idx,
1968 int out_type);
1969
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001970static int indep_hp_put(struct snd_kcontrol *kcontrol,
1971 struct snd_ctl_elem_value *ucontrol)
1972{
1973 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1974 struct hda_gen_spec *spec = codec->spec;
1975 unsigned int select = ucontrol->value.enumerated.item[0];
1976 int ret = 0;
1977
1978 mutex_lock(&spec->pcm_mutex);
1979 if (spec->active_streams) {
1980 ret = -EBUSY;
1981 goto unlock;
1982 }
1983
1984 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001985 hda_nid_t *dacp;
1986 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1987 dacp = &spec->private_dac_nids[0];
1988 else
1989 dacp = &spec->multiout.hp_out_nid[0];
1990
1991 /* update HP aamix paths in case it conflicts with indep HP */
1992 if (spec->have_aamix_ctl) {
1993 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1994 update_aamix_paths(codec, spec->aamix_mode,
1995 spec->out_paths[0],
1996 spec->aamix_out_paths[0],
1997 spec->autocfg.line_out_type);
1998 else
1999 update_aamix_paths(codec, spec->aamix_mode,
2000 spec->hp_paths[0],
2001 spec->aamix_out_paths[1],
2002 AUTO_PIN_HP_OUT);
2003 }
2004
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002005 spec->indep_hp_enabled = select;
2006 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002007 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002008 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002009 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002010
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002011 update_hp_automute_hook(codec);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002012 ret = 1;
2013 }
2014 unlock:
2015 mutex_unlock(&spec->pcm_mutex);
2016 return ret;
2017}
2018
2019static const struct snd_kcontrol_new indep_hp_ctl = {
2020 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2021 .name = "Independent HP",
2022 .info = indep_hp_info,
2023 .get = indep_hp_get,
2024 .put = indep_hp_put,
2025};
2026
2027
2028static int create_indep_hp_ctls(struct hda_codec *codec)
2029{
2030 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002031 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002032
2033 if (!spec->indep_hp)
2034 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002035 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2036 dac = spec->multiout.dac_nids[0];
2037 else
2038 dac = spec->multiout.hp_out_nid[0];
2039 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002040 spec->indep_hp = 0;
2041 return 0;
2042 }
2043
2044 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002045 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002046 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2047 return -ENOMEM;
2048 return 0;
2049}
2050
2051/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002052 * channel mode enum control
2053 */
2054
2055static int ch_mode_info(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_info *uinfo)
2057{
2058 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2059 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002060 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002061
2062 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2063 uinfo->count = 1;
2064 uinfo->value.enumerated.items = spec->multi_ios + 1;
2065 if (uinfo->value.enumerated.item > spec->multi_ios)
2066 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002067 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2068 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002069 return 0;
2070}
2071
2072static int ch_mode_get(struct snd_kcontrol *kcontrol,
2073 struct snd_ctl_elem_value *ucontrol)
2074{
2075 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2076 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002077 ucontrol->value.enumerated.item[0] =
2078 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002079 return 0;
2080}
2081
Takashi Iwai196c17662013-01-04 15:01:40 +01002082static inline struct nid_path *
2083get_multiio_path(struct hda_codec *codec, int idx)
2084{
2085 struct hda_gen_spec *spec = codec->spec;
2086 return snd_hda_get_path_from_idx(codec,
2087 spec->out_paths[spec->autocfg.line_outs + idx]);
2088}
2089
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002090static void update_automute_all(struct hda_codec *codec);
2091
Takashi Iwai352f7f92012-12-19 12:52:06 +01002092static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2093{
2094 struct hda_gen_spec *spec = codec->spec;
2095 hda_nid_t nid = spec->multi_io[idx].pin;
2096 struct nid_path *path;
2097
Takashi Iwai196c17662013-01-04 15:01:40 +01002098 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002099 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002101
2102 if (path->active == output)
2103 return 0;
2104
2105 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002106 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002107 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002108 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002109 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002110 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002111 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002112 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002113 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002115
2116 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002117 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002118
Takashi Iwai352f7f92012-12-19 12:52:06 +01002119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Takashi Iwai352f7f92012-12-19 12:52:06 +01002122static int ch_mode_put(struct snd_kcontrol *kcontrol,
2123 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002125 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2126 struct hda_gen_spec *spec = codec->spec;
2127 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Takashi Iwai352f7f92012-12-19 12:52:06 +01002129 ch = ucontrol->value.enumerated.item[0];
2130 if (ch < 0 || ch > spec->multi_ios)
2131 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002132 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002133 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002134 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002135 for (i = 0; i < spec->multi_ios; i++)
2136 set_multi_io(codec, i, i < ch);
2137 spec->multiout.max_channels = max(spec->ext_channel_count,
2138 spec->const_channel_count);
2139 if (spec->need_dac_fix)
2140 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return 1;
2142}
2143
Takashi Iwai352f7f92012-12-19 12:52:06 +01002144static const struct snd_kcontrol_new channel_mode_enum = {
2145 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2146 .name = "Channel Mode",
2147 .info = ch_mode_info,
2148 .get = ch_mode_get,
2149 .put = ch_mode_put,
2150};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Takashi Iwai352f7f92012-12-19 12:52:06 +01002152static int create_multi_channel_mode(struct hda_codec *codec)
2153{
2154 struct hda_gen_spec *spec = codec->spec;
2155
2156 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002157 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002158 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return 0;
2161}
2162
Takashi Iwai352f7f92012-12-19 12:52:06 +01002163/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002164 * aamix loopback enable/disable switch
2165 */
2166
2167#define loopback_mixing_info indep_hp_info
2168
2169static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2170 struct snd_ctl_elem_value *ucontrol)
2171{
2172 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2173 struct hda_gen_spec *spec = codec->spec;
2174 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2175 return 0;
2176}
2177
2178static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002179 int nomix_path_idx, int mix_path_idx,
2180 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002181{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002182 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002183 struct nid_path *nomix_path, *mix_path;
2184
2185 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2186 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2187 if (!nomix_path || !mix_path)
2188 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002189
2190 /* if HP aamix path is driven from a different DAC and the
2191 * independent HP mode is ON, can't turn on aamix path
2192 */
2193 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2194 mix_path->path[0] != spec->alt_dac_nid)
2195 do_mix = false;
2196
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002197 if (do_mix) {
2198 snd_hda_activate_path(codec, nomix_path, false, true);
2199 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002200 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002201 } else {
2202 snd_hda_activate_path(codec, mix_path, false, true);
2203 snd_hda_activate_path(codec, nomix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002204 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002205 }
2206}
2207
2208static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2209 struct snd_ctl_elem_value *ucontrol)
2210{
2211 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2212 struct hda_gen_spec *spec = codec->spec;
2213 unsigned int val = ucontrol->value.enumerated.item[0];
2214
2215 if (val == spec->aamix_mode)
2216 return 0;
2217 spec->aamix_mode = val;
2218 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002219 spec->aamix_out_paths[0],
2220 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002221 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002222 spec->aamix_out_paths[1],
2223 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002224 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002225 spec->aamix_out_paths[2],
2226 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002227 return 1;
2228}
2229
2230static const struct snd_kcontrol_new loopback_mixing_enum = {
2231 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2232 .name = "Loopback Mixing",
2233 .info = loopback_mixing_info,
2234 .get = loopback_mixing_get,
2235 .put = loopback_mixing_put,
2236};
2237
2238static int create_loopback_mixing_ctl(struct hda_codec *codec)
2239{
2240 struct hda_gen_spec *spec = codec->spec;
2241
2242 if (!spec->mixer_nid)
2243 return 0;
2244 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2245 spec->aamix_out_paths[2]))
2246 return 0;
2247 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2248 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002249 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002250 return 0;
2251}
2252
2253/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002254 * shared headphone/mic handling
2255 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002256
Takashi Iwai352f7f92012-12-19 12:52:06 +01002257static void call_update_outputs(struct hda_codec *codec);
2258
2259/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002260static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002261{
2262 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002263 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002264 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002265 hda_nid_t pin;
2266
2267 pin = spec->hp_mic_pin;
2268 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2269
2270 if (!force) {
2271 val = snd_hda_codec_get_pin_target(codec, pin);
2272 if (as_mic) {
2273 if (val & PIN_IN)
2274 return;
2275 } else {
2276 if (val & PIN_OUT)
2277 return;
2278 }
2279 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002280
2281 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002282 /* if the HP pin doesn't support VREF and the codec driver gives an
2283 * alternative pin, set up the VREF on that pin instead
2284 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002285 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2286 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2287 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2288 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002289 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002290 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002291 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002292
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002293 if (!spec->hp_mic_jack_modes) {
2294 if (as_mic)
2295 val |= PIN_IN;
2296 else
2297 val = PIN_HP;
2298 set_pin_target(codec, pin, val, true);
2299 update_hp_automute_hook(codec);
2300 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002301}
2302
2303/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002304static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002305{
2306 struct hda_gen_spec *spec = codec->spec;
2307 struct auto_pin_cfg *cfg = &spec->autocfg;
2308 unsigned int defcfg;
2309 hda_nid_t nid;
2310
Takashi Iwai967303d2013-02-19 17:12:42 +01002311 if (!spec->hp_mic) {
2312 if (spec->suppress_hp_mic_detect)
2313 return 0;
2314 /* automatic detection: only if no input or a single internal
2315 * input pin is found, try to detect the shared hp/mic
2316 */
2317 if (cfg->num_inputs > 1)
2318 return 0;
2319 else if (cfg->num_inputs == 1) {
2320 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2321 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2322 return 0;
2323 }
2324 }
2325
2326 spec->hp_mic = 0; /* clear once */
2327 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002328 return 0;
2329
Takashi Iwai967303d2013-02-19 17:12:42 +01002330 nid = 0;
2331 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2332 nid = cfg->line_out_pins[0];
2333 else if (cfg->hp_outs > 0)
2334 nid = cfg->hp_pins[0];
2335 if (!nid)
2336 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002337
2338 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2339 return 0; /* no input */
2340
Takashi Iwai967303d2013-02-19 17:12:42 +01002341 cfg->inputs[cfg->num_inputs].pin = nid;
2342 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2343 cfg->num_inputs++;
2344 spec->hp_mic = 1;
2345 spec->hp_mic_pin = nid;
2346 /* we can't handle auto-mic together with HP-mic */
2347 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002348 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2349 return 0;
2350}
2351
Takashi Iwai978e77e2013-01-10 16:57:58 +01002352/*
2353 * output jack mode
2354 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002355
2356static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2357
2358static const char * const out_jack_texts[] = {
2359 "Line Out", "Headphone Out",
2360};
2361
Takashi Iwai978e77e2013-01-10 16:57:58 +01002362static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2363 struct snd_ctl_elem_info *uinfo)
2364{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002365 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002366}
2367
2368static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2369 struct snd_ctl_elem_value *ucontrol)
2370{
2371 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2372 hda_nid_t nid = kcontrol->private_value;
2373 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2374 ucontrol->value.enumerated.item[0] = 1;
2375 else
2376 ucontrol->value.enumerated.item[0] = 0;
2377 return 0;
2378}
2379
2380static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2381 struct snd_ctl_elem_value *ucontrol)
2382{
2383 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2384 hda_nid_t nid = kcontrol->private_value;
2385 unsigned int val;
2386
2387 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2388 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2389 return 0;
2390 snd_hda_set_pin_ctl_cache(codec, nid, val);
2391 return 1;
2392}
2393
2394static const struct snd_kcontrol_new out_jack_mode_enum = {
2395 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2396 .info = out_jack_mode_info,
2397 .get = out_jack_mode_get,
2398 .put = out_jack_mode_put,
2399};
2400
2401static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2402{
2403 struct hda_gen_spec *spec = codec->spec;
2404 int i;
2405
2406 for (i = 0; i < spec->kctls.used; i++) {
2407 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2408 if (!strcmp(kctl->name, name) && kctl->index == idx)
2409 return true;
2410 }
2411 return false;
2412}
2413
2414static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2415 char *name, size_t name_len)
2416{
2417 struct hda_gen_spec *spec = codec->spec;
2418 int idx = 0;
2419
2420 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2421 strlcat(name, " Jack Mode", name_len);
2422
2423 for (; find_kctl_name(codec, name, idx); idx++)
2424 ;
2425}
2426
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002427static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2428{
2429 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002430 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002431 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2432 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2433 return 2;
2434 }
2435 return 1;
2436}
2437
Takashi Iwai978e77e2013-01-10 16:57:58 +01002438static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2439 hda_nid_t *pins)
2440{
2441 struct hda_gen_spec *spec = codec->spec;
2442 int i;
2443
2444 for (i = 0; i < num_pins; i++) {
2445 hda_nid_t pin = pins[i];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002446 if (pin == spec->hp_mic_pin) {
2447 int ret = create_hp_mic_jack_mode(codec, pin);
2448 if (ret < 0)
2449 return ret;
2450 continue;
2451 }
2452 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002453 struct snd_kcontrol_new *knew;
2454 char name[44];
2455 get_jack_mode_name(codec, pin, name, sizeof(name));
2456 knew = snd_hda_gen_add_kctl(spec, name,
2457 &out_jack_mode_enum);
2458 if (!knew)
2459 return -ENOMEM;
2460 knew->private_value = pin;
2461 }
2462 }
2463
2464 return 0;
2465}
2466
Takashi Iwai294765582013-01-17 09:52:11 +01002467/*
2468 * input jack mode
2469 */
2470
2471/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2472#define NUM_VREFS 6
2473
2474static const char * const vref_texts[NUM_VREFS] = {
2475 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2476 "", "Mic 80pc Bias", "Mic 100pc Bias"
2477};
2478
2479static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2480{
2481 unsigned int pincap;
2482
2483 pincap = snd_hda_query_pin_caps(codec, pin);
2484 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2485 /* filter out unusual vrefs */
2486 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2487 return pincap;
2488}
2489
2490/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2491static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2492{
2493 unsigned int i, n = 0;
2494
2495 for (i = 0; i < NUM_VREFS; i++) {
2496 if (vref_caps & (1 << i)) {
2497 if (n == item_idx)
2498 return i;
2499 n++;
2500 }
2501 }
2502 return 0;
2503}
2504
2505/* convert back from the vref ctl index to the enum item index */
2506static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2507{
2508 unsigned int i, n = 0;
2509
2510 for (i = 0; i < NUM_VREFS; i++) {
2511 if (i == idx)
2512 return n;
2513 if (vref_caps & (1 << i))
2514 n++;
2515 }
2516 return 0;
2517}
2518
2519static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2520 struct snd_ctl_elem_info *uinfo)
2521{
2522 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2523 hda_nid_t nid = kcontrol->private_value;
2524 unsigned int vref_caps = get_vref_caps(codec, nid);
2525
2526 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2527 vref_texts);
2528 /* set the right text */
2529 strcpy(uinfo->value.enumerated.name,
2530 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2531 return 0;
2532}
2533
2534static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2535 struct snd_ctl_elem_value *ucontrol)
2536{
2537 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2538 hda_nid_t nid = kcontrol->private_value;
2539 unsigned int vref_caps = get_vref_caps(codec, nid);
2540 unsigned int idx;
2541
2542 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2543 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2544 return 0;
2545}
2546
2547static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2548 struct snd_ctl_elem_value *ucontrol)
2549{
2550 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2551 hda_nid_t nid = kcontrol->private_value;
2552 unsigned int vref_caps = get_vref_caps(codec, nid);
2553 unsigned int val, idx;
2554
2555 val = snd_hda_codec_get_pin_target(codec, nid);
2556 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2557 if (idx == ucontrol->value.enumerated.item[0])
2558 return 0;
2559
2560 val &= ~AC_PINCTL_VREFEN;
2561 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2562 snd_hda_set_pin_ctl_cache(codec, nid, val);
2563 return 1;
2564}
2565
2566static const struct snd_kcontrol_new in_jack_mode_enum = {
2567 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2568 .info = in_jack_mode_info,
2569 .get = in_jack_mode_get,
2570 .put = in_jack_mode_put,
2571};
2572
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002573static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2574{
2575 struct hda_gen_spec *spec = codec->spec;
2576 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002577 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002578 nitems = hweight32(get_vref_caps(codec, pin));
2579 return nitems ? nitems : 1;
2580}
2581
Takashi Iwai294765582013-01-17 09:52:11 +01002582static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2583{
2584 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002585 struct snd_kcontrol_new *knew;
2586 char name[44];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002587 unsigned int defcfg;
2588
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002589 if (pin == spec->hp_mic_pin)
2590 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002591
2592 /* no jack mode for fixed pins */
2593 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2594 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2595 return 0;
2596
2597 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002598 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002599 return 0;
2600
2601 get_jack_mode_name(codec, pin, name, sizeof(name));
2602 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2603 if (!knew)
2604 return -ENOMEM;
2605 knew->private_value = pin;
2606 return 0;
2607}
2608
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002609/*
2610 * HP/mic shared jack mode
2611 */
2612static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2613 struct snd_ctl_elem_info *uinfo)
2614{
2615 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2616 hda_nid_t nid = kcontrol->private_value;
2617 int out_jacks = get_out_jack_num_items(codec, nid);
2618 int in_jacks = get_in_jack_num_items(codec, nid);
2619 const char *text = NULL;
2620 int idx;
2621
2622 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2623 uinfo->count = 1;
2624 uinfo->value.enumerated.items = out_jacks + in_jacks;
2625 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2626 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2627 idx = uinfo->value.enumerated.item;
2628 if (idx < out_jacks) {
2629 if (out_jacks > 1)
2630 text = out_jack_texts[idx];
2631 else
2632 text = "Headphone Out";
2633 } else {
2634 idx -= out_jacks;
2635 if (in_jacks > 1) {
2636 unsigned int vref_caps = get_vref_caps(codec, nid);
2637 text = vref_texts[get_vref_idx(vref_caps, idx)];
2638 } else
2639 text = "Mic In";
2640 }
2641
2642 strcpy(uinfo->value.enumerated.name, text);
2643 return 0;
2644}
2645
2646static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2647{
2648 int out_jacks = get_out_jack_num_items(codec, nid);
2649 int in_jacks = get_in_jack_num_items(codec, nid);
2650 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2651 int idx = 0;
2652
2653 if (val & PIN_OUT) {
2654 if (out_jacks > 1 && val == PIN_HP)
2655 idx = 1;
2656 } else if (val & PIN_IN) {
2657 idx = out_jacks;
2658 if (in_jacks > 1) {
2659 unsigned int vref_caps = get_vref_caps(codec, nid);
2660 val &= AC_PINCTL_VREFEN;
2661 idx += cvt_from_vref_idx(vref_caps, val);
2662 }
2663 }
2664 return idx;
2665}
2666
2667static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2668 struct snd_ctl_elem_value *ucontrol)
2669{
2670 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2671 hda_nid_t nid = kcontrol->private_value;
2672 ucontrol->value.enumerated.item[0] =
2673 get_cur_hp_mic_jack_mode(codec, nid);
2674 return 0;
2675}
2676
2677static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2678 struct snd_ctl_elem_value *ucontrol)
2679{
2680 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2681 hda_nid_t nid = kcontrol->private_value;
2682 int out_jacks = get_out_jack_num_items(codec, nid);
2683 int in_jacks = get_in_jack_num_items(codec, nid);
2684 unsigned int val, oldval, idx;
2685
2686 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2687 idx = ucontrol->value.enumerated.item[0];
2688 if (oldval == idx)
2689 return 0;
2690
2691 if (idx < out_jacks) {
2692 if (out_jacks > 1)
2693 val = idx ? PIN_HP : PIN_OUT;
2694 else
2695 val = PIN_HP;
2696 } else {
2697 idx -= out_jacks;
2698 if (in_jacks > 1) {
2699 unsigned int vref_caps = get_vref_caps(codec, nid);
2700 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002701 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2702 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002703 } else
2704 val = snd_hda_get_default_vref(codec, nid);
2705 }
2706 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002707 update_hp_automute_hook(codec);
2708
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002709 return 1;
2710}
2711
2712static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2713 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2714 .info = hp_mic_jack_mode_info,
2715 .get = hp_mic_jack_mode_get,
2716 .put = hp_mic_jack_mode_put,
2717};
2718
2719static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2720{
2721 struct hda_gen_spec *spec = codec->spec;
2722 struct snd_kcontrol_new *knew;
2723
2724 if (get_out_jack_num_items(codec, pin) <= 1 &&
2725 get_in_jack_num_items(codec, pin) <= 1)
2726 return 0; /* no need */
2727 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2728 &hp_mic_jack_mode_enum);
2729 if (!knew)
2730 return -ENOMEM;
2731 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002732 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002733 return 0;
2734}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002735
2736/*
2737 * Parse input paths
2738 */
2739
Takashi Iwai352f7f92012-12-19 12:52:06 +01002740/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002741static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002742{
2743 struct hda_amp_list *list;
2744
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002745 list = snd_array_new(&spec->loopback_list);
2746 if (!list)
2747 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002748 list->nid = mix;
2749 list->dir = HDA_INPUT;
2750 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002751 spec->loopback.amplist = spec->loopback_list.list;
2752 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002753}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002754
Takashi Iwai352f7f92012-12-19 12:52:06 +01002755/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002756static int new_analog_input(struct hda_codec *codec, int input_idx,
2757 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002758 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002760 struct hda_gen_spec *spec = codec->spec;
2761 struct nid_path *path;
2762 unsigned int val;
2763 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
Takashi Iwai352f7f92012-12-19 12:52:06 +01002765 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2766 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2767 return 0; /* no need for analog loopback */
2768
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002769 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002770 if (!path)
2771 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002772 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002773 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002774
2775 idx = path->idx[path->depth - 1];
2776 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2777 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2778 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002779 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002781 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 }
2783
Takashi Iwai352f7f92012-12-19 12:52:06 +01002784 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2785 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2786 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002787 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002789 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
2791
Takashi Iwai352f7f92012-12-19 12:52:06 +01002792 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002793 err = add_loopback_list(spec, mix_nid, idx);
2794 if (err < 0)
2795 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002796
2797 if (spec->mixer_nid != spec->mixer_merge_nid &&
2798 !spec->loopback_merge_path) {
2799 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2800 spec->mixer_merge_nid, 0);
2801 if (path) {
2802 print_nid_path("loopback-merge", path);
2803 path->active = true;
2804 spec->loopback_merge_path =
2805 snd_hda_get_path_idx(codec, path);
2806 }
2807 }
2808
Takashi Iwai352f7f92012-12-19 12:52:06 +01002809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810}
2811
Takashi Iwai352f7f92012-12-19 12:52:06 +01002812static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002814 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2815 return (pincap & AC_PINCAP_IN) != 0;
2816}
2817
2818/* Parse the codec tree and retrieve ADCs */
2819static int fill_adc_nids(struct hda_codec *codec)
2820{
2821 struct hda_gen_spec *spec = codec->spec;
2822 hda_nid_t nid;
2823 hda_nid_t *adc_nids = spec->adc_nids;
2824 int max_nums = ARRAY_SIZE(spec->adc_nids);
2825 int i, nums = 0;
2826
2827 nid = codec->start_nid;
2828 for (i = 0; i < codec->num_nodes; i++, nid++) {
2829 unsigned int caps = get_wcaps(codec, nid);
2830 int type = get_wcaps_type(caps);
2831
2832 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2833 continue;
2834 adc_nids[nums] = nid;
2835 if (++nums >= max_nums)
2836 break;
2837 }
2838 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002839
2840 /* copy the detected ADCs to all_adcs[] */
2841 spec->num_all_adcs = nums;
2842 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2843
Takashi Iwai352f7f92012-12-19 12:52:06 +01002844 return nums;
2845}
2846
2847/* filter out invalid adc_nids that don't give all active input pins;
2848 * if needed, check whether dynamic ADC-switching is available
2849 */
2850static int check_dyn_adc_switch(struct hda_codec *codec)
2851{
2852 struct hda_gen_spec *spec = codec->spec;
2853 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002854 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002855 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002856
Takashi Iwai352f7f92012-12-19 12:52:06 +01002857 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002858 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002859 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002860 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002861 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002862 break;
2863 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002864 if (i >= imux->num_items) {
2865 ok_bits |= (1 << n);
2866 nums++;
2867 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002868 }
2869
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002870 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002871 /* check whether ADC-switch is possible */
2872 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002873 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002874 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002875 spec->dyn_adc_idx[i] = n;
2876 break;
2877 }
2878 }
2879 }
2880
2881 snd_printdd("hda-codec: enabling ADC switching\n");
2882 spec->dyn_adc_switch = 1;
2883 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002884 /* shrink the invalid adcs and input paths */
2885 nums = 0;
2886 for (n = 0; n < spec->num_adc_nids; n++) {
2887 if (!(ok_bits & (1 << n)))
2888 continue;
2889 if (n != nums) {
2890 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002891 for (i = 0; i < imux->num_items; i++) {
2892 invalidate_nid_path(codec,
2893 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002894 spec->input_paths[i][nums] =
2895 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002896 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002897 }
2898 nums++;
2899 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002900 spec->num_adc_nids = nums;
2901 }
2902
Takashi Iwai967303d2013-02-19 17:12:42 +01002903 if (imux->num_items == 1 ||
2904 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002905 snd_printdd("hda-codec: reducing to a single ADC\n");
2906 spec->num_adc_nids = 1; /* reduce to a single ADC */
2907 }
2908
2909 /* single index for individual volumes ctls */
2910 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2911 spec->num_adc_nids = 1;
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 return 0;
2914}
2915
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002916/* parse capture source paths from the given pin and create imux items */
2917static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002918 int cfg_idx, int num_adcs,
2919 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002920{
2921 struct hda_gen_spec *spec = codec->spec;
2922 struct hda_input_mux *imux = &spec->input_mux;
2923 int imux_idx = imux->num_items;
2924 bool imux_added = false;
2925 int c;
2926
2927 for (c = 0; c < num_adcs; c++) {
2928 struct nid_path *path;
2929 hda_nid_t adc = spec->adc_nids[c];
2930
2931 if (!is_reachable_path(codec, pin, adc))
2932 continue;
2933 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2934 if (!path)
2935 continue;
2936 print_nid_path("input", path);
2937 spec->input_paths[imux_idx][c] =
2938 snd_hda_get_path_idx(codec, path);
2939
2940 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01002941 if (spec->hp_mic_pin == pin)
2942 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002943 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002944 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002945 imux_added = true;
2946 }
2947 }
2948
2949 return 0;
2950}
2951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002953 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002955
Takashi Iwaic9700422013-01-18 10:17:30 +01002956/* fill the label for each input at first */
2957static int fill_input_pin_labels(struct hda_codec *codec)
2958{
2959 struct hda_gen_spec *spec = codec->spec;
2960 const struct auto_pin_cfg *cfg = &spec->autocfg;
2961 int i;
2962
2963 for (i = 0; i < cfg->num_inputs; i++) {
2964 hda_nid_t pin = cfg->inputs[i].pin;
2965 const char *label;
2966 int j, idx;
2967
2968 if (!is_input_pin(codec, pin))
2969 continue;
2970
2971 label = hda_get_autocfg_input_label(codec, cfg, i);
2972 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002973 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002974 if (spec->input_labels[j] &&
2975 !strcmp(spec->input_labels[j], label)) {
2976 idx = spec->input_label_idxs[j] + 1;
2977 break;
2978 }
2979 }
2980
2981 spec->input_labels[i] = label;
2982 spec->input_label_idxs[i] = idx;
2983 }
2984
2985 return 0;
2986}
2987
Takashi Iwai9dba2052013-01-18 10:01:15 +01002988#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2989
Takashi Iwai352f7f92012-12-19 12:52:06 +01002990static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002991{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002992 struct hda_gen_spec *spec = codec->spec;
2993 const struct auto_pin_cfg *cfg = &spec->autocfg;
2994 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002995 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002996 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002997 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002998
Takashi Iwai352f7f92012-12-19 12:52:06 +01002999 num_adcs = fill_adc_nids(codec);
3000 if (num_adcs < 0)
3001 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003002
Takashi Iwaic9700422013-01-18 10:17:30 +01003003 err = fill_input_pin_labels(codec);
3004 if (err < 0)
3005 return err;
3006
Takashi Iwai352f7f92012-12-19 12:52:06 +01003007 for (i = 0; i < cfg->num_inputs; i++) {
3008 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Takashi Iwai352f7f92012-12-19 12:52:06 +01003010 pin = cfg->inputs[i].pin;
3011 if (!is_input_pin(codec, pin))
3012 continue;
3013
Takashi Iwai2c12c302013-01-10 09:33:29 +01003014 val = PIN_IN;
3015 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3016 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003017 if (pin != spec->hp_mic_pin)
3018 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003019
Takashi Iwai352f7f92012-12-19 12:52:06 +01003020 if (mixer) {
3021 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003022 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003023 spec->input_labels[i],
3024 spec->input_label_idxs[i],
3025 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003026 if (err < 0)
3027 return err;
3028 }
3029 }
3030
Takashi Iwaic9700422013-01-18 10:17:30 +01003031 err = parse_capture_source(codec, pin, i, num_adcs,
3032 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003033 if (err < 0)
3034 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003035
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003036 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003037 err = create_in_jack_mode(codec, pin);
3038 if (err < 0)
3039 return err;
3040 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003041 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003042
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003043 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003044 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003045 "Stereo Mix", 0);
3046 if (err < 0)
3047 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003048 }
3049
3050 return 0;
3051}
3052
3053
3054/*
3055 * input source mux
3056 */
3057
Takashi Iwaic697b712013-01-07 17:09:26 +01003058/* get the input path specified by the given adc and imux indices */
3059static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003060{
3061 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003062 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3063 snd_BUG();
3064 return NULL;
3065 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003066 if (spec->dyn_adc_switch)
3067 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003068 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003069 snd_BUG();
3070 return NULL;
3071 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003072 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003073}
3074
3075static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3076 unsigned int idx);
3077
3078static int mux_enum_info(struct snd_kcontrol *kcontrol,
3079 struct snd_ctl_elem_info *uinfo)
3080{
3081 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3082 struct hda_gen_spec *spec = codec->spec;
3083 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3084}
3085
3086static int mux_enum_get(struct snd_kcontrol *kcontrol,
3087 struct snd_ctl_elem_value *ucontrol)
3088{
3089 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3090 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003091 /* the ctls are created at once with multiple counts */
3092 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003093
3094 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3095 return 0;
3096}
3097
3098static int mux_enum_put(struct snd_kcontrol *kcontrol,
3099 struct snd_ctl_elem_value *ucontrol)
3100{
3101 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003102 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003103 return mux_select(codec, adc_idx,
3104 ucontrol->value.enumerated.item[0]);
3105}
3106
Takashi Iwai352f7f92012-12-19 12:52:06 +01003107static const struct snd_kcontrol_new cap_src_temp = {
3108 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3109 .name = "Input Source",
3110 .info = mux_enum_info,
3111 .get = mux_enum_get,
3112 .put = mux_enum_put,
3113};
3114
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003115/*
3116 * capture volume and capture switch ctls
3117 */
3118
Takashi Iwai352f7f92012-12-19 12:52:06 +01003119typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3120 struct snd_ctl_elem_value *ucontrol);
3121
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003122/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003123static int cap_put_caller(struct snd_kcontrol *kcontrol,
3124 struct snd_ctl_elem_value *ucontrol,
3125 put_call_t func, int type)
3126{
3127 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3128 struct hda_gen_spec *spec = codec->spec;
3129 const struct hda_input_mux *imux;
3130 struct nid_path *path;
3131 int i, adc_idx, err = 0;
3132
3133 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003134 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003135 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003136 /* we use the cache-only update at first since multiple input paths
3137 * may shared the same amp; by updating only caches, the redundant
3138 * writes to hardware can be reduced.
3139 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003140 codec->cached_write = 1;
3141 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003142 path = get_input_path(codec, adc_idx, i);
3143 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003144 continue;
3145 kcontrol->private_value = path->ctls[type];
3146 err = func(kcontrol, ucontrol);
3147 if (err < 0)
3148 goto error;
3149 }
3150 error:
3151 codec->cached_write = 0;
3152 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003153 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003154 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003155 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003156 return err;
3157}
3158
3159/* capture volume ctl callbacks */
3160#define cap_vol_info snd_hda_mixer_amp_volume_info
3161#define cap_vol_get snd_hda_mixer_amp_volume_get
3162#define cap_vol_tlv snd_hda_mixer_amp_tlv
3163
3164static int cap_vol_put(struct snd_kcontrol *kcontrol,
3165 struct snd_ctl_elem_value *ucontrol)
3166{
3167 return cap_put_caller(kcontrol, ucontrol,
3168 snd_hda_mixer_amp_volume_put,
3169 NID_PATH_VOL_CTL);
3170}
3171
3172static const struct snd_kcontrol_new cap_vol_temp = {
3173 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3174 .name = "Capture Volume",
3175 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3176 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3177 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3178 .info = cap_vol_info,
3179 .get = cap_vol_get,
3180 .put = cap_vol_put,
3181 .tlv = { .c = cap_vol_tlv },
3182};
3183
3184/* capture switch ctl callbacks */
3185#define cap_sw_info snd_ctl_boolean_stereo_info
3186#define cap_sw_get snd_hda_mixer_amp_switch_get
3187
3188static int cap_sw_put(struct snd_kcontrol *kcontrol,
3189 struct snd_ctl_elem_value *ucontrol)
3190{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003191 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003192 snd_hda_mixer_amp_switch_put,
3193 NID_PATH_MUTE_CTL);
3194}
3195
3196static const struct snd_kcontrol_new cap_sw_temp = {
3197 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3198 .name = "Capture Switch",
3199 .info = cap_sw_info,
3200 .get = cap_sw_get,
3201 .put = cap_sw_put,
3202};
3203
3204static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3205{
3206 hda_nid_t nid;
3207 int i, depth;
3208
3209 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3210 for (depth = 0; depth < 3; depth++) {
3211 if (depth >= path->depth)
3212 return -EINVAL;
3213 i = path->depth - depth - 1;
3214 nid = path->path[i];
3215 if (!path->ctls[NID_PATH_VOL_CTL]) {
3216 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3217 path->ctls[NID_PATH_VOL_CTL] =
3218 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3219 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3220 int idx = path->idx[i];
3221 if (!depth && codec->single_adc_amp)
3222 idx = 0;
3223 path->ctls[NID_PATH_VOL_CTL] =
3224 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3225 }
3226 }
3227 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3228 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3229 path->ctls[NID_PATH_MUTE_CTL] =
3230 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3231 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3232 int idx = path->idx[i];
3233 if (!depth && codec->single_adc_amp)
3234 idx = 0;
3235 path->ctls[NID_PATH_MUTE_CTL] =
3236 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3237 }
3238 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return 0;
3241}
3242
Takashi Iwai352f7f92012-12-19 12:52:06 +01003243static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003245 struct hda_gen_spec *spec = codec->spec;
3246 struct auto_pin_cfg *cfg = &spec->autocfg;
3247 unsigned int val;
3248 int i;
3249
3250 if (!spec->inv_dmic_split)
3251 return false;
3252 for (i = 0; i < cfg->num_inputs; i++) {
3253 if (cfg->inputs[i].pin != nid)
3254 continue;
3255 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3256 return false;
3257 val = snd_hda_codec_get_pincfg(codec, nid);
3258 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3259 }
3260 return false;
3261}
3262
Takashi Iwaia90229e2013-01-18 14:10:00 +01003263/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003264static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3265 struct snd_ctl_elem_value *ucontrol)
3266{
3267 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3268 struct hda_gen_spec *spec = codec->spec;
3269 int ret;
3270
3271 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3272 if (ret < 0)
3273 return ret;
3274
Takashi Iwaia90229e2013-01-18 14:10:00 +01003275 if (spec->cap_sync_hook)
3276 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003277
3278 return ret;
3279}
3280
Takashi Iwai352f7f92012-12-19 12:52:06 +01003281static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3282 int idx, bool is_switch, unsigned int ctl,
3283 bool inv_dmic)
3284{
3285 struct hda_gen_spec *spec = codec->spec;
3286 char tmpname[44];
3287 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3288 const char *sfx = is_switch ? "Switch" : "Volume";
3289 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003290 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003291
3292 if (!ctl)
3293 return 0;
3294
3295 if (label)
3296 snprintf(tmpname, sizeof(tmpname),
3297 "%s Capture %s", label, sfx);
3298 else
3299 snprintf(tmpname, sizeof(tmpname),
3300 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003301 knew = add_control(spec, type, tmpname, idx,
3302 amp_val_replace_channels(ctl, chs));
3303 if (!knew)
3304 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003305 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003306 knew->put = cap_single_sw_put;
3307 if (!inv_dmic)
3308 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003309
3310 /* Make independent right kcontrol */
3311 if (label)
3312 snprintf(tmpname, sizeof(tmpname),
3313 "Inverted %s Capture %s", label, sfx);
3314 else
3315 snprintf(tmpname, sizeof(tmpname),
3316 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003317 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003318 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003319 if (!knew)
3320 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003321 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003322 knew->put = cap_single_sw_put;
3323 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003324}
3325
3326/* create single (and simple) capture volume and switch controls */
3327static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3328 unsigned int vol_ctl, unsigned int sw_ctl,
3329 bool inv_dmic)
3330{
3331 int err;
3332 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3333 if (err < 0)
3334 return err;
3335 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3336 if (err < 0)
3337 return err;
3338 return 0;
3339}
3340
3341/* create bound capture volume and switch controls */
3342static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3343 unsigned int vol_ctl, unsigned int sw_ctl)
3344{
3345 struct hda_gen_spec *spec = codec->spec;
3346 struct snd_kcontrol_new *knew;
3347
3348 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003349 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003350 if (!knew)
3351 return -ENOMEM;
3352 knew->index = idx;
3353 knew->private_value = vol_ctl;
3354 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3355 }
3356 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003357 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003358 if (!knew)
3359 return -ENOMEM;
3360 knew->index = idx;
3361 knew->private_value = sw_ctl;
3362 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3363 }
3364 return 0;
3365}
3366
3367/* return the vol ctl when used first in the imux list */
3368static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3369{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003370 struct nid_path *path;
3371 unsigned int ctl;
3372 int i;
3373
Takashi Iwaic697b712013-01-07 17:09:26 +01003374 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003375 if (!path)
3376 return 0;
3377 ctl = path->ctls[type];
3378 if (!ctl)
3379 return 0;
3380 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003381 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003382 if (path && path->ctls[type] == ctl)
3383 return 0;
3384 }
3385 return ctl;
3386}
3387
3388/* create individual capture volume and switch controls per input */
3389static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3390{
3391 struct hda_gen_spec *spec = codec->spec;
3392 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003393 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003394
3395 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003396 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003397 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003398
Takashi Iwaic9700422013-01-18 10:17:30 +01003399 idx = imux->items[i].index;
3400 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003401 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003402 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3403
3404 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003405 err = add_single_cap_ctl(codec,
3406 spec->input_labels[idx],
3407 spec->input_label_idxs[idx],
3408 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003409 get_first_cap_ctl(codec, i, type),
3410 inv_dmic);
3411 if (err < 0)
3412 return err;
3413 }
3414 }
3415 return 0;
3416}
3417
3418static int create_capture_mixers(struct hda_codec *codec)
3419{
3420 struct hda_gen_spec *spec = codec->spec;
3421 struct hda_input_mux *imux = &spec->input_mux;
3422 int i, n, nums, err;
3423
3424 if (spec->dyn_adc_switch)
3425 nums = 1;
3426 else
3427 nums = spec->num_adc_nids;
3428
3429 if (!spec->auto_mic && imux->num_items > 1) {
3430 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003431 const char *name;
3432 name = nums > 1 ? "Input Source" : "Capture Source";
3433 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003434 if (!knew)
3435 return -ENOMEM;
3436 knew->count = nums;
3437 }
3438
3439 for (n = 0; n < nums; n++) {
3440 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003441 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003442 bool inv_dmic = false;
3443 int vol, sw;
3444
3445 vol = sw = 0;
3446 for (i = 0; i < imux->num_items; i++) {
3447 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003448 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003449 if (!path)
3450 continue;
3451 parse_capvol_in_path(codec, path);
3452 if (!vol)
3453 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003454 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003455 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003456 if (!same_amp_caps(codec, vol,
3457 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3458 multi_cap_vol = true;
3459 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003460 if (!sw)
3461 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003462 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003463 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003464 if (!same_amp_caps(codec, sw,
3465 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3466 multi_cap_vol = true;
3467 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003468 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3469 inv_dmic = true;
3470 }
3471
3472 if (!multi)
3473 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3474 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003475 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003476 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3477 else
3478 err = create_multi_cap_vol_ctl(codec);
3479 if (err < 0)
3480 return err;
3481 }
3482
3483 return 0;
3484}
3485
3486/*
3487 * add mic boosts if needed
3488 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003489
3490/* check whether the given amp is feasible as a boost volume */
3491static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3492 int dir, int idx)
3493{
3494 unsigned int step;
3495
3496 if (!nid_has_volume(codec, nid, dir) ||
3497 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3498 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3499 return false;
3500
3501 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3502 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3503 if (step < 0x20)
3504 return false;
3505 return true;
3506}
3507
3508/* look for a boost amp in a widget close to the pin */
3509static unsigned int look_for_boost_amp(struct hda_codec *codec,
3510 struct nid_path *path)
3511{
3512 unsigned int val = 0;
3513 hda_nid_t nid;
3514 int depth;
3515
3516 for (depth = 0; depth < 3; depth++) {
3517 if (depth >= path->depth - 1)
3518 break;
3519 nid = path->path[depth];
3520 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3521 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3522 break;
3523 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3524 path->idx[depth])) {
3525 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3526 HDA_INPUT);
3527 break;
3528 }
3529 }
3530
3531 return val;
3532}
3533
Takashi Iwai352f7f92012-12-19 12:52:06 +01003534static int parse_mic_boost(struct hda_codec *codec)
3535{
3536 struct hda_gen_spec *spec = codec->spec;
3537 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003538 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003539 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003540
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003541 if (!spec->num_adc_nids)
3542 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003543
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003544 for (i = 0; i < imux->num_items; i++) {
3545 struct nid_path *path;
3546 unsigned int val;
3547 int idx;
3548 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003549
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003550 idx = imux->items[i].index;
3551 if (idx >= imux->num_items)
3552 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003553
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003554 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003555 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003556 continue;
3557
3558 path = get_input_path(codec, 0, i);
3559 if (!path)
3560 continue;
3561
3562 val = look_for_boost_amp(codec, path);
3563 if (!val)
3564 continue;
3565
3566 /* create a boost control */
3567 snprintf(boost_label, sizeof(boost_label),
3568 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003569 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3570 spec->input_label_idxs[idx], val))
3571 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003572
3573 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003574 }
3575 return 0;
3576}
3577
3578/*
3579 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3580 */
3581static void parse_digital(struct hda_codec *codec)
3582{
3583 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003584 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003585 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003586 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003587
3588 /* support multiple SPDIFs; the secondary is set up as a slave */
3589 nums = 0;
3590 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003591 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003592 dig_nid = look_for_dac(codec, pin, true);
3593 if (!dig_nid)
3594 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003595 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003596 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003597 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003598 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003599 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003600 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003601 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003602 if (!nums) {
3603 spec->multiout.dig_out_nid = dig_nid;
3604 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3605 } else {
3606 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3607 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3608 break;
3609 spec->slave_dig_outs[nums - 1] = dig_nid;
3610 }
3611 nums++;
3612 }
3613
3614 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003615 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003616 dig_nid = codec->start_nid;
3617 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003618 unsigned int wcaps = get_wcaps(codec, dig_nid);
3619 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3620 continue;
3621 if (!(wcaps & AC_WCAP_DIGITAL))
3622 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003623 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003624 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003625 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003626 path->active = true;
3627 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003628 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003629 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003630 break;
3631 }
3632 }
3633 }
3634}
3635
3636
3637/*
3638 * input MUX handling
3639 */
3640
3641static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3642
3643/* select the given imux item; either unmute exclusively or select the route */
3644static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3645 unsigned int idx)
3646{
3647 struct hda_gen_spec *spec = codec->spec;
3648 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003649 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003650
3651 imux = &spec->input_mux;
3652 if (!imux->num_items)
3653 return 0;
3654
3655 if (idx >= imux->num_items)
3656 idx = imux->num_items - 1;
3657 if (spec->cur_mux[adc_idx] == idx)
3658 return 0;
3659
Takashi Iwai55196ff2013-01-24 17:32:56 +01003660 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3661 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003662 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003663 if (old_path->active)
3664 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003665
3666 spec->cur_mux[adc_idx] = idx;
3667
Takashi Iwai967303d2013-02-19 17:12:42 +01003668 if (spec->hp_mic)
3669 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003670
3671 if (spec->dyn_adc_switch)
3672 dyn_adc_pcm_resetup(codec, idx);
3673
Takashi Iwaic697b712013-01-07 17:09:26 +01003674 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003675 if (!path)
3676 return 0;
3677 if (path->active)
3678 return 0;
3679 snd_hda_activate_path(codec, path, true, false);
3680 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003681 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003682 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003683 return 1;
3684}
3685
3686
3687/*
3688 * Jack detections for HP auto-mute and mic-switch
3689 */
3690
3691/* check each pin in the given array; returns true if any of them is plugged */
3692static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3693{
3694 int i, present = 0;
3695
3696 for (i = 0; i < num_pins; i++) {
3697 hda_nid_t nid = pins[i];
3698 if (!nid)
3699 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003700 /* don't detect pins retasked as inputs */
3701 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3702 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003703 present |= snd_hda_jack_detect(codec, nid);
3704 }
3705 return present;
3706}
3707
3708/* standard HP/line-out auto-mute helper */
3709static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003710 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003711{
3712 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003713 int i;
3714
3715 for (i = 0; i < num_pins; i++) {
3716 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003717 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003718 if (!nid)
3719 break;
Takashi Iwai967303d2013-02-19 17:12:42 +01003720 oldval = snd_hda_codec_get_pin_target(codec, nid);
3721 if (oldval & PIN_IN)
3722 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003723 /* don't reset VREF value in case it's controlling
3724 * the amp (see alc861_fixup_asus_amp_vref_0f())
3725 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003726 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003727 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003728 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003729 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003730 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003731 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003732 /* here we call update_pin_ctl() so that the pinctl is changed
3733 * without changing the pinctl target value;
3734 * the original target value will be still referred at the
3735 * init / resume again
3736 */
3737 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003738 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003739 }
3740}
3741
3742/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003743void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003744{
3745 struct hda_gen_spec *spec = codec->spec;
3746 int on;
3747
3748 /* Control HP pins/amps depending on master_mute state;
3749 * in general, HP pins/amps control should be enabled in all cases,
3750 * but currently set only for master_mute, just to be safe
3751 */
Takashi Iwai967303d2013-02-19 17:12:42 +01003752 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003753 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003754
3755 if (!spec->automute_speaker)
3756 on = 0;
3757 else
3758 on = spec->hp_jack_present | spec->line_jack_present;
3759 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003760 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003761 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003762 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003763
3764 /* toggle line-out mutes if needed, too */
3765 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3766 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3767 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3768 return;
3769 if (!spec->automute_lo)
3770 on = 0;
3771 else
3772 on = spec->hp_jack_present;
3773 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003774 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003775 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003776 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003777}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003778EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003779
3780static void call_update_outputs(struct hda_codec *codec)
3781{
3782 struct hda_gen_spec *spec = codec->spec;
3783 if (spec->automute_hook)
3784 spec->automute_hook(codec);
3785 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003786 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003787}
3788
3789/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003790void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003791{
3792 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003793 hda_nid_t *pins = spec->autocfg.hp_pins;
3794 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003795
Takashi Iwai92603c52013-01-22 07:46:31 +01003796 /* No detection for the first HP jack during indep-HP mode */
3797 if (spec->indep_hp_enabled) {
3798 pins++;
3799 num_pins--;
3800 }
3801
3802 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003803 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3804 return;
3805 call_update_outputs(codec);
3806}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003807EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003808
3809/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003810void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003811{
3812 struct hda_gen_spec *spec = codec->spec;
3813
3814 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3815 return;
3816 /* check LO jack only when it's different from HP */
3817 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3818 return;
3819
3820 spec->line_jack_present =
3821 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3822 spec->autocfg.line_out_pins);
3823 if (!spec->automute_speaker || !spec->detect_lo)
3824 return;
3825 call_update_outputs(codec);
3826}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003827EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003828
3829/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003830void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003831{
3832 struct hda_gen_spec *spec = codec->spec;
3833 int i;
3834
3835 if (!spec->auto_mic)
3836 return;
3837
3838 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003839 hda_nid_t pin = spec->am_entry[i].pin;
3840 /* don't detect pins retasked as outputs */
3841 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3842 continue;
3843 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003844 mux_select(codec, 0, spec->am_entry[i].idx);
3845 return;
3846 }
3847 }
3848 mux_select(codec, 0, spec->am_entry[0].idx);
3849}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003850EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003851
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003852/* update jack retasking */
3853static void update_automute_all(struct hda_codec *codec)
3854{
3855 struct hda_gen_spec *spec = codec->spec;
3856
Takashi Iwai8ba955c2013-03-07 18:40:58 +01003857 update_hp_automute_hook(codec);
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003858 if (spec->line_automute_hook)
3859 spec->line_automute_hook(codec, NULL);
3860 else
3861 snd_hda_gen_line_automute(codec, NULL);
3862 if (spec->mic_autoswitch_hook)
3863 spec->mic_autoswitch_hook(codec, NULL);
3864 else
3865 snd_hda_gen_mic_autoswitch(codec, NULL);
3866}
3867
Takashi Iwai352f7f92012-12-19 12:52:06 +01003868/*
3869 * Auto-Mute mode mixer enum support
3870 */
3871static int automute_mode_info(struct snd_kcontrol *kcontrol,
3872 struct snd_ctl_elem_info *uinfo)
3873{
3874 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3875 struct hda_gen_spec *spec = codec->spec;
3876 static const char * const texts3[] = {
3877 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003878 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Takashi Iwai352f7f92012-12-19 12:52:06 +01003880 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3881 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3882 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3883}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Takashi Iwai352f7f92012-12-19 12:52:06 +01003885static int automute_mode_get(struct snd_kcontrol *kcontrol,
3886 struct snd_ctl_elem_value *ucontrol)
3887{
3888 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3889 struct hda_gen_spec *spec = codec->spec;
3890 unsigned int val = 0;
3891 if (spec->automute_speaker)
3892 val++;
3893 if (spec->automute_lo)
3894 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003895
Takashi Iwai352f7f92012-12-19 12:52:06 +01003896 ucontrol->value.enumerated.item[0] = val;
3897 return 0;
3898}
3899
3900static int automute_mode_put(struct snd_kcontrol *kcontrol,
3901 struct snd_ctl_elem_value *ucontrol)
3902{
3903 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3904 struct hda_gen_spec *spec = codec->spec;
3905
3906 switch (ucontrol->value.enumerated.item[0]) {
3907 case 0:
3908 if (!spec->automute_speaker && !spec->automute_lo)
3909 return 0;
3910 spec->automute_speaker = 0;
3911 spec->automute_lo = 0;
3912 break;
3913 case 1:
3914 if (spec->automute_speaker_possible) {
3915 if (!spec->automute_lo && spec->automute_speaker)
3916 return 0;
3917 spec->automute_speaker = 1;
3918 spec->automute_lo = 0;
3919 } else if (spec->automute_lo_possible) {
3920 if (spec->automute_lo)
3921 return 0;
3922 spec->automute_lo = 1;
3923 } else
3924 return -EINVAL;
3925 break;
3926 case 2:
3927 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3928 return -EINVAL;
3929 if (spec->automute_speaker && spec->automute_lo)
3930 return 0;
3931 spec->automute_speaker = 1;
3932 spec->automute_lo = 1;
3933 break;
3934 default:
3935 return -EINVAL;
3936 }
3937 call_update_outputs(codec);
3938 return 1;
3939}
3940
3941static const struct snd_kcontrol_new automute_mode_enum = {
3942 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3943 .name = "Auto-Mute Mode",
3944 .info = automute_mode_info,
3945 .get = automute_mode_get,
3946 .put = automute_mode_put,
3947};
3948
3949static int add_automute_mode_enum(struct hda_codec *codec)
3950{
3951 struct hda_gen_spec *spec = codec->spec;
3952
Takashi Iwai12c93df2012-12-19 14:38:33 +01003953 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003954 return -ENOMEM;
3955 return 0;
3956}
3957
3958/*
3959 * Check the availability of HP/line-out auto-mute;
3960 * Set up appropriately if really supported
3961 */
3962static int check_auto_mute_availability(struct hda_codec *codec)
3963{
3964 struct hda_gen_spec *spec = codec->spec;
3965 struct auto_pin_cfg *cfg = &spec->autocfg;
3966 int present = 0;
3967 int i, err;
3968
Takashi Iwaif72706b2013-01-16 18:20:07 +01003969 if (spec->suppress_auto_mute)
3970 return 0;
3971
Takashi Iwai352f7f92012-12-19 12:52:06 +01003972 if (cfg->hp_pins[0])
3973 present++;
3974 if (cfg->line_out_pins[0])
3975 present++;
3976 if (cfg->speaker_pins[0])
3977 present++;
3978 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003979 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003980
3981 if (!cfg->speaker_pins[0] &&
3982 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3983 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3984 sizeof(cfg->speaker_pins));
3985 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Takashi Iwai352f7f92012-12-19 12:52:06 +01003988 if (!cfg->hp_pins[0] &&
3989 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3990 memcpy(cfg->hp_pins, cfg->line_out_pins,
3991 sizeof(cfg->hp_pins));
3992 cfg->hp_outs = cfg->line_outs;
3993 }
3994
3995 for (i = 0; i < cfg->hp_outs; i++) {
3996 hda_nid_t nid = cfg->hp_pins[i];
3997 if (!is_jack_detectable(codec, nid))
3998 continue;
3999 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4000 nid);
4001 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004002 spec->hp_automute_hook ?
4003 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004004 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004005 spec->detect_hp = 1;
4006 }
4007
4008 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4009 if (cfg->speaker_outs)
4010 for (i = 0; i < cfg->line_outs; i++) {
4011 hda_nid_t nid = cfg->line_out_pins[i];
4012 if (!is_jack_detectable(codec, nid))
4013 continue;
4014 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4015 snd_hda_jack_detect_enable_callback(codec, nid,
4016 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004017 spec->line_automute_hook ?
4018 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004019 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004020 spec->detect_lo = 1;
4021 }
4022 spec->automute_lo_possible = spec->detect_hp;
4023 }
4024
4025 spec->automute_speaker_possible = cfg->speaker_outs &&
4026 (spec->detect_hp || spec->detect_lo);
4027
4028 spec->automute_lo = spec->automute_lo_possible;
4029 spec->automute_speaker = spec->automute_speaker_possible;
4030
4031 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4032 /* create a control for automute mode */
4033 err = add_automute_mode_enum(codec);
4034 if (err < 0)
4035 return err;
4036 }
4037 return 0;
4038}
4039
Takashi Iwai352f7f92012-12-19 12:52:06 +01004040/* check whether all auto-mic pins are valid; setup indices if OK */
4041static bool auto_mic_check_imux(struct hda_codec *codec)
4042{
4043 struct hda_gen_spec *spec = codec->spec;
4044 const struct hda_input_mux *imux;
4045 int i;
4046
4047 imux = &spec->input_mux;
4048 for (i = 0; i < spec->am_num_entries; i++) {
4049 spec->am_entry[i].idx =
4050 find_idx_in_nid_list(spec->am_entry[i].pin,
4051 spec->imux_pins, imux->num_items);
4052 if (spec->am_entry[i].idx < 0)
4053 return false; /* no corresponding imux */
4054 }
4055
4056 /* we don't need the jack detection for the first pin */
4057 for (i = 1; i < spec->am_num_entries; i++)
4058 snd_hda_jack_detect_enable_callback(codec,
4059 spec->am_entry[i].pin,
4060 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004061 spec->mic_autoswitch_hook ?
4062 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004063 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004064 return true;
4065}
4066
4067static int compare_attr(const void *ap, const void *bp)
4068{
4069 const struct automic_entry *a = ap;
4070 const struct automic_entry *b = bp;
4071 return (int)(a->attr - b->attr);
4072}
4073
4074/*
4075 * Check the availability of auto-mic switch;
4076 * Set up if really supported
4077 */
4078static int check_auto_mic_availability(struct hda_codec *codec)
4079{
4080 struct hda_gen_spec *spec = codec->spec;
4081 struct auto_pin_cfg *cfg = &spec->autocfg;
4082 unsigned int types;
4083 int i, num_pins;
4084
Takashi Iwaid12daf62013-01-07 16:32:11 +01004085 if (spec->suppress_auto_mic)
4086 return 0;
4087
Takashi Iwai352f7f92012-12-19 12:52:06 +01004088 types = 0;
4089 num_pins = 0;
4090 for (i = 0; i < cfg->num_inputs; i++) {
4091 hda_nid_t nid = cfg->inputs[i].pin;
4092 unsigned int attr;
4093 attr = snd_hda_codec_get_pincfg(codec, nid);
4094 attr = snd_hda_get_input_pin_attr(attr);
4095 if (types & (1 << attr))
4096 return 0; /* already occupied */
4097 switch (attr) {
4098 case INPUT_PIN_ATTR_INT:
4099 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4100 return 0; /* invalid type */
4101 break;
4102 case INPUT_PIN_ATTR_UNUSED:
4103 return 0; /* invalid entry */
4104 default:
4105 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4106 return 0; /* invalid type */
4107 if (!spec->line_in_auto_switch &&
4108 cfg->inputs[i].type != AUTO_PIN_MIC)
4109 return 0; /* only mic is allowed */
4110 if (!is_jack_detectable(codec, nid))
4111 return 0; /* no unsol support */
4112 break;
4113 }
4114 if (num_pins >= MAX_AUTO_MIC_PINS)
4115 return 0;
4116 types |= (1 << attr);
4117 spec->am_entry[num_pins].pin = nid;
4118 spec->am_entry[num_pins].attr = attr;
4119 num_pins++;
4120 }
4121
4122 if (num_pins < 2)
4123 return 0;
4124
4125 spec->am_num_entries = num_pins;
4126 /* sort the am_entry in the order of attr so that the pin with a
4127 * higher attr will be selected when the jack is plugged.
4128 */
4129 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4130 compare_attr, NULL);
4131
4132 if (!auto_mic_check_imux(codec))
4133 return 0;
4134
4135 spec->auto_mic = 1;
4136 spec->num_adc_nids = 1;
4137 spec->cur_mux[0] = spec->am_entry[0].idx;
4138 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4139 spec->am_entry[0].pin,
4140 spec->am_entry[1].pin,
4141 spec->am_entry[2].pin);
4142
4143 return 0;
4144}
4145
Takashi Iwai55196ff2013-01-24 17:32:56 +01004146/* power_filter hook; make inactive widgets into power down */
4147static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4148 hda_nid_t nid,
4149 unsigned int power_state)
4150{
4151 if (power_state != AC_PWRST_D0)
4152 return power_state;
4153 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4154 return power_state;
4155 if (is_active_nid(codec, nid, HDA_OUTPUT, 0))
4156 return power_state;
4157 return AC_PWRST_D3;
4158}
4159
Takashi Iwai352f7f92012-12-19 12:52:06 +01004160
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004161/*
4162 * Parse the given BIOS configuration and set up the hda_gen_spec
4163 *
4164 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004165 * or a negative error code
4166 */
4167int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004168 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004169{
4170 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004171 int err;
4172
Takashi Iwai1c70a582013-01-11 17:48:22 +01004173 parse_user_hints(codec);
4174
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004175 if (spec->mixer_nid && !spec->mixer_merge_nid)
4176 spec->mixer_merge_nid = spec->mixer_nid;
4177
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004178 if (cfg != &spec->autocfg) {
4179 spec->autocfg = *cfg;
4180 cfg = &spec->autocfg;
4181 }
4182
David Henningsson6fc4cb92013-01-16 15:58:45 +01004183 fill_all_dac_nids(codec);
4184
Takashi Iwai352f7f92012-12-19 12:52:06 +01004185 if (!cfg->line_outs) {
4186 if (cfg->dig_outs || cfg->dig_in_pin) {
4187 spec->multiout.max_channels = 2;
4188 spec->no_analog = 1;
4189 goto dig_only;
4190 }
4191 return 0; /* can't find valid BIOS pin config */
4192 }
4193
4194 if (!spec->no_primary_hp &&
4195 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4196 cfg->line_outs <= cfg->hp_outs) {
4197 /* use HP as primary out */
4198 cfg->speaker_outs = cfg->line_outs;
4199 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4200 sizeof(cfg->speaker_pins));
4201 cfg->line_outs = cfg->hp_outs;
4202 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4203 cfg->hp_outs = 0;
4204 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4205 cfg->line_out_type = AUTO_PIN_HP_OUT;
4206 }
4207
4208 err = parse_output_paths(codec);
4209 if (err < 0)
4210 return err;
4211 err = create_multi_channel_mode(codec);
4212 if (err < 0)
4213 return err;
4214 err = create_multi_out_ctls(codec, cfg);
4215 if (err < 0)
4216 return err;
4217 err = create_hp_out_ctls(codec);
4218 if (err < 0)
4219 return err;
4220 err = create_speaker_out_ctls(codec);
4221 if (err < 0)
4222 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004223 err = create_indep_hp_ctls(codec);
4224 if (err < 0)
4225 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004226 err = create_loopback_mixing_ctl(codec);
4227 if (err < 0)
4228 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004229 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004230 if (err < 0)
4231 return err;
4232 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004233 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004234 return err;
4235
Takashi Iwaia07a9492013-01-07 16:44:06 +01004236 spec->const_channel_count = spec->ext_channel_count;
4237 /* check the multiple speaker and headphone pins */
4238 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4239 spec->const_channel_count = max(spec->const_channel_count,
4240 cfg->speaker_outs * 2);
4241 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4242 spec->const_channel_count = max(spec->const_channel_count,
4243 cfg->hp_outs * 2);
4244 spec->multiout.max_channels = max(spec->ext_channel_count,
4245 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004246
4247 err = check_auto_mute_availability(codec);
4248 if (err < 0)
4249 return err;
4250
4251 err = check_dyn_adc_switch(codec);
4252 if (err < 0)
4253 return err;
4254
Takashi Iwai967303d2013-02-19 17:12:42 +01004255 err = check_auto_mic_availability(codec);
4256 if (err < 0)
4257 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004258
Takashi Iwai352f7f92012-12-19 12:52:06 +01004259 err = create_capture_mixers(codec);
4260 if (err < 0)
4261 return err;
4262
4263 err = parse_mic_boost(codec);
4264 if (err < 0)
4265 return err;
4266
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004267 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004268 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4269 err = create_out_jack_modes(codec, cfg->line_outs,
4270 cfg->line_out_pins);
4271 if (err < 0)
4272 return err;
4273 }
4274 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4275 err = create_out_jack_modes(codec, cfg->hp_outs,
4276 cfg->hp_pins);
4277 if (err < 0)
4278 return err;
4279 }
4280 }
4281
Takashi Iwai352f7f92012-12-19 12:52:06 +01004282 dig_only:
4283 parse_digital(codec);
4284
Takashi Iwai55196ff2013-01-24 17:32:56 +01004285 if (spec->power_down_unused)
4286 codec->power_filter = snd_hda_gen_path_power_filter;
4287
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004288 if (!spec->no_analog && spec->beep_nid) {
4289 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4290 if (err < 0)
4291 return err;
4292 }
4293
Takashi Iwai352f7f92012-12-19 12:52:06 +01004294 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004296EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
4298
4299/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004300 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004302
4303/* slave controls for virtual master */
4304static const char * const slave_pfxs[] = {
4305 "Front", "Surround", "Center", "LFE", "Side",
4306 "Headphone", "Speaker", "Mono", "Line Out",
4307 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004308 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4309 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4310 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004311 NULL,
4312};
4313
4314int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004316 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Takashi Iwai36502d02012-12-19 15:15:10 +01004319 if (spec->kctls.used) {
4320 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4321 if (err < 0)
4322 return err;
4323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Takashi Iwai352f7f92012-12-19 12:52:06 +01004325 if (spec->multiout.dig_out_nid) {
4326 err = snd_hda_create_dig_out_ctls(codec,
4327 spec->multiout.dig_out_nid,
4328 spec->multiout.dig_out_nid,
4329 spec->pcm_rec[1].pcm_type);
4330 if (err < 0)
4331 return err;
4332 if (!spec->no_analog) {
4333 err = snd_hda_create_spdif_share_sw(codec,
4334 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 if (err < 0)
4336 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004337 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 }
4339 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004340 if (spec->dig_in_nid) {
4341 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4342 if (err < 0)
4343 return err;
4344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
Takashi Iwai352f7f92012-12-19 12:52:06 +01004346 /* if we have no master control, let's create it */
4347 if (!spec->no_analog &&
4348 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004349 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004350 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004351 "Playback Volume");
4352 if (err < 0)
4353 return err;
4354 }
4355 if (!spec->no_analog &&
4356 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4357 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4358 NULL, slave_pfxs,
4359 "Playback Switch",
4360 true, &spec->vmaster_mute.sw_kctl);
4361 if (err < 0)
4362 return err;
4363 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004364 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4365 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
Takashi Iwai352f7f92012-12-19 12:52:06 +01004368 free_kctls(spec); /* no longer needed */
4369
Takashi Iwai967303d2013-02-19 17:12:42 +01004370 if (spec->hp_mic_pin) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004371 int err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004372 int nid = spec->hp_mic_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004373 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4374 if (err < 0)
4375 return err;
4376 err = snd_hda_jack_detect_enable(codec, nid, 0);
4377 if (err < 0)
4378 return err;
4379 }
4380
4381 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4382 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 return err;
4384
4385 return 0;
4386}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004387EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4388
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
4390/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004391 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004394static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4395 struct hda_codec *codec,
4396 struct snd_pcm_substream *substream,
4397 int action)
4398{
4399 struct hda_gen_spec *spec = codec->spec;
4400 if (spec->pcm_playback_hook)
4401 spec->pcm_playback_hook(hinfo, codec, substream, action);
4402}
4403
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004404static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4405 struct hda_codec *codec,
4406 struct snd_pcm_substream *substream,
4407 int action)
4408{
4409 struct hda_gen_spec *spec = codec->spec;
4410 if (spec->pcm_capture_hook)
4411 spec->pcm_capture_hook(hinfo, codec, substream, action);
4412}
4413
Takashi Iwai352f7f92012-12-19 12:52:06 +01004414/*
4415 * Analog playback callbacks
4416 */
4417static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4418 struct hda_codec *codec,
4419 struct snd_pcm_substream *substream)
4420{
4421 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004422 int err;
4423
4424 mutex_lock(&spec->pcm_mutex);
4425 err = snd_hda_multi_out_analog_open(codec,
4426 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004427 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004428 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004429 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004430 call_pcm_playback_hook(hinfo, codec, substream,
4431 HDA_GEN_PCM_ACT_OPEN);
4432 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004433 mutex_unlock(&spec->pcm_mutex);
4434 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004435}
4436
4437static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004438 struct hda_codec *codec,
4439 unsigned int stream_tag,
4440 unsigned int format,
4441 struct snd_pcm_substream *substream)
4442{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004443 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004444 int err;
4445
4446 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4447 stream_tag, format, substream);
4448 if (!err)
4449 call_pcm_playback_hook(hinfo, codec, substream,
4450 HDA_GEN_PCM_ACT_PREPARE);
4451 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004452}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004453
Takashi Iwai352f7f92012-12-19 12:52:06 +01004454static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4455 struct hda_codec *codec,
4456 struct snd_pcm_substream *substream)
4457{
4458 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004459 int err;
4460
4461 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4462 if (!err)
4463 call_pcm_playback_hook(hinfo, codec, substream,
4464 HDA_GEN_PCM_ACT_CLEANUP);
4465 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004466}
4467
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004468static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4469 struct hda_codec *codec,
4470 struct snd_pcm_substream *substream)
4471{
4472 struct hda_gen_spec *spec = codec->spec;
4473 mutex_lock(&spec->pcm_mutex);
4474 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004475 call_pcm_playback_hook(hinfo, codec, substream,
4476 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004477 mutex_unlock(&spec->pcm_mutex);
4478 return 0;
4479}
4480
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004481static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4482 struct hda_codec *codec,
4483 struct snd_pcm_substream *substream)
4484{
4485 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4486 return 0;
4487}
4488
4489static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4490 struct hda_codec *codec,
4491 unsigned int stream_tag,
4492 unsigned int format,
4493 struct snd_pcm_substream *substream)
4494{
4495 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4496 call_pcm_capture_hook(hinfo, codec, substream,
4497 HDA_GEN_PCM_ACT_PREPARE);
4498 return 0;
4499}
4500
4501static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4502 struct hda_codec *codec,
4503 struct snd_pcm_substream *substream)
4504{
4505 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4506 call_pcm_capture_hook(hinfo, codec, substream,
4507 HDA_GEN_PCM_ACT_CLEANUP);
4508 return 0;
4509}
4510
4511static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4512 struct hda_codec *codec,
4513 struct snd_pcm_substream *substream)
4514{
4515 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4516 return 0;
4517}
4518
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004519static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4520 struct hda_codec *codec,
4521 struct snd_pcm_substream *substream)
4522{
4523 struct hda_gen_spec *spec = codec->spec;
4524 int err = 0;
4525
4526 mutex_lock(&spec->pcm_mutex);
4527 if (!spec->indep_hp_enabled)
4528 err = -EBUSY;
4529 else
4530 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004531 call_pcm_playback_hook(hinfo, codec, substream,
4532 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004533 mutex_unlock(&spec->pcm_mutex);
4534 return err;
4535}
4536
4537static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4538 struct hda_codec *codec,
4539 struct snd_pcm_substream *substream)
4540{
4541 struct hda_gen_spec *spec = codec->spec;
4542 mutex_lock(&spec->pcm_mutex);
4543 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004544 call_pcm_playback_hook(hinfo, codec, substream,
4545 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004546 mutex_unlock(&spec->pcm_mutex);
4547 return 0;
4548}
4549
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004550static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4551 struct hda_codec *codec,
4552 unsigned int stream_tag,
4553 unsigned int format,
4554 struct snd_pcm_substream *substream)
4555{
4556 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4557 call_pcm_playback_hook(hinfo, codec, substream,
4558 HDA_GEN_PCM_ACT_PREPARE);
4559 return 0;
4560}
4561
4562static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4563 struct hda_codec *codec,
4564 struct snd_pcm_substream *substream)
4565{
4566 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4567 call_pcm_playback_hook(hinfo, codec, substream,
4568 HDA_GEN_PCM_ACT_CLEANUP);
4569 return 0;
4570}
4571
Takashi Iwai352f7f92012-12-19 12:52:06 +01004572/*
4573 * Digital out
4574 */
4575static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4576 struct hda_codec *codec,
4577 struct snd_pcm_substream *substream)
4578{
4579 struct hda_gen_spec *spec = codec->spec;
4580 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4581}
4582
4583static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4584 struct hda_codec *codec,
4585 unsigned int stream_tag,
4586 unsigned int format,
4587 struct snd_pcm_substream *substream)
4588{
4589 struct hda_gen_spec *spec = codec->spec;
4590 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4591 stream_tag, format, substream);
4592}
4593
4594static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4595 struct hda_codec *codec,
4596 struct snd_pcm_substream *substream)
4597{
4598 struct hda_gen_spec *spec = codec->spec;
4599 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4600}
4601
4602static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4603 struct hda_codec *codec,
4604 struct snd_pcm_substream *substream)
4605{
4606 struct hda_gen_spec *spec = codec->spec;
4607 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4608}
4609
4610/*
4611 * Analog capture
4612 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004613#define alt_capture_pcm_open capture_pcm_open
4614#define alt_capture_pcm_close capture_pcm_close
4615
Takashi Iwai352f7f92012-12-19 12:52:06 +01004616static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4617 struct hda_codec *codec,
4618 unsigned int stream_tag,
4619 unsigned int format,
4620 struct snd_pcm_substream *substream)
4621{
4622 struct hda_gen_spec *spec = codec->spec;
4623
4624 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004625 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004626 call_pcm_capture_hook(hinfo, codec, substream,
4627 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004628 return 0;
4629}
4630
Takashi Iwai352f7f92012-12-19 12:52:06 +01004631static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4632 struct hda_codec *codec,
4633 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004634{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004635 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004636
Takashi Iwai352f7f92012-12-19 12:52:06 +01004637 snd_hda_codec_cleanup_stream(codec,
4638 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004639 call_pcm_capture_hook(hinfo, codec, substream,
4640 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004641 return 0;
4642}
4643
Takashi Iwai352f7f92012-12-19 12:52:06 +01004644/*
4645 */
4646static const struct hda_pcm_stream pcm_analog_playback = {
4647 .substreams = 1,
4648 .channels_min = 2,
4649 .channels_max = 8,
4650 /* NID is set in build_pcms */
4651 .ops = {
4652 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004653 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004654 .prepare = playback_pcm_prepare,
4655 .cleanup = playback_pcm_cleanup
4656 },
4657};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
Takashi Iwai352f7f92012-12-19 12:52:06 +01004659static const struct hda_pcm_stream pcm_analog_capture = {
4660 .substreams = 1,
4661 .channels_min = 2,
4662 .channels_max = 2,
4663 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004664 .ops = {
4665 .open = capture_pcm_open,
4666 .close = capture_pcm_close,
4667 .prepare = capture_pcm_prepare,
4668 .cleanup = capture_pcm_cleanup
4669 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004670};
4671
4672static const struct hda_pcm_stream pcm_analog_alt_playback = {
4673 .substreams = 1,
4674 .channels_min = 2,
4675 .channels_max = 2,
4676 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004677 .ops = {
4678 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004679 .close = alt_playback_pcm_close,
4680 .prepare = alt_playback_pcm_prepare,
4681 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004682 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004683};
4684
4685static const struct hda_pcm_stream pcm_analog_alt_capture = {
4686 .substreams = 2, /* can be overridden */
4687 .channels_min = 2,
4688 .channels_max = 2,
4689 /* NID is set in build_pcms */
4690 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004691 .open = alt_capture_pcm_open,
4692 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004693 .prepare = alt_capture_pcm_prepare,
4694 .cleanup = alt_capture_pcm_cleanup
4695 },
4696};
4697
4698static const struct hda_pcm_stream pcm_digital_playback = {
4699 .substreams = 1,
4700 .channels_min = 2,
4701 .channels_max = 2,
4702 /* NID is set in build_pcms */
4703 .ops = {
4704 .open = dig_playback_pcm_open,
4705 .close = dig_playback_pcm_close,
4706 .prepare = dig_playback_pcm_prepare,
4707 .cleanup = dig_playback_pcm_cleanup
4708 },
4709};
4710
4711static const struct hda_pcm_stream pcm_digital_capture = {
4712 .substreams = 1,
4713 .channels_min = 2,
4714 .channels_max = 2,
4715 /* NID is set in build_pcms */
4716};
4717
4718/* Used by build_pcms to flag that a PCM has no playback stream */
4719static const struct hda_pcm_stream pcm_null_stream = {
4720 .substreams = 0,
4721 .channels_min = 0,
4722 .channels_max = 0,
4723};
4724
4725/*
4726 * dynamic changing ADC PCM streams
4727 */
4728static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4729{
4730 struct hda_gen_spec *spec = codec->spec;
4731 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4732
4733 if (spec->cur_adc && spec->cur_adc != new_adc) {
4734 /* stream is running, let's swap the current ADC */
4735 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4736 spec->cur_adc = new_adc;
4737 snd_hda_codec_setup_stream(codec, new_adc,
4738 spec->cur_adc_stream_tag, 0,
4739 spec->cur_adc_format);
4740 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004742 return false;
4743}
4744
4745/* analog capture with dynamic dual-adc changes */
4746static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4747 struct hda_codec *codec,
4748 unsigned int stream_tag,
4749 unsigned int format,
4750 struct snd_pcm_substream *substream)
4751{
4752 struct hda_gen_spec *spec = codec->spec;
4753 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4754 spec->cur_adc_stream_tag = stream_tag;
4755 spec->cur_adc_format = format;
4756 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4757 return 0;
4758}
4759
4760static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4761 struct hda_codec *codec,
4762 struct snd_pcm_substream *substream)
4763{
4764 struct hda_gen_spec *spec = codec->spec;
4765 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4766 spec->cur_adc = 0;
4767 return 0;
4768}
4769
4770static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4771 .substreams = 1,
4772 .channels_min = 2,
4773 .channels_max = 2,
4774 .nid = 0, /* fill later */
4775 .ops = {
4776 .prepare = dyn_adc_capture_pcm_prepare,
4777 .cleanup = dyn_adc_capture_pcm_cleanup
4778 },
4779};
4780
Takashi Iwaif873e532012-12-20 16:58:39 +01004781static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4782 const char *chip_name)
4783{
4784 char *p;
4785
4786 if (*str)
4787 return;
4788 strlcpy(str, chip_name, len);
4789
4790 /* drop non-alnum chars after a space */
4791 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4792 if (!isalnum(p[1])) {
4793 *p = 0;
4794 break;
4795 }
4796 }
4797 strlcat(str, sfx, len);
4798}
4799
Takashi Iwai352f7f92012-12-19 12:52:06 +01004800/* build PCM streams based on the parsed results */
4801int snd_hda_gen_build_pcms(struct hda_codec *codec)
4802{
4803 struct hda_gen_spec *spec = codec->spec;
4804 struct hda_pcm *info = spec->pcm_rec;
4805 const struct hda_pcm_stream *p;
4806 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
4808 codec->num_pcms = 1;
4809 codec->pcm_info = info;
4810
Takashi Iwai352f7f92012-12-19 12:52:06 +01004811 if (spec->no_analog)
4812 goto skip_analog;
4813
Takashi Iwaif873e532012-12-20 16:58:39 +01004814 fill_pcm_stream_name(spec->stream_name_analog,
4815 sizeof(spec->stream_name_analog),
4816 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004817 info->name = spec->stream_name_analog;
4818
4819 if (spec->multiout.num_dacs > 0) {
4820 p = spec->stream_analog_playback;
4821 if (!p)
4822 p = &pcm_analog_playback;
4823 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4824 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4825 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4826 spec->multiout.max_channels;
4827 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4828 spec->autocfg.line_outs == 2)
4829 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4830 snd_pcm_2_1_chmaps;
4831 }
4832 if (spec->num_adc_nids) {
4833 p = spec->stream_analog_capture;
4834 if (!p) {
4835 if (spec->dyn_adc_switch)
4836 p = &dyn_adc_pcm_analog_capture;
4837 else
4838 p = &pcm_analog_capture;
4839 }
4840 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4841 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4842 }
4843
Takashi Iwai352f7f92012-12-19 12:52:06 +01004844 skip_analog:
4845 /* SPDIF for stream index #1 */
4846 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004847 fill_pcm_stream_name(spec->stream_name_digital,
4848 sizeof(spec->stream_name_digital),
4849 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004850 codec->num_pcms = 2;
4851 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4852 info = spec->pcm_rec + 1;
4853 info->name = spec->stream_name_digital;
4854 if (spec->dig_out_type)
4855 info->pcm_type = spec->dig_out_type;
4856 else
4857 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4858 if (spec->multiout.dig_out_nid) {
4859 p = spec->stream_digital_playback;
4860 if (!p)
4861 p = &pcm_digital_playback;
4862 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4863 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4864 }
4865 if (spec->dig_in_nid) {
4866 p = spec->stream_digital_capture;
4867 if (!p)
4868 p = &pcm_digital_capture;
4869 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4870 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4871 }
4872 }
4873
4874 if (spec->no_analog)
4875 return 0;
4876
4877 /* If the use of more than one ADC is requested for the current
4878 * model, configure a second analog capture-only PCM.
4879 */
4880 have_multi_adcs = (spec->num_adc_nids > 1) &&
4881 !spec->dyn_adc_switch && !spec->auto_mic;
4882 /* Additional Analaog capture for index #2 */
4883 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004884 fill_pcm_stream_name(spec->stream_name_alt_analog,
4885 sizeof(spec->stream_name_alt_analog),
4886 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004887 codec->num_pcms = 3;
4888 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004889 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004890 if (spec->alt_dac_nid) {
4891 p = spec->stream_analog_alt_playback;
4892 if (!p)
4893 p = &pcm_analog_alt_playback;
4894 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4895 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4896 spec->alt_dac_nid;
4897 } else {
4898 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4899 pcm_null_stream;
4900 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4901 }
4902 if (have_multi_adcs) {
4903 p = spec->stream_analog_alt_capture;
4904 if (!p)
4905 p = &pcm_analog_alt_capture;
4906 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4907 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4908 spec->adc_nids[1];
4909 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4910 spec->num_adc_nids - 1;
4911 } else {
4912 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4913 pcm_null_stream;
4914 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 }
4917
4918 return 0;
4919}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004920EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4921
4922
4923/*
4924 * Standard auto-parser initializations
4925 */
4926
Takashi Iwaid4156932013-01-07 10:08:02 +01004927/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004928static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004929{
4930 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004931 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004932
Takashi Iwai196c17662013-01-04 15:01:40 +01004933 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004934 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004935 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004936 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004937 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004938 snd_hda_activate_path(codec, path, path->active, true);
4939 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004940}
4941
4942/* initialize primary output paths */
4943static void init_multi_out(struct hda_codec *codec)
4944{
4945 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004946 int i;
4947
Takashi Iwaid4156932013-01-07 10:08:02 +01004948 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004949 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004950}
4951
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004952
Takashi Iwai2c12c302013-01-10 09:33:29 +01004953static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004954{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004955 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004956
Takashi Iwaid4156932013-01-07 10:08:02 +01004957 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004958 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004959}
4960
4961/* initialize hp and speaker paths */
4962static void init_extra_out(struct hda_codec *codec)
4963{
4964 struct hda_gen_spec *spec = codec->spec;
4965
4966 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004967 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004968 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4969 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004970 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004971}
4972
4973/* initialize multi-io paths */
4974static void init_multi_io(struct hda_codec *codec)
4975{
4976 struct hda_gen_spec *spec = codec->spec;
4977 int i;
4978
4979 for (i = 0; i < spec->multi_ios; i++) {
4980 hda_nid_t pin = spec->multi_io[i].pin;
4981 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004982 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004983 if (!path)
4984 continue;
4985 if (!spec->multi_io[i].ctl_in)
4986 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004987 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004988 snd_hda_activate_path(codec, path, path->active, true);
4989 }
4990}
4991
Takashi Iwai352f7f92012-12-19 12:52:06 +01004992/* set up input pins and loopback paths */
4993static void init_analog_input(struct hda_codec *codec)
4994{
4995 struct hda_gen_spec *spec = codec->spec;
4996 struct auto_pin_cfg *cfg = &spec->autocfg;
4997 int i;
4998
4999 for (i = 0; i < cfg->num_inputs; i++) {
5000 hda_nid_t nid = cfg->inputs[i].pin;
5001 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005002 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005003
5004 /* init loopback inputs */
5005 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005006 resume_path_from_idx(codec, spec->loopback_paths[i]);
5007 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005008 }
5009 }
5010}
5011
5012/* initialize ADC paths */
5013static void init_input_src(struct hda_codec *codec)
5014{
5015 struct hda_gen_spec *spec = codec->spec;
5016 struct hda_input_mux *imux = &spec->input_mux;
5017 struct nid_path *path;
5018 int i, c, nums;
5019
5020 if (spec->dyn_adc_switch)
5021 nums = 1;
5022 else
5023 nums = spec->num_adc_nids;
5024
5025 for (c = 0; c < nums; c++) {
5026 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005027 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005028 if (path) {
5029 bool active = path->active;
5030 if (i == spec->cur_mux[c])
5031 active = true;
5032 snd_hda_activate_path(codec, path, active, false);
5033 }
5034 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005035 if (spec->hp_mic)
5036 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005037 }
5038
Takashi Iwai352f7f92012-12-19 12:52:06 +01005039 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01005040 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005041}
5042
5043/* set right pin controls for digital I/O */
5044static void init_digital(struct hda_codec *codec)
5045{
5046 struct hda_gen_spec *spec = codec->spec;
5047 int i;
5048 hda_nid_t pin;
5049
Takashi Iwaid4156932013-01-07 10:08:02 +01005050 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005051 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005052 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005053 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005054 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005055 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005056 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005057}
5058
Takashi Iwai973e4972012-12-20 15:16:09 +01005059/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5060 * invalid unsol tags by some reason
5061 */
5062static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5063{
5064 int i;
5065
5066 for (i = 0; i < codec->init_pins.used; i++) {
5067 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5068 hda_nid_t nid = pin->nid;
5069 if (is_jack_detectable(codec, nid) &&
5070 !snd_hda_jack_tbl_get(codec, nid))
5071 snd_hda_codec_update_cache(codec, nid, 0,
5072 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5073 }
5074}
5075
Takashi Iwai5187ac12013-01-07 12:52:16 +01005076/*
5077 * initialize the generic spec;
5078 * this can be put as patch_ops.init function
5079 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005080int snd_hda_gen_init(struct hda_codec *codec)
5081{
5082 struct hda_gen_spec *spec = codec->spec;
5083
5084 if (spec->init_hook)
5085 spec->init_hook(codec);
5086
5087 snd_hda_apply_verbs(codec);
5088
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005089 codec->cached_write = 1;
5090
Takashi Iwai352f7f92012-12-19 12:52:06 +01005091 init_multi_out(codec);
5092 init_extra_out(codec);
5093 init_multi_io(codec);
5094 init_analog_input(codec);
5095 init_input_src(codec);
5096 init_digital(codec);
5097
Takashi Iwai973e4972012-12-20 15:16:09 +01005098 clear_unsol_on_unused_pins(codec);
5099
Takashi Iwai352f7f92012-12-19 12:52:06 +01005100 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005101 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005102
Takashi Iwaidc870f32013-01-22 15:24:30 +01005103 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005104
Takashi Iwai352f7f92012-12-19 12:52:06 +01005105 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5106 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5107
5108 hda_call_check_power_status(codec, 0x01);
5109 return 0;
5110}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005111EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5112
Takashi Iwai5187ac12013-01-07 12:52:16 +01005113/*
5114 * free the generic spec;
5115 * this can be put as patch_ops.free function
5116 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005117void snd_hda_gen_free(struct hda_codec *codec)
5118{
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005119 snd_hda_detach_beep_device(codec);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005120 snd_hda_gen_spec_free(codec->spec);
5121 kfree(codec->spec);
5122 codec->spec = NULL;
5123}
5124EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5125
5126#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005127/*
5128 * check the loopback power save state;
5129 * this can be put as patch_ops.check_power_status function
5130 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005131int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5132{
5133 struct hda_gen_spec *spec = codec->spec;
5134 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5135}
5136EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5137#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005138
5139
5140/*
5141 * the generic codec support
5142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
Takashi Iwai352f7f92012-12-19 12:52:06 +01005144static const struct hda_codec_ops generic_patch_ops = {
5145 .build_controls = snd_hda_gen_build_controls,
5146 .build_pcms = snd_hda_gen_build_pcms,
5147 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005148 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005149 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005150#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005151 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005152#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153};
5154
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155int snd_hda_parse_generic_codec(struct hda_codec *codec)
5156{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005157 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 int err;
5159
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005160 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005161 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005163 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005166 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5167 if (err < 0)
5168 return err;
5169
5170 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005171 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 goto error;
5173
5174 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 return 0;
5176
Takashi Iwai352f7f92012-12-19 12:52:06 +01005177error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005178 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 return err;
5180}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005181EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);