blob: 27ce54701f0f6e239b9392d178232d8a9a87d3f0 [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 Iwaidda42bd2014-10-30 12:04:50 +010043/**
44 * snd_hda_gen_spec_init - initialize hda_gen_spec struct
45 * @spec: hda_gen_spec object to initialize
46 *
47 * Initialize the given hda_gen_spec object.
48 */
Takashi Iwai352f7f92012-12-19 12:52:06 +010049int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Takashi Iwai352f7f92012-12-19 12:52:06 +010051 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010052 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010053 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010054 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010055 return 0;
56}
Takashi Iwai2698ea92013-12-18 07:45:52 +010057EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Takashi Iwaidda42bd2014-10-30 12:04:50 +010059/**
60 * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
61 * @spec: hda_gen_spec object
62 * @name: name string to override the template, NULL if unchanged
63 * @temp: template for the new kctl
64 *
65 * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
66 * element based on the given snd_kcontrol_new template @temp and the
67 * name string @name to the list in @spec.
68 * Returns the newly created object or NULL as error.
69 */
Takashi Iwai12c93df2012-12-19 14:38:33 +010070struct snd_kcontrol_new *
71snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
72 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010073{
74 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
75 if (!knew)
76 return NULL;
77 *knew = *temp;
78 if (name)
79 knew->name = kstrdup(name, GFP_KERNEL);
80 else if (knew->name)
81 knew->name = kstrdup(knew->name, GFP_KERNEL);
82 if (!knew->name)
83 return NULL;
84 return knew;
85}
Takashi Iwai2698ea92013-12-18 07:45:52 +010086EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010087
88static void free_kctls(struct hda_gen_spec *spec)
89{
90 if (spec->kctls.list) {
91 struct snd_kcontrol_new *kctl = spec->kctls.list;
92 int i;
93 for (i = 0; i < spec->kctls.used; i++)
94 kfree(kctl[i].name);
95 }
96 snd_array_free(&spec->kctls);
97}
98
Takashi Iwaia8dca462014-02-10 18:23:57 +010099static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100100{
101 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100103 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100104 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +0100105 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
Takashi Iwai1c70a582013-01-11 17:48:22 +0100109 * store user hints
110 */
111static void parse_user_hints(struct hda_codec *codec)
112{
113 struct hda_gen_spec *spec = codec->spec;
114 int val;
115
116 val = snd_hda_get_bool_hint(codec, "jack_detect");
117 if (val >= 0)
118 codec->no_jack_detect = !val;
119 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
120 if (val >= 0)
121 codec->inv_jack_detect = !!val;
122 val = snd_hda_get_bool_hint(codec, "trigger_sense");
123 if (val >= 0)
124 codec->no_trigger_sense = !val;
125 val = snd_hda_get_bool_hint(codec, "inv_eapd");
126 if (val >= 0)
127 codec->inv_eapd = !!val;
128 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
129 if (val >= 0)
130 codec->pcm_format_first = !!val;
131 val = snd_hda_get_bool_hint(codec, "sticky_stream");
132 if (val >= 0)
133 codec->no_sticky_stream = !val;
134 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
135 if (val >= 0)
136 codec->spdif_status_reset = !!val;
137 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
138 if (val >= 0)
139 codec->pin_amp_workaround = !!val;
140 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
141 if (val >= 0)
142 codec->single_adc_amp = !!val;
143
Takashi Iwaif72706b2013-01-16 18:20:07 +0100144 val = snd_hda_get_bool_hint(codec, "auto_mute");
145 if (val >= 0)
146 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100147 val = snd_hda_get_bool_hint(codec, "auto_mic");
148 if (val >= 0)
149 spec->suppress_auto_mic = !val;
150 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
151 if (val >= 0)
152 spec->line_in_auto_switch = !!val;
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200153 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
154 if (val >= 0)
155 spec->auto_mute_via_amp = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100156 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
157 if (val >= 0)
158 spec->need_dac_fix = !!val;
159 val = snd_hda_get_bool_hint(codec, "primary_hp");
160 if (val >= 0)
161 spec->no_primary_hp = !val;
Takashi Iwaida96fb52013-07-29 16:54:36 +0200162 val = snd_hda_get_bool_hint(codec, "multi_io");
163 if (val >= 0)
164 spec->no_multi_io = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100165 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
166 if (val >= 0)
167 spec->multi_cap_vol = !!val;
168 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
169 if (val >= 0)
170 spec->inv_dmic_split = !!val;
171 val = snd_hda_get_bool_hint(codec, "indep_hp");
172 if (val >= 0)
173 spec->indep_hp = !!val;
174 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
175 if (val >= 0)
176 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100177 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100178 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
179 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100180 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100181 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
182 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100183 spec->add_jack_modes = !!val;
184 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
185 if (val >= 0)
186 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100187 val = snd_hda_get_bool_hint(codec, "power_down_unused");
188 if (val >= 0)
189 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100190 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
191 if (val >= 0)
192 spec->hp_mic = !!val;
193 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
194 if (val >= 0)
195 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100196
197 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
198 spec->mixer_nid = val;
199}
200
201/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100202 * pin control value accesses
203 */
204
205#define update_pin_ctl(codec, pin, val) \
206 snd_hda_codec_update_cache(codec, pin, 0, \
207 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
208
209/* restore the pinctl based on the cached value */
210static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
211{
212 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
213}
214
215/* set the pinctl target value and write it if requested */
216static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
217 unsigned int val, bool do_write)
218{
219 if (!pin)
220 return;
221 val = snd_hda_correct_pin_ctl(codec, pin, val);
222 snd_hda_codec_set_pin_target(codec, pin, val);
223 if (do_write)
224 update_pin_ctl(codec, pin, val);
225}
226
227/* set pinctl target values for all given pins */
228static void set_pin_targets(struct hda_codec *codec, int num_pins,
229 hda_nid_t *pins, unsigned int val)
230{
231 int i;
232 for (i = 0; i < num_pins; i++)
233 set_pin_target(codec, pins[i], val, false);
234}
235
236/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100237 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100240/* return the position of NID in the list, or -1 if not found */
241static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
242{
243 int i;
244 for (i = 0; i < nums; i++)
245 if (list[i] == nid)
246 return i;
247 return -1;
248}
249
250/* return true if the given NID is contained in the path */
251static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
252{
253 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
254}
255
Takashi Iwaif5172a72013-01-04 13:19:55 +0100256static struct nid_path *get_nid_path(struct hda_codec *codec,
257 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100258 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100260 struct hda_gen_spec *spec = codec->spec;
261 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Takashi Iwai352f7f92012-12-19 12:52:06 +0100263 for (i = 0; i < spec->paths.used; i++) {
264 struct nid_path *path = snd_array_elem(&spec->paths, i);
265 if (path->depth <= 0)
266 continue;
267 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100268 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100269 if (!anchor_nid ||
270 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
271 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100272 return path;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 return NULL;
276}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100277
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100278/**
279 * snd_hda_get_nid_path - get the path between the given NIDs
280 * @codec: the HDA codec
281 * @from_nid: the NID where the path start from
282 * @to_nid: the NID where the path ends at
283 *
284 * Return the found nid_path object or NULL for error.
285 * Passing 0 to either @from_nid or @to_nid behaves as a wildcard.
Takashi Iwaif5172a72013-01-04 13:19:55 +0100286 */
287struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
288 hda_nid_t from_nid, hda_nid_t to_nid)
289{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100290 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100291}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100292EXPORT_SYMBOL_GPL(snd_hda_get_nid_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100293
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100294/**
295 * snd_hda_get_path_idx - get the index number corresponding to the path
296 * instance
297 * @codec: the HDA codec
298 * @path: nid_path object
299 *
300 * The returned index starts from 1, i.e. the actual array index with offset 1,
301 * and zero is handled as an invalid path
Takashi Iwai196c17662013-01-04 15:01:40 +0100302 */
303int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
304{
305 struct hda_gen_spec *spec = codec->spec;
306 struct nid_path *array = spec->paths.list;
307 ssize_t idx;
308
309 if (!spec->paths.used)
310 return 0;
311 idx = path - array;
312 if (idx < 0 || idx >= spec->paths.used)
313 return 0;
314 return idx + 1;
315}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100316EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100317
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100318/**
319 * snd_hda_get_path_from_idx - get the path instance corresponding to the
320 * given index number
321 * @codec: the HDA codec
322 * @idx: the path index
323 */
Takashi Iwai196c17662013-01-04 15:01:40 +0100324struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
325{
326 struct hda_gen_spec *spec = codec->spec;
327
328 if (idx <= 0 || idx > spec->paths.used)
329 return NULL;
330 return snd_array_elem(&spec->paths, idx - 1);
331}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100332EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100333
Takashi Iwai352f7f92012-12-19 12:52:06 +0100334/* check whether the given DAC is already found in any existing paths */
335static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
336{
337 struct hda_gen_spec *spec = codec->spec;
338 int i;
339
340 for (i = 0; i < spec->paths.used; i++) {
341 struct nid_path *path = snd_array_elem(&spec->paths, i);
342 if (path->path[0] == nid)
343 return true;
344 }
345 return false;
346}
347
348/* check whether the given two widgets can be connected */
349static bool is_reachable_path(struct hda_codec *codec,
350 hda_nid_t from_nid, hda_nid_t to_nid)
351{
352 if (!from_nid || !to_nid)
353 return false;
354 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
355}
356
357/* nid, dir and idx */
358#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
359
360/* check whether the given ctl is already assigned in any path elements */
361static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
362{
363 struct hda_gen_spec *spec = codec->spec;
364 int i;
365
366 val &= AMP_VAL_COMPARE_MASK;
367 for (i = 0; i < spec->paths.used; i++) {
368 struct nid_path *path = snd_array_elem(&spec->paths, i);
369 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
370 return true;
371 }
372 return false;
373}
374
375/* check whether a control with the given (nid, dir, idx) was assigned */
376static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100377 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100378{
379 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100380 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100381}
382
Takashi Iwai4e76a882014-02-25 12:21:03 +0100383static void print_nid_path(struct hda_codec *codec,
384 const char *pfx, struct nid_path *path)
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100385{
386 char buf[40];
Joe Perchesd82353e2014-07-05 13:02:02 -0700387 char *pos = buf;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100388 int i;
389
Joe Perchesd82353e2014-07-05 13:02:02 -0700390 *pos = 0;
391 for (i = 0; i < path->depth; i++)
392 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
393 pos != buf ? ":" : "",
394 path->path[i]);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100395
Joe Perchesd82353e2014-07-05 13:02:02 -0700396 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100397}
398
Takashi Iwai352f7f92012-12-19 12:52:06 +0100399/* called recursively */
400static bool __parse_nid_path(struct hda_codec *codec,
401 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100402 int anchor_nid, struct nid_path *path,
403 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100404{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100405 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100406 int i, nums;
407
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100408 if (to_nid == anchor_nid)
409 anchor_nid = 0; /* anchor passed */
410 else if (to_nid == (hda_nid_t)(-anchor_nid))
411 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100412
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100413 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100414 for (i = 0; i < nums; i++) {
415 if (conn[i] != from_nid) {
416 /* special case: when from_nid is 0,
417 * try to find an empty DAC
418 */
419 if (from_nid ||
420 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
421 is_dac_already_used(codec, conn[i]))
422 continue;
423 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100424 /* anchor is not requested or already passed? */
425 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100426 goto found;
427 }
428 if (depth >= MAX_NID_PATH_DEPTH)
429 return false;
430 for (i = 0; i < nums; i++) {
431 unsigned int type;
432 type = get_wcaps_type(get_wcaps(codec, conn[i]));
433 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
434 type == AC_WID_PIN)
435 continue;
436 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100437 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100438 goto found;
439 }
440 return false;
441
442 found:
443 path->path[path->depth] = conn[i];
444 path->idx[path->depth + 1] = i;
445 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
446 path->multi[path->depth + 1] = 1;
447 path->depth++;
448 return true;
449}
450
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100451/**
452 * snd_hda_parse_nid_path - parse the widget path from the given nid to
453 * the target nid
454 * @codec: the HDA codec
455 * @from_nid: the NID where the path start from
456 * @to_nid: the NID where the path ends at
457 * @anchor_nid: the anchor indication
458 * @path: the path object to store the result
459 *
460 * Returns true if a matching path is found.
461 *
462 * The parsing behavior depends on parameters:
Takashi Iwai352f7f92012-12-19 12:52:06 +0100463 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100464 * when @anchor_nid is set to a positive value, only paths through the widget
465 * with the given value are evaluated.
466 * when @anchor_nid is set to a negative value, paths through the widget
467 * with the negative of given value are excluded, only other paths are chosen.
468 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100469 */
470bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100471 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100472 struct nid_path *path)
473{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100474 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100475 path->path[path->depth] = to_nid;
476 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100477 return true;
478 }
479 return false;
480}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100481EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100483/**
484 * snd_hda_add_new_path - parse the path between the given NIDs and
485 * add to the path list
486 * @codec: the HDA codec
487 * @from_nid: the NID where the path start from
488 * @to_nid: the NID where the path ends at
489 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
490 *
491 * If no valid path is found, returns NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100493struct nid_path *
494snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100495 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100497 struct hda_gen_spec *spec = codec->spec;
498 struct nid_path *path;
499
500 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
501 return NULL;
502
Takashi Iwaif5172a72013-01-04 13:19:55 +0100503 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100504 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100505 if (path)
506 return path;
507
Takashi Iwai352f7f92012-12-19 12:52:06 +0100508 path = snd_array_new(&spec->paths);
509 if (!path)
510 return NULL;
511 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100512 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100513 return path;
514 /* push back */
515 spec->paths.used--;
516 return NULL;
517}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100518EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100519
Takashi Iwai980428c2013-01-09 09:28:20 +0100520/* clear the given path as invalid so that it won't be picked up later */
521static void invalidate_nid_path(struct hda_codec *codec, int idx)
522{
523 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
524 if (!path)
525 return;
526 memset(path, 0, sizeof(*path));
527}
528
Takashi Iwai36907392013-12-10 17:29:26 +0100529/* return a DAC if paired to the given pin by codec driver */
530static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
531{
532 struct hda_gen_spec *spec = codec->spec;
533 const hda_nid_t *list = spec->preferred_dacs;
534
535 if (!list)
536 return 0;
537 for (; *list; list += 2)
538 if (*list == pin)
539 return list[1];
540 return 0;
541}
542
Takashi Iwai352f7f92012-12-19 12:52:06 +0100543/* look for an empty DAC slot */
544static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
545 bool is_digital)
546{
547 struct hda_gen_spec *spec = codec->spec;
548 bool cap_digital;
549 int i;
550
551 for (i = 0; i < spec->num_all_dacs; i++) {
552 hda_nid_t nid = spec->all_dacs[i];
553 if (!nid || is_dac_already_used(codec, nid))
554 continue;
555 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
556 if (is_digital != cap_digital)
557 continue;
558 if (is_reachable_path(codec, nid, pin))
559 return nid;
560 }
561 return 0;
562}
563
564/* replace the channels in the composed amp value with the given number */
565static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
566{
567 val &= ~(0x3U << 16);
568 val |= chs << 16;
569 return val;
570}
571
David Henningsson99a55922013-01-16 15:58:44 +0100572static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
573 hda_nid_t nid2, int dir)
574{
575 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
576 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
577 return (query_amp_caps(codec, nid1, dir) ==
578 query_amp_caps(codec, nid2, dir));
579}
580
Takashi Iwai352f7f92012-12-19 12:52:06 +0100581/* look for a widget suitable for assigning a mute switch in the path */
582static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
583 struct nid_path *path)
584{
585 int i;
586
587 for (i = path->depth - 1; i >= 0; i--) {
588 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
589 return path->path[i];
590 if (i != path->depth - 1 && i != 0 &&
591 nid_has_mute(codec, path->path[i], HDA_INPUT))
592 return path->path[i];
593 }
594 return 0;
595}
596
597/* look for a widget suitable for assigning a volume ctl in the path */
598static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
599 struct nid_path *path)
600{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100601 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100602 int i;
603
604 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100605 hda_nid_t nid = path->path[i];
606 if ((spec->out_vol_mask >> nid) & 1)
607 continue;
608 if (nid_has_volume(codec, nid, HDA_OUTPUT))
609 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100610 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
614/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100615 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100617
618/* can have the amp-in capability? */
619static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100621 hda_nid_t nid = path->path[idx];
622 unsigned int caps = get_wcaps(codec, nid);
623 unsigned int type = get_wcaps_type(caps);
624
625 if (!(caps & AC_WCAP_IN_AMP))
626 return false;
627 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
628 return false;
629 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Takashi Iwai352f7f92012-12-19 12:52:06 +0100632/* can have the amp-out capability? */
633static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100635 hda_nid_t nid = path->path[idx];
636 unsigned int caps = get_wcaps(codec, nid);
637 unsigned int type = get_wcaps_type(caps);
638
639 if (!(caps & AC_WCAP_OUT_AMP))
640 return false;
641 if (type == AC_WID_PIN && !idx) /* only for output pins */
642 return false;
643 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Takashi Iwai352f7f92012-12-19 12:52:06 +0100646/* check whether the given (nid,dir,idx) is active */
647static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100648 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100650 struct hda_gen_spec *spec = codec->spec;
651 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Takashi Iwai352f7f92012-12-19 12:52:06 +0100653 for (n = 0; n < spec->paths.used; n++) {
654 struct nid_path *path = snd_array_elem(&spec->paths, n);
655 if (!path->active)
656 continue;
657 for (i = 0; i < path->depth; i++) {
658 if (path->path[i] == nid) {
659 if (dir == HDA_OUTPUT || path->idx[i] == idx)
660 return true;
661 break;
662 }
663 }
664 }
665 return false;
666}
667
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200668/* check whether the NID is referred by any active paths */
669#define is_active_nid_for_any(codec, nid) \
670 is_active_nid(codec, nid, HDA_OUTPUT, 0)
671
Takashi Iwai352f7f92012-12-19 12:52:06 +0100672/* get the default amp value for the target state */
673static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100674 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100675{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100676 unsigned int val = 0;
677
Takashi Iwai352f7f92012-12-19 12:52:06 +0100678 if (caps & AC_AMPCAP_NUM_STEPS) {
679 /* set to 0dB */
680 if (enable)
681 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
682 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200683 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100684 if (!enable)
685 val |= HDA_AMP_MUTE;
686 }
687 return val;
688}
689
Takashi Iwaicc261732015-03-16 10:18:08 +0100690/* is this a stereo widget or a stereo-to-mono mix? */
691static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
692{
693 unsigned int wcaps = get_wcaps(codec, nid);
694 hda_nid_t conn;
695
696 if (wcaps & AC_WCAP_STEREO)
697 return true;
698 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
699 return false;
700 if (snd_hda_get_num_conns(codec, nid) != 1)
701 return false;
702 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
703 return false;
704 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
705}
706
Takashi Iwai352f7f92012-12-19 12:52:06 +0100707/* initialize the amp value (only at the first time) */
708static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
709{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100710 unsigned int caps = query_amp_caps(codec, nid, dir);
711 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwaief403ed2015-03-12 08:30:11 +0100712
Takashi Iwaicc261732015-03-16 10:18:08 +0100713 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100714 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
715 else
716 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
717}
718
719/* update the amp, doing in stereo or mono depending on NID */
720static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
721 unsigned int mask, unsigned int val)
722{
Takashi Iwaicc261732015-03-16 10:18:08 +0100723 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100724 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
725 mask, val);
726 else
727 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
728 mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100729}
730
Takashi Iwai8999bf02013-01-18 11:01:33 +0100731/* calculate amp value mask we can modify;
732 * if the given amp is controlled by mixers, don't touch it
733 */
734static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
735 hda_nid_t nid, int dir, int idx,
736 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100737{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100738 unsigned int mask = 0xff;
739
Takashi Iwaif69910d2013-08-08 09:32:37 +0200740 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100741 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
742 mask &= ~0x80;
743 }
744 if (caps & AC_AMPCAP_NUM_STEPS) {
745 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
746 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
747 mask &= ~0x7f;
748 }
749 return mask;
750}
751
752static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
753 int idx, int idx_to_check, bool enable)
754{
755 unsigned int caps;
756 unsigned int mask, val;
757
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100758 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100759 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100760
761 caps = query_amp_caps(codec, nid, dir);
762 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
763 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
764 if (!mask)
765 return;
766
767 val &= mask;
Takashi Iwaief403ed2015-03-12 08:30:11 +0100768 update_amp(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100769}
770
771static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
772 int i, bool enable)
773{
774 hda_nid_t nid = path->path[i];
775 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100776 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100777}
778
779static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
780 int i, bool enable, bool add_aamix)
781{
782 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100783 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100784 int n, nums, idx;
785 int type;
786 hda_nid_t nid = path->path[i];
787
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100788 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100789 type = get_wcaps_type(get_wcaps(codec, nid));
790 if (type == AC_WID_PIN ||
791 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
792 nums = 1;
793 idx = 0;
794 } else
795 idx = path->idx[i];
796
797 for (n = 0; n < nums; n++)
798 init_amp(codec, nid, HDA_INPUT, n);
799
Takashi Iwai352f7f92012-12-19 12:52:06 +0100800 /* here is a little bit tricky in comparison with activate_amp_out();
801 * when aa-mixer is available, we need to enable the path as well
802 */
803 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100804 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100805 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100806 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808}
809
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100810/**
811 * snd_hda_activate_path - activate or deactivate the given path
812 * @codec: the HDA codec
813 * @path: the path to activate/deactivate
814 * @enable: flag to activate or not
815 * @add_aamix: enable the input from aamix NID
816 *
817 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100819void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
820 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100822 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100823 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Takashi Iwai352f7f92012-12-19 12:52:06 +0100825 if (!enable)
826 path->active = false;
827
828 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100829 hda_nid_t nid = path->path[i];
830 if (enable && spec->power_down_unused) {
831 /* make sure the widget is powered up */
832 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
833 snd_hda_codec_write(codec, nid, 0,
834 AC_VERB_SET_POWER_STATE,
835 AC_PWRST_D0);
836 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100837 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100838 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100839 AC_VERB_SET_CONNECT_SEL,
840 path->idx[i]);
841 if (has_amp_in(codec, path, i))
842 activate_amp_in(codec, path, i, enable, add_aamix);
843 if (has_amp_out(codec, path, i))
844 activate_amp_out(codec, path, i, enable);
845 }
846
847 if (enable)
848 path->active = true;
849}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100850EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100851
Takashi Iwai55196ff2013-01-24 17:32:56 +0100852/* if the given path is inactive, put widgets into D3 (only if suitable) */
853static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
854{
855 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200856 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100857 int i;
858
859 if (!spec->power_down_unused || path->active)
860 return;
861
862 for (i = 0; i < path->depth; i++) {
863 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200864 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
865 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100866 snd_hda_codec_write(codec, nid, 0,
867 AC_VERB_SET_POWER_STATE,
868 AC_PWRST_D3);
869 changed = true;
870 }
871 }
872
873 if (changed) {
874 msleep(10);
875 snd_hda_codec_read(codec, path->path[0], 0,
876 AC_VERB_GET_POWER_STATE, 0);
877 }
878}
879
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100880/* turn on/off EAPD on the given pin */
881static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
882{
883 struct hda_gen_spec *spec = codec->spec;
884 if (spec->own_eapd_ctl ||
885 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
886 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200887 if (spec->keep_eapd_on && !enable)
888 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100889 if (codec->inv_eapd)
890 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100891 snd_hda_codec_update_cache(codec, pin, 0,
892 AC_VERB_SET_EAPD_BTLENABLE,
893 enable ? 0x02 : 0x00);
894}
895
Takashi Iwai3e367f12013-01-23 17:07:23 +0100896/* re-initialize the path specified by the given path index */
897static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
898{
899 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
900 if (path)
901 snd_hda_activate_path(codec, path, path->active, false);
902}
903
Takashi Iwai352f7f92012-12-19 12:52:06 +0100904
905/*
906 * Helper functions for creating mixer ctl elements
907 */
908
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200909static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
910 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200911static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
912 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200913
Takashi Iwai352f7f92012-12-19 12:52:06 +0100914enum {
915 HDA_CTL_WIDGET_VOL,
916 HDA_CTL_WIDGET_MUTE,
917 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100918};
919static const struct snd_kcontrol_new control_templates[] = {
920 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200921 /* only the put callback is replaced for handling the special mute */
922 {
923 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
924 .subdevice = HDA_SUBDEV_AMP_FLAG,
925 .info = snd_hda_mixer_amp_switch_info,
926 .get = snd_hda_mixer_amp_switch_get,
927 .put = hda_gen_mixer_mute_put, /* replaced */
928 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
929 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200930 {
931 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
932 .info = snd_hda_mixer_amp_switch_info,
933 .get = snd_hda_mixer_bind_switch_get,
934 .put = hda_gen_bind_mute_put, /* replaced */
935 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
936 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100937};
938
939/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100940static struct snd_kcontrol_new *
941add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100942 int cidx, unsigned long val)
943{
944 struct snd_kcontrol_new *knew;
945
Takashi Iwai12c93df2012-12-19 14:38:33 +0100946 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100947 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100948 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100949 knew->index = cidx;
950 if (get_amp_nid_(val))
951 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
952 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100953 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100954}
955
956static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
957 const char *pfx, const char *dir,
958 const char *sfx, int cidx, unsigned long val)
959{
Takashi Iwai975cc022013-06-28 11:56:49 +0200960 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100961 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100962 if (!add_control(spec, type, name, cidx, val))
963 return -ENOMEM;
964 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100965}
966
967#define add_pb_vol_ctrl(spec, type, pfx, val) \
968 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
969#define add_pb_sw_ctrl(spec, type, pfx, val) \
970 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
971#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
972 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
973#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
974 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
975
976static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
977 unsigned int chs, struct nid_path *path)
978{
979 unsigned int val;
980 if (!path)
981 return 0;
982 val = path->ctls[NID_PATH_VOL_CTL];
983 if (!val)
984 return 0;
985 val = amp_val_replace_channels(val, chs);
986 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
987}
988
989/* return the channel bits suitable for the given path->ctls[] */
990static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
991 int type)
992{
993 int chs = 1; /* mono (left only) */
994 if (path) {
995 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
996 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
997 chs = 3; /* stereo */
998 }
999 return chs;
1000}
1001
1002static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1003 struct nid_path *path)
1004{
1005 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1006 return add_vol_ctl(codec, pfx, cidx, chs, path);
1007}
1008
1009/* create a mute-switch for the given mixer widget;
1010 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1011 */
1012static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1013 unsigned int chs, struct nid_path *path)
1014{
1015 unsigned int val;
1016 int type = HDA_CTL_WIDGET_MUTE;
1017
1018 if (!path)
1019 return 0;
1020 val = path->ctls[NID_PATH_MUTE_CTL];
1021 if (!val)
1022 return 0;
1023 val = amp_val_replace_channels(val, chs);
1024 if (get_amp_direction_(val) == HDA_INPUT) {
1025 hda_nid_t nid = get_amp_nid_(val);
1026 int nums = snd_hda_get_num_conns(codec, nid);
1027 if (nums > 1) {
1028 type = HDA_CTL_BIND_MUTE;
1029 val |= nums << 19;
1030 }
1031 }
1032 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1033}
1034
1035static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1036 int cidx, struct nid_path *path)
1037{
1038 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1039 return add_sw_ctl(codec, pfx, cidx, chs, path);
1040}
1041
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001042/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001043static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1044 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001045{
1046 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1047 struct hda_gen_spec *spec = codec->spec;
1048
1049 if (spec->auto_mute_via_amp) {
1050 hda_nid_t nid = get_amp_nid(kcontrol);
1051 bool enabled = !((spec->mute_bits >> nid) & 1);
1052 ucontrol->value.integer.value[0] &= enabled;
1053 ucontrol->value.integer.value[1] &= enabled;
1054 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001055}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001056
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001057static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1058 struct snd_ctl_elem_value *ucontrol)
1059{
1060 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001061 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1062}
1063
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001064static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1065 struct snd_ctl_elem_value *ucontrol)
1066{
1067 sync_auto_mute_bits(kcontrol, ucontrol);
1068 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1069}
1070
Takashi Iwai247d85e2013-01-17 16:18:11 +01001071/* any ctl assigned to the path with the given index? */
1072static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1073{
1074 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1075 return path && path->ctls[ctl_type];
1076}
1077
Takashi Iwai352f7f92012-12-19 12:52:06 +01001078static const char * const channel_name[4] = {
1079 "Front", "Surround", "CLFE", "Side"
1080};
1081
1082/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001083static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1084 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001085{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001086 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001087 struct auto_pin_cfg *cfg = &spec->autocfg;
1088
1089 *index = 0;
1090 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001091 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001092 return spec->vmaster_mute.hook ? "PCM" : "Master";
1093
1094 /* if there is really a single DAC used in the whole output paths,
1095 * use it master (or "PCM" if a vmaster hook is present)
1096 */
1097 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1098 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1099 return spec->vmaster_mute.hook ? "PCM" : "Master";
1100
Takashi Iwai247d85e2013-01-17 16:18:11 +01001101 /* multi-io channels */
1102 if (ch >= cfg->line_outs)
1103 return channel_name[ch];
1104
Takashi Iwai352f7f92012-12-19 12:52:06 +01001105 switch (cfg->line_out_type) {
1106 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001107 /* if the primary channel vol/mute is shared with HP volume,
1108 * don't name it as Speaker
1109 */
1110 if (!ch && cfg->hp_outs &&
1111 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1112 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001113 if (cfg->line_outs == 1)
1114 return "Speaker";
1115 if (cfg->line_outs == 2)
1116 return ch ? "Bass Speaker" : "Speaker";
1117 break;
1118 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001119 /* if the primary channel vol/mute is shared with spk volume,
1120 * don't name it as Headphone
1121 */
1122 if (!ch && cfg->speaker_outs &&
1123 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1124 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001125 /* for multi-io case, only the primary out */
1126 if (ch && spec->multi_ios)
1127 break;
1128 *index = ch;
1129 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001130 case AUTO_PIN_LINE_OUT:
1131 /* This deals with the case where we have two DACs and
1132 * one LO, one HP and one Speaker */
1133 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1134 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1135 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1136 if (hp_lo_shared && spk_lo_shared)
1137 return spec->vmaster_mute.hook ? "PCM" : "Master";
1138 if (hp_lo_shared)
1139 return "Headphone+LO";
1140 if (spk_lo_shared)
1141 return "Speaker+LO";
1142 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001143 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001144
1145 /* for a single channel output, we don't have to name the channel */
1146 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001147 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001148
Takashi Iwai352f7f92012-12-19 12:52:06 +01001149 if (ch >= ARRAY_SIZE(channel_name)) {
1150 snd_BUG();
1151 return "PCM";
1152 }
1153
1154 return channel_name[ch];
1155}
1156
1157/*
1158 * Parse output paths
1159 */
1160
1161/* badness definition */
1162enum {
1163 /* No primary DAC is found for the main output */
1164 BAD_NO_PRIMARY_DAC = 0x10000,
1165 /* No DAC is found for the extra output */
1166 BAD_NO_DAC = 0x4000,
1167 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001168 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001169 /* No individual DAC for extra output */
1170 BAD_NO_EXTRA_DAC = 0x102,
1171 /* No individual DAC for extra surrounds */
1172 BAD_NO_EXTRA_SURR_DAC = 0x101,
1173 /* Primary DAC shared with main surrounds */
1174 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001175 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001176 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001177 /* Primary DAC shared with main CLFE */
1178 BAD_SHARED_CLFE = 0x10,
1179 /* Primary DAC shared with extra surrounds */
1180 BAD_SHARED_EXTRA_SURROUND = 0x10,
1181 /* Volume widget is shared */
1182 BAD_SHARED_VOL = 0x10,
1183};
1184
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001185/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001186 * volume and mute controls, and assign the values to ctls[].
1187 *
1188 * When no appropriate widget is found in the path, the badness value
1189 * is incremented depending on the situation. The function returns the
1190 * total badness for both volume and mute controls.
1191 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001192static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001193{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001194 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001195 hda_nid_t nid;
1196 unsigned int val;
1197 int badness = 0;
1198
1199 if (!path)
1200 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001201
1202 if (path->ctls[NID_PATH_VOL_CTL] ||
1203 path->ctls[NID_PATH_MUTE_CTL])
1204 return 0; /* already evaluated */
1205
Takashi Iwai352f7f92012-12-19 12:52:06 +01001206 nid = look_for_out_vol_nid(codec, path);
1207 if (nid) {
1208 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001209 if (spec->dac_min_mute)
1210 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001211 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1212 badness += BAD_SHARED_VOL;
1213 else
1214 path->ctls[NID_PATH_VOL_CTL] = val;
1215 } else
1216 badness += BAD_SHARED_VOL;
1217 nid = look_for_out_mute_nid(codec, path);
1218 if (nid) {
1219 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1220 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1221 nid_has_mute(codec, nid, HDA_OUTPUT))
1222 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1223 else
1224 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1225 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1226 badness += BAD_SHARED_VOL;
1227 else
1228 path->ctls[NID_PATH_MUTE_CTL] = val;
1229 } else
1230 badness += BAD_SHARED_VOL;
1231 return badness;
1232}
1233
Takashi Iwai98bd1112013-03-22 14:53:50 +01001234const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001235 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1236 .no_dac = BAD_NO_DAC,
1237 .shared_primary = BAD_NO_PRIMARY_DAC,
1238 .shared_surr = BAD_SHARED_SURROUND,
1239 .shared_clfe = BAD_SHARED_CLFE,
1240 .shared_surr_main = BAD_SHARED_SURROUND,
1241};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001242EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001243
Takashi Iwai98bd1112013-03-22 14:53:50 +01001244const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001245 .no_primary_dac = BAD_NO_DAC,
1246 .no_dac = BAD_NO_DAC,
1247 .shared_primary = BAD_NO_EXTRA_DAC,
1248 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1249 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1250 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1251};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001252EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001253
Takashi Iwai7385df62013-01-07 09:50:52 +01001254/* get the DAC of the primary output corresponding to the given array index */
1255static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1256{
1257 struct hda_gen_spec *spec = codec->spec;
1258 struct auto_pin_cfg *cfg = &spec->autocfg;
1259
1260 if (cfg->line_outs > idx)
1261 return spec->private_dac_nids[idx];
1262 idx -= cfg->line_outs;
1263 if (spec->multi_ios > idx)
1264 return spec->multi_io[idx].dac;
1265 return 0;
1266}
1267
1268/* return the DAC if it's reachable, otherwise zero */
1269static inline hda_nid_t try_dac(struct hda_codec *codec,
1270 hda_nid_t dac, hda_nid_t pin)
1271{
1272 return is_reachable_path(codec, dac, pin) ? dac : 0;
1273}
1274
Takashi Iwai352f7f92012-12-19 12:52:06 +01001275/* try to assign DACs to pins and return the resultant badness */
1276static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1277 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001278 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001279 const struct badness_table *bad)
1280{
1281 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001282 int i, j;
1283 int badness = 0;
1284 hda_nid_t dac;
1285
1286 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return 0;
1288
Takashi Iwai352f7f92012-12-19 12:52:06 +01001289 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001290 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001291 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001292
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001293 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1294 if (path) {
1295 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001296 continue;
1297 }
1298
Takashi Iwai36907392013-12-10 17:29:26 +01001299 dacs[i] = get_preferred_dac(codec, pin);
1300 if (dacs[i]) {
1301 if (is_dac_already_used(codec, dacs[i]))
1302 badness += bad->shared_primary;
1303 }
1304
1305 if (!dacs[i])
1306 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001307 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001308 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001309 for (j = 1; j < num_outs; j++) {
1310 if (is_reachable_path(codec, dacs[j], pin)) {
1311 dacs[0] = dacs[j];
1312 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001313 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001314 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001315 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001318 }
1319 dac = dacs[i];
1320 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001321 if (num_outs > 2)
1322 dac = try_dac(codec, get_primary_out(codec, i), pin);
1323 if (!dac)
1324 dac = try_dac(codec, dacs[0], pin);
1325 if (!dac)
1326 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001327 if (dac) {
1328 if (!i)
1329 badness += bad->shared_primary;
1330 else if (i == 1)
1331 badness += bad->shared_surr;
1332 else
1333 badness += bad->shared_clfe;
1334 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1335 dac = spec->private_dac_nids[0];
1336 badness += bad->shared_surr_main;
1337 } else if (!i)
1338 badness += bad->no_primary_dac;
1339 else
1340 badness += bad->no_dac;
1341 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001342 if (!dac)
1343 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001344 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001345 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001346 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001347 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001348 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001349 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001350 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001351 badness += bad->no_dac;
1352 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001353 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001354 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001355 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001356 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001357 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001358 }
1359
1360 return badness;
1361}
1362
1363/* return NID if the given pin has only a single connection to a certain DAC */
1364static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1365{
1366 struct hda_gen_spec *spec = codec->spec;
1367 int i;
1368 hda_nid_t nid_found = 0;
1369
1370 for (i = 0; i < spec->num_all_dacs; i++) {
1371 hda_nid_t nid = spec->all_dacs[i];
1372 if (!nid || is_dac_already_used(codec, nid))
1373 continue;
1374 if (is_reachable_path(codec, nid, pin)) {
1375 if (nid_found)
1376 return 0;
1377 nid_found = nid;
1378 }
1379 }
1380 return nid_found;
1381}
1382
1383/* check whether the given pin can be a multi-io pin */
1384static bool can_be_multiio_pin(struct hda_codec *codec,
1385 unsigned int location, hda_nid_t nid)
1386{
1387 unsigned int defcfg, caps;
1388
1389 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1390 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1391 return false;
1392 if (location && get_defcfg_location(defcfg) != location)
1393 return false;
1394 caps = snd_hda_query_pin_caps(codec, nid);
1395 if (!(caps & AC_PINCAP_OUT))
1396 return false;
1397 return true;
1398}
1399
Takashi Iwaie22aab72013-01-04 14:50:04 +01001400/* count the number of input pins that are capable to be multi-io */
1401static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1402{
1403 struct hda_gen_spec *spec = codec->spec;
1404 struct auto_pin_cfg *cfg = &spec->autocfg;
1405 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1406 unsigned int location = get_defcfg_location(defcfg);
1407 int type, i;
1408 int num_pins = 0;
1409
1410 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1411 for (i = 0; i < cfg->num_inputs; i++) {
1412 if (cfg->inputs[i].type != type)
1413 continue;
1414 if (can_be_multiio_pin(codec, location,
1415 cfg->inputs[i].pin))
1416 num_pins++;
1417 }
1418 }
1419 return num_pins;
1420}
1421
Takashi Iwai352f7f92012-12-19 12:52:06 +01001422/*
1423 * multi-io helper
1424 *
1425 * When hardwired is set, try to fill ony hardwired pins, and returns
1426 * zero if any pins are filled, non-zero if nothing found.
1427 * When hardwired is off, try to fill possible input pins, and returns
1428 * the badness value.
1429 */
1430static int fill_multi_ios(struct hda_codec *codec,
1431 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001432 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001433{
1434 struct hda_gen_spec *spec = codec->spec;
1435 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001436 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001437 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1438 unsigned int location = get_defcfg_location(defcfg);
1439 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001440 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001441
1442 old_pins = spec->multi_ios;
1443 if (old_pins >= 2)
1444 goto end_fill;
1445
Takashi Iwaie22aab72013-01-04 14:50:04 +01001446 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001447 if (num_pins < 2)
1448 goto end_fill;
1449
Takashi Iwai352f7f92012-12-19 12:52:06 +01001450 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1451 for (i = 0; i < cfg->num_inputs; i++) {
1452 hda_nid_t nid = cfg->inputs[i].pin;
1453 hda_nid_t dac = 0;
1454
1455 if (cfg->inputs[i].type != type)
1456 continue;
1457 if (!can_be_multiio_pin(codec, location, nid))
1458 continue;
1459 for (j = 0; j < spec->multi_ios; j++) {
1460 if (nid == spec->multi_io[j].pin)
1461 break;
1462 }
1463 if (j < spec->multi_ios)
1464 continue;
1465
Takashi Iwai352f7f92012-12-19 12:52:06 +01001466 if (hardwired)
1467 dac = get_dac_if_single(codec, nid);
1468 else if (!dac)
1469 dac = look_for_dac(codec, nid, false);
1470 if (!dac) {
1471 badness++;
1472 continue;
1473 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001474 path = snd_hda_add_new_path(codec, dac, nid,
1475 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001476 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001477 badness++;
1478 continue;
1479 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001480 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001481 spec->multi_io[spec->multi_ios].pin = nid;
1482 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001483 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1484 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001485 spec->multi_ios++;
1486 if (spec->multi_ios >= 2)
1487 break;
1488 }
1489 }
1490 end_fill:
1491 if (badness)
1492 badness = BAD_MULTI_IO;
1493 if (old_pins == spec->multi_ios) {
1494 if (hardwired)
1495 return 1; /* nothing found */
1496 else
1497 return badness; /* no badness if nothing found */
1498 }
1499 if (!hardwired && spec->multi_ios < 2) {
1500 /* cancel newly assigned paths */
1501 spec->paths.used -= spec->multi_ios - old_pins;
1502 spec->multi_ios = old_pins;
1503 return badness;
1504 }
1505
1506 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001507 for (i = old_pins; i < spec->multi_ios; i++) {
1508 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1509 badness += assign_out_path_ctls(codec, path);
1510 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001511
1512 return badness;
1513}
1514
1515/* map DACs for all pins in the list if they are single connections */
1516static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001517 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001518{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001519 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001520 int i;
1521 bool found = false;
1522 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001523 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001524 hda_nid_t dac;
1525 if (dacs[i])
1526 continue;
1527 dac = get_dac_if_single(codec, pins[i]);
1528 if (!dac)
1529 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001530 path = snd_hda_add_new_path(codec, dac, pins[i],
1531 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001532 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001533 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001534 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001535 dacs[i] = dac;
1536 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001537 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001538 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001539 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001540 }
1541 }
1542 return found;
1543}
1544
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001545/* create a new path including aamix if available, and return its index */
1546static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1547{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001548 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001549 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001550 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001551
1552 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001553 if (!path || !path->depth ||
1554 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001555 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001556 path_dac = path->path[0];
1557 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001558 pin = path->path[path->depth - 1];
1559 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1560 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001561 if (dac != path_dac)
1562 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001563 else if (spec->multiout.hp_out_nid[0])
1564 dac = spec->multiout.hp_out_nid[0];
1565 else if (spec->multiout.extra_out_nid[0])
1566 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001567 else
1568 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001569 if (dac)
1570 path = snd_hda_add_new_path(codec, dac, pin,
1571 spec->mixer_nid);
1572 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001573 if (!path)
1574 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001575 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001576 path->active = false; /* unused as default */
1577 return snd_hda_get_path_idx(codec, path);
1578}
1579
Takashi Iwai55a63d42013-03-21 17:20:12 +01001580/* check whether the independent HP is available with the current config */
1581static bool indep_hp_possible(struct hda_codec *codec)
1582{
1583 struct hda_gen_spec *spec = codec->spec;
1584 struct auto_pin_cfg *cfg = &spec->autocfg;
1585 struct nid_path *path;
1586 int i, idx;
1587
1588 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1589 idx = spec->out_paths[0];
1590 else
1591 idx = spec->hp_paths[0];
1592 path = snd_hda_get_path_from_idx(codec, idx);
1593 if (!path)
1594 return false;
1595
1596 /* assume no path conflicts unless aamix is involved */
1597 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1598 return true;
1599
1600 /* check whether output paths contain aamix */
1601 for (i = 0; i < cfg->line_outs; i++) {
1602 if (spec->out_paths[i] == idx)
1603 break;
1604 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1605 if (path && is_nid_contained(path, spec->mixer_nid))
1606 return false;
1607 }
1608 for (i = 0; i < cfg->speaker_outs; i++) {
1609 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1610 if (path && is_nid_contained(path, spec->mixer_nid))
1611 return false;
1612 }
1613
1614 return true;
1615}
1616
Takashi Iwaia07a9492013-01-07 16:44:06 +01001617/* fill the empty entries in the dac array for speaker/hp with the
1618 * shared dac pointed by the paths
1619 */
1620static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1621 hda_nid_t *dacs, int *path_idx)
1622{
1623 struct nid_path *path;
1624 int i;
1625
1626 for (i = 0; i < num_outs; i++) {
1627 if (dacs[i])
1628 continue;
1629 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1630 if (!path)
1631 continue;
1632 dacs[i] = path->path[0];
1633 }
1634}
1635
Takashi Iwai352f7f92012-12-19 12:52:06 +01001636/* fill in the dac_nids table from the parsed pin configuration */
1637static int fill_and_eval_dacs(struct hda_codec *codec,
1638 bool fill_hardwired,
1639 bool fill_mio_first)
1640{
1641 struct hda_gen_spec *spec = codec->spec;
1642 struct auto_pin_cfg *cfg = &spec->autocfg;
1643 int i, err, badness;
1644
1645 /* set num_dacs once to full for look_for_dac() */
1646 spec->multiout.num_dacs = cfg->line_outs;
1647 spec->multiout.dac_nids = spec->private_dac_nids;
1648 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1649 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1650 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1651 spec->multi_ios = 0;
1652 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001653
1654 /* clear path indices */
1655 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1656 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1657 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1658 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1659 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001660 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001661 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1662 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1663
Takashi Iwai352f7f92012-12-19 12:52:06 +01001664 badness = 0;
1665
1666 /* fill hard-wired DACs first */
1667 if (fill_hardwired) {
1668 bool mapped;
1669 do {
1670 mapped = map_singles(codec, cfg->line_outs,
1671 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001672 spec->private_dac_nids,
1673 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001674 mapped |= map_singles(codec, cfg->hp_outs,
1675 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001676 spec->multiout.hp_out_nid,
1677 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001678 mapped |= map_singles(codec, cfg->speaker_outs,
1679 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001680 spec->multiout.extra_out_nid,
1681 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001682 if (!spec->no_multi_io &&
1683 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001684 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001685 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001686 if (!err)
1687 mapped = true;
1688 }
1689 } while (mapped);
1690 }
1691
1692 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001693 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001694 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001695
Takashi Iwaida96fb52013-07-29 16:54:36 +02001696 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001697 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1698 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001699 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001700 if (err < 0)
1701 return err;
1702 /* we don't count badness at this stage yet */
1703 }
1704
1705 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1706 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1707 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001708 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001709 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001710 if (err < 0)
1711 return err;
1712 badness += err;
1713 }
1714 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1715 err = try_assign_dacs(codec, cfg->speaker_outs,
1716 cfg->speaker_pins,
1717 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001718 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001719 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001720 if (err < 0)
1721 return err;
1722 badness += err;
1723 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001724 if (!spec->no_multi_io &&
1725 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001726 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001727 if (err < 0)
1728 return err;
1729 badness += err;
1730 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001731
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001732 if (spec->mixer_nid) {
1733 spec->aamix_out_paths[0] =
1734 check_aamix_out_path(codec, spec->out_paths[0]);
1735 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1736 spec->aamix_out_paths[1] =
1737 check_aamix_out_path(codec, spec->hp_paths[0]);
1738 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1739 spec->aamix_out_paths[2] =
1740 check_aamix_out_path(codec, spec->speaker_paths[0]);
1741 }
1742
Takashi Iwaida96fb52013-07-29 16:54:36 +02001743 if (!spec->no_multi_io &&
1744 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001745 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1746 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001747
Takashi Iwaia07a9492013-01-07 16:44:06 +01001748 /* re-count num_dacs and squash invalid entries */
1749 spec->multiout.num_dacs = 0;
1750 for (i = 0; i < cfg->line_outs; i++) {
1751 if (spec->private_dac_nids[i])
1752 spec->multiout.num_dacs++;
1753 else {
1754 memmove(spec->private_dac_nids + i,
1755 spec->private_dac_nids + i + 1,
1756 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1757 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1758 }
1759 }
1760
1761 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001762 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001763
Takashi Iwai352f7f92012-12-19 12:52:06 +01001764 if (spec->multi_ios == 2) {
1765 for (i = 0; i < 2; i++)
1766 spec->private_dac_nids[spec->multiout.num_dacs++] =
1767 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001768 } else if (spec->multi_ios) {
1769 spec->multi_ios = 0;
1770 badness += BAD_MULTI_IO;
1771 }
1772
Takashi Iwai55a63d42013-03-21 17:20:12 +01001773 if (spec->indep_hp && !indep_hp_possible(codec))
1774 badness += BAD_NO_INDEP_HP;
1775
Takashi Iwaia07a9492013-01-07 16:44:06 +01001776 /* re-fill the shared DAC for speaker / headphone */
1777 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1778 refill_shared_dacs(codec, cfg->hp_outs,
1779 spec->multiout.hp_out_nid,
1780 spec->hp_paths);
1781 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1782 refill_shared_dacs(codec, cfg->speaker_outs,
1783 spec->multiout.extra_out_nid,
1784 spec->speaker_paths);
1785
Takashi Iwai352f7f92012-12-19 12:52:06 +01001786 return badness;
1787}
1788
1789#define DEBUG_BADNESS
1790
1791#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001792#define debug_badness(fmt, ...) \
1793 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001794#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001795#define debug_badness(fmt, ...) \
1796 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001797#endif
1798
Takashi Iwaia7694092013-01-21 10:43:18 +01001799#ifdef DEBUG_BADNESS
1800static inline void print_nid_path_idx(struct hda_codec *codec,
1801 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001802{
Takashi Iwaia7694092013-01-21 10:43:18 +01001803 struct nid_path *path;
1804
1805 path = snd_hda_get_path_from_idx(codec, idx);
1806 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001807 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001808}
1809
1810static void debug_show_configs(struct hda_codec *codec,
1811 struct auto_pin_cfg *cfg)
1812{
1813 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001814 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001815 int i;
1816
1817 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001818 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001819 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001820 spec->multiout.dac_nids[0],
1821 spec->multiout.dac_nids[1],
1822 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001823 spec->multiout.dac_nids[3],
1824 lo_type[cfg->line_out_type]);
1825 for (i = 0; i < cfg->line_outs; i++)
1826 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001827 if (spec->multi_ios > 0)
1828 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1829 spec->multi_ios,
1830 spec->multi_io[0].pin, spec->multi_io[1].pin,
1831 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001832 for (i = 0; i < spec->multi_ios; i++)
1833 print_nid_path_idx(codec, " mio",
1834 spec->out_paths[cfg->line_outs + i]);
1835 if (cfg->hp_outs)
1836 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001837 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001838 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001839 spec->multiout.hp_out_nid[0],
1840 spec->multiout.hp_out_nid[1],
1841 spec->multiout.hp_out_nid[2],
1842 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001843 for (i = 0; i < cfg->hp_outs; i++)
1844 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1845 if (cfg->speaker_outs)
1846 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001847 cfg->speaker_pins[0], cfg->speaker_pins[1],
1848 cfg->speaker_pins[2], cfg->speaker_pins[3],
1849 spec->multiout.extra_out_nid[0],
1850 spec->multiout.extra_out_nid[1],
1851 spec->multiout.extra_out_nid[2],
1852 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001853 for (i = 0; i < cfg->speaker_outs; i++)
1854 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1855 for (i = 0; i < 3; i++)
1856 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001857}
Takashi Iwaia7694092013-01-21 10:43:18 +01001858#else
1859#define debug_show_configs(codec, cfg) /* NOP */
1860#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001861
1862/* find all available DACs of the codec */
1863static void fill_all_dac_nids(struct hda_codec *codec)
1864{
1865 struct hda_gen_spec *spec = codec->spec;
1866 int i;
1867 hda_nid_t nid = codec->start_nid;
1868
1869 spec->num_all_dacs = 0;
1870 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1871 for (i = 0; i < codec->num_nodes; i++, nid++) {
1872 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1873 continue;
1874 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001875 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001876 break;
1877 }
1878 spec->all_dacs[spec->num_all_dacs++] = nid;
1879 }
1880}
1881
1882static int parse_output_paths(struct hda_codec *codec)
1883{
1884 struct hda_gen_spec *spec = codec->spec;
1885 struct auto_pin_cfg *cfg = &spec->autocfg;
1886 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001887 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001888 int best_badness = INT_MAX;
1889 int badness;
1890 bool fill_hardwired = true, fill_mio_first = true;
1891 bool best_wired = true, best_mio = true;
1892 bool hp_spk_swapped = false;
1893
Takashi Iwai352f7f92012-12-19 12:52:06 +01001894 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1895 if (!best_cfg)
1896 return -ENOMEM;
1897 *best_cfg = *cfg;
1898
1899 for (;;) {
1900 badness = fill_and_eval_dacs(codec, fill_hardwired,
1901 fill_mio_first);
1902 if (badness < 0) {
1903 kfree(best_cfg);
1904 return badness;
1905 }
1906 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1907 cfg->line_out_type, fill_hardwired, fill_mio_first,
1908 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001909 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001910 if (badness < best_badness) {
1911 best_badness = badness;
1912 *best_cfg = *cfg;
1913 best_wired = fill_hardwired;
1914 best_mio = fill_mio_first;
1915 }
1916 if (!badness)
1917 break;
1918 fill_mio_first = !fill_mio_first;
1919 if (!fill_mio_first)
1920 continue;
1921 fill_hardwired = !fill_hardwired;
1922 if (!fill_hardwired)
1923 continue;
1924 if (hp_spk_swapped)
1925 break;
1926 hp_spk_swapped = true;
1927 if (cfg->speaker_outs > 0 &&
1928 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1929 cfg->hp_outs = cfg->line_outs;
1930 memcpy(cfg->hp_pins, cfg->line_out_pins,
1931 sizeof(cfg->hp_pins));
1932 cfg->line_outs = cfg->speaker_outs;
1933 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1934 sizeof(cfg->speaker_pins));
1935 cfg->speaker_outs = 0;
1936 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1937 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1938 fill_hardwired = true;
1939 continue;
1940 }
1941 if (cfg->hp_outs > 0 &&
1942 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1943 cfg->speaker_outs = cfg->line_outs;
1944 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1945 sizeof(cfg->speaker_pins));
1946 cfg->line_outs = cfg->hp_outs;
1947 memcpy(cfg->line_out_pins, cfg->hp_pins,
1948 sizeof(cfg->hp_pins));
1949 cfg->hp_outs = 0;
1950 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1951 cfg->line_out_type = AUTO_PIN_HP_OUT;
1952 fill_hardwired = true;
1953 continue;
1954 }
1955 break;
1956 }
1957
1958 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001959 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001960 *cfg = *best_cfg;
1961 fill_and_eval_dacs(codec, best_wired, best_mio);
1962 }
1963 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1964 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001965 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001966
1967 if (cfg->line_out_pins[0]) {
1968 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001969 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001970 if (path)
1971 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001972 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001973 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1974 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001975 if (spec->dac_min_mute)
1976 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
1977 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001978 }
1979
Takashi Iwai9314a582013-01-21 10:49:05 +01001980 /* set initial pinctl targets */
1981 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1982 val = PIN_HP;
1983 else
1984 val = PIN_OUT;
1985 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1986 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1987 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1988 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1989 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1990 set_pin_targets(codec, cfg->speaker_outs,
1991 cfg->speaker_pins, val);
1992 }
1993
Takashi Iwai55a63d42013-03-21 17:20:12 +01001994 /* clear indep_hp flag if not available */
1995 if (spec->indep_hp && !indep_hp_possible(codec))
1996 spec->indep_hp = 0;
1997
Takashi Iwai352f7f92012-12-19 12:52:06 +01001998 kfree(best_cfg);
1999 return 0;
2000}
2001
2002/* add playback controls from the parsed DAC table */
2003static int create_multi_out_ctls(struct hda_codec *codec,
2004 const struct auto_pin_cfg *cfg)
2005{
2006 struct hda_gen_spec *spec = codec->spec;
2007 int i, err, noutputs;
2008
2009 noutputs = cfg->line_outs;
2010 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2011 noutputs += spec->multi_ios;
2012
2013 for (i = 0; i < noutputs; i++) {
2014 const char *name;
2015 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002016 struct nid_path *path;
2017
Takashi Iwai196c17662013-01-04 15:01:40 +01002018 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002019 if (!path)
2020 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002021
2022 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002023 if (!name || !strcmp(name, "CLFE")) {
2024 /* Center/LFE */
2025 err = add_vol_ctl(codec, "Center", 0, 1, path);
2026 if (err < 0)
2027 return err;
2028 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2029 if (err < 0)
2030 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002031 } else {
2032 err = add_stereo_vol(codec, name, index, path);
2033 if (err < 0)
2034 return err;
2035 }
2036
2037 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2038 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002039 err = add_sw_ctl(codec, "Center", 0, 1, path);
2040 if (err < 0)
2041 return err;
2042 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2043 if (err < 0)
2044 return err;
2045 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002046 err = add_stereo_sw(codec, name, index, path);
2047 if (err < 0)
2048 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050 }
2051 return 0;
2052}
2053
Takashi Iwaic2c80382013-01-07 10:33:57 +01002054static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002055 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002057 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 int err;
2059
Takashi Iwai196c17662013-01-04 15:01:40 +01002060 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002061 if (!path)
2062 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002063 err = add_stereo_vol(codec, pfx, cidx, path);
2064 if (err < 0)
2065 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002066 err = add_stereo_sw(codec, pfx, cidx, path);
2067 if (err < 0)
2068 return err;
2069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
Takashi Iwai352f7f92012-12-19 12:52:06 +01002072/* add playback controls for speaker and HP outputs */
2073static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002074 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002076 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002077
2078 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002079 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002080 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002081 int err, idx = 0;
2082
2083 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2084 name = "Bass Speaker";
2085 else if (num_pins >= 3) {
2086 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002087 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002088 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002089 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002090 name = pfx;
2091 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002093 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002094 if (err < 0)
2095 return err;
2096 }
2097 return 0;
2098}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002099
Takashi Iwai352f7f92012-12-19 12:52:06 +01002100static int create_hp_out_ctls(struct hda_codec *codec)
2101{
2102 struct hda_gen_spec *spec = codec->spec;
2103 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002104 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002105 "Headphone");
2106}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Takashi Iwai352f7f92012-12-19 12:52:06 +01002108static int create_speaker_out_ctls(struct hda_codec *codec)
2109{
2110 struct hda_gen_spec *spec = codec->spec;
2111 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002112 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002113 "Speaker");
2114}
2115
2116/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002117 * independent HP controls
2118 */
2119
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002120static void call_hp_automute(struct hda_codec *codec,
2121 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002122static int indep_hp_info(struct snd_kcontrol *kcontrol,
2123 struct snd_ctl_elem_info *uinfo)
2124{
2125 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2126}
2127
2128static int indep_hp_get(struct snd_kcontrol *kcontrol,
2129 struct snd_ctl_elem_value *ucontrol)
2130{
2131 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2132 struct hda_gen_spec *spec = codec->spec;
2133 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2134 return 0;
2135}
2136
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002137static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2138 int nomix_path_idx, int mix_path_idx,
2139 int out_type);
2140
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002141static int indep_hp_put(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;
2146 unsigned int select = ucontrol->value.enumerated.item[0];
2147 int ret = 0;
2148
2149 mutex_lock(&spec->pcm_mutex);
2150 if (spec->active_streams) {
2151 ret = -EBUSY;
2152 goto unlock;
2153 }
2154
2155 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002156 hda_nid_t *dacp;
2157 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2158 dacp = &spec->private_dac_nids[0];
2159 else
2160 dacp = &spec->multiout.hp_out_nid[0];
2161
2162 /* update HP aamix paths in case it conflicts with indep HP */
2163 if (spec->have_aamix_ctl) {
2164 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2165 update_aamix_paths(codec, spec->aamix_mode,
2166 spec->out_paths[0],
2167 spec->aamix_out_paths[0],
2168 spec->autocfg.line_out_type);
2169 else
2170 update_aamix_paths(codec, spec->aamix_mode,
2171 spec->hp_paths[0],
2172 spec->aamix_out_paths[1],
2173 AUTO_PIN_HP_OUT);
2174 }
2175
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002176 spec->indep_hp_enabled = select;
2177 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002178 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002179 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002180 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002181
Takashi Iwai963afde2013-05-31 15:20:31 +02002182 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002183 ret = 1;
2184 }
2185 unlock:
2186 mutex_unlock(&spec->pcm_mutex);
2187 return ret;
2188}
2189
2190static const struct snd_kcontrol_new indep_hp_ctl = {
2191 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2192 .name = "Independent HP",
2193 .info = indep_hp_info,
2194 .get = indep_hp_get,
2195 .put = indep_hp_put,
2196};
2197
2198
2199static int create_indep_hp_ctls(struct hda_codec *codec)
2200{
2201 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002202 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002203
2204 if (!spec->indep_hp)
2205 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002206 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2207 dac = spec->multiout.dac_nids[0];
2208 else
2209 dac = spec->multiout.hp_out_nid[0];
2210 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002211 spec->indep_hp = 0;
2212 return 0;
2213 }
2214
2215 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002216 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002217 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2218 return -ENOMEM;
2219 return 0;
2220}
2221
2222/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002223 * channel mode enum control
2224 */
2225
2226static int ch_mode_info(struct snd_kcontrol *kcontrol,
2227 struct snd_ctl_elem_info *uinfo)
2228{
2229 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2230 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002231 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002232
2233 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2234 uinfo->count = 1;
2235 uinfo->value.enumerated.items = spec->multi_ios + 1;
2236 if (uinfo->value.enumerated.item > spec->multi_ios)
2237 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002238 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2239 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002240 return 0;
2241}
2242
2243static int ch_mode_get(struct snd_kcontrol *kcontrol,
2244 struct snd_ctl_elem_value *ucontrol)
2245{
2246 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2247 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002248 ucontrol->value.enumerated.item[0] =
2249 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002250 return 0;
2251}
2252
Takashi Iwai196c17662013-01-04 15:01:40 +01002253static inline struct nid_path *
2254get_multiio_path(struct hda_codec *codec, int idx)
2255{
2256 struct hda_gen_spec *spec = codec->spec;
2257 return snd_hda_get_path_from_idx(codec,
2258 spec->out_paths[spec->autocfg.line_outs + idx]);
2259}
2260
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002261static void update_automute_all(struct hda_codec *codec);
2262
Takashi Iwai65033cc2013-04-16 12:31:05 +02002263/* Default value to be passed as aamix argument for snd_hda_activate_path();
2264 * used for output paths
2265 */
2266static bool aamix_default(struct hda_gen_spec *spec)
2267{
2268 return !spec->have_aamix_ctl || spec->aamix_mode;
2269}
2270
Takashi Iwai352f7f92012-12-19 12:52:06 +01002271static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2272{
2273 struct hda_gen_spec *spec = codec->spec;
2274 hda_nid_t nid = spec->multi_io[idx].pin;
2275 struct nid_path *path;
2276
Takashi Iwai196c17662013-01-04 15:01:40 +01002277 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002278 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002280
2281 if (path->active == output)
2282 return 0;
2283
2284 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002285 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002286 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002287 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002288 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002289 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002290 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002291 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002292 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002294
2295 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002296 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002297
Takashi Iwai352f7f92012-12-19 12:52:06 +01002298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
Takashi Iwai352f7f92012-12-19 12:52:06 +01002301static int ch_mode_put(struct snd_kcontrol *kcontrol,
2302 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002304 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2305 struct hda_gen_spec *spec = codec->spec;
2306 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Takashi Iwai352f7f92012-12-19 12:52:06 +01002308 ch = ucontrol->value.enumerated.item[0];
2309 if (ch < 0 || ch > spec->multi_ios)
2310 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002311 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002312 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002313 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002314 for (i = 0; i < spec->multi_ios; i++)
2315 set_multi_io(codec, i, i < ch);
2316 spec->multiout.max_channels = max(spec->ext_channel_count,
2317 spec->const_channel_count);
2318 if (spec->need_dac_fix)
2319 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return 1;
2321}
2322
Takashi Iwai352f7f92012-12-19 12:52:06 +01002323static const struct snd_kcontrol_new channel_mode_enum = {
2324 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2325 .name = "Channel Mode",
2326 .info = ch_mode_info,
2327 .get = ch_mode_get,
2328 .put = ch_mode_put,
2329};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Takashi Iwai352f7f92012-12-19 12:52:06 +01002331static int create_multi_channel_mode(struct hda_codec *codec)
2332{
2333 struct hda_gen_spec *spec = codec->spec;
2334
2335 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002336 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002337 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return 0;
2340}
2341
Takashi Iwai352f7f92012-12-19 12:52:06 +01002342/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002343 * aamix loopback enable/disable switch
2344 */
2345
2346#define loopback_mixing_info indep_hp_info
2347
2348static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2349 struct snd_ctl_elem_value *ucontrol)
2350{
2351 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2352 struct hda_gen_spec *spec = codec->spec;
2353 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2354 return 0;
2355}
2356
2357static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002358 int nomix_path_idx, int mix_path_idx,
2359 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002360{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002361 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002362 struct nid_path *nomix_path, *mix_path;
2363
2364 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2365 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2366 if (!nomix_path || !mix_path)
2367 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002368
2369 /* if HP aamix path is driven from a different DAC and the
2370 * independent HP mode is ON, can't turn on aamix path
2371 */
2372 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2373 mix_path->path[0] != spec->alt_dac_nid)
2374 do_mix = false;
2375
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002376 if (do_mix) {
2377 snd_hda_activate_path(codec, nomix_path, false, true);
2378 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002379 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002380 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002381 snd_hda_activate_path(codec, mix_path, false, false);
2382 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002383 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002384 }
2385}
2386
2387static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2388 struct snd_ctl_elem_value *ucontrol)
2389{
2390 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2391 struct hda_gen_spec *spec = codec->spec;
2392 unsigned int val = ucontrol->value.enumerated.item[0];
2393
2394 if (val == spec->aamix_mode)
2395 return 0;
2396 spec->aamix_mode = val;
2397 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002398 spec->aamix_out_paths[0],
2399 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002400 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002401 spec->aamix_out_paths[1],
2402 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002403 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002404 spec->aamix_out_paths[2],
2405 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002406 return 1;
2407}
2408
2409static const struct snd_kcontrol_new loopback_mixing_enum = {
2410 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2411 .name = "Loopback Mixing",
2412 .info = loopback_mixing_info,
2413 .get = loopback_mixing_get,
2414 .put = loopback_mixing_put,
2415};
2416
2417static int create_loopback_mixing_ctl(struct hda_codec *codec)
2418{
2419 struct hda_gen_spec *spec = codec->spec;
2420
2421 if (!spec->mixer_nid)
2422 return 0;
2423 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2424 spec->aamix_out_paths[2]))
2425 return 0;
2426 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2427 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002428 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002429 return 0;
2430}
2431
2432/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002433 * shared headphone/mic handling
2434 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002435
Takashi Iwai352f7f92012-12-19 12:52:06 +01002436static void call_update_outputs(struct hda_codec *codec);
2437
2438/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002439static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002440{
2441 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002442 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002443 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002444 hda_nid_t pin;
2445
2446 pin = spec->hp_mic_pin;
2447 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2448
2449 if (!force) {
2450 val = snd_hda_codec_get_pin_target(codec, pin);
2451 if (as_mic) {
2452 if (val & PIN_IN)
2453 return;
2454 } else {
2455 if (val & PIN_OUT)
2456 return;
2457 }
2458 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002459
2460 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002461 /* if the HP pin doesn't support VREF and the codec driver gives an
2462 * alternative pin, set up the VREF on that pin instead
2463 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002464 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2465 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2466 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2467 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002468 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002469 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002470 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002471
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002472 if (!spec->hp_mic_jack_modes) {
2473 if (as_mic)
2474 val |= PIN_IN;
2475 else
2476 val = PIN_HP;
2477 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002478 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002479 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002480}
2481
2482/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002483static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002484{
2485 struct hda_gen_spec *spec = codec->spec;
2486 struct auto_pin_cfg *cfg = &spec->autocfg;
2487 unsigned int defcfg;
2488 hda_nid_t nid;
2489
Takashi Iwai967303d2013-02-19 17:12:42 +01002490 if (!spec->hp_mic) {
2491 if (spec->suppress_hp_mic_detect)
2492 return 0;
2493 /* automatic detection: only if no input or a single internal
2494 * input pin is found, try to detect the shared hp/mic
2495 */
2496 if (cfg->num_inputs > 1)
2497 return 0;
2498 else if (cfg->num_inputs == 1) {
2499 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2500 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2501 return 0;
2502 }
2503 }
2504
2505 spec->hp_mic = 0; /* clear once */
2506 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002507 return 0;
2508
Takashi Iwai967303d2013-02-19 17:12:42 +01002509 nid = 0;
2510 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2511 nid = cfg->line_out_pins[0];
2512 else if (cfg->hp_outs > 0)
2513 nid = cfg->hp_pins[0];
2514 if (!nid)
2515 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002516
2517 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2518 return 0; /* no input */
2519
Takashi Iwai967303d2013-02-19 17:12:42 +01002520 cfg->inputs[cfg->num_inputs].pin = nid;
2521 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002522 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002523 cfg->num_inputs++;
2524 spec->hp_mic = 1;
2525 spec->hp_mic_pin = nid;
2526 /* we can't handle auto-mic together with HP-mic */
2527 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002528 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002529 return 0;
2530}
2531
Takashi Iwai978e77e2013-01-10 16:57:58 +01002532/*
2533 * output jack mode
2534 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002535
2536static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2537
2538static const char * const out_jack_texts[] = {
2539 "Line Out", "Headphone Out",
2540};
2541
Takashi Iwai978e77e2013-01-10 16:57:58 +01002542static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2543 struct snd_ctl_elem_info *uinfo)
2544{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002545 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002546}
2547
2548static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2549 struct snd_ctl_elem_value *ucontrol)
2550{
2551 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2552 hda_nid_t nid = kcontrol->private_value;
2553 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2554 ucontrol->value.enumerated.item[0] = 1;
2555 else
2556 ucontrol->value.enumerated.item[0] = 0;
2557 return 0;
2558}
2559
2560static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2561 struct snd_ctl_elem_value *ucontrol)
2562{
2563 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2564 hda_nid_t nid = kcontrol->private_value;
2565 unsigned int val;
2566
2567 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2568 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2569 return 0;
2570 snd_hda_set_pin_ctl_cache(codec, nid, val);
2571 return 1;
2572}
2573
2574static const struct snd_kcontrol_new out_jack_mode_enum = {
2575 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2576 .info = out_jack_mode_info,
2577 .get = out_jack_mode_get,
2578 .put = out_jack_mode_put,
2579};
2580
2581static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2582{
2583 struct hda_gen_spec *spec = codec->spec;
2584 int i;
2585
2586 for (i = 0; i < spec->kctls.used; i++) {
2587 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2588 if (!strcmp(kctl->name, name) && kctl->index == idx)
2589 return true;
2590 }
2591 return false;
2592}
2593
2594static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2595 char *name, size_t name_len)
2596{
2597 struct hda_gen_spec *spec = codec->spec;
2598 int idx = 0;
2599
2600 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2601 strlcat(name, " Jack Mode", name_len);
2602
2603 for (; find_kctl_name(codec, name, idx); idx++)
2604 ;
2605}
2606
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002607static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2608{
2609 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002610 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002611 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2612 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2613 return 2;
2614 }
2615 return 1;
2616}
2617
Takashi Iwai978e77e2013-01-10 16:57:58 +01002618static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2619 hda_nid_t *pins)
2620{
2621 struct hda_gen_spec *spec = codec->spec;
2622 int i;
2623
2624 for (i = 0; i < num_pins; i++) {
2625 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002626 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002627 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002628 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002629 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002630 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002631 get_jack_mode_name(codec, pin, name, sizeof(name));
2632 knew = snd_hda_gen_add_kctl(spec, name,
2633 &out_jack_mode_enum);
2634 if (!knew)
2635 return -ENOMEM;
2636 knew->private_value = pin;
2637 }
2638 }
2639
2640 return 0;
2641}
2642
Takashi Iwai294765582013-01-17 09:52:11 +01002643/*
2644 * input jack mode
2645 */
2646
2647/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2648#define NUM_VREFS 6
2649
2650static const char * const vref_texts[NUM_VREFS] = {
2651 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2652 "", "Mic 80pc Bias", "Mic 100pc Bias"
2653};
2654
2655static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2656{
2657 unsigned int pincap;
2658
2659 pincap = snd_hda_query_pin_caps(codec, pin);
2660 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2661 /* filter out unusual vrefs */
2662 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2663 return pincap;
2664}
2665
2666/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2667static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2668{
2669 unsigned int i, n = 0;
2670
2671 for (i = 0; i < NUM_VREFS; i++) {
2672 if (vref_caps & (1 << i)) {
2673 if (n == item_idx)
2674 return i;
2675 n++;
2676 }
2677 }
2678 return 0;
2679}
2680
2681/* convert back from the vref ctl index to the enum item index */
2682static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2683{
2684 unsigned int i, n = 0;
2685
2686 for (i = 0; i < NUM_VREFS; i++) {
2687 if (i == idx)
2688 return n;
2689 if (vref_caps & (1 << i))
2690 n++;
2691 }
2692 return 0;
2693}
2694
2695static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2696 struct snd_ctl_elem_info *uinfo)
2697{
2698 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2699 hda_nid_t nid = kcontrol->private_value;
2700 unsigned int vref_caps = get_vref_caps(codec, nid);
2701
2702 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2703 vref_texts);
2704 /* set the right text */
2705 strcpy(uinfo->value.enumerated.name,
2706 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2707 return 0;
2708}
2709
2710static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2711 struct snd_ctl_elem_value *ucontrol)
2712{
2713 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2714 hda_nid_t nid = kcontrol->private_value;
2715 unsigned int vref_caps = get_vref_caps(codec, nid);
2716 unsigned int idx;
2717
2718 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2719 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2720 return 0;
2721}
2722
2723static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2724 struct snd_ctl_elem_value *ucontrol)
2725{
2726 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2727 hda_nid_t nid = kcontrol->private_value;
2728 unsigned int vref_caps = get_vref_caps(codec, nid);
2729 unsigned int val, idx;
2730
2731 val = snd_hda_codec_get_pin_target(codec, nid);
2732 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2733 if (idx == ucontrol->value.enumerated.item[0])
2734 return 0;
2735
2736 val &= ~AC_PINCTL_VREFEN;
2737 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2738 snd_hda_set_pin_ctl_cache(codec, nid, val);
2739 return 1;
2740}
2741
2742static const struct snd_kcontrol_new in_jack_mode_enum = {
2743 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2744 .info = in_jack_mode_info,
2745 .get = in_jack_mode_get,
2746 .put = in_jack_mode_put,
2747};
2748
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002749static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2750{
2751 struct hda_gen_spec *spec = codec->spec;
2752 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002753 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002754 nitems = hweight32(get_vref_caps(codec, pin));
2755 return nitems ? nitems : 1;
2756}
2757
Takashi Iwai294765582013-01-17 09:52:11 +01002758static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2759{
2760 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002761 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002762 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002763 unsigned int defcfg;
2764
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002765 if (pin == spec->hp_mic_pin)
2766 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002767
2768 /* no jack mode for fixed pins */
2769 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2770 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2771 return 0;
2772
2773 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002774 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002775 return 0;
2776
2777 get_jack_mode_name(codec, pin, name, sizeof(name));
2778 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2779 if (!knew)
2780 return -ENOMEM;
2781 knew->private_value = pin;
2782 return 0;
2783}
2784
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002785/*
2786 * HP/mic shared jack mode
2787 */
2788static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2789 struct snd_ctl_elem_info *uinfo)
2790{
2791 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2792 hda_nid_t nid = kcontrol->private_value;
2793 int out_jacks = get_out_jack_num_items(codec, nid);
2794 int in_jacks = get_in_jack_num_items(codec, nid);
2795 const char *text = NULL;
2796 int idx;
2797
2798 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2799 uinfo->count = 1;
2800 uinfo->value.enumerated.items = out_jacks + in_jacks;
2801 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2802 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2803 idx = uinfo->value.enumerated.item;
2804 if (idx < out_jacks) {
2805 if (out_jacks > 1)
2806 text = out_jack_texts[idx];
2807 else
2808 text = "Headphone Out";
2809 } else {
2810 idx -= out_jacks;
2811 if (in_jacks > 1) {
2812 unsigned int vref_caps = get_vref_caps(codec, nid);
2813 text = vref_texts[get_vref_idx(vref_caps, idx)];
2814 } else
2815 text = "Mic In";
2816 }
2817
2818 strcpy(uinfo->value.enumerated.name, text);
2819 return 0;
2820}
2821
2822static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2823{
2824 int out_jacks = get_out_jack_num_items(codec, nid);
2825 int in_jacks = get_in_jack_num_items(codec, nid);
2826 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2827 int idx = 0;
2828
2829 if (val & PIN_OUT) {
2830 if (out_jacks > 1 && val == PIN_HP)
2831 idx = 1;
2832 } else if (val & PIN_IN) {
2833 idx = out_jacks;
2834 if (in_jacks > 1) {
2835 unsigned int vref_caps = get_vref_caps(codec, nid);
2836 val &= AC_PINCTL_VREFEN;
2837 idx += cvt_from_vref_idx(vref_caps, val);
2838 }
2839 }
2840 return idx;
2841}
2842
2843static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2844 struct snd_ctl_elem_value *ucontrol)
2845{
2846 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2847 hda_nid_t nid = kcontrol->private_value;
2848 ucontrol->value.enumerated.item[0] =
2849 get_cur_hp_mic_jack_mode(codec, nid);
2850 return 0;
2851}
2852
2853static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
2855{
2856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2857 hda_nid_t nid = kcontrol->private_value;
2858 int out_jacks = get_out_jack_num_items(codec, nid);
2859 int in_jacks = get_in_jack_num_items(codec, nid);
2860 unsigned int val, oldval, idx;
2861
2862 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2863 idx = ucontrol->value.enumerated.item[0];
2864 if (oldval == idx)
2865 return 0;
2866
2867 if (idx < out_jacks) {
2868 if (out_jacks > 1)
2869 val = idx ? PIN_HP : PIN_OUT;
2870 else
2871 val = PIN_HP;
2872 } else {
2873 idx -= out_jacks;
2874 if (in_jacks > 1) {
2875 unsigned int vref_caps = get_vref_caps(codec, nid);
2876 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002877 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2878 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002879 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002880 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002881 }
2882 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002883 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002884
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002885 return 1;
2886}
2887
2888static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2889 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2890 .info = hp_mic_jack_mode_info,
2891 .get = hp_mic_jack_mode_get,
2892 .put = hp_mic_jack_mode_put,
2893};
2894
2895static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2896{
2897 struct hda_gen_spec *spec = codec->spec;
2898 struct snd_kcontrol_new *knew;
2899
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002900 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2901 &hp_mic_jack_mode_enum);
2902 if (!knew)
2903 return -ENOMEM;
2904 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002905 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002906 return 0;
2907}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002908
2909/*
2910 * Parse input paths
2911 */
2912
Takashi Iwai352f7f92012-12-19 12:52:06 +01002913/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002914static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002915{
2916 struct hda_amp_list *list;
2917
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002918 list = snd_array_new(&spec->loopback_list);
2919 if (!list)
2920 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002921 list->nid = mix;
2922 list->dir = HDA_INPUT;
2923 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002924 spec->loopback.amplist = spec->loopback_list.list;
2925 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002926}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002927
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002928/* return true if either a volume or a mute amp is found for the given
2929 * aamix path; the amp has to be either in the mixer node or its direct leaf
2930 */
2931static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2932 hda_nid_t pin, unsigned int *mix_val,
2933 unsigned int *mute_val)
2934{
2935 int idx, num_conns;
2936 const hda_nid_t *list;
2937 hda_nid_t nid;
2938
2939 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2940 if (idx < 0)
2941 return false;
2942
2943 *mix_val = *mute_val = 0;
2944 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2945 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2946 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2947 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2948 if (*mix_val && *mute_val)
2949 return true;
2950
2951 /* check leaf node */
2952 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2953 if (num_conns < idx)
2954 return false;
2955 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01002956 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2957 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002958 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01002959 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2960 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002961 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2962
2963 return *mix_val || *mute_val;
2964}
2965
Takashi Iwai352f7f92012-12-19 12:52:06 +01002966/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002967static int new_analog_input(struct hda_codec *codec, int input_idx,
2968 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002969 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002971 struct hda_gen_spec *spec = codec->spec;
2972 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002973 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002974 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002976 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2977 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002978
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002979 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002980 if (!path)
2981 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002982 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002983 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002984
2985 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002986 if (mix_val) {
2987 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002988 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002990 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 }
2992
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002993 if (mute_val) {
2994 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002995 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002997 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 }
2999
Takashi Iwai352f7f92012-12-19 12:52:06 +01003000 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003001 err = add_loopback_list(spec, mix_nid, idx);
3002 if (err < 0)
3003 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003004
3005 if (spec->mixer_nid != spec->mixer_merge_nid &&
3006 !spec->loopback_merge_path) {
3007 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3008 spec->mixer_merge_nid, 0);
3009 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003010 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003011 path->active = true;
3012 spec->loopback_merge_path =
3013 snd_hda_get_path_idx(codec, path);
3014 }
3015 }
3016
Takashi Iwai352f7f92012-12-19 12:52:06 +01003017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
3019
Takashi Iwai352f7f92012-12-19 12:52:06 +01003020static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003022 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3023 return (pincap & AC_PINCAP_IN) != 0;
3024}
3025
3026/* Parse the codec tree and retrieve ADCs */
3027static int fill_adc_nids(struct hda_codec *codec)
3028{
3029 struct hda_gen_spec *spec = codec->spec;
3030 hda_nid_t nid;
3031 hda_nid_t *adc_nids = spec->adc_nids;
3032 int max_nums = ARRAY_SIZE(spec->adc_nids);
3033 int i, nums = 0;
3034
3035 nid = codec->start_nid;
3036 for (i = 0; i < codec->num_nodes; i++, nid++) {
3037 unsigned int caps = get_wcaps(codec, nid);
3038 int type = get_wcaps_type(caps);
3039
3040 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3041 continue;
3042 adc_nids[nums] = nid;
3043 if (++nums >= max_nums)
3044 break;
3045 }
3046 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003047
3048 /* copy the detected ADCs to all_adcs[] */
3049 spec->num_all_adcs = nums;
3050 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3051
Takashi Iwai352f7f92012-12-19 12:52:06 +01003052 return nums;
3053}
3054
3055/* filter out invalid adc_nids that don't give all active input pins;
3056 * if needed, check whether dynamic ADC-switching is available
3057 */
3058static int check_dyn_adc_switch(struct hda_codec *codec)
3059{
3060 struct hda_gen_spec *spec = codec->spec;
3061 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003062 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003063 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003064
Takashi Iwai352f7f92012-12-19 12:52:06 +01003065 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003066 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003067 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003068 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003069 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003070 break;
3071 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003072 if (i >= imux->num_items) {
3073 ok_bits |= (1 << n);
3074 nums++;
3075 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003076 }
3077
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003078 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003079 /* check whether ADC-switch is possible */
3080 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003081 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003082 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003083 spec->dyn_adc_idx[i] = n;
3084 break;
3085 }
3086 }
3087 }
3088
Takashi Iwai4e76a882014-02-25 12:21:03 +01003089 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003090 spec->dyn_adc_switch = 1;
3091 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003092 /* shrink the invalid adcs and input paths */
3093 nums = 0;
3094 for (n = 0; n < spec->num_adc_nids; n++) {
3095 if (!(ok_bits & (1 << n)))
3096 continue;
3097 if (n != nums) {
3098 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003099 for (i = 0; i < imux->num_items; i++) {
3100 invalidate_nid_path(codec,
3101 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003102 spec->input_paths[i][nums] =
3103 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003104 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003105 }
3106 nums++;
3107 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003108 spec->num_adc_nids = nums;
3109 }
3110
Takashi Iwai967303d2013-02-19 17:12:42 +01003111 if (imux->num_items == 1 ||
3112 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003113 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003114 spec->num_adc_nids = 1; /* reduce to a single ADC */
3115 }
3116
3117 /* single index for individual volumes ctls */
3118 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3119 spec->num_adc_nids = 1;
3120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return 0;
3122}
3123
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003124/* parse capture source paths from the given pin and create imux items */
3125static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003126 int cfg_idx, int num_adcs,
3127 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003128{
3129 struct hda_gen_spec *spec = codec->spec;
3130 struct hda_input_mux *imux = &spec->input_mux;
3131 int imux_idx = imux->num_items;
3132 bool imux_added = false;
3133 int c;
3134
3135 for (c = 0; c < num_adcs; c++) {
3136 struct nid_path *path;
3137 hda_nid_t adc = spec->adc_nids[c];
3138
3139 if (!is_reachable_path(codec, pin, adc))
3140 continue;
3141 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3142 if (!path)
3143 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003144 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003145 spec->input_paths[imux_idx][c] =
3146 snd_hda_get_path_idx(codec, path);
3147
3148 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003149 if (spec->hp_mic_pin == pin)
3150 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003151 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003152 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003153 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003154 if (spec->dyn_adc_switch)
3155 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003156 }
3157 }
3158
3159 return 0;
3160}
3161
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003163 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003165
Takashi Iwaic9700422013-01-18 10:17:30 +01003166/* fill the label for each input at first */
3167static int fill_input_pin_labels(struct hda_codec *codec)
3168{
3169 struct hda_gen_spec *spec = codec->spec;
3170 const struct auto_pin_cfg *cfg = &spec->autocfg;
3171 int i;
3172
3173 for (i = 0; i < cfg->num_inputs; i++) {
3174 hda_nid_t pin = cfg->inputs[i].pin;
3175 const char *label;
3176 int j, idx;
3177
3178 if (!is_input_pin(codec, pin))
3179 continue;
3180
3181 label = hda_get_autocfg_input_label(codec, cfg, i);
3182 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003183 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003184 if (spec->input_labels[j] &&
3185 !strcmp(spec->input_labels[j], label)) {
3186 idx = spec->input_label_idxs[j] + 1;
3187 break;
3188 }
3189 }
3190
3191 spec->input_labels[i] = label;
3192 spec->input_label_idxs[i] = idx;
3193 }
3194
3195 return 0;
3196}
3197
Takashi Iwai9dba2052013-01-18 10:01:15 +01003198#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3199
Takashi Iwai352f7f92012-12-19 12:52:06 +01003200static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003201{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003202 struct hda_gen_spec *spec = codec->spec;
3203 const struct auto_pin_cfg *cfg = &spec->autocfg;
3204 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003205 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003206 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003207 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003208
Takashi Iwai352f7f92012-12-19 12:52:06 +01003209 num_adcs = fill_adc_nids(codec);
3210 if (num_adcs < 0)
3211 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003212
Takashi Iwaic9700422013-01-18 10:17:30 +01003213 err = fill_input_pin_labels(codec);
3214 if (err < 0)
3215 return err;
3216
Takashi Iwai352f7f92012-12-19 12:52:06 +01003217 for (i = 0; i < cfg->num_inputs; i++) {
3218 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Takashi Iwai352f7f92012-12-19 12:52:06 +01003220 pin = cfg->inputs[i].pin;
3221 if (!is_input_pin(codec, pin))
3222 continue;
3223
Takashi Iwai2c12c302013-01-10 09:33:29 +01003224 val = PIN_IN;
3225 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3226 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003227 if (pin != spec->hp_mic_pin)
3228 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003229
Takashi Iwai352f7f92012-12-19 12:52:06 +01003230 if (mixer) {
3231 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003232 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003233 spec->input_labels[i],
3234 spec->input_label_idxs[i],
3235 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003236 if (err < 0)
3237 return err;
3238 }
3239 }
3240
Takashi Iwaic9700422013-01-18 10:17:30 +01003241 err = parse_capture_source(codec, pin, i, num_adcs,
3242 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003243 if (err < 0)
3244 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003245
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003246 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003247 err = create_in_jack_mode(codec, pin);
3248 if (err < 0)
3249 return err;
3250 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003251 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003252
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003253 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003254 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003255 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003256 "Stereo Mix", 0);
3257 if (err < 0)
3258 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003259 else
3260 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003261 }
3262
3263 return 0;
3264}
3265
3266
3267/*
3268 * input source mux
3269 */
3270
Takashi Iwaic697b712013-01-07 17:09:26 +01003271/* get the input path specified by the given adc and imux indices */
3272static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003273{
3274 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003275 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3276 snd_BUG();
3277 return NULL;
3278 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003279 if (spec->dyn_adc_switch)
3280 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003281 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003282 snd_BUG();
3283 return NULL;
3284 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003285 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003286}
3287
3288static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3289 unsigned int idx);
3290
3291static int mux_enum_info(struct snd_kcontrol *kcontrol,
3292 struct snd_ctl_elem_info *uinfo)
3293{
3294 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3295 struct hda_gen_spec *spec = codec->spec;
3296 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3297}
3298
3299static int mux_enum_get(struct snd_kcontrol *kcontrol,
3300 struct snd_ctl_elem_value *ucontrol)
3301{
3302 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3303 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003304 /* the ctls are created at once with multiple counts */
3305 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003306
3307 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3308 return 0;
3309}
3310
3311static int mux_enum_put(struct snd_kcontrol *kcontrol,
3312 struct snd_ctl_elem_value *ucontrol)
3313{
3314 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003315 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003316 return mux_select(codec, adc_idx,
3317 ucontrol->value.enumerated.item[0]);
3318}
3319
Takashi Iwai352f7f92012-12-19 12:52:06 +01003320static const struct snd_kcontrol_new cap_src_temp = {
3321 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3322 .name = "Input Source",
3323 .info = mux_enum_info,
3324 .get = mux_enum_get,
3325 .put = mux_enum_put,
3326};
3327
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003328/*
3329 * capture volume and capture switch ctls
3330 */
3331
Takashi Iwai352f7f92012-12-19 12:52:06 +01003332typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3333 struct snd_ctl_elem_value *ucontrol);
3334
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003335/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003336static int cap_put_caller(struct snd_kcontrol *kcontrol,
3337 struct snd_ctl_elem_value *ucontrol,
3338 put_call_t func, int type)
3339{
3340 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3341 struct hda_gen_spec *spec = codec->spec;
3342 const struct hda_input_mux *imux;
3343 struct nid_path *path;
3344 int i, adc_idx, err = 0;
3345
3346 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003347 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003348 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003349 /* we use the cache-only update at first since multiple input paths
3350 * may shared the same amp; by updating only caches, the redundant
3351 * writes to hardware can be reduced.
3352 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003353 codec->cached_write = 1;
3354 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003355 path = get_input_path(codec, adc_idx, i);
3356 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003357 continue;
3358 kcontrol->private_value = path->ctls[type];
3359 err = func(kcontrol, ucontrol);
3360 if (err < 0)
3361 goto error;
3362 }
3363 error:
3364 codec->cached_write = 0;
3365 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003366 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003367 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003368 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003369 return err;
3370}
3371
3372/* capture volume ctl callbacks */
3373#define cap_vol_info snd_hda_mixer_amp_volume_info
3374#define cap_vol_get snd_hda_mixer_amp_volume_get
3375#define cap_vol_tlv snd_hda_mixer_amp_tlv
3376
3377static int cap_vol_put(struct snd_kcontrol *kcontrol,
3378 struct snd_ctl_elem_value *ucontrol)
3379{
3380 return cap_put_caller(kcontrol, ucontrol,
3381 snd_hda_mixer_amp_volume_put,
3382 NID_PATH_VOL_CTL);
3383}
3384
3385static const struct snd_kcontrol_new cap_vol_temp = {
3386 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3387 .name = "Capture Volume",
3388 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3389 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3390 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3391 .info = cap_vol_info,
3392 .get = cap_vol_get,
3393 .put = cap_vol_put,
3394 .tlv = { .c = cap_vol_tlv },
3395};
3396
3397/* capture switch ctl callbacks */
3398#define cap_sw_info snd_ctl_boolean_stereo_info
3399#define cap_sw_get snd_hda_mixer_amp_switch_get
3400
3401static int cap_sw_put(struct snd_kcontrol *kcontrol,
3402 struct snd_ctl_elem_value *ucontrol)
3403{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003404 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003405 snd_hda_mixer_amp_switch_put,
3406 NID_PATH_MUTE_CTL);
3407}
3408
3409static const struct snd_kcontrol_new cap_sw_temp = {
3410 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3411 .name = "Capture Switch",
3412 .info = cap_sw_info,
3413 .get = cap_sw_get,
3414 .put = cap_sw_put,
3415};
3416
3417static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3418{
3419 hda_nid_t nid;
3420 int i, depth;
3421
3422 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3423 for (depth = 0; depth < 3; depth++) {
3424 if (depth >= path->depth)
3425 return -EINVAL;
3426 i = path->depth - depth - 1;
3427 nid = path->path[i];
3428 if (!path->ctls[NID_PATH_VOL_CTL]) {
3429 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3430 path->ctls[NID_PATH_VOL_CTL] =
3431 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3432 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3433 int idx = path->idx[i];
3434 if (!depth && codec->single_adc_amp)
3435 idx = 0;
3436 path->ctls[NID_PATH_VOL_CTL] =
3437 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3438 }
3439 }
3440 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3441 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3442 path->ctls[NID_PATH_MUTE_CTL] =
3443 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3444 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3445 int idx = path->idx[i];
3446 if (!depth && codec->single_adc_amp)
3447 idx = 0;
3448 path->ctls[NID_PATH_MUTE_CTL] =
3449 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3450 }
3451 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 return 0;
3454}
3455
Takashi Iwai352f7f92012-12-19 12:52:06 +01003456static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003458 struct hda_gen_spec *spec = codec->spec;
3459 struct auto_pin_cfg *cfg = &spec->autocfg;
3460 unsigned int val;
3461 int i;
3462
3463 if (!spec->inv_dmic_split)
3464 return false;
3465 for (i = 0; i < cfg->num_inputs; i++) {
3466 if (cfg->inputs[i].pin != nid)
3467 continue;
3468 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3469 return false;
3470 val = snd_hda_codec_get_pincfg(codec, nid);
3471 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3472 }
3473 return false;
3474}
3475
Takashi Iwaia90229e2013-01-18 14:10:00 +01003476/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003477static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3478 struct snd_ctl_elem_value *ucontrol)
3479{
3480 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3481 struct hda_gen_spec *spec = codec->spec;
3482 int ret;
3483
3484 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3485 if (ret < 0)
3486 return ret;
3487
Takashi Iwaia90229e2013-01-18 14:10:00 +01003488 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003489 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003490
3491 return ret;
3492}
3493
Takashi Iwai352f7f92012-12-19 12:52:06 +01003494static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3495 int idx, bool is_switch, unsigned int ctl,
3496 bool inv_dmic)
3497{
3498 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003499 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003500 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3501 const char *sfx = is_switch ? "Switch" : "Volume";
3502 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003503 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003504
3505 if (!ctl)
3506 return 0;
3507
3508 if (label)
3509 snprintf(tmpname, sizeof(tmpname),
3510 "%s Capture %s", label, sfx);
3511 else
3512 snprintf(tmpname, sizeof(tmpname),
3513 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003514 knew = add_control(spec, type, tmpname, idx,
3515 amp_val_replace_channels(ctl, chs));
3516 if (!knew)
3517 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003518 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003519 knew->put = cap_single_sw_put;
3520 if (!inv_dmic)
3521 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003522
3523 /* Make independent right kcontrol */
3524 if (label)
3525 snprintf(tmpname, sizeof(tmpname),
3526 "Inverted %s Capture %s", label, sfx);
3527 else
3528 snprintf(tmpname, sizeof(tmpname),
3529 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003530 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003531 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003532 if (!knew)
3533 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003534 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003535 knew->put = cap_single_sw_put;
3536 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003537}
3538
3539/* create single (and simple) capture volume and switch controls */
3540static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3541 unsigned int vol_ctl, unsigned int sw_ctl,
3542 bool inv_dmic)
3543{
3544 int err;
3545 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3546 if (err < 0)
3547 return err;
3548 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3549 if (err < 0)
3550 return err;
3551 return 0;
3552}
3553
3554/* create bound capture volume and switch controls */
3555static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3556 unsigned int vol_ctl, unsigned int sw_ctl)
3557{
3558 struct hda_gen_spec *spec = codec->spec;
3559 struct snd_kcontrol_new *knew;
3560
3561 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003562 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003563 if (!knew)
3564 return -ENOMEM;
3565 knew->index = idx;
3566 knew->private_value = vol_ctl;
3567 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3568 }
3569 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003570 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003571 if (!knew)
3572 return -ENOMEM;
3573 knew->index = idx;
3574 knew->private_value = sw_ctl;
3575 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3576 }
3577 return 0;
3578}
3579
3580/* return the vol ctl when used first in the imux list */
3581static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3582{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003583 struct nid_path *path;
3584 unsigned int ctl;
3585 int i;
3586
Takashi Iwaic697b712013-01-07 17:09:26 +01003587 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003588 if (!path)
3589 return 0;
3590 ctl = path->ctls[type];
3591 if (!ctl)
3592 return 0;
3593 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003594 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003595 if (path && path->ctls[type] == ctl)
3596 return 0;
3597 }
3598 return ctl;
3599}
3600
3601/* create individual capture volume and switch controls per input */
3602static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3603{
3604 struct hda_gen_spec *spec = codec->spec;
3605 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003606 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003607
3608 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003609 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003610 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003611
Takashi Iwaic9700422013-01-18 10:17:30 +01003612 idx = imux->items[i].index;
3613 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003614 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003615 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3616
3617 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003618 err = add_single_cap_ctl(codec,
3619 spec->input_labels[idx],
3620 spec->input_label_idxs[idx],
3621 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003622 get_first_cap_ctl(codec, i, type),
3623 inv_dmic);
3624 if (err < 0)
3625 return err;
3626 }
3627 }
3628 return 0;
3629}
3630
3631static int create_capture_mixers(struct hda_codec *codec)
3632{
3633 struct hda_gen_spec *spec = codec->spec;
3634 struct hda_input_mux *imux = &spec->input_mux;
3635 int i, n, nums, err;
3636
3637 if (spec->dyn_adc_switch)
3638 nums = 1;
3639 else
3640 nums = spec->num_adc_nids;
3641
3642 if (!spec->auto_mic && imux->num_items > 1) {
3643 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003644 const char *name;
3645 name = nums > 1 ? "Input Source" : "Capture Source";
3646 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003647 if (!knew)
3648 return -ENOMEM;
3649 knew->count = nums;
3650 }
3651
3652 for (n = 0; n < nums; n++) {
3653 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003654 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003655 bool inv_dmic = false;
3656 int vol, sw;
3657
3658 vol = sw = 0;
3659 for (i = 0; i < imux->num_items; i++) {
3660 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003661 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003662 if (!path)
3663 continue;
3664 parse_capvol_in_path(codec, path);
3665 if (!vol)
3666 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003667 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003668 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003669 if (!same_amp_caps(codec, vol,
3670 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3671 multi_cap_vol = true;
3672 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003673 if (!sw)
3674 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003675 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003676 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003677 if (!same_amp_caps(codec, sw,
3678 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3679 multi_cap_vol = true;
3680 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003681 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3682 inv_dmic = true;
3683 }
3684
3685 if (!multi)
3686 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3687 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003688 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003689 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3690 else
3691 err = create_multi_cap_vol_ctl(codec);
3692 if (err < 0)
3693 return err;
3694 }
3695
3696 return 0;
3697}
3698
3699/*
3700 * add mic boosts if needed
3701 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003702
3703/* check whether the given amp is feasible as a boost volume */
3704static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3705 int dir, int idx)
3706{
3707 unsigned int step;
3708
3709 if (!nid_has_volume(codec, nid, dir) ||
3710 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3711 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3712 return false;
3713
3714 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3715 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3716 if (step < 0x20)
3717 return false;
3718 return true;
3719}
3720
3721/* look for a boost amp in a widget close to the pin */
3722static unsigned int look_for_boost_amp(struct hda_codec *codec,
3723 struct nid_path *path)
3724{
3725 unsigned int val = 0;
3726 hda_nid_t nid;
3727 int depth;
3728
3729 for (depth = 0; depth < 3; depth++) {
3730 if (depth >= path->depth - 1)
3731 break;
3732 nid = path->path[depth];
3733 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3734 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3735 break;
3736 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3737 path->idx[depth])) {
3738 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3739 HDA_INPUT);
3740 break;
3741 }
3742 }
3743
3744 return val;
3745}
3746
Takashi Iwai352f7f92012-12-19 12:52:06 +01003747static int parse_mic_boost(struct hda_codec *codec)
3748{
3749 struct hda_gen_spec *spec = codec->spec;
3750 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003751 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003752 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003753
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003754 if (!spec->num_adc_nids)
3755 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003756
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003757 for (i = 0; i < imux->num_items; i++) {
3758 struct nid_path *path;
3759 unsigned int val;
3760 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003761 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003762
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003763 idx = imux->items[i].index;
3764 if (idx >= imux->num_items)
3765 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003766
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003767 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003768 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003769 continue;
3770
3771 path = get_input_path(codec, 0, i);
3772 if (!path)
3773 continue;
3774
3775 val = look_for_boost_amp(codec, path);
3776 if (!val)
3777 continue;
3778
3779 /* create a boost control */
3780 snprintf(boost_label, sizeof(boost_label),
3781 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003782 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3783 spec->input_label_idxs[idx], val))
3784 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003785
3786 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003787 }
3788 return 0;
3789}
3790
3791/*
3792 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3793 */
3794static void parse_digital(struct hda_codec *codec)
3795{
3796 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003797 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003798 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003799 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003800
3801 /* support multiple SPDIFs; the secondary is set up as a slave */
3802 nums = 0;
3803 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003804 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003805 dig_nid = look_for_dac(codec, pin, true);
3806 if (!dig_nid)
3807 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003808 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003809 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003810 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003811 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003812 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003813 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003814 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003815 if (!nums) {
3816 spec->multiout.dig_out_nid = dig_nid;
3817 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3818 } else {
3819 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3820 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd576422e2014-05-14 17:18:31 +03003821 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003822 spec->slave_dig_outs[nums - 1] = dig_nid;
3823 }
3824 nums++;
3825 }
3826
3827 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003828 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003829 dig_nid = codec->start_nid;
3830 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003831 unsigned int wcaps = get_wcaps(codec, dig_nid);
3832 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3833 continue;
3834 if (!(wcaps & AC_WCAP_DIGITAL))
3835 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003836 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003837 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003838 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003839 path->active = true;
3840 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003841 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003842 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003843 break;
3844 }
3845 }
3846 }
3847}
3848
3849
3850/*
3851 * input MUX handling
3852 */
3853
3854static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3855
3856/* select the given imux item; either unmute exclusively or select the route */
3857static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3858 unsigned int idx)
3859{
3860 struct hda_gen_spec *spec = codec->spec;
3861 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003862 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003863
3864 imux = &spec->input_mux;
3865 if (!imux->num_items)
3866 return 0;
3867
3868 if (idx >= imux->num_items)
3869 idx = imux->num_items - 1;
3870 if (spec->cur_mux[adc_idx] == idx)
3871 return 0;
3872
Takashi Iwai55196ff2013-01-24 17:32:56 +01003873 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3874 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003875 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003876 if (old_path->active)
3877 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003878
3879 spec->cur_mux[adc_idx] = idx;
3880
Takashi Iwai967303d2013-02-19 17:12:42 +01003881 if (spec->hp_mic)
3882 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003883
3884 if (spec->dyn_adc_switch)
3885 dyn_adc_pcm_resetup(codec, idx);
3886
Takashi Iwaic697b712013-01-07 17:09:26 +01003887 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003888 if (!path)
3889 return 0;
3890 if (path->active)
3891 return 0;
3892 snd_hda_activate_path(codec, path, true, false);
3893 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003894 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003895 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003896 return 1;
3897}
3898
3899
3900/*
3901 * Jack detections for HP auto-mute and mic-switch
3902 */
3903
3904/* check each pin in the given array; returns true if any of them is plugged */
3905static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3906{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003907 int i;
3908 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003909
3910 for (i = 0; i < num_pins; i++) {
3911 hda_nid_t nid = pins[i];
3912 if (!nid)
3913 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003914 /* don't detect pins retasked as inputs */
3915 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3916 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003917 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3918 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003919 }
3920 return present;
3921}
3922
3923/* standard HP/line-out auto-mute helper */
3924static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003925 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003926{
3927 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003928 int i;
3929
3930 for (i = 0; i < num_pins; i++) {
3931 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003932 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003933 if (!nid)
3934 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003935
3936 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003937 struct nid_path *path;
3938 hda_nid_t mute_nid;
3939
3940 path = snd_hda_get_path_from_idx(codec, paths[i]);
3941 if (!path)
3942 continue;
3943 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3944 if (!mute_nid)
3945 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003946 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003947 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003948 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003949 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003950 set_pin_eapd(codec, nid, !mute);
3951 continue;
3952 }
3953
Takashi Iwai967303d2013-02-19 17:12:42 +01003954 oldval = snd_hda_codec_get_pin_target(codec, nid);
3955 if (oldval & PIN_IN)
3956 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003957 /* don't reset VREF value in case it's controlling
3958 * the amp (see alc861_fixup_asus_amp_vref_0f())
3959 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003960 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003961 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003962 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003963 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003964 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003965 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003966 /* here we call update_pin_ctl() so that the pinctl is changed
3967 * without changing the pinctl target value;
3968 * the original target value will be still referred at the
3969 * init / resume again
3970 */
3971 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003972 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003973 }
3974}
3975
Takashi Iwaidda42bd2014-10-30 12:04:50 +01003976/**
3977 * snd_hda_gen_update_outputs - Toggle outputs muting
3978 * @codec: the HDA codec
3979 *
3980 * Update the mute status of all outputs based on the current jack states.
3981 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003982void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003983{
3984 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003985 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003986 int on;
3987
3988 /* Control HP pins/amps depending on master_mute state;
3989 * in general, HP pins/amps control should be enabled in all cases,
3990 * but currently set only for master_mute, just to be safe
3991 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003992 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3993 paths = spec->out_paths;
3994 else
3995 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01003996 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003997 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003998
3999 if (!spec->automute_speaker)
4000 on = 0;
4001 else
4002 on = spec->hp_jack_present | spec->line_jack_present;
4003 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01004004 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004005 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4006 paths = spec->out_paths;
4007 else
4008 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004009 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004010 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004011
4012 /* toggle line-out mutes if needed, too */
4013 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4014 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4015 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4016 return;
4017 if (!spec->automute_lo)
4018 on = 0;
4019 else
4020 on = spec->hp_jack_present;
4021 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01004022 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004023 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004024 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004025 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004026}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004027EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004028
4029static void call_update_outputs(struct hda_codec *codec)
4030{
4031 struct hda_gen_spec *spec = codec->spec;
4032 if (spec->automute_hook)
4033 spec->automute_hook(codec);
4034 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004035 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004036
4037 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4038 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4039 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004040}
4041
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004042/**
4043 * snd_hda_gen_hp_automute - standard HP-automute helper
4044 * @codec: the HDA codec
4045 * @jack: jack object, NULL for the whole
4046 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004047void snd_hda_gen_hp_automute(struct hda_codec *codec,
4048 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004049{
4050 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004051 hda_nid_t *pins = spec->autocfg.hp_pins;
4052 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004053
Takashi Iwai92603c52013-01-22 07:46:31 +01004054 /* No detection for the first HP jack during indep-HP mode */
4055 if (spec->indep_hp_enabled) {
4056 pins++;
4057 num_pins--;
4058 }
4059
4060 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004061 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4062 return;
4063 call_update_outputs(codec);
4064}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004065EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004066
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004067/**
4068 * snd_hda_gen_line_automute - standard line-out-automute helper
4069 * @codec: the HDA codec
4070 * @jack: jack object, NULL for the whole
4071 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004072void snd_hda_gen_line_automute(struct hda_codec *codec,
4073 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004074{
4075 struct hda_gen_spec *spec = codec->spec;
4076
4077 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4078 return;
4079 /* check LO jack only when it's different from HP */
4080 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4081 return;
4082
4083 spec->line_jack_present =
4084 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4085 spec->autocfg.line_out_pins);
4086 if (!spec->automute_speaker || !spec->detect_lo)
4087 return;
4088 call_update_outputs(codec);
4089}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004090EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004091
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004092/**
4093 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4094 * @codec: the HDA codec
4095 * @jack: jack object, NULL for the whole
4096 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004097void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4098 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004099{
4100 struct hda_gen_spec *spec = codec->spec;
4101 int i;
4102
4103 if (!spec->auto_mic)
4104 return;
4105
4106 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004107 hda_nid_t pin = spec->am_entry[i].pin;
4108 /* don't detect pins retasked as outputs */
4109 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4110 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004111 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004112 mux_select(codec, 0, spec->am_entry[i].idx);
4113 return;
4114 }
4115 }
4116 mux_select(codec, 0, spec->am_entry[0].idx);
4117}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004118EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004119
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004120/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004121static void call_hp_automute(struct hda_codec *codec,
4122 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004123{
4124 struct hda_gen_spec *spec = codec->spec;
4125 if (spec->hp_automute_hook)
4126 spec->hp_automute_hook(codec, jack);
4127 else
4128 snd_hda_gen_hp_automute(codec, jack);
4129}
4130
4131static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004132 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004133{
4134 struct hda_gen_spec *spec = codec->spec;
4135 if (spec->line_automute_hook)
4136 spec->line_automute_hook(codec, jack);
4137 else
4138 snd_hda_gen_line_automute(codec, jack);
4139}
4140
4141static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004142 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004143{
4144 struct hda_gen_spec *spec = codec->spec;
4145 if (spec->mic_autoswitch_hook)
4146 spec->mic_autoswitch_hook(codec, jack);
4147 else
4148 snd_hda_gen_mic_autoswitch(codec, jack);
4149}
4150
Takashi Iwai963afde2013-05-31 15:20:31 +02004151/* update jack retasking */
4152static void update_automute_all(struct hda_codec *codec)
4153{
4154 call_hp_automute(codec, NULL);
4155 call_line_automute(codec, NULL);
4156 call_mic_autoswitch(codec, NULL);
4157}
4158
Takashi Iwai352f7f92012-12-19 12:52:06 +01004159/*
4160 * Auto-Mute mode mixer enum support
4161 */
4162static int automute_mode_info(struct snd_kcontrol *kcontrol,
4163 struct snd_ctl_elem_info *uinfo)
4164{
4165 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4166 struct hda_gen_spec *spec = codec->spec;
4167 static const char * const texts3[] = {
4168 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004169 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
Takashi Iwai352f7f92012-12-19 12:52:06 +01004171 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4172 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4173 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4174}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Takashi Iwai352f7f92012-12-19 12:52:06 +01004176static int automute_mode_get(struct snd_kcontrol *kcontrol,
4177 struct snd_ctl_elem_value *ucontrol)
4178{
4179 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4180 struct hda_gen_spec *spec = codec->spec;
4181 unsigned int val = 0;
4182 if (spec->automute_speaker)
4183 val++;
4184 if (spec->automute_lo)
4185 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004186
Takashi Iwai352f7f92012-12-19 12:52:06 +01004187 ucontrol->value.enumerated.item[0] = val;
4188 return 0;
4189}
4190
4191static int automute_mode_put(struct snd_kcontrol *kcontrol,
4192 struct snd_ctl_elem_value *ucontrol)
4193{
4194 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4195 struct hda_gen_spec *spec = codec->spec;
4196
4197 switch (ucontrol->value.enumerated.item[0]) {
4198 case 0:
4199 if (!spec->automute_speaker && !spec->automute_lo)
4200 return 0;
4201 spec->automute_speaker = 0;
4202 spec->automute_lo = 0;
4203 break;
4204 case 1:
4205 if (spec->automute_speaker_possible) {
4206 if (!spec->automute_lo && spec->automute_speaker)
4207 return 0;
4208 spec->automute_speaker = 1;
4209 spec->automute_lo = 0;
4210 } else if (spec->automute_lo_possible) {
4211 if (spec->automute_lo)
4212 return 0;
4213 spec->automute_lo = 1;
4214 } else
4215 return -EINVAL;
4216 break;
4217 case 2:
4218 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4219 return -EINVAL;
4220 if (spec->automute_speaker && spec->automute_lo)
4221 return 0;
4222 spec->automute_speaker = 1;
4223 spec->automute_lo = 1;
4224 break;
4225 default:
4226 return -EINVAL;
4227 }
4228 call_update_outputs(codec);
4229 return 1;
4230}
4231
4232static const struct snd_kcontrol_new automute_mode_enum = {
4233 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4234 .name = "Auto-Mute Mode",
4235 .info = automute_mode_info,
4236 .get = automute_mode_get,
4237 .put = automute_mode_put,
4238};
4239
4240static int add_automute_mode_enum(struct hda_codec *codec)
4241{
4242 struct hda_gen_spec *spec = codec->spec;
4243
Takashi Iwai12c93df2012-12-19 14:38:33 +01004244 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004245 return -ENOMEM;
4246 return 0;
4247}
4248
4249/*
4250 * Check the availability of HP/line-out auto-mute;
4251 * Set up appropriately if really supported
4252 */
4253static int check_auto_mute_availability(struct hda_codec *codec)
4254{
4255 struct hda_gen_spec *spec = codec->spec;
4256 struct auto_pin_cfg *cfg = &spec->autocfg;
4257 int present = 0;
4258 int i, err;
4259
Takashi Iwaif72706b2013-01-16 18:20:07 +01004260 if (spec->suppress_auto_mute)
4261 return 0;
4262
Takashi Iwai352f7f92012-12-19 12:52:06 +01004263 if (cfg->hp_pins[0])
4264 present++;
4265 if (cfg->line_out_pins[0])
4266 present++;
4267 if (cfg->speaker_pins[0])
4268 present++;
4269 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004270 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004271
4272 if (!cfg->speaker_pins[0] &&
4273 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4274 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4275 sizeof(cfg->speaker_pins));
4276 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
Takashi Iwai352f7f92012-12-19 12:52:06 +01004279 if (!cfg->hp_pins[0] &&
4280 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4281 memcpy(cfg->hp_pins, cfg->line_out_pins,
4282 sizeof(cfg->hp_pins));
4283 cfg->hp_outs = cfg->line_outs;
4284 }
4285
4286 for (i = 0; i < cfg->hp_outs; i++) {
4287 hda_nid_t nid = cfg->hp_pins[i];
4288 if (!is_jack_detectable(codec, nid))
4289 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004290 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004291 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004292 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004293 spec->detect_hp = 1;
4294 }
4295
4296 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4297 if (cfg->speaker_outs)
4298 for (i = 0; i < cfg->line_outs; i++) {
4299 hda_nid_t nid = cfg->line_out_pins[i];
4300 if (!is_jack_detectable(codec, nid))
4301 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004302 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004303 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004304 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004305 spec->detect_lo = 1;
4306 }
4307 spec->automute_lo_possible = spec->detect_hp;
4308 }
4309
4310 spec->automute_speaker_possible = cfg->speaker_outs &&
4311 (spec->detect_hp || spec->detect_lo);
4312
4313 spec->automute_lo = spec->automute_lo_possible;
4314 spec->automute_speaker = spec->automute_speaker_possible;
4315
4316 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4317 /* create a control for automute mode */
4318 err = add_automute_mode_enum(codec);
4319 if (err < 0)
4320 return err;
4321 }
4322 return 0;
4323}
4324
Takashi Iwai352f7f92012-12-19 12:52:06 +01004325/* check whether all auto-mic pins are valid; setup indices if OK */
4326static bool auto_mic_check_imux(struct hda_codec *codec)
4327{
4328 struct hda_gen_spec *spec = codec->spec;
4329 const struct hda_input_mux *imux;
4330 int i;
4331
4332 imux = &spec->input_mux;
4333 for (i = 0; i < spec->am_num_entries; i++) {
4334 spec->am_entry[i].idx =
4335 find_idx_in_nid_list(spec->am_entry[i].pin,
4336 spec->imux_pins, imux->num_items);
4337 if (spec->am_entry[i].idx < 0)
4338 return false; /* no corresponding imux */
4339 }
4340
4341 /* we don't need the jack detection for the first pin */
4342 for (i = 1; i < spec->am_num_entries; i++)
4343 snd_hda_jack_detect_enable_callback(codec,
4344 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004345 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004346 return true;
4347}
4348
4349static int compare_attr(const void *ap, const void *bp)
4350{
4351 const struct automic_entry *a = ap;
4352 const struct automic_entry *b = bp;
4353 return (int)(a->attr - b->attr);
4354}
4355
4356/*
4357 * Check the availability of auto-mic switch;
4358 * Set up if really supported
4359 */
4360static int check_auto_mic_availability(struct hda_codec *codec)
4361{
4362 struct hda_gen_spec *spec = codec->spec;
4363 struct auto_pin_cfg *cfg = &spec->autocfg;
4364 unsigned int types;
4365 int i, num_pins;
4366
Takashi Iwaid12daf62013-01-07 16:32:11 +01004367 if (spec->suppress_auto_mic)
4368 return 0;
4369
Takashi Iwai352f7f92012-12-19 12:52:06 +01004370 types = 0;
4371 num_pins = 0;
4372 for (i = 0; i < cfg->num_inputs; i++) {
4373 hda_nid_t nid = cfg->inputs[i].pin;
4374 unsigned int attr;
4375 attr = snd_hda_codec_get_pincfg(codec, nid);
4376 attr = snd_hda_get_input_pin_attr(attr);
4377 if (types & (1 << attr))
4378 return 0; /* already occupied */
4379 switch (attr) {
4380 case INPUT_PIN_ATTR_INT:
4381 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4382 return 0; /* invalid type */
4383 break;
4384 case INPUT_PIN_ATTR_UNUSED:
4385 return 0; /* invalid entry */
4386 default:
4387 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4388 return 0; /* invalid type */
4389 if (!spec->line_in_auto_switch &&
4390 cfg->inputs[i].type != AUTO_PIN_MIC)
4391 return 0; /* only mic is allowed */
4392 if (!is_jack_detectable(codec, nid))
4393 return 0; /* no unsol support */
4394 break;
4395 }
4396 if (num_pins >= MAX_AUTO_MIC_PINS)
4397 return 0;
4398 types |= (1 << attr);
4399 spec->am_entry[num_pins].pin = nid;
4400 spec->am_entry[num_pins].attr = attr;
4401 num_pins++;
4402 }
4403
4404 if (num_pins < 2)
4405 return 0;
4406
4407 spec->am_num_entries = num_pins;
4408 /* sort the am_entry in the order of attr so that the pin with a
4409 * higher attr will be selected when the jack is plugged.
4410 */
4411 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4412 compare_attr, NULL);
4413
4414 if (!auto_mic_check_imux(codec))
4415 return 0;
4416
4417 spec->auto_mic = 1;
4418 spec->num_adc_nids = 1;
4419 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004420 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004421 spec->am_entry[0].pin,
4422 spec->am_entry[1].pin,
4423 spec->am_entry[2].pin);
4424
4425 return 0;
4426}
4427
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004428/**
4429 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4430 * into power down
4431 * @codec: the HDA codec
4432 * @nid: NID to evalute
4433 * @power_state: target power state
4434 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004435unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004436 hda_nid_t nid,
4437 unsigned int power_state)
4438{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004439 if (power_state != AC_PWRST_D0 || nid == codec->afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004440 return power_state;
4441 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4442 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004443 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004444 return power_state;
4445 return AC_PWRST_D3;
4446}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004447EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004448
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004449/* mute all aamix inputs initially; parse up to the first leaves */
4450static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4451{
4452 int i, nums;
4453 const hda_nid_t *conn;
4454 bool has_amp;
4455
4456 nums = snd_hda_get_conn_list(codec, mix, &conn);
4457 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4458 for (i = 0; i < nums; i++) {
4459 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004460 update_amp(codec, mix, HDA_INPUT, i,
4461 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004462 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004463 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4464 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004465 }
4466}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004467
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004468/**
4469 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4470 * set up the hda_gen_spec
4471 * @codec: the HDA codec
4472 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004473 *
4474 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004475 * or a negative error code
4476 */
4477int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004478 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004479{
4480 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004481 int err;
4482
Takashi Iwai1c70a582013-01-11 17:48:22 +01004483 parse_user_hints(codec);
4484
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004485 if (spec->mixer_nid && !spec->mixer_merge_nid)
4486 spec->mixer_merge_nid = spec->mixer_nid;
4487
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004488 if (cfg != &spec->autocfg) {
4489 spec->autocfg = *cfg;
4490 cfg = &spec->autocfg;
4491 }
4492
Takashi Iwai98bd1112013-03-22 14:53:50 +01004493 if (!spec->main_out_badness)
4494 spec->main_out_badness = &hda_main_out_badness;
4495 if (!spec->extra_out_badness)
4496 spec->extra_out_badness = &hda_extra_out_badness;
4497
David Henningsson6fc4cb92013-01-16 15:58:45 +01004498 fill_all_dac_nids(codec);
4499
Takashi Iwai352f7f92012-12-19 12:52:06 +01004500 if (!cfg->line_outs) {
4501 if (cfg->dig_outs || cfg->dig_in_pin) {
4502 spec->multiout.max_channels = 2;
4503 spec->no_analog = 1;
4504 goto dig_only;
4505 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004506 if (!cfg->num_inputs && !cfg->dig_in_pin)
4507 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004508 }
4509
4510 if (!spec->no_primary_hp &&
4511 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4512 cfg->line_outs <= cfg->hp_outs) {
4513 /* use HP as primary out */
4514 cfg->speaker_outs = cfg->line_outs;
4515 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4516 sizeof(cfg->speaker_pins));
4517 cfg->line_outs = cfg->hp_outs;
4518 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4519 cfg->hp_outs = 0;
4520 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4521 cfg->line_out_type = AUTO_PIN_HP_OUT;
4522 }
4523
4524 err = parse_output_paths(codec);
4525 if (err < 0)
4526 return err;
4527 err = create_multi_channel_mode(codec);
4528 if (err < 0)
4529 return err;
4530 err = create_multi_out_ctls(codec, cfg);
4531 if (err < 0)
4532 return err;
4533 err = create_hp_out_ctls(codec);
4534 if (err < 0)
4535 return err;
4536 err = create_speaker_out_ctls(codec);
4537 if (err < 0)
4538 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004539 err = create_indep_hp_ctls(codec);
4540 if (err < 0)
4541 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004542 err = create_loopback_mixing_ctl(codec);
4543 if (err < 0)
4544 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004545 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004546 if (err < 0)
4547 return err;
4548 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004549 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004550 return err;
4551
Takashi Iwaia07a9492013-01-07 16:44:06 +01004552 spec->const_channel_count = spec->ext_channel_count;
4553 /* check the multiple speaker and headphone pins */
4554 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4555 spec->const_channel_count = max(spec->const_channel_count,
4556 cfg->speaker_outs * 2);
4557 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4558 spec->const_channel_count = max(spec->const_channel_count,
4559 cfg->hp_outs * 2);
4560 spec->multiout.max_channels = max(spec->ext_channel_count,
4561 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004562
4563 err = check_auto_mute_availability(codec);
4564 if (err < 0)
4565 return err;
4566
4567 err = check_dyn_adc_switch(codec);
4568 if (err < 0)
4569 return err;
4570
Takashi Iwai967303d2013-02-19 17:12:42 +01004571 err = check_auto_mic_availability(codec);
4572 if (err < 0)
4573 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004574
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004575 /* add stereo mix if available and not enabled yet */
4576 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01004577 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4578 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004579 err = parse_capture_source(codec, spec->mixer_nid,
4580 CFG_IDX_MIX, spec->num_all_adcs,
4581 "Stereo Mix", 0);
4582 if (err < 0)
4583 return err;
4584 }
4585
4586
Takashi Iwai352f7f92012-12-19 12:52:06 +01004587 err = create_capture_mixers(codec);
4588 if (err < 0)
4589 return err;
4590
4591 err = parse_mic_boost(codec);
4592 if (err < 0)
4593 return err;
4594
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004595 /* create "Headphone Mic Jack Mode" if no input selection is
4596 * available (or user specifies add_jack_modes hint)
4597 */
4598 if (spec->hp_mic_pin &&
4599 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4600 spec->add_jack_modes)) {
4601 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4602 if (err < 0)
4603 return err;
4604 }
4605
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004606 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004607 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4608 err = create_out_jack_modes(codec, cfg->line_outs,
4609 cfg->line_out_pins);
4610 if (err < 0)
4611 return err;
4612 }
4613 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4614 err = create_out_jack_modes(codec, cfg->hp_outs,
4615 cfg->hp_pins);
4616 if (err < 0)
4617 return err;
4618 }
4619 }
4620
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004621 /* mute all aamix input initially */
4622 if (spec->mixer_nid)
4623 mute_all_mixer_nid(codec, spec->mixer_nid);
4624
Takashi Iwai352f7f92012-12-19 12:52:06 +01004625 dig_only:
4626 parse_digital(codec);
4627
Takashi Iwai55196ff2013-01-24 17:32:56 +01004628 if (spec->power_down_unused)
4629 codec->power_filter = snd_hda_gen_path_power_filter;
4630
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004631 if (!spec->no_analog && spec->beep_nid) {
4632 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4633 if (err < 0)
4634 return err;
4635 }
4636
Takashi Iwai352f7f92012-12-19 12:52:06 +01004637 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004639EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
4641
4642/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004643 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004645
4646/* slave controls for virtual master */
4647static const char * const slave_pfxs[] = {
4648 "Front", "Surround", "Center", "LFE", "Side",
4649 "Headphone", "Speaker", "Mono", "Line Out",
4650 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004651 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4652 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02004653 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004654 NULL,
4655};
4656
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004657/**
4658 * snd_hda_gen_build_controls - Build controls from the parsed results
4659 * @codec: the HDA codec
4660 *
4661 * Pass this to build_controls patch_ops.
4662 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004663int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004665 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Takashi Iwai36502d02012-12-19 15:15:10 +01004668 if (spec->kctls.used) {
4669 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4670 if (err < 0)
4671 return err;
4672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Takashi Iwai352f7f92012-12-19 12:52:06 +01004674 if (spec->multiout.dig_out_nid) {
4675 err = snd_hda_create_dig_out_ctls(codec,
4676 spec->multiout.dig_out_nid,
4677 spec->multiout.dig_out_nid,
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004678 spec->pcm_rec[1]->pcm_type);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004679 if (err < 0)
4680 return err;
4681 if (!spec->no_analog) {
4682 err = snd_hda_create_spdif_share_sw(codec,
4683 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 if (err < 0)
4685 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004686 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 }
4688 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004689 if (spec->dig_in_nid) {
4690 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4691 if (err < 0)
4692 return err;
4693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694
Takashi Iwai352f7f92012-12-19 12:52:06 +01004695 /* if we have no master control, let's create it */
4696 if (!spec->no_analog &&
4697 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004698 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004699 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004700 "Playback Volume");
4701 if (err < 0)
4702 return err;
4703 }
4704 if (!spec->no_analog &&
4705 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4706 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4707 NULL, slave_pfxs,
4708 "Playback Switch",
4709 true, &spec->vmaster_mute.sw_kctl);
4710 if (err < 0)
4711 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02004712 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01004713 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4714 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02004715 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4716 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
Takashi Iwai352f7f92012-12-19 12:52:06 +01004719 free_kctls(spec); /* no longer needed */
4720
Takashi Iwai352f7f92012-12-19 12:52:06 +01004721 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4722 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 return err;
4724
4725 return 0;
4726}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004727EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004728
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
4730/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004731 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004734static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4735 struct hda_codec *codec,
4736 struct snd_pcm_substream *substream,
4737 int action)
4738{
4739 struct hda_gen_spec *spec = codec->spec;
4740 if (spec->pcm_playback_hook)
4741 spec->pcm_playback_hook(hinfo, codec, substream, action);
4742}
4743
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004744static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4745 struct hda_codec *codec,
4746 struct snd_pcm_substream *substream,
4747 int action)
4748{
4749 struct hda_gen_spec *spec = codec->spec;
4750 if (spec->pcm_capture_hook)
4751 spec->pcm_capture_hook(hinfo, codec, substream, action);
4752}
4753
Takashi Iwai352f7f92012-12-19 12:52:06 +01004754/*
4755 * Analog playback callbacks
4756 */
4757static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4758 struct hda_codec *codec,
4759 struct snd_pcm_substream *substream)
4760{
4761 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004762 int err;
4763
4764 mutex_lock(&spec->pcm_mutex);
4765 err = snd_hda_multi_out_analog_open(codec,
4766 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004767 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004768 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004769 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004770 call_pcm_playback_hook(hinfo, codec, substream,
4771 HDA_GEN_PCM_ACT_OPEN);
4772 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004773 mutex_unlock(&spec->pcm_mutex);
4774 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004775}
4776
4777static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004778 struct hda_codec *codec,
4779 unsigned int stream_tag,
4780 unsigned int format,
4781 struct snd_pcm_substream *substream)
4782{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004783 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004784 int err;
4785
4786 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4787 stream_tag, format, substream);
4788 if (!err)
4789 call_pcm_playback_hook(hinfo, codec, substream,
4790 HDA_GEN_PCM_ACT_PREPARE);
4791 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004792}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004793
Takashi Iwai352f7f92012-12-19 12:52:06 +01004794static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4795 struct hda_codec *codec,
4796 struct snd_pcm_substream *substream)
4797{
4798 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004799 int err;
4800
4801 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4802 if (!err)
4803 call_pcm_playback_hook(hinfo, codec, substream,
4804 HDA_GEN_PCM_ACT_CLEANUP);
4805 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004806}
4807
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004808static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4809 struct hda_codec *codec,
4810 struct snd_pcm_substream *substream)
4811{
4812 struct hda_gen_spec *spec = codec->spec;
4813 mutex_lock(&spec->pcm_mutex);
4814 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004815 call_pcm_playback_hook(hinfo, codec, substream,
4816 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004817 mutex_unlock(&spec->pcm_mutex);
4818 return 0;
4819}
4820
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004821static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4822 struct hda_codec *codec,
4823 struct snd_pcm_substream *substream)
4824{
4825 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4826 return 0;
4827}
4828
4829static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4830 struct hda_codec *codec,
4831 unsigned int stream_tag,
4832 unsigned int format,
4833 struct snd_pcm_substream *substream)
4834{
4835 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4836 call_pcm_capture_hook(hinfo, codec, substream,
4837 HDA_GEN_PCM_ACT_PREPARE);
4838 return 0;
4839}
4840
4841static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4842 struct hda_codec *codec,
4843 struct snd_pcm_substream *substream)
4844{
4845 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4846 call_pcm_capture_hook(hinfo, codec, substream,
4847 HDA_GEN_PCM_ACT_CLEANUP);
4848 return 0;
4849}
4850
4851static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4852 struct hda_codec *codec,
4853 struct snd_pcm_substream *substream)
4854{
4855 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4856 return 0;
4857}
4858
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004859static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4860 struct hda_codec *codec,
4861 struct snd_pcm_substream *substream)
4862{
4863 struct hda_gen_spec *spec = codec->spec;
4864 int err = 0;
4865
4866 mutex_lock(&spec->pcm_mutex);
4867 if (!spec->indep_hp_enabled)
4868 err = -EBUSY;
4869 else
4870 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004871 call_pcm_playback_hook(hinfo, codec, substream,
4872 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004873 mutex_unlock(&spec->pcm_mutex);
4874 return err;
4875}
4876
4877static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4878 struct hda_codec *codec,
4879 struct snd_pcm_substream *substream)
4880{
4881 struct hda_gen_spec *spec = codec->spec;
4882 mutex_lock(&spec->pcm_mutex);
4883 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004884 call_pcm_playback_hook(hinfo, codec, substream,
4885 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004886 mutex_unlock(&spec->pcm_mutex);
4887 return 0;
4888}
4889
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004890static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4891 struct hda_codec *codec,
4892 unsigned int stream_tag,
4893 unsigned int format,
4894 struct snd_pcm_substream *substream)
4895{
4896 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4897 call_pcm_playback_hook(hinfo, codec, substream,
4898 HDA_GEN_PCM_ACT_PREPARE);
4899 return 0;
4900}
4901
4902static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4903 struct hda_codec *codec,
4904 struct snd_pcm_substream *substream)
4905{
4906 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4907 call_pcm_playback_hook(hinfo, codec, substream,
4908 HDA_GEN_PCM_ACT_CLEANUP);
4909 return 0;
4910}
4911
Takashi Iwai352f7f92012-12-19 12:52:06 +01004912/*
4913 * Digital out
4914 */
4915static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4916 struct hda_codec *codec,
4917 struct snd_pcm_substream *substream)
4918{
4919 struct hda_gen_spec *spec = codec->spec;
4920 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4921}
4922
4923static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4924 struct hda_codec *codec,
4925 unsigned int stream_tag,
4926 unsigned int format,
4927 struct snd_pcm_substream *substream)
4928{
4929 struct hda_gen_spec *spec = codec->spec;
4930 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4931 stream_tag, format, substream);
4932}
4933
4934static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4935 struct hda_codec *codec,
4936 struct snd_pcm_substream *substream)
4937{
4938 struct hda_gen_spec *spec = codec->spec;
4939 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4940}
4941
4942static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4943 struct hda_codec *codec,
4944 struct snd_pcm_substream *substream)
4945{
4946 struct hda_gen_spec *spec = codec->spec;
4947 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4948}
4949
4950/*
4951 * Analog capture
4952 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004953#define alt_capture_pcm_open capture_pcm_open
4954#define alt_capture_pcm_close capture_pcm_close
4955
Takashi Iwai352f7f92012-12-19 12:52:06 +01004956static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4957 struct hda_codec *codec,
4958 unsigned int stream_tag,
4959 unsigned int format,
4960 struct snd_pcm_substream *substream)
4961{
4962 struct hda_gen_spec *spec = codec->spec;
4963
4964 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004965 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004966 call_pcm_capture_hook(hinfo, codec, substream,
4967 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004968 return 0;
4969}
4970
Takashi Iwai352f7f92012-12-19 12:52:06 +01004971static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4972 struct hda_codec *codec,
4973 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004974{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004975 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004976
Takashi Iwai352f7f92012-12-19 12:52:06 +01004977 snd_hda_codec_cleanup_stream(codec,
4978 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004979 call_pcm_capture_hook(hinfo, codec, substream,
4980 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004981 return 0;
4982}
4983
Takashi Iwai352f7f92012-12-19 12:52:06 +01004984/*
4985 */
4986static const struct hda_pcm_stream pcm_analog_playback = {
4987 .substreams = 1,
4988 .channels_min = 2,
4989 .channels_max = 8,
4990 /* NID is set in build_pcms */
4991 .ops = {
4992 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004993 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004994 .prepare = playback_pcm_prepare,
4995 .cleanup = playback_pcm_cleanup
4996 },
4997};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
Takashi Iwai352f7f92012-12-19 12:52:06 +01004999static const struct hda_pcm_stream pcm_analog_capture = {
5000 .substreams = 1,
5001 .channels_min = 2,
5002 .channels_max = 2,
5003 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005004 .ops = {
5005 .open = capture_pcm_open,
5006 .close = capture_pcm_close,
5007 .prepare = capture_pcm_prepare,
5008 .cleanup = capture_pcm_cleanup
5009 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005010};
5011
5012static const struct hda_pcm_stream pcm_analog_alt_playback = {
5013 .substreams = 1,
5014 .channels_min = 2,
5015 .channels_max = 2,
5016 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005017 .ops = {
5018 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005019 .close = alt_playback_pcm_close,
5020 .prepare = alt_playback_pcm_prepare,
5021 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005022 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005023};
5024
5025static const struct hda_pcm_stream pcm_analog_alt_capture = {
5026 .substreams = 2, /* can be overridden */
5027 .channels_min = 2,
5028 .channels_max = 2,
5029 /* NID is set in build_pcms */
5030 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005031 .open = alt_capture_pcm_open,
5032 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005033 .prepare = alt_capture_pcm_prepare,
5034 .cleanup = alt_capture_pcm_cleanup
5035 },
5036};
5037
5038static const struct hda_pcm_stream pcm_digital_playback = {
5039 .substreams = 1,
5040 .channels_min = 2,
5041 .channels_max = 2,
5042 /* NID is set in build_pcms */
5043 .ops = {
5044 .open = dig_playback_pcm_open,
5045 .close = dig_playback_pcm_close,
5046 .prepare = dig_playback_pcm_prepare,
5047 .cleanup = dig_playback_pcm_cleanup
5048 },
5049};
5050
5051static const struct hda_pcm_stream pcm_digital_capture = {
5052 .substreams = 1,
5053 .channels_min = 2,
5054 .channels_max = 2,
5055 /* NID is set in build_pcms */
5056};
5057
5058/* Used by build_pcms to flag that a PCM has no playback stream */
5059static const struct hda_pcm_stream pcm_null_stream = {
5060 .substreams = 0,
5061 .channels_min = 0,
5062 .channels_max = 0,
5063};
5064
5065/*
5066 * dynamic changing ADC PCM streams
5067 */
5068static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5069{
5070 struct hda_gen_spec *spec = codec->spec;
5071 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5072
5073 if (spec->cur_adc && spec->cur_adc != new_adc) {
5074 /* stream is running, let's swap the current ADC */
5075 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5076 spec->cur_adc = new_adc;
5077 snd_hda_codec_setup_stream(codec, new_adc,
5078 spec->cur_adc_stream_tag, 0,
5079 spec->cur_adc_format);
5080 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005082 return false;
5083}
5084
5085/* analog capture with dynamic dual-adc changes */
5086static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5087 struct hda_codec *codec,
5088 unsigned int stream_tag,
5089 unsigned int format,
5090 struct snd_pcm_substream *substream)
5091{
5092 struct hda_gen_spec *spec = codec->spec;
5093 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5094 spec->cur_adc_stream_tag = stream_tag;
5095 spec->cur_adc_format = format;
5096 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5097 return 0;
5098}
5099
5100static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5101 struct hda_codec *codec,
5102 struct snd_pcm_substream *substream)
5103{
5104 struct hda_gen_spec *spec = codec->spec;
5105 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5106 spec->cur_adc = 0;
5107 return 0;
5108}
5109
5110static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5111 .substreams = 1,
5112 .channels_min = 2,
5113 .channels_max = 2,
5114 .nid = 0, /* fill later */
5115 .ops = {
5116 .prepare = dyn_adc_capture_pcm_prepare,
5117 .cleanup = dyn_adc_capture_pcm_cleanup
5118 },
5119};
5120
Takashi Iwaif873e532012-12-20 16:58:39 +01005121static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5122 const char *chip_name)
5123{
5124 char *p;
5125
5126 if (*str)
5127 return;
5128 strlcpy(str, chip_name, len);
5129
5130 /* drop non-alnum chars after a space */
5131 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5132 if (!isalnum(p[1])) {
5133 *p = 0;
5134 break;
5135 }
5136 }
5137 strlcat(str, sfx, len);
5138}
5139
Takashi Iwaifb83b632015-03-16 23:34:34 +01005140/* copy PCM stream info from @default_str, and override non-NULL entries
5141 * from @spec_str and @nid
5142 */
5143static void setup_pcm_stream(struct hda_pcm_stream *str,
5144 const struct hda_pcm_stream *default_str,
5145 const struct hda_pcm_stream *spec_str,
5146 hda_nid_t nid)
5147{
5148 *str = *default_str;
5149 if (nid)
5150 str->nid = nid;
5151 if (spec_str) {
5152 if (spec_str->substreams)
5153 str->substreams = spec_str->substreams;
5154 if (spec_str->channels_min)
5155 str->channels_min = spec_str->channels_min;
5156 if (spec_str->channels_max)
5157 str->channels_max = spec_str->channels_max;
5158 if (spec_str->rates)
5159 str->rates = spec_str->rates;
5160 if (spec_str->formats)
5161 str->formats = spec_str->formats;
5162 if (spec_str->maxbps)
5163 str->maxbps = spec_str->maxbps;
5164 }
5165}
5166
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005167/**
5168 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5169 * @codec: the HDA codec
5170 *
5171 * Pass this to build_pcms patch_ops.
5172 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005173int snd_hda_gen_build_pcms(struct hda_codec *codec)
5174{
5175 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005176 struct hda_pcm *info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005177 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
Takashi Iwai352f7f92012-12-19 12:52:06 +01005179 if (spec->no_analog)
5180 goto skip_analog;
5181
Takashi Iwaif873e532012-12-20 16:58:39 +01005182 fill_pcm_stream_name(spec->stream_name_analog,
5183 sizeof(spec->stream_name_analog),
5184 " Analog", codec->chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005185 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5186 if (!info)
5187 return -ENOMEM;
5188 spec->pcm_rec[0] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005189
5190 if (spec->multiout.num_dacs > 0) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005191 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5192 &pcm_analog_playback,
5193 spec->stream_analog_playback,
5194 spec->multiout.dac_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005195 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5196 spec->multiout.max_channels;
5197 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5198 spec->autocfg.line_outs == 2)
5199 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5200 snd_pcm_2_1_chmaps;
5201 }
5202 if (spec->num_adc_nids) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005203 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5204 (spec->dyn_adc_switch ?
5205 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5206 spec->stream_analog_capture,
5207 spec->adc_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005208 }
5209
Takashi Iwai352f7f92012-12-19 12:52:06 +01005210 skip_analog:
5211 /* SPDIF for stream index #1 */
5212 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005213 fill_pcm_stream_name(spec->stream_name_digital,
5214 sizeof(spec->stream_name_digital),
5215 " Digital", codec->chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005216 info = snd_hda_codec_pcm_new(codec, "%s",
5217 spec->stream_name_digital);
5218 if (!info)
5219 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005220 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005221 spec->pcm_rec[1] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005222 if (spec->dig_out_type)
5223 info->pcm_type = spec->dig_out_type;
5224 else
5225 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005226 if (spec->multiout.dig_out_nid)
5227 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5228 &pcm_digital_playback,
5229 spec->stream_digital_playback,
5230 spec->multiout.dig_out_nid);
5231 if (spec->dig_in_nid)
5232 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5233 &pcm_digital_capture,
5234 spec->stream_digital_capture,
5235 spec->dig_in_nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005236 }
5237
5238 if (spec->no_analog)
5239 return 0;
5240
5241 /* If the use of more than one ADC is requested for the current
5242 * model, configure a second analog capture-only PCM.
5243 */
5244 have_multi_adcs = (spec->num_adc_nids > 1) &&
5245 !spec->dyn_adc_switch && !spec->auto_mic;
5246 /* Additional Analaog capture for index #2 */
5247 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005248 fill_pcm_stream_name(spec->stream_name_alt_analog,
5249 sizeof(spec->stream_name_alt_analog),
5250 " Alt Analog", codec->chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005251 info = snd_hda_codec_pcm_new(codec, "%s",
5252 spec->stream_name_alt_analog);
5253 if (!info)
5254 return -ENOMEM;
5255 spec->pcm_rec[2] = info;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005256 if (spec->alt_dac_nid)
5257 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5258 &pcm_analog_alt_playback,
5259 spec->stream_analog_alt_playback,
5260 spec->alt_dac_nid);
5261 else
5262 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5263 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005264 if (have_multi_adcs) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005265 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5266 &pcm_analog_alt_capture,
5267 spec->stream_analog_alt_capture,
5268 spec->adc_nids[1]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005269 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5270 spec->num_adc_nids - 1;
5271 } else {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005272 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5273 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 }
5276
5277 return 0;
5278}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005279EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005280
5281
5282/*
5283 * Standard auto-parser initializations
5284 */
5285
Takashi Iwaid4156932013-01-07 10:08:02 +01005286/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005287static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005288{
5289 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005290 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005291
Takashi Iwai196c17662013-01-04 15:01:40 +01005292 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005293 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005294 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005295 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005296 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005297 snd_hda_activate_path(codec, path, path->active,
5298 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005299 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005300}
5301
5302/* initialize primary output paths */
5303static void init_multi_out(struct hda_codec *codec)
5304{
5305 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005306 int i;
5307
Takashi Iwaid4156932013-01-07 10:08:02 +01005308 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005309 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005310}
5311
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005312
Takashi Iwai2c12c302013-01-10 09:33:29 +01005313static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005314{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005315 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005316
Takashi Iwaid4156932013-01-07 10:08:02 +01005317 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005318 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005319}
5320
5321/* initialize hp and speaker paths */
5322static void init_extra_out(struct hda_codec *codec)
5323{
5324 struct hda_gen_spec *spec = codec->spec;
5325
5326 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005327 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005328 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5329 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005330 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005331}
5332
5333/* initialize multi-io paths */
5334static void init_multi_io(struct hda_codec *codec)
5335{
5336 struct hda_gen_spec *spec = codec->spec;
5337 int i;
5338
5339 for (i = 0; i < spec->multi_ios; i++) {
5340 hda_nid_t pin = spec->multi_io[i].pin;
5341 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005342 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005343 if (!path)
5344 continue;
5345 if (!spec->multi_io[i].ctl_in)
5346 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005347 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005348 snd_hda_activate_path(codec, path, path->active,
5349 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005350 }
5351}
5352
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005353static void init_aamix_paths(struct hda_codec *codec)
5354{
5355 struct hda_gen_spec *spec = codec->spec;
5356
5357 if (!spec->have_aamix_ctl)
5358 return;
5359 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5360 spec->aamix_out_paths[0],
5361 spec->autocfg.line_out_type);
5362 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5363 spec->aamix_out_paths[1],
5364 AUTO_PIN_HP_OUT);
5365 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5366 spec->aamix_out_paths[2],
5367 AUTO_PIN_SPEAKER_OUT);
5368}
5369
Takashi Iwai352f7f92012-12-19 12:52:06 +01005370/* set up input pins and loopback paths */
5371static void init_analog_input(struct hda_codec *codec)
5372{
5373 struct hda_gen_spec *spec = codec->spec;
5374 struct auto_pin_cfg *cfg = &spec->autocfg;
5375 int i;
5376
5377 for (i = 0; i < cfg->num_inputs; i++) {
5378 hda_nid_t nid = cfg->inputs[i].pin;
5379 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005380 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005381
5382 /* init loopback inputs */
5383 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005384 resume_path_from_idx(codec, spec->loopback_paths[i]);
5385 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005386 }
5387 }
5388}
5389
5390/* initialize ADC paths */
5391static void init_input_src(struct hda_codec *codec)
5392{
5393 struct hda_gen_spec *spec = codec->spec;
5394 struct hda_input_mux *imux = &spec->input_mux;
5395 struct nid_path *path;
5396 int i, c, nums;
5397
5398 if (spec->dyn_adc_switch)
5399 nums = 1;
5400 else
5401 nums = spec->num_adc_nids;
5402
5403 for (c = 0; c < nums; c++) {
5404 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005405 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005406 if (path) {
5407 bool active = path->active;
5408 if (i == spec->cur_mux[c])
5409 active = true;
5410 snd_hda_activate_path(codec, path, active, false);
5411 }
5412 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005413 if (spec->hp_mic)
5414 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005415 }
5416
Takashi Iwai352f7f92012-12-19 12:52:06 +01005417 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005418 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005419}
5420
5421/* set right pin controls for digital I/O */
5422static void init_digital(struct hda_codec *codec)
5423{
5424 struct hda_gen_spec *spec = codec->spec;
5425 int i;
5426 hda_nid_t pin;
5427
Takashi Iwaid4156932013-01-07 10:08:02 +01005428 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005429 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005430 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005431 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005432 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005433 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005434 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005435}
5436
Takashi Iwai973e4972012-12-20 15:16:09 +01005437/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5438 * invalid unsol tags by some reason
5439 */
5440static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5441{
5442 int i;
5443
5444 for (i = 0; i < codec->init_pins.used; i++) {
5445 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5446 hda_nid_t nid = pin->nid;
5447 if (is_jack_detectable(codec, nid) &&
5448 !snd_hda_jack_tbl_get(codec, nid))
5449 snd_hda_codec_update_cache(codec, nid, 0,
5450 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5451 }
5452}
5453
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005454/**
5455 * snd_hda_gen_init - initialize the generic spec
5456 * @codec: the HDA codec
5457 *
5458 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005459 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005460int snd_hda_gen_init(struct hda_codec *codec)
5461{
5462 struct hda_gen_spec *spec = codec->spec;
5463
5464 if (spec->init_hook)
5465 spec->init_hook(codec);
5466
5467 snd_hda_apply_verbs(codec);
5468
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005469 codec->cached_write = 1;
5470
Takashi Iwai352f7f92012-12-19 12:52:06 +01005471 init_multi_out(codec);
5472 init_extra_out(codec);
5473 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005474 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005475 init_analog_input(codec);
5476 init_input_src(codec);
5477 init_digital(codec);
5478
Takashi Iwai973e4972012-12-20 15:16:09 +01005479 clear_unsol_on_unused_pins(codec);
5480
Takashi Iwai352f7f92012-12-19 12:52:06 +01005481 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005482 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005483
Takashi Iwaidc870f32013-01-22 15:24:30 +01005484 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005485
Takashi Iwai352f7f92012-12-19 12:52:06 +01005486 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5487 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5488
5489 hda_call_check_power_status(codec, 0x01);
5490 return 0;
5491}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005492EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005493
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005494/**
5495 * snd_hda_gen_free - free the generic spec
5496 * @codec: the HDA codec
5497 *
5498 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005499 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005500void snd_hda_gen_free(struct hda_codec *codec)
5501{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005502 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005503 snd_hda_gen_spec_free(codec->spec);
5504 kfree(codec->spec);
5505 codec->spec = NULL;
5506}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005507EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005508
5509#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005510/**
5511 * snd_hda_gen_check_power_status - check the loopback power save state
5512 * @codec: the HDA codec
5513 * @nid: NID to inspect
5514 *
5515 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005516 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005517int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5518{
5519 struct hda_gen_spec *spec = codec->spec;
5520 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5521}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005522EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005523#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005524
5525
5526/*
5527 * the generic codec support
5528 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Takashi Iwai352f7f92012-12-19 12:52:06 +01005530static const struct hda_codec_ops generic_patch_ops = {
5531 .build_controls = snd_hda_gen_build_controls,
5532 .build_pcms = snd_hda_gen_build_pcms,
5533 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005534 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005535 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005536#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005537 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005538#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539};
5540
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005541/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005542 * snd_hda_parse_generic_codec - Generic codec parser
5543 * @codec: the HDA codec
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005544 */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005545static int snd_hda_parse_generic_codec(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005547 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 int err;
5549
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005550 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005551 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005553 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005556 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5557 if (err < 0)
5558 return err;
5559
5560 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005561 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 goto error;
5563
5564 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 return 0;
5566
Takashi Iwai352f7f92012-12-19 12:52:06 +01005567error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005568 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 return err;
5570}
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005571
5572static const struct hda_codec_preset snd_hda_preset_generic[] = {
5573 { .id = HDA_CODEC_ID_GENERIC, .patch = snd_hda_parse_generic_codec },
5574 {} /* terminator */
5575};
5576
5577static struct hda_codec_driver generic_driver = {
5578 .preset = snd_hda_preset_generic,
5579};
5580
5581module_hda_codec_driver(generic_driver);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005582
5583MODULE_LICENSE("GPL");
5584MODULE_DESCRIPTION("Generic HD-audio codec parser");