blob: 28e265a88383d3266b7fca8a4ae80dd2146dac0a [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;
Takashi Iwai967b1302015-03-20 18:21:03 +0100143 val = snd_hda_get_bool_hint(codec, "power_save_node");
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100144 if (val >= 0)
Takashi Iwai967b1302015-03-20 18:21:03 +0100145 codec->power_save_node = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100146
Takashi Iwaif72706b2013-01-16 18:20:07 +0100147 val = snd_hda_get_bool_hint(codec, "auto_mute");
148 if (val >= 0)
149 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100150 val = snd_hda_get_bool_hint(codec, "auto_mic");
151 if (val >= 0)
152 spec->suppress_auto_mic = !val;
153 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
154 if (val >= 0)
155 spec->line_in_auto_switch = !!val;
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200156 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
157 if (val >= 0)
158 spec->auto_mute_via_amp = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100159 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
160 if (val >= 0)
161 spec->need_dac_fix = !!val;
162 val = snd_hda_get_bool_hint(codec, "primary_hp");
163 if (val >= 0)
164 spec->no_primary_hp = !val;
Takashi Iwaida96fb52013-07-29 16:54:36 +0200165 val = snd_hda_get_bool_hint(codec, "multi_io");
166 if (val >= 0)
167 spec->no_multi_io = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100168 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
169 if (val >= 0)
170 spec->multi_cap_vol = !!val;
171 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
172 if (val >= 0)
173 spec->inv_dmic_split = !!val;
174 val = snd_hda_get_bool_hint(codec, "indep_hp");
175 if (val >= 0)
176 spec->indep_hp = !!val;
177 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
178 if (val >= 0)
179 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100180 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100181 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
182 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100183 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100184 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
185 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100186 spec->add_jack_modes = !!val;
187 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
188 if (val >= 0)
189 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100190 val = snd_hda_get_bool_hint(codec, "power_down_unused");
191 if (val >= 0)
192 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100193 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
194 if (val >= 0)
195 spec->hp_mic = !!val;
196 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
197 if (val >= 0)
198 spec->suppress_hp_mic_detect = !val;
Takashi Iwai74803162017-04-10 17:37:34 +0200199 val = snd_hda_get_bool_hint(codec, "vmaster");
200 if (val >= 0)
201 spec->suppress_vmaster = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100202
203 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
204 spec->mixer_nid = val;
205}
206
207/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100208 * pin control value accesses
209 */
210
211#define update_pin_ctl(codec, pin, val) \
212 snd_hda_codec_update_cache(codec, pin, 0, \
213 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
214
215/* restore the pinctl based on the cached value */
216static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
217{
218 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
219}
220
221/* set the pinctl target value and write it if requested */
222static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
223 unsigned int val, bool do_write)
224{
225 if (!pin)
226 return;
227 val = snd_hda_correct_pin_ctl(codec, pin, val);
228 snd_hda_codec_set_pin_target(codec, pin, val);
229 if (do_write)
230 update_pin_ctl(codec, pin, val);
231}
232
233/* set pinctl target values for all given pins */
234static void set_pin_targets(struct hda_codec *codec, int num_pins,
235 hda_nid_t *pins, unsigned int val)
236{
237 int i;
238 for (i = 0; i < num_pins; i++)
239 set_pin_target(codec, pins[i], val, false);
240}
241
242/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100243 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100246/* return the position of NID in the list, or -1 if not found */
247static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
248{
249 int i;
250 for (i = 0; i < nums; i++)
251 if (list[i] == nid)
252 return i;
253 return -1;
254}
255
256/* return true if the given NID is contained in the path */
257static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
258{
259 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
260}
261
Takashi Iwaif5172a72013-01-04 13:19:55 +0100262static struct nid_path *get_nid_path(struct hda_codec *codec,
263 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100264 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100266 struct hda_gen_spec *spec = codec->spec;
267 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Takashi Iwai352f7f92012-12-19 12:52:06 +0100269 for (i = 0; i < spec->paths.used; i++) {
270 struct nid_path *path = snd_array_elem(&spec->paths, i);
271 if (path->depth <= 0)
272 continue;
273 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100274 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100275 if (!anchor_nid ||
276 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
277 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100278 return path;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281 return NULL;
282}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100283
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100284/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100285 * snd_hda_get_path_idx - get the index number corresponding to the path
286 * instance
287 * @codec: the HDA codec
288 * @path: nid_path object
289 *
290 * The returned index starts from 1, i.e. the actual array index with offset 1,
291 * and zero is handled as an invalid path
Takashi Iwai196c17662013-01-04 15:01:40 +0100292 */
293int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
294{
295 struct hda_gen_spec *spec = codec->spec;
296 struct nid_path *array = spec->paths.list;
297 ssize_t idx;
298
299 if (!spec->paths.used)
300 return 0;
301 idx = path - array;
302 if (idx < 0 || idx >= spec->paths.used)
303 return 0;
304 return idx + 1;
305}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100306EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100307
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100308/**
309 * snd_hda_get_path_from_idx - get the path instance corresponding to the
310 * given index number
311 * @codec: the HDA codec
312 * @idx: the path index
313 */
Takashi Iwai196c17662013-01-04 15:01:40 +0100314struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
315{
316 struct hda_gen_spec *spec = codec->spec;
317
318 if (idx <= 0 || idx > spec->paths.used)
319 return NULL;
320 return snd_array_elem(&spec->paths, idx - 1);
321}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100322EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100323
Takashi Iwai352f7f92012-12-19 12:52:06 +0100324/* check whether the given DAC is already found in any existing paths */
325static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
326{
327 struct hda_gen_spec *spec = codec->spec;
328 int i;
329
330 for (i = 0; i < spec->paths.used; i++) {
331 struct nid_path *path = snd_array_elem(&spec->paths, i);
332 if (path->path[0] == nid)
333 return true;
334 }
335 return false;
336}
337
338/* check whether the given two widgets can be connected */
339static bool is_reachable_path(struct hda_codec *codec,
340 hda_nid_t from_nid, hda_nid_t to_nid)
341{
342 if (!from_nid || !to_nid)
343 return false;
344 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
345}
346
347/* nid, dir and idx */
348#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
349
350/* check whether the given ctl is already assigned in any path elements */
351static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
352{
353 struct hda_gen_spec *spec = codec->spec;
354 int i;
355
356 val &= AMP_VAL_COMPARE_MASK;
357 for (i = 0; i < spec->paths.used; i++) {
358 struct nid_path *path = snd_array_elem(&spec->paths, i);
359 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
360 return true;
361 }
362 return false;
363}
364
365/* check whether a control with the given (nid, dir, idx) was assigned */
366static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100367 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100368{
369 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100370 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100371}
372
Takashi Iwai4e76a882014-02-25 12:21:03 +0100373static void print_nid_path(struct hda_codec *codec,
374 const char *pfx, struct nid_path *path)
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100375{
376 char buf[40];
Joe Perchesd82353e2014-07-05 13:02:02 -0700377 char *pos = buf;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100378 int i;
379
Joe Perchesd82353e2014-07-05 13:02:02 -0700380 *pos = 0;
381 for (i = 0; i < path->depth; i++)
382 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
383 pos != buf ? ":" : "",
384 path->path[i]);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100385
Joe Perchesd82353e2014-07-05 13:02:02 -0700386 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100387}
388
Takashi Iwai352f7f92012-12-19 12:52:06 +0100389/* called recursively */
390static bool __parse_nid_path(struct hda_codec *codec,
391 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100392 int anchor_nid, struct nid_path *path,
393 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100394{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100395 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100396 int i, nums;
397
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100398 if (to_nid == anchor_nid)
399 anchor_nid = 0; /* anchor passed */
400 else if (to_nid == (hda_nid_t)(-anchor_nid))
401 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100402
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100403 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100404 for (i = 0; i < nums; i++) {
405 if (conn[i] != from_nid) {
406 /* special case: when from_nid is 0,
407 * try to find an empty DAC
408 */
409 if (from_nid ||
410 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
411 is_dac_already_used(codec, conn[i]))
412 continue;
413 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100414 /* anchor is not requested or already passed? */
415 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100416 goto found;
417 }
418 if (depth >= MAX_NID_PATH_DEPTH)
419 return false;
420 for (i = 0; i < nums; i++) {
421 unsigned int type;
422 type = get_wcaps_type(get_wcaps(codec, conn[i]));
423 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
424 type == AC_WID_PIN)
425 continue;
426 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100427 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100428 goto found;
429 }
430 return false;
431
432 found:
433 path->path[path->depth] = conn[i];
434 path->idx[path->depth + 1] = i;
435 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
436 path->multi[path->depth + 1] = 1;
437 path->depth++;
438 return true;
439}
440
Takashi Iwaic4a58c32015-12-08 11:48:39 +0100441/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100442 * snd_hda_parse_nid_path - parse the widget path from the given nid to
443 * the target nid
444 * @codec: the HDA codec
445 * @from_nid: the NID where the path start from
446 * @to_nid: the NID where the path ends at
447 * @anchor_nid: the anchor indication
448 * @path: the path object to store the result
449 *
450 * Returns true if a matching path is found.
451 *
452 * The parsing behavior depends on parameters:
Takashi Iwai352f7f92012-12-19 12:52:06 +0100453 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100454 * when @anchor_nid is set to a positive value, only paths through the widget
455 * with the given value are evaluated.
456 * when @anchor_nid is set to a negative value, paths through the widget
457 * with the negative of given value are excluded, only other paths are chosen.
458 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100459 */
Takashi Iwaic4a58c32015-12-08 11:48:39 +0100460static bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100461 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100462 struct nid_path *path)
463{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100464 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100465 path->path[path->depth] = to_nid;
466 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100467 return true;
468 }
469 return false;
470}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100472/**
473 * snd_hda_add_new_path - parse the path between the given NIDs and
474 * add to the path list
475 * @codec: the HDA codec
476 * @from_nid: the NID where the path start from
477 * @to_nid: the NID where the path ends at
478 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
479 *
480 * If no valid path is found, returns NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100482struct nid_path *
483snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100484 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100486 struct hda_gen_spec *spec = codec->spec;
487 struct nid_path *path;
488
489 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
490 return NULL;
491
Takashi Iwaif5172a72013-01-04 13:19:55 +0100492 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100493 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100494 if (path)
495 return path;
496
Takashi Iwai352f7f92012-12-19 12:52:06 +0100497 path = snd_array_new(&spec->paths);
498 if (!path)
499 return NULL;
500 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100501 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100502 return path;
503 /* push back */
504 spec->paths.used--;
505 return NULL;
506}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100507EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100508
Takashi Iwai980428c2013-01-09 09:28:20 +0100509/* clear the given path as invalid so that it won't be picked up later */
510static void invalidate_nid_path(struct hda_codec *codec, int idx)
511{
512 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
513 if (!path)
514 return;
515 memset(path, 0, sizeof(*path));
516}
517
Takashi Iwai36907392013-12-10 17:29:26 +0100518/* return a DAC if paired to the given pin by codec driver */
519static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
520{
521 struct hda_gen_spec *spec = codec->spec;
522 const hda_nid_t *list = spec->preferred_dacs;
523
524 if (!list)
525 return 0;
526 for (; *list; list += 2)
527 if (*list == pin)
528 return list[1];
529 return 0;
530}
531
Takashi Iwai352f7f92012-12-19 12:52:06 +0100532/* look for an empty DAC slot */
533static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
534 bool is_digital)
535{
536 struct hda_gen_spec *spec = codec->spec;
537 bool cap_digital;
538 int i;
539
540 for (i = 0; i < spec->num_all_dacs; i++) {
541 hda_nid_t nid = spec->all_dacs[i];
542 if (!nid || is_dac_already_used(codec, nid))
543 continue;
544 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
545 if (is_digital != cap_digital)
546 continue;
547 if (is_reachable_path(codec, nid, pin))
548 return nid;
549 }
550 return 0;
551}
552
553/* replace the channels in the composed amp value with the given number */
554static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
555{
556 val &= ~(0x3U << 16);
557 val |= chs << 16;
558 return val;
559}
560
David Henningsson99a55922013-01-16 15:58:44 +0100561static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
562 hda_nid_t nid2, int dir)
563{
564 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
565 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
566 return (query_amp_caps(codec, nid1, dir) ==
567 query_amp_caps(codec, nid2, dir));
568}
569
Takashi Iwai352f7f92012-12-19 12:52:06 +0100570/* look for a widget suitable for assigning a mute switch in the path */
571static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
572 struct nid_path *path)
573{
574 int i;
575
576 for (i = path->depth - 1; i >= 0; i--) {
577 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
578 return path->path[i];
579 if (i != path->depth - 1 && i != 0 &&
580 nid_has_mute(codec, path->path[i], HDA_INPUT))
581 return path->path[i];
582 }
583 return 0;
584}
585
586/* look for a widget suitable for assigning a volume ctl in the path */
587static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
588 struct nid_path *path)
589{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100590 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100591 int i;
592
593 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100594 hda_nid_t nid = path->path[i];
595 if ((spec->out_vol_mask >> nid) & 1)
596 continue;
597 if (nid_has_volume(codec, nid, HDA_OUTPUT))
598 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100599 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200600 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100604 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100606
607/* can have the amp-in capability? */
608static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100610 hda_nid_t nid = path->path[idx];
611 unsigned int caps = get_wcaps(codec, nid);
612 unsigned int type = get_wcaps_type(caps);
613
614 if (!(caps & AC_WCAP_IN_AMP))
615 return false;
616 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
617 return false;
618 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Takashi Iwai352f7f92012-12-19 12:52:06 +0100621/* can have the amp-out capability? */
622static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100624 hda_nid_t nid = path->path[idx];
625 unsigned int caps = get_wcaps(codec, nid);
626 unsigned int type = get_wcaps_type(caps);
627
628 if (!(caps & AC_WCAP_OUT_AMP))
629 return false;
630 if (type == AC_WID_PIN && !idx) /* only for output pins */
631 return false;
632 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Takashi Iwai352f7f92012-12-19 12:52:06 +0100635/* check whether the given (nid,dir,idx) is active */
636static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100637 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100639 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100640 int type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai352f7f92012-12-19 12:52:06 +0100641 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Takashi Iwai7639a062015-03-03 10:07:24 +0100643 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100644 return true;
645
Takashi Iwai352f7f92012-12-19 12:52:06 +0100646 for (n = 0; n < spec->paths.used; n++) {
647 struct nid_path *path = snd_array_elem(&spec->paths, n);
648 if (!path->active)
649 continue;
Takashi Iwai967b1302015-03-20 18:21:03 +0100650 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100651 if (!path->stream_enabled)
652 continue;
653 /* ignore unplugged paths except for DAC/ADC */
Takashi Iwai6b275b12015-03-20 18:11:05 +0100654 if (!(path->pin_enabled || path->pin_fixed) &&
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100655 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
656 continue;
657 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100658 for (i = 0; i < path->depth; i++) {
659 if (path->path[i] == nid) {
Takashi Iwai9d2b48f2015-08-24 10:45:27 +0200660 if (dir == HDA_OUTPUT || idx == -1 ||
661 path->idx[i] == idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100662 return true;
663 break;
664 }
665 }
666 }
667 return false;
668}
669
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200670/* check whether the NID is referred by any active paths */
671#define is_active_nid_for_any(codec, nid) \
Takashi Iwai9d2b48f2015-08-24 10:45:27 +0200672 is_active_nid(codec, nid, HDA_OUTPUT, -1)
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200673
Takashi Iwai352f7f92012-12-19 12:52:06 +0100674/* get the default amp value for the target state */
675static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100676 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100677{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100678 unsigned int val = 0;
679
Takashi Iwai352f7f92012-12-19 12:52:06 +0100680 if (caps & AC_AMPCAP_NUM_STEPS) {
681 /* set to 0dB */
682 if (enable)
683 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
684 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200685 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100686 if (!enable)
687 val |= HDA_AMP_MUTE;
688 }
689 return val;
690}
691
Takashi Iwaicc261732015-03-16 10:18:08 +0100692/* is this a stereo widget or a stereo-to-mono mix? */
693static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
694{
695 unsigned int wcaps = get_wcaps(codec, nid);
696 hda_nid_t conn;
697
698 if (wcaps & AC_WCAP_STEREO)
699 return true;
700 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
701 return false;
702 if (snd_hda_get_num_conns(codec, nid) != 1)
703 return false;
704 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
705 return false;
706 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
707}
708
Takashi Iwai352f7f92012-12-19 12:52:06 +0100709/* initialize the amp value (only at the first time) */
710static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
711{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100712 unsigned int caps = query_amp_caps(codec, nid, dir);
713 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwaief403ed2015-03-12 08:30:11 +0100714
Takashi Iwaicc261732015-03-16 10:18:08 +0100715 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100716 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
717 else
718 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
719}
720
721/* update the amp, doing in stereo or mono depending on NID */
722static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
723 unsigned int mask, unsigned int val)
724{
Takashi Iwaicc261732015-03-16 10:18:08 +0100725 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100726 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
727 mask, val);
728 else
729 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
730 mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100731}
732
Takashi Iwai8999bf02013-01-18 11:01:33 +0100733/* calculate amp value mask we can modify;
734 * if the given amp is controlled by mixers, don't touch it
735 */
736static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
737 hda_nid_t nid, int dir, int idx,
738 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100739{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100740 unsigned int mask = 0xff;
741
Takashi Iwaif69910d2013-08-08 09:32:37 +0200742 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100743 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
744 mask &= ~0x80;
745 }
746 if (caps & AC_AMPCAP_NUM_STEPS) {
747 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
748 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
749 mask &= ~0x7f;
750 }
751 return mask;
752}
753
754static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
755 int idx, int idx_to_check, bool enable)
756{
757 unsigned int caps;
758 unsigned int mask, val;
759
Takashi Iwai8999bf02013-01-18 11:01:33 +0100760 caps = query_amp_caps(codec, nid, dir);
761 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
762 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
763 if (!mask)
764 return;
765
766 val &= mask;
Takashi Iwaief403ed2015-03-12 08:30:11 +0100767 update_amp(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100768}
769
Takashi Iwaie7fdd522015-12-08 17:00:42 +0100770static void check_and_activate_amp(struct hda_codec *codec, hda_nid_t nid,
771 int dir, int idx, int idx_to_check,
772 bool enable)
773{
774 /* check whether the given amp is still used by others */
775 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
776 return;
777 activate_amp(codec, nid, dir, idx, idx_to_check, enable);
778}
779
Takashi Iwai352f7f92012-12-19 12:52:06 +0100780static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
781 int i, bool enable)
782{
783 hda_nid_t nid = path->path[i];
784 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwaie7fdd522015-12-08 17:00:42 +0100785 check_and_activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100786}
787
788static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
789 int i, bool enable, bool add_aamix)
790{
791 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100792 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100793 int n, nums, idx;
794 int type;
795 hda_nid_t nid = path->path[i];
796
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100797 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100798 type = get_wcaps_type(get_wcaps(codec, nid));
799 if (type == AC_WID_PIN ||
800 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
801 nums = 1;
802 idx = 0;
803 } else
804 idx = path->idx[i];
805
806 for (n = 0; n < nums; n++)
807 init_amp(codec, nid, HDA_INPUT, n);
808
Takashi Iwai352f7f92012-12-19 12:52:06 +0100809 /* here is a little bit tricky in comparison with activate_amp_out();
810 * when aa-mixer is available, we need to enable the path as well
811 */
812 for (n = 0; n < nums; n++) {
Takashi Iwaie7fdd522015-12-08 17:00:42 +0100813 if (n != idx) {
814 if (conn[n] != spec->mixer_merge_nid)
815 continue;
816 /* when aamix is disabled, force to off */
817 if (!add_aamix) {
818 activate_amp(codec, nid, HDA_INPUT, n, n, false);
819 continue;
820 }
821 }
822 check_and_activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824}
825
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100826/* sync power of each widget in the the given path */
827static hda_nid_t path_power_update(struct hda_codec *codec,
828 struct nid_path *path,
829 bool allow_powerdown)
830{
831 hda_nid_t nid, changed = 0;
Takashi Iwai50fd4982016-04-17 09:39:41 +0200832 int i, state, power;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100833
834 for (i = 0; i < path->depth; i++) {
835 nid = path->path[i];
Takashi Iwai2206dc92015-04-09 10:18:31 +0200836 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
837 continue;
Takashi Iwai7639a062015-03-03 10:07:24 +0100838 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100839 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100840 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
841 state = AC_PWRST_D0;
842 else
843 state = AC_PWRST_D3;
Takashi Iwai50fd4982016-04-17 09:39:41 +0200844 power = snd_hda_codec_read(codec, nid, 0,
845 AC_VERB_GET_POWER_STATE, 0);
846 if (power != (state | (state << 4))) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100847 snd_hda_codec_write(codec, nid, 0,
848 AC_VERB_SET_POWER_STATE, state);
849 changed = nid;
Takashi Iwai48f4b3a2015-05-20 06:49:37 +0200850 /* all known codecs seem to be capable to handl
851 * widgets state even in D3, so far.
852 * if any new codecs need to restore the widget
853 * states after D0 transition, call the function
854 * below.
855 */
856#if 0 /* disabled */
Takashi Iwaid545a572015-04-04 12:17:28 +0200857 if (state == AC_PWRST_D0)
858 snd_hdac_regmap_sync_node(&codec->core, nid);
Takashi Iwai48f4b3a2015-05-20 06:49:37 +0200859#endif
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100860 }
861 }
862 return changed;
863}
864
865/* do sync with the last power state change */
866static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
867{
868 if (nid) {
869 msleep(10);
870 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
871 }
872}
873
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100874/**
875 * snd_hda_activate_path - activate or deactivate the given path
876 * @codec: the HDA codec
877 * @path: the path to activate/deactivate
878 * @enable: flag to activate or not
879 * @add_aamix: enable the input from aamix NID
880 *
881 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100883void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
884 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100886 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100887 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Takashi Iwaic7cd0ef2015-08-24 10:52:06 +0200889 path->active = enable;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100890
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100891 /* make sure the widget is powered up */
Takashi Iwai967b1302015-03-20 18:21:03 +0100892 if (enable && (spec->power_down_unused || codec->power_save_node))
893 path_power_update(codec, path, codec->power_save_node);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100894
Takashi Iwai352f7f92012-12-19 12:52:06 +0100895 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100896 hda_nid_t nid = path->path[i];
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100897
Takashi Iwai352f7f92012-12-19 12:52:06 +0100898 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100899 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100900 AC_VERB_SET_CONNECT_SEL,
901 path->idx[i]);
902 if (has_amp_in(codec, path, i))
903 activate_amp_in(codec, path, i, enable, add_aamix);
904 if (has_amp_out(codec, path, i))
905 activate_amp_out(codec, path, i, enable);
906 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100907}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100908EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100909
Takashi Iwai55196ff2013-01-24 17:32:56 +0100910/* if the given path is inactive, put widgets into D3 (only if suitable) */
911static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
912{
913 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100914
Takashi Iwai967b1302015-03-20 18:21:03 +0100915 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
Takashi Iwai55196ff2013-01-24 17:32:56 +0100916 return;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100917 sync_power_state_change(codec, path_power_update(codec, path, true));
Takashi Iwai55196ff2013-01-24 17:32:56 +0100918}
919
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100920/* turn on/off EAPD on the given pin */
921static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
922{
923 struct hda_gen_spec *spec = codec->spec;
924 if (spec->own_eapd_ctl ||
925 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
926 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200927 if (spec->keep_eapd_on && !enable)
928 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100929 if (codec->inv_eapd)
930 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100931 snd_hda_codec_update_cache(codec, pin, 0,
932 AC_VERB_SET_EAPD_BTLENABLE,
933 enable ? 0x02 : 0x00);
934}
935
Takashi Iwai3e367f12013-01-23 17:07:23 +0100936/* re-initialize the path specified by the given path index */
937static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
938{
939 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
940 if (path)
941 snd_hda_activate_path(codec, path, path->active, false);
942}
943
Takashi Iwai352f7f92012-12-19 12:52:06 +0100944
945/*
946 * Helper functions for creating mixer ctl elements
947 */
948
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200949static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
950 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai698f5ee2017-05-10 12:13:45 +0200951static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
952 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200953static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
954 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200955
Takashi Iwai352f7f92012-12-19 12:52:06 +0100956enum {
957 HDA_CTL_WIDGET_VOL,
958 HDA_CTL_WIDGET_MUTE,
959 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100960};
961static const struct snd_kcontrol_new control_templates[] = {
962 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200963 /* only the put callback is replaced for handling the special mute */
964 {
965 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
966 .subdevice = HDA_SUBDEV_AMP_FLAG,
967 .info = snd_hda_mixer_amp_switch_info,
968 .get = snd_hda_mixer_amp_switch_get,
969 .put = hda_gen_mixer_mute_put, /* replaced */
970 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
971 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200972 {
973 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
974 .info = snd_hda_mixer_amp_switch_info,
Takashi Iwai698f5ee2017-05-10 12:13:45 +0200975 .get = hda_gen_bind_mute_get,
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200976 .put = hda_gen_bind_mute_put, /* replaced */
977 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
978 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100979};
980
981/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100982static struct snd_kcontrol_new *
983add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100984 int cidx, unsigned long val)
985{
986 struct snd_kcontrol_new *knew;
987
Takashi Iwai12c93df2012-12-19 14:38:33 +0100988 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100989 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100990 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100991 knew->index = cidx;
992 if (get_amp_nid_(val))
993 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
994 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100995 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100996}
997
998static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
999 const char *pfx, const char *dir,
1000 const char *sfx, int cidx, unsigned long val)
1001{
Takashi Iwai975cc022013-06-28 11:56:49 +02001002 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01001003 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01001004 if (!add_control(spec, type, name, cidx, val))
1005 return -ENOMEM;
1006 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001007}
1008
1009#define add_pb_vol_ctrl(spec, type, pfx, val) \
1010 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1011#define add_pb_sw_ctrl(spec, type, pfx, val) \
1012 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1013#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1014 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1015#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1016 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1017
1018static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1019 unsigned int chs, struct nid_path *path)
1020{
1021 unsigned int val;
1022 if (!path)
1023 return 0;
1024 val = path->ctls[NID_PATH_VOL_CTL];
1025 if (!val)
1026 return 0;
1027 val = amp_val_replace_channels(val, chs);
1028 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1029}
1030
1031/* return the channel bits suitable for the given path->ctls[] */
1032static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1033 int type)
1034{
1035 int chs = 1; /* mono (left only) */
1036 if (path) {
1037 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1038 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1039 chs = 3; /* stereo */
1040 }
1041 return chs;
1042}
1043
1044static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1045 struct nid_path *path)
1046{
1047 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1048 return add_vol_ctl(codec, pfx, cidx, chs, path);
1049}
1050
1051/* create a mute-switch for the given mixer widget;
1052 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1053 */
1054static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1055 unsigned int chs, struct nid_path *path)
1056{
1057 unsigned int val;
1058 int type = HDA_CTL_WIDGET_MUTE;
1059
1060 if (!path)
1061 return 0;
1062 val = path->ctls[NID_PATH_MUTE_CTL];
1063 if (!val)
1064 return 0;
1065 val = amp_val_replace_channels(val, chs);
1066 if (get_amp_direction_(val) == HDA_INPUT) {
1067 hda_nid_t nid = get_amp_nid_(val);
1068 int nums = snd_hda_get_num_conns(codec, nid);
1069 if (nums > 1) {
1070 type = HDA_CTL_BIND_MUTE;
1071 val |= nums << 19;
1072 }
1073 }
1074 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1075}
1076
1077static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1078 int cidx, struct nid_path *path)
1079{
1080 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1081 return add_sw_ctl(codec, pfx, cidx, chs, path);
1082}
1083
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001084/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001085static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1086 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001087{
1088 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1089 struct hda_gen_spec *spec = codec->spec;
1090
1091 if (spec->auto_mute_via_amp) {
1092 hda_nid_t nid = get_amp_nid(kcontrol);
1093 bool enabled = !((spec->mute_bits >> nid) & 1);
1094 ucontrol->value.integer.value[0] &= enabled;
1095 ucontrol->value.integer.value[1] &= enabled;
1096 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001097}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001098
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001099static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1100 struct snd_ctl_elem_value *ucontrol)
1101{
1102 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001103 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1104}
1105
Takashi Iwai698f5ee2017-05-10 12:13:45 +02001106/*
1107 * Bound mute controls
1108 */
1109#define AMP_VAL_IDX_SHIFT 19
1110#define AMP_VAL_IDX_MASK (0x0f<<19)
1111
1112static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
1113 struct snd_ctl_elem_value *ucontrol)
1114{
1115 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1116 unsigned long pval;
1117 int err;
1118
1119 mutex_lock(&codec->control_mutex);
1120 pval = kcontrol->private_value;
1121 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1122 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1123 kcontrol->private_value = pval;
1124 mutex_unlock(&codec->control_mutex);
1125 return err;
1126}
1127
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001128static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1129 struct snd_ctl_elem_value *ucontrol)
1130{
Takashi Iwai698f5ee2017-05-10 12:13:45 +02001131 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1132 unsigned long pval;
1133 int i, indices, err = 0, change = 0;
1134
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001135 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai698f5ee2017-05-10 12:13:45 +02001136
1137 mutex_lock(&codec->control_mutex);
1138 pval = kcontrol->private_value;
1139 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1140 for (i = 0; i < indices; i++) {
1141 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1142 (i << AMP_VAL_IDX_SHIFT);
1143 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1144 if (err < 0)
1145 break;
1146 change |= err;
1147 }
1148 kcontrol->private_value = pval;
1149 mutex_unlock(&codec->control_mutex);
1150 return err < 0 ? err : change;
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001151}
1152
Takashi Iwai247d85e2013-01-17 16:18:11 +01001153/* any ctl assigned to the path with the given index? */
1154static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1155{
1156 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1157 return path && path->ctls[ctl_type];
1158}
1159
Takashi Iwai352f7f92012-12-19 12:52:06 +01001160static const char * const channel_name[4] = {
1161 "Front", "Surround", "CLFE", "Side"
1162};
1163
1164/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001165static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1166 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001167{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001168 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001169 struct auto_pin_cfg *cfg = &spec->autocfg;
1170
1171 *index = 0;
1172 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai9f3dadb2017-04-10 17:12:33 +02001173 !codec->force_pin_prefix &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001174 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001175 return spec->vmaster_mute.hook ? "PCM" : "Master";
1176
1177 /* if there is really a single DAC used in the whole output paths,
1178 * use it master (or "PCM" if a vmaster hook is present)
1179 */
1180 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
Takashi Iwai9f3dadb2017-04-10 17:12:33 +02001181 !codec->force_pin_prefix &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001182 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1183 return spec->vmaster_mute.hook ? "PCM" : "Master";
1184
Takashi Iwai247d85e2013-01-17 16:18:11 +01001185 /* multi-io channels */
1186 if (ch >= cfg->line_outs)
1187 return channel_name[ch];
1188
Takashi Iwai352f7f92012-12-19 12:52:06 +01001189 switch (cfg->line_out_type) {
1190 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001191 /* if the primary channel vol/mute is shared with HP volume,
1192 * don't name it as Speaker
1193 */
1194 if (!ch && cfg->hp_outs &&
1195 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1196 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001197 if (cfg->line_outs == 1)
1198 return "Speaker";
1199 if (cfg->line_outs == 2)
1200 return ch ? "Bass Speaker" : "Speaker";
1201 break;
1202 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001203 /* if the primary channel vol/mute is shared with spk volume,
1204 * don't name it as Headphone
1205 */
1206 if (!ch && cfg->speaker_outs &&
1207 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1208 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001209 /* for multi-io case, only the primary out */
1210 if (ch && spec->multi_ios)
1211 break;
1212 *index = ch;
1213 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001214 case AUTO_PIN_LINE_OUT:
1215 /* This deals with the case where we have two DACs and
1216 * one LO, one HP and one Speaker */
1217 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1218 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1219 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1220 if (hp_lo_shared && spk_lo_shared)
1221 return spec->vmaster_mute.hook ? "PCM" : "Master";
1222 if (hp_lo_shared)
1223 return "Headphone+LO";
1224 if (spk_lo_shared)
1225 return "Speaker+LO";
1226 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001227 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001228
1229 /* for a single channel output, we don't have to name the channel */
1230 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001231 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001232
Takashi Iwai352f7f92012-12-19 12:52:06 +01001233 if (ch >= ARRAY_SIZE(channel_name)) {
1234 snd_BUG();
1235 return "PCM";
1236 }
1237
1238 return channel_name[ch];
1239}
1240
1241/*
1242 * Parse output paths
1243 */
1244
1245/* badness definition */
1246enum {
1247 /* No primary DAC is found for the main output */
1248 BAD_NO_PRIMARY_DAC = 0x10000,
1249 /* No DAC is found for the extra output */
1250 BAD_NO_DAC = 0x4000,
1251 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001252 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001253 /* No individual DAC for extra output */
1254 BAD_NO_EXTRA_DAC = 0x102,
1255 /* No individual DAC for extra surrounds */
1256 BAD_NO_EXTRA_SURR_DAC = 0x101,
1257 /* Primary DAC shared with main surrounds */
1258 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001259 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001260 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001261 /* Primary DAC shared with main CLFE */
1262 BAD_SHARED_CLFE = 0x10,
1263 /* Primary DAC shared with extra surrounds */
1264 BAD_SHARED_EXTRA_SURROUND = 0x10,
1265 /* Volume widget is shared */
1266 BAD_SHARED_VOL = 0x10,
1267};
1268
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001269/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001270 * volume and mute controls, and assign the values to ctls[].
1271 *
1272 * When no appropriate widget is found in the path, the badness value
1273 * is incremented depending on the situation. The function returns the
1274 * total badness for both volume and mute controls.
1275 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001276static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001277{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001278 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001279 hda_nid_t nid;
1280 unsigned int val;
1281 int badness = 0;
1282
1283 if (!path)
1284 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001285
1286 if (path->ctls[NID_PATH_VOL_CTL] ||
1287 path->ctls[NID_PATH_MUTE_CTL])
1288 return 0; /* already evaluated */
1289
Takashi Iwai352f7f92012-12-19 12:52:06 +01001290 nid = look_for_out_vol_nid(codec, path);
1291 if (nid) {
1292 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001293 if (spec->dac_min_mute)
1294 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001295 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1296 badness += BAD_SHARED_VOL;
1297 else
1298 path->ctls[NID_PATH_VOL_CTL] = val;
1299 } else
1300 badness += BAD_SHARED_VOL;
1301 nid = look_for_out_mute_nid(codec, path);
1302 if (nid) {
1303 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1304 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1305 nid_has_mute(codec, nid, HDA_OUTPUT))
1306 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1307 else
1308 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1309 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1310 badness += BAD_SHARED_VOL;
1311 else
1312 path->ctls[NID_PATH_MUTE_CTL] = val;
1313 } else
1314 badness += BAD_SHARED_VOL;
1315 return badness;
1316}
1317
Takashi Iwai98bd1112013-03-22 14:53:50 +01001318const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001319 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1320 .no_dac = BAD_NO_DAC,
1321 .shared_primary = BAD_NO_PRIMARY_DAC,
1322 .shared_surr = BAD_SHARED_SURROUND,
1323 .shared_clfe = BAD_SHARED_CLFE,
1324 .shared_surr_main = BAD_SHARED_SURROUND,
1325};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001326EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001327
Takashi Iwai98bd1112013-03-22 14:53:50 +01001328const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001329 .no_primary_dac = BAD_NO_DAC,
1330 .no_dac = BAD_NO_DAC,
1331 .shared_primary = BAD_NO_EXTRA_DAC,
1332 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1333 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1334 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1335};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001336EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001337
Takashi Iwai7385df62013-01-07 09:50:52 +01001338/* get the DAC of the primary output corresponding to the given array index */
1339static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1340{
1341 struct hda_gen_spec *spec = codec->spec;
1342 struct auto_pin_cfg *cfg = &spec->autocfg;
1343
1344 if (cfg->line_outs > idx)
1345 return spec->private_dac_nids[idx];
1346 idx -= cfg->line_outs;
1347 if (spec->multi_ios > idx)
1348 return spec->multi_io[idx].dac;
1349 return 0;
1350}
1351
1352/* return the DAC if it's reachable, otherwise zero */
1353static inline hda_nid_t try_dac(struct hda_codec *codec,
1354 hda_nid_t dac, hda_nid_t pin)
1355{
1356 return is_reachable_path(codec, dac, pin) ? dac : 0;
1357}
1358
Takashi Iwai352f7f92012-12-19 12:52:06 +01001359/* try to assign DACs to pins and return the resultant badness */
1360static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1361 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001362 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001363 const struct badness_table *bad)
1364{
1365 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001366 int i, j;
1367 int badness = 0;
1368 hda_nid_t dac;
1369
1370 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return 0;
1372
Takashi Iwai352f7f92012-12-19 12:52:06 +01001373 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001374 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001375 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001376
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001377 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1378 if (path) {
1379 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001380 continue;
1381 }
1382
Takashi Iwai36907392013-12-10 17:29:26 +01001383 dacs[i] = get_preferred_dac(codec, pin);
1384 if (dacs[i]) {
1385 if (is_dac_already_used(codec, dacs[i]))
1386 badness += bad->shared_primary;
1387 }
1388
1389 if (!dacs[i])
1390 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001391 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001392 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001393 for (j = 1; j < num_outs; j++) {
1394 if (is_reachable_path(codec, dacs[j], pin)) {
1395 dacs[0] = dacs[j];
1396 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001397 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001398 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001399 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001402 }
1403 dac = dacs[i];
1404 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001405 if (num_outs > 2)
1406 dac = try_dac(codec, get_primary_out(codec, i), pin);
1407 if (!dac)
1408 dac = try_dac(codec, dacs[0], pin);
1409 if (!dac)
1410 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001411 if (dac) {
1412 if (!i)
1413 badness += bad->shared_primary;
1414 else if (i == 1)
1415 badness += bad->shared_surr;
1416 else
1417 badness += bad->shared_clfe;
1418 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1419 dac = spec->private_dac_nids[0];
1420 badness += bad->shared_surr_main;
1421 } else if (!i)
1422 badness += bad->no_primary_dac;
1423 else
1424 badness += bad->no_dac;
1425 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001426 if (!dac)
1427 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001428 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001429 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001430 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001431 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001432 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001433 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001434 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001435 badness += bad->no_dac;
1436 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001437 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001438 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001439 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001440 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001441 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001442 }
1443
1444 return badness;
1445}
1446
1447/* return NID if the given pin has only a single connection to a certain DAC */
1448static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1449{
1450 struct hda_gen_spec *spec = codec->spec;
1451 int i;
1452 hda_nid_t nid_found = 0;
1453
1454 for (i = 0; i < spec->num_all_dacs; i++) {
1455 hda_nid_t nid = spec->all_dacs[i];
1456 if (!nid || is_dac_already_used(codec, nid))
1457 continue;
1458 if (is_reachable_path(codec, nid, pin)) {
1459 if (nid_found)
1460 return 0;
1461 nid_found = nid;
1462 }
1463 }
1464 return nid_found;
1465}
1466
1467/* check whether the given pin can be a multi-io pin */
1468static bool can_be_multiio_pin(struct hda_codec *codec,
1469 unsigned int location, hda_nid_t nid)
1470{
1471 unsigned int defcfg, caps;
1472
1473 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1474 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1475 return false;
1476 if (location && get_defcfg_location(defcfg) != location)
1477 return false;
1478 caps = snd_hda_query_pin_caps(codec, nid);
1479 if (!(caps & AC_PINCAP_OUT))
1480 return false;
1481 return true;
1482}
1483
Takashi Iwaie22aab72013-01-04 14:50:04 +01001484/* count the number of input pins that are capable to be multi-io */
1485static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1486{
1487 struct hda_gen_spec *spec = codec->spec;
1488 struct auto_pin_cfg *cfg = &spec->autocfg;
1489 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1490 unsigned int location = get_defcfg_location(defcfg);
1491 int type, i;
1492 int num_pins = 0;
1493
1494 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1495 for (i = 0; i < cfg->num_inputs; i++) {
1496 if (cfg->inputs[i].type != type)
1497 continue;
1498 if (can_be_multiio_pin(codec, location,
1499 cfg->inputs[i].pin))
1500 num_pins++;
1501 }
1502 }
1503 return num_pins;
1504}
1505
Takashi Iwai352f7f92012-12-19 12:52:06 +01001506/*
1507 * multi-io helper
1508 *
1509 * When hardwired is set, try to fill ony hardwired pins, and returns
1510 * zero if any pins are filled, non-zero if nothing found.
1511 * When hardwired is off, try to fill possible input pins, and returns
1512 * the badness value.
1513 */
1514static int fill_multi_ios(struct hda_codec *codec,
1515 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001516 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001517{
1518 struct hda_gen_spec *spec = codec->spec;
1519 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001520 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001521 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1522 unsigned int location = get_defcfg_location(defcfg);
1523 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001524 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001525
1526 old_pins = spec->multi_ios;
1527 if (old_pins >= 2)
1528 goto end_fill;
1529
Takashi Iwaie22aab72013-01-04 14:50:04 +01001530 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001531 if (num_pins < 2)
1532 goto end_fill;
1533
Takashi Iwai352f7f92012-12-19 12:52:06 +01001534 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1535 for (i = 0; i < cfg->num_inputs; i++) {
1536 hda_nid_t nid = cfg->inputs[i].pin;
1537 hda_nid_t dac = 0;
1538
1539 if (cfg->inputs[i].type != type)
1540 continue;
1541 if (!can_be_multiio_pin(codec, location, nid))
1542 continue;
1543 for (j = 0; j < spec->multi_ios; j++) {
1544 if (nid == spec->multi_io[j].pin)
1545 break;
1546 }
1547 if (j < spec->multi_ios)
1548 continue;
1549
Takashi Iwai352f7f92012-12-19 12:52:06 +01001550 if (hardwired)
1551 dac = get_dac_if_single(codec, nid);
1552 else if (!dac)
1553 dac = look_for_dac(codec, nid, false);
1554 if (!dac) {
1555 badness++;
1556 continue;
1557 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001558 path = snd_hda_add_new_path(codec, dac, nid,
1559 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001560 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001561 badness++;
1562 continue;
1563 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001564 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001565 spec->multi_io[spec->multi_ios].pin = nid;
1566 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001567 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1568 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001569 spec->multi_ios++;
1570 if (spec->multi_ios >= 2)
1571 break;
1572 }
1573 }
1574 end_fill:
1575 if (badness)
1576 badness = BAD_MULTI_IO;
1577 if (old_pins == spec->multi_ios) {
1578 if (hardwired)
1579 return 1; /* nothing found */
1580 else
1581 return badness; /* no badness if nothing found */
1582 }
1583 if (!hardwired && spec->multi_ios < 2) {
1584 /* cancel newly assigned paths */
1585 spec->paths.used -= spec->multi_ios - old_pins;
1586 spec->multi_ios = old_pins;
1587 return badness;
1588 }
1589
1590 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001591 for (i = old_pins; i < spec->multi_ios; i++) {
1592 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1593 badness += assign_out_path_ctls(codec, path);
1594 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001595
1596 return badness;
1597}
1598
1599/* map DACs for all pins in the list if they are single connections */
1600static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001601 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001602{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001603 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001604 int i;
1605 bool found = false;
1606 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001607 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001608 hda_nid_t dac;
1609 if (dacs[i])
1610 continue;
1611 dac = get_dac_if_single(codec, pins[i]);
1612 if (!dac)
1613 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001614 path = snd_hda_add_new_path(codec, dac, pins[i],
1615 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001616 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001617 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001618 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001619 dacs[i] = dac;
1620 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001621 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001622 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001623 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001624 }
1625 }
1626 return found;
1627}
1628
Takashi Iwaie7fdd522015-12-08 17:00:42 +01001629static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1630{
1631 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1632 spec->aamix_out_paths[2];
1633}
1634
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001635/* create a new path including aamix if available, and return its index */
1636static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1637{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001638 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001639 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001640 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001641
1642 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001643 if (!path || !path->depth ||
1644 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001645 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001646 path_dac = path->path[0];
1647 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001648 pin = path->path[path->depth - 1];
1649 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1650 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001651 if (dac != path_dac)
1652 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001653 else if (spec->multiout.hp_out_nid[0])
1654 dac = spec->multiout.hp_out_nid[0];
1655 else if (spec->multiout.extra_out_nid[0])
1656 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001657 else
1658 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001659 if (dac)
1660 path = snd_hda_add_new_path(codec, dac, pin,
1661 spec->mixer_nid);
1662 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001663 if (!path)
1664 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001665 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001666 path->active = false; /* unused as default */
Takashi Iwai6b275b12015-03-20 18:11:05 +01001667 path->pin_fixed = true; /* static route */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001668 return snd_hda_get_path_idx(codec, path);
1669}
1670
Takashi Iwai55a63d42013-03-21 17:20:12 +01001671/* check whether the independent HP is available with the current config */
1672static bool indep_hp_possible(struct hda_codec *codec)
1673{
1674 struct hda_gen_spec *spec = codec->spec;
1675 struct auto_pin_cfg *cfg = &spec->autocfg;
1676 struct nid_path *path;
1677 int i, idx;
1678
1679 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1680 idx = spec->out_paths[0];
1681 else
1682 idx = spec->hp_paths[0];
1683 path = snd_hda_get_path_from_idx(codec, idx);
1684 if (!path)
1685 return false;
1686
1687 /* assume no path conflicts unless aamix is involved */
1688 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1689 return true;
1690
1691 /* check whether output paths contain aamix */
1692 for (i = 0; i < cfg->line_outs; i++) {
1693 if (spec->out_paths[i] == idx)
1694 break;
1695 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1696 if (path && is_nid_contained(path, spec->mixer_nid))
1697 return false;
1698 }
1699 for (i = 0; i < cfg->speaker_outs; i++) {
1700 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1701 if (path && is_nid_contained(path, spec->mixer_nid))
1702 return false;
1703 }
1704
1705 return true;
1706}
1707
Takashi Iwaia07a9492013-01-07 16:44:06 +01001708/* fill the empty entries in the dac array for speaker/hp with the
1709 * shared dac pointed by the paths
1710 */
1711static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1712 hda_nid_t *dacs, int *path_idx)
1713{
1714 struct nid_path *path;
1715 int i;
1716
1717 for (i = 0; i < num_outs; i++) {
1718 if (dacs[i])
1719 continue;
1720 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1721 if (!path)
1722 continue;
1723 dacs[i] = path->path[0];
1724 }
1725}
1726
Takashi Iwai352f7f92012-12-19 12:52:06 +01001727/* fill in the dac_nids table from the parsed pin configuration */
1728static int fill_and_eval_dacs(struct hda_codec *codec,
1729 bool fill_hardwired,
1730 bool fill_mio_first)
1731{
1732 struct hda_gen_spec *spec = codec->spec;
1733 struct auto_pin_cfg *cfg = &spec->autocfg;
1734 int i, err, badness;
1735
1736 /* set num_dacs once to full for look_for_dac() */
1737 spec->multiout.num_dacs = cfg->line_outs;
1738 spec->multiout.dac_nids = spec->private_dac_nids;
1739 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1740 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1741 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1742 spec->multi_ios = 0;
1743 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001744
1745 /* clear path indices */
1746 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1747 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1748 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1749 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1750 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001751 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001752 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1753 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1754
Takashi Iwai352f7f92012-12-19 12:52:06 +01001755 badness = 0;
1756
1757 /* fill hard-wired DACs first */
1758 if (fill_hardwired) {
1759 bool mapped;
1760 do {
1761 mapped = map_singles(codec, cfg->line_outs,
1762 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001763 spec->private_dac_nids,
1764 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001765 mapped |= map_singles(codec, cfg->hp_outs,
1766 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001767 spec->multiout.hp_out_nid,
1768 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001769 mapped |= map_singles(codec, cfg->speaker_outs,
1770 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001771 spec->multiout.extra_out_nid,
1772 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001773 if (!spec->no_multi_io &&
1774 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001775 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001776 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001777 if (!err)
1778 mapped = true;
1779 }
1780 } while (mapped);
1781 }
1782
1783 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001784 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001785 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001786
Takashi Iwaida96fb52013-07-29 16:54:36 +02001787 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001788 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1789 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001790 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001791 if (err < 0)
1792 return err;
1793 /* we don't count badness at this stage yet */
1794 }
1795
1796 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1797 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1798 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001799 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001800 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001801 if (err < 0)
1802 return err;
1803 badness += err;
1804 }
1805 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1806 err = try_assign_dacs(codec, cfg->speaker_outs,
1807 cfg->speaker_pins,
1808 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001809 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001810 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001811 if (err < 0)
1812 return err;
1813 badness += err;
1814 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001815 if (!spec->no_multi_io &&
1816 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001817 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001818 if (err < 0)
1819 return err;
1820 badness += err;
1821 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001822
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001823 if (spec->mixer_nid) {
1824 spec->aamix_out_paths[0] =
1825 check_aamix_out_path(codec, spec->out_paths[0]);
1826 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1827 spec->aamix_out_paths[1] =
1828 check_aamix_out_path(codec, spec->hp_paths[0]);
1829 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1830 spec->aamix_out_paths[2] =
1831 check_aamix_out_path(codec, spec->speaker_paths[0]);
1832 }
1833
Takashi Iwaida96fb52013-07-29 16:54:36 +02001834 if (!spec->no_multi_io &&
1835 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001836 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1837 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001838
Takashi Iwaia07a9492013-01-07 16:44:06 +01001839 /* re-count num_dacs and squash invalid entries */
1840 spec->multiout.num_dacs = 0;
1841 for (i = 0; i < cfg->line_outs; i++) {
1842 if (spec->private_dac_nids[i])
1843 spec->multiout.num_dacs++;
1844 else {
1845 memmove(spec->private_dac_nids + i,
1846 spec->private_dac_nids + i + 1,
1847 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1848 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1849 }
1850 }
1851
1852 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001853 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001854
Takashi Iwai352f7f92012-12-19 12:52:06 +01001855 if (spec->multi_ios == 2) {
1856 for (i = 0; i < 2; i++)
1857 spec->private_dac_nids[spec->multiout.num_dacs++] =
1858 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001859 } else if (spec->multi_ios) {
1860 spec->multi_ios = 0;
1861 badness += BAD_MULTI_IO;
1862 }
1863
Takashi Iwai55a63d42013-03-21 17:20:12 +01001864 if (spec->indep_hp && !indep_hp_possible(codec))
1865 badness += BAD_NO_INDEP_HP;
1866
Takashi Iwaia07a9492013-01-07 16:44:06 +01001867 /* re-fill the shared DAC for speaker / headphone */
1868 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1869 refill_shared_dacs(codec, cfg->hp_outs,
1870 spec->multiout.hp_out_nid,
1871 spec->hp_paths);
1872 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1873 refill_shared_dacs(codec, cfg->speaker_outs,
1874 spec->multiout.extra_out_nid,
1875 spec->speaker_paths);
1876
Takashi Iwai352f7f92012-12-19 12:52:06 +01001877 return badness;
1878}
1879
1880#define DEBUG_BADNESS
1881
1882#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001883#define debug_badness(fmt, ...) \
1884 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001885#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001886#define debug_badness(fmt, ...) \
1887 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001888#endif
1889
Takashi Iwaia7694092013-01-21 10:43:18 +01001890#ifdef DEBUG_BADNESS
1891static inline void print_nid_path_idx(struct hda_codec *codec,
1892 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001893{
Takashi Iwaia7694092013-01-21 10:43:18 +01001894 struct nid_path *path;
1895
1896 path = snd_hda_get_path_from_idx(codec, idx);
1897 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001898 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001899}
1900
1901static void debug_show_configs(struct hda_codec *codec,
1902 struct auto_pin_cfg *cfg)
1903{
1904 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001905 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001906 int i;
1907
1908 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001909 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001910 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001911 spec->multiout.dac_nids[0],
1912 spec->multiout.dac_nids[1],
1913 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001914 spec->multiout.dac_nids[3],
1915 lo_type[cfg->line_out_type]);
1916 for (i = 0; i < cfg->line_outs; i++)
1917 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001918 if (spec->multi_ios > 0)
1919 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1920 spec->multi_ios,
1921 spec->multi_io[0].pin, spec->multi_io[1].pin,
1922 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001923 for (i = 0; i < spec->multi_ios; i++)
1924 print_nid_path_idx(codec, " mio",
1925 spec->out_paths[cfg->line_outs + i]);
1926 if (cfg->hp_outs)
1927 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001928 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001929 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001930 spec->multiout.hp_out_nid[0],
1931 spec->multiout.hp_out_nid[1],
1932 spec->multiout.hp_out_nid[2],
1933 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001934 for (i = 0; i < cfg->hp_outs; i++)
1935 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1936 if (cfg->speaker_outs)
1937 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001938 cfg->speaker_pins[0], cfg->speaker_pins[1],
1939 cfg->speaker_pins[2], cfg->speaker_pins[3],
1940 spec->multiout.extra_out_nid[0],
1941 spec->multiout.extra_out_nid[1],
1942 spec->multiout.extra_out_nid[2],
1943 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001944 for (i = 0; i < cfg->speaker_outs; i++)
1945 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1946 for (i = 0; i < 3; i++)
1947 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001948}
Takashi Iwaia7694092013-01-21 10:43:18 +01001949#else
1950#define debug_show_configs(codec, cfg) /* NOP */
1951#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001952
1953/* find all available DACs of the codec */
1954static void fill_all_dac_nids(struct hda_codec *codec)
1955{
1956 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001957 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001958
1959 spec->num_all_dacs = 0;
1960 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
Takashi Iwai7639a062015-03-03 10:07:24 +01001961 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001962 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1963 continue;
1964 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001965 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001966 break;
1967 }
1968 spec->all_dacs[spec->num_all_dacs++] = nid;
1969 }
1970}
1971
1972static int parse_output_paths(struct hda_codec *codec)
1973{
1974 struct hda_gen_spec *spec = codec->spec;
1975 struct auto_pin_cfg *cfg = &spec->autocfg;
1976 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001977 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001978 int best_badness = INT_MAX;
1979 int badness;
1980 bool fill_hardwired = true, fill_mio_first = true;
1981 bool best_wired = true, best_mio = true;
1982 bool hp_spk_swapped = false;
1983
Takashi Iwai352f7f92012-12-19 12:52:06 +01001984 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1985 if (!best_cfg)
1986 return -ENOMEM;
1987 *best_cfg = *cfg;
1988
1989 for (;;) {
1990 badness = fill_and_eval_dacs(codec, fill_hardwired,
1991 fill_mio_first);
1992 if (badness < 0) {
1993 kfree(best_cfg);
1994 return badness;
1995 }
1996 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1997 cfg->line_out_type, fill_hardwired, fill_mio_first,
1998 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001999 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002000 if (badness < best_badness) {
2001 best_badness = badness;
2002 *best_cfg = *cfg;
2003 best_wired = fill_hardwired;
2004 best_mio = fill_mio_first;
2005 }
2006 if (!badness)
2007 break;
2008 fill_mio_first = !fill_mio_first;
2009 if (!fill_mio_first)
2010 continue;
2011 fill_hardwired = !fill_hardwired;
2012 if (!fill_hardwired)
2013 continue;
2014 if (hp_spk_swapped)
2015 break;
2016 hp_spk_swapped = true;
2017 if (cfg->speaker_outs > 0 &&
2018 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2019 cfg->hp_outs = cfg->line_outs;
2020 memcpy(cfg->hp_pins, cfg->line_out_pins,
2021 sizeof(cfg->hp_pins));
2022 cfg->line_outs = cfg->speaker_outs;
2023 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2024 sizeof(cfg->speaker_pins));
2025 cfg->speaker_outs = 0;
2026 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2027 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2028 fill_hardwired = true;
2029 continue;
2030 }
2031 if (cfg->hp_outs > 0 &&
2032 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2033 cfg->speaker_outs = cfg->line_outs;
2034 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2035 sizeof(cfg->speaker_pins));
2036 cfg->line_outs = cfg->hp_outs;
2037 memcpy(cfg->line_out_pins, cfg->hp_pins,
2038 sizeof(cfg->hp_pins));
2039 cfg->hp_outs = 0;
2040 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2041 cfg->line_out_type = AUTO_PIN_HP_OUT;
2042 fill_hardwired = true;
2043 continue;
2044 }
2045 break;
2046 }
2047
2048 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002049 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01002050 *cfg = *best_cfg;
2051 fill_and_eval_dacs(codec, best_wired, best_mio);
2052 }
2053 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2054 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01002055 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002056
2057 if (cfg->line_out_pins[0]) {
2058 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01002059 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002060 if (path)
2061 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002062 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01002063 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2064 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002065 if (spec->dac_min_mute)
2066 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2067 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002068 }
2069
Takashi Iwai9314a582013-01-21 10:49:05 +01002070 /* set initial pinctl targets */
2071 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2072 val = PIN_HP;
2073 else
2074 val = PIN_OUT;
2075 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2076 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2077 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2078 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2079 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2080 set_pin_targets(codec, cfg->speaker_outs,
2081 cfg->speaker_pins, val);
2082 }
2083
Takashi Iwai55a63d42013-03-21 17:20:12 +01002084 /* clear indep_hp flag if not available */
2085 if (spec->indep_hp && !indep_hp_possible(codec))
2086 spec->indep_hp = 0;
2087
Takashi Iwai352f7f92012-12-19 12:52:06 +01002088 kfree(best_cfg);
2089 return 0;
2090}
2091
2092/* add playback controls from the parsed DAC table */
2093static int create_multi_out_ctls(struct hda_codec *codec,
2094 const struct auto_pin_cfg *cfg)
2095{
2096 struct hda_gen_spec *spec = codec->spec;
2097 int i, err, noutputs;
2098
2099 noutputs = cfg->line_outs;
2100 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2101 noutputs += spec->multi_ios;
2102
2103 for (i = 0; i < noutputs; i++) {
2104 const char *name;
2105 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002106 struct nid_path *path;
2107
Takashi Iwai196c17662013-01-04 15:01:40 +01002108 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002109 if (!path)
2110 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002111
2112 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002113 if (!name || !strcmp(name, "CLFE")) {
2114 /* Center/LFE */
2115 err = add_vol_ctl(codec, "Center", 0, 1, path);
2116 if (err < 0)
2117 return err;
2118 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2119 if (err < 0)
2120 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002121 } else {
2122 err = add_stereo_vol(codec, name, index, path);
2123 if (err < 0)
2124 return err;
2125 }
2126
2127 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2128 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002129 err = add_sw_ctl(codec, "Center", 0, 1, path);
2130 if (err < 0)
2131 return err;
2132 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2133 if (err < 0)
2134 return err;
2135 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002136 err = add_stereo_sw(codec, name, index, path);
2137 if (err < 0)
2138 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140 }
2141 return 0;
2142}
2143
Takashi Iwaic2c80382013-01-07 10:33:57 +01002144static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002145 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002147 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 int err;
2149
Takashi Iwai196c17662013-01-04 15:01:40 +01002150 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002151 if (!path)
2152 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002153 err = add_stereo_vol(codec, pfx, cidx, path);
2154 if (err < 0)
2155 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002156 err = add_stereo_sw(codec, pfx, cidx, path);
2157 if (err < 0)
2158 return err;
2159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
Takashi Iwai352f7f92012-12-19 12:52:06 +01002162/* add playback controls for speaker and HP outputs */
2163static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002164 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002166 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002167
2168 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002169 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002170 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002171 int err, idx = 0;
2172
2173 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2174 name = "Bass Speaker";
2175 else if (num_pins >= 3) {
2176 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002177 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002178 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002179 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002180 name = pfx;
2181 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002183 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002184 if (err < 0)
2185 return err;
2186 }
2187 return 0;
2188}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002189
Takashi Iwai352f7f92012-12-19 12:52:06 +01002190static int create_hp_out_ctls(struct hda_codec *codec)
2191{
2192 struct hda_gen_spec *spec = codec->spec;
2193 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002194 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002195 "Headphone");
2196}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Takashi Iwai352f7f92012-12-19 12:52:06 +01002198static int create_speaker_out_ctls(struct hda_codec *codec)
2199{
2200 struct hda_gen_spec *spec = codec->spec;
2201 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002202 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002203 "Speaker");
2204}
2205
2206/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002207 * independent HP controls
2208 */
2209
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002210static void call_hp_automute(struct hda_codec *codec,
2211 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002212static int indep_hp_info(struct snd_kcontrol *kcontrol,
2213 struct snd_ctl_elem_info *uinfo)
2214{
2215 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2216}
2217
2218static int indep_hp_get(struct snd_kcontrol *kcontrol,
2219 struct snd_ctl_elem_value *ucontrol)
2220{
2221 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2222 struct hda_gen_spec *spec = codec->spec;
2223 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2224 return 0;
2225}
2226
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002227static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2228 int nomix_path_idx, int mix_path_idx,
2229 int out_type);
2230
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002231static int indep_hp_put(struct snd_kcontrol *kcontrol,
2232 struct snd_ctl_elem_value *ucontrol)
2233{
2234 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2235 struct hda_gen_spec *spec = codec->spec;
2236 unsigned int select = ucontrol->value.enumerated.item[0];
2237 int ret = 0;
2238
2239 mutex_lock(&spec->pcm_mutex);
2240 if (spec->active_streams) {
2241 ret = -EBUSY;
2242 goto unlock;
2243 }
2244
2245 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002246 hda_nid_t *dacp;
2247 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2248 dacp = &spec->private_dac_nids[0];
2249 else
2250 dacp = &spec->multiout.hp_out_nid[0];
2251
2252 /* update HP aamix paths in case it conflicts with indep HP */
2253 if (spec->have_aamix_ctl) {
2254 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2255 update_aamix_paths(codec, spec->aamix_mode,
2256 spec->out_paths[0],
2257 spec->aamix_out_paths[0],
2258 spec->autocfg.line_out_type);
2259 else
2260 update_aamix_paths(codec, spec->aamix_mode,
2261 spec->hp_paths[0],
2262 spec->aamix_out_paths[1],
2263 AUTO_PIN_HP_OUT);
2264 }
2265
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002266 spec->indep_hp_enabled = select;
2267 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002268 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002269 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002270 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002271
Takashi Iwai963afde2013-05-31 15:20:31 +02002272 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002273 ret = 1;
2274 }
2275 unlock:
2276 mutex_unlock(&spec->pcm_mutex);
2277 return ret;
2278}
2279
2280static const struct snd_kcontrol_new indep_hp_ctl = {
2281 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2282 .name = "Independent HP",
2283 .info = indep_hp_info,
2284 .get = indep_hp_get,
2285 .put = indep_hp_put,
2286};
2287
2288
2289static int create_indep_hp_ctls(struct hda_codec *codec)
2290{
2291 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002292 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002293
2294 if (!spec->indep_hp)
2295 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002296 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2297 dac = spec->multiout.dac_nids[0];
2298 else
2299 dac = spec->multiout.hp_out_nid[0];
2300 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002301 spec->indep_hp = 0;
2302 return 0;
2303 }
2304
2305 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002306 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002307 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2308 return -ENOMEM;
2309 return 0;
2310}
2311
2312/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002313 * channel mode enum control
2314 */
2315
2316static int ch_mode_info(struct snd_kcontrol *kcontrol,
2317 struct snd_ctl_elem_info *uinfo)
2318{
2319 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2320 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002321 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002322
2323 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2324 uinfo->count = 1;
2325 uinfo->value.enumerated.items = spec->multi_ios + 1;
2326 if (uinfo->value.enumerated.item > spec->multi_ios)
2327 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002328 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2329 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002330 return 0;
2331}
2332
2333static int ch_mode_get(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_value *ucontrol)
2335{
2336 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2337 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002338 ucontrol->value.enumerated.item[0] =
2339 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002340 return 0;
2341}
2342
Takashi Iwai196c17662013-01-04 15:01:40 +01002343static inline struct nid_path *
2344get_multiio_path(struct hda_codec *codec, int idx)
2345{
2346 struct hda_gen_spec *spec = codec->spec;
2347 return snd_hda_get_path_from_idx(codec,
2348 spec->out_paths[spec->autocfg.line_outs + idx]);
2349}
2350
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002351static void update_automute_all(struct hda_codec *codec);
2352
Takashi Iwai65033cc2013-04-16 12:31:05 +02002353/* Default value to be passed as aamix argument for snd_hda_activate_path();
2354 * used for output paths
2355 */
2356static bool aamix_default(struct hda_gen_spec *spec)
2357{
2358 return !spec->have_aamix_ctl || spec->aamix_mode;
2359}
2360
Takashi Iwai352f7f92012-12-19 12:52:06 +01002361static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2362{
2363 struct hda_gen_spec *spec = codec->spec;
2364 hda_nid_t nid = spec->multi_io[idx].pin;
2365 struct nid_path *path;
2366
Takashi Iwai196c17662013-01-04 15:01:40 +01002367 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002368 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002370
2371 if (path->active == output)
2372 return 0;
2373
2374 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002375 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002376 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002377 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002378 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002379 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002380 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002381 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002382 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002384
2385 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002386 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002387
Takashi Iwai352f7f92012-12-19 12:52:06 +01002388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
Takashi Iwai352f7f92012-12-19 12:52:06 +01002391static int ch_mode_put(struct snd_kcontrol *kcontrol,
2392 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002394 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2395 struct hda_gen_spec *spec = codec->spec;
2396 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Takashi Iwai352f7f92012-12-19 12:52:06 +01002398 ch = ucontrol->value.enumerated.item[0];
2399 if (ch < 0 || ch > spec->multi_ios)
2400 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002401 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002402 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002403 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002404 for (i = 0; i < spec->multi_ios; i++)
2405 set_multi_io(codec, i, i < ch);
2406 spec->multiout.max_channels = max(spec->ext_channel_count,
2407 spec->const_channel_count);
2408 if (spec->need_dac_fix)
2409 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 return 1;
2411}
2412
Takashi Iwai352f7f92012-12-19 12:52:06 +01002413static const struct snd_kcontrol_new channel_mode_enum = {
2414 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2415 .name = "Channel Mode",
2416 .info = ch_mode_info,
2417 .get = ch_mode_get,
2418 .put = ch_mode_put,
2419};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Takashi Iwai352f7f92012-12-19 12:52:06 +01002421static int create_multi_channel_mode(struct hda_codec *codec)
2422{
2423 struct hda_gen_spec *spec = codec->spec;
2424
2425 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002426 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002427 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return 0;
2430}
2431
Takashi Iwai352f7f92012-12-19 12:52:06 +01002432/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002433 * aamix loopback enable/disable switch
2434 */
2435
2436#define loopback_mixing_info indep_hp_info
2437
2438static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2439 struct snd_ctl_elem_value *ucontrol)
2440{
2441 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2442 struct hda_gen_spec *spec = codec->spec;
2443 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2444 return 0;
2445}
2446
2447static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002448 int nomix_path_idx, int mix_path_idx,
2449 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002450{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002451 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002452 struct nid_path *nomix_path, *mix_path;
2453
2454 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2455 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2456 if (!nomix_path || !mix_path)
2457 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002458
2459 /* if HP aamix path is driven from a different DAC and the
2460 * independent HP mode is ON, can't turn on aamix path
2461 */
2462 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2463 mix_path->path[0] != spec->alt_dac_nid)
2464 do_mix = false;
2465
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002466 if (do_mix) {
2467 snd_hda_activate_path(codec, nomix_path, false, true);
2468 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002469 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002470 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002471 snd_hda_activate_path(codec, mix_path, false, false);
2472 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002473 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002474 }
2475}
2476
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002477/* re-initialize the output paths; only called from loopback_mixing_put() */
2478static void update_output_paths(struct hda_codec *codec, int num_outs,
2479 const int *paths)
2480{
2481 struct hda_gen_spec *spec = codec->spec;
2482 struct nid_path *path;
2483 int i;
2484
2485 for (i = 0; i < num_outs; i++) {
2486 path = snd_hda_get_path_from_idx(codec, paths[i]);
2487 if (path)
2488 snd_hda_activate_path(codec, path, path->active,
2489 spec->aamix_mode);
2490 }
2491}
2492
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002493static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2494 struct snd_ctl_elem_value *ucontrol)
2495{
2496 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2497 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002498 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002499 unsigned int val = ucontrol->value.enumerated.item[0];
2500
2501 if (val == spec->aamix_mode)
2502 return 0;
2503 spec->aamix_mode = val;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002504 if (has_aamix_out_paths(spec)) {
2505 update_aamix_paths(codec, val, spec->out_paths[0],
2506 spec->aamix_out_paths[0],
2507 cfg->line_out_type);
2508 update_aamix_paths(codec, val, spec->hp_paths[0],
2509 spec->aamix_out_paths[1],
2510 AUTO_PIN_HP_OUT);
2511 update_aamix_paths(codec, val, spec->speaker_paths[0],
2512 spec->aamix_out_paths[2],
2513 AUTO_PIN_SPEAKER_OUT);
2514 } else {
2515 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2516 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2517 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2518 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2519 update_output_paths(codec, cfg->speaker_outs,
2520 spec->speaker_paths);
2521 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002522 return 1;
2523}
2524
2525static const struct snd_kcontrol_new loopback_mixing_enum = {
2526 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2527 .name = "Loopback Mixing",
2528 .info = loopback_mixing_info,
2529 .get = loopback_mixing_get,
2530 .put = loopback_mixing_put,
2531};
2532
2533static int create_loopback_mixing_ctl(struct hda_codec *codec)
2534{
2535 struct hda_gen_spec *spec = codec->spec;
2536
2537 if (!spec->mixer_nid)
2538 return 0;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002539 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2540 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002541 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002542 return 0;
2543}
2544
2545/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002546 * shared headphone/mic handling
2547 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002548
Takashi Iwai352f7f92012-12-19 12:52:06 +01002549static void call_update_outputs(struct hda_codec *codec);
2550
2551/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002552static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002553{
2554 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002555 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002556 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002557 hda_nid_t pin;
2558
2559 pin = spec->hp_mic_pin;
2560 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2561
2562 if (!force) {
2563 val = snd_hda_codec_get_pin_target(codec, pin);
2564 if (as_mic) {
2565 if (val & PIN_IN)
2566 return;
2567 } else {
2568 if (val & PIN_OUT)
2569 return;
2570 }
2571 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002572
2573 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002574 /* if the HP pin doesn't support VREF and the codec driver gives an
2575 * alternative pin, set up the VREF on that pin instead
2576 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002577 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2578 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2579 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2580 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002581 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002582 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002583 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002584
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002585 if (!spec->hp_mic_jack_modes) {
2586 if (as_mic)
2587 val |= PIN_IN;
2588 else
2589 val = PIN_HP;
2590 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002591 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002592 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002593}
2594
2595/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002596static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002597{
2598 struct hda_gen_spec *spec = codec->spec;
2599 struct auto_pin_cfg *cfg = &spec->autocfg;
2600 unsigned int defcfg;
2601 hda_nid_t nid;
2602
Takashi Iwai967303d2013-02-19 17:12:42 +01002603 if (!spec->hp_mic) {
2604 if (spec->suppress_hp_mic_detect)
2605 return 0;
2606 /* automatic detection: only if no input or a single internal
2607 * input pin is found, try to detect the shared hp/mic
2608 */
2609 if (cfg->num_inputs > 1)
2610 return 0;
2611 else if (cfg->num_inputs == 1) {
2612 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2613 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2614 return 0;
2615 }
2616 }
2617
2618 spec->hp_mic = 0; /* clear once */
2619 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002620 return 0;
2621
Takashi Iwai967303d2013-02-19 17:12:42 +01002622 nid = 0;
2623 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2624 nid = cfg->line_out_pins[0];
2625 else if (cfg->hp_outs > 0)
2626 nid = cfg->hp_pins[0];
2627 if (!nid)
2628 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002629
2630 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2631 return 0; /* no input */
2632
Takashi Iwai967303d2013-02-19 17:12:42 +01002633 cfg->inputs[cfg->num_inputs].pin = nid;
2634 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002635 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002636 cfg->num_inputs++;
2637 spec->hp_mic = 1;
2638 spec->hp_mic_pin = nid;
2639 /* we can't handle auto-mic together with HP-mic */
2640 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002641 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002642 return 0;
2643}
2644
Takashi Iwai978e77e2013-01-10 16:57:58 +01002645/*
2646 * output jack mode
2647 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002648
2649static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2650
2651static const char * const out_jack_texts[] = {
2652 "Line Out", "Headphone Out",
2653};
2654
Takashi Iwai978e77e2013-01-10 16:57:58 +01002655static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2656 struct snd_ctl_elem_info *uinfo)
2657{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002658 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002659}
2660
2661static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2662 struct snd_ctl_elem_value *ucontrol)
2663{
2664 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2665 hda_nid_t nid = kcontrol->private_value;
2666 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2667 ucontrol->value.enumerated.item[0] = 1;
2668 else
2669 ucontrol->value.enumerated.item[0] = 0;
2670 return 0;
2671}
2672
2673static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2674 struct snd_ctl_elem_value *ucontrol)
2675{
2676 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2677 hda_nid_t nid = kcontrol->private_value;
2678 unsigned int val;
2679
2680 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2681 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2682 return 0;
2683 snd_hda_set_pin_ctl_cache(codec, nid, val);
2684 return 1;
2685}
2686
2687static const struct snd_kcontrol_new out_jack_mode_enum = {
2688 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2689 .info = out_jack_mode_info,
2690 .get = out_jack_mode_get,
2691 .put = out_jack_mode_put,
2692};
2693
2694static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2695{
2696 struct hda_gen_spec *spec = codec->spec;
2697 int i;
2698
2699 for (i = 0; i < spec->kctls.used; i++) {
2700 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2701 if (!strcmp(kctl->name, name) && kctl->index == idx)
2702 return true;
2703 }
2704 return false;
2705}
2706
2707static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2708 char *name, size_t name_len)
2709{
2710 struct hda_gen_spec *spec = codec->spec;
2711 int idx = 0;
2712
2713 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2714 strlcat(name, " Jack Mode", name_len);
2715
2716 for (; find_kctl_name(codec, name, idx); idx++)
2717 ;
2718}
2719
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002720static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2721{
2722 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002723 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002724 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2725 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2726 return 2;
2727 }
2728 return 1;
2729}
2730
Takashi Iwai978e77e2013-01-10 16:57:58 +01002731static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2732 hda_nid_t *pins)
2733{
2734 struct hda_gen_spec *spec = codec->spec;
2735 int i;
2736
2737 for (i = 0; i < num_pins; i++) {
2738 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002739 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002740 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002741 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002742 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002743 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002744 get_jack_mode_name(codec, pin, name, sizeof(name));
2745 knew = snd_hda_gen_add_kctl(spec, name,
2746 &out_jack_mode_enum);
2747 if (!knew)
2748 return -ENOMEM;
2749 knew->private_value = pin;
2750 }
2751 }
2752
2753 return 0;
2754}
2755
Takashi Iwai294765582013-01-17 09:52:11 +01002756/*
2757 * input jack mode
2758 */
2759
2760/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2761#define NUM_VREFS 6
2762
2763static const char * const vref_texts[NUM_VREFS] = {
2764 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2765 "", "Mic 80pc Bias", "Mic 100pc Bias"
2766};
2767
2768static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2769{
2770 unsigned int pincap;
2771
2772 pincap = snd_hda_query_pin_caps(codec, pin);
2773 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2774 /* filter out unusual vrefs */
2775 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2776 return pincap;
2777}
2778
2779/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2780static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2781{
2782 unsigned int i, n = 0;
2783
2784 for (i = 0; i < NUM_VREFS; i++) {
2785 if (vref_caps & (1 << i)) {
2786 if (n == item_idx)
2787 return i;
2788 n++;
2789 }
2790 }
2791 return 0;
2792}
2793
2794/* convert back from the vref ctl index to the enum item index */
2795static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2796{
2797 unsigned int i, n = 0;
2798
2799 for (i = 0; i < NUM_VREFS; i++) {
2800 if (i == idx)
2801 return n;
2802 if (vref_caps & (1 << i))
2803 n++;
2804 }
2805 return 0;
2806}
2807
2808static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2809 struct snd_ctl_elem_info *uinfo)
2810{
2811 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2812 hda_nid_t nid = kcontrol->private_value;
2813 unsigned int vref_caps = get_vref_caps(codec, nid);
2814
2815 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2816 vref_texts);
2817 /* set the right text */
2818 strcpy(uinfo->value.enumerated.name,
2819 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2820 return 0;
2821}
2822
2823static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2824 struct snd_ctl_elem_value *ucontrol)
2825{
2826 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2827 hda_nid_t nid = kcontrol->private_value;
2828 unsigned int vref_caps = get_vref_caps(codec, nid);
2829 unsigned int idx;
2830
2831 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2832 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2833 return 0;
2834}
2835
2836static int in_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 unsigned int vref_caps = get_vref_caps(codec, nid);
2842 unsigned int val, idx;
2843
2844 val = snd_hda_codec_get_pin_target(codec, nid);
2845 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2846 if (idx == ucontrol->value.enumerated.item[0])
2847 return 0;
2848
2849 val &= ~AC_PINCTL_VREFEN;
2850 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2851 snd_hda_set_pin_ctl_cache(codec, nid, val);
2852 return 1;
2853}
2854
2855static const struct snd_kcontrol_new in_jack_mode_enum = {
2856 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2857 .info = in_jack_mode_info,
2858 .get = in_jack_mode_get,
2859 .put = in_jack_mode_put,
2860};
2861
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002862static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2863{
2864 struct hda_gen_spec *spec = codec->spec;
2865 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002866 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002867 nitems = hweight32(get_vref_caps(codec, pin));
2868 return nitems ? nitems : 1;
2869}
2870
Takashi Iwai294765582013-01-17 09:52:11 +01002871static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2872{
2873 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002874 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002875 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002876 unsigned int defcfg;
2877
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002878 if (pin == spec->hp_mic_pin)
2879 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002880
2881 /* no jack mode for fixed pins */
2882 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2883 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2884 return 0;
2885
2886 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002887 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002888 return 0;
2889
2890 get_jack_mode_name(codec, pin, name, sizeof(name));
2891 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2892 if (!knew)
2893 return -ENOMEM;
2894 knew->private_value = pin;
2895 return 0;
2896}
2897
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002898/*
2899 * HP/mic shared jack mode
2900 */
2901static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2902 struct snd_ctl_elem_info *uinfo)
2903{
2904 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2905 hda_nid_t nid = kcontrol->private_value;
2906 int out_jacks = get_out_jack_num_items(codec, nid);
2907 int in_jacks = get_in_jack_num_items(codec, nid);
2908 const char *text = NULL;
2909 int idx;
2910
2911 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2912 uinfo->count = 1;
2913 uinfo->value.enumerated.items = out_jacks + in_jacks;
2914 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2915 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2916 idx = uinfo->value.enumerated.item;
2917 if (idx < out_jacks) {
2918 if (out_jacks > 1)
2919 text = out_jack_texts[idx];
2920 else
2921 text = "Headphone Out";
2922 } else {
2923 idx -= out_jacks;
2924 if (in_jacks > 1) {
2925 unsigned int vref_caps = get_vref_caps(codec, nid);
2926 text = vref_texts[get_vref_idx(vref_caps, idx)];
2927 } else
2928 text = "Mic In";
2929 }
2930
2931 strcpy(uinfo->value.enumerated.name, text);
2932 return 0;
2933}
2934
2935static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2936{
2937 int out_jacks = get_out_jack_num_items(codec, nid);
2938 int in_jacks = get_in_jack_num_items(codec, nid);
2939 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2940 int idx = 0;
2941
2942 if (val & PIN_OUT) {
2943 if (out_jacks > 1 && val == PIN_HP)
2944 idx = 1;
2945 } else if (val & PIN_IN) {
2946 idx = out_jacks;
2947 if (in_jacks > 1) {
2948 unsigned int vref_caps = get_vref_caps(codec, nid);
2949 val &= AC_PINCTL_VREFEN;
2950 idx += cvt_from_vref_idx(vref_caps, val);
2951 }
2952 }
2953 return idx;
2954}
2955
2956static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2957 struct snd_ctl_elem_value *ucontrol)
2958{
2959 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2960 hda_nid_t nid = kcontrol->private_value;
2961 ucontrol->value.enumerated.item[0] =
2962 get_cur_hp_mic_jack_mode(codec, nid);
2963 return 0;
2964}
2965
2966static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2967 struct snd_ctl_elem_value *ucontrol)
2968{
2969 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2970 hda_nid_t nid = kcontrol->private_value;
2971 int out_jacks = get_out_jack_num_items(codec, nid);
2972 int in_jacks = get_in_jack_num_items(codec, nid);
2973 unsigned int val, oldval, idx;
2974
2975 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2976 idx = ucontrol->value.enumerated.item[0];
2977 if (oldval == idx)
2978 return 0;
2979
2980 if (idx < out_jacks) {
2981 if (out_jacks > 1)
2982 val = idx ? PIN_HP : PIN_OUT;
2983 else
2984 val = PIN_HP;
2985 } else {
2986 idx -= out_jacks;
2987 if (in_jacks > 1) {
2988 unsigned int vref_caps = get_vref_caps(codec, nid);
2989 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002990 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2991 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002992 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002993 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002994 }
2995 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002996 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002997
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002998 return 1;
2999}
3000
3001static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
3002 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3003 .info = hp_mic_jack_mode_info,
3004 .get = hp_mic_jack_mode_get,
3005 .put = hp_mic_jack_mode_put,
3006};
3007
3008static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
3009{
3010 struct hda_gen_spec *spec = codec->spec;
3011 struct snd_kcontrol_new *knew;
3012
Takashi Iwai5f171ba2013-02-19 18:14:54 +01003013 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
3014 &hp_mic_jack_mode_enum);
3015 if (!knew)
3016 return -ENOMEM;
3017 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01003018 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01003019 return 0;
3020}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003021
3022/*
3023 * Parse input paths
3024 */
3025
Takashi Iwai352f7f92012-12-19 12:52:06 +01003026/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003027static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003028{
3029 struct hda_amp_list *list;
3030
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003031 list = snd_array_new(&spec->loopback_list);
3032 if (!list)
3033 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003034 list->nid = mix;
3035 list->dir = HDA_INPUT;
3036 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003037 spec->loopback.amplist = spec->loopback_list.list;
3038 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003039}
Takashi Iwaicb53c622007-08-10 17:21:45 +02003040
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003041/* return true if either a volume or a mute amp is found for the given
3042 * aamix path; the amp has to be either in the mixer node or its direct leaf
3043 */
3044static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3045 hda_nid_t pin, unsigned int *mix_val,
3046 unsigned int *mute_val)
3047{
3048 int idx, num_conns;
3049 const hda_nid_t *list;
3050 hda_nid_t nid;
3051
3052 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3053 if (idx < 0)
3054 return false;
3055
3056 *mix_val = *mute_val = 0;
3057 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3058 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3059 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3060 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3061 if (*mix_val && *mute_val)
3062 return true;
3063
3064 /* check leaf node */
3065 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3066 if (num_conns < idx)
3067 return false;
3068 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01003069 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3070 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003071 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01003072 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3073 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003074 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3075
3076 return *mix_val || *mute_val;
3077}
3078
Takashi Iwai352f7f92012-12-19 12:52:06 +01003079/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01003080static int new_analog_input(struct hda_codec *codec, int input_idx,
3081 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003082 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003084 struct hda_gen_spec *spec = codec->spec;
3085 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003086 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003087 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003089 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3090 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003091
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003092 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003093 if (!path)
3094 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003095 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01003096 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003097
3098 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003099 if (mix_val) {
3100 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003101 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003103 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 }
3105
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003106 if (mute_val) {
3107 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003108 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003110 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 }
3112
Takashi Iwai352f7f92012-12-19 12:52:06 +01003113 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003114 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003115 err = add_loopback_list(spec, mix_nid, idx);
3116 if (err < 0)
3117 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003118
3119 if (spec->mixer_nid != spec->mixer_merge_nid &&
3120 !spec->loopback_merge_path) {
3121 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3122 spec->mixer_merge_nid, 0);
3123 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003124 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003125 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003126 path->pin_fixed = true; /* static route */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003127 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003128 spec->loopback_merge_path =
3129 snd_hda_get_path_idx(codec, path);
3130 }
3131 }
3132
Takashi Iwai352f7f92012-12-19 12:52:06 +01003133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134}
3135
Takashi Iwai352f7f92012-12-19 12:52:06 +01003136static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003138 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3139 return (pincap & AC_PINCAP_IN) != 0;
3140}
3141
3142/* Parse the codec tree and retrieve ADCs */
3143static int fill_adc_nids(struct hda_codec *codec)
3144{
3145 struct hda_gen_spec *spec = codec->spec;
3146 hda_nid_t nid;
3147 hda_nid_t *adc_nids = spec->adc_nids;
3148 int max_nums = ARRAY_SIZE(spec->adc_nids);
Takashi Iwai7639a062015-03-03 10:07:24 +01003149 int nums = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003150
Takashi Iwai7639a062015-03-03 10:07:24 +01003151 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003152 unsigned int caps = get_wcaps(codec, nid);
3153 int type = get_wcaps_type(caps);
3154
3155 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3156 continue;
3157 adc_nids[nums] = nid;
3158 if (++nums >= max_nums)
3159 break;
3160 }
3161 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003162
3163 /* copy the detected ADCs to all_adcs[] */
3164 spec->num_all_adcs = nums;
3165 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3166
Takashi Iwai352f7f92012-12-19 12:52:06 +01003167 return nums;
3168}
3169
3170/* filter out invalid adc_nids that don't give all active input pins;
3171 * if needed, check whether dynamic ADC-switching is available
3172 */
3173static int check_dyn_adc_switch(struct hda_codec *codec)
3174{
3175 struct hda_gen_spec *spec = codec->spec;
3176 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003177 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003178 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003179
Takashi Iwai352f7f92012-12-19 12:52:06 +01003180 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003181 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003182 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003183 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003184 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003185 break;
3186 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003187 if (i >= imux->num_items) {
3188 ok_bits |= (1 << n);
3189 nums++;
3190 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003191 }
3192
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003193 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003194 /* check whether ADC-switch is possible */
3195 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003196 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003197 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003198 spec->dyn_adc_idx[i] = n;
3199 break;
3200 }
3201 }
3202 }
3203
Takashi Iwai4e76a882014-02-25 12:21:03 +01003204 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003205 spec->dyn_adc_switch = 1;
3206 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003207 /* shrink the invalid adcs and input paths */
3208 nums = 0;
3209 for (n = 0; n < spec->num_adc_nids; n++) {
3210 if (!(ok_bits & (1 << n)))
3211 continue;
3212 if (n != nums) {
3213 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003214 for (i = 0; i < imux->num_items; i++) {
3215 invalidate_nid_path(codec,
3216 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003217 spec->input_paths[i][nums] =
3218 spec->input_paths[i][n];
Hui Wanga8f20fd2017-06-28 08:59:16 +08003219 spec->input_paths[i][n] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01003220 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003221 }
3222 nums++;
3223 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003224 spec->num_adc_nids = nums;
3225 }
3226
Takashi Iwai967303d2013-02-19 17:12:42 +01003227 if (imux->num_items == 1 ||
3228 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003229 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003230 spec->num_adc_nids = 1; /* reduce to a single ADC */
3231 }
3232
3233 /* single index for individual volumes ctls */
3234 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3235 spec->num_adc_nids = 1;
3236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return 0;
3238}
3239
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003240/* parse capture source paths from the given pin and create imux items */
3241static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003242 int cfg_idx, int num_adcs,
3243 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003244{
3245 struct hda_gen_spec *spec = codec->spec;
3246 struct hda_input_mux *imux = &spec->input_mux;
3247 int imux_idx = imux->num_items;
3248 bool imux_added = false;
3249 int c;
3250
3251 for (c = 0; c < num_adcs; c++) {
3252 struct nid_path *path;
3253 hda_nid_t adc = spec->adc_nids[c];
3254
3255 if (!is_reachable_path(codec, pin, adc))
3256 continue;
3257 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3258 if (!path)
3259 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003260 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003261 spec->input_paths[imux_idx][c] =
3262 snd_hda_get_path_idx(codec, path);
3263
3264 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003265 if (spec->hp_mic_pin == pin)
3266 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003267 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003268 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003269 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003270 if (spec->dyn_adc_switch)
3271 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003272 }
3273 }
3274
3275 return 0;
3276}
3277
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003279 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003281
Takashi Iwaic9700422013-01-18 10:17:30 +01003282/* fill the label for each input at first */
3283static int fill_input_pin_labels(struct hda_codec *codec)
3284{
3285 struct hda_gen_spec *spec = codec->spec;
3286 const struct auto_pin_cfg *cfg = &spec->autocfg;
3287 int i;
3288
3289 for (i = 0; i < cfg->num_inputs; i++) {
3290 hda_nid_t pin = cfg->inputs[i].pin;
3291 const char *label;
3292 int j, idx;
3293
3294 if (!is_input_pin(codec, pin))
3295 continue;
3296
3297 label = hda_get_autocfg_input_label(codec, cfg, i);
3298 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003299 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003300 if (spec->input_labels[j] &&
3301 !strcmp(spec->input_labels[j], label)) {
3302 idx = spec->input_label_idxs[j] + 1;
3303 break;
3304 }
3305 }
3306
3307 spec->input_labels[i] = label;
3308 spec->input_label_idxs[i] = idx;
3309 }
3310
3311 return 0;
3312}
3313
Takashi Iwai9dba2052013-01-18 10:01:15 +01003314#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3315
Takashi Iwai352f7f92012-12-19 12:52:06 +01003316static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003317{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003318 struct hda_gen_spec *spec = codec->spec;
3319 const struct auto_pin_cfg *cfg = &spec->autocfg;
3320 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003321 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003322 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003323 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003324
Takashi Iwai352f7f92012-12-19 12:52:06 +01003325 num_adcs = fill_adc_nids(codec);
3326 if (num_adcs < 0)
3327 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003328
Takashi Iwaic9700422013-01-18 10:17:30 +01003329 err = fill_input_pin_labels(codec);
3330 if (err < 0)
3331 return err;
3332
Takashi Iwai352f7f92012-12-19 12:52:06 +01003333 for (i = 0; i < cfg->num_inputs; i++) {
3334 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Takashi Iwai352f7f92012-12-19 12:52:06 +01003336 pin = cfg->inputs[i].pin;
3337 if (!is_input_pin(codec, pin))
3338 continue;
3339
Takashi Iwai2c12c302013-01-10 09:33:29 +01003340 val = PIN_IN;
3341 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3342 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai3e1b0c42015-04-27 10:43:22 +02003343 if (pin != spec->hp_mic_pin &&
3344 !snd_hda_codec_get_pin_target(codec, pin))
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003345 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003346
Takashi Iwai352f7f92012-12-19 12:52:06 +01003347 if (mixer) {
3348 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003349 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003350 spec->input_labels[i],
3351 spec->input_label_idxs[i],
3352 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003353 if (err < 0)
3354 return err;
3355 }
3356 }
3357
Takashi Iwaic9700422013-01-18 10:17:30 +01003358 err = parse_capture_source(codec, pin, i, num_adcs,
3359 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003360 if (err < 0)
3361 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003362
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003363 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003364 err = create_in_jack_mode(codec, pin);
3365 if (err < 0)
3366 return err;
3367 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003368 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003369
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003370 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003371 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003372 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003373 "Stereo Mix", 0);
3374 if (err < 0)
3375 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003376 else
3377 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003378 }
3379
3380 return 0;
3381}
3382
3383
3384/*
3385 * input source mux
3386 */
3387
Takashi Iwaic697b712013-01-07 17:09:26 +01003388/* get the input path specified by the given adc and imux indices */
3389static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003390{
3391 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003392 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3393 snd_BUG();
3394 return NULL;
3395 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003396 if (spec->dyn_adc_switch)
3397 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003398 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003399 snd_BUG();
3400 return NULL;
3401 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003402 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003403}
3404
3405static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3406 unsigned int idx);
3407
3408static int mux_enum_info(struct snd_kcontrol *kcontrol,
3409 struct snd_ctl_elem_info *uinfo)
3410{
3411 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3412 struct hda_gen_spec *spec = codec->spec;
3413 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3414}
3415
3416static int mux_enum_get(struct snd_kcontrol *kcontrol,
3417 struct snd_ctl_elem_value *ucontrol)
3418{
3419 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3420 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003421 /* the ctls are created at once with multiple counts */
3422 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003423
3424 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3425 return 0;
3426}
3427
3428static int mux_enum_put(struct snd_kcontrol *kcontrol,
3429 struct snd_ctl_elem_value *ucontrol)
3430{
3431 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003432 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003433 return mux_select(codec, adc_idx,
3434 ucontrol->value.enumerated.item[0]);
3435}
3436
Takashi Iwai352f7f92012-12-19 12:52:06 +01003437static const struct snd_kcontrol_new cap_src_temp = {
3438 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3439 .name = "Input Source",
3440 .info = mux_enum_info,
3441 .get = mux_enum_get,
3442 .put = mux_enum_put,
3443};
3444
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003445/*
3446 * capture volume and capture switch ctls
3447 */
3448
Takashi Iwai352f7f92012-12-19 12:52:06 +01003449typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3450 struct snd_ctl_elem_value *ucontrol);
3451
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003452/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003453static int cap_put_caller(struct snd_kcontrol *kcontrol,
3454 struct snd_ctl_elem_value *ucontrol,
3455 put_call_t func, int type)
3456{
3457 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3458 struct hda_gen_spec *spec = codec->spec;
3459 const struct hda_input_mux *imux;
3460 struct nid_path *path;
3461 int i, adc_idx, err = 0;
3462
3463 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003464 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003465 mutex_lock(&codec->control_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003466 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003467 path = get_input_path(codec, adc_idx, i);
3468 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003469 continue;
3470 kcontrol->private_value = path->ctls[type];
3471 err = func(kcontrol, ucontrol);
3472 if (err < 0)
Takashi Iwaia551d912015-02-26 12:34:49 +01003473 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003474 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003475 mutex_unlock(&codec->control_mutex);
3476 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003477 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003478 return err;
3479}
3480
3481/* capture volume ctl callbacks */
3482#define cap_vol_info snd_hda_mixer_amp_volume_info
3483#define cap_vol_get snd_hda_mixer_amp_volume_get
3484#define cap_vol_tlv snd_hda_mixer_amp_tlv
3485
3486static int cap_vol_put(struct snd_kcontrol *kcontrol,
3487 struct snd_ctl_elem_value *ucontrol)
3488{
3489 return cap_put_caller(kcontrol, ucontrol,
3490 snd_hda_mixer_amp_volume_put,
3491 NID_PATH_VOL_CTL);
3492}
3493
3494static const struct snd_kcontrol_new cap_vol_temp = {
3495 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3496 .name = "Capture Volume",
3497 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3498 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3499 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3500 .info = cap_vol_info,
3501 .get = cap_vol_get,
3502 .put = cap_vol_put,
3503 .tlv = { .c = cap_vol_tlv },
3504};
3505
3506/* capture switch ctl callbacks */
3507#define cap_sw_info snd_ctl_boolean_stereo_info
3508#define cap_sw_get snd_hda_mixer_amp_switch_get
3509
3510static int cap_sw_put(struct snd_kcontrol *kcontrol,
3511 struct snd_ctl_elem_value *ucontrol)
3512{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003513 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003514 snd_hda_mixer_amp_switch_put,
3515 NID_PATH_MUTE_CTL);
3516}
3517
3518static const struct snd_kcontrol_new cap_sw_temp = {
3519 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3520 .name = "Capture Switch",
3521 .info = cap_sw_info,
3522 .get = cap_sw_get,
3523 .put = cap_sw_put,
3524};
3525
3526static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3527{
3528 hda_nid_t nid;
3529 int i, depth;
3530
3531 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3532 for (depth = 0; depth < 3; depth++) {
3533 if (depth >= path->depth)
3534 return -EINVAL;
3535 i = path->depth - depth - 1;
3536 nid = path->path[i];
3537 if (!path->ctls[NID_PATH_VOL_CTL]) {
3538 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3539 path->ctls[NID_PATH_VOL_CTL] =
3540 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3541 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3542 int idx = path->idx[i];
3543 if (!depth && codec->single_adc_amp)
3544 idx = 0;
3545 path->ctls[NID_PATH_VOL_CTL] =
3546 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3547 }
3548 }
3549 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3550 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3551 path->ctls[NID_PATH_MUTE_CTL] =
3552 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3553 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3554 int idx = path->idx[i];
3555 if (!depth && codec->single_adc_amp)
3556 idx = 0;
3557 path->ctls[NID_PATH_MUTE_CTL] =
3558 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3559 }
3560 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 return 0;
3563}
3564
Takashi Iwai352f7f92012-12-19 12:52:06 +01003565static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003567 struct hda_gen_spec *spec = codec->spec;
3568 struct auto_pin_cfg *cfg = &spec->autocfg;
3569 unsigned int val;
3570 int i;
3571
3572 if (!spec->inv_dmic_split)
3573 return false;
3574 for (i = 0; i < cfg->num_inputs; i++) {
3575 if (cfg->inputs[i].pin != nid)
3576 continue;
3577 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3578 return false;
3579 val = snd_hda_codec_get_pincfg(codec, nid);
3580 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3581 }
3582 return false;
3583}
3584
Takashi Iwaia90229e2013-01-18 14:10:00 +01003585/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003586static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3587 struct snd_ctl_elem_value *ucontrol)
3588{
3589 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3590 struct hda_gen_spec *spec = codec->spec;
3591 int ret;
3592
3593 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3594 if (ret < 0)
3595 return ret;
3596
Takashi Iwaia90229e2013-01-18 14:10:00 +01003597 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003598 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003599
3600 return ret;
3601}
3602
Takashi Iwai352f7f92012-12-19 12:52:06 +01003603static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3604 int idx, bool is_switch, unsigned int ctl,
3605 bool inv_dmic)
3606{
3607 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003608 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003609 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3610 const char *sfx = is_switch ? "Switch" : "Volume";
3611 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003612 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003613
3614 if (!ctl)
3615 return 0;
3616
3617 if (label)
3618 snprintf(tmpname, sizeof(tmpname),
3619 "%s Capture %s", label, sfx);
3620 else
3621 snprintf(tmpname, sizeof(tmpname),
3622 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003623 knew = add_control(spec, type, tmpname, idx,
3624 amp_val_replace_channels(ctl, chs));
3625 if (!knew)
3626 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003627 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003628 knew->put = cap_single_sw_put;
3629 if (!inv_dmic)
3630 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003631
3632 /* Make independent right kcontrol */
3633 if (label)
3634 snprintf(tmpname, sizeof(tmpname),
3635 "Inverted %s Capture %s", label, sfx);
3636 else
3637 snprintf(tmpname, sizeof(tmpname),
3638 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003639 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003640 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003641 if (!knew)
3642 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003643 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003644 knew->put = cap_single_sw_put;
3645 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003646}
3647
3648/* create single (and simple) capture volume and switch controls */
3649static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3650 unsigned int vol_ctl, unsigned int sw_ctl,
3651 bool inv_dmic)
3652{
3653 int err;
3654 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3655 if (err < 0)
3656 return err;
3657 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3658 if (err < 0)
3659 return err;
3660 return 0;
3661}
3662
3663/* create bound capture volume and switch controls */
3664static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3665 unsigned int vol_ctl, unsigned int sw_ctl)
3666{
3667 struct hda_gen_spec *spec = codec->spec;
3668 struct snd_kcontrol_new *knew;
3669
3670 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003671 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003672 if (!knew)
3673 return -ENOMEM;
3674 knew->index = idx;
3675 knew->private_value = vol_ctl;
3676 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3677 }
3678 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003679 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003680 if (!knew)
3681 return -ENOMEM;
3682 knew->index = idx;
3683 knew->private_value = sw_ctl;
3684 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3685 }
3686 return 0;
3687}
3688
3689/* return the vol ctl when used first in the imux list */
3690static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3691{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003692 struct nid_path *path;
3693 unsigned int ctl;
3694 int i;
3695
Takashi Iwaic697b712013-01-07 17:09:26 +01003696 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003697 if (!path)
3698 return 0;
3699 ctl = path->ctls[type];
3700 if (!ctl)
3701 return 0;
3702 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003703 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003704 if (path && path->ctls[type] == ctl)
3705 return 0;
3706 }
3707 return ctl;
3708}
3709
3710/* create individual capture volume and switch controls per input */
3711static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3712{
3713 struct hda_gen_spec *spec = codec->spec;
3714 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003715 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003716
3717 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003718 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003719 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003720
Takashi Iwaic9700422013-01-18 10:17:30 +01003721 idx = imux->items[i].index;
3722 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003723 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003724 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3725
3726 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003727 err = add_single_cap_ctl(codec,
3728 spec->input_labels[idx],
3729 spec->input_label_idxs[idx],
3730 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003731 get_first_cap_ctl(codec, i, type),
3732 inv_dmic);
3733 if (err < 0)
3734 return err;
3735 }
3736 }
3737 return 0;
3738}
3739
3740static int create_capture_mixers(struct hda_codec *codec)
3741{
3742 struct hda_gen_spec *spec = codec->spec;
3743 struct hda_input_mux *imux = &spec->input_mux;
3744 int i, n, nums, err;
3745
3746 if (spec->dyn_adc_switch)
3747 nums = 1;
3748 else
3749 nums = spec->num_adc_nids;
3750
3751 if (!spec->auto_mic && imux->num_items > 1) {
3752 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003753 const char *name;
3754 name = nums > 1 ? "Input Source" : "Capture Source";
3755 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003756 if (!knew)
3757 return -ENOMEM;
3758 knew->count = nums;
3759 }
3760
3761 for (n = 0; n < nums; n++) {
3762 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003763 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003764 bool inv_dmic = false;
3765 int vol, sw;
3766
3767 vol = sw = 0;
3768 for (i = 0; i < imux->num_items; i++) {
3769 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003770 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003771 if (!path)
3772 continue;
3773 parse_capvol_in_path(codec, path);
3774 if (!vol)
3775 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003776 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003777 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003778 if (!same_amp_caps(codec, vol,
3779 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3780 multi_cap_vol = true;
3781 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003782 if (!sw)
3783 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003784 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003785 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003786 if (!same_amp_caps(codec, sw,
3787 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3788 multi_cap_vol = true;
3789 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003790 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3791 inv_dmic = true;
3792 }
3793
3794 if (!multi)
3795 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3796 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003797 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003798 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3799 else
3800 err = create_multi_cap_vol_ctl(codec);
3801 if (err < 0)
3802 return err;
3803 }
3804
3805 return 0;
3806}
3807
3808/*
3809 * add mic boosts if needed
3810 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003811
3812/* check whether the given amp is feasible as a boost volume */
3813static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3814 int dir, int idx)
3815{
3816 unsigned int step;
3817
3818 if (!nid_has_volume(codec, nid, dir) ||
3819 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3820 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3821 return false;
3822
3823 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3824 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3825 if (step < 0x20)
3826 return false;
3827 return true;
3828}
3829
3830/* look for a boost amp in a widget close to the pin */
3831static unsigned int look_for_boost_amp(struct hda_codec *codec,
3832 struct nid_path *path)
3833{
3834 unsigned int val = 0;
3835 hda_nid_t nid;
3836 int depth;
3837
3838 for (depth = 0; depth < 3; depth++) {
3839 if (depth >= path->depth - 1)
3840 break;
3841 nid = path->path[depth];
3842 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3843 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3844 break;
3845 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3846 path->idx[depth])) {
3847 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3848 HDA_INPUT);
3849 break;
3850 }
3851 }
3852
3853 return val;
3854}
3855
Takashi Iwai352f7f92012-12-19 12:52:06 +01003856static int parse_mic_boost(struct hda_codec *codec)
3857{
3858 struct hda_gen_spec *spec = codec->spec;
3859 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003860 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003861 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003862
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003863 if (!spec->num_adc_nids)
3864 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003865
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003866 for (i = 0; i < imux->num_items; i++) {
3867 struct nid_path *path;
3868 unsigned int val;
3869 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003870 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003871
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003872 idx = imux->items[i].index;
3873 if (idx >= imux->num_items)
3874 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003875
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003876 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003877 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003878 continue;
3879
3880 path = get_input_path(codec, 0, i);
3881 if (!path)
3882 continue;
3883
3884 val = look_for_boost_amp(codec, path);
3885 if (!val)
3886 continue;
3887
3888 /* create a boost control */
3889 snprintf(boost_label, sizeof(boost_label),
3890 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003891 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3892 spec->input_label_idxs[idx], val))
3893 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003894
3895 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003896 }
3897 return 0;
3898}
3899
3900/*
3901 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3902 */
3903static void parse_digital(struct hda_codec *codec)
3904{
3905 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003906 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003907 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003908 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003909
3910 /* support multiple SPDIFs; the secondary is set up as a slave */
3911 nums = 0;
3912 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003913 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003914 dig_nid = look_for_dac(codec, pin, true);
3915 if (!dig_nid)
3916 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003917 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003918 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003919 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003920 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003921 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003922 path->pin_fixed = true; /* no jack detection */
Takashi Iwai196c17662013-01-04 15:01:40 +01003923 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003924 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003925 if (!nums) {
3926 spec->multiout.dig_out_nid = dig_nid;
3927 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3928 } else {
3929 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3930 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd5764222014-05-14 17:18:31 +03003931 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003932 spec->slave_dig_outs[nums - 1] = dig_nid;
3933 }
3934 nums++;
3935 }
3936
3937 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003938 pin = spec->autocfg.dig_in_pin;
Takashi Iwai7639a062015-03-03 10:07:24 +01003939 for_each_hda_codec_node(dig_nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003940 unsigned int wcaps = get_wcaps(codec, dig_nid);
3941 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3942 continue;
3943 if (!(wcaps & AC_WCAP_DIGITAL))
3944 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003945 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003946 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003947 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003948 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003949 path->pin_fixed = true; /* no jack */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003950 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003951 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003952 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003953 break;
3954 }
3955 }
3956 }
3957}
3958
3959
3960/*
3961 * input MUX handling
3962 */
3963
3964static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3965
3966/* select the given imux item; either unmute exclusively or select the route */
3967static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3968 unsigned int idx)
3969{
3970 struct hda_gen_spec *spec = codec->spec;
3971 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003972 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003973
3974 imux = &spec->input_mux;
3975 if (!imux->num_items)
3976 return 0;
3977
3978 if (idx >= imux->num_items)
3979 idx = imux->num_items - 1;
3980 if (spec->cur_mux[adc_idx] == idx)
3981 return 0;
3982
Takashi Iwai55196ff2013-01-24 17:32:56 +01003983 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3984 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003985 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003986 if (old_path->active)
3987 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003988
3989 spec->cur_mux[adc_idx] = idx;
3990
Takashi Iwai967303d2013-02-19 17:12:42 +01003991 if (spec->hp_mic)
3992 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003993
3994 if (spec->dyn_adc_switch)
3995 dyn_adc_pcm_resetup(codec, idx);
3996
Takashi Iwaic697b712013-01-07 17:09:26 +01003997 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003998 if (!path)
3999 return 0;
4000 if (path->active)
4001 return 0;
4002 snd_hda_activate_path(codec, path, true, false);
4003 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01004004 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004005 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004006 return 1;
4007}
4008
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004009/* power up/down widgets in the all paths that match with the given NID
4010 * as terminals (either start- or endpoint)
4011 *
4012 * returns the last changed NID, or zero if unchanged.
4013 */
4014static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
4015 int pin_state, int stream_state)
4016{
4017 struct hda_gen_spec *spec = codec->spec;
4018 hda_nid_t last, changed = 0;
4019 struct nid_path *path;
4020 int n;
4021
4022 for (n = 0; n < spec->paths.used; n++) {
4023 path = snd_array_elem(&spec->paths, n);
Bob Copeland81e43962016-06-25 07:58:45 -04004024 if (!path->depth)
4025 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004026 if (path->path[0] == nid ||
4027 path->path[path->depth - 1] == nid) {
4028 bool pin_old = path->pin_enabled;
4029 bool stream_old = path->stream_enabled;
4030
4031 if (pin_state >= 0)
4032 path->pin_enabled = pin_state;
4033 if (stream_state >= 0)
4034 path->stream_enabled = stream_state;
Takashi Iwai6b275b12015-03-20 18:11:05 +01004035 if ((!path->pin_fixed && path->pin_enabled != pin_old)
4036 || path->stream_enabled != stream_old) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004037 last = path_power_update(codec, path, true);
4038 if (last)
4039 changed = last;
4040 }
4041 }
4042 }
4043 return changed;
4044}
4045
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004046/* check the jack status for power control */
4047static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4048{
4049 if (!is_jack_detectable(codec, pin))
4050 return true;
4051 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4052}
4053
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004054/* power up/down the paths of the given pin according to the jack state;
4055 * power = 0/1 : only power up/down if it matches with the jack state,
4056 * < 0 : force power up/down to follow the jack sate
4057 *
4058 * returns the last changed NID, or zero if unchanged.
4059 */
4060static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4061 int power)
4062{
4063 bool on;
4064
Takashi Iwai967b1302015-03-20 18:21:03 +01004065 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004066 return 0;
4067
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004068 on = detect_pin_state(codec, pin);
4069
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004070 if (power >= 0 && on != power)
4071 return 0;
4072 return set_path_power(codec, pin, on, -1);
4073}
4074
4075static void pin_power_callback(struct hda_codec *codec,
4076 struct hda_jack_callback *jack,
4077 bool on)
4078{
Takashi Iwai2ebab402016-02-09 10:23:52 +01004079 if (jack && jack->nid)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004080 sync_power_state_change(codec,
Takashi Iwai2ebab402016-02-09 10:23:52 +01004081 set_pin_power_jack(codec, jack->nid, on));
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004082}
4083
4084/* callback only doing power up -- called at first */
4085static void pin_power_up_callback(struct hda_codec *codec,
4086 struct hda_jack_callback *jack)
4087{
4088 pin_power_callback(codec, jack, true);
4089}
4090
4091/* callback only doing power down -- called at last */
4092static void pin_power_down_callback(struct hda_codec *codec,
4093 struct hda_jack_callback *jack)
4094{
4095 pin_power_callback(codec, jack, false);
4096}
4097
4098/* set up the power up/down callbacks */
4099static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4100 const hda_nid_t *pins, bool on)
4101{
4102 int i;
4103 hda_jack_callback_fn cb =
4104 on ? pin_power_up_callback : pin_power_down_callback;
4105
4106 for (i = 0; i < num_pins && pins[i]; i++) {
4107 if (is_jack_detectable(codec, pins[i]))
4108 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4109 else
4110 set_path_power(codec, pins[i], true, -1);
4111 }
4112}
4113
4114/* enabled power callback to each available I/O pin with jack detections;
4115 * the digital I/O pins are excluded because of the unreliable detectsion
4116 */
4117static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4118{
4119 struct hda_gen_spec *spec = codec->spec;
4120 struct auto_pin_cfg *cfg = &spec->autocfg;
4121 int i;
4122
Takashi Iwai967b1302015-03-20 18:21:03 +01004123 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004124 return;
4125 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4126 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4127 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4128 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4129 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4130 for (i = 0; i < cfg->num_inputs; i++)
4131 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4132}
4133
4134/* sync path power up/down with the jack states of given pins */
4135static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4136 const hda_nid_t *pins)
4137{
4138 int i;
4139
4140 for (i = 0; i < num_pins && pins[i]; i++)
4141 if (is_jack_detectable(codec, pins[i]))
4142 set_pin_power_jack(codec, pins[i], -1);
4143}
4144
4145/* sync path power up/down with pins; called at init and resume */
4146static void sync_all_pin_power_ctls(struct hda_codec *codec)
4147{
4148 struct hda_gen_spec *spec = codec->spec;
4149 struct auto_pin_cfg *cfg = &spec->autocfg;
4150 int i;
4151
Takashi Iwai967b1302015-03-20 18:21:03 +01004152 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004153 return;
4154 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4155 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4156 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4157 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4158 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4159 for (i = 0; i < cfg->num_inputs; i++)
4160 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4161}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004162
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004163/* add fake paths if not present yet */
4164static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4165 int num_pins, const hda_nid_t *pins)
4166{
4167 struct hda_gen_spec *spec = codec->spec;
4168 struct nid_path *path;
4169 int i;
4170
4171 for (i = 0; i < num_pins; i++) {
4172 if (!pins[i])
4173 break;
4174 if (get_nid_path(codec, nid, pins[i], 0))
4175 continue;
4176 path = snd_array_new(&spec->paths);
4177 if (!path)
4178 return -ENOMEM;
4179 memset(path, 0, sizeof(*path));
4180 path->depth = 2;
4181 path->path[0] = nid;
4182 path->path[1] = pins[i];
4183 path->active = true;
4184 }
4185 return 0;
4186}
4187
4188/* create fake paths to all outputs from beep */
4189static int add_fake_beep_paths(struct hda_codec *codec)
4190{
4191 struct hda_gen_spec *spec = codec->spec;
4192 struct auto_pin_cfg *cfg = &spec->autocfg;
4193 hda_nid_t nid = spec->beep_nid;
4194 int err;
4195
Takashi Iwai967b1302015-03-20 18:21:03 +01004196 if (!codec->power_save_node || !nid)
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004197 return 0;
4198 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4199 if (err < 0)
4200 return err;
4201 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4202 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4203 if (err < 0)
4204 return err;
4205 }
4206 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4207 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4208 cfg->speaker_pins);
4209 if (err < 0)
4210 return err;
4211 }
4212 return 0;
4213}
4214
4215/* power up/down beep widget and its output paths */
4216static void beep_power_hook(struct hda_beep *beep, bool on)
4217{
4218 set_path_power(beep->codec, beep->nid, -1, on);
4219}
4220
Takashi Iwai6b275b12015-03-20 18:11:05 +01004221/**
4222 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4223 * @codec: the HDA codec
4224 * @pin: NID of pin to fix
4225 */
4226int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4227{
4228 struct hda_gen_spec *spec = codec->spec;
4229 struct nid_path *path;
4230
4231 path = snd_array_new(&spec->paths);
4232 if (!path)
4233 return -ENOMEM;
4234 memset(path, 0, sizeof(*path));
4235 path->depth = 1;
4236 path->path[0] = pin;
4237 path->active = true;
4238 path->pin_fixed = true;
4239 path->stream_enabled = true;
4240 return 0;
4241}
4242EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4243
Takashi Iwai352f7f92012-12-19 12:52:06 +01004244/*
4245 * Jack detections for HP auto-mute and mic-switch
4246 */
4247
4248/* check each pin in the given array; returns true if any of them is plugged */
4249static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4250{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004251 int i;
4252 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004253
4254 for (i = 0; i < num_pins; i++) {
4255 hda_nid_t nid = pins[i];
4256 if (!nid)
4257 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01004258 /* don't detect pins retasked as inputs */
4259 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4260 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004261 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4262 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004263 }
4264 return present;
4265}
4266
4267/* standard HP/line-out auto-mute helper */
4268static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004269 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004270{
4271 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004272 int i;
4273
4274 for (i = 0; i < num_pins; i++) {
4275 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01004276 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004277 if (!nid)
4278 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004279
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004280 oldval = snd_hda_codec_get_pin_target(codec, nid);
4281 if (oldval & PIN_IN)
4282 continue; /* no mute for inputs */
4283
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004284 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004285 struct nid_path *path;
4286 hda_nid_t mute_nid;
4287
4288 path = snd_hda_get_path_from_idx(codec, paths[i]);
4289 if (!path)
4290 continue;
4291 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4292 if (!mute_nid)
4293 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004294 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004295 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004296 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004297 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004298 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004299 } else {
4300 /* don't reset VREF value in case it's controlling
4301 * the amp (see alc861_fixup_asus_amp_vref_0f())
4302 */
4303 if (spec->keep_vref_in_automute)
4304 val = oldval & ~PIN_HP;
4305 else
4306 val = 0;
4307 if (!mute)
4308 val |= oldval;
4309 /* here we call update_pin_ctl() so that the pinctl is
4310 * changed without changing the pinctl target value;
4311 * the original target value will be still referred at
4312 * the init / resume again
4313 */
4314 update_pin_ctl(codec, nid, val);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004315 }
4316
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01004317 set_pin_eapd(codec, nid, !mute);
Takashi Iwai967b1302015-03-20 18:21:03 +01004318 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004319 bool on = !mute;
4320 if (on)
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004321 on = detect_pin_state(codec, nid);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004322 set_path_power(codec, nid, on, -1);
4323 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004324 }
4325}
4326
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004327/**
4328 * snd_hda_gen_update_outputs - Toggle outputs muting
4329 * @codec: the HDA codec
4330 *
4331 * Update the mute status of all outputs based on the current jack states.
4332 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004333void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004334{
4335 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004336 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004337 int on;
4338
4339 /* Control HP pins/amps depending on master_mute state;
4340 * in general, HP pins/amps control should be enabled in all cases,
4341 * but currently set only for master_mute, just to be safe
4342 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004343 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4344 paths = spec->out_paths;
4345 else
4346 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01004347 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004348 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004349
4350 if (!spec->automute_speaker)
4351 on = 0;
4352 else
4353 on = spec->hp_jack_present | spec->line_jack_present;
4354 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004355 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004356 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4357 paths = spec->out_paths;
4358 else
4359 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004360 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004361 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004362
4363 /* toggle line-out mutes if needed, too */
4364 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4365 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4366 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4367 return;
4368 if (!spec->automute_lo)
4369 on = 0;
4370 else
4371 on = spec->hp_jack_present;
4372 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004373 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004374 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004375 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004376 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004377}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004378EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004379
4380static void call_update_outputs(struct hda_codec *codec)
4381{
4382 struct hda_gen_spec *spec = codec->spec;
4383 if (spec->automute_hook)
4384 spec->automute_hook(codec);
4385 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004386 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004387
4388 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4389 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4390 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004391}
4392
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004393/**
4394 * snd_hda_gen_hp_automute - standard HP-automute helper
4395 * @codec: the HDA codec
4396 * @jack: jack object, NULL for the whole
4397 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004398void snd_hda_gen_hp_automute(struct hda_codec *codec,
4399 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004400{
4401 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004402 hda_nid_t *pins = spec->autocfg.hp_pins;
4403 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004404
Takashi Iwai92603c52013-01-22 07:46:31 +01004405 /* No detection for the first HP jack during indep-HP mode */
4406 if (spec->indep_hp_enabled) {
4407 pins++;
4408 num_pins--;
4409 }
4410
4411 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004412 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4413 return;
4414 call_update_outputs(codec);
4415}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004416EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004417
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004418/**
4419 * snd_hda_gen_line_automute - standard line-out-automute helper
4420 * @codec: the HDA codec
4421 * @jack: jack object, NULL for the whole
4422 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004423void snd_hda_gen_line_automute(struct hda_codec *codec,
4424 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004425{
4426 struct hda_gen_spec *spec = codec->spec;
4427
4428 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4429 return;
4430 /* check LO jack only when it's different from HP */
4431 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4432 return;
4433
4434 spec->line_jack_present =
4435 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4436 spec->autocfg.line_out_pins);
4437 if (!spec->automute_speaker || !spec->detect_lo)
4438 return;
4439 call_update_outputs(codec);
4440}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004441EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004442
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004443/**
4444 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4445 * @codec: the HDA codec
4446 * @jack: jack object, NULL for the whole
4447 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004448void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4449 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004450{
4451 struct hda_gen_spec *spec = codec->spec;
4452 int i;
4453
4454 if (!spec->auto_mic)
4455 return;
4456
4457 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004458 hda_nid_t pin = spec->am_entry[i].pin;
4459 /* don't detect pins retasked as outputs */
4460 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4461 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004462 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004463 mux_select(codec, 0, spec->am_entry[i].idx);
4464 return;
4465 }
4466 }
4467 mux_select(codec, 0, spec->am_entry[0].idx);
4468}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004469EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004470
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004471/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004472static void call_hp_automute(struct hda_codec *codec,
4473 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004474{
4475 struct hda_gen_spec *spec = codec->spec;
4476 if (spec->hp_automute_hook)
4477 spec->hp_automute_hook(codec, jack);
4478 else
4479 snd_hda_gen_hp_automute(codec, jack);
4480}
4481
4482static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004483 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004484{
4485 struct hda_gen_spec *spec = codec->spec;
4486 if (spec->line_automute_hook)
4487 spec->line_automute_hook(codec, jack);
4488 else
4489 snd_hda_gen_line_automute(codec, jack);
4490}
4491
4492static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004493 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004494{
4495 struct hda_gen_spec *spec = codec->spec;
4496 if (spec->mic_autoswitch_hook)
4497 spec->mic_autoswitch_hook(codec, jack);
4498 else
4499 snd_hda_gen_mic_autoswitch(codec, jack);
4500}
4501
Takashi Iwai963afde2013-05-31 15:20:31 +02004502/* update jack retasking */
4503static void update_automute_all(struct hda_codec *codec)
4504{
4505 call_hp_automute(codec, NULL);
4506 call_line_automute(codec, NULL);
4507 call_mic_autoswitch(codec, NULL);
4508}
4509
Takashi Iwai352f7f92012-12-19 12:52:06 +01004510/*
4511 * Auto-Mute mode mixer enum support
4512 */
4513static int automute_mode_info(struct snd_kcontrol *kcontrol,
4514 struct snd_ctl_elem_info *uinfo)
4515{
4516 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4517 struct hda_gen_spec *spec = codec->spec;
4518 static const char * const texts3[] = {
4519 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004520 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
Takashi Iwai352f7f92012-12-19 12:52:06 +01004522 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4523 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4524 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4525}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
Takashi Iwai352f7f92012-12-19 12:52:06 +01004527static int automute_mode_get(struct snd_kcontrol *kcontrol,
4528 struct snd_ctl_elem_value *ucontrol)
4529{
4530 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4531 struct hda_gen_spec *spec = codec->spec;
4532 unsigned int val = 0;
4533 if (spec->automute_speaker)
4534 val++;
4535 if (spec->automute_lo)
4536 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004537
Takashi Iwai352f7f92012-12-19 12:52:06 +01004538 ucontrol->value.enumerated.item[0] = val;
4539 return 0;
4540}
4541
4542static int automute_mode_put(struct snd_kcontrol *kcontrol,
4543 struct snd_ctl_elem_value *ucontrol)
4544{
4545 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4546 struct hda_gen_spec *spec = codec->spec;
4547
4548 switch (ucontrol->value.enumerated.item[0]) {
4549 case 0:
4550 if (!spec->automute_speaker && !spec->automute_lo)
4551 return 0;
4552 spec->automute_speaker = 0;
4553 spec->automute_lo = 0;
4554 break;
4555 case 1:
4556 if (spec->automute_speaker_possible) {
4557 if (!spec->automute_lo && spec->automute_speaker)
4558 return 0;
4559 spec->automute_speaker = 1;
4560 spec->automute_lo = 0;
4561 } else if (spec->automute_lo_possible) {
4562 if (spec->automute_lo)
4563 return 0;
4564 spec->automute_lo = 1;
4565 } else
4566 return -EINVAL;
4567 break;
4568 case 2:
4569 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4570 return -EINVAL;
4571 if (spec->automute_speaker && spec->automute_lo)
4572 return 0;
4573 spec->automute_speaker = 1;
4574 spec->automute_lo = 1;
4575 break;
4576 default:
4577 return -EINVAL;
4578 }
4579 call_update_outputs(codec);
4580 return 1;
4581}
4582
4583static const struct snd_kcontrol_new automute_mode_enum = {
4584 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4585 .name = "Auto-Mute Mode",
4586 .info = automute_mode_info,
4587 .get = automute_mode_get,
4588 .put = automute_mode_put,
4589};
4590
4591static int add_automute_mode_enum(struct hda_codec *codec)
4592{
4593 struct hda_gen_spec *spec = codec->spec;
4594
Takashi Iwai12c93df2012-12-19 14:38:33 +01004595 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004596 return -ENOMEM;
4597 return 0;
4598}
4599
4600/*
4601 * Check the availability of HP/line-out auto-mute;
4602 * Set up appropriately if really supported
4603 */
4604static int check_auto_mute_availability(struct hda_codec *codec)
4605{
4606 struct hda_gen_spec *spec = codec->spec;
4607 struct auto_pin_cfg *cfg = &spec->autocfg;
4608 int present = 0;
4609 int i, err;
4610
Takashi Iwaif72706b2013-01-16 18:20:07 +01004611 if (spec->suppress_auto_mute)
4612 return 0;
4613
Takashi Iwai352f7f92012-12-19 12:52:06 +01004614 if (cfg->hp_pins[0])
4615 present++;
4616 if (cfg->line_out_pins[0])
4617 present++;
4618 if (cfg->speaker_pins[0])
4619 present++;
4620 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004621 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004622
4623 if (!cfg->speaker_pins[0] &&
4624 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4625 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4626 sizeof(cfg->speaker_pins));
4627 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
Takashi Iwai352f7f92012-12-19 12:52:06 +01004630 if (!cfg->hp_pins[0] &&
4631 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4632 memcpy(cfg->hp_pins, cfg->line_out_pins,
4633 sizeof(cfg->hp_pins));
4634 cfg->hp_outs = cfg->line_outs;
4635 }
4636
4637 for (i = 0; i < cfg->hp_outs; i++) {
4638 hda_nid_t nid = cfg->hp_pins[i];
4639 if (!is_jack_detectable(codec, nid))
4640 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004641 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004642 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004643 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004644 spec->detect_hp = 1;
4645 }
4646
4647 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4648 if (cfg->speaker_outs)
4649 for (i = 0; i < cfg->line_outs; i++) {
4650 hda_nid_t nid = cfg->line_out_pins[i];
4651 if (!is_jack_detectable(codec, nid))
4652 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004653 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004654 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004655 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004656 spec->detect_lo = 1;
4657 }
4658 spec->automute_lo_possible = spec->detect_hp;
4659 }
4660
4661 spec->automute_speaker_possible = cfg->speaker_outs &&
4662 (spec->detect_hp || spec->detect_lo);
4663
4664 spec->automute_lo = spec->automute_lo_possible;
4665 spec->automute_speaker = spec->automute_speaker_possible;
4666
4667 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4668 /* create a control for automute mode */
4669 err = add_automute_mode_enum(codec);
4670 if (err < 0)
4671 return err;
4672 }
4673 return 0;
4674}
4675
Takashi Iwai352f7f92012-12-19 12:52:06 +01004676/* check whether all auto-mic pins are valid; setup indices if OK */
4677static bool auto_mic_check_imux(struct hda_codec *codec)
4678{
4679 struct hda_gen_spec *spec = codec->spec;
4680 const struct hda_input_mux *imux;
4681 int i;
4682
4683 imux = &spec->input_mux;
4684 for (i = 0; i < spec->am_num_entries; i++) {
4685 spec->am_entry[i].idx =
4686 find_idx_in_nid_list(spec->am_entry[i].pin,
4687 spec->imux_pins, imux->num_items);
4688 if (spec->am_entry[i].idx < 0)
4689 return false; /* no corresponding imux */
4690 }
4691
4692 /* we don't need the jack detection for the first pin */
4693 for (i = 1; i < spec->am_num_entries; i++)
4694 snd_hda_jack_detect_enable_callback(codec,
4695 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004696 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004697 return true;
4698}
4699
4700static int compare_attr(const void *ap, const void *bp)
4701{
4702 const struct automic_entry *a = ap;
4703 const struct automic_entry *b = bp;
4704 return (int)(a->attr - b->attr);
4705}
4706
4707/*
4708 * Check the availability of auto-mic switch;
4709 * Set up if really supported
4710 */
4711static int check_auto_mic_availability(struct hda_codec *codec)
4712{
4713 struct hda_gen_spec *spec = codec->spec;
4714 struct auto_pin_cfg *cfg = &spec->autocfg;
4715 unsigned int types;
4716 int i, num_pins;
4717
Takashi Iwaid12daf62013-01-07 16:32:11 +01004718 if (spec->suppress_auto_mic)
4719 return 0;
4720
Takashi Iwai352f7f92012-12-19 12:52:06 +01004721 types = 0;
4722 num_pins = 0;
4723 for (i = 0; i < cfg->num_inputs; i++) {
4724 hda_nid_t nid = cfg->inputs[i].pin;
4725 unsigned int attr;
4726 attr = snd_hda_codec_get_pincfg(codec, nid);
4727 attr = snd_hda_get_input_pin_attr(attr);
4728 if (types & (1 << attr))
4729 return 0; /* already occupied */
4730 switch (attr) {
4731 case INPUT_PIN_ATTR_INT:
4732 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4733 return 0; /* invalid type */
4734 break;
4735 case INPUT_PIN_ATTR_UNUSED:
4736 return 0; /* invalid entry */
4737 default:
4738 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4739 return 0; /* invalid type */
4740 if (!spec->line_in_auto_switch &&
4741 cfg->inputs[i].type != AUTO_PIN_MIC)
4742 return 0; /* only mic is allowed */
4743 if (!is_jack_detectable(codec, nid))
4744 return 0; /* no unsol support */
4745 break;
4746 }
4747 if (num_pins >= MAX_AUTO_MIC_PINS)
4748 return 0;
4749 types |= (1 << attr);
4750 spec->am_entry[num_pins].pin = nid;
4751 spec->am_entry[num_pins].attr = attr;
4752 num_pins++;
4753 }
4754
4755 if (num_pins < 2)
4756 return 0;
4757
4758 spec->am_num_entries = num_pins;
4759 /* sort the am_entry in the order of attr so that the pin with a
4760 * higher attr will be selected when the jack is plugged.
4761 */
4762 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4763 compare_attr, NULL);
4764
4765 if (!auto_mic_check_imux(codec))
4766 return 0;
4767
4768 spec->auto_mic = 1;
4769 spec->num_adc_nids = 1;
4770 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004771 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004772 spec->am_entry[0].pin,
4773 spec->am_entry[1].pin,
4774 spec->am_entry[2].pin);
4775
4776 return 0;
4777}
4778
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004779/**
4780 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4781 * into power down
4782 * @codec: the HDA codec
4783 * @nid: NID to evalute
4784 * @power_state: target power state
4785 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004786unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004787 hda_nid_t nid,
4788 unsigned int power_state)
4789{
Takashi Iwaib6c09b32015-04-09 10:25:03 +02004790 struct hda_gen_spec *spec = codec->spec;
4791
4792 if (!spec->power_down_unused && !codec->power_save_node)
4793 return power_state;
Takashi Iwai7639a062015-03-03 10:07:24 +01004794 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004795 return power_state;
4796 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4797 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004798 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004799 return power_state;
4800 return AC_PWRST_D3;
4801}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004802EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004803
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004804/* mute all aamix inputs initially; parse up to the first leaves */
4805static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4806{
4807 int i, nums;
4808 const hda_nid_t *conn;
4809 bool has_amp;
4810
4811 nums = snd_hda_get_conn_list(codec, mix, &conn);
4812 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4813 for (i = 0; i < nums; i++) {
4814 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004815 update_amp(codec, mix, HDA_INPUT, i,
4816 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004817 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004818 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4819 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004820 }
4821}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004822
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004823/**
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004824 * snd_hda_gen_stream_pm - Stream power management callback
4825 * @codec: the HDA codec
4826 * @nid: audio widget
4827 * @on: power on/off flag
4828 *
Takashi Iwai967b1302015-03-20 18:21:03 +01004829 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004830 */
4831void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4832{
Takashi Iwai967b1302015-03-20 18:21:03 +01004833 if (codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004834 set_path_power(codec, nid, -1, on);
4835}
4836EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4837
4838/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004839 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4840 * set up the hda_gen_spec
4841 * @codec: the HDA codec
4842 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004843 *
4844 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004845 * or a negative error code
4846 */
4847int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004848 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004849{
4850 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004851 int err;
4852
Takashi Iwai1c70a582013-01-11 17:48:22 +01004853 parse_user_hints(codec);
4854
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004855 if (spec->mixer_nid && !spec->mixer_merge_nid)
4856 spec->mixer_merge_nid = spec->mixer_nid;
4857
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004858 if (cfg != &spec->autocfg) {
4859 spec->autocfg = *cfg;
4860 cfg = &spec->autocfg;
4861 }
4862
Takashi Iwai98bd1112013-03-22 14:53:50 +01004863 if (!spec->main_out_badness)
4864 spec->main_out_badness = &hda_main_out_badness;
4865 if (!spec->extra_out_badness)
4866 spec->extra_out_badness = &hda_extra_out_badness;
4867
David Henningsson6fc4cb92013-01-16 15:58:45 +01004868 fill_all_dac_nids(codec);
4869
Takashi Iwai352f7f92012-12-19 12:52:06 +01004870 if (!cfg->line_outs) {
4871 if (cfg->dig_outs || cfg->dig_in_pin) {
4872 spec->multiout.max_channels = 2;
4873 spec->no_analog = 1;
4874 goto dig_only;
4875 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004876 if (!cfg->num_inputs && !cfg->dig_in_pin)
4877 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004878 }
4879
4880 if (!spec->no_primary_hp &&
4881 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4882 cfg->line_outs <= cfg->hp_outs) {
4883 /* use HP as primary out */
4884 cfg->speaker_outs = cfg->line_outs;
4885 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4886 sizeof(cfg->speaker_pins));
4887 cfg->line_outs = cfg->hp_outs;
4888 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4889 cfg->hp_outs = 0;
4890 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4891 cfg->line_out_type = AUTO_PIN_HP_OUT;
4892 }
4893
4894 err = parse_output_paths(codec);
4895 if (err < 0)
4896 return err;
4897 err = create_multi_channel_mode(codec);
4898 if (err < 0)
4899 return err;
4900 err = create_multi_out_ctls(codec, cfg);
4901 if (err < 0)
4902 return err;
4903 err = create_hp_out_ctls(codec);
4904 if (err < 0)
4905 return err;
4906 err = create_speaker_out_ctls(codec);
4907 if (err < 0)
4908 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004909 err = create_indep_hp_ctls(codec);
4910 if (err < 0)
4911 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004912 err = create_loopback_mixing_ctl(codec);
4913 if (err < 0)
4914 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004915 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004916 if (err < 0)
4917 return err;
4918 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004919 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004920 return err;
4921
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004922 /* add power-down pin callbacks at first */
4923 add_all_pin_power_ctls(codec, false);
4924
Takashi Iwaia07a9492013-01-07 16:44:06 +01004925 spec->const_channel_count = spec->ext_channel_count;
4926 /* check the multiple speaker and headphone pins */
4927 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4928 spec->const_channel_count = max(spec->const_channel_count,
4929 cfg->speaker_outs * 2);
4930 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4931 spec->const_channel_count = max(spec->const_channel_count,
4932 cfg->hp_outs * 2);
4933 spec->multiout.max_channels = max(spec->ext_channel_count,
4934 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004935
4936 err = check_auto_mute_availability(codec);
4937 if (err < 0)
4938 return err;
4939
4940 err = check_dyn_adc_switch(codec);
4941 if (err < 0)
4942 return err;
4943
Takashi Iwai967303d2013-02-19 17:12:42 +01004944 err = check_auto_mic_availability(codec);
4945 if (err < 0)
4946 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004947
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004948 /* add stereo mix if available and not enabled yet */
4949 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01004950 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4951 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004952 err = parse_capture_source(codec, spec->mixer_nid,
4953 CFG_IDX_MIX, spec->num_all_adcs,
4954 "Stereo Mix", 0);
4955 if (err < 0)
4956 return err;
4957 }
4958
4959
Takashi Iwai352f7f92012-12-19 12:52:06 +01004960 err = create_capture_mixers(codec);
4961 if (err < 0)
4962 return err;
4963
4964 err = parse_mic_boost(codec);
4965 if (err < 0)
4966 return err;
4967
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004968 /* create "Headphone Mic Jack Mode" if no input selection is
4969 * available (or user specifies add_jack_modes hint)
4970 */
4971 if (spec->hp_mic_pin &&
4972 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4973 spec->add_jack_modes)) {
4974 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4975 if (err < 0)
4976 return err;
4977 }
4978
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004979 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004980 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4981 err = create_out_jack_modes(codec, cfg->line_outs,
4982 cfg->line_out_pins);
4983 if (err < 0)
4984 return err;
4985 }
4986 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4987 err = create_out_jack_modes(codec, cfg->hp_outs,
4988 cfg->hp_pins);
4989 if (err < 0)
4990 return err;
4991 }
4992 }
4993
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004994 /* add power-up pin callbacks at last */
4995 add_all_pin_power_ctls(codec, true);
4996
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004997 /* mute all aamix input initially */
4998 if (spec->mixer_nid)
4999 mute_all_mixer_nid(codec, spec->mixer_nid);
5000
Takashi Iwai352f7f92012-12-19 12:52:06 +01005001 dig_only:
5002 parse_digital(codec);
5003
Takashi Iwai49fb1892015-05-27 08:37:19 +02005004 if (spec->power_down_unused || codec->power_save_node) {
Takashi Iwai24fef902015-04-09 10:28:15 +02005005 if (!codec->power_filter)
5006 codec->power_filter = snd_hda_gen_path_power_filter;
Takashi Iwai49fb1892015-05-27 08:37:19 +02005007 if (!codec->patch_ops.stream_pm)
5008 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
5009 }
Takashi Iwai55196ff2013-01-24 17:32:56 +01005010
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005011 if (!spec->no_analog && spec->beep_nid) {
5012 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
5013 if (err < 0)
5014 return err;
Takashi Iwai967b1302015-03-20 18:21:03 +01005015 if (codec->beep && codec->power_save_node) {
Takashi Iwai5ccf8352015-03-18 09:23:10 +01005016 err = add_fake_beep_paths(codec);
5017 if (err < 0)
5018 return err;
5019 codec->beep->power_hook = beep_power_hook;
5020 }
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005021 }
5022
Takashi Iwai352f7f92012-12-19 12:52:06 +01005023 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005025EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
5027
5028/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005029 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005031
5032/* slave controls for virtual master */
5033static const char * const slave_pfxs[] = {
5034 "Front", "Surround", "Center", "LFE", "Side",
5035 "Headphone", "Speaker", "Mono", "Line Out",
5036 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01005037 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5038 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02005039 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01005040 NULL,
5041};
5042
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005043/**
5044 * snd_hda_gen_build_controls - Build controls from the parsed results
5045 * @codec: the HDA codec
5046 *
5047 * Pass this to build_controls patch_ops.
5048 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005049int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005051 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
Takashi Iwai36502d02012-12-19 15:15:10 +01005054 if (spec->kctls.used) {
5055 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5056 if (err < 0)
5057 return err;
5058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
Takashi Iwai352f7f92012-12-19 12:52:06 +01005060 if (spec->multiout.dig_out_nid) {
5061 err = snd_hda_create_dig_out_ctls(codec,
5062 spec->multiout.dig_out_nid,
5063 spec->multiout.dig_out_nid,
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005064 spec->pcm_rec[1]->pcm_type);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005065 if (err < 0)
5066 return err;
5067 if (!spec->no_analog) {
5068 err = snd_hda_create_spdif_share_sw(codec,
5069 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 if (err < 0)
5071 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005072 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 }
5074 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005075 if (spec->dig_in_nid) {
5076 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5077 if (err < 0)
5078 return err;
5079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
Takashi Iwai352f7f92012-12-19 12:52:06 +01005081 /* if we have no master control, let's create it */
Takashi Iwai74803162017-04-10 17:37:34 +02005082 if (!spec->no_analog && !spec->suppress_vmaster &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01005083 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01005084 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01005085 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005086 "Playback Volume");
5087 if (err < 0)
5088 return err;
5089 }
Takashi Iwai74803162017-04-10 17:37:34 +02005090 if (!spec->no_analog && !spec->suppress_vmaster &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01005091 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5092 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5093 NULL, slave_pfxs,
5094 "Playback Switch",
5095 true, &spec->vmaster_mute.sw_kctl);
5096 if (err < 0)
5097 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02005098 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01005099 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5100 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02005101 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5102 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
Takashi Iwai352f7f92012-12-19 12:52:06 +01005105 free_kctls(spec); /* no longer needed */
5106
Takashi Iwai352f7f92012-12-19 12:52:06 +01005107 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5108 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 return err;
5110
5111 return 0;
5112}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005113EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005114
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115
5116/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005117 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005120static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5121 struct hda_codec *codec,
5122 struct snd_pcm_substream *substream,
5123 int action)
5124{
5125 struct hda_gen_spec *spec = codec->spec;
5126 if (spec->pcm_playback_hook)
5127 spec->pcm_playback_hook(hinfo, codec, substream, action);
5128}
5129
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005130static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5131 struct hda_codec *codec,
5132 struct snd_pcm_substream *substream,
5133 int action)
5134{
5135 struct hda_gen_spec *spec = codec->spec;
5136 if (spec->pcm_capture_hook)
5137 spec->pcm_capture_hook(hinfo, codec, substream, action);
5138}
5139
Takashi Iwai352f7f92012-12-19 12:52:06 +01005140/*
5141 * Analog playback callbacks
5142 */
5143static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5144 struct hda_codec *codec,
5145 struct snd_pcm_substream *substream)
5146{
5147 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005148 int err;
5149
5150 mutex_lock(&spec->pcm_mutex);
5151 err = snd_hda_multi_out_analog_open(codec,
5152 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005153 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005154 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005155 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005156 call_pcm_playback_hook(hinfo, codec, substream,
5157 HDA_GEN_PCM_ACT_OPEN);
5158 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005159 mutex_unlock(&spec->pcm_mutex);
5160 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005161}
5162
5163static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01005164 struct hda_codec *codec,
5165 unsigned int stream_tag,
5166 unsigned int format,
5167 struct snd_pcm_substream *substream)
5168{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005169 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005170 int err;
5171
5172 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5173 stream_tag, format, substream);
5174 if (!err)
5175 call_pcm_playback_hook(hinfo, codec, substream,
5176 HDA_GEN_PCM_ACT_PREPARE);
5177 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005178}
Takashi Iwai97ec5582006-03-21 11:29:07 +01005179
Takashi Iwai352f7f92012-12-19 12:52:06 +01005180static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5181 struct hda_codec *codec,
5182 struct snd_pcm_substream *substream)
5183{
5184 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005185 int err;
5186
5187 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5188 if (!err)
5189 call_pcm_playback_hook(hinfo, codec, substream,
5190 HDA_GEN_PCM_ACT_CLEANUP);
5191 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005192}
5193
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005194static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5195 struct hda_codec *codec,
5196 struct snd_pcm_substream *substream)
5197{
5198 struct hda_gen_spec *spec = codec->spec;
5199 mutex_lock(&spec->pcm_mutex);
5200 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005201 call_pcm_playback_hook(hinfo, codec, substream,
5202 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005203 mutex_unlock(&spec->pcm_mutex);
5204 return 0;
5205}
5206
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005207static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5208 struct hda_codec *codec,
5209 struct snd_pcm_substream *substream)
5210{
5211 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5212 return 0;
5213}
5214
5215static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5216 struct hda_codec *codec,
5217 unsigned int stream_tag,
5218 unsigned int format,
5219 struct snd_pcm_substream *substream)
5220{
5221 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5222 call_pcm_capture_hook(hinfo, codec, substream,
5223 HDA_GEN_PCM_ACT_PREPARE);
5224 return 0;
5225}
5226
5227static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5228 struct hda_codec *codec,
5229 struct snd_pcm_substream *substream)
5230{
5231 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5232 call_pcm_capture_hook(hinfo, codec, substream,
5233 HDA_GEN_PCM_ACT_CLEANUP);
5234 return 0;
5235}
5236
5237static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5238 struct hda_codec *codec,
5239 struct snd_pcm_substream *substream)
5240{
5241 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5242 return 0;
5243}
5244
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005245static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5246 struct hda_codec *codec,
5247 struct snd_pcm_substream *substream)
5248{
5249 struct hda_gen_spec *spec = codec->spec;
5250 int err = 0;
5251
5252 mutex_lock(&spec->pcm_mutex);
Takashi Iwaid1f15e02015-07-08 09:22:10 +02005253 if (spec->indep_hp && !spec->indep_hp_enabled)
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005254 err = -EBUSY;
5255 else
5256 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005257 call_pcm_playback_hook(hinfo, codec, substream,
5258 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005259 mutex_unlock(&spec->pcm_mutex);
5260 return err;
5261}
5262
5263static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5264 struct hda_codec *codec,
5265 struct snd_pcm_substream *substream)
5266{
5267 struct hda_gen_spec *spec = codec->spec;
5268 mutex_lock(&spec->pcm_mutex);
5269 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005270 call_pcm_playback_hook(hinfo, codec, substream,
5271 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005272 mutex_unlock(&spec->pcm_mutex);
5273 return 0;
5274}
5275
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005276static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5277 struct hda_codec *codec,
5278 unsigned int stream_tag,
5279 unsigned int format,
5280 struct snd_pcm_substream *substream)
5281{
5282 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5283 call_pcm_playback_hook(hinfo, codec, substream,
5284 HDA_GEN_PCM_ACT_PREPARE);
5285 return 0;
5286}
5287
5288static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5289 struct hda_codec *codec,
5290 struct snd_pcm_substream *substream)
5291{
5292 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5293 call_pcm_playback_hook(hinfo, codec, substream,
5294 HDA_GEN_PCM_ACT_CLEANUP);
5295 return 0;
5296}
5297
Takashi Iwai352f7f92012-12-19 12:52:06 +01005298/*
5299 * Digital out
5300 */
5301static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5302 struct hda_codec *codec,
5303 struct snd_pcm_substream *substream)
5304{
5305 struct hda_gen_spec *spec = codec->spec;
5306 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5307}
5308
5309static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5310 struct hda_codec *codec,
5311 unsigned int stream_tag,
5312 unsigned int format,
5313 struct snd_pcm_substream *substream)
5314{
5315 struct hda_gen_spec *spec = codec->spec;
5316 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5317 stream_tag, format, substream);
5318}
5319
5320static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5321 struct hda_codec *codec,
5322 struct snd_pcm_substream *substream)
5323{
5324 struct hda_gen_spec *spec = codec->spec;
5325 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5326}
5327
5328static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5329 struct hda_codec *codec,
5330 struct snd_pcm_substream *substream)
5331{
5332 struct hda_gen_spec *spec = codec->spec;
5333 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5334}
5335
5336/*
5337 * Analog capture
5338 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005339#define alt_capture_pcm_open capture_pcm_open
5340#define alt_capture_pcm_close capture_pcm_close
5341
Takashi Iwai352f7f92012-12-19 12:52:06 +01005342static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5343 struct hda_codec *codec,
5344 unsigned int stream_tag,
5345 unsigned int format,
5346 struct snd_pcm_substream *substream)
5347{
5348 struct hda_gen_spec *spec = codec->spec;
5349
5350 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01005351 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005352 call_pcm_capture_hook(hinfo, codec, substream,
5353 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005354 return 0;
5355}
5356
Takashi Iwai352f7f92012-12-19 12:52:06 +01005357static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5358 struct hda_codec *codec,
5359 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01005360{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005361 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01005362
Takashi Iwai352f7f92012-12-19 12:52:06 +01005363 snd_hda_codec_cleanup_stream(codec,
5364 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005365 call_pcm_capture_hook(hinfo, codec, substream,
5366 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005367 return 0;
5368}
5369
Takashi Iwai352f7f92012-12-19 12:52:06 +01005370/*
5371 */
5372static const struct hda_pcm_stream pcm_analog_playback = {
5373 .substreams = 1,
5374 .channels_min = 2,
5375 .channels_max = 8,
5376 /* NID is set in build_pcms */
5377 .ops = {
5378 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005379 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005380 .prepare = playback_pcm_prepare,
5381 .cleanup = playback_pcm_cleanup
5382 },
5383};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384
Takashi Iwai352f7f92012-12-19 12:52:06 +01005385static const struct hda_pcm_stream pcm_analog_capture = {
5386 .substreams = 1,
5387 .channels_min = 2,
5388 .channels_max = 2,
5389 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005390 .ops = {
5391 .open = capture_pcm_open,
5392 .close = capture_pcm_close,
5393 .prepare = capture_pcm_prepare,
5394 .cleanup = capture_pcm_cleanup
5395 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005396};
5397
5398static const struct hda_pcm_stream pcm_analog_alt_playback = {
5399 .substreams = 1,
5400 .channels_min = 2,
5401 .channels_max = 2,
5402 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005403 .ops = {
5404 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005405 .close = alt_playback_pcm_close,
5406 .prepare = alt_playback_pcm_prepare,
5407 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005408 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005409};
5410
5411static const struct hda_pcm_stream pcm_analog_alt_capture = {
5412 .substreams = 2, /* can be overridden */
5413 .channels_min = 2,
5414 .channels_max = 2,
5415 /* NID is set in build_pcms */
5416 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005417 .open = alt_capture_pcm_open,
5418 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005419 .prepare = alt_capture_pcm_prepare,
5420 .cleanup = alt_capture_pcm_cleanup
5421 },
5422};
5423
5424static const struct hda_pcm_stream pcm_digital_playback = {
5425 .substreams = 1,
5426 .channels_min = 2,
5427 .channels_max = 2,
5428 /* NID is set in build_pcms */
5429 .ops = {
5430 .open = dig_playback_pcm_open,
5431 .close = dig_playback_pcm_close,
5432 .prepare = dig_playback_pcm_prepare,
5433 .cleanup = dig_playback_pcm_cleanup
5434 },
5435};
5436
5437static const struct hda_pcm_stream pcm_digital_capture = {
5438 .substreams = 1,
5439 .channels_min = 2,
5440 .channels_max = 2,
5441 /* NID is set in build_pcms */
5442};
5443
5444/* Used by build_pcms to flag that a PCM has no playback stream */
5445static const struct hda_pcm_stream pcm_null_stream = {
5446 .substreams = 0,
5447 .channels_min = 0,
5448 .channels_max = 0,
5449};
5450
5451/*
5452 * dynamic changing ADC PCM streams
5453 */
5454static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5455{
5456 struct hda_gen_spec *spec = codec->spec;
5457 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5458
5459 if (spec->cur_adc && spec->cur_adc != new_adc) {
5460 /* stream is running, let's swap the current ADC */
5461 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5462 spec->cur_adc = new_adc;
5463 snd_hda_codec_setup_stream(codec, new_adc,
5464 spec->cur_adc_stream_tag, 0,
5465 spec->cur_adc_format);
5466 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005468 return false;
5469}
5470
5471/* analog capture with dynamic dual-adc changes */
5472static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5473 struct hda_codec *codec,
5474 unsigned int stream_tag,
5475 unsigned int format,
5476 struct snd_pcm_substream *substream)
5477{
5478 struct hda_gen_spec *spec = codec->spec;
5479 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5480 spec->cur_adc_stream_tag = stream_tag;
5481 spec->cur_adc_format = format;
5482 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
Takashi Iwai4f29efc2016-04-12 15:16:24 +02005483 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005484 return 0;
5485}
5486
5487static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5488 struct hda_codec *codec,
5489 struct snd_pcm_substream *substream)
5490{
5491 struct hda_gen_spec *spec = codec->spec;
5492 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5493 spec->cur_adc = 0;
Takashi Iwai4f29efc2016-04-12 15:16:24 +02005494 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005495 return 0;
5496}
5497
5498static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5499 .substreams = 1,
5500 .channels_min = 2,
5501 .channels_max = 2,
5502 .nid = 0, /* fill later */
5503 .ops = {
5504 .prepare = dyn_adc_capture_pcm_prepare,
5505 .cleanup = dyn_adc_capture_pcm_cleanup
5506 },
5507};
5508
Takashi Iwaif873e532012-12-20 16:58:39 +01005509static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5510 const char *chip_name)
5511{
5512 char *p;
5513
5514 if (*str)
5515 return;
5516 strlcpy(str, chip_name, len);
5517
5518 /* drop non-alnum chars after a space */
5519 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5520 if (!isalnum(p[1])) {
5521 *p = 0;
5522 break;
5523 }
5524 }
5525 strlcat(str, sfx, len);
5526}
5527
Takashi Iwaifb83b632015-03-16 23:34:34 +01005528/* copy PCM stream info from @default_str, and override non-NULL entries
5529 * from @spec_str and @nid
5530 */
5531static void setup_pcm_stream(struct hda_pcm_stream *str,
5532 const struct hda_pcm_stream *default_str,
5533 const struct hda_pcm_stream *spec_str,
5534 hda_nid_t nid)
5535{
5536 *str = *default_str;
5537 if (nid)
5538 str->nid = nid;
5539 if (spec_str) {
5540 if (spec_str->substreams)
5541 str->substreams = spec_str->substreams;
5542 if (spec_str->channels_min)
5543 str->channels_min = spec_str->channels_min;
5544 if (spec_str->channels_max)
5545 str->channels_max = spec_str->channels_max;
5546 if (spec_str->rates)
5547 str->rates = spec_str->rates;
5548 if (spec_str->formats)
5549 str->formats = spec_str->formats;
5550 if (spec_str->maxbps)
5551 str->maxbps = spec_str->maxbps;
5552 }
5553}
5554
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005555/**
5556 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5557 * @codec: the HDA codec
5558 *
5559 * Pass this to build_pcms patch_ops.
5560 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005561int snd_hda_gen_build_pcms(struct hda_codec *codec)
5562{
5563 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005564 struct hda_pcm *info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005565 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
Takashi Iwai352f7f92012-12-19 12:52:06 +01005567 if (spec->no_analog)
5568 goto skip_analog;
5569
Takashi Iwaif873e532012-12-20 16:58:39 +01005570 fill_pcm_stream_name(spec->stream_name_analog,
5571 sizeof(spec->stream_name_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005572 " Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005573 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5574 if (!info)
5575 return -ENOMEM;
5576 spec->pcm_rec[0] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005577
5578 if (spec->multiout.num_dacs > 0) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005579 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5580 &pcm_analog_playback,
5581 spec->stream_analog_playback,
5582 spec->multiout.dac_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005583 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5584 spec->multiout.max_channels;
5585 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5586 spec->autocfg.line_outs == 2)
5587 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5588 snd_pcm_2_1_chmaps;
5589 }
5590 if (spec->num_adc_nids) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005591 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5592 (spec->dyn_adc_switch ?
5593 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5594 spec->stream_analog_capture,
5595 spec->adc_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005596 }
5597
Takashi Iwai352f7f92012-12-19 12:52:06 +01005598 skip_analog:
5599 /* SPDIF for stream index #1 */
5600 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005601 fill_pcm_stream_name(spec->stream_name_digital,
5602 sizeof(spec->stream_name_digital),
Takashi Iwai7639a062015-03-03 10:07:24 +01005603 " Digital", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005604 info = snd_hda_codec_pcm_new(codec, "%s",
5605 spec->stream_name_digital);
5606 if (!info)
5607 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005608 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005609 spec->pcm_rec[1] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005610 if (spec->dig_out_type)
5611 info->pcm_type = spec->dig_out_type;
5612 else
5613 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005614 if (spec->multiout.dig_out_nid)
5615 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5616 &pcm_digital_playback,
5617 spec->stream_digital_playback,
5618 spec->multiout.dig_out_nid);
5619 if (spec->dig_in_nid)
5620 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5621 &pcm_digital_capture,
5622 spec->stream_digital_capture,
5623 spec->dig_in_nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005624 }
5625
5626 if (spec->no_analog)
5627 return 0;
5628
5629 /* If the use of more than one ADC is requested for the current
5630 * model, configure a second analog capture-only PCM.
5631 */
5632 have_multi_adcs = (spec->num_adc_nids > 1) &&
5633 !spec->dyn_adc_switch && !spec->auto_mic;
5634 /* Additional Analaog capture for index #2 */
5635 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005636 fill_pcm_stream_name(spec->stream_name_alt_analog,
5637 sizeof(spec->stream_name_alt_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005638 " Alt Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005639 info = snd_hda_codec_pcm_new(codec, "%s",
5640 spec->stream_name_alt_analog);
5641 if (!info)
5642 return -ENOMEM;
5643 spec->pcm_rec[2] = info;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005644 if (spec->alt_dac_nid)
5645 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5646 &pcm_analog_alt_playback,
5647 spec->stream_analog_alt_playback,
5648 spec->alt_dac_nid);
5649 else
5650 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5651 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005652 if (have_multi_adcs) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005653 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5654 &pcm_analog_alt_capture,
5655 spec->stream_analog_alt_capture,
5656 spec->adc_nids[1]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005657 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5658 spec->num_adc_nids - 1;
5659 } else {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005660 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5661 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 }
5664
5665 return 0;
5666}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005667EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005668
5669
5670/*
5671 * Standard auto-parser initializations
5672 */
5673
Takashi Iwaid4156932013-01-07 10:08:02 +01005674/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005675static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005676{
5677 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005678 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005679
Takashi Iwai196c17662013-01-04 15:01:40 +01005680 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005681 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005682 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005683 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005684 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005685 snd_hda_activate_path(codec, path, path->active,
5686 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005687 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005688}
5689
5690/* initialize primary output paths */
5691static void init_multi_out(struct hda_codec *codec)
5692{
5693 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005694 int i;
5695
Takashi Iwaid4156932013-01-07 10:08:02 +01005696 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005697 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005698}
5699
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005700
Takashi Iwai2c12c302013-01-10 09:33:29 +01005701static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005702{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005703 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005704
Takashi Iwaid4156932013-01-07 10:08:02 +01005705 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005706 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005707}
5708
5709/* initialize hp and speaker paths */
5710static void init_extra_out(struct hda_codec *codec)
5711{
5712 struct hda_gen_spec *spec = codec->spec;
5713
5714 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005715 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005716 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5717 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005718 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005719}
5720
5721/* initialize multi-io paths */
5722static void init_multi_io(struct hda_codec *codec)
5723{
5724 struct hda_gen_spec *spec = codec->spec;
5725 int i;
5726
5727 for (i = 0; i < spec->multi_ios; i++) {
5728 hda_nid_t pin = spec->multi_io[i].pin;
5729 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005730 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005731 if (!path)
5732 continue;
5733 if (!spec->multi_io[i].ctl_in)
5734 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005735 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005736 snd_hda_activate_path(codec, path, path->active,
5737 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005738 }
5739}
5740
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005741static void init_aamix_paths(struct hda_codec *codec)
5742{
5743 struct hda_gen_spec *spec = codec->spec;
5744
5745 if (!spec->have_aamix_ctl)
5746 return;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01005747 if (!has_aamix_out_paths(spec))
5748 return;
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005749 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5750 spec->aamix_out_paths[0],
5751 spec->autocfg.line_out_type);
5752 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5753 spec->aamix_out_paths[1],
5754 AUTO_PIN_HP_OUT);
5755 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5756 spec->aamix_out_paths[2],
5757 AUTO_PIN_SPEAKER_OUT);
5758}
5759
Takashi Iwai352f7f92012-12-19 12:52:06 +01005760/* set up input pins and loopback paths */
5761static void init_analog_input(struct hda_codec *codec)
5762{
5763 struct hda_gen_spec *spec = codec->spec;
5764 struct auto_pin_cfg *cfg = &spec->autocfg;
5765 int i;
5766
5767 for (i = 0; i < cfg->num_inputs; i++) {
5768 hda_nid_t nid = cfg->inputs[i].pin;
5769 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005770 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005771
5772 /* init loopback inputs */
5773 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005774 resume_path_from_idx(codec, spec->loopback_paths[i]);
5775 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005776 }
5777 }
5778}
5779
5780/* initialize ADC paths */
5781static void init_input_src(struct hda_codec *codec)
5782{
5783 struct hda_gen_spec *spec = codec->spec;
5784 struct hda_input_mux *imux = &spec->input_mux;
5785 struct nid_path *path;
5786 int i, c, nums;
5787
5788 if (spec->dyn_adc_switch)
5789 nums = 1;
5790 else
5791 nums = spec->num_adc_nids;
5792
5793 for (c = 0; c < nums; c++) {
5794 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005795 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005796 if (path) {
5797 bool active = path->active;
5798 if (i == spec->cur_mux[c])
5799 active = true;
5800 snd_hda_activate_path(codec, path, active, false);
5801 }
5802 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005803 if (spec->hp_mic)
5804 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005805 }
5806
Takashi Iwai352f7f92012-12-19 12:52:06 +01005807 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005808 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005809}
5810
5811/* set right pin controls for digital I/O */
5812static void init_digital(struct hda_codec *codec)
5813{
5814 struct hda_gen_spec *spec = codec->spec;
5815 int i;
5816 hda_nid_t pin;
5817
Takashi Iwaid4156932013-01-07 10:08:02 +01005818 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005819 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005820 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005821 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005822 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005823 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005824 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005825}
5826
Takashi Iwai973e4972012-12-20 15:16:09 +01005827/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5828 * invalid unsol tags by some reason
5829 */
5830static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5831{
5832 int i;
5833
5834 for (i = 0; i < codec->init_pins.used; i++) {
5835 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5836 hda_nid_t nid = pin->nid;
5837 if (is_jack_detectable(codec, nid) &&
5838 !snd_hda_jack_tbl_get(codec, nid))
5839 snd_hda_codec_update_cache(codec, nid, 0,
5840 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5841 }
5842}
5843
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005844/**
5845 * snd_hda_gen_init - initialize the generic spec
5846 * @codec: the HDA codec
5847 *
5848 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005849 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005850int snd_hda_gen_init(struct hda_codec *codec)
5851{
5852 struct hda_gen_spec *spec = codec->spec;
5853
5854 if (spec->init_hook)
5855 spec->init_hook(codec);
5856
5857 snd_hda_apply_verbs(codec);
5858
5859 init_multi_out(codec);
5860 init_extra_out(codec);
5861 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005862 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005863 init_analog_input(codec);
5864 init_input_src(codec);
5865 init_digital(codec);
5866
Takashi Iwai973e4972012-12-20 15:16:09 +01005867 clear_unsol_on_unused_pins(codec);
5868
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005869 sync_all_pin_power_ctls(codec);
5870
Takashi Iwai352f7f92012-12-19 12:52:06 +01005871 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005872 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005873
Takashi Iwaia551d912015-02-26 12:34:49 +01005874 regcache_sync(codec->core.regmap);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005875
Takashi Iwai352f7f92012-12-19 12:52:06 +01005876 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5877 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5878
5879 hda_call_check_power_status(codec, 0x01);
5880 return 0;
5881}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005882EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005883
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005884/**
5885 * snd_hda_gen_free - free the generic spec
5886 * @codec: the HDA codec
5887 *
5888 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005889 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005890void snd_hda_gen_free(struct hda_codec *codec)
5891{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005892 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005893 snd_hda_gen_spec_free(codec->spec);
5894 kfree(codec->spec);
5895 codec->spec = NULL;
5896}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005897EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005898
5899#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005900/**
5901 * snd_hda_gen_check_power_status - check the loopback power save state
5902 * @codec: the HDA codec
5903 * @nid: NID to inspect
5904 *
5905 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005906 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005907int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5908{
5909 struct hda_gen_spec *spec = codec->spec;
5910 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5911}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005912EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005913#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005914
5915
5916/*
5917 * the generic codec support
5918 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919
Takashi Iwai352f7f92012-12-19 12:52:06 +01005920static const struct hda_codec_ops generic_patch_ops = {
5921 .build_controls = snd_hda_gen_build_controls,
5922 .build_pcms = snd_hda_gen_build_pcms,
5923 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005924 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005925 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005926#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005927 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005928#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929};
5930
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005931/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005932 * snd_hda_parse_generic_codec - Generic codec parser
5933 * @codec: the HDA codec
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005934 */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005935static int snd_hda_parse_generic_codec(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005937 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 int err;
5939
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005940 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005941 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005943 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005946 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5947 if (err < 0)
5948 return err;
5949
5950 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005951 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 goto error;
5953
5954 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 return 0;
5956
Takashi Iwai352f7f92012-12-19 12:52:06 +01005957error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005958 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 return err;
5960}
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005961
Takashi Iwaib9a94a92015-10-01 16:20:04 +02005962static const struct hda_device_id snd_hda_id_generic[] = {
5963 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005964 {} /* terminator */
5965};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02005966MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005967
5968static struct hda_codec_driver generic_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02005969 .id = snd_hda_id_generic,
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005970};
5971
5972module_hda_codec_driver(generic_driver);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005973
5974MODULE_LICENSE("GPL");
5975MODULE_DESCRIPTION("Generic HD-audio codec parser");