blob: 64220c08bd982b01851ab13b70660104d2d6e2d2 [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>
Takashi Iwaib21bdd02013-11-18 12:03:56 +010031#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include <sound/jack.h>
Takashi Iwaid89c6c02014-09-01 10:07:04 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_codec.h"
36#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010037#include "hda_auto_parser.h"
38#include "hda_jack.h"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010039#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010040#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Takashi Iwai352f7f92012-12-19 12:52:06 +010043/* initialize hda_gen_spec struct */
44int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Takashi Iwai352f7f92012-12-19 12:52:06 +010046 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010047 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010048 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010049 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010050 return 0;
51}
Takashi Iwai2698ea92013-12-18 07:45:52 +010052EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Takashi Iwai12c93df2012-12-19 14:38:33 +010054struct snd_kcontrol_new *
55snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
56 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010057{
58 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
59 if (!knew)
60 return NULL;
61 *knew = *temp;
62 if (name)
63 knew->name = kstrdup(name, GFP_KERNEL);
64 else if (knew->name)
65 knew->name = kstrdup(knew->name, GFP_KERNEL);
66 if (!knew->name)
67 return NULL;
68 return knew;
69}
Takashi Iwai2698ea92013-12-18 07:45:52 +010070EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010071
72static void free_kctls(struct hda_gen_spec *spec)
73{
74 if (spec->kctls.list) {
75 struct snd_kcontrol_new *kctl = spec->kctls.list;
76 int i;
77 for (i = 0; i < spec->kctls.used; i++)
78 kfree(kctl[i].name);
79 }
80 snd_array_free(&spec->kctls);
81}
82
Takashi Iwaia8dca462014-02-10 18:23:57 +010083static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
Takashi Iwai352f7f92012-12-19 12:52:06 +010084{
85 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010087 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010088 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010089 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010093 * store user hints
94 */
95static void parse_user_hints(struct hda_codec *codec)
96{
97 struct hda_gen_spec *spec = codec->spec;
98 int val;
99
100 val = snd_hda_get_bool_hint(codec, "jack_detect");
101 if (val >= 0)
102 codec->no_jack_detect = !val;
103 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
104 if (val >= 0)
105 codec->inv_jack_detect = !!val;
106 val = snd_hda_get_bool_hint(codec, "trigger_sense");
107 if (val >= 0)
108 codec->no_trigger_sense = !val;
109 val = snd_hda_get_bool_hint(codec, "inv_eapd");
110 if (val >= 0)
111 codec->inv_eapd = !!val;
112 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
113 if (val >= 0)
114 codec->pcm_format_first = !!val;
115 val = snd_hda_get_bool_hint(codec, "sticky_stream");
116 if (val >= 0)
117 codec->no_sticky_stream = !val;
118 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
119 if (val >= 0)
120 codec->spdif_status_reset = !!val;
121 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
122 if (val >= 0)
123 codec->pin_amp_workaround = !!val;
124 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
125 if (val >= 0)
126 codec->single_adc_amp = !!val;
127
Takashi Iwaif72706b2013-01-16 18:20:07 +0100128 val = snd_hda_get_bool_hint(codec, "auto_mute");
129 if (val >= 0)
130 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100131 val = snd_hda_get_bool_hint(codec, "auto_mic");
132 if (val >= 0)
133 spec->suppress_auto_mic = !val;
134 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
135 if (val >= 0)
136 spec->line_in_auto_switch = !!val;
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200137 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
138 if (val >= 0)
139 spec->auto_mute_via_amp = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100140 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
141 if (val >= 0)
142 spec->need_dac_fix = !!val;
143 val = snd_hda_get_bool_hint(codec, "primary_hp");
144 if (val >= 0)
145 spec->no_primary_hp = !val;
Takashi Iwaida96fb52013-07-29 16:54:36 +0200146 val = snd_hda_get_bool_hint(codec, "multi_io");
147 if (val >= 0)
148 spec->no_multi_io = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100149 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
150 if (val >= 0)
151 spec->multi_cap_vol = !!val;
152 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
153 if (val >= 0)
154 spec->inv_dmic_split = !!val;
155 val = snd_hda_get_bool_hint(codec, "indep_hp");
156 if (val >= 0)
157 spec->indep_hp = !!val;
158 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
159 if (val >= 0)
160 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100161 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100162 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
163 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100164 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100165 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
166 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100167 spec->add_jack_modes = !!val;
168 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
169 if (val >= 0)
170 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100171 val = snd_hda_get_bool_hint(codec, "power_down_unused");
172 if (val >= 0)
173 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100174 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
175 if (val >= 0)
176 spec->hp_mic = !!val;
177 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
178 if (val >= 0)
179 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100180
181 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
182 spec->mixer_nid = val;
183}
184
185/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100186 * pin control value accesses
187 */
188
189#define update_pin_ctl(codec, pin, val) \
190 snd_hda_codec_update_cache(codec, pin, 0, \
191 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
192
193/* restore the pinctl based on the cached value */
194static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
195{
196 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
197}
198
199/* set the pinctl target value and write it if requested */
200static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
201 unsigned int val, bool do_write)
202{
203 if (!pin)
204 return;
205 val = snd_hda_correct_pin_ctl(codec, pin, val);
206 snd_hda_codec_set_pin_target(codec, pin, val);
207 if (do_write)
208 update_pin_ctl(codec, pin, val);
209}
210
211/* set pinctl target values for all given pins */
212static void set_pin_targets(struct hda_codec *codec, int num_pins,
213 hda_nid_t *pins, unsigned int val)
214{
215 int i;
216 for (i = 0; i < num_pins; i++)
217 set_pin_target(codec, pins[i], val, false);
218}
219
220/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100221 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100224/* return the position of NID in the list, or -1 if not found */
225static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
226{
227 int i;
228 for (i = 0; i < nums; i++)
229 if (list[i] == nid)
230 return i;
231 return -1;
232}
233
234/* return true if the given NID is contained in the path */
235static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
236{
237 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
238}
239
Takashi Iwaif5172a72013-01-04 13:19:55 +0100240static struct nid_path *get_nid_path(struct hda_codec *codec,
241 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100242 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100244 struct hda_gen_spec *spec = codec->spec;
245 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Takashi Iwai352f7f92012-12-19 12:52:06 +0100247 for (i = 0; i < spec->paths.used; i++) {
248 struct nid_path *path = snd_array_elem(&spec->paths, i);
249 if (path->depth <= 0)
250 continue;
251 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100252 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100253 if (!anchor_nid ||
254 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
255 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100256 return path;
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 return NULL;
260}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100261
262/* get the path between the given NIDs;
263 * passing 0 to either @pin or @dac behaves as a wildcard
264 */
265struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
266 hda_nid_t from_nid, hda_nid_t to_nid)
267{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100268 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100269}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100270EXPORT_SYMBOL_GPL(snd_hda_get_nid_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100271
Takashi Iwai196c17662013-01-04 15:01:40 +0100272/* get the index number corresponding to the path instance;
273 * the index starts from 1, for easier checking the invalid value
274 */
275int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
276{
277 struct hda_gen_spec *spec = codec->spec;
278 struct nid_path *array = spec->paths.list;
279 ssize_t idx;
280
281 if (!spec->paths.used)
282 return 0;
283 idx = path - array;
284 if (idx < 0 || idx >= spec->paths.used)
285 return 0;
286 return idx + 1;
287}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100288EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100289
290/* get the path instance corresponding to the given index number */
291struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
292{
293 struct hda_gen_spec *spec = codec->spec;
294
295 if (idx <= 0 || idx > spec->paths.used)
296 return NULL;
297 return snd_array_elem(&spec->paths, idx - 1);
298}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100299EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100300
Takashi Iwai352f7f92012-12-19 12:52:06 +0100301/* check whether the given DAC is already found in any existing paths */
302static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
303{
304 struct hda_gen_spec *spec = codec->spec;
305 int i;
306
307 for (i = 0; i < spec->paths.used; i++) {
308 struct nid_path *path = snd_array_elem(&spec->paths, i);
309 if (path->path[0] == nid)
310 return true;
311 }
312 return false;
313}
314
315/* check whether the given two widgets can be connected */
316static bool is_reachable_path(struct hda_codec *codec,
317 hda_nid_t from_nid, hda_nid_t to_nid)
318{
319 if (!from_nid || !to_nid)
320 return false;
321 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
322}
323
324/* nid, dir and idx */
325#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
326
327/* check whether the given ctl is already assigned in any path elements */
328static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
329{
330 struct hda_gen_spec *spec = codec->spec;
331 int i;
332
333 val &= AMP_VAL_COMPARE_MASK;
334 for (i = 0; i < spec->paths.used; i++) {
335 struct nid_path *path = snd_array_elem(&spec->paths, i);
336 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
337 return true;
338 }
339 return false;
340}
341
342/* check whether a control with the given (nid, dir, idx) was assigned */
343static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100344 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100345{
346 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100347 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100348}
349
Takashi Iwai4e76a882014-02-25 12:21:03 +0100350static void print_nid_path(struct hda_codec *codec,
351 const char *pfx, struct nid_path *path)
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100352{
353 char buf[40];
Joe Perchesd82353e2014-07-05 13:02:02 -0700354 char *pos = buf;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100355 int i;
356
Joe Perchesd82353e2014-07-05 13:02:02 -0700357 *pos = 0;
358 for (i = 0; i < path->depth; i++)
359 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
360 pos != buf ? ":" : "",
361 path->path[i]);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100362
Joe Perchesd82353e2014-07-05 13:02:02 -0700363 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100364}
365
Takashi Iwai352f7f92012-12-19 12:52:06 +0100366/* called recursively */
367static bool __parse_nid_path(struct hda_codec *codec,
368 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100369 int anchor_nid, struct nid_path *path,
370 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100371{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100372 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100373 int i, nums;
374
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100375 if (to_nid == anchor_nid)
376 anchor_nid = 0; /* anchor passed */
377 else if (to_nid == (hda_nid_t)(-anchor_nid))
378 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100379
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100380 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100381 for (i = 0; i < nums; i++) {
382 if (conn[i] != from_nid) {
383 /* special case: when from_nid is 0,
384 * try to find an empty DAC
385 */
386 if (from_nid ||
387 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
388 is_dac_already_used(codec, conn[i]))
389 continue;
390 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100391 /* anchor is not requested or already passed? */
392 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100393 goto found;
394 }
395 if (depth >= MAX_NID_PATH_DEPTH)
396 return false;
397 for (i = 0; i < nums; i++) {
398 unsigned int type;
399 type = get_wcaps_type(get_wcaps(codec, conn[i]));
400 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
401 type == AC_WID_PIN)
402 continue;
403 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100404 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100405 goto found;
406 }
407 return false;
408
409 found:
410 path->path[path->depth] = conn[i];
411 path->idx[path->depth + 1] = i;
412 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
413 path->multi[path->depth + 1] = 1;
414 path->depth++;
415 return true;
416}
417
418/* parse the widget path from the given nid to the target nid;
419 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100420 * when @anchor_nid is set to a positive value, only paths through the widget
421 * with the given value are evaluated.
422 * when @anchor_nid is set to a negative value, paths through the widget
423 * with the negative of given value are excluded, only other paths are chosen.
424 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100425 */
426bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100427 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100428 struct nid_path *path)
429{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100430 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100431 path->path[path->depth] = to_nid;
432 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100433 return true;
434 }
435 return false;
436}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100437EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100440 * parse the path between the given NIDs and add to the path list.
441 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100443struct nid_path *
444snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100445 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100447 struct hda_gen_spec *spec = codec->spec;
448 struct nid_path *path;
449
450 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
451 return NULL;
452
Takashi Iwaif5172a72013-01-04 13:19:55 +0100453 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100454 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100455 if (path)
456 return path;
457
Takashi Iwai352f7f92012-12-19 12:52:06 +0100458 path = snd_array_new(&spec->paths);
459 if (!path)
460 return NULL;
461 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100462 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100463 return path;
464 /* push back */
465 spec->paths.used--;
466 return NULL;
467}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100468EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100469
Takashi Iwai980428c2013-01-09 09:28:20 +0100470/* clear the given path as invalid so that it won't be picked up later */
471static void invalidate_nid_path(struct hda_codec *codec, int idx)
472{
473 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
474 if (!path)
475 return;
476 memset(path, 0, sizeof(*path));
477}
478
Takashi Iwai36907392013-12-10 17:29:26 +0100479/* return a DAC if paired to the given pin by codec driver */
480static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
481{
482 struct hda_gen_spec *spec = codec->spec;
483 const hda_nid_t *list = spec->preferred_dacs;
484
485 if (!list)
486 return 0;
487 for (; *list; list += 2)
488 if (*list == pin)
489 return list[1];
490 return 0;
491}
492
Takashi Iwai352f7f92012-12-19 12:52:06 +0100493/* look for an empty DAC slot */
494static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
495 bool is_digital)
496{
497 struct hda_gen_spec *spec = codec->spec;
498 bool cap_digital;
499 int i;
500
501 for (i = 0; i < spec->num_all_dacs; i++) {
502 hda_nid_t nid = spec->all_dacs[i];
503 if (!nid || is_dac_already_used(codec, nid))
504 continue;
505 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
506 if (is_digital != cap_digital)
507 continue;
508 if (is_reachable_path(codec, nid, pin))
509 return nid;
510 }
511 return 0;
512}
513
514/* replace the channels in the composed amp value with the given number */
515static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
516{
517 val &= ~(0x3U << 16);
518 val |= chs << 16;
519 return val;
520}
521
David Henningsson99a55922013-01-16 15:58:44 +0100522static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
523 hda_nid_t nid2, int dir)
524{
525 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
526 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
527 return (query_amp_caps(codec, nid1, dir) ==
528 query_amp_caps(codec, nid2, dir));
529}
530
Takashi Iwai352f7f92012-12-19 12:52:06 +0100531/* look for a widget suitable for assigning a mute switch in the path */
532static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
533 struct nid_path *path)
534{
535 int i;
536
537 for (i = path->depth - 1; i >= 0; i--) {
538 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
539 return path->path[i];
540 if (i != path->depth - 1 && i != 0 &&
541 nid_has_mute(codec, path->path[i], HDA_INPUT))
542 return path->path[i];
543 }
544 return 0;
545}
546
547/* look for a widget suitable for assigning a volume ctl in the path */
548static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
549 struct nid_path *path)
550{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100551 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100552 int i;
553
554 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100555 hda_nid_t nid = path->path[i];
556 if ((spec->out_vol_mask >> nid) & 1)
557 continue;
558 if (nid_has_volume(codec, nid, HDA_OUTPUT))
559 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100560 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100565 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100567
568/* can have the amp-in capability? */
569static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100571 hda_nid_t nid = path->path[idx];
572 unsigned int caps = get_wcaps(codec, nid);
573 unsigned int type = get_wcaps_type(caps);
574
575 if (!(caps & AC_WCAP_IN_AMP))
576 return false;
577 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
578 return false;
579 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Takashi Iwai352f7f92012-12-19 12:52:06 +0100582/* can have the amp-out capability? */
583static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100585 hda_nid_t nid = path->path[idx];
586 unsigned int caps = get_wcaps(codec, nid);
587 unsigned int type = get_wcaps_type(caps);
588
589 if (!(caps & AC_WCAP_OUT_AMP))
590 return false;
591 if (type == AC_WID_PIN && !idx) /* only for output pins */
592 return false;
593 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Takashi Iwai352f7f92012-12-19 12:52:06 +0100596/* check whether the given (nid,dir,idx) is active */
597static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100598 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100600 struct hda_gen_spec *spec = codec->spec;
601 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Takashi Iwai352f7f92012-12-19 12:52:06 +0100603 for (n = 0; n < spec->paths.used; n++) {
604 struct nid_path *path = snd_array_elem(&spec->paths, n);
605 if (!path->active)
606 continue;
607 for (i = 0; i < path->depth; i++) {
608 if (path->path[i] == nid) {
609 if (dir == HDA_OUTPUT || path->idx[i] == idx)
610 return true;
611 break;
612 }
613 }
614 }
615 return false;
616}
617
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200618/* check whether the NID is referred by any active paths */
619#define is_active_nid_for_any(codec, nid) \
620 is_active_nid(codec, nid, HDA_OUTPUT, 0)
621
Takashi Iwai352f7f92012-12-19 12:52:06 +0100622/* get the default amp value for the target state */
623static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100624 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100625{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100626 unsigned int val = 0;
627
Takashi Iwai352f7f92012-12-19 12:52:06 +0100628 if (caps & AC_AMPCAP_NUM_STEPS) {
629 /* set to 0dB */
630 if (enable)
631 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
632 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200633 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100634 if (!enable)
635 val |= HDA_AMP_MUTE;
636 }
637 return val;
638}
639
640/* initialize the amp value (only at the first time) */
641static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
642{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100643 unsigned int caps = query_amp_caps(codec, nid, dir);
644 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100645 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
646}
647
Takashi Iwai8999bf02013-01-18 11:01:33 +0100648/* calculate amp value mask we can modify;
649 * if the given amp is controlled by mixers, don't touch it
650 */
651static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
652 hda_nid_t nid, int dir, int idx,
653 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100654{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100655 unsigned int mask = 0xff;
656
Takashi Iwaif69910d2013-08-08 09:32:37 +0200657 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100658 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
659 mask &= ~0x80;
660 }
661 if (caps & AC_AMPCAP_NUM_STEPS) {
662 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
663 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
664 mask &= ~0x7f;
665 }
666 return mask;
667}
668
669static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
670 int idx, int idx_to_check, bool enable)
671{
672 unsigned int caps;
673 unsigned int mask, val;
674
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100675 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100676 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100677
678 caps = query_amp_caps(codec, nid, dir);
679 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
680 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
681 if (!mask)
682 return;
683
684 val &= mask;
685 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100686}
687
688static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
689 int i, bool enable)
690{
691 hda_nid_t nid = path->path[i];
692 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100693 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100694}
695
696static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
697 int i, bool enable, bool add_aamix)
698{
699 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100700 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100701 int n, nums, idx;
702 int type;
703 hda_nid_t nid = path->path[i];
704
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100705 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100706 type = get_wcaps_type(get_wcaps(codec, nid));
707 if (type == AC_WID_PIN ||
708 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
709 nums = 1;
710 idx = 0;
711 } else
712 idx = path->idx[i];
713
714 for (n = 0; n < nums; n++)
715 init_amp(codec, nid, HDA_INPUT, n);
716
Takashi Iwai352f7f92012-12-19 12:52:06 +0100717 /* here is a little bit tricky in comparison with activate_amp_out();
718 * when aa-mixer is available, we need to enable the path as well
719 */
720 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100721 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100722 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100723 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725}
726
Takashi Iwai352f7f92012-12-19 12:52:06 +0100727/* activate or deactivate the given path
728 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100730void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
731 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100733 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100734 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Takashi Iwai352f7f92012-12-19 12:52:06 +0100736 if (!enable)
737 path->active = false;
738
739 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100740 hda_nid_t nid = path->path[i];
741 if (enable && spec->power_down_unused) {
742 /* make sure the widget is powered up */
743 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
744 snd_hda_codec_write(codec, nid, 0,
745 AC_VERB_SET_POWER_STATE,
746 AC_PWRST_D0);
747 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100748 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100749 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100750 AC_VERB_SET_CONNECT_SEL,
751 path->idx[i]);
752 if (has_amp_in(codec, path, i))
753 activate_amp_in(codec, path, i, enable, add_aamix);
754 if (has_amp_out(codec, path, i))
755 activate_amp_out(codec, path, i, enable);
756 }
757
758 if (enable)
759 path->active = true;
760}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100761EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100762
Takashi Iwai55196ff2013-01-24 17:32:56 +0100763/* if the given path is inactive, put widgets into D3 (only if suitable) */
764static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
765{
766 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200767 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100768 int i;
769
770 if (!spec->power_down_unused || path->active)
771 return;
772
773 for (i = 0; i < path->depth; i++) {
774 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200775 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
776 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100777 snd_hda_codec_write(codec, nid, 0,
778 AC_VERB_SET_POWER_STATE,
779 AC_PWRST_D3);
780 changed = true;
781 }
782 }
783
784 if (changed) {
785 msleep(10);
786 snd_hda_codec_read(codec, path->path[0], 0,
787 AC_VERB_GET_POWER_STATE, 0);
788 }
789}
790
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100791/* turn on/off EAPD on the given pin */
792static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
793{
794 struct hda_gen_spec *spec = codec->spec;
795 if (spec->own_eapd_ctl ||
796 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
797 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200798 if (spec->keep_eapd_on && !enable)
799 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100800 if (codec->inv_eapd)
801 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100802 snd_hda_codec_update_cache(codec, pin, 0,
803 AC_VERB_SET_EAPD_BTLENABLE,
804 enable ? 0x02 : 0x00);
805}
806
Takashi Iwai3e367f12013-01-23 17:07:23 +0100807/* re-initialize the path specified by the given path index */
808static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
809{
810 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
811 if (path)
812 snd_hda_activate_path(codec, path, path->active, false);
813}
814
Takashi Iwai352f7f92012-12-19 12:52:06 +0100815
816/*
817 * Helper functions for creating mixer ctl elements
818 */
819
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200820static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
821 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200822static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
823 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200824
Takashi Iwai352f7f92012-12-19 12:52:06 +0100825enum {
826 HDA_CTL_WIDGET_VOL,
827 HDA_CTL_WIDGET_MUTE,
828 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100829};
830static const struct snd_kcontrol_new control_templates[] = {
831 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200832 /* only the put callback is replaced for handling the special mute */
833 {
834 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
835 .subdevice = HDA_SUBDEV_AMP_FLAG,
836 .info = snd_hda_mixer_amp_switch_info,
837 .get = snd_hda_mixer_amp_switch_get,
838 .put = hda_gen_mixer_mute_put, /* replaced */
839 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
840 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200841 {
842 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
843 .info = snd_hda_mixer_amp_switch_info,
844 .get = snd_hda_mixer_bind_switch_get,
845 .put = hda_gen_bind_mute_put, /* replaced */
846 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
847 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100848};
849
850/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100851static struct snd_kcontrol_new *
852add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100853 int cidx, unsigned long val)
854{
855 struct snd_kcontrol_new *knew;
856
Takashi Iwai12c93df2012-12-19 14:38:33 +0100857 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100858 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100859 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100860 knew->index = cidx;
861 if (get_amp_nid_(val))
862 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
863 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100864 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100865}
866
867static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
868 const char *pfx, const char *dir,
869 const char *sfx, int cidx, unsigned long val)
870{
Takashi Iwai975cc022013-06-28 11:56:49 +0200871 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100872 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100873 if (!add_control(spec, type, name, cidx, val))
874 return -ENOMEM;
875 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100876}
877
878#define add_pb_vol_ctrl(spec, type, pfx, val) \
879 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
880#define add_pb_sw_ctrl(spec, type, pfx, val) \
881 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
882#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
883 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
884#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
885 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
886
887static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
888 unsigned int chs, struct nid_path *path)
889{
890 unsigned int val;
891 if (!path)
892 return 0;
893 val = path->ctls[NID_PATH_VOL_CTL];
894 if (!val)
895 return 0;
896 val = amp_val_replace_channels(val, chs);
897 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
898}
899
900/* return the channel bits suitable for the given path->ctls[] */
901static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
902 int type)
903{
904 int chs = 1; /* mono (left only) */
905 if (path) {
906 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
907 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
908 chs = 3; /* stereo */
909 }
910 return chs;
911}
912
913static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
914 struct nid_path *path)
915{
916 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
917 return add_vol_ctl(codec, pfx, cidx, chs, path);
918}
919
920/* create a mute-switch for the given mixer widget;
921 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
922 */
923static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
924 unsigned int chs, struct nid_path *path)
925{
926 unsigned int val;
927 int type = HDA_CTL_WIDGET_MUTE;
928
929 if (!path)
930 return 0;
931 val = path->ctls[NID_PATH_MUTE_CTL];
932 if (!val)
933 return 0;
934 val = amp_val_replace_channels(val, chs);
935 if (get_amp_direction_(val) == HDA_INPUT) {
936 hda_nid_t nid = get_amp_nid_(val);
937 int nums = snd_hda_get_num_conns(codec, nid);
938 if (nums > 1) {
939 type = HDA_CTL_BIND_MUTE;
940 val |= nums << 19;
941 }
942 }
943 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
944}
945
946static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
947 int cidx, struct nid_path *path)
948{
949 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
950 return add_sw_ctl(codec, pfx, cidx, chs, path);
951}
952
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200953/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200954static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
955 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200956{
957 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
958 struct hda_gen_spec *spec = codec->spec;
959
960 if (spec->auto_mute_via_amp) {
961 hda_nid_t nid = get_amp_nid(kcontrol);
962 bool enabled = !((spec->mute_bits >> nid) & 1);
963 ucontrol->value.integer.value[0] &= enabled;
964 ucontrol->value.integer.value[1] &= enabled;
965 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200966}
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200967
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200968static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
969 struct snd_ctl_elem_value *ucontrol)
970{
971 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200972 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
973}
974
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200975static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
976 struct snd_ctl_elem_value *ucontrol)
977{
978 sync_auto_mute_bits(kcontrol, ucontrol);
979 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
980}
981
Takashi Iwai247d85e2013-01-17 16:18:11 +0100982/* any ctl assigned to the path with the given index? */
983static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
984{
985 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
986 return path && path->ctls[ctl_type];
987}
988
Takashi Iwai352f7f92012-12-19 12:52:06 +0100989static const char * const channel_name[4] = {
990 "Front", "Surround", "CLFE", "Side"
991};
992
993/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100994static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
995 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100996{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100997 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100998 struct auto_pin_cfg *cfg = &spec->autocfg;
999
1000 *index = 0;
1001 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001002 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001003 return spec->vmaster_mute.hook ? "PCM" : "Master";
1004
1005 /* if there is really a single DAC used in the whole output paths,
1006 * use it master (or "PCM" if a vmaster hook is present)
1007 */
1008 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1009 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1010 return spec->vmaster_mute.hook ? "PCM" : "Master";
1011
Takashi Iwai247d85e2013-01-17 16:18:11 +01001012 /* multi-io channels */
1013 if (ch >= cfg->line_outs)
1014 return channel_name[ch];
1015
Takashi Iwai352f7f92012-12-19 12:52:06 +01001016 switch (cfg->line_out_type) {
1017 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001018 /* if the primary channel vol/mute is shared with HP volume,
1019 * don't name it as Speaker
1020 */
1021 if (!ch && cfg->hp_outs &&
1022 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1023 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001024 if (cfg->line_outs == 1)
1025 return "Speaker";
1026 if (cfg->line_outs == 2)
1027 return ch ? "Bass Speaker" : "Speaker";
1028 break;
1029 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001030 /* if the primary channel vol/mute is shared with spk volume,
1031 * don't name it as Headphone
1032 */
1033 if (!ch && cfg->speaker_outs &&
1034 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1035 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001036 /* for multi-io case, only the primary out */
1037 if (ch && spec->multi_ios)
1038 break;
1039 *index = ch;
1040 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +01001041 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001042
1043 /* for a single channel output, we don't have to name the channel */
1044 if (cfg->line_outs == 1 && !spec->multi_ios)
1045 return "PCM";
1046
Takashi Iwai352f7f92012-12-19 12:52:06 +01001047 if (ch >= ARRAY_SIZE(channel_name)) {
1048 snd_BUG();
1049 return "PCM";
1050 }
1051
1052 return channel_name[ch];
1053}
1054
1055/*
1056 * Parse output paths
1057 */
1058
1059/* badness definition */
1060enum {
1061 /* No primary DAC is found for the main output */
1062 BAD_NO_PRIMARY_DAC = 0x10000,
1063 /* No DAC is found for the extra output */
1064 BAD_NO_DAC = 0x4000,
1065 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001066 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001067 /* No individual DAC for extra output */
1068 BAD_NO_EXTRA_DAC = 0x102,
1069 /* No individual DAC for extra surrounds */
1070 BAD_NO_EXTRA_SURR_DAC = 0x101,
1071 /* Primary DAC shared with main surrounds */
1072 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001073 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001074 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001075 /* Primary DAC shared with main CLFE */
1076 BAD_SHARED_CLFE = 0x10,
1077 /* Primary DAC shared with extra surrounds */
1078 BAD_SHARED_EXTRA_SURROUND = 0x10,
1079 /* Volume widget is shared */
1080 BAD_SHARED_VOL = 0x10,
1081};
1082
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001083/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001084 * volume and mute controls, and assign the values to ctls[].
1085 *
1086 * When no appropriate widget is found in the path, the badness value
1087 * is incremented depending on the situation. The function returns the
1088 * total badness for both volume and mute controls.
1089 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001090static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001091{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001092 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001093 hda_nid_t nid;
1094 unsigned int val;
1095 int badness = 0;
1096
1097 if (!path)
1098 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001099
1100 if (path->ctls[NID_PATH_VOL_CTL] ||
1101 path->ctls[NID_PATH_MUTE_CTL])
1102 return 0; /* already evaluated */
1103
Takashi Iwai352f7f92012-12-19 12:52:06 +01001104 nid = look_for_out_vol_nid(codec, path);
1105 if (nid) {
1106 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001107 if (spec->dac_min_mute)
1108 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001109 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1110 badness += BAD_SHARED_VOL;
1111 else
1112 path->ctls[NID_PATH_VOL_CTL] = val;
1113 } else
1114 badness += BAD_SHARED_VOL;
1115 nid = look_for_out_mute_nid(codec, path);
1116 if (nid) {
1117 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1118 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1119 nid_has_mute(codec, nid, HDA_OUTPUT))
1120 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1121 else
1122 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1123 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1124 badness += BAD_SHARED_VOL;
1125 else
1126 path->ctls[NID_PATH_MUTE_CTL] = val;
1127 } else
1128 badness += BAD_SHARED_VOL;
1129 return badness;
1130}
1131
Takashi Iwai98bd1112013-03-22 14:53:50 +01001132const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001133 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1134 .no_dac = BAD_NO_DAC,
1135 .shared_primary = BAD_NO_PRIMARY_DAC,
1136 .shared_surr = BAD_SHARED_SURROUND,
1137 .shared_clfe = BAD_SHARED_CLFE,
1138 .shared_surr_main = BAD_SHARED_SURROUND,
1139};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001140EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001141
Takashi Iwai98bd1112013-03-22 14:53:50 +01001142const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001143 .no_primary_dac = BAD_NO_DAC,
1144 .no_dac = BAD_NO_DAC,
1145 .shared_primary = BAD_NO_EXTRA_DAC,
1146 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1147 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1148 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1149};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001150EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001151
Takashi Iwai7385df62013-01-07 09:50:52 +01001152/* get the DAC of the primary output corresponding to the given array index */
1153static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1154{
1155 struct hda_gen_spec *spec = codec->spec;
1156 struct auto_pin_cfg *cfg = &spec->autocfg;
1157
1158 if (cfg->line_outs > idx)
1159 return spec->private_dac_nids[idx];
1160 idx -= cfg->line_outs;
1161 if (spec->multi_ios > idx)
1162 return spec->multi_io[idx].dac;
1163 return 0;
1164}
1165
1166/* return the DAC if it's reachable, otherwise zero */
1167static inline hda_nid_t try_dac(struct hda_codec *codec,
1168 hda_nid_t dac, hda_nid_t pin)
1169{
1170 return is_reachable_path(codec, dac, pin) ? dac : 0;
1171}
1172
Takashi Iwai352f7f92012-12-19 12:52:06 +01001173/* try to assign DACs to pins and return the resultant badness */
1174static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1175 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001176 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001177 const struct badness_table *bad)
1178{
1179 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001180 int i, j;
1181 int badness = 0;
1182 hda_nid_t dac;
1183
1184 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return 0;
1186
Takashi Iwai352f7f92012-12-19 12:52:06 +01001187 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001188 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001189 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001190
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001191 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1192 if (path) {
1193 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001194 continue;
1195 }
1196
Takashi Iwai36907392013-12-10 17:29:26 +01001197 dacs[i] = get_preferred_dac(codec, pin);
1198 if (dacs[i]) {
1199 if (is_dac_already_used(codec, dacs[i]))
1200 badness += bad->shared_primary;
1201 }
1202
1203 if (!dacs[i])
1204 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001205 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001206 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001207 for (j = 1; j < num_outs; j++) {
1208 if (is_reachable_path(codec, dacs[j], pin)) {
1209 dacs[0] = dacs[j];
1210 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001211 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001212 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001213 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001216 }
1217 dac = dacs[i];
1218 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001219 if (num_outs > 2)
1220 dac = try_dac(codec, get_primary_out(codec, i), pin);
1221 if (!dac)
1222 dac = try_dac(codec, dacs[0], pin);
1223 if (!dac)
1224 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001225 if (dac) {
1226 if (!i)
1227 badness += bad->shared_primary;
1228 else if (i == 1)
1229 badness += bad->shared_surr;
1230 else
1231 badness += bad->shared_clfe;
1232 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1233 dac = spec->private_dac_nids[0];
1234 badness += bad->shared_surr_main;
1235 } else if (!i)
1236 badness += bad->no_primary_dac;
1237 else
1238 badness += bad->no_dac;
1239 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001240 if (!dac)
1241 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001242 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001243 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001244 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001245 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001246 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001247 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001248 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001249 badness += bad->no_dac;
1250 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001251 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001252 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001253 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001254 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001255 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001256 }
1257
1258 return badness;
1259}
1260
1261/* return NID if the given pin has only a single connection to a certain DAC */
1262static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1263{
1264 struct hda_gen_spec *spec = codec->spec;
1265 int i;
1266 hda_nid_t nid_found = 0;
1267
1268 for (i = 0; i < spec->num_all_dacs; i++) {
1269 hda_nid_t nid = spec->all_dacs[i];
1270 if (!nid || is_dac_already_used(codec, nid))
1271 continue;
1272 if (is_reachable_path(codec, nid, pin)) {
1273 if (nid_found)
1274 return 0;
1275 nid_found = nid;
1276 }
1277 }
1278 return nid_found;
1279}
1280
1281/* check whether the given pin can be a multi-io pin */
1282static bool can_be_multiio_pin(struct hda_codec *codec,
1283 unsigned int location, hda_nid_t nid)
1284{
1285 unsigned int defcfg, caps;
1286
1287 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1288 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1289 return false;
1290 if (location && get_defcfg_location(defcfg) != location)
1291 return false;
1292 caps = snd_hda_query_pin_caps(codec, nid);
1293 if (!(caps & AC_PINCAP_OUT))
1294 return false;
1295 return true;
1296}
1297
Takashi Iwaie22aab72013-01-04 14:50:04 +01001298/* count the number of input pins that are capable to be multi-io */
1299static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1300{
1301 struct hda_gen_spec *spec = codec->spec;
1302 struct auto_pin_cfg *cfg = &spec->autocfg;
1303 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1304 unsigned int location = get_defcfg_location(defcfg);
1305 int type, i;
1306 int num_pins = 0;
1307
1308 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1309 for (i = 0; i < cfg->num_inputs; i++) {
1310 if (cfg->inputs[i].type != type)
1311 continue;
1312 if (can_be_multiio_pin(codec, location,
1313 cfg->inputs[i].pin))
1314 num_pins++;
1315 }
1316 }
1317 return num_pins;
1318}
1319
Takashi Iwai352f7f92012-12-19 12:52:06 +01001320/*
1321 * multi-io helper
1322 *
1323 * When hardwired is set, try to fill ony hardwired pins, and returns
1324 * zero if any pins are filled, non-zero if nothing found.
1325 * When hardwired is off, try to fill possible input pins, and returns
1326 * the badness value.
1327 */
1328static int fill_multi_ios(struct hda_codec *codec,
1329 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001330 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001331{
1332 struct hda_gen_spec *spec = codec->spec;
1333 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001334 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001335 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1336 unsigned int location = get_defcfg_location(defcfg);
1337 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001338 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001339
1340 old_pins = spec->multi_ios;
1341 if (old_pins >= 2)
1342 goto end_fill;
1343
Takashi Iwaie22aab72013-01-04 14:50:04 +01001344 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001345 if (num_pins < 2)
1346 goto end_fill;
1347
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1349 for (i = 0; i < cfg->num_inputs; i++) {
1350 hda_nid_t nid = cfg->inputs[i].pin;
1351 hda_nid_t dac = 0;
1352
1353 if (cfg->inputs[i].type != type)
1354 continue;
1355 if (!can_be_multiio_pin(codec, location, nid))
1356 continue;
1357 for (j = 0; j < spec->multi_ios; j++) {
1358 if (nid == spec->multi_io[j].pin)
1359 break;
1360 }
1361 if (j < spec->multi_ios)
1362 continue;
1363
Takashi Iwai352f7f92012-12-19 12:52:06 +01001364 if (hardwired)
1365 dac = get_dac_if_single(codec, nid);
1366 else if (!dac)
1367 dac = look_for_dac(codec, nid, false);
1368 if (!dac) {
1369 badness++;
1370 continue;
1371 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001372 path = snd_hda_add_new_path(codec, dac, nid,
1373 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001374 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001375 badness++;
1376 continue;
1377 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001378 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001379 spec->multi_io[spec->multi_ios].pin = nid;
1380 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001381 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1382 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001383 spec->multi_ios++;
1384 if (spec->multi_ios >= 2)
1385 break;
1386 }
1387 }
1388 end_fill:
1389 if (badness)
1390 badness = BAD_MULTI_IO;
1391 if (old_pins == spec->multi_ios) {
1392 if (hardwired)
1393 return 1; /* nothing found */
1394 else
1395 return badness; /* no badness if nothing found */
1396 }
1397 if (!hardwired && spec->multi_ios < 2) {
1398 /* cancel newly assigned paths */
1399 spec->paths.used -= spec->multi_ios - old_pins;
1400 spec->multi_ios = old_pins;
1401 return badness;
1402 }
1403
1404 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001405 for (i = old_pins; i < spec->multi_ios; i++) {
1406 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1407 badness += assign_out_path_ctls(codec, path);
1408 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001409
1410 return badness;
1411}
1412
1413/* map DACs for all pins in the list if they are single connections */
1414static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001415 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001416{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001417 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001418 int i;
1419 bool found = false;
1420 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001421 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001422 hda_nid_t dac;
1423 if (dacs[i])
1424 continue;
1425 dac = get_dac_if_single(codec, pins[i]);
1426 if (!dac)
1427 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001428 path = snd_hda_add_new_path(codec, dac, pins[i],
1429 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001430 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001431 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001432 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001433 dacs[i] = dac;
1434 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001435 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001436 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001437 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001438 }
1439 }
1440 return found;
1441}
1442
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001443/* create a new path including aamix if available, and return its index */
1444static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1445{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001446 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001447 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001448 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001449
1450 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001451 if (!path || !path->depth ||
1452 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001453 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001454 path_dac = path->path[0];
1455 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001456 pin = path->path[path->depth - 1];
1457 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1458 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001459 if (dac != path_dac)
1460 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001461 else if (spec->multiout.hp_out_nid[0])
1462 dac = spec->multiout.hp_out_nid[0];
1463 else if (spec->multiout.extra_out_nid[0])
1464 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001465 else
1466 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001467 if (dac)
1468 path = snd_hda_add_new_path(codec, dac, pin,
1469 spec->mixer_nid);
1470 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001471 if (!path)
1472 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001473 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001474 path->active = false; /* unused as default */
1475 return snd_hda_get_path_idx(codec, path);
1476}
1477
Takashi Iwai55a63d42013-03-21 17:20:12 +01001478/* check whether the independent HP is available with the current config */
1479static bool indep_hp_possible(struct hda_codec *codec)
1480{
1481 struct hda_gen_spec *spec = codec->spec;
1482 struct auto_pin_cfg *cfg = &spec->autocfg;
1483 struct nid_path *path;
1484 int i, idx;
1485
1486 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1487 idx = spec->out_paths[0];
1488 else
1489 idx = spec->hp_paths[0];
1490 path = snd_hda_get_path_from_idx(codec, idx);
1491 if (!path)
1492 return false;
1493
1494 /* assume no path conflicts unless aamix is involved */
1495 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1496 return true;
1497
1498 /* check whether output paths contain aamix */
1499 for (i = 0; i < cfg->line_outs; i++) {
1500 if (spec->out_paths[i] == idx)
1501 break;
1502 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1503 if (path && is_nid_contained(path, spec->mixer_nid))
1504 return false;
1505 }
1506 for (i = 0; i < cfg->speaker_outs; i++) {
1507 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1508 if (path && is_nid_contained(path, spec->mixer_nid))
1509 return false;
1510 }
1511
1512 return true;
1513}
1514
Takashi Iwaia07a9492013-01-07 16:44:06 +01001515/* fill the empty entries in the dac array for speaker/hp with the
1516 * shared dac pointed by the paths
1517 */
1518static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1519 hda_nid_t *dacs, int *path_idx)
1520{
1521 struct nid_path *path;
1522 int i;
1523
1524 for (i = 0; i < num_outs; i++) {
1525 if (dacs[i])
1526 continue;
1527 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1528 if (!path)
1529 continue;
1530 dacs[i] = path->path[0];
1531 }
1532}
1533
Takashi Iwai352f7f92012-12-19 12:52:06 +01001534/* fill in the dac_nids table from the parsed pin configuration */
1535static int fill_and_eval_dacs(struct hda_codec *codec,
1536 bool fill_hardwired,
1537 bool fill_mio_first)
1538{
1539 struct hda_gen_spec *spec = codec->spec;
1540 struct auto_pin_cfg *cfg = &spec->autocfg;
1541 int i, err, badness;
1542
1543 /* set num_dacs once to full for look_for_dac() */
1544 spec->multiout.num_dacs = cfg->line_outs;
1545 spec->multiout.dac_nids = spec->private_dac_nids;
1546 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1547 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1548 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1549 spec->multi_ios = 0;
1550 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001551
1552 /* clear path indices */
1553 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1554 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1555 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1556 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1557 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001558 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001559 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1560 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1561
Takashi Iwai352f7f92012-12-19 12:52:06 +01001562 badness = 0;
1563
1564 /* fill hard-wired DACs first */
1565 if (fill_hardwired) {
1566 bool mapped;
1567 do {
1568 mapped = map_singles(codec, cfg->line_outs,
1569 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001570 spec->private_dac_nids,
1571 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001572 mapped |= map_singles(codec, cfg->hp_outs,
1573 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001574 spec->multiout.hp_out_nid,
1575 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001576 mapped |= map_singles(codec, cfg->speaker_outs,
1577 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001578 spec->multiout.extra_out_nid,
1579 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001580 if (!spec->no_multi_io &&
1581 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001582 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001583 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001584 if (!err)
1585 mapped = true;
1586 }
1587 } while (mapped);
1588 }
1589
1590 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001591 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001592 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001593
Takashi Iwaida96fb52013-07-29 16:54:36 +02001594 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001595 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1596 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001597 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001598 if (err < 0)
1599 return err;
1600 /* we don't count badness at this stage yet */
1601 }
1602
1603 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1604 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1605 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001606 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001607 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001608 if (err < 0)
1609 return err;
1610 badness += err;
1611 }
1612 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1613 err = try_assign_dacs(codec, cfg->speaker_outs,
1614 cfg->speaker_pins,
1615 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001616 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001617 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001618 if (err < 0)
1619 return err;
1620 badness += err;
1621 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001622 if (!spec->no_multi_io &&
1623 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001624 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001625 if (err < 0)
1626 return err;
1627 badness += err;
1628 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001629
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001630 if (spec->mixer_nid) {
1631 spec->aamix_out_paths[0] =
1632 check_aamix_out_path(codec, spec->out_paths[0]);
1633 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1634 spec->aamix_out_paths[1] =
1635 check_aamix_out_path(codec, spec->hp_paths[0]);
1636 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1637 spec->aamix_out_paths[2] =
1638 check_aamix_out_path(codec, spec->speaker_paths[0]);
1639 }
1640
Takashi Iwaida96fb52013-07-29 16:54:36 +02001641 if (!spec->no_multi_io &&
1642 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001643 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1644 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001645
Takashi Iwaia07a9492013-01-07 16:44:06 +01001646 /* re-count num_dacs and squash invalid entries */
1647 spec->multiout.num_dacs = 0;
1648 for (i = 0; i < cfg->line_outs; i++) {
1649 if (spec->private_dac_nids[i])
1650 spec->multiout.num_dacs++;
1651 else {
1652 memmove(spec->private_dac_nids + i,
1653 spec->private_dac_nids + i + 1,
1654 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1655 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1656 }
1657 }
1658
1659 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001660 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001661
Takashi Iwai352f7f92012-12-19 12:52:06 +01001662 if (spec->multi_ios == 2) {
1663 for (i = 0; i < 2; i++)
1664 spec->private_dac_nids[spec->multiout.num_dacs++] =
1665 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001666 } else if (spec->multi_ios) {
1667 spec->multi_ios = 0;
1668 badness += BAD_MULTI_IO;
1669 }
1670
Takashi Iwai55a63d42013-03-21 17:20:12 +01001671 if (spec->indep_hp && !indep_hp_possible(codec))
1672 badness += BAD_NO_INDEP_HP;
1673
Takashi Iwaia07a9492013-01-07 16:44:06 +01001674 /* re-fill the shared DAC for speaker / headphone */
1675 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1676 refill_shared_dacs(codec, cfg->hp_outs,
1677 spec->multiout.hp_out_nid,
1678 spec->hp_paths);
1679 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1680 refill_shared_dacs(codec, cfg->speaker_outs,
1681 spec->multiout.extra_out_nid,
1682 spec->speaker_paths);
1683
Takashi Iwai352f7f92012-12-19 12:52:06 +01001684 return badness;
1685}
1686
1687#define DEBUG_BADNESS
1688
1689#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001690#define debug_badness(fmt, ...) \
1691 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001692#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001693#define debug_badness(fmt, ...) \
1694 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001695#endif
1696
Takashi Iwaia7694092013-01-21 10:43:18 +01001697#ifdef DEBUG_BADNESS
1698static inline void print_nid_path_idx(struct hda_codec *codec,
1699 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001700{
Takashi Iwaia7694092013-01-21 10:43:18 +01001701 struct nid_path *path;
1702
1703 path = snd_hda_get_path_from_idx(codec, idx);
1704 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001705 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001706}
1707
1708static void debug_show_configs(struct hda_codec *codec,
1709 struct auto_pin_cfg *cfg)
1710{
1711 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001712 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001713 int i;
1714
1715 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001716 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001717 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001718 spec->multiout.dac_nids[0],
1719 spec->multiout.dac_nids[1],
1720 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001721 spec->multiout.dac_nids[3],
1722 lo_type[cfg->line_out_type]);
1723 for (i = 0; i < cfg->line_outs; i++)
1724 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001725 if (spec->multi_ios > 0)
1726 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1727 spec->multi_ios,
1728 spec->multi_io[0].pin, spec->multi_io[1].pin,
1729 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001730 for (i = 0; i < spec->multi_ios; i++)
1731 print_nid_path_idx(codec, " mio",
1732 spec->out_paths[cfg->line_outs + i]);
1733 if (cfg->hp_outs)
1734 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001735 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001736 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001737 spec->multiout.hp_out_nid[0],
1738 spec->multiout.hp_out_nid[1],
1739 spec->multiout.hp_out_nid[2],
1740 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001741 for (i = 0; i < cfg->hp_outs; i++)
1742 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1743 if (cfg->speaker_outs)
1744 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001745 cfg->speaker_pins[0], cfg->speaker_pins[1],
1746 cfg->speaker_pins[2], cfg->speaker_pins[3],
1747 spec->multiout.extra_out_nid[0],
1748 spec->multiout.extra_out_nid[1],
1749 spec->multiout.extra_out_nid[2],
1750 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001751 for (i = 0; i < cfg->speaker_outs; i++)
1752 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1753 for (i = 0; i < 3; i++)
1754 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001755}
Takashi Iwaia7694092013-01-21 10:43:18 +01001756#else
1757#define debug_show_configs(codec, cfg) /* NOP */
1758#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001759
1760/* find all available DACs of the codec */
1761static void fill_all_dac_nids(struct hda_codec *codec)
1762{
1763 struct hda_gen_spec *spec = codec->spec;
1764 int i;
1765 hda_nid_t nid = codec->start_nid;
1766
1767 spec->num_all_dacs = 0;
1768 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1769 for (i = 0; i < codec->num_nodes; i++, nid++) {
1770 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1771 continue;
1772 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001773 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001774 break;
1775 }
1776 spec->all_dacs[spec->num_all_dacs++] = nid;
1777 }
1778}
1779
1780static int parse_output_paths(struct hda_codec *codec)
1781{
1782 struct hda_gen_spec *spec = codec->spec;
1783 struct auto_pin_cfg *cfg = &spec->autocfg;
1784 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001785 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001786 int best_badness = INT_MAX;
1787 int badness;
1788 bool fill_hardwired = true, fill_mio_first = true;
1789 bool best_wired = true, best_mio = true;
1790 bool hp_spk_swapped = false;
1791
Takashi Iwai352f7f92012-12-19 12:52:06 +01001792 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1793 if (!best_cfg)
1794 return -ENOMEM;
1795 *best_cfg = *cfg;
1796
1797 for (;;) {
1798 badness = fill_and_eval_dacs(codec, fill_hardwired,
1799 fill_mio_first);
1800 if (badness < 0) {
1801 kfree(best_cfg);
1802 return badness;
1803 }
1804 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1805 cfg->line_out_type, fill_hardwired, fill_mio_first,
1806 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001807 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001808 if (badness < best_badness) {
1809 best_badness = badness;
1810 *best_cfg = *cfg;
1811 best_wired = fill_hardwired;
1812 best_mio = fill_mio_first;
1813 }
1814 if (!badness)
1815 break;
1816 fill_mio_first = !fill_mio_first;
1817 if (!fill_mio_first)
1818 continue;
1819 fill_hardwired = !fill_hardwired;
1820 if (!fill_hardwired)
1821 continue;
1822 if (hp_spk_swapped)
1823 break;
1824 hp_spk_swapped = true;
1825 if (cfg->speaker_outs > 0 &&
1826 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1827 cfg->hp_outs = cfg->line_outs;
1828 memcpy(cfg->hp_pins, cfg->line_out_pins,
1829 sizeof(cfg->hp_pins));
1830 cfg->line_outs = cfg->speaker_outs;
1831 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1832 sizeof(cfg->speaker_pins));
1833 cfg->speaker_outs = 0;
1834 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1835 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1836 fill_hardwired = true;
1837 continue;
1838 }
1839 if (cfg->hp_outs > 0 &&
1840 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1841 cfg->speaker_outs = cfg->line_outs;
1842 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1843 sizeof(cfg->speaker_pins));
1844 cfg->line_outs = cfg->hp_outs;
1845 memcpy(cfg->line_out_pins, cfg->hp_pins,
1846 sizeof(cfg->hp_pins));
1847 cfg->hp_outs = 0;
1848 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1849 cfg->line_out_type = AUTO_PIN_HP_OUT;
1850 fill_hardwired = true;
1851 continue;
1852 }
1853 break;
1854 }
1855
1856 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001857 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001858 *cfg = *best_cfg;
1859 fill_and_eval_dacs(codec, best_wired, best_mio);
1860 }
1861 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1862 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001863 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001864
1865 if (cfg->line_out_pins[0]) {
1866 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001867 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001868 if (path)
1869 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001870 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001871 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1872 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001873 if (spec->dac_min_mute)
1874 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
1875 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001876 }
1877
Takashi Iwai9314a582013-01-21 10:49:05 +01001878 /* set initial pinctl targets */
1879 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1880 val = PIN_HP;
1881 else
1882 val = PIN_OUT;
1883 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1884 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1885 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1886 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1887 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1888 set_pin_targets(codec, cfg->speaker_outs,
1889 cfg->speaker_pins, val);
1890 }
1891
Takashi Iwai55a63d42013-03-21 17:20:12 +01001892 /* clear indep_hp flag if not available */
1893 if (spec->indep_hp && !indep_hp_possible(codec))
1894 spec->indep_hp = 0;
1895
Takashi Iwai352f7f92012-12-19 12:52:06 +01001896 kfree(best_cfg);
1897 return 0;
1898}
1899
1900/* add playback controls from the parsed DAC table */
1901static int create_multi_out_ctls(struct hda_codec *codec,
1902 const struct auto_pin_cfg *cfg)
1903{
1904 struct hda_gen_spec *spec = codec->spec;
1905 int i, err, noutputs;
1906
1907 noutputs = cfg->line_outs;
1908 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1909 noutputs += spec->multi_ios;
1910
1911 for (i = 0; i < noutputs; i++) {
1912 const char *name;
1913 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001914 struct nid_path *path;
1915
Takashi Iwai196c17662013-01-04 15:01:40 +01001916 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001917 if (!path)
1918 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001919
1920 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001921 if (!name || !strcmp(name, "CLFE")) {
1922 /* Center/LFE */
1923 err = add_vol_ctl(codec, "Center", 0, 1, path);
1924 if (err < 0)
1925 return err;
1926 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1927 if (err < 0)
1928 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001929 } else {
1930 err = add_stereo_vol(codec, name, index, path);
1931 if (err < 0)
1932 return err;
1933 }
1934
1935 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1936 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001937 err = add_sw_ctl(codec, "Center", 0, 1, path);
1938 if (err < 0)
1939 return err;
1940 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1941 if (err < 0)
1942 return err;
1943 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001944 err = add_stereo_sw(codec, name, index, path);
1945 if (err < 0)
1946 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948 }
1949 return 0;
1950}
1951
Takashi Iwaic2c80382013-01-07 10:33:57 +01001952static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001953 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001955 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 int err;
1957
Takashi Iwai196c17662013-01-04 15:01:40 +01001958 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001959 if (!path)
1960 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001961 err = add_stereo_vol(codec, pfx, cidx, path);
1962 if (err < 0)
1963 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001964 err = add_stereo_sw(codec, pfx, cidx, path);
1965 if (err < 0)
1966 return err;
1967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
Takashi Iwai352f7f92012-12-19 12:52:06 +01001970/* add playback controls for speaker and HP outputs */
1971static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001972 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001974 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001975
1976 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001977 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02001978 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01001979 int err, idx = 0;
1980
1981 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1982 name = "Bass Speaker";
1983 else if (num_pins >= 3) {
1984 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001985 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001986 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001987 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001988 name = pfx;
1989 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001991 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001992 if (err < 0)
1993 return err;
1994 }
1995 return 0;
1996}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001997
Takashi Iwai352f7f92012-12-19 12:52:06 +01001998static int create_hp_out_ctls(struct hda_codec *codec)
1999{
2000 struct hda_gen_spec *spec = codec->spec;
2001 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002002 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002003 "Headphone");
2004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Takashi Iwai352f7f92012-12-19 12:52:06 +01002006static int create_speaker_out_ctls(struct hda_codec *codec)
2007{
2008 struct hda_gen_spec *spec = codec->spec;
2009 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002010 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002011 "Speaker");
2012}
2013
2014/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002015 * independent HP controls
2016 */
2017
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002018static void call_hp_automute(struct hda_codec *codec,
2019 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002020static int indep_hp_info(struct snd_kcontrol *kcontrol,
2021 struct snd_ctl_elem_info *uinfo)
2022{
2023 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2024}
2025
2026static int indep_hp_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;
2031 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2032 return 0;
2033}
2034
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002035static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2036 int nomix_path_idx, int mix_path_idx,
2037 int out_type);
2038
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002039static int indep_hp_put(struct snd_kcontrol *kcontrol,
2040 struct snd_ctl_elem_value *ucontrol)
2041{
2042 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2043 struct hda_gen_spec *spec = codec->spec;
2044 unsigned int select = ucontrol->value.enumerated.item[0];
2045 int ret = 0;
2046
2047 mutex_lock(&spec->pcm_mutex);
2048 if (spec->active_streams) {
2049 ret = -EBUSY;
2050 goto unlock;
2051 }
2052
2053 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002054 hda_nid_t *dacp;
2055 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2056 dacp = &spec->private_dac_nids[0];
2057 else
2058 dacp = &spec->multiout.hp_out_nid[0];
2059
2060 /* update HP aamix paths in case it conflicts with indep HP */
2061 if (spec->have_aamix_ctl) {
2062 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2063 update_aamix_paths(codec, spec->aamix_mode,
2064 spec->out_paths[0],
2065 spec->aamix_out_paths[0],
2066 spec->autocfg.line_out_type);
2067 else
2068 update_aamix_paths(codec, spec->aamix_mode,
2069 spec->hp_paths[0],
2070 spec->aamix_out_paths[1],
2071 AUTO_PIN_HP_OUT);
2072 }
2073
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002074 spec->indep_hp_enabled = select;
2075 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002076 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002077 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002078 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002079
Takashi Iwai963afde2013-05-31 15:20:31 +02002080 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002081 ret = 1;
2082 }
2083 unlock:
2084 mutex_unlock(&spec->pcm_mutex);
2085 return ret;
2086}
2087
2088static const struct snd_kcontrol_new indep_hp_ctl = {
2089 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2090 .name = "Independent HP",
2091 .info = indep_hp_info,
2092 .get = indep_hp_get,
2093 .put = indep_hp_put,
2094};
2095
2096
2097static int create_indep_hp_ctls(struct hda_codec *codec)
2098{
2099 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002100 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002101
2102 if (!spec->indep_hp)
2103 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002104 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2105 dac = spec->multiout.dac_nids[0];
2106 else
2107 dac = spec->multiout.hp_out_nid[0];
2108 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002109 spec->indep_hp = 0;
2110 return 0;
2111 }
2112
2113 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002114 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002115 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2116 return -ENOMEM;
2117 return 0;
2118}
2119
2120/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002121 * channel mode enum control
2122 */
2123
2124static int ch_mode_info(struct snd_kcontrol *kcontrol,
2125 struct snd_ctl_elem_info *uinfo)
2126{
2127 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2128 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002129 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002130
2131 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2132 uinfo->count = 1;
2133 uinfo->value.enumerated.items = spec->multi_ios + 1;
2134 if (uinfo->value.enumerated.item > spec->multi_ios)
2135 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002136 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2137 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002138 return 0;
2139}
2140
2141static int ch_mode_get(struct snd_kcontrol *kcontrol,
2142 struct snd_ctl_elem_value *ucontrol)
2143{
2144 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2145 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002146 ucontrol->value.enumerated.item[0] =
2147 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002148 return 0;
2149}
2150
Takashi Iwai196c17662013-01-04 15:01:40 +01002151static inline struct nid_path *
2152get_multiio_path(struct hda_codec *codec, int idx)
2153{
2154 struct hda_gen_spec *spec = codec->spec;
2155 return snd_hda_get_path_from_idx(codec,
2156 spec->out_paths[spec->autocfg.line_outs + idx]);
2157}
2158
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002159static void update_automute_all(struct hda_codec *codec);
2160
Takashi Iwai65033cc2013-04-16 12:31:05 +02002161/* Default value to be passed as aamix argument for snd_hda_activate_path();
2162 * used for output paths
2163 */
2164static bool aamix_default(struct hda_gen_spec *spec)
2165{
2166 return !spec->have_aamix_ctl || spec->aamix_mode;
2167}
2168
Takashi Iwai352f7f92012-12-19 12:52:06 +01002169static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2170{
2171 struct hda_gen_spec *spec = codec->spec;
2172 hda_nid_t nid = spec->multi_io[idx].pin;
2173 struct nid_path *path;
2174
Takashi Iwai196c17662013-01-04 15:01:40 +01002175 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002176 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002178
2179 if (path->active == output)
2180 return 0;
2181
2182 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002183 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002184 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002185 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002186 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002187 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002188 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002189 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002190 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002192
2193 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002194 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002195
Takashi Iwai352f7f92012-12-19 12:52:06 +01002196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
Takashi Iwai352f7f92012-12-19 12:52:06 +01002199static int ch_mode_put(struct snd_kcontrol *kcontrol,
2200 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002202 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2203 struct hda_gen_spec *spec = codec->spec;
2204 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Takashi Iwai352f7f92012-12-19 12:52:06 +01002206 ch = ucontrol->value.enumerated.item[0];
2207 if (ch < 0 || ch > spec->multi_ios)
2208 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002209 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002210 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002211 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002212 for (i = 0; i < spec->multi_ios; i++)
2213 set_multi_io(codec, i, i < ch);
2214 spec->multiout.max_channels = max(spec->ext_channel_count,
2215 spec->const_channel_count);
2216 if (spec->need_dac_fix)
2217 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return 1;
2219}
2220
Takashi Iwai352f7f92012-12-19 12:52:06 +01002221static const struct snd_kcontrol_new channel_mode_enum = {
2222 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2223 .name = "Channel Mode",
2224 .info = ch_mode_info,
2225 .get = ch_mode_get,
2226 .put = ch_mode_put,
2227};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Takashi Iwai352f7f92012-12-19 12:52:06 +01002229static int create_multi_channel_mode(struct hda_codec *codec)
2230{
2231 struct hda_gen_spec *spec = codec->spec;
2232
2233 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002234 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002235 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return 0;
2238}
2239
Takashi Iwai352f7f92012-12-19 12:52:06 +01002240/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002241 * aamix loopback enable/disable switch
2242 */
2243
2244#define loopback_mixing_info indep_hp_info
2245
2246static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2247 struct snd_ctl_elem_value *ucontrol)
2248{
2249 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2250 struct hda_gen_spec *spec = codec->spec;
2251 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2252 return 0;
2253}
2254
2255static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002256 int nomix_path_idx, int mix_path_idx,
2257 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002258{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002259 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002260 struct nid_path *nomix_path, *mix_path;
2261
2262 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2263 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2264 if (!nomix_path || !mix_path)
2265 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002266
2267 /* if HP aamix path is driven from a different DAC and the
2268 * independent HP mode is ON, can't turn on aamix path
2269 */
2270 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2271 mix_path->path[0] != spec->alt_dac_nid)
2272 do_mix = false;
2273
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002274 if (do_mix) {
2275 snd_hda_activate_path(codec, nomix_path, false, true);
2276 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002277 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002278 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002279 snd_hda_activate_path(codec, mix_path, false, false);
2280 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002281 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002282 }
2283}
2284
2285static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2286 struct snd_ctl_elem_value *ucontrol)
2287{
2288 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2289 struct hda_gen_spec *spec = codec->spec;
2290 unsigned int val = ucontrol->value.enumerated.item[0];
2291
2292 if (val == spec->aamix_mode)
2293 return 0;
2294 spec->aamix_mode = val;
2295 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002296 spec->aamix_out_paths[0],
2297 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002298 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002299 spec->aamix_out_paths[1],
2300 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002301 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002302 spec->aamix_out_paths[2],
2303 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002304 return 1;
2305}
2306
2307static const struct snd_kcontrol_new loopback_mixing_enum = {
2308 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2309 .name = "Loopback Mixing",
2310 .info = loopback_mixing_info,
2311 .get = loopback_mixing_get,
2312 .put = loopback_mixing_put,
2313};
2314
2315static int create_loopback_mixing_ctl(struct hda_codec *codec)
2316{
2317 struct hda_gen_spec *spec = codec->spec;
2318
2319 if (!spec->mixer_nid)
2320 return 0;
2321 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2322 spec->aamix_out_paths[2]))
2323 return 0;
2324 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2325 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002326 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002327 return 0;
2328}
2329
2330/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002331 * shared headphone/mic handling
2332 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002333
Takashi Iwai352f7f92012-12-19 12:52:06 +01002334static void call_update_outputs(struct hda_codec *codec);
2335
2336/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002337static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002338{
2339 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002340 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002341 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002342 hda_nid_t pin;
2343
2344 pin = spec->hp_mic_pin;
2345 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2346
2347 if (!force) {
2348 val = snd_hda_codec_get_pin_target(codec, pin);
2349 if (as_mic) {
2350 if (val & PIN_IN)
2351 return;
2352 } else {
2353 if (val & PIN_OUT)
2354 return;
2355 }
2356 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002357
2358 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002359 /* if the HP pin doesn't support VREF and the codec driver gives an
2360 * alternative pin, set up the VREF on that pin instead
2361 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002362 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2363 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2364 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2365 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002366 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002367 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002368 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002369
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002370 if (!spec->hp_mic_jack_modes) {
2371 if (as_mic)
2372 val |= PIN_IN;
2373 else
2374 val = PIN_HP;
2375 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002376 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002377 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002378}
2379
2380/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002381static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002382{
2383 struct hda_gen_spec *spec = codec->spec;
2384 struct auto_pin_cfg *cfg = &spec->autocfg;
2385 unsigned int defcfg;
2386 hda_nid_t nid;
2387
Takashi Iwai967303d2013-02-19 17:12:42 +01002388 if (!spec->hp_mic) {
2389 if (spec->suppress_hp_mic_detect)
2390 return 0;
2391 /* automatic detection: only if no input or a single internal
2392 * input pin is found, try to detect the shared hp/mic
2393 */
2394 if (cfg->num_inputs > 1)
2395 return 0;
2396 else if (cfg->num_inputs == 1) {
2397 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2398 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2399 return 0;
2400 }
2401 }
2402
2403 spec->hp_mic = 0; /* clear once */
2404 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002405 return 0;
2406
Takashi Iwai967303d2013-02-19 17:12:42 +01002407 nid = 0;
2408 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2409 nid = cfg->line_out_pins[0];
2410 else if (cfg->hp_outs > 0)
2411 nid = cfg->hp_pins[0];
2412 if (!nid)
2413 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002414
2415 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2416 return 0; /* no input */
2417
Takashi Iwai967303d2013-02-19 17:12:42 +01002418 cfg->inputs[cfg->num_inputs].pin = nid;
2419 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002420 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002421 cfg->num_inputs++;
2422 spec->hp_mic = 1;
2423 spec->hp_mic_pin = nid;
2424 /* we can't handle auto-mic together with HP-mic */
2425 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002426 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002427 return 0;
2428}
2429
Takashi Iwai978e77e2013-01-10 16:57:58 +01002430/*
2431 * output jack mode
2432 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002433
2434static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2435
2436static const char * const out_jack_texts[] = {
2437 "Line Out", "Headphone Out",
2438};
2439
Takashi Iwai978e77e2013-01-10 16:57:58 +01002440static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2441 struct snd_ctl_elem_info *uinfo)
2442{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002443 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002444}
2445
2446static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2447 struct snd_ctl_elem_value *ucontrol)
2448{
2449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2450 hda_nid_t nid = kcontrol->private_value;
2451 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2452 ucontrol->value.enumerated.item[0] = 1;
2453 else
2454 ucontrol->value.enumerated.item[0] = 0;
2455 return 0;
2456}
2457
2458static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2459 struct snd_ctl_elem_value *ucontrol)
2460{
2461 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2462 hda_nid_t nid = kcontrol->private_value;
2463 unsigned int val;
2464
2465 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2466 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2467 return 0;
2468 snd_hda_set_pin_ctl_cache(codec, nid, val);
2469 return 1;
2470}
2471
2472static const struct snd_kcontrol_new out_jack_mode_enum = {
2473 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2474 .info = out_jack_mode_info,
2475 .get = out_jack_mode_get,
2476 .put = out_jack_mode_put,
2477};
2478
2479static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2480{
2481 struct hda_gen_spec *spec = codec->spec;
2482 int i;
2483
2484 for (i = 0; i < spec->kctls.used; i++) {
2485 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2486 if (!strcmp(kctl->name, name) && kctl->index == idx)
2487 return true;
2488 }
2489 return false;
2490}
2491
2492static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2493 char *name, size_t name_len)
2494{
2495 struct hda_gen_spec *spec = codec->spec;
2496 int idx = 0;
2497
2498 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2499 strlcat(name, " Jack Mode", name_len);
2500
2501 for (; find_kctl_name(codec, name, idx); idx++)
2502 ;
2503}
2504
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002505static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2506{
2507 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002508 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002509 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2510 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2511 return 2;
2512 }
2513 return 1;
2514}
2515
Takashi Iwai978e77e2013-01-10 16:57:58 +01002516static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2517 hda_nid_t *pins)
2518{
2519 struct hda_gen_spec *spec = codec->spec;
2520 int i;
2521
2522 for (i = 0; i < num_pins; i++) {
2523 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002524 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002525 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002526 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002527 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002528 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002529 get_jack_mode_name(codec, pin, name, sizeof(name));
2530 knew = snd_hda_gen_add_kctl(spec, name,
2531 &out_jack_mode_enum);
2532 if (!knew)
2533 return -ENOMEM;
2534 knew->private_value = pin;
2535 }
2536 }
2537
2538 return 0;
2539}
2540
Takashi Iwai294765582013-01-17 09:52:11 +01002541/*
2542 * input jack mode
2543 */
2544
2545/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2546#define NUM_VREFS 6
2547
2548static const char * const vref_texts[NUM_VREFS] = {
2549 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2550 "", "Mic 80pc Bias", "Mic 100pc Bias"
2551};
2552
2553static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2554{
2555 unsigned int pincap;
2556
2557 pincap = snd_hda_query_pin_caps(codec, pin);
2558 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2559 /* filter out unusual vrefs */
2560 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2561 return pincap;
2562}
2563
2564/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2565static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2566{
2567 unsigned int i, n = 0;
2568
2569 for (i = 0; i < NUM_VREFS; i++) {
2570 if (vref_caps & (1 << i)) {
2571 if (n == item_idx)
2572 return i;
2573 n++;
2574 }
2575 }
2576 return 0;
2577}
2578
2579/* convert back from the vref ctl index to the enum item index */
2580static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2581{
2582 unsigned int i, n = 0;
2583
2584 for (i = 0; i < NUM_VREFS; i++) {
2585 if (i == idx)
2586 return n;
2587 if (vref_caps & (1 << i))
2588 n++;
2589 }
2590 return 0;
2591}
2592
2593static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_info *uinfo)
2595{
2596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2597 hda_nid_t nid = kcontrol->private_value;
2598 unsigned int vref_caps = get_vref_caps(codec, nid);
2599
2600 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2601 vref_texts);
2602 /* set the right text */
2603 strcpy(uinfo->value.enumerated.name,
2604 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2605 return 0;
2606}
2607
2608static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2609 struct snd_ctl_elem_value *ucontrol)
2610{
2611 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2612 hda_nid_t nid = kcontrol->private_value;
2613 unsigned int vref_caps = get_vref_caps(codec, nid);
2614 unsigned int idx;
2615
2616 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2617 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2618 return 0;
2619}
2620
2621static int in_jack_mode_put(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 unsigned int vref_caps = get_vref_caps(codec, nid);
2627 unsigned int val, idx;
2628
2629 val = snd_hda_codec_get_pin_target(codec, nid);
2630 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2631 if (idx == ucontrol->value.enumerated.item[0])
2632 return 0;
2633
2634 val &= ~AC_PINCTL_VREFEN;
2635 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2636 snd_hda_set_pin_ctl_cache(codec, nid, val);
2637 return 1;
2638}
2639
2640static const struct snd_kcontrol_new in_jack_mode_enum = {
2641 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2642 .info = in_jack_mode_info,
2643 .get = in_jack_mode_get,
2644 .put = in_jack_mode_put,
2645};
2646
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002647static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2648{
2649 struct hda_gen_spec *spec = codec->spec;
2650 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002651 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002652 nitems = hweight32(get_vref_caps(codec, pin));
2653 return nitems ? nitems : 1;
2654}
2655
Takashi Iwai294765582013-01-17 09:52:11 +01002656static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2657{
2658 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002659 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002660 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002661 unsigned int defcfg;
2662
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002663 if (pin == spec->hp_mic_pin)
2664 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002665
2666 /* no jack mode for fixed pins */
2667 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2668 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2669 return 0;
2670
2671 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002672 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002673 return 0;
2674
2675 get_jack_mode_name(codec, pin, name, sizeof(name));
2676 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2677 if (!knew)
2678 return -ENOMEM;
2679 knew->private_value = pin;
2680 return 0;
2681}
2682
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002683/*
2684 * HP/mic shared jack mode
2685 */
2686static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2687 struct snd_ctl_elem_info *uinfo)
2688{
2689 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2690 hda_nid_t nid = kcontrol->private_value;
2691 int out_jacks = get_out_jack_num_items(codec, nid);
2692 int in_jacks = get_in_jack_num_items(codec, nid);
2693 const char *text = NULL;
2694 int idx;
2695
2696 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2697 uinfo->count = 1;
2698 uinfo->value.enumerated.items = out_jacks + in_jacks;
2699 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2700 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2701 idx = uinfo->value.enumerated.item;
2702 if (idx < out_jacks) {
2703 if (out_jacks > 1)
2704 text = out_jack_texts[idx];
2705 else
2706 text = "Headphone Out";
2707 } else {
2708 idx -= out_jacks;
2709 if (in_jacks > 1) {
2710 unsigned int vref_caps = get_vref_caps(codec, nid);
2711 text = vref_texts[get_vref_idx(vref_caps, idx)];
2712 } else
2713 text = "Mic In";
2714 }
2715
2716 strcpy(uinfo->value.enumerated.name, text);
2717 return 0;
2718}
2719
2720static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2721{
2722 int out_jacks = get_out_jack_num_items(codec, nid);
2723 int in_jacks = get_in_jack_num_items(codec, nid);
2724 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2725 int idx = 0;
2726
2727 if (val & PIN_OUT) {
2728 if (out_jacks > 1 && val == PIN_HP)
2729 idx = 1;
2730 } else if (val & PIN_IN) {
2731 idx = out_jacks;
2732 if (in_jacks > 1) {
2733 unsigned int vref_caps = get_vref_caps(codec, nid);
2734 val &= AC_PINCTL_VREFEN;
2735 idx += cvt_from_vref_idx(vref_caps, val);
2736 }
2737 }
2738 return idx;
2739}
2740
2741static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2742 struct snd_ctl_elem_value *ucontrol)
2743{
2744 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2745 hda_nid_t nid = kcontrol->private_value;
2746 ucontrol->value.enumerated.item[0] =
2747 get_cur_hp_mic_jack_mode(codec, nid);
2748 return 0;
2749}
2750
2751static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2752 struct snd_ctl_elem_value *ucontrol)
2753{
2754 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2755 hda_nid_t nid = kcontrol->private_value;
2756 int out_jacks = get_out_jack_num_items(codec, nid);
2757 int in_jacks = get_in_jack_num_items(codec, nid);
2758 unsigned int val, oldval, idx;
2759
2760 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2761 idx = ucontrol->value.enumerated.item[0];
2762 if (oldval == idx)
2763 return 0;
2764
2765 if (idx < out_jacks) {
2766 if (out_jacks > 1)
2767 val = idx ? PIN_HP : PIN_OUT;
2768 else
2769 val = PIN_HP;
2770 } else {
2771 idx -= out_jacks;
2772 if (in_jacks > 1) {
2773 unsigned int vref_caps = get_vref_caps(codec, nid);
2774 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002775 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2776 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002777 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002778 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002779 }
2780 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002781 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002782
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002783 return 1;
2784}
2785
2786static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2787 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2788 .info = hp_mic_jack_mode_info,
2789 .get = hp_mic_jack_mode_get,
2790 .put = hp_mic_jack_mode_put,
2791};
2792
2793static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2794{
2795 struct hda_gen_spec *spec = codec->spec;
2796 struct snd_kcontrol_new *knew;
2797
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002798 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2799 &hp_mic_jack_mode_enum);
2800 if (!knew)
2801 return -ENOMEM;
2802 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002803 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002804 return 0;
2805}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002806
2807/*
2808 * Parse input paths
2809 */
2810
Takashi Iwai352f7f92012-12-19 12:52:06 +01002811/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002812static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002813{
2814 struct hda_amp_list *list;
2815
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002816 list = snd_array_new(&spec->loopback_list);
2817 if (!list)
2818 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002819 list->nid = mix;
2820 list->dir = HDA_INPUT;
2821 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002822 spec->loopback.amplist = spec->loopback_list.list;
2823 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002824}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002825
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002826/* return true if either a volume or a mute amp is found for the given
2827 * aamix path; the amp has to be either in the mixer node or its direct leaf
2828 */
2829static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2830 hda_nid_t pin, unsigned int *mix_val,
2831 unsigned int *mute_val)
2832{
2833 int idx, num_conns;
2834 const hda_nid_t *list;
2835 hda_nid_t nid;
2836
2837 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2838 if (idx < 0)
2839 return false;
2840
2841 *mix_val = *mute_val = 0;
2842 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2843 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2844 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2845 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2846 if (*mix_val && *mute_val)
2847 return true;
2848
2849 /* check leaf node */
2850 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2851 if (num_conns < idx)
2852 return false;
2853 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01002854 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2855 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002856 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01002857 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2858 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002859 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2860
2861 return *mix_val || *mute_val;
2862}
2863
Takashi Iwai352f7f92012-12-19 12:52:06 +01002864/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002865static int new_analog_input(struct hda_codec *codec, int input_idx,
2866 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002867 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002869 struct hda_gen_spec *spec = codec->spec;
2870 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002871 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002872 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002874 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2875 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002876
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002877 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002878 if (!path)
2879 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002880 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002881 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002882
2883 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002884 if (mix_val) {
2885 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002886 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002888 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
2890
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002891 if (mute_val) {
2892 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002893 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002895 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 }
2897
Takashi Iwai352f7f92012-12-19 12:52:06 +01002898 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002899 err = add_loopback_list(spec, mix_nid, idx);
2900 if (err < 0)
2901 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002902
2903 if (spec->mixer_nid != spec->mixer_merge_nid &&
2904 !spec->loopback_merge_path) {
2905 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2906 spec->mixer_merge_nid, 0);
2907 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002908 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002909 path->active = true;
2910 spec->loopback_merge_path =
2911 snd_hda_get_path_idx(codec, path);
2912 }
2913 }
2914
Takashi Iwai352f7f92012-12-19 12:52:06 +01002915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916}
2917
Takashi Iwai352f7f92012-12-19 12:52:06 +01002918static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002920 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2921 return (pincap & AC_PINCAP_IN) != 0;
2922}
2923
2924/* Parse the codec tree and retrieve ADCs */
2925static int fill_adc_nids(struct hda_codec *codec)
2926{
2927 struct hda_gen_spec *spec = codec->spec;
2928 hda_nid_t nid;
2929 hda_nid_t *adc_nids = spec->adc_nids;
2930 int max_nums = ARRAY_SIZE(spec->adc_nids);
2931 int i, nums = 0;
2932
2933 nid = codec->start_nid;
2934 for (i = 0; i < codec->num_nodes; i++, nid++) {
2935 unsigned int caps = get_wcaps(codec, nid);
2936 int type = get_wcaps_type(caps);
2937
2938 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2939 continue;
2940 adc_nids[nums] = nid;
2941 if (++nums >= max_nums)
2942 break;
2943 }
2944 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002945
2946 /* copy the detected ADCs to all_adcs[] */
2947 spec->num_all_adcs = nums;
2948 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2949
Takashi Iwai352f7f92012-12-19 12:52:06 +01002950 return nums;
2951}
2952
2953/* filter out invalid adc_nids that don't give all active input pins;
2954 * if needed, check whether dynamic ADC-switching is available
2955 */
2956static int check_dyn_adc_switch(struct hda_codec *codec)
2957{
2958 struct hda_gen_spec *spec = codec->spec;
2959 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002960 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002961 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002962
Takashi Iwai352f7f92012-12-19 12:52:06 +01002963 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002964 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002965 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002966 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002967 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002968 break;
2969 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002970 if (i >= imux->num_items) {
2971 ok_bits |= (1 << n);
2972 nums++;
2973 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002974 }
2975
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002976 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002977 /* check whether ADC-switch is possible */
2978 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002979 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002980 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002981 spec->dyn_adc_idx[i] = n;
2982 break;
2983 }
2984 }
2985 }
2986
Takashi Iwai4e76a882014-02-25 12:21:03 +01002987 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01002988 spec->dyn_adc_switch = 1;
2989 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002990 /* shrink the invalid adcs and input paths */
2991 nums = 0;
2992 for (n = 0; n < spec->num_adc_nids; n++) {
2993 if (!(ok_bits & (1 << n)))
2994 continue;
2995 if (n != nums) {
2996 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002997 for (i = 0; i < imux->num_items; i++) {
2998 invalidate_nid_path(codec,
2999 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003000 spec->input_paths[i][nums] =
3001 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003002 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003003 }
3004 nums++;
3005 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003006 spec->num_adc_nids = nums;
3007 }
3008
Takashi Iwai967303d2013-02-19 17:12:42 +01003009 if (imux->num_items == 1 ||
3010 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003011 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003012 spec->num_adc_nids = 1; /* reduce to a single ADC */
3013 }
3014
3015 /* single index for individual volumes ctls */
3016 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3017 spec->num_adc_nids = 1;
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 return 0;
3020}
3021
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003022/* parse capture source paths from the given pin and create imux items */
3023static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003024 int cfg_idx, int num_adcs,
3025 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003026{
3027 struct hda_gen_spec *spec = codec->spec;
3028 struct hda_input_mux *imux = &spec->input_mux;
3029 int imux_idx = imux->num_items;
3030 bool imux_added = false;
3031 int c;
3032
3033 for (c = 0; c < num_adcs; c++) {
3034 struct nid_path *path;
3035 hda_nid_t adc = spec->adc_nids[c];
3036
3037 if (!is_reachable_path(codec, pin, adc))
3038 continue;
3039 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3040 if (!path)
3041 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003042 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003043 spec->input_paths[imux_idx][c] =
3044 snd_hda_get_path_idx(codec, path);
3045
3046 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003047 if (spec->hp_mic_pin == pin)
3048 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003049 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003050 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003051 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003052 if (spec->dyn_adc_switch)
3053 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003054 }
3055 }
3056
3057 return 0;
3058}
3059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003061 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003063
Takashi Iwaic9700422013-01-18 10:17:30 +01003064/* fill the label for each input at first */
3065static int fill_input_pin_labels(struct hda_codec *codec)
3066{
3067 struct hda_gen_spec *spec = codec->spec;
3068 const struct auto_pin_cfg *cfg = &spec->autocfg;
3069 int i;
3070
3071 for (i = 0; i < cfg->num_inputs; i++) {
3072 hda_nid_t pin = cfg->inputs[i].pin;
3073 const char *label;
3074 int j, idx;
3075
3076 if (!is_input_pin(codec, pin))
3077 continue;
3078
3079 label = hda_get_autocfg_input_label(codec, cfg, i);
3080 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003081 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003082 if (spec->input_labels[j] &&
3083 !strcmp(spec->input_labels[j], label)) {
3084 idx = spec->input_label_idxs[j] + 1;
3085 break;
3086 }
3087 }
3088
3089 spec->input_labels[i] = label;
3090 spec->input_label_idxs[i] = idx;
3091 }
3092
3093 return 0;
3094}
3095
Takashi Iwai9dba2052013-01-18 10:01:15 +01003096#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3097
Takashi Iwai352f7f92012-12-19 12:52:06 +01003098static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003099{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003100 struct hda_gen_spec *spec = codec->spec;
3101 const struct auto_pin_cfg *cfg = &spec->autocfg;
3102 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003103 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003104 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003105 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003106
Takashi Iwai352f7f92012-12-19 12:52:06 +01003107 num_adcs = fill_adc_nids(codec);
3108 if (num_adcs < 0)
3109 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003110
Takashi Iwaic9700422013-01-18 10:17:30 +01003111 err = fill_input_pin_labels(codec);
3112 if (err < 0)
3113 return err;
3114
Takashi Iwai352f7f92012-12-19 12:52:06 +01003115 for (i = 0; i < cfg->num_inputs; i++) {
3116 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Takashi Iwai352f7f92012-12-19 12:52:06 +01003118 pin = cfg->inputs[i].pin;
3119 if (!is_input_pin(codec, pin))
3120 continue;
3121
Takashi Iwai2c12c302013-01-10 09:33:29 +01003122 val = PIN_IN;
3123 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3124 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003125 if (pin != spec->hp_mic_pin)
3126 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003127
Takashi Iwai352f7f92012-12-19 12:52:06 +01003128 if (mixer) {
3129 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003130 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003131 spec->input_labels[i],
3132 spec->input_label_idxs[i],
3133 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003134 if (err < 0)
3135 return err;
3136 }
3137 }
3138
Takashi Iwaic9700422013-01-18 10:17:30 +01003139 err = parse_capture_source(codec, pin, i, num_adcs,
3140 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003141 if (err < 0)
3142 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003143
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003144 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003145 err = create_in_jack_mode(codec, pin);
3146 if (err < 0)
3147 return err;
3148 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003149 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003150
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003151 /* add stereo mix when explicitly enabled via hint */
3152 if (mixer && spec->add_stereo_mix_input &&
3153 snd_hda_get_bool_hint(codec, "add_stereo_mix_input") > 0) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003154 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003155 "Stereo Mix", 0);
3156 if (err < 0)
3157 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003158 }
3159
3160 return 0;
3161}
3162
3163
3164/*
3165 * input source mux
3166 */
3167
Takashi Iwaic697b712013-01-07 17:09:26 +01003168/* get the input path specified by the given adc and imux indices */
3169static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003170{
3171 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003172 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3173 snd_BUG();
3174 return NULL;
3175 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003176 if (spec->dyn_adc_switch)
3177 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003178 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003179 snd_BUG();
3180 return NULL;
3181 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003182 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003183}
3184
3185static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3186 unsigned int idx);
3187
3188static int mux_enum_info(struct snd_kcontrol *kcontrol,
3189 struct snd_ctl_elem_info *uinfo)
3190{
3191 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3192 struct hda_gen_spec *spec = codec->spec;
3193 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3194}
3195
3196static int mux_enum_get(struct snd_kcontrol *kcontrol,
3197 struct snd_ctl_elem_value *ucontrol)
3198{
3199 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3200 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003201 /* the ctls are created at once with multiple counts */
3202 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003203
3204 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3205 return 0;
3206}
3207
3208static int mux_enum_put(struct snd_kcontrol *kcontrol,
3209 struct snd_ctl_elem_value *ucontrol)
3210{
3211 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003212 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003213 return mux_select(codec, adc_idx,
3214 ucontrol->value.enumerated.item[0]);
3215}
3216
Takashi Iwai352f7f92012-12-19 12:52:06 +01003217static const struct snd_kcontrol_new cap_src_temp = {
3218 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3219 .name = "Input Source",
3220 .info = mux_enum_info,
3221 .get = mux_enum_get,
3222 .put = mux_enum_put,
3223};
3224
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003225/*
3226 * capture volume and capture switch ctls
3227 */
3228
Takashi Iwai352f7f92012-12-19 12:52:06 +01003229typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3230 struct snd_ctl_elem_value *ucontrol);
3231
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003232/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003233static int cap_put_caller(struct snd_kcontrol *kcontrol,
3234 struct snd_ctl_elem_value *ucontrol,
3235 put_call_t func, int type)
3236{
3237 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3238 struct hda_gen_spec *spec = codec->spec;
3239 const struct hda_input_mux *imux;
3240 struct nid_path *path;
3241 int i, adc_idx, err = 0;
3242
3243 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003244 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003245 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003246 /* we use the cache-only update at first since multiple input paths
3247 * may shared the same amp; by updating only caches, the redundant
3248 * writes to hardware can be reduced.
3249 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003250 codec->cached_write = 1;
3251 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003252 path = get_input_path(codec, adc_idx, i);
3253 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003254 continue;
3255 kcontrol->private_value = path->ctls[type];
3256 err = func(kcontrol, ucontrol);
3257 if (err < 0)
3258 goto error;
3259 }
3260 error:
3261 codec->cached_write = 0;
3262 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003263 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003264 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003265 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003266 return err;
3267}
3268
3269/* capture volume ctl callbacks */
3270#define cap_vol_info snd_hda_mixer_amp_volume_info
3271#define cap_vol_get snd_hda_mixer_amp_volume_get
3272#define cap_vol_tlv snd_hda_mixer_amp_tlv
3273
3274static int cap_vol_put(struct snd_kcontrol *kcontrol,
3275 struct snd_ctl_elem_value *ucontrol)
3276{
3277 return cap_put_caller(kcontrol, ucontrol,
3278 snd_hda_mixer_amp_volume_put,
3279 NID_PATH_VOL_CTL);
3280}
3281
3282static const struct snd_kcontrol_new cap_vol_temp = {
3283 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3284 .name = "Capture Volume",
3285 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3286 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3287 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3288 .info = cap_vol_info,
3289 .get = cap_vol_get,
3290 .put = cap_vol_put,
3291 .tlv = { .c = cap_vol_tlv },
3292};
3293
3294/* capture switch ctl callbacks */
3295#define cap_sw_info snd_ctl_boolean_stereo_info
3296#define cap_sw_get snd_hda_mixer_amp_switch_get
3297
3298static int cap_sw_put(struct snd_kcontrol *kcontrol,
3299 struct snd_ctl_elem_value *ucontrol)
3300{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003301 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003302 snd_hda_mixer_amp_switch_put,
3303 NID_PATH_MUTE_CTL);
3304}
3305
3306static const struct snd_kcontrol_new cap_sw_temp = {
3307 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3308 .name = "Capture Switch",
3309 .info = cap_sw_info,
3310 .get = cap_sw_get,
3311 .put = cap_sw_put,
3312};
3313
3314static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3315{
3316 hda_nid_t nid;
3317 int i, depth;
3318
3319 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3320 for (depth = 0; depth < 3; depth++) {
3321 if (depth >= path->depth)
3322 return -EINVAL;
3323 i = path->depth - depth - 1;
3324 nid = path->path[i];
3325 if (!path->ctls[NID_PATH_VOL_CTL]) {
3326 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3327 path->ctls[NID_PATH_VOL_CTL] =
3328 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3329 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3330 int idx = path->idx[i];
3331 if (!depth && codec->single_adc_amp)
3332 idx = 0;
3333 path->ctls[NID_PATH_VOL_CTL] =
3334 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3335 }
3336 }
3337 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3338 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3339 path->ctls[NID_PATH_MUTE_CTL] =
3340 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3341 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3342 int idx = path->idx[i];
3343 if (!depth && codec->single_adc_amp)
3344 idx = 0;
3345 path->ctls[NID_PATH_MUTE_CTL] =
3346 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3347 }
3348 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 return 0;
3351}
3352
Takashi Iwai352f7f92012-12-19 12:52:06 +01003353static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003355 struct hda_gen_spec *spec = codec->spec;
3356 struct auto_pin_cfg *cfg = &spec->autocfg;
3357 unsigned int val;
3358 int i;
3359
3360 if (!spec->inv_dmic_split)
3361 return false;
3362 for (i = 0; i < cfg->num_inputs; i++) {
3363 if (cfg->inputs[i].pin != nid)
3364 continue;
3365 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3366 return false;
3367 val = snd_hda_codec_get_pincfg(codec, nid);
3368 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3369 }
3370 return false;
3371}
3372
Takashi Iwaia90229e2013-01-18 14:10:00 +01003373/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003374static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3375 struct snd_ctl_elem_value *ucontrol)
3376{
3377 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3378 struct hda_gen_spec *spec = codec->spec;
3379 int ret;
3380
3381 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3382 if (ret < 0)
3383 return ret;
3384
Takashi Iwaia90229e2013-01-18 14:10:00 +01003385 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003386 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003387
3388 return ret;
3389}
3390
Takashi Iwai352f7f92012-12-19 12:52:06 +01003391static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3392 int idx, bool is_switch, unsigned int ctl,
3393 bool inv_dmic)
3394{
3395 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003396 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003397 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3398 const char *sfx = is_switch ? "Switch" : "Volume";
3399 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003400 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003401
3402 if (!ctl)
3403 return 0;
3404
3405 if (label)
3406 snprintf(tmpname, sizeof(tmpname),
3407 "%s Capture %s", label, sfx);
3408 else
3409 snprintf(tmpname, sizeof(tmpname),
3410 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003411 knew = add_control(spec, type, tmpname, idx,
3412 amp_val_replace_channels(ctl, chs));
3413 if (!knew)
3414 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003415 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003416 knew->put = cap_single_sw_put;
3417 if (!inv_dmic)
3418 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003419
3420 /* Make independent right kcontrol */
3421 if (label)
3422 snprintf(tmpname, sizeof(tmpname),
3423 "Inverted %s Capture %s", label, sfx);
3424 else
3425 snprintf(tmpname, sizeof(tmpname),
3426 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003427 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003428 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003429 if (!knew)
3430 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003431 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003432 knew->put = cap_single_sw_put;
3433 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003434}
3435
3436/* create single (and simple) capture volume and switch controls */
3437static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3438 unsigned int vol_ctl, unsigned int sw_ctl,
3439 bool inv_dmic)
3440{
3441 int err;
3442 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3443 if (err < 0)
3444 return err;
3445 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3446 if (err < 0)
3447 return err;
3448 return 0;
3449}
3450
3451/* create bound capture volume and switch controls */
3452static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3453 unsigned int vol_ctl, unsigned int sw_ctl)
3454{
3455 struct hda_gen_spec *spec = codec->spec;
3456 struct snd_kcontrol_new *knew;
3457
3458 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003459 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003460 if (!knew)
3461 return -ENOMEM;
3462 knew->index = idx;
3463 knew->private_value = vol_ctl;
3464 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3465 }
3466 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003467 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003468 if (!knew)
3469 return -ENOMEM;
3470 knew->index = idx;
3471 knew->private_value = sw_ctl;
3472 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3473 }
3474 return 0;
3475}
3476
3477/* return the vol ctl when used first in the imux list */
3478static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3479{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003480 struct nid_path *path;
3481 unsigned int ctl;
3482 int i;
3483
Takashi Iwaic697b712013-01-07 17:09:26 +01003484 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003485 if (!path)
3486 return 0;
3487 ctl = path->ctls[type];
3488 if (!ctl)
3489 return 0;
3490 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003491 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003492 if (path && path->ctls[type] == ctl)
3493 return 0;
3494 }
3495 return ctl;
3496}
3497
3498/* create individual capture volume and switch controls per input */
3499static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3500{
3501 struct hda_gen_spec *spec = codec->spec;
3502 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003503 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003504
3505 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003506 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003507 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003508
Takashi Iwaic9700422013-01-18 10:17:30 +01003509 idx = imux->items[i].index;
3510 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003511 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003512 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3513
3514 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003515 err = add_single_cap_ctl(codec,
3516 spec->input_labels[idx],
3517 spec->input_label_idxs[idx],
3518 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003519 get_first_cap_ctl(codec, i, type),
3520 inv_dmic);
3521 if (err < 0)
3522 return err;
3523 }
3524 }
3525 return 0;
3526}
3527
3528static int create_capture_mixers(struct hda_codec *codec)
3529{
3530 struct hda_gen_spec *spec = codec->spec;
3531 struct hda_input_mux *imux = &spec->input_mux;
3532 int i, n, nums, err;
3533
3534 if (spec->dyn_adc_switch)
3535 nums = 1;
3536 else
3537 nums = spec->num_adc_nids;
3538
3539 if (!spec->auto_mic && imux->num_items > 1) {
3540 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003541 const char *name;
3542 name = nums > 1 ? "Input Source" : "Capture Source";
3543 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003544 if (!knew)
3545 return -ENOMEM;
3546 knew->count = nums;
3547 }
3548
3549 for (n = 0; n < nums; n++) {
3550 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003551 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003552 bool inv_dmic = false;
3553 int vol, sw;
3554
3555 vol = sw = 0;
3556 for (i = 0; i < imux->num_items; i++) {
3557 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003558 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003559 if (!path)
3560 continue;
3561 parse_capvol_in_path(codec, path);
3562 if (!vol)
3563 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003564 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003565 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003566 if (!same_amp_caps(codec, vol,
3567 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3568 multi_cap_vol = true;
3569 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003570 if (!sw)
3571 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003572 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003573 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003574 if (!same_amp_caps(codec, sw,
3575 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3576 multi_cap_vol = true;
3577 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003578 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3579 inv_dmic = true;
3580 }
3581
3582 if (!multi)
3583 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3584 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003585 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003586 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3587 else
3588 err = create_multi_cap_vol_ctl(codec);
3589 if (err < 0)
3590 return err;
3591 }
3592
3593 return 0;
3594}
3595
3596/*
3597 * add mic boosts if needed
3598 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003599
3600/* check whether the given amp is feasible as a boost volume */
3601static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3602 int dir, int idx)
3603{
3604 unsigned int step;
3605
3606 if (!nid_has_volume(codec, nid, dir) ||
3607 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3608 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3609 return false;
3610
3611 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3612 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3613 if (step < 0x20)
3614 return false;
3615 return true;
3616}
3617
3618/* look for a boost amp in a widget close to the pin */
3619static unsigned int look_for_boost_amp(struct hda_codec *codec,
3620 struct nid_path *path)
3621{
3622 unsigned int val = 0;
3623 hda_nid_t nid;
3624 int depth;
3625
3626 for (depth = 0; depth < 3; depth++) {
3627 if (depth >= path->depth - 1)
3628 break;
3629 nid = path->path[depth];
3630 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3631 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3632 break;
3633 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3634 path->idx[depth])) {
3635 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3636 HDA_INPUT);
3637 break;
3638 }
3639 }
3640
3641 return val;
3642}
3643
Takashi Iwai352f7f92012-12-19 12:52:06 +01003644static int parse_mic_boost(struct hda_codec *codec)
3645{
3646 struct hda_gen_spec *spec = codec->spec;
3647 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003648 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003649 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003650
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003651 if (!spec->num_adc_nids)
3652 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003653
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003654 for (i = 0; i < imux->num_items; i++) {
3655 struct nid_path *path;
3656 unsigned int val;
3657 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003658 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003659
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003660 idx = imux->items[i].index;
3661 if (idx >= imux->num_items)
3662 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003663
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003664 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003665 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003666 continue;
3667
3668 path = get_input_path(codec, 0, i);
3669 if (!path)
3670 continue;
3671
3672 val = look_for_boost_amp(codec, path);
3673 if (!val)
3674 continue;
3675
3676 /* create a boost control */
3677 snprintf(boost_label, sizeof(boost_label),
3678 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003679 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3680 spec->input_label_idxs[idx], val))
3681 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003682
3683 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003684 }
3685 return 0;
3686}
3687
3688/*
3689 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3690 */
3691static void parse_digital(struct hda_codec *codec)
3692{
3693 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003694 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003695 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003696 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003697
3698 /* support multiple SPDIFs; the secondary is set up as a slave */
3699 nums = 0;
3700 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003701 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003702 dig_nid = look_for_dac(codec, pin, true);
3703 if (!dig_nid)
3704 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003705 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003706 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003707 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003708 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003709 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003710 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003711 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003712 if (!nums) {
3713 spec->multiout.dig_out_nid = dig_nid;
3714 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3715 } else {
3716 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3717 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd5764222014-05-14 17:18:31 +03003718 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003719 spec->slave_dig_outs[nums - 1] = dig_nid;
3720 }
3721 nums++;
3722 }
3723
3724 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003725 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003726 dig_nid = codec->start_nid;
3727 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003728 unsigned int wcaps = get_wcaps(codec, dig_nid);
3729 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3730 continue;
3731 if (!(wcaps & AC_WCAP_DIGITAL))
3732 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003733 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003734 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003735 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003736 path->active = true;
3737 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003738 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003739 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003740 break;
3741 }
3742 }
3743 }
3744}
3745
3746
3747/*
3748 * input MUX handling
3749 */
3750
3751static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3752
3753/* select the given imux item; either unmute exclusively or select the route */
3754static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3755 unsigned int idx)
3756{
3757 struct hda_gen_spec *spec = codec->spec;
3758 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003759 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003760
3761 imux = &spec->input_mux;
3762 if (!imux->num_items)
3763 return 0;
3764
3765 if (idx >= imux->num_items)
3766 idx = imux->num_items - 1;
3767 if (spec->cur_mux[adc_idx] == idx)
3768 return 0;
3769
Takashi Iwai55196ff2013-01-24 17:32:56 +01003770 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3771 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003772 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003773 if (old_path->active)
3774 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003775
3776 spec->cur_mux[adc_idx] = idx;
3777
Takashi Iwai967303d2013-02-19 17:12:42 +01003778 if (spec->hp_mic)
3779 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003780
3781 if (spec->dyn_adc_switch)
3782 dyn_adc_pcm_resetup(codec, idx);
3783
Takashi Iwaic697b712013-01-07 17:09:26 +01003784 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003785 if (!path)
3786 return 0;
3787 if (path->active)
3788 return 0;
3789 snd_hda_activate_path(codec, path, true, false);
3790 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003791 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003792 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003793 return 1;
3794}
3795
3796
3797/*
3798 * Jack detections for HP auto-mute and mic-switch
3799 */
3800
3801/* check each pin in the given array; returns true if any of them is plugged */
3802static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3803{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003804 int i;
3805 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003806
3807 for (i = 0; i < num_pins; i++) {
3808 hda_nid_t nid = pins[i];
3809 if (!nid)
3810 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003811 /* don't detect pins retasked as inputs */
3812 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3813 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003814 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3815 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003816 }
3817 return present;
3818}
3819
3820/* standard HP/line-out auto-mute helper */
3821static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003822 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003823{
3824 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003825 int i;
3826
3827 for (i = 0; i < num_pins; i++) {
3828 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003829 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003830 if (!nid)
3831 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003832
3833 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003834 struct nid_path *path;
3835 hda_nid_t mute_nid;
3836
3837 path = snd_hda_get_path_from_idx(codec, paths[i]);
3838 if (!path)
3839 continue;
3840 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3841 if (!mute_nid)
3842 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003843 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003844 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003845 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003846 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003847 set_pin_eapd(codec, nid, !mute);
3848 continue;
3849 }
3850
Takashi Iwai967303d2013-02-19 17:12:42 +01003851 oldval = snd_hda_codec_get_pin_target(codec, nid);
3852 if (oldval & PIN_IN)
3853 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003854 /* don't reset VREF value in case it's controlling
3855 * the amp (see alc861_fixup_asus_amp_vref_0f())
3856 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003857 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003858 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003859 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003860 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003861 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003862 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003863 /* here we call update_pin_ctl() so that the pinctl is changed
3864 * without changing the pinctl target value;
3865 * the original target value will be still referred at the
3866 * init / resume again
3867 */
3868 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003869 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003870 }
3871}
3872
3873/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003874void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003875{
3876 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003877 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003878 int on;
3879
3880 /* Control HP pins/amps depending on master_mute state;
3881 * in general, HP pins/amps control should be enabled in all cases,
3882 * but currently set only for master_mute, just to be safe
3883 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003884 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3885 paths = spec->out_paths;
3886 else
3887 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01003888 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003889 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003890
3891 if (!spec->automute_speaker)
3892 on = 0;
3893 else
3894 on = spec->hp_jack_present | spec->line_jack_present;
3895 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003896 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003897 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3898 paths = spec->out_paths;
3899 else
3900 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003901 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003902 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003903
3904 /* toggle line-out mutes if needed, too */
3905 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3906 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3907 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3908 return;
3909 if (!spec->automute_lo)
3910 on = 0;
3911 else
3912 on = spec->hp_jack_present;
3913 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003914 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003915 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003916 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003917 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003918}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003919EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003920
3921static void call_update_outputs(struct hda_codec *codec)
3922{
3923 struct hda_gen_spec *spec = codec->spec;
3924 if (spec->automute_hook)
3925 spec->automute_hook(codec);
3926 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003927 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003928
3929 /* sync the whole vmaster slaves to reflect the new auto-mute status */
3930 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
3931 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003932}
3933
3934/* standard HP-automute helper */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02003935void snd_hda_gen_hp_automute(struct hda_codec *codec,
3936 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003937{
3938 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003939 hda_nid_t *pins = spec->autocfg.hp_pins;
3940 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003941
Takashi Iwai92603c52013-01-22 07:46:31 +01003942 /* No detection for the first HP jack during indep-HP mode */
3943 if (spec->indep_hp_enabled) {
3944 pins++;
3945 num_pins--;
3946 }
3947
3948 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003949 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3950 return;
3951 call_update_outputs(codec);
3952}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003953EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003954
3955/* standard line-out-automute helper */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02003956void snd_hda_gen_line_automute(struct hda_codec *codec,
3957 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003958{
3959 struct hda_gen_spec *spec = codec->spec;
3960
3961 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3962 return;
3963 /* check LO jack only when it's different from HP */
3964 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3965 return;
3966
3967 spec->line_jack_present =
3968 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3969 spec->autocfg.line_out_pins);
3970 if (!spec->automute_speaker || !spec->detect_lo)
3971 return;
3972 call_update_outputs(codec);
3973}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003974EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003975
3976/* standard mic auto-switch helper */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02003977void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
3978 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003979{
3980 struct hda_gen_spec *spec = codec->spec;
3981 int i;
3982
3983 if (!spec->auto_mic)
3984 return;
3985
3986 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003987 hda_nid_t pin = spec->am_entry[i].pin;
3988 /* don't detect pins retasked as outputs */
3989 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3990 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003991 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003992 mux_select(codec, 0, spec->am_entry[i].idx);
3993 return;
3994 }
3995 }
3996 mux_select(codec, 0, spec->am_entry[0].idx);
3997}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003998EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003999
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004000/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004001static void call_hp_automute(struct hda_codec *codec,
4002 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004003{
4004 struct hda_gen_spec *spec = codec->spec;
4005 if (spec->hp_automute_hook)
4006 spec->hp_automute_hook(codec, jack);
4007 else
4008 snd_hda_gen_hp_automute(codec, jack);
4009}
4010
4011static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004012 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004013{
4014 struct hda_gen_spec *spec = codec->spec;
4015 if (spec->line_automute_hook)
4016 spec->line_automute_hook(codec, jack);
4017 else
4018 snd_hda_gen_line_automute(codec, jack);
4019}
4020
4021static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004022 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004023{
4024 struct hda_gen_spec *spec = codec->spec;
4025 if (spec->mic_autoswitch_hook)
4026 spec->mic_autoswitch_hook(codec, jack);
4027 else
4028 snd_hda_gen_mic_autoswitch(codec, jack);
4029}
4030
Takashi Iwai963afde2013-05-31 15:20:31 +02004031/* update jack retasking */
4032static void update_automute_all(struct hda_codec *codec)
4033{
4034 call_hp_automute(codec, NULL);
4035 call_line_automute(codec, NULL);
4036 call_mic_autoswitch(codec, NULL);
4037}
4038
Takashi Iwai352f7f92012-12-19 12:52:06 +01004039/*
4040 * Auto-Mute mode mixer enum support
4041 */
4042static int automute_mode_info(struct snd_kcontrol *kcontrol,
4043 struct snd_ctl_elem_info *uinfo)
4044{
4045 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4046 struct hda_gen_spec *spec = codec->spec;
4047 static const char * const texts3[] = {
4048 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004049 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
Takashi Iwai352f7f92012-12-19 12:52:06 +01004051 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4052 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4053 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4054}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
Takashi Iwai352f7f92012-12-19 12:52:06 +01004056static int automute_mode_get(struct snd_kcontrol *kcontrol,
4057 struct snd_ctl_elem_value *ucontrol)
4058{
4059 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4060 struct hda_gen_spec *spec = codec->spec;
4061 unsigned int val = 0;
4062 if (spec->automute_speaker)
4063 val++;
4064 if (spec->automute_lo)
4065 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004066
Takashi Iwai352f7f92012-12-19 12:52:06 +01004067 ucontrol->value.enumerated.item[0] = val;
4068 return 0;
4069}
4070
4071static int automute_mode_put(struct snd_kcontrol *kcontrol,
4072 struct snd_ctl_elem_value *ucontrol)
4073{
4074 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4075 struct hda_gen_spec *spec = codec->spec;
4076
4077 switch (ucontrol->value.enumerated.item[0]) {
4078 case 0:
4079 if (!spec->automute_speaker && !spec->automute_lo)
4080 return 0;
4081 spec->automute_speaker = 0;
4082 spec->automute_lo = 0;
4083 break;
4084 case 1:
4085 if (spec->automute_speaker_possible) {
4086 if (!spec->automute_lo && spec->automute_speaker)
4087 return 0;
4088 spec->automute_speaker = 1;
4089 spec->automute_lo = 0;
4090 } else if (spec->automute_lo_possible) {
4091 if (spec->automute_lo)
4092 return 0;
4093 spec->automute_lo = 1;
4094 } else
4095 return -EINVAL;
4096 break;
4097 case 2:
4098 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4099 return -EINVAL;
4100 if (spec->automute_speaker && spec->automute_lo)
4101 return 0;
4102 spec->automute_speaker = 1;
4103 spec->automute_lo = 1;
4104 break;
4105 default:
4106 return -EINVAL;
4107 }
4108 call_update_outputs(codec);
4109 return 1;
4110}
4111
4112static const struct snd_kcontrol_new automute_mode_enum = {
4113 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4114 .name = "Auto-Mute Mode",
4115 .info = automute_mode_info,
4116 .get = automute_mode_get,
4117 .put = automute_mode_put,
4118};
4119
4120static int add_automute_mode_enum(struct hda_codec *codec)
4121{
4122 struct hda_gen_spec *spec = codec->spec;
4123
Takashi Iwai12c93df2012-12-19 14:38:33 +01004124 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004125 return -ENOMEM;
4126 return 0;
4127}
4128
4129/*
4130 * Check the availability of HP/line-out auto-mute;
4131 * Set up appropriately if really supported
4132 */
4133static int check_auto_mute_availability(struct hda_codec *codec)
4134{
4135 struct hda_gen_spec *spec = codec->spec;
4136 struct auto_pin_cfg *cfg = &spec->autocfg;
4137 int present = 0;
4138 int i, err;
4139
Takashi Iwaif72706b2013-01-16 18:20:07 +01004140 if (spec->suppress_auto_mute)
4141 return 0;
4142
Takashi Iwai352f7f92012-12-19 12:52:06 +01004143 if (cfg->hp_pins[0])
4144 present++;
4145 if (cfg->line_out_pins[0])
4146 present++;
4147 if (cfg->speaker_pins[0])
4148 present++;
4149 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004150 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004151
4152 if (!cfg->speaker_pins[0] &&
4153 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4154 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4155 sizeof(cfg->speaker_pins));
4156 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Takashi Iwai352f7f92012-12-19 12:52:06 +01004159 if (!cfg->hp_pins[0] &&
4160 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4161 memcpy(cfg->hp_pins, cfg->line_out_pins,
4162 sizeof(cfg->hp_pins));
4163 cfg->hp_outs = cfg->line_outs;
4164 }
4165
4166 for (i = 0; i < cfg->hp_outs; i++) {
4167 hda_nid_t nid = cfg->hp_pins[i];
4168 if (!is_jack_detectable(codec, nid))
4169 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004170 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004171 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004172 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004173 spec->detect_hp = 1;
4174 }
4175
4176 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4177 if (cfg->speaker_outs)
4178 for (i = 0; i < cfg->line_outs; i++) {
4179 hda_nid_t nid = cfg->line_out_pins[i];
4180 if (!is_jack_detectable(codec, nid))
4181 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004182 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004183 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004184 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004185 spec->detect_lo = 1;
4186 }
4187 spec->automute_lo_possible = spec->detect_hp;
4188 }
4189
4190 spec->automute_speaker_possible = cfg->speaker_outs &&
4191 (spec->detect_hp || spec->detect_lo);
4192
4193 spec->automute_lo = spec->automute_lo_possible;
4194 spec->automute_speaker = spec->automute_speaker_possible;
4195
4196 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4197 /* create a control for automute mode */
4198 err = add_automute_mode_enum(codec);
4199 if (err < 0)
4200 return err;
4201 }
4202 return 0;
4203}
4204
Takashi Iwai352f7f92012-12-19 12:52:06 +01004205/* check whether all auto-mic pins are valid; setup indices if OK */
4206static bool auto_mic_check_imux(struct hda_codec *codec)
4207{
4208 struct hda_gen_spec *spec = codec->spec;
4209 const struct hda_input_mux *imux;
4210 int i;
4211
4212 imux = &spec->input_mux;
4213 for (i = 0; i < spec->am_num_entries; i++) {
4214 spec->am_entry[i].idx =
4215 find_idx_in_nid_list(spec->am_entry[i].pin,
4216 spec->imux_pins, imux->num_items);
4217 if (spec->am_entry[i].idx < 0)
4218 return false; /* no corresponding imux */
4219 }
4220
4221 /* we don't need the jack detection for the first pin */
4222 for (i = 1; i < spec->am_num_entries; i++)
4223 snd_hda_jack_detect_enable_callback(codec,
4224 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004225 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004226 return true;
4227}
4228
4229static int compare_attr(const void *ap, const void *bp)
4230{
4231 const struct automic_entry *a = ap;
4232 const struct automic_entry *b = bp;
4233 return (int)(a->attr - b->attr);
4234}
4235
4236/*
4237 * Check the availability of auto-mic switch;
4238 * Set up if really supported
4239 */
4240static int check_auto_mic_availability(struct hda_codec *codec)
4241{
4242 struct hda_gen_spec *spec = codec->spec;
4243 struct auto_pin_cfg *cfg = &spec->autocfg;
4244 unsigned int types;
4245 int i, num_pins;
4246
Takashi Iwaid12daf62013-01-07 16:32:11 +01004247 if (spec->suppress_auto_mic)
4248 return 0;
4249
Takashi Iwai352f7f92012-12-19 12:52:06 +01004250 types = 0;
4251 num_pins = 0;
4252 for (i = 0; i < cfg->num_inputs; i++) {
4253 hda_nid_t nid = cfg->inputs[i].pin;
4254 unsigned int attr;
4255 attr = snd_hda_codec_get_pincfg(codec, nid);
4256 attr = snd_hda_get_input_pin_attr(attr);
4257 if (types & (1 << attr))
4258 return 0; /* already occupied */
4259 switch (attr) {
4260 case INPUT_PIN_ATTR_INT:
4261 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4262 return 0; /* invalid type */
4263 break;
4264 case INPUT_PIN_ATTR_UNUSED:
4265 return 0; /* invalid entry */
4266 default:
4267 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4268 return 0; /* invalid type */
4269 if (!spec->line_in_auto_switch &&
4270 cfg->inputs[i].type != AUTO_PIN_MIC)
4271 return 0; /* only mic is allowed */
4272 if (!is_jack_detectable(codec, nid))
4273 return 0; /* no unsol support */
4274 break;
4275 }
4276 if (num_pins >= MAX_AUTO_MIC_PINS)
4277 return 0;
4278 types |= (1 << attr);
4279 spec->am_entry[num_pins].pin = nid;
4280 spec->am_entry[num_pins].attr = attr;
4281 num_pins++;
4282 }
4283
4284 if (num_pins < 2)
4285 return 0;
4286
4287 spec->am_num_entries = num_pins;
4288 /* sort the am_entry in the order of attr so that the pin with a
4289 * higher attr will be selected when the jack is plugged.
4290 */
4291 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4292 compare_attr, NULL);
4293
4294 if (!auto_mic_check_imux(codec))
4295 return 0;
4296
4297 spec->auto_mic = 1;
4298 spec->num_adc_nids = 1;
4299 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004300 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004301 spec->am_entry[0].pin,
4302 spec->am_entry[1].pin,
4303 spec->am_entry[2].pin);
4304
4305 return 0;
4306}
4307
Takashi Iwai55196ff2013-01-24 17:32:56 +01004308/* power_filter hook; make inactive widgets into power down */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004309unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004310 hda_nid_t nid,
4311 unsigned int power_state)
4312{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004313 if (power_state != AC_PWRST_D0 || nid == codec->afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004314 return power_state;
4315 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4316 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004317 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004318 return power_state;
4319 return AC_PWRST_D3;
4320}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004321EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004322
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004323/* mute all aamix inputs initially; parse up to the first leaves */
4324static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4325{
4326 int i, nums;
4327 const hda_nid_t *conn;
4328 bool has_amp;
4329
4330 nums = snd_hda_get_conn_list(codec, mix, &conn);
4331 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4332 for (i = 0; i < nums; i++) {
4333 if (has_amp)
4334 snd_hda_codec_amp_stereo(codec, mix,
4335 HDA_INPUT, i,
4336 0xff, HDA_AMP_MUTE);
4337 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4338 snd_hda_codec_amp_stereo(codec, conn[i],
4339 HDA_OUTPUT, 0,
4340 0xff, HDA_AMP_MUTE);
4341 }
4342}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004343
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004344/*
4345 * Parse the given BIOS configuration and set up the hda_gen_spec
4346 *
4347 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004348 * or a negative error code
4349 */
4350int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004351 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004352{
4353 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004354 int err;
4355
Takashi Iwai1c70a582013-01-11 17:48:22 +01004356 parse_user_hints(codec);
4357
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004358 if (spec->mixer_nid && !spec->mixer_merge_nid)
4359 spec->mixer_merge_nid = spec->mixer_nid;
4360
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004361 if (cfg != &spec->autocfg) {
4362 spec->autocfg = *cfg;
4363 cfg = &spec->autocfg;
4364 }
4365
Takashi Iwai98bd1112013-03-22 14:53:50 +01004366 if (!spec->main_out_badness)
4367 spec->main_out_badness = &hda_main_out_badness;
4368 if (!spec->extra_out_badness)
4369 spec->extra_out_badness = &hda_extra_out_badness;
4370
David Henningsson6fc4cb92013-01-16 15:58:45 +01004371 fill_all_dac_nids(codec);
4372
Takashi Iwai352f7f92012-12-19 12:52:06 +01004373 if (!cfg->line_outs) {
4374 if (cfg->dig_outs || cfg->dig_in_pin) {
4375 spec->multiout.max_channels = 2;
4376 spec->no_analog = 1;
4377 goto dig_only;
4378 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004379 if (!cfg->num_inputs && !cfg->dig_in_pin)
4380 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004381 }
4382
4383 if (!spec->no_primary_hp &&
4384 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4385 cfg->line_outs <= cfg->hp_outs) {
4386 /* use HP as primary out */
4387 cfg->speaker_outs = cfg->line_outs;
4388 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4389 sizeof(cfg->speaker_pins));
4390 cfg->line_outs = cfg->hp_outs;
4391 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4392 cfg->hp_outs = 0;
4393 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4394 cfg->line_out_type = AUTO_PIN_HP_OUT;
4395 }
4396
4397 err = parse_output_paths(codec);
4398 if (err < 0)
4399 return err;
4400 err = create_multi_channel_mode(codec);
4401 if (err < 0)
4402 return err;
4403 err = create_multi_out_ctls(codec, cfg);
4404 if (err < 0)
4405 return err;
4406 err = create_hp_out_ctls(codec);
4407 if (err < 0)
4408 return err;
4409 err = create_speaker_out_ctls(codec);
4410 if (err < 0)
4411 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004412 err = create_indep_hp_ctls(codec);
4413 if (err < 0)
4414 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004415 err = create_loopback_mixing_ctl(codec);
4416 if (err < 0)
4417 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004418 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004419 if (err < 0)
4420 return err;
4421 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004422 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004423 return err;
4424
Takashi Iwaia07a9492013-01-07 16:44:06 +01004425 spec->const_channel_count = spec->ext_channel_count;
4426 /* check the multiple speaker and headphone pins */
4427 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4428 spec->const_channel_count = max(spec->const_channel_count,
4429 cfg->speaker_outs * 2);
4430 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4431 spec->const_channel_count = max(spec->const_channel_count,
4432 cfg->hp_outs * 2);
4433 spec->multiout.max_channels = max(spec->ext_channel_count,
4434 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004435
4436 err = check_auto_mute_availability(codec);
4437 if (err < 0)
4438 return err;
4439
4440 err = check_dyn_adc_switch(codec);
4441 if (err < 0)
4442 return err;
4443
Takashi Iwai967303d2013-02-19 17:12:42 +01004444 err = check_auto_mic_availability(codec);
4445 if (err < 0)
4446 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004447
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004448 /* add stereo mix if available and not enabled yet */
4449 if (!spec->auto_mic && spec->mixer_nid &&
4450 spec->add_stereo_mix_input &&
4451 spec->input_mux.num_items > 1 &&
4452 snd_hda_get_bool_hint(codec, "add_stereo_mix_input") < 0) {
4453 err = parse_capture_source(codec, spec->mixer_nid,
4454 CFG_IDX_MIX, spec->num_all_adcs,
4455 "Stereo Mix", 0);
4456 if (err < 0)
4457 return err;
4458 }
4459
4460
Takashi Iwai352f7f92012-12-19 12:52:06 +01004461 err = create_capture_mixers(codec);
4462 if (err < 0)
4463 return err;
4464
4465 err = parse_mic_boost(codec);
4466 if (err < 0)
4467 return err;
4468
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004469 /* create "Headphone Mic Jack Mode" if no input selection is
4470 * available (or user specifies add_jack_modes hint)
4471 */
4472 if (spec->hp_mic_pin &&
4473 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4474 spec->add_jack_modes)) {
4475 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4476 if (err < 0)
4477 return err;
4478 }
4479
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004480 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004481 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4482 err = create_out_jack_modes(codec, cfg->line_outs,
4483 cfg->line_out_pins);
4484 if (err < 0)
4485 return err;
4486 }
4487 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4488 err = create_out_jack_modes(codec, cfg->hp_outs,
4489 cfg->hp_pins);
4490 if (err < 0)
4491 return err;
4492 }
4493 }
4494
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004495 /* mute all aamix input initially */
4496 if (spec->mixer_nid)
4497 mute_all_mixer_nid(codec, spec->mixer_nid);
4498
Takashi Iwai352f7f92012-12-19 12:52:06 +01004499 dig_only:
4500 parse_digital(codec);
4501
Takashi Iwai55196ff2013-01-24 17:32:56 +01004502 if (spec->power_down_unused)
4503 codec->power_filter = snd_hda_gen_path_power_filter;
4504
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004505 if (!spec->no_analog && spec->beep_nid) {
4506 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4507 if (err < 0)
4508 return err;
4509 }
4510
Takashi Iwai352f7f92012-12-19 12:52:06 +01004511 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004513EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
4515
4516/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004517 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004519
4520/* slave controls for virtual master */
4521static const char * const slave_pfxs[] = {
4522 "Front", "Surround", "Center", "LFE", "Side",
4523 "Headphone", "Speaker", "Mono", "Line Out",
4524 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004525 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4526 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4527 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004528 NULL,
4529};
4530
4531int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004533 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
Takashi Iwai36502d02012-12-19 15:15:10 +01004536 if (spec->kctls.used) {
4537 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4538 if (err < 0)
4539 return err;
4540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
Takashi Iwai352f7f92012-12-19 12:52:06 +01004542 if (spec->multiout.dig_out_nid) {
4543 err = snd_hda_create_dig_out_ctls(codec,
4544 spec->multiout.dig_out_nid,
4545 spec->multiout.dig_out_nid,
4546 spec->pcm_rec[1].pcm_type);
4547 if (err < 0)
4548 return err;
4549 if (!spec->no_analog) {
4550 err = snd_hda_create_spdif_share_sw(codec,
4551 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 if (err < 0)
4553 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004554 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 }
4556 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004557 if (spec->dig_in_nid) {
4558 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4559 if (err < 0)
4560 return err;
4561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
Takashi Iwai352f7f92012-12-19 12:52:06 +01004563 /* if we have no master control, let's create it */
4564 if (!spec->no_analog &&
4565 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004566 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004567 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004568 "Playback Volume");
4569 if (err < 0)
4570 return err;
4571 }
4572 if (!spec->no_analog &&
4573 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4574 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4575 NULL, slave_pfxs,
4576 "Playback Switch",
4577 true, &spec->vmaster_mute.sw_kctl);
4578 if (err < 0)
4579 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02004580 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01004581 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4582 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02004583 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4584 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Takashi Iwai352f7f92012-12-19 12:52:06 +01004587 free_kctls(spec); /* no longer needed */
4588
Takashi Iwai352f7f92012-12-19 12:52:06 +01004589 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4590 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 return err;
4592
4593 return 0;
4594}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004595EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004596
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
4598/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004599 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004602static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4603 struct hda_codec *codec,
4604 struct snd_pcm_substream *substream,
4605 int action)
4606{
4607 struct hda_gen_spec *spec = codec->spec;
4608 if (spec->pcm_playback_hook)
4609 spec->pcm_playback_hook(hinfo, codec, substream, action);
4610}
4611
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004612static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4613 struct hda_codec *codec,
4614 struct snd_pcm_substream *substream,
4615 int action)
4616{
4617 struct hda_gen_spec *spec = codec->spec;
4618 if (spec->pcm_capture_hook)
4619 spec->pcm_capture_hook(hinfo, codec, substream, action);
4620}
4621
Takashi Iwai352f7f92012-12-19 12:52:06 +01004622/*
4623 * Analog playback callbacks
4624 */
4625static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4626 struct hda_codec *codec,
4627 struct snd_pcm_substream *substream)
4628{
4629 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004630 int err;
4631
4632 mutex_lock(&spec->pcm_mutex);
4633 err = snd_hda_multi_out_analog_open(codec,
4634 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004635 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004636 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004637 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004638 call_pcm_playback_hook(hinfo, codec, substream,
4639 HDA_GEN_PCM_ACT_OPEN);
4640 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004641 mutex_unlock(&spec->pcm_mutex);
4642 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004643}
4644
4645static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004646 struct hda_codec *codec,
4647 unsigned int stream_tag,
4648 unsigned int format,
4649 struct snd_pcm_substream *substream)
4650{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004651 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004652 int err;
4653
4654 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4655 stream_tag, format, substream);
4656 if (!err)
4657 call_pcm_playback_hook(hinfo, codec, substream,
4658 HDA_GEN_PCM_ACT_PREPARE);
4659 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004660}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004661
Takashi Iwai352f7f92012-12-19 12:52:06 +01004662static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4663 struct hda_codec *codec,
4664 struct snd_pcm_substream *substream)
4665{
4666 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004667 int err;
4668
4669 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4670 if (!err)
4671 call_pcm_playback_hook(hinfo, codec, substream,
4672 HDA_GEN_PCM_ACT_CLEANUP);
4673 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004674}
4675
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004676static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4677 struct hda_codec *codec,
4678 struct snd_pcm_substream *substream)
4679{
4680 struct hda_gen_spec *spec = codec->spec;
4681 mutex_lock(&spec->pcm_mutex);
4682 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004683 call_pcm_playback_hook(hinfo, codec, substream,
4684 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004685 mutex_unlock(&spec->pcm_mutex);
4686 return 0;
4687}
4688
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004689static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4690 struct hda_codec *codec,
4691 struct snd_pcm_substream *substream)
4692{
4693 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4694 return 0;
4695}
4696
4697static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4698 struct hda_codec *codec,
4699 unsigned int stream_tag,
4700 unsigned int format,
4701 struct snd_pcm_substream *substream)
4702{
4703 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4704 call_pcm_capture_hook(hinfo, codec, substream,
4705 HDA_GEN_PCM_ACT_PREPARE);
4706 return 0;
4707}
4708
4709static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4710 struct hda_codec *codec,
4711 struct snd_pcm_substream *substream)
4712{
4713 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4714 call_pcm_capture_hook(hinfo, codec, substream,
4715 HDA_GEN_PCM_ACT_CLEANUP);
4716 return 0;
4717}
4718
4719static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4720 struct hda_codec *codec,
4721 struct snd_pcm_substream *substream)
4722{
4723 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4724 return 0;
4725}
4726
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004727static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4728 struct hda_codec *codec,
4729 struct snd_pcm_substream *substream)
4730{
4731 struct hda_gen_spec *spec = codec->spec;
4732 int err = 0;
4733
4734 mutex_lock(&spec->pcm_mutex);
4735 if (!spec->indep_hp_enabled)
4736 err = -EBUSY;
4737 else
4738 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004739 call_pcm_playback_hook(hinfo, codec, substream,
4740 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004741 mutex_unlock(&spec->pcm_mutex);
4742 return err;
4743}
4744
4745static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4746 struct hda_codec *codec,
4747 struct snd_pcm_substream *substream)
4748{
4749 struct hda_gen_spec *spec = codec->spec;
4750 mutex_lock(&spec->pcm_mutex);
4751 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004752 call_pcm_playback_hook(hinfo, codec, substream,
4753 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004754 mutex_unlock(&spec->pcm_mutex);
4755 return 0;
4756}
4757
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004758static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4759 struct hda_codec *codec,
4760 unsigned int stream_tag,
4761 unsigned int format,
4762 struct snd_pcm_substream *substream)
4763{
4764 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4765 call_pcm_playback_hook(hinfo, codec, substream,
4766 HDA_GEN_PCM_ACT_PREPARE);
4767 return 0;
4768}
4769
4770static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4771 struct hda_codec *codec,
4772 struct snd_pcm_substream *substream)
4773{
4774 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4775 call_pcm_playback_hook(hinfo, codec, substream,
4776 HDA_GEN_PCM_ACT_CLEANUP);
4777 return 0;
4778}
4779
Takashi Iwai352f7f92012-12-19 12:52:06 +01004780/*
4781 * Digital out
4782 */
4783static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4784 struct hda_codec *codec,
4785 struct snd_pcm_substream *substream)
4786{
4787 struct hda_gen_spec *spec = codec->spec;
4788 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4789}
4790
4791static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4792 struct hda_codec *codec,
4793 unsigned int stream_tag,
4794 unsigned int format,
4795 struct snd_pcm_substream *substream)
4796{
4797 struct hda_gen_spec *spec = codec->spec;
4798 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4799 stream_tag, format, substream);
4800}
4801
4802static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4803 struct hda_codec *codec,
4804 struct snd_pcm_substream *substream)
4805{
4806 struct hda_gen_spec *spec = codec->spec;
4807 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4808}
4809
4810static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4811 struct hda_codec *codec,
4812 struct snd_pcm_substream *substream)
4813{
4814 struct hda_gen_spec *spec = codec->spec;
4815 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4816}
4817
4818/*
4819 * Analog capture
4820 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004821#define alt_capture_pcm_open capture_pcm_open
4822#define alt_capture_pcm_close capture_pcm_close
4823
Takashi Iwai352f7f92012-12-19 12:52:06 +01004824static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4825 struct hda_codec *codec,
4826 unsigned int stream_tag,
4827 unsigned int format,
4828 struct snd_pcm_substream *substream)
4829{
4830 struct hda_gen_spec *spec = codec->spec;
4831
4832 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004833 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004834 call_pcm_capture_hook(hinfo, codec, substream,
4835 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004836 return 0;
4837}
4838
Takashi Iwai352f7f92012-12-19 12:52:06 +01004839static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4840 struct hda_codec *codec,
4841 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004842{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004843 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004844
Takashi Iwai352f7f92012-12-19 12:52:06 +01004845 snd_hda_codec_cleanup_stream(codec,
4846 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004847 call_pcm_capture_hook(hinfo, codec, substream,
4848 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004849 return 0;
4850}
4851
Takashi Iwai352f7f92012-12-19 12:52:06 +01004852/*
4853 */
4854static const struct hda_pcm_stream pcm_analog_playback = {
4855 .substreams = 1,
4856 .channels_min = 2,
4857 .channels_max = 8,
4858 /* NID is set in build_pcms */
4859 .ops = {
4860 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004861 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004862 .prepare = playback_pcm_prepare,
4863 .cleanup = playback_pcm_cleanup
4864 },
4865};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Takashi Iwai352f7f92012-12-19 12:52:06 +01004867static const struct hda_pcm_stream pcm_analog_capture = {
4868 .substreams = 1,
4869 .channels_min = 2,
4870 .channels_max = 2,
4871 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004872 .ops = {
4873 .open = capture_pcm_open,
4874 .close = capture_pcm_close,
4875 .prepare = capture_pcm_prepare,
4876 .cleanup = capture_pcm_cleanup
4877 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004878};
4879
4880static const struct hda_pcm_stream pcm_analog_alt_playback = {
4881 .substreams = 1,
4882 .channels_min = 2,
4883 .channels_max = 2,
4884 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004885 .ops = {
4886 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004887 .close = alt_playback_pcm_close,
4888 .prepare = alt_playback_pcm_prepare,
4889 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004890 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004891};
4892
4893static const struct hda_pcm_stream pcm_analog_alt_capture = {
4894 .substreams = 2, /* can be overridden */
4895 .channels_min = 2,
4896 .channels_max = 2,
4897 /* NID is set in build_pcms */
4898 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004899 .open = alt_capture_pcm_open,
4900 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004901 .prepare = alt_capture_pcm_prepare,
4902 .cleanup = alt_capture_pcm_cleanup
4903 },
4904};
4905
4906static const struct hda_pcm_stream pcm_digital_playback = {
4907 .substreams = 1,
4908 .channels_min = 2,
4909 .channels_max = 2,
4910 /* NID is set in build_pcms */
4911 .ops = {
4912 .open = dig_playback_pcm_open,
4913 .close = dig_playback_pcm_close,
4914 .prepare = dig_playback_pcm_prepare,
4915 .cleanup = dig_playback_pcm_cleanup
4916 },
4917};
4918
4919static const struct hda_pcm_stream pcm_digital_capture = {
4920 .substreams = 1,
4921 .channels_min = 2,
4922 .channels_max = 2,
4923 /* NID is set in build_pcms */
4924};
4925
4926/* Used by build_pcms to flag that a PCM has no playback stream */
4927static const struct hda_pcm_stream pcm_null_stream = {
4928 .substreams = 0,
4929 .channels_min = 0,
4930 .channels_max = 0,
4931};
4932
4933/*
4934 * dynamic changing ADC PCM streams
4935 */
4936static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4937{
4938 struct hda_gen_spec *spec = codec->spec;
4939 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4940
4941 if (spec->cur_adc && spec->cur_adc != new_adc) {
4942 /* stream is running, let's swap the current ADC */
4943 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4944 spec->cur_adc = new_adc;
4945 snd_hda_codec_setup_stream(codec, new_adc,
4946 spec->cur_adc_stream_tag, 0,
4947 spec->cur_adc_format);
4948 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004950 return false;
4951}
4952
4953/* analog capture with dynamic dual-adc changes */
4954static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4955 struct hda_codec *codec,
4956 unsigned int stream_tag,
4957 unsigned int format,
4958 struct snd_pcm_substream *substream)
4959{
4960 struct hda_gen_spec *spec = codec->spec;
4961 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4962 spec->cur_adc_stream_tag = stream_tag;
4963 spec->cur_adc_format = format;
4964 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4965 return 0;
4966}
4967
4968static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4969 struct hda_codec *codec,
4970 struct snd_pcm_substream *substream)
4971{
4972 struct hda_gen_spec *spec = codec->spec;
4973 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4974 spec->cur_adc = 0;
4975 return 0;
4976}
4977
4978static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4979 .substreams = 1,
4980 .channels_min = 2,
4981 .channels_max = 2,
4982 .nid = 0, /* fill later */
4983 .ops = {
4984 .prepare = dyn_adc_capture_pcm_prepare,
4985 .cleanup = dyn_adc_capture_pcm_cleanup
4986 },
4987};
4988
Takashi Iwaif873e532012-12-20 16:58:39 +01004989static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4990 const char *chip_name)
4991{
4992 char *p;
4993
4994 if (*str)
4995 return;
4996 strlcpy(str, chip_name, len);
4997
4998 /* drop non-alnum chars after a space */
4999 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5000 if (!isalnum(p[1])) {
5001 *p = 0;
5002 break;
5003 }
5004 }
5005 strlcat(str, sfx, len);
5006}
5007
Takashi Iwai352f7f92012-12-19 12:52:06 +01005008/* build PCM streams based on the parsed results */
5009int snd_hda_gen_build_pcms(struct hda_codec *codec)
5010{
5011 struct hda_gen_spec *spec = codec->spec;
5012 struct hda_pcm *info = spec->pcm_rec;
5013 const struct hda_pcm_stream *p;
5014 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
5016 codec->num_pcms = 1;
5017 codec->pcm_info = info;
5018
Takashi Iwai352f7f92012-12-19 12:52:06 +01005019 if (spec->no_analog)
5020 goto skip_analog;
5021
Takashi Iwaif873e532012-12-20 16:58:39 +01005022 fill_pcm_stream_name(spec->stream_name_analog,
5023 sizeof(spec->stream_name_analog),
5024 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005025 info->name = spec->stream_name_analog;
5026
5027 if (spec->multiout.num_dacs > 0) {
5028 p = spec->stream_analog_playback;
5029 if (!p)
5030 p = &pcm_analog_playback;
5031 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5032 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
5033 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5034 spec->multiout.max_channels;
5035 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5036 spec->autocfg.line_outs == 2)
5037 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5038 snd_pcm_2_1_chmaps;
5039 }
5040 if (spec->num_adc_nids) {
5041 p = spec->stream_analog_capture;
5042 if (!p) {
5043 if (spec->dyn_adc_switch)
5044 p = &dyn_adc_pcm_analog_capture;
5045 else
5046 p = &pcm_analog_capture;
5047 }
5048 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5049 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
5050 }
5051
Takashi Iwai352f7f92012-12-19 12:52:06 +01005052 skip_analog:
5053 /* SPDIF for stream index #1 */
5054 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005055 fill_pcm_stream_name(spec->stream_name_digital,
5056 sizeof(spec->stream_name_digital),
5057 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005058 codec->num_pcms = 2;
5059 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5060 info = spec->pcm_rec + 1;
5061 info->name = spec->stream_name_digital;
5062 if (spec->dig_out_type)
5063 info->pcm_type = spec->dig_out_type;
5064 else
5065 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5066 if (spec->multiout.dig_out_nid) {
5067 p = spec->stream_digital_playback;
5068 if (!p)
5069 p = &pcm_digital_playback;
5070 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5071 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
5072 }
5073 if (spec->dig_in_nid) {
5074 p = spec->stream_digital_capture;
5075 if (!p)
5076 p = &pcm_digital_capture;
5077 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5078 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
5079 }
5080 }
5081
5082 if (spec->no_analog)
5083 return 0;
5084
5085 /* If the use of more than one ADC is requested for the current
5086 * model, configure a second analog capture-only PCM.
5087 */
5088 have_multi_adcs = (spec->num_adc_nids > 1) &&
5089 !spec->dyn_adc_switch && !spec->auto_mic;
5090 /* Additional Analaog capture for index #2 */
5091 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005092 fill_pcm_stream_name(spec->stream_name_alt_analog,
5093 sizeof(spec->stream_name_alt_analog),
5094 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005095 codec->num_pcms = 3;
5096 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01005097 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005098 if (spec->alt_dac_nid) {
5099 p = spec->stream_analog_alt_playback;
5100 if (!p)
5101 p = &pcm_analog_alt_playback;
5102 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5103 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5104 spec->alt_dac_nid;
5105 } else {
5106 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5107 pcm_null_stream;
5108 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5109 }
5110 if (have_multi_adcs) {
5111 p = spec->stream_analog_alt_capture;
5112 if (!p)
5113 p = &pcm_analog_alt_capture;
5114 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5115 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5116 spec->adc_nids[1];
5117 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5118 spec->num_adc_nids - 1;
5119 } else {
5120 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5121 pcm_null_stream;
5122 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 }
5125
5126 return 0;
5127}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005128EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005129
5130
5131/*
5132 * Standard auto-parser initializations
5133 */
5134
Takashi Iwaid4156932013-01-07 10:08:02 +01005135/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005136static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005137{
5138 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005139 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005140
Takashi Iwai196c17662013-01-04 15:01:40 +01005141 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005142 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005143 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005144 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005145 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005146 snd_hda_activate_path(codec, path, path->active,
5147 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005148 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005149}
5150
5151/* initialize primary output paths */
5152static void init_multi_out(struct hda_codec *codec)
5153{
5154 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005155 int i;
5156
Takashi Iwaid4156932013-01-07 10:08:02 +01005157 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005158 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005159}
5160
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005161
Takashi Iwai2c12c302013-01-10 09:33:29 +01005162static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005163{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005164 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005165
Takashi Iwaid4156932013-01-07 10:08:02 +01005166 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005167 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005168}
5169
5170/* initialize hp and speaker paths */
5171static void init_extra_out(struct hda_codec *codec)
5172{
5173 struct hda_gen_spec *spec = codec->spec;
5174
5175 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005176 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005177 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5178 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005179 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005180}
5181
5182/* initialize multi-io paths */
5183static void init_multi_io(struct hda_codec *codec)
5184{
5185 struct hda_gen_spec *spec = codec->spec;
5186 int i;
5187
5188 for (i = 0; i < spec->multi_ios; i++) {
5189 hda_nid_t pin = spec->multi_io[i].pin;
5190 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005191 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005192 if (!path)
5193 continue;
5194 if (!spec->multi_io[i].ctl_in)
5195 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005196 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005197 snd_hda_activate_path(codec, path, path->active,
5198 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005199 }
5200}
5201
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005202static void init_aamix_paths(struct hda_codec *codec)
5203{
5204 struct hda_gen_spec *spec = codec->spec;
5205
5206 if (!spec->have_aamix_ctl)
5207 return;
5208 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5209 spec->aamix_out_paths[0],
5210 spec->autocfg.line_out_type);
5211 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5212 spec->aamix_out_paths[1],
5213 AUTO_PIN_HP_OUT);
5214 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5215 spec->aamix_out_paths[2],
5216 AUTO_PIN_SPEAKER_OUT);
5217}
5218
Takashi Iwai352f7f92012-12-19 12:52:06 +01005219/* set up input pins and loopback paths */
5220static void init_analog_input(struct hda_codec *codec)
5221{
5222 struct hda_gen_spec *spec = codec->spec;
5223 struct auto_pin_cfg *cfg = &spec->autocfg;
5224 int i;
5225
5226 for (i = 0; i < cfg->num_inputs; i++) {
5227 hda_nid_t nid = cfg->inputs[i].pin;
5228 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005229 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005230
5231 /* init loopback inputs */
5232 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005233 resume_path_from_idx(codec, spec->loopback_paths[i]);
5234 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005235 }
5236 }
5237}
5238
5239/* initialize ADC paths */
5240static void init_input_src(struct hda_codec *codec)
5241{
5242 struct hda_gen_spec *spec = codec->spec;
5243 struct hda_input_mux *imux = &spec->input_mux;
5244 struct nid_path *path;
5245 int i, c, nums;
5246
5247 if (spec->dyn_adc_switch)
5248 nums = 1;
5249 else
5250 nums = spec->num_adc_nids;
5251
5252 for (c = 0; c < nums; c++) {
5253 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005254 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005255 if (path) {
5256 bool active = path->active;
5257 if (i == spec->cur_mux[c])
5258 active = true;
5259 snd_hda_activate_path(codec, path, active, false);
5260 }
5261 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005262 if (spec->hp_mic)
5263 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005264 }
5265
Takashi Iwai352f7f92012-12-19 12:52:06 +01005266 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005267 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005268}
5269
5270/* set right pin controls for digital I/O */
5271static void init_digital(struct hda_codec *codec)
5272{
5273 struct hda_gen_spec *spec = codec->spec;
5274 int i;
5275 hda_nid_t pin;
5276
Takashi Iwaid4156932013-01-07 10:08:02 +01005277 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005278 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005279 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005280 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005281 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005282 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005283 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005284}
5285
Takashi Iwai973e4972012-12-20 15:16:09 +01005286/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5287 * invalid unsol tags by some reason
5288 */
5289static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5290{
5291 int i;
5292
5293 for (i = 0; i < codec->init_pins.used; i++) {
5294 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5295 hda_nid_t nid = pin->nid;
5296 if (is_jack_detectable(codec, nid) &&
5297 !snd_hda_jack_tbl_get(codec, nid))
5298 snd_hda_codec_update_cache(codec, nid, 0,
5299 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5300 }
5301}
5302
Takashi Iwai5187ac12013-01-07 12:52:16 +01005303/*
5304 * initialize the generic spec;
5305 * this can be put as patch_ops.init function
5306 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005307int snd_hda_gen_init(struct hda_codec *codec)
5308{
5309 struct hda_gen_spec *spec = codec->spec;
5310
5311 if (spec->init_hook)
5312 spec->init_hook(codec);
5313
5314 snd_hda_apply_verbs(codec);
5315
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005316 codec->cached_write = 1;
5317
Takashi Iwai352f7f92012-12-19 12:52:06 +01005318 init_multi_out(codec);
5319 init_extra_out(codec);
5320 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005321 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005322 init_analog_input(codec);
5323 init_input_src(codec);
5324 init_digital(codec);
5325
Takashi Iwai973e4972012-12-20 15:16:09 +01005326 clear_unsol_on_unused_pins(codec);
5327
Takashi Iwai352f7f92012-12-19 12:52:06 +01005328 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005329 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005330
Takashi Iwaidc870f32013-01-22 15:24:30 +01005331 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005332
Takashi Iwai352f7f92012-12-19 12:52:06 +01005333 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5334 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5335
5336 hda_call_check_power_status(codec, 0x01);
5337 return 0;
5338}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005339EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005340
Takashi Iwai5187ac12013-01-07 12:52:16 +01005341/*
5342 * free the generic spec;
5343 * this can be put as patch_ops.free function
5344 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005345void snd_hda_gen_free(struct hda_codec *codec)
5346{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005347 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005348 snd_hda_gen_spec_free(codec->spec);
5349 kfree(codec->spec);
5350 codec->spec = NULL;
5351}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005352EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005353
5354#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005355/*
5356 * check the loopback power save state;
5357 * this can be put as patch_ops.check_power_status function
5358 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005359int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5360{
5361 struct hda_gen_spec *spec = codec->spec;
5362 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5363}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005364EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005365#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005366
5367
5368/*
5369 * the generic codec support
5370 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371
Takashi Iwai352f7f92012-12-19 12:52:06 +01005372static const struct hda_codec_ops generic_patch_ops = {
5373 .build_controls = snd_hda_gen_build_controls,
5374 .build_pcms = snd_hda_gen_build_pcms,
5375 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005376 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005377 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005378#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005379 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005380#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381};
5382
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383int snd_hda_parse_generic_codec(struct hda_codec *codec)
5384{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005385 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 int err;
5387
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005388 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005389 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005391 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005394 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5395 if (err < 0)
5396 return err;
5397
5398 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005399 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 goto error;
5401
5402 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 return 0;
5404
Takashi Iwai352f7f92012-12-19 12:52:06 +01005405error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005406 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 return err;
5408}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005409EXPORT_SYMBOL_GPL(snd_hda_parse_generic_codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005410
5411MODULE_LICENSE("GPL");
5412MODULE_DESCRIPTION("Generic HD-audio codec parser");