blob: 443832870a44582d632deb585ee15b503e5982c7 [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 Iwai1c70a582013-01-11 17:48:22 +0100199
200 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
201 spec->mixer_nid = val;
202}
203
204/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100205 * pin control value accesses
206 */
207
208#define update_pin_ctl(codec, pin, val) \
209 snd_hda_codec_update_cache(codec, pin, 0, \
210 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
211
212/* restore the pinctl based on the cached value */
213static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
214{
215 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
216}
217
218/* set the pinctl target value and write it if requested */
219static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
220 unsigned int val, bool do_write)
221{
222 if (!pin)
223 return;
224 val = snd_hda_correct_pin_ctl(codec, pin, val);
225 snd_hda_codec_set_pin_target(codec, pin, val);
226 if (do_write)
227 update_pin_ctl(codec, pin, val);
228}
229
230/* set pinctl target values for all given pins */
231static void set_pin_targets(struct hda_codec *codec, int num_pins,
232 hda_nid_t *pins, unsigned int val)
233{
234 int i;
235 for (i = 0; i < num_pins; i++)
236 set_pin_target(codec, pins[i], val, false);
237}
238
239/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100240 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100243/* return the position of NID in the list, or -1 if not found */
244static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
245{
246 int i;
247 for (i = 0; i < nums; i++)
248 if (list[i] == nid)
249 return i;
250 return -1;
251}
252
253/* return true if the given NID is contained in the path */
254static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
255{
256 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
257}
258
Takashi Iwaif5172a72013-01-04 13:19:55 +0100259static struct nid_path *get_nid_path(struct hda_codec *codec,
260 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100261 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100263 struct hda_gen_spec *spec = codec->spec;
264 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Takashi Iwai352f7f92012-12-19 12:52:06 +0100266 for (i = 0; i < spec->paths.used; i++) {
267 struct nid_path *path = snd_array_elem(&spec->paths, i);
268 if (path->depth <= 0)
269 continue;
270 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100271 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100272 if (!anchor_nid ||
273 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
274 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100275 return path;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 return NULL;
279}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100280
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100281/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100282 * snd_hda_get_path_idx - get the index number corresponding to the path
283 * instance
284 * @codec: the HDA codec
285 * @path: nid_path object
286 *
287 * The returned index starts from 1, i.e. the actual array index with offset 1,
288 * and zero is handled as an invalid path
Takashi Iwai196c17662013-01-04 15:01:40 +0100289 */
290int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
291{
292 struct hda_gen_spec *spec = codec->spec;
293 struct nid_path *array = spec->paths.list;
294 ssize_t idx;
295
296 if (!spec->paths.used)
297 return 0;
298 idx = path - array;
299 if (idx < 0 || idx >= spec->paths.used)
300 return 0;
301 return idx + 1;
302}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100303EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100304
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100305/**
306 * snd_hda_get_path_from_idx - get the path instance corresponding to the
307 * given index number
308 * @codec: the HDA codec
309 * @idx: the path index
310 */
Takashi Iwai196c17662013-01-04 15:01:40 +0100311struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
312{
313 struct hda_gen_spec *spec = codec->spec;
314
315 if (idx <= 0 || idx > spec->paths.used)
316 return NULL;
317 return snd_array_elem(&spec->paths, idx - 1);
318}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100319EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100320
Takashi Iwai352f7f92012-12-19 12:52:06 +0100321/* check whether the given DAC is already found in any existing paths */
322static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
323{
324 struct hda_gen_spec *spec = codec->spec;
325 int i;
326
327 for (i = 0; i < spec->paths.used; i++) {
328 struct nid_path *path = snd_array_elem(&spec->paths, i);
329 if (path->path[0] == nid)
330 return true;
331 }
332 return false;
333}
334
335/* check whether the given two widgets can be connected */
336static bool is_reachable_path(struct hda_codec *codec,
337 hda_nid_t from_nid, hda_nid_t to_nid)
338{
339 if (!from_nid || !to_nid)
340 return false;
341 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
342}
343
344/* nid, dir and idx */
345#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
346
347/* check whether the given ctl is already assigned in any path elements */
348static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
349{
350 struct hda_gen_spec *spec = codec->spec;
351 int i;
352
353 val &= AMP_VAL_COMPARE_MASK;
354 for (i = 0; i < spec->paths.used; i++) {
355 struct nid_path *path = snd_array_elem(&spec->paths, i);
356 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
357 return true;
358 }
359 return false;
360}
361
362/* check whether a control with the given (nid, dir, idx) was assigned */
363static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100364 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100365{
366 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100367 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100368}
369
Takashi Iwai4e76a882014-02-25 12:21:03 +0100370static void print_nid_path(struct hda_codec *codec,
371 const char *pfx, struct nid_path *path)
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100372{
373 char buf[40];
Joe Perchesd82353e2014-07-05 13:02:02 -0700374 char *pos = buf;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100375 int i;
376
Joe Perchesd82353e2014-07-05 13:02:02 -0700377 *pos = 0;
378 for (i = 0; i < path->depth; i++)
379 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
380 pos != buf ? ":" : "",
381 path->path[i]);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100382
Joe Perchesd82353e2014-07-05 13:02:02 -0700383 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100384}
385
Takashi Iwai352f7f92012-12-19 12:52:06 +0100386/* called recursively */
387static bool __parse_nid_path(struct hda_codec *codec,
388 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100389 int anchor_nid, struct nid_path *path,
390 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100391{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100392 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100393 int i, nums;
394
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100395 if (to_nid == anchor_nid)
396 anchor_nid = 0; /* anchor passed */
397 else if (to_nid == (hda_nid_t)(-anchor_nid))
398 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100399
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100400 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100401 for (i = 0; i < nums; i++) {
402 if (conn[i] != from_nid) {
403 /* special case: when from_nid is 0,
404 * try to find an empty DAC
405 */
406 if (from_nid ||
407 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
408 is_dac_already_used(codec, conn[i]))
409 continue;
410 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100411 /* anchor is not requested or already passed? */
412 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100413 goto found;
414 }
415 if (depth >= MAX_NID_PATH_DEPTH)
416 return false;
417 for (i = 0; i < nums; i++) {
418 unsigned int type;
419 type = get_wcaps_type(get_wcaps(codec, conn[i]));
420 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
421 type == AC_WID_PIN)
422 continue;
423 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100424 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100425 goto found;
426 }
427 return false;
428
429 found:
430 path->path[path->depth] = conn[i];
431 path->idx[path->depth + 1] = i;
432 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
433 path->multi[path->depth + 1] = 1;
434 path->depth++;
435 return true;
436}
437
Takashi Iwaic4a58c32015-12-08 11:48:39 +0100438/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100439 * snd_hda_parse_nid_path - parse the widget path from the given nid to
440 * the target nid
441 * @codec: the HDA codec
442 * @from_nid: the NID where the path start from
443 * @to_nid: the NID where the path ends at
444 * @anchor_nid: the anchor indication
445 * @path: the path object to store the result
446 *
447 * Returns true if a matching path is found.
448 *
449 * The parsing behavior depends on parameters:
Takashi Iwai352f7f92012-12-19 12:52:06 +0100450 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100451 * when @anchor_nid is set to a positive value, only paths through the widget
452 * with the given value are evaluated.
453 * when @anchor_nid is set to a negative value, paths through the widget
454 * with the negative of given value are excluded, only other paths are chosen.
455 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100456 */
Takashi Iwaic4a58c32015-12-08 11:48:39 +0100457static bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100458 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100459 struct nid_path *path)
460{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100461 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100462 path->path[path->depth] = to_nid;
463 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100464 return true;
465 }
466 return false;
467}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100469/**
470 * snd_hda_add_new_path - parse the path between the given NIDs and
471 * add to the path list
472 * @codec: the HDA codec
473 * @from_nid: the NID where the path start from
474 * @to_nid: the NID where the path ends at
475 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
476 *
477 * If no valid path is found, returns NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100479struct nid_path *
480snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100481 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100483 struct hda_gen_spec *spec = codec->spec;
484 struct nid_path *path;
485
486 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
487 return NULL;
488
Takashi Iwaif5172a72013-01-04 13:19:55 +0100489 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100490 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100491 if (path)
492 return path;
493
Takashi Iwai352f7f92012-12-19 12:52:06 +0100494 path = snd_array_new(&spec->paths);
495 if (!path)
496 return NULL;
497 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100498 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100499 return path;
500 /* push back */
501 spec->paths.used--;
502 return NULL;
503}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100504EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100505
Takashi Iwai980428c2013-01-09 09:28:20 +0100506/* clear the given path as invalid so that it won't be picked up later */
507static void invalidate_nid_path(struct hda_codec *codec, int idx)
508{
509 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
510 if (!path)
511 return;
512 memset(path, 0, sizeof(*path));
513}
514
Takashi Iwai36907392013-12-10 17:29:26 +0100515/* return a DAC if paired to the given pin by codec driver */
516static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
517{
518 struct hda_gen_spec *spec = codec->spec;
519 const hda_nid_t *list = spec->preferred_dacs;
520
521 if (!list)
522 return 0;
523 for (; *list; list += 2)
524 if (*list == pin)
525 return list[1];
526 return 0;
527}
528
Takashi Iwai352f7f92012-12-19 12:52:06 +0100529/* look for an empty DAC slot */
530static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
531 bool is_digital)
532{
533 struct hda_gen_spec *spec = codec->spec;
534 bool cap_digital;
535 int i;
536
537 for (i = 0; i < spec->num_all_dacs; i++) {
538 hda_nid_t nid = spec->all_dacs[i];
539 if (!nid || is_dac_already_used(codec, nid))
540 continue;
541 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
542 if (is_digital != cap_digital)
543 continue;
544 if (is_reachable_path(codec, nid, pin))
545 return nid;
546 }
547 return 0;
548}
549
550/* replace the channels in the composed amp value with the given number */
551static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
552{
553 val &= ~(0x3U << 16);
554 val |= chs << 16;
555 return val;
556}
557
David Henningsson99a55922013-01-16 15:58:44 +0100558static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
559 hda_nid_t nid2, int dir)
560{
561 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
562 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
563 return (query_amp_caps(codec, nid1, dir) ==
564 query_amp_caps(codec, nid2, dir));
565}
566
Takashi Iwai352f7f92012-12-19 12:52:06 +0100567/* look for a widget suitable for assigning a mute switch in the path */
568static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
569 struct nid_path *path)
570{
571 int i;
572
573 for (i = path->depth - 1; i >= 0; i--) {
574 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
575 return path->path[i];
576 if (i != path->depth - 1 && i != 0 &&
577 nid_has_mute(codec, path->path[i], HDA_INPUT))
578 return path->path[i];
579 }
580 return 0;
581}
582
583/* look for a widget suitable for assigning a volume ctl in the path */
584static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
585 struct nid_path *path)
586{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100587 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100588 int i;
589
590 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100591 hda_nid_t nid = path->path[i];
592 if ((spec->out_vol_mask >> nid) & 1)
593 continue;
594 if (nid_has_volume(codec, nid, HDA_OUTPUT))
595 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100596 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100601 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100603
604/* can have the amp-in capability? */
605static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100607 hda_nid_t nid = path->path[idx];
608 unsigned int caps = get_wcaps(codec, nid);
609 unsigned int type = get_wcaps_type(caps);
610
611 if (!(caps & AC_WCAP_IN_AMP))
612 return false;
613 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
614 return false;
615 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Takashi Iwai352f7f92012-12-19 12:52:06 +0100618/* can have the amp-out capability? */
619static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100621 hda_nid_t nid = path->path[idx];
622 unsigned int caps = get_wcaps(codec, nid);
623 unsigned int type = get_wcaps_type(caps);
624
625 if (!(caps & AC_WCAP_OUT_AMP))
626 return false;
627 if (type == AC_WID_PIN && !idx) /* only for output pins */
628 return false;
629 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Takashi Iwai352f7f92012-12-19 12:52:06 +0100632/* check whether the given (nid,dir,idx) is active */
633static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100634 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100636 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100637 int type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai352f7f92012-12-19 12:52:06 +0100638 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Takashi Iwai7639a062015-03-03 10:07:24 +0100640 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100641 return true;
642
Takashi Iwai352f7f92012-12-19 12:52:06 +0100643 for (n = 0; n < spec->paths.used; n++) {
644 struct nid_path *path = snd_array_elem(&spec->paths, n);
645 if (!path->active)
646 continue;
Takashi Iwai967b1302015-03-20 18:21:03 +0100647 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100648 if (!path->stream_enabled)
649 continue;
650 /* ignore unplugged paths except for DAC/ADC */
Takashi Iwai6b275b12015-03-20 18:11:05 +0100651 if (!(path->pin_enabled || path->pin_fixed) &&
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100652 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
653 continue;
654 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100655 for (i = 0; i < path->depth; i++) {
656 if (path->path[i] == nid) {
Takashi Iwai9d2b48f2015-08-24 10:45:27 +0200657 if (dir == HDA_OUTPUT || idx == -1 ||
658 path->idx[i] == idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100659 return true;
660 break;
661 }
662 }
663 }
664 return false;
665}
666
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200667/* check whether the NID is referred by any active paths */
668#define is_active_nid_for_any(codec, nid) \
Takashi Iwai9d2b48f2015-08-24 10:45:27 +0200669 is_active_nid(codec, nid, HDA_OUTPUT, -1)
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200670
Takashi Iwai352f7f92012-12-19 12:52:06 +0100671/* get the default amp value for the target state */
672static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100673 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100674{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100675 unsigned int val = 0;
676
Takashi Iwai352f7f92012-12-19 12:52:06 +0100677 if (caps & AC_AMPCAP_NUM_STEPS) {
678 /* set to 0dB */
679 if (enable)
680 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
681 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200682 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100683 if (!enable)
684 val |= HDA_AMP_MUTE;
685 }
686 return val;
687}
688
Takashi Iwaicc261732015-03-16 10:18:08 +0100689/* is this a stereo widget or a stereo-to-mono mix? */
690static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
691{
692 unsigned int wcaps = get_wcaps(codec, nid);
693 hda_nid_t conn;
694
695 if (wcaps & AC_WCAP_STEREO)
696 return true;
697 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
698 return false;
699 if (snd_hda_get_num_conns(codec, nid) != 1)
700 return false;
701 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
702 return false;
703 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
704}
705
Takashi Iwai352f7f92012-12-19 12:52:06 +0100706/* initialize the amp value (only at the first time) */
707static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
708{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100709 unsigned int caps = query_amp_caps(codec, nid, dir);
710 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwaief403ed2015-03-12 08:30:11 +0100711
Takashi Iwaicc261732015-03-16 10:18:08 +0100712 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100713 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
714 else
715 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
716}
717
718/* update the amp, doing in stereo or mono depending on NID */
719static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
720 unsigned int mask, unsigned int val)
721{
Takashi Iwaicc261732015-03-16 10:18:08 +0100722 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100723 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
724 mask, val);
725 else
726 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
727 mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100728}
729
Takashi Iwai8999bf02013-01-18 11:01:33 +0100730/* calculate amp value mask we can modify;
731 * if the given amp is controlled by mixers, don't touch it
732 */
733static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
734 hda_nid_t nid, int dir, int idx,
735 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100736{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100737 unsigned int mask = 0xff;
738
Takashi Iwaif69910d2013-08-08 09:32:37 +0200739 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100740 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
741 mask &= ~0x80;
742 }
743 if (caps & AC_AMPCAP_NUM_STEPS) {
744 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
745 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
746 mask &= ~0x7f;
747 }
748 return mask;
749}
750
751static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
752 int idx, int idx_to_check, bool enable)
753{
754 unsigned int caps;
755 unsigned int mask, val;
756
Takashi Iwai8999bf02013-01-18 11:01:33 +0100757 caps = query_amp_caps(codec, nid, dir);
758 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
759 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
760 if (!mask)
761 return;
762
763 val &= mask;
Takashi Iwaief403ed2015-03-12 08:30:11 +0100764 update_amp(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100765}
766
Takashi Iwaie7fdd522015-12-08 17:00:42 +0100767static void check_and_activate_amp(struct hda_codec *codec, hda_nid_t nid,
768 int dir, int idx, int idx_to_check,
769 bool enable)
770{
771 /* check whether the given amp is still used by others */
772 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
773 return;
774 activate_amp(codec, nid, dir, idx, idx_to_check, enable);
775}
776
Takashi Iwai352f7f92012-12-19 12:52:06 +0100777static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
778 int i, bool enable)
779{
780 hda_nid_t nid = path->path[i];
781 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwaie7fdd522015-12-08 17:00:42 +0100782 check_and_activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100783}
784
785static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
786 int i, bool enable, bool add_aamix)
787{
788 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100789 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100790 int n, nums, idx;
791 int type;
792 hda_nid_t nid = path->path[i];
793
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100794 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100795 type = get_wcaps_type(get_wcaps(codec, nid));
796 if (type == AC_WID_PIN ||
797 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
798 nums = 1;
799 idx = 0;
800 } else
801 idx = path->idx[i];
802
803 for (n = 0; n < nums; n++)
804 init_amp(codec, nid, HDA_INPUT, n);
805
Takashi Iwai352f7f92012-12-19 12:52:06 +0100806 /* here is a little bit tricky in comparison with activate_amp_out();
807 * when aa-mixer is available, we need to enable the path as well
808 */
809 for (n = 0; n < nums; n++) {
Takashi Iwaie7fdd522015-12-08 17:00:42 +0100810 if (n != idx) {
811 if (conn[n] != spec->mixer_merge_nid)
812 continue;
813 /* when aamix is disabled, force to off */
814 if (!add_aamix) {
815 activate_amp(codec, nid, HDA_INPUT, n, n, false);
816 continue;
817 }
818 }
819 check_and_activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821}
822
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100823/* sync power of each widget in the the given path */
824static hda_nid_t path_power_update(struct hda_codec *codec,
825 struct nid_path *path,
826 bool allow_powerdown)
827{
828 hda_nid_t nid, changed = 0;
Takashi Iwai50fd4982016-04-17 09:39:41 +0200829 int i, state, power;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100830
831 for (i = 0; i < path->depth; i++) {
832 nid = path->path[i];
Takashi Iwai2206dc92015-04-09 10:18:31 +0200833 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
834 continue;
Takashi Iwai7639a062015-03-03 10:07:24 +0100835 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100836 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100837 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
838 state = AC_PWRST_D0;
839 else
840 state = AC_PWRST_D3;
Takashi Iwai50fd4982016-04-17 09:39:41 +0200841 power = snd_hda_codec_read(codec, nid, 0,
842 AC_VERB_GET_POWER_STATE, 0);
843 if (power != (state | (state << 4))) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100844 snd_hda_codec_write(codec, nid, 0,
845 AC_VERB_SET_POWER_STATE, state);
846 changed = nid;
Takashi Iwai48f4b3a2015-05-20 06:49:37 +0200847 /* all known codecs seem to be capable to handl
848 * widgets state even in D3, so far.
849 * if any new codecs need to restore the widget
850 * states after D0 transition, call the function
851 * below.
852 */
853#if 0 /* disabled */
Takashi Iwaid545a572015-04-04 12:17:28 +0200854 if (state == AC_PWRST_D0)
855 snd_hdac_regmap_sync_node(&codec->core, nid);
Takashi Iwai48f4b3a2015-05-20 06:49:37 +0200856#endif
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100857 }
858 }
859 return changed;
860}
861
862/* do sync with the last power state change */
863static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
864{
865 if (nid) {
866 msleep(10);
867 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
868 }
869}
870
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100871/**
872 * snd_hda_activate_path - activate or deactivate the given path
873 * @codec: the HDA codec
874 * @path: the path to activate/deactivate
875 * @enable: flag to activate or not
876 * @add_aamix: enable the input from aamix NID
877 *
878 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100880void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
881 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100883 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100884 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Takashi Iwaic7cd0ef2015-08-24 10:52:06 +0200886 path->active = enable;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100887
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100888 /* make sure the widget is powered up */
Takashi Iwai967b1302015-03-20 18:21:03 +0100889 if (enable && (spec->power_down_unused || codec->power_save_node))
890 path_power_update(codec, path, codec->power_save_node);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100891
Takashi Iwai352f7f92012-12-19 12:52:06 +0100892 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100893 hda_nid_t nid = path->path[i];
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100894
Takashi Iwai352f7f92012-12-19 12:52:06 +0100895 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100896 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100897 AC_VERB_SET_CONNECT_SEL,
898 path->idx[i]);
899 if (has_amp_in(codec, path, i))
900 activate_amp_in(codec, path, i, enable, add_aamix);
901 if (has_amp_out(codec, path, i))
902 activate_amp_out(codec, path, i, enable);
903 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100904}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100905EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100906
Takashi Iwai55196ff2013-01-24 17:32:56 +0100907/* if the given path is inactive, put widgets into D3 (only if suitable) */
908static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
909{
910 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100911
Takashi Iwai967b1302015-03-20 18:21:03 +0100912 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
Takashi Iwai55196ff2013-01-24 17:32:56 +0100913 return;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100914 sync_power_state_change(codec, path_power_update(codec, path, true));
Takashi Iwai55196ff2013-01-24 17:32:56 +0100915}
916
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100917/* turn on/off EAPD on the given pin */
918static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
919{
920 struct hda_gen_spec *spec = codec->spec;
921 if (spec->own_eapd_ctl ||
922 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
923 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200924 if (spec->keep_eapd_on && !enable)
925 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100926 if (codec->inv_eapd)
927 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100928 snd_hda_codec_update_cache(codec, pin, 0,
929 AC_VERB_SET_EAPD_BTLENABLE,
930 enable ? 0x02 : 0x00);
931}
932
Takashi Iwai3e367f12013-01-23 17:07:23 +0100933/* re-initialize the path specified by the given path index */
934static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
935{
936 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
937 if (path)
938 snd_hda_activate_path(codec, path, path->active, false);
939}
940
Takashi Iwai352f7f92012-12-19 12:52:06 +0100941
942/*
943 * Helper functions for creating mixer ctl elements
944 */
945
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200946static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
947 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200948static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
949 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200950
Takashi Iwai352f7f92012-12-19 12:52:06 +0100951enum {
952 HDA_CTL_WIDGET_VOL,
953 HDA_CTL_WIDGET_MUTE,
954 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100955};
956static const struct snd_kcontrol_new control_templates[] = {
957 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200958 /* only the put callback is replaced for handling the special mute */
959 {
960 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
961 .subdevice = HDA_SUBDEV_AMP_FLAG,
962 .info = snd_hda_mixer_amp_switch_info,
963 .get = snd_hda_mixer_amp_switch_get,
964 .put = hda_gen_mixer_mute_put, /* replaced */
965 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
966 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200967 {
968 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
969 .info = snd_hda_mixer_amp_switch_info,
970 .get = snd_hda_mixer_bind_switch_get,
971 .put = hda_gen_bind_mute_put, /* replaced */
972 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
973 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100974};
975
976/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100977static struct snd_kcontrol_new *
978add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100979 int cidx, unsigned long val)
980{
981 struct snd_kcontrol_new *knew;
982
Takashi Iwai12c93df2012-12-19 14:38:33 +0100983 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100984 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100985 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100986 knew->index = cidx;
987 if (get_amp_nid_(val))
988 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
989 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100990 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100991}
992
993static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
994 const char *pfx, const char *dir,
995 const char *sfx, int cidx, unsigned long val)
996{
Takashi Iwai975cc022013-06-28 11:56:49 +0200997 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100998 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100999 if (!add_control(spec, type, name, cidx, val))
1000 return -ENOMEM;
1001 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001002}
1003
1004#define add_pb_vol_ctrl(spec, type, pfx, val) \
1005 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1006#define add_pb_sw_ctrl(spec, type, pfx, val) \
1007 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1008#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1009 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1010#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1011 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1012
1013static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1014 unsigned int chs, struct nid_path *path)
1015{
1016 unsigned int val;
1017 if (!path)
1018 return 0;
1019 val = path->ctls[NID_PATH_VOL_CTL];
1020 if (!val)
1021 return 0;
1022 val = amp_val_replace_channels(val, chs);
1023 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1024}
1025
1026/* return the channel bits suitable for the given path->ctls[] */
1027static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1028 int type)
1029{
1030 int chs = 1; /* mono (left only) */
1031 if (path) {
1032 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1033 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1034 chs = 3; /* stereo */
1035 }
1036 return chs;
1037}
1038
1039static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1040 struct nid_path *path)
1041{
1042 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1043 return add_vol_ctl(codec, pfx, cidx, chs, path);
1044}
1045
1046/* create a mute-switch for the given mixer widget;
1047 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1048 */
1049static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1050 unsigned int chs, struct nid_path *path)
1051{
1052 unsigned int val;
1053 int type = HDA_CTL_WIDGET_MUTE;
1054
1055 if (!path)
1056 return 0;
1057 val = path->ctls[NID_PATH_MUTE_CTL];
1058 if (!val)
1059 return 0;
1060 val = amp_val_replace_channels(val, chs);
1061 if (get_amp_direction_(val) == HDA_INPUT) {
1062 hda_nid_t nid = get_amp_nid_(val);
1063 int nums = snd_hda_get_num_conns(codec, nid);
1064 if (nums > 1) {
1065 type = HDA_CTL_BIND_MUTE;
1066 val |= nums << 19;
1067 }
1068 }
1069 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1070}
1071
1072static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1073 int cidx, struct nid_path *path)
1074{
1075 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1076 return add_sw_ctl(codec, pfx, cidx, chs, path);
1077}
1078
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001079/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001080static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1081 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001082{
1083 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1084 struct hda_gen_spec *spec = codec->spec;
1085
1086 if (spec->auto_mute_via_amp) {
1087 hda_nid_t nid = get_amp_nid(kcontrol);
1088 bool enabled = !((spec->mute_bits >> nid) & 1);
1089 ucontrol->value.integer.value[0] &= enabled;
1090 ucontrol->value.integer.value[1] &= enabled;
1091 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001092}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001093
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001094static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1095 struct snd_ctl_elem_value *ucontrol)
1096{
1097 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001098 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1099}
1100
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001101static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1102 struct snd_ctl_elem_value *ucontrol)
1103{
1104 sync_auto_mute_bits(kcontrol, ucontrol);
1105 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1106}
1107
Takashi Iwai247d85e2013-01-17 16:18:11 +01001108/* any ctl assigned to the path with the given index? */
1109static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1110{
1111 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1112 return path && path->ctls[ctl_type];
1113}
1114
Takashi Iwai352f7f92012-12-19 12:52:06 +01001115static const char * const channel_name[4] = {
1116 "Front", "Surround", "CLFE", "Side"
1117};
1118
1119/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001120static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1121 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001122{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001123 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001124 struct auto_pin_cfg *cfg = &spec->autocfg;
1125
1126 *index = 0;
1127 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai9f3dadb2017-04-10 17:12:33 +02001128 !codec->force_pin_prefix &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001129 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001130 return spec->vmaster_mute.hook ? "PCM" : "Master";
1131
1132 /* if there is really a single DAC used in the whole output paths,
1133 * use it master (or "PCM" if a vmaster hook is present)
1134 */
1135 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
Takashi Iwai9f3dadb2017-04-10 17:12:33 +02001136 !codec->force_pin_prefix &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001137 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1138 return spec->vmaster_mute.hook ? "PCM" : "Master";
1139
Takashi Iwai247d85e2013-01-17 16:18:11 +01001140 /* multi-io channels */
1141 if (ch >= cfg->line_outs)
1142 return channel_name[ch];
1143
Takashi Iwai352f7f92012-12-19 12:52:06 +01001144 switch (cfg->line_out_type) {
1145 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001146 /* if the primary channel vol/mute is shared with HP volume,
1147 * don't name it as Speaker
1148 */
1149 if (!ch && cfg->hp_outs &&
1150 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1151 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001152 if (cfg->line_outs == 1)
1153 return "Speaker";
1154 if (cfg->line_outs == 2)
1155 return ch ? "Bass Speaker" : "Speaker";
1156 break;
1157 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001158 /* if the primary channel vol/mute is shared with spk volume,
1159 * don't name it as Headphone
1160 */
1161 if (!ch && cfg->speaker_outs &&
1162 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1163 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001164 /* for multi-io case, only the primary out */
1165 if (ch && spec->multi_ios)
1166 break;
1167 *index = ch;
1168 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001169 case AUTO_PIN_LINE_OUT:
1170 /* This deals with the case where we have two DACs and
1171 * one LO, one HP and one Speaker */
1172 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1173 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1174 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1175 if (hp_lo_shared && spk_lo_shared)
1176 return spec->vmaster_mute.hook ? "PCM" : "Master";
1177 if (hp_lo_shared)
1178 return "Headphone+LO";
1179 if (spk_lo_shared)
1180 return "Speaker+LO";
1181 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001182 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001183
1184 /* for a single channel output, we don't have to name the channel */
1185 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001186 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001187
Takashi Iwai352f7f92012-12-19 12:52:06 +01001188 if (ch >= ARRAY_SIZE(channel_name)) {
1189 snd_BUG();
1190 return "PCM";
1191 }
1192
1193 return channel_name[ch];
1194}
1195
1196/*
1197 * Parse output paths
1198 */
1199
1200/* badness definition */
1201enum {
1202 /* No primary DAC is found for the main output */
1203 BAD_NO_PRIMARY_DAC = 0x10000,
1204 /* No DAC is found for the extra output */
1205 BAD_NO_DAC = 0x4000,
1206 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001207 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001208 /* No individual DAC for extra output */
1209 BAD_NO_EXTRA_DAC = 0x102,
1210 /* No individual DAC for extra surrounds */
1211 BAD_NO_EXTRA_SURR_DAC = 0x101,
1212 /* Primary DAC shared with main surrounds */
1213 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001214 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001215 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001216 /* Primary DAC shared with main CLFE */
1217 BAD_SHARED_CLFE = 0x10,
1218 /* Primary DAC shared with extra surrounds */
1219 BAD_SHARED_EXTRA_SURROUND = 0x10,
1220 /* Volume widget is shared */
1221 BAD_SHARED_VOL = 0x10,
1222};
1223
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001224/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001225 * volume and mute controls, and assign the values to ctls[].
1226 *
1227 * When no appropriate widget is found in the path, the badness value
1228 * is incremented depending on the situation. The function returns the
1229 * total badness for both volume and mute controls.
1230 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001231static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001232{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001233 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001234 hda_nid_t nid;
1235 unsigned int val;
1236 int badness = 0;
1237
1238 if (!path)
1239 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001240
1241 if (path->ctls[NID_PATH_VOL_CTL] ||
1242 path->ctls[NID_PATH_MUTE_CTL])
1243 return 0; /* already evaluated */
1244
Takashi Iwai352f7f92012-12-19 12:52:06 +01001245 nid = look_for_out_vol_nid(codec, path);
1246 if (nid) {
1247 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001248 if (spec->dac_min_mute)
1249 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001250 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1251 badness += BAD_SHARED_VOL;
1252 else
1253 path->ctls[NID_PATH_VOL_CTL] = val;
1254 } else
1255 badness += BAD_SHARED_VOL;
1256 nid = look_for_out_mute_nid(codec, path);
1257 if (nid) {
1258 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1259 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1260 nid_has_mute(codec, nid, HDA_OUTPUT))
1261 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1262 else
1263 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1264 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1265 badness += BAD_SHARED_VOL;
1266 else
1267 path->ctls[NID_PATH_MUTE_CTL] = val;
1268 } else
1269 badness += BAD_SHARED_VOL;
1270 return badness;
1271}
1272
Takashi Iwai98bd1112013-03-22 14:53:50 +01001273const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001274 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1275 .no_dac = BAD_NO_DAC,
1276 .shared_primary = BAD_NO_PRIMARY_DAC,
1277 .shared_surr = BAD_SHARED_SURROUND,
1278 .shared_clfe = BAD_SHARED_CLFE,
1279 .shared_surr_main = BAD_SHARED_SURROUND,
1280};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001281EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001282
Takashi Iwai98bd1112013-03-22 14:53:50 +01001283const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001284 .no_primary_dac = BAD_NO_DAC,
1285 .no_dac = BAD_NO_DAC,
1286 .shared_primary = BAD_NO_EXTRA_DAC,
1287 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1288 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1289 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1290};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001291EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001292
Takashi Iwai7385df62013-01-07 09:50:52 +01001293/* get the DAC of the primary output corresponding to the given array index */
1294static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1295{
1296 struct hda_gen_spec *spec = codec->spec;
1297 struct auto_pin_cfg *cfg = &spec->autocfg;
1298
1299 if (cfg->line_outs > idx)
1300 return spec->private_dac_nids[idx];
1301 idx -= cfg->line_outs;
1302 if (spec->multi_ios > idx)
1303 return spec->multi_io[idx].dac;
1304 return 0;
1305}
1306
1307/* return the DAC if it's reachable, otherwise zero */
1308static inline hda_nid_t try_dac(struct hda_codec *codec,
1309 hda_nid_t dac, hda_nid_t pin)
1310{
1311 return is_reachable_path(codec, dac, pin) ? dac : 0;
1312}
1313
Takashi Iwai352f7f92012-12-19 12:52:06 +01001314/* try to assign DACs to pins and return the resultant badness */
1315static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1316 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001317 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001318 const struct badness_table *bad)
1319{
1320 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001321 int i, j;
1322 int badness = 0;
1323 hda_nid_t dac;
1324
1325 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return 0;
1327
Takashi Iwai352f7f92012-12-19 12:52:06 +01001328 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001329 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001330 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001331
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001332 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1333 if (path) {
1334 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001335 continue;
1336 }
1337
Takashi Iwai36907392013-12-10 17:29:26 +01001338 dacs[i] = get_preferred_dac(codec, pin);
1339 if (dacs[i]) {
1340 if (is_dac_already_used(codec, dacs[i]))
1341 badness += bad->shared_primary;
1342 }
1343
1344 if (!dacs[i])
1345 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001346 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001347 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 for (j = 1; j < num_outs; j++) {
1349 if (is_reachable_path(codec, dacs[j], pin)) {
1350 dacs[0] = dacs[j];
1351 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001352 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001353 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001354 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001357 }
1358 dac = dacs[i];
1359 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001360 if (num_outs > 2)
1361 dac = try_dac(codec, get_primary_out(codec, i), pin);
1362 if (!dac)
1363 dac = try_dac(codec, dacs[0], pin);
1364 if (!dac)
1365 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001366 if (dac) {
1367 if (!i)
1368 badness += bad->shared_primary;
1369 else if (i == 1)
1370 badness += bad->shared_surr;
1371 else
1372 badness += bad->shared_clfe;
1373 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1374 dac = spec->private_dac_nids[0];
1375 badness += bad->shared_surr_main;
1376 } else if (!i)
1377 badness += bad->no_primary_dac;
1378 else
1379 badness += bad->no_dac;
1380 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001381 if (!dac)
1382 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001383 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001384 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001385 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001386 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001387 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001388 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001389 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001390 badness += bad->no_dac;
1391 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001392 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001393 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001394 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001395 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001396 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001397 }
1398
1399 return badness;
1400}
1401
1402/* return NID if the given pin has only a single connection to a certain DAC */
1403static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1404{
1405 struct hda_gen_spec *spec = codec->spec;
1406 int i;
1407 hda_nid_t nid_found = 0;
1408
1409 for (i = 0; i < spec->num_all_dacs; i++) {
1410 hda_nid_t nid = spec->all_dacs[i];
1411 if (!nid || is_dac_already_used(codec, nid))
1412 continue;
1413 if (is_reachable_path(codec, nid, pin)) {
1414 if (nid_found)
1415 return 0;
1416 nid_found = nid;
1417 }
1418 }
1419 return nid_found;
1420}
1421
1422/* check whether the given pin can be a multi-io pin */
1423static bool can_be_multiio_pin(struct hda_codec *codec,
1424 unsigned int location, hda_nid_t nid)
1425{
1426 unsigned int defcfg, caps;
1427
1428 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1429 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1430 return false;
1431 if (location && get_defcfg_location(defcfg) != location)
1432 return false;
1433 caps = snd_hda_query_pin_caps(codec, nid);
1434 if (!(caps & AC_PINCAP_OUT))
1435 return false;
1436 return true;
1437}
1438
Takashi Iwaie22aab72013-01-04 14:50:04 +01001439/* count the number of input pins that are capable to be multi-io */
1440static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1441{
1442 struct hda_gen_spec *spec = codec->spec;
1443 struct auto_pin_cfg *cfg = &spec->autocfg;
1444 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1445 unsigned int location = get_defcfg_location(defcfg);
1446 int type, i;
1447 int num_pins = 0;
1448
1449 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1450 for (i = 0; i < cfg->num_inputs; i++) {
1451 if (cfg->inputs[i].type != type)
1452 continue;
1453 if (can_be_multiio_pin(codec, location,
1454 cfg->inputs[i].pin))
1455 num_pins++;
1456 }
1457 }
1458 return num_pins;
1459}
1460
Takashi Iwai352f7f92012-12-19 12:52:06 +01001461/*
1462 * multi-io helper
1463 *
1464 * When hardwired is set, try to fill ony hardwired pins, and returns
1465 * zero if any pins are filled, non-zero if nothing found.
1466 * When hardwired is off, try to fill possible input pins, and returns
1467 * the badness value.
1468 */
1469static int fill_multi_ios(struct hda_codec *codec,
1470 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001471 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001472{
1473 struct hda_gen_spec *spec = codec->spec;
1474 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001475 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001476 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1477 unsigned int location = get_defcfg_location(defcfg);
1478 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001479 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001480
1481 old_pins = spec->multi_ios;
1482 if (old_pins >= 2)
1483 goto end_fill;
1484
Takashi Iwaie22aab72013-01-04 14:50:04 +01001485 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001486 if (num_pins < 2)
1487 goto end_fill;
1488
Takashi Iwai352f7f92012-12-19 12:52:06 +01001489 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1490 for (i = 0; i < cfg->num_inputs; i++) {
1491 hda_nid_t nid = cfg->inputs[i].pin;
1492 hda_nid_t dac = 0;
1493
1494 if (cfg->inputs[i].type != type)
1495 continue;
1496 if (!can_be_multiio_pin(codec, location, nid))
1497 continue;
1498 for (j = 0; j < spec->multi_ios; j++) {
1499 if (nid == spec->multi_io[j].pin)
1500 break;
1501 }
1502 if (j < spec->multi_ios)
1503 continue;
1504
Takashi Iwai352f7f92012-12-19 12:52:06 +01001505 if (hardwired)
1506 dac = get_dac_if_single(codec, nid);
1507 else if (!dac)
1508 dac = look_for_dac(codec, nid, false);
1509 if (!dac) {
1510 badness++;
1511 continue;
1512 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001513 path = snd_hda_add_new_path(codec, dac, nid,
1514 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001515 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001516 badness++;
1517 continue;
1518 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001519 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001520 spec->multi_io[spec->multi_ios].pin = nid;
1521 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001522 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1523 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001524 spec->multi_ios++;
1525 if (spec->multi_ios >= 2)
1526 break;
1527 }
1528 }
1529 end_fill:
1530 if (badness)
1531 badness = BAD_MULTI_IO;
1532 if (old_pins == spec->multi_ios) {
1533 if (hardwired)
1534 return 1; /* nothing found */
1535 else
1536 return badness; /* no badness if nothing found */
1537 }
1538 if (!hardwired && spec->multi_ios < 2) {
1539 /* cancel newly assigned paths */
1540 spec->paths.used -= spec->multi_ios - old_pins;
1541 spec->multi_ios = old_pins;
1542 return badness;
1543 }
1544
1545 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001546 for (i = old_pins; i < spec->multi_ios; i++) {
1547 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1548 badness += assign_out_path_ctls(codec, path);
1549 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001550
1551 return badness;
1552}
1553
1554/* map DACs for all pins in the list if they are single connections */
1555static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001556 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001557{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001558 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001559 int i;
1560 bool found = false;
1561 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001562 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001563 hda_nid_t dac;
1564 if (dacs[i])
1565 continue;
1566 dac = get_dac_if_single(codec, pins[i]);
1567 if (!dac)
1568 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001569 path = snd_hda_add_new_path(codec, dac, pins[i],
1570 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001571 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001572 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001573 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001574 dacs[i] = dac;
1575 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001576 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001577 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001578 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001579 }
1580 }
1581 return found;
1582}
1583
Takashi Iwaie7fdd522015-12-08 17:00:42 +01001584static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1585{
1586 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1587 spec->aamix_out_paths[2];
1588}
1589
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001590/* create a new path including aamix if available, and return its index */
1591static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1592{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001593 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001594 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001595 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001596
1597 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001598 if (!path || !path->depth ||
1599 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001600 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001601 path_dac = path->path[0];
1602 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001603 pin = path->path[path->depth - 1];
1604 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1605 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001606 if (dac != path_dac)
1607 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001608 else if (spec->multiout.hp_out_nid[0])
1609 dac = spec->multiout.hp_out_nid[0];
1610 else if (spec->multiout.extra_out_nid[0])
1611 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001612 else
1613 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001614 if (dac)
1615 path = snd_hda_add_new_path(codec, dac, pin,
1616 spec->mixer_nid);
1617 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001618 if (!path)
1619 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001620 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001621 path->active = false; /* unused as default */
Takashi Iwai6b275b12015-03-20 18:11:05 +01001622 path->pin_fixed = true; /* static route */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001623 return snd_hda_get_path_idx(codec, path);
1624}
1625
Takashi Iwai55a63d42013-03-21 17:20:12 +01001626/* check whether the independent HP is available with the current config */
1627static bool indep_hp_possible(struct hda_codec *codec)
1628{
1629 struct hda_gen_spec *spec = codec->spec;
1630 struct auto_pin_cfg *cfg = &spec->autocfg;
1631 struct nid_path *path;
1632 int i, idx;
1633
1634 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1635 idx = spec->out_paths[0];
1636 else
1637 idx = spec->hp_paths[0];
1638 path = snd_hda_get_path_from_idx(codec, idx);
1639 if (!path)
1640 return false;
1641
1642 /* assume no path conflicts unless aamix is involved */
1643 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1644 return true;
1645
1646 /* check whether output paths contain aamix */
1647 for (i = 0; i < cfg->line_outs; i++) {
1648 if (spec->out_paths[i] == idx)
1649 break;
1650 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1651 if (path && is_nid_contained(path, spec->mixer_nid))
1652 return false;
1653 }
1654 for (i = 0; i < cfg->speaker_outs; i++) {
1655 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1656 if (path && is_nid_contained(path, spec->mixer_nid))
1657 return false;
1658 }
1659
1660 return true;
1661}
1662
Takashi Iwaia07a9492013-01-07 16:44:06 +01001663/* fill the empty entries in the dac array for speaker/hp with the
1664 * shared dac pointed by the paths
1665 */
1666static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1667 hda_nid_t *dacs, int *path_idx)
1668{
1669 struct nid_path *path;
1670 int i;
1671
1672 for (i = 0; i < num_outs; i++) {
1673 if (dacs[i])
1674 continue;
1675 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1676 if (!path)
1677 continue;
1678 dacs[i] = path->path[0];
1679 }
1680}
1681
Takashi Iwai352f7f92012-12-19 12:52:06 +01001682/* fill in the dac_nids table from the parsed pin configuration */
1683static int fill_and_eval_dacs(struct hda_codec *codec,
1684 bool fill_hardwired,
1685 bool fill_mio_first)
1686{
1687 struct hda_gen_spec *spec = codec->spec;
1688 struct auto_pin_cfg *cfg = &spec->autocfg;
1689 int i, err, badness;
1690
1691 /* set num_dacs once to full for look_for_dac() */
1692 spec->multiout.num_dacs = cfg->line_outs;
1693 spec->multiout.dac_nids = spec->private_dac_nids;
1694 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1695 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1696 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1697 spec->multi_ios = 0;
1698 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001699
1700 /* clear path indices */
1701 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1702 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1703 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1704 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1705 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001706 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001707 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1708 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1709
Takashi Iwai352f7f92012-12-19 12:52:06 +01001710 badness = 0;
1711
1712 /* fill hard-wired DACs first */
1713 if (fill_hardwired) {
1714 bool mapped;
1715 do {
1716 mapped = map_singles(codec, cfg->line_outs,
1717 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001718 spec->private_dac_nids,
1719 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001720 mapped |= map_singles(codec, cfg->hp_outs,
1721 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001722 spec->multiout.hp_out_nid,
1723 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001724 mapped |= map_singles(codec, cfg->speaker_outs,
1725 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001726 spec->multiout.extra_out_nid,
1727 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001728 if (!spec->no_multi_io &&
1729 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001730 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001731 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001732 if (!err)
1733 mapped = true;
1734 }
1735 } while (mapped);
1736 }
1737
1738 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001739 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001740 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001741
Takashi Iwaida96fb52013-07-29 16:54:36 +02001742 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001743 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1744 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001745 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001746 if (err < 0)
1747 return err;
1748 /* we don't count badness at this stage yet */
1749 }
1750
1751 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1752 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1753 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001754 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001755 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001756 if (err < 0)
1757 return err;
1758 badness += err;
1759 }
1760 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1761 err = try_assign_dacs(codec, cfg->speaker_outs,
1762 cfg->speaker_pins,
1763 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001764 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001765 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001766 if (err < 0)
1767 return err;
1768 badness += err;
1769 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001770 if (!spec->no_multi_io &&
1771 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001772 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001773 if (err < 0)
1774 return err;
1775 badness += err;
1776 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001777
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001778 if (spec->mixer_nid) {
1779 spec->aamix_out_paths[0] =
1780 check_aamix_out_path(codec, spec->out_paths[0]);
1781 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1782 spec->aamix_out_paths[1] =
1783 check_aamix_out_path(codec, spec->hp_paths[0]);
1784 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1785 spec->aamix_out_paths[2] =
1786 check_aamix_out_path(codec, spec->speaker_paths[0]);
1787 }
1788
Takashi Iwaida96fb52013-07-29 16:54:36 +02001789 if (!spec->no_multi_io &&
1790 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001791 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1792 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001793
Takashi Iwaia07a9492013-01-07 16:44:06 +01001794 /* re-count num_dacs and squash invalid entries */
1795 spec->multiout.num_dacs = 0;
1796 for (i = 0; i < cfg->line_outs; i++) {
1797 if (spec->private_dac_nids[i])
1798 spec->multiout.num_dacs++;
1799 else {
1800 memmove(spec->private_dac_nids + i,
1801 spec->private_dac_nids + i + 1,
1802 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1803 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1804 }
1805 }
1806
1807 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001808 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001809
Takashi Iwai352f7f92012-12-19 12:52:06 +01001810 if (spec->multi_ios == 2) {
1811 for (i = 0; i < 2; i++)
1812 spec->private_dac_nids[spec->multiout.num_dacs++] =
1813 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001814 } else if (spec->multi_ios) {
1815 spec->multi_ios = 0;
1816 badness += BAD_MULTI_IO;
1817 }
1818
Takashi Iwai55a63d42013-03-21 17:20:12 +01001819 if (spec->indep_hp && !indep_hp_possible(codec))
1820 badness += BAD_NO_INDEP_HP;
1821
Takashi Iwaia07a9492013-01-07 16:44:06 +01001822 /* re-fill the shared DAC for speaker / headphone */
1823 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1824 refill_shared_dacs(codec, cfg->hp_outs,
1825 spec->multiout.hp_out_nid,
1826 spec->hp_paths);
1827 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1828 refill_shared_dacs(codec, cfg->speaker_outs,
1829 spec->multiout.extra_out_nid,
1830 spec->speaker_paths);
1831
Takashi Iwai352f7f92012-12-19 12:52:06 +01001832 return badness;
1833}
1834
1835#define DEBUG_BADNESS
1836
1837#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001838#define debug_badness(fmt, ...) \
1839 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001840#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001841#define debug_badness(fmt, ...) \
1842 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001843#endif
1844
Takashi Iwaia7694092013-01-21 10:43:18 +01001845#ifdef DEBUG_BADNESS
1846static inline void print_nid_path_idx(struct hda_codec *codec,
1847 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001848{
Takashi Iwaia7694092013-01-21 10:43:18 +01001849 struct nid_path *path;
1850
1851 path = snd_hda_get_path_from_idx(codec, idx);
1852 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001853 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001854}
1855
1856static void debug_show_configs(struct hda_codec *codec,
1857 struct auto_pin_cfg *cfg)
1858{
1859 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001860 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001861 int i;
1862
1863 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001864 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001865 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001866 spec->multiout.dac_nids[0],
1867 spec->multiout.dac_nids[1],
1868 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001869 spec->multiout.dac_nids[3],
1870 lo_type[cfg->line_out_type]);
1871 for (i = 0; i < cfg->line_outs; i++)
1872 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001873 if (spec->multi_ios > 0)
1874 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1875 spec->multi_ios,
1876 spec->multi_io[0].pin, spec->multi_io[1].pin,
1877 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001878 for (i = 0; i < spec->multi_ios; i++)
1879 print_nid_path_idx(codec, " mio",
1880 spec->out_paths[cfg->line_outs + i]);
1881 if (cfg->hp_outs)
1882 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001884 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001885 spec->multiout.hp_out_nid[0],
1886 spec->multiout.hp_out_nid[1],
1887 spec->multiout.hp_out_nid[2],
1888 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001889 for (i = 0; i < cfg->hp_outs; i++)
1890 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1891 if (cfg->speaker_outs)
1892 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001893 cfg->speaker_pins[0], cfg->speaker_pins[1],
1894 cfg->speaker_pins[2], cfg->speaker_pins[3],
1895 spec->multiout.extra_out_nid[0],
1896 spec->multiout.extra_out_nid[1],
1897 spec->multiout.extra_out_nid[2],
1898 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001899 for (i = 0; i < cfg->speaker_outs; i++)
1900 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1901 for (i = 0; i < 3; i++)
1902 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001903}
Takashi Iwaia7694092013-01-21 10:43:18 +01001904#else
1905#define debug_show_configs(codec, cfg) /* NOP */
1906#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001907
1908/* find all available DACs of the codec */
1909static void fill_all_dac_nids(struct hda_codec *codec)
1910{
1911 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001912 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001913
1914 spec->num_all_dacs = 0;
1915 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
Takashi Iwai7639a062015-03-03 10:07:24 +01001916 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001917 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1918 continue;
1919 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001920 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001921 break;
1922 }
1923 spec->all_dacs[spec->num_all_dacs++] = nid;
1924 }
1925}
1926
1927static int parse_output_paths(struct hda_codec *codec)
1928{
1929 struct hda_gen_spec *spec = codec->spec;
1930 struct auto_pin_cfg *cfg = &spec->autocfg;
1931 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001932 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001933 int best_badness = INT_MAX;
1934 int badness;
1935 bool fill_hardwired = true, fill_mio_first = true;
1936 bool best_wired = true, best_mio = true;
1937 bool hp_spk_swapped = false;
1938
Takashi Iwai352f7f92012-12-19 12:52:06 +01001939 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1940 if (!best_cfg)
1941 return -ENOMEM;
1942 *best_cfg = *cfg;
1943
1944 for (;;) {
1945 badness = fill_and_eval_dacs(codec, fill_hardwired,
1946 fill_mio_first);
1947 if (badness < 0) {
1948 kfree(best_cfg);
1949 return badness;
1950 }
1951 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1952 cfg->line_out_type, fill_hardwired, fill_mio_first,
1953 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001954 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001955 if (badness < best_badness) {
1956 best_badness = badness;
1957 *best_cfg = *cfg;
1958 best_wired = fill_hardwired;
1959 best_mio = fill_mio_first;
1960 }
1961 if (!badness)
1962 break;
1963 fill_mio_first = !fill_mio_first;
1964 if (!fill_mio_first)
1965 continue;
1966 fill_hardwired = !fill_hardwired;
1967 if (!fill_hardwired)
1968 continue;
1969 if (hp_spk_swapped)
1970 break;
1971 hp_spk_swapped = true;
1972 if (cfg->speaker_outs > 0 &&
1973 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1974 cfg->hp_outs = cfg->line_outs;
1975 memcpy(cfg->hp_pins, cfg->line_out_pins,
1976 sizeof(cfg->hp_pins));
1977 cfg->line_outs = cfg->speaker_outs;
1978 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1979 sizeof(cfg->speaker_pins));
1980 cfg->speaker_outs = 0;
1981 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1982 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1983 fill_hardwired = true;
1984 continue;
1985 }
1986 if (cfg->hp_outs > 0 &&
1987 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1988 cfg->speaker_outs = cfg->line_outs;
1989 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1990 sizeof(cfg->speaker_pins));
1991 cfg->line_outs = cfg->hp_outs;
1992 memcpy(cfg->line_out_pins, cfg->hp_pins,
1993 sizeof(cfg->hp_pins));
1994 cfg->hp_outs = 0;
1995 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1996 cfg->line_out_type = AUTO_PIN_HP_OUT;
1997 fill_hardwired = true;
1998 continue;
1999 }
2000 break;
2001 }
2002
2003 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002004 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01002005 *cfg = *best_cfg;
2006 fill_and_eval_dacs(codec, best_wired, best_mio);
2007 }
2008 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2009 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01002010 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002011
2012 if (cfg->line_out_pins[0]) {
2013 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01002014 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002015 if (path)
2016 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002017 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01002018 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2019 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002020 if (spec->dac_min_mute)
2021 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2022 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002023 }
2024
Takashi Iwai9314a582013-01-21 10:49:05 +01002025 /* set initial pinctl targets */
2026 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2027 val = PIN_HP;
2028 else
2029 val = PIN_OUT;
2030 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2031 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2032 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2033 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2034 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2035 set_pin_targets(codec, cfg->speaker_outs,
2036 cfg->speaker_pins, val);
2037 }
2038
Takashi Iwai55a63d42013-03-21 17:20:12 +01002039 /* clear indep_hp flag if not available */
2040 if (spec->indep_hp && !indep_hp_possible(codec))
2041 spec->indep_hp = 0;
2042
Takashi Iwai352f7f92012-12-19 12:52:06 +01002043 kfree(best_cfg);
2044 return 0;
2045}
2046
2047/* add playback controls from the parsed DAC table */
2048static int create_multi_out_ctls(struct hda_codec *codec,
2049 const struct auto_pin_cfg *cfg)
2050{
2051 struct hda_gen_spec *spec = codec->spec;
2052 int i, err, noutputs;
2053
2054 noutputs = cfg->line_outs;
2055 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2056 noutputs += spec->multi_ios;
2057
2058 for (i = 0; i < noutputs; i++) {
2059 const char *name;
2060 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002061 struct nid_path *path;
2062
Takashi Iwai196c17662013-01-04 15:01:40 +01002063 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002064 if (!path)
2065 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002066
2067 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002068 if (!name || !strcmp(name, "CLFE")) {
2069 /* Center/LFE */
2070 err = add_vol_ctl(codec, "Center", 0, 1, path);
2071 if (err < 0)
2072 return err;
2073 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2074 if (err < 0)
2075 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002076 } else {
2077 err = add_stereo_vol(codec, name, index, path);
2078 if (err < 0)
2079 return err;
2080 }
2081
2082 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2083 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002084 err = add_sw_ctl(codec, "Center", 0, 1, path);
2085 if (err < 0)
2086 return err;
2087 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2088 if (err < 0)
2089 return err;
2090 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002091 err = add_stereo_sw(codec, name, index, path);
2092 if (err < 0)
2093 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
2095 }
2096 return 0;
2097}
2098
Takashi Iwaic2c80382013-01-07 10:33:57 +01002099static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002100 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002102 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 int err;
2104
Takashi Iwai196c17662013-01-04 15:01:40 +01002105 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002106 if (!path)
2107 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002108 err = add_stereo_vol(codec, pfx, cidx, path);
2109 if (err < 0)
2110 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002111 err = add_stereo_sw(codec, pfx, cidx, path);
2112 if (err < 0)
2113 return err;
2114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116
Takashi Iwai352f7f92012-12-19 12:52:06 +01002117/* add playback controls for speaker and HP outputs */
2118static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002119 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002121 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002122
2123 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002124 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002125 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002126 int err, idx = 0;
2127
2128 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2129 name = "Bass Speaker";
2130 else if (num_pins >= 3) {
2131 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002132 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002133 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002134 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002135 name = pfx;
2136 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002138 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002139 if (err < 0)
2140 return err;
2141 }
2142 return 0;
2143}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002144
Takashi Iwai352f7f92012-12-19 12:52:06 +01002145static int create_hp_out_ctls(struct hda_codec *codec)
2146{
2147 struct hda_gen_spec *spec = codec->spec;
2148 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002149 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002150 "Headphone");
2151}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Takashi Iwai352f7f92012-12-19 12:52:06 +01002153static int create_speaker_out_ctls(struct hda_codec *codec)
2154{
2155 struct hda_gen_spec *spec = codec->spec;
2156 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002157 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002158 "Speaker");
2159}
2160
2161/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002162 * independent HP controls
2163 */
2164
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002165static void call_hp_automute(struct hda_codec *codec,
2166 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002167static int indep_hp_info(struct snd_kcontrol *kcontrol,
2168 struct snd_ctl_elem_info *uinfo)
2169{
2170 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2171}
2172
2173static int indep_hp_get(struct snd_kcontrol *kcontrol,
2174 struct snd_ctl_elem_value *ucontrol)
2175{
2176 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2177 struct hda_gen_spec *spec = codec->spec;
2178 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2179 return 0;
2180}
2181
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002182static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2183 int nomix_path_idx, int mix_path_idx,
2184 int out_type);
2185
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002186static int indep_hp_put(struct snd_kcontrol *kcontrol,
2187 struct snd_ctl_elem_value *ucontrol)
2188{
2189 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2190 struct hda_gen_spec *spec = codec->spec;
2191 unsigned int select = ucontrol->value.enumerated.item[0];
2192 int ret = 0;
2193
2194 mutex_lock(&spec->pcm_mutex);
2195 if (spec->active_streams) {
2196 ret = -EBUSY;
2197 goto unlock;
2198 }
2199
2200 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002201 hda_nid_t *dacp;
2202 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2203 dacp = &spec->private_dac_nids[0];
2204 else
2205 dacp = &spec->multiout.hp_out_nid[0];
2206
2207 /* update HP aamix paths in case it conflicts with indep HP */
2208 if (spec->have_aamix_ctl) {
2209 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2210 update_aamix_paths(codec, spec->aamix_mode,
2211 spec->out_paths[0],
2212 spec->aamix_out_paths[0],
2213 spec->autocfg.line_out_type);
2214 else
2215 update_aamix_paths(codec, spec->aamix_mode,
2216 spec->hp_paths[0],
2217 spec->aamix_out_paths[1],
2218 AUTO_PIN_HP_OUT);
2219 }
2220
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002221 spec->indep_hp_enabled = select;
2222 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002223 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002224 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002225 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002226
Takashi Iwai963afde2013-05-31 15:20:31 +02002227 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002228 ret = 1;
2229 }
2230 unlock:
2231 mutex_unlock(&spec->pcm_mutex);
2232 return ret;
2233}
2234
2235static const struct snd_kcontrol_new indep_hp_ctl = {
2236 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2237 .name = "Independent HP",
2238 .info = indep_hp_info,
2239 .get = indep_hp_get,
2240 .put = indep_hp_put,
2241};
2242
2243
2244static int create_indep_hp_ctls(struct hda_codec *codec)
2245{
2246 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002247 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002248
2249 if (!spec->indep_hp)
2250 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002251 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2252 dac = spec->multiout.dac_nids[0];
2253 else
2254 dac = spec->multiout.hp_out_nid[0];
2255 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002256 spec->indep_hp = 0;
2257 return 0;
2258 }
2259
2260 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002261 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002262 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2263 return -ENOMEM;
2264 return 0;
2265}
2266
2267/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002268 * channel mode enum control
2269 */
2270
2271static int ch_mode_info(struct snd_kcontrol *kcontrol,
2272 struct snd_ctl_elem_info *uinfo)
2273{
2274 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2275 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002276 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002277
2278 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2279 uinfo->count = 1;
2280 uinfo->value.enumerated.items = spec->multi_ios + 1;
2281 if (uinfo->value.enumerated.item > spec->multi_ios)
2282 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002283 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2284 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002285 return 0;
2286}
2287
2288static int ch_mode_get(struct snd_kcontrol *kcontrol,
2289 struct snd_ctl_elem_value *ucontrol)
2290{
2291 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2292 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002293 ucontrol->value.enumerated.item[0] =
2294 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002295 return 0;
2296}
2297
Takashi Iwai196c17662013-01-04 15:01:40 +01002298static inline struct nid_path *
2299get_multiio_path(struct hda_codec *codec, int idx)
2300{
2301 struct hda_gen_spec *spec = codec->spec;
2302 return snd_hda_get_path_from_idx(codec,
2303 spec->out_paths[spec->autocfg.line_outs + idx]);
2304}
2305
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002306static void update_automute_all(struct hda_codec *codec);
2307
Takashi Iwai65033cc2013-04-16 12:31:05 +02002308/* Default value to be passed as aamix argument for snd_hda_activate_path();
2309 * used for output paths
2310 */
2311static bool aamix_default(struct hda_gen_spec *spec)
2312{
2313 return !spec->have_aamix_ctl || spec->aamix_mode;
2314}
2315
Takashi Iwai352f7f92012-12-19 12:52:06 +01002316static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2317{
2318 struct hda_gen_spec *spec = codec->spec;
2319 hda_nid_t nid = spec->multi_io[idx].pin;
2320 struct nid_path *path;
2321
Takashi Iwai196c17662013-01-04 15:01:40 +01002322 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002323 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002325
2326 if (path->active == output)
2327 return 0;
2328
2329 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002330 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002331 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002332 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002333 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002334 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002335 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002336 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002337 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002339
2340 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002341 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002342
Takashi Iwai352f7f92012-12-19 12:52:06 +01002343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
2345
Takashi Iwai352f7f92012-12-19 12:52:06 +01002346static int ch_mode_put(struct snd_kcontrol *kcontrol,
2347 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002349 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2350 struct hda_gen_spec *spec = codec->spec;
2351 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Takashi Iwai352f7f92012-12-19 12:52:06 +01002353 ch = ucontrol->value.enumerated.item[0];
2354 if (ch < 0 || ch > spec->multi_ios)
2355 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002356 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002357 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002358 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002359 for (i = 0; i < spec->multi_ios; i++)
2360 set_multi_io(codec, i, i < ch);
2361 spec->multiout.max_channels = max(spec->ext_channel_count,
2362 spec->const_channel_count);
2363 if (spec->need_dac_fix)
2364 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 return 1;
2366}
2367
Takashi Iwai352f7f92012-12-19 12:52:06 +01002368static const struct snd_kcontrol_new channel_mode_enum = {
2369 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2370 .name = "Channel Mode",
2371 .info = ch_mode_info,
2372 .get = ch_mode_get,
2373 .put = ch_mode_put,
2374};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Takashi Iwai352f7f92012-12-19 12:52:06 +01002376static int create_multi_channel_mode(struct hda_codec *codec)
2377{
2378 struct hda_gen_spec *spec = codec->spec;
2379
2380 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002381 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002382 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return 0;
2385}
2386
Takashi Iwai352f7f92012-12-19 12:52:06 +01002387/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002388 * aamix loopback enable/disable switch
2389 */
2390
2391#define loopback_mixing_info indep_hp_info
2392
2393static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2394 struct snd_ctl_elem_value *ucontrol)
2395{
2396 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2397 struct hda_gen_spec *spec = codec->spec;
2398 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2399 return 0;
2400}
2401
2402static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002403 int nomix_path_idx, int mix_path_idx,
2404 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002405{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002406 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002407 struct nid_path *nomix_path, *mix_path;
2408
2409 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2410 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2411 if (!nomix_path || !mix_path)
2412 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002413
2414 /* if HP aamix path is driven from a different DAC and the
2415 * independent HP mode is ON, can't turn on aamix path
2416 */
2417 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2418 mix_path->path[0] != spec->alt_dac_nid)
2419 do_mix = false;
2420
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002421 if (do_mix) {
2422 snd_hda_activate_path(codec, nomix_path, false, true);
2423 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002424 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002425 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002426 snd_hda_activate_path(codec, mix_path, false, false);
2427 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002428 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002429 }
2430}
2431
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002432/* re-initialize the output paths; only called from loopback_mixing_put() */
2433static void update_output_paths(struct hda_codec *codec, int num_outs,
2434 const int *paths)
2435{
2436 struct hda_gen_spec *spec = codec->spec;
2437 struct nid_path *path;
2438 int i;
2439
2440 for (i = 0; i < num_outs; i++) {
2441 path = snd_hda_get_path_from_idx(codec, paths[i]);
2442 if (path)
2443 snd_hda_activate_path(codec, path, path->active,
2444 spec->aamix_mode);
2445 }
2446}
2447
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002448static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2449 struct snd_ctl_elem_value *ucontrol)
2450{
2451 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2452 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002453 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002454 unsigned int val = ucontrol->value.enumerated.item[0];
2455
2456 if (val == spec->aamix_mode)
2457 return 0;
2458 spec->aamix_mode = val;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002459 if (has_aamix_out_paths(spec)) {
2460 update_aamix_paths(codec, val, spec->out_paths[0],
2461 spec->aamix_out_paths[0],
2462 cfg->line_out_type);
2463 update_aamix_paths(codec, val, spec->hp_paths[0],
2464 spec->aamix_out_paths[1],
2465 AUTO_PIN_HP_OUT);
2466 update_aamix_paths(codec, val, spec->speaker_paths[0],
2467 spec->aamix_out_paths[2],
2468 AUTO_PIN_SPEAKER_OUT);
2469 } else {
2470 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2471 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2472 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2473 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2474 update_output_paths(codec, cfg->speaker_outs,
2475 spec->speaker_paths);
2476 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002477 return 1;
2478}
2479
2480static const struct snd_kcontrol_new loopback_mixing_enum = {
2481 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2482 .name = "Loopback Mixing",
2483 .info = loopback_mixing_info,
2484 .get = loopback_mixing_get,
2485 .put = loopback_mixing_put,
2486};
2487
2488static int create_loopback_mixing_ctl(struct hda_codec *codec)
2489{
2490 struct hda_gen_spec *spec = codec->spec;
2491
2492 if (!spec->mixer_nid)
2493 return 0;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002494 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2495 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002496 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002497 return 0;
2498}
2499
2500/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002501 * shared headphone/mic handling
2502 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002503
Takashi Iwai352f7f92012-12-19 12:52:06 +01002504static void call_update_outputs(struct hda_codec *codec);
2505
2506/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002507static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002508{
2509 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002510 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002511 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002512 hda_nid_t pin;
2513
2514 pin = spec->hp_mic_pin;
2515 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2516
2517 if (!force) {
2518 val = snd_hda_codec_get_pin_target(codec, pin);
2519 if (as_mic) {
2520 if (val & PIN_IN)
2521 return;
2522 } else {
2523 if (val & PIN_OUT)
2524 return;
2525 }
2526 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002527
2528 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002529 /* if the HP pin doesn't support VREF and the codec driver gives an
2530 * alternative pin, set up the VREF on that pin instead
2531 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002532 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2533 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2534 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2535 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002536 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002537 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002538 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002539
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002540 if (!spec->hp_mic_jack_modes) {
2541 if (as_mic)
2542 val |= PIN_IN;
2543 else
2544 val = PIN_HP;
2545 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002546 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002547 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002548}
2549
2550/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002551static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002552{
2553 struct hda_gen_spec *spec = codec->spec;
2554 struct auto_pin_cfg *cfg = &spec->autocfg;
2555 unsigned int defcfg;
2556 hda_nid_t nid;
2557
Takashi Iwai967303d2013-02-19 17:12:42 +01002558 if (!spec->hp_mic) {
2559 if (spec->suppress_hp_mic_detect)
2560 return 0;
2561 /* automatic detection: only if no input or a single internal
2562 * input pin is found, try to detect the shared hp/mic
2563 */
2564 if (cfg->num_inputs > 1)
2565 return 0;
2566 else if (cfg->num_inputs == 1) {
2567 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2568 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2569 return 0;
2570 }
2571 }
2572
2573 spec->hp_mic = 0; /* clear once */
2574 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002575 return 0;
2576
Takashi Iwai967303d2013-02-19 17:12:42 +01002577 nid = 0;
2578 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2579 nid = cfg->line_out_pins[0];
2580 else if (cfg->hp_outs > 0)
2581 nid = cfg->hp_pins[0];
2582 if (!nid)
2583 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002584
2585 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2586 return 0; /* no input */
2587
Takashi Iwai967303d2013-02-19 17:12:42 +01002588 cfg->inputs[cfg->num_inputs].pin = nid;
2589 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002590 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002591 cfg->num_inputs++;
2592 spec->hp_mic = 1;
2593 spec->hp_mic_pin = nid;
2594 /* we can't handle auto-mic together with HP-mic */
2595 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002596 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002597 return 0;
2598}
2599
Takashi Iwai978e77e2013-01-10 16:57:58 +01002600/*
2601 * output jack mode
2602 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002603
2604static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2605
2606static const char * const out_jack_texts[] = {
2607 "Line Out", "Headphone Out",
2608};
2609
Takashi Iwai978e77e2013-01-10 16:57:58 +01002610static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2611 struct snd_ctl_elem_info *uinfo)
2612{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002613 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002614}
2615
2616static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2617 struct snd_ctl_elem_value *ucontrol)
2618{
2619 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2620 hda_nid_t nid = kcontrol->private_value;
2621 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2622 ucontrol->value.enumerated.item[0] = 1;
2623 else
2624 ucontrol->value.enumerated.item[0] = 0;
2625 return 0;
2626}
2627
2628static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2629 struct snd_ctl_elem_value *ucontrol)
2630{
2631 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2632 hda_nid_t nid = kcontrol->private_value;
2633 unsigned int val;
2634
2635 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2636 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2637 return 0;
2638 snd_hda_set_pin_ctl_cache(codec, nid, val);
2639 return 1;
2640}
2641
2642static const struct snd_kcontrol_new out_jack_mode_enum = {
2643 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2644 .info = out_jack_mode_info,
2645 .get = out_jack_mode_get,
2646 .put = out_jack_mode_put,
2647};
2648
2649static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2650{
2651 struct hda_gen_spec *spec = codec->spec;
2652 int i;
2653
2654 for (i = 0; i < spec->kctls.used; i++) {
2655 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2656 if (!strcmp(kctl->name, name) && kctl->index == idx)
2657 return true;
2658 }
2659 return false;
2660}
2661
2662static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2663 char *name, size_t name_len)
2664{
2665 struct hda_gen_spec *spec = codec->spec;
2666 int idx = 0;
2667
2668 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2669 strlcat(name, " Jack Mode", name_len);
2670
2671 for (; find_kctl_name(codec, name, idx); idx++)
2672 ;
2673}
2674
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002675static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2676{
2677 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002678 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002679 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2680 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2681 return 2;
2682 }
2683 return 1;
2684}
2685
Takashi Iwai978e77e2013-01-10 16:57:58 +01002686static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2687 hda_nid_t *pins)
2688{
2689 struct hda_gen_spec *spec = codec->spec;
2690 int i;
2691
2692 for (i = 0; i < num_pins; i++) {
2693 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002694 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002695 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002696 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002697 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002698 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002699 get_jack_mode_name(codec, pin, name, sizeof(name));
2700 knew = snd_hda_gen_add_kctl(spec, name,
2701 &out_jack_mode_enum);
2702 if (!knew)
2703 return -ENOMEM;
2704 knew->private_value = pin;
2705 }
2706 }
2707
2708 return 0;
2709}
2710
Takashi Iwai294765582013-01-17 09:52:11 +01002711/*
2712 * input jack mode
2713 */
2714
2715/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2716#define NUM_VREFS 6
2717
2718static const char * const vref_texts[NUM_VREFS] = {
2719 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2720 "", "Mic 80pc Bias", "Mic 100pc Bias"
2721};
2722
2723static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2724{
2725 unsigned int pincap;
2726
2727 pincap = snd_hda_query_pin_caps(codec, pin);
2728 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2729 /* filter out unusual vrefs */
2730 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2731 return pincap;
2732}
2733
2734/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2735static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2736{
2737 unsigned int i, n = 0;
2738
2739 for (i = 0; i < NUM_VREFS; i++) {
2740 if (vref_caps & (1 << i)) {
2741 if (n == item_idx)
2742 return i;
2743 n++;
2744 }
2745 }
2746 return 0;
2747}
2748
2749/* convert back from the vref ctl index to the enum item index */
2750static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2751{
2752 unsigned int i, n = 0;
2753
2754 for (i = 0; i < NUM_VREFS; i++) {
2755 if (i == idx)
2756 return n;
2757 if (vref_caps & (1 << i))
2758 n++;
2759 }
2760 return 0;
2761}
2762
2763static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2764 struct snd_ctl_elem_info *uinfo)
2765{
2766 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2767 hda_nid_t nid = kcontrol->private_value;
2768 unsigned int vref_caps = get_vref_caps(codec, nid);
2769
2770 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2771 vref_texts);
2772 /* set the right text */
2773 strcpy(uinfo->value.enumerated.name,
2774 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2775 return 0;
2776}
2777
2778static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2779 struct snd_ctl_elem_value *ucontrol)
2780{
2781 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2782 hda_nid_t nid = kcontrol->private_value;
2783 unsigned int vref_caps = get_vref_caps(codec, nid);
2784 unsigned int idx;
2785
2786 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2787 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2788 return 0;
2789}
2790
2791static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2792 struct snd_ctl_elem_value *ucontrol)
2793{
2794 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2795 hda_nid_t nid = kcontrol->private_value;
2796 unsigned int vref_caps = get_vref_caps(codec, nid);
2797 unsigned int val, idx;
2798
2799 val = snd_hda_codec_get_pin_target(codec, nid);
2800 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2801 if (idx == ucontrol->value.enumerated.item[0])
2802 return 0;
2803
2804 val &= ~AC_PINCTL_VREFEN;
2805 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2806 snd_hda_set_pin_ctl_cache(codec, nid, val);
2807 return 1;
2808}
2809
2810static const struct snd_kcontrol_new in_jack_mode_enum = {
2811 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2812 .info = in_jack_mode_info,
2813 .get = in_jack_mode_get,
2814 .put = in_jack_mode_put,
2815};
2816
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002817static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2818{
2819 struct hda_gen_spec *spec = codec->spec;
2820 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002821 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002822 nitems = hweight32(get_vref_caps(codec, pin));
2823 return nitems ? nitems : 1;
2824}
2825
Takashi Iwai294765582013-01-17 09:52:11 +01002826static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2827{
2828 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002829 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002830 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002831 unsigned int defcfg;
2832
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002833 if (pin == spec->hp_mic_pin)
2834 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002835
2836 /* no jack mode for fixed pins */
2837 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2838 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2839 return 0;
2840
2841 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002842 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002843 return 0;
2844
2845 get_jack_mode_name(codec, pin, name, sizeof(name));
2846 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2847 if (!knew)
2848 return -ENOMEM;
2849 knew->private_value = pin;
2850 return 0;
2851}
2852
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002853/*
2854 * HP/mic shared jack mode
2855 */
2856static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2857 struct snd_ctl_elem_info *uinfo)
2858{
2859 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2860 hda_nid_t nid = kcontrol->private_value;
2861 int out_jacks = get_out_jack_num_items(codec, nid);
2862 int in_jacks = get_in_jack_num_items(codec, nid);
2863 const char *text = NULL;
2864 int idx;
2865
2866 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2867 uinfo->count = 1;
2868 uinfo->value.enumerated.items = out_jacks + in_jacks;
2869 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2870 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2871 idx = uinfo->value.enumerated.item;
2872 if (idx < out_jacks) {
2873 if (out_jacks > 1)
2874 text = out_jack_texts[idx];
2875 else
2876 text = "Headphone Out";
2877 } else {
2878 idx -= out_jacks;
2879 if (in_jacks > 1) {
2880 unsigned int vref_caps = get_vref_caps(codec, nid);
2881 text = vref_texts[get_vref_idx(vref_caps, idx)];
2882 } else
2883 text = "Mic In";
2884 }
2885
2886 strcpy(uinfo->value.enumerated.name, text);
2887 return 0;
2888}
2889
2890static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2891{
2892 int out_jacks = get_out_jack_num_items(codec, nid);
2893 int in_jacks = get_in_jack_num_items(codec, nid);
2894 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2895 int idx = 0;
2896
2897 if (val & PIN_OUT) {
2898 if (out_jacks > 1 && val == PIN_HP)
2899 idx = 1;
2900 } else if (val & PIN_IN) {
2901 idx = out_jacks;
2902 if (in_jacks > 1) {
2903 unsigned int vref_caps = get_vref_caps(codec, nid);
2904 val &= AC_PINCTL_VREFEN;
2905 idx += cvt_from_vref_idx(vref_caps, val);
2906 }
2907 }
2908 return idx;
2909}
2910
2911static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2912 struct snd_ctl_elem_value *ucontrol)
2913{
2914 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2915 hda_nid_t nid = kcontrol->private_value;
2916 ucontrol->value.enumerated.item[0] =
2917 get_cur_hp_mic_jack_mode(codec, nid);
2918 return 0;
2919}
2920
2921static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2922 struct snd_ctl_elem_value *ucontrol)
2923{
2924 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2925 hda_nid_t nid = kcontrol->private_value;
2926 int out_jacks = get_out_jack_num_items(codec, nid);
2927 int in_jacks = get_in_jack_num_items(codec, nid);
2928 unsigned int val, oldval, idx;
2929
2930 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2931 idx = ucontrol->value.enumerated.item[0];
2932 if (oldval == idx)
2933 return 0;
2934
2935 if (idx < out_jacks) {
2936 if (out_jacks > 1)
2937 val = idx ? PIN_HP : PIN_OUT;
2938 else
2939 val = PIN_HP;
2940 } else {
2941 idx -= out_jacks;
2942 if (in_jacks > 1) {
2943 unsigned int vref_caps = get_vref_caps(codec, nid);
2944 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002945 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2946 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002947 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002948 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002949 }
2950 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002951 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002952
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002953 return 1;
2954}
2955
2956static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2957 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2958 .info = hp_mic_jack_mode_info,
2959 .get = hp_mic_jack_mode_get,
2960 .put = hp_mic_jack_mode_put,
2961};
2962
2963static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2964{
2965 struct hda_gen_spec *spec = codec->spec;
2966 struct snd_kcontrol_new *knew;
2967
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002968 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2969 &hp_mic_jack_mode_enum);
2970 if (!knew)
2971 return -ENOMEM;
2972 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002973 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002974 return 0;
2975}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002976
2977/*
2978 * Parse input paths
2979 */
2980
Takashi Iwai352f7f92012-12-19 12:52:06 +01002981/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002982static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002983{
2984 struct hda_amp_list *list;
2985
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002986 list = snd_array_new(&spec->loopback_list);
2987 if (!list)
2988 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002989 list->nid = mix;
2990 list->dir = HDA_INPUT;
2991 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002992 spec->loopback.amplist = spec->loopback_list.list;
2993 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002994}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002995
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002996/* return true if either a volume or a mute amp is found for the given
2997 * aamix path; the amp has to be either in the mixer node or its direct leaf
2998 */
2999static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3000 hda_nid_t pin, unsigned int *mix_val,
3001 unsigned int *mute_val)
3002{
3003 int idx, num_conns;
3004 const hda_nid_t *list;
3005 hda_nid_t nid;
3006
3007 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3008 if (idx < 0)
3009 return false;
3010
3011 *mix_val = *mute_val = 0;
3012 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3013 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3014 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3015 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3016 if (*mix_val && *mute_val)
3017 return true;
3018
3019 /* check leaf node */
3020 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3021 if (num_conns < idx)
3022 return false;
3023 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01003024 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3025 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003026 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01003027 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3028 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003029 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3030
3031 return *mix_val || *mute_val;
3032}
3033
Takashi Iwai352f7f92012-12-19 12:52:06 +01003034/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01003035static int new_analog_input(struct hda_codec *codec, int input_idx,
3036 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003037 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003039 struct hda_gen_spec *spec = codec->spec;
3040 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003041 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003042 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003044 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3045 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003046
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003047 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003048 if (!path)
3049 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003050 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01003051 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003052
3053 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003054 if (mix_val) {
3055 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003056 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003058 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
3060
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003061 if (mute_val) {
3062 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003063 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003065 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 }
3067
Takashi Iwai352f7f92012-12-19 12:52:06 +01003068 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003069 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003070 err = add_loopback_list(spec, mix_nid, idx);
3071 if (err < 0)
3072 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003073
3074 if (spec->mixer_nid != spec->mixer_merge_nid &&
3075 !spec->loopback_merge_path) {
3076 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3077 spec->mixer_merge_nid, 0);
3078 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003079 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003080 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003081 path->pin_fixed = true; /* static route */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003082 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003083 spec->loopback_merge_path =
3084 snd_hda_get_path_idx(codec, path);
3085 }
3086 }
3087
Takashi Iwai352f7f92012-12-19 12:52:06 +01003088 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089}
3090
Takashi Iwai352f7f92012-12-19 12:52:06 +01003091static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003093 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3094 return (pincap & AC_PINCAP_IN) != 0;
3095}
3096
3097/* Parse the codec tree and retrieve ADCs */
3098static int fill_adc_nids(struct hda_codec *codec)
3099{
3100 struct hda_gen_spec *spec = codec->spec;
3101 hda_nid_t nid;
3102 hda_nid_t *adc_nids = spec->adc_nids;
3103 int max_nums = ARRAY_SIZE(spec->adc_nids);
Takashi Iwai7639a062015-03-03 10:07:24 +01003104 int nums = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003105
Takashi Iwai7639a062015-03-03 10:07:24 +01003106 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003107 unsigned int caps = get_wcaps(codec, nid);
3108 int type = get_wcaps_type(caps);
3109
3110 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3111 continue;
3112 adc_nids[nums] = nid;
3113 if (++nums >= max_nums)
3114 break;
3115 }
3116 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003117
3118 /* copy the detected ADCs to all_adcs[] */
3119 spec->num_all_adcs = nums;
3120 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3121
Takashi Iwai352f7f92012-12-19 12:52:06 +01003122 return nums;
3123}
3124
3125/* filter out invalid adc_nids that don't give all active input pins;
3126 * if needed, check whether dynamic ADC-switching is available
3127 */
3128static int check_dyn_adc_switch(struct hda_codec *codec)
3129{
3130 struct hda_gen_spec *spec = codec->spec;
3131 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003132 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003133 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003134
Takashi Iwai352f7f92012-12-19 12:52:06 +01003135 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003136 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003137 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003138 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003139 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003140 break;
3141 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003142 if (i >= imux->num_items) {
3143 ok_bits |= (1 << n);
3144 nums++;
3145 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003146 }
3147
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003148 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003149 /* check whether ADC-switch is possible */
3150 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003151 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003152 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003153 spec->dyn_adc_idx[i] = n;
3154 break;
3155 }
3156 }
3157 }
3158
Takashi Iwai4e76a882014-02-25 12:21:03 +01003159 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003160 spec->dyn_adc_switch = 1;
3161 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003162 /* shrink the invalid adcs and input paths */
3163 nums = 0;
3164 for (n = 0; n < spec->num_adc_nids; n++) {
3165 if (!(ok_bits & (1 << n)))
3166 continue;
3167 if (n != nums) {
3168 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003169 for (i = 0; i < imux->num_items; i++) {
3170 invalidate_nid_path(codec,
3171 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003172 spec->input_paths[i][nums] =
3173 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003174 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003175 }
3176 nums++;
3177 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003178 spec->num_adc_nids = nums;
3179 }
3180
Takashi Iwai967303d2013-02-19 17:12:42 +01003181 if (imux->num_items == 1 ||
3182 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003183 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003184 spec->num_adc_nids = 1; /* reduce to a single ADC */
3185 }
3186
3187 /* single index for individual volumes ctls */
3188 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3189 spec->num_adc_nids = 1;
3190
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 return 0;
3192}
3193
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003194/* parse capture source paths from the given pin and create imux items */
3195static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003196 int cfg_idx, int num_adcs,
3197 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003198{
3199 struct hda_gen_spec *spec = codec->spec;
3200 struct hda_input_mux *imux = &spec->input_mux;
3201 int imux_idx = imux->num_items;
3202 bool imux_added = false;
3203 int c;
3204
3205 for (c = 0; c < num_adcs; c++) {
3206 struct nid_path *path;
3207 hda_nid_t adc = spec->adc_nids[c];
3208
3209 if (!is_reachable_path(codec, pin, adc))
3210 continue;
3211 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3212 if (!path)
3213 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003214 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003215 spec->input_paths[imux_idx][c] =
3216 snd_hda_get_path_idx(codec, path);
3217
3218 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003219 if (spec->hp_mic_pin == pin)
3220 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003221 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003222 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003223 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003224 if (spec->dyn_adc_switch)
3225 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003226 }
3227 }
3228
3229 return 0;
3230}
3231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003233 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003235
Takashi Iwaic9700422013-01-18 10:17:30 +01003236/* fill the label for each input at first */
3237static int fill_input_pin_labels(struct hda_codec *codec)
3238{
3239 struct hda_gen_spec *spec = codec->spec;
3240 const struct auto_pin_cfg *cfg = &spec->autocfg;
3241 int i;
3242
3243 for (i = 0; i < cfg->num_inputs; i++) {
3244 hda_nid_t pin = cfg->inputs[i].pin;
3245 const char *label;
3246 int j, idx;
3247
3248 if (!is_input_pin(codec, pin))
3249 continue;
3250
3251 label = hda_get_autocfg_input_label(codec, cfg, i);
3252 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003253 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003254 if (spec->input_labels[j] &&
3255 !strcmp(spec->input_labels[j], label)) {
3256 idx = spec->input_label_idxs[j] + 1;
3257 break;
3258 }
3259 }
3260
3261 spec->input_labels[i] = label;
3262 spec->input_label_idxs[i] = idx;
3263 }
3264
3265 return 0;
3266}
3267
Takashi Iwai9dba2052013-01-18 10:01:15 +01003268#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3269
Takashi Iwai352f7f92012-12-19 12:52:06 +01003270static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003271{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003272 struct hda_gen_spec *spec = codec->spec;
3273 const struct auto_pin_cfg *cfg = &spec->autocfg;
3274 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003275 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003276 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003277 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003278
Takashi Iwai352f7f92012-12-19 12:52:06 +01003279 num_adcs = fill_adc_nids(codec);
3280 if (num_adcs < 0)
3281 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003282
Takashi Iwaic9700422013-01-18 10:17:30 +01003283 err = fill_input_pin_labels(codec);
3284 if (err < 0)
3285 return err;
3286
Takashi Iwai352f7f92012-12-19 12:52:06 +01003287 for (i = 0; i < cfg->num_inputs; i++) {
3288 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Takashi Iwai352f7f92012-12-19 12:52:06 +01003290 pin = cfg->inputs[i].pin;
3291 if (!is_input_pin(codec, pin))
3292 continue;
3293
Takashi Iwai2c12c302013-01-10 09:33:29 +01003294 val = PIN_IN;
3295 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3296 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai3e1b0c42015-04-27 10:43:22 +02003297 if (pin != spec->hp_mic_pin &&
3298 !snd_hda_codec_get_pin_target(codec, pin))
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003299 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003300
Takashi Iwai352f7f92012-12-19 12:52:06 +01003301 if (mixer) {
3302 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003303 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003304 spec->input_labels[i],
3305 spec->input_label_idxs[i],
3306 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003307 if (err < 0)
3308 return err;
3309 }
3310 }
3311
Takashi Iwaic9700422013-01-18 10:17:30 +01003312 err = parse_capture_source(codec, pin, i, num_adcs,
3313 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003314 if (err < 0)
3315 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003316
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003317 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003318 err = create_in_jack_mode(codec, pin);
3319 if (err < 0)
3320 return err;
3321 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003322 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003323
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003324 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003325 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003326 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003327 "Stereo Mix", 0);
3328 if (err < 0)
3329 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003330 else
3331 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003332 }
3333
3334 return 0;
3335}
3336
3337
3338/*
3339 * input source mux
3340 */
3341
Takashi Iwaic697b712013-01-07 17:09:26 +01003342/* get the input path specified by the given adc and imux indices */
3343static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003344{
3345 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003346 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3347 snd_BUG();
3348 return NULL;
3349 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003350 if (spec->dyn_adc_switch)
3351 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003352 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003353 snd_BUG();
3354 return NULL;
3355 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003356 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003357}
3358
3359static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3360 unsigned int idx);
3361
3362static int mux_enum_info(struct snd_kcontrol *kcontrol,
3363 struct snd_ctl_elem_info *uinfo)
3364{
3365 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3366 struct hda_gen_spec *spec = codec->spec;
3367 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3368}
3369
3370static int mux_enum_get(struct snd_kcontrol *kcontrol,
3371 struct snd_ctl_elem_value *ucontrol)
3372{
3373 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3374 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003375 /* the ctls are created at once with multiple counts */
3376 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003377
3378 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3379 return 0;
3380}
3381
3382static int mux_enum_put(struct snd_kcontrol *kcontrol,
3383 struct snd_ctl_elem_value *ucontrol)
3384{
3385 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003386 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003387 return mux_select(codec, adc_idx,
3388 ucontrol->value.enumerated.item[0]);
3389}
3390
Takashi Iwai352f7f92012-12-19 12:52:06 +01003391static const struct snd_kcontrol_new cap_src_temp = {
3392 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3393 .name = "Input Source",
3394 .info = mux_enum_info,
3395 .get = mux_enum_get,
3396 .put = mux_enum_put,
3397};
3398
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003399/*
3400 * capture volume and capture switch ctls
3401 */
3402
Takashi Iwai352f7f92012-12-19 12:52:06 +01003403typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3404 struct snd_ctl_elem_value *ucontrol);
3405
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003406/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003407static int cap_put_caller(struct snd_kcontrol *kcontrol,
3408 struct snd_ctl_elem_value *ucontrol,
3409 put_call_t func, int type)
3410{
3411 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3412 struct hda_gen_spec *spec = codec->spec;
3413 const struct hda_input_mux *imux;
3414 struct nid_path *path;
3415 int i, adc_idx, err = 0;
3416
3417 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003418 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003419 mutex_lock(&codec->control_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003420 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003421 path = get_input_path(codec, adc_idx, i);
3422 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003423 continue;
3424 kcontrol->private_value = path->ctls[type];
3425 err = func(kcontrol, ucontrol);
3426 if (err < 0)
Takashi Iwaia551d912015-02-26 12:34:49 +01003427 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003428 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003429 mutex_unlock(&codec->control_mutex);
3430 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003431 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003432 return err;
3433}
3434
3435/* capture volume ctl callbacks */
3436#define cap_vol_info snd_hda_mixer_amp_volume_info
3437#define cap_vol_get snd_hda_mixer_amp_volume_get
3438#define cap_vol_tlv snd_hda_mixer_amp_tlv
3439
3440static int cap_vol_put(struct snd_kcontrol *kcontrol,
3441 struct snd_ctl_elem_value *ucontrol)
3442{
3443 return cap_put_caller(kcontrol, ucontrol,
3444 snd_hda_mixer_amp_volume_put,
3445 NID_PATH_VOL_CTL);
3446}
3447
3448static const struct snd_kcontrol_new cap_vol_temp = {
3449 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3450 .name = "Capture Volume",
3451 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3452 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3453 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3454 .info = cap_vol_info,
3455 .get = cap_vol_get,
3456 .put = cap_vol_put,
3457 .tlv = { .c = cap_vol_tlv },
3458};
3459
3460/* capture switch ctl callbacks */
3461#define cap_sw_info snd_ctl_boolean_stereo_info
3462#define cap_sw_get snd_hda_mixer_amp_switch_get
3463
3464static int cap_sw_put(struct snd_kcontrol *kcontrol,
3465 struct snd_ctl_elem_value *ucontrol)
3466{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003467 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003468 snd_hda_mixer_amp_switch_put,
3469 NID_PATH_MUTE_CTL);
3470}
3471
3472static const struct snd_kcontrol_new cap_sw_temp = {
3473 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3474 .name = "Capture Switch",
3475 .info = cap_sw_info,
3476 .get = cap_sw_get,
3477 .put = cap_sw_put,
3478};
3479
3480static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3481{
3482 hda_nid_t nid;
3483 int i, depth;
3484
3485 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3486 for (depth = 0; depth < 3; depth++) {
3487 if (depth >= path->depth)
3488 return -EINVAL;
3489 i = path->depth - depth - 1;
3490 nid = path->path[i];
3491 if (!path->ctls[NID_PATH_VOL_CTL]) {
3492 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3493 path->ctls[NID_PATH_VOL_CTL] =
3494 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3495 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3496 int idx = path->idx[i];
3497 if (!depth && codec->single_adc_amp)
3498 idx = 0;
3499 path->ctls[NID_PATH_VOL_CTL] =
3500 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3501 }
3502 }
3503 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3504 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3505 path->ctls[NID_PATH_MUTE_CTL] =
3506 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3507 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3508 int idx = path->idx[i];
3509 if (!depth && codec->single_adc_amp)
3510 idx = 0;
3511 path->ctls[NID_PATH_MUTE_CTL] =
3512 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3513 }
3514 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 return 0;
3517}
3518
Takashi Iwai352f7f92012-12-19 12:52:06 +01003519static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003521 struct hda_gen_spec *spec = codec->spec;
3522 struct auto_pin_cfg *cfg = &spec->autocfg;
3523 unsigned int val;
3524 int i;
3525
3526 if (!spec->inv_dmic_split)
3527 return false;
3528 for (i = 0; i < cfg->num_inputs; i++) {
3529 if (cfg->inputs[i].pin != nid)
3530 continue;
3531 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3532 return false;
3533 val = snd_hda_codec_get_pincfg(codec, nid);
3534 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3535 }
3536 return false;
3537}
3538
Takashi Iwaia90229e2013-01-18 14:10:00 +01003539/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003540static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3541 struct snd_ctl_elem_value *ucontrol)
3542{
3543 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3544 struct hda_gen_spec *spec = codec->spec;
3545 int ret;
3546
3547 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3548 if (ret < 0)
3549 return ret;
3550
Takashi Iwaia90229e2013-01-18 14:10:00 +01003551 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003552 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003553
3554 return ret;
3555}
3556
Takashi Iwai352f7f92012-12-19 12:52:06 +01003557static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3558 int idx, bool is_switch, unsigned int ctl,
3559 bool inv_dmic)
3560{
3561 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003562 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003563 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3564 const char *sfx = is_switch ? "Switch" : "Volume";
3565 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003566 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003567
3568 if (!ctl)
3569 return 0;
3570
3571 if (label)
3572 snprintf(tmpname, sizeof(tmpname),
3573 "%s Capture %s", label, sfx);
3574 else
3575 snprintf(tmpname, sizeof(tmpname),
3576 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003577 knew = add_control(spec, type, tmpname, idx,
3578 amp_val_replace_channels(ctl, chs));
3579 if (!knew)
3580 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003581 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003582 knew->put = cap_single_sw_put;
3583 if (!inv_dmic)
3584 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003585
3586 /* Make independent right kcontrol */
3587 if (label)
3588 snprintf(tmpname, sizeof(tmpname),
3589 "Inverted %s Capture %s", label, sfx);
3590 else
3591 snprintf(tmpname, sizeof(tmpname),
3592 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003593 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003594 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003595 if (!knew)
3596 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003597 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003598 knew->put = cap_single_sw_put;
3599 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003600}
3601
3602/* create single (and simple) capture volume and switch controls */
3603static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3604 unsigned int vol_ctl, unsigned int sw_ctl,
3605 bool inv_dmic)
3606{
3607 int err;
3608 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3609 if (err < 0)
3610 return err;
3611 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3612 if (err < 0)
3613 return err;
3614 return 0;
3615}
3616
3617/* create bound capture volume and switch controls */
3618static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3619 unsigned int vol_ctl, unsigned int sw_ctl)
3620{
3621 struct hda_gen_spec *spec = codec->spec;
3622 struct snd_kcontrol_new *knew;
3623
3624 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003625 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003626 if (!knew)
3627 return -ENOMEM;
3628 knew->index = idx;
3629 knew->private_value = vol_ctl;
3630 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3631 }
3632 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003633 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003634 if (!knew)
3635 return -ENOMEM;
3636 knew->index = idx;
3637 knew->private_value = sw_ctl;
3638 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3639 }
3640 return 0;
3641}
3642
3643/* return the vol ctl when used first in the imux list */
3644static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3645{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003646 struct nid_path *path;
3647 unsigned int ctl;
3648 int i;
3649
Takashi Iwaic697b712013-01-07 17:09:26 +01003650 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003651 if (!path)
3652 return 0;
3653 ctl = path->ctls[type];
3654 if (!ctl)
3655 return 0;
3656 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003657 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003658 if (path && path->ctls[type] == ctl)
3659 return 0;
3660 }
3661 return ctl;
3662}
3663
3664/* create individual capture volume and switch controls per input */
3665static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3666{
3667 struct hda_gen_spec *spec = codec->spec;
3668 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003669 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003670
3671 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003672 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003673 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003674
Takashi Iwaic9700422013-01-18 10:17:30 +01003675 idx = imux->items[i].index;
3676 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003677 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003678 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3679
3680 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003681 err = add_single_cap_ctl(codec,
3682 spec->input_labels[idx],
3683 spec->input_label_idxs[idx],
3684 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003685 get_first_cap_ctl(codec, i, type),
3686 inv_dmic);
3687 if (err < 0)
3688 return err;
3689 }
3690 }
3691 return 0;
3692}
3693
3694static int create_capture_mixers(struct hda_codec *codec)
3695{
3696 struct hda_gen_spec *spec = codec->spec;
3697 struct hda_input_mux *imux = &spec->input_mux;
3698 int i, n, nums, err;
3699
3700 if (spec->dyn_adc_switch)
3701 nums = 1;
3702 else
3703 nums = spec->num_adc_nids;
3704
3705 if (!spec->auto_mic && imux->num_items > 1) {
3706 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003707 const char *name;
3708 name = nums > 1 ? "Input Source" : "Capture Source";
3709 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003710 if (!knew)
3711 return -ENOMEM;
3712 knew->count = nums;
3713 }
3714
3715 for (n = 0; n < nums; n++) {
3716 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003717 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003718 bool inv_dmic = false;
3719 int vol, sw;
3720
3721 vol = sw = 0;
3722 for (i = 0; i < imux->num_items; i++) {
3723 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003724 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003725 if (!path)
3726 continue;
3727 parse_capvol_in_path(codec, path);
3728 if (!vol)
3729 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003730 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003731 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003732 if (!same_amp_caps(codec, vol,
3733 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3734 multi_cap_vol = true;
3735 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003736 if (!sw)
3737 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003738 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003739 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003740 if (!same_amp_caps(codec, sw,
3741 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3742 multi_cap_vol = true;
3743 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003744 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3745 inv_dmic = true;
3746 }
3747
3748 if (!multi)
3749 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3750 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003751 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003752 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3753 else
3754 err = create_multi_cap_vol_ctl(codec);
3755 if (err < 0)
3756 return err;
3757 }
3758
3759 return 0;
3760}
3761
3762/*
3763 * add mic boosts if needed
3764 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003765
3766/* check whether the given amp is feasible as a boost volume */
3767static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3768 int dir, int idx)
3769{
3770 unsigned int step;
3771
3772 if (!nid_has_volume(codec, nid, dir) ||
3773 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3774 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3775 return false;
3776
3777 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3778 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3779 if (step < 0x20)
3780 return false;
3781 return true;
3782}
3783
3784/* look for a boost amp in a widget close to the pin */
3785static unsigned int look_for_boost_amp(struct hda_codec *codec,
3786 struct nid_path *path)
3787{
3788 unsigned int val = 0;
3789 hda_nid_t nid;
3790 int depth;
3791
3792 for (depth = 0; depth < 3; depth++) {
3793 if (depth >= path->depth - 1)
3794 break;
3795 nid = path->path[depth];
3796 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3797 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3798 break;
3799 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3800 path->idx[depth])) {
3801 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3802 HDA_INPUT);
3803 break;
3804 }
3805 }
3806
3807 return val;
3808}
3809
Takashi Iwai352f7f92012-12-19 12:52:06 +01003810static int parse_mic_boost(struct hda_codec *codec)
3811{
3812 struct hda_gen_spec *spec = codec->spec;
3813 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003814 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003815 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003816
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003817 if (!spec->num_adc_nids)
3818 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003819
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003820 for (i = 0; i < imux->num_items; i++) {
3821 struct nid_path *path;
3822 unsigned int val;
3823 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003824 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003825
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003826 idx = imux->items[i].index;
3827 if (idx >= imux->num_items)
3828 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003829
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003830 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003831 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003832 continue;
3833
3834 path = get_input_path(codec, 0, i);
3835 if (!path)
3836 continue;
3837
3838 val = look_for_boost_amp(codec, path);
3839 if (!val)
3840 continue;
3841
3842 /* create a boost control */
3843 snprintf(boost_label, sizeof(boost_label),
3844 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003845 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3846 spec->input_label_idxs[idx], val))
3847 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003848
3849 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003850 }
3851 return 0;
3852}
3853
3854/*
3855 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3856 */
3857static void parse_digital(struct hda_codec *codec)
3858{
3859 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003860 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003861 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003862 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003863
3864 /* support multiple SPDIFs; the secondary is set up as a slave */
3865 nums = 0;
3866 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003867 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003868 dig_nid = look_for_dac(codec, pin, true);
3869 if (!dig_nid)
3870 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003871 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003872 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003873 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003874 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003875 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003876 path->pin_fixed = true; /* no jack detection */
Takashi Iwai196c17662013-01-04 15:01:40 +01003877 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003878 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003879 if (!nums) {
3880 spec->multiout.dig_out_nid = dig_nid;
3881 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3882 } else {
3883 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3884 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd5764222014-05-14 17:18:31 +03003885 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003886 spec->slave_dig_outs[nums - 1] = dig_nid;
3887 }
3888 nums++;
3889 }
3890
3891 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003892 pin = spec->autocfg.dig_in_pin;
Takashi Iwai7639a062015-03-03 10:07:24 +01003893 for_each_hda_codec_node(dig_nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003894 unsigned int wcaps = get_wcaps(codec, dig_nid);
3895 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3896 continue;
3897 if (!(wcaps & AC_WCAP_DIGITAL))
3898 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003899 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003900 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003901 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003902 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003903 path->pin_fixed = true; /* no jack */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003904 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003905 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003906 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003907 break;
3908 }
3909 }
3910 }
3911}
3912
3913
3914/*
3915 * input MUX handling
3916 */
3917
3918static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3919
3920/* select the given imux item; either unmute exclusively or select the route */
3921static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3922 unsigned int idx)
3923{
3924 struct hda_gen_spec *spec = codec->spec;
3925 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003926 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003927
3928 imux = &spec->input_mux;
3929 if (!imux->num_items)
3930 return 0;
3931
3932 if (idx >= imux->num_items)
3933 idx = imux->num_items - 1;
3934 if (spec->cur_mux[adc_idx] == idx)
3935 return 0;
3936
Takashi Iwai55196ff2013-01-24 17:32:56 +01003937 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3938 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003939 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003940 if (old_path->active)
3941 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003942
3943 spec->cur_mux[adc_idx] = idx;
3944
Takashi Iwai967303d2013-02-19 17:12:42 +01003945 if (spec->hp_mic)
3946 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003947
3948 if (spec->dyn_adc_switch)
3949 dyn_adc_pcm_resetup(codec, idx);
3950
Takashi Iwaic697b712013-01-07 17:09:26 +01003951 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003952 if (!path)
3953 return 0;
3954 if (path->active)
3955 return 0;
3956 snd_hda_activate_path(codec, path, true, false);
3957 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003958 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003959 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003960 return 1;
3961}
3962
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003963/* power up/down widgets in the all paths that match with the given NID
3964 * as terminals (either start- or endpoint)
3965 *
3966 * returns the last changed NID, or zero if unchanged.
3967 */
3968static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3969 int pin_state, int stream_state)
3970{
3971 struct hda_gen_spec *spec = codec->spec;
3972 hda_nid_t last, changed = 0;
3973 struct nid_path *path;
3974 int n;
3975
3976 for (n = 0; n < spec->paths.used; n++) {
3977 path = snd_array_elem(&spec->paths, n);
Bob Copeland81e43962016-06-25 07:58:45 -04003978 if (!path->depth)
3979 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003980 if (path->path[0] == nid ||
3981 path->path[path->depth - 1] == nid) {
3982 bool pin_old = path->pin_enabled;
3983 bool stream_old = path->stream_enabled;
3984
3985 if (pin_state >= 0)
3986 path->pin_enabled = pin_state;
3987 if (stream_state >= 0)
3988 path->stream_enabled = stream_state;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003989 if ((!path->pin_fixed && path->pin_enabled != pin_old)
3990 || path->stream_enabled != stream_old) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003991 last = path_power_update(codec, path, true);
3992 if (last)
3993 changed = last;
3994 }
3995 }
3996 }
3997 return changed;
3998}
3999
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004000/* check the jack status for power control */
4001static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4002{
4003 if (!is_jack_detectable(codec, pin))
4004 return true;
4005 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4006}
4007
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004008/* power up/down the paths of the given pin according to the jack state;
4009 * power = 0/1 : only power up/down if it matches with the jack state,
4010 * < 0 : force power up/down to follow the jack sate
4011 *
4012 * returns the last changed NID, or zero if unchanged.
4013 */
4014static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4015 int power)
4016{
4017 bool on;
4018
Takashi Iwai967b1302015-03-20 18:21:03 +01004019 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004020 return 0;
4021
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004022 on = detect_pin_state(codec, pin);
4023
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004024 if (power >= 0 && on != power)
4025 return 0;
4026 return set_path_power(codec, pin, on, -1);
4027}
4028
4029static void pin_power_callback(struct hda_codec *codec,
4030 struct hda_jack_callback *jack,
4031 bool on)
4032{
Takashi Iwai2ebab402016-02-09 10:23:52 +01004033 if (jack && jack->nid)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004034 sync_power_state_change(codec,
Takashi Iwai2ebab402016-02-09 10:23:52 +01004035 set_pin_power_jack(codec, jack->nid, on));
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004036}
4037
4038/* callback only doing power up -- called at first */
4039static void pin_power_up_callback(struct hda_codec *codec,
4040 struct hda_jack_callback *jack)
4041{
4042 pin_power_callback(codec, jack, true);
4043}
4044
4045/* callback only doing power down -- called at last */
4046static void pin_power_down_callback(struct hda_codec *codec,
4047 struct hda_jack_callback *jack)
4048{
4049 pin_power_callback(codec, jack, false);
4050}
4051
4052/* set up the power up/down callbacks */
4053static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4054 const hda_nid_t *pins, bool on)
4055{
4056 int i;
4057 hda_jack_callback_fn cb =
4058 on ? pin_power_up_callback : pin_power_down_callback;
4059
4060 for (i = 0; i < num_pins && pins[i]; i++) {
4061 if (is_jack_detectable(codec, pins[i]))
4062 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4063 else
4064 set_path_power(codec, pins[i], true, -1);
4065 }
4066}
4067
4068/* enabled power callback to each available I/O pin with jack detections;
4069 * the digital I/O pins are excluded because of the unreliable detectsion
4070 */
4071static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4072{
4073 struct hda_gen_spec *spec = codec->spec;
4074 struct auto_pin_cfg *cfg = &spec->autocfg;
4075 int i;
4076
Takashi Iwai967b1302015-03-20 18:21:03 +01004077 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004078 return;
4079 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4080 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4081 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4082 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4083 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4084 for (i = 0; i < cfg->num_inputs; i++)
4085 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4086}
4087
4088/* sync path power up/down with the jack states of given pins */
4089static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4090 const hda_nid_t *pins)
4091{
4092 int i;
4093
4094 for (i = 0; i < num_pins && pins[i]; i++)
4095 if (is_jack_detectable(codec, pins[i]))
4096 set_pin_power_jack(codec, pins[i], -1);
4097}
4098
4099/* sync path power up/down with pins; called at init and resume */
4100static void sync_all_pin_power_ctls(struct hda_codec *codec)
4101{
4102 struct hda_gen_spec *spec = codec->spec;
4103 struct auto_pin_cfg *cfg = &spec->autocfg;
4104 int i;
4105
Takashi Iwai967b1302015-03-20 18:21:03 +01004106 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004107 return;
4108 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4109 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4110 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4111 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4112 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4113 for (i = 0; i < cfg->num_inputs; i++)
4114 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4115}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004116
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004117/* add fake paths if not present yet */
4118static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4119 int num_pins, const hda_nid_t *pins)
4120{
4121 struct hda_gen_spec *spec = codec->spec;
4122 struct nid_path *path;
4123 int i;
4124
4125 for (i = 0; i < num_pins; i++) {
4126 if (!pins[i])
4127 break;
4128 if (get_nid_path(codec, nid, pins[i], 0))
4129 continue;
4130 path = snd_array_new(&spec->paths);
4131 if (!path)
4132 return -ENOMEM;
4133 memset(path, 0, sizeof(*path));
4134 path->depth = 2;
4135 path->path[0] = nid;
4136 path->path[1] = pins[i];
4137 path->active = true;
4138 }
4139 return 0;
4140}
4141
4142/* create fake paths to all outputs from beep */
4143static int add_fake_beep_paths(struct hda_codec *codec)
4144{
4145 struct hda_gen_spec *spec = codec->spec;
4146 struct auto_pin_cfg *cfg = &spec->autocfg;
4147 hda_nid_t nid = spec->beep_nid;
4148 int err;
4149
Takashi Iwai967b1302015-03-20 18:21:03 +01004150 if (!codec->power_save_node || !nid)
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004151 return 0;
4152 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4153 if (err < 0)
4154 return err;
4155 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4156 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4157 if (err < 0)
4158 return err;
4159 }
4160 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4161 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4162 cfg->speaker_pins);
4163 if (err < 0)
4164 return err;
4165 }
4166 return 0;
4167}
4168
4169/* power up/down beep widget and its output paths */
4170static void beep_power_hook(struct hda_beep *beep, bool on)
4171{
4172 set_path_power(beep->codec, beep->nid, -1, on);
4173}
4174
Takashi Iwai6b275b12015-03-20 18:11:05 +01004175/**
4176 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4177 * @codec: the HDA codec
4178 * @pin: NID of pin to fix
4179 */
4180int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4181{
4182 struct hda_gen_spec *spec = codec->spec;
4183 struct nid_path *path;
4184
4185 path = snd_array_new(&spec->paths);
4186 if (!path)
4187 return -ENOMEM;
4188 memset(path, 0, sizeof(*path));
4189 path->depth = 1;
4190 path->path[0] = pin;
4191 path->active = true;
4192 path->pin_fixed = true;
4193 path->stream_enabled = true;
4194 return 0;
4195}
4196EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4197
Takashi Iwai352f7f92012-12-19 12:52:06 +01004198/*
4199 * Jack detections for HP auto-mute and mic-switch
4200 */
4201
4202/* check each pin in the given array; returns true if any of them is plugged */
4203static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4204{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004205 int i;
4206 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004207
4208 for (i = 0; i < num_pins; i++) {
4209 hda_nid_t nid = pins[i];
4210 if (!nid)
4211 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01004212 /* don't detect pins retasked as inputs */
4213 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4214 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004215 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4216 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004217 }
4218 return present;
4219}
4220
4221/* standard HP/line-out auto-mute helper */
4222static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004223 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004224{
4225 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004226 int i;
4227
4228 for (i = 0; i < num_pins; i++) {
4229 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01004230 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004231 if (!nid)
4232 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004233
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004234 oldval = snd_hda_codec_get_pin_target(codec, nid);
4235 if (oldval & PIN_IN)
4236 continue; /* no mute for inputs */
4237
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004238 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004239 struct nid_path *path;
4240 hda_nid_t mute_nid;
4241
4242 path = snd_hda_get_path_from_idx(codec, paths[i]);
4243 if (!path)
4244 continue;
4245 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4246 if (!mute_nid)
4247 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004248 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004249 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004250 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004251 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004252 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004253 } else {
4254 /* don't reset VREF value in case it's controlling
4255 * the amp (see alc861_fixup_asus_amp_vref_0f())
4256 */
4257 if (spec->keep_vref_in_automute)
4258 val = oldval & ~PIN_HP;
4259 else
4260 val = 0;
4261 if (!mute)
4262 val |= oldval;
4263 /* here we call update_pin_ctl() so that the pinctl is
4264 * changed without changing the pinctl target value;
4265 * the original target value will be still referred at
4266 * the init / resume again
4267 */
4268 update_pin_ctl(codec, nid, val);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004269 }
4270
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01004271 set_pin_eapd(codec, nid, !mute);
Takashi Iwai967b1302015-03-20 18:21:03 +01004272 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004273 bool on = !mute;
4274 if (on)
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004275 on = detect_pin_state(codec, nid);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004276 set_path_power(codec, nid, on, -1);
4277 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004278 }
4279}
4280
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004281/**
4282 * snd_hda_gen_update_outputs - Toggle outputs muting
4283 * @codec: the HDA codec
4284 *
4285 * Update the mute status of all outputs based on the current jack states.
4286 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004287void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004288{
4289 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004290 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004291 int on;
4292
4293 /* Control HP pins/amps depending on master_mute state;
4294 * in general, HP pins/amps control should be enabled in all cases,
4295 * but currently set only for master_mute, just to be safe
4296 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004297 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4298 paths = spec->out_paths;
4299 else
4300 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01004301 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004302 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004303
4304 if (!spec->automute_speaker)
4305 on = 0;
4306 else
4307 on = spec->hp_jack_present | spec->line_jack_present;
4308 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004309 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004310 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4311 paths = spec->out_paths;
4312 else
4313 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004314 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004315 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004316
4317 /* toggle line-out mutes if needed, too */
4318 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4319 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4320 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4321 return;
4322 if (!spec->automute_lo)
4323 on = 0;
4324 else
4325 on = spec->hp_jack_present;
4326 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004327 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004328 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004329 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004330 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004331}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004332EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004333
4334static void call_update_outputs(struct hda_codec *codec)
4335{
4336 struct hda_gen_spec *spec = codec->spec;
4337 if (spec->automute_hook)
4338 spec->automute_hook(codec);
4339 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004340 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004341
4342 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4343 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4344 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004345}
4346
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004347/**
4348 * snd_hda_gen_hp_automute - standard HP-automute helper
4349 * @codec: the HDA codec
4350 * @jack: jack object, NULL for the whole
4351 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004352void snd_hda_gen_hp_automute(struct hda_codec *codec,
4353 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004354{
4355 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004356 hda_nid_t *pins = spec->autocfg.hp_pins;
4357 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004358
Takashi Iwai92603c52013-01-22 07:46:31 +01004359 /* No detection for the first HP jack during indep-HP mode */
4360 if (spec->indep_hp_enabled) {
4361 pins++;
4362 num_pins--;
4363 }
4364
4365 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004366 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4367 return;
4368 call_update_outputs(codec);
4369}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004370EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004371
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004372/**
4373 * snd_hda_gen_line_automute - standard line-out-automute helper
4374 * @codec: the HDA codec
4375 * @jack: jack object, NULL for the whole
4376 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004377void snd_hda_gen_line_automute(struct hda_codec *codec,
4378 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004379{
4380 struct hda_gen_spec *spec = codec->spec;
4381
4382 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4383 return;
4384 /* check LO jack only when it's different from HP */
4385 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4386 return;
4387
4388 spec->line_jack_present =
4389 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4390 spec->autocfg.line_out_pins);
4391 if (!spec->automute_speaker || !spec->detect_lo)
4392 return;
4393 call_update_outputs(codec);
4394}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004395EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004396
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004397/**
4398 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4399 * @codec: the HDA codec
4400 * @jack: jack object, NULL for the whole
4401 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004402void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4403 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004404{
4405 struct hda_gen_spec *spec = codec->spec;
4406 int i;
4407
4408 if (!spec->auto_mic)
4409 return;
4410
4411 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004412 hda_nid_t pin = spec->am_entry[i].pin;
4413 /* don't detect pins retasked as outputs */
4414 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4415 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004416 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004417 mux_select(codec, 0, spec->am_entry[i].idx);
4418 return;
4419 }
4420 }
4421 mux_select(codec, 0, spec->am_entry[0].idx);
4422}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004423EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004424
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004425/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004426static void call_hp_automute(struct hda_codec *codec,
4427 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004428{
4429 struct hda_gen_spec *spec = codec->spec;
4430 if (spec->hp_automute_hook)
4431 spec->hp_automute_hook(codec, jack);
4432 else
4433 snd_hda_gen_hp_automute(codec, jack);
4434}
4435
4436static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004437 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004438{
4439 struct hda_gen_spec *spec = codec->spec;
4440 if (spec->line_automute_hook)
4441 spec->line_automute_hook(codec, jack);
4442 else
4443 snd_hda_gen_line_automute(codec, jack);
4444}
4445
4446static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004447 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004448{
4449 struct hda_gen_spec *spec = codec->spec;
4450 if (spec->mic_autoswitch_hook)
4451 spec->mic_autoswitch_hook(codec, jack);
4452 else
4453 snd_hda_gen_mic_autoswitch(codec, jack);
4454}
4455
Takashi Iwai963afde2013-05-31 15:20:31 +02004456/* update jack retasking */
4457static void update_automute_all(struct hda_codec *codec)
4458{
4459 call_hp_automute(codec, NULL);
4460 call_line_automute(codec, NULL);
4461 call_mic_autoswitch(codec, NULL);
4462}
4463
Takashi Iwai352f7f92012-12-19 12:52:06 +01004464/*
4465 * Auto-Mute mode mixer enum support
4466 */
4467static int automute_mode_info(struct snd_kcontrol *kcontrol,
4468 struct snd_ctl_elem_info *uinfo)
4469{
4470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4471 struct hda_gen_spec *spec = codec->spec;
4472 static const char * const texts3[] = {
4473 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004474 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Takashi Iwai352f7f92012-12-19 12:52:06 +01004476 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4477 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4478 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4479}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
Takashi Iwai352f7f92012-12-19 12:52:06 +01004481static int automute_mode_get(struct snd_kcontrol *kcontrol,
4482 struct snd_ctl_elem_value *ucontrol)
4483{
4484 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4485 struct hda_gen_spec *spec = codec->spec;
4486 unsigned int val = 0;
4487 if (spec->automute_speaker)
4488 val++;
4489 if (spec->automute_lo)
4490 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004491
Takashi Iwai352f7f92012-12-19 12:52:06 +01004492 ucontrol->value.enumerated.item[0] = val;
4493 return 0;
4494}
4495
4496static int automute_mode_put(struct snd_kcontrol *kcontrol,
4497 struct snd_ctl_elem_value *ucontrol)
4498{
4499 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4500 struct hda_gen_spec *spec = codec->spec;
4501
4502 switch (ucontrol->value.enumerated.item[0]) {
4503 case 0:
4504 if (!spec->automute_speaker && !spec->automute_lo)
4505 return 0;
4506 spec->automute_speaker = 0;
4507 spec->automute_lo = 0;
4508 break;
4509 case 1:
4510 if (spec->automute_speaker_possible) {
4511 if (!spec->automute_lo && spec->automute_speaker)
4512 return 0;
4513 spec->automute_speaker = 1;
4514 spec->automute_lo = 0;
4515 } else if (spec->automute_lo_possible) {
4516 if (spec->automute_lo)
4517 return 0;
4518 spec->automute_lo = 1;
4519 } else
4520 return -EINVAL;
4521 break;
4522 case 2:
4523 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4524 return -EINVAL;
4525 if (spec->automute_speaker && spec->automute_lo)
4526 return 0;
4527 spec->automute_speaker = 1;
4528 spec->automute_lo = 1;
4529 break;
4530 default:
4531 return -EINVAL;
4532 }
4533 call_update_outputs(codec);
4534 return 1;
4535}
4536
4537static const struct snd_kcontrol_new automute_mode_enum = {
4538 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4539 .name = "Auto-Mute Mode",
4540 .info = automute_mode_info,
4541 .get = automute_mode_get,
4542 .put = automute_mode_put,
4543};
4544
4545static int add_automute_mode_enum(struct hda_codec *codec)
4546{
4547 struct hda_gen_spec *spec = codec->spec;
4548
Takashi Iwai12c93df2012-12-19 14:38:33 +01004549 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004550 return -ENOMEM;
4551 return 0;
4552}
4553
4554/*
4555 * Check the availability of HP/line-out auto-mute;
4556 * Set up appropriately if really supported
4557 */
4558static int check_auto_mute_availability(struct hda_codec *codec)
4559{
4560 struct hda_gen_spec *spec = codec->spec;
4561 struct auto_pin_cfg *cfg = &spec->autocfg;
4562 int present = 0;
4563 int i, err;
4564
Takashi Iwaif72706b2013-01-16 18:20:07 +01004565 if (spec->suppress_auto_mute)
4566 return 0;
4567
Takashi Iwai352f7f92012-12-19 12:52:06 +01004568 if (cfg->hp_pins[0])
4569 present++;
4570 if (cfg->line_out_pins[0])
4571 present++;
4572 if (cfg->speaker_pins[0])
4573 present++;
4574 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004575 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004576
4577 if (!cfg->speaker_pins[0] &&
4578 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4579 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4580 sizeof(cfg->speaker_pins));
4581 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Takashi Iwai352f7f92012-12-19 12:52:06 +01004584 if (!cfg->hp_pins[0] &&
4585 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4586 memcpy(cfg->hp_pins, cfg->line_out_pins,
4587 sizeof(cfg->hp_pins));
4588 cfg->hp_outs = cfg->line_outs;
4589 }
4590
4591 for (i = 0; i < cfg->hp_outs; i++) {
4592 hda_nid_t nid = cfg->hp_pins[i];
4593 if (!is_jack_detectable(codec, nid))
4594 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004595 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004596 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004597 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004598 spec->detect_hp = 1;
4599 }
4600
4601 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4602 if (cfg->speaker_outs)
4603 for (i = 0; i < cfg->line_outs; i++) {
4604 hda_nid_t nid = cfg->line_out_pins[i];
4605 if (!is_jack_detectable(codec, nid))
4606 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004607 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004608 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004609 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004610 spec->detect_lo = 1;
4611 }
4612 spec->automute_lo_possible = spec->detect_hp;
4613 }
4614
4615 spec->automute_speaker_possible = cfg->speaker_outs &&
4616 (spec->detect_hp || spec->detect_lo);
4617
4618 spec->automute_lo = spec->automute_lo_possible;
4619 spec->automute_speaker = spec->automute_speaker_possible;
4620
4621 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4622 /* create a control for automute mode */
4623 err = add_automute_mode_enum(codec);
4624 if (err < 0)
4625 return err;
4626 }
4627 return 0;
4628}
4629
Takashi Iwai352f7f92012-12-19 12:52:06 +01004630/* check whether all auto-mic pins are valid; setup indices if OK */
4631static bool auto_mic_check_imux(struct hda_codec *codec)
4632{
4633 struct hda_gen_spec *spec = codec->spec;
4634 const struct hda_input_mux *imux;
4635 int i;
4636
4637 imux = &spec->input_mux;
4638 for (i = 0; i < spec->am_num_entries; i++) {
4639 spec->am_entry[i].idx =
4640 find_idx_in_nid_list(spec->am_entry[i].pin,
4641 spec->imux_pins, imux->num_items);
4642 if (spec->am_entry[i].idx < 0)
4643 return false; /* no corresponding imux */
4644 }
4645
4646 /* we don't need the jack detection for the first pin */
4647 for (i = 1; i < spec->am_num_entries; i++)
4648 snd_hda_jack_detect_enable_callback(codec,
4649 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004650 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004651 return true;
4652}
4653
4654static int compare_attr(const void *ap, const void *bp)
4655{
4656 const struct automic_entry *a = ap;
4657 const struct automic_entry *b = bp;
4658 return (int)(a->attr - b->attr);
4659}
4660
4661/*
4662 * Check the availability of auto-mic switch;
4663 * Set up if really supported
4664 */
4665static int check_auto_mic_availability(struct hda_codec *codec)
4666{
4667 struct hda_gen_spec *spec = codec->spec;
4668 struct auto_pin_cfg *cfg = &spec->autocfg;
4669 unsigned int types;
4670 int i, num_pins;
4671
Takashi Iwaid12daf62013-01-07 16:32:11 +01004672 if (spec->suppress_auto_mic)
4673 return 0;
4674
Takashi Iwai352f7f92012-12-19 12:52:06 +01004675 types = 0;
4676 num_pins = 0;
4677 for (i = 0; i < cfg->num_inputs; i++) {
4678 hda_nid_t nid = cfg->inputs[i].pin;
4679 unsigned int attr;
4680 attr = snd_hda_codec_get_pincfg(codec, nid);
4681 attr = snd_hda_get_input_pin_attr(attr);
4682 if (types & (1 << attr))
4683 return 0; /* already occupied */
4684 switch (attr) {
4685 case INPUT_PIN_ATTR_INT:
4686 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4687 return 0; /* invalid type */
4688 break;
4689 case INPUT_PIN_ATTR_UNUSED:
4690 return 0; /* invalid entry */
4691 default:
4692 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4693 return 0; /* invalid type */
4694 if (!spec->line_in_auto_switch &&
4695 cfg->inputs[i].type != AUTO_PIN_MIC)
4696 return 0; /* only mic is allowed */
4697 if (!is_jack_detectable(codec, nid))
4698 return 0; /* no unsol support */
4699 break;
4700 }
4701 if (num_pins >= MAX_AUTO_MIC_PINS)
4702 return 0;
4703 types |= (1 << attr);
4704 spec->am_entry[num_pins].pin = nid;
4705 spec->am_entry[num_pins].attr = attr;
4706 num_pins++;
4707 }
4708
4709 if (num_pins < 2)
4710 return 0;
4711
4712 spec->am_num_entries = num_pins;
4713 /* sort the am_entry in the order of attr so that the pin with a
4714 * higher attr will be selected when the jack is plugged.
4715 */
4716 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4717 compare_attr, NULL);
4718
4719 if (!auto_mic_check_imux(codec))
4720 return 0;
4721
4722 spec->auto_mic = 1;
4723 spec->num_adc_nids = 1;
4724 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004725 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004726 spec->am_entry[0].pin,
4727 spec->am_entry[1].pin,
4728 spec->am_entry[2].pin);
4729
4730 return 0;
4731}
4732
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004733/**
4734 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4735 * into power down
4736 * @codec: the HDA codec
4737 * @nid: NID to evalute
4738 * @power_state: target power state
4739 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004740unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004741 hda_nid_t nid,
4742 unsigned int power_state)
4743{
Takashi Iwaib6c09b32015-04-09 10:25:03 +02004744 struct hda_gen_spec *spec = codec->spec;
4745
4746 if (!spec->power_down_unused && !codec->power_save_node)
4747 return power_state;
Takashi Iwai7639a062015-03-03 10:07:24 +01004748 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004749 return power_state;
4750 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4751 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004752 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004753 return power_state;
4754 return AC_PWRST_D3;
4755}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004756EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004757
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004758/* mute all aamix inputs initially; parse up to the first leaves */
4759static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4760{
4761 int i, nums;
4762 const hda_nid_t *conn;
4763 bool has_amp;
4764
4765 nums = snd_hda_get_conn_list(codec, mix, &conn);
4766 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4767 for (i = 0; i < nums; i++) {
4768 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004769 update_amp(codec, mix, HDA_INPUT, i,
4770 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004771 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004772 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4773 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004774 }
4775}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004776
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004777/**
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004778 * snd_hda_gen_stream_pm - Stream power management callback
4779 * @codec: the HDA codec
4780 * @nid: audio widget
4781 * @on: power on/off flag
4782 *
Takashi Iwai967b1302015-03-20 18:21:03 +01004783 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004784 */
4785void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4786{
Takashi Iwai967b1302015-03-20 18:21:03 +01004787 if (codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004788 set_path_power(codec, nid, -1, on);
4789}
4790EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4791
4792/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004793 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4794 * set up the hda_gen_spec
4795 * @codec: the HDA codec
4796 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004797 *
4798 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004799 * or a negative error code
4800 */
4801int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004802 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004803{
4804 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004805 int err;
4806
Takashi Iwai1c70a582013-01-11 17:48:22 +01004807 parse_user_hints(codec);
4808
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004809 if (spec->mixer_nid && !spec->mixer_merge_nid)
4810 spec->mixer_merge_nid = spec->mixer_nid;
4811
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004812 if (cfg != &spec->autocfg) {
4813 spec->autocfg = *cfg;
4814 cfg = &spec->autocfg;
4815 }
4816
Takashi Iwai98bd1112013-03-22 14:53:50 +01004817 if (!spec->main_out_badness)
4818 spec->main_out_badness = &hda_main_out_badness;
4819 if (!spec->extra_out_badness)
4820 spec->extra_out_badness = &hda_extra_out_badness;
4821
David Henningsson6fc4cb92013-01-16 15:58:45 +01004822 fill_all_dac_nids(codec);
4823
Takashi Iwai352f7f92012-12-19 12:52:06 +01004824 if (!cfg->line_outs) {
4825 if (cfg->dig_outs || cfg->dig_in_pin) {
4826 spec->multiout.max_channels = 2;
4827 spec->no_analog = 1;
4828 goto dig_only;
4829 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004830 if (!cfg->num_inputs && !cfg->dig_in_pin)
4831 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004832 }
4833
4834 if (!spec->no_primary_hp &&
4835 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4836 cfg->line_outs <= cfg->hp_outs) {
4837 /* use HP as primary out */
4838 cfg->speaker_outs = cfg->line_outs;
4839 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4840 sizeof(cfg->speaker_pins));
4841 cfg->line_outs = cfg->hp_outs;
4842 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4843 cfg->hp_outs = 0;
4844 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4845 cfg->line_out_type = AUTO_PIN_HP_OUT;
4846 }
4847
4848 err = parse_output_paths(codec);
4849 if (err < 0)
4850 return err;
4851 err = create_multi_channel_mode(codec);
4852 if (err < 0)
4853 return err;
4854 err = create_multi_out_ctls(codec, cfg);
4855 if (err < 0)
4856 return err;
4857 err = create_hp_out_ctls(codec);
4858 if (err < 0)
4859 return err;
4860 err = create_speaker_out_ctls(codec);
4861 if (err < 0)
4862 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004863 err = create_indep_hp_ctls(codec);
4864 if (err < 0)
4865 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004866 err = create_loopback_mixing_ctl(codec);
4867 if (err < 0)
4868 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004869 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004870 if (err < 0)
4871 return err;
4872 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004873 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004874 return err;
4875
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004876 /* add power-down pin callbacks at first */
4877 add_all_pin_power_ctls(codec, false);
4878
Takashi Iwaia07a9492013-01-07 16:44:06 +01004879 spec->const_channel_count = spec->ext_channel_count;
4880 /* check the multiple speaker and headphone pins */
4881 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4882 spec->const_channel_count = max(spec->const_channel_count,
4883 cfg->speaker_outs * 2);
4884 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4885 spec->const_channel_count = max(spec->const_channel_count,
4886 cfg->hp_outs * 2);
4887 spec->multiout.max_channels = max(spec->ext_channel_count,
4888 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004889
4890 err = check_auto_mute_availability(codec);
4891 if (err < 0)
4892 return err;
4893
4894 err = check_dyn_adc_switch(codec);
4895 if (err < 0)
4896 return err;
4897
Takashi Iwai967303d2013-02-19 17:12:42 +01004898 err = check_auto_mic_availability(codec);
4899 if (err < 0)
4900 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004901
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004902 /* add stereo mix if available and not enabled yet */
4903 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01004904 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4905 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004906 err = parse_capture_source(codec, spec->mixer_nid,
4907 CFG_IDX_MIX, spec->num_all_adcs,
4908 "Stereo Mix", 0);
4909 if (err < 0)
4910 return err;
4911 }
4912
4913
Takashi Iwai352f7f92012-12-19 12:52:06 +01004914 err = create_capture_mixers(codec);
4915 if (err < 0)
4916 return err;
4917
4918 err = parse_mic_boost(codec);
4919 if (err < 0)
4920 return err;
4921
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004922 /* create "Headphone Mic Jack Mode" if no input selection is
4923 * available (or user specifies add_jack_modes hint)
4924 */
4925 if (spec->hp_mic_pin &&
4926 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4927 spec->add_jack_modes)) {
4928 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4929 if (err < 0)
4930 return err;
4931 }
4932
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004933 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004934 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4935 err = create_out_jack_modes(codec, cfg->line_outs,
4936 cfg->line_out_pins);
4937 if (err < 0)
4938 return err;
4939 }
4940 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4941 err = create_out_jack_modes(codec, cfg->hp_outs,
4942 cfg->hp_pins);
4943 if (err < 0)
4944 return err;
4945 }
4946 }
4947
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004948 /* add power-up pin callbacks at last */
4949 add_all_pin_power_ctls(codec, true);
4950
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004951 /* mute all aamix input initially */
4952 if (spec->mixer_nid)
4953 mute_all_mixer_nid(codec, spec->mixer_nid);
4954
Takashi Iwai352f7f92012-12-19 12:52:06 +01004955 dig_only:
4956 parse_digital(codec);
4957
Takashi Iwai49fb1892015-05-27 08:37:19 +02004958 if (spec->power_down_unused || codec->power_save_node) {
Takashi Iwai24fef902015-04-09 10:28:15 +02004959 if (!codec->power_filter)
4960 codec->power_filter = snd_hda_gen_path_power_filter;
Takashi Iwai49fb1892015-05-27 08:37:19 +02004961 if (!codec->patch_ops.stream_pm)
4962 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
4963 }
Takashi Iwai55196ff2013-01-24 17:32:56 +01004964
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004965 if (!spec->no_analog && spec->beep_nid) {
4966 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4967 if (err < 0)
4968 return err;
Takashi Iwai967b1302015-03-20 18:21:03 +01004969 if (codec->beep && codec->power_save_node) {
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004970 err = add_fake_beep_paths(codec);
4971 if (err < 0)
4972 return err;
4973 codec->beep->power_hook = beep_power_hook;
4974 }
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004975 }
4976
Takashi Iwai352f7f92012-12-19 12:52:06 +01004977 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004979EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
4981
4982/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004983 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004985
4986/* slave controls for virtual master */
4987static const char * const slave_pfxs[] = {
4988 "Front", "Surround", "Center", "LFE", "Side",
4989 "Headphone", "Speaker", "Mono", "Line Out",
4990 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004991 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4992 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02004993 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004994 NULL,
4995};
4996
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004997/**
4998 * snd_hda_gen_build_controls - Build controls from the parsed results
4999 * @codec: the HDA codec
5000 *
5001 * Pass this to build_controls patch_ops.
5002 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005003int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005005 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
Takashi Iwai36502d02012-12-19 15:15:10 +01005008 if (spec->kctls.used) {
5009 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5010 if (err < 0)
5011 return err;
5012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013
Takashi Iwai352f7f92012-12-19 12:52:06 +01005014 if (spec->multiout.dig_out_nid) {
5015 err = snd_hda_create_dig_out_ctls(codec,
5016 spec->multiout.dig_out_nid,
5017 spec->multiout.dig_out_nid,
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005018 spec->pcm_rec[1]->pcm_type);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005019 if (err < 0)
5020 return err;
5021 if (!spec->no_analog) {
5022 err = snd_hda_create_spdif_share_sw(codec,
5023 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 if (err < 0)
5025 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005026 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 }
5028 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005029 if (spec->dig_in_nid) {
5030 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5031 if (err < 0)
5032 return err;
5033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
Takashi Iwai352f7f92012-12-19 12:52:06 +01005035 /* if we have no master control, let's create it */
5036 if (!spec->no_analog &&
5037 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01005038 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01005039 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005040 "Playback Volume");
5041 if (err < 0)
5042 return err;
5043 }
5044 if (!spec->no_analog &&
5045 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5046 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5047 NULL, slave_pfxs,
5048 "Playback Switch",
5049 true, &spec->vmaster_mute.sw_kctl);
5050 if (err < 0)
5051 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02005052 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01005053 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5054 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02005055 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5056 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058
Takashi Iwai352f7f92012-12-19 12:52:06 +01005059 free_kctls(spec); /* no longer needed */
5060
Takashi Iwai352f7f92012-12-19 12:52:06 +01005061 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5062 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 return err;
5064
5065 return 0;
5066}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005067EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005068
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
5070/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005071 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005074static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5075 struct hda_codec *codec,
5076 struct snd_pcm_substream *substream,
5077 int action)
5078{
5079 struct hda_gen_spec *spec = codec->spec;
5080 if (spec->pcm_playback_hook)
5081 spec->pcm_playback_hook(hinfo, codec, substream, action);
5082}
5083
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005084static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5085 struct hda_codec *codec,
5086 struct snd_pcm_substream *substream,
5087 int action)
5088{
5089 struct hda_gen_spec *spec = codec->spec;
5090 if (spec->pcm_capture_hook)
5091 spec->pcm_capture_hook(hinfo, codec, substream, action);
5092}
5093
Takashi Iwai352f7f92012-12-19 12:52:06 +01005094/*
5095 * Analog playback callbacks
5096 */
5097static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5098 struct hda_codec *codec,
5099 struct snd_pcm_substream *substream)
5100{
5101 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005102 int err;
5103
5104 mutex_lock(&spec->pcm_mutex);
5105 err = snd_hda_multi_out_analog_open(codec,
5106 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005107 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005108 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005109 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005110 call_pcm_playback_hook(hinfo, codec, substream,
5111 HDA_GEN_PCM_ACT_OPEN);
5112 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005113 mutex_unlock(&spec->pcm_mutex);
5114 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005115}
5116
5117static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01005118 struct hda_codec *codec,
5119 unsigned int stream_tag,
5120 unsigned int format,
5121 struct snd_pcm_substream *substream)
5122{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005123 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005124 int err;
5125
5126 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5127 stream_tag, format, substream);
5128 if (!err)
5129 call_pcm_playback_hook(hinfo, codec, substream,
5130 HDA_GEN_PCM_ACT_PREPARE);
5131 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005132}
Takashi Iwai97ec5582006-03-21 11:29:07 +01005133
Takashi Iwai352f7f92012-12-19 12:52:06 +01005134static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5135 struct hda_codec *codec,
5136 struct snd_pcm_substream *substream)
5137{
5138 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005139 int err;
5140
5141 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5142 if (!err)
5143 call_pcm_playback_hook(hinfo, codec, substream,
5144 HDA_GEN_PCM_ACT_CLEANUP);
5145 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005146}
5147
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005148static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5149 struct hda_codec *codec,
5150 struct snd_pcm_substream *substream)
5151{
5152 struct hda_gen_spec *spec = codec->spec;
5153 mutex_lock(&spec->pcm_mutex);
5154 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005155 call_pcm_playback_hook(hinfo, codec, substream,
5156 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005157 mutex_unlock(&spec->pcm_mutex);
5158 return 0;
5159}
5160
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005161static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5162 struct hda_codec *codec,
5163 struct snd_pcm_substream *substream)
5164{
5165 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5166 return 0;
5167}
5168
5169static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5170 struct hda_codec *codec,
5171 unsigned int stream_tag,
5172 unsigned int format,
5173 struct snd_pcm_substream *substream)
5174{
5175 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5176 call_pcm_capture_hook(hinfo, codec, substream,
5177 HDA_GEN_PCM_ACT_PREPARE);
5178 return 0;
5179}
5180
5181static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5182 struct hda_codec *codec,
5183 struct snd_pcm_substream *substream)
5184{
5185 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5186 call_pcm_capture_hook(hinfo, codec, substream,
5187 HDA_GEN_PCM_ACT_CLEANUP);
5188 return 0;
5189}
5190
5191static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5192 struct hda_codec *codec,
5193 struct snd_pcm_substream *substream)
5194{
5195 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5196 return 0;
5197}
5198
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005199static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5200 struct hda_codec *codec,
5201 struct snd_pcm_substream *substream)
5202{
5203 struct hda_gen_spec *spec = codec->spec;
5204 int err = 0;
5205
5206 mutex_lock(&spec->pcm_mutex);
Takashi Iwaid1f15e02015-07-08 09:22:10 +02005207 if (spec->indep_hp && !spec->indep_hp_enabled)
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005208 err = -EBUSY;
5209 else
5210 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005211 call_pcm_playback_hook(hinfo, codec, substream,
5212 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005213 mutex_unlock(&spec->pcm_mutex);
5214 return err;
5215}
5216
5217static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5218 struct hda_codec *codec,
5219 struct snd_pcm_substream *substream)
5220{
5221 struct hda_gen_spec *spec = codec->spec;
5222 mutex_lock(&spec->pcm_mutex);
5223 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005224 call_pcm_playback_hook(hinfo, codec, substream,
5225 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005226 mutex_unlock(&spec->pcm_mutex);
5227 return 0;
5228}
5229
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005230static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5231 struct hda_codec *codec,
5232 unsigned int stream_tag,
5233 unsigned int format,
5234 struct snd_pcm_substream *substream)
5235{
5236 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5237 call_pcm_playback_hook(hinfo, codec, substream,
5238 HDA_GEN_PCM_ACT_PREPARE);
5239 return 0;
5240}
5241
5242static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5243 struct hda_codec *codec,
5244 struct snd_pcm_substream *substream)
5245{
5246 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5247 call_pcm_playback_hook(hinfo, codec, substream,
5248 HDA_GEN_PCM_ACT_CLEANUP);
5249 return 0;
5250}
5251
Takashi Iwai352f7f92012-12-19 12:52:06 +01005252/*
5253 * Digital out
5254 */
5255static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5256 struct hda_codec *codec,
5257 struct snd_pcm_substream *substream)
5258{
5259 struct hda_gen_spec *spec = codec->spec;
5260 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5261}
5262
5263static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5264 struct hda_codec *codec,
5265 unsigned int stream_tag,
5266 unsigned int format,
5267 struct snd_pcm_substream *substream)
5268{
5269 struct hda_gen_spec *spec = codec->spec;
5270 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5271 stream_tag, format, substream);
5272}
5273
5274static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5275 struct hda_codec *codec,
5276 struct snd_pcm_substream *substream)
5277{
5278 struct hda_gen_spec *spec = codec->spec;
5279 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5280}
5281
5282static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5283 struct hda_codec *codec,
5284 struct snd_pcm_substream *substream)
5285{
5286 struct hda_gen_spec *spec = codec->spec;
5287 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5288}
5289
5290/*
5291 * Analog capture
5292 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005293#define alt_capture_pcm_open capture_pcm_open
5294#define alt_capture_pcm_close capture_pcm_close
5295
Takashi Iwai352f7f92012-12-19 12:52:06 +01005296static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5297 struct hda_codec *codec,
5298 unsigned int stream_tag,
5299 unsigned int format,
5300 struct snd_pcm_substream *substream)
5301{
5302 struct hda_gen_spec *spec = codec->spec;
5303
5304 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01005305 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005306 call_pcm_capture_hook(hinfo, codec, substream,
5307 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005308 return 0;
5309}
5310
Takashi Iwai352f7f92012-12-19 12:52:06 +01005311static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5312 struct hda_codec *codec,
5313 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01005314{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005315 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01005316
Takashi Iwai352f7f92012-12-19 12:52:06 +01005317 snd_hda_codec_cleanup_stream(codec,
5318 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005319 call_pcm_capture_hook(hinfo, codec, substream,
5320 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005321 return 0;
5322}
5323
Takashi Iwai352f7f92012-12-19 12:52:06 +01005324/*
5325 */
5326static const struct hda_pcm_stream pcm_analog_playback = {
5327 .substreams = 1,
5328 .channels_min = 2,
5329 .channels_max = 8,
5330 /* NID is set in build_pcms */
5331 .ops = {
5332 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005333 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005334 .prepare = playback_pcm_prepare,
5335 .cleanup = playback_pcm_cleanup
5336 },
5337};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
Takashi Iwai352f7f92012-12-19 12:52:06 +01005339static const struct hda_pcm_stream pcm_analog_capture = {
5340 .substreams = 1,
5341 .channels_min = 2,
5342 .channels_max = 2,
5343 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005344 .ops = {
5345 .open = capture_pcm_open,
5346 .close = capture_pcm_close,
5347 .prepare = capture_pcm_prepare,
5348 .cleanup = capture_pcm_cleanup
5349 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005350};
5351
5352static const struct hda_pcm_stream pcm_analog_alt_playback = {
5353 .substreams = 1,
5354 .channels_min = 2,
5355 .channels_max = 2,
5356 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005357 .ops = {
5358 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005359 .close = alt_playback_pcm_close,
5360 .prepare = alt_playback_pcm_prepare,
5361 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005362 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005363};
5364
5365static const struct hda_pcm_stream pcm_analog_alt_capture = {
5366 .substreams = 2, /* can be overridden */
5367 .channels_min = 2,
5368 .channels_max = 2,
5369 /* NID is set in build_pcms */
5370 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005371 .open = alt_capture_pcm_open,
5372 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005373 .prepare = alt_capture_pcm_prepare,
5374 .cleanup = alt_capture_pcm_cleanup
5375 },
5376};
5377
5378static const struct hda_pcm_stream pcm_digital_playback = {
5379 .substreams = 1,
5380 .channels_min = 2,
5381 .channels_max = 2,
5382 /* NID is set in build_pcms */
5383 .ops = {
5384 .open = dig_playback_pcm_open,
5385 .close = dig_playback_pcm_close,
5386 .prepare = dig_playback_pcm_prepare,
5387 .cleanup = dig_playback_pcm_cleanup
5388 },
5389};
5390
5391static const struct hda_pcm_stream pcm_digital_capture = {
5392 .substreams = 1,
5393 .channels_min = 2,
5394 .channels_max = 2,
5395 /* NID is set in build_pcms */
5396};
5397
5398/* Used by build_pcms to flag that a PCM has no playback stream */
5399static const struct hda_pcm_stream pcm_null_stream = {
5400 .substreams = 0,
5401 .channels_min = 0,
5402 .channels_max = 0,
5403};
5404
5405/*
5406 * dynamic changing ADC PCM streams
5407 */
5408static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5409{
5410 struct hda_gen_spec *spec = codec->spec;
5411 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5412
5413 if (spec->cur_adc && spec->cur_adc != new_adc) {
5414 /* stream is running, let's swap the current ADC */
5415 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5416 spec->cur_adc = new_adc;
5417 snd_hda_codec_setup_stream(codec, new_adc,
5418 spec->cur_adc_stream_tag, 0,
5419 spec->cur_adc_format);
5420 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005422 return false;
5423}
5424
5425/* analog capture with dynamic dual-adc changes */
5426static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5427 struct hda_codec *codec,
5428 unsigned int stream_tag,
5429 unsigned int format,
5430 struct snd_pcm_substream *substream)
5431{
5432 struct hda_gen_spec *spec = codec->spec;
5433 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5434 spec->cur_adc_stream_tag = stream_tag;
5435 spec->cur_adc_format = format;
5436 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
Takashi Iwai4f29efc2016-04-12 15:16:24 +02005437 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005438 return 0;
5439}
5440
5441static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5442 struct hda_codec *codec,
5443 struct snd_pcm_substream *substream)
5444{
5445 struct hda_gen_spec *spec = codec->spec;
5446 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5447 spec->cur_adc = 0;
Takashi Iwai4f29efc2016-04-12 15:16:24 +02005448 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005449 return 0;
5450}
5451
5452static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5453 .substreams = 1,
5454 .channels_min = 2,
5455 .channels_max = 2,
5456 .nid = 0, /* fill later */
5457 .ops = {
5458 .prepare = dyn_adc_capture_pcm_prepare,
5459 .cleanup = dyn_adc_capture_pcm_cleanup
5460 },
5461};
5462
Takashi Iwaif873e532012-12-20 16:58:39 +01005463static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5464 const char *chip_name)
5465{
5466 char *p;
5467
5468 if (*str)
5469 return;
5470 strlcpy(str, chip_name, len);
5471
5472 /* drop non-alnum chars after a space */
5473 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5474 if (!isalnum(p[1])) {
5475 *p = 0;
5476 break;
5477 }
5478 }
5479 strlcat(str, sfx, len);
5480}
5481
Takashi Iwaifb83b632015-03-16 23:34:34 +01005482/* copy PCM stream info from @default_str, and override non-NULL entries
5483 * from @spec_str and @nid
5484 */
5485static void setup_pcm_stream(struct hda_pcm_stream *str,
5486 const struct hda_pcm_stream *default_str,
5487 const struct hda_pcm_stream *spec_str,
5488 hda_nid_t nid)
5489{
5490 *str = *default_str;
5491 if (nid)
5492 str->nid = nid;
5493 if (spec_str) {
5494 if (spec_str->substreams)
5495 str->substreams = spec_str->substreams;
5496 if (spec_str->channels_min)
5497 str->channels_min = spec_str->channels_min;
5498 if (spec_str->channels_max)
5499 str->channels_max = spec_str->channels_max;
5500 if (spec_str->rates)
5501 str->rates = spec_str->rates;
5502 if (spec_str->formats)
5503 str->formats = spec_str->formats;
5504 if (spec_str->maxbps)
5505 str->maxbps = spec_str->maxbps;
5506 }
5507}
5508
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005509/**
5510 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5511 * @codec: the HDA codec
5512 *
5513 * Pass this to build_pcms patch_ops.
5514 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005515int snd_hda_gen_build_pcms(struct hda_codec *codec)
5516{
5517 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005518 struct hda_pcm *info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005519 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
Takashi Iwai352f7f92012-12-19 12:52:06 +01005521 if (spec->no_analog)
5522 goto skip_analog;
5523
Takashi Iwaif873e532012-12-20 16:58:39 +01005524 fill_pcm_stream_name(spec->stream_name_analog,
5525 sizeof(spec->stream_name_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005526 " Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005527 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5528 if (!info)
5529 return -ENOMEM;
5530 spec->pcm_rec[0] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005531
5532 if (spec->multiout.num_dacs > 0) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005533 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5534 &pcm_analog_playback,
5535 spec->stream_analog_playback,
5536 spec->multiout.dac_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005537 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5538 spec->multiout.max_channels;
5539 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5540 spec->autocfg.line_outs == 2)
5541 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5542 snd_pcm_2_1_chmaps;
5543 }
5544 if (spec->num_adc_nids) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005545 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5546 (spec->dyn_adc_switch ?
5547 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5548 spec->stream_analog_capture,
5549 spec->adc_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005550 }
5551
Takashi Iwai352f7f92012-12-19 12:52:06 +01005552 skip_analog:
5553 /* SPDIF for stream index #1 */
5554 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005555 fill_pcm_stream_name(spec->stream_name_digital,
5556 sizeof(spec->stream_name_digital),
Takashi Iwai7639a062015-03-03 10:07:24 +01005557 " Digital", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005558 info = snd_hda_codec_pcm_new(codec, "%s",
5559 spec->stream_name_digital);
5560 if (!info)
5561 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005562 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005563 spec->pcm_rec[1] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005564 if (spec->dig_out_type)
5565 info->pcm_type = spec->dig_out_type;
5566 else
5567 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005568 if (spec->multiout.dig_out_nid)
5569 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5570 &pcm_digital_playback,
5571 spec->stream_digital_playback,
5572 spec->multiout.dig_out_nid);
5573 if (spec->dig_in_nid)
5574 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5575 &pcm_digital_capture,
5576 spec->stream_digital_capture,
5577 spec->dig_in_nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005578 }
5579
5580 if (spec->no_analog)
5581 return 0;
5582
5583 /* If the use of more than one ADC is requested for the current
5584 * model, configure a second analog capture-only PCM.
5585 */
5586 have_multi_adcs = (spec->num_adc_nids > 1) &&
5587 !spec->dyn_adc_switch && !spec->auto_mic;
5588 /* Additional Analaog capture for index #2 */
5589 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005590 fill_pcm_stream_name(spec->stream_name_alt_analog,
5591 sizeof(spec->stream_name_alt_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005592 " Alt Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005593 info = snd_hda_codec_pcm_new(codec, "%s",
5594 spec->stream_name_alt_analog);
5595 if (!info)
5596 return -ENOMEM;
5597 spec->pcm_rec[2] = info;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005598 if (spec->alt_dac_nid)
5599 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5600 &pcm_analog_alt_playback,
5601 spec->stream_analog_alt_playback,
5602 spec->alt_dac_nid);
5603 else
5604 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5605 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005606 if (have_multi_adcs) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005607 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5608 &pcm_analog_alt_capture,
5609 spec->stream_analog_alt_capture,
5610 spec->adc_nids[1]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005611 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5612 spec->num_adc_nids - 1;
5613 } else {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005614 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5615 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 }
5618
5619 return 0;
5620}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005621EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005622
5623
5624/*
5625 * Standard auto-parser initializations
5626 */
5627
Takashi Iwaid4156932013-01-07 10:08:02 +01005628/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005629static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005630{
5631 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005632 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005633
Takashi Iwai196c17662013-01-04 15:01:40 +01005634 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005635 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005636 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005637 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005638 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005639 snd_hda_activate_path(codec, path, path->active,
5640 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005641 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005642}
5643
5644/* initialize primary output paths */
5645static void init_multi_out(struct hda_codec *codec)
5646{
5647 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005648 int i;
5649
Takashi Iwaid4156932013-01-07 10:08:02 +01005650 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005651 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005652}
5653
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005654
Takashi Iwai2c12c302013-01-10 09:33:29 +01005655static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005656{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005657 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005658
Takashi Iwaid4156932013-01-07 10:08:02 +01005659 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005660 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005661}
5662
5663/* initialize hp and speaker paths */
5664static void init_extra_out(struct hda_codec *codec)
5665{
5666 struct hda_gen_spec *spec = codec->spec;
5667
5668 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005669 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005670 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5671 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005672 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005673}
5674
5675/* initialize multi-io paths */
5676static void init_multi_io(struct hda_codec *codec)
5677{
5678 struct hda_gen_spec *spec = codec->spec;
5679 int i;
5680
5681 for (i = 0; i < spec->multi_ios; i++) {
5682 hda_nid_t pin = spec->multi_io[i].pin;
5683 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005684 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005685 if (!path)
5686 continue;
5687 if (!spec->multi_io[i].ctl_in)
5688 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005689 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005690 snd_hda_activate_path(codec, path, path->active,
5691 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005692 }
5693}
5694
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005695static void init_aamix_paths(struct hda_codec *codec)
5696{
5697 struct hda_gen_spec *spec = codec->spec;
5698
5699 if (!spec->have_aamix_ctl)
5700 return;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01005701 if (!has_aamix_out_paths(spec))
5702 return;
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005703 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5704 spec->aamix_out_paths[0],
5705 spec->autocfg.line_out_type);
5706 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5707 spec->aamix_out_paths[1],
5708 AUTO_PIN_HP_OUT);
5709 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5710 spec->aamix_out_paths[2],
5711 AUTO_PIN_SPEAKER_OUT);
5712}
5713
Takashi Iwai352f7f92012-12-19 12:52:06 +01005714/* set up input pins and loopback paths */
5715static void init_analog_input(struct hda_codec *codec)
5716{
5717 struct hda_gen_spec *spec = codec->spec;
5718 struct auto_pin_cfg *cfg = &spec->autocfg;
5719 int i;
5720
5721 for (i = 0; i < cfg->num_inputs; i++) {
5722 hda_nid_t nid = cfg->inputs[i].pin;
5723 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005724 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005725
5726 /* init loopback inputs */
5727 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005728 resume_path_from_idx(codec, spec->loopback_paths[i]);
5729 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005730 }
5731 }
5732}
5733
5734/* initialize ADC paths */
5735static void init_input_src(struct hda_codec *codec)
5736{
5737 struct hda_gen_spec *spec = codec->spec;
5738 struct hda_input_mux *imux = &spec->input_mux;
5739 struct nid_path *path;
5740 int i, c, nums;
5741
5742 if (spec->dyn_adc_switch)
5743 nums = 1;
5744 else
5745 nums = spec->num_adc_nids;
5746
5747 for (c = 0; c < nums; c++) {
5748 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005749 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005750 if (path) {
5751 bool active = path->active;
5752 if (i == spec->cur_mux[c])
5753 active = true;
5754 snd_hda_activate_path(codec, path, active, false);
5755 }
5756 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005757 if (spec->hp_mic)
5758 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005759 }
5760
Takashi Iwai352f7f92012-12-19 12:52:06 +01005761 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005762 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005763}
5764
5765/* set right pin controls for digital I/O */
5766static void init_digital(struct hda_codec *codec)
5767{
5768 struct hda_gen_spec *spec = codec->spec;
5769 int i;
5770 hda_nid_t pin;
5771
Takashi Iwaid4156932013-01-07 10:08:02 +01005772 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005773 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005774 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005775 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005776 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005777 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005778 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005779}
5780
Takashi Iwai973e4972012-12-20 15:16:09 +01005781/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5782 * invalid unsol tags by some reason
5783 */
5784static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5785{
5786 int i;
5787
5788 for (i = 0; i < codec->init_pins.used; i++) {
5789 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5790 hda_nid_t nid = pin->nid;
5791 if (is_jack_detectable(codec, nid) &&
5792 !snd_hda_jack_tbl_get(codec, nid))
5793 snd_hda_codec_update_cache(codec, nid, 0,
5794 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5795 }
5796}
5797
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005798/**
5799 * snd_hda_gen_init - initialize the generic spec
5800 * @codec: the HDA codec
5801 *
5802 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005803 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005804int snd_hda_gen_init(struct hda_codec *codec)
5805{
5806 struct hda_gen_spec *spec = codec->spec;
5807
5808 if (spec->init_hook)
5809 spec->init_hook(codec);
5810
5811 snd_hda_apply_verbs(codec);
5812
5813 init_multi_out(codec);
5814 init_extra_out(codec);
5815 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005816 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005817 init_analog_input(codec);
5818 init_input_src(codec);
5819 init_digital(codec);
5820
Takashi Iwai973e4972012-12-20 15:16:09 +01005821 clear_unsol_on_unused_pins(codec);
5822
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005823 sync_all_pin_power_ctls(codec);
5824
Takashi Iwai352f7f92012-12-19 12:52:06 +01005825 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005826 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005827
Takashi Iwaia551d912015-02-26 12:34:49 +01005828 regcache_sync(codec->core.regmap);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005829
Takashi Iwai352f7f92012-12-19 12:52:06 +01005830 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5831 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5832
5833 hda_call_check_power_status(codec, 0x01);
5834 return 0;
5835}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005836EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005837
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005838/**
5839 * snd_hda_gen_free - free the generic spec
5840 * @codec: the HDA codec
5841 *
5842 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005843 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005844void snd_hda_gen_free(struct hda_codec *codec)
5845{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005846 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005847 snd_hda_gen_spec_free(codec->spec);
5848 kfree(codec->spec);
5849 codec->spec = NULL;
5850}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005851EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005852
5853#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005854/**
5855 * snd_hda_gen_check_power_status - check the loopback power save state
5856 * @codec: the HDA codec
5857 * @nid: NID to inspect
5858 *
5859 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005860 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005861int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5862{
5863 struct hda_gen_spec *spec = codec->spec;
5864 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5865}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005866EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005867#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005868
5869
5870/*
5871 * the generic codec support
5872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
Takashi Iwai352f7f92012-12-19 12:52:06 +01005874static const struct hda_codec_ops generic_patch_ops = {
5875 .build_controls = snd_hda_gen_build_controls,
5876 .build_pcms = snd_hda_gen_build_pcms,
5877 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005878 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005879 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005880#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005881 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005882#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883};
5884
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005885/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005886 * snd_hda_parse_generic_codec - Generic codec parser
5887 * @codec: the HDA codec
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005888 */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005889static int snd_hda_parse_generic_codec(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005891 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 int err;
5893
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005894 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005895 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005897 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005900 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5901 if (err < 0)
5902 return err;
5903
5904 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005905 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 goto error;
5907
5908 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 return 0;
5910
Takashi Iwai352f7f92012-12-19 12:52:06 +01005911error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005912 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 return err;
5914}
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005915
Takashi Iwaib9a94a92015-10-01 16:20:04 +02005916static const struct hda_device_id snd_hda_id_generic[] = {
5917 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005918 {} /* terminator */
5919};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02005920MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005921
5922static struct hda_codec_driver generic_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02005923 .id = snd_hda_id_generic,
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005924};
5925
5926module_hda_codec_driver(generic_driver);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005927
5928MODULE_LICENSE("GPL");
5929MODULE_DESCRIPTION("Generic HD-audio codec parser");