blob: 79c7b340acc2361518b51504b7ecb032a71991b2 [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 Iwai247d85e2013-01-17 16:18:11 +01001128 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001129 return spec->vmaster_mute.hook ? "PCM" : "Master";
1130
1131 /* if there is really a single DAC used in the whole output paths,
1132 * use it master (or "PCM" if a vmaster hook is present)
1133 */
1134 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1135 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1136 return spec->vmaster_mute.hook ? "PCM" : "Master";
1137
Takashi Iwai247d85e2013-01-17 16:18:11 +01001138 /* multi-io channels */
1139 if (ch >= cfg->line_outs)
1140 return channel_name[ch];
1141
Takashi Iwai352f7f92012-12-19 12:52:06 +01001142 switch (cfg->line_out_type) {
1143 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001144 /* if the primary channel vol/mute is shared with HP volume,
1145 * don't name it as Speaker
1146 */
1147 if (!ch && cfg->hp_outs &&
1148 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1149 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001150 if (cfg->line_outs == 1)
1151 return "Speaker";
1152 if (cfg->line_outs == 2)
1153 return ch ? "Bass Speaker" : "Speaker";
1154 break;
1155 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001156 /* if the primary channel vol/mute is shared with spk volume,
1157 * don't name it as Headphone
1158 */
1159 if (!ch && cfg->speaker_outs &&
1160 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1161 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001162 /* for multi-io case, only the primary out */
1163 if (ch && spec->multi_ios)
1164 break;
1165 *index = ch;
1166 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001167 case AUTO_PIN_LINE_OUT:
1168 /* This deals with the case where we have two DACs and
1169 * one LO, one HP and one Speaker */
1170 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1171 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1172 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1173 if (hp_lo_shared && spk_lo_shared)
1174 return spec->vmaster_mute.hook ? "PCM" : "Master";
1175 if (hp_lo_shared)
1176 return "Headphone+LO";
1177 if (spk_lo_shared)
1178 return "Speaker+LO";
1179 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001180 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001181
1182 /* for a single channel output, we don't have to name the channel */
1183 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001184 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001185
Takashi Iwai352f7f92012-12-19 12:52:06 +01001186 if (ch >= ARRAY_SIZE(channel_name)) {
1187 snd_BUG();
1188 return "PCM";
1189 }
1190
1191 return channel_name[ch];
1192}
1193
1194/*
1195 * Parse output paths
1196 */
1197
1198/* badness definition */
1199enum {
1200 /* No primary DAC is found for the main output */
1201 BAD_NO_PRIMARY_DAC = 0x10000,
1202 /* No DAC is found for the extra output */
1203 BAD_NO_DAC = 0x4000,
1204 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001205 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001206 /* No individual DAC for extra output */
1207 BAD_NO_EXTRA_DAC = 0x102,
1208 /* No individual DAC for extra surrounds */
1209 BAD_NO_EXTRA_SURR_DAC = 0x101,
1210 /* Primary DAC shared with main surrounds */
1211 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001212 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001213 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001214 /* Primary DAC shared with main CLFE */
1215 BAD_SHARED_CLFE = 0x10,
1216 /* Primary DAC shared with extra surrounds */
1217 BAD_SHARED_EXTRA_SURROUND = 0x10,
1218 /* Volume widget is shared */
1219 BAD_SHARED_VOL = 0x10,
1220};
1221
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001222/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001223 * volume and mute controls, and assign the values to ctls[].
1224 *
1225 * When no appropriate widget is found in the path, the badness value
1226 * is incremented depending on the situation. The function returns the
1227 * total badness for both volume and mute controls.
1228 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001229static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001230{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001231 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001232 hda_nid_t nid;
1233 unsigned int val;
1234 int badness = 0;
1235
1236 if (!path)
1237 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001238
1239 if (path->ctls[NID_PATH_VOL_CTL] ||
1240 path->ctls[NID_PATH_MUTE_CTL])
1241 return 0; /* already evaluated */
1242
Takashi Iwai352f7f92012-12-19 12:52:06 +01001243 nid = look_for_out_vol_nid(codec, path);
1244 if (nid) {
1245 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001246 if (spec->dac_min_mute)
1247 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001248 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1249 badness += BAD_SHARED_VOL;
1250 else
1251 path->ctls[NID_PATH_VOL_CTL] = val;
1252 } else
1253 badness += BAD_SHARED_VOL;
1254 nid = look_for_out_mute_nid(codec, path);
1255 if (nid) {
1256 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1257 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1258 nid_has_mute(codec, nid, HDA_OUTPUT))
1259 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1260 else
1261 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1262 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1263 badness += BAD_SHARED_VOL;
1264 else
1265 path->ctls[NID_PATH_MUTE_CTL] = val;
1266 } else
1267 badness += BAD_SHARED_VOL;
1268 return badness;
1269}
1270
Takashi Iwai98bd1112013-03-22 14:53:50 +01001271const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001272 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1273 .no_dac = BAD_NO_DAC,
1274 .shared_primary = BAD_NO_PRIMARY_DAC,
1275 .shared_surr = BAD_SHARED_SURROUND,
1276 .shared_clfe = BAD_SHARED_CLFE,
1277 .shared_surr_main = BAD_SHARED_SURROUND,
1278};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001279EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001280
Takashi Iwai98bd1112013-03-22 14:53:50 +01001281const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001282 .no_primary_dac = BAD_NO_DAC,
1283 .no_dac = BAD_NO_DAC,
1284 .shared_primary = BAD_NO_EXTRA_DAC,
1285 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1286 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1287 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1288};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001289EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001290
Takashi Iwai7385df62013-01-07 09:50:52 +01001291/* get the DAC of the primary output corresponding to the given array index */
1292static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1293{
1294 struct hda_gen_spec *spec = codec->spec;
1295 struct auto_pin_cfg *cfg = &spec->autocfg;
1296
1297 if (cfg->line_outs > idx)
1298 return spec->private_dac_nids[idx];
1299 idx -= cfg->line_outs;
1300 if (spec->multi_ios > idx)
1301 return spec->multi_io[idx].dac;
1302 return 0;
1303}
1304
1305/* return the DAC if it's reachable, otherwise zero */
1306static inline hda_nid_t try_dac(struct hda_codec *codec,
1307 hda_nid_t dac, hda_nid_t pin)
1308{
1309 return is_reachable_path(codec, dac, pin) ? dac : 0;
1310}
1311
Takashi Iwai352f7f92012-12-19 12:52:06 +01001312/* try to assign DACs to pins and return the resultant badness */
1313static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1314 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001315 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001316 const struct badness_table *bad)
1317{
1318 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001319 int i, j;
1320 int badness = 0;
1321 hda_nid_t dac;
1322
1323 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return 0;
1325
Takashi Iwai352f7f92012-12-19 12:52:06 +01001326 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001327 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001328 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001329
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001330 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1331 if (path) {
1332 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001333 continue;
1334 }
1335
Takashi Iwai36907392013-12-10 17:29:26 +01001336 dacs[i] = get_preferred_dac(codec, pin);
1337 if (dacs[i]) {
1338 if (is_dac_already_used(codec, dacs[i]))
1339 badness += bad->shared_primary;
1340 }
1341
1342 if (!dacs[i])
1343 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001344 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001345 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001346 for (j = 1; j < num_outs; j++) {
1347 if (is_reachable_path(codec, dacs[j], pin)) {
1348 dacs[0] = dacs[j];
1349 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001350 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001351 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001355 }
1356 dac = dacs[i];
1357 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001358 if (num_outs > 2)
1359 dac = try_dac(codec, get_primary_out(codec, i), pin);
1360 if (!dac)
1361 dac = try_dac(codec, dacs[0], pin);
1362 if (!dac)
1363 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001364 if (dac) {
1365 if (!i)
1366 badness += bad->shared_primary;
1367 else if (i == 1)
1368 badness += bad->shared_surr;
1369 else
1370 badness += bad->shared_clfe;
1371 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1372 dac = spec->private_dac_nids[0];
1373 badness += bad->shared_surr_main;
1374 } else if (!i)
1375 badness += bad->no_primary_dac;
1376 else
1377 badness += bad->no_dac;
1378 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001379 if (!dac)
1380 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001381 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001382 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001383 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001384 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001385 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001386 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001387 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001388 badness += bad->no_dac;
1389 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001390 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001391 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001392 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001393 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001394 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001395 }
1396
1397 return badness;
1398}
1399
1400/* return NID if the given pin has only a single connection to a certain DAC */
1401static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1402{
1403 struct hda_gen_spec *spec = codec->spec;
1404 int i;
1405 hda_nid_t nid_found = 0;
1406
1407 for (i = 0; i < spec->num_all_dacs; i++) {
1408 hda_nid_t nid = spec->all_dacs[i];
1409 if (!nid || is_dac_already_used(codec, nid))
1410 continue;
1411 if (is_reachable_path(codec, nid, pin)) {
1412 if (nid_found)
1413 return 0;
1414 nid_found = nid;
1415 }
1416 }
1417 return nid_found;
1418}
1419
1420/* check whether the given pin can be a multi-io pin */
1421static bool can_be_multiio_pin(struct hda_codec *codec,
1422 unsigned int location, hda_nid_t nid)
1423{
1424 unsigned int defcfg, caps;
1425
1426 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1427 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1428 return false;
1429 if (location && get_defcfg_location(defcfg) != location)
1430 return false;
1431 caps = snd_hda_query_pin_caps(codec, nid);
1432 if (!(caps & AC_PINCAP_OUT))
1433 return false;
1434 return true;
1435}
1436
Takashi Iwaie22aab72013-01-04 14:50:04 +01001437/* count the number of input pins that are capable to be multi-io */
1438static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1439{
1440 struct hda_gen_spec *spec = codec->spec;
1441 struct auto_pin_cfg *cfg = &spec->autocfg;
1442 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1443 unsigned int location = get_defcfg_location(defcfg);
1444 int type, i;
1445 int num_pins = 0;
1446
1447 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1448 for (i = 0; i < cfg->num_inputs; i++) {
1449 if (cfg->inputs[i].type != type)
1450 continue;
1451 if (can_be_multiio_pin(codec, location,
1452 cfg->inputs[i].pin))
1453 num_pins++;
1454 }
1455 }
1456 return num_pins;
1457}
1458
Takashi Iwai352f7f92012-12-19 12:52:06 +01001459/*
1460 * multi-io helper
1461 *
1462 * When hardwired is set, try to fill ony hardwired pins, and returns
1463 * zero if any pins are filled, non-zero if nothing found.
1464 * When hardwired is off, try to fill possible input pins, and returns
1465 * the badness value.
1466 */
1467static int fill_multi_ios(struct hda_codec *codec,
1468 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001469 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001470{
1471 struct hda_gen_spec *spec = codec->spec;
1472 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001473 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001474 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1475 unsigned int location = get_defcfg_location(defcfg);
1476 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001477 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001478
1479 old_pins = spec->multi_ios;
1480 if (old_pins >= 2)
1481 goto end_fill;
1482
Takashi Iwaie22aab72013-01-04 14:50:04 +01001483 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001484 if (num_pins < 2)
1485 goto end_fill;
1486
Takashi Iwai352f7f92012-12-19 12:52:06 +01001487 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1488 for (i = 0; i < cfg->num_inputs; i++) {
1489 hda_nid_t nid = cfg->inputs[i].pin;
1490 hda_nid_t dac = 0;
1491
1492 if (cfg->inputs[i].type != type)
1493 continue;
1494 if (!can_be_multiio_pin(codec, location, nid))
1495 continue;
1496 for (j = 0; j < spec->multi_ios; j++) {
1497 if (nid == spec->multi_io[j].pin)
1498 break;
1499 }
1500 if (j < spec->multi_ios)
1501 continue;
1502
Takashi Iwai352f7f92012-12-19 12:52:06 +01001503 if (hardwired)
1504 dac = get_dac_if_single(codec, nid);
1505 else if (!dac)
1506 dac = look_for_dac(codec, nid, false);
1507 if (!dac) {
1508 badness++;
1509 continue;
1510 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001511 path = snd_hda_add_new_path(codec, dac, nid,
1512 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001513 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001514 badness++;
1515 continue;
1516 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001517 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001518 spec->multi_io[spec->multi_ios].pin = nid;
1519 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001520 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1521 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001522 spec->multi_ios++;
1523 if (spec->multi_ios >= 2)
1524 break;
1525 }
1526 }
1527 end_fill:
1528 if (badness)
1529 badness = BAD_MULTI_IO;
1530 if (old_pins == spec->multi_ios) {
1531 if (hardwired)
1532 return 1; /* nothing found */
1533 else
1534 return badness; /* no badness if nothing found */
1535 }
1536 if (!hardwired && spec->multi_ios < 2) {
1537 /* cancel newly assigned paths */
1538 spec->paths.used -= spec->multi_ios - old_pins;
1539 spec->multi_ios = old_pins;
1540 return badness;
1541 }
1542
1543 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001544 for (i = old_pins; i < spec->multi_ios; i++) {
1545 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1546 badness += assign_out_path_ctls(codec, path);
1547 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001548
1549 return badness;
1550}
1551
1552/* map DACs for all pins in the list if they are single connections */
1553static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001554 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001555{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001556 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001557 int i;
1558 bool found = false;
1559 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001560 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001561 hda_nid_t dac;
1562 if (dacs[i])
1563 continue;
1564 dac = get_dac_if_single(codec, pins[i]);
1565 if (!dac)
1566 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001567 path = snd_hda_add_new_path(codec, dac, pins[i],
1568 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001569 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001570 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001571 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001572 dacs[i] = dac;
1573 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001574 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001575 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001576 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001577 }
1578 }
1579 return found;
1580}
1581
Takashi Iwaie7fdd522015-12-08 17:00:42 +01001582static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1583{
1584 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1585 spec->aamix_out_paths[2];
1586}
1587
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001588/* create a new path including aamix if available, and return its index */
1589static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1590{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001591 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001592 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001593 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001594
1595 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001596 if (!path || !path->depth ||
1597 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001598 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001599 path_dac = path->path[0];
1600 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001601 pin = path->path[path->depth - 1];
1602 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1603 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001604 if (dac != path_dac)
1605 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001606 else if (spec->multiout.hp_out_nid[0])
1607 dac = spec->multiout.hp_out_nid[0];
1608 else if (spec->multiout.extra_out_nid[0])
1609 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001610 else
1611 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001612 if (dac)
1613 path = snd_hda_add_new_path(codec, dac, pin,
1614 spec->mixer_nid);
1615 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001616 if (!path)
1617 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001618 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001619 path->active = false; /* unused as default */
Takashi Iwai6b275b12015-03-20 18:11:05 +01001620 path->pin_fixed = true; /* static route */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001621 return snd_hda_get_path_idx(codec, path);
1622}
1623
Takashi Iwai55a63d42013-03-21 17:20:12 +01001624/* check whether the independent HP is available with the current config */
1625static bool indep_hp_possible(struct hda_codec *codec)
1626{
1627 struct hda_gen_spec *spec = codec->spec;
1628 struct auto_pin_cfg *cfg = &spec->autocfg;
1629 struct nid_path *path;
1630 int i, idx;
1631
1632 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1633 idx = spec->out_paths[0];
1634 else
1635 idx = spec->hp_paths[0];
1636 path = snd_hda_get_path_from_idx(codec, idx);
1637 if (!path)
1638 return false;
1639
1640 /* assume no path conflicts unless aamix is involved */
1641 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1642 return true;
1643
1644 /* check whether output paths contain aamix */
1645 for (i = 0; i < cfg->line_outs; i++) {
1646 if (spec->out_paths[i] == idx)
1647 break;
1648 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1649 if (path && is_nid_contained(path, spec->mixer_nid))
1650 return false;
1651 }
1652 for (i = 0; i < cfg->speaker_outs; i++) {
1653 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1654 if (path && is_nid_contained(path, spec->mixer_nid))
1655 return false;
1656 }
1657
1658 return true;
1659}
1660
Takashi Iwaia07a9492013-01-07 16:44:06 +01001661/* fill the empty entries in the dac array for speaker/hp with the
1662 * shared dac pointed by the paths
1663 */
1664static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1665 hda_nid_t *dacs, int *path_idx)
1666{
1667 struct nid_path *path;
1668 int i;
1669
1670 for (i = 0; i < num_outs; i++) {
1671 if (dacs[i])
1672 continue;
1673 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1674 if (!path)
1675 continue;
1676 dacs[i] = path->path[0];
1677 }
1678}
1679
Takashi Iwai352f7f92012-12-19 12:52:06 +01001680/* fill in the dac_nids table from the parsed pin configuration */
1681static int fill_and_eval_dacs(struct hda_codec *codec,
1682 bool fill_hardwired,
1683 bool fill_mio_first)
1684{
1685 struct hda_gen_spec *spec = codec->spec;
1686 struct auto_pin_cfg *cfg = &spec->autocfg;
1687 int i, err, badness;
1688
1689 /* set num_dacs once to full for look_for_dac() */
1690 spec->multiout.num_dacs = cfg->line_outs;
1691 spec->multiout.dac_nids = spec->private_dac_nids;
1692 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1693 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1694 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1695 spec->multi_ios = 0;
1696 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001697
1698 /* clear path indices */
1699 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1700 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1701 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1702 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1703 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001704 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001705 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1706 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1707
Takashi Iwai352f7f92012-12-19 12:52:06 +01001708 badness = 0;
1709
1710 /* fill hard-wired DACs first */
1711 if (fill_hardwired) {
1712 bool mapped;
1713 do {
1714 mapped = map_singles(codec, cfg->line_outs,
1715 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001716 spec->private_dac_nids,
1717 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001718 mapped |= map_singles(codec, cfg->hp_outs,
1719 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001720 spec->multiout.hp_out_nid,
1721 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001722 mapped |= map_singles(codec, cfg->speaker_outs,
1723 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001724 spec->multiout.extra_out_nid,
1725 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001726 if (!spec->no_multi_io &&
1727 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001728 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001729 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001730 if (!err)
1731 mapped = true;
1732 }
1733 } while (mapped);
1734 }
1735
1736 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001737 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001738 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001739
Takashi Iwaida96fb52013-07-29 16:54:36 +02001740 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001741 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1742 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001743 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001744 if (err < 0)
1745 return err;
1746 /* we don't count badness at this stage yet */
1747 }
1748
1749 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1750 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1751 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001752 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001753 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001754 if (err < 0)
1755 return err;
1756 badness += err;
1757 }
1758 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1759 err = try_assign_dacs(codec, cfg->speaker_outs,
1760 cfg->speaker_pins,
1761 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001762 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001763 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001764 if (err < 0)
1765 return err;
1766 badness += err;
1767 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001768 if (!spec->no_multi_io &&
1769 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001770 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001771 if (err < 0)
1772 return err;
1773 badness += err;
1774 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001775
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001776 if (spec->mixer_nid) {
1777 spec->aamix_out_paths[0] =
1778 check_aamix_out_path(codec, spec->out_paths[0]);
1779 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1780 spec->aamix_out_paths[1] =
1781 check_aamix_out_path(codec, spec->hp_paths[0]);
1782 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1783 spec->aamix_out_paths[2] =
1784 check_aamix_out_path(codec, spec->speaker_paths[0]);
1785 }
1786
Takashi Iwaida96fb52013-07-29 16:54:36 +02001787 if (!spec->no_multi_io &&
1788 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001789 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1790 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001791
Takashi Iwaia07a9492013-01-07 16:44:06 +01001792 /* re-count num_dacs and squash invalid entries */
1793 spec->multiout.num_dacs = 0;
1794 for (i = 0; i < cfg->line_outs; i++) {
1795 if (spec->private_dac_nids[i])
1796 spec->multiout.num_dacs++;
1797 else {
1798 memmove(spec->private_dac_nids + i,
1799 spec->private_dac_nids + i + 1,
1800 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1801 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1802 }
1803 }
1804
1805 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001806 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001807
Takashi Iwai352f7f92012-12-19 12:52:06 +01001808 if (spec->multi_ios == 2) {
1809 for (i = 0; i < 2; i++)
1810 spec->private_dac_nids[spec->multiout.num_dacs++] =
1811 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001812 } else if (spec->multi_ios) {
1813 spec->multi_ios = 0;
1814 badness += BAD_MULTI_IO;
1815 }
1816
Takashi Iwai55a63d42013-03-21 17:20:12 +01001817 if (spec->indep_hp && !indep_hp_possible(codec))
1818 badness += BAD_NO_INDEP_HP;
1819
Takashi Iwaia07a9492013-01-07 16:44:06 +01001820 /* re-fill the shared DAC for speaker / headphone */
1821 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1822 refill_shared_dacs(codec, cfg->hp_outs,
1823 spec->multiout.hp_out_nid,
1824 spec->hp_paths);
1825 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1826 refill_shared_dacs(codec, cfg->speaker_outs,
1827 spec->multiout.extra_out_nid,
1828 spec->speaker_paths);
1829
Takashi Iwai352f7f92012-12-19 12:52:06 +01001830 return badness;
1831}
1832
1833#define DEBUG_BADNESS
1834
1835#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001836#define debug_badness(fmt, ...) \
1837 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001838#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001839#define debug_badness(fmt, ...) \
1840 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001841#endif
1842
Takashi Iwaia7694092013-01-21 10:43:18 +01001843#ifdef DEBUG_BADNESS
1844static inline void print_nid_path_idx(struct hda_codec *codec,
1845 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001846{
Takashi Iwaia7694092013-01-21 10:43:18 +01001847 struct nid_path *path;
1848
1849 path = snd_hda_get_path_from_idx(codec, idx);
1850 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001851 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001852}
1853
1854static void debug_show_configs(struct hda_codec *codec,
1855 struct auto_pin_cfg *cfg)
1856{
1857 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001858 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001859 int i;
1860
1861 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001862 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001863 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001864 spec->multiout.dac_nids[0],
1865 spec->multiout.dac_nids[1],
1866 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001867 spec->multiout.dac_nids[3],
1868 lo_type[cfg->line_out_type]);
1869 for (i = 0; i < cfg->line_outs; i++)
1870 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001871 if (spec->multi_ios > 0)
1872 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1873 spec->multi_ios,
1874 spec->multi_io[0].pin, spec->multi_io[1].pin,
1875 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001876 for (i = 0; i < spec->multi_ios; i++)
1877 print_nid_path_idx(codec, " mio",
1878 spec->out_paths[cfg->line_outs + i]);
1879 if (cfg->hp_outs)
1880 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001881 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001882 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883 spec->multiout.hp_out_nid[0],
1884 spec->multiout.hp_out_nid[1],
1885 spec->multiout.hp_out_nid[2],
1886 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001887 for (i = 0; i < cfg->hp_outs; i++)
1888 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1889 if (cfg->speaker_outs)
1890 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001891 cfg->speaker_pins[0], cfg->speaker_pins[1],
1892 cfg->speaker_pins[2], cfg->speaker_pins[3],
1893 spec->multiout.extra_out_nid[0],
1894 spec->multiout.extra_out_nid[1],
1895 spec->multiout.extra_out_nid[2],
1896 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001897 for (i = 0; i < cfg->speaker_outs; i++)
1898 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1899 for (i = 0; i < 3; i++)
1900 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001901}
Takashi Iwaia7694092013-01-21 10:43:18 +01001902#else
1903#define debug_show_configs(codec, cfg) /* NOP */
1904#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001905
1906/* find all available DACs of the codec */
1907static void fill_all_dac_nids(struct hda_codec *codec)
1908{
1909 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001910 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001911
1912 spec->num_all_dacs = 0;
1913 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
Takashi Iwai7639a062015-03-03 10:07:24 +01001914 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001915 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1916 continue;
1917 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001918 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001919 break;
1920 }
1921 spec->all_dacs[spec->num_all_dacs++] = nid;
1922 }
1923}
1924
1925static int parse_output_paths(struct hda_codec *codec)
1926{
1927 struct hda_gen_spec *spec = codec->spec;
1928 struct auto_pin_cfg *cfg = &spec->autocfg;
1929 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001930 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001931 int best_badness = INT_MAX;
1932 int badness;
1933 bool fill_hardwired = true, fill_mio_first = true;
1934 bool best_wired = true, best_mio = true;
1935 bool hp_spk_swapped = false;
1936
Takashi Iwai352f7f92012-12-19 12:52:06 +01001937 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1938 if (!best_cfg)
1939 return -ENOMEM;
1940 *best_cfg = *cfg;
1941
1942 for (;;) {
1943 badness = fill_and_eval_dacs(codec, fill_hardwired,
1944 fill_mio_first);
1945 if (badness < 0) {
1946 kfree(best_cfg);
1947 return badness;
1948 }
1949 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1950 cfg->line_out_type, fill_hardwired, fill_mio_first,
1951 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001952 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001953 if (badness < best_badness) {
1954 best_badness = badness;
1955 *best_cfg = *cfg;
1956 best_wired = fill_hardwired;
1957 best_mio = fill_mio_first;
1958 }
1959 if (!badness)
1960 break;
1961 fill_mio_first = !fill_mio_first;
1962 if (!fill_mio_first)
1963 continue;
1964 fill_hardwired = !fill_hardwired;
1965 if (!fill_hardwired)
1966 continue;
1967 if (hp_spk_swapped)
1968 break;
1969 hp_spk_swapped = true;
1970 if (cfg->speaker_outs > 0 &&
1971 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1972 cfg->hp_outs = cfg->line_outs;
1973 memcpy(cfg->hp_pins, cfg->line_out_pins,
1974 sizeof(cfg->hp_pins));
1975 cfg->line_outs = cfg->speaker_outs;
1976 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1977 sizeof(cfg->speaker_pins));
1978 cfg->speaker_outs = 0;
1979 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1980 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1981 fill_hardwired = true;
1982 continue;
1983 }
1984 if (cfg->hp_outs > 0 &&
1985 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1986 cfg->speaker_outs = cfg->line_outs;
1987 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1988 sizeof(cfg->speaker_pins));
1989 cfg->line_outs = cfg->hp_outs;
1990 memcpy(cfg->line_out_pins, cfg->hp_pins,
1991 sizeof(cfg->hp_pins));
1992 cfg->hp_outs = 0;
1993 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1994 cfg->line_out_type = AUTO_PIN_HP_OUT;
1995 fill_hardwired = true;
1996 continue;
1997 }
1998 break;
1999 }
2000
2001 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002002 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01002003 *cfg = *best_cfg;
2004 fill_and_eval_dacs(codec, best_wired, best_mio);
2005 }
2006 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2007 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01002008 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002009
2010 if (cfg->line_out_pins[0]) {
2011 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01002012 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002013 if (path)
2014 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002015 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01002016 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2017 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002018 if (spec->dac_min_mute)
2019 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2020 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002021 }
2022
Takashi Iwai9314a582013-01-21 10:49:05 +01002023 /* set initial pinctl targets */
2024 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2025 val = PIN_HP;
2026 else
2027 val = PIN_OUT;
2028 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2029 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2030 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2031 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2032 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2033 set_pin_targets(codec, cfg->speaker_outs,
2034 cfg->speaker_pins, val);
2035 }
2036
Takashi Iwai55a63d42013-03-21 17:20:12 +01002037 /* clear indep_hp flag if not available */
2038 if (spec->indep_hp && !indep_hp_possible(codec))
2039 spec->indep_hp = 0;
2040
Takashi Iwai352f7f92012-12-19 12:52:06 +01002041 kfree(best_cfg);
2042 return 0;
2043}
2044
2045/* add playback controls from the parsed DAC table */
2046static int create_multi_out_ctls(struct hda_codec *codec,
2047 const struct auto_pin_cfg *cfg)
2048{
2049 struct hda_gen_spec *spec = codec->spec;
2050 int i, err, noutputs;
2051
2052 noutputs = cfg->line_outs;
2053 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2054 noutputs += spec->multi_ios;
2055
2056 for (i = 0; i < noutputs; i++) {
2057 const char *name;
2058 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002059 struct nid_path *path;
2060
Takashi Iwai196c17662013-01-04 15:01:40 +01002061 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002062 if (!path)
2063 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002064
2065 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002066 if (!name || !strcmp(name, "CLFE")) {
2067 /* Center/LFE */
2068 err = add_vol_ctl(codec, "Center", 0, 1, path);
2069 if (err < 0)
2070 return err;
2071 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2072 if (err < 0)
2073 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002074 } else {
2075 err = add_stereo_vol(codec, name, index, path);
2076 if (err < 0)
2077 return err;
2078 }
2079
2080 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2081 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002082 err = add_sw_ctl(codec, "Center", 0, 1, path);
2083 if (err < 0)
2084 return err;
2085 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2086 if (err < 0)
2087 return err;
2088 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002089 err = add_stereo_sw(codec, name, index, path);
2090 if (err < 0)
2091 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
2093 }
2094 return 0;
2095}
2096
Takashi Iwaic2c80382013-01-07 10:33:57 +01002097static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002098 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002100 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 int err;
2102
Takashi Iwai196c17662013-01-04 15:01:40 +01002103 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002104 if (!path)
2105 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002106 err = add_stereo_vol(codec, pfx, cidx, path);
2107 if (err < 0)
2108 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002109 err = add_stereo_sw(codec, pfx, cidx, path);
2110 if (err < 0)
2111 return err;
2112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
Takashi Iwai352f7f92012-12-19 12:52:06 +01002115/* add playback controls for speaker and HP outputs */
2116static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002117 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002119 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002120
2121 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002122 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002123 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002124 int err, idx = 0;
2125
2126 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2127 name = "Bass Speaker";
2128 else if (num_pins >= 3) {
2129 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002130 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002131 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002132 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002133 name = pfx;
2134 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002136 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002137 if (err < 0)
2138 return err;
2139 }
2140 return 0;
2141}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002142
Takashi Iwai352f7f92012-12-19 12:52:06 +01002143static int create_hp_out_ctls(struct hda_codec *codec)
2144{
2145 struct hda_gen_spec *spec = codec->spec;
2146 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002147 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002148 "Headphone");
2149}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Takashi Iwai352f7f92012-12-19 12:52:06 +01002151static int create_speaker_out_ctls(struct hda_codec *codec)
2152{
2153 struct hda_gen_spec *spec = codec->spec;
2154 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002155 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002156 "Speaker");
2157}
2158
2159/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002160 * independent HP controls
2161 */
2162
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002163static void call_hp_automute(struct hda_codec *codec,
2164 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002165static int indep_hp_info(struct snd_kcontrol *kcontrol,
2166 struct snd_ctl_elem_info *uinfo)
2167{
2168 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2169}
2170
2171static int indep_hp_get(struct snd_kcontrol *kcontrol,
2172 struct snd_ctl_elem_value *ucontrol)
2173{
2174 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2175 struct hda_gen_spec *spec = codec->spec;
2176 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2177 return 0;
2178}
2179
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002180static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2181 int nomix_path_idx, int mix_path_idx,
2182 int out_type);
2183
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002184static int indep_hp_put(struct snd_kcontrol *kcontrol,
2185 struct snd_ctl_elem_value *ucontrol)
2186{
2187 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2188 struct hda_gen_spec *spec = codec->spec;
2189 unsigned int select = ucontrol->value.enumerated.item[0];
2190 int ret = 0;
2191
2192 mutex_lock(&spec->pcm_mutex);
2193 if (spec->active_streams) {
2194 ret = -EBUSY;
2195 goto unlock;
2196 }
2197
2198 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002199 hda_nid_t *dacp;
2200 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2201 dacp = &spec->private_dac_nids[0];
2202 else
2203 dacp = &spec->multiout.hp_out_nid[0];
2204
2205 /* update HP aamix paths in case it conflicts with indep HP */
2206 if (spec->have_aamix_ctl) {
2207 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2208 update_aamix_paths(codec, spec->aamix_mode,
2209 spec->out_paths[0],
2210 spec->aamix_out_paths[0],
2211 spec->autocfg.line_out_type);
2212 else
2213 update_aamix_paths(codec, spec->aamix_mode,
2214 spec->hp_paths[0],
2215 spec->aamix_out_paths[1],
2216 AUTO_PIN_HP_OUT);
2217 }
2218
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002219 spec->indep_hp_enabled = select;
2220 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002221 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002222 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002223 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002224
Takashi Iwai963afde2013-05-31 15:20:31 +02002225 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002226 ret = 1;
2227 }
2228 unlock:
2229 mutex_unlock(&spec->pcm_mutex);
2230 return ret;
2231}
2232
2233static const struct snd_kcontrol_new indep_hp_ctl = {
2234 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2235 .name = "Independent HP",
2236 .info = indep_hp_info,
2237 .get = indep_hp_get,
2238 .put = indep_hp_put,
2239};
2240
2241
2242static int create_indep_hp_ctls(struct hda_codec *codec)
2243{
2244 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002245 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002246
2247 if (!spec->indep_hp)
2248 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002249 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2250 dac = spec->multiout.dac_nids[0];
2251 else
2252 dac = spec->multiout.hp_out_nid[0];
2253 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002254 spec->indep_hp = 0;
2255 return 0;
2256 }
2257
2258 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002259 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002260 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2261 return -ENOMEM;
2262 return 0;
2263}
2264
2265/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002266 * channel mode enum control
2267 */
2268
2269static int ch_mode_info(struct snd_kcontrol *kcontrol,
2270 struct snd_ctl_elem_info *uinfo)
2271{
2272 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2273 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002274 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002275
2276 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2277 uinfo->count = 1;
2278 uinfo->value.enumerated.items = spec->multi_ios + 1;
2279 if (uinfo->value.enumerated.item > spec->multi_ios)
2280 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002281 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2282 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002283 return 0;
2284}
2285
2286static int ch_mode_get(struct snd_kcontrol *kcontrol,
2287 struct snd_ctl_elem_value *ucontrol)
2288{
2289 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2290 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002291 ucontrol->value.enumerated.item[0] =
2292 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002293 return 0;
2294}
2295
Takashi Iwai196c17662013-01-04 15:01:40 +01002296static inline struct nid_path *
2297get_multiio_path(struct hda_codec *codec, int idx)
2298{
2299 struct hda_gen_spec *spec = codec->spec;
2300 return snd_hda_get_path_from_idx(codec,
2301 spec->out_paths[spec->autocfg.line_outs + idx]);
2302}
2303
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002304static void update_automute_all(struct hda_codec *codec);
2305
Takashi Iwai65033cc2013-04-16 12:31:05 +02002306/* Default value to be passed as aamix argument for snd_hda_activate_path();
2307 * used for output paths
2308 */
2309static bool aamix_default(struct hda_gen_spec *spec)
2310{
2311 return !spec->have_aamix_ctl || spec->aamix_mode;
2312}
2313
Takashi Iwai352f7f92012-12-19 12:52:06 +01002314static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2315{
2316 struct hda_gen_spec *spec = codec->spec;
2317 hda_nid_t nid = spec->multi_io[idx].pin;
2318 struct nid_path *path;
2319
Takashi Iwai196c17662013-01-04 15:01:40 +01002320 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002321 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002323
2324 if (path->active == output)
2325 return 0;
2326
2327 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002328 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002329 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002330 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002331 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002332 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002333 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002334 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002335 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002337
2338 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002339 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002340
Takashi Iwai352f7f92012-12-19 12:52:06 +01002341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
Takashi Iwai352f7f92012-12-19 12:52:06 +01002344static int ch_mode_put(struct snd_kcontrol *kcontrol,
2345 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2348 struct hda_gen_spec *spec = codec->spec;
2349 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Takashi Iwai352f7f92012-12-19 12:52:06 +01002351 ch = ucontrol->value.enumerated.item[0];
2352 if (ch < 0 || ch > spec->multi_ios)
2353 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002354 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002355 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002356 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002357 for (i = 0; i < spec->multi_ios; i++)
2358 set_multi_io(codec, i, i < ch);
2359 spec->multiout.max_channels = max(spec->ext_channel_count,
2360 spec->const_channel_count);
2361 if (spec->need_dac_fix)
2362 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 return 1;
2364}
2365
Takashi Iwai352f7f92012-12-19 12:52:06 +01002366static const struct snd_kcontrol_new channel_mode_enum = {
2367 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2368 .name = "Channel Mode",
2369 .info = ch_mode_info,
2370 .get = ch_mode_get,
2371 .put = ch_mode_put,
2372};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Takashi Iwai352f7f92012-12-19 12:52:06 +01002374static int create_multi_channel_mode(struct hda_codec *codec)
2375{
2376 struct hda_gen_spec *spec = codec->spec;
2377
2378 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002379 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002380 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return 0;
2383}
2384
Takashi Iwai352f7f92012-12-19 12:52:06 +01002385/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002386 * aamix loopback enable/disable switch
2387 */
2388
2389#define loopback_mixing_info indep_hp_info
2390
2391static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2392 struct snd_ctl_elem_value *ucontrol)
2393{
2394 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2395 struct hda_gen_spec *spec = codec->spec;
2396 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2397 return 0;
2398}
2399
2400static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002401 int nomix_path_idx, int mix_path_idx,
2402 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002403{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002404 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002405 struct nid_path *nomix_path, *mix_path;
2406
2407 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2408 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2409 if (!nomix_path || !mix_path)
2410 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002411
2412 /* if HP aamix path is driven from a different DAC and the
2413 * independent HP mode is ON, can't turn on aamix path
2414 */
2415 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2416 mix_path->path[0] != spec->alt_dac_nid)
2417 do_mix = false;
2418
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002419 if (do_mix) {
2420 snd_hda_activate_path(codec, nomix_path, false, true);
2421 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002422 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002423 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002424 snd_hda_activate_path(codec, mix_path, false, false);
2425 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002426 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002427 }
2428}
2429
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002430/* re-initialize the output paths; only called from loopback_mixing_put() */
2431static void update_output_paths(struct hda_codec *codec, int num_outs,
2432 const int *paths)
2433{
2434 struct hda_gen_spec *spec = codec->spec;
2435 struct nid_path *path;
2436 int i;
2437
2438 for (i = 0; i < num_outs; i++) {
2439 path = snd_hda_get_path_from_idx(codec, paths[i]);
2440 if (path)
2441 snd_hda_activate_path(codec, path, path->active,
2442 spec->aamix_mode);
2443 }
2444}
2445
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002446static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2447 struct snd_ctl_elem_value *ucontrol)
2448{
2449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2450 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002451 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002452 unsigned int val = ucontrol->value.enumerated.item[0];
2453
2454 if (val == spec->aamix_mode)
2455 return 0;
2456 spec->aamix_mode = val;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002457 if (has_aamix_out_paths(spec)) {
2458 update_aamix_paths(codec, val, spec->out_paths[0],
2459 spec->aamix_out_paths[0],
2460 cfg->line_out_type);
2461 update_aamix_paths(codec, val, spec->hp_paths[0],
2462 spec->aamix_out_paths[1],
2463 AUTO_PIN_HP_OUT);
2464 update_aamix_paths(codec, val, spec->speaker_paths[0],
2465 spec->aamix_out_paths[2],
2466 AUTO_PIN_SPEAKER_OUT);
2467 } else {
2468 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2469 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2470 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2471 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2472 update_output_paths(codec, cfg->speaker_outs,
2473 spec->speaker_paths);
2474 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002475 return 1;
2476}
2477
2478static const struct snd_kcontrol_new loopback_mixing_enum = {
2479 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2480 .name = "Loopback Mixing",
2481 .info = loopback_mixing_info,
2482 .get = loopback_mixing_get,
2483 .put = loopback_mixing_put,
2484};
2485
2486static int create_loopback_mixing_ctl(struct hda_codec *codec)
2487{
2488 struct hda_gen_spec *spec = codec->spec;
2489
2490 if (!spec->mixer_nid)
2491 return 0;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002492 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2493 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002494 spec->have_aamix_ctl = 1;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002495 /* if no explicit aamix path is present (e.g. for Realtek codecs),
2496 * enable aamix as default -- just for compatibility
2497 */
2498 spec->aamix_mode = !has_aamix_out_paths(spec);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002499 return 0;
2500}
2501
2502/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002503 * shared headphone/mic handling
2504 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002505
Takashi Iwai352f7f92012-12-19 12:52:06 +01002506static void call_update_outputs(struct hda_codec *codec);
2507
2508/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002509static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002510{
2511 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002512 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002513 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002514 hda_nid_t pin;
2515
2516 pin = spec->hp_mic_pin;
2517 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2518
2519 if (!force) {
2520 val = snd_hda_codec_get_pin_target(codec, pin);
2521 if (as_mic) {
2522 if (val & PIN_IN)
2523 return;
2524 } else {
2525 if (val & PIN_OUT)
2526 return;
2527 }
2528 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002529
2530 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002531 /* if the HP pin doesn't support VREF and the codec driver gives an
2532 * alternative pin, set up the VREF on that pin instead
2533 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002534 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2535 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2536 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2537 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002538 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002539 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002540 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002541
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002542 if (!spec->hp_mic_jack_modes) {
2543 if (as_mic)
2544 val |= PIN_IN;
2545 else
2546 val = PIN_HP;
2547 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002548 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002549 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002550}
2551
2552/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002553static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002554{
2555 struct hda_gen_spec *spec = codec->spec;
2556 struct auto_pin_cfg *cfg = &spec->autocfg;
2557 unsigned int defcfg;
2558 hda_nid_t nid;
2559
Takashi Iwai967303d2013-02-19 17:12:42 +01002560 if (!spec->hp_mic) {
2561 if (spec->suppress_hp_mic_detect)
2562 return 0;
2563 /* automatic detection: only if no input or a single internal
2564 * input pin is found, try to detect the shared hp/mic
2565 */
2566 if (cfg->num_inputs > 1)
2567 return 0;
2568 else if (cfg->num_inputs == 1) {
2569 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2570 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2571 return 0;
2572 }
2573 }
2574
2575 spec->hp_mic = 0; /* clear once */
2576 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002577 return 0;
2578
Takashi Iwai967303d2013-02-19 17:12:42 +01002579 nid = 0;
2580 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2581 nid = cfg->line_out_pins[0];
2582 else if (cfg->hp_outs > 0)
2583 nid = cfg->hp_pins[0];
2584 if (!nid)
2585 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002586
2587 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2588 return 0; /* no input */
2589
Takashi Iwai967303d2013-02-19 17:12:42 +01002590 cfg->inputs[cfg->num_inputs].pin = nid;
2591 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002592 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002593 cfg->num_inputs++;
2594 spec->hp_mic = 1;
2595 spec->hp_mic_pin = nid;
2596 /* we can't handle auto-mic together with HP-mic */
2597 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002598 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002599 return 0;
2600}
2601
Takashi Iwai978e77e2013-01-10 16:57:58 +01002602/*
2603 * output jack mode
2604 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002605
2606static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2607
2608static const char * const out_jack_texts[] = {
2609 "Line Out", "Headphone Out",
2610};
2611
Takashi Iwai978e77e2013-01-10 16:57:58 +01002612static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2613 struct snd_ctl_elem_info *uinfo)
2614{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002615 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002616}
2617
2618static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2619 struct snd_ctl_elem_value *ucontrol)
2620{
2621 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2622 hda_nid_t nid = kcontrol->private_value;
2623 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2624 ucontrol->value.enumerated.item[0] = 1;
2625 else
2626 ucontrol->value.enumerated.item[0] = 0;
2627 return 0;
2628}
2629
2630static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2631 struct snd_ctl_elem_value *ucontrol)
2632{
2633 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2634 hda_nid_t nid = kcontrol->private_value;
2635 unsigned int val;
2636
2637 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2638 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2639 return 0;
2640 snd_hda_set_pin_ctl_cache(codec, nid, val);
2641 return 1;
2642}
2643
2644static const struct snd_kcontrol_new out_jack_mode_enum = {
2645 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2646 .info = out_jack_mode_info,
2647 .get = out_jack_mode_get,
2648 .put = out_jack_mode_put,
2649};
2650
2651static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2652{
2653 struct hda_gen_spec *spec = codec->spec;
2654 int i;
2655
2656 for (i = 0; i < spec->kctls.used; i++) {
2657 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2658 if (!strcmp(kctl->name, name) && kctl->index == idx)
2659 return true;
2660 }
2661 return false;
2662}
2663
2664static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2665 char *name, size_t name_len)
2666{
2667 struct hda_gen_spec *spec = codec->spec;
2668 int idx = 0;
2669
2670 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2671 strlcat(name, " Jack Mode", name_len);
2672
2673 for (; find_kctl_name(codec, name, idx); idx++)
2674 ;
2675}
2676
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002677static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2678{
2679 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002680 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002681 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2682 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2683 return 2;
2684 }
2685 return 1;
2686}
2687
Takashi Iwai978e77e2013-01-10 16:57:58 +01002688static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2689 hda_nid_t *pins)
2690{
2691 struct hda_gen_spec *spec = codec->spec;
2692 int i;
2693
2694 for (i = 0; i < num_pins; i++) {
2695 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002696 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002697 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002698 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002699 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002700 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002701 get_jack_mode_name(codec, pin, name, sizeof(name));
2702 knew = snd_hda_gen_add_kctl(spec, name,
2703 &out_jack_mode_enum);
2704 if (!knew)
2705 return -ENOMEM;
2706 knew->private_value = pin;
2707 }
2708 }
2709
2710 return 0;
2711}
2712
Takashi Iwai294765582013-01-17 09:52:11 +01002713/*
2714 * input jack mode
2715 */
2716
2717/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2718#define NUM_VREFS 6
2719
2720static const char * const vref_texts[NUM_VREFS] = {
2721 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2722 "", "Mic 80pc Bias", "Mic 100pc Bias"
2723};
2724
2725static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2726{
2727 unsigned int pincap;
2728
2729 pincap = snd_hda_query_pin_caps(codec, pin);
2730 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2731 /* filter out unusual vrefs */
2732 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2733 return pincap;
2734}
2735
2736/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2737static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2738{
2739 unsigned int i, n = 0;
2740
2741 for (i = 0; i < NUM_VREFS; i++) {
2742 if (vref_caps & (1 << i)) {
2743 if (n == item_idx)
2744 return i;
2745 n++;
2746 }
2747 }
2748 return 0;
2749}
2750
2751/* convert back from the vref ctl index to the enum item index */
2752static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2753{
2754 unsigned int i, n = 0;
2755
2756 for (i = 0; i < NUM_VREFS; i++) {
2757 if (i == idx)
2758 return n;
2759 if (vref_caps & (1 << i))
2760 n++;
2761 }
2762 return 0;
2763}
2764
2765static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2766 struct snd_ctl_elem_info *uinfo)
2767{
2768 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2769 hda_nid_t nid = kcontrol->private_value;
2770 unsigned int vref_caps = get_vref_caps(codec, nid);
2771
2772 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2773 vref_texts);
2774 /* set the right text */
2775 strcpy(uinfo->value.enumerated.name,
2776 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2777 return 0;
2778}
2779
2780static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2781 struct snd_ctl_elem_value *ucontrol)
2782{
2783 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2784 hda_nid_t nid = kcontrol->private_value;
2785 unsigned int vref_caps = get_vref_caps(codec, nid);
2786 unsigned int idx;
2787
2788 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2789 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2790 return 0;
2791}
2792
2793static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2794 struct snd_ctl_elem_value *ucontrol)
2795{
2796 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2797 hda_nid_t nid = kcontrol->private_value;
2798 unsigned int vref_caps = get_vref_caps(codec, nid);
2799 unsigned int val, idx;
2800
2801 val = snd_hda_codec_get_pin_target(codec, nid);
2802 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2803 if (idx == ucontrol->value.enumerated.item[0])
2804 return 0;
2805
2806 val &= ~AC_PINCTL_VREFEN;
2807 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2808 snd_hda_set_pin_ctl_cache(codec, nid, val);
2809 return 1;
2810}
2811
2812static const struct snd_kcontrol_new in_jack_mode_enum = {
2813 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2814 .info = in_jack_mode_info,
2815 .get = in_jack_mode_get,
2816 .put = in_jack_mode_put,
2817};
2818
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002819static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2820{
2821 struct hda_gen_spec *spec = codec->spec;
2822 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002823 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002824 nitems = hweight32(get_vref_caps(codec, pin));
2825 return nitems ? nitems : 1;
2826}
2827
Takashi Iwai294765582013-01-17 09:52:11 +01002828static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2829{
2830 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002831 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002832 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002833 unsigned int defcfg;
2834
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002835 if (pin == spec->hp_mic_pin)
2836 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002837
2838 /* no jack mode for fixed pins */
2839 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2840 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2841 return 0;
2842
2843 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002844 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002845 return 0;
2846
2847 get_jack_mode_name(codec, pin, name, sizeof(name));
2848 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2849 if (!knew)
2850 return -ENOMEM;
2851 knew->private_value = pin;
2852 return 0;
2853}
2854
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002855/*
2856 * HP/mic shared jack mode
2857 */
2858static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2859 struct snd_ctl_elem_info *uinfo)
2860{
2861 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2862 hda_nid_t nid = kcontrol->private_value;
2863 int out_jacks = get_out_jack_num_items(codec, nid);
2864 int in_jacks = get_in_jack_num_items(codec, nid);
2865 const char *text = NULL;
2866 int idx;
2867
2868 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2869 uinfo->count = 1;
2870 uinfo->value.enumerated.items = out_jacks + in_jacks;
2871 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2872 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2873 idx = uinfo->value.enumerated.item;
2874 if (idx < out_jacks) {
2875 if (out_jacks > 1)
2876 text = out_jack_texts[idx];
2877 else
2878 text = "Headphone Out";
2879 } else {
2880 idx -= out_jacks;
2881 if (in_jacks > 1) {
2882 unsigned int vref_caps = get_vref_caps(codec, nid);
2883 text = vref_texts[get_vref_idx(vref_caps, idx)];
2884 } else
2885 text = "Mic In";
2886 }
2887
2888 strcpy(uinfo->value.enumerated.name, text);
2889 return 0;
2890}
2891
2892static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2893{
2894 int out_jacks = get_out_jack_num_items(codec, nid);
2895 int in_jacks = get_in_jack_num_items(codec, nid);
2896 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2897 int idx = 0;
2898
2899 if (val & PIN_OUT) {
2900 if (out_jacks > 1 && val == PIN_HP)
2901 idx = 1;
2902 } else if (val & PIN_IN) {
2903 idx = out_jacks;
2904 if (in_jacks > 1) {
2905 unsigned int vref_caps = get_vref_caps(codec, nid);
2906 val &= AC_PINCTL_VREFEN;
2907 idx += cvt_from_vref_idx(vref_caps, val);
2908 }
2909 }
2910 return idx;
2911}
2912
2913static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2914 struct snd_ctl_elem_value *ucontrol)
2915{
2916 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2917 hda_nid_t nid = kcontrol->private_value;
2918 ucontrol->value.enumerated.item[0] =
2919 get_cur_hp_mic_jack_mode(codec, nid);
2920 return 0;
2921}
2922
2923static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2924 struct snd_ctl_elem_value *ucontrol)
2925{
2926 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2927 hda_nid_t nid = kcontrol->private_value;
2928 int out_jacks = get_out_jack_num_items(codec, nid);
2929 int in_jacks = get_in_jack_num_items(codec, nid);
2930 unsigned int val, oldval, idx;
2931
2932 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2933 idx = ucontrol->value.enumerated.item[0];
2934 if (oldval == idx)
2935 return 0;
2936
2937 if (idx < out_jacks) {
2938 if (out_jacks > 1)
2939 val = idx ? PIN_HP : PIN_OUT;
2940 else
2941 val = PIN_HP;
2942 } else {
2943 idx -= out_jacks;
2944 if (in_jacks > 1) {
2945 unsigned int vref_caps = get_vref_caps(codec, nid);
2946 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002947 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2948 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002949 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002950 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002951 }
2952 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002953 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002954
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002955 return 1;
2956}
2957
2958static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2959 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2960 .info = hp_mic_jack_mode_info,
2961 .get = hp_mic_jack_mode_get,
2962 .put = hp_mic_jack_mode_put,
2963};
2964
2965static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2966{
2967 struct hda_gen_spec *spec = codec->spec;
2968 struct snd_kcontrol_new *knew;
2969
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002970 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2971 &hp_mic_jack_mode_enum);
2972 if (!knew)
2973 return -ENOMEM;
2974 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002975 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002976 return 0;
2977}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002978
2979/*
2980 * Parse input paths
2981 */
2982
Takashi Iwai352f7f92012-12-19 12:52:06 +01002983/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002984static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002985{
2986 struct hda_amp_list *list;
2987
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002988 list = snd_array_new(&spec->loopback_list);
2989 if (!list)
2990 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002991 list->nid = mix;
2992 list->dir = HDA_INPUT;
2993 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002994 spec->loopback.amplist = spec->loopback_list.list;
2995 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002996}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002997
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002998/* return true if either a volume or a mute amp is found for the given
2999 * aamix path; the amp has to be either in the mixer node or its direct leaf
3000 */
3001static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3002 hda_nid_t pin, unsigned int *mix_val,
3003 unsigned int *mute_val)
3004{
3005 int idx, num_conns;
3006 const hda_nid_t *list;
3007 hda_nid_t nid;
3008
3009 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3010 if (idx < 0)
3011 return false;
3012
3013 *mix_val = *mute_val = 0;
3014 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3015 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3016 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3017 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3018 if (*mix_val && *mute_val)
3019 return true;
3020
3021 /* check leaf node */
3022 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3023 if (num_conns < idx)
3024 return false;
3025 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01003026 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3027 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003028 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01003029 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3030 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003031 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3032
3033 return *mix_val || *mute_val;
3034}
3035
Takashi Iwai352f7f92012-12-19 12:52:06 +01003036/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01003037static int new_analog_input(struct hda_codec *codec, int input_idx,
3038 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003039 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003041 struct hda_gen_spec *spec = codec->spec;
3042 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003043 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003044 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003046 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3047 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003048
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003049 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003050 if (!path)
3051 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003052 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01003053 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003054
3055 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003056 if (mix_val) {
3057 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003058 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003060 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
3062
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003063 if (mute_val) {
3064 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003065 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003067 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
3069
Takashi Iwai352f7f92012-12-19 12:52:06 +01003070 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003071 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003072 err = add_loopback_list(spec, mix_nid, idx);
3073 if (err < 0)
3074 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003075
3076 if (spec->mixer_nid != spec->mixer_merge_nid &&
3077 !spec->loopback_merge_path) {
3078 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3079 spec->mixer_merge_nid, 0);
3080 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003081 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003082 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003083 path->pin_fixed = true; /* static route */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003084 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003085 spec->loopback_merge_path =
3086 snd_hda_get_path_idx(codec, path);
3087 }
3088 }
3089
Takashi Iwai352f7f92012-12-19 12:52:06 +01003090 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091}
3092
Takashi Iwai352f7f92012-12-19 12:52:06 +01003093static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003095 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3096 return (pincap & AC_PINCAP_IN) != 0;
3097}
3098
3099/* Parse the codec tree and retrieve ADCs */
3100static int fill_adc_nids(struct hda_codec *codec)
3101{
3102 struct hda_gen_spec *spec = codec->spec;
3103 hda_nid_t nid;
3104 hda_nid_t *adc_nids = spec->adc_nids;
3105 int max_nums = ARRAY_SIZE(spec->adc_nids);
Takashi Iwai7639a062015-03-03 10:07:24 +01003106 int nums = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003107
Takashi Iwai7639a062015-03-03 10:07:24 +01003108 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003109 unsigned int caps = get_wcaps(codec, nid);
3110 int type = get_wcaps_type(caps);
3111
3112 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3113 continue;
3114 adc_nids[nums] = nid;
3115 if (++nums >= max_nums)
3116 break;
3117 }
3118 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003119
3120 /* copy the detected ADCs to all_adcs[] */
3121 spec->num_all_adcs = nums;
3122 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3123
Takashi Iwai352f7f92012-12-19 12:52:06 +01003124 return nums;
3125}
3126
3127/* filter out invalid adc_nids that don't give all active input pins;
3128 * if needed, check whether dynamic ADC-switching is available
3129 */
3130static int check_dyn_adc_switch(struct hda_codec *codec)
3131{
3132 struct hda_gen_spec *spec = codec->spec;
3133 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003134 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003135 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003136
Takashi Iwai352f7f92012-12-19 12:52:06 +01003137 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003138 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003139 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003140 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003141 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003142 break;
3143 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003144 if (i >= imux->num_items) {
3145 ok_bits |= (1 << n);
3146 nums++;
3147 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003148 }
3149
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003150 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003151 /* check whether ADC-switch is possible */
3152 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003153 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003154 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003155 spec->dyn_adc_idx[i] = n;
3156 break;
3157 }
3158 }
3159 }
3160
Takashi Iwai4e76a882014-02-25 12:21:03 +01003161 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003162 spec->dyn_adc_switch = 1;
3163 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003164 /* shrink the invalid adcs and input paths */
3165 nums = 0;
3166 for (n = 0; n < spec->num_adc_nids; n++) {
3167 if (!(ok_bits & (1 << n)))
3168 continue;
3169 if (n != nums) {
3170 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003171 for (i = 0; i < imux->num_items; i++) {
3172 invalidate_nid_path(codec,
3173 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003174 spec->input_paths[i][nums] =
3175 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003176 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003177 }
3178 nums++;
3179 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003180 spec->num_adc_nids = nums;
3181 }
3182
Takashi Iwai967303d2013-02-19 17:12:42 +01003183 if (imux->num_items == 1 ||
3184 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003185 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003186 spec->num_adc_nids = 1; /* reduce to a single ADC */
3187 }
3188
3189 /* single index for individual volumes ctls */
3190 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3191 spec->num_adc_nids = 1;
3192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return 0;
3194}
3195
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003196/* parse capture source paths from the given pin and create imux items */
3197static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003198 int cfg_idx, int num_adcs,
3199 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003200{
3201 struct hda_gen_spec *spec = codec->spec;
3202 struct hda_input_mux *imux = &spec->input_mux;
3203 int imux_idx = imux->num_items;
3204 bool imux_added = false;
3205 int c;
3206
3207 for (c = 0; c < num_adcs; c++) {
3208 struct nid_path *path;
3209 hda_nid_t adc = spec->adc_nids[c];
3210
3211 if (!is_reachable_path(codec, pin, adc))
3212 continue;
3213 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3214 if (!path)
3215 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003216 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003217 spec->input_paths[imux_idx][c] =
3218 snd_hda_get_path_idx(codec, path);
3219
3220 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003221 if (spec->hp_mic_pin == pin)
3222 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003223 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003224 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003225 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003226 if (spec->dyn_adc_switch)
3227 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003228 }
3229 }
3230
3231 return 0;
3232}
3233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003235 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003237
Takashi Iwaic9700422013-01-18 10:17:30 +01003238/* fill the label for each input at first */
3239static int fill_input_pin_labels(struct hda_codec *codec)
3240{
3241 struct hda_gen_spec *spec = codec->spec;
3242 const struct auto_pin_cfg *cfg = &spec->autocfg;
3243 int i;
3244
3245 for (i = 0; i < cfg->num_inputs; i++) {
3246 hda_nid_t pin = cfg->inputs[i].pin;
3247 const char *label;
3248 int j, idx;
3249
3250 if (!is_input_pin(codec, pin))
3251 continue;
3252
3253 label = hda_get_autocfg_input_label(codec, cfg, i);
3254 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003255 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003256 if (spec->input_labels[j] &&
3257 !strcmp(spec->input_labels[j], label)) {
3258 idx = spec->input_label_idxs[j] + 1;
3259 break;
3260 }
3261 }
3262
3263 spec->input_labels[i] = label;
3264 spec->input_label_idxs[i] = idx;
3265 }
3266
3267 return 0;
3268}
3269
Takashi Iwai9dba2052013-01-18 10:01:15 +01003270#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3271
Takashi Iwai352f7f92012-12-19 12:52:06 +01003272static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003273{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003274 struct hda_gen_spec *spec = codec->spec;
3275 const struct auto_pin_cfg *cfg = &spec->autocfg;
3276 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003277 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003278 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003279 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003280
Takashi Iwai352f7f92012-12-19 12:52:06 +01003281 num_adcs = fill_adc_nids(codec);
3282 if (num_adcs < 0)
3283 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003284
Takashi Iwaic9700422013-01-18 10:17:30 +01003285 err = fill_input_pin_labels(codec);
3286 if (err < 0)
3287 return err;
3288
Takashi Iwai352f7f92012-12-19 12:52:06 +01003289 for (i = 0; i < cfg->num_inputs; i++) {
3290 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Takashi Iwai352f7f92012-12-19 12:52:06 +01003292 pin = cfg->inputs[i].pin;
3293 if (!is_input_pin(codec, pin))
3294 continue;
3295
Takashi Iwai2c12c302013-01-10 09:33:29 +01003296 val = PIN_IN;
3297 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3298 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai3e1b0c42015-04-27 10:43:22 +02003299 if (pin != spec->hp_mic_pin &&
3300 !snd_hda_codec_get_pin_target(codec, pin))
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003301 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003302
Takashi Iwai352f7f92012-12-19 12:52:06 +01003303 if (mixer) {
3304 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003305 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003306 spec->input_labels[i],
3307 spec->input_label_idxs[i],
3308 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003309 if (err < 0)
3310 return err;
3311 }
3312 }
3313
Takashi Iwaic9700422013-01-18 10:17:30 +01003314 err = parse_capture_source(codec, pin, i, num_adcs,
3315 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003316 if (err < 0)
3317 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003318
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003319 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003320 err = create_in_jack_mode(codec, pin);
3321 if (err < 0)
3322 return err;
3323 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003324 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003325
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003326 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003327 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003328 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003329 "Stereo Mix", 0);
3330 if (err < 0)
3331 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003332 else
3333 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003334 }
3335
3336 return 0;
3337}
3338
3339
3340/*
3341 * input source mux
3342 */
3343
Takashi Iwaic697b712013-01-07 17:09:26 +01003344/* get the input path specified by the given adc and imux indices */
3345static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003346{
3347 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003348 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3349 snd_BUG();
3350 return NULL;
3351 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003352 if (spec->dyn_adc_switch)
3353 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003354 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003355 snd_BUG();
3356 return NULL;
3357 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003358 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003359}
3360
3361static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3362 unsigned int idx);
3363
3364static int mux_enum_info(struct snd_kcontrol *kcontrol,
3365 struct snd_ctl_elem_info *uinfo)
3366{
3367 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3368 struct hda_gen_spec *spec = codec->spec;
3369 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3370}
3371
3372static int mux_enum_get(struct snd_kcontrol *kcontrol,
3373 struct snd_ctl_elem_value *ucontrol)
3374{
3375 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3376 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003377 /* the ctls are created at once with multiple counts */
3378 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003379
3380 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3381 return 0;
3382}
3383
3384static int mux_enum_put(struct snd_kcontrol *kcontrol,
3385 struct snd_ctl_elem_value *ucontrol)
3386{
3387 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003388 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003389 return mux_select(codec, adc_idx,
3390 ucontrol->value.enumerated.item[0]);
3391}
3392
Takashi Iwai352f7f92012-12-19 12:52:06 +01003393static const struct snd_kcontrol_new cap_src_temp = {
3394 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3395 .name = "Input Source",
3396 .info = mux_enum_info,
3397 .get = mux_enum_get,
3398 .put = mux_enum_put,
3399};
3400
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003401/*
3402 * capture volume and capture switch ctls
3403 */
3404
Takashi Iwai352f7f92012-12-19 12:52:06 +01003405typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3406 struct snd_ctl_elem_value *ucontrol);
3407
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003408/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003409static int cap_put_caller(struct snd_kcontrol *kcontrol,
3410 struct snd_ctl_elem_value *ucontrol,
3411 put_call_t func, int type)
3412{
3413 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3414 struct hda_gen_spec *spec = codec->spec;
3415 const struct hda_input_mux *imux;
3416 struct nid_path *path;
3417 int i, adc_idx, err = 0;
3418
3419 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003420 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003421 mutex_lock(&codec->control_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003422 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003423 path = get_input_path(codec, adc_idx, i);
3424 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003425 continue;
3426 kcontrol->private_value = path->ctls[type];
3427 err = func(kcontrol, ucontrol);
3428 if (err < 0)
Takashi Iwaia551d912015-02-26 12:34:49 +01003429 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003430 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003431 mutex_unlock(&codec->control_mutex);
3432 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003433 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003434 return err;
3435}
3436
3437/* capture volume ctl callbacks */
3438#define cap_vol_info snd_hda_mixer_amp_volume_info
3439#define cap_vol_get snd_hda_mixer_amp_volume_get
3440#define cap_vol_tlv snd_hda_mixer_amp_tlv
3441
3442static int cap_vol_put(struct snd_kcontrol *kcontrol,
3443 struct snd_ctl_elem_value *ucontrol)
3444{
3445 return cap_put_caller(kcontrol, ucontrol,
3446 snd_hda_mixer_amp_volume_put,
3447 NID_PATH_VOL_CTL);
3448}
3449
3450static const struct snd_kcontrol_new cap_vol_temp = {
3451 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3452 .name = "Capture Volume",
3453 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3454 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3455 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3456 .info = cap_vol_info,
3457 .get = cap_vol_get,
3458 .put = cap_vol_put,
3459 .tlv = { .c = cap_vol_tlv },
3460};
3461
3462/* capture switch ctl callbacks */
3463#define cap_sw_info snd_ctl_boolean_stereo_info
3464#define cap_sw_get snd_hda_mixer_amp_switch_get
3465
3466static int cap_sw_put(struct snd_kcontrol *kcontrol,
3467 struct snd_ctl_elem_value *ucontrol)
3468{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003469 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003470 snd_hda_mixer_amp_switch_put,
3471 NID_PATH_MUTE_CTL);
3472}
3473
3474static const struct snd_kcontrol_new cap_sw_temp = {
3475 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3476 .name = "Capture Switch",
3477 .info = cap_sw_info,
3478 .get = cap_sw_get,
3479 .put = cap_sw_put,
3480};
3481
3482static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3483{
3484 hda_nid_t nid;
3485 int i, depth;
3486
3487 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3488 for (depth = 0; depth < 3; depth++) {
3489 if (depth >= path->depth)
3490 return -EINVAL;
3491 i = path->depth - depth - 1;
3492 nid = path->path[i];
3493 if (!path->ctls[NID_PATH_VOL_CTL]) {
3494 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3495 path->ctls[NID_PATH_VOL_CTL] =
3496 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3497 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3498 int idx = path->idx[i];
3499 if (!depth && codec->single_adc_amp)
3500 idx = 0;
3501 path->ctls[NID_PATH_VOL_CTL] =
3502 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3503 }
3504 }
3505 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3506 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3507 path->ctls[NID_PATH_MUTE_CTL] =
3508 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3509 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3510 int idx = path->idx[i];
3511 if (!depth && codec->single_adc_amp)
3512 idx = 0;
3513 path->ctls[NID_PATH_MUTE_CTL] =
3514 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3515 }
3516 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 return 0;
3519}
3520
Takashi Iwai352f7f92012-12-19 12:52:06 +01003521static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003523 struct hda_gen_spec *spec = codec->spec;
3524 struct auto_pin_cfg *cfg = &spec->autocfg;
3525 unsigned int val;
3526 int i;
3527
3528 if (!spec->inv_dmic_split)
3529 return false;
3530 for (i = 0; i < cfg->num_inputs; i++) {
3531 if (cfg->inputs[i].pin != nid)
3532 continue;
3533 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3534 return false;
3535 val = snd_hda_codec_get_pincfg(codec, nid);
3536 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3537 }
3538 return false;
3539}
3540
Takashi Iwaia90229e2013-01-18 14:10:00 +01003541/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003542static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3543 struct snd_ctl_elem_value *ucontrol)
3544{
3545 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3546 struct hda_gen_spec *spec = codec->spec;
3547 int ret;
3548
3549 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3550 if (ret < 0)
3551 return ret;
3552
Takashi Iwaia90229e2013-01-18 14:10:00 +01003553 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003554 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003555
3556 return ret;
3557}
3558
Takashi Iwai352f7f92012-12-19 12:52:06 +01003559static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3560 int idx, bool is_switch, unsigned int ctl,
3561 bool inv_dmic)
3562{
3563 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003564 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003565 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3566 const char *sfx = is_switch ? "Switch" : "Volume";
3567 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003568 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003569
3570 if (!ctl)
3571 return 0;
3572
3573 if (label)
3574 snprintf(tmpname, sizeof(tmpname),
3575 "%s Capture %s", label, sfx);
3576 else
3577 snprintf(tmpname, sizeof(tmpname),
3578 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003579 knew = add_control(spec, type, tmpname, idx,
3580 amp_val_replace_channels(ctl, chs));
3581 if (!knew)
3582 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003583 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003584 knew->put = cap_single_sw_put;
3585 if (!inv_dmic)
3586 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003587
3588 /* Make independent right kcontrol */
3589 if (label)
3590 snprintf(tmpname, sizeof(tmpname),
3591 "Inverted %s Capture %s", label, sfx);
3592 else
3593 snprintf(tmpname, sizeof(tmpname),
3594 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003595 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003596 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003597 if (!knew)
3598 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003599 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003600 knew->put = cap_single_sw_put;
3601 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003602}
3603
3604/* create single (and simple) capture volume and switch controls */
3605static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3606 unsigned int vol_ctl, unsigned int sw_ctl,
3607 bool inv_dmic)
3608{
3609 int err;
3610 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3611 if (err < 0)
3612 return err;
3613 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3614 if (err < 0)
3615 return err;
3616 return 0;
3617}
3618
3619/* create bound capture volume and switch controls */
3620static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3621 unsigned int vol_ctl, unsigned int sw_ctl)
3622{
3623 struct hda_gen_spec *spec = codec->spec;
3624 struct snd_kcontrol_new *knew;
3625
3626 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003627 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003628 if (!knew)
3629 return -ENOMEM;
3630 knew->index = idx;
3631 knew->private_value = vol_ctl;
3632 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3633 }
3634 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003635 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003636 if (!knew)
3637 return -ENOMEM;
3638 knew->index = idx;
3639 knew->private_value = sw_ctl;
3640 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3641 }
3642 return 0;
3643}
3644
3645/* return the vol ctl when used first in the imux list */
3646static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3647{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003648 struct nid_path *path;
3649 unsigned int ctl;
3650 int i;
3651
Takashi Iwaic697b712013-01-07 17:09:26 +01003652 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003653 if (!path)
3654 return 0;
3655 ctl = path->ctls[type];
3656 if (!ctl)
3657 return 0;
3658 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003659 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003660 if (path && path->ctls[type] == ctl)
3661 return 0;
3662 }
3663 return ctl;
3664}
3665
3666/* create individual capture volume and switch controls per input */
3667static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3668{
3669 struct hda_gen_spec *spec = codec->spec;
3670 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003671 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003672
3673 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003674 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003675 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003676
Takashi Iwaic9700422013-01-18 10:17:30 +01003677 idx = imux->items[i].index;
3678 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003679 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003680 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3681
3682 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003683 err = add_single_cap_ctl(codec,
3684 spec->input_labels[idx],
3685 spec->input_label_idxs[idx],
3686 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003687 get_first_cap_ctl(codec, i, type),
3688 inv_dmic);
3689 if (err < 0)
3690 return err;
3691 }
3692 }
3693 return 0;
3694}
3695
3696static int create_capture_mixers(struct hda_codec *codec)
3697{
3698 struct hda_gen_spec *spec = codec->spec;
3699 struct hda_input_mux *imux = &spec->input_mux;
3700 int i, n, nums, err;
3701
3702 if (spec->dyn_adc_switch)
3703 nums = 1;
3704 else
3705 nums = spec->num_adc_nids;
3706
3707 if (!spec->auto_mic && imux->num_items > 1) {
3708 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003709 const char *name;
3710 name = nums > 1 ? "Input Source" : "Capture Source";
3711 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003712 if (!knew)
3713 return -ENOMEM;
3714 knew->count = nums;
3715 }
3716
3717 for (n = 0; n < nums; n++) {
3718 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003719 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003720 bool inv_dmic = false;
3721 int vol, sw;
3722
3723 vol = sw = 0;
3724 for (i = 0; i < imux->num_items; i++) {
3725 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003726 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003727 if (!path)
3728 continue;
3729 parse_capvol_in_path(codec, path);
3730 if (!vol)
3731 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003732 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003733 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003734 if (!same_amp_caps(codec, vol,
3735 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3736 multi_cap_vol = true;
3737 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003738 if (!sw)
3739 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003740 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003741 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003742 if (!same_amp_caps(codec, sw,
3743 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3744 multi_cap_vol = true;
3745 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003746 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3747 inv_dmic = true;
3748 }
3749
3750 if (!multi)
3751 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3752 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003753 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003754 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3755 else
3756 err = create_multi_cap_vol_ctl(codec);
3757 if (err < 0)
3758 return err;
3759 }
3760
3761 return 0;
3762}
3763
3764/*
3765 * add mic boosts if needed
3766 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003767
3768/* check whether the given amp is feasible as a boost volume */
3769static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3770 int dir, int idx)
3771{
3772 unsigned int step;
3773
3774 if (!nid_has_volume(codec, nid, dir) ||
3775 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3776 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3777 return false;
3778
3779 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3780 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3781 if (step < 0x20)
3782 return false;
3783 return true;
3784}
3785
3786/* look for a boost amp in a widget close to the pin */
3787static unsigned int look_for_boost_amp(struct hda_codec *codec,
3788 struct nid_path *path)
3789{
3790 unsigned int val = 0;
3791 hda_nid_t nid;
3792 int depth;
3793
3794 for (depth = 0; depth < 3; depth++) {
3795 if (depth >= path->depth - 1)
3796 break;
3797 nid = path->path[depth];
3798 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3799 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3800 break;
3801 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3802 path->idx[depth])) {
3803 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3804 HDA_INPUT);
3805 break;
3806 }
3807 }
3808
3809 return val;
3810}
3811
Takashi Iwai352f7f92012-12-19 12:52:06 +01003812static int parse_mic_boost(struct hda_codec *codec)
3813{
3814 struct hda_gen_spec *spec = codec->spec;
3815 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003816 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003817 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003818
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003819 if (!spec->num_adc_nids)
3820 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003821
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003822 for (i = 0; i < imux->num_items; i++) {
3823 struct nid_path *path;
3824 unsigned int val;
3825 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003826 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003827
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003828 idx = imux->items[i].index;
3829 if (idx >= imux->num_items)
3830 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003831
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003832 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003833 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003834 continue;
3835
3836 path = get_input_path(codec, 0, i);
3837 if (!path)
3838 continue;
3839
3840 val = look_for_boost_amp(codec, path);
3841 if (!val)
3842 continue;
3843
3844 /* create a boost control */
3845 snprintf(boost_label, sizeof(boost_label),
3846 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003847 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3848 spec->input_label_idxs[idx], val))
3849 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003850
3851 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003852 }
3853 return 0;
3854}
3855
3856/*
3857 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3858 */
3859static void parse_digital(struct hda_codec *codec)
3860{
3861 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003862 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003863 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003864 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003865
3866 /* support multiple SPDIFs; the secondary is set up as a slave */
3867 nums = 0;
3868 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003869 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003870 dig_nid = look_for_dac(codec, pin, true);
3871 if (!dig_nid)
3872 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003873 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003874 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003875 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003876 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003877 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003878 path->pin_fixed = true; /* no jack detection */
Takashi Iwai196c17662013-01-04 15:01:40 +01003879 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003880 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003881 if (!nums) {
3882 spec->multiout.dig_out_nid = dig_nid;
3883 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3884 } else {
3885 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3886 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd5764222014-05-14 17:18:31 +03003887 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003888 spec->slave_dig_outs[nums - 1] = dig_nid;
3889 }
3890 nums++;
3891 }
3892
3893 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003894 pin = spec->autocfg.dig_in_pin;
Takashi Iwai7639a062015-03-03 10:07:24 +01003895 for_each_hda_codec_node(dig_nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003896 unsigned int wcaps = get_wcaps(codec, dig_nid);
3897 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3898 continue;
3899 if (!(wcaps & AC_WCAP_DIGITAL))
3900 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003901 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003902 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003903 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003904 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003905 path->pin_fixed = true; /* no jack */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003906 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003907 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003908 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003909 break;
3910 }
3911 }
3912 }
3913}
3914
3915
3916/*
3917 * input MUX handling
3918 */
3919
3920static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3921
3922/* select the given imux item; either unmute exclusively or select the route */
3923static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3924 unsigned int idx)
3925{
3926 struct hda_gen_spec *spec = codec->spec;
3927 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003928 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003929
3930 imux = &spec->input_mux;
3931 if (!imux->num_items)
3932 return 0;
3933
3934 if (idx >= imux->num_items)
3935 idx = imux->num_items - 1;
3936 if (spec->cur_mux[adc_idx] == idx)
3937 return 0;
3938
Takashi Iwai55196ff2013-01-24 17:32:56 +01003939 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3940 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003941 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003942 if (old_path->active)
3943 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003944
3945 spec->cur_mux[adc_idx] = idx;
3946
Takashi Iwai967303d2013-02-19 17:12:42 +01003947 if (spec->hp_mic)
3948 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003949
3950 if (spec->dyn_adc_switch)
3951 dyn_adc_pcm_resetup(codec, idx);
3952
Takashi Iwaic697b712013-01-07 17:09:26 +01003953 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003954 if (!path)
3955 return 0;
3956 if (path->active)
3957 return 0;
3958 snd_hda_activate_path(codec, path, true, false);
3959 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003960 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003961 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003962 return 1;
3963}
3964
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003965/* power up/down widgets in the all paths that match with the given NID
3966 * as terminals (either start- or endpoint)
3967 *
3968 * returns the last changed NID, or zero if unchanged.
3969 */
3970static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3971 int pin_state, int stream_state)
3972{
3973 struct hda_gen_spec *spec = codec->spec;
3974 hda_nid_t last, changed = 0;
3975 struct nid_path *path;
3976 int n;
3977
3978 for (n = 0; n < spec->paths.used; n++) {
3979 path = snd_array_elem(&spec->paths, n);
Bob Copeland81e43962016-06-25 07:58:45 -04003980 if (!path->depth)
3981 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003982 if (path->path[0] == nid ||
3983 path->path[path->depth - 1] == nid) {
3984 bool pin_old = path->pin_enabled;
3985 bool stream_old = path->stream_enabled;
3986
3987 if (pin_state >= 0)
3988 path->pin_enabled = pin_state;
3989 if (stream_state >= 0)
3990 path->stream_enabled = stream_state;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003991 if ((!path->pin_fixed && path->pin_enabled != pin_old)
3992 || path->stream_enabled != stream_old) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003993 last = path_power_update(codec, path, true);
3994 if (last)
3995 changed = last;
3996 }
3997 }
3998 }
3999 return changed;
4000}
4001
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004002/* check the jack status for power control */
4003static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4004{
4005 if (!is_jack_detectable(codec, pin))
4006 return true;
4007 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4008}
4009
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004010/* power up/down the paths of the given pin according to the jack state;
4011 * power = 0/1 : only power up/down if it matches with the jack state,
4012 * < 0 : force power up/down to follow the jack sate
4013 *
4014 * returns the last changed NID, or zero if unchanged.
4015 */
4016static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4017 int power)
4018{
4019 bool on;
4020
Takashi Iwai967b1302015-03-20 18:21:03 +01004021 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004022 return 0;
4023
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004024 on = detect_pin_state(codec, pin);
4025
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004026 if (power >= 0 && on != power)
4027 return 0;
4028 return set_path_power(codec, pin, on, -1);
4029}
4030
4031static void pin_power_callback(struct hda_codec *codec,
4032 struct hda_jack_callback *jack,
4033 bool on)
4034{
Takashi Iwai2ebab402016-02-09 10:23:52 +01004035 if (jack && jack->nid)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004036 sync_power_state_change(codec,
Takashi Iwai2ebab402016-02-09 10:23:52 +01004037 set_pin_power_jack(codec, jack->nid, on));
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004038}
4039
4040/* callback only doing power up -- called at first */
4041static void pin_power_up_callback(struct hda_codec *codec,
4042 struct hda_jack_callback *jack)
4043{
4044 pin_power_callback(codec, jack, true);
4045}
4046
4047/* callback only doing power down -- called at last */
4048static void pin_power_down_callback(struct hda_codec *codec,
4049 struct hda_jack_callback *jack)
4050{
4051 pin_power_callback(codec, jack, false);
4052}
4053
4054/* set up the power up/down callbacks */
4055static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4056 const hda_nid_t *pins, bool on)
4057{
4058 int i;
4059 hda_jack_callback_fn cb =
4060 on ? pin_power_up_callback : pin_power_down_callback;
4061
4062 for (i = 0; i < num_pins && pins[i]; i++) {
4063 if (is_jack_detectable(codec, pins[i]))
4064 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4065 else
4066 set_path_power(codec, pins[i], true, -1);
4067 }
4068}
4069
4070/* enabled power callback to each available I/O pin with jack detections;
4071 * the digital I/O pins are excluded because of the unreliable detectsion
4072 */
4073static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4074{
4075 struct hda_gen_spec *spec = codec->spec;
4076 struct auto_pin_cfg *cfg = &spec->autocfg;
4077 int i;
4078
Takashi Iwai967b1302015-03-20 18:21:03 +01004079 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004080 return;
4081 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4082 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4083 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4084 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4085 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4086 for (i = 0; i < cfg->num_inputs; i++)
4087 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4088}
4089
4090/* sync path power up/down with the jack states of given pins */
4091static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4092 const hda_nid_t *pins)
4093{
4094 int i;
4095
4096 for (i = 0; i < num_pins && pins[i]; i++)
4097 if (is_jack_detectable(codec, pins[i]))
4098 set_pin_power_jack(codec, pins[i], -1);
4099}
4100
4101/* sync path power up/down with pins; called at init and resume */
4102static void sync_all_pin_power_ctls(struct hda_codec *codec)
4103{
4104 struct hda_gen_spec *spec = codec->spec;
4105 struct auto_pin_cfg *cfg = &spec->autocfg;
4106 int i;
4107
Takashi Iwai967b1302015-03-20 18:21:03 +01004108 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004109 return;
4110 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4111 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4112 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4113 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4114 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4115 for (i = 0; i < cfg->num_inputs; i++)
4116 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4117}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004118
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004119/* add fake paths if not present yet */
4120static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4121 int num_pins, const hda_nid_t *pins)
4122{
4123 struct hda_gen_spec *spec = codec->spec;
4124 struct nid_path *path;
4125 int i;
4126
4127 for (i = 0; i < num_pins; i++) {
4128 if (!pins[i])
4129 break;
4130 if (get_nid_path(codec, nid, pins[i], 0))
4131 continue;
4132 path = snd_array_new(&spec->paths);
4133 if (!path)
4134 return -ENOMEM;
4135 memset(path, 0, sizeof(*path));
4136 path->depth = 2;
4137 path->path[0] = nid;
4138 path->path[1] = pins[i];
4139 path->active = true;
4140 }
4141 return 0;
4142}
4143
4144/* create fake paths to all outputs from beep */
4145static int add_fake_beep_paths(struct hda_codec *codec)
4146{
4147 struct hda_gen_spec *spec = codec->spec;
4148 struct auto_pin_cfg *cfg = &spec->autocfg;
4149 hda_nid_t nid = spec->beep_nid;
4150 int err;
4151
Takashi Iwai967b1302015-03-20 18:21:03 +01004152 if (!codec->power_save_node || !nid)
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004153 return 0;
4154 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4155 if (err < 0)
4156 return err;
4157 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4158 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4159 if (err < 0)
4160 return err;
4161 }
4162 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4163 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4164 cfg->speaker_pins);
4165 if (err < 0)
4166 return err;
4167 }
4168 return 0;
4169}
4170
4171/* power up/down beep widget and its output paths */
4172static void beep_power_hook(struct hda_beep *beep, bool on)
4173{
4174 set_path_power(beep->codec, beep->nid, -1, on);
4175}
4176
Takashi Iwai6b275b12015-03-20 18:11:05 +01004177/**
4178 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4179 * @codec: the HDA codec
4180 * @pin: NID of pin to fix
4181 */
4182int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4183{
4184 struct hda_gen_spec *spec = codec->spec;
4185 struct nid_path *path;
4186
4187 path = snd_array_new(&spec->paths);
4188 if (!path)
4189 return -ENOMEM;
4190 memset(path, 0, sizeof(*path));
4191 path->depth = 1;
4192 path->path[0] = pin;
4193 path->active = true;
4194 path->pin_fixed = true;
4195 path->stream_enabled = true;
4196 return 0;
4197}
4198EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4199
Takashi Iwai352f7f92012-12-19 12:52:06 +01004200/*
4201 * Jack detections for HP auto-mute and mic-switch
4202 */
4203
4204/* check each pin in the given array; returns true if any of them is plugged */
4205static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4206{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004207 int i;
4208 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004209
4210 for (i = 0; i < num_pins; i++) {
4211 hda_nid_t nid = pins[i];
4212 if (!nid)
4213 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01004214 /* don't detect pins retasked as inputs */
4215 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4216 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004217 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4218 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004219 }
4220 return present;
4221}
4222
4223/* standard HP/line-out auto-mute helper */
4224static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004225 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004226{
4227 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004228 int i;
4229
4230 for (i = 0; i < num_pins; i++) {
4231 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01004232 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004233 if (!nid)
4234 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004235
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004236 oldval = snd_hda_codec_get_pin_target(codec, nid);
4237 if (oldval & PIN_IN)
4238 continue; /* no mute for inputs */
4239
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004240 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004241 struct nid_path *path;
4242 hda_nid_t mute_nid;
4243
4244 path = snd_hda_get_path_from_idx(codec, paths[i]);
4245 if (!path)
4246 continue;
4247 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4248 if (!mute_nid)
4249 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004250 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004251 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004252 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004253 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004254 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004255 } else {
4256 /* don't reset VREF value in case it's controlling
4257 * the amp (see alc861_fixup_asus_amp_vref_0f())
4258 */
4259 if (spec->keep_vref_in_automute)
4260 val = oldval & ~PIN_HP;
4261 else
4262 val = 0;
4263 if (!mute)
4264 val |= oldval;
4265 /* here we call update_pin_ctl() so that the pinctl is
4266 * changed without changing the pinctl target value;
4267 * the original target value will be still referred at
4268 * the init / resume again
4269 */
4270 update_pin_ctl(codec, nid, val);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004271 }
4272
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01004273 set_pin_eapd(codec, nid, !mute);
Takashi Iwai967b1302015-03-20 18:21:03 +01004274 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004275 bool on = !mute;
4276 if (on)
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004277 on = detect_pin_state(codec, nid);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004278 set_path_power(codec, nid, on, -1);
4279 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004280 }
4281}
4282
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004283/**
4284 * snd_hda_gen_update_outputs - Toggle outputs muting
4285 * @codec: the HDA codec
4286 *
4287 * Update the mute status of all outputs based on the current jack states.
4288 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004289void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004290{
4291 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004292 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004293 int on;
4294
4295 /* Control HP pins/amps depending on master_mute state;
4296 * in general, HP pins/amps control should be enabled in all cases,
4297 * but currently set only for master_mute, just to be safe
4298 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004299 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4300 paths = spec->out_paths;
4301 else
4302 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01004303 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004304 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004305
4306 if (!spec->automute_speaker)
4307 on = 0;
4308 else
4309 on = spec->hp_jack_present | spec->line_jack_present;
4310 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004311 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004312 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4313 paths = spec->out_paths;
4314 else
4315 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004316 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004317 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004318
4319 /* toggle line-out mutes if needed, too */
4320 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4321 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4322 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4323 return;
4324 if (!spec->automute_lo)
4325 on = 0;
4326 else
4327 on = spec->hp_jack_present;
4328 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004329 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004330 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004331 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004332 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004333}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004334EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004335
4336static void call_update_outputs(struct hda_codec *codec)
4337{
4338 struct hda_gen_spec *spec = codec->spec;
4339 if (spec->automute_hook)
4340 spec->automute_hook(codec);
4341 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004342 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004343
4344 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4345 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4346 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004347}
4348
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004349/**
4350 * snd_hda_gen_hp_automute - standard HP-automute helper
4351 * @codec: the HDA codec
4352 * @jack: jack object, NULL for the whole
4353 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004354void snd_hda_gen_hp_automute(struct hda_codec *codec,
4355 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004356{
4357 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004358 hda_nid_t *pins = spec->autocfg.hp_pins;
4359 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004360
Takashi Iwai92603c52013-01-22 07:46:31 +01004361 /* No detection for the first HP jack during indep-HP mode */
4362 if (spec->indep_hp_enabled) {
4363 pins++;
4364 num_pins--;
4365 }
4366
4367 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004368 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4369 return;
4370 call_update_outputs(codec);
4371}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004372EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004373
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004374/**
4375 * snd_hda_gen_line_automute - standard line-out-automute helper
4376 * @codec: the HDA codec
4377 * @jack: jack object, NULL for the whole
4378 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004379void snd_hda_gen_line_automute(struct hda_codec *codec,
4380 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004381{
4382 struct hda_gen_spec *spec = codec->spec;
4383
4384 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4385 return;
4386 /* check LO jack only when it's different from HP */
4387 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4388 return;
4389
4390 spec->line_jack_present =
4391 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4392 spec->autocfg.line_out_pins);
4393 if (!spec->automute_speaker || !spec->detect_lo)
4394 return;
4395 call_update_outputs(codec);
4396}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004397EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004398
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004399/**
4400 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4401 * @codec: the HDA codec
4402 * @jack: jack object, NULL for the whole
4403 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004404void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4405 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004406{
4407 struct hda_gen_spec *spec = codec->spec;
4408 int i;
4409
4410 if (!spec->auto_mic)
4411 return;
4412
4413 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004414 hda_nid_t pin = spec->am_entry[i].pin;
4415 /* don't detect pins retasked as outputs */
4416 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4417 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004418 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004419 mux_select(codec, 0, spec->am_entry[i].idx);
4420 return;
4421 }
4422 }
4423 mux_select(codec, 0, spec->am_entry[0].idx);
4424}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004425EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004426
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004427/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004428static void call_hp_automute(struct hda_codec *codec,
4429 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004430{
4431 struct hda_gen_spec *spec = codec->spec;
4432 if (spec->hp_automute_hook)
4433 spec->hp_automute_hook(codec, jack);
4434 else
4435 snd_hda_gen_hp_automute(codec, jack);
4436}
4437
4438static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004439 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004440{
4441 struct hda_gen_spec *spec = codec->spec;
4442 if (spec->line_automute_hook)
4443 spec->line_automute_hook(codec, jack);
4444 else
4445 snd_hda_gen_line_automute(codec, jack);
4446}
4447
4448static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004449 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004450{
4451 struct hda_gen_spec *spec = codec->spec;
4452 if (spec->mic_autoswitch_hook)
4453 spec->mic_autoswitch_hook(codec, jack);
4454 else
4455 snd_hda_gen_mic_autoswitch(codec, jack);
4456}
4457
Takashi Iwai963afde2013-05-31 15:20:31 +02004458/* update jack retasking */
4459static void update_automute_all(struct hda_codec *codec)
4460{
4461 call_hp_automute(codec, NULL);
4462 call_line_automute(codec, NULL);
4463 call_mic_autoswitch(codec, NULL);
4464}
4465
Takashi Iwai352f7f92012-12-19 12:52:06 +01004466/*
4467 * Auto-Mute mode mixer enum support
4468 */
4469static int automute_mode_info(struct snd_kcontrol *kcontrol,
4470 struct snd_ctl_elem_info *uinfo)
4471{
4472 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4473 struct hda_gen_spec *spec = codec->spec;
4474 static const char * const texts3[] = {
4475 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004476 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Takashi Iwai352f7f92012-12-19 12:52:06 +01004478 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4479 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4480 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4481}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Takashi Iwai352f7f92012-12-19 12:52:06 +01004483static int automute_mode_get(struct snd_kcontrol *kcontrol,
4484 struct snd_ctl_elem_value *ucontrol)
4485{
4486 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4487 struct hda_gen_spec *spec = codec->spec;
4488 unsigned int val = 0;
4489 if (spec->automute_speaker)
4490 val++;
4491 if (spec->automute_lo)
4492 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004493
Takashi Iwai352f7f92012-12-19 12:52:06 +01004494 ucontrol->value.enumerated.item[0] = val;
4495 return 0;
4496}
4497
4498static int automute_mode_put(struct snd_kcontrol *kcontrol,
4499 struct snd_ctl_elem_value *ucontrol)
4500{
4501 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4502 struct hda_gen_spec *spec = codec->spec;
4503
4504 switch (ucontrol->value.enumerated.item[0]) {
4505 case 0:
4506 if (!spec->automute_speaker && !spec->automute_lo)
4507 return 0;
4508 spec->automute_speaker = 0;
4509 spec->automute_lo = 0;
4510 break;
4511 case 1:
4512 if (spec->automute_speaker_possible) {
4513 if (!spec->automute_lo && spec->automute_speaker)
4514 return 0;
4515 spec->automute_speaker = 1;
4516 spec->automute_lo = 0;
4517 } else if (spec->automute_lo_possible) {
4518 if (spec->automute_lo)
4519 return 0;
4520 spec->automute_lo = 1;
4521 } else
4522 return -EINVAL;
4523 break;
4524 case 2:
4525 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4526 return -EINVAL;
4527 if (spec->automute_speaker && spec->automute_lo)
4528 return 0;
4529 spec->automute_speaker = 1;
4530 spec->automute_lo = 1;
4531 break;
4532 default:
4533 return -EINVAL;
4534 }
4535 call_update_outputs(codec);
4536 return 1;
4537}
4538
4539static const struct snd_kcontrol_new automute_mode_enum = {
4540 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4541 .name = "Auto-Mute Mode",
4542 .info = automute_mode_info,
4543 .get = automute_mode_get,
4544 .put = automute_mode_put,
4545};
4546
4547static int add_automute_mode_enum(struct hda_codec *codec)
4548{
4549 struct hda_gen_spec *spec = codec->spec;
4550
Takashi Iwai12c93df2012-12-19 14:38:33 +01004551 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004552 return -ENOMEM;
4553 return 0;
4554}
4555
4556/*
4557 * Check the availability of HP/line-out auto-mute;
4558 * Set up appropriately if really supported
4559 */
4560static int check_auto_mute_availability(struct hda_codec *codec)
4561{
4562 struct hda_gen_spec *spec = codec->spec;
4563 struct auto_pin_cfg *cfg = &spec->autocfg;
4564 int present = 0;
4565 int i, err;
4566
Takashi Iwaif72706b2013-01-16 18:20:07 +01004567 if (spec->suppress_auto_mute)
4568 return 0;
4569
Takashi Iwai352f7f92012-12-19 12:52:06 +01004570 if (cfg->hp_pins[0])
4571 present++;
4572 if (cfg->line_out_pins[0])
4573 present++;
4574 if (cfg->speaker_pins[0])
4575 present++;
4576 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004577 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004578
4579 if (!cfg->speaker_pins[0] &&
4580 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4581 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4582 sizeof(cfg->speaker_pins));
4583 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Takashi Iwai352f7f92012-12-19 12:52:06 +01004586 if (!cfg->hp_pins[0] &&
4587 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4588 memcpy(cfg->hp_pins, cfg->line_out_pins,
4589 sizeof(cfg->hp_pins));
4590 cfg->hp_outs = cfg->line_outs;
4591 }
4592
4593 for (i = 0; i < cfg->hp_outs; i++) {
4594 hda_nid_t nid = cfg->hp_pins[i];
4595 if (!is_jack_detectable(codec, nid))
4596 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004597 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004598 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004599 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004600 spec->detect_hp = 1;
4601 }
4602
4603 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4604 if (cfg->speaker_outs)
4605 for (i = 0; i < cfg->line_outs; i++) {
4606 hda_nid_t nid = cfg->line_out_pins[i];
4607 if (!is_jack_detectable(codec, nid))
4608 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004609 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004610 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004611 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004612 spec->detect_lo = 1;
4613 }
4614 spec->automute_lo_possible = spec->detect_hp;
4615 }
4616
4617 spec->automute_speaker_possible = cfg->speaker_outs &&
4618 (spec->detect_hp || spec->detect_lo);
4619
4620 spec->automute_lo = spec->automute_lo_possible;
4621 spec->automute_speaker = spec->automute_speaker_possible;
4622
4623 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4624 /* create a control for automute mode */
4625 err = add_automute_mode_enum(codec);
4626 if (err < 0)
4627 return err;
4628 }
4629 return 0;
4630}
4631
Takashi Iwai352f7f92012-12-19 12:52:06 +01004632/* check whether all auto-mic pins are valid; setup indices if OK */
4633static bool auto_mic_check_imux(struct hda_codec *codec)
4634{
4635 struct hda_gen_spec *spec = codec->spec;
4636 const struct hda_input_mux *imux;
4637 int i;
4638
4639 imux = &spec->input_mux;
4640 for (i = 0; i < spec->am_num_entries; i++) {
4641 spec->am_entry[i].idx =
4642 find_idx_in_nid_list(spec->am_entry[i].pin,
4643 spec->imux_pins, imux->num_items);
4644 if (spec->am_entry[i].idx < 0)
4645 return false; /* no corresponding imux */
4646 }
4647
4648 /* we don't need the jack detection for the first pin */
4649 for (i = 1; i < spec->am_num_entries; i++)
4650 snd_hda_jack_detect_enable_callback(codec,
4651 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004652 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004653 return true;
4654}
4655
4656static int compare_attr(const void *ap, const void *bp)
4657{
4658 const struct automic_entry *a = ap;
4659 const struct automic_entry *b = bp;
4660 return (int)(a->attr - b->attr);
4661}
4662
4663/*
4664 * Check the availability of auto-mic switch;
4665 * Set up if really supported
4666 */
4667static int check_auto_mic_availability(struct hda_codec *codec)
4668{
4669 struct hda_gen_spec *spec = codec->spec;
4670 struct auto_pin_cfg *cfg = &spec->autocfg;
4671 unsigned int types;
4672 int i, num_pins;
4673
Takashi Iwaid12daf62013-01-07 16:32:11 +01004674 if (spec->suppress_auto_mic)
4675 return 0;
4676
Takashi Iwai352f7f92012-12-19 12:52:06 +01004677 types = 0;
4678 num_pins = 0;
4679 for (i = 0; i < cfg->num_inputs; i++) {
4680 hda_nid_t nid = cfg->inputs[i].pin;
4681 unsigned int attr;
4682 attr = snd_hda_codec_get_pincfg(codec, nid);
4683 attr = snd_hda_get_input_pin_attr(attr);
4684 if (types & (1 << attr))
4685 return 0; /* already occupied */
4686 switch (attr) {
4687 case INPUT_PIN_ATTR_INT:
4688 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4689 return 0; /* invalid type */
4690 break;
4691 case INPUT_PIN_ATTR_UNUSED:
4692 return 0; /* invalid entry */
4693 default:
4694 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4695 return 0; /* invalid type */
4696 if (!spec->line_in_auto_switch &&
4697 cfg->inputs[i].type != AUTO_PIN_MIC)
4698 return 0; /* only mic is allowed */
4699 if (!is_jack_detectable(codec, nid))
4700 return 0; /* no unsol support */
4701 break;
4702 }
4703 if (num_pins >= MAX_AUTO_MIC_PINS)
4704 return 0;
4705 types |= (1 << attr);
4706 spec->am_entry[num_pins].pin = nid;
4707 spec->am_entry[num_pins].attr = attr;
4708 num_pins++;
4709 }
4710
4711 if (num_pins < 2)
4712 return 0;
4713
4714 spec->am_num_entries = num_pins;
4715 /* sort the am_entry in the order of attr so that the pin with a
4716 * higher attr will be selected when the jack is plugged.
4717 */
4718 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4719 compare_attr, NULL);
4720
4721 if (!auto_mic_check_imux(codec))
4722 return 0;
4723
4724 spec->auto_mic = 1;
4725 spec->num_adc_nids = 1;
4726 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004727 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004728 spec->am_entry[0].pin,
4729 spec->am_entry[1].pin,
4730 spec->am_entry[2].pin);
4731
4732 return 0;
4733}
4734
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004735/**
4736 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4737 * into power down
4738 * @codec: the HDA codec
4739 * @nid: NID to evalute
4740 * @power_state: target power state
4741 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004742unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004743 hda_nid_t nid,
4744 unsigned int power_state)
4745{
Takashi Iwaib6c09b32015-04-09 10:25:03 +02004746 struct hda_gen_spec *spec = codec->spec;
4747
4748 if (!spec->power_down_unused && !codec->power_save_node)
4749 return power_state;
Takashi Iwai7639a062015-03-03 10:07:24 +01004750 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004751 return power_state;
4752 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4753 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004754 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004755 return power_state;
4756 return AC_PWRST_D3;
4757}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004758EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004759
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004760/* mute all aamix inputs initially; parse up to the first leaves */
4761static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4762{
4763 int i, nums;
4764 const hda_nid_t *conn;
4765 bool has_amp;
4766
4767 nums = snd_hda_get_conn_list(codec, mix, &conn);
4768 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4769 for (i = 0; i < nums; i++) {
4770 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004771 update_amp(codec, mix, HDA_INPUT, i,
4772 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004773 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004774 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4775 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004776 }
4777}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004778
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004779/**
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004780 * snd_hda_gen_stream_pm - Stream power management callback
4781 * @codec: the HDA codec
4782 * @nid: audio widget
4783 * @on: power on/off flag
4784 *
Takashi Iwai967b1302015-03-20 18:21:03 +01004785 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004786 */
4787void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4788{
Takashi Iwai967b1302015-03-20 18:21:03 +01004789 if (codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004790 set_path_power(codec, nid, -1, on);
4791}
4792EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4793
4794/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004795 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4796 * set up the hda_gen_spec
4797 * @codec: the HDA codec
4798 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004799 *
4800 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004801 * or a negative error code
4802 */
4803int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004804 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004805{
4806 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004807 int err;
4808
Takashi Iwai1c70a582013-01-11 17:48:22 +01004809 parse_user_hints(codec);
4810
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004811 if (spec->mixer_nid && !spec->mixer_merge_nid)
4812 spec->mixer_merge_nid = spec->mixer_nid;
4813
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004814 if (cfg != &spec->autocfg) {
4815 spec->autocfg = *cfg;
4816 cfg = &spec->autocfg;
4817 }
4818
Takashi Iwai98bd1112013-03-22 14:53:50 +01004819 if (!spec->main_out_badness)
4820 spec->main_out_badness = &hda_main_out_badness;
4821 if (!spec->extra_out_badness)
4822 spec->extra_out_badness = &hda_extra_out_badness;
4823
David Henningsson6fc4cb92013-01-16 15:58:45 +01004824 fill_all_dac_nids(codec);
4825
Takashi Iwai352f7f92012-12-19 12:52:06 +01004826 if (!cfg->line_outs) {
4827 if (cfg->dig_outs || cfg->dig_in_pin) {
4828 spec->multiout.max_channels = 2;
4829 spec->no_analog = 1;
4830 goto dig_only;
4831 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004832 if (!cfg->num_inputs && !cfg->dig_in_pin)
4833 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004834 }
4835
4836 if (!spec->no_primary_hp &&
4837 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4838 cfg->line_outs <= cfg->hp_outs) {
4839 /* use HP as primary out */
4840 cfg->speaker_outs = cfg->line_outs;
4841 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4842 sizeof(cfg->speaker_pins));
4843 cfg->line_outs = cfg->hp_outs;
4844 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4845 cfg->hp_outs = 0;
4846 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4847 cfg->line_out_type = AUTO_PIN_HP_OUT;
4848 }
4849
4850 err = parse_output_paths(codec);
4851 if (err < 0)
4852 return err;
4853 err = create_multi_channel_mode(codec);
4854 if (err < 0)
4855 return err;
4856 err = create_multi_out_ctls(codec, cfg);
4857 if (err < 0)
4858 return err;
4859 err = create_hp_out_ctls(codec);
4860 if (err < 0)
4861 return err;
4862 err = create_speaker_out_ctls(codec);
4863 if (err < 0)
4864 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004865 err = create_indep_hp_ctls(codec);
4866 if (err < 0)
4867 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004868 err = create_loopback_mixing_ctl(codec);
4869 if (err < 0)
4870 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004871 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004872 if (err < 0)
4873 return err;
4874 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004875 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004876 return err;
4877
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004878 /* add power-down pin callbacks at first */
4879 add_all_pin_power_ctls(codec, false);
4880
Takashi Iwaia07a9492013-01-07 16:44:06 +01004881 spec->const_channel_count = spec->ext_channel_count;
4882 /* check the multiple speaker and headphone pins */
4883 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4884 spec->const_channel_count = max(spec->const_channel_count,
4885 cfg->speaker_outs * 2);
4886 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4887 spec->const_channel_count = max(spec->const_channel_count,
4888 cfg->hp_outs * 2);
4889 spec->multiout.max_channels = max(spec->ext_channel_count,
4890 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004891
4892 err = check_auto_mute_availability(codec);
4893 if (err < 0)
4894 return err;
4895
4896 err = check_dyn_adc_switch(codec);
4897 if (err < 0)
4898 return err;
4899
Takashi Iwai967303d2013-02-19 17:12:42 +01004900 err = check_auto_mic_availability(codec);
4901 if (err < 0)
4902 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004903
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004904 /* add stereo mix if available and not enabled yet */
4905 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01004906 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4907 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004908 err = parse_capture_source(codec, spec->mixer_nid,
4909 CFG_IDX_MIX, spec->num_all_adcs,
4910 "Stereo Mix", 0);
4911 if (err < 0)
4912 return err;
4913 }
4914
4915
Takashi Iwai352f7f92012-12-19 12:52:06 +01004916 err = create_capture_mixers(codec);
4917 if (err < 0)
4918 return err;
4919
4920 err = parse_mic_boost(codec);
4921 if (err < 0)
4922 return err;
4923
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004924 /* create "Headphone Mic Jack Mode" if no input selection is
4925 * available (or user specifies add_jack_modes hint)
4926 */
4927 if (spec->hp_mic_pin &&
4928 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4929 spec->add_jack_modes)) {
4930 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4931 if (err < 0)
4932 return err;
4933 }
4934
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004935 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004936 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4937 err = create_out_jack_modes(codec, cfg->line_outs,
4938 cfg->line_out_pins);
4939 if (err < 0)
4940 return err;
4941 }
4942 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4943 err = create_out_jack_modes(codec, cfg->hp_outs,
4944 cfg->hp_pins);
4945 if (err < 0)
4946 return err;
4947 }
4948 }
4949
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004950 /* add power-up pin callbacks at last */
4951 add_all_pin_power_ctls(codec, true);
4952
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004953 /* mute all aamix input initially */
4954 if (spec->mixer_nid)
4955 mute_all_mixer_nid(codec, spec->mixer_nid);
4956
Takashi Iwai352f7f92012-12-19 12:52:06 +01004957 dig_only:
4958 parse_digital(codec);
4959
Takashi Iwai49fb1892015-05-27 08:37:19 +02004960 if (spec->power_down_unused || codec->power_save_node) {
Takashi Iwai24fef902015-04-09 10:28:15 +02004961 if (!codec->power_filter)
4962 codec->power_filter = snd_hda_gen_path_power_filter;
Takashi Iwai49fb1892015-05-27 08:37:19 +02004963 if (!codec->patch_ops.stream_pm)
4964 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
4965 }
Takashi Iwai55196ff2013-01-24 17:32:56 +01004966
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004967 if (!spec->no_analog && spec->beep_nid) {
4968 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4969 if (err < 0)
4970 return err;
Takashi Iwai967b1302015-03-20 18:21:03 +01004971 if (codec->beep && codec->power_save_node) {
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004972 err = add_fake_beep_paths(codec);
4973 if (err < 0)
4974 return err;
4975 codec->beep->power_hook = beep_power_hook;
4976 }
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004977 }
4978
Takashi Iwai352f7f92012-12-19 12:52:06 +01004979 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004981EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
4983
4984/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004985 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004987
4988/* slave controls for virtual master */
4989static const char * const slave_pfxs[] = {
4990 "Front", "Surround", "Center", "LFE", "Side",
4991 "Headphone", "Speaker", "Mono", "Line Out",
4992 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004993 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4994 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02004995 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004996 NULL,
4997};
4998
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004999/**
5000 * snd_hda_gen_build_controls - Build controls from the parsed results
5001 * @codec: the HDA codec
5002 *
5003 * Pass this to build_controls patch_ops.
5004 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005005int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005007 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
Takashi Iwai36502d02012-12-19 15:15:10 +01005010 if (spec->kctls.used) {
5011 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5012 if (err < 0)
5013 return err;
5014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
Takashi Iwai352f7f92012-12-19 12:52:06 +01005016 if (spec->multiout.dig_out_nid) {
5017 err = snd_hda_create_dig_out_ctls(codec,
5018 spec->multiout.dig_out_nid,
5019 spec->multiout.dig_out_nid,
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005020 spec->pcm_rec[1]->pcm_type);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005021 if (err < 0)
5022 return err;
5023 if (!spec->no_analog) {
5024 err = snd_hda_create_spdif_share_sw(codec,
5025 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 if (err < 0)
5027 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005028 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 }
5030 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005031 if (spec->dig_in_nid) {
5032 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5033 if (err < 0)
5034 return err;
5035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036
Takashi Iwai352f7f92012-12-19 12:52:06 +01005037 /* if we have no master control, let's create it */
5038 if (!spec->no_analog &&
5039 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01005040 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01005041 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005042 "Playback Volume");
5043 if (err < 0)
5044 return err;
5045 }
5046 if (!spec->no_analog &&
5047 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5048 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5049 NULL, slave_pfxs,
5050 "Playback Switch",
5051 true, &spec->vmaster_mute.sw_kctl);
5052 if (err < 0)
5053 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02005054 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01005055 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5056 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02005057 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5058 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
Takashi Iwai352f7f92012-12-19 12:52:06 +01005061 free_kctls(spec); /* no longer needed */
5062
Takashi Iwai352f7f92012-12-19 12:52:06 +01005063 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5064 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 return err;
5066
5067 return 0;
5068}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005069EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005070
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071
5072/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005073 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005076static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5077 struct hda_codec *codec,
5078 struct snd_pcm_substream *substream,
5079 int action)
5080{
5081 struct hda_gen_spec *spec = codec->spec;
5082 if (spec->pcm_playback_hook)
5083 spec->pcm_playback_hook(hinfo, codec, substream, action);
5084}
5085
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005086static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5087 struct hda_codec *codec,
5088 struct snd_pcm_substream *substream,
5089 int action)
5090{
5091 struct hda_gen_spec *spec = codec->spec;
5092 if (spec->pcm_capture_hook)
5093 spec->pcm_capture_hook(hinfo, codec, substream, action);
5094}
5095
Takashi Iwai352f7f92012-12-19 12:52:06 +01005096/*
5097 * Analog playback callbacks
5098 */
5099static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5100 struct hda_codec *codec,
5101 struct snd_pcm_substream *substream)
5102{
5103 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005104 int err;
5105
5106 mutex_lock(&spec->pcm_mutex);
5107 err = snd_hda_multi_out_analog_open(codec,
5108 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005109 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005110 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005111 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005112 call_pcm_playback_hook(hinfo, codec, substream,
5113 HDA_GEN_PCM_ACT_OPEN);
5114 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005115 mutex_unlock(&spec->pcm_mutex);
5116 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005117}
5118
5119static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01005120 struct hda_codec *codec,
5121 unsigned int stream_tag,
5122 unsigned int format,
5123 struct snd_pcm_substream *substream)
5124{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005125 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005126 int err;
5127
5128 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5129 stream_tag, format, substream);
5130 if (!err)
5131 call_pcm_playback_hook(hinfo, codec, substream,
5132 HDA_GEN_PCM_ACT_PREPARE);
5133 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005134}
Takashi Iwai97ec5582006-03-21 11:29:07 +01005135
Takashi Iwai352f7f92012-12-19 12:52:06 +01005136static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5137 struct hda_codec *codec,
5138 struct snd_pcm_substream *substream)
5139{
5140 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005141 int err;
5142
5143 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5144 if (!err)
5145 call_pcm_playback_hook(hinfo, codec, substream,
5146 HDA_GEN_PCM_ACT_CLEANUP);
5147 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005148}
5149
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005150static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5151 struct hda_codec *codec,
5152 struct snd_pcm_substream *substream)
5153{
5154 struct hda_gen_spec *spec = codec->spec;
5155 mutex_lock(&spec->pcm_mutex);
5156 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005157 call_pcm_playback_hook(hinfo, codec, substream,
5158 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005159 mutex_unlock(&spec->pcm_mutex);
5160 return 0;
5161}
5162
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005163static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5164 struct hda_codec *codec,
5165 struct snd_pcm_substream *substream)
5166{
5167 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5168 return 0;
5169}
5170
5171static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5172 struct hda_codec *codec,
5173 unsigned int stream_tag,
5174 unsigned int format,
5175 struct snd_pcm_substream *substream)
5176{
5177 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5178 call_pcm_capture_hook(hinfo, codec, substream,
5179 HDA_GEN_PCM_ACT_PREPARE);
5180 return 0;
5181}
5182
5183static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5184 struct hda_codec *codec,
5185 struct snd_pcm_substream *substream)
5186{
5187 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5188 call_pcm_capture_hook(hinfo, codec, substream,
5189 HDA_GEN_PCM_ACT_CLEANUP);
5190 return 0;
5191}
5192
5193static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5194 struct hda_codec *codec,
5195 struct snd_pcm_substream *substream)
5196{
5197 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5198 return 0;
5199}
5200
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005201static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5202 struct hda_codec *codec,
5203 struct snd_pcm_substream *substream)
5204{
5205 struct hda_gen_spec *spec = codec->spec;
5206 int err = 0;
5207
5208 mutex_lock(&spec->pcm_mutex);
Takashi Iwaid1f15e02015-07-08 09:22:10 +02005209 if (spec->indep_hp && !spec->indep_hp_enabled)
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005210 err = -EBUSY;
5211 else
5212 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005213 call_pcm_playback_hook(hinfo, codec, substream,
5214 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005215 mutex_unlock(&spec->pcm_mutex);
5216 return err;
5217}
5218
5219static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5220 struct hda_codec *codec,
5221 struct snd_pcm_substream *substream)
5222{
5223 struct hda_gen_spec *spec = codec->spec;
5224 mutex_lock(&spec->pcm_mutex);
5225 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005226 call_pcm_playback_hook(hinfo, codec, substream,
5227 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005228 mutex_unlock(&spec->pcm_mutex);
5229 return 0;
5230}
5231
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005232static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5233 struct hda_codec *codec,
5234 unsigned int stream_tag,
5235 unsigned int format,
5236 struct snd_pcm_substream *substream)
5237{
5238 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5239 call_pcm_playback_hook(hinfo, codec, substream,
5240 HDA_GEN_PCM_ACT_PREPARE);
5241 return 0;
5242}
5243
5244static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5245 struct hda_codec *codec,
5246 struct snd_pcm_substream *substream)
5247{
5248 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5249 call_pcm_playback_hook(hinfo, codec, substream,
5250 HDA_GEN_PCM_ACT_CLEANUP);
5251 return 0;
5252}
5253
Takashi Iwai352f7f92012-12-19 12:52:06 +01005254/*
5255 * Digital out
5256 */
5257static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5258 struct hda_codec *codec,
5259 struct snd_pcm_substream *substream)
5260{
5261 struct hda_gen_spec *spec = codec->spec;
5262 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5263}
5264
5265static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5266 struct hda_codec *codec,
5267 unsigned int stream_tag,
5268 unsigned int format,
5269 struct snd_pcm_substream *substream)
5270{
5271 struct hda_gen_spec *spec = codec->spec;
5272 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5273 stream_tag, format, substream);
5274}
5275
5276static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5277 struct hda_codec *codec,
5278 struct snd_pcm_substream *substream)
5279{
5280 struct hda_gen_spec *spec = codec->spec;
5281 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5282}
5283
5284static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5285 struct hda_codec *codec,
5286 struct snd_pcm_substream *substream)
5287{
5288 struct hda_gen_spec *spec = codec->spec;
5289 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5290}
5291
5292/*
5293 * Analog capture
5294 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005295#define alt_capture_pcm_open capture_pcm_open
5296#define alt_capture_pcm_close capture_pcm_close
5297
Takashi Iwai352f7f92012-12-19 12:52:06 +01005298static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5299 struct hda_codec *codec,
5300 unsigned int stream_tag,
5301 unsigned int format,
5302 struct snd_pcm_substream *substream)
5303{
5304 struct hda_gen_spec *spec = codec->spec;
5305
5306 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01005307 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005308 call_pcm_capture_hook(hinfo, codec, substream,
5309 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005310 return 0;
5311}
5312
Takashi Iwai352f7f92012-12-19 12:52:06 +01005313static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5314 struct hda_codec *codec,
5315 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01005316{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005317 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01005318
Takashi Iwai352f7f92012-12-19 12:52:06 +01005319 snd_hda_codec_cleanup_stream(codec,
5320 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005321 call_pcm_capture_hook(hinfo, codec, substream,
5322 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005323 return 0;
5324}
5325
Takashi Iwai352f7f92012-12-19 12:52:06 +01005326/*
5327 */
5328static const struct hda_pcm_stream pcm_analog_playback = {
5329 .substreams = 1,
5330 .channels_min = 2,
5331 .channels_max = 8,
5332 /* NID is set in build_pcms */
5333 .ops = {
5334 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005335 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005336 .prepare = playback_pcm_prepare,
5337 .cleanup = playback_pcm_cleanup
5338 },
5339};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
Takashi Iwai352f7f92012-12-19 12:52:06 +01005341static const struct hda_pcm_stream pcm_analog_capture = {
5342 .substreams = 1,
5343 .channels_min = 2,
5344 .channels_max = 2,
5345 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005346 .ops = {
5347 .open = capture_pcm_open,
5348 .close = capture_pcm_close,
5349 .prepare = capture_pcm_prepare,
5350 .cleanup = capture_pcm_cleanup
5351 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005352};
5353
5354static const struct hda_pcm_stream pcm_analog_alt_playback = {
5355 .substreams = 1,
5356 .channels_min = 2,
5357 .channels_max = 2,
5358 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005359 .ops = {
5360 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005361 .close = alt_playback_pcm_close,
5362 .prepare = alt_playback_pcm_prepare,
5363 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005364 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005365};
5366
5367static const struct hda_pcm_stream pcm_analog_alt_capture = {
5368 .substreams = 2, /* can be overridden */
5369 .channels_min = 2,
5370 .channels_max = 2,
5371 /* NID is set in build_pcms */
5372 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005373 .open = alt_capture_pcm_open,
5374 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005375 .prepare = alt_capture_pcm_prepare,
5376 .cleanup = alt_capture_pcm_cleanup
5377 },
5378};
5379
5380static const struct hda_pcm_stream pcm_digital_playback = {
5381 .substreams = 1,
5382 .channels_min = 2,
5383 .channels_max = 2,
5384 /* NID is set in build_pcms */
5385 .ops = {
5386 .open = dig_playback_pcm_open,
5387 .close = dig_playback_pcm_close,
5388 .prepare = dig_playback_pcm_prepare,
5389 .cleanup = dig_playback_pcm_cleanup
5390 },
5391};
5392
5393static const struct hda_pcm_stream pcm_digital_capture = {
5394 .substreams = 1,
5395 .channels_min = 2,
5396 .channels_max = 2,
5397 /* NID is set in build_pcms */
5398};
5399
5400/* Used by build_pcms to flag that a PCM has no playback stream */
5401static const struct hda_pcm_stream pcm_null_stream = {
5402 .substreams = 0,
5403 .channels_min = 0,
5404 .channels_max = 0,
5405};
5406
5407/*
5408 * dynamic changing ADC PCM streams
5409 */
5410static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5411{
5412 struct hda_gen_spec *spec = codec->spec;
5413 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5414
5415 if (spec->cur_adc && spec->cur_adc != new_adc) {
5416 /* stream is running, let's swap the current ADC */
5417 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5418 spec->cur_adc = new_adc;
5419 snd_hda_codec_setup_stream(codec, new_adc,
5420 spec->cur_adc_stream_tag, 0,
5421 spec->cur_adc_format);
5422 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005424 return false;
5425}
5426
5427/* analog capture with dynamic dual-adc changes */
5428static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5429 struct hda_codec *codec,
5430 unsigned int stream_tag,
5431 unsigned int format,
5432 struct snd_pcm_substream *substream)
5433{
5434 struct hda_gen_spec *spec = codec->spec;
5435 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5436 spec->cur_adc_stream_tag = stream_tag;
5437 spec->cur_adc_format = format;
5438 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
Takashi Iwai4f29efc2016-04-12 15:16:24 +02005439 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005440 return 0;
5441}
5442
5443static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5444 struct hda_codec *codec,
5445 struct snd_pcm_substream *substream)
5446{
5447 struct hda_gen_spec *spec = codec->spec;
5448 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5449 spec->cur_adc = 0;
Takashi Iwai4f29efc2016-04-12 15:16:24 +02005450 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005451 return 0;
5452}
5453
5454static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5455 .substreams = 1,
5456 .channels_min = 2,
5457 .channels_max = 2,
5458 .nid = 0, /* fill later */
5459 .ops = {
5460 .prepare = dyn_adc_capture_pcm_prepare,
5461 .cleanup = dyn_adc_capture_pcm_cleanup
5462 },
5463};
5464
Takashi Iwaif873e532012-12-20 16:58:39 +01005465static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5466 const char *chip_name)
5467{
5468 char *p;
5469
5470 if (*str)
5471 return;
5472 strlcpy(str, chip_name, len);
5473
5474 /* drop non-alnum chars after a space */
5475 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5476 if (!isalnum(p[1])) {
5477 *p = 0;
5478 break;
5479 }
5480 }
5481 strlcat(str, sfx, len);
5482}
5483
Takashi Iwaifb83b632015-03-16 23:34:34 +01005484/* copy PCM stream info from @default_str, and override non-NULL entries
5485 * from @spec_str and @nid
5486 */
5487static void setup_pcm_stream(struct hda_pcm_stream *str,
5488 const struct hda_pcm_stream *default_str,
5489 const struct hda_pcm_stream *spec_str,
5490 hda_nid_t nid)
5491{
5492 *str = *default_str;
5493 if (nid)
5494 str->nid = nid;
5495 if (spec_str) {
5496 if (spec_str->substreams)
5497 str->substreams = spec_str->substreams;
5498 if (spec_str->channels_min)
5499 str->channels_min = spec_str->channels_min;
5500 if (spec_str->channels_max)
5501 str->channels_max = spec_str->channels_max;
5502 if (spec_str->rates)
5503 str->rates = spec_str->rates;
5504 if (spec_str->formats)
5505 str->formats = spec_str->formats;
5506 if (spec_str->maxbps)
5507 str->maxbps = spec_str->maxbps;
5508 }
5509}
5510
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005511/**
5512 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5513 * @codec: the HDA codec
5514 *
5515 * Pass this to build_pcms patch_ops.
5516 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005517int snd_hda_gen_build_pcms(struct hda_codec *codec)
5518{
5519 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005520 struct hda_pcm *info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005521 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522
Takashi Iwai352f7f92012-12-19 12:52:06 +01005523 if (spec->no_analog)
5524 goto skip_analog;
5525
Takashi Iwaif873e532012-12-20 16:58:39 +01005526 fill_pcm_stream_name(spec->stream_name_analog,
5527 sizeof(spec->stream_name_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005528 " Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005529 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5530 if (!info)
5531 return -ENOMEM;
5532 spec->pcm_rec[0] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005533
5534 if (spec->multiout.num_dacs > 0) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005535 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5536 &pcm_analog_playback,
5537 spec->stream_analog_playback,
5538 spec->multiout.dac_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005539 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5540 spec->multiout.max_channels;
5541 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5542 spec->autocfg.line_outs == 2)
5543 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5544 snd_pcm_2_1_chmaps;
5545 }
5546 if (spec->num_adc_nids) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005547 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5548 (spec->dyn_adc_switch ?
5549 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5550 spec->stream_analog_capture,
5551 spec->adc_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005552 }
5553
Takashi Iwai352f7f92012-12-19 12:52:06 +01005554 skip_analog:
5555 /* SPDIF for stream index #1 */
5556 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005557 fill_pcm_stream_name(spec->stream_name_digital,
5558 sizeof(spec->stream_name_digital),
Takashi Iwai7639a062015-03-03 10:07:24 +01005559 " Digital", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005560 info = snd_hda_codec_pcm_new(codec, "%s",
5561 spec->stream_name_digital);
5562 if (!info)
5563 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005564 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005565 spec->pcm_rec[1] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005566 if (spec->dig_out_type)
5567 info->pcm_type = spec->dig_out_type;
5568 else
5569 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005570 if (spec->multiout.dig_out_nid)
5571 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5572 &pcm_digital_playback,
5573 spec->stream_digital_playback,
5574 spec->multiout.dig_out_nid);
5575 if (spec->dig_in_nid)
5576 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5577 &pcm_digital_capture,
5578 spec->stream_digital_capture,
5579 spec->dig_in_nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005580 }
5581
5582 if (spec->no_analog)
5583 return 0;
5584
5585 /* If the use of more than one ADC is requested for the current
5586 * model, configure a second analog capture-only PCM.
5587 */
5588 have_multi_adcs = (spec->num_adc_nids > 1) &&
5589 !spec->dyn_adc_switch && !spec->auto_mic;
5590 /* Additional Analaog capture for index #2 */
5591 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005592 fill_pcm_stream_name(spec->stream_name_alt_analog,
5593 sizeof(spec->stream_name_alt_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005594 " Alt Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005595 info = snd_hda_codec_pcm_new(codec, "%s",
5596 spec->stream_name_alt_analog);
5597 if (!info)
5598 return -ENOMEM;
5599 spec->pcm_rec[2] = info;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005600 if (spec->alt_dac_nid)
5601 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5602 &pcm_analog_alt_playback,
5603 spec->stream_analog_alt_playback,
5604 spec->alt_dac_nid);
5605 else
5606 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5607 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005608 if (have_multi_adcs) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005609 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5610 &pcm_analog_alt_capture,
5611 spec->stream_analog_alt_capture,
5612 spec->adc_nids[1]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005613 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5614 spec->num_adc_nids - 1;
5615 } else {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005616 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5617 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 }
5620
5621 return 0;
5622}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005623EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005624
5625
5626/*
5627 * Standard auto-parser initializations
5628 */
5629
Takashi Iwaid4156932013-01-07 10:08:02 +01005630/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005631static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005632{
5633 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005634 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005635
Takashi Iwai196c17662013-01-04 15:01:40 +01005636 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005637 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005638 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005639 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005640 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005641 snd_hda_activate_path(codec, path, path->active,
5642 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005643 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005644}
5645
5646/* initialize primary output paths */
5647static void init_multi_out(struct hda_codec *codec)
5648{
5649 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005650 int i;
5651
Takashi Iwaid4156932013-01-07 10:08:02 +01005652 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005653 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005654}
5655
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005656
Takashi Iwai2c12c302013-01-10 09:33:29 +01005657static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005658{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005659 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005660
Takashi Iwaid4156932013-01-07 10:08:02 +01005661 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005662 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005663}
5664
5665/* initialize hp and speaker paths */
5666static void init_extra_out(struct hda_codec *codec)
5667{
5668 struct hda_gen_spec *spec = codec->spec;
5669
5670 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005671 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005672 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5673 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005674 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005675}
5676
5677/* initialize multi-io paths */
5678static void init_multi_io(struct hda_codec *codec)
5679{
5680 struct hda_gen_spec *spec = codec->spec;
5681 int i;
5682
5683 for (i = 0; i < spec->multi_ios; i++) {
5684 hda_nid_t pin = spec->multi_io[i].pin;
5685 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005686 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005687 if (!path)
5688 continue;
5689 if (!spec->multi_io[i].ctl_in)
5690 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005691 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005692 snd_hda_activate_path(codec, path, path->active,
5693 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005694 }
5695}
5696
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005697static void init_aamix_paths(struct hda_codec *codec)
5698{
5699 struct hda_gen_spec *spec = codec->spec;
5700
5701 if (!spec->have_aamix_ctl)
5702 return;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01005703 if (!has_aamix_out_paths(spec))
5704 return;
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005705 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5706 spec->aamix_out_paths[0],
5707 spec->autocfg.line_out_type);
5708 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5709 spec->aamix_out_paths[1],
5710 AUTO_PIN_HP_OUT);
5711 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5712 spec->aamix_out_paths[2],
5713 AUTO_PIN_SPEAKER_OUT);
5714}
5715
Takashi Iwai352f7f92012-12-19 12:52:06 +01005716/* set up input pins and loopback paths */
5717static void init_analog_input(struct hda_codec *codec)
5718{
5719 struct hda_gen_spec *spec = codec->spec;
5720 struct auto_pin_cfg *cfg = &spec->autocfg;
5721 int i;
5722
5723 for (i = 0; i < cfg->num_inputs; i++) {
5724 hda_nid_t nid = cfg->inputs[i].pin;
5725 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005726 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005727
5728 /* init loopback inputs */
5729 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005730 resume_path_from_idx(codec, spec->loopback_paths[i]);
5731 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005732 }
5733 }
5734}
5735
5736/* initialize ADC paths */
5737static void init_input_src(struct hda_codec *codec)
5738{
5739 struct hda_gen_spec *spec = codec->spec;
5740 struct hda_input_mux *imux = &spec->input_mux;
5741 struct nid_path *path;
5742 int i, c, nums;
5743
5744 if (spec->dyn_adc_switch)
5745 nums = 1;
5746 else
5747 nums = spec->num_adc_nids;
5748
5749 for (c = 0; c < nums; c++) {
5750 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005751 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005752 if (path) {
5753 bool active = path->active;
5754 if (i == spec->cur_mux[c])
5755 active = true;
5756 snd_hda_activate_path(codec, path, active, false);
5757 }
5758 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005759 if (spec->hp_mic)
5760 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005761 }
5762
Takashi Iwai352f7f92012-12-19 12:52:06 +01005763 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005764 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005765}
5766
5767/* set right pin controls for digital I/O */
5768static void init_digital(struct hda_codec *codec)
5769{
5770 struct hda_gen_spec *spec = codec->spec;
5771 int i;
5772 hda_nid_t pin;
5773
Takashi Iwaid4156932013-01-07 10:08:02 +01005774 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005775 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005776 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005777 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005778 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005779 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005780 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005781}
5782
Takashi Iwai973e4972012-12-20 15:16:09 +01005783/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5784 * invalid unsol tags by some reason
5785 */
5786static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5787{
5788 int i;
5789
5790 for (i = 0; i < codec->init_pins.used; i++) {
5791 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5792 hda_nid_t nid = pin->nid;
5793 if (is_jack_detectable(codec, nid) &&
5794 !snd_hda_jack_tbl_get(codec, nid))
5795 snd_hda_codec_update_cache(codec, nid, 0,
5796 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5797 }
5798}
5799
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005800/**
5801 * snd_hda_gen_init - initialize the generic spec
5802 * @codec: the HDA codec
5803 *
5804 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005805 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005806int snd_hda_gen_init(struct hda_codec *codec)
5807{
5808 struct hda_gen_spec *spec = codec->spec;
5809
5810 if (spec->init_hook)
5811 spec->init_hook(codec);
5812
5813 snd_hda_apply_verbs(codec);
5814
5815 init_multi_out(codec);
5816 init_extra_out(codec);
5817 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005818 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005819 init_analog_input(codec);
5820 init_input_src(codec);
5821 init_digital(codec);
5822
Takashi Iwai973e4972012-12-20 15:16:09 +01005823 clear_unsol_on_unused_pins(codec);
5824
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005825 sync_all_pin_power_ctls(codec);
5826
Takashi Iwai352f7f92012-12-19 12:52:06 +01005827 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005828 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005829
Takashi Iwaia551d912015-02-26 12:34:49 +01005830 regcache_sync(codec->core.regmap);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005831
Takashi Iwai352f7f92012-12-19 12:52:06 +01005832 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5833 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5834
5835 hda_call_check_power_status(codec, 0x01);
5836 return 0;
5837}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005838EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005839
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005840/**
5841 * snd_hda_gen_free - free the generic spec
5842 * @codec: the HDA codec
5843 *
5844 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005845 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005846void snd_hda_gen_free(struct hda_codec *codec)
5847{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005848 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005849 snd_hda_gen_spec_free(codec->spec);
5850 kfree(codec->spec);
5851 codec->spec = NULL;
5852}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005853EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005854
5855#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005856/**
5857 * snd_hda_gen_check_power_status - check the loopback power save state
5858 * @codec: the HDA codec
5859 * @nid: NID to inspect
5860 *
5861 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005862 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005863int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5864{
5865 struct hda_gen_spec *spec = codec->spec;
5866 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5867}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005868EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005869#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005870
5871
5872/*
5873 * the generic codec support
5874 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875
Takashi Iwai352f7f92012-12-19 12:52:06 +01005876static const struct hda_codec_ops generic_patch_ops = {
5877 .build_controls = snd_hda_gen_build_controls,
5878 .build_pcms = snd_hda_gen_build_pcms,
5879 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005880 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005881 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005882#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005883 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005884#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885};
5886
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005887/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005888 * snd_hda_parse_generic_codec - Generic codec parser
5889 * @codec: the HDA codec
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005890 */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005891static int snd_hda_parse_generic_codec(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005893 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 int err;
5895
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005896 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005897 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005899 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005902 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5903 if (err < 0)
5904 return err;
5905
5906 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005907 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 goto error;
5909
5910 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 return 0;
5912
Takashi Iwai352f7f92012-12-19 12:52:06 +01005913error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005914 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 return err;
5916}
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005917
Takashi Iwaib9a94a92015-10-01 16:20:04 +02005918static const struct hda_device_id snd_hda_id_generic[] = {
5919 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005920 {} /* terminator */
5921};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02005922MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005923
5924static struct hda_codec_driver generic_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02005925 .id = snd_hda_id_generic,
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005926};
5927
5928module_hda_codec_driver(generic_driver);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005929
5930MODULE_LICENSE("GPL");
5931MODULE_DESCRIPTION("Generic HD-audio codec parser");