blob: fe18071bf93aab4d44e779f9b7daaca3a2815b5b [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 Iwaief403ed2015-03-12 08:30:11 +0100695
696 if (get_wcaps(codec, nid) & AC_WCAP_STEREO)
697 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
698 else
699 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
700}
701
702/* update the amp, doing in stereo or mono depending on NID */
703static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
704 unsigned int mask, unsigned int val)
705{
706 if (get_wcaps(codec, nid) & AC_WCAP_STEREO)
707 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
708 mask, val);
709 else
710 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
711 mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100712}
713
Takashi Iwai8999bf02013-01-18 11:01:33 +0100714/* calculate amp value mask we can modify;
715 * if the given amp is controlled by mixers, don't touch it
716 */
717static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
718 hda_nid_t nid, int dir, int idx,
719 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100720{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100721 unsigned int mask = 0xff;
722
Takashi Iwaif69910d2013-08-08 09:32:37 +0200723 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100724 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
725 mask &= ~0x80;
726 }
727 if (caps & AC_AMPCAP_NUM_STEPS) {
728 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
729 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
730 mask &= ~0x7f;
731 }
732 return mask;
733}
734
735static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
736 int idx, int idx_to_check, bool enable)
737{
738 unsigned int caps;
739 unsigned int mask, val;
740
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100741 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100742 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100743
744 caps = query_amp_caps(codec, nid, dir);
745 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
746 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
747 if (!mask)
748 return;
749
750 val &= mask;
Takashi Iwaief403ed2015-03-12 08:30:11 +0100751 update_amp(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100752}
753
754static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
755 int i, bool enable)
756{
757 hda_nid_t nid = path->path[i];
758 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100759 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100760}
761
762static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
763 int i, bool enable, bool add_aamix)
764{
765 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100766 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100767 int n, nums, idx;
768 int type;
769 hda_nid_t nid = path->path[i];
770
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100771 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100772 type = get_wcaps_type(get_wcaps(codec, nid));
773 if (type == AC_WID_PIN ||
774 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
775 nums = 1;
776 idx = 0;
777 } else
778 idx = path->idx[i];
779
780 for (n = 0; n < nums; n++)
781 init_amp(codec, nid, HDA_INPUT, n);
782
Takashi Iwai352f7f92012-12-19 12:52:06 +0100783 /* here is a little bit tricky in comparison with activate_amp_out();
784 * when aa-mixer is available, we need to enable the path as well
785 */
786 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100787 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100788 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100789 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791}
792
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100793/**
794 * snd_hda_activate_path - activate or deactivate the given path
795 * @codec: the HDA codec
796 * @path: the path to activate/deactivate
797 * @enable: flag to activate or not
798 * @add_aamix: enable the input from aamix NID
799 *
800 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100802void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
803 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100805 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100806 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Takashi Iwai352f7f92012-12-19 12:52:06 +0100808 if (!enable)
809 path->active = false;
810
811 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100812 hda_nid_t nid = path->path[i];
813 if (enable && spec->power_down_unused) {
814 /* make sure the widget is powered up */
815 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
816 snd_hda_codec_write(codec, nid, 0,
817 AC_VERB_SET_POWER_STATE,
818 AC_PWRST_D0);
819 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100820 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100821 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100822 AC_VERB_SET_CONNECT_SEL,
823 path->idx[i]);
824 if (has_amp_in(codec, path, i))
825 activate_amp_in(codec, path, i, enable, add_aamix);
826 if (has_amp_out(codec, path, i))
827 activate_amp_out(codec, path, i, enable);
828 }
829
830 if (enable)
831 path->active = true;
832}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100833EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100834
Takashi Iwai55196ff2013-01-24 17:32:56 +0100835/* if the given path is inactive, put widgets into D3 (only if suitable) */
836static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
837{
838 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200839 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100840 int i;
841
842 if (!spec->power_down_unused || path->active)
843 return;
844
845 for (i = 0; i < path->depth; i++) {
846 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200847 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
848 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100849 snd_hda_codec_write(codec, nid, 0,
850 AC_VERB_SET_POWER_STATE,
851 AC_PWRST_D3);
852 changed = true;
853 }
854 }
855
856 if (changed) {
857 msleep(10);
858 snd_hda_codec_read(codec, path->path[0], 0,
859 AC_VERB_GET_POWER_STATE, 0);
860 }
861}
862
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100863/* turn on/off EAPD on the given pin */
864static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
865{
866 struct hda_gen_spec *spec = codec->spec;
867 if (spec->own_eapd_ctl ||
868 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
869 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200870 if (spec->keep_eapd_on && !enable)
871 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100872 if (codec->inv_eapd)
873 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100874 snd_hda_codec_update_cache(codec, pin, 0,
875 AC_VERB_SET_EAPD_BTLENABLE,
876 enable ? 0x02 : 0x00);
877}
878
Takashi Iwai3e367f12013-01-23 17:07:23 +0100879/* re-initialize the path specified by the given path index */
880static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
881{
882 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
883 if (path)
884 snd_hda_activate_path(codec, path, path->active, false);
885}
886
Takashi Iwai352f7f92012-12-19 12:52:06 +0100887
888/*
889 * Helper functions for creating mixer ctl elements
890 */
891
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200892static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
893 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200894static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
895 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200896
Takashi Iwai352f7f92012-12-19 12:52:06 +0100897enum {
898 HDA_CTL_WIDGET_VOL,
899 HDA_CTL_WIDGET_MUTE,
900 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100901};
902static const struct snd_kcontrol_new control_templates[] = {
903 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200904 /* only the put callback is replaced for handling the special mute */
905 {
906 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
907 .subdevice = HDA_SUBDEV_AMP_FLAG,
908 .info = snd_hda_mixer_amp_switch_info,
909 .get = snd_hda_mixer_amp_switch_get,
910 .put = hda_gen_mixer_mute_put, /* replaced */
911 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
912 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200913 {
914 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
915 .info = snd_hda_mixer_amp_switch_info,
916 .get = snd_hda_mixer_bind_switch_get,
917 .put = hda_gen_bind_mute_put, /* replaced */
918 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
919 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100920};
921
922/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100923static struct snd_kcontrol_new *
924add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100925 int cidx, unsigned long val)
926{
927 struct snd_kcontrol_new *knew;
928
Takashi Iwai12c93df2012-12-19 14:38:33 +0100929 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100930 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100931 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100932 knew->index = cidx;
933 if (get_amp_nid_(val))
934 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
935 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100936 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100937}
938
939static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
940 const char *pfx, const char *dir,
941 const char *sfx, int cidx, unsigned long val)
942{
Takashi Iwai975cc022013-06-28 11:56:49 +0200943 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100944 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100945 if (!add_control(spec, type, name, cidx, val))
946 return -ENOMEM;
947 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100948}
949
950#define add_pb_vol_ctrl(spec, type, pfx, val) \
951 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
952#define add_pb_sw_ctrl(spec, type, pfx, val) \
953 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
954#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
955 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
956#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
957 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
958
959static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
960 unsigned int chs, struct nid_path *path)
961{
962 unsigned int val;
963 if (!path)
964 return 0;
965 val = path->ctls[NID_PATH_VOL_CTL];
966 if (!val)
967 return 0;
968 val = amp_val_replace_channels(val, chs);
969 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
970}
971
972/* return the channel bits suitable for the given path->ctls[] */
973static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
974 int type)
975{
976 int chs = 1; /* mono (left only) */
977 if (path) {
978 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
979 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
980 chs = 3; /* stereo */
981 }
982 return chs;
983}
984
985static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
986 struct nid_path *path)
987{
988 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
989 return add_vol_ctl(codec, pfx, cidx, chs, path);
990}
991
992/* create a mute-switch for the given mixer widget;
993 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
994 */
995static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
996 unsigned int chs, struct nid_path *path)
997{
998 unsigned int val;
999 int type = HDA_CTL_WIDGET_MUTE;
1000
1001 if (!path)
1002 return 0;
1003 val = path->ctls[NID_PATH_MUTE_CTL];
1004 if (!val)
1005 return 0;
1006 val = amp_val_replace_channels(val, chs);
1007 if (get_amp_direction_(val) == HDA_INPUT) {
1008 hda_nid_t nid = get_amp_nid_(val);
1009 int nums = snd_hda_get_num_conns(codec, nid);
1010 if (nums > 1) {
1011 type = HDA_CTL_BIND_MUTE;
1012 val |= nums << 19;
1013 }
1014 }
1015 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1016}
1017
1018static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1019 int cidx, struct nid_path *path)
1020{
1021 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1022 return add_sw_ctl(codec, pfx, cidx, chs, path);
1023}
1024
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001025/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001026static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1027 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001028{
1029 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1030 struct hda_gen_spec *spec = codec->spec;
1031
1032 if (spec->auto_mute_via_amp) {
1033 hda_nid_t nid = get_amp_nid(kcontrol);
1034 bool enabled = !((spec->mute_bits >> nid) & 1);
1035 ucontrol->value.integer.value[0] &= enabled;
1036 ucontrol->value.integer.value[1] &= enabled;
1037 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001038}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001039
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001040static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1041 struct snd_ctl_elem_value *ucontrol)
1042{
1043 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001044 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1045}
1046
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001047static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1048 struct snd_ctl_elem_value *ucontrol)
1049{
1050 sync_auto_mute_bits(kcontrol, ucontrol);
1051 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1052}
1053
Takashi Iwai247d85e2013-01-17 16:18:11 +01001054/* any ctl assigned to the path with the given index? */
1055static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1056{
1057 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1058 return path && path->ctls[ctl_type];
1059}
1060
Takashi Iwai352f7f92012-12-19 12:52:06 +01001061static const char * const channel_name[4] = {
1062 "Front", "Surround", "CLFE", "Side"
1063};
1064
1065/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001066static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1067 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001068{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001069 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001070 struct auto_pin_cfg *cfg = &spec->autocfg;
1071
1072 *index = 0;
1073 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001074 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001075 return spec->vmaster_mute.hook ? "PCM" : "Master";
1076
1077 /* if there is really a single DAC used in the whole output paths,
1078 * use it master (or "PCM" if a vmaster hook is present)
1079 */
1080 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1081 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1082 return spec->vmaster_mute.hook ? "PCM" : "Master";
1083
Takashi Iwai247d85e2013-01-17 16:18:11 +01001084 /* multi-io channels */
1085 if (ch >= cfg->line_outs)
1086 return channel_name[ch];
1087
Takashi Iwai352f7f92012-12-19 12:52:06 +01001088 switch (cfg->line_out_type) {
1089 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001090 /* if the primary channel vol/mute is shared with HP volume,
1091 * don't name it as Speaker
1092 */
1093 if (!ch && cfg->hp_outs &&
1094 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1095 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001096 if (cfg->line_outs == 1)
1097 return "Speaker";
1098 if (cfg->line_outs == 2)
1099 return ch ? "Bass Speaker" : "Speaker";
1100 break;
1101 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001102 /* if the primary channel vol/mute is shared with spk volume,
1103 * don't name it as Headphone
1104 */
1105 if (!ch && cfg->speaker_outs &&
1106 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1107 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001108 /* for multi-io case, only the primary out */
1109 if (ch && spec->multi_ios)
1110 break;
1111 *index = ch;
1112 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001113 case AUTO_PIN_LINE_OUT:
1114 /* This deals with the case where we have two DACs and
1115 * one LO, one HP and one Speaker */
1116 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1117 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1118 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1119 if (hp_lo_shared && spk_lo_shared)
1120 return spec->vmaster_mute.hook ? "PCM" : "Master";
1121 if (hp_lo_shared)
1122 return "Headphone+LO";
1123 if (spk_lo_shared)
1124 return "Speaker+LO";
1125 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001126 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001127
1128 /* for a single channel output, we don't have to name the channel */
1129 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001130 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001131
Takashi Iwai352f7f92012-12-19 12:52:06 +01001132 if (ch >= ARRAY_SIZE(channel_name)) {
1133 snd_BUG();
1134 return "PCM";
1135 }
1136
1137 return channel_name[ch];
1138}
1139
1140/*
1141 * Parse output paths
1142 */
1143
1144/* badness definition */
1145enum {
1146 /* No primary DAC is found for the main output */
1147 BAD_NO_PRIMARY_DAC = 0x10000,
1148 /* No DAC is found for the extra output */
1149 BAD_NO_DAC = 0x4000,
1150 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001151 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001152 /* No individual DAC for extra output */
1153 BAD_NO_EXTRA_DAC = 0x102,
1154 /* No individual DAC for extra surrounds */
1155 BAD_NO_EXTRA_SURR_DAC = 0x101,
1156 /* Primary DAC shared with main surrounds */
1157 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001158 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001159 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001160 /* Primary DAC shared with main CLFE */
1161 BAD_SHARED_CLFE = 0x10,
1162 /* Primary DAC shared with extra surrounds */
1163 BAD_SHARED_EXTRA_SURROUND = 0x10,
1164 /* Volume widget is shared */
1165 BAD_SHARED_VOL = 0x10,
1166};
1167
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001168/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001169 * volume and mute controls, and assign the values to ctls[].
1170 *
1171 * When no appropriate widget is found in the path, the badness value
1172 * is incremented depending on the situation. The function returns the
1173 * total badness for both volume and mute controls.
1174 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001175static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001176{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001177 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001178 hda_nid_t nid;
1179 unsigned int val;
1180 int badness = 0;
1181
1182 if (!path)
1183 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001184
1185 if (path->ctls[NID_PATH_VOL_CTL] ||
1186 path->ctls[NID_PATH_MUTE_CTL])
1187 return 0; /* already evaluated */
1188
Takashi Iwai352f7f92012-12-19 12:52:06 +01001189 nid = look_for_out_vol_nid(codec, path);
1190 if (nid) {
1191 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001192 if (spec->dac_min_mute)
1193 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001194 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1195 badness += BAD_SHARED_VOL;
1196 else
1197 path->ctls[NID_PATH_VOL_CTL] = val;
1198 } else
1199 badness += BAD_SHARED_VOL;
1200 nid = look_for_out_mute_nid(codec, path);
1201 if (nid) {
1202 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1203 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1204 nid_has_mute(codec, nid, HDA_OUTPUT))
1205 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1206 else
1207 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1208 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1209 badness += BAD_SHARED_VOL;
1210 else
1211 path->ctls[NID_PATH_MUTE_CTL] = val;
1212 } else
1213 badness += BAD_SHARED_VOL;
1214 return badness;
1215}
1216
Takashi Iwai98bd1112013-03-22 14:53:50 +01001217const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001218 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1219 .no_dac = BAD_NO_DAC,
1220 .shared_primary = BAD_NO_PRIMARY_DAC,
1221 .shared_surr = BAD_SHARED_SURROUND,
1222 .shared_clfe = BAD_SHARED_CLFE,
1223 .shared_surr_main = BAD_SHARED_SURROUND,
1224};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001225EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001226
Takashi Iwai98bd1112013-03-22 14:53:50 +01001227const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001228 .no_primary_dac = BAD_NO_DAC,
1229 .no_dac = BAD_NO_DAC,
1230 .shared_primary = BAD_NO_EXTRA_DAC,
1231 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1232 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1233 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1234};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001235EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001236
Takashi Iwai7385df62013-01-07 09:50:52 +01001237/* get the DAC of the primary output corresponding to the given array index */
1238static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1239{
1240 struct hda_gen_spec *spec = codec->spec;
1241 struct auto_pin_cfg *cfg = &spec->autocfg;
1242
1243 if (cfg->line_outs > idx)
1244 return spec->private_dac_nids[idx];
1245 idx -= cfg->line_outs;
1246 if (spec->multi_ios > idx)
1247 return spec->multi_io[idx].dac;
1248 return 0;
1249}
1250
1251/* return the DAC if it's reachable, otherwise zero */
1252static inline hda_nid_t try_dac(struct hda_codec *codec,
1253 hda_nid_t dac, hda_nid_t pin)
1254{
1255 return is_reachable_path(codec, dac, pin) ? dac : 0;
1256}
1257
Takashi Iwai352f7f92012-12-19 12:52:06 +01001258/* try to assign DACs to pins and return the resultant badness */
1259static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1260 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001261 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001262 const struct badness_table *bad)
1263{
1264 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001265 int i, j;
1266 int badness = 0;
1267 hda_nid_t dac;
1268
1269 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return 0;
1271
Takashi Iwai352f7f92012-12-19 12:52:06 +01001272 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001273 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001274 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001275
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001276 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1277 if (path) {
1278 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001279 continue;
1280 }
1281
Takashi Iwai36907392013-12-10 17:29:26 +01001282 dacs[i] = get_preferred_dac(codec, pin);
1283 if (dacs[i]) {
1284 if (is_dac_already_used(codec, dacs[i]))
1285 badness += bad->shared_primary;
1286 }
1287
1288 if (!dacs[i])
1289 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001290 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001291 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001292 for (j = 1; j < num_outs; j++) {
1293 if (is_reachable_path(codec, dacs[j], pin)) {
1294 dacs[0] = dacs[j];
1295 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001296 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001297 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001298 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001301 }
1302 dac = dacs[i];
1303 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001304 if (num_outs > 2)
1305 dac = try_dac(codec, get_primary_out(codec, i), pin);
1306 if (!dac)
1307 dac = try_dac(codec, dacs[0], pin);
1308 if (!dac)
1309 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001310 if (dac) {
1311 if (!i)
1312 badness += bad->shared_primary;
1313 else if (i == 1)
1314 badness += bad->shared_surr;
1315 else
1316 badness += bad->shared_clfe;
1317 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1318 dac = spec->private_dac_nids[0];
1319 badness += bad->shared_surr_main;
1320 } else if (!i)
1321 badness += bad->no_primary_dac;
1322 else
1323 badness += bad->no_dac;
1324 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001325 if (!dac)
1326 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001327 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001328 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001329 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001330 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001331 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001332 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001333 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001334 badness += bad->no_dac;
1335 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001336 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001337 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001338 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001339 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001340 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001341 }
1342
1343 return badness;
1344}
1345
1346/* return NID if the given pin has only a single connection to a certain DAC */
1347static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1348{
1349 struct hda_gen_spec *spec = codec->spec;
1350 int i;
1351 hda_nid_t nid_found = 0;
1352
1353 for (i = 0; i < spec->num_all_dacs; i++) {
1354 hda_nid_t nid = spec->all_dacs[i];
1355 if (!nid || is_dac_already_used(codec, nid))
1356 continue;
1357 if (is_reachable_path(codec, nid, pin)) {
1358 if (nid_found)
1359 return 0;
1360 nid_found = nid;
1361 }
1362 }
1363 return nid_found;
1364}
1365
1366/* check whether the given pin can be a multi-io pin */
1367static bool can_be_multiio_pin(struct hda_codec *codec,
1368 unsigned int location, hda_nid_t nid)
1369{
1370 unsigned int defcfg, caps;
1371
1372 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1373 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1374 return false;
1375 if (location && get_defcfg_location(defcfg) != location)
1376 return false;
1377 caps = snd_hda_query_pin_caps(codec, nid);
1378 if (!(caps & AC_PINCAP_OUT))
1379 return false;
1380 return true;
1381}
1382
Takashi Iwaie22aab72013-01-04 14:50:04 +01001383/* count the number of input pins that are capable to be multi-io */
1384static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1385{
1386 struct hda_gen_spec *spec = codec->spec;
1387 struct auto_pin_cfg *cfg = &spec->autocfg;
1388 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1389 unsigned int location = get_defcfg_location(defcfg);
1390 int type, i;
1391 int num_pins = 0;
1392
1393 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1394 for (i = 0; i < cfg->num_inputs; i++) {
1395 if (cfg->inputs[i].type != type)
1396 continue;
1397 if (can_be_multiio_pin(codec, location,
1398 cfg->inputs[i].pin))
1399 num_pins++;
1400 }
1401 }
1402 return num_pins;
1403}
1404
Takashi Iwai352f7f92012-12-19 12:52:06 +01001405/*
1406 * multi-io helper
1407 *
1408 * When hardwired is set, try to fill ony hardwired pins, and returns
1409 * zero if any pins are filled, non-zero if nothing found.
1410 * When hardwired is off, try to fill possible input pins, and returns
1411 * the badness value.
1412 */
1413static int fill_multi_ios(struct hda_codec *codec,
1414 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001415 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001416{
1417 struct hda_gen_spec *spec = codec->spec;
1418 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001419 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001420 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1421 unsigned int location = get_defcfg_location(defcfg);
1422 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001423 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001424
1425 old_pins = spec->multi_ios;
1426 if (old_pins >= 2)
1427 goto end_fill;
1428
Takashi Iwaie22aab72013-01-04 14:50:04 +01001429 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001430 if (num_pins < 2)
1431 goto end_fill;
1432
Takashi Iwai352f7f92012-12-19 12:52:06 +01001433 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1434 for (i = 0; i < cfg->num_inputs; i++) {
1435 hda_nid_t nid = cfg->inputs[i].pin;
1436 hda_nid_t dac = 0;
1437
1438 if (cfg->inputs[i].type != type)
1439 continue;
1440 if (!can_be_multiio_pin(codec, location, nid))
1441 continue;
1442 for (j = 0; j < spec->multi_ios; j++) {
1443 if (nid == spec->multi_io[j].pin)
1444 break;
1445 }
1446 if (j < spec->multi_ios)
1447 continue;
1448
Takashi Iwai352f7f92012-12-19 12:52:06 +01001449 if (hardwired)
1450 dac = get_dac_if_single(codec, nid);
1451 else if (!dac)
1452 dac = look_for_dac(codec, nid, false);
1453 if (!dac) {
1454 badness++;
1455 continue;
1456 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001457 path = snd_hda_add_new_path(codec, dac, nid,
1458 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001459 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001460 badness++;
1461 continue;
1462 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001463 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001464 spec->multi_io[spec->multi_ios].pin = nid;
1465 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001466 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1467 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001468 spec->multi_ios++;
1469 if (spec->multi_ios >= 2)
1470 break;
1471 }
1472 }
1473 end_fill:
1474 if (badness)
1475 badness = BAD_MULTI_IO;
1476 if (old_pins == spec->multi_ios) {
1477 if (hardwired)
1478 return 1; /* nothing found */
1479 else
1480 return badness; /* no badness if nothing found */
1481 }
1482 if (!hardwired && spec->multi_ios < 2) {
1483 /* cancel newly assigned paths */
1484 spec->paths.used -= spec->multi_ios - old_pins;
1485 spec->multi_ios = old_pins;
1486 return badness;
1487 }
1488
1489 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001490 for (i = old_pins; i < spec->multi_ios; i++) {
1491 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1492 badness += assign_out_path_ctls(codec, path);
1493 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001494
1495 return badness;
1496}
1497
1498/* map DACs for all pins in the list if they are single connections */
1499static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001500 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001501{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001502 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001503 int i;
1504 bool found = false;
1505 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001506 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001507 hda_nid_t dac;
1508 if (dacs[i])
1509 continue;
1510 dac = get_dac_if_single(codec, pins[i]);
1511 if (!dac)
1512 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001513 path = snd_hda_add_new_path(codec, dac, pins[i],
1514 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001515 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001516 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001517 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001518 dacs[i] = dac;
1519 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001520 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001521 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001522 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001523 }
1524 }
1525 return found;
1526}
1527
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001528/* create a new path including aamix if available, and return its index */
1529static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1530{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001531 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001532 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001533 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001534
1535 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001536 if (!path || !path->depth ||
1537 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001538 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001539 path_dac = path->path[0];
1540 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001541 pin = path->path[path->depth - 1];
1542 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1543 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001544 if (dac != path_dac)
1545 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001546 else if (spec->multiout.hp_out_nid[0])
1547 dac = spec->multiout.hp_out_nid[0];
1548 else if (spec->multiout.extra_out_nid[0])
1549 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001550 else
1551 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001552 if (dac)
1553 path = snd_hda_add_new_path(codec, dac, pin,
1554 spec->mixer_nid);
1555 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001556 if (!path)
1557 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001558 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001559 path->active = false; /* unused as default */
1560 return snd_hda_get_path_idx(codec, path);
1561}
1562
Takashi Iwai55a63d42013-03-21 17:20:12 +01001563/* check whether the independent HP is available with the current config */
1564static bool indep_hp_possible(struct hda_codec *codec)
1565{
1566 struct hda_gen_spec *spec = codec->spec;
1567 struct auto_pin_cfg *cfg = &spec->autocfg;
1568 struct nid_path *path;
1569 int i, idx;
1570
1571 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1572 idx = spec->out_paths[0];
1573 else
1574 idx = spec->hp_paths[0];
1575 path = snd_hda_get_path_from_idx(codec, idx);
1576 if (!path)
1577 return false;
1578
1579 /* assume no path conflicts unless aamix is involved */
1580 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1581 return true;
1582
1583 /* check whether output paths contain aamix */
1584 for (i = 0; i < cfg->line_outs; i++) {
1585 if (spec->out_paths[i] == idx)
1586 break;
1587 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1588 if (path && is_nid_contained(path, spec->mixer_nid))
1589 return false;
1590 }
1591 for (i = 0; i < cfg->speaker_outs; i++) {
1592 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1593 if (path && is_nid_contained(path, spec->mixer_nid))
1594 return false;
1595 }
1596
1597 return true;
1598}
1599
Takashi Iwaia07a9492013-01-07 16:44:06 +01001600/* fill the empty entries in the dac array for speaker/hp with the
1601 * shared dac pointed by the paths
1602 */
1603static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1604 hda_nid_t *dacs, int *path_idx)
1605{
1606 struct nid_path *path;
1607 int i;
1608
1609 for (i = 0; i < num_outs; i++) {
1610 if (dacs[i])
1611 continue;
1612 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1613 if (!path)
1614 continue;
1615 dacs[i] = path->path[0];
1616 }
1617}
1618
Takashi Iwai352f7f92012-12-19 12:52:06 +01001619/* fill in the dac_nids table from the parsed pin configuration */
1620static int fill_and_eval_dacs(struct hda_codec *codec,
1621 bool fill_hardwired,
1622 bool fill_mio_first)
1623{
1624 struct hda_gen_spec *spec = codec->spec;
1625 struct auto_pin_cfg *cfg = &spec->autocfg;
1626 int i, err, badness;
1627
1628 /* set num_dacs once to full for look_for_dac() */
1629 spec->multiout.num_dacs = cfg->line_outs;
1630 spec->multiout.dac_nids = spec->private_dac_nids;
1631 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1632 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1633 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1634 spec->multi_ios = 0;
1635 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001636
1637 /* clear path indices */
1638 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1639 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1640 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1641 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1642 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001643 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001644 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1645 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1646
Takashi Iwai352f7f92012-12-19 12:52:06 +01001647 badness = 0;
1648
1649 /* fill hard-wired DACs first */
1650 if (fill_hardwired) {
1651 bool mapped;
1652 do {
1653 mapped = map_singles(codec, cfg->line_outs,
1654 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001655 spec->private_dac_nids,
1656 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001657 mapped |= map_singles(codec, cfg->hp_outs,
1658 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001659 spec->multiout.hp_out_nid,
1660 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001661 mapped |= map_singles(codec, cfg->speaker_outs,
1662 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001663 spec->multiout.extra_out_nid,
1664 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001665 if (!spec->no_multi_io &&
1666 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001667 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001668 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001669 if (!err)
1670 mapped = true;
1671 }
1672 } while (mapped);
1673 }
1674
1675 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001676 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001677 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001678
Takashi Iwaida96fb52013-07-29 16:54:36 +02001679 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001680 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1681 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001682 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001683 if (err < 0)
1684 return err;
1685 /* we don't count badness at this stage yet */
1686 }
1687
1688 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1689 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1690 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001691 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001692 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001693 if (err < 0)
1694 return err;
1695 badness += err;
1696 }
1697 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1698 err = try_assign_dacs(codec, cfg->speaker_outs,
1699 cfg->speaker_pins,
1700 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001701 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001702 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001703 if (err < 0)
1704 return err;
1705 badness += err;
1706 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001707 if (!spec->no_multi_io &&
1708 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001709 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001710 if (err < 0)
1711 return err;
1712 badness += err;
1713 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001714
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001715 if (spec->mixer_nid) {
1716 spec->aamix_out_paths[0] =
1717 check_aamix_out_path(codec, spec->out_paths[0]);
1718 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1719 spec->aamix_out_paths[1] =
1720 check_aamix_out_path(codec, spec->hp_paths[0]);
1721 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1722 spec->aamix_out_paths[2] =
1723 check_aamix_out_path(codec, spec->speaker_paths[0]);
1724 }
1725
Takashi Iwaida96fb52013-07-29 16:54:36 +02001726 if (!spec->no_multi_io &&
1727 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001728 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1729 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001730
Takashi Iwaia07a9492013-01-07 16:44:06 +01001731 /* re-count num_dacs and squash invalid entries */
1732 spec->multiout.num_dacs = 0;
1733 for (i = 0; i < cfg->line_outs; i++) {
1734 if (spec->private_dac_nids[i])
1735 spec->multiout.num_dacs++;
1736 else {
1737 memmove(spec->private_dac_nids + i,
1738 spec->private_dac_nids + i + 1,
1739 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1740 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1741 }
1742 }
1743
1744 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001745 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001746
Takashi Iwai352f7f92012-12-19 12:52:06 +01001747 if (spec->multi_ios == 2) {
1748 for (i = 0; i < 2; i++)
1749 spec->private_dac_nids[spec->multiout.num_dacs++] =
1750 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001751 } else if (spec->multi_ios) {
1752 spec->multi_ios = 0;
1753 badness += BAD_MULTI_IO;
1754 }
1755
Takashi Iwai55a63d42013-03-21 17:20:12 +01001756 if (spec->indep_hp && !indep_hp_possible(codec))
1757 badness += BAD_NO_INDEP_HP;
1758
Takashi Iwaia07a9492013-01-07 16:44:06 +01001759 /* re-fill the shared DAC for speaker / headphone */
1760 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1761 refill_shared_dacs(codec, cfg->hp_outs,
1762 spec->multiout.hp_out_nid,
1763 spec->hp_paths);
1764 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1765 refill_shared_dacs(codec, cfg->speaker_outs,
1766 spec->multiout.extra_out_nid,
1767 spec->speaker_paths);
1768
Takashi Iwai352f7f92012-12-19 12:52:06 +01001769 return badness;
1770}
1771
1772#define DEBUG_BADNESS
1773
1774#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001775#define debug_badness(fmt, ...) \
1776 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001777#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001778#define debug_badness(fmt, ...) \
1779 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001780#endif
1781
Takashi Iwaia7694092013-01-21 10:43:18 +01001782#ifdef DEBUG_BADNESS
1783static inline void print_nid_path_idx(struct hda_codec *codec,
1784 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001785{
Takashi Iwaia7694092013-01-21 10:43:18 +01001786 struct nid_path *path;
1787
1788 path = snd_hda_get_path_from_idx(codec, idx);
1789 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001790 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001791}
1792
1793static void debug_show_configs(struct hda_codec *codec,
1794 struct auto_pin_cfg *cfg)
1795{
1796 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001797 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001798 int i;
1799
1800 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001801 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001802 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001803 spec->multiout.dac_nids[0],
1804 spec->multiout.dac_nids[1],
1805 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001806 spec->multiout.dac_nids[3],
1807 lo_type[cfg->line_out_type]);
1808 for (i = 0; i < cfg->line_outs; i++)
1809 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001810 if (spec->multi_ios > 0)
1811 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1812 spec->multi_ios,
1813 spec->multi_io[0].pin, spec->multi_io[1].pin,
1814 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001815 for (i = 0; i < spec->multi_ios; i++)
1816 print_nid_path_idx(codec, " mio",
1817 spec->out_paths[cfg->line_outs + i]);
1818 if (cfg->hp_outs)
1819 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001820 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001821 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001822 spec->multiout.hp_out_nid[0],
1823 spec->multiout.hp_out_nid[1],
1824 spec->multiout.hp_out_nid[2],
1825 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001826 for (i = 0; i < cfg->hp_outs; i++)
1827 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1828 if (cfg->speaker_outs)
1829 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001830 cfg->speaker_pins[0], cfg->speaker_pins[1],
1831 cfg->speaker_pins[2], cfg->speaker_pins[3],
1832 spec->multiout.extra_out_nid[0],
1833 spec->multiout.extra_out_nid[1],
1834 spec->multiout.extra_out_nid[2],
1835 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001836 for (i = 0; i < cfg->speaker_outs; i++)
1837 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1838 for (i = 0; i < 3; i++)
1839 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001840}
Takashi Iwaia7694092013-01-21 10:43:18 +01001841#else
1842#define debug_show_configs(codec, cfg) /* NOP */
1843#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001844
1845/* find all available DACs of the codec */
1846static void fill_all_dac_nids(struct hda_codec *codec)
1847{
1848 struct hda_gen_spec *spec = codec->spec;
1849 int i;
1850 hda_nid_t nid = codec->start_nid;
1851
1852 spec->num_all_dacs = 0;
1853 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1854 for (i = 0; i < codec->num_nodes; i++, nid++) {
1855 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1856 continue;
1857 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001858 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001859 break;
1860 }
1861 spec->all_dacs[spec->num_all_dacs++] = nid;
1862 }
1863}
1864
1865static int parse_output_paths(struct hda_codec *codec)
1866{
1867 struct hda_gen_spec *spec = codec->spec;
1868 struct auto_pin_cfg *cfg = &spec->autocfg;
1869 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001870 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001871 int best_badness = INT_MAX;
1872 int badness;
1873 bool fill_hardwired = true, fill_mio_first = true;
1874 bool best_wired = true, best_mio = true;
1875 bool hp_spk_swapped = false;
1876
Takashi Iwai352f7f92012-12-19 12:52:06 +01001877 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1878 if (!best_cfg)
1879 return -ENOMEM;
1880 *best_cfg = *cfg;
1881
1882 for (;;) {
1883 badness = fill_and_eval_dacs(codec, fill_hardwired,
1884 fill_mio_first);
1885 if (badness < 0) {
1886 kfree(best_cfg);
1887 return badness;
1888 }
1889 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1890 cfg->line_out_type, fill_hardwired, fill_mio_first,
1891 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001892 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001893 if (badness < best_badness) {
1894 best_badness = badness;
1895 *best_cfg = *cfg;
1896 best_wired = fill_hardwired;
1897 best_mio = fill_mio_first;
1898 }
1899 if (!badness)
1900 break;
1901 fill_mio_first = !fill_mio_first;
1902 if (!fill_mio_first)
1903 continue;
1904 fill_hardwired = !fill_hardwired;
1905 if (!fill_hardwired)
1906 continue;
1907 if (hp_spk_swapped)
1908 break;
1909 hp_spk_swapped = true;
1910 if (cfg->speaker_outs > 0 &&
1911 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1912 cfg->hp_outs = cfg->line_outs;
1913 memcpy(cfg->hp_pins, cfg->line_out_pins,
1914 sizeof(cfg->hp_pins));
1915 cfg->line_outs = cfg->speaker_outs;
1916 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1917 sizeof(cfg->speaker_pins));
1918 cfg->speaker_outs = 0;
1919 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1920 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1921 fill_hardwired = true;
1922 continue;
1923 }
1924 if (cfg->hp_outs > 0 &&
1925 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1926 cfg->speaker_outs = cfg->line_outs;
1927 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1928 sizeof(cfg->speaker_pins));
1929 cfg->line_outs = cfg->hp_outs;
1930 memcpy(cfg->line_out_pins, cfg->hp_pins,
1931 sizeof(cfg->hp_pins));
1932 cfg->hp_outs = 0;
1933 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1934 cfg->line_out_type = AUTO_PIN_HP_OUT;
1935 fill_hardwired = true;
1936 continue;
1937 }
1938 break;
1939 }
1940
1941 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001942 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001943 *cfg = *best_cfg;
1944 fill_and_eval_dacs(codec, best_wired, best_mio);
1945 }
1946 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1947 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001948 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001949
1950 if (cfg->line_out_pins[0]) {
1951 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001952 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001953 if (path)
1954 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001955 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001956 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1957 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001958 if (spec->dac_min_mute)
1959 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
1960 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001961 }
1962
Takashi Iwai9314a582013-01-21 10:49:05 +01001963 /* set initial pinctl targets */
1964 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1965 val = PIN_HP;
1966 else
1967 val = PIN_OUT;
1968 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1969 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1970 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1971 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1972 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1973 set_pin_targets(codec, cfg->speaker_outs,
1974 cfg->speaker_pins, val);
1975 }
1976
Takashi Iwai55a63d42013-03-21 17:20:12 +01001977 /* clear indep_hp flag if not available */
1978 if (spec->indep_hp && !indep_hp_possible(codec))
1979 spec->indep_hp = 0;
1980
Takashi Iwai352f7f92012-12-19 12:52:06 +01001981 kfree(best_cfg);
1982 return 0;
1983}
1984
1985/* add playback controls from the parsed DAC table */
1986static int create_multi_out_ctls(struct hda_codec *codec,
1987 const struct auto_pin_cfg *cfg)
1988{
1989 struct hda_gen_spec *spec = codec->spec;
1990 int i, err, noutputs;
1991
1992 noutputs = cfg->line_outs;
1993 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1994 noutputs += spec->multi_ios;
1995
1996 for (i = 0; i < noutputs; i++) {
1997 const char *name;
1998 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001999 struct nid_path *path;
2000
Takashi Iwai196c17662013-01-04 15:01:40 +01002001 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002002 if (!path)
2003 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002004
2005 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002006 if (!name || !strcmp(name, "CLFE")) {
2007 /* Center/LFE */
2008 err = add_vol_ctl(codec, "Center", 0, 1, path);
2009 if (err < 0)
2010 return err;
2011 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2012 if (err < 0)
2013 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002014 } else {
2015 err = add_stereo_vol(codec, name, index, path);
2016 if (err < 0)
2017 return err;
2018 }
2019
2020 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2021 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002022 err = add_sw_ctl(codec, "Center", 0, 1, path);
2023 if (err < 0)
2024 return err;
2025 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2026 if (err < 0)
2027 return err;
2028 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002029 err = add_stereo_sw(codec, name, index, path);
2030 if (err < 0)
2031 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033 }
2034 return 0;
2035}
2036
Takashi Iwaic2c80382013-01-07 10:33:57 +01002037static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002038 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002040 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 int err;
2042
Takashi Iwai196c17662013-01-04 15:01:40 +01002043 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002044 if (!path)
2045 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002046 err = add_stereo_vol(codec, pfx, cidx, path);
2047 if (err < 0)
2048 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002049 err = add_stereo_sw(codec, pfx, cidx, path);
2050 if (err < 0)
2051 return err;
2052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
Takashi Iwai352f7f92012-12-19 12:52:06 +01002055/* add playback controls for speaker and HP outputs */
2056static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002057 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002059 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002060
2061 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002062 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002063 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002064 int err, idx = 0;
2065
2066 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2067 name = "Bass Speaker";
2068 else if (num_pins >= 3) {
2069 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002070 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002071 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002072 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002073 name = pfx;
2074 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002076 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002077 if (err < 0)
2078 return err;
2079 }
2080 return 0;
2081}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002082
Takashi Iwai352f7f92012-12-19 12:52:06 +01002083static int create_hp_out_ctls(struct hda_codec *codec)
2084{
2085 struct hda_gen_spec *spec = codec->spec;
2086 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002087 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002088 "Headphone");
2089}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Takashi Iwai352f7f92012-12-19 12:52:06 +01002091static int create_speaker_out_ctls(struct hda_codec *codec)
2092{
2093 struct hda_gen_spec *spec = codec->spec;
2094 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002095 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002096 "Speaker");
2097}
2098
2099/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002100 * independent HP controls
2101 */
2102
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002103static void call_hp_automute(struct hda_codec *codec,
2104 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002105static int indep_hp_info(struct snd_kcontrol *kcontrol,
2106 struct snd_ctl_elem_info *uinfo)
2107{
2108 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2109}
2110
2111static int indep_hp_get(struct snd_kcontrol *kcontrol,
2112 struct snd_ctl_elem_value *ucontrol)
2113{
2114 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2115 struct hda_gen_spec *spec = codec->spec;
2116 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2117 return 0;
2118}
2119
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002120static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2121 int nomix_path_idx, int mix_path_idx,
2122 int out_type);
2123
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002124static int indep_hp_put(struct snd_kcontrol *kcontrol,
2125 struct snd_ctl_elem_value *ucontrol)
2126{
2127 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2128 struct hda_gen_spec *spec = codec->spec;
2129 unsigned int select = ucontrol->value.enumerated.item[0];
2130 int ret = 0;
2131
2132 mutex_lock(&spec->pcm_mutex);
2133 if (spec->active_streams) {
2134 ret = -EBUSY;
2135 goto unlock;
2136 }
2137
2138 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002139 hda_nid_t *dacp;
2140 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2141 dacp = &spec->private_dac_nids[0];
2142 else
2143 dacp = &spec->multiout.hp_out_nid[0];
2144
2145 /* update HP aamix paths in case it conflicts with indep HP */
2146 if (spec->have_aamix_ctl) {
2147 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2148 update_aamix_paths(codec, spec->aamix_mode,
2149 spec->out_paths[0],
2150 spec->aamix_out_paths[0],
2151 spec->autocfg.line_out_type);
2152 else
2153 update_aamix_paths(codec, spec->aamix_mode,
2154 spec->hp_paths[0],
2155 spec->aamix_out_paths[1],
2156 AUTO_PIN_HP_OUT);
2157 }
2158
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002159 spec->indep_hp_enabled = select;
2160 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002161 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002162 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002163 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002164
Takashi Iwai963afde2013-05-31 15:20:31 +02002165 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002166 ret = 1;
2167 }
2168 unlock:
2169 mutex_unlock(&spec->pcm_mutex);
2170 return ret;
2171}
2172
2173static const struct snd_kcontrol_new indep_hp_ctl = {
2174 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2175 .name = "Independent HP",
2176 .info = indep_hp_info,
2177 .get = indep_hp_get,
2178 .put = indep_hp_put,
2179};
2180
2181
2182static int create_indep_hp_ctls(struct hda_codec *codec)
2183{
2184 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002185 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002186
2187 if (!spec->indep_hp)
2188 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002189 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2190 dac = spec->multiout.dac_nids[0];
2191 else
2192 dac = spec->multiout.hp_out_nid[0];
2193 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002194 spec->indep_hp = 0;
2195 return 0;
2196 }
2197
2198 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002199 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002200 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2201 return -ENOMEM;
2202 return 0;
2203}
2204
2205/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002206 * channel mode enum control
2207 */
2208
2209static int ch_mode_info(struct snd_kcontrol *kcontrol,
2210 struct snd_ctl_elem_info *uinfo)
2211{
2212 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2213 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002214 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002215
2216 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2217 uinfo->count = 1;
2218 uinfo->value.enumerated.items = spec->multi_ios + 1;
2219 if (uinfo->value.enumerated.item > spec->multi_ios)
2220 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002221 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2222 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002223 return 0;
2224}
2225
2226static int ch_mode_get(struct snd_kcontrol *kcontrol,
2227 struct snd_ctl_elem_value *ucontrol)
2228{
2229 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2230 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002231 ucontrol->value.enumerated.item[0] =
2232 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002233 return 0;
2234}
2235
Takashi Iwai196c17662013-01-04 15:01:40 +01002236static inline struct nid_path *
2237get_multiio_path(struct hda_codec *codec, int idx)
2238{
2239 struct hda_gen_spec *spec = codec->spec;
2240 return snd_hda_get_path_from_idx(codec,
2241 spec->out_paths[spec->autocfg.line_outs + idx]);
2242}
2243
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002244static void update_automute_all(struct hda_codec *codec);
2245
Takashi Iwai65033cc2013-04-16 12:31:05 +02002246/* Default value to be passed as aamix argument for snd_hda_activate_path();
2247 * used for output paths
2248 */
2249static bool aamix_default(struct hda_gen_spec *spec)
2250{
2251 return !spec->have_aamix_ctl || spec->aamix_mode;
2252}
2253
Takashi Iwai352f7f92012-12-19 12:52:06 +01002254static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2255{
2256 struct hda_gen_spec *spec = codec->spec;
2257 hda_nid_t nid = spec->multi_io[idx].pin;
2258 struct nid_path *path;
2259
Takashi Iwai196c17662013-01-04 15:01:40 +01002260 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002261 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002263
2264 if (path->active == output)
2265 return 0;
2266
2267 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002268 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002269 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002270 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002271 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002272 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002273 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002274 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002275 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002277
2278 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002279 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002280
Takashi Iwai352f7f92012-12-19 12:52:06 +01002281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
Takashi Iwai352f7f92012-12-19 12:52:06 +01002284static int ch_mode_put(struct snd_kcontrol *kcontrol,
2285 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002287 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2288 struct hda_gen_spec *spec = codec->spec;
2289 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Takashi Iwai352f7f92012-12-19 12:52:06 +01002291 ch = ucontrol->value.enumerated.item[0];
2292 if (ch < 0 || ch > spec->multi_ios)
2293 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002294 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002295 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002296 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002297 for (i = 0; i < spec->multi_ios; i++)
2298 set_multi_io(codec, i, i < ch);
2299 spec->multiout.max_channels = max(spec->ext_channel_count,
2300 spec->const_channel_count);
2301 if (spec->need_dac_fix)
2302 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return 1;
2304}
2305
Takashi Iwai352f7f92012-12-19 12:52:06 +01002306static const struct snd_kcontrol_new channel_mode_enum = {
2307 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2308 .name = "Channel Mode",
2309 .info = ch_mode_info,
2310 .get = ch_mode_get,
2311 .put = ch_mode_put,
2312};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Takashi Iwai352f7f92012-12-19 12:52:06 +01002314static int create_multi_channel_mode(struct hda_codec *codec)
2315{
2316 struct hda_gen_spec *spec = codec->spec;
2317
2318 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002319 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002320 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return 0;
2323}
2324
Takashi Iwai352f7f92012-12-19 12:52:06 +01002325/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002326 * aamix loopback enable/disable switch
2327 */
2328
2329#define loopback_mixing_info indep_hp_info
2330
2331static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2332 struct snd_ctl_elem_value *ucontrol)
2333{
2334 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2335 struct hda_gen_spec *spec = codec->spec;
2336 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2337 return 0;
2338}
2339
2340static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002341 int nomix_path_idx, int mix_path_idx,
2342 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002343{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002344 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002345 struct nid_path *nomix_path, *mix_path;
2346
2347 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2348 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2349 if (!nomix_path || !mix_path)
2350 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002351
2352 /* if HP aamix path is driven from a different DAC and the
2353 * independent HP mode is ON, can't turn on aamix path
2354 */
2355 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2356 mix_path->path[0] != spec->alt_dac_nid)
2357 do_mix = false;
2358
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002359 if (do_mix) {
2360 snd_hda_activate_path(codec, nomix_path, false, true);
2361 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002362 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002363 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002364 snd_hda_activate_path(codec, mix_path, false, false);
2365 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002366 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002367 }
2368}
2369
2370static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2371 struct snd_ctl_elem_value *ucontrol)
2372{
2373 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2374 struct hda_gen_spec *spec = codec->spec;
2375 unsigned int val = ucontrol->value.enumerated.item[0];
2376
2377 if (val == spec->aamix_mode)
2378 return 0;
2379 spec->aamix_mode = val;
2380 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002381 spec->aamix_out_paths[0],
2382 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002383 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002384 spec->aamix_out_paths[1],
2385 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002386 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002387 spec->aamix_out_paths[2],
2388 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002389 return 1;
2390}
2391
2392static const struct snd_kcontrol_new loopback_mixing_enum = {
2393 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2394 .name = "Loopback Mixing",
2395 .info = loopback_mixing_info,
2396 .get = loopback_mixing_get,
2397 .put = loopback_mixing_put,
2398};
2399
2400static int create_loopback_mixing_ctl(struct hda_codec *codec)
2401{
2402 struct hda_gen_spec *spec = codec->spec;
2403
2404 if (!spec->mixer_nid)
2405 return 0;
2406 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2407 spec->aamix_out_paths[2]))
2408 return 0;
2409 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2410 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002411 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002412 return 0;
2413}
2414
2415/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002416 * shared headphone/mic handling
2417 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002418
Takashi Iwai352f7f92012-12-19 12:52:06 +01002419static void call_update_outputs(struct hda_codec *codec);
2420
2421/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002422static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002423{
2424 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002425 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002426 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002427 hda_nid_t pin;
2428
2429 pin = spec->hp_mic_pin;
2430 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2431
2432 if (!force) {
2433 val = snd_hda_codec_get_pin_target(codec, pin);
2434 if (as_mic) {
2435 if (val & PIN_IN)
2436 return;
2437 } else {
2438 if (val & PIN_OUT)
2439 return;
2440 }
2441 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002442
2443 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002444 /* if the HP pin doesn't support VREF and the codec driver gives an
2445 * alternative pin, set up the VREF on that pin instead
2446 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002447 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2448 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2449 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2450 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002451 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002452 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002453 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002454
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002455 if (!spec->hp_mic_jack_modes) {
2456 if (as_mic)
2457 val |= PIN_IN;
2458 else
2459 val = PIN_HP;
2460 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002461 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002462 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002463}
2464
2465/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002466static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002467{
2468 struct hda_gen_spec *spec = codec->spec;
2469 struct auto_pin_cfg *cfg = &spec->autocfg;
2470 unsigned int defcfg;
2471 hda_nid_t nid;
2472
Takashi Iwai967303d2013-02-19 17:12:42 +01002473 if (!spec->hp_mic) {
2474 if (spec->suppress_hp_mic_detect)
2475 return 0;
2476 /* automatic detection: only if no input or a single internal
2477 * input pin is found, try to detect the shared hp/mic
2478 */
2479 if (cfg->num_inputs > 1)
2480 return 0;
2481 else if (cfg->num_inputs == 1) {
2482 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2483 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2484 return 0;
2485 }
2486 }
2487
2488 spec->hp_mic = 0; /* clear once */
2489 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002490 return 0;
2491
Takashi Iwai967303d2013-02-19 17:12:42 +01002492 nid = 0;
2493 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2494 nid = cfg->line_out_pins[0];
2495 else if (cfg->hp_outs > 0)
2496 nid = cfg->hp_pins[0];
2497 if (!nid)
2498 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002499
2500 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2501 return 0; /* no input */
2502
Takashi Iwai967303d2013-02-19 17:12:42 +01002503 cfg->inputs[cfg->num_inputs].pin = nid;
2504 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002505 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002506 cfg->num_inputs++;
2507 spec->hp_mic = 1;
2508 spec->hp_mic_pin = nid;
2509 /* we can't handle auto-mic together with HP-mic */
2510 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002511 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002512 return 0;
2513}
2514
Takashi Iwai978e77e2013-01-10 16:57:58 +01002515/*
2516 * output jack mode
2517 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002518
2519static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2520
2521static const char * const out_jack_texts[] = {
2522 "Line Out", "Headphone Out",
2523};
2524
Takashi Iwai978e77e2013-01-10 16:57:58 +01002525static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2526 struct snd_ctl_elem_info *uinfo)
2527{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002528 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002529}
2530
2531static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2532 struct snd_ctl_elem_value *ucontrol)
2533{
2534 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2535 hda_nid_t nid = kcontrol->private_value;
2536 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2537 ucontrol->value.enumerated.item[0] = 1;
2538 else
2539 ucontrol->value.enumerated.item[0] = 0;
2540 return 0;
2541}
2542
2543static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2544 struct snd_ctl_elem_value *ucontrol)
2545{
2546 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2547 hda_nid_t nid = kcontrol->private_value;
2548 unsigned int val;
2549
2550 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2551 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2552 return 0;
2553 snd_hda_set_pin_ctl_cache(codec, nid, val);
2554 return 1;
2555}
2556
2557static const struct snd_kcontrol_new out_jack_mode_enum = {
2558 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2559 .info = out_jack_mode_info,
2560 .get = out_jack_mode_get,
2561 .put = out_jack_mode_put,
2562};
2563
2564static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2565{
2566 struct hda_gen_spec *spec = codec->spec;
2567 int i;
2568
2569 for (i = 0; i < spec->kctls.used; i++) {
2570 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2571 if (!strcmp(kctl->name, name) && kctl->index == idx)
2572 return true;
2573 }
2574 return false;
2575}
2576
2577static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2578 char *name, size_t name_len)
2579{
2580 struct hda_gen_spec *spec = codec->spec;
2581 int idx = 0;
2582
2583 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2584 strlcat(name, " Jack Mode", name_len);
2585
2586 for (; find_kctl_name(codec, name, idx); idx++)
2587 ;
2588}
2589
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002590static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2591{
2592 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002593 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002594 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2595 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2596 return 2;
2597 }
2598 return 1;
2599}
2600
Takashi Iwai978e77e2013-01-10 16:57:58 +01002601static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2602 hda_nid_t *pins)
2603{
2604 struct hda_gen_spec *spec = codec->spec;
2605 int i;
2606
2607 for (i = 0; i < num_pins; i++) {
2608 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002609 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002610 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002611 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002612 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002613 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002614 get_jack_mode_name(codec, pin, name, sizeof(name));
2615 knew = snd_hda_gen_add_kctl(spec, name,
2616 &out_jack_mode_enum);
2617 if (!knew)
2618 return -ENOMEM;
2619 knew->private_value = pin;
2620 }
2621 }
2622
2623 return 0;
2624}
2625
Takashi Iwai294765582013-01-17 09:52:11 +01002626/*
2627 * input jack mode
2628 */
2629
2630/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2631#define NUM_VREFS 6
2632
2633static const char * const vref_texts[NUM_VREFS] = {
2634 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2635 "", "Mic 80pc Bias", "Mic 100pc Bias"
2636};
2637
2638static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2639{
2640 unsigned int pincap;
2641
2642 pincap = snd_hda_query_pin_caps(codec, pin);
2643 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2644 /* filter out unusual vrefs */
2645 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2646 return pincap;
2647}
2648
2649/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2650static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2651{
2652 unsigned int i, n = 0;
2653
2654 for (i = 0; i < NUM_VREFS; i++) {
2655 if (vref_caps & (1 << i)) {
2656 if (n == item_idx)
2657 return i;
2658 n++;
2659 }
2660 }
2661 return 0;
2662}
2663
2664/* convert back from the vref ctl index to the enum item index */
2665static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2666{
2667 unsigned int i, n = 0;
2668
2669 for (i = 0; i < NUM_VREFS; i++) {
2670 if (i == idx)
2671 return n;
2672 if (vref_caps & (1 << i))
2673 n++;
2674 }
2675 return 0;
2676}
2677
2678static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2679 struct snd_ctl_elem_info *uinfo)
2680{
2681 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2682 hda_nid_t nid = kcontrol->private_value;
2683 unsigned int vref_caps = get_vref_caps(codec, nid);
2684
2685 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2686 vref_texts);
2687 /* set the right text */
2688 strcpy(uinfo->value.enumerated.name,
2689 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2690 return 0;
2691}
2692
2693static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2694 struct snd_ctl_elem_value *ucontrol)
2695{
2696 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2697 hda_nid_t nid = kcontrol->private_value;
2698 unsigned int vref_caps = get_vref_caps(codec, nid);
2699 unsigned int idx;
2700
2701 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2702 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2703 return 0;
2704}
2705
2706static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2707 struct snd_ctl_elem_value *ucontrol)
2708{
2709 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2710 hda_nid_t nid = kcontrol->private_value;
2711 unsigned int vref_caps = get_vref_caps(codec, nid);
2712 unsigned int val, idx;
2713
2714 val = snd_hda_codec_get_pin_target(codec, nid);
2715 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2716 if (idx == ucontrol->value.enumerated.item[0])
2717 return 0;
2718
2719 val &= ~AC_PINCTL_VREFEN;
2720 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2721 snd_hda_set_pin_ctl_cache(codec, nid, val);
2722 return 1;
2723}
2724
2725static const struct snd_kcontrol_new in_jack_mode_enum = {
2726 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2727 .info = in_jack_mode_info,
2728 .get = in_jack_mode_get,
2729 .put = in_jack_mode_put,
2730};
2731
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002732static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2733{
2734 struct hda_gen_spec *spec = codec->spec;
2735 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002736 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002737 nitems = hweight32(get_vref_caps(codec, pin));
2738 return nitems ? nitems : 1;
2739}
2740
Takashi Iwai294765582013-01-17 09:52:11 +01002741static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2742{
2743 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002744 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002745 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002746 unsigned int defcfg;
2747
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002748 if (pin == spec->hp_mic_pin)
2749 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002750
2751 /* no jack mode for fixed pins */
2752 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2753 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2754 return 0;
2755
2756 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002757 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002758 return 0;
2759
2760 get_jack_mode_name(codec, pin, name, sizeof(name));
2761 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2762 if (!knew)
2763 return -ENOMEM;
2764 knew->private_value = pin;
2765 return 0;
2766}
2767
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002768/*
2769 * HP/mic shared jack mode
2770 */
2771static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2772 struct snd_ctl_elem_info *uinfo)
2773{
2774 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2775 hda_nid_t nid = kcontrol->private_value;
2776 int out_jacks = get_out_jack_num_items(codec, nid);
2777 int in_jacks = get_in_jack_num_items(codec, nid);
2778 const char *text = NULL;
2779 int idx;
2780
2781 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2782 uinfo->count = 1;
2783 uinfo->value.enumerated.items = out_jacks + in_jacks;
2784 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2785 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2786 idx = uinfo->value.enumerated.item;
2787 if (idx < out_jacks) {
2788 if (out_jacks > 1)
2789 text = out_jack_texts[idx];
2790 else
2791 text = "Headphone Out";
2792 } else {
2793 idx -= out_jacks;
2794 if (in_jacks > 1) {
2795 unsigned int vref_caps = get_vref_caps(codec, nid);
2796 text = vref_texts[get_vref_idx(vref_caps, idx)];
2797 } else
2798 text = "Mic In";
2799 }
2800
2801 strcpy(uinfo->value.enumerated.name, text);
2802 return 0;
2803}
2804
2805static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2806{
2807 int out_jacks = get_out_jack_num_items(codec, nid);
2808 int in_jacks = get_in_jack_num_items(codec, nid);
2809 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2810 int idx = 0;
2811
2812 if (val & PIN_OUT) {
2813 if (out_jacks > 1 && val == PIN_HP)
2814 idx = 1;
2815 } else if (val & PIN_IN) {
2816 idx = out_jacks;
2817 if (in_jacks > 1) {
2818 unsigned int vref_caps = get_vref_caps(codec, nid);
2819 val &= AC_PINCTL_VREFEN;
2820 idx += cvt_from_vref_idx(vref_caps, val);
2821 }
2822 }
2823 return idx;
2824}
2825
2826static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2827 struct snd_ctl_elem_value *ucontrol)
2828{
2829 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2830 hda_nid_t nid = kcontrol->private_value;
2831 ucontrol->value.enumerated.item[0] =
2832 get_cur_hp_mic_jack_mode(codec, nid);
2833 return 0;
2834}
2835
2836static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2837 struct snd_ctl_elem_value *ucontrol)
2838{
2839 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2840 hda_nid_t nid = kcontrol->private_value;
2841 int out_jacks = get_out_jack_num_items(codec, nid);
2842 int in_jacks = get_in_jack_num_items(codec, nid);
2843 unsigned int val, oldval, idx;
2844
2845 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2846 idx = ucontrol->value.enumerated.item[0];
2847 if (oldval == idx)
2848 return 0;
2849
2850 if (idx < out_jacks) {
2851 if (out_jacks > 1)
2852 val = idx ? PIN_HP : PIN_OUT;
2853 else
2854 val = PIN_HP;
2855 } else {
2856 idx -= out_jacks;
2857 if (in_jacks > 1) {
2858 unsigned int vref_caps = get_vref_caps(codec, nid);
2859 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002860 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2861 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002862 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002863 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002864 }
2865 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002866 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002867
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002868 return 1;
2869}
2870
2871static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2872 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2873 .info = hp_mic_jack_mode_info,
2874 .get = hp_mic_jack_mode_get,
2875 .put = hp_mic_jack_mode_put,
2876};
2877
2878static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2879{
2880 struct hda_gen_spec *spec = codec->spec;
2881 struct snd_kcontrol_new *knew;
2882
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002883 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2884 &hp_mic_jack_mode_enum);
2885 if (!knew)
2886 return -ENOMEM;
2887 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002888 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002889 return 0;
2890}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002891
2892/*
2893 * Parse input paths
2894 */
2895
Takashi Iwai352f7f92012-12-19 12:52:06 +01002896/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002897static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002898{
2899 struct hda_amp_list *list;
2900
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002901 list = snd_array_new(&spec->loopback_list);
2902 if (!list)
2903 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002904 list->nid = mix;
2905 list->dir = HDA_INPUT;
2906 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002907 spec->loopback.amplist = spec->loopback_list.list;
2908 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002909}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002910
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002911/* return true if either a volume or a mute amp is found for the given
2912 * aamix path; the amp has to be either in the mixer node or its direct leaf
2913 */
2914static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2915 hda_nid_t pin, unsigned int *mix_val,
2916 unsigned int *mute_val)
2917{
2918 int idx, num_conns;
2919 const hda_nid_t *list;
2920 hda_nid_t nid;
2921
2922 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2923 if (idx < 0)
2924 return false;
2925
2926 *mix_val = *mute_val = 0;
2927 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2928 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2929 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2930 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2931 if (*mix_val && *mute_val)
2932 return true;
2933
2934 /* check leaf node */
2935 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2936 if (num_conns < idx)
2937 return false;
2938 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01002939 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2940 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002941 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01002942 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2943 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002944 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2945
2946 return *mix_val || *mute_val;
2947}
2948
Takashi Iwai352f7f92012-12-19 12:52:06 +01002949/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002950static int new_analog_input(struct hda_codec *codec, int input_idx,
2951 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002952 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002954 struct hda_gen_spec *spec = codec->spec;
2955 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002956 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002957 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002959 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2960 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002961
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002962 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002963 if (!path)
2964 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002965 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002966 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002967
2968 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002969 if (mix_val) {
2970 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002971 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002973 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
2975
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002976 if (mute_val) {
2977 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002978 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002980 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 }
2982
Takashi Iwai352f7f92012-12-19 12:52:06 +01002983 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002984 err = add_loopback_list(spec, mix_nid, idx);
2985 if (err < 0)
2986 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002987
2988 if (spec->mixer_nid != spec->mixer_merge_nid &&
2989 !spec->loopback_merge_path) {
2990 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2991 spec->mixer_merge_nid, 0);
2992 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002993 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002994 path->active = true;
2995 spec->loopback_merge_path =
2996 snd_hda_get_path_idx(codec, path);
2997 }
2998 }
2999
Takashi Iwai352f7f92012-12-19 12:52:06 +01003000 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
Takashi Iwai352f7f92012-12-19 12:52:06 +01003003static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003005 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3006 return (pincap & AC_PINCAP_IN) != 0;
3007}
3008
3009/* Parse the codec tree and retrieve ADCs */
3010static int fill_adc_nids(struct hda_codec *codec)
3011{
3012 struct hda_gen_spec *spec = codec->spec;
3013 hda_nid_t nid;
3014 hda_nid_t *adc_nids = spec->adc_nids;
3015 int max_nums = ARRAY_SIZE(spec->adc_nids);
3016 int i, nums = 0;
3017
3018 nid = codec->start_nid;
3019 for (i = 0; i < codec->num_nodes; i++, nid++) {
3020 unsigned int caps = get_wcaps(codec, nid);
3021 int type = get_wcaps_type(caps);
3022
3023 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3024 continue;
3025 adc_nids[nums] = nid;
3026 if (++nums >= max_nums)
3027 break;
3028 }
3029 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003030
3031 /* copy the detected ADCs to all_adcs[] */
3032 spec->num_all_adcs = nums;
3033 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3034
Takashi Iwai352f7f92012-12-19 12:52:06 +01003035 return nums;
3036}
3037
3038/* filter out invalid adc_nids that don't give all active input pins;
3039 * if needed, check whether dynamic ADC-switching is available
3040 */
3041static int check_dyn_adc_switch(struct hda_codec *codec)
3042{
3043 struct hda_gen_spec *spec = codec->spec;
3044 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003045 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003046 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003047
Takashi Iwai352f7f92012-12-19 12:52:06 +01003048 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003049 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003050 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003051 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003052 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003053 break;
3054 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003055 if (i >= imux->num_items) {
3056 ok_bits |= (1 << n);
3057 nums++;
3058 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003059 }
3060
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003061 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003062 /* check whether ADC-switch is possible */
3063 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003064 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003065 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003066 spec->dyn_adc_idx[i] = n;
3067 break;
3068 }
3069 }
3070 }
3071
Takashi Iwai4e76a882014-02-25 12:21:03 +01003072 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003073 spec->dyn_adc_switch = 1;
3074 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003075 /* shrink the invalid adcs and input paths */
3076 nums = 0;
3077 for (n = 0; n < spec->num_adc_nids; n++) {
3078 if (!(ok_bits & (1 << n)))
3079 continue;
3080 if (n != nums) {
3081 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003082 for (i = 0; i < imux->num_items; i++) {
3083 invalidate_nid_path(codec,
3084 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003085 spec->input_paths[i][nums] =
3086 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003087 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003088 }
3089 nums++;
3090 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003091 spec->num_adc_nids = nums;
3092 }
3093
Takashi Iwai967303d2013-02-19 17:12:42 +01003094 if (imux->num_items == 1 ||
3095 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003096 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003097 spec->num_adc_nids = 1; /* reduce to a single ADC */
3098 }
3099
3100 /* single index for individual volumes ctls */
3101 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3102 spec->num_adc_nids = 1;
3103
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return 0;
3105}
3106
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003107/* parse capture source paths from the given pin and create imux items */
3108static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003109 int cfg_idx, int num_adcs,
3110 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003111{
3112 struct hda_gen_spec *spec = codec->spec;
3113 struct hda_input_mux *imux = &spec->input_mux;
3114 int imux_idx = imux->num_items;
3115 bool imux_added = false;
3116 int c;
3117
3118 for (c = 0; c < num_adcs; c++) {
3119 struct nid_path *path;
3120 hda_nid_t adc = spec->adc_nids[c];
3121
3122 if (!is_reachable_path(codec, pin, adc))
3123 continue;
3124 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3125 if (!path)
3126 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003127 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003128 spec->input_paths[imux_idx][c] =
3129 snd_hda_get_path_idx(codec, path);
3130
3131 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003132 if (spec->hp_mic_pin == pin)
3133 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003134 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003135 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003136 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003137 if (spec->dyn_adc_switch)
3138 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003139 }
3140 }
3141
3142 return 0;
3143}
3144
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003146 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003148
Takashi Iwaic9700422013-01-18 10:17:30 +01003149/* fill the label for each input at first */
3150static int fill_input_pin_labels(struct hda_codec *codec)
3151{
3152 struct hda_gen_spec *spec = codec->spec;
3153 const struct auto_pin_cfg *cfg = &spec->autocfg;
3154 int i;
3155
3156 for (i = 0; i < cfg->num_inputs; i++) {
3157 hda_nid_t pin = cfg->inputs[i].pin;
3158 const char *label;
3159 int j, idx;
3160
3161 if (!is_input_pin(codec, pin))
3162 continue;
3163
3164 label = hda_get_autocfg_input_label(codec, cfg, i);
3165 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003166 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003167 if (spec->input_labels[j] &&
3168 !strcmp(spec->input_labels[j], label)) {
3169 idx = spec->input_label_idxs[j] + 1;
3170 break;
3171 }
3172 }
3173
3174 spec->input_labels[i] = label;
3175 spec->input_label_idxs[i] = idx;
3176 }
3177
3178 return 0;
3179}
3180
Takashi Iwai9dba2052013-01-18 10:01:15 +01003181#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3182
Takashi Iwai352f7f92012-12-19 12:52:06 +01003183static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003184{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003185 struct hda_gen_spec *spec = codec->spec;
3186 const struct auto_pin_cfg *cfg = &spec->autocfg;
3187 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003188 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003189 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003190 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003191
Takashi Iwai352f7f92012-12-19 12:52:06 +01003192 num_adcs = fill_adc_nids(codec);
3193 if (num_adcs < 0)
3194 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003195
Takashi Iwaic9700422013-01-18 10:17:30 +01003196 err = fill_input_pin_labels(codec);
3197 if (err < 0)
3198 return err;
3199
Takashi Iwai352f7f92012-12-19 12:52:06 +01003200 for (i = 0; i < cfg->num_inputs; i++) {
3201 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Takashi Iwai352f7f92012-12-19 12:52:06 +01003203 pin = cfg->inputs[i].pin;
3204 if (!is_input_pin(codec, pin))
3205 continue;
3206
Takashi Iwai2c12c302013-01-10 09:33:29 +01003207 val = PIN_IN;
3208 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3209 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003210 if (pin != spec->hp_mic_pin)
3211 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003212
Takashi Iwai352f7f92012-12-19 12:52:06 +01003213 if (mixer) {
3214 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003215 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003216 spec->input_labels[i],
3217 spec->input_label_idxs[i],
3218 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003219 if (err < 0)
3220 return err;
3221 }
3222 }
3223
Takashi Iwaic9700422013-01-18 10:17:30 +01003224 err = parse_capture_source(codec, pin, i, num_adcs,
3225 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003226 if (err < 0)
3227 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003228
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003229 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003230 err = create_in_jack_mode(codec, pin);
3231 if (err < 0)
3232 return err;
3233 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003234 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003235
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003236 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003237 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003238 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003239 "Stereo Mix", 0);
3240 if (err < 0)
3241 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003242 else
3243 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003244 }
3245
3246 return 0;
3247}
3248
3249
3250/*
3251 * input source mux
3252 */
3253
Takashi Iwaic697b712013-01-07 17:09:26 +01003254/* get the input path specified by the given adc and imux indices */
3255static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003256{
3257 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003258 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3259 snd_BUG();
3260 return NULL;
3261 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003262 if (spec->dyn_adc_switch)
3263 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003264 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003265 snd_BUG();
3266 return NULL;
3267 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003268 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003269}
3270
3271static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3272 unsigned int idx);
3273
3274static int mux_enum_info(struct snd_kcontrol *kcontrol,
3275 struct snd_ctl_elem_info *uinfo)
3276{
3277 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3278 struct hda_gen_spec *spec = codec->spec;
3279 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3280}
3281
3282static int mux_enum_get(struct snd_kcontrol *kcontrol,
3283 struct snd_ctl_elem_value *ucontrol)
3284{
3285 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3286 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003287 /* the ctls are created at once with multiple counts */
3288 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003289
3290 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3291 return 0;
3292}
3293
3294static int mux_enum_put(struct snd_kcontrol *kcontrol,
3295 struct snd_ctl_elem_value *ucontrol)
3296{
3297 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003298 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003299 return mux_select(codec, adc_idx,
3300 ucontrol->value.enumerated.item[0]);
3301}
3302
Takashi Iwai352f7f92012-12-19 12:52:06 +01003303static const struct snd_kcontrol_new cap_src_temp = {
3304 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3305 .name = "Input Source",
3306 .info = mux_enum_info,
3307 .get = mux_enum_get,
3308 .put = mux_enum_put,
3309};
3310
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003311/*
3312 * capture volume and capture switch ctls
3313 */
3314
Takashi Iwai352f7f92012-12-19 12:52:06 +01003315typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3316 struct snd_ctl_elem_value *ucontrol);
3317
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003318/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003319static int cap_put_caller(struct snd_kcontrol *kcontrol,
3320 struct snd_ctl_elem_value *ucontrol,
3321 put_call_t func, int type)
3322{
3323 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3324 struct hda_gen_spec *spec = codec->spec;
3325 const struct hda_input_mux *imux;
3326 struct nid_path *path;
3327 int i, adc_idx, err = 0;
3328
3329 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003330 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003331 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003332 /* we use the cache-only update at first since multiple input paths
3333 * may shared the same amp; by updating only caches, the redundant
3334 * writes to hardware can be reduced.
3335 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003336 codec->cached_write = 1;
3337 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003338 path = get_input_path(codec, adc_idx, i);
3339 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003340 continue;
3341 kcontrol->private_value = path->ctls[type];
3342 err = func(kcontrol, ucontrol);
3343 if (err < 0)
3344 goto error;
3345 }
3346 error:
3347 codec->cached_write = 0;
3348 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003349 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003350 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003351 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003352 return err;
3353}
3354
3355/* capture volume ctl callbacks */
3356#define cap_vol_info snd_hda_mixer_amp_volume_info
3357#define cap_vol_get snd_hda_mixer_amp_volume_get
3358#define cap_vol_tlv snd_hda_mixer_amp_tlv
3359
3360static int cap_vol_put(struct snd_kcontrol *kcontrol,
3361 struct snd_ctl_elem_value *ucontrol)
3362{
3363 return cap_put_caller(kcontrol, ucontrol,
3364 snd_hda_mixer_amp_volume_put,
3365 NID_PATH_VOL_CTL);
3366}
3367
3368static const struct snd_kcontrol_new cap_vol_temp = {
3369 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3370 .name = "Capture Volume",
3371 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3372 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3373 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3374 .info = cap_vol_info,
3375 .get = cap_vol_get,
3376 .put = cap_vol_put,
3377 .tlv = { .c = cap_vol_tlv },
3378};
3379
3380/* capture switch ctl callbacks */
3381#define cap_sw_info snd_ctl_boolean_stereo_info
3382#define cap_sw_get snd_hda_mixer_amp_switch_get
3383
3384static int cap_sw_put(struct snd_kcontrol *kcontrol,
3385 struct snd_ctl_elem_value *ucontrol)
3386{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003387 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003388 snd_hda_mixer_amp_switch_put,
3389 NID_PATH_MUTE_CTL);
3390}
3391
3392static const struct snd_kcontrol_new cap_sw_temp = {
3393 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3394 .name = "Capture Switch",
3395 .info = cap_sw_info,
3396 .get = cap_sw_get,
3397 .put = cap_sw_put,
3398};
3399
3400static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3401{
3402 hda_nid_t nid;
3403 int i, depth;
3404
3405 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3406 for (depth = 0; depth < 3; depth++) {
3407 if (depth >= path->depth)
3408 return -EINVAL;
3409 i = path->depth - depth - 1;
3410 nid = path->path[i];
3411 if (!path->ctls[NID_PATH_VOL_CTL]) {
3412 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3413 path->ctls[NID_PATH_VOL_CTL] =
3414 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3415 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3416 int idx = path->idx[i];
3417 if (!depth && codec->single_adc_amp)
3418 idx = 0;
3419 path->ctls[NID_PATH_VOL_CTL] =
3420 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3421 }
3422 }
3423 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3424 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3425 path->ctls[NID_PATH_MUTE_CTL] =
3426 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3427 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3428 int idx = path->idx[i];
3429 if (!depth && codec->single_adc_amp)
3430 idx = 0;
3431 path->ctls[NID_PATH_MUTE_CTL] =
3432 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3433 }
3434 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 return 0;
3437}
3438
Takashi Iwai352f7f92012-12-19 12:52:06 +01003439static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003441 struct hda_gen_spec *spec = codec->spec;
3442 struct auto_pin_cfg *cfg = &spec->autocfg;
3443 unsigned int val;
3444 int i;
3445
3446 if (!spec->inv_dmic_split)
3447 return false;
3448 for (i = 0; i < cfg->num_inputs; i++) {
3449 if (cfg->inputs[i].pin != nid)
3450 continue;
3451 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3452 return false;
3453 val = snd_hda_codec_get_pincfg(codec, nid);
3454 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3455 }
3456 return false;
3457}
3458
Takashi Iwaia90229e2013-01-18 14:10:00 +01003459/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003460static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3461 struct snd_ctl_elem_value *ucontrol)
3462{
3463 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3464 struct hda_gen_spec *spec = codec->spec;
3465 int ret;
3466
3467 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3468 if (ret < 0)
3469 return ret;
3470
Takashi Iwaia90229e2013-01-18 14:10:00 +01003471 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003472 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003473
3474 return ret;
3475}
3476
Takashi Iwai352f7f92012-12-19 12:52:06 +01003477static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3478 int idx, bool is_switch, unsigned int ctl,
3479 bool inv_dmic)
3480{
3481 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003482 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003483 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3484 const char *sfx = is_switch ? "Switch" : "Volume";
3485 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003486 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003487
3488 if (!ctl)
3489 return 0;
3490
3491 if (label)
3492 snprintf(tmpname, sizeof(tmpname),
3493 "%s Capture %s", label, sfx);
3494 else
3495 snprintf(tmpname, sizeof(tmpname),
3496 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003497 knew = add_control(spec, type, tmpname, idx,
3498 amp_val_replace_channels(ctl, chs));
3499 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 if (!inv_dmic)
3504 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003505
3506 /* Make independent right kcontrol */
3507 if (label)
3508 snprintf(tmpname, sizeof(tmpname),
3509 "Inverted %s Capture %s", label, sfx);
3510 else
3511 snprintf(tmpname, sizeof(tmpname),
3512 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003513 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003514 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003515 if (!knew)
3516 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003517 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003518 knew->put = cap_single_sw_put;
3519 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003520}
3521
3522/* create single (and simple) capture volume and switch controls */
3523static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3524 unsigned int vol_ctl, unsigned int sw_ctl,
3525 bool inv_dmic)
3526{
3527 int err;
3528 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3529 if (err < 0)
3530 return err;
3531 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3532 if (err < 0)
3533 return err;
3534 return 0;
3535}
3536
3537/* create bound capture volume and switch controls */
3538static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3539 unsigned int vol_ctl, unsigned int sw_ctl)
3540{
3541 struct hda_gen_spec *spec = codec->spec;
3542 struct snd_kcontrol_new *knew;
3543
3544 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003545 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003546 if (!knew)
3547 return -ENOMEM;
3548 knew->index = idx;
3549 knew->private_value = vol_ctl;
3550 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3551 }
3552 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003553 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003554 if (!knew)
3555 return -ENOMEM;
3556 knew->index = idx;
3557 knew->private_value = sw_ctl;
3558 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3559 }
3560 return 0;
3561}
3562
3563/* return the vol ctl when used first in the imux list */
3564static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3565{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003566 struct nid_path *path;
3567 unsigned int ctl;
3568 int i;
3569
Takashi Iwaic697b712013-01-07 17:09:26 +01003570 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003571 if (!path)
3572 return 0;
3573 ctl = path->ctls[type];
3574 if (!ctl)
3575 return 0;
3576 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003577 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003578 if (path && path->ctls[type] == ctl)
3579 return 0;
3580 }
3581 return ctl;
3582}
3583
3584/* create individual capture volume and switch controls per input */
3585static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3586{
3587 struct hda_gen_spec *spec = codec->spec;
3588 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003589 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003590
3591 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003592 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003593 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003594
Takashi Iwaic9700422013-01-18 10:17:30 +01003595 idx = imux->items[i].index;
3596 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003597 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003598 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3599
3600 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003601 err = add_single_cap_ctl(codec,
3602 spec->input_labels[idx],
3603 spec->input_label_idxs[idx],
3604 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003605 get_first_cap_ctl(codec, i, type),
3606 inv_dmic);
3607 if (err < 0)
3608 return err;
3609 }
3610 }
3611 return 0;
3612}
3613
3614static int create_capture_mixers(struct hda_codec *codec)
3615{
3616 struct hda_gen_spec *spec = codec->spec;
3617 struct hda_input_mux *imux = &spec->input_mux;
3618 int i, n, nums, err;
3619
3620 if (spec->dyn_adc_switch)
3621 nums = 1;
3622 else
3623 nums = spec->num_adc_nids;
3624
3625 if (!spec->auto_mic && imux->num_items > 1) {
3626 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003627 const char *name;
3628 name = nums > 1 ? "Input Source" : "Capture Source";
3629 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003630 if (!knew)
3631 return -ENOMEM;
3632 knew->count = nums;
3633 }
3634
3635 for (n = 0; n < nums; n++) {
3636 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003637 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003638 bool inv_dmic = false;
3639 int vol, sw;
3640
3641 vol = sw = 0;
3642 for (i = 0; i < imux->num_items; i++) {
3643 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003644 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003645 if (!path)
3646 continue;
3647 parse_capvol_in_path(codec, path);
3648 if (!vol)
3649 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003650 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003651 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003652 if (!same_amp_caps(codec, vol,
3653 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3654 multi_cap_vol = true;
3655 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003656 if (!sw)
3657 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003658 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003659 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003660 if (!same_amp_caps(codec, sw,
3661 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3662 multi_cap_vol = true;
3663 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003664 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3665 inv_dmic = true;
3666 }
3667
3668 if (!multi)
3669 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3670 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003671 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003672 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3673 else
3674 err = create_multi_cap_vol_ctl(codec);
3675 if (err < 0)
3676 return err;
3677 }
3678
3679 return 0;
3680}
3681
3682/*
3683 * add mic boosts if needed
3684 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003685
3686/* check whether the given amp is feasible as a boost volume */
3687static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3688 int dir, int idx)
3689{
3690 unsigned int step;
3691
3692 if (!nid_has_volume(codec, nid, dir) ||
3693 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3694 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3695 return false;
3696
3697 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3698 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3699 if (step < 0x20)
3700 return false;
3701 return true;
3702}
3703
3704/* look for a boost amp in a widget close to the pin */
3705static unsigned int look_for_boost_amp(struct hda_codec *codec,
3706 struct nid_path *path)
3707{
3708 unsigned int val = 0;
3709 hda_nid_t nid;
3710 int depth;
3711
3712 for (depth = 0; depth < 3; depth++) {
3713 if (depth >= path->depth - 1)
3714 break;
3715 nid = path->path[depth];
3716 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3717 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3718 break;
3719 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3720 path->idx[depth])) {
3721 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3722 HDA_INPUT);
3723 break;
3724 }
3725 }
3726
3727 return val;
3728}
3729
Takashi Iwai352f7f92012-12-19 12:52:06 +01003730static int parse_mic_boost(struct hda_codec *codec)
3731{
3732 struct hda_gen_spec *spec = codec->spec;
3733 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003734 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003735 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003736
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003737 if (!spec->num_adc_nids)
3738 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003739
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003740 for (i = 0; i < imux->num_items; i++) {
3741 struct nid_path *path;
3742 unsigned int val;
3743 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003744 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003745
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003746 idx = imux->items[i].index;
3747 if (idx >= imux->num_items)
3748 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003749
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003750 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003751 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003752 continue;
3753
3754 path = get_input_path(codec, 0, i);
3755 if (!path)
3756 continue;
3757
3758 val = look_for_boost_amp(codec, path);
3759 if (!val)
3760 continue;
3761
3762 /* create a boost control */
3763 snprintf(boost_label, sizeof(boost_label),
3764 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003765 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3766 spec->input_label_idxs[idx], val))
3767 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003768
3769 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003770 }
3771 return 0;
3772}
3773
3774/*
3775 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3776 */
3777static void parse_digital(struct hda_codec *codec)
3778{
3779 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003780 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003781 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003782 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003783
3784 /* support multiple SPDIFs; the secondary is set up as a slave */
3785 nums = 0;
3786 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003787 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003788 dig_nid = look_for_dac(codec, pin, true);
3789 if (!dig_nid)
3790 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003791 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003792 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003793 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003794 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003795 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003796 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003797 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003798 if (!nums) {
3799 spec->multiout.dig_out_nid = dig_nid;
3800 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3801 } else {
3802 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3803 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd576422e2014-05-14 17:18:31 +03003804 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003805 spec->slave_dig_outs[nums - 1] = dig_nid;
3806 }
3807 nums++;
3808 }
3809
3810 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003811 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003812 dig_nid = codec->start_nid;
3813 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003814 unsigned int wcaps = get_wcaps(codec, dig_nid);
3815 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3816 continue;
3817 if (!(wcaps & AC_WCAP_DIGITAL))
3818 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003819 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003820 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003821 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003822 path->active = true;
3823 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003824 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003825 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003826 break;
3827 }
3828 }
3829 }
3830}
3831
3832
3833/*
3834 * input MUX handling
3835 */
3836
3837static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3838
3839/* select the given imux item; either unmute exclusively or select the route */
3840static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3841 unsigned int idx)
3842{
3843 struct hda_gen_spec *spec = codec->spec;
3844 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003845 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003846
3847 imux = &spec->input_mux;
3848 if (!imux->num_items)
3849 return 0;
3850
3851 if (idx >= imux->num_items)
3852 idx = imux->num_items - 1;
3853 if (spec->cur_mux[adc_idx] == idx)
3854 return 0;
3855
Takashi Iwai55196ff2013-01-24 17:32:56 +01003856 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3857 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003858 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003859 if (old_path->active)
3860 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003861
3862 spec->cur_mux[adc_idx] = idx;
3863
Takashi Iwai967303d2013-02-19 17:12:42 +01003864 if (spec->hp_mic)
3865 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003866
3867 if (spec->dyn_adc_switch)
3868 dyn_adc_pcm_resetup(codec, idx);
3869
Takashi Iwaic697b712013-01-07 17:09:26 +01003870 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003871 if (!path)
3872 return 0;
3873 if (path->active)
3874 return 0;
3875 snd_hda_activate_path(codec, path, true, false);
3876 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003877 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003878 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003879 return 1;
3880}
3881
3882
3883/*
3884 * Jack detections for HP auto-mute and mic-switch
3885 */
3886
3887/* check each pin in the given array; returns true if any of them is plugged */
3888static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3889{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003890 int i;
3891 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003892
3893 for (i = 0; i < num_pins; i++) {
3894 hda_nid_t nid = pins[i];
3895 if (!nid)
3896 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003897 /* don't detect pins retasked as inputs */
3898 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3899 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003900 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3901 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003902 }
3903 return present;
3904}
3905
3906/* standard HP/line-out auto-mute helper */
3907static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003908 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003909{
3910 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003911 int i;
3912
3913 for (i = 0; i < num_pins; i++) {
3914 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003915 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003916 if (!nid)
3917 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003918
3919 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003920 struct nid_path *path;
3921 hda_nid_t mute_nid;
3922
3923 path = snd_hda_get_path_from_idx(codec, paths[i]);
3924 if (!path)
3925 continue;
3926 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3927 if (!mute_nid)
3928 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003929 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003930 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003931 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003932 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003933 set_pin_eapd(codec, nid, !mute);
3934 continue;
3935 }
3936
Takashi Iwai967303d2013-02-19 17:12:42 +01003937 oldval = snd_hda_codec_get_pin_target(codec, nid);
3938 if (oldval & PIN_IN)
3939 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003940 /* don't reset VREF value in case it's controlling
3941 * the amp (see alc861_fixup_asus_amp_vref_0f())
3942 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003943 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003944 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003945 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003946 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003947 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003948 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003949 /* here we call update_pin_ctl() so that the pinctl is changed
3950 * without changing the pinctl target value;
3951 * the original target value will be still referred at the
3952 * init / resume again
3953 */
3954 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003955 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003956 }
3957}
3958
Takashi Iwaidda42bd2014-10-30 12:04:50 +01003959/**
3960 * snd_hda_gen_update_outputs - Toggle outputs muting
3961 * @codec: the HDA codec
3962 *
3963 * Update the mute status of all outputs based on the current jack states.
3964 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003965void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003966{
3967 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003968 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003969 int on;
3970
3971 /* Control HP pins/amps depending on master_mute state;
3972 * in general, HP pins/amps control should be enabled in all cases,
3973 * but currently set only for master_mute, just to be safe
3974 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003975 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3976 paths = spec->out_paths;
3977 else
3978 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01003979 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003980 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003981
3982 if (!spec->automute_speaker)
3983 on = 0;
3984 else
3985 on = spec->hp_jack_present | spec->line_jack_present;
3986 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01003987 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003988 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3989 paths = spec->out_paths;
3990 else
3991 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003992 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003993 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003994
3995 /* toggle line-out mutes if needed, too */
3996 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3997 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3998 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3999 return;
4000 if (!spec->automute_lo)
4001 on = 0;
4002 else
4003 on = spec->hp_jack_present;
4004 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01004005 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004006 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004007 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004008 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004009}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004010EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004011
4012static void call_update_outputs(struct hda_codec *codec)
4013{
4014 struct hda_gen_spec *spec = codec->spec;
4015 if (spec->automute_hook)
4016 spec->automute_hook(codec);
4017 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004018 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004019
4020 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4021 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4022 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004023}
4024
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004025/**
4026 * snd_hda_gen_hp_automute - standard HP-automute helper
4027 * @codec: the HDA codec
4028 * @jack: jack object, NULL for the whole
4029 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004030void snd_hda_gen_hp_automute(struct hda_codec *codec,
4031 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004032{
4033 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004034 hda_nid_t *pins = spec->autocfg.hp_pins;
4035 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004036
Takashi Iwai92603c52013-01-22 07:46:31 +01004037 /* No detection for the first HP jack during indep-HP mode */
4038 if (spec->indep_hp_enabled) {
4039 pins++;
4040 num_pins--;
4041 }
4042
4043 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004044 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4045 return;
4046 call_update_outputs(codec);
4047}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004048EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004049
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004050/**
4051 * snd_hda_gen_line_automute - standard line-out-automute helper
4052 * @codec: the HDA codec
4053 * @jack: jack object, NULL for the whole
4054 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004055void snd_hda_gen_line_automute(struct hda_codec *codec,
4056 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004057{
4058 struct hda_gen_spec *spec = codec->spec;
4059
4060 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4061 return;
4062 /* check LO jack only when it's different from HP */
4063 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4064 return;
4065
4066 spec->line_jack_present =
4067 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4068 spec->autocfg.line_out_pins);
4069 if (!spec->automute_speaker || !spec->detect_lo)
4070 return;
4071 call_update_outputs(codec);
4072}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004073EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004074
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004075/**
4076 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4077 * @codec: the HDA codec
4078 * @jack: jack object, NULL for the whole
4079 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004080void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4081 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004082{
4083 struct hda_gen_spec *spec = codec->spec;
4084 int i;
4085
4086 if (!spec->auto_mic)
4087 return;
4088
4089 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004090 hda_nid_t pin = spec->am_entry[i].pin;
4091 /* don't detect pins retasked as outputs */
4092 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4093 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004094 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004095 mux_select(codec, 0, spec->am_entry[i].idx);
4096 return;
4097 }
4098 }
4099 mux_select(codec, 0, spec->am_entry[0].idx);
4100}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004101EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004102
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004103/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004104static void call_hp_automute(struct hda_codec *codec,
4105 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004106{
4107 struct hda_gen_spec *spec = codec->spec;
4108 if (spec->hp_automute_hook)
4109 spec->hp_automute_hook(codec, jack);
4110 else
4111 snd_hda_gen_hp_automute(codec, jack);
4112}
4113
4114static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004115 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004116{
4117 struct hda_gen_spec *spec = codec->spec;
4118 if (spec->line_automute_hook)
4119 spec->line_automute_hook(codec, jack);
4120 else
4121 snd_hda_gen_line_automute(codec, jack);
4122}
4123
4124static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004125 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004126{
4127 struct hda_gen_spec *spec = codec->spec;
4128 if (spec->mic_autoswitch_hook)
4129 spec->mic_autoswitch_hook(codec, jack);
4130 else
4131 snd_hda_gen_mic_autoswitch(codec, jack);
4132}
4133
Takashi Iwai963afde2013-05-31 15:20:31 +02004134/* update jack retasking */
4135static void update_automute_all(struct hda_codec *codec)
4136{
4137 call_hp_automute(codec, NULL);
4138 call_line_automute(codec, NULL);
4139 call_mic_autoswitch(codec, NULL);
4140}
4141
Takashi Iwai352f7f92012-12-19 12:52:06 +01004142/*
4143 * Auto-Mute mode mixer enum support
4144 */
4145static int automute_mode_info(struct snd_kcontrol *kcontrol,
4146 struct snd_ctl_elem_info *uinfo)
4147{
4148 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4149 struct hda_gen_spec *spec = codec->spec;
4150 static const char * const texts3[] = {
4151 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
Takashi Iwai352f7f92012-12-19 12:52:06 +01004154 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4155 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4156 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4157}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Takashi Iwai352f7f92012-12-19 12:52:06 +01004159static int automute_mode_get(struct snd_kcontrol *kcontrol,
4160 struct snd_ctl_elem_value *ucontrol)
4161{
4162 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4163 struct hda_gen_spec *spec = codec->spec;
4164 unsigned int val = 0;
4165 if (spec->automute_speaker)
4166 val++;
4167 if (spec->automute_lo)
4168 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004169
Takashi Iwai352f7f92012-12-19 12:52:06 +01004170 ucontrol->value.enumerated.item[0] = val;
4171 return 0;
4172}
4173
4174static int automute_mode_put(struct snd_kcontrol *kcontrol,
4175 struct snd_ctl_elem_value *ucontrol)
4176{
4177 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4178 struct hda_gen_spec *spec = codec->spec;
4179
4180 switch (ucontrol->value.enumerated.item[0]) {
4181 case 0:
4182 if (!spec->automute_speaker && !spec->automute_lo)
4183 return 0;
4184 spec->automute_speaker = 0;
4185 spec->automute_lo = 0;
4186 break;
4187 case 1:
4188 if (spec->automute_speaker_possible) {
4189 if (!spec->automute_lo && spec->automute_speaker)
4190 return 0;
4191 spec->automute_speaker = 1;
4192 spec->automute_lo = 0;
4193 } else if (spec->automute_lo_possible) {
4194 if (spec->automute_lo)
4195 return 0;
4196 spec->automute_lo = 1;
4197 } else
4198 return -EINVAL;
4199 break;
4200 case 2:
4201 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4202 return -EINVAL;
4203 if (spec->automute_speaker && spec->automute_lo)
4204 return 0;
4205 spec->automute_speaker = 1;
4206 spec->automute_lo = 1;
4207 break;
4208 default:
4209 return -EINVAL;
4210 }
4211 call_update_outputs(codec);
4212 return 1;
4213}
4214
4215static const struct snd_kcontrol_new automute_mode_enum = {
4216 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4217 .name = "Auto-Mute Mode",
4218 .info = automute_mode_info,
4219 .get = automute_mode_get,
4220 .put = automute_mode_put,
4221};
4222
4223static int add_automute_mode_enum(struct hda_codec *codec)
4224{
4225 struct hda_gen_spec *spec = codec->spec;
4226
Takashi Iwai12c93df2012-12-19 14:38:33 +01004227 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004228 return -ENOMEM;
4229 return 0;
4230}
4231
4232/*
4233 * Check the availability of HP/line-out auto-mute;
4234 * Set up appropriately if really supported
4235 */
4236static int check_auto_mute_availability(struct hda_codec *codec)
4237{
4238 struct hda_gen_spec *spec = codec->spec;
4239 struct auto_pin_cfg *cfg = &spec->autocfg;
4240 int present = 0;
4241 int i, err;
4242
Takashi Iwaif72706b2013-01-16 18:20:07 +01004243 if (spec->suppress_auto_mute)
4244 return 0;
4245
Takashi Iwai352f7f92012-12-19 12:52:06 +01004246 if (cfg->hp_pins[0])
4247 present++;
4248 if (cfg->line_out_pins[0])
4249 present++;
4250 if (cfg->speaker_pins[0])
4251 present++;
4252 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004253 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004254
4255 if (!cfg->speaker_pins[0] &&
4256 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4257 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4258 sizeof(cfg->speaker_pins));
4259 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Takashi Iwai352f7f92012-12-19 12:52:06 +01004262 if (!cfg->hp_pins[0] &&
4263 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4264 memcpy(cfg->hp_pins, cfg->line_out_pins,
4265 sizeof(cfg->hp_pins));
4266 cfg->hp_outs = cfg->line_outs;
4267 }
4268
4269 for (i = 0; i < cfg->hp_outs; i++) {
4270 hda_nid_t nid = cfg->hp_pins[i];
4271 if (!is_jack_detectable(codec, nid))
4272 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004273 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004274 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004275 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004276 spec->detect_hp = 1;
4277 }
4278
4279 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4280 if (cfg->speaker_outs)
4281 for (i = 0; i < cfg->line_outs; i++) {
4282 hda_nid_t nid = cfg->line_out_pins[i];
4283 if (!is_jack_detectable(codec, nid))
4284 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004285 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004286 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004287 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004288 spec->detect_lo = 1;
4289 }
4290 spec->automute_lo_possible = spec->detect_hp;
4291 }
4292
4293 spec->automute_speaker_possible = cfg->speaker_outs &&
4294 (spec->detect_hp || spec->detect_lo);
4295
4296 spec->automute_lo = spec->automute_lo_possible;
4297 spec->automute_speaker = spec->automute_speaker_possible;
4298
4299 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4300 /* create a control for automute mode */
4301 err = add_automute_mode_enum(codec);
4302 if (err < 0)
4303 return err;
4304 }
4305 return 0;
4306}
4307
Takashi Iwai352f7f92012-12-19 12:52:06 +01004308/* check whether all auto-mic pins are valid; setup indices if OK */
4309static bool auto_mic_check_imux(struct hda_codec *codec)
4310{
4311 struct hda_gen_spec *spec = codec->spec;
4312 const struct hda_input_mux *imux;
4313 int i;
4314
4315 imux = &spec->input_mux;
4316 for (i = 0; i < spec->am_num_entries; i++) {
4317 spec->am_entry[i].idx =
4318 find_idx_in_nid_list(spec->am_entry[i].pin,
4319 spec->imux_pins, imux->num_items);
4320 if (spec->am_entry[i].idx < 0)
4321 return false; /* no corresponding imux */
4322 }
4323
4324 /* we don't need the jack detection for the first pin */
4325 for (i = 1; i < spec->am_num_entries; i++)
4326 snd_hda_jack_detect_enable_callback(codec,
4327 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004328 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004329 return true;
4330}
4331
4332static int compare_attr(const void *ap, const void *bp)
4333{
4334 const struct automic_entry *a = ap;
4335 const struct automic_entry *b = bp;
4336 return (int)(a->attr - b->attr);
4337}
4338
4339/*
4340 * Check the availability of auto-mic switch;
4341 * Set up if really supported
4342 */
4343static int check_auto_mic_availability(struct hda_codec *codec)
4344{
4345 struct hda_gen_spec *spec = codec->spec;
4346 struct auto_pin_cfg *cfg = &spec->autocfg;
4347 unsigned int types;
4348 int i, num_pins;
4349
Takashi Iwaid12daf62013-01-07 16:32:11 +01004350 if (spec->suppress_auto_mic)
4351 return 0;
4352
Takashi Iwai352f7f92012-12-19 12:52:06 +01004353 types = 0;
4354 num_pins = 0;
4355 for (i = 0; i < cfg->num_inputs; i++) {
4356 hda_nid_t nid = cfg->inputs[i].pin;
4357 unsigned int attr;
4358 attr = snd_hda_codec_get_pincfg(codec, nid);
4359 attr = snd_hda_get_input_pin_attr(attr);
4360 if (types & (1 << attr))
4361 return 0; /* already occupied */
4362 switch (attr) {
4363 case INPUT_PIN_ATTR_INT:
4364 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4365 return 0; /* invalid type */
4366 break;
4367 case INPUT_PIN_ATTR_UNUSED:
4368 return 0; /* invalid entry */
4369 default:
4370 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4371 return 0; /* invalid type */
4372 if (!spec->line_in_auto_switch &&
4373 cfg->inputs[i].type != AUTO_PIN_MIC)
4374 return 0; /* only mic is allowed */
4375 if (!is_jack_detectable(codec, nid))
4376 return 0; /* no unsol support */
4377 break;
4378 }
4379 if (num_pins >= MAX_AUTO_MIC_PINS)
4380 return 0;
4381 types |= (1 << attr);
4382 spec->am_entry[num_pins].pin = nid;
4383 spec->am_entry[num_pins].attr = attr;
4384 num_pins++;
4385 }
4386
4387 if (num_pins < 2)
4388 return 0;
4389
4390 spec->am_num_entries = num_pins;
4391 /* sort the am_entry in the order of attr so that the pin with a
4392 * higher attr will be selected when the jack is plugged.
4393 */
4394 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4395 compare_attr, NULL);
4396
4397 if (!auto_mic_check_imux(codec))
4398 return 0;
4399
4400 spec->auto_mic = 1;
4401 spec->num_adc_nids = 1;
4402 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004403 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004404 spec->am_entry[0].pin,
4405 spec->am_entry[1].pin,
4406 spec->am_entry[2].pin);
4407
4408 return 0;
4409}
4410
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004411/**
4412 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4413 * into power down
4414 * @codec: the HDA codec
4415 * @nid: NID to evalute
4416 * @power_state: target power state
4417 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004418unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004419 hda_nid_t nid,
4420 unsigned int power_state)
4421{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004422 if (power_state != AC_PWRST_D0 || nid == codec->afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004423 return power_state;
4424 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4425 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004426 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004427 return power_state;
4428 return AC_PWRST_D3;
4429}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004430EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004431
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004432/* mute all aamix inputs initially; parse up to the first leaves */
4433static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4434{
4435 int i, nums;
4436 const hda_nid_t *conn;
4437 bool has_amp;
4438
4439 nums = snd_hda_get_conn_list(codec, mix, &conn);
4440 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4441 for (i = 0; i < nums; i++) {
4442 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004443 update_amp(codec, mix, HDA_INPUT, i,
4444 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004445 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004446 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4447 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004448 }
4449}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004450
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004451/**
4452 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4453 * set up the hda_gen_spec
4454 * @codec: the HDA codec
4455 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004456 *
4457 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004458 * or a negative error code
4459 */
4460int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004461 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004462{
4463 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004464 int err;
4465
Takashi Iwai1c70a582013-01-11 17:48:22 +01004466 parse_user_hints(codec);
4467
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004468 if (spec->mixer_nid && !spec->mixer_merge_nid)
4469 spec->mixer_merge_nid = spec->mixer_nid;
4470
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004471 if (cfg != &spec->autocfg) {
4472 spec->autocfg = *cfg;
4473 cfg = &spec->autocfg;
4474 }
4475
Takashi Iwai98bd1112013-03-22 14:53:50 +01004476 if (!spec->main_out_badness)
4477 spec->main_out_badness = &hda_main_out_badness;
4478 if (!spec->extra_out_badness)
4479 spec->extra_out_badness = &hda_extra_out_badness;
4480
David Henningsson6fc4cb92013-01-16 15:58:45 +01004481 fill_all_dac_nids(codec);
4482
Takashi Iwai352f7f92012-12-19 12:52:06 +01004483 if (!cfg->line_outs) {
4484 if (cfg->dig_outs || cfg->dig_in_pin) {
4485 spec->multiout.max_channels = 2;
4486 spec->no_analog = 1;
4487 goto dig_only;
4488 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004489 if (!cfg->num_inputs && !cfg->dig_in_pin)
4490 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004491 }
4492
4493 if (!spec->no_primary_hp &&
4494 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4495 cfg->line_outs <= cfg->hp_outs) {
4496 /* use HP as primary out */
4497 cfg->speaker_outs = cfg->line_outs;
4498 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4499 sizeof(cfg->speaker_pins));
4500 cfg->line_outs = cfg->hp_outs;
4501 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4502 cfg->hp_outs = 0;
4503 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4504 cfg->line_out_type = AUTO_PIN_HP_OUT;
4505 }
4506
4507 err = parse_output_paths(codec);
4508 if (err < 0)
4509 return err;
4510 err = create_multi_channel_mode(codec);
4511 if (err < 0)
4512 return err;
4513 err = create_multi_out_ctls(codec, cfg);
4514 if (err < 0)
4515 return err;
4516 err = create_hp_out_ctls(codec);
4517 if (err < 0)
4518 return err;
4519 err = create_speaker_out_ctls(codec);
4520 if (err < 0)
4521 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004522 err = create_indep_hp_ctls(codec);
4523 if (err < 0)
4524 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004525 err = create_loopback_mixing_ctl(codec);
4526 if (err < 0)
4527 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004528 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004529 if (err < 0)
4530 return err;
4531 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004532 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004533 return err;
4534
Takashi Iwaia07a9492013-01-07 16:44:06 +01004535 spec->const_channel_count = spec->ext_channel_count;
4536 /* check the multiple speaker and headphone pins */
4537 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4538 spec->const_channel_count = max(spec->const_channel_count,
4539 cfg->speaker_outs * 2);
4540 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4541 spec->const_channel_count = max(spec->const_channel_count,
4542 cfg->hp_outs * 2);
4543 spec->multiout.max_channels = max(spec->ext_channel_count,
4544 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004545
4546 err = check_auto_mute_availability(codec);
4547 if (err < 0)
4548 return err;
4549
4550 err = check_dyn_adc_switch(codec);
4551 if (err < 0)
4552 return err;
4553
Takashi Iwai967303d2013-02-19 17:12:42 +01004554 err = check_auto_mic_availability(codec);
4555 if (err < 0)
4556 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004557
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004558 /* add stereo mix if available and not enabled yet */
4559 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01004560 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4561 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004562 err = parse_capture_source(codec, spec->mixer_nid,
4563 CFG_IDX_MIX, spec->num_all_adcs,
4564 "Stereo Mix", 0);
4565 if (err < 0)
4566 return err;
4567 }
4568
4569
Takashi Iwai352f7f92012-12-19 12:52:06 +01004570 err = create_capture_mixers(codec);
4571 if (err < 0)
4572 return err;
4573
4574 err = parse_mic_boost(codec);
4575 if (err < 0)
4576 return err;
4577
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004578 /* create "Headphone Mic Jack Mode" if no input selection is
4579 * available (or user specifies add_jack_modes hint)
4580 */
4581 if (spec->hp_mic_pin &&
4582 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4583 spec->add_jack_modes)) {
4584 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4585 if (err < 0)
4586 return err;
4587 }
4588
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004589 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004590 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4591 err = create_out_jack_modes(codec, cfg->line_outs,
4592 cfg->line_out_pins);
4593 if (err < 0)
4594 return err;
4595 }
4596 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4597 err = create_out_jack_modes(codec, cfg->hp_outs,
4598 cfg->hp_pins);
4599 if (err < 0)
4600 return err;
4601 }
4602 }
4603
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004604 /* mute all aamix input initially */
4605 if (spec->mixer_nid)
4606 mute_all_mixer_nid(codec, spec->mixer_nid);
4607
Takashi Iwai352f7f92012-12-19 12:52:06 +01004608 dig_only:
4609 parse_digital(codec);
4610
Takashi Iwai55196ff2013-01-24 17:32:56 +01004611 if (spec->power_down_unused)
4612 codec->power_filter = snd_hda_gen_path_power_filter;
4613
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004614 if (!spec->no_analog && spec->beep_nid) {
4615 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4616 if (err < 0)
4617 return err;
4618 }
4619
Takashi Iwai352f7f92012-12-19 12:52:06 +01004620 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004622EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
4624
4625/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004626 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004628
4629/* slave controls for virtual master */
4630static const char * const slave_pfxs[] = {
4631 "Front", "Surround", "Center", "LFE", "Side",
4632 "Headphone", "Speaker", "Mono", "Line Out",
4633 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004634 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4635 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02004636 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004637 NULL,
4638};
4639
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004640/**
4641 * snd_hda_gen_build_controls - Build controls from the parsed results
4642 * @codec: the HDA codec
4643 *
4644 * Pass this to build_controls patch_ops.
4645 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004646int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004648 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
Takashi Iwai36502d02012-12-19 15:15:10 +01004651 if (spec->kctls.used) {
4652 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4653 if (err < 0)
4654 return err;
4655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
Takashi Iwai352f7f92012-12-19 12:52:06 +01004657 if (spec->multiout.dig_out_nid) {
4658 err = snd_hda_create_dig_out_ctls(codec,
4659 spec->multiout.dig_out_nid,
4660 spec->multiout.dig_out_nid,
4661 spec->pcm_rec[1].pcm_type);
4662 if (err < 0)
4663 return err;
4664 if (!spec->no_analog) {
4665 err = snd_hda_create_spdif_share_sw(codec,
4666 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 if (err < 0)
4668 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004669 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 }
4671 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004672 if (spec->dig_in_nid) {
4673 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4674 if (err < 0)
4675 return err;
4676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Takashi Iwai352f7f92012-12-19 12:52:06 +01004678 /* if we have no master control, let's create it */
4679 if (!spec->no_analog &&
4680 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004681 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004682 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004683 "Playback Volume");
4684 if (err < 0)
4685 return err;
4686 }
4687 if (!spec->no_analog &&
4688 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4689 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4690 NULL, slave_pfxs,
4691 "Playback Switch",
4692 true, &spec->vmaster_mute.sw_kctl);
4693 if (err < 0)
4694 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02004695 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01004696 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4697 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02004698 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4699 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
Takashi Iwai352f7f92012-12-19 12:52:06 +01004702 free_kctls(spec); /* no longer needed */
4703
Takashi Iwai352f7f92012-12-19 12:52:06 +01004704 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4705 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 return err;
4707
4708 return 0;
4709}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004710EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004711
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
4713/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004714 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004717static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4718 struct hda_codec *codec,
4719 struct snd_pcm_substream *substream,
4720 int action)
4721{
4722 struct hda_gen_spec *spec = codec->spec;
4723 if (spec->pcm_playback_hook)
4724 spec->pcm_playback_hook(hinfo, codec, substream, action);
4725}
4726
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004727static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4728 struct hda_codec *codec,
4729 struct snd_pcm_substream *substream,
4730 int action)
4731{
4732 struct hda_gen_spec *spec = codec->spec;
4733 if (spec->pcm_capture_hook)
4734 spec->pcm_capture_hook(hinfo, codec, substream, action);
4735}
4736
Takashi Iwai352f7f92012-12-19 12:52:06 +01004737/*
4738 * Analog playback callbacks
4739 */
4740static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4741 struct hda_codec *codec,
4742 struct snd_pcm_substream *substream)
4743{
4744 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004745 int err;
4746
4747 mutex_lock(&spec->pcm_mutex);
4748 err = snd_hda_multi_out_analog_open(codec,
4749 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004750 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004751 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004752 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004753 call_pcm_playback_hook(hinfo, codec, substream,
4754 HDA_GEN_PCM_ACT_OPEN);
4755 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004756 mutex_unlock(&spec->pcm_mutex);
4757 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004758}
4759
4760static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004761 struct hda_codec *codec,
4762 unsigned int stream_tag,
4763 unsigned int format,
4764 struct snd_pcm_substream *substream)
4765{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004766 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004767 int err;
4768
4769 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4770 stream_tag, format, substream);
4771 if (!err)
4772 call_pcm_playback_hook(hinfo, codec, substream,
4773 HDA_GEN_PCM_ACT_PREPARE);
4774 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004775}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004776
Takashi Iwai352f7f92012-12-19 12:52:06 +01004777static int playback_pcm_cleanup(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;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004782 int err;
4783
4784 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4785 if (!err)
4786 call_pcm_playback_hook(hinfo, codec, substream,
4787 HDA_GEN_PCM_ACT_CLEANUP);
4788 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004789}
4790
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004791static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4792 struct hda_codec *codec,
4793 struct snd_pcm_substream *substream)
4794{
4795 struct hda_gen_spec *spec = codec->spec;
4796 mutex_lock(&spec->pcm_mutex);
4797 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004798 call_pcm_playback_hook(hinfo, codec, substream,
4799 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004800 mutex_unlock(&spec->pcm_mutex);
4801 return 0;
4802}
4803
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004804static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4805 struct hda_codec *codec,
4806 struct snd_pcm_substream *substream)
4807{
4808 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4809 return 0;
4810}
4811
4812static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4813 struct hda_codec *codec,
4814 unsigned int stream_tag,
4815 unsigned int format,
4816 struct snd_pcm_substream *substream)
4817{
4818 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4819 call_pcm_capture_hook(hinfo, codec, substream,
4820 HDA_GEN_PCM_ACT_PREPARE);
4821 return 0;
4822}
4823
4824static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4825 struct hda_codec *codec,
4826 struct snd_pcm_substream *substream)
4827{
4828 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4829 call_pcm_capture_hook(hinfo, codec, substream,
4830 HDA_GEN_PCM_ACT_CLEANUP);
4831 return 0;
4832}
4833
4834static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4835 struct hda_codec *codec,
4836 struct snd_pcm_substream *substream)
4837{
4838 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4839 return 0;
4840}
4841
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004842static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4843 struct hda_codec *codec,
4844 struct snd_pcm_substream *substream)
4845{
4846 struct hda_gen_spec *spec = codec->spec;
4847 int err = 0;
4848
4849 mutex_lock(&spec->pcm_mutex);
4850 if (!spec->indep_hp_enabled)
4851 err = -EBUSY;
4852 else
4853 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004854 call_pcm_playback_hook(hinfo, codec, substream,
4855 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004856 mutex_unlock(&spec->pcm_mutex);
4857 return err;
4858}
4859
4860static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4861 struct hda_codec *codec,
4862 struct snd_pcm_substream *substream)
4863{
4864 struct hda_gen_spec *spec = codec->spec;
4865 mutex_lock(&spec->pcm_mutex);
4866 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004867 call_pcm_playback_hook(hinfo, codec, substream,
4868 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004869 mutex_unlock(&spec->pcm_mutex);
4870 return 0;
4871}
4872
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004873static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4874 struct hda_codec *codec,
4875 unsigned int stream_tag,
4876 unsigned int format,
4877 struct snd_pcm_substream *substream)
4878{
4879 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4880 call_pcm_playback_hook(hinfo, codec, substream,
4881 HDA_GEN_PCM_ACT_PREPARE);
4882 return 0;
4883}
4884
4885static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4886 struct hda_codec *codec,
4887 struct snd_pcm_substream *substream)
4888{
4889 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4890 call_pcm_playback_hook(hinfo, codec, substream,
4891 HDA_GEN_PCM_ACT_CLEANUP);
4892 return 0;
4893}
4894
Takashi Iwai352f7f92012-12-19 12:52:06 +01004895/*
4896 * Digital out
4897 */
4898static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4899 struct hda_codec *codec,
4900 struct snd_pcm_substream *substream)
4901{
4902 struct hda_gen_spec *spec = codec->spec;
4903 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4904}
4905
4906static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4907 struct hda_codec *codec,
4908 unsigned int stream_tag,
4909 unsigned int format,
4910 struct snd_pcm_substream *substream)
4911{
4912 struct hda_gen_spec *spec = codec->spec;
4913 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4914 stream_tag, format, substream);
4915}
4916
4917static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4918 struct hda_codec *codec,
4919 struct snd_pcm_substream *substream)
4920{
4921 struct hda_gen_spec *spec = codec->spec;
4922 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4923}
4924
4925static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4926 struct hda_codec *codec,
4927 struct snd_pcm_substream *substream)
4928{
4929 struct hda_gen_spec *spec = codec->spec;
4930 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4931}
4932
4933/*
4934 * Analog capture
4935 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004936#define alt_capture_pcm_open capture_pcm_open
4937#define alt_capture_pcm_close capture_pcm_close
4938
Takashi Iwai352f7f92012-12-19 12:52:06 +01004939static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4940 struct hda_codec *codec,
4941 unsigned int stream_tag,
4942 unsigned int format,
4943 struct snd_pcm_substream *substream)
4944{
4945 struct hda_gen_spec *spec = codec->spec;
4946
4947 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004948 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004949 call_pcm_capture_hook(hinfo, codec, substream,
4950 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004951 return 0;
4952}
4953
Takashi Iwai352f7f92012-12-19 12:52:06 +01004954static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4955 struct hda_codec *codec,
4956 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004957{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004958 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004959
Takashi Iwai352f7f92012-12-19 12:52:06 +01004960 snd_hda_codec_cleanup_stream(codec,
4961 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004962 call_pcm_capture_hook(hinfo, codec, substream,
4963 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004964 return 0;
4965}
4966
Takashi Iwai352f7f92012-12-19 12:52:06 +01004967/*
4968 */
4969static const struct hda_pcm_stream pcm_analog_playback = {
4970 .substreams = 1,
4971 .channels_min = 2,
4972 .channels_max = 8,
4973 /* NID is set in build_pcms */
4974 .ops = {
4975 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004976 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004977 .prepare = playback_pcm_prepare,
4978 .cleanup = playback_pcm_cleanup
4979 },
4980};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
Takashi Iwai352f7f92012-12-19 12:52:06 +01004982static const struct hda_pcm_stream pcm_analog_capture = {
4983 .substreams = 1,
4984 .channels_min = 2,
4985 .channels_max = 2,
4986 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004987 .ops = {
4988 .open = capture_pcm_open,
4989 .close = capture_pcm_close,
4990 .prepare = capture_pcm_prepare,
4991 .cleanup = capture_pcm_cleanup
4992 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004993};
4994
4995static const struct hda_pcm_stream pcm_analog_alt_playback = {
4996 .substreams = 1,
4997 .channels_min = 2,
4998 .channels_max = 2,
4999 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005000 .ops = {
5001 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005002 .close = alt_playback_pcm_close,
5003 .prepare = alt_playback_pcm_prepare,
5004 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005005 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005006};
5007
5008static const struct hda_pcm_stream pcm_analog_alt_capture = {
5009 .substreams = 2, /* can be overridden */
5010 .channels_min = 2,
5011 .channels_max = 2,
5012 /* NID is set in build_pcms */
5013 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005014 .open = alt_capture_pcm_open,
5015 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005016 .prepare = alt_capture_pcm_prepare,
5017 .cleanup = alt_capture_pcm_cleanup
5018 },
5019};
5020
5021static const struct hda_pcm_stream pcm_digital_playback = {
5022 .substreams = 1,
5023 .channels_min = 2,
5024 .channels_max = 2,
5025 /* NID is set in build_pcms */
5026 .ops = {
5027 .open = dig_playback_pcm_open,
5028 .close = dig_playback_pcm_close,
5029 .prepare = dig_playback_pcm_prepare,
5030 .cleanup = dig_playback_pcm_cleanup
5031 },
5032};
5033
5034static const struct hda_pcm_stream pcm_digital_capture = {
5035 .substreams = 1,
5036 .channels_min = 2,
5037 .channels_max = 2,
5038 /* NID is set in build_pcms */
5039};
5040
5041/* Used by build_pcms to flag that a PCM has no playback stream */
5042static const struct hda_pcm_stream pcm_null_stream = {
5043 .substreams = 0,
5044 .channels_min = 0,
5045 .channels_max = 0,
5046};
5047
5048/*
5049 * dynamic changing ADC PCM streams
5050 */
5051static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5052{
5053 struct hda_gen_spec *spec = codec->spec;
5054 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5055
5056 if (spec->cur_adc && spec->cur_adc != new_adc) {
5057 /* stream is running, let's swap the current ADC */
5058 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5059 spec->cur_adc = new_adc;
5060 snd_hda_codec_setup_stream(codec, new_adc,
5061 spec->cur_adc_stream_tag, 0,
5062 spec->cur_adc_format);
5063 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005065 return false;
5066}
5067
5068/* analog capture with dynamic dual-adc changes */
5069static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5070 struct hda_codec *codec,
5071 unsigned int stream_tag,
5072 unsigned int format,
5073 struct snd_pcm_substream *substream)
5074{
5075 struct hda_gen_spec *spec = codec->spec;
5076 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5077 spec->cur_adc_stream_tag = stream_tag;
5078 spec->cur_adc_format = format;
5079 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5080 return 0;
5081}
5082
5083static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5084 struct hda_codec *codec,
5085 struct snd_pcm_substream *substream)
5086{
5087 struct hda_gen_spec *spec = codec->spec;
5088 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5089 spec->cur_adc = 0;
5090 return 0;
5091}
5092
5093static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5094 .substreams = 1,
5095 .channels_min = 2,
5096 .channels_max = 2,
5097 .nid = 0, /* fill later */
5098 .ops = {
5099 .prepare = dyn_adc_capture_pcm_prepare,
5100 .cleanup = dyn_adc_capture_pcm_cleanup
5101 },
5102};
5103
Takashi Iwaif873e532012-12-20 16:58:39 +01005104static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5105 const char *chip_name)
5106{
5107 char *p;
5108
5109 if (*str)
5110 return;
5111 strlcpy(str, chip_name, len);
5112
5113 /* drop non-alnum chars after a space */
5114 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5115 if (!isalnum(p[1])) {
5116 *p = 0;
5117 break;
5118 }
5119 }
5120 strlcat(str, sfx, len);
5121}
5122
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005123/**
5124 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5125 * @codec: the HDA codec
5126 *
5127 * Pass this to build_pcms patch_ops.
5128 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005129int snd_hda_gen_build_pcms(struct hda_codec *codec)
5130{
5131 struct hda_gen_spec *spec = codec->spec;
5132 struct hda_pcm *info = spec->pcm_rec;
5133 const struct hda_pcm_stream *p;
5134 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
5136 codec->num_pcms = 1;
5137 codec->pcm_info = info;
5138
Takashi Iwai352f7f92012-12-19 12:52:06 +01005139 if (spec->no_analog)
5140 goto skip_analog;
5141
Takashi Iwaif873e532012-12-20 16:58:39 +01005142 fill_pcm_stream_name(spec->stream_name_analog,
5143 sizeof(spec->stream_name_analog),
5144 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005145 info->name = spec->stream_name_analog;
5146
5147 if (spec->multiout.num_dacs > 0) {
5148 p = spec->stream_analog_playback;
5149 if (!p)
5150 p = &pcm_analog_playback;
5151 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5152 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
5153 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5154 spec->multiout.max_channels;
5155 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5156 spec->autocfg.line_outs == 2)
5157 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5158 snd_pcm_2_1_chmaps;
5159 }
5160 if (spec->num_adc_nids) {
5161 p = spec->stream_analog_capture;
5162 if (!p) {
5163 if (spec->dyn_adc_switch)
5164 p = &dyn_adc_pcm_analog_capture;
5165 else
5166 p = &pcm_analog_capture;
5167 }
5168 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5169 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
5170 }
5171
Takashi Iwai352f7f92012-12-19 12:52:06 +01005172 skip_analog:
5173 /* SPDIF for stream index #1 */
5174 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005175 fill_pcm_stream_name(spec->stream_name_digital,
5176 sizeof(spec->stream_name_digital),
5177 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005178 codec->num_pcms = 2;
5179 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5180 info = spec->pcm_rec + 1;
5181 info->name = spec->stream_name_digital;
5182 if (spec->dig_out_type)
5183 info->pcm_type = spec->dig_out_type;
5184 else
5185 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5186 if (spec->multiout.dig_out_nid) {
5187 p = spec->stream_digital_playback;
5188 if (!p)
5189 p = &pcm_digital_playback;
5190 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5191 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
5192 }
5193 if (spec->dig_in_nid) {
5194 p = spec->stream_digital_capture;
5195 if (!p)
5196 p = &pcm_digital_capture;
5197 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5198 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
5199 }
5200 }
5201
5202 if (spec->no_analog)
5203 return 0;
5204
5205 /* If the use of more than one ADC is requested for the current
5206 * model, configure a second analog capture-only PCM.
5207 */
5208 have_multi_adcs = (spec->num_adc_nids > 1) &&
5209 !spec->dyn_adc_switch && !spec->auto_mic;
5210 /* Additional Analaog capture for index #2 */
5211 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005212 fill_pcm_stream_name(spec->stream_name_alt_analog,
5213 sizeof(spec->stream_name_alt_analog),
5214 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005215 codec->num_pcms = 3;
5216 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01005217 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005218 if (spec->alt_dac_nid) {
5219 p = spec->stream_analog_alt_playback;
5220 if (!p)
5221 p = &pcm_analog_alt_playback;
5222 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5223 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5224 spec->alt_dac_nid;
5225 } else {
5226 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5227 pcm_null_stream;
5228 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5229 }
5230 if (have_multi_adcs) {
5231 p = spec->stream_analog_alt_capture;
5232 if (!p)
5233 p = &pcm_analog_alt_capture;
5234 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5235 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5236 spec->adc_nids[1];
5237 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5238 spec->num_adc_nids - 1;
5239 } else {
5240 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5241 pcm_null_stream;
5242 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 }
5245
5246 return 0;
5247}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005248EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005249
5250
5251/*
5252 * Standard auto-parser initializations
5253 */
5254
Takashi Iwaid4156932013-01-07 10:08:02 +01005255/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005256static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005257{
5258 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005259 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005260
Takashi Iwai196c17662013-01-04 15:01:40 +01005261 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005262 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005263 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005264 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005265 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005266 snd_hda_activate_path(codec, path, path->active,
5267 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005268 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005269}
5270
5271/* initialize primary output paths */
5272static void init_multi_out(struct hda_codec *codec)
5273{
5274 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005275 int i;
5276
Takashi Iwaid4156932013-01-07 10:08:02 +01005277 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005278 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005279}
5280
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005281
Takashi Iwai2c12c302013-01-10 09:33:29 +01005282static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005283{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005284 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005285
Takashi Iwaid4156932013-01-07 10:08:02 +01005286 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005287 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005288}
5289
5290/* initialize hp and speaker paths */
5291static void init_extra_out(struct hda_codec *codec)
5292{
5293 struct hda_gen_spec *spec = codec->spec;
5294
5295 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005296 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005297 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5298 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005299 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005300}
5301
5302/* initialize multi-io paths */
5303static void init_multi_io(struct hda_codec *codec)
5304{
5305 struct hda_gen_spec *spec = codec->spec;
5306 int i;
5307
5308 for (i = 0; i < spec->multi_ios; i++) {
5309 hda_nid_t pin = spec->multi_io[i].pin;
5310 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005311 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005312 if (!path)
5313 continue;
5314 if (!spec->multi_io[i].ctl_in)
5315 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005316 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005317 snd_hda_activate_path(codec, path, path->active,
5318 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005319 }
5320}
5321
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005322static void init_aamix_paths(struct hda_codec *codec)
5323{
5324 struct hda_gen_spec *spec = codec->spec;
5325
5326 if (!spec->have_aamix_ctl)
5327 return;
5328 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5329 spec->aamix_out_paths[0],
5330 spec->autocfg.line_out_type);
5331 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5332 spec->aamix_out_paths[1],
5333 AUTO_PIN_HP_OUT);
5334 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5335 spec->aamix_out_paths[2],
5336 AUTO_PIN_SPEAKER_OUT);
5337}
5338
Takashi Iwai352f7f92012-12-19 12:52:06 +01005339/* set up input pins and loopback paths */
5340static void init_analog_input(struct hda_codec *codec)
5341{
5342 struct hda_gen_spec *spec = codec->spec;
5343 struct auto_pin_cfg *cfg = &spec->autocfg;
5344 int i;
5345
5346 for (i = 0; i < cfg->num_inputs; i++) {
5347 hda_nid_t nid = cfg->inputs[i].pin;
5348 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005349 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005350
5351 /* init loopback inputs */
5352 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005353 resume_path_from_idx(codec, spec->loopback_paths[i]);
5354 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005355 }
5356 }
5357}
5358
5359/* initialize ADC paths */
5360static void init_input_src(struct hda_codec *codec)
5361{
5362 struct hda_gen_spec *spec = codec->spec;
5363 struct hda_input_mux *imux = &spec->input_mux;
5364 struct nid_path *path;
5365 int i, c, nums;
5366
5367 if (spec->dyn_adc_switch)
5368 nums = 1;
5369 else
5370 nums = spec->num_adc_nids;
5371
5372 for (c = 0; c < nums; c++) {
5373 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005374 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005375 if (path) {
5376 bool active = path->active;
5377 if (i == spec->cur_mux[c])
5378 active = true;
5379 snd_hda_activate_path(codec, path, active, false);
5380 }
5381 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005382 if (spec->hp_mic)
5383 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005384 }
5385
Takashi Iwai352f7f92012-12-19 12:52:06 +01005386 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005387 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005388}
5389
5390/* set right pin controls for digital I/O */
5391static void init_digital(struct hda_codec *codec)
5392{
5393 struct hda_gen_spec *spec = codec->spec;
5394 int i;
5395 hda_nid_t pin;
5396
Takashi Iwaid4156932013-01-07 10:08:02 +01005397 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005398 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005399 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005400 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005401 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005402 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005403 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005404}
5405
Takashi Iwai973e4972012-12-20 15:16:09 +01005406/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5407 * invalid unsol tags by some reason
5408 */
5409static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5410{
5411 int i;
5412
5413 for (i = 0; i < codec->init_pins.used; i++) {
5414 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5415 hda_nid_t nid = pin->nid;
5416 if (is_jack_detectable(codec, nid) &&
5417 !snd_hda_jack_tbl_get(codec, nid))
5418 snd_hda_codec_update_cache(codec, nid, 0,
5419 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5420 }
5421}
5422
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005423/**
5424 * snd_hda_gen_init - initialize the generic spec
5425 * @codec: the HDA codec
5426 *
5427 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005428 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005429int snd_hda_gen_init(struct hda_codec *codec)
5430{
5431 struct hda_gen_spec *spec = codec->spec;
5432
5433 if (spec->init_hook)
5434 spec->init_hook(codec);
5435
5436 snd_hda_apply_verbs(codec);
5437
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005438 codec->cached_write = 1;
5439
Takashi Iwai352f7f92012-12-19 12:52:06 +01005440 init_multi_out(codec);
5441 init_extra_out(codec);
5442 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005443 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005444 init_analog_input(codec);
5445 init_input_src(codec);
5446 init_digital(codec);
5447
Takashi Iwai973e4972012-12-20 15:16:09 +01005448 clear_unsol_on_unused_pins(codec);
5449
Takashi Iwai352f7f92012-12-19 12:52:06 +01005450 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005451 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005452
Takashi Iwaidc870f32013-01-22 15:24:30 +01005453 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005454
Takashi Iwai352f7f92012-12-19 12:52:06 +01005455 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5456 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5457
5458 hda_call_check_power_status(codec, 0x01);
5459 return 0;
5460}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005461EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005462
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005463/**
5464 * snd_hda_gen_free - free the generic spec
5465 * @codec: the HDA codec
5466 *
5467 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005468 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005469void snd_hda_gen_free(struct hda_codec *codec)
5470{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005471 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005472 snd_hda_gen_spec_free(codec->spec);
5473 kfree(codec->spec);
5474 codec->spec = NULL;
5475}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005476EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005477
5478#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005479/**
5480 * snd_hda_gen_check_power_status - check the loopback power save state
5481 * @codec: the HDA codec
5482 * @nid: NID to inspect
5483 *
5484 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005485 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005486int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5487{
5488 struct hda_gen_spec *spec = codec->spec;
5489 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5490}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005491EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005492#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005493
5494
5495/*
5496 * the generic codec support
5497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498
Takashi Iwai352f7f92012-12-19 12:52:06 +01005499static const struct hda_codec_ops generic_patch_ops = {
5500 .build_controls = snd_hda_gen_build_controls,
5501 .build_pcms = snd_hda_gen_build_pcms,
5502 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005503 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005504 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005505#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005506 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005507#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508};
5509
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005510/**
5511 * snd_hda_parse_generic_codec - Generic codec parser
5512 * @codec: the HDA codec
5513 *
5514 * This should be called from the HDA codec core.
5515 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516int snd_hda_parse_generic_codec(struct hda_codec *codec)
5517{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005518 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 int err;
5520
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005521 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005522 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005524 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005527 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5528 if (err < 0)
5529 return err;
5530
5531 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005532 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 goto error;
5534
5535 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 return 0;
5537
Takashi Iwai352f7f92012-12-19 12:52:06 +01005538error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005539 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 return err;
5541}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005542EXPORT_SYMBOL_GPL(snd_hda_parse_generic_codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005543
5544MODULE_LICENSE("GPL");
5545MODULE_DESCRIPTION("Generic HD-audio codec parser");