blob: d7fa1ee8302c633e575898de90a24421a0c0ec66 [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,
1009 /* Primary DAC shared with main CLFE */
1010 BAD_SHARED_CLFE = 0x10,
1011 /* Primary DAC shared with extra surrounds */
1012 BAD_SHARED_EXTRA_SURROUND = 0x10,
1013 /* Volume widget is shared */
1014 BAD_SHARED_VOL = 0x10,
1015};
1016
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001017/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001018 * volume and mute controls, and assign the values to ctls[].
1019 *
1020 * When no appropriate widget is found in the path, the badness value
1021 * is incremented depending on the situation. The function returns the
1022 * total badness for both volume and mute controls.
1023 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001024static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001025{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001026 hda_nid_t nid;
1027 unsigned int val;
1028 int badness = 0;
1029
1030 if (!path)
1031 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001032
1033 if (path->ctls[NID_PATH_VOL_CTL] ||
1034 path->ctls[NID_PATH_MUTE_CTL])
1035 return 0; /* already evaluated */
1036
Takashi Iwai352f7f92012-12-19 12:52:06 +01001037 nid = look_for_out_vol_nid(codec, path);
1038 if (nid) {
1039 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1040 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1041 badness += BAD_SHARED_VOL;
1042 else
1043 path->ctls[NID_PATH_VOL_CTL] = val;
1044 } else
1045 badness += BAD_SHARED_VOL;
1046 nid = look_for_out_mute_nid(codec, path);
1047 if (nid) {
1048 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1049 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1050 nid_has_mute(codec, nid, HDA_OUTPUT))
1051 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1052 else
1053 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1054 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1055 badness += BAD_SHARED_VOL;
1056 else
1057 path->ctls[NID_PATH_MUTE_CTL] = val;
1058 } else
1059 badness += BAD_SHARED_VOL;
1060 return badness;
1061}
1062
1063struct badness_table {
1064 int no_primary_dac; /* no primary DAC */
1065 int no_dac; /* no secondary DACs */
1066 int shared_primary; /* primary DAC is shared with main output */
1067 int shared_surr; /* secondary DAC shared with main or primary */
1068 int shared_clfe; /* third DAC shared with main or primary */
1069 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1070};
1071
1072static struct badness_table main_out_badness = {
1073 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1074 .no_dac = BAD_NO_DAC,
1075 .shared_primary = BAD_NO_PRIMARY_DAC,
1076 .shared_surr = BAD_SHARED_SURROUND,
1077 .shared_clfe = BAD_SHARED_CLFE,
1078 .shared_surr_main = BAD_SHARED_SURROUND,
1079};
1080
1081static struct badness_table extra_out_badness = {
1082 .no_primary_dac = BAD_NO_DAC,
1083 .no_dac = BAD_NO_DAC,
1084 .shared_primary = BAD_NO_EXTRA_DAC,
1085 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1086 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1087 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1088};
1089
Takashi Iwai7385df62013-01-07 09:50:52 +01001090/* get the DAC of the primary output corresponding to the given array index */
1091static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1092{
1093 struct hda_gen_spec *spec = codec->spec;
1094 struct auto_pin_cfg *cfg = &spec->autocfg;
1095
1096 if (cfg->line_outs > idx)
1097 return spec->private_dac_nids[idx];
1098 idx -= cfg->line_outs;
1099 if (spec->multi_ios > idx)
1100 return spec->multi_io[idx].dac;
1101 return 0;
1102}
1103
1104/* return the DAC if it's reachable, otherwise zero */
1105static inline hda_nid_t try_dac(struct hda_codec *codec,
1106 hda_nid_t dac, hda_nid_t pin)
1107{
1108 return is_reachable_path(codec, dac, pin) ? dac : 0;
1109}
1110
Takashi Iwai352f7f92012-12-19 12:52:06 +01001111/* try to assign DACs to pins and return the resultant badness */
1112static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1113 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001114 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001115 const struct badness_table *bad)
1116{
1117 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001118 int i, j;
1119 int badness = 0;
1120 hda_nid_t dac;
1121
1122 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return 0;
1124
Takashi Iwai352f7f92012-12-19 12:52:06 +01001125 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001126 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001127 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001128
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001129 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1130 if (path) {
1131 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001132 continue;
1133 }
1134
1135 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001136 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001137 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001138 for (j = 1; j < num_outs; j++) {
1139 if (is_reachable_path(codec, dacs[j], pin)) {
1140 dacs[0] = dacs[j];
1141 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001142 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001143 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001144 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001147 }
1148 dac = dacs[i];
1149 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001150 if (num_outs > 2)
1151 dac = try_dac(codec, get_primary_out(codec, i), pin);
1152 if (!dac)
1153 dac = try_dac(codec, dacs[0], pin);
1154 if (!dac)
1155 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001156 if (dac) {
1157 if (!i)
1158 badness += bad->shared_primary;
1159 else if (i == 1)
1160 badness += bad->shared_surr;
1161 else
1162 badness += bad->shared_clfe;
1163 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1164 dac = spec->private_dac_nids[0];
1165 badness += bad->shared_surr_main;
1166 } else if (!i)
1167 badness += bad->no_primary_dac;
1168 else
1169 badness += bad->no_dac;
1170 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001171 if (!dac)
1172 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001173 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001174 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001175 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001176 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001177 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001178 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001179 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001180 badness += bad->no_dac;
1181 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001182 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001183 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001184 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001185 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001186 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001187 }
1188
1189 return badness;
1190}
1191
1192/* return NID if the given pin has only a single connection to a certain DAC */
1193static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1194{
1195 struct hda_gen_spec *spec = codec->spec;
1196 int i;
1197 hda_nid_t nid_found = 0;
1198
1199 for (i = 0; i < spec->num_all_dacs; i++) {
1200 hda_nid_t nid = spec->all_dacs[i];
1201 if (!nid || is_dac_already_used(codec, nid))
1202 continue;
1203 if (is_reachable_path(codec, nid, pin)) {
1204 if (nid_found)
1205 return 0;
1206 nid_found = nid;
1207 }
1208 }
1209 return nid_found;
1210}
1211
1212/* check whether the given pin can be a multi-io pin */
1213static bool can_be_multiio_pin(struct hda_codec *codec,
1214 unsigned int location, hda_nid_t nid)
1215{
1216 unsigned int defcfg, caps;
1217
1218 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1219 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1220 return false;
1221 if (location && get_defcfg_location(defcfg) != location)
1222 return false;
1223 caps = snd_hda_query_pin_caps(codec, nid);
1224 if (!(caps & AC_PINCAP_OUT))
1225 return false;
1226 return true;
1227}
1228
Takashi Iwaie22aab72013-01-04 14:50:04 +01001229/* count the number of input pins that are capable to be multi-io */
1230static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1231{
1232 struct hda_gen_spec *spec = codec->spec;
1233 struct auto_pin_cfg *cfg = &spec->autocfg;
1234 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1235 unsigned int location = get_defcfg_location(defcfg);
1236 int type, i;
1237 int num_pins = 0;
1238
1239 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1240 for (i = 0; i < cfg->num_inputs; i++) {
1241 if (cfg->inputs[i].type != type)
1242 continue;
1243 if (can_be_multiio_pin(codec, location,
1244 cfg->inputs[i].pin))
1245 num_pins++;
1246 }
1247 }
1248 return num_pins;
1249}
1250
Takashi Iwai352f7f92012-12-19 12:52:06 +01001251/*
1252 * multi-io helper
1253 *
1254 * When hardwired is set, try to fill ony hardwired pins, and returns
1255 * zero if any pins are filled, non-zero if nothing found.
1256 * When hardwired is off, try to fill possible input pins, and returns
1257 * the badness value.
1258 */
1259static int fill_multi_ios(struct hda_codec *codec,
1260 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001261 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001262{
1263 struct hda_gen_spec *spec = codec->spec;
1264 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001265 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001266 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1267 unsigned int location = get_defcfg_location(defcfg);
1268 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001269 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001270
1271 old_pins = spec->multi_ios;
1272 if (old_pins >= 2)
1273 goto end_fill;
1274
Takashi Iwaie22aab72013-01-04 14:50:04 +01001275 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001276 if (num_pins < 2)
1277 goto end_fill;
1278
Takashi Iwai352f7f92012-12-19 12:52:06 +01001279 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1280 for (i = 0; i < cfg->num_inputs; i++) {
1281 hda_nid_t nid = cfg->inputs[i].pin;
1282 hda_nid_t dac = 0;
1283
1284 if (cfg->inputs[i].type != type)
1285 continue;
1286 if (!can_be_multiio_pin(codec, location, nid))
1287 continue;
1288 for (j = 0; j < spec->multi_ios; j++) {
1289 if (nid == spec->multi_io[j].pin)
1290 break;
1291 }
1292 if (j < spec->multi_ios)
1293 continue;
1294
Takashi Iwai352f7f92012-12-19 12:52:06 +01001295 if (hardwired)
1296 dac = get_dac_if_single(codec, nid);
1297 else if (!dac)
1298 dac = look_for_dac(codec, nid, false);
1299 if (!dac) {
1300 badness++;
1301 continue;
1302 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001303 path = snd_hda_add_new_path(codec, dac, nid,
1304 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001305 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001306 badness++;
1307 continue;
1308 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001309 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001310 spec->multi_io[spec->multi_ios].pin = nid;
1311 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001312 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1313 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001314 spec->multi_ios++;
1315 if (spec->multi_ios >= 2)
1316 break;
1317 }
1318 }
1319 end_fill:
1320 if (badness)
1321 badness = BAD_MULTI_IO;
1322 if (old_pins == spec->multi_ios) {
1323 if (hardwired)
1324 return 1; /* nothing found */
1325 else
1326 return badness; /* no badness if nothing found */
1327 }
1328 if (!hardwired && spec->multi_ios < 2) {
1329 /* cancel newly assigned paths */
1330 spec->paths.used -= spec->multi_ios - old_pins;
1331 spec->multi_ios = old_pins;
1332 return badness;
1333 }
1334
1335 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001336 for (i = old_pins; i < spec->multi_ios; i++) {
1337 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1338 badness += assign_out_path_ctls(codec, path);
1339 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001340
1341 return badness;
1342}
1343
1344/* map DACs for all pins in the list if they are single connections */
1345static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001346 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001347{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001348 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001349 int i;
1350 bool found = false;
1351 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001352 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001353 hda_nid_t dac;
1354 if (dacs[i])
1355 continue;
1356 dac = get_dac_if_single(codec, pins[i]);
1357 if (!dac)
1358 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001359 path = snd_hda_add_new_path(codec, dac, pins[i],
1360 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001361 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001362 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001363 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001364 dacs[i] = dac;
1365 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001366 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001367 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001368 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001369 }
1370 }
1371 return found;
1372}
1373
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001374/* create a new path including aamix if available, and return its index */
1375static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1376{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001377 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001378 struct nid_path *path;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001379 hda_nid_t dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001380
1381 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001382 if (!path || !path->depth ||
1383 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001384 return 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001385 dac = path->path[0];
1386 pin = path->path[path->depth - 1];
1387 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1388 if (!path) {
1389 if (dac != spec->multiout.dac_nids[0])
1390 dac = spec->multiout.dac_nids[0];
1391 else if (spec->multiout.hp_out_nid[0])
1392 dac = spec->multiout.hp_out_nid[0];
1393 else if (spec->multiout.extra_out_nid[0])
1394 dac = spec->multiout.extra_out_nid[0];
1395 if (dac)
1396 path = snd_hda_add_new_path(codec, dac, pin,
1397 spec->mixer_nid);
1398 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001399 if (!path)
1400 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001401 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001402 path->active = false; /* unused as default */
1403 return snd_hda_get_path_idx(codec, path);
1404}
1405
Takashi Iwaia07a9492013-01-07 16:44:06 +01001406/* fill the empty entries in the dac array for speaker/hp with the
1407 * shared dac pointed by the paths
1408 */
1409static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1410 hda_nid_t *dacs, int *path_idx)
1411{
1412 struct nid_path *path;
1413 int i;
1414
1415 for (i = 0; i < num_outs; i++) {
1416 if (dacs[i])
1417 continue;
1418 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1419 if (!path)
1420 continue;
1421 dacs[i] = path->path[0];
1422 }
1423}
1424
Takashi Iwai352f7f92012-12-19 12:52:06 +01001425/* fill in the dac_nids table from the parsed pin configuration */
1426static int fill_and_eval_dacs(struct hda_codec *codec,
1427 bool fill_hardwired,
1428 bool fill_mio_first)
1429{
1430 struct hda_gen_spec *spec = codec->spec;
1431 struct auto_pin_cfg *cfg = &spec->autocfg;
1432 int i, err, badness;
1433
1434 /* set num_dacs once to full for look_for_dac() */
1435 spec->multiout.num_dacs = cfg->line_outs;
1436 spec->multiout.dac_nids = spec->private_dac_nids;
1437 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1438 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1439 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1440 spec->multi_ios = 0;
1441 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001442
1443 /* clear path indices */
1444 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1445 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1446 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1447 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1448 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001449 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001450 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1451 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1452
Takashi Iwai352f7f92012-12-19 12:52:06 +01001453 badness = 0;
1454
1455 /* fill hard-wired DACs first */
1456 if (fill_hardwired) {
1457 bool mapped;
1458 do {
1459 mapped = map_singles(codec, cfg->line_outs,
1460 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001461 spec->private_dac_nids,
1462 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001463 mapped |= map_singles(codec, cfg->hp_outs,
1464 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001465 spec->multiout.hp_out_nid,
1466 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001467 mapped |= map_singles(codec, cfg->speaker_outs,
1468 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001469 spec->multiout.extra_out_nid,
1470 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001471 if (fill_mio_first && cfg->line_outs == 1 &&
1472 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001473 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001474 if (!err)
1475 mapped = true;
1476 }
1477 } while (mapped);
1478 }
1479
1480 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001481 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001482 &main_out_badness);
1483
Takashi Iwai352f7f92012-12-19 12:52:06 +01001484 if (fill_mio_first &&
1485 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1486 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001487 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001488 if (err < 0)
1489 return err;
1490 /* we don't count badness at this stage yet */
1491 }
1492
1493 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1494 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1495 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001496 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001497 &extra_out_badness);
1498 if (err < 0)
1499 return err;
1500 badness += err;
1501 }
1502 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1503 err = try_assign_dacs(codec, cfg->speaker_outs,
1504 cfg->speaker_pins,
1505 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001506 spec->speaker_paths,
1507 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001508 if (err < 0)
1509 return err;
1510 badness += err;
1511 }
1512 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001513 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001514 if (err < 0)
1515 return err;
1516 badness += err;
1517 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001518
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001519 if (spec->mixer_nid) {
1520 spec->aamix_out_paths[0] =
1521 check_aamix_out_path(codec, spec->out_paths[0]);
1522 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1523 spec->aamix_out_paths[1] =
1524 check_aamix_out_path(codec, spec->hp_paths[0]);
1525 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1526 spec->aamix_out_paths[2] =
1527 check_aamix_out_path(codec, spec->speaker_paths[0]);
1528 }
1529
Takashi Iwaie22aab72013-01-04 14:50:04 +01001530 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1531 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1532 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001533
Takashi Iwaia07a9492013-01-07 16:44:06 +01001534 /* re-count num_dacs and squash invalid entries */
1535 spec->multiout.num_dacs = 0;
1536 for (i = 0; i < cfg->line_outs; i++) {
1537 if (spec->private_dac_nids[i])
1538 spec->multiout.num_dacs++;
1539 else {
1540 memmove(spec->private_dac_nids + i,
1541 spec->private_dac_nids + i + 1,
1542 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1543 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1544 }
1545 }
1546
1547 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001548 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001549
Takashi Iwai352f7f92012-12-19 12:52:06 +01001550 if (spec->multi_ios == 2) {
1551 for (i = 0; i < 2; i++)
1552 spec->private_dac_nids[spec->multiout.num_dacs++] =
1553 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001554 } else if (spec->multi_ios) {
1555 spec->multi_ios = 0;
1556 badness += BAD_MULTI_IO;
1557 }
1558
Takashi Iwaia07a9492013-01-07 16:44:06 +01001559 /* re-fill the shared DAC for speaker / headphone */
1560 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1561 refill_shared_dacs(codec, cfg->hp_outs,
1562 spec->multiout.hp_out_nid,
1563 spec->hp_paths);
1564 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1565 refill_shared_dacs(codec, cfg->speaker_outs,
1566 spec->multiout.extra_out_nid,
1567 spec->speaker_paths);
1568
Takashi Iwai352f7f92012-12-19 12:52:06 +01001569 return badness;
1570}
1571
1572#define DEBUG_BADNESS
1573
1574#ifdef DEBUG_BADNESS
1575#define debug_badness snd_printdd
1576#else
1577#define debug_badness(...)
1578#endif
1579
Takashi Iwaia7694092013-01-21 10:43:18 +01001580#ifdef DEBUG_BADNESS
1581static inline void print_nid_path_idx(struct hda_codec *codec,
1582 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001583{
Takashi Iwaia7694092013-01-21 10:43:18 +01001584 struct nid_path *path;
1585
1586 path = snd_hda_get_path_from_idx(codec, idx);
1587 if (path)
1588 print_nid_path(pfx, path);
1589}
1590
1591static void debug_show_configs(struct hda_codec *codec,
1592 struct auto_pin_cfg *cfg)
1593{
1594 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001595 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001596 int i;
1597
1598 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001599 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001600 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001601 spec->multiout.dac_nids[0],
1602 spec->multiout.dac_nids[1],
1603 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001604 spec->multiout.dac_nids[3],
1605 lo_type[cfg->line_out_type]);
1606 for (i = 0; i < cfg->line_outs; i++)
1607 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001608 if (spec->multi_ios > 0)
1609 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1610 spec->multi_ios,
1611 spec->multi_io[0].pin, spec->multi_io[1].pin,
1612 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001613 for (i = 0; i < spec->multi_ios; i++)
1614 print_nid_path_idx(codec, " mio",
1615 spec->out_paths[cfg->line_outs + i]);
1616 if (cfg->hp_outs)
1617 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001618 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001619 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001620 spec->multiout.hp_out_nid[0],
1621 spec->multiout.hp_out_nid[1],
1622 spec->multiout.hp_out_nid[2],
1623 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001624 for (i = 0; i < cfg->hp_outs; i++)
1625 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1626 if (cfg->speaker_outs)
1627 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001628 cfg->speaker_pins[0], cfg->speaker_pins[1],
1629 cfg->speaker_pins[2], cfg->speaker_pins[3],
1630 spec->multiout.extra_out_nid[0],
1631 spec->multiout.extra_out_nid[1],
1632 spec->multiout.extra_out_nid[2],
1633 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001634 for (i = 0; i < cfg->speaker_outs; i++)
1635 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1636 for (i = 0; i < 3; i++)
1637 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001638}
Takashi Iwaia7694092013-01-21 10:43:18 +01001639#else
1640#define debug_show_configs(codec, cfg) /* NOP */
1641#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001642
1643/* find all available DACs of the codec */
1644static void fill_all_dac_nids(struct hda_codec *codec)
1645{
1646 struct hda_gen_spec *spec = codec->spec;
1647 int i;
1648 hda_nid_t nid = codec->start_nid;
1649
1650 spec->num_all_dacs = 0;
1651 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1652 for (i = 0; i < codec->num_nodes; i++, nid++) {
1653 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1654 continue;
1655 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1656 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1657 break;
1658 }
1659 spec->all_dacs[spec->num_all_dacs++] = nid;
1660 }
1661}
1662
1663static int parse_output_paths(struct hda_codec *codec)
1664{
1665 struct hda_gen_spec *spec = codec->spec;
1666 struct auto_pin_cfg *cfg = &spec->autocfg;
1667 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001668 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001669 int best_badness = INT_MAX;
1670 int badness;
1671 bool fill_hardwired = true, fill_mio_first = true;
1672 bool best_wired = true, best_mio = true;
1673 bool hp_spk_swapped = false;
1674
Takashi Iwai352f7f92012-12-19 12:52:06 +01001675 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1676 if (!best_cfg)
1677 return -ENOMEM;
1678 *best_cfg = *cfg;
1679
1680 for (;;) {
1681 badness = fill_and_eval_dacs(codec, fill_hardwired,
1682 fill_mio_first);
1683 if (badness < 0) {
1684 kfree(best_cfg);
1685 return badness;
1686 }
1687 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1688 cfg->line_out_type, fill_hardwired, fill_mio_first,
1689 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001690 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001691 if (badness < best_badness) {
1692 best_badness = badness;
1693 *best_cfg = *cfg;
1694 best_wired = fill_hardwired;
1695 best_mio = fill_mio_first;
1696 }
1697 if (!badness)
1698 break;
1699 fill_mio_first = !fill_mio_first;
1700 if (!fill_mio_first)
1701 continue;
1702 fill_hardwired = !fill_hardwired;
1703 if (!fill_hardwired)
1704 continue;
1705 if (hp_spk_swapped)
1706 break;
1707 hp_spk_swapped = true;
1708 if (cfg->speaker_outs > 0 &&
1709 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1710 cfg->hp_outs = cfg->line_outs;
1711 memcpy(cfg->hp_pins, cfg->line_out_pins,
1712 sizeof(cfg->hp_pins));
1713 cfg->line_outs = cfg->speaker_outs;
1714 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1715 sizeof(cfg->speaker_pins));
1716 cfg->speaker_outs = 0;
1717 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1718 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1719 fill_hardwired = true;
1720 continue;
1721 }
1722 if (cfg->hp_outs > 0 &&
1723 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1724 cfg->speaker_outs = cfg->line_outs;
1725 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1726 sizeof(cfg->speaker_pins));
1727 cfg->line_outs = cfg->hp_outs;
1728 memcpy(cfg->line_out_pins, cfg->hp_pins,
1729 sizeof(cfg->hp_pins));
1730 cfg->hp_outs = 0;
1731 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1732 cfg->line_out_type = AUTO_PIN_HP_OUT;
1733 fill_hardwired = true;
1734 continue;
1735 }
1736 break;
1737 }
1738
1739 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001740 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001741 *cfg = *best_cfg;
1742 fill_and_eval_dacs(codec, best_wired, best_mio);
1743 }
1744 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1745 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001746 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001747
1748 if (cfg->line_out_pins[0]) {
1749 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001750 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001751 if (path)
1752 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001753 if (spec->vmaster_nid)
1754 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1755 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001756 }
1757
Takashi Iwai9314a582013-01-21 10:49:05 +01001758 /* set initial pinctl targets */
1759 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1760 val = PIN_HP;
1761 else
1762 val = PIN_OUT;
1763 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1764 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1765 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1766 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1767 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1768 set_pin_targets(codec, cfg->speaker_outs,
1769 cfg->speaker_pins, val);
1770 }
1771
Takashi Iwai352f7f92012-12-19 12:52:06 +01001772 kfree(best_cfg);
1773 return 0;
1774}
1775
1776/* add playback controls from the parsed DAC table */
1777static int create_multi_out_ctls(struct hda_codec *codec,
1778 const struct auto_pin_cfg *cfg)
1779{
1780 struct hda_gen_spec *spec = codec->spec;
1781 int i, err, noutputs;
1782
1783 noutputs = cfg->line_outs;
1784 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1785 noutputs += spec->multi_ios;
1786
1787 for (i = 0; i < noutputs; i++) {
1788 const char *name;
1789 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001790 struct nid_path *path;
1791
Takashi Iwai196c17662013-01-04 15:01:40 +01001792 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001793 if (!path)
1794 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001795
1796 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001797 if (!name || !strcmp(name, "CLFE")) {
1798 /* Center/LFE */
1799 err = add_vol_ctl(codec, "Center", 0, 1, path);
1800 if (err < 0)
1801 return err;
1802 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1803 if (err < 0)
1804 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001805 } else {
1806 err = add_stereo_vol(codec, name, index, path);
1807 if (err < 0)
1808 return err;
1809 }
1810
1811 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1812 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001813 err = add_sw_ctl(codec, "Center", 0, 1, path);
1814 if (err < 0)
1815 return err;
1816 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1817 if (err < 0)
1818 return err;
1819 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001820 err = add_stereo_sw(codec, name, index, path);
1821 if (err < 0)
1822 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824 }
1825 return 0;
1826}
1827
Takashi Iwaic2c80382013-01-07 10:33:57 +01001828static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001829 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001831 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 int err;
1833
Takashi Iwai196c17662013-01-04 15:01:40 +01001834 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001835 if (!path)
1836 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001837 err = add_stereo_vol(codec, pfx, cidx, path);
1838 if (err < 0)
1839 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001840 err = add_stereo_sw(codec, pfx, cidx, path);
1841 if (err < 0)
1842 return err;
1843 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Takashi Iwai352f7f92012-12-19 12:52:06 +01001846/* add playback controls for speaker and HP outputs */
1847static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001848 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001850 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001851
1852 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001853 const char *name;
1854 char tmp[44];
1855 int err, idx = 0;
1856
1857 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1858 name = "Bass Speaker";
1859 else if (num_pins >= 3) {
1860 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001861 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001862 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001863 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001864 name = pfx;
1865 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001867 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001868 if (err < 0)
1869 return err;
1870 }
1871 return 0;
1872}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001873
Takashi Iwai352f7f92012-12-19 12:52:06 +01001874static int create_hp_out_ctls(struct hda_codec *codec)
1875{
1876 struct hda_gen_spec *spec = codec->spec;
1877 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001878 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001879 "Headphone");
1880}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Takashi Iwai352f7f92012-12-19 12:52:06 +01001882static int create_speaker_out_ctls(struct hda_codec *codec)
1883{
1884 struct hda_gen_spec *spec = codec->spec;
1885 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001886 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001887 "Speaker");
1888}
1889
1890/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001891 * independent HP controls
1892 */
1893
Takashi Iwai8ba955c2013-03-07 18:40:58 +01001894/* update HP auto-mute state too */
1895static void update_hp_automute_hook(struct hda_codec *codec)
1896{
1897 struct hda_gen_spec *spec = codec->spec;
1898
1899 if (spec->hp_automute_hook)
1900 spec->hp_automute_hook(codec, NULL);
1901 else
1902 snd_hda_gen_hp_automute(codec, NULL);
1903}
1904
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001905static int indep_hp_info(struct snd_kcontrol *kcontrol,
1906 struct snd_ctl_elem_info *uinfo)
1907{
1908 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1909}
1910
1911static int indep_hp_get(struct snd_kcontrol *kcontrol,
1912 struct snd_ctl_elem_value *ucontrol)
1913{
1914 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1915 struct hda_gen_spec *spec = codec->spec;
1916 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1917 return 0;
1918}
1919
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001920static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1921 int nomix_path_idx, int mix_path_idx,
1922 int out_type);
1923
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001924static int indep_hp_put(struct snd_kcontrol *kcontrol,
1925 struct snd_ctl_elem_value *ucontrol)
1926{
1927 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1928 struct hda_gen_spec *spec = codec->spec;
1929 unsigned int select = ucontrol->value.enumerated.item[0];
1930 int ret = 0;
1931
1932 mutex_lock(&spec->pcm_mutex);
1933 if (spec->active_streams) {
1934 ret = -EBUSY;
1935 goto unlock;
1936 }
1937
1938 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001939 hda_nid_t *dacp;
1940 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1941 dacp = &spec->private_dac_nids[0];
1942 else
1943 dacp = &spec->multiout.hp_out_nid[0];
1944
1945 /* update HP aamix paths in case it conflicts with indep HP */
1946 if (spec->have_aamix_ctl) {
1947 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1948 update_aamix_paths(codec, spec->aamix_mode,
1949 spec->out_paths[0],
1950 spec->aamix_out_paths[0],
1951 spec->autocfg.line_out_type);
1952 else
1953 update_aamix_paths(codec, spec->aamix_mode,
1954 spec->hp_paths[0],
1955 spec->aamix_out_paths[1],
1956 AUTO_PIN_HP_OUT);
1957 }
1958
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001959 spec->indep_hp_enabled = select;
1960 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001961 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001962 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001963 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01001964
Takashi Iwai8ba955c2013-03-07 18:40:58 +01001965 update_hp_automute_hook(codec);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001966 ret = 1;
1967 }
1968 unlock:
1969 mutex_unlock(&spec->pcm_mutex);
1970 return ret;
1971}
1972
1973static const struct snd_kcontrol_new indep_hp_ctl = {
1974 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1975 .name = "Independent HP",
1976 .info = indep_hp_info,
1977 .get = indep_hp_get,
1978 .put = indep_hp_put,
1979};
1980
1981
1982static int create_indep_hp_ctls(struct hda_codec *codec)
1983{
1984 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001985 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001986
1987 if (!spec->indep_hp)
1988 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001989 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1990 dac = spec->multiout.dac_nids[0];
1991 else
1992 dac = spec->multiout.hp_out_nid[0];
1993 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001994 spec->indep_hp = 0;
1995 return 0;
1996 }
1997
1998 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001999 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002000 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2001 return -ENOMEM;
2002 return 0;
2003}
2004
2005/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002006 * channel mode enum control
2007 */
2008
2009static int ch_mode_info(struct snd_kcontrol *kcontrol,
2010 struct snd_ctl_elem_info *uinfo)
2011{
2012 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2013 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002014 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002015
2016 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2017 uinfo->count = 1;
2018 uinfo->value.enumerated.items = spec->multi_ios + 1;
2019 if (uinfo->value.enumerated.item > spec->multi_ios)
2020 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002021 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2022 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002023 return 0;
2024}
2025
2026static int ch_mode_get(struct snd_kcontrol *kcontrol,
2027 struct snd_ctl_elem_value *ucontrol)
2028{
2029 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2030 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002031 ucontrol->value.enumerated.item[0] =
2032 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002033 return 0;
2034}
2035
Takashi Iwai196c17662013-01-04 15:01:40 +01002036static inline struct nid_path *
2037get_multiio_path(struct hda_codec *codec, int idx)
2038{
2039 struct hda_gen_spec *spec = codec->spec;
2040 return snd_hda_get_path_from_idx(codec,
2041 spec->out_paths[spec->autocfg.line_outs + idx]);
2042}
2043
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002044static void update_automute_all(struct hda_codec *codec);
2045
Takashi Iwai352f7f92012-12-19 12:52:06 +01002046static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2047{
2048 struct hda_gen_spec *spec = codec->spec;
2049 hda_nid_t nid = spec->multi_io[idx].pin;
2050 struct nid_path *path;
2051
Takashi Iwai196c17662013-01-04 15:01:40 +01002052 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002053 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002055
2056 if (path->active == output)
2057 return 0;
2058
2059 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002060 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002061 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002062 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002063 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002064 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002065 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002066 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002067 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002069
2070 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002071 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002072
Takashi Iwai352f7f92012-12-19 12:52:06 +01002073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
Takashi Iwai352f7f92012-12-19 12:52:06 +01002076static int ch_mode_put(struct snd_kcontrol *kcontrol,
2077 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002079 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2080 struct hda_gen_spec *spec = codec->spec;
2081 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Takashi Iwai352f7f92012-12-19 12:52:06 +01002083 ch = ucontrol->value.enumerated.item[0];
2084 if (ch < 0 || ch > spec->multi_ios)
2085 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002086 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002087 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002088 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002089 for (i = 0; i < spec->multi_ios; i++)
2090 set_multi_io(codec, i, i < ch);
2091 spec->multiout.max_channels = max(spec->ext_channel_count,
2092 spec->const_channel_count);
2093 if (spec->need_dac_fix)
2094 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 return 1;
2096}
2097
Takashi Iwai352f7f92012-12-19 12:52:06 +01002098static const struct snd_kcontrol_new channel_mode_enum = {
2099 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2100 .name = "Channel Mode",
2101 .info = ch_mode_info,
2102 .get = ch_mode_get,
2103 .put = ch_mode_put,
2104};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Takashi Iwai352f7f92012-12-19 12:52:06 +01002106static int create_multi_channel_mode(struct hda_codec *codec)
2107{
2108 struct hda_gen_spec *spec = codec->spec;
2109
2110 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002111 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002112 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return 0;
2115}
2116
Takashi Iwai352f7f92012-12-19 12:52:06 +01002117/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002118 * aamix loopback enable/disable switch
2119 */
2120
2121#define loopback_mixing_info indep_hp_info
2122
2123static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2124 struct snd_ctl_elem_value *ucontrol)
2125{
2126 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2127 struct hda_gen_spec *spec = codec->spec;
2128 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2129 return 0;
2130}
2131
2132static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002133 int nomix_path_idx, int mix_path_idx,
2134 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002135{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002136 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002137 struct nid_path *nomix_path, *mix_path;
2138
2139 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2140 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2141 if (!nomix_path || !mix_path)
2142 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002143
2144 /* if HP aamix path is driven from a different DAC and the
2145 * independent HP mode is ON, can't turn on aamix path
2146 */
2147 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2148 mix_path->path[0] != spec->alt_dac_nid)
2149 do_mix = false;
2150
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002151 if (do_mix) {
2152 snd_hda_activate_path(codec, nomix_path, false, true);
2153 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002154 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002155 } else {
2156 snd_hda_activate_path(codec, mix_path, false, true);
2157 snd_hda_activate_path(codec, nomix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002158 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002159 }
2160}
2161
2162static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2163 struct snd_ctl_elem_value *ucontrol)
2164{
2165 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2166 struct hda_gen_spec *spec = codec->spec;
2167 unsigned int val = ucontrol->value.enumerated.item[0];
2168
2169 if (val == spec->aamix_mode)
2170 return 0;
2171 spec->aamix_mode = val;
2172 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002173 spec->aamix_out_paths[0],
2174 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002175 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002176 spec->aamix_out_paths[1],
2177 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002178 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002179 spec->aamix_out_paths[2],
2180 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002181 return 1;
2182}
2183
2184static const struct snd_kcontrol_new loopback_mixing_enum = {
2185 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2186 .name = "Loopback Mixing",
2187 .info = loopback_mixing_info,
2188 .get = loopback_mixing_get,
2189 .put = loopback_mixing_put,
2190};
2191
2192static int create_loopback_mixing_ctl(struct hda_codec *codec)
2193{
2194 struct hda_gen_spec *spec = codec->spec;
2195
2196 if (!spec->mixer_nid)
2197 return 0;
2198 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2199 spec->aamix_out_paths[2]))
2200 return 0;
2201 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2202 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002203 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002204 return 0;
2205}
2206
2207/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002208 * shared headphone/mic handling
2209 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002210
Takashi Iwai352f7f92012-12-19 12:52:06 +01002211static void call_update_outputs(struct hda_codec *codec);
2212
2213/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002214static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002215{
2216 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002217 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002218 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002219 hda_nid_t pin;
2220
2221 pin = spec->hp_mic_pin;
2222 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2223
2224 if (!force) {
2225 val = snd_hda_codec_get_pin_target(codec, pin);
2226 if (as_mic) {
2227 if (val & PIN_IN)
2228 return;
2229 } else {
2230 if (val & PIN_OUT)
2231 return;
2232 }
2233 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002234
2235 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002236 /* if the HP pin doesn't support VREF and the codec driver gives an
2237 * alternative pin, set up the VREF on that pin instead
2238 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002239 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2240 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2241 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2242 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002243 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002244 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002245 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002246
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002247 if (!spec->hp_mic_jack_modes) {
2248 if (as_mic)
2249 val |= PIN_IN;
2250 else
2251 val = PIN_HP;
2252 set_pin_target(codec, pin, val, true);
2253 update_hp_automute_hook(codec);
2254 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002255}
2256
2257/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002258static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002259{
2260 struct hda_gen_spec *spec = codec->spec;
2261 struct auto_pin_cfg *cfg = &spec->autocfg;
2262 unsigned int defcfg;
2263 hda_nid_t nid;
2264
Takashi Iwai967303d2013-02-19 17:12:42 +01002265 if (!spec->hp_mic) {
2266 if (spec->suppress_hp_mic_detect)
2267 return 0;
2268 /* automatic detection: only if no input or a single internal
2269 * input pin is found, try to detect the shared hp/mic
2270 */
2271 if (cfg->num_inputs > 1)
2272 return 0;
2273 else if (cfg->num_inputs == 1) {
2274 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2275 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2276 return 0;
2277 }
2278 }
2279
2280 spec->hp_mic = 0; /* clear once */
2281 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002282 return 0;
2283
Takashi Iwai967303d2013-02-19 17:12:42 +01002284 nid = 0;
2285 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2286 nid = cfg->line_out_pins[0];
2287 else if (cfg->hp_outs > 0)
2288 nid = cfg->hp_pins[0];
2289 if (!nid)
2290 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002291
2292 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2293 return 0; /* no input */
2294
Takashi Iwai967303d2013-02-19 17:12:42 +01002295 cfg->inputs[cfg->num_inputs].pin = nid;
2296 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2297 cfg->num_inputs++;
2298 spec->hp_mic = 1;
2299 spec->hp_mic_pin = nid;
2300 /* we can't handle auto-mic together with HP-mic */
2301 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002302 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2303 return 0;
2304}
2305
Takashi Iwai978e77e2013-01-10 16:57:58 +01002306/*
2307 * output jack mode
2308 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002309
2310static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2311
2312static const char * const out_jack_texts[] = {
2313 "Line Out", "Headphone Out",
2314};
2315
Takashi Iwai978e77e2013-01-10 16:57:58 +01002316static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2317 struct snd_ctl_elem_info *uinfo)
2318{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002319 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002320}
2321
2322static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2323 struct snd_ctl_elem_value *ucontrol)
2324{
2325 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2326 hda_nid_t nid = kcontrol->private_value;
2327 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2328 ucontrol->value.enumerated.item[0] = 1;
2329 else
2330 ucontrol->value.enumerated.item[0] = 0;
2331 return 0;
2332}
2333
2334static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2335 struct snd_ctl_elem_value *ucontrol)
2336{
2337 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2338 hda_nid_t nid = kcontrol->private_value;
2339 unsigned int val;
2340
2341 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2342 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2343 return 0;
2344 snd_hda_set_pin_ctl_cache(codec, nid, val);
2345 return 1;
2346}
2347
2348static const struct snd_kcontrol_new out_jack_mode_enum = {
2349 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2350 .info = out_jack_mode_info,
2351 .get = out_jack_mode_get,
2352 .put = out_jack_mode_put,
2353};
2354
2355static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2356{
2357 struct hda_gen_spec *spec = codec->spec;
2358 int i;
2359
2360 for (i = 0; i < spec->kctls.used; i++) {
2361 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2362 if (!strcmp(kctl->name, name) && kctl->index == idx)
2363 return true;
2364 }
2365 return false;
2366}
2367
2368static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2369 char *name, size_t name_len)
2370{
2371 struct hda_gen_spec *spec = codec->spec;
2372 int idx = 0;
2373
2374 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2375 strlcat(name, " Jack Mode", name_len);
2376
2377 for (; find_kctl_name(codec, name, idx); idx++)
2378 ;
2379}
2380
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002381static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2382{
2383 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002384 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002385 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2386 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2387 return 2;
2388 }
2389 return 1;
2390}
2391
Takashi Iwai978e77e2013-01-10 16:57:58 +01002392static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2393 hda_nid_t *pins)
2394{
2395 struct hda_gen_spec *spec = codec->spec;
2396 int i;
2397
2398 for (i = 0; i < num_pins; i++) {
2399 hda_nid_t pin = pins[i];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002400 if (pin == spec->hp_mic_pin) {
2401 int ret = create_hp_mic_jack_mode(codec, pin);
2402 if (ret < 0)
2403 return ret;
2404 continue;
2405 }
2406 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002407 struct snd_kcontrol_new *knew;
2408 char name[44];
2409 get_jack_mode_name(codec, pin, name, sizeof(name));
2410 knew = snd_hda_gen_add_kctl(spec, name,
2411 &out_jack_mode_enum);
2412 if (!knew)
2413 return -ENOMEM;
2414 knew->private_value = pin;
2415 }
2416 }
2417
2418 return 0;
2419}
2420
Takashi Iwai294765582013-01-17 09:52:11 +01002421/*
2422 * input jack mode
2423 */
2424
2425/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2426#define NUM_VREFS 6
2427
2428static const char * const vref_texts[NUM_VREFS] = {
2429 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2430 "", "Mic 80pc Bias", "Mic 100pc Bias"
2431};
2432
2433static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2434{
2435 unsigned int pincap;
2436
2437 pincap = snd_hda_query_pin_caps(codec, pin);
2438 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2439 /* filter out unusual vrefs */
2440 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2441 return pincap;
2442}
2443
2444/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2445static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2446{
2447 unsigned int i, n = 0;
2448
2449 for (i = 0; i < NUM_VREFS; i++) {
2450 if (vref_caps & (1 << i)) {
2451 if (n == item_idx)
2452 return i;
2453 n++;
2454 }
2455 }
2456 return 0;
2457}
2458
2459/* convert back from the vref ctl index to the enum item index */
2460static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2461{
2462 unsigned int i, n = 0;
2463
2464 for (i = 0; i < NUM_VREFS; i++) {
2465 if (i == idx)
2466 return n;
2467 if (vref_caps & (1 << i))
2468 n++;
2469 }
2470 return 0;
2471}
2472
2473static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2474 struct snd_ctl_elem_info *uinfo)
2475{
2476 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2477 hda_nid_t nid = kcontrol->private_value;
2478 unsigned int vref_caps = get_vref_caps(codec, nid);
2479
2480 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2481 vref_texts);
2482 /* set the right text */
2483 strcpy(uinfo->value.enumerated.name,
2484 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2485 return 0;
2486}
2487
2488static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2489 struct snd_ctl_elem_value *ucontrol)
2490{
2491 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2492 hda_nid_t nid = kcontrol->private_value;
2493 unsigned int vref_caps = get_vref_caps(codec, nid);
2494 unsigned int idx;
2495
2496 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2497 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2498 return 0;
2499}
2500
2501static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2502 struct snd_ctl_elem_value *ucontrol)
2503{
2504 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2505 hda_nid_t nid = kcontrol->private_value;
2506 unsigned int vref_caps = get_vref_caps(codec, nid);
2507 unsigned int val, idx;
2508
2509 val = snd_hda_codec_get_pin_target(codec, nid);
2510 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2511 if (idx == ucontrol->value.enumerated.item[0])
2512 return 0;
2513
2514 val &= ~AC_PINCTL_VREFEN;
2515 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2516 snd_hda_set_pin_ctl_cache(codec, nid, val);
2517 return 1;
2518}
2519
2520static const struct snd_kcontrol_new in_jack_mode_enum = {
2521 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2522 .info = in_jack_mode_info,
2523 .get = in_jack_mode_get,
2524 .put = in_jack_mode_put,
2525};
2526
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002527static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2528{
2529 struct hda_gen_spec *spec = codec->spec;
2530 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002531 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002532 nitems = hweight32(get_vref_caps(codec, pin));
2533 return nitems ? nitems : 1;
2534}
2535
Takashi Iwai294765582013-01-17 09:52:11 +01002536static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2537{
2538 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002539 struct snd_kcontrol_new *knew;
2540 char name[44];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002541 unsigned int defcfg;
2542
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002543 if (pin == spec->hp_mic_pin)
2544 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002545
2546 /* no jack mode for fixed pins */
2547 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2548 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2549 return 0;
2550
2551 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002552 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002553 return 0;
2554
2555 get_jack_mode_name(codec, pin, name, sizeof(name));
2556 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2557 if (!knew)
2558 return -ENOMEM;
2559 knew->private_value = pin;
2560 return 0;
2561}
2562
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002563/*
2564 * HP/mic shared jack mode
2565 */
2566static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2567 struct snd_ctl_elem_info *uinfo)
2568{
2569 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2570 hda_nid_t nid = kcontrol->private_value;
2571 int out_jacks = get_out_jack_num_items(codec, nid);
2572 int in_jacks = get_in_jack_num_items(codec, nid);
2573 const char *text = NULL;
2574 int idx;
2575
2576 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2577 uinfo->count = 1;
2578 uinfo->value.enumerated.items = out_jacks + in_jacks;
2579 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2580 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2581 idx = uinfo->value.enumerated.item;
2582 if (idx < out_jacks) {
2583 if (out_jacks > 1)
2584 text = out_jack_texts[idx];
2585 else
2586 text = "Headphone Out";
2587 } else {
2588 idx -= out_jacks;
2589 if (in_jacks > 1) {
2590 unsigned int vref_caps = get_vref_caps(codec, nid);
2591 text = vref_texts[get_vref_idx(vref_caps, idx)];
2592 } else
2593 text = "Mic In";
2594 }
2595
2596 strcpy(uinfo->value.enumerated.name, text);
2597 return 0;
2598}
2599
2600static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2601{
2602 int out_jacks = get_out_jack_num_items(codec, nid);
2603 int in_jacks = get_in_jack_num_items(codec, nid);
2604 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2605 int idx = 0;
2606
2607 if (val & PIN_OUT) {
2608 if (out_jacks > 1 && val == PIN_HP)
2609 idx = 1;
2610 } else if (val & PIN_IN) {
2611 idx = out_jacks;
2612 if (in_jacks > 1) {
2613 unsigned int vref_caps = get_vref_caps(codec, nid);
2614 val &= AC_PINCTL_VREFEN;
2615 idx += cvt_from_vref_idx(vref_caps, val);
2616 }
2617 }
2618 return idx;
2619}
2620
2621static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2622 struct snd_ctl_elem_value *ucontrol)
2623{
2624 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2625 hda_nid_t nid = kcontrol->private_value;
2626 ucontrol->value.enumerated.item[0] =
2627 get_cur_hp_mic_jack_mode(codec, nid);
2628 return 0;
2629}
2630
2631static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2632 struct snd_ctl_elem_value *ucontrol)
2633{
2634 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2635 hda_nid_t nid = kcontrol->private_value;
2636 int out_jacks = get_out_jack_num_items(codec, nid);
2637 int in_jacks = get_in_jack_num_items(codec, nid);
2638 unsigned int val, oldval, idx;
2639
2640 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2641 idx = ucontrol->value.enumerated.item[0];
2642 if (oldval == idx)
2643 return 0;
2644
2645 if (idx < out_jacks) {
2646 if (out_jacks > 1)
2647 val = idx ? PIN_HP : PIN_OUT;
2648 else
2649 val = PIN_HP;
2650 } else {
2651 idx -= out_jacks;
2652 if (in_jacks > 1) {
2653 unsigned int vref_caps = get_vref_caps(codec, nid);
2654 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002655 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2656 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002657 } else
2658 val = snd_hda_get_default_vref(codec, nid);
2659 }
2660 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002661 update_hp_automute_hook(codec);
2662
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002663 return 1;
2664}
2665
2666static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2667 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2668 .info = hp_mic_jack_mode_info,
2669 .get = hp_mic_jack_mode_get,
2670 .put = hp_mic_jack_mode_put,
2671};
2672
2673static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2674{
2675 struct hda_gen_spec *spec = codec->spec;
2676 struct snd_kcontrol_new *knew;
2677
2678 if (get_out_jack_num_items(codec, pin) <= 1 &&
2679 get_in_jack_num_items(codec, pin) <= 1)
2680 return 0; /* no need */
2681 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2682 &hp_mic_jack_mode_enum);
2683 if (!knew)
2684 return -ENOMEM;
2685 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002686 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002687 return 0;
2688}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002689
2690/*
2691 * Parse input paths
2692 */
2693
Takashi Iwai352f7f92012-12-19 12:52:06 +01002694/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002695static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002696{
2697 struct hda_amp_list *list;
2698
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002699 list = snd_array_new(&spec->loopback_list);
2700 if (!list)
2701 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002702 list->nid = mix;
2703 list->dir = HDA_INPUT;
2704 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002705 spec->loopback.amplist = spec->loopback_list.list;
2706 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002707}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002708
Takashi Iwai352f7f92012-12-19 12:52:06 +01002709/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002710static int new_analog_input(struct hda_codec *codec, int input_idx,
2711 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002712 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002714 struct hda_gen_spec *spec = codec->spec;
2715 struct nid_path *path;
2716 unsigned int val;
2717 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Takashi Iwai352f7f92012-12-19 12:52:06 +01002719 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2720 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2721 return 0; /* no need for analog loopback */
2722
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002723 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002724 if (!path)
2725 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002726 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002727 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002728
2729 idx = path->idx[path->depth - 1];
2730 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2731 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2732 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002733 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002735 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 }
2737
Takashi Iwai352f7f92012-12-19 12:52:06 +01002738 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2739 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2740 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002741 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002743 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 }
2745
Takashi Iwai352f7f92012-12-19 12:52:06 +01002746 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002747 err = add_loopback_list(spec, mix_nid, idx);
2748 if (err < 0)
2749 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002750
2751 if (spec->mixer_nid != spec->mixer_merge_nid &&
2752 !spec->loopback_merge_path) {
2753 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2754 spec->mixer_merge_nid, 0);
2755 if (path) {
2756 print_nid_path("loopback-merge", path);
2757 path->active = true;
2758 spec->loopback_merge_path =
2759 snd_hda_get_path_idx(codec, path);
2760 }
2761 }
2762
Takashi Iwai352f7f92012-12-19 12:52:06 +01002763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
Takashi Iwai352f7f92012-12-19 12:52:06 +01002766static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002768 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2769 return (pincap & AC_PINCAP_IN) != 0;
2770}
2771
2772/* Parse the codec tree and retrieve ADCs */
2773static int fill_adc_nids(struct hda_codec *codec)
2774{
2775 struct hda_gen_spec *spec = codec->spec;
2776 hda_nid_t nid;
2777 hda_nid_t *adc_nids = spec->adc_nids;
2778 int max_nums = ARRAY_SIZE(spec->adc_nids);
2779 int i, nums = 0;
2780
2781 nid = codec->start_nid;
2782 for (i = 0; i < codec->num_nodes; i++, nid++) {
2783 unsigned int caps = get_wcaps(codec, nid);
2784 int type = get_wcaps_type(caps);
2785
2786 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2787 continue;
2788 adc_nids[nums] = nid;
2789 if (++nums >= max_nums)
2790 break;
2791 }
2792 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002793
2794 /* copy the detected ADCs to all_adcs[] */
2795 spec->num_all_adcs = nums;
2796 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2797
Takashi Iwai352f7f92012-12-19 12:52:06 +01002798 return nums;
2799}
2800
2801/* filter out invalid adc_nids that don't give all active input pins;
2802 * if needed, check whether dynamic ADC-switching is available
2803 */
2804static int check_dyn_adc_switch(struct hda_codec *codec)
2805{
2806 struct hda_gen_spec *spec = codec->spec;
2807 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002808 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002809 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002810
Takashi Iwai352f7f92012-12-19 12:52:06 +01002811 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002812 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002813 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002814 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002815 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002816 break;
2817 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002818 if (i >= imux->num_items) {
2819 ok_bits |= (1 << n);
2820 nums++;
2821 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002822 }
2823
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002824 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002825 /* check whether ADC-switch is possible */
2826 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002827 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002828 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002829 spec->dyn_adc_idx[i] = n;
2830 break;
2831 }
2832 }
2833 }
2834
2835 snd_printdd("hda-codec: enabling ADC switching\n");
2836 spec->dyn_adc_switch = 1;
2837 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002838 /* shrink the invalid adcs and input paths */
2839 nums = 0;
2840 for (n = 0; n < spec->num_adc_nids; n++) {
2841 if (!(ok_bits & (1 << n)))
2842 continue;
2843 if (n != nums) {
2844 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002845 for (i = 0; i < imux->num_items; i++) {
2846 invalidate_nid_path(codec,
2847 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002848 spec->input_paths[i][nums] =
2849 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002850 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002851 }
2852 nums++;
2853 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002854 spec->num_adc_nids = nums;
2855 }
2856
Takashi Iwai967303d2013-02-19 17:12:42 +01002857 if (imux->num_items == 1 ||
2858 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002859 snd_printdd("hda-codec: reducing to a single ADC\n");
2860 spec->num_adc_nids = 1; /* reduce to a single ADC */
2861 }
2862
2863 /* single index for individual volumes ctls */
2864 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2865 spec->num_adc_nids = 1;
2866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 return 0;
2868}
2869
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002870/* parse capture source paths from the given pin and create imux items */
2871static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002872 int cfg_idx, int num_adcs,
2873 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002874{
2875 struct hda_gen_spec *spec = codec->spec;
2876 struct hda_input_mux *imux = &spec->input_mux;
2877 int imux_idx = imux->num_items;
2878 bool imux_added = false;
2879 int c;
2880
2881 for (c = 0; c < num_adcs; c++) {
2882 struct nid_path *path;
2883 hda_nid_t adc = spec->adc_nids[c];
2884
2885 if (!is_reachable_path(codec, pin, adc))
2886 continue;
2887 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2888 if (!path)
2889 continue;
2890 print_nid_path("input", path);
2891 spec->input_paths[imux_idx][c] =
2892 snd_hda_get_path_idx(codec, path);
2893
2894 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01002895 if (spec->hp_mic_pin == pin)
2896 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002897 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002898 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002899 imux_added = true;
2900 }
2901 }
2902
2903 return 0;
2904}
2905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002907 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002909
Takashi Iwaic9700422013-01-18 10:17:30 +01002910/* fill the label for each input at first */
2911static int fill_input_pin_labels(struct hda_codec *codec)
2912{
2913 struct hda_gen_spec *spec = codec->spec;
2914 const struct auto_pin_cfg *cfg = &spec->autocfg;
2915 int i;
2916
2917 for (i = 0; i < cfg->num_inputs; i++) {
2918 hda_nid_t pin = cfg->inputs[i].pin;
2919 const char *label;
2920 int j, idx;
2921
2922 if (!is_input_pin(codec, pin))
2923 continue;
2924
2925 label = hda_get_autocfg_input_label(codec, cfg, i);
2926 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002927 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002928 if (spec->input_labels[j] &&
2929 !strcmp(spec->input_labels[j], label)) {
2930 idx = spec->input_label_idxs[j] + 1;
2931 break;
2932 }
2933 }
2934
2935 spec->input_labels[i] = label;
2936 spec->input_label_idxs[i] = idx;
2937 }
2938
2939 return 0;
2940}
2941
Takashi Iwai9dba2052013-01-18 10:01:15 +01002942#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2943
Takashi Iwai352f7f92012-12-19 12:52:06 +01002944static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002945{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002946 struct hda_gen_spec *spec = codec->spec;
2947 const struct auto_pin_cfg *cfg = &spec->autocfg;
2948 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002949 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002950 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002951 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002952
Takashi Iwai352f7f92012-12-19 12:52:06 +01002953 num_adcs = fill_adc_nids(codec);
2954 if (num_adcs < 0)
2955 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002956
Takashi Iwaic9700422013-01-18 10:17:30 +01002957 err = fill_input_pin_labels(codec);
2958 if (err < 0)
2959 return err;
2960
Takashi Iwai352f7f92012-12-19 12:52:06 +01002961 for (i = 0; i < cfg->num_inputs; i++) {
2962 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Takashi Iwai352f7f92012-12-19 12:52:06 +01002964 pin = cfg->inputs[i].pin;
2965 if (!is_input_pin(codec, pin))
2966 continue;
2967
Takashi Iwai2c12c302013-01-10 09:33:29 +01002968 val = PIN_IN;
2969 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2970 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01002971 if (pin != spec->hp_mic_pin)
2972 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002973
Takashi Iwai352f7f92012-12-19 12:52:06 +01002974 if (mixer) {
2975 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002976 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002977 spec->input_labels[i],
2978 spec->input_label_idxs[i],
2979 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002980 if (err < 0)
2981 return err;
2982 }
2983 }
2984
Takashi Iwaic9700422013-01-18 10:17:30 +01002985 err = parse_capture_source(codec, pin, i, num_adcs,
2986 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002987 if (err < 0)
2988 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002989
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002990 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01002991 err = create_in_jack_mode(codec, pin);
2992 if (err < 0)
2993 return err;
2994 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002995 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002996
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002997 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002998 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002999 "Stereo Mix", 0);
3000 if (err < 0)
3001 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003002 }
3003
3004 return 0;
3005}
3006
3007
3008/*
3009 * input source mux
3010 */
3011
Takashi Iwaic697b712013-01-07 17:09:26 +01003012/* get the input path specified by the given adc and imux indices */
3013static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003014{
3015 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003016 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3017 snd_BUG();
3018 return NULL;
3019 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003020 if (spec->dyn_adc_switch)
3021 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003022 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003023 snd_BUG();
3024 return NULL;
3025 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003026 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003027}
3028
3029static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3030 unsigned int idx);
3031
3032static int mux_enum_info(struct snd_kcontrol *kcontrol,
3033 struct snd_ctl_elem_info *uinfo)
3034{
3035 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3036 struct hda_gen_spec *spec = codec->spec;
3037 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3038}
3039
3040static int mux_enum_get(struct snd_kcontrol *kcontrol,
3041 struct snd_ctl_elem_value *ucontrol)
3042{
3043 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3044 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003045 /* the ctls are created at once with multiple counts */
3046 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003047
3048 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3049 return 0;
3050}
3051
3052static int mux_enum_put(struct snd_kcontrol *kcontrol,
3053 struct snd_ctl_elem_value *ucontrol)
3054{
3055 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003056 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003057 return mux_select(codec, adc_idx,
3058 ucontrol->value.enumerated.item[0]);
3059}
3060
Takashi Iwai352f7f92012-12-19 12:52:06 +01003061static const struct snd_kcontrol_new cap_src_temp = {
3062 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3063 .name = "Input Source",
3064 .info = mux_enum_info,
3065 .get = mux_enum_get,
3066 .put = mux_enum_put,
3067};
3068
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003069/*
3070 * capture volume and capture switch ctls
3071 */
3072
Takashi Iwai352f7f92012-12-19 12:52:06 +01003073typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3074 struct snd_ctl_elem_value *ucontrol);
3075
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003076/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003077static int cap_put_caller(struct snd_kcontrol *kcontrol,
3078 struct snd_ctl_elem_value *ucontrol,
3079 put_call_t func, int type)
3080{
3081 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3082 struct hda_gen_spec *spec = codec->spec;
3083 const struct hda_input_mux *imux;
3084 struct nid_path *path;
3085 int i, adc_idx, err = 0;
3086
3087 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003088 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003089 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003090 /* we use the cache-only update at first since multiple input paths
3091 * may shared the same amp; by updating only caches, the redundant
3092 * writes to hardware can be reduced.
3093 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003094 codec->cached_write = 1;
3095 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003096 path = get_input_path(codec, adc_idx, i);
3097 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003098 continue;
3099 kcontrol->private_value = path->ctls[type];
3100 err = func(kcontrol, ucontrol);
3101 if (err < 0)
3102 goto error;
3103 }
3104 error:
3105 codec->cached_write = 0;
3106 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003107 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003108 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003109 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003110 return err;
3111}
3112
3113/* capture volume ctl callbacks */
3114#define cap_vol_info snd_hda_mixer_amp_volume_info
3115#define cap_vol_get snd_hda_mixer_amp_volume_get
3116#define cap_vol_tlv snd_hda_mixer_amp_tlv
3117
3118static int cap_vol_put(struct snd_kcontrol *kcontrol,
3119 struct snd_ctl_elem_value *ucontrol)
3120{
3121 return cap_put_caller(kcontrol, ucontrol,
3122 snd_hda_mixer_amp_volume_put,
3123 NID_PATH_VOL_CTL);
3124}
3125
3126static const struct snd_kcontrol_new cap_vol_temp = {
3127 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3128 .name = "Capture Volume",
3129 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3130 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3131 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3132 .info = cap_vol_info,
3133 .get = cap_vol_get,
3134 .put = cap_vol_put,
3135 .tlv = { .c = cap_vol_tlv },
3136};
3137
3138/* capture switch ctl callbacks */
3139#define cap_sw_info snd_ctl_boolean_stereo_info
3140#define cap_sw_get snd_hda_mixer_amp_switch_get
3141
3142static int cap_sw_put(struct snd_kcontrol *kcontrol,
3143 struct snd_ctl_elem_value *ucontrol)
3144{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003145 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003146 snd_hda_mixer_amp_switch_put,
3147 NID_PATH_MUTE_CTL);
3148}
3149
3150static const struct snd_kcontrol_new cap_sw_temp = {
3151 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3152 .name = "Capture Switch",
3153 .info = cap_sw_info,
3154 .get = cap_sw_get,
3155 .put = cap_sw_put,
3156};
3157
3158static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3159{
3160 hda_nid_t nid;
3161 int i, depth;
3162
3163 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3164 for (depth = 0; depth < 3; depth++) {
3165 if (depth >= path->depth)
3166 return -EINVAL;
3167 i = path->depth - depth - 1;
3168 nid = path->path[i];
3169 if (!path->ctls[NID_PATH_VOL_CTL]) {
3170 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3171 path->ctls[NID_PATH_VOL_CTL] =
3172 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3173 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3174 int idx = path->idx[i];
3175 if (!depth && codec->single_adc_amp)
3176 idx = 0;
3177 path->ctls[NID_PATH_VOL_CTL] =
3178 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3179 }
3180 }
3181 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3182 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3183 path->ctls[NID_PATH_MUTE_CTL] =
3184 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3185 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3186 int idx = path->idx[i];
3187 if (!depth && codec->single_adc_amp)
3188 idx = 0;
3189 path->ctls[NID_PATH_MUTE_CTL] =
3190 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3191 }
3192 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 return 0;
3195}
3196
Takashi Iwai352f7f92012-12-19 12:52:06 +01003197static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003199 struct hda_gen_spec *spec = codec->spec;
3200 struct auto_pin_cfg *cfg = &spec->autocfg;
3201 unsigned int val;
3202 int i;
3203
3204 if (!spec->inv_dmic_split)
3205 return false;
3206 for (i = 0; i < cfg->num_inputs; i++) {
3207 if (cfg->inputs[i].pin != nid)
3208 continue;
3209 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3210 return false;
3211 val = snd_hda_codec_get_pincfg(codec, nid);
3212 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3213 }
3214 return false;
3215}
3216
Takashi Iwaia90229e2013-01-18 14:10:00 +01003217/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003218static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3219 struct snd_ctl_elem_value *ucontrol)
3220{
3221 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3222 struct hda_gen_spec *spec = codec->spec;
3223 int ret;
3224
3225 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3226 if (ret < 0)
3227 return ret;
3228
Takashi Iwaia90229e2013-01-18 14:10:00 +01003229 if (spec->cap_sync_hook)
3230 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003231
3232 return ret;
3233}
3234
Takashi Iwai352f7f92012-12-19 12:52:06 +01003235static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3236 int idx, bool is_switch, unsigned int ctl,
3237 bool inv_dmic)
3238{
3239 struct hda_gen_spec *spec = codec->spec;
3240 char tmpname[44];
3241 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3242 const char *sfx = is_switch ? "Switch" : "Volume";
3243 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003244 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003245
3246 if (!ctl)
3247 return 0;
3248
3249 if (label)
3250 snprintf(tmpname, sizeof(tmpname),
3251 "%s Capture %s", label, sfx);
3252 else
3253 snprintf(tmpname, sizeof(tmpname),
3254 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003255 knew = add_control(spec, type, tmpname, idx,
3256 amp_val_replace_channels(ctl, chs));
3257 if (!knew)
3258 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003259 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003260 knew->put = cap_single_sw_put;
3261 if (!inv_dmic)
3262 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003263
3264 /* Make independent right kcontrol */
3265 if (label)
3266 snprintf(tmpname, sizeof(tmpname),
3267 "Inverted %s Capture %s", label, sfx);
3268 else
3269 snprintf(tmpname, sizeof(tmpname),
3270 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003271 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003272 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003273 if (!knew)
3274 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003275 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003276 knew->put = cap_single_sw_put;
3277 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003278}
3279
3280/* create single (and simple) capture volume and switch controls */
3281static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3282 unsigned int vol_ctl, unsigned int sw_ctl,
3283 bool inv_dmic)
3284{
3285 int err;
3286 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3287 if (err < 0)
3288 return err;
3289 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3290 if (err < 0)
3291 return err;
3292 return 0;
3293}
3294
3295/* create bound capture volume and switch controls */
3296static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3297 unsigned int vol_ctl, unsigned int sw_ctl)
3298{
3299 struct hda_gen_spec *spec = codec->spec;
3300 struct snd_kcontrol_new *knew;
3301
3302 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003303 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003304 if (!knew)
3305 return -ENOMEM;
3306 knew->index = idx;
3307 knew->private_value = vol_ctl;
3308 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3309 }
3310 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003311 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003312 if (!knew)
3313 return -ENOMEM;
3314 knew->index = idx;
3315 knew->private_value = sw_ctl;
3316 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3317 }
3318 return 0;
3319}
3320
3321/* return the vol ctl when used first in the imux list */
3322static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3323{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003324 struct nid_path *path;
3325 unsigned int ctl;
3326 int i;
3327
Takashi Iwaic697b712013-01-07 17:09:26 +01003328 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003329 if (!path)
3330 return 0;
3331 ctl = path->ctls[type];
3332 if (!ctl)
3333 return 0;
3334 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003335 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003336 if (path && path->ctls[type] == ctl)
3337 return 0;
3338 }
3339 return ctl;
3340}
3341
3342/* create individual capture volume and switch controls per input */
3343static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3344{
3345 struct hda_gen_spec *spec = codec->spec;
3346 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003347 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003348
3349 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003350 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003351 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003352
Takashi Iwaic9700422013-01-18 10:17:30 +01003353 idx = imux->items[i].index;
3354 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003355 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003356 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3357
3358 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003359 err = add_single_cap_ctl(codec,
3360 spec->input_labels[idx],
3361 spec->input_label_idxs[idx],
3362 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003363 get_first_cap_ctl(codec, i, type),
3364 inv_dmic);
3365 if (err < 0)
3366 return err;
3367 }
3368 }
3369 return 0;
3370}
3371
3372static int create_capture_mixers(struct hda_codec *codec)
3373{
3374 struct hda_gen_spec *spec = codec->spec;
3375 struct hda_input_mux *imux = &spec->input_mux;
3376 int i, n, nums, err;
3377
3378 if (spec->dyn_adc_switch)
3379 nums = 1;
3380 else
3381 nums = spec->num_adc_nids;
3382
3383 if (!spec->auto_mic && imux->num_items > 1) {
3384 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003385 const char *name;
3386 name = nums > 1 ? "Input Source" : "Capture Source";
3387 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003388 if (!knew)
3389 return -ENOMEM;
3390 knew->count = nums;
3391 }
3392
3393 for (n = 0; n < nums; n++) {
3394 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003395 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003396 bool inv_dmic = false;
3397 int vol, sw;
3398
3399 vol = sw = 0;
3400 for (i = 0; i < imux->num_items; i++) {
3401 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003402 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003403 if (!path)
3404 continue;
3405 parse_capvol_in_path(codec, path);
3406 if (!vol)
3407 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003408 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003409 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003410 if (!same_amp_caps(codec, vol,
3411 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3412 multi_cap_vol = true;
3413 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003414 if (!sw)
3415 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003416 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003417 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003418 if (!same_amp_caps(codec, sw,
3419 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3420 multi_cap_vol = true;
3421 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003422 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3423 inv_dmic = true;
3424 }
3425
3426 if (!multi)
3427 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3428 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003429 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003430 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3431 else
3432 err = create_multi_cap_vol_ctl(codec);
3433 if (err < 0)
3434 return err;
3435 }
3436
3437 return 0;
3438}
3439
3440/*
3441 * add mic boosts if needed
3442 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003443
3444/* check whether the given amp is feasible as a boost volume */
3445static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3446 int dir, int idx)
3447{
3448 unsigned int step;
3449
3450 if (!nid_has_volume(codec, nid, dir) ||
3451 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3452 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3453 return false;
3454
3455 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3456 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3457 if (step < 0x20)
3458 return false;
3459 return true;
3460}
3461
3462/* look for a boost amp in a widget close to the pin */
3463static unsigned int look_for_boost_amp(struct hda_codec *codec,
3464 struct nid_path *path)
3465{
3466 unsigned int val = 0;
3467 hda_nid_t nid;
3468 int depth;
3469
3470 for (depth = 0; depth < 3; depth++) {
3471 if (depth >= path->depth - 1)
3472 break;
3473 nid = path->path[depth];
3474 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3475 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3476 break;
3477 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3478 path->idx[depth])) {
3479 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3480 HDA_INPUT);
3481 break;
3482 }
3483 }
3484
3485 return val;
3486}
3487
Takashi Iwai352f7f92012-12-19 12:52:06 +01003488static int parse_mic_boost(struct hda_codec *codec)
3489{
3490 struct hda_gen_spec *spec = codec->spec;
3491 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003492 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003493 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003494
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003495 if (!spec->num_adc_nids)
3496 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003497
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003498 for (i = 0; i < imux->num_items; i++) {
3499 struct nid_path *path;
3500 unsigned int val;
3501 int idx;
3502 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003503
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003504 idx = imux->items[i].index;
3505 if (idx >= imux->num_items)
3506 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003507
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003508 /* check only line-in and mic pins */
Takashi Iwai1799cdd2013-01-18 14:37:16 +01003509 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003510 continue;
3511
3512 path = get_input_path(codec, 0, i);
3513 if (!path)
3514 continue;
3515
3516 val = look_for_boost_amp(codec, path);
3517 if (!val)
3518 continue;
3519
3520 /* create a boost control */
3521 snprintf(boost_label, sizeof(boost_label),
3522 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003523 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3524 spec->input_label_idxs[idx], val))
3525 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003526
3527 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003528 }
3529 return 0;
3530}
3531
3532/*
3533 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3534 */
3535static void parse_digital(struct hda_codec *codec)
3536{
3537 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003538 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003539 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003540 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003541
3542 /* support multiple SPDIFs; the secondary is set up as a slave */
3543 nums = 0;
3544 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003545 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003546 dig_nid = look_for_dac(codec, pin, true);
3547 if (!dig_nid)
3548 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003549 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003550 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003551 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003552 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003553 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003554 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003555 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003556 if (!nums) {
3557 spec->multiout.dig_out_nid = dig_nid;
3558 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3559 } else {
3560 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3561 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3562 break;
3563 spec->slave_dig_outs[nums - 1] = dig_nid;
3564 }
3565 nums++;
3566 }
3567
3568 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003569 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003570 dig_nid = codec->start_nid;
3571 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003572 unsigned int wcaps = get_wcaps(codec, dig_nid);
3573 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3574 continue;
3575 if (!(wcaps & AC_WCAP_DIGITAL))
3576 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003577 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003578 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003579 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003580 path->active = true;
3581 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003582 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003583 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003584 break;
3585 }
3586 }
3587 }
3588}
3589
3590
3591/*
3592 * input MUX handling
3593 */
3594
3595static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3596
3597/* select the given imux item; either unmute exclusively or select the route */
3598static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3599 unsigned int idx)
3600{
3601 struct hda_gen_spec *spec = codec->spec;
3602 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003603 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003604
3605 imux = &spec->input_mux;
3606 if (!imux->num_items)
3607 return 0;
3608
3609 if (idx >= imux->num_items)
3610 idx = imux->num_items - 1;
3611 if (spec->cur_mux[adc_idx] == idx)
3612 return 0;
3613
Takashi Iwai55196ff2013-01-24 17:32:56 +01003614 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3615 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003616 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003617 if (old_path->active)
3618 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003619
3620 spec->cur_mux[adc_idx] = idx;
3621
Takashi Iwai967303d2013-02-19 17:12:42 +01003622 if (spec->hp_mic)
3623 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003624
3625 if (spec->dyn_adc_switch)
3626 dyn_adc_pcm_resetup(codec, idx);
3627
Takashi Iwaic697b712013-01-07 17:09:26 +01003628 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003629 if (!path)
3630 return 0;
3631 if (path->active)
3632 return 0;
3633 snd_hda_activate_path(codec, path, true, false);
3634 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003635 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003636 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003637 return 1;
3638}
3639
3640
3641/*
3642 * Jack detections for HP auto-mute and mic-switch
3643 */
3644
3645/* check each pin in the given array; returns true if any of them is plugged */
3646static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3647{
3648 int i, present = 0;
3649
3650 for (i = 0; i < num_pins; i++) {
3651 hda_nid_t nid = pins[i];
3652 if (!nid)
3653 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003654 /* don't detect pins retasked as inputs */
3655 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3656 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003657 present |= snd_hda_jack_detect(codec, nid);
3658 }
3659 return present;
3660}
3661
3662/* standard HP/line-out auto-mute helper */
3663static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003664 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003665{
3666 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003667 int i;
3668
3669 for (i = 0; i < num_pins; i++) {
3670 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003671 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003672 if (!nid)
3673 break;
Takashi Iwai967303d2013-02-19 17:12:42 +01003674 oldval = snd_hda_codec_get_pin_target(codec, nid);
3675 if (oldval & PIN_IN)
3676 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003677 /* don't reset VREF value in case it's controlling
3678 * the amp (see alc861_fixup_asus_amp_vref_0f())
3679 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003680 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003681 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003682 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003683 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003684 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003685 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003686 /* here we call update_pin_ctl() so that the pinctl is changed
3687 * without changing the pinctl target value;
3688 * the original target value will be still referred at the
3689 * init / resume again
3690 */
3691 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003692 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003693 }
3694}
3695
3696/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003697void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003698{
3699 struct hda_gen_spec *spec = codec->spec;
3700 int on;
3701
3702 /* Control HP pins/amps depending on master_mute state;
3703 * in general, HP pins/amps control should be enabled in all cases,
3704 * but currently set only for master_mute, just to be safe
3705 */
Takashi Iwai967303d2013-02-19 17:12:42 +01003706 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003707 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003708
3709 if (!spec->automute_speaker)
3710 on = 0;
3711 else
3712 on = spec->hp_jack_present | spec->line_jack_present;
3713 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003714 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003715 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003716 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003717
3718 /* toggle line-out mutes if needed, too */
3719 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3720 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3721 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3722 return;
3723 if (!spec->automute_lo)
3724 on = 0;
3725 else
3726 on = spec->hp_jack_present;
3727 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003728 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003729 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003730 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003731}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003732EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003733
3734static void call_update_outputs(struct hda_codec *codec)
3735{
3736 struct hda_gen_spec *spec = codec->spec;
3737 if (spec->automute_hook)
3738 spec->automute_hook(codec);
3739 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003740 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003741}
3742
3743/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003744void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003745{
3746 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003747 hda_nid_t *pins = spec->autocfg.hp_pins;
3748 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003749
Takashi Iwai92603c52013-01-22 07:46:31 +01003750 /* No detection for the first HP jack during indep-HP mode */
3751 if (spec->indep_hp_enabled) {
3752 pins++;
3753 num_pins--;
3754 }
3755
3756 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003757 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3758 return;
3759 call_update_outputs(codec);
3760}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003761EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003762
3763/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003764void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003765{
3766 struct hda_gen_spec *spec = codec->spec;
3767
3768 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3769 return;
3770 /* check LO jack only when it's different from HP */
3771 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3772 return;
3773
3774 spec->line_jack_present =
3775 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3776 spec->autocfg.line_out_pins);
3777 if (!spec->automute_speaker || !spec->detect_lo)
3778 return;
3779 call_update_outputs(codec);
3780}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003781EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003782
3783/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003784void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003785{
3786 struct hda_gen_spec *spec = codec->spec;
3787 int i;
3788
3789 if (!spec->auto_mic)
3790 return;
3791
3792 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003793 hda_nid_t pin = spec->am_entry[i].pin;
3794 /* don't detect pins retasked as outputs */
3795 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3796 continue;
3797 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003798 mux_select(codec, 0, spec->am_entry[i].idx);
3799 return;
3800 }
3801 }
3802 mux_select(codec, 0, spec->am_entry[0].idx);
3803}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003804EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003805
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003806/* update jack retasking */
3807static void update_automute_all(struct hda_codec *codec)
3808{
3809 struct hda_gen_spec *spec = codec->spec;
3810
Takashi Iwai8ba955c2013-03-07 18:40:58 +01003811 update_hp_automute_hook(codec);
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003812 if (spec->line_automute_hook)
3813 spec->line_automute_hook(codec, NULL);
3814 else
3815 snd_hda_gen_line_automute(codec, NULL);
3816 if (spec->mic_autoswitch_hook)
3817 spec->mic_autoswitch_hook(codec, NULL);
3818 else
3819 snd_hda_gen_mic_autoswitch(codec, NULL);
3820}
3821
Takashi Iwai352f7f92012-12-19 12:52:06 +01003822/*
3823 * Auto-Mute mode mixer enum support
3824 */
3825static int automute_mode_info(struct snd_kcontrol *kcontrol,
3826 struct snd_ctl_elem_info *uinfo)
3827{
3828 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3829 struct hda_gen_spec *spec = codec->spec;
3830 static const char * const texts3[] = {
3831 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003832 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
Takashi Iwai352f7f92012-12-19 12:52:06 +01003834 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3835 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3836 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3837}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
Takashi Iwai352f7f92012-12-19 12:52:06 +01003839static int automute_mode_get(struct snd_kcontrol *kcontrol,
3840 struct snd_ctl_elem_value *ucontrol)
3841{
3842 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3843 struct hda_gen_spec *spec = codec->spec;
3844 unsigned int val = 0;
3845 if (spec->automute_speaker)
3846 val++;
3847 if (spec->automute_lo)
3848 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003849
Takashi Iwai352f7f92012-12-19 12:52:06 +01003850 ucontrol->value.enumerated.item[0] = val;
3851 return 0;
3852}
3853
3854static int automute_mode_put(struct snd_kcontrol *kcontrol,
3855 struct snd_ctl_elem_value *ucontrol)
3856{
3857 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3858 struct hda_gen_spec *spec = codec->spec;
3859
3860 switch (ucontrol->value.enumerated.item[0]) {
3861 case 0:
3862 if (!spec->automute_speaker && !spec->automute_lo)
3863 return 0;
3864 spec->automute_speaker = 0;
3865 spec->automute_lo = 0;
3866 break;
3867 case 1:
3868 if (spec->automute_speaker_possible) {
3869 if (!spec->automute_lo && spec->automute_speaker)
3870 return 0;
3871 spec->automute_speaker = 1;
3872 spec->automute_lo = 0;
3873 } else if (spec->automute_lo_possible) {
3874 if (spec->automute_lo)
3875 return 0;
3876 spec->automute_lo = 1;
3877 } else
3878 return -EINVAL;
3879 break;
3880 case 2:
3881 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3882 return -EINVAL;
3883 if (spec->automute_speaker && spec->automute_lo)
3884 return 0;
3885 spec->automute_speaker = 1;
3886 spec->automute_lo = 1;
3887 break;
3888 default:
3889 return -EINVAL;
3890 }
3891 call_update_outputs(codec);
3892 return 1;
3893}
3894
3895static const struct snd_kcontrol_new automute_mode_enum = {
3896 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3897 .name = "Auto-Mute Mode",
3898 .info = automute_mode_info,
3899 .get = automute_mode_get,
3900 .put = automute_mode_put,
3901};
3902
3903static int add_automute_mode_enum(struct hda_codec *codec)
3904{
3905 struct hda_gen_spec *spec = codec->spec;
3906
Takashi Iwai12c93df2012-12-19 14:38:33 +01003907 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003908 return -ENOMEM;
3909 return 0;
3910}
3911
3912/*
3913 * Check the availability of HP/line-out auto-mute;
3914 * Set up appropriately if really supported
3915 */
3916static int check_auto_mute_availability(struct hda_codec *codec)
3917{
3918 struct hda_gen_spec *spec = codec->spec;
3919 struct auto_pin_cfg *cfg = &spec->autocfg;
3920 int present = 0;
3921 int i, err;
3922
Takashi Iwaif72706b2013-01-16 18:20:07 +01003923 if (spec->suppress_auto_mute)
3924 return 0;
3925
Takashi Iwai352f7f92012-12-19 12:52:06 +01003926 if (cfg->hp_pins[0])
3927 present++;
3928 if (cfg->line_out_pins[0])
3929 present++;
3930 if (cfg->speaker_pins[0])
3931 present++;
3932 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003933 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003934
3935 if (!cfg->speaker_pins[0] &&
3936 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3937 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3938 sizeof(cfg->speaker_pins));
3939 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
Takashi Iwai352f7f92012-12-19 12:52:06 +01003942 if (!cfg->hp_pins[0] &&
3943 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3944 memcpy(cfg->hp_pins, cfg->line_out_pins,
3945 sizeof(cfg->hp_pins));
3946 cfg->hp_outs = cfg->line_outs;
3947 }
3948
3949 for (i = 0; i < cfg->hp_outs; i++) {
3950 hda_nid_t nid = cfg->hp_pins[i];
3951 if (!is_jack_detectable(codec, nid))
3952 continue;
3953 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3954 nid);
3955 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003956 spec->hp_automute_hook ?
3957 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003958 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003959 spec->detect_hp = 1;
3960 }
3961
3962 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3963 if (cfg->speaker_outs)
3964 for (i = 0; i < cfg->line_outs; i++) {
3965 hda_nid_t nid = cfg->line_out_pins[i];
3966 if (!is_jack_detectable(codec, nid))
3967 continue;
3968 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3969 snd_hda_jack_detect_enable_callback(codec, nid,
3970 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003971 spec->line_automute_hook ?
3972 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003973 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003974 spec->detect_lo = 1;
3975 }
3976 spec->automute_lo_possible = spec->detect_hp;
3977 }
3978
3979 spec->automute_speaker_possible = cfg->speaker_outs &&
3980 (spec->detect_hp || spec->detect_lo);
3981
3982 spec->automute_lo = spec->automute_lo_possible;
3983 spec->automute_speaker = spec->automute_speaker_possible;
3984
3985 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3986 /* create a control for automute mode */
3987 err = add_automute_mode_enum(codec);
3988 if (err < 0)
3989 return err;
3990 }
3991 return 0;
3992}
3993
Takashi Iwai352f7f92012-12-19 12:52:06 +01003994/* check whether all auto-mic pins are valid; setup indices if OK */
3995static bool auto_mic_check_imux(struct hda_codec *codec)
3996{
3997 struct hda_gen_spec *spec = codec->spec;
3998 const struct hda_input_mux *imux;
3999 int i;
4000
4001 imux = &spec->input_mux;
4002 for (i = 0; i < spec->am_num_entries; i++) {
4003 spec->am_entry[i].idx =
4004 find_idx_in_nid_list(spec->am_entry[i].pin,
4005 spec->imux_pins, imux->num_items);
4006 if (spec->am_entry[i].idx < 0)
4007 return false; /* no corresponding imux */
4008 }
4009
4010 /* we don't need the jack detection for the first pin */
4011 for (i = 1; i < spec->am_num_entries; i++)
4012 snd_hda_jack_detect_enable_callback(codec,
4013 spec->am_entry[i].pin,
4014 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004015 spec->mic_autoswitch_hook ?
4016 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004017 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004018 return true;
4019}
4020
4021static int compare_attr(const void *ap, const void *bp)
4022{
4023 const struct automic_entry *a = ap;
4024 const struct automic_entry *b = bp;
4025 return (int)(a->attr - b->attr);
4026}
4027
4028/*
4029 * Check the availability of auto-mic switch;
4030 * Set up if really supported
4031 */
4032static int check_auto_mic_availability(struct hda_codec *codec)
4033{
4034 struct hda_gen_spec *spec = codec->spec;
4035 struct auto_pin_cfg *cfg = &spec->autocfg;
4036 unsigned int types;
4037 int i, num_pins;
4038
Takashi Iwaid12daf62013-01-07 16:32:11 +01004039 if (spec->suppress_auto_mic)
4040 return 0;
4041
Takashi Iwai352f7f92012-12-19 12:52:06 +01004042 types = 0;
4043 num_pins = 0;
4044 for (i = 0; i < cfg->num_inputs; i++) {
4045 hda_nid_t nid = cfg->inputs[i].pin;
4046 unsigned int attr;
4047 attr = snd_hda_codec_get_pincfg(codec, nid);
4048 attr = snd_hda_get_input_pin_attr(attr);
4049 if (types & (1 << attr))
4050 return 0; /* already occupied */
4051 switch (attr) {
4052 case INPUT_PIN_ATTR_INT:
4053 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4054 return 0; /* invalid type */
4055 break;
4056 case INPUT_PIN_ATTR_UNUSED:
4057 return 0; /* invalid entry */
4058 default:
4059 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4060 return 0; /* invalid type */
4061 if (!spec->line_in_auto_switch &&
4062 cfg->inputs[i].type != AUTO_PIN_MIC)
4063 return 0; /* only mic is allowed */
4064 if (!is_jack_detectable(codec, nid))
4065 return 0; /* no unsol support */
4066 break;
4067 }
4068 if (num_pins >= MAX_AUTO_MIC_PINS)
4069 return 0;
4070 types |= (1 << attr);
4071 spec->am_entry[num_pins].pin = nid;
4072 spec->am_entry[num_pins].attr = attr;
4073 num_pins++;
4074 }
4075
4076 if (num_pins < 2)
4077 return 0;
4078
4079 spec->am_num_entries = num_pins;
4080 /* sort the am_entry in the order of attr so that the pin with a
4081 * higher attr will be selected when the jack is plugged.
4082 */
4083 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4084 compare_attr, NULL);
4085
4086 if (!auto_mic_check_imux(codec))
4087 return 0;
4088
4089 spec->auto_mic = 1;
4090 spec->num_adc_nids = 1;
4091 spec->cur_mux[0] = spec->am_entry[0].idx;
4092 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4093 spec->am_entry[0].pin,
4094 spec->am_entry[1].pin,
4095 spec->am_entry[2].pin);
4096
4097 return 0;
4098}
4099
Takashi Iwai55196ff2013-01-24 17:32:56 +01004100/* power_filter hook; make inactive widgets into power down */
4101static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4102 hda_nid_t nid,
4103 unsigned int power_state)
4104{
4105 if (power_state != AC_PWRST_D0)
4106 return power_state;
4107 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4108 return power_state;
4109 if (is_active_nid(codec, nid, HDA_OUTPUT, 0))
4110 return power_state;
4111 return AC_PWRST_D3;
4112}
4113
Takashi Iwai352f7f92012-12-19 12:52:06 +01004114
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004115/*
4116 * Parse the given BIOS configuration and set up the hda_gen_spec
4117 *
4118 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004119 * or a negative error code
4120 */
4121int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004122 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004123{
4124 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004125 int err;
4126
Takashi Iwai1c70a582013-01-11 17:48:22 +01004127 parse_user_hints(codec);
4128
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004129 if (spec->mixer_nid && !spec->mixer_merge_nid)
4130 spec->mixer_merge_nid = spec->mixer_nid;
4131
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004132 if (cfg != &spec->autocfg) {
4133 spec->autocfg = *cfg;
4134 cfg = &spec->autocfg;
4135 }
4136
David Henningsson6fc4cb92013-01-16 15:58:45 +01004137 fill_all_dac_nids(codec);
4138
Takashi Iwai352f7f92012-12-19 12:52:06 +01004139 if (!cfg->line_outs) {
4140 if (cfg->dig_outs || cfg->dig_in_pin) {
4141 spec->multiout.max_channels = 2;
4142 spec->no_analog = 1;
4143 goto dig_only;
4144 }
4145 return 0; /* can't find valid BIOS pin config */
4146 }
4147
4148 if (!spec->no_primary_hp &&
4149 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4150 cfg->line_outs <= cfg->hp_outs) {
4151 /* use HP as primary out */
4152 cfg->speaker_outs = cfg->line_outs;
4153 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4154 sizeof(cfg->speaker_pins));
4155 cfg->line_outs = cfg->hp_outs;
4156 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4157 cfg->hp_outs = 0;
4158 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4159 cfg->line_out_type = AUTO_PIN_HP_OUT;
4160 }
4161
4162 err = parse_output_paths(codec);
4163 if (err < 0)
4164 return err;
4165 err = create_multi_channel_mode(codec);
4166 if (err < 0)
4167 return err;
4168 err = create_multi_out_ctls(codec, cfg);
4169 if (err < 0)
4170 return err;
4171 err = create_hp_out_ctls(codec);
4172 if (err < 0)
4173 return err;
4174 err = create_speaker_out_ctls(codec);
4175 if (err < 0)
4176 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004177 err = create_indep_hp_ctls(codec);
4178 if (err < 0)
4179 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004180 err = create_loopback_mixing_ctl(codec);
4181 if (err < 0)
4182 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004183 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004184 if (err < 0)
4185 return err;
4186 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004187 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004188 return err;
4189
Takashi Iwaia07a9492013-01-07 16:44:06 +01004190 spec->const_channel_count = spec->ext_channel_count;
4191 /* check the multiple speaker and headphone pins */
4192 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4193 spec->const_channel_count = max(spec->const_channel_count,
4194 cfg->speaker_outs * 2);
4195 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4196 spec->const_channel_count = max(spec->const_channel_count,
4197 cfg->hp_outs * 2);
4198 spec->multiout.max_channels = max(spec->ext_channel_count,
4199 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004200
4201 err = check_auto_mute_availability(codec);
4202 if (err < 0)
4203 return err;
4204
4205 err = check_dyn_adc_switch(codec);
4206 if (err < 0)
4207 return err;
4208
Takashi Iwai967303d2013-02-19 17:12:42 +01004209 err = check_auto_mic_availability(codec);
4210 if (err < 0)
4211 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004212
Takashi Iwai352f7f92012-12-19 12:52:06 +01004213 err = create_capture_mixers(codec);
4214 if (err < 0)
4215 return err;
4216
4217 err = parse_mic_boost(codec);
4218 if (err < 0)
4219 return err;
4220
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004221 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004222 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4223 err = create_out_jack_modes(codec, cfg->line_outs,
4224 cfg->line_out_pins);
4225 if (err < 0)
4226 return err;
4227 }
4228 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4229 err = create_out_jack_modes(codec, cfg->hp_outs,
4230 cfg->hp_pins);
4231 if (err < 0)
4232 return err;
4233 }
4234 }
4235
Takashi Iwai352f7f92012-12-19 12:52:06 +01004236 dig_only:
4237 parse_digital(codec);
4238
Takashi Iwai55196ff2013-01-24 17:32:56 +01004239 if (spec->power_down_unused)
4240 codec->power_filter = snd_hda_gen_path_power_filter;
4241
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004242 if (!spec->no_analog && spec->beep_nid) {
4243 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4244 if (err < 0)
4245 return err;
4246 }
4247
Takashi Iwai352f7f92012-12-19 12:52:06 +01004248 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004250EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
4252
4253/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004254 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004256
4257/* slave controls for virtual master */
4258static const char * const slave_pfxs[] = {
4259 "Front", "Surround", "Center", "LFE", "Side",
4260 "Headphone", "Speaker", "Mono", "Line Out",
4261 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004262 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4263 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4264 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004265 NULL,
4266};
4267
4268int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004270 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
Takashi Iwai36502d02012-12-19 15:15:10 +01004273 if (spec->kctls.used) {
4274 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4275 if (err < 0)
4276 return err;
4277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
Takashi Iwai352f7f92012-12-19 12:52:06 +01004279 if (spec->multiout.dig_out_nid) {
4280 err = snd_hda_create_dig_out_ctls(codec,
4281 spec->multiout.dig_out_nid,
4282 spec->multiout.dig_out_nid,
4283 spec->pcm_rec[1].pcm_type);
4284 if (err < 0)
4285 return err;
4286 if (!spec->no_analog) {
4287 err = snd_hda_create_spdif_share_sw(codec,
4288 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 if (err < 0)
4290 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004291 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 }
4293 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004294 if (spec->dig_in_nid) {
4295 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4296 if (err < 0)
4297 return err;
4298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
Takashi Iwai352f7f92012-12-19 12:52:06 +01004300 /* if we have no master control, let's create it */
4301 if (!spec->no_analog &&
4302 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004303 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004304 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004305 "Playback Volume");
4306 if (err < 0)
4307 return err;
4308 }
4309 if (!spec->no_analog &&
4310 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4311 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4312 NULL, slave_pfxs,
4313 "Playback Switch",
4314 true, &spec->vmaster_mute.sw_kctl);
4315 if (err < 0)
4316 return err;
4317 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004318 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4319 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
Takashi Iwai352f7f92012-12-19 12:52:06 +01004322 free_kctls(spec); /* no longer needed */
4323
Takashi Iwai967303d2013-02-19 17:12:42 +01004324 if (spec->hp_mic_pin) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004325 int err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004326 int nid = spec->hp_mic_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004327 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4328 if (err < 0)
4329 return err;
4330 err = snd_hda_jack_detect_enable(codec, nid, 0);
4331 if (err < 0)
4332 return err;
4333 }
4334
4335 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4336 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 return err;
4338
4339 return 0;
4340}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004341EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
4344/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004345 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004348static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4349 struct hda_codec *codec,
4350 struct snd_pcm_substream *substream,
4351 int action)
4352{
4353 struct hda_gen_spec *spec = codec->spec;
4354 if (spec->pcm_playback_hook)
4355 spec->pcm_playback_hook(hinfo, codec, substream, action);
4356}
4357
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004358static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4359 struct hda_codec *codec,
4360 struct snd_pcm_substream *substream,
4361 int action)
4362{
4363 struct hda_gen_spec *spec = codec->spec;
4364 if (spec->pcm_capture_hook)
4365 spec->pcm_capture_hook(hinfo, codec, substream, action);
4366}
4367
Takashi Iwai352f7f92012-12-19 12:52:06 +01004368/*
4369 * Analog playback callbacks
4370 */
4371static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4372 struct hda_codec *codec,
4373 struct snd_pcm_substream *substream)
4374{
4375 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004376 int err;
4377
4378 mutex_lock(&spec->pcm_mutex);
4379 err = snd_hda_multi_out_analog_open(codec,
4380 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004381 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004382 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004383 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004384 call_pcm_playback_hook(hinfo, codec, substream,
4385 HDA_GEN_PCM_ACT_OPEN);
4386 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004387 mutex_unlock(&spec->pcm_mutex);
4388 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004389}
4390
4391static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004392 struct hda_codec *codec,
4393 unsigned int stream_tag,
4394 unsigned int format,
4395 struct snd_pcm_substream *substream)
4396{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004397 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004398 int err;
4399
4400 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4401 stream_tag, format, substream);
4402 if (!err)
4403 call_pcm_playback_hook(hinfo, codec, substream,
4404 HDA_GEN_PCM_ACT_PREPARE);
4405 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004406}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004407
Takashi Iwai352f7f92012-12-19 12:52:06 +01004408static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4409 struct hda_codec *codec,
4410 struct snd_pcm_substream *substream)
4411{
4412 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004413 int err;
4414
4415 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4416 if (!err)
4417 call_pcm_playback_hook(hinfo, codec, substream,
4418 HDA_GEN_PCM_ACT_CLEANUP);
4419 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004420}
4421
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004422static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4423 struct hda_codec *codec,
4424 struct snd_pcm_substream *substream)
4425{
4426 struct hda_gen_spec *spec = codec->spec;
4427 mutex_lock(&spec->pcm_mutex);
4428 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004429 call_pcm_playback_hook(hinfo, codec, substream,
4430 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004431 mutex_unlock(&spec->pcm_mutex);
4432 return 0;
4433}
4434
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004435static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4436 struct hda_codec *codec,
4437 struct snd_pcm_substream *substream)
4438{
4439 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4440 return 0;
4441}
4442
4443static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4444 struct hda_codec *codec,
4445 unsigned int stream_tag,
4446 unsigned int format,
4447 struct snd_pcm_substream *substream)
4448{
4449 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4450 call_pcm_capture_hook(hinfo, codec, substream,
4451 HDA_GEN_PCM_ACT_PREPARE);
4452 return 0;
4453}
4454
4455static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4456 struct hda_codec *codec,
4457 struct snd_pcm_substream *substream)
4458{
4459 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4460 call_pcm_capture_hook(hinfo, codec, substream,
4461 HDA_GEN_PCM_ACT_CLEANUP);
4462 return 0;
4463}
4464
4465static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4466 struct hda_codec *codec,
4467 struct snd_pcm_substream *substream)
4468{
4469 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4470 return 0;
4471}
4472
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004473static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4474 struct hda_codec *codec,
4475 struct snd_pcm_substream *substream)
4476{
4477 struct hda_gen_spec *spec = codec->spec;
4478 int err = 0;
4479
4480 mutex_lock(&spec->pcm_mutex);
4481 if (!spec->indep_hp_enabled)
4482 err = -EBUSY;
4483 else
4484 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004485 call_pcm_playback_hook(hinfo, codec, substream,
4486 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004487 mutex_unlock(&spec->pcm_mutex);
4488 return err;
4489}
4490
4491static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4492 struct hda_codec *codec,
4493 struct snd_pcm_substream *substream)
4494{
4495 struct hda_gen_spec *spec = codec->spec;
4496 mutex_lock(&spec->pcm_mutex);
4497 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004498 call_pcm_playback_hook(hinfo, codec, substream,
4499 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004500 mutex_unlock(&spec->pcm_mutex);
4501 return 0;
4502}
4503
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004504static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4505 struct hda_codec *codec,
4506 unsigned int stream_tag,
4507 unsigned int format,
4508 struct snd_pcm_substream *substream)
4509{
4510 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4511 call_pcm_playback_hook(hinfo, codec, substream,
4512 HDA_GEN_PCM_ACT_PREPARE);
4513 return 0;
4514}
4515
4516static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4517 struct hda_codec *codec,
4518 struct snd_pcm_substream *substream)
4519{
4520 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4521 call_pcm_playback_hook(hinfo, codec, substream,
4522 HDA_GEN_PCM_ACT_CLEANUP);
4523 return 0;
4524}
4525
Takashi Iwai352f7f92012-12-19 12:52:06 +01004526/*
4527 * Digital out
4528 */
4529static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4530 struct hda_codec *codec,
4531 struct snd_pcm_substream *substream)
4532{
4533 struct hda_gen_spec *spec = codec->spec;
4534 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4535}
4536
4537static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4538 struct hda_codec *codec,
4539 unsigned int stream_tag,
4540 unsigned int format,
4541 struct snd_pcm_substream *substream)
4542{
4543 struct hda_gen_spec *spec = codec->spec;
4544 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4545 stream_tag, format, substream);
4546}
4547
4548static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4549 struct hda_codec *codec,
4550 struct snd_pcm_substream *substream)
4551{
4552 struct hda_gen_spec *spec = codec->spec;
4553 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4554}
4555
4556static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4557 struct hda_codec *codec,
4558 struct snd_pcm_substream *substream)
4559{
4560 struct hda_gen_spec *spec = codec->spec;
4561 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4562}
4563
4564/*
4565 * Analog capture
4566 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004567#define alt_capture_pcm_open capture_pcm_open
4568#define alt_capture_pcm_close capture_pcm_close
4569
Takashi Iwai352f7f92012-12-19 12:52:06 +01004570static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4571 struct hda_codec *codec,
4572 unsigned int stream_tag,
4573 unsigned int format,
4574 struct snd_pcm_substream *substream)
4575{
4576 struct hda_gen_spec *spec = codec->spec;
4577
4578 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004579 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004580 call_pcm_capture_hook(hinfo, codec, substream,
4581 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004582 return 0;
4583}
4584
Takashi Iwai352f7f92012-12-19 12:52:06 +01004585static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4586 struct hda_codec *codec,
4587 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004588{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004589 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004590
Takashi Iwai352f7f92012-12-19 12:52:06 +01004591 snd_hda_codec_cleanup_stream(codec,
4592 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004593 call_pcm_capture_hook(hinfo, codec, substream,
4594 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004595 return 0;
4596}
4597
Takashi Iwai352f7f92012-12-19 12:52:06 +01004598/*
4599 */
4600static const struct hda_pcm_stream pcm_analog_playback = {
4601 .substreams = 1,
4602 .channels_min = 2,
4603 .channels_max = 8,
4604 /* NID is set in build_pcms */
4605 .ops = {
4606 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004607 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004608 .prepare = playback_pcm_prepare,
4609 .cleanup = playback_pcm_cleanup
4610 },
4611};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
Takashi Iwai352f7f92012-12-19 12:52:06 +01004613static const struct hda_pcm_stream pcm_analog_capture = {
4614 .substreams = 1,
4615 .channels_min = 2,
4616 .channels_max = 2,
4617 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004618 .ops = {
4619 .open = capture_pcm_open,
4620 .close = capture_pcm_close,
4621 .prepare = capture_pcm_prepare,
4622 .cleanup = capture_pcm_cleanup
4623 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004624};
4625
4626static const struct hda_pcm_stream pcm_analog_alt_playback = {
4627 .substreams = 1,
4628 .channels_min = 2,
4629 .channels_max = 2,
4630 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004631 .ops = {
4632 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004633 .close = alt_playback_pcm_close,
4634 .prepare = alt_playback_pcm_prepare,
4635 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004636 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004637};
4638
4639static const struct hda_pcm_stream pcm_analog_alt_capture = {
4640 .substreams = 2, /* can be overridden */
4641 .channels_min = 2,
4642 .channels_max = 2,
4643 /* NID is set in build_pcms */
4644 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004645 .open = alt_capture_pcm_open,
4646 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004647 .prepare = alt_capture_pcm_prepare,
4648 .cleanup = alt_capture_pcm_cleanup
4649 },
4650};
4651
4652static const struct hda_pcm_stream pcm_digital_playback = {
4653 .substreams = 1,
4654 .channels_min = 2,
4655 .channels_max = 2,
4656 /* NID is set in build_pcms */
4657 .ops = {
4658 .open = dig_playback_pcm_open,
4659 .close = dig_playback_pcm_close,
4660 .prepare = dig_playback_pcm_prepare,
4661 .cleanup = dig_playback_pcm_cleanup
4662 },
4663};
4664
4665static const struct hda_pcm_stream pcm_digital_capture = {
4666 .substreams = 1,
4667 .channels_min = 2,
4668 .channels_max = 2,
4669 /* NID is set in build_pcms */
4670};
4671
4672/* Used by build_pcms to flag that a PCM has no playback stream */
4673static const struct hda_pcm_stream pcm_null_stream = {
4674 .substreams = 0,
4675 .channels_min = 0,
4676 .channels_max = 0,
4677};
4678
4679/*
4680 * dynamic changing ADC PCM streams
4681 */
4682static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4683{
4684 struct hda_gen_spec *spec = codec->spec;
4685 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4686
4687 if (spec->cur_adc && spec->cur_adc != new_adc) {
4688 /* stream is running, let's swap the current ADC */
4689 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4690 spec->cur_adc = new_adc;
4691 snd_hda_codec_setup_stream(codec, new_adc,
4692 spec->cur_adc_stream_tag, 0,
4693 spec->cur_adc_format);
4694 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004696 return false;
4697}
4698
4699/* analog capture with dynamic dual-adc changes */
4700static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4701 struct hda_codec *codec,
4702 unsigned int stream_tag,
4703 unsigned int format,
4704 struct snd_pcm_substream *substream)
4705{
4706 struct hda_gen_spec *spec = codec->spec;
4707 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4708 spec->cur_adc_stream_tag = stream_tag;
4709 spec->cur_adc_format = format;
4710 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4711 return 0;
4712}
4713
4714static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4715 struct hda_codec *codec,
4716 struct snd_pcm_substream *substream)
4717{
4718 struct hda_gen_spec *spec = codec->spec;
4719 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4720 spec->cur_adc = 0;
4721 return 0;
4722}
4723
4724static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4725 .substreams = 1,
4726 .channels_min = 2,
4727 .channels_max = 2,
4728 .nid = 0, /* fill later */
4729 .ops = {
4730 .prepare = dyn_adc_capture_pcm_prepare,
4731 .cleanup = dyn_adc_capture_pcm_cleanup
4732 },
4733};
4734
Takashi Iwaif873e532012-12-20 16:58:39 +01004735static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4736 const char *chip_name)
4737{
4738 char *p;
4739
4740 if (*str)
4741 return;
4742 strlcpy(str, chip_name, len);
4743
4744 /* drop non-alnum chars after a space */
4745 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4746 if (!isalnum(p[1])) {
4747 *p = 0;
4748 break;
4749 }
4750 }
4751 strlcat(str, sfx, len);
4752}
4753
Takashi Iwai352f7f92012-12-19 12:52:06 +01004754/* build PCM streams based on the parsed results */
4755int snd_hda_gen_build_pcms(struct hda_codec *codec)
4756{
4757 struct hda_gen_spec *spec = codec->spec;
4758 struct hda_pcm *info = spec->pcm_rec;
4759 const struct hda_pcm_stream *p;
4760 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
4762 codec->num_pcms = 1;
4763 codec->pcm_info = info;
4764
Takashi Iwai352f7f92012-12-19 12:52:06 +01004765 if (spec->no_analog)
4766 goto skip_analog;
4767
Takashi Iwaif873e532012-12-20 16:58:39 +01004768 fill_pcm_stream_name(spec->stream_name_analog,
4769 sizeof(spec->stream_name_analog),
4770 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004771 info->name = spec->stream_name_analog;
4772
4773 if (spec->multiout.num_dacs > 0) {
4774 p = spec->stream_analog_playback;
4775 if (!p)
4776 p = &pcm_analog_playback;
4777 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4778 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4779 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4780 spec->multiout.max_channels;
4781 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4782 spec->autocfg.line_outs == 2)
4783 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4784 snd_pcm_2_1_chmaps;
4785 }
4786 if (spec->num_adc_nids) {
4787 p = spec->stream_analog_capture;
4788 if (!p) {
4789 if (spec->dyn_adc_switch)
4790 p = &dyn_adc_pcm_analog_capture;
4791 else
4792 p = &pcm_analog_capture;
4793 }
4794 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4795 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4796 }
4797
Takashi Iwai352f7f92012-12-19 12:52:06 +01004798 skip_analog:
4799 /* SPDIF for stream index #1 */
4800 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004801 fill_pcm_stream_name(spec->stream_name_digital,
4802 sizeof(spec->stream_name_digital),
4803 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004804 codec->num_pcms = 2;
4805 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4806 info = spec->pcm_rec + 1;
4807 info->name = spec->stream_name_digital;
4808 if (spec->dig_out_type)
4809 info->pcm_type = spec->dig_out_type;
4810 else
4811 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4812 if (spec->multiout.dig_out_nid) {
4813 p = spec->stream_digital_playback;
4814 if (!p)
4815 p = &pcm_digital_playback;
4816 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4817 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4818 }
4819 if (spec->dig_in_nid) {
4820 p = spec->stream_digital_capture;
4821 if (!p)
4822 p = &pcm_digital_capture;
4823 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4824 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4825 }
4826 }
4827
4828 if (spec->no_analog)
4829 return 0;
4830
4831 /* If the use of more than one ADC is requested for the current
4832 * model, configure a second analog capture-only PCM.
4833 */
4834 have_multi_adcs = (spec->num_adc_nids > 1) &&
4835 !spec->dyn_adc_switch && !spec->auto_mic;
4836 /* Additional Analaog capture for index #2 */
4837 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004838 fill_pcm_stream_name(spec->stream_name_alt_analog,
4839 sizeof(spec->stream_name_alt_analog),
4840 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004841 codec->num_pcms = 3;
4842 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004843 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004844 if (spec->alt_dac_nid) {
4845 p = spec->stream_analog_alt_playback;
4846 if (!p)
4847 p = &pcm_analog_alt_playback;
4848 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4849 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4850 spec->alt_dac_nid;
4851 } else {
4852 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4853 pcm_null_stream;
4854 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4855 }
4856 if (have_multi_adcs) {
4857 p = spec->stream_analog_alt_capture;
4858 if (!p)
4859 p = &pcm_analog_alt_capture;
4860 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4861 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4862 spec->adc_nids[1];
4863 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4864 spec->num_adc_nids - 1;
4865 } else {
4866 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4867 pcm_null_stream;
4868 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 }
4871
4872 return 0;
4873}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004874EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4875
4876
4877/*
4878 * Standard auto-parser initializations
4879 */
4880
Takashi Iwaid4156932013-01-07 10:08:02 +01004881/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004882static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004883{
4884 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004885 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004886
Takashi Iwai196c17662013-01-04 15:01:40 +01004887 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004888 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004889 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004890 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004891 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004892 snd_hda_activate_path(codec, path, path->active, true);
4893 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004894}
4895
4896/* initialize primary output paths */
4897static void init_multi_out(struct hda_codec *codec)
4898{
4899 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004900 int i;
4901
Takashi Iwaid4156932013-01-07 10:08:02 +01004902 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004903 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004904}
4905
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004906
Takashi Iwai2c12c302013-01-10 09:33:29 +01004907static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004908{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004909 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004910
Takashi Iwaid4156932013-01-07 10:08:02 +01004911 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004912 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004913}
4914
4915/* initialize hp and speaker paths */
4916static void init_extra_out(struct hda_codec *codec)
4917{
4918 struct hda_gen_spec *spec = codec->spec;
4919
4920 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004921 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004922 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4923 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004924 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004925}
4926
4927/* initialize multi-io paths */
4928static void init_multi_io(struct hda_codec *codec)
4929{
4930 struct hda_gen_spec *spec = codec->spec;
4931 int i;
4932
4933 for (i = 0; i < spec->multi_ios; i++) {
4934 hda_nid_t pin = spec->multi_io[i].pin;
4935 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004936 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004937 if (!path)
4938 continue;
4939 if (!spec->multi_io[i].ctl_in)
4940 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004941 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004942 snd_hda_activate_path(codec, path, path->active, true);
4943 }
4944}
4945
Takashi Iwai352f7f92012-12-19 12:52:06 +01004946/* set up input pins and loopback paths */
4947static void init_analog_input(struct hda_codec *codec)
4948{
4949 struct hda_gen_spec *spec = codec->spec;
4950 struct auto_pin_cfg *cfg = &spec->autocfg;
4951 int i;
4952
4953 for (i = 0; i < cfg->num_inputs; i++) {
4954 hda_nid_t nid = cfg->inputs[i].pin;
4955 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004956 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004957
4958 /* init loopback inputs */
4959 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01004960 resume_path_from_idx(codec, spec->loopback_paths[i]);
4961 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004962 }
4963 }
4964}
4965
4966/* initialize ADC paths */
4967static void init_input_src(struct hda_codec *codec)
4968{
4969 struct hda_gen_spec *spec = codec->spec;
4970 struct hda_input_mux *imux = &spec->input_mux;
4971 struct nid_path *path;
4972 int i, c, nums;
4973
4974 if (spec->dyn_adc_switch)
4975 nums = 1;
4976 else
4977 nums = spec->num_adc_nids;
4978
4979 for (c = 0; c < nums; c++) {
4980 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004981 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004982 if (path) {
4983 bool active = path->active;
4984 if (i == spec->cur_mux[c])
4985 active = true;
4986 snd_hda_activate_path(codec, path, active, false);
4987 }
4988 }
Takashi Iwai967303d2013-02-19 17:12:42 +01004989 if (spec->hp_mic)
4990 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004991 }
4992
Takashi Iwai352f7f92012-12-19 12:52:06 +01004993 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004994 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004995}
4996
4997/* set right pin controls for digital I/O */
4998static void init_digital(struct hda_codec *codec)
4999{
5000 struct hda_gen_spec *spec = codec->spec;
5001 int i;
5002 hda_nid_t pin;
5003
Takashi Iwaid4156932013-01-07 10:08:02 +01005004 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005005 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005006 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005007 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005008 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005009 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005010 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005011}
5012
Takashi Iwai973e4972012-12-20 15:16:09 +01005013/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5014 * invalid unsol tags by some reason
5015 */
5016static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5017{
5018 int i;
5019
5020 for (i = 0; i < codec->init_pins.used; i++) {
5021 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5022 hda_nid_t nid = pin->nid;
5023 if (is_jack_detectable(codec, nid) &&
5024 !snd_hda_jack_tbl_get(codec, nid))
5025 snd_hda_codec_update_cache(codec, nid, 0,
5026 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5027 }
5028}
5029
Takashi Iwai5187ac12013-01-07 12:52:16 +01005030/*
5031 * initialize the generic spec;
5032 * this can be put as patch_ops.init function
5033 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005034int snd_hda_gen_init(struct hda_codec *codec)
5035{
5036 struct hda_gen_spec *spec = codec->spec;
5037
5038 if (spec->init_hook)
5039 spec->init_hook(codec);
5040
5041 snd_hda_apply_verbs(codec);
5042
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005043 codec->cached_write = 1;
5044
Takashi Iwai352f7f92012-12-19 12:52:06 +01005045 init_multi_out(codec);
5046 init_extra_out(codec);
5047 init_multi_io(codec);
5048 init_analog_input(codec);
5049 init_input_src(codec);
5050 init_digital(codec);
5051
Takashi Iwai973e4972012-12-20 15:16:09 +01005052 clear_unsol_on_unused_pins(codec);
5053
Takashi Iwai352f7f92012-12-19 12:52:06 +01005054 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005055 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005056
Takashi Iwaidc870f32013-01-22 15:24:30 +01005057 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005058
Takashi Iwai352f7f92012-12-19 12:52:06 +01005059 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5060 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5061
5062 hda_call_check_power_status(codec, 0x01);
5063 return 0;
5064}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005065EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5066
Takashi Iwai5187ac12013-01-07 12:52:16 +01005067/*
5068 * free the generic spec;
5069 * this can be put as patch_ops.free function
5070 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005071void snd_hda_gen_free(struct hda_codec *codec)
5072{
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005073 snd_hda_detach_beep_device(codec);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005074 snd_hda_gen_spec_free(codec->spec);
5075 kfree(codec->spec);
5076 codec->spec = NULL;
5077}
5078EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5079
5080#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005081/*
5082 * check the loopback power save state;
5083 * this can be put as patch_ops.check_power_status function
5084 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005085int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5086{
5087 struct hda_gen_spec *spec = codec->spec;
5088 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5089}
5090EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5091#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005092
5093
5094/*
5095 * the generic codec support
5096 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
Takashi Iwai352f7f92012-12-19 12:52:06 +01005098static const struct hda_codec_ops generic_patch_ops = {
5099 .build_controls = snd_hda_gen_build_controls,
5100 .build_pcms = snd_hda_gen_build_pcms,
5101 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005102 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005103 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005104#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005105 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005106#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107};
5108
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109int snd_hda_parse_generic_codec(struct hda_codec *codec)
5110{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005111 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 int err;
5113
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005114 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005115 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005117 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005120 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5121 if (err < 0)
5122 return err;
5123
5124 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005125 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 goto error;
5127
5128 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 return 0;
5130
Takashi Iwai352f7f92012-12-19 12:52:06 +01005131error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005132 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 return err;
5134}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005135EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);