blob: b680b4ec63313c8b1152390dbbf602018a212952 [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
690/* initialize the amp value (only at the first time) */
691static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
692{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100693 unsigned int caps = query_amp_caps(codec, nid, dir);
694 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100695 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
696}
697
Takashi Iwai8999bf02013-01-18 11:01:33 +0100698/* calculate amp value mask we can modify;
699 * if the given amp is controlled by mixers, don't touch it
700 */
701static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
702 hda_nid_t nid, int dir, int idx,
703 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100704{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100705 unsigned int mask = 0xff;
706
Takashi Iwaif69910d2013-08-08 09:32:37 +0200707 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100708 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
709 mask &= ~0x80;
710 }
711 if (caps & AC_AMPCAP_NUM_STEPS) {
712 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
713 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
714 mask &= ~0x7f;
715 }
716 return mask;
717}
718
719static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
720 int idx, int idx_to_check, bool enable)
721{
722 unsigned int caps;
723 unsigned int mask, val;
724
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100725 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100726 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100727
728 caps = query_amp_caps(codec, nid, dir);
729 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
730 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
731 if (!mask)
732 return;
733
734 val &= mask;
735 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100736}
737
738static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
739 int i, bool enable)
740{
741 hda_nid_t nid = path->path[i];
742 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100743 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100744}
745
746static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
747 int i, bool enable, bool add_aamix)
748{
749 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100750 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100751 int n, nums, idx;
752 int type;
753 hda_nid_t nid = path->path[i];
754
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100755 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100756 type = get_wcaps_type(get_wcaps(codec, nid));
757 if (type == AC_WID_PIN ||
758 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
759 nums = 1;
760 idx = 0;
761 } else
762 idx = path->idx[i];
763
764 for (n = 0; n < nums; n++)
765 init_amp(codec, nid, HDA_INPUT, n);
766
Takashi Iwai352f7f92012-12-19 12:52:06 +0100767 /* here is a little bit tricky in comparison with activate_amp_out();
768 * when aa-mixer is available, we need to enable the path as well
769 */
770 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100771 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100772 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100773 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775}
776
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100777/**
778 * snd_hda_activate_path - activate or deactivate the given path
779 * @codec: the HDA codec
780 * @path: the path to activate/deactivate
781 * @enable: flag to activate or not
782 * @add_aamix: enable the input from aamix NID
783 *
784 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100786void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
787 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100789 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100790 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Takashi Iwai352f7f92012-12-19 12:52:06 +0100792 if (!enable)
793 path->active = false;
794
795 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100796 hda_nid_t nid = path->path[i];
797 if (enable && spec->power_down_unused) {
798 /* make sure the widget is powered up */
799 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
800 snd_hda_codec_write(codec, nid, 0,
801 AC_VERB_SET_POWER_STATE,
802 AC_PWRST_D0);
803 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100804 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100805 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100806 AC_VERB_SET_CONNECT_SEL,
807 path->idx[i]);
808 if (has_amp_in(codec, path, i))
809 activate_amp_in(codec, path, i, enable, add_aamix);
810 if (has_amp_out(codec, path, i))
811 activate_amp_out(codec, path, i, enable);
812 }
813
814 if (enable)
815 path->active = true;
816}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100817EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100818
Takashi Iwai55196ff2013-01-24 17:32:56 +0100819/* if the given path is inactive, put widgets into D3 (only if suitable) */
820static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
821{
822 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200823 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100824 int i;
825
826 if (!spec->power_down_unused || path->active)
827 return;
828
829 for (i = 0; i < path->depth; i++) {
830 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200831 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
832 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100833 snd_hda_codec_write(codec, nid, 0,
834 AC_VERB_SET_POWER_STATE,
835 AC_PWRST_D3);
836 changed = true;
837 }
838 }
839
840 if (changed) {
841 msleep(10);
842 snd_hda_codec_read(codec, path->path[0], 0,
843 AC_VERB_GET_POWER_STATE, 0);
844 }
845}
846
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100847/* turn on/off EAPD on the given pin */
848static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
849{
850 struct hda_gen_spec *spec = codec->spec;
851 if (spec->own_eapd_ctl ||
852 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
853 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200854 if (spec->keep_eapd_on && !enable)
855 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100856 if (codec->inv_eapd)
857 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100858 snd_hda_codec_update_cache(codec, pin, 0,
859 AC_VERB_SET_EAPD_BTLENABLE,
860 enable ? 0x02 : 0x00);
861}
862
Takashi Iwai3e367f12013-01-23 17:07:23 +0100863/* re-initialize the path specified by the given path index */
864static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
865{
866 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
867 if (path)
868 snd_hda_activate_path(codec, path, path->active, false);
869}
870
Takashi Iwai352f7f92012-12-19 12:52:06 +0100871
872/*
873 * Helper functions for creating mixer ctl elements
874 */
875
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200876static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
877 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200878static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
879 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200880
Takashi Iwai352f7f92012-12-19 12:52:06 +0100881enum {
882 HDA_CTL_WIDGET_VOL,
883 HDA_CTL_WIDGET_MUTE,
884 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100885};
886static const struct snd_kcontrol_new control_templates[] = {
887 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200888 /* only the put callback is replaced for handling the special mute */
889 {
890 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
891 .subdevice = HDA_SUBDEV_AMP_FLAG,
892 .info = snd_hda_mixer_amp_switch_info,
893 .get = snd_hda_mixer_amp_switch_get,
894 .put = hda_gen_mixer_mute_put, /* replaced */
895 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
896 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200897 {
898 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
899 .info = snd_hda_mixer_amp_switch_info,
900 .get = snd_hda_mixer_bind_switch_get,
901 .put = hda_gen_bind_mute_put, /* replaced */
902 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
903 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100904};
905
906/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100907static struct snd_kcontrol_new *
908add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100909 int cidx, unsigned long val)
910{
911 struct snd_kcontrol_new *knew;
912
Takashi Iwai12c93df2012-12-19 14:38:33 +0100913 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100914 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100915 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100916 knew->index = cidx;
917 if (get_amp_nid_(val))
918 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
919 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100920 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100921}
922
923static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
924 const char *pfx, const char *dir,
925 const char *sfx, int cidx, unsigned long val)
926{
Takashi Iwai975cc022013-06-28 11:56:49 +0200927 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100928 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100929 if (!add_control(spec, type, name, cidx, val))
930 return -ENOMEM;
931 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100932}
933
934#define add_pb_vol_ctrl(spec, type, pfx, val) \
935 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
936#define add_pb_sw_ctrl(spec, type, pfx, val) \
937 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
938#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
939 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
940#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
941 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
942
943static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
944 unsigned int chs, struct nid_path *path)
945{
946 unsigned int val;
947 if (!path)
948 return 0;
949 val = path->ctls[NID_PATH_VOL_CTL];
950 if (!val)
951 return 0;
952 val = amp_val_replace_channels(val, chs);
953 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
954}
955
956/* return the channel bits suitable for the given path->ctls[] */
957static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
958 int type)
959{
960 int chs = 1; /* mono (left only) */
961 if (path) {
962 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
963 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
964 chs = 3; /* stereo */
965 }
966 return chs;
967}
968
969static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
970 struct nid_path *path)
971{
972 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
973 return add_vol_ctl(codec, pfx, cidx, chs, path);
974}
975
976/* create a mute-switch for the given mixer widget;
977 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
978 */
979static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
980 unsigned int chs, struct nid_path *path)
981{
982 unsigned int val;
983 int type = HDA_CTL_WIDGET_MUTE;
984
985 if (!path)
986 return 0;
987 val = path->ctls[NID_PATH_MUTE_CTL];
988 if (!val)
989 return 0;
990 val = amp_val_replace_channels(val, chs);
991 if (get_amp_direction_(val) == HDA_INPUT) {
992 hda_nid_t nid = get_amp_nid_(val);
993 int nums = snd_hda_get_num_conns(codec, nid);
994 if (nums > 1) {
995 type = HDA_CTL_BIND_MUTE;
996 val |= nums << 19;
997 }
998 }
999 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1000}
1001
1002static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1003 int cidx, struct nid_path *path)
1004{
1005 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1006 return add_sw_ctl(codec, pfx, cidx, chs, path);
1007}
1008
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001009/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001010static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1011 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001012{
1013 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1014 struct hda_gen_spec *spec = codec->spec;
1015
1016 if (spec->auto_mute_via_amp) {
1017 hda_nid_t nid = get_amp_nid(kcontrol);
1018 bool enabled = !((spec->mute_bits >> nid) & 1);
1019 ucontrol->value.integer.value[0] &= enabled;
1020 ucontrol->value.integer.value[1] &= enabled;
1021 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001022}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001023
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001024static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1025 struct snd_ctl_elem_value *ucontrol)
1026{
1027 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001028 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1029}
1030
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001031static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1032 struct snd_ctl_elem_value *ucontrol)
1033{
1034 sync_auto_mute_bits(kcontrol, ucontrol);
1035 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1036}
1037
Takashi Iwai247d85e2013-01-17 16:18:11 +01001038/* any ctl assigned to the path with the given index? */
1039static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1040{
1041 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1042 return path && path->ctls[ctl_type];
1043}
1044
Takashi Iwai352f7f92012-12-19 12:52:06 +01001045static const char * const channel_name[4] = {
1046 "Front", "Surround", "CLFE", "Side"
1047};
1048
1049/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001050static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1051 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001052{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001053 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001054 struct auto_pin_cfg *cfg = &spec->autocfg;
1055
1056 *index = 0;
1057 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001058 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001059 return spec->vmaster_mute.hook ? "PCM" : "Master";
1060
1061 /* if there is really a single DAC used in the whole output paths,
1062 * use it master (or "PCM" if a vmaster hook is present)
1063 */
1064 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1065 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1066 return spec->vmaster_mute.hook ? "PCM" : "Master";
1067
Takashi Iwai247d85e2013-01-17 16:18:11 +01001068 /* multi-io channels */
1069 if (ch >= cfg->line_outs)
1070 return channel_name[ch];
1071
Takashi Iwai352f7f92012-12-19 12:52:06 +01001072 switch (cfg->line_out_type) {
1073 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001074 /* if the primary channel vol/mute is shared with HP volume,
1075 * don't name it as Speaker
1076 */
1077 if (!ch && cfg->hp_outs &&
1078 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1079 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001080 if (cfg->line_outs == 1)
1081 return "Speaker";
1082 if (cfg->line_outs == 2)
1083 return ch ? "Bass Speaker" : "Speaker";
1084 break;
1085 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001086 /* if the primary channel vol/mute is shared with spk volume,
1087 * don't name it as Headphone
1088 */
1089 if (!ch && cfg->speaker_outs &&
1090 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1091 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001092 /* for multi-io case, only the primary out */
1093 if (ch && spec->multi_ios)
1094 break;
1095 *index = ch;
1096 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001097 case AUTO_PIN_LINE_OUT:
1098 /* This deals with the case where we have two DACs and
1099 * one LO, one HP and one Speaker */
1100 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1101 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1102 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1103 if (hp_lo_shared && spk_lo_shared)
1104 return spec->vmaster_mute.hook ? "PCM" : "Master";
1105 if (hp_lo_shared)
1106 return "Headphone+LO";
1107 if (spk_lo_shared)
1108 return "Speaker+LO";
1109 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001110 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001111
1112 /* for a single channel output, we don't have to name the channel */
1113 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001114 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001115
Takashi Iwai352f7f92012-12-19 12:52:06 +01001116 if (ch >= ARRAY_SIZE(channel_name)) {
1117 snd_BUG();
1118 return "PCM";
1119 }
1120
1121 return channel_name[ch];
1122}
1123
1124/*
1125 * Parse output paths
1126 */
1127
1128/* badness definition */
1129enum {
1130 /* No primary DAC is found for the main output */
1131 BAD_NO_PRIMARY_DAC = 0x10000,
1132 /* No DAC is found for the extra output */
1133 BAD_NO_DAC = 0x4000,
1134 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001135 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001136 /* No individual DAC for extra output */
1137 BAD_NO_EXTRA_DAC = 0x102,
1138 /* No individual DAC for extra surrounds */
1139 BAD_NO_EXTRA_SURR_DAC = 0x101,
1140 /* Primary DAC shared with main surrounds */
1141 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001142 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001143 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001144 /* Primary DAC shared with main CLFE */
1145 BAD_SHARED_CLFE = 0x10,
1146 /* Primary DAC shared with extra surrounds */
1147 BAD_SHARED_EXTRA_SURROUND = 0x10,
1148 /* Volume widget is shared */
1149 BAD_SHARED_VOL = 0x10,
1150};
1151
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001152/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001153 * volume and mute controls, and assign the values to ctls[].
1154 *
1155 * When no appropriate widget is found in the path, the badness value
1156 * is incremented depending on the situation. The function returns the
1157 * total badness for both volume and mute controls.
1158 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001159static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001160{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001161 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001162 hda_nid_t nid;
1163 unsigned int val;
1164 int badness = 0;
1165
1166 if (!path)
1167 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001168
1169 if (path->ctls[NID_PATH_VOL_CTL] ||
1170 path->ctls[NID_PATH_MUTE_CTL])
1171 return 0; /* already evaluated */
1172
Takashi Iwai352f7f92012-12-19 12:52:06 +01001173 nid = look_for_out_vol_nid(codec, path);
1174 if (nid) {
1175 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001176 if (spec->dac_min_mute)
1177 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001178 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1179 badness += BAD_SHARED_VOL;
1180 else
1181 path->ctls[NID_PATH_VOL_CTL] = val;
1182 } else
1183 badness += BAD_SHARED_VOL;
1184 nid = look_for_out_mute_nid(codec, path);
1185 if (nid) {
1186 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1187 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1188 nid_has_mute(codec, nid, HDA_OUTPUT))
1189 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1190 else
1191 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1192 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1193 badness += BAD_SHARED_VOL;
1194 else
1195 path->ctls[NID_PATH_MUTE_CTL] = val;
1196 } else
1197 badness += BAD_SHARED_VOL;
1198 return badness;
1199}
1200
Takashi Iwai98bd1112013-03-22 14:53:50 +01001201const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001202 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1203 .no_dac = BAD_NO_DAC,
1204 .shared_primary = BAD_NO_PRIMARY_DAC,
1205 .shared_surr = BAD_SHARED_SURROUND,
1206 .shared_clfe = BAD_SHARED_CLFE,
1207 .shared_surr_main = BAD_SHARED_SURROUND,
1208};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001209EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001210
Takashi Iwai98bd1112013-03-22 14:53:50 +01001211const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001212 .no_primary_dac = BAD_NO_DAC,
1213 .no_dac = BAD_NO_DAC,
1214 .shared_primary = BAD_NO_EXTRA_DAC,
1215 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1216 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1217 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1218};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001219EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001220
Takashi Iwai7385df62013-01-07 09:50:52 +01001221/* get the DAC of the primary output corresponding to the given array index */
1222static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1223{
1224 struct hda_gen_spec *spec = codec->spec;
1225 struct auto_pin_cfg *cfg = &spec->autocfg;
1226
1227 if (cfg->line_outs > idx)
1228 return spec->private_dac_nids[idx];
1229 idx -= cfg->line_outs;
1230 if (spec->multi_ios > idx)
1231 return spec->multi_io[idx].dac;
1232 return 0;
1233}
1234
1235/* return the DAC if it's reachable, otherwise zero */
1236static inline hda_nid_t try_dac(struct hda_codec *codec,
1237 hda_nid_t dac, hda_nid_t pin)
1238{
1239 return is_reachable_path(codec, dac, pin) ? dac : 0;
1240}
1241
Takashi Iwai352f7f92012-12-19 12:52:06 +01001242/* try to assign DACs to pins and return the resultant badness */
1243static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1244 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001245 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001246 const struct badness_table *bad)
1247{
1248 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001249 int i, j;
1250 int badness = 0;
1251 hda_nid_t dac;
1252
1253 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return 0;
1255
Takashi Iwai352f7f92012-12-19 12:52:06 +01001256 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001257 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001258 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001259
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001260 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1261 if (path) {
1262 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001263 continue;
1264 }
1265
Takashi Iwai36907392013-12-10 17:29:26 +01001266 dacs[i] = get_preferred_dac(codec, pin);
1267 if (dacs[i]) {
1268 if (is_dac_already_used(codec, dacs[i]))
1269 badness += bad->shared_primary;
1270 }
1271
1272 if (!dacs[i])
1273 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001274 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001275 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001276 for (j = 1; j < num_outs; j++) {
1277 if (is_reachable_path(codec, dacs[j], pin)) {
1278 dacs[0] = dacs[j];
1279 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001280 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001281 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001285 }
1286 dac = dacs[i];
1287 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001288 if (num_outs > 2)
1289 dac = try_dac(codec, get_primary_out(codec, i), pin);
1290 if (!dac)
1291 dac = try_dac(codec, dacs[0], pin);
1292 if (!dac)
1293 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001294 if (dac) {
1295 if (!i)
1296 badness += bad->shared_primary;
1297 else if (i == 1)
1298 badness += bad->shared_surr;
1299 else
1300 badness += bad->shared_clfe;
1301 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1302 dac = spec->private_dac_nids[0];
1303 badness += bad->shared_surr_main;
1304 } else if (!i)
1305 badness += bad->no_primary_dac;
1306 else
1307 badness += bad->no_dac;
1308 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001309 if (!dac)
1310 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001311 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001312 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001313 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001314 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001315 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001316 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001317 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001318 badness += bad->no_dac;
1319 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001320 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001321 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001322 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001323 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001324 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001325 }
1326
1327 return badness;
1328}
1329
1330/* return NID if the given pin has only a single connection to a certain DAC */
1331static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1332{
1333 struct hda_gen_spec *spec = codec->spec;
1334 int i;
1335 hda_nid_t nid_found = 0;
1336
1337 for (i = 0; i < spec->num_all_dacs; i++) {
1338 hda_nid_t nid = spec->all_dacs[i];
1339 if (!nid || is_dac_already_used(codec, nid))
1340 continue;
1341 if (is_reachable_path(codec, nid, pin)) {
1342 if (nid_found)
1343 return 0;
1344 nid_found = nid;
1345 }
1346 }
1347 return nid_found;
1348}
1349
1350/* check whether the given pin can be a multi-io pin */
1351static bool can_be_multiio_pin(struct hda_codec *codec,
1352 unsigned int location, hda_nid_t nid)
1353{
1354 unsigned int defcfg, caps;
1355
1356 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1357 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1358 return false;
1359 if (location && get_defcfg_location(defcfg) != location)
1360 return false;
1361 caps = snd_hda_query_pin_caps(codec, nid);
1362 if (!(caps & AC_PINCAP_OUT))
1363 return false;
1364 return true;
1365}
1366
Takashi Iwaie22aab72013-01-04 14:50:04 +01001367/* count the number of input pins that are capable to be multi-io */
1368static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1369{
1370 struct hda_gen_spec *spec = codec->spec;
1371 struct auto_pin_cfg *cfg = &spec->autocfg;
1372 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1373 unsigned int location = get_defcfg_location(defcfg);
1374 int type, i;
1375 int num_pins = 0;
1376
1377 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1378 for (i = 0; i < cfg->num_inputs; i++) {
1379 if (cfg->inputs[i].type != type)
1380 continue;
1381 if (can_be_multiio_pin(codec, location,
1382 cfg->inputs[i].pin))
1383 num_pins++;
1384 }
1385 }
1386 return num_pins;
1387}
1388
Takashi Iwai352f7f92012-12-19 12:52:06 +01001389/*
1390 * multi-io helper
1391 *
1392 * When hardwired is set, try to fill ony hardwired pins, and returns
1393 * zero if any pins are filled, non-zero if nothing found.
1394 * When hardwired is off, try to fill possible input pins, and returns
1395 * the badness value.
1396 */
1397static int fill_multi_ios(struct hda_codec *codec,
1398 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001399 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001400{
1401 struct hda_gen_spec *spec = codec->spec;
1402 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001403 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001404 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1405 unsigned int location = get_defcfg_location(defcfg);
1406 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001407 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001408
1409 old_pins = spec->multi_ios;
1410 if (old_pins >= 2)
1411 goto end_fill;
1412
Takashi Iwaie22aab72013-01-04 14:50:04 +01001413 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001414 if (num_pins < 2)
1415 goto end_fill;
1416
Takashi Iwai352f7f92012-12-19 12:52:06 +01001417 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1418 for (i = 0; i < cfg->num_inputs; i++) {
1419 hda_nid_t nid = cfg->inputs[i].pin;
1420 hda_nid_t dac = 0;
1421
1422 if (cfg->inputs[i].type != type)
1423 continue;
1424 if (!can_be_multiio_pin(codec, location, nid))
1425 continue;
1426 for (j = 0; j < spec->multi_ios; j++) {
1427 if (nid == spec->multi_io[j].pin)
1428 break;
1429 }
1430 if (j < spec->multi_ios)
1431 continue;
1432
Takashi Iwai352f7f92012-12-19 12:52:06 +01001433 if (hardwired)
1434 dac = get_dac_if_single(codec, nid);
1435 else if (!dac)
1436 dac = look_for_dac(codec, nid, false);
1437 if (!dac) {
1438 badness++;
1439 continue;
1440 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001441 path = snd_hda_add_new_path(codec, dac, nid,
1442 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001443 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001444 badness++;
1445 continue;
1446 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001447 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001448 spec->multi_io[spec->multi_ios].pin = nid;
1449 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001450 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1451 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001452 spec->multi_ios++;
1453 if (spec->multi_ios >= 2)
1454 break;
1455 }
1456 }
1457 end_fill:
1458 if (badness)
1459 badness = BAD_MULTI_IO;
1460 if (old_pins == spec->multi_ios) {
1461 if (hardwired)
1462 return 1; /* nothing found */
1463 else
1464 return badness; /* no badness if nothing found */
1465 }
1466 if (!hardwired && spec->multi_ios < 2) {
1467 /* cancel newly assigned paths */
1468 spec->paths.used -= spec->multi_ios - old_pins;
1469 spec->multi_ios = old_pins;
1470 return badness;
1471 }
1472
1473 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001474 for (i = old_pins; i < spec->multi_ios; i++) {
1475 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1476 badness += assign_out_path_ctls(codec, path);
1477 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001478
1479 return badness;
1480}
1481
1482/* map DACs for all pins in the list if they are single connections */
1483static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001484 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001485{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001486 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001487 int i;
1488 bool found = false;
1489 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001490 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001491 hda_nid_t dac;
1492 if (dacs[i])
1493 continue;
1494 dac = get_dac_if_single(codec, pins[i]);
1495 if (!dac)
1496 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001497 path = snd_hda_add_new_path(codec, dac, pins[i],
1498 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001499 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001500 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001501 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001502 dacs[i] = dac;
1503 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001504 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001505 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001506 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001507 }
1508 }
1509 return found;
1510}
1511
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001512/* create a new path including aamix if available, and return its index */
1513static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1514{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001515 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001516 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001517 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001518
1519 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001520 if (!path || !path->depth ||
1521 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001522 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001523 path_dac = path->path[0];
1524 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001525 pin = path->path[path->depth - 1];
1526 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1527 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001528 if (dac != path_dac)
1529 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001530 else if (spec->multiout.hp_out_nid[0])
1531 dac = spec->multiout.hp_out_nid[0];
1532 else if (spec->multiout.extra_out_nid[0])
1533 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001534 else
1535 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001536 if (dac)
1537 path = snd_hda_add_new_path(codec, dac, pin,
1538 spec->mixer_nid);
1539 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001540 if (!path)
1541 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001542 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001543 path->active = false; /* unused as default */
1544 return snd_hda_get_path_idx(codec, path);
1545}
1546
Takashi Iwai55a63d42013-03-21 17:20:12 +01001547/* check whether the independent HP is available with the current config */
1548static bool indep_hp_possible(struct hda_codec *codec)
1549{
1550 struct hda_gen_spec *spec = codec->spec;
1551 struct auto_pin_cfg *cfg = &spec->autocfg;
1552 struct nid_path *path;
1553 int i, idx;
1554
1555 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1556 idx = spec->out_paths[0];
1557 else
1558 idx = spec->hp_paths[0];
1559 path = snd_hda_get_path_from_idx(codec, idx);
1560 if (!path)
1561 return false;
1562
1563 /* assume no path conflicts unless aamix is involved */
1564 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1565 return true;
1566
1567 /* check whether output paths contain aamix */
1568 for (i = 0; i < cfg->line_outs; i++) {
1569 if (spec->out_paths[i] == idx)
1570 break;
1571 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1572 if (path && is_nid_contained(path, spec->mixer_nid))
1573 return false;
1574 }
1575 for (i = 0; i < cfg->speaker_outs; i++) {
1576 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1577 if (path && is_nid_contained(path, spec->mixer_nid))
1578 return false;
1579 }
1580
1581 return true;
1582}
1583
Takashi Iwaia07a9492013-01-07 16:44:06 +01001584/* fill the empty entries in the dac array for speaker/hp with the
1585 * shared dac pointed by the paths
1586 */
1587static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1588 hda_nid_t *dacs, int *path_idx)
1589{
1590 struct nid_path *path;
1591 int i;
1592
1593 for (i = 0; i < num_outs; i++) {
1594 if (dacs[i])
1595 continue;
1596 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1597 if (!path)
1598 continue;
1599 dacs[i] = path->path[0];
1600 }
1601}
1602
Takashi Iwai352f7f92012-12-19 12:52:06 +01001603/* fill in the dac_nids table from the parsed pin configuration */
1604static int fill_and_eval_dacs(struct hda_codec *codec,
1605 bool fill_hardwired,
1606 bool fill_mio_first)
1607{
1608 struct hda_gen_spec *spec = codec->spec;
1609 struct auto_pin_cfg *cfg = &spec->autocfg;
1610 int i, err, badness;
1611
1612 /* set num_dacs once to full for look_for_dac() */
1613 spec->multiout.num_dacs = cfg->line_outs;
1614 spec->multiout.dac_nids = spec->private_dac_nids;
1615 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1616 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1617 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1618 spec->multi_ios = 0;
1619 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001620
1621 /* clear path indices */
1622 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1623 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1624 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1625 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1626 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001627 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001628 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1629 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1630
Takashi Iwai352f7f92012-12-19 12:52:06 +01001631 badness = 0;
1632
1633 /* fill hard-wired DACs first */
1634 if (fill_hardwired) {
1635 bool mapped;
1636 do {
1637 mapped = map_singles(codec, cfg->line_outs,
1638 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001639 spec->private_dac_nids,
1640 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001641 mapped |= map_singles(codec, cfg->hp_outs,
1642 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001643 spec->multiout.hp_out_nid,
1644 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001645 mapped |= map_singles(codec, cfg->speaker_outs,
1646 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001647 spec->multiout.extra_out_nid,
1648 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001649 if (!spec->no_multi_io &&
1650 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001651 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001652 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001653 if (!err)
1654 mapped = true;
1655 }
1656 } while (mapped);
1657 }
1658
1659 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001660 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001661 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001662
Takashi Iwaida96fb52013-07-29 16:54:36 +02001663 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001664 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1665 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001666 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001667 if (err < 0)
1668 return err;
1669 /* we don't count badness at this stage yet */
1670 }
1671
1672 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1673 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1674 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001675 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001676 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001677 if (err < 0)
1678 return err;
1679 badness += err;
1680 }
1681 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1682 err = try_assign_dacs(codec, cfg->speaker_outs,
1683 cfg->speaker_pins,
1684 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001685 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001686 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001687 if (err < 0)
1688 return err;
1689 badness += err;
1690 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001691 if (!spec->no_multi_io &&
1692 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001693 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001694 if (err < 0)
1695 return err;
1696 badness += err;
1697 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001698
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001699 if (spec->mixer_nid) {
1700 spec->aamix_out_paths[0] =
1701 check_aamix_out_path(codec, spec->out_paths[0]);
1702 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1703 spec->aamix_out_paths[1] =
1704 check_aamix_out_path(codec, spec->hp_paths[0]);
1705 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1706 spec->aamix_out_paths[2] =
1707 check_aamix_out_path(codec, spec->speaker_paths[0]);
1708 }
1709
Takashi Iwaida96fb52013-07-29 16:54:36 +02001710 if (!spec->no_multi_io &&
1711 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001712 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1713 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001714
Takashi Iwaia07a9492013-01-07 16:44:06 +01001715 /* re-count num_dacs and squash invalid entries */
1716 spec->multiout.num_dacs = 0;
1717 for (i = 0; i < cfg->line_outs; i++) {
1718 if (spec->private_dac_nids[i])
1719 spec->multiout.num_dacs++;
1720 else {
1721 memmove(spec->private_dac_nids + i,
1722 spec->private_dac_nids + i + 1,
1723 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1724 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1725 }
1726 }
1727
1728 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001729 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001730
Takashi Iwai352f7f92012-12-19 12:52:06 +01001731 if (spec->multi_ios == 2) {
1732 for (i = 0; i < 2; i++)
1733 spec->private_dac_nids[spec->multiout.num_dacs++] =
1734 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001735 } else if (spec->multi_ios) {
1736 spec->multi_ios = 0;
1737 badness += BAD_MULTI_IO;
1738 }
1739
Takashi Iwai55a63d42013-03-21 17:20:12 +01001740 if (spec->indep_hp && !indep_hp_possible(codec))
1741 badness += BAD_NO_INDEP_HP;
1742
Takashi Iwaia07a9492013-01-07 16:44:06 +01001743 /* re-fill the shared DAC for speaker / headphone */
1744 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1745 refill_shared_dacs(codec, cfg->hp_outs,
1746 spec->multiout.hp_out_nid,
1747 spec->hp_paths);
1748 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1749 refill_shared_dacs(codec, cfg->speaker_outs,
1750 spec->multiout.extra_out_nid,
1751 spec->speaker_paths);
1752
Takashi Iwai352f7f92012-12-19 12:52:06 +01001753 return badness;
1754}
1755
1756#define DEBUG_BADNESS
1757
1758#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001759#define debug_badness(fmt, ...) \
1760 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001761#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001762#define debug_badness(fmt, ...) \
1763 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001764#endif
1765
Takashi Iwaia7694092013-01-21 10:43:18 +01001766#ifdef DEBUG_BADNESS
1767static inline void print_nid_path_idx(struct hda_codec *codec,
1768 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001769{
Takashi Iwaia7694092013-01-21 10:43:18 +01001770 struct nid_path *path;
1771
1772 path = snd_hda_get_path_from_idx(codec, idx);
1773 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001774 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001775}
1776
1777static void debug_show_configs(struct hda_codec *codec,
1778 struct auto_pin_cfg *cfg)
1779{
1780 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001781 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001782 int i;
1783
1784 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001785 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001786 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001787 spec->multiout.dac_nids[0],
1788 spec->multiout.dac_nids[1],
1789 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001790 spec->multiout.dac_nids[3],
1791 lo_type[cfg->line_out_type]);
1792 for (i = 0; i < cfg->line_outs; i++)
1793 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001794 if (spec->multi_ios > 0)
1795 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1796 spec->multi_ios,
1797 spec->multi_io[0].pin, spec->multi_io[1].pin,
1798 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001799 for (i = 0; i < spec->multi_ios; i++)
1800 print_nid_path_idx(codec, " mio",
1801 spec->out_paths[cfg->line_outs + i]);
1802 if (cfg->hp_outs)
1803 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001804 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001805 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001806 spec->multiout.hp_out_nid[0],
1807 spec->multiout.hp_out_nid[1],
1808 spec->multiout.hp_out_nid[2],
1809 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001810 for (i = 0; i < cfg->hp_outs; i++)
1811 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1812 if (cfg->speaker_outs)
1813 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001814 cfg->speaker_pins[0], cfg->speaker_pins[1],
1815 cfg->speaker_pins[2], cfg->speaker_pins[3],
1816 spec->multiout.extra_out_nid[0],
1817 spec->multiout.extra_out_nid[1],
1818 spec->multiout.extra_out_nid[2],
1819 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001820 for (i = 0; i < cfg->speaker_outs; i++)
1821 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1822 for (i = 0; i < 3; i++)
1823 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001824}
Takashi Iwaia7694092013-01-21 10:43:18 +01001825#else
1826#define debug_show_configs(codec, cfg) /* NOP */
1827#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001828
1829/* find all available DACs of the codec */
1830static void fill_all_dac_nids(struct hda_codec *codec)
1831{
1832 struct hda_gen_spec *spec = codec->spec;
1833 int i;
1834 hda_nid_t nid = codec->start_nid;
1835
1836 spec->num_all_dacs = 0;
1837 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1838 for (i = 0; i < codec->num_nodes; i++, nid++) {
1839 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1840 continue;
1841 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001842 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001843 break;
1844 }
1845 spec->all_dacs[spec->num_all_dacs++] = nid;
1846 }
1847}
1848
1849static int parse_output_paths(struct hda_codec *codec)
1850{
1851 struct hda_gen_spec *spec = codec->spec;
1852 struct auto_pin_cfg *cfg = &spec->autocfg;
1853 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001854 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001855 int best_badness = INT_MAX;
1856 int badness;
1857 bool fill_hardwired = true, fill_mio_first = true;
1858 bool best_wired = true, best_mio = true;
1859 bool hp_spk_swapped = false;
1860
Takashi Iwai352f7f92012-12-19 12:52:06 +01001861 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1862 if (!best_cfg)
1863 return -ENOMEM;
1864 *best_cfg = *cfg;
1865
1866 for (;;) {
1867 badness = fill_and_eval_dacs(codec, fill_hardwired,
1868 fill_mio_first);
1869 if (badness < 0) {
1870 kfree(best_cfg);
1871 return badness;
1872 }
1873 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1874 cfg->line_out_type, fill_hardwired, fill_mio_first,
1875 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001876 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001877 if (badness < best_badness) {
1878 best_badness = badness;
1879 *best_cfg = *cfg;
1880 best_wired = fill_hardwired;
1881 best_mio = fill_mio_first;
1882 }
1883 if (!badness)
1884 break;
1885 fill_mio_first = !fill_mio_first;
1886 if (!fill_mio_first)
1887 continue;
1888 fill_hardwired = !fill_hardwired;
1889 if (!fill_hardwired)
1890 continue;
1891 if (hp_spk_swapped)
1892 break;
1893 hp_spk_swapped = true;
1894 if (cfg->speaker_outs > 0 &&
1895 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1896 cfg->hp_outs = cfg->line_outs;
1897 memcpy(cfg->hp_pins, cfg->line_out_pins,
1898 sizeof(cfg->hp_pins));
1899 cfg->line_outs = cfg->speaker_outs;
1900 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1901 sizeof(cfg->speaker_pins));
1902 cfg->speaker_outs = 0;
1903 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1904 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1905 fill_hardwired = true;
1906 continue;
1907 }
1908 if (cfg->hp_outs > 0 &&
1909 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1910 cfg->speaker_outs = cfg->line_outs;
1911 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1912 sizeof(cfg->speaker_pins));
1913 cfg->line_outs = cfg->hp_outs;
1914 memcpy(cfg->line_out_pins, cfg->hp_pins,
1915 sizeof(cfg->hp_pins));
1916 cfg->hp_outs = 0;
1917 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1918 cfg->line_out_type = AUTO_PIN_HP_OUT;
1919 fill_hardwired = true;
1920 continue;
1921 }
1922 break;
1923 }
1924
1925 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001926 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001927 *cfg = *best_cfg;
1928 fill_and_eval_dacs(codec, best_wired, best_mio);
1929 }
1930 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1931 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001932 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001933
1934 if (cfg->line_out_pins[0]) {
1935 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001936 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001937 if (path)
1938 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001939 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001940 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1941 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001942 if (spec->dac_min_mute)
1943 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
1944 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001945 }
1946
Takashi Iwai9314a582013-01-21 10:49:05 +01001947 /* set initial pinctl targets */
1948 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1949 val = PIN_HP;
1950 else
1951 val = PIN_OUT;
1952 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1953 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1954 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1955 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1956 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1957 set_pin_targets(codec, cfg->speaker_outs,
1958 cfg->speaker_pins, val);
1959 }
1960
Takashi Iwai55a63d42013-03-21 17:20:12 +01001961 /* clear indep_hp flag if not available */
1962 if (spec->indep_hp && !indep_hp_possible(codec))
1963 spec->indep_hp = 0;
1964
Takashi Iwai352f7f92012-12-19 12:52:06 +01001965 kfree(best_cfg);
1966 return 0;
1967}
1968
1969/* add playback controls from the parsed DAC table */
1970static int create_multi_out_ctls(struct hda_codec *codec,
1971 const struct auto_pin_cfg *cfg)
1972{
1973 struct hda_gen_spec *spec = codec->spec;
1974 int i, err, noutputs;
1975
1976 noutputs = cfg->line_outs;
1977 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1978 noutputs += spec->multi_ios;
1979
1980 for (i = 0; i < noutputs; i++) {
1981 const char *name;
1982 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001983 struct nid_path *path;
1984
Takashi Iwai196c17662013-01-04 15:01:40 +01001985 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001986 if (!path)
1987 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001988
1989 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001990 if (!name || !strcmp(name, "CLFE")) {
1991 /* Center/LFE */
1992 err = add_vol_ctl(codec, "Center", 0, 1, path);
1993 if (err < 0)
1994 return err;
1995 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1996 if (err < 0)
1997 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001998 } else {
1999 err = add_stereo_vol(codec, name, index, path);
2000 if (err < 0)
2001 return err;
2002 }
2003
2004 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2005 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002006 err = add_sw_ctl(codec, "Center", 0, 1, path);
2007 if (err < 0)
2008 return err;
2009 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2010 if (err < 0)
2011 return err;
2012 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002013 err = add_stereo_sw(codec, name, index, path);
2014 if (err < 0)
2015 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017 }
2018 return 0;
2019}
2020
Takashi Iwaic2c80382013-01-07 10:33:57 +01002021static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002022 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002024 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 int err;
2026
Takashi Iwai196c17662013-01-04 15:01:40 +01002027 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002028 if (!path)
2029 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002030 err = add_stereo_vol(codec, pfx, cidx, path);
2031 if (err < 0)
2032 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002033 err = add_stereo_sw(codec, pfx, cidx, path);
2034 if (err < 0)
2035 return err;
2036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
Takashi Iwai352f7f92012-12-19 12:52:06 +01002039/* add playback controls for speaker and HP outputs */
2040static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002041 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002043 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002044
2045 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002046 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002047 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002048 int err, idx = 0;
2049
2050 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2051 name = "Bass Speaker";
2052 else if (num_pins >= 3) {
2053 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002054 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002055 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002056 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002057 name = pfx;
2058 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002060 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002061 if (err < 0)
2062 return err;
2063 }
2064 return 0;
2065}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002066
Takashi Iwai352f7f92012-12-19 12:52:06 +01002067static int create_hp_out_ctls(struct hda_codec *codec)
2068{
2069 struct hda_gen_spec *spec = codec->spec;
2070 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002071 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002072 "Headphone");
2073}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Takashi Iwai352f7f92012-12-19 12:52:06 +01002075static int create_speaker_out_ctls(struct hda_codec *codec)
2076{
2077 struct hda_gen_spec *spec = codec->spec;
2078 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002079 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002080 "Speaker");
2081}
2082
2083/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002084 * independent HP controls
2085 */
2086
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002087static void call_hp_automute(struct hda_codec *codec,
2088 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002089static int indep_hp_info(struct snd_kcontrol *kcontrol,
2090 struct snd_ctl_elem_info *uinfo)
2091{
2092 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2093}
2094
2095static int indep_hp_get(struct snd_kcontrol *kcontrol,
2096 struct snd_ctl_elem_value *ucontrol)
2097{
2098 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2099 struct hda_gen_spec *spec = codec->spec;
2100 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2101 return 0;
2102}
2103
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002104static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2105 int nomix_path_idx, int mix_path_idx,
2106 int out_type);
2107
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002108static int indep_hp_put(struct snd_kcontrol *kcontrol,
2109 struct snd_ctl_elem_value *ucontrol)
2110{
2111 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2112 struct hda_gen_spec *spec = codec->spec;
2113 unsigned int select = ucontrol->value.enumerated.item[0];
2114 int ret = 0;
2115
2116 mutex_lock(&spec->pcm_mutex);
2117 if (spec->active_streams) {
2118 ret = -EBUSY;
2119 goto unlock;
2120 }
2121
2122 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002123 hda_nid_t *dacp;
2124 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2125 dacp = &spec->private_dac_nids[0];
2126 else
2127 dacp = &spec->multiout.hp_out_nid[0];
2128
2129 /* update HP aamix paths in case it conflicts with indep HP */
2130 if (spec->have_aamix_ctl) {
2131 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2132 update_aamix_paths(codec, spec->aamix_mode,
2133 spec->out_paths[0],
2134 spec->aamix_out_paths[0],
2135 spec->autocfg.line_out_type);
2136 else
2137 update_aamix_paths(codec, spec->aamix_mode,
2138 spec->hp_paths[0],
2139 spec->aamix_out_paths[1],
2140 AUTO_PIN_HP_OUT);
2141 }
2142
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002143 spec->indep_hp_enabled = select;
2144 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002145 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002146 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002147 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002148
Takashi Iwai963afde2013-05-31 15:20:31 +02002149 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002150 ret = 1;
2151 }
2152 unlock:
2153 mutex_unlock(&spec->pcm_mutex);
2154 return ret;
2155}
2156
2157static const struct snd_kcontrol_new indep_hp_ctl = {
2158 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2159 .name = "Independent HP",
2160 .info = indep_hp_info,
2161 .get = indep_hp_get,
2162 .put = indep_hp_put,
2163};
2164
2165
2166static int create_indep_hp_ctls(struct hda_codec *codec)
2167{
2168 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002169 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002170
2171 if (!spec->indep_hp)
2172 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002173 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2174 dac = spec->multiout.dac_nids[0];
2175 else
2176 dac = spec->multiout.hp_out_nid[0];
2177 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002178 spec->indep_hp = 0;
2179 return 0;
2180 }
2181
2182 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002183 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002184 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2185 return -ENOMEM;
2186 return 0;
2187}
2188
2189/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002190 * channel mode enum control
2191 */
2192
2193static int ch_mode_info(struct snd_kcontrol *kcontrol,
2194 struct snd_ctl_elem_info *uinfo)
2195{
2196 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2197 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002198 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002199
2200 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2201 uinfo->count = 1;
2202 uinfo->value.enumerated.items = spec->multi_ios + 1;
2203 if (uinfo->value.enumerated.item > spec->multi_ios)
2204 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002205 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2206 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002207 return 0;
2208}
2209
2210static int ch_mode_get(struct snd_kcontrol *kcontrol,
2211 struct snd_ctl_elem_value *ucontrol)
2212{
2213 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2214 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002215 ucontrol->value.enumerated.item[0] =
2216 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002217 return 0;
2218}
2219
Takashi Iwai196c17662013-01-04 15:01:40 +01002220static inline struct nid_path *
2221get_multiio_path(struct hda_codec *codec, int idx)
2222{
2223 struct hda_gen_spec *spec = codec->spec;
2224 return snd_hda_get_path_from_idx(codec,
2225 spec->out_paths[spec->autocfg.line_outs + idx]);
2226}
2227
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002228static void update_automute_all(struct hda_codec *codec);
2229
Takashi Iwai65033cc2013-04-16 12:31:05 +02002230/* Default value to be passed as aamix argument for snd_hda_activate_path();
2231 * used for output paths
2232 */
2233static bool aamix_default(struct hda_gen_spec *spec)
2234{
2235 return !spec->have_aamix_ctl || spec->aamix_mode;
2236}
2237
Takashi Iwai352f7f92012-12-19 12:52:06 +01002238static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2239{
2240 struct hda_gen_spec *spec = codec->spec;
2241 hda_nid_t nid = spec->multi_io[idx].pin;
2242 struct nid_path *path;
2243
Takashi Iwai196c17662013-01-04 15:01:40 +01002244 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002245 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002247
2248 if (path->active == output)
2249 return 0;
2250
2251 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002252 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002253 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002254 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002255 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002256 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002257 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002258 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002259 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002261
2262 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002263 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002264
Takashi Iwai352f7f92012-12-19 12:52:06 +01002265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Takashi Iwai352f7f92012-12-19 12:52:06 +01002268static int ch_mode_put(struct snd_kcontrol *kcontrol,
2269 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002271 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2272 struct hda_gen_spec *spec = codec->spec;
2273 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Takashi Iwai352f7f92012-12-19 12:52:06 +01002275 ch = ucontrol->value.enumerated.item[0];
2276 if (ch < 0 || ch > spec->multi_ios)
2277 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002278 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002279 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002280 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002281 for (i = 0; i < spec->multi_ios; i++)
2282 set_multi_io(codec, i, i < ch);
2283 spec->multiout.max_channels = max(spec->ext_channel_count,
2284 spec->const_channel_count);
2285 if (spec->need_dac_fix)
2286 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return 1;
2288}
2289
Takashi Iwai352f7f92012-12-19 12:52:06 +01002290static const struct snd_kcontrol_new channel_mode_enum = {
2291 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2292 .name = "Channel Mode",
2293 .info = ch_mode_info,
2294 .get = ch_mode_get,
2295 .put = ch_mode_put,
2296};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Takashi Iwai352f7f92012-12-19 12:52:06 +01002298static int create_multi_channel_mode(struct hda_codec *codec)
2299{
2300 struct hda_gen_spec *spec = codec->spec;
2301
2302 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002303 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002304 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return 0;
2307}
2308
Takashi Iwai352f7f92012-12-19 12:52:06 +01002309/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002310 * aamix loopback enable/disable switch
2311 */
2312
2313#define loopback_mixing_info indep_hp_info
2314
2315static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2316 struct snd_ctl_elem_value *ucontrol)
2317{
2318 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2319 struct hda_gen_spec *spec = codec->spec;
2320 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2321 return 0;
2322}
2323
2324static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002325 int nomix_path_idx, int mix_path_idx,
2326 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002327{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002328 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002329 struct nid_path *nomix_path, *mix_path;
2330
2331 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2332 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2333 if (!nomix_path || !mix_path)
2334 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002335
2336 /* if HP aamix path is driven from a different DAC and the
2337 * independent HP mode is ON, can't turn on aamix path
2338 */
2339 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2340 mix_path->path[0] != spec->alt_dac_nid)
2341 do_mix = false;
2342
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002343 if (do_mix) {
2344 snd_hda_activate_path(codec, nomix_path, false, true);
2345 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002346 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002347 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002348 snd_hda_activate_path(codec, mix_path, false, false);
2349 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002350 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002351 }
2352}
2353
2354static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2355 struct snd_ctl_elem_value *ucontrol)
2356{
2357 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2358 struct hda_gen_spec *spec = codec->spec;
2359 unsigned int val = ucontrol->value.enumerated.item[0];
2360
2361 if (val == spec->aamix_mode)
2362 return 0;
2363 spec->aamix_mode = val;
2364 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002365 spec->aamix_out_paths[0],
2366 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002367 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002368 spec->aamix_out_paths[1],
2369 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002370 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002371 spec->aamix_out_paths[2],
2372 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002373 return 1;
2374}
2375
2376static const struct snd_kcontrol_new loopback_mixing_enum = {
2377 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2378 .name = "Loopback Mixing",
2379 .info = loopback_mixing_info,
2380 .get = loopback_mixing_get,
2381 .put = loopback_mixing_put,
2382};
2383
2384static int create_loopback_mixing_ctl(struct hda_codec *codec)
2385{
2386 struct hda_gen_spec *spec = codec->spec;
2387
2388 if (!spec->mixer_nid)
2389 return 0;
2390 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2391 spec->aamix_out_paths[2]))
2392 return 0;
2393 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2394 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002395 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002396 return 0;
2397}
2398
2399/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002400 * shared headphone/mic handling
2401 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002402
Takashi Iwai352f7f92012-12-19 12:52:06 +01002403static void call_update_outputs(struct hda_codec *codec);
2404
2405/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002406static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002407{
2408 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002409 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002410 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002411 hda_nid_t pin;
2412
2413 pin = spec->hp_mic_pin;
2414 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2415
2416 if (!force) {
2417 val = snd_hda_codec_get_pin_target(codec, pin);
2418 if (as_mic) {
2419 if (val & PIN_IN)
2420 return;
2421 } else {
2422 if (val & PIN_OUT)
2423 return;
2424 }
2425 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002426
2427 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002428 /* if the HP pin doesn't support VREF and the codec driver gives an
2429 * alternative pin, set up the VREF on that pin instead
2430 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002431 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2432 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2433 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2434 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002435 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002436 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002437 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002438
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002439 if (!spec->hp_mic_jack_modes) {
2440 if (as_mic)
2441 val |= PIN_IN;
2442 else
2443 val = PIN_HP;
2444 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002445 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002446 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002447}
2448
2449/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002450static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002451{
2452 struct hda_gen_spec *spec = codec->spec;
2453 struct auto_pin_cfg *cfg = &spec->autocfg;
2454 unsigned int defcfg;
2455 hda_nid_t nid;
2456
Takashi Iwai967303d2013-02-19 17:12:42 +01002457 if (!spec->hp_mic) {
2458 if (spec->suppress_hp_mic_detect)
2459 return 0;
2460 /* automatic detection: only if no input or a single internal
2461 * input pin is found, try to detect the shared hp/mic
2462 */
2463 if (cfg->num_inputs > 1)
2464 return 0;
2465 else if (cfg->num_inputs == 1) {
2466 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2467 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2468 return 0;
2469 }
2470 }
2471
2472 spec->hp_mic = 0; /* clear once */
2473 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002474 return 0;
2475
Takashi Iwai967303d2013-02-19 17:12:42 +01002476 nid = 0;
2477 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2478 nid = cfg->line_out_pins[0];
2479 else if (cfg->hp_outs > 0)
2480 nid = cfg->hp_pins[0];
2481 if (!nid)
2482 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002483
2484 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2485 return 0; /* no input */
2486
Takashi Iwai967303d2013-02-19 17:12:42 +01002487 cfg->inputs[cfg->num_inputs].pin = nid;
2488 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002489 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002490 cfg->num_inputs++;
2491 spec->hp_mic = 1;
2492 spec->hp_mic_pin = nid;
2493 /* we can't handle auto-mic together with HP-mic */
2494 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002495 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002496 return 0;
2497}
2498
Takashi Iwai978e77e2013-01-10 16:57:58 +01002499/*
2500 * output jack mode
2501 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002502
2503static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2504
2505static const char * const out_jack_texts[] = {
2506 "Line Out", "Headphone Out",
2507};
2508
Takashi Iwai978e77e2013-01-10 16:57:58 +01002509static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2510 struct snd_ctl_elem_info *uinfo)
2511{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002512 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002513}
2514
2515static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2516 struct snd_ctl_elem_value *ucontrol)
2517{
2518 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2519 hda_nid_t nid = kcontrol->private_value;
2520 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2521 ucontrol->value.enumerated.item[0] = 1;
2522 else
2523 ucontrol->value.enumerated.item[0] = 0;
2524 return 0;
2525}
2526
2527static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2528 struct snd_ctl_elem_value *ucontrol)
2529{
2530 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2531 hda_nid_t nid = kcontrol->private_value;
2532 unsigned int val;
2533
2534 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2535 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2536 return 0;
2537 snd_hda_set_pin_ctl_cache(codec, nid, val);
2538 return 1;
2539}
2540
2541static const struct snd_kcontrol_new out_jack_mode_enum = {
2542 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2543 .info = out_jack_mode_info,
2544 .get = out_jack_mode_get,
2545 .put = out_jack_mode_put,
2546};
2547
2548static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2549{
2550 struct hda_gen_spec *spec = codec->spec;
2551 int i;
2552
2553 for (i = 0; i < spec->kctls.used; i++) {
2554 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2555 if (!strcmp(kctl->name, name) && kctl->index == idx)
2556 return true;
2557 }
2558 return false;
2559}
2560
2561static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2562 char *name, size_t name_len)
2563{
2564 struct hda_gen_spec *spec = codec->spec;
2565 int idx = 0;
2566
2567 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2568 strlcat(name, " Jack Mode", name_len);
2569
2570 for (; find_kctl_name(codec, name, idx); idx++)
2571 ;
2572}
2573
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002574static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2575{
2576 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002577 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002578 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2579 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2580 return 2;
2581 }
2582 return 1;
2583}
2584
Takashi Iwai978e77e2013-01-10 16:57:58 +01002585static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2586 hda_nid_t *pins)
2587{
2588 struct hda_gen_spec *spec = codec->spec;
2589 int i;
2590
2591 for (i = 0; i < num_pins; i++) {
2592 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002593 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002594 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002595 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002596 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002597 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002598 get_jack_mode_name(codec, pin, name, sizeof(name));
2599 knew = snd_hda_gen_add_kctl(spec, name,
2600 &out_jack_mode_enum);
2601 if (!knew)
2602 return -ENOMEM;
2603 knew->private_value = pin;
2604 }
2605 }
2606
2607 return 0;
2608}
2609
Takashi Iwai294765582013-01-17 09:52:11 +01002610/*
2611 * input jack mode
2612 */
2613
2614/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2615#define NUM_VREFS 6
2616
2617static const char * const vref_texts[NUM_VREFS] = {
2618 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2619 "", "Mic 80pc Bias", "Mic 100pc Bias"
2620};
2621
2622static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2623{
2624 unsigned int pincap;
2625
2626 pincap = snd_hda_query_pin_caps(codec, pin);
2627 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2628 /* filter out unusual vrefs */
2629 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2630 return pincap;
2631}
2632
2633/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2634static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2635{
2636 unsigned int i, n = 0;
2637
2638 for (i = 0; i < NUM_VREFS; i++) {
2639 if (vref_caps & (1 << i)) {
2640 if (n == item_idx)
2641 return i;
2642 n++;
2643 }
2644 }
2645 return 0;
2646}
2647
2648/* convert back from the vref ctl index to the enum item index */
2649static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2650{
2651 unsigned int i, n = 0;
2652
2653 for (i = 0; i < NUM_VREFS; i++) {
2654 if (i == idx)
2655 return n;
2656 if (vref_caps & (1 << i))
2657 n++;
2658 }
2659 return 0;
2660}
2661
2662static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2663 struct snd_ctl_elem_info *uinfo)
2664{
2665 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2666 hda_nid_t nid = kcontrol->private_value;
2667 unsigned int vref_caps = get_vref_caps(codec, nid);
2668
2669 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2670 vref_texts);
2671 /* set the right text */
2672 strcpy(uinfo->value.enumerated.name,
2673 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2674 return 0;
2675}
2676
2677static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2678 struct snd_ctl_elem_value *ucontrol)
2679{
2680 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2681 hda_nid_t nid = kcontrol->private_value;
2682 unsigned int vref_caps = get_vref_caps(codec, nid);
2683 unsigned int idx;
2684
2685 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2686 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2687 return 0;
2688}
2689
2690static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2691 struct snd_ctl_elem_value *ucontrol)
2692{
2693 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2694 hda_nid_t nid = kcontrol->private_value;
2695 unsigned int vref_caps = get_vref_caps(codec, nid);
2696 unsigned int val, idx;
2697
2698 val = snd_hda_codec_get_pin_target(codec, nid);
2699 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2700 if (idx == ucontrol->value.enumerated.item[0])
2701 return 0;
2702
2703 val &= ~AC_PINCTL_VREFEN;
2704 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2705 snd_hda_set_pin_ctl_cache(codec, nid, val);
2706 return 1;
2707}
2708
2709static const struct snd_kcontrol_new in_jack_mode_enum = {
2710 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2711 .info = in_jack_mode_info,
2712 .get = in_jack_mode_get,
2713 .put = in_jack_mode_put,
2714};
2715
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002716static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2717{
2718 struct hda_gen_spec *spec = codec->spec;
2719 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002720 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002721 nitems = hweight32(get_vref_caps(codec, pin));
2722 return nitems ? nitems : 1;
2723}
2724
Takashi Iwai294765582013-01-17 09:52:11 +01002725static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2726{
2727 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002728 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002729 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002730 unsigned int defcfg;
2731
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002732 if (pin == spec->hp_mic_pin)
2733 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002734
2735 /* no jack mode for fixed pins */
2736 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2737 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2738 return 0;
2739
2740 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002741 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002742 return 0;
2743
2744 get_jack_mode_name(codec, pin, name, sizeof(name));
2745 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2746 if (!knew)
2747 return -ENOMEM;
2748 knew->private_value = pin;
2749 return 0;
2750}
2751
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002752/*
2753 * HP/mic shared jack mode
2754 */
2755static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2756 struct snd_ctl_elem_info *uinfo)
2757{
2758 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2759 hda_nid_t nid = kcontrol->private_value;
2760 int out_jacks = get_out_jack_num_items(codec, nid);
2761 int in_jacks = get_in_jack_num_items(codec, nid);
2762 const char *text = NULL;
2763 int idx;
2764
2765 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2766 uinfo->count = 1;
2767 uinfo->value.enumerated.items = out_jacks + in_jacks;
2768 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2769 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2770 idx = uinfo->value.enumerated.item;
2771 if (idx < out_jacks) {
2772 if (out_jacks > 1)
2773 text = out_jack_texts[idx];
2774 else
2775 text = "Headphone Out";
2776 } else {
2777 idx -= out_jacks;
2778 if (in_jacks > 1) {
2779 unsigned int vref_caps = get_vref_caps(codec, nid);
2780 text = vref_texts[get_vref_idx(vref_caps, idx)];
2781 } else
2782 text = "Mic In";
2783 }
2784
2785 strcpy(uinfo->value.enumerated.name, text);
2786 return 0;
2787}
2788
2789static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2790{
2791 int out_jacks = get_out_jack_num_items(codec, nid);
2792 int in_jacks = get_in_jack_num_items(codec, nid);
2793 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2794 int idx = 0;
2795
2796 if (val & PIN_OUT) {
2797 if (out_jacks > 1 && val == PIN_HP)
2798 idx = 1;
2799 } else if (val & PIN_IN) {
2800 idx = out_jacks;
2801 if (in_jacks > 1) {
2802 unsigned int vref_caps = get_vref_caps(codec, nid);
2803 val &= AC_PINCTL_VREFEN;
2804 idx += cvt_from_vref_idx(vref_caps, val);
2805 }
2806 }
2807 return idx;
2808}
2809
2810static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2811 struct snd_ctl_elem_value *ucontrol)
2812{
2813 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2814 hda_nid_t nid = kcontrol->private_value;
2815 ucontrol->value.enumerated.item[0] =
2816 get_cur_hp_mic_jack_mode(codec, nid);
2817 return 0;
2818}
2819
2820static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2821 struct snd_ctl_elem_value *ucontrol)
2822{
2823 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2824 hda_nid_t nid = kcontrol->private_value;
2825 int out_jacks = get_out_jack_num_items(codec, nid);
2826 int in_jacks = get_in_jack_num_items(codec, nid);
2827 unsigned int val, oldval, idx;
2828
2829 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2830 idx = ucontrol->value.enumerated.item[0];
2831 if (oldval == idx)
2832 return 0;
2833
2834 if (idx < out_jacks) {
2835 if (out_jacks > 1)
2836 val = idx ? PIN_HP : PIN_OUT;
2837 else
2838 val = PIN_HP;
2839 } else {
2840 idx -= out_jacks;
2841 if (in_jacks > 1) {
2842 unsigned int vref_caps = get_vref_caps(codec, nid);
2843 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002844 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2845 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002846 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002847 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002848 }
2849 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002850 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002851
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002852 return 1;
2853}
2854
2855static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2856 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2857 .info = hp_mic_jack_mode_info,
2858 .get = hp_mic_jack_mode_get,
2859 .put = hp_mic_jack_mode_put,
2860};
2861
2862static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2863{
2864 struct hda_gen_spec *spec = codec->spec;
2865 struct snd_kcontrol_new *knew;
2866
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002867 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2868 &hp_mic_jack_mode_enum);
2869 if (!knew)
2870 return -ENOMEM;
2871 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002872 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002873 return 0;
2874}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002875
2876/*
2877 * Parse input paths
2878 */
2879
Takashi Iwai352f7f92012-12-19 12:52:06 +01002880/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002881static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002882{
2883 struct hda_amp_list *list;
2884
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002885 list = snd_array_new(&spec->loopback_list);
2886 if (!list)
2887 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002888 list->nid = mix;
2889 list->dir = HDA_INPUT;
2890 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002891 spec->loopback.amplist = spec->loopback_list.list;
2892 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002893}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002894
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002895/* return true if either a volume or a mute amp is found for the given
2896 * aamix path; the amp has to be either in the mixer node or its direct leaf
2897 */
2898static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2899 hda_nid_t pin, unsigned int *mix_val,
2900 unsigned int *mute_val)
2901{
2902 int idx, num_conns;
2903 const hda_nid_t *list;
2904 hda_nid_t nid;
2905
2906 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2907 if (idx < 0)
2908 return false;
2909
2910 *mix_val = *mute_val = 0;
2911 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2912 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2913 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2914 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2915 if (*mix_val && *mute_val)
2916 return true;
2917
2918 /* check leaf node */
2919 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2920 if (num_conns < idx)
2921 return false;
2922 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01002923 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2924 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002925 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01002926 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2927 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002928 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2929
2930 return *mix_val || *mute_val;
2931}
2932
Takashi Iwai352f7f92012-12-19 12:52:06 +01002933/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002934static int new_analog_input(struct hda_codec *codec, int input_idx,
2935 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002936 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002938 struct hda_gen_spec *spec = codec->spec;
2939 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002940 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002941 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002943 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2944 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002945
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002946 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002947 if (!path)
2948 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002949 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002950 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002951
2952 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002953 if (mix_val) {
2954 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002955 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002957 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 }
2959
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002960 if (mute_val) {
2961 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002962 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002964 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 }
2966
Takashi Iwai352f7f92012-12-19 12:52:06 +01002967 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002968 err = add_loopback_list(spec, mix_nid, idx);
2969 if (err < 0)
2970 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002971
2972 if (spec->mixer_nid != spec->mixer_merge_nid &&
2973 !spec->loopback_merge_path) {
2974 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2975 spec->mixer_merge_nid, 0);
2976 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002977 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002978 path->active = true;
2979 spec->loopback_merge_path =
2980 snd_hda_get_path_idx(codec, path);
2981 }
2982 }
2983
Takashi Iwai352f7f92012-12-19 12:52:06 +01002984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985}
2986
Takashi Iwai352f7f92012-12-19 12:52:06 +01002987static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002989 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2990 return (pincap & AC_PINCAP_IN) != 0;
2991}
2992
2993/* Parse the codec tree and retrieve ADCs */
2994static int fill_adc_nids(struct hda_codec *codec)
2995{
2996 struct hda_gen_spec *spec = codec->spec;
2997 hda_nid_t nid;
2998 hda_nid_t *adc_nids = spec->adc_nids;
2999 int max_nums = ARRAY_SIZE(spec->adc_nids);
3000 int i, nums = 0;
3001
3002 nid = codec->start_nid;
3003 for (i = 0; i < codec->num_nodes; i++, nid++) {
3004 unsigned int caps = get_wcaps(codec, nid);
3005 int type = get_wcaps_type(caps);
3006
3007 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3008 continue;
3009 adc_nids[nums] = nid;
3010 if (++nums >= max_nums)
3011 break;
3012 }
3013 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003014
3015 /* copy the detected ADCs to all_adcs[] */
3016 spec->num_all_adcs = nums;
3017 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3018
Takashi Iwai352f7f92012-12-19 12:52:06 +01003019 return nums;
3020}
3021
3022/* filter out invalid adc_nids that don't give all active input pins;
3023 * if needed, check whether dynamic ADC-switching is available
3024 */
3025static int check_dyn_adc_switch(struct hda_codec *codec)
3026{
3027 struct hda_gen_spec *spec = codec->spec;
3028 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003029 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003030 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003031
Takashi Iwai352f7f92012-12-19 12:52:06 +01003032 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003033 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003034 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003035 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003036 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003037 break;
3038 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003039 if (i >= imux->num_items) {
3040 ok_bits |= (1 << n);
3041 nums++;
3042 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003043 }
3044
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003045 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003046 /* check whether ADC-switch is possible */
3047 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003048 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003049 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003050 spec->dyn_adc_idx[i] = n;
3051 break;
3052 }
3053 }
3054 }
3055
Takashi Iwai4e76a882014-02-25 12:21:03 +01003056 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003057 spec->dyn_adc_switch = 1;
3058 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003059 /* shrink the invalid adcs and input paths */
3060 nums = 0;
3061 for (n = 0; n < spec->num_adc_nids; n++) {
3062 if (!(ok_bits & (1 << n)))
3063 continue;
3064 if (n != nums) {
3065 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003066 for (i = 0; i < imux->num_items; i++) {
3067 invalidate_nid_path(codec,
3068 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003069 spec->input_paths[i][nums] =
3070 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003071 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003072 }
3073 nums++;
3074 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003075 spec->num_adc_nids = nums;
3076 }
3077
Takashi Iwai967303d2013-02-19 17:12:42 +01003078 if (imux->num_items == 1 ||
3079 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003080 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003081 spec->num_adc_nids = 1; /* reduce to a single ADC */
3082 }
3083
3084 /* single index for individual volumes ctls */
3085 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3086 spec->num_adc_nids = 1;
3087
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 return 0;
3089}
3090
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003091/* parse capture source paths from the given pin and create imux items */
3092static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003093 int cfg_idx, int num_adcs,
3094 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003095{
3096 struct hda_gen_spec *spec = codec->spec;
3097 struct hda_input_mux *imux = &spec->input_mux;
3098 int imux_idx = imux->num_items;
3099 bool imux_added = false;
3100 int c;
3101
3102 for (c = 0; c < num_adcs; c++) {
3103 struct nid_path *path;
3104 hda_nid_t adc = spec->adc_nids[c];
3105
3106 if (!is_reachable_path(codec, pin, adc))
3107 continue;
3108 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3109 if (!path)
3110 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003111 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003112 spec->input_paths[imux_idx][c] =
3113 snd_hda_get_path_idx(codec, path);
3114
3115 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003116 if (spec->hp_mic_pin == pin)
3117 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003118 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003119 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003120 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003121 if (spec->dyn_adc_switch)
3122 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003123 }
3124 }
3125
3126 return 0;
3127}
3128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003130 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003132
Takashi Iwaic9700422013-01-18 10:17:30 +01003133/* fill the label for each input at first */
3134static int fill_input_pin_labels(struct hda_codec *codec)
3135{
3136 struct hda_gen_spec *spec = codec->spec;
3137 const struct auto_pin_cfg *cfg = &spec->autocfg;
3138 int i;
3139
3140 for (i = 0; i < cfg->num_inputs; i++) {
3141 hda_nid_t pin = cfg->inputs[i].pin;
3142 const char *label;
3143 int j, idx;
3144
3145 if (!is_input_pin(codec, pin))
3146 continue;
3147
3148 label = hda_get_autocfg_input_label(codec, cfg, i);
3149 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003150 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003151 if (spec->input_labels[j] &&
3152 !strcmp(spec->input_labels[j], label)) {
3153 idx = spec->input_label_idxs[j] + 1;
3154 break;
3155 }
3156 }
3157
3158 spec->input_labels[i] = label;
3159 spec->input_label_idxs[i] = idx;
3160 }
3161
3162 return 0;
3163}
3164
Takashi Iwai9dba2052013-01-18 10:01:15 +01003165#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3166
Takashi Iwai352f7f92012-12-19 12:52:06 +01003167static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003168{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003169 struct hda_gen_spec *spec = codec->spec;
3170 const struct auto_pin_cfg *cfg = &spec->autocfg;
3171 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003172 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003173 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003174 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003175
Takashi Iwai352f7f92012-12-19 12:52:06 +01003176 num_adcs = fill_adc_nids(codec);
3177 if (num_adcs < 0)
3178 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003179
Takashi Iwaic9700422013-01-18 10:17:30 +01003180 err = fill_input_pin_labels(codec);
3181 if (err < 0)
3182 return err;
3183
Takashi Iwai352f7f92012-12-19 12:52:06 +01003184 for (i = 0; i < cfg->num_inputs; i++) {
3185 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Takashi Iwai352f7f92012-12-19 12:52:06 +01003187 pin = cfg->inputs[i].pin;
3188 if (!is_input_pin(codec, pin))
3189 continue;
3190
Takashi Iwai2c12c302013-01-10 09:33:29 +01003191 val = PIN_IN;
3192 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3193 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003194 if (pin != spec->hp_mic_pin)
3195 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003196
Takashi Iwai352f7f92012-12-19 12:52:06 +01003197 if (mixer) {
3198 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003199 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003200 spec->input_labels[i],
3201 spec->input_label_idxs[i],
3202 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003203 if (err < 0)
3204 return err;
3205 }
3206 }
3207
Takashi Iwaic9700422013-01-18 10:17:30 +01003208 err = parse_capture_source(codec, pin, i, num_adcs,
3209 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003210 if (err < 0)
3211 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003212
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003213 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003214 err = create_in_jack_mode(codec, pin);
3215 if (err < 0)
3216 return err;
3217 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003218 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003219
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003220 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003221 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003222 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003223 "Stereo Mix", 0);
3224 if (err < 0)
3225 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003226 else
3227 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003228 }
3229
3230 return 0;
3231}
3232
3233
3234/*
3235 * input source mux
3236 */
3237
Takashi Iwaic697b712013-01-07 17:09:26 +01003238/* get the input path specified by the given adc and imux indices */
3239static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003240{
3241 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003242 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3243 snd_BUG();
3244 return NULL;
3245 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003246 if (spec->dyn_adc_switch)
3247 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003248 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003249 snd_BUG();
3250 return NULL;
3251 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003252 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003253}
3254
3255static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3256 unsigned int idx);
3257
3258static int mux_enum_info(struct snd_kcontrol *kcontrol,
3259 struct snd_ctl_elem_info *uinfo)
3260{
3261 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3262 struct hda_gen_spec *spec = codec->spec;
3263 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3264}
3265
3266static int mux_enum_get(struct snd_kcontrol *kcontrol,
3267 struct snd_ctl_elem_value *ucontrol)
3268{
3269 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3270 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003271 /* the ctls are created at once with multiple counts */
3272 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003273
3274 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3275 return 0;
3276}
3277
3278static int mux_enum_put(struct snd_kcontrol *kcontrol,
3279 struct snd_ctl_elem_value *ucontrol)
3280{
3281 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003282 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003283 return mux_select(codec, adc_idx,
3284 ucontrol->value.enumerated.item[0]);
3285}
3286
Takashi Iwai352f7f92012-12-19 12:52:06 +01003287static const struct snd_kcontrol_new cap_src_temp = {
3288 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3289 .name = "Input Source",
3290 .info = mux_enum_info,
3291 .get = mux_enum_get,
3292 .put = mux_enum_put,
3293};
3294
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003295/*
3296 * capture volume and capture switch ctls
3297 */
3298
Takashi Iwai352f7f92012-12-19 12:52:06 +01003299typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3300 struct snd_ctl_elem_value *ucontrol);
3301
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003302/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003303static int cap_put_caller(struct snd_kcontrol *kcontrol,
3304 struct snd_ctl_elem_value *ucontrol,
3305 put_call_t func, int type)
3306{
3307 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3308 struct hda_gen_spec *spec = codec->spec;
3309 const struct hda_input_mux *imux;
3310 struct nid_path *path;
3311 int i, adc_idx, err = 0;
3312
3313 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003314 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003315 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003316 /* we use the cache-only update at first since multiple input paths
3317 * may shared the same amp; by updating only caches, the redundant
3318 * writes to hardware can be reduced.
3319 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003320 codec->cached_write = 1;
3321 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003322 path = get_input_path(codec, adc_idx, i);
3323 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003324 continue;
3325 kcontrol->private_value = path->ctls[type];
3326 err = func(kcontrol, ucontrol);
3327 if (err < 0)
3328 goto error;
3329 }
3330 error:
3331 codec->cached_write = 0;
3332 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003333 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003334 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003335 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003336 return err;
3337}
3338
3339/* capture volume ctl callbacks */
3340#define cap_vol_info snd_hda_mixer_amp_volume_info
3341#define cap_vol_get snd_hda_mixer_amp_volume_get
3342#define cap_vol_tlv snd_hda_mixer_amp_tlv
3343
3344static int cap_vol_put(struct snd_kcontrol *kcontrol,
3345 struct snd_ctl_elem_value *ucontrol)
3346{
3347 return cap_put_caller(kcontrol, ucontrol,
3348 snd_hda_mixer_amp_volume_put,
3349 NID_PATH_VOL_CTL);
3350}
3351
3352static const struct snd_kcontrol_new cap_vol_temp = {
3353 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3354 .name = "Capture Volume",
3355 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3356 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3357 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3358 .info = cap_vol_info,
3359 .get = cap_vol_get,
3360 .put = cap_vol_put,
3361 .tlv = { .c = cap_vol_tlv },
3362};
3363
3364/* capture switch ctl callbacks */
3365#define cap_sw_info snd_ctl_boolean_stereo_info
3366#define cap_sw_get snd_hda_mixer_amp_switch_get
3367
3368static int cap_sw_put(struct snd_kcontrol *kcontrol,
3369 struct snd_ctl_elem_value *ucontrol)
3370{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003371 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003372 snd_hda_mixer_amp_switch_put,
3373 NID_PATH_MUTE_CTL);
3374}
3375
3376static const struct snd_kcontrol_new cap_sw_temp = {
3377 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3378 .name = "Capture Switch",
3379 .info = cap_sw_info,
3380 .get = cap_sw_get,
3381 .put = cap_sw_put,
3382};
3383
3384static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3385{
3386 hda_nid_t nid;
3387 int i, depth;
3388
3389 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3390 for (depth = 0; depth < 3; depth++) {
3391 if (depth >= path->depth)
3392 return -EINVAL;
3393 i = path->depth - depth - 1;
3394 nid = path->path[i];
3395 if (!path->ctls[NID_PATH_VOL_CTL]) {
3396 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3397 path->ctls[NID_PATH_VOL_CTL] =
3398 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3399 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3400 int idx = path->idx[i];
3401 if (!depth && codec->single_adc_amp)
3402 idx = 0;
3403 path->ctls[NID_PATH_VOL_CTL] =
3404 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3405 }
3406 }
3407 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3408 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3409 path->ctls[NID_PATH_MUTE_CTL] =
3410 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3411 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3412 int idx = path->idx[i];
3413 if (!depth && codec->single_adc_amp)
3414 idx = 0;
3415 path->ctls[NID_PATH_MUTE_CTL] =
3416 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3417 }
3418 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 return 0;
3421}
3422
Takashi Iwai352f7f92012-12-19 12:52:06 +01003423static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003425 struct hda_gen_spec *spec = codec->spec;
3426 struct auto_pin_cfg *cfg = &spec->autocfg;
3427 unsigned int val;
3428 int i;
3429
3430 if (!spec->inv_dmic_split)
3431 return false;
3432 for (i = 0; i < cfg->num_inputs; i++) {
3433 if (cfg->inputs[i].pin != nid)
3434 continue;
3435 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3436 return false;
3437 val = snd_hda_codec_get_pincfg(codec, nid);
3438 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3439 }
3440 return false;
3441}
3442
Takashi Iwaia90229e2013-01-18 14:10:00 +01003443/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003444static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3445 struct snd_ctl_elem_value *ucontrol)
3446{
3447 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3448 struct hda_gen_spec *spec = codec->spec;
3449 int ret;
3450
3451 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3452 if (ret < 0)
3453 return ret;
3454
Takashi Iwaia90229e2013-01-18 14:10:00 +01003455 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003456 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003457
3458 return ret;
3459}
3460
Takashi Iwai352f7f92012-12-19 12:52:06 +01003461static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3462 int idx, bool is_switch, unsigned int ctl,
3463 bool inv_dmic)
3464{
3465 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003466 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003467 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3468 const char *sfx = is_switch ? "Switch" : "Volume";
3469 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003470 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003471
3472 if (!ctl)
3473 return 0;
3474
3475 if (label)
3476 snprintf(tmpname, sizeof(tmpname),
3477 "%s Capture %s", label, sfx);
3478 else
3479 snprintf(tmpname, sizeof(tmpname),
3480 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003481 knew = add_control(spec, type, tmpname, idx,
3482 amp_val_replace_channels(ctl, chs));
3483 if (!knew)
3484 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003485 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003486 knew->put = cap_single_sw_put;
3487 if (!inv_dmic)
3488 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003489
3490 /* Make independent right kcontrol */
3491 if (label)
3492 snprintf(tmpname, sizeof(tmpname),
3493 "Inverted %s Capture %s", label, sfx);
3494 else
3495 snprintf(tmpname, sizeof(tmpname),
3496 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003497 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003498 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003499 if (!knew)
3500 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003501 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003502 knew->put = cap_single_sw_put;
3503 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003504}
3505
3506/* create single (and simple) capture volume and switch controls */
3507static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3508 unsigned int vol_ctl, unsigned int sw_ctl,
3509 bool inv_dmic)
3510{
3511 int err;
3512 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3513 if (err < 0)
3514 return err;
3515 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3516 if (err < 0)
3517 return err;
3518 return 0;
3519}
3520
3521/* create bound capture volume and switch controls */
3522static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3523 unsigned int vol_ctl, unsigned int sw_ctl)
3524{
3525 struct hda_gen_spec *spec = codec->spec;
3526 struct snd_kcontrol_new *knew;
3527
3528 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003529 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003530 if (!knew)
3531 return -ENOMEM;
3532 knew->index = idx;
3533 knew->private_value = vol_ctl;
3534 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3535 }
3536 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003537 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003538 if (!knew)
3539 return -ENOMEM;
3540 knew->index = idx;
3541 knew->private_value = sw_ctl;
3542 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3543 }
3544 return 0;
3545}
3546
3547/* return the vol ctl when used first in the imux list */
3548static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3549{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003550 struct nid_path *path;
3551 unsigned int ctl;
3552 int i;
3553
Takashi Iwaic697b712013-01-07 17:09:26 +01003554 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003555 if (!path)
3556 return 0;
3557 ctl = path->ctls[type];
3558 if (!ctl)
3559 return 0;
3560 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003561 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003562 if (path && path->ctls[type] == ctl)
3563 return 0;
3564 }
3565 return ctl;
3566}
3567
3568/* create individual capture volume and switch controls per input */
3569static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3570{
3571 struct hda_gen_spec *spec = codec->spec;
3572 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003573 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003574
3575 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003576 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003577 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003578
Takashi Iwaic9700422013-01-18 10:17:30 +01003579 idx = imux->items[i].index;
3580 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003581 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003582 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3583
3584 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003585 err = add_single_cap_ctl(codec,
3586 spec->input_labels[idx],
3587 spec->input_label_idxs[idx],
3588 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003589 get_first_cap_ctl(codec, i, type),
3590 inv_dmic);
3591 if (err < 0)
3592 return err;
3593 }
3594 }
3595 return 0;
3596}
3597
3598static int create_capture_mixers(struct hda_codec *codec)
3599{
3600 struct hda_gen_spec *spec = codec->spec;
3601 struct hda_input_mux *imux = &spec->input_mux;
3602 int i, n, nums, err;
3603
3604 if (spec->dyn_adc_switch)
3605 nums = 1;
3606 else
3607 nums = spec->num_adc_nids;
3608
3609 if (!spec->auto_mic && imux->num_items > 1) {
3610 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003611 const char *name;
3612 name = nums > 1 ? "Input Source" : "Capture Source";
3613 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003614 if (!knew)
3615 return -ENOMEM;
3616 knew->count = nums;
3617 }
3618
3619 for (n = 0; n < nums; n++) {
3620 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003621 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003622 bool inv_dmic = false;
3623 int vol, sw;
3624
3625 vol = sw = 0;
3626 for (i = 0; i < imux->num_items; i++) {
3627 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003628 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003629 if (!path)
3630 continue;
3631 parse_capvol_in_path(codec, path);
3632 if (!vol)
3633 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003634 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003635 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003636 if (!same_amp_caps(codec, vol,
3637 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3638 multi_cap_vol = true;
3639 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003640 if (!sw)
3641 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003642 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003643 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003644 if (!same_amp_caps(codec, sw,
3645 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3646 multi_cap_vol = true;
3647 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003648 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3649 inv_dmic = true;
3650 }
3651
3652 if (!multi)
3653 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3654 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003655 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003656 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3657 else
3658 err = create_multi_cap_vol_ctl(codec);
3659 if (err < 0)
3660 return err;
3661 }
3662
3663 return 0;
3664}
3665
3666/*
3667 * add mic boosts if needed
3668 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003669
3670/* check whether the given amp is feasible as a boost volume */
3671static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3672 int dir, int idx)
3673{
3674 unsigned int step;
3675
3676 if (!nid_has_volume(codec, nid, dir) ||
3677 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3678 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3679 return false;
3680
3681 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3682 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3683 if (step < 0x20)
3684 return false;
3685 return true;
3686}
3687
3688/* look for a boost amp in a widget close to the pin */
3689static unsigned int look_for_boost_amp(struct hda_codec *codec,
3690 struct nid_path *path)
3691{
3692 unsigned int val = 0;
3693 hda_nid_t nid;
3694 int depth;
3695
3696 for (depth = 0; depth < 3; depth++) {
3697 if (depth >= path->depth - 1)
3698 break;
3699 nid = path->path[depth];
3700 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3701 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3702 break;
3703 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3704 path->idx[depth])) {
3705 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3706 HDA_INPUT);
3707 break;
3708 }
3709 }
3710
3711 return val;
3712}
3713
Takashi Iwai352f7f92012-12-19 12:52:06 +01003714static int parse_mic_boost(struct hda_codec *codec)
3715{
3716 struct hda_gen_spec *spec = codec->spec;
3717 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003718 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003719 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003720
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003721 if (!spec->num_adc_nids)
3722 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003723
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003724 for (i = 0; i < imux->num_items; i++) {
3725 struct nid_path *path;
3726 unsigned int val;
3727 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003728 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003729
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003730 idx = imux->items[i].index;
3731 if (idx >= imux->num_items)
3732 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003733
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003734 /* check only line-in and mic pins */
Takashi Iwai1799cdd2013-01-18 14:37:16 +01003735 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003736 continue;
3737
3738 path = get_input_path(codec, 0, i);
3739 if (!path)
3740 continue;
3741
3742 val = look_for_boost_amp(codec, path);
3743 if (!val)
3744 continue;
3745
3746 /* create a boost control */
3747 snprintf(boost_label, sizeof(boost_label),
3748 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003749 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3750 spec->input_label_idxs[idx], val))
3751 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003752
3753 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003754 }
3755 return 0;
3756}
3757
3758/*
3759 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3760 */
3761static void parse_digital(struct hda_codec *codec)
3762{
3763 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003764 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003765 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003766 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003767
3768 /* support multiple SPDIFs; the secondary is set up as a slave */
3769 nums = 0;
3770 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003771 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003772 dig_nid = look_for_dac(codec, pin, true);
3773 if (!dig_nid)
3774 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003775 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003776 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003777 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003778 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003779 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003780 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003781 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003782 if (!nums) {
3783 spec->multiout.dig_out_nid = dig_nid;
3784 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3785 } else {
3786 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3787 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd5764222014-05-14 17:18:31 +03003788 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003789 spec->slave_dig_outs[nums - 1] = dig_nid;
3790 }
3791 nums++;
3792 }
3793
3794 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003795 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003796 dig_nid = codec->start_nid;
3797 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003798 unsigned int wcaps = get_wcaps(codec, dig_nid);
3799 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3800 continue;
3801 if (!(wcaps & AC_WCAP_DIGITAL))
3802 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003803 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003804 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003805 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003806 path->active = true;
3807 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003808 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003809 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003810 break;
3811 }
3812 }
3813 }
3814}
3815
3816
3817/*
3818 * input MUX handling
3819 */
3820
3821static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3822
3823/* select the given imux item; either unmute exclusively or select the route */
3824static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3825 unsigned int idx)
3826{
3827 struct hda_gen_spec *spec = codec->spec;
3828 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003829 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003830
3831 imux = &spec->input_mux;
3832 if (!imux->num_items)
3833 return 0;
3834
3835 if (idx >= imux->num_items)
3836 idx = imux->num_items - 1;
3837 if (spec->cur_mux[adc_idx] == idx)
3838 return 0;
3839
Takashi Iwai55196ff2013-01-24 17:32:56 +01003840 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3841 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003842 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003843 if (old_path->active)
3844 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003845
3846 spec->cur_mux[adc_idx] = idx;
3847
Takashi Iwai967303d2013-02-19 17:12:42 +01003848 if (spec->hp_mic)
3849 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003850
3851 if (spec->dyn_adc_switch)
3852 dyn_adc_pcm_resetup(codec, idx);
3853
Takashi Iwaic697b712013-01-07 17:09:26 +01003854 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003855 if (!path)
3856 return 0;
3857 if (path->active)
3858 return 0;
3859 snd_hda_activate_path(codec, path, true, false);
3860 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003861 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003862 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003863 return 1;
3864}
3865
3866
3867/*
3868 * Jack detections for HP auto-mute and mic-switch
3869 */
3870
3871/* check each pin in the given array; returns true if any of them is plugged */
3872static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3873{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003874 int i;
3875 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003876
3877 for (i = 0; i < num_pins; i++) {
3878 hda_nid_t nid = pins[i];
3879 if (!nid)
3880 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003881 /* don't detect pins retasked as inputs */
3882 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3883 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003884 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3885 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003886 }
3887 return present;
3888}
3889
3890/* standard HP/line-out auto-mute helper */
3891static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003892 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003893{
3894 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003895 int i;
3896
3897 for (i = 0; i < num_pins; i++) {
3898 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003899 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003900 if (!nid)
3901 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003902
3903 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003904 struct nid_path *path;
3905 hda_nid_t mute_nid;
3906
3907 path = snd_hda_get_path_from_idx(codec, paths[i]);
3908 if (!path)
3909 continue;
3910 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3911 if (!mute_nid)
3912 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003913 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003914 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003915 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003916 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003917 set_pin_eapd(codec, nid, !mute);
3918 continue;
3919 }
3920
Takashi Iwai967303d2013-02-19 17:12:42 +01003921 oldval = snd_hda_codec_get_pin_target(codec, nid);
3922 if (oldval & PIN_IN)
3923 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003924 /* don't reset VREF value in case it's controlling
3925 * the amp (see alc861_fixup_asus_amp_vref_0f())
3926 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003927 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003928 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003929 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003930 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003931 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003932 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003933 /* here we call update_pin_ctl() so that the pinctl is changed
3934 * without changing the pinctl target value;
3935 * the original target value will be still referred at the
3936 * init / resume again
3937 */
3938 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003939 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003940 }
3941}
3942
Takashi Iwaidda42bd2014-10-30 12:04:50 +01003943/**
3944 * snd_hda_gen_update_outputs - Toggle outputs muting
3945 * @codec: the HDA codec
3946 *
3947 * Update the mute status of all outputs based on the current jack states.
3948 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003949void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003950{
3951 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003952 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003953 int on;
3954
3955 /* Control HP pins/amps depending on master_mute state;
3956 * in general, HP pins/amps control should be enabled in all cases,
3957 * but currently set only for master_mute, just to be safe
3958 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003959 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3960 paths = spec->out_paths;
3961 else
3962 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01003963 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003964 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003965
3966 if (!spec->automute_speaker)
3967 on = 0;
3968 else
3969 on = spec->hp_jack_present | spec->line_jack_present;
3970 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003971 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003972 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3973 paths = spec->out_paths;
3974 else
3975 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003976 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003977 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003978
3979 /* toggle line-out mutes if needed, too */
3980 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3981 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3982 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3983 return;
3984 if (!spec->automute_lo)
3985 on = 0;
3986 else
3987 on = spec->hp_jack_present;
3988 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003989 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003990 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003991 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003992 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003993}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003994EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003995
3996static void call_update_outputs(struct hda_codec *codec)
3997{
3998 struct hda_gen_spec *spec = codec->spec;
3999 if (spec->automute_hook)
4000 spec->automute_hook(codec);
4001 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004002 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004003
4004 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4005 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4006 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004007}
4008
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004009/**
4010 * snd_hda_gen_hp_automute - standard HP-automute helper
4011 * @codec: the HDA codec
4012 * @jack: jack object, NULL for the whole
4013 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004014void snd_hda_gen_hp_automute(struct hda_codec *codec,
4015 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004016{
4017 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004018 hda_nid_t *pins = spec->autocfg.hp_pins;
4019 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004020
Takashi Iwai92603c52013-01-22 07:46:31 +01004021 /* No detection for the first HP jack during indep-HP mode */
4022 if (spec->indep_hp_enabled) {
4023 pins++;
4024 num_pins--;
4025 }
4026
4027 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004028 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4029 return;
4030 call_update_outputs(codec);
4031}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004032EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004033
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004034/**
4035 * snd_hda_gen_line_automute - standard line-out-automute helper
4036 * @codec: the HDA codec
4037 * @jack: jack object, NULL for the whole
4038 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004039void snd_hda_gen_line_automute(struct hda_codec *codec,
4040 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004041{
4042 struct hda_gen_spec *spec = codec->spec;
4043
4044 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4045 return;
4046 /* check LO jack only when it's different from HP */
4047 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4048 return;
4049
4050 spec->line_jack_present =
4051 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4052 spec->autocfg.line_out_pins);
4053 if (!spec->automute_speaker || !spec->detect_lo)
4054 return;
4055 call_update_outputs(codec);
4056}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004057EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004058
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004059/**
4060 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4061 * @codec: the HDA codec
4062 * @jack: jack object, NULL for the whole
4063 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004064void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4065 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004066{
4067 struct hda_gen_spec *spec = codec->spec;
4068 int i;
4069
4070 if (!spec->auto_mic)
4071 return;
4072
4073 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004074 hda_nid_t pin = spec->am_entry[i].pin;
4075 /* don't detect pins retasked as outputs */
4076 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4077 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004078 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004079 mux_select(codec, 0, spec->am_entry[i].idx);
4080 return;
4081 }
4082 }
4083 mux_select(codec, 0, spec->am_entry[0].idx);
4084}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004085EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004086
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004087/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004088static void call_hp_automute(struct hda_codec *codec,
4089 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004090{
4091 struct hda_gen_spec *spec = codec->spec;
4092 if (spec->hp_automute_hook)
4093 spec->hp_automute_hook(codec, jack);
4094 else
4095 snd_hda_gen_hp_automute(codec, jack);
4096}
4097
4098static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004099 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004100{
4101 struct hda_gen_spec *spec = codec->spec;
4102 if (spec->line_automute_hook)
4103 spec->line_automute_hook(codec, jack);
4104 else
4105 snd_hda_gen_line_automute(codec, jack);
4106}
4107
4108static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004109 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004110{
4111 struct hda_gen_spec *spec = codec->spec;
4112 if (spec->mic_autoswitch_hook)
4113 spec->mic_autoswitch_hook(codec, jack);
4114 else
4115 snd_hda_gen_mic_autoswitch(codec, jack);
4116}
4117
Takashi Iwai963afde2013-05-31 15:20:31 +02004118/* update jack retasking */
4119static void update_automute_all(struct hda_codec *codec)
4120{
4121 call_hp_automute(codec, NULL);
4122 call_line_automute(codec, NULL);
4123 call_mic_autoswitch(codec, NULL);
4124}
4125
Takashi Iwai352f7f92012-12-19 12:52:06 +01004126/*
4127 * Auto-Mute mode mixer enum support
4128 */
4129static int automute_mode_info(struct snd_kcontrol *kcontrol,
4130 struct snd_ctl_elem_info *uinfo)
4131{
4132 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4133 struct hda_gen_spec *spec = codec->spec;
4134 static const char * const texts3[] = {
4135 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004136 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
Takashi Iwai352f7f92012-12-19 12:52:06 +01004138 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4139 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4140 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4141}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Takashi Iwai352f7f92012-12-19 12:52:06 +01004143static int automute_mode_get(struct snd_kcontrol *kcontrol,
4144 struct snd_ctl_elem_value *ucontrol)
4145{
4146 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4147 struct hda_gen_spec *spec = codec->spec;
4148 unsigned int val = 0;
4149 if (spec->automute_speaker)
4150 val++;
4151 if (spec->automute_lo)
4152 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004153
Takashi Iwai352f7f92012-12-19 12:52:06 +01004154 ucontrol->value.enumerated.item[0] = val;
4155 return 0;
4156}
4157
4158static int automute_mode_put(struct snd_kcontrol *kcontrol,
4159 struct snd_ctl_elem_value *ucontrol)
4160{
4161 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4162 struct hda_gen_spec *spec = codec->spec;
4163
4164 switch (ucontrol->value.enumerated.item[0]) {
4165 case 0:
4166 if (!spec->automute_speaker && !spec->automute_lo)
4167 return 0;
4168 spec->automute_speaker = 0;
4169 spec->automute_lo = 0;
4170 break;
4171 case 1:
4172 if (spec->automute_speaker_possible) {
4173 if (!spec->automute_lo && spec->automute_speaker)
4174 return 0;
4175 spec->automute_speaker = 1;
4176 spec->automute_lo = 0;
4177 } else if (spec->automute_lo_possible) {
4178 if (spec->automute_lo)
4179 return 0;
4180 spec->automute_lo = 1;
4181 } else
4182 return -EINVAL;
4183 break;
4184 case 2:
4185 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4186 return -EINVAL;
4187 if (spec->automute_speaker && spec->automute_lo)
4188 return 0;
4189 spec->automute_speaker = 1;
4190 spec->automute_lo = 1;
4191 break;
4192 default:
4193 return -EINVAL;
4194 }
4195 call_update_outputs(codec);
4196 return 1;
4197}
4198
4199static const struct snd_kcontrol_new automute_mode_enum = {
4200 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4201 .name = "Auto-Mute Mode",
4202 .info = automute_mode_info,
4203 .get = automute_mode_get,
4204 .put = automute_mode_put,
4205};
4206
4207static int add_automute_mode_enum(struct hda_codec *codec)
4208{
4209 struct hda_gen_spec *spec = codec->spec;
4210
Takashi Iwai12c93df2012-12-19 14:38:33 +01004211 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004212 return -ENOMEM;
4213 return 0;
4214}
4215
4216/*
4217 * Check the availability of HP/line-out auto-mute;
4218 * Set up appropriately if really supported
4219 */
4220static int check_auto_mute_availability(struct hda_codec *codec)
4221{
4222 struct hda_gen_spec *spec = codec->spec;
4223 struct auto_pin_cfg *cfg = &spec->autocfg;
4224 int present = 0;
4225 int i, err;
4226
Takashi Iwaif72706b2013-01-16 18:20:07 +01004227 if (spec->suppress_auto_mute)
4228 return 0;
4229
Takashi Iwai352f7f92012-12-19 12:52:06 +01004230 if (cfg->hp_pins[0])
4231 present++;
4232 if (cfg->line_out_pins[0])
4233 present++;
4234 if (cfg->speaker_pins[0])
4235 present++;
4236 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004237 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004238
4239 if (!cfg->speaker_pins[0] &&
4240 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4241 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4242 sizeof(cfg->speaker_pins));
4243 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
Takashi Iwai352f7f92012-12-19 12:52:06 +01004246 if (!cfg->hp_pins[0] &&
4247 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4248 memcpy(cfg->hp_pins, cfg->line_out_pins,
4249 sizeof(cfg->hp_pins));
4250 cfg->hp_outs = cfg->line_outs;
4251 }
4252
4253 for (i = 0; i < cfg->hp_outs; i++) {
4254 hda_nid_t nid = cfg->hp_pins[i];
4255 if (!is_jack_detectable(codec, nid))
4256 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004257 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004258 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004259 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004260 spec->detect_hp = 1;
4261 }
4262
4263 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4264 if (cfg->speaker_outs)
4265 for (i = 0; i < cfg->line_outs; i++) {
4266 hda_nid_t nid = cfg->line_out_pins[i];
4267 if (!is_jack_detectable(codec, nid))
4268 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004269 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004270 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004271 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004272 spec->detect_lo = 1;
4273 }
4274 spec->automute_lo_possible = spec->detect_hp;
4275 }
4276
4277 spec->automute_speaker_possible = cfg->speaker_outs &&
4278 (spec->detect_hp || spec->detect_lo);
4279
4280 spec->automute_lo = spec->automute_lo_possible;
4281 spec->automute_speaker = spec->automute_speaker_possible;
4282
4283 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4284 /* create a control for automute mode */
4285 err = add_automute_mode_enum(codec);
4286 if (err < 0)
4287 return err;
4288 }
4289 return 0;
4290}
4291
Takashi Iwai352f7f92012-12-19 12:52:06 +01004292/* check whether all auto-mic pins are valid; setup indices if OK */
4293static bool auto_mic_check_imux(struct hda_codec *codec)
4294{
4295 struct hda_gen_spec *spec = codec->spec;
4296 const struct hda_input_mux *imux;
4297 int i;
4298
4299 imux = &spec->input_mux;
4300 for (i = 0; i < spec->am_num_entries; i++) {
4301 spec->am_entry[i].idx =
4302 find_idx_in_nid_list(spec->am_entry[i].pin,
4303 spec->imux_pins, imux->num_items);
4304 if (spec->am_entry[i].idx < 0)
4305 return false; /* no corresponding imux */
4306 }
4307
4308 /* we don't need the jack detection for the first pin */
4309 for (i = 1; i < spec->am_num_entries; i++)
4310 snd_hda_jack_detect_enable_callback(codec,
4311 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004312 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004313 return true;
4314}
4315
4316static int compare_attr(const void *ap, const void *bp)
4317{
4318 const struct automic_entry *a = ap;
4319 const struct automic_entry *b = bp;
4320 return (int)(a->attr - b->attr);
4321}
4322
4323/*
4324 * Check the availability of auto-mic switch;
4325 * Set up if really supported
4326 */
4327static int check_auto_mic_availability(struct hda_codec *codec)
4328{
4329 struct hda_gen_spec *spec = codec->spec;
4330 struct auto_pin_cfg *cfg = &spec->autocfg;
4331 unsigned int types;
4332 int i, num_pins;
4333
Takashi Iwaid12daf62013-01-07 16:32:11 +01004334 if (spec->suppress_auto_mic)
4335 return 0;
4336
Takashi Iwai352f7f92012-12-19 12:52:06 +01004337 types = 0;
4338 num_pins = 0;
4339 for (i = 0; i < cfg->num_inputs; i++) {
4340 hda_nid_t nid = cfg->inputs[i].pin;
4341 unsigned int attr;
4342 attr = snd_hda_codec_get_pincfg(codec, nid);
4343 attr = snd_hda_get_input_pin_attr(attr);
4344 if (types & (1 << attr))
4345 return 0; /* already occupied */
4346 switch (attr) {
4347 case INPUT_PIN_ATTR_INT:
4348 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4349 return 0; /* invalid type */
4350 break;
4351 case INPUT_PIN_ATTR_UNUSED:
4352 return 0; /* invalid entry */
4353 default:
4354 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4355 return 0; /* invalid type */
4356 if (!spec->line_in_auto_switch &&
4357 cfg->inputs[i].type != AUTO_PIN_MIC)
4358 return 0; /* only mic is allowed */
4359 if (!is_jack_detectable(codec, nid))
4360 return 0; /* no unsol support */
4361 break;
4362 }
4363 if (num_pins >= MAX_AUTO_MIC_PINS)
4364 return 0;
4365 types |= (1 << attr);
4366 spec->am_entry[num_pins].pin = nid;
4367 spec->am_entry[num_pins].attr = attr;
4368 num_pins++;
4369 }
4370
4371 if (num_pins < 2)
4372 return 0;
4373
4374 spec->am_num_entries = num_pins;
4375 /* sort the am_entry in the order of attr so that the pin with a
4376 * higher attr will be selected when the jack is plugged.
4377 */
4378 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4379 compare_attr, NULL);
4380
4381 if (!auto_mic_check_imux(codec))
4382 return 0;
4383
4384 spec->auto_mic = 1;
4385 spec->num_adc_nids = 1;
4386 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004387 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004388 spec->am_entry[0].pin,
4389 spec->am_entry[1].pin,
4390 spec->am_entry[2].pin);
4391
4392 return 0;
4393}
4394
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004395/**
4396 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4397 * into power down
4398 * @codec: the HDA codec
4399 * @nid: NID to evalute
4400 * @power_state: target power state
4401 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004402unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004403 hda_nid_t nid,
4404 unsigned int power_state)
4405{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004406 if (power_state != AC_PWRST_D0 || nid == codec->afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004407 return power_state;
4408 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4409 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004410 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004411 return power_state;
4412 return AC_PWRST_D3;
4413}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004414EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004415
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004416/* mute all aamix inputs initially; parse up to the first leaves */
4417static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4418{
4419 int i, nums;
4420 const hda_nid_t *conn;
4421 bool has_amp;
4422
4423 nums = snd_hda_get_conn_list(codec, mix, &conn);
4424 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4425 for (i = 0; i < nums; i++) {
4426 if (has_amp)
4427 snd_hda_codec_amp_stereo(codec, mix,
4428 HDA_INPUT, i,
4429 0xff, HDA_AMP_MUTE);
4430 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4431 snd_hda_codec_amp_stereo(codec, conn[i],
4432 HDA_OUTPUT, 0,
4433 0xff, HDA_AMP_MUTE);
4434 }
4435}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004436
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004437/**
4438 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4439 * set up the hda_gen_spec
4440 * @codec: the HDA codec
4441 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004442 *
4443 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004444 * or a negative error code
4445 */
4446int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004447 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004448{
4449 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004450 int err;
4451
Takashi Iwai1c70a582013-01-11 17:48:22 +01004452 parse_user_hints(codec);
4453
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004454 if (spec->mixer_nid && !spec->mixer_merge_nid)
4455 spec->mixer_merge_nid = spec->mixer_nid;
4456
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004457 if (cfg != &spec->autocfg) {
4458 spec->autocfg = *cfg;
4459 cfg = &spec->autocfg;
4460 }
4461
Takashi Iwai98bd1112013-03-22 14:53:50 +01004462 if (!spec->main_out_badness)
4463 spec->main_out_badness = &hda_main_out_badness;
4464 if (!spec->extra_out_badness)
4465 spec->extra_out_badness = &hda_extra_out_badness;
4466
David Henningsson6fc4cb92013-01-16 15:58:45 +01004467 fill_all_dac_nids(codec);
4468
Takashi Iwai352f7f92012-12-19 12:52:06 +01004469 if (!cfg->line_outs) {
4470 if (cfg->dig_outs || cfg->dig_in_pin) {
4471 spec->multiout.max_channels = 2;
4472 spec->no_analog = 1;
4473 goto dig_only;
4474 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004475 if (!cfg->num_inputs && !cfg->dig_in_pin)
4476 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004477 }
4478
4479 if (!spec->no_primary_hp &&
4480 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4481 cfg->line_outs <= cfg->hp_outs) {
4482 /* use HP as primary out */
4483 cfg->speaker_outs = cfg->line_outs;
4484 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4485 sizeof(cfg->speaker_pins));
4486 cfg->line_outs = cfg->hp_outs;
4487 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4488 cfg->hp_outs = 0;
4489 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4490 cfg->line_out_type = AUTO_PIN_HP_OUT;
4491 }
4492
4493 err = parse_output_paths(codec);
4494 if (err < 0)
4495 return err;
4496 err = create_multi_channel_mode(codec);
4497 if (err < 0)
4498 return err;
4499 err = create_multi_out_ctls(codec, cfg);
4500 if (err < 0)
4501 return err;
4502 err = create_hp_out_ctls(codec);
4503 if (err < 0)
4504 return err;
4505 err = create_speaker_out_ctls(codec);
4506 if (err < 0)
4507 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004508 err = create_indep_hp_ctls(codec);
4509 if (err < 0)
4510 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004511 err = create_loopback_mixing_ctl(codec);
4512 if (err < 0)
4513 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004514 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004515 if (err < 0)
4516 return err;
4517 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004518 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004519 return err;
4520
Takashi Iwaia07a9492013-01-07 16:44:06 +01004521 spec->const_channel_count = spec->ext_channel_count;
4522 /* check the multiple speaker and headphone pins */
4523 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4524 spec->const_channel_count = max(spec->const_channel_count,
4525 cfg->speaker_outs * 2);
4526 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4527 spec->const_channel_count = max(spec->const_channel_count,
4528 cfg->hp_outs * 2);
4529 spec->multiout.max_channels = max(spec->ext_channel_count,
4530 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004531
4532 err = check_auto_mute_availability(codec);
4533 if (err < 0)
4534 return err;
4535
4536 err = check_dyn_adc_switch(codec);
4537 if (err < 0)
4538 return err;
4539
Takashi Iwai967303d2013-02-19 17:12:42 +01004540 err = check_auto_mic_availability(codec);
4541 if (err < 0)
4542 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004543
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004544 /* add stereo mix if available and not enabled yet */
4545 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01004546 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4547 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004548 err = parse_capture_source(codec, spec->mixer_nid,
4549 CFG_IDX_MIX, spec->num_all_adcs,
4550 "Stereo Mix", 0);
4551 if (err < 0)
4552 return err;
4553 }
4554
4555
Takashi Iwai352f7f92012-12-19 12:52:06 +01004556 err = create_capture_mixers(codec);
4557 if (err < 0)
4558 return err;
4559
4560 err = parse_mic_boost(codec);
4561 if (err < 0)
4562 return err;
4563
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004564 /* create "Headphone Mic Jack Mode" if no input selection is
4565 * available (or user specifies add_jack_modes hint)
4566 */
4567 if (spec->hp_mic_pin &&
4568 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4569 spec->add_jack_modes)) {
4570 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4571 if (err < 0)
4572 return err;
4573 }
4574
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004575 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004576 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4577 err = create_out_jack_modes(codec, cfg->line_outs,
4578 cfg->line_out_pins);
4579 if (err < 0)
4580 return err;
4581 }
4582 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4583 err = create_out_jack_modes(codec, cfg->hp_outs,
4584 cfg->hp_pins);
4585 if (err < 0)
4586 return err;
4587 }
4588 }
4589
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004590 /* mute all aamix input initially */
4591 if (spec->mixer_nid)
4592 mute_all_mixer_nid(codec, spec->mixer_nid);
4593
Takashi Iwai352f7f92012-12-19 12:52:06 +01004594 dig_only:
4595 parse_digital(codec);
4596
Takashi Iwai55196ff2013-01-24 17:32:56 +01004597 if (spec->power_down_unused)
4598 codec->power_filter = snd_hda_gen_path_power_filter;
4599
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004600 if (!spec->no_analog && spec->beep_nid) {
4601 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4602 if (err < 0)
4603 return err;
4604 }
4605
Takashi Iwai352f7f92012-12-19 12:52:06 +01004606 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004608EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
4610
4611/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004612 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004614
4615/* slave controls for virtual master */
4616static const char * const slave_pfxs[] = {
4617 "Front", "Surround", "Center", "LFE", "Side",
4618 "Headphone", "Speaker", "Mono", "Line Out",
4619 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004620 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4621 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02004622 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004623 NULL,
4624};
4625
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004626/**
4627 * snd_hda_gen_build_controls - Build controls from the parsed results
4628 * @codec: the HDA codec
4629 *
4630 * Pass this to build_controls patch_ops.
4631 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004632int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004634 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
Takashi Iwai36502d02012-12-19 15:15:10 +01004637 if (spec->kctls.used) {
4638 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4639 if (err < 0)
4640 return err;
4641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Takashi Iwai352f7f92012-12-19 12:52:06 +01004643 if (spec->multiout.dig_out_nid) {
4644 err = snd_hda_create_dig_out_ctls(codec,
4645 spec->multiout.dig_out_nid,
4646 spec->multiout.dig_out_nid,
4647 spec->pcm_rec[1].pcm_type);
4648 if (err < 0)
4649 return err;
4650 if (!spec->no_analog) {
4651 err = snd_hda_create_spdif_share_sw(codec,
4652 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 if (err < 0)
4654 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004655 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 }
4657 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004658 if (spec->dig_in_nid) {
4659 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4660 if (err < 0)
4661 return err;
4662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Takashi Iwai352f7f92012-12-19 12:52:06 +01004664 /* if we have no master control, let's create it */
4665 if (!spec->no_analog &&
4666 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004667 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004668 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004669 "Playback Volume");
4670 if (err < 0)
4671 return err;
4672 }
4673 if (!spec->no_analog &&
4674 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4675 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4676 NULL, slave_pfxs,
4677 "Playback Switch",
4678 true, &spec->vmaster_mute.sw_kctl);
4679 if (err < 0)
4680 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02004681 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01004682 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4683 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02004684 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4685 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Takashi Iwai352f7f92012-12-19 12:52:06 +01004688 free_kctls(spec); /* no longer needed */
4689
Takashi Iwai352f7f92012-12-19 12:52:06 +01004690 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4691 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 return err;
4693
4694 return 0;
4695}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004696EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004697
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
4699/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004700 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004703static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4704 struct hda_codec *codec,
4705 struct snd_pcm_substream *substream,
4706 int action)
4707{
4708 struct hda_gen_spec *spec = codec->spec;
4709 if (spec->pcm_playback_hook)
4710 spec->pcm_playback_hook(hinfo, codec, substream, action);
4711}
4712
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004713static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4714 struct hda_codec *codec,
4715 struct snd_pcm_substream *substream,
4716 int action)
4717{
4718 struct hda_gen_spec *spec = codec->spec;
4719 if (spec->pcm_capture_hook)
4720 spec->pcm_capture_hook(hinfo, codec, substream, action);
4721}
4722
Takashi Iwai352f7f92012-12-19 12:52:06 +01004723/*
4724 * Analog playback callbacks
4725 */
4726static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4727 struct hda_codec *codec,
4728 struct snd_pcm_substream *substream)
4729{
4730 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004731 int err;
4732
4733 mutex_lock(&spec->pcm_mutex);
4734 err = snd_hda_multi_out_analog_open(codec,
4735 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004736 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004737 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004738 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004739 call_pcm_playback_hook(hinfo, codec, substream,
4740 HDA_GEN_PCM_ACT_OPEN);
4741 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004742 mutex_unlock(&spec->pcm_mutex);
4743 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004744}
4745
4746static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004747 struct hda_codec *codec,
4748 unsigned int stream_tag,
4749 unsigned int format,
4750 struct snd_pcm_substream *substream)
4751{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004752 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004753 int err;
4754
4755 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4756 stream_tag, format, substream);
4757 if (!err)
4758 call_pcm_playback_hook(hinfo, codec, substream,
4759 HDA_GEN_PCM_ACT_PREPARE);
4760 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004761}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004762
Takashi Iwai352f7f92012-12-19 12:52:06 +01004763static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4764 struct hda_codec *codec,
4765 struct snd_pcm_substream *substream)
4766{
4767 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004768 int err;
4769
4770 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4771 if (!err)
4772 call_pcm_playback_hook(hinfo, codec, substream,
4773 HDA_GEN_PCM_ACT_CLEANUP);
4774 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004775}
4776
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004777static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4778 struct hda_codec *codec,
4779 struct snd_pcm_substream *substream)
4780{
4781 struct hda_gen_spec *spec = codec->spec;
4782 mutex_lock(&spec->pcm_mutex);
4783 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004784 call_pcm_playback_hook(hinfo, codec, substream,
4785 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004786 mutex_unlock(&spec->pcm_mutex);
4787 return 0;
4788}
4789
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004790static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4791 struct hda_codec *codec,
4792 struct snd_pcm_substream *substream)
4793{
4794 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4795 return 0;
4796}
4797
4798static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4799 struct hda_codec *codec,
4800 unsigned int stream_tag,
4801 unsigned int format,
4802 struct snd_pcm_substream *substream)
4803{
4804 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4805 call_pcm_capture_hook(hinfo, codec, substream,
4806 HDA_GEN_PCM_ACT_PREPARE);
4807 return 0;
4808}
4809
4810static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4811 struct hda_codec *codec,
4812 struct snd_pcm_substream *substream)
4813{
4814 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4815 call_pcm_capture_hook(hinfo, codec, substream,
4816 HDA_GEN_PCM_ACT_CLEANUP);
4817 return 0;
4818}
4819
4820static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4821 struct hda_codec *codec,
4822 struct snd_pcm_substream *substream)
4823{
4824 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4825 return 0;
4826}
4827
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004828static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4829 struct hda_codec *codec,
4830 struct snd_pcm_substream *substream)
4831{
4832 struct hda_gen_spec *spec = codec->spec;
4833 int err = 0;
4834
4835 mutex_lock(&spec->pcm_mutex);
4836 if (!spec->indep_hp_enabled)
4837 err = -EBUSY;
4838 else
4839 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004840 call_pcm_playback_hook(hinfo, codec, substream,
4841 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004842 mutex_unlock(&spec->pcm_mutex);
4843 return err;
4844}
4845
4846static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4847 struct hda_codec *codec,
4848 struct snd_pcm_substream *substream)
4849{
4850 struct hda_gen_spec *spec = codec->spec;
4851 mutex_lock(&spec->pcm_mutex);
4852 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004853 call_pcm_playback_hook(hinfo, codec, substream,
4854 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004855 mutex_unlock(&spec->pcm_mutex);
4856 return 0;
4857}
4858
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004859static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4860 struct hda_codec *codec,
4861 unsigned int stream_tag,
4862 unsigned int format,
4863 struct snd_pcm_substream *substream)
4864{
4865 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4866 call_pcm_playback_hook(hinfo, codec, substream,
4867 HDA_GEN_PCM_ACT_PREPARE);
4868 return 0;
4869}
4870
4871static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4872 struct hda_codec *codec,
4873 struct snd_pcm_substream *substream)
4874{
4875 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4876 call_pcm_playback_hook(hinfo, codec, substream,
4877 HDA_GEN_PCM_ACT_CLEANUP);
4878 return 0;
4879}
4880
Takashi Iwai352f7f92012-12-19 12:52:06 +01004881/*
4882 * Digital out
4883 */
4884static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4885 struct hda_codec *codec,
4886 struct snd_pcm_substream *substream)
4887{
4888 struct hda_gen_spec *spec = codec->spec;
4889 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4890}
4891
4892static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4893 struct hda_codec *codec,
4894 unsigned int stream_tag,
4895 unsigned int format,
4896 struct snd_pcm_substream *substream)
4897{
4898 struct hda_gen_spec *spec = codec->spec;
4899 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4900 stream_tag, format, substream);
4901}
4902
4903static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4904 struct hda_codec *codec,
4905 struct snd_pcm_substream *substream)
4906{
4907 struct hda_gen_spec *spec = codec->spec;
4908 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4909}
4910
4911static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4912 struct hda_codec *codec,
4913 struct snd_pcm_substream *substream)
4914{
4915 struct hda_gen_spec *spec = codec->spec;
4916 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4917}
4918
4919/*
4920 * Analog capture
4921 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004922#define alt_capture_pcm_open capture_pcm_open
4923#define alt_capture_pcm_close capture_pcm_close
4924
Takashi Iwai352f7f92012-12-19 12:52:06 +01004925static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4926 struct hda_codec *codec,
4927 unsigned int stream_tag,
4928 unsigned int format,
4929 struct snd_pcm_substream *substream)
4930{
4931 struct hda_gen_spec *spec = codec->spec;
4932
4933 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004934 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004935 call_pcm_capture_hook(hinfo, codec, substream,
4936 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004937 return 0;
4938}
4939
Takashi Iwai352f7f92012-12-19 12:52:06 +01004940static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4941 struct hda_codec *codec,
4942 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004943{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004944 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004945
Takashi Iwai352f7f92012-12-19 12:52:06 +01004946 snd_hda_codec_cleanup_stream(codec,
4947 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004948 call_pcm_capture_hook(hinfo, codec, substream,
4949 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004950 return 0;
4951}
4952
Takashi Iwai352f7f92012-12-19 12:52:06 +01004953/*
4954 */
4955static const struct hda_pcm_stream pcm_analog_playback = {
4956 .substreams = 1,
4957 .channels_min = 2,
4958 .channels_max = 8,
4959 /* NID is set in build_pcms */
4960 .ops = {
4961 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004962 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004963 .prepare = playback_pcm_prepare,
4964 .cleanup = playback_pcm_cleanup
4965 },
4966};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
Takashi Iwai352f7f92012-12-19 12:52:06 +01004968static const struct hda_pcm_stream pcm_analog_capture = {
4969 .substreams = 1,
4970 .channels_min = 2,
4971 .channels_max = 2,
4972 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004973 .ops = {
4974 .open = capture_pcm_open,
4975 .close = capture_pcm_close,
4976 .prepare = capture_pcm_prepare,
4977 .cleanup = capture_pcm_cleanup
4978 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004979};
4980
4981static const struct hda_pcm_stream pcm_analog_alt_playback = {
4982 .substreams = 1,
4983 .channels_min = 2,
4984 .channels_max = 2,
4985 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004986 .ops = {
4987 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004988 .close = alt_playback_pcm_close,
4989 .prepare = alt_playback_pcm_prepare,
4990 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004991 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004992};
4993
4994static const struct hda_pcm_stream pcm_analog_alt_capture = {
4995 .substreams = 2, /* can be overridden */
4996 .channels_min = 2,
4997 .channels_max = 2,
4998 /* NID is set in build_pcms */
4999 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005000 .open = alt_capture_pcm_open,
5001 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005002 .prepare = alt_capture_pcm_prepare,
5003 .cleanup = alt_capture_pcm_cleanup
5004 },
5005};
5006
5007static const struct hda_pcm_stream pcm_digital_playback = {
5008 .substreams = 1,
5009 .channels_min = 2,
5010 .channels_max = 2,
5011 /* NID is set in build_pcms */
5012 .ops = {
5013 .open = dig_playback_pcm_open,
5014 .close = dig_playback_pcm_close,
5015 .prepare = dig_playback_pcm_prepare,
5016 .cleanup = dig_playback_pcm_cleanup
5017 },
5018};
5019
5020static const struct hda_pcm_stream pcm_digital_capture = {
5021 .substreams = 1,
5022 .channels_min = 2,
5023 .channels_max = 2,
5024 /* NID is set in build_pcms */
5025};
5026
5027/* Used by build_pcms to flag that a PCM has no playback stream */
5028static const struct hda_pcm_stream pcm_null_stream = {
5029 .substreams = 0,
5030 .channels_min = 0,
5031 .channels_max = 0,
5032};
5033
5034/*
5035 * dynamic changing ADC PCM streams
5036 */
5037static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5038{
5039 struct hda_gen_spec *spec = codec->spec;
5040 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5041
5042 if (spec->cur_adc && spec->cur_adc != new_adc) {
5043 /* stream is running, let's swap the current ADC */
5044 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5045 spec->cur_adc = new_adc;
5046 snd_hda_codec_setup_stream(codec, new_adc,
5047 spec->cur_adc_stream_tag, 0,
5048 spec->cur_adc_format);
5049 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005051 return false;
5052}
5053
5054/* analog capture with dynamic dual-adc changes */
5055static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5056 struct hda_codec *codec,
5057 unsigned int stream_tag,
5058 unsigned int format,
5059 struct snd_pcm_substream *substream)
5060{
5061 struct hda_gen_spec *spec = codec->spec;
5062 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5063 spec->cur_adc_stream_tag = stream_tag;
5064 spec->cur_adc_format = format;
5065 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5066 return 0;
5067}
5068
5069static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5070 struct hda_codec *codec,
5071 struct snd_pcm_substream *substream)
5072{
5073 struct hda_gen_spec *spec = codec->spec;
5074 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5075 spec->cur_adc = 0;
5076 return 0;
5077}
5078
5079static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5080 .substreams = 1,
5081 .channels_min = 2,
5082 .channels_max = 2,
5083 .nid = 0, /* fill later */
5084 .ops = {
5085 .prepare = dyn_adc_capture_pcm_prepare,
5086 .cleanup = dyn_adc_capture_pcm_cleanup
5087 },
5088};
5089
Takashi Iwaif873e532012-12-20 16:58:39 +01005090static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5091 const char *chip_name)
5092{
5093 char *p;
5094
5095 if (*str)
5096 return;
5097 strlcpy(str, chip_name, len);
5098
5099 /* drop non-alnum chars after a space */
5100 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5101 if (!isalnum(p[1])) {
5102 *p = 0;
5103 break;
5104 }
5105 }
5106 strlcat(str, sfx, len);
5107}
5108
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005109/**
5110 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5111 * @codec: the HDA codec
5112 *
5113 * Pass this to build_pcms patch_ops.
5114 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005115int snd_hda_gen_build_pcms(struct hda_codec *codec)
5116{
5117 struct hda_gen_spec *spec = codec->spec;
5118 struct hda_pcm *info = spec->pcm_rec;
5119 const struct hda_pcm_stream *p;
5120 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
5122 codec->num_pcms = 1;
5123 codec->pcm_info = info;
5124
Takashi Iwai352f7f92012-12-19 12:52:06 +01005125 if (spec->no_analog)
5126 goto skip_analog;
5127
Takashi Iwaif873e532012-12-20 16:58:39 +01005128 fill_pcm_stream_name(spec->stream_name_analog,
5129 sizeof(spec->stream_name_analog),
5130 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005131 info->name = spec->stream_name_analog;
5132
5133 if (spec->multiout.num_dacs > 0) {
5134 p = spec->stream_analog_playback;
5135 if (!p)
5136 p = &pcm_analog_playback;
5137 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5138 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
5139 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5140 spec->multiout.max_channels;
5141 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5142 spec->autocfg.line_outs == 2)
5143 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5144 snd_pcm_2_1_chmaps;
5145 }
5146 if (spec->num_adc_nids) {
5147 p = spec->stream_analog_capture;
5148 if (!p) {
5149 if (spec->dyn_adc_switch)
5150 p = &dyn_adc_pcm_analog_capture;
5151 else
5152 p = &pcm_analog_capture;
5153 }
5154 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5155 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
5156 }
5157
Takashi Iwai352f7f92012-12-19 12:52:06 +01005158 skip_analog:
5159 /* SPDIF for stream index #1 */
5160 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005161 fill_pcm_stream_name(spec->stream_name_digital,
5162 sizeof(spec->stream_name_digital),
5163 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005164 codec->num_pcms = 2;
5165 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5166 info = spec->pcm_rec + 1;
5167 info->name = spec->stream_name_digital;
5168 if (spec->dig_out_type)
5169 info->pcm_type = spec->dig_out_type;
5170 else
5171 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5172 if (spec->multiout.dig_out_nid) {
5173 p = spec->stream_digital_playback;
5174 if (!p)
5175 p = &pcm_digital_playback;
5176 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5177 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
5178 }
5179 if (spec->dig_in_nid) {
5180 p = spec->stream_digital_capture;
5181 if (!p)
5182 p = &pcm_digital_capture;
5183 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5184 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
5185 }
5186 }
5187
5188 if (spec->no_analog)
5189 return 0;
5190
5191 /* If the use of more than one ADC is requested for the current
5192 * model, configure a second analog capture-only PCM.
5193 */
5194 have_multi_adcs = (spec->num_adc_nids > 1) &&
5195 !spec->dyn_adc_switch && !spec->auto_mic;
5196 /* Additional Analaog capture for index #2 */
5197 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005198 fill_pcm_stream_name(spec->stream_name_alt_analog,
5199 sizeof(spec->stream_name_alt_analog),
5200 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005201 codec->num_pcms = 3;
5202 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01005203 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005204 if (spec->alt_dac_nid) {
5205 p = spec->stream_analog_alt_playback;
5206 if (!p)
5207 p = &pcm_analog_alt_playback;
5208 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5209 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5210 spec->alt_dac_nid;
5211 } else {
5212 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5213 pcm_null_stream;
5214 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5215 }
5216 if (have_multi_adcs) {
5217 p = spec->stream_analog_alt_capture;
5218 if (!p)
5219 p = &pcm_analog_alt_capture;
5220 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5221 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5222 spec->adc_nids[1];
5223 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5224 spec->num_adc_nids - 1;
5225 } else {
5226 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5227 pcm_null_stream;
5228 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 }
5231
5232 return 0;
5233}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005234EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005235
5236
5237/*
5238 * Standard auto-parser initializations
5239 */
5240
Takashi Iwaid4156932013-01-07 10:08:02 +01005241/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005242static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005243{
5244 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005245 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005246
Takashi Iwai196c17662013-01-04 15:01:40 +01005247 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005248 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005249 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005250 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005251 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005252 snd_hda_activate_path(codec, path, path->active,
5253 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005254 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005255}
5256
5257/* initialize primary output paths */
5258static void init_multi_out(struct hda_codec *codec)
5259{
5260 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005261 int i;
5262
Takashi Iwaid4156932013-01-07 10:08:02 +01005263 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005264 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005265}
5266
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005267
Takashi Iwai2c12c302013-01-10 09:33:29 +01005268static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005269{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005270 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005271
Takashi Iwaid4156932013-01-07 10:08:02 +01005272 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005273 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005274}
5275
5276/* initialize hp and speaker paths */
5277static void init_extra_out(struct hda_codec *codec)
5278{
5279 struct hda_gen_spec *spec = codec->spec;
5280
5281 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005282 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005283 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5284 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005285 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005286}
5287
5288/* initialize multi-io paths */
5289static void init_multi_io(struct hda_codec *codec)
5290{
5291 struct hda_gen_spec *spec = codec->spec;
5292 int i;
5293
5294 for (i = 0; i < spec->multi_ios; i++) {
5295 hda_nid_t pin = spec->multi_io[i].pin;
5296 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005297 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005298 if (!path)
5299 continue;
5300 if (!spec->multi_io[i].ctl_in)
5301 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005302 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005303 snd_hda_activate_path(codec, path, path->active,
5304 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005305 }
5306}
5307
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005308static void init_aamix_paths(struct hda_codec *codec)
5309{
5310 struct hda_gen_spec *spec = codec->spec;
5311
5312 if (!spec->have_aamix_ctl)
5313 return;
5314 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5315 spec->aamix_out_paths[0],
5316 spec->autocfg.line_out_type);
5317 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5318 spec->aamix_out_paths[1],
5319 AUTO_PIN_HP_OUT);
5320 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5321 spec->aamix_out_paths[2],
5322 AUTO_PIN_SPEAKER_OUT);
5323}
5324
Takashi Iwai352f7f92012-12-19 12:52:06 +01005325/* set up input pins and loopback paths */
5326static void init_analog_input(struct hda_codec *codec)
5327{
5328 struct hda_gen_spec *spec = codec->spec;
5329 struct auto_pin_cfg *cfg = &spec->autocfg;
5330 int i;
5331
5332 for (i = 0; i < cfg->num_inputs; i++) {
5333 hda_nid_t nid = cfg->inputs[i].pin;
5334 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005335 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005336
5337 /* init loopback inputs */
5338 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005339 resume_path_from_idx(codec, spec->loopback_paths[i]);
5340 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005341 }
5342 }
5343}
5344
5345/* initialize ADC paths */
5346static void init_input_src(struct hda_codec *codec)
5347{
5348 struct hda_gen_spec *spec = codec->spec;
5349 struct hda_input_mux *imux = &spec->input_mux;
5350 struct nid_path *path;
5351 int i, c, nums;
5352
5353 if (spec->dyn_adc_switch)
5354 nums = 1;
5355 else
5356 nums = spec->num_adc_nids;
5357
5358 for (c = 0; c < nums; c++) {
5359 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005360 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005361 if (path) {
5362 bool active = path->active;
5363 if (i == spec->cur_mux[c])
5364 active = true;
5365 snd_hda_activate_path(codec, path, active, false);
5366 }
5367 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005368 if (spec->hp_mic)
5369 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005370 }
5371
Takashi Iwai352f7f92012-12-19 12:52:06 +01005372 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005373 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005374}
5375
5376/* set right pin controls for digital I/O */
5377static void init_digital(struct hda_codec *codec)
5378{
5379 struct hda_gen_spec *spec = codec->spec;
5380 int i;
5381 hda_nid_t pin;
5382
Takashi Iwaid4156932013-01-07 10:08:02 +01005383 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005384 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005385 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005386 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005387 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005388 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005389 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005390}
5391
Takashi Iwai973e4972012-12-20 15:16:09 +01005392/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5393 * invalid unsol tags by some reason
5394 */
5395static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5396{
5397 int i;
5398
5399 for (i = 0; i < codec->init_pins.used; i++) {
5400 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5401 hda_nid_t nid = pin->nid;
5402 if (is_jack_detectable(codec, nid) &&
5403 !snd_hda_jack_tbl_get(codec, nid))
5404 snd_hda_codec_update_cache(codec, nid, 0,
5405 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5406 }
5407}
5408
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005409/**
5410 * snd_hda_gen_init - initialize the generic spec
5411 * @codec: the HDA codec
5412 *
5413 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005414 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005415int snd_hda_gen_init(struct hda_codec *codec)
5416{
5417 struct hda_gen_spec *spec = codec->spec;
5418
5419 if (spec->init_hook)
5420 spec->init_hook(codec);
5421
5422 snd_hda_apply_verbs(codec);
5423
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005424 codec->cached_write = 1;
5425
Takashi Iwai352f7f92012-12-19 12:52:06 +01005426 init_multi_out(codec);
5427 init_extra_out(codec);
5428 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005429 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005430 init_analog_input(codec);
5431 init_input_src(codec);
5432 init_digital(codec);
5433
Takashi Iwai973e4972012-12-20 15:16:09 +01005434 clear_unsol_on_unused_pins(codec);
5435
Takashi Iwai352f7f92012-12-19 12:52:06 +01005436 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005437 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005438
Takashi Iwaidc870f32013-01-22 15:24:30 +01005439 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005440
Takashi Iwai352f7f92012-12-19 12:52:06 +01005441 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5442 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5443
5444 hda_call_check_power_status(codec, 0x01);
5445 return 0;
5446}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005447EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005448
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005449/**
5450 * snd_hda_gen_free - free the generic spec
5451 * @codec: the HDA codec
5452 *
5453 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005454 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005455void snd_hda_gen_free(struct hda_codec *codec)
5456{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005457 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005458 snd_hda_gen_spec_free(codec->spec);
5459 kfree(codec->spec);
5460 codec->spec = NULL;
5461}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005462EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005463
5464#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005465/**
5466 * snd_hda_gen_check_power_status - check the loopback power save state
5467 * @codec: the HDA codec
5468 * @nid: NID to inspect
5469 *
5470 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005471 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005472int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5473{
5474 struct hda_gen_spec *spec = codec->spec;
5475 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5476}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005477EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005478#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005479
5480
5481/*
5482 * the generic codec support
5483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484
Takashi Iwai352f7f92012-12-19 12:52:06 +01005485static const struct hda_codec_ops generic_patch_ops = {
5486 .build_controls = snd_hda_gen_build_controls,
5487 .build_pcms = snd_hda_gen_build_pcms,
5488 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005489 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005490 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005491#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005492 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005493#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494};
5495
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005496/**
5497 * snd_hda_parse_generic_codec - Generic codec parser
5498 * @codec: the HDA codec
5499 *
5500 * This should be called from the HDA codec core.
5501 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502int snd_hda_parse_generic_codec(struct hda_codec *codec)
5503{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005504 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 int err;
5506
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005507 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005508 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005510 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005513 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5514 if (err < 0)
5515 return err;
5516
5517 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005518 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 goto error;
5520
5521 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 return 0;
5523
Takashi Iwai352f7f92012-12-19 12:52:06 +01005524error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005525 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 return err;
5527}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005528EXPORT_SYMBOL_GPL(snd_hda_parse_generic_codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005529
5530MODULE_LICENSE("GPL");
5531MODULE_DESCRIPTION("Generic HD-audio codec parser");