blob: 24f91114a32cc73f54d72d8b008db51935519893 [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/**
282 * snd_hda_get_nid_path - get the path between the given NIDs
283 * @codec: the HDA codec
284 * @from_nid: the NID where the path start from
285 * @to_nid: the NID where the path ends at
286 *
287 * Return the found nid_path object or NULL for error.
288 * Passing 0 to either @from_nid or @to_nid behaves as a wildcard.
Takashi Iwaif5172a72013-01-04 13:19:55 +0100289 */
290struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
291 hda_nid_t from_nid, hda_nid_t to_nid)
292{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100293 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100294}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100295EXPORT_SYMBOL_GPL(snd_hda_get_nid_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100296
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100297/**
298 * snd_hda_get_path_idx - get the index number corresponding to the path
299 * instance
300 * @codec: the HDA codec
301 * @path: nid_path object
302 *
303 * The returned index starts from 1, i.e. the actual array index with offset 1,
304 * and zero is handled as an invalid path
Takashi Iwai196c17662013-01-04 15:01:40 +0100305 */
306int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
307{
308 struct hda_gen_spec *spec = codec->spec;
309 struct nid_path *array = spec->paths.list;
310 ssize_t idx;
311
312 if (!spec->paths.used)
313 return 0;
314 idx = path - array;
315 if (idx < 0 || idx >= spec->paths.used)
316 return 0;
317 return idx + 1;
318}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100319EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100320
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100321/**
322 * snd_hda_get_path_from_idx - get the path instance corresponding to the
323 * given index number
324 * @codec: the HDA codec
325 * @idx: the path index
326 */
Takashi Iwai196c17662013-01-04 15:01:40 +0100327struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
328{
329 struct hda_gen_spec *spec = codec->spec;
330
331 if (idx <= 0 || idx > spec->paths.used)
332 return NULL;
333 return snd_array_elem(&spec->paths, idx - 1);
334}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100335EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100336
Takashi Iwai352f7f92012-12-19 12:52:06 +0100337/* check whether the given DAC is already found in any existing paths */
338static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
339{
340 struct hda_gen_spec *spec = codec->spec;
341 int i;
342
343 for (i = 0; i < spec->paths.used; i++) {
344 struct nid_path *path = snd_array_elem(&spec->paths, i);
345 if (path->path[0] == nid)
346 return true;
347 }
348 return false;
349}
350
351/* check whether the given two widgets can be connected */
352static bool is_reachable_path(struct hda_codec *codec,
353 hda_nid_t from_nid, hda_nid_t to_nid)
354{
355 if (!from_nid || !to_nid)
356 return false;
357 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
358}
359
360/* nid, dir and idx */
361#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
362
363/* check whether the given ctl is already assigned in any path elements */
364static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
365{
366 struct hda_gen_spec *spec = codec->spec;
367 int i;
368
369 val &= AMP_VAL_COMPARE_MASK;
370 for (i = 0; i < spec->paths.used; i++) {
371 struct nid_path *path = snd_array_elem(&spec->paths, i);
372 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
373 return true;
374 }
375 return false;
376}
377
378/* check whether a control with the given (nid, dir, idx) was assigned */
379static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100380 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100381{
382 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100383 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100384}
385
Takashi Iwai4e76a882014-02-25 12:21:03 +0100386static void print_nid_path(struct hda_codec *codec,
387 const char *pfx, struct nid_path *path)
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100388{
389 char buf[40];
Joe Perchesd82353e2014-07-05 13:02:02 -0700390 char *pos = buf;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100391 int i;
392
Joe Perchesd82353e2014-07-05 13:02:02 -0700393 *pos = 0;
394 for (i = 0; i < path->depth; i++)
395 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
396 pos != buf ? ":" : "",
397 path->path[i]);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100398
Joe Perchesd82353e2014-07-05 13:02:02 -0700399 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100400}
401
Takashi Iwai352f7f92012-12-19 12:52:06 +0100402/* called recursively */
403static bool __parse_nid_path(struct hda_codec *codec,
404 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100405 int anchor_nid, struct nid_path *path,
406 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100407{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100408 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100409 int i, nums;
410
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100411 if (to_nid == anchor_nid)
412 anchor_nid = 0; /* anchor passed */
413 else if (to_nid == (hda_nid_t)(-anchor_nid))
414 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100415
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100416 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100417 for (i = 0; i < nums; i++) {
418 if (conn[i] != from_nid) {
419 /* special case: when from_nid is 0,
420 * try to find an empty DAC
421 */
422 if (from_nid ||
423 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
424 is_dac_already_used(codec, conn[i]))
425 continue;
426 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100427 /* anchor is not requested or already passed? */
428 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100429 goto found;
430 }
431 if (depth >= MAX_NID_PATH_DEPTH)
432 return false;
433 for (i = 0; i < nums; i++) {
434 unsigned int type;
435 type = get_wcaps_type(get_wcaps(codec, conn[i]));
436 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
437 type == AC_WID_PIN)
438 continue;
439 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100440 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100441 goto found;
442 }
443 return false;
444
445 found:
446 path->path[path->depth] = conn[i];
447 path->idx[path->depth + 1] = i;
448 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
449 path->multi[path->depth + 1] = 1;
450 path->depth++;
451 return true;
452}
453
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100454/**
455 * snd_hda_parse_nid_path - parse the widget path from the given nid to
456 * the target nid
457 * @codec: the HDA codec
458 * @from_nid: the NID where the path start from
459 * @to_nid: the NID where the path ends at
460 * @anchor_nid: the anchor indication
461 * @path: the path object to store the result
462 *
463 * Returns true if a matching path is found.
464 *
465 * The parsing behavior depends on parameters:
Takashi Iwai352f7f92012-12-19 12:52:06 +0100466 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100467 * when @anchor_nid is set to a positive value, only paths through the widget
468 * with the given value are evaluated.
469 * when @anchor_nid is set to a negative value, paths through the widget
470 * with the negative of given value are excluded, only other paths are chosen.
471 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100472 */
473bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100474 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100475 struct nid_path *path)
476{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100477 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100478 path->path[path->depth] = to_nid;
479 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100480 return true;
481 }
482 return false;
483}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100484EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100486/**
487 * snd_hda_add_new_path - parse the path between the given NIDs and
488 * add to the path list
489 * @codec: the HDA codec
490 * @from_nid: the NID where the path start from
491 * @to_nid: the NID where the path ends at
492 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
493 *
494 * If no valid path is found, returns NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100496struct nid_path *
497snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100498 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100500 struct hda_gen_spec *spec = codec->spec;
501 struct nid_path *path;
502
503 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
504 return NULL;
505
Takashi Iwaif5172a72013-01-04 13:19:55 +0100506 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100507 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100508 if (path)
509 return path;
510
Takashi Iwai352f7f92012-12-19 12:52:06 +0100511 path = snd_array_new(&spec->paths);
512 if (!path)
513 return NULL;
514 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100515 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100516 return path;
517 /* push back */
518 spec->paths.used--;
519 return NULL;
520}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100521EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100522
Takashi Iwai980428c2013-01-09 09:28:20 +0100523/* clear the given path as invalid so that it won't be picked up later */
524static void invalidate_nid_path(struct hda_codec *codec, int idx)
525{
526 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
527 if (!path)
528 return;
529 memset(path, 0, sizeof(*path));
530}
531
Takashi Iwai36907392013-12-10 17:29:26 +0100532/* return a DAC if paired to the given pin by codec driver */
533static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
534{
535 struct hda_gen_spec *spec = codec->spec;
536 const hda_nid_t *list = spec->preferred_dacs;
537
538 if (!list)
539 return 0;
540 for (; *list; list += 2)
541 if (*list == pin)
542 return list[1];
543 return 0;
544}
545
Takashi Iwai352f7f92012-12-19 12:52:06 +0100546/* look for an empty DAC slot */
547static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
548 bool is_digital)
549{
550 struct hda_gen_spec *spec = codec->spec;
551 bool cap_digital;
552 int i;
553
554 for (i = 0; i < spec->num_all_dacs; i++) {
555 hda_nid_t nid = spec->all_dacs[i];
556 if (!nid || is_dac_already_used(codec, nid))
557 continue;
558 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
559 if (is_digital != cap_digital)
560 continue;
561 if (is_reachable_path(codec, nid, pin))
562 return nid;
563 }
564 return 0;
565}
566
567/* replace the channels in the composed amp value with the given number */
568static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
569{
570 val &= ~(0x3U << 16);
571 val |= chs << 16;
572 return val;
573}
574
David Henningsson99a55922013-01-16 15:58:44 +0100575static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
576 hda_nid_t nid2, int dir)
577{
578 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
579 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
580 return (query_amp_caps(codec, nid1, dir) ==
581 query_amp_caps(codec, nid2, dir));
582}
583
Takashi Iwai352f7f92012-12-19 12:52:06 +0100584/* look for a widget suitable for assigning a mute switch in the path */
585static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
586 struct nid_path *path)
587{
588 int i;
589
590 for (i = path->depth - 1; i >= 0; i--) {
591 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
592 return path->path[i];
593 if (i != path->depth - 1 && i != 0 &&
594 nid_has_mute(codec, path->path[i], HDA_INPUT))
595 return path->path[i];
596 }
597 return 0;
598}
599
600/* look for a widget suitable for assigning a volume ctl in the path */
601static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
602 struct nid_path *path)
603{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100604 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100605 int i;
606
607 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100608 hda_nid_t nid = path->path[i];
609 if ((spec->out_vol_mask >> nid) & 1)
610 continue;
611 if (nid_has_volume(codec, nid, HDA_OUTPUT))
612 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100613 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100618 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100620
621/* can have the amp-in capability? */
622static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100624 hda_nid_t nid = path->path[idx];
625 unsigned int caps = get_wcaps(codec, nid);
626 unsigned int type = get_wcaps_type(caps);
627
628 if (!(caps & AC_WCAP_IN_AMP))
629 return false;
630 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
631 return false;
632 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Takashi Iwai352f7f92012-12-19 12:52:06 +0100635/* can have the amp-out capability? */
636static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100638 hda_nid_t nid = path->path[idx];
639 unsigned int caps = get_wcaps(codec, nid);
640 unsigned int type = get_wcaps_type(caps);
641
642 if (!(caps & AC_WCAP_OUT_AMP))
643 return false;
644 if (type == AC_WID_PIN && !idx) /* only for output pins */
645 return false;
646 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Takashi Iwai352f7f92012-12-19 12:52:06 +0100649/* check whether the given (nid,dir,idx) is active */
650static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100651 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100653 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100654 int type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai352f7f92012-12-19 12:52:06 +0100655 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Takashi Iwai7639a062015-03-03 10:07:24 +0100657 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100658 return true;
659
Takashi Iwai352f7f92012-12-19 12:52:06 +0100660 for (n = 0; n < spec->paths.used; n++) {
661 struct nid_path *path = snd_array_elem(&spec->paths, n);
662 if (!path->active)
663 continue;
Takashi Iwai967b1302015-03-20 18:21:03 +0100664 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100665 if (!path->stream_enabled)
666 continue;
667 /* ignore unplugged paths except for DAC/ADC */
Takashi Iwai6b275b12015-03-20 18:11:05 +0100668 if (!(path->pin_enabled || path->pin_fixed) &&
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100669 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
670 continue;
671 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100672 for (i = 0; i < path->depth; i++) {
673 if (path->path[i] == nid) {
Takashi Iwai9d2b48f2015-08-24 10:45:27 +0200674 if (dir == HDA_OUTPUT || idx == -1 ||
675 path->idx[i] == idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100676 return true;
677 break;
678 }
679 }
680 }
681 return false;
682}
683
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200684/* check whether the NID is referred by any active paths */
685#define is_active_nid_for_any(codec, nid) \
Takashi Iwai9d2b48f2015-08-24 10:45:27 +0200686 is_active_nid(codec, nid, HDA_OUTPUT, -1)
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200687
Takashi Iwai352f7f92012-12-19 12:52:06 +0100688/* get the default amp value for the target state */
689static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100690 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100691{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100692 unsigned int val = 0;
693
Takashi Iwai352f7f92012-12-19 12:52:06 +0100694 if (caps & AC_AMPCAP_NUM_STEPS) {
695 /* set to 0dB */
696 if (enable)
697 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
698 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200699 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100700 if (!enable)
701 val |= HDA_AMP_MUTE;
702 }
703 return val;
704}
705
Takashi Iwaicc261732015-03-16 10:18:08 +0100706/* is this a stereo widget or a stereo-to-mono mix? */
707static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
708{
709 unsigned int wcaps = get_wcaps(codec, nid);
710 hda_nid_t conn;
711
712 if (wcaps & AC_WCAP_STEREO)
713 return true;
714 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
715 return false;
716 if (snd_hda_get_num_conns(codec, nid) != 1)
717 return false;
718 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
719 return false;
720 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
721}
722
Takashi Iwai352f7f92012-12-19 12:52:06 +0100723/* initialize the amp value (only at the first time) */
724static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
725{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100726 unsigned int caps = query_amp_caps(codec, nid, dir);
727 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwaief403ed2015-03-12 08:30:11 +0100728
Takashi Iwaicc261732015-03-16 10:18:08 +0100729 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100730 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
731 else
732 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
733}
734
735/* update the amp, doing in stereo or mono depending on NID */
736static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
737 unsigned int mask, unsigned int val)
738{
Takashi Iwaicc261732015-03-16 10:18:08 +0100739 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100740 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
741 mask, val);
742 else
743 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
744 mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100745}
746
Takashi Iwai8999bf02013-01-18 11:01:33 +0100747/* calculate amp value mask we can modify;
748 * if the given amp is controlled by mixers, don't touch it
749 */
750static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
751 hda_nid_t nid, int dir, int idx,
752 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100753{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100754 unsigned int mask = 0xff;
755
Takashi Iwaif69910d2013-08-08 09:32:37 +0200756 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100757 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
758 mask &= ~0x80;
759 }
760 if (caps & AC_AMPCAP_NUM_STEPS) {
761 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
762 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
763 mask &= ~0x7f;
764 }
765 return mask;
766}
767
768static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
769 int idx, int idx_to_check, bool enable)
770{
771 unsigned int caps;
772 unsigned int mask, val;
773
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100774 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100775 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100776
777 caps = query_amp_caps(codec, nid, dir);
778 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
779 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
780 if (!mask)
781 return;
782
783 val &= mask;
Takashi Iwaief403ed2015-03-12 08:30:11 +0100784 update_amp(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100785}
786
787static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
788 int i, bool enable)
789{
790 hda_nid_t nid = path->path[i];
791 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100792 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100793}
794
795static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
796 int i, bool enable, bool add_aamix)
797{
798 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100799 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100800 int n, nums, idx;
801 int type;
802 hda_nid_t nid = path->path[i];
803
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100804 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100805 type = get_wcaps_type(get_wcaps(codec, nid));
806 if (type == AC_WID_PIN ||
807 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
808 nums = 1;
809 idx = 0;
810 } else
811 idx = path->idx[i];
812
813 for (n = 0; n < nums; n++)
814 init_amp(codec, nid, HDA_INPUT, n);
815
Takashi Iwai352f7f92012-12-19 12:52:06 +0100816 /* here is a little bit tricky in comparison with activate_amp_out();
817 * when aa-mixer is available, we need to enable the path as well
818 */
819 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100820 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100821 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100822 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824}
825
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100826/* sync power of each widget in the the given path */
827static hda_nid_t path_power_update(struct hda_codec *codec,
828 struct nid_path *path,
829 bool allow_powerdown)
830{
831 hda_nid_t nid, changed = 0;
832 int i, state;
833
834 for (i = 0; i < path->depth; i++) {
835 nid = path->path[i];
Takashi Iwai2206dc92015-04-09 10:18:31 +0200836 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
837 continue;
Takashi Iwai7639a062015-03-03 10:07:24 +0100838 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100839 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100840 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
841 state = AC_PWRST_D0;
842 else
843 state = AC_PWRST_D3;
844 if (!snd_hda_check_power_state(codec, nid, state)) {
845 snd_hda_codec_write(codec, nid, 0,
846 AC_VERB_SET_POWER_STATE, state);
847 changed = nid;
Takashi Iwai48f4b3a2015-05-20 06:49:37 +0200848 /* all known codecs seem to be capable to handl
849 * widgets state even in D3, so far.
850 * if any new codecs need to restore the widget
851 * states after D0 transition, call the function
852 * below.
853 */
854#if 0 /* disabled */
Takashi Iwaid545a572015-04-04 12:17:28 +0200855 if (state == AC_PWRST_D0)
856 snd_hdac_regmap_sync_node(&codec->core, nid);
Takashi Iwai48f4b3a2015-05-20 06:49:37 +0200857#endif
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100858 }
859 }
860 return changed;
861}
862
863/* do sync with the last power state change */
864static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
865{
866 if (nid) {
867 msleep(10);
868 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
869 }
870}
871
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100872/**
873 * snd_hda_activate_path - activate or deactivate the given path
874 * @codec: the HDA codec
875 * @path: the path to activate/deactivate
876 * @enable: flag to activate or not
877 * @add_aamix: enable the input from aamix NID
878 *
879 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100881void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
882 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100884 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100885 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Takashi Iwaic7cd0ef2015-08-24 10:52:06 +0200887 path->active = enable;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100888
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100889 /* make sure the widget is powered up */
Takashi Iwai967b1302015-03-20 18:21:03 +0100890 if (enable && (spec->power_down_unused || codec->power_save_node))
891 path_power_update(codec, path, codec->power_save_node);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100892
Takashi Iwai352f7f92012-12-19 12:52:06 +0100893 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100894 hda_nid_t nid = path->path[i];
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100895
Takashi Iwai352f7f92012-12-19 12:52:06 +0100896 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100897 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100898 AC_VERB_SET_CONNECT_SEL,
899 path->idx[i]);
900 if (has_amp_in(codec, path, i))
901 activate_amp_in(codec, path, i, enable, add_aamix);
902 if (has_amp_out(codec, path, i))
903 activate_amp_out(codec, path, i, enable);
904 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100905}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100906EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100907
Takashi Iwai55196ff2013-01-24 17:32:56 +0100908/* if the given path is inactive, put widgets into D3 (only if suitable) */
909static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
910{
911 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100912
Takashi Iwai967b1302015-03-20 18:21:03 +0100913 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
Takashi Iwai55196ff2013-01-24 17:32:56 +0100914 return;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100915 sync_power_state_change(codec, path_power_update(codec, path, true));
Takashi Iwai55196ff2013-01-24 17:32:56 +0100916}
917
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100918/* turn on/off EAPD on the given pin */
919static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
920{
921 struct hda_gen_spec *spec = codec->spec;
922 if (spec->own_eapd_ctl ||
923 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
924 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200925 if (spec->keep_eapd_on && !enable)
926 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100927 if (codec->inv_eapd)
928 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100929 snd_hda_codec_update_cache(codec, pin, 0,
930 AC_VERB_SET_EAPD_BTLENABLE,
931 enable ? 0x02 : 0x00);
932}
933
Takashi Iwai3e367f12013-01-23 17:07:23 +0100934/* re-initialize the path specified by the given path index */
935static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
936{
937 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
938 if (path)
939 snd_hda_activate_path(codec, path, path->active, false);
940}
941
Takashi Iwai352f7f92012-12-19 12:52:06 +0100942
943/*
944 * Helper functions for creating mixer ctl elements
945 */
946
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200947static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
948 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200949static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
950 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200951
Takashi Iwai352f7f92012-12-19 12:52:06 +0100952enum {
953 HDA_CTL_WIDGET_VOL,
954 HDA_CTL_WIDGET_MUTE,
955 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100956};
957static const struct snd_kcontrol_new control_templates[] = {
958 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200959 /* only the put callback is replaced for handling the special mute */
960 {
961 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
962 .subdevice = HDA_SUBDEV_AMP_FLAG,
963 .info = snd_hda_mixer_amp_switch_info,
964 .get = snd_hda_mixer_amp_switch_get,
965 .put = hda_gen_mixer_mute_put, /* replaced */
966 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
967 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200968 {
969 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
970 .info = snd_hda_mixer_amp_switch_info,
971 .get = snd_hda_mixer_bind_switch_get,
972 .put = hda_gen_bind_mute_put, /* replaced */
973 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
974 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100975};
976
977/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100978static struct snd_kcontrol_new *
979add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100980 int cidx, unsigned long val)
981{
982 struct snd_kcontrol_new *knew;
983
Takashi Iwai12c93df2012-12-19 14:38:33 +0100984 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100985 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100986 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100987 knew->index = cidx;
988 if (get_amp_nid_(val))
989 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
990 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100991 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100992}
993
994static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
995 const char *pfx, const char *dir,
996 const char *sfx, int cidx, unsigned long val)
997{
Takashi Iwai975cc022013-06-28 11:56:49 +0200998 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100999 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01001000 if (!add_control(spec, type, name, cidx, val))
1001 return -ENOMEM;
1002 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001003}
1004
1005#define add_pb_vol_ctrl(spec, type, pfx, val) \
1006 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1007#define add_pb_sw_ctrl(spec, type, pfx, val) \
1008 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1009#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1010 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1011#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1012 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1013
1014static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1015 unsigned int chs, struct nid_path *path)
1016{
1017 unsigned int val;
1018 if (!path)
1019 return 0;
1020 val = path->ctls[NID_PATH_VOL_CTL];
1021 if (!val)
1022 return 0;
1023 val = amp_val_replace_channels(val, chs);
1024 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1025}
1026
1027/* return the channel bits suitable for the given path->ctls[] */
1028static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1029 int type)
1030{
1031 int chs = 1; /* mono (left only) */
1032 if (path) {
1033 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1034 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1035 chs = 3; /* stereo */
1036 }
1037 return chs;
1038}
1039
1040static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1041 struct nid_path *path)
1042{
1043 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1044 return add_vol_ctl(codec, pfx, cidx, chs, path);
1045}
1046
1047/* create a mute-switch for the given mixer widget;
1048 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1049 */
1050static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1051 unsigned int chs, struct nid_path *path)
1052{
1053 unsigned int val;
1054 int type = HDA_CTL_WIDGET_MUTE;
1055
1056 if (!path)
1057 return 0;
1058 val = path->ctls[NID_PATH_MUTE_CTL];
1059 if (!val)
1060 return 0;
1061 val = amp_val_replace_channels(val, chs);
1062 if (get_amp_direction_(val) == HDA_INPUT) {
1063 hda_nid_t nid = get_amp_nid_(val);
1064 int nums = snd_hda_get_num_conns(codec, nid);
1065 if (nums > 1) {
1066 type = HDA_CTL_BIND_MUTE;
1067 val |= nums << 19;
1068 }
1069 }
1070 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1071}
1072
1073static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1074 int cidx, struct nid_path *path)
1075{
1076 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1077 return add_sw_ctl(codec, pfx, cidx, chs, path);
1078}
1079
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001080/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001081static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1082 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001083{
1084 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1085 struct hda_gen_spec *spec = codec->spec;
1086
1087 if (spec->auto_mute_via_amp) {
1088 hda_nid_t nid = get_amp_nid(kcontrol);
1089 bool enabled = !((spec->mute_bits >> nid) & 1);
1090 ucontrol->value.integer.value[0] &= enabled;
1091 ucontrol->value.integer.value[1] &= enabled;
1092 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001093}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001094
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001095static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1096 struct snd_ctl_elem_value *ucontrol)
1097{
1098 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001099 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1100}
1101
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001102static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1103 struct snd_ctl_elem_value *ucontrol)
1104{
1105 sync_auto_mute_bits(kcontrol, ucontrol);
1106 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1107}
1108
Takashi Iwai247d85e2013-01-17 16:18:11 +01001109/* any ctl assigned to the path with the given index? */
1110static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1111{
1112 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1113 return path && path->ctls[ctl_type];
1114}
1115
Takashi Iwai352f7f92012-12-19 12:52:06 +01001116static const char * const channel_name[4] = {
1117 "Front", "Surround", "CLFE", "Side"
1118};
1119
1120/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001121static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1122 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001123{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001124 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001125 struct auto_pin_cfg *cfg = &spec->autocfg;
1126
1127 *index = 0;
1128 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001129 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001130 return spec->vmaster_mute.hook ? "PCM" : "Master";
1131
1132 /* if there is really a single DAC used in the whole output paths,
1133 * use it master (or "PCM" if a vmaster hook is present)
1134 */
1135 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1136 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1137 return spec->vmaster_mute.hook ? "PCM" : "Master";
1138
Takashi Iwai247d85e2013-01-17 16:18:11 +01001139 /* multi-io channels */
1140 if (ch >= cfg->line_outs)
1141 return channel_name[ch];
1142
Takashi Iwai352f7f92012-12-19 12:52:06 +01001143 switch (cfg->line_out_type) {
1144 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001145 /* if the primary channel vol/mute is shared with HP volume,
1146 * don't name it as Speaker
1147 */
1148 if (!ch && cfg->hp_outs &&
1149 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1150 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001151 if (cfg->line_outs == 1)
1152 return "Speaker";
1153 if (cfg->line_outs == 2)
1154 return ch ? "Bass Speaker" : "Speaker";
1155 break;
1156 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001157 /* if the primary channel vol/mute is shared with spk volume,
1158 * don't name it as Headphone
1159 */
1160 if (!ch && cfg->speaker_outs &&
1161 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1162 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001163 /* for multi-io case, only the primary out */
1164 if (ch && spec->multi_ios)
1165 break;
1166 *index = ch;
1167 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001168 case AUTO_PIN_LINE_OUT:
1169 /* This deals with the case where we have two DACs and
1170 * one LO, one HP and one Speaker */
1171 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1172 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1173 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1174 if (hp_lo_shared && spk_lo_shared)
1175 return spec->vmaster_mute.hook ? "PCM" : "Master";
1176 if (hp_lo_shared)
1177 return "Headphone+LO";
1178 if (spk_lo_shared)
1179 return "Speaker+LO";
1180 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001181 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001182
1183 /* for a single channel output, we don't have to name the channel */
1184 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001185 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001186
Takashi Iwai352f7f92012-12-19 12:52:06 +01001187 if (ch >= ARRAY_SIZE(channel_name)) {
1188 snd_BUG();
1189 return "PCM";
1190 }
1191
1192 return channel_name[ch];
1193}
1194
1195/*
1196 * Parse output paths
1197 */
1198
1199/* badness definition */
1200enum {
1201 /* No primary DAC is found for the main output */
1202 BAD_NO_PRIMARY_DAC = 0x10000,
1203 /* No DAC is found for the extra output */
1204 BAD_NO_DAC = 0x4000,
1205 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001206 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001207 /* No individual DAC for extra output */
1208 BAD_NO_EXTRA_DAC = 0x102,
1209 /* No individual DAC for extra surrounds */
1210 BAD_NO_EXTRA_SURR_DAC = 0x101,
1211 /* Primary DAC shared with main surrounds */
1212 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001213 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001214 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001215 /* Primary DAC shared with main CLFE */
1216 BAD_SHARED_CLFE = 0x10,
1217 /* Primary DAC shared with extra surrounds */
1218 BAD_SHARED_EXTRA_SURROUND = 0x10,
1219 /* Volume widget is shared */
1220 BAD_SHARED_VOL = 0x10,
1221};
1222
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001223/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001224 * volume and mute controls, and assign the values to ctls[].
1225 *
1226 * When no appropriate widget is found in the path, the badness value
1227 * is incremented depending on the situation. The function returns the
1228 * total badness for both volume and mute controls.
1229 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001230static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001231{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001232 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001233 hda_nid_t nid;
1234 unsigned int val;
1235 int badness = 0;
1236
1237 if (!path)
1238 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001239
1240 if (path->ctls[NID_PATH_VOL_CTL] ||
1241 path->ctls[NID_PATH_MUTE_CTL])
1242 return 0; /* already evaluated */
1243
Takashi Iwai352f7f92012-12-19 12:52:06 +01001244 nid = look_for_out_vol_nid(codec, path);
1245 if (nid) {
1246 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001247 if (spec->dac_min_mute)
1248 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001249 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1250 badness += BAD_SHARED_VOL;
1251 else
1252 path->ctls[NID_PATH_VOL_CTL] = val;
1253 } else
1254 badness += BAD_SHARED_VOL;
1255 nid = look_for_out_mute_nid(codec, path);
1256 if (nid) {
1257 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1258 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1259 nid_has_mute(codec, nid, HDA_OUTPUT))
1260 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1261 else
1262 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1263 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1264 badness += BAD_SHARED_VOL;
1265 else
1266 path->ctls[NID_PATH_MUTE_CTL] = val;
1267 } else
1268 badness += BAD_SHARED_VOL;
1269 return badness;
1270}
1271
Takashi Iwai98bd1112013-03-22 14:53:50 +01001272const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001273 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1274 .no_dac = BAD_NO_DAC,
1275 .shared_primary = BAD_NO_PRIMARY_DAC,
1276 .shared_surr = BAD_SHARED_SURROUND,
1277 .shared_clfe = BAD_SHARED_CLFE,
1278 .shared_surr_main = BAD_SHARED_SURROUND,
1279};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001280EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001281
Takashi Iwai98bd1112013-03-22 14:53:50 +01001282const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001283 .no_primary_dac = BAD_NO_DAC,
1284 .no_dac = BAD_NO_DAC,
1285 .shared_primary = BAD_NO_EXTRA_DAC,
1286 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1287 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1288 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1289};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001290EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001291
Takashi Iwai7385df62013-01-07 09:50:52 +01001292/* get the DAC of the primary output corresponding to the given array index */
1293static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1294{
1295 struct hda_gen_spec *spec = codec->spec;
1296 struct auto_pin_cfg *cfg = &spec->autocfg;
1297
1298 if (cfg->line_outs > idx)
1299 return spec->private_dac_nids[idx];
1300 idx -= cfg->line_outs;
1301 if (spec->multi_ios > idx)
1302 return spec->multi_io[idx].dac;
1303 return 0;
1304}
1305
1306/* return the DAC if it's reachable, otherwise zero */
1307static inline hda_nid_t try_dac(struct hda_codec *codec,
1308 hda_nid_t dac, hda_nid_t pin)
1309{
1310 return is_reachable_path(codec, dac, pin) ? dac : 0;
1311}
1312
Takashi Iwai352f7f92012-12-19 12:52:06 +01001313/* try to assign DACs to pins and return the resultant badness */
1314static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1315 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001316 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001317 const struct badness_table *bad)
1318{
1319 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001320 int i, j;
1321 int badness = 0;
1322 hda_nid_t dac;
1323
1324 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return 0;
1326
Takashi Iwai352f7f92012-12-19 12:52:06 +01001327 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001328 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001329 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001330
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001331 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1332 if (path) {
1333 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001334 continue;
1335 }
1336
Takashi Iwai36907392013-12-10 17:29:26 +01001337 dacs[i] = get_preferred_dac(codec, pin);
1338 if (dacs[i]) {
1339 if (is_dac_already_used(codec, dacs[i]))
1340 badness += bad->shared_primary;
1341 }
1342
1343 if (!dacs[i])
1344 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001345 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001346 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001347 for (j = 1; j < num_outs; j++) {
1348 if (is_reachable_path(codec, dacs[j], pin)) {
1349 dacs[0] = dacs[j];
1350 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001351 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001352 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001353 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001356 }
1357 dac = dacs[i];
1358 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001359 if (num_outs > 2)
1360 dac = try_dac(codec, get_primary_out(codec, i), pin);
1361 if (!dac)
1362 dac = try_dac(codec, dacs[0], pin);
1363 if (!dac)
1364 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001365 if (dac) {
1366 if (!i)
1367 badness += bad->shared_primary;
1368 else if (i == 1)
1369 badness += bad->shared_surr;
1370 else
1371 badness += bad->shared_clfe;
1372 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1373 dac = spec->private_dac_nids[0];
1374 badness += bad->shared_surr_main;
1375 } else if (!i)
1376 badness += bad->no_primary_dac;
1377 else
1378 badness += bad->no_dac;
1379 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001380 if (!dac)
1381 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001382 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001383 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001384 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001385 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001386 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001387 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001388 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001389 badness += bad->no_dac;
1390 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001391 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001392 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001393 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001394 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001395 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001396 }
1397
1398 return badness;
1399}
1400
1401/* return NID if the given pin has only a single connection to a certain DAC */
1402static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1403{
1404 struct hda_gen_spec *spec = codec->spec;
1405 int i;
1406 hda_nid_t nid_found = 0;
1407
1408 for (i = 0; i < spec->num_all_dacs; i++) {
1409 hda_nid_t nid = spec->all_dacs[i];
1410 if (!nid || is_dac_already_used(codec, nid))
1411 continue;
1412 if (is_reachable_path(codec, nid, pin)) {
1413 if (nid_found)
1414 return 0;
1415 nid_found = nid;
1416 }
1417 }
1418 return nid_found;
1419}
1420
1421/* check whether the given pin can be a multi-io pin */
1422static bool can_be_multiio_pin(struct hda_codec *codec,
1423 unsigned int location, hda_nid_t nid)
1424{
1425 unsigned int defcfg, caps;
1426
1427 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1428 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1429 return false;
1430 if (location && get_defcfg_location(defcfg) != location)
1431 return false;
1432 caps = snd_hda_query_pin_caps(codec, nid);
1433 if (!(caps & AC_PINCAP_OUT))
1434 return false;
1435 return true;
1436}
1437
Takashi Iwaie22aab72013-01-04 14:50:04 +01001438/* count the number of input pins that are capable to be multi-io */
1439static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1440{
1441 struct hda_gen_spec *spec = codec->spec;
1442 struct auto_pin_cfg *cfg = &spec->autocfg;
1443 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1444 unsigned int location = get_defcfg_location(defcfg);
1445 int type, i;
1446 int num_pins = 0;
1447
1448 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1449 for (i = 0; i < cfg->num_inputs; i++) {
1450 if (cfg->inputs[i].type != type)
1451 continue;
1452 if (can_be_multiio_pin(codec, location,
1453 cfg->inputs[i].pin))
1454 num_pins++;
1455 }
1456 }
1457 return num_pins;
1458}
1459
Takashi Iwai352f7f92012-12-19 12:52:06 +01001460/*
1461 * multi-io helper
1462 *
1463 * When hardwired is set, try to fill ony hardwired pins, and returns
1464 * zero if any pins are filled, non-zero if nothing found.
1465 * When hardwired is off, try to fill possible input pins, and returns
1466 * the badness value.
1467 */
1468static int fill_multi_ios(struct hda_codec *codec,
1469 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001470 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001471{
1472 struct hda_gen_spec *spec = codec->spec;
1473 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001474 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001475 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1476 unsigned int location = get_defcfg_location(defcfg);
1477 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001478 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001479
1480 old_pins = spec->multi_ios;
1481 if (old_pins >= 2)
1482 goto end_fill;
1483
Takashi Iwaie22aab72013-01-04 14:50:04 +01001484 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001485 if (num_pins < 2)
1486 goto end_fill;
1487
Takashi Iwai352f7f92012-12-19 12:52:06 +01001488 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1489 for (i = 0; i < cfg->num_inputs; i++) {
1490 hda_nid_t nid = cfg->inputs[i].pin;
1491 hda_nid_t dac = 0;
1492
1493 if (cfg->inputs[i].type != type)
1494 continue;
1495 if (!can_be_multiio_pin(codec, location, nid))
1496 continue;
1497 for (j = 0; j < spec->multi_ios; j++) {
1498 if (nid == spec->multi_io[j].pin)
1499 break;
1500 }
1501 if (j < spec->multi_ios)
1502 continue;
1503
Takashi Iwai352f7f92012-12-19 12:52:06 +01001504 if (hardwired)
1505 dac = get_dac_if_single(codec, nid);
1506 else if (!dac)
1507 dac = look_for_dac(codec, nid, false);
1508 if (!dac) {
1509 badness++;
1510 continue;
1511 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001512 path = snd_hda_add_new_path(codec, dac, nid,
1513 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001514 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001515 badness++;
1516 continue;
1517 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001518 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001519 spec->multi_io[spec->multi_ios].pin = nid;
1520 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001521 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1522 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001523 spec->multi_ios++;
1524 if (spec->multi_ios >= 2)
1525 break;
1526 }
1527 }
1528 end_fill:
1529 if (badness)
1530 badness = BAD_MULTI_IO;
1531 if (old_pins == spec->multi_ios) {
1532 if (hardwired)
1533 return 1; /* nothing found */
1534 else
1535 return badness; /* no badness if nothing found */
1536 }
1537 if (!hardwired && spec->multi_ios < 2) {
1538 /* cancel newly assigned paths */
1539 spec->paths.used -= spec->multi_ios - old_pins;
1540 spec->multi_ios = old_pins;
1541 return badness;
1542 }
1543
1544 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001545 for (i = old_pins; i < spec->multi_ios; i++) {
1546 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1547 badness += assign_out_path_ctls(codec, path);
1548 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001549
1550 return badness;
1551}
1552
1553/* map DACs for all pins in the list if they are single connections */
1554static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001555 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001556{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001557 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001558 int i;
1559 bool found = false;
1560 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001561 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001562 hda_nid_t dac;
1563 if (dacs[i])
1564 continue;
1565 dac = get_dac_if_single(codec, pins[i]);
1566 if (!dac)
1567 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001568 path = snd_hda_add_new_path(codec, dac, pins[i],
1569 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001570 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001571 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001572 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001573 dacs[i] = dac;
1574 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001575 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001576 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001577 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001578 }
1579 }
1580 return found;
1581}
1582
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001583/* create a new path including aamix if available, and return its index */
1584static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1585{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001586 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001587 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001588 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001589
1590 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001591 if (!path || !path->depth ||
1592 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001593 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001594 path_dac = path->path[0];
1595 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001596 pin = path->path[path->depth - 1];
1597 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1598 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001599 if (dac != path_dac)
1600 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001601 else if (spec->multiout.hp_out_nid[0])
1602 dac = spec->multiout.hp_out_nid[0];
1603 else if (spec->multiout.extra_out_nid[0])
1604 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001605 else
1606 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001607 if (dac)
1608 path = snd_hda_add_new_path(codec, dac, pin,
1609 spec->mixer_nid);
1610 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001611 if (!path)
1612 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001613 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001614 path->active = false; /* unused as default */
Takashi Iwai6b275b12015-03-20 18:11:05 +01001615 path->pin_fixed = true; /* static route */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001616 return snd_hda_get_path_idx(codec, path);
1617}
1618
Takashi Iwai55a63d42013-03-21 17:20:12 +01001619/* check whether the independent HP is available with the current config */
1620static bool indep_hp_possible(struct hda_codec *codec)
1621{
1622 struct hda_gen_spec *spec = codec->spec;
1623 struct auto_pin_cfg *cfg = &spec->autocfg;
1624 struct nid_path *path;
1625 int i, idx;
1626
1627 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1628 idx = spec->out_paths[0];
1629 else
1630 idx = spec->hp_paths[0];
1631 path = snd_hda_get_path_from_idx(codec, idx);
1632 if (!path)
1633 return false;
1634
1635 /* assume no path conflicts unless aamix is involved */
1636 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1637 return true;
1638
1639 /* check whether output paths contain aamix */
1640 for (i = 0; i < cfg->line_outs; i++) {
1641 if (spec->out_paths[i] == idx)
1642 break;
1643 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1644 if (path && is_nid_contained(path, spec->mixer_nid))
1645 return false;
1646 }
1647 for (i = 0; i < cfg->speaker_outs; i++) {
1648 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1649 if (path && is_nid_contained(path, spec->mixer_nid))
1650 return false;
1651 }
1652
1653 return true;
1654}
1655
Takashi Iwaia07a9492013-01-07 16:44:06 +01001656/* fill the empty entries in the dac array for speaker/hp with the
1657 * shared dac pointed by the paths
1658 */
1659static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1660 hda_nid_t *dacs, int *path_idx)
1661{
1662 struct nid_path *path;
1663 int i;
1664
1665 for (i = 0; i < num_outs; i++) {
1666 if (dacs[i])
1667 continue;
1668 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1669 if (!path)
1670 continue;
1671 dacs[i] = path->path[0];
1672 }
1673}
1674
Takashi Iwai352f7f92012-12-19 12:52:06 +01001675/* fill in the dac_nids table from the parsed pin configuration */
1676static int fill_and_eval_dacs(struct hda_codec *codec,
1677 bool fill_hardwired,
1678 bool fill_mio_first)
1679{
1680 struct hda_gen_spec *spec = codec->spec;
1681 struct auto_pin_cfg *cfg = &spec->autocfg;
1682 int i, err, badness;
1683
1684 /* set num_dacs once to full for look_for_dac() */
1685 spec->multiout.num_dacs = cfg->line_outs;
1686 spec->multiout.dac_nids = spec->private_dac_nids;
1687 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1688 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1689 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1690 spec->multi_ios = 0;
1691 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001692
1693 /* clear path indices */
1694 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1695 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1696 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1697 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1698 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001699 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001700 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1701 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1702
Takashi Iwai352f7f92012-12-19 12:52:06 +01001703 badness = 0;
1704
1705 /* fill hard-wired DACs first */
1706 if (fill_hardwired) {
1707 bool mapped;
1708 do {
1709 mapped = map_singles(codec, cfg->line_outs,
1710 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001711 spec->private_dac_nids,
1712 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001713 mapped |= map_singles(codec, cfg->hp_outs,
1714 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001715 spec->multiout.hp_out_nid,
1716 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001717 mapped |= map_singles(codec, cfg->speaker_outs,
1718 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001719 spec->multiout.extra_out_nid,
1720 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001721 if (!spec->no_multi_io &&
1722 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001723 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001724 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001725 if (!err)
1726 mapped = true;
1727 }
1728 } while (mapped);
1729 }
1730
1731 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001732 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001733 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001734
Takashi Iwaida96fb52013-07-29 16:54:36 +02001735 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001736 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1737 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001738 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001739 if (err < 0)
1740 return err;
1741 /* we don't count badness at this stage yet */
1742 }
1743
1744 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1745 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1746 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001747 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001748 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001749 if (err < 0)
1750 return err;
1751 badness += err;
1752 }
1753 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1754 err = try_assign_dacs(codec, cfg->speaker_outs,
1755 cfg->speaker_pins,
1756 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001757 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001758 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001759 if (err < 0)
1760 return err;
1761 badness += err;
1762 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001763 if (!spec->no_multi_io &&
1764 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001765 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001766 if (err < 0)
1767 return err;
1768 badness += err;
1769 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001770
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001771 if (spec->mixer_nid) {
1772 spec->aamix_out_paths[0] =
1773 check_aamix_out_path(codec, spec->out_paths[0]);
1774 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1775 spec->aamix_out_paths[1] =
1776 check_aamix_out_path(codec, spec->hp_paths[0]);
1777 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1778 spec->aamix_out_paths[2] =
1779 check_aamix_out_path(codec, spec->speaker_paths[0]);
1780 }
1781
Takashi Iwaida96fb52013-07-29 16:54:36 +02001782 if (!spec->no_multi_io &&
1783 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001784 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1785 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001786
Takashi Iwaia07a9492013-01-07 16:44:06 +01001787 /* re-count num_dacs and squash invalid entries */
1788 spec->multiout.num_dacs = 0;
1789 for (i = 0; i < cfg->line_outs; i++) {
1790 if (spec->private_dac_nids[i])
1791 spec->multiout.num_dacs++;
1792 else {
1793 memmove(spec->private_dac_nids + i,
1794 spec->private_dac_nids + i + 1,
1795 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1796 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1797 }
1798 }
1799
1800 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001801 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001802
Takashi Iwai352f7f92012-12-19 12:52:06 +01001803 if (spec->multi_ios == 2) {
1804 for (i = 0; i < 2; i++)
1805 spec->private_dac_nids[spec->multiout.num_dacs++] =
1806 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001807 } else if (spec->multi_ios) {
1808 spec->multi_ios = 0;
1809 badness += BAD_MULTI_IO;
1810 }
1811
Takashi Iwai55a63d42013-03-21 17:20:12 +01001812 if (spec->indep_hp && !indep_hp_possible(codec))
1813 badness += BAD_NO_INDEP_HP;
1814
Takashi Iwaia07a9492013-01-07 16:44:06 +01001815 /* re-fill the shared DAC for speaker / headphone */
1816 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1817 refill_shared_dacs(codec, cfg->hp_outs,
1818 spec->multiout.hp_out_nid,
1819 spec->hp_paths);
1820 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1821 refill_shared_dacs(codec, cfg->speaker_outs,
1822 spec->multiout.extra_out_nid,
1823 spec->speaker_paths);
1824
Takashi Iwai352f7f92012-12-19 12:52:06 +01001825 return badness;
1826}
1827
1828#define DEBUG_BADNESS
1829
1830#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001831#define debug_badness(fmt, ...) \
1832 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001833#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001834#define debug_badness(fmt, ...) \
1835 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001836#endif
1837
Takashi Iwaia7694092013-01-21 10:43:18 +01001838#ifdef DEBUG_BADNESS
1839static inline void print_nid_path_idx(struct hda_codec *codec,
1840 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001841{
Takashi Iwaia7694092013-01-21 10:43:18 +01001842 struct nid_path *path;
1843
1844 path = snd_hda_get_path_from_idx(codec, idx);
1845 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001846 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001847}
1848
1849static void debug_show_configs(struct hda_codec *codec,
1850 struct auto_pin_cfg *cfg)
1851{
1852 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001853 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001854 int i;
1855
1856 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001857 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001858 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001859 spec->multiout.dac_nids[0],
1860 spec->multiout.dac_nids[1],
1861 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001862 spec->multiout.dac_nids[3],
1863 lo_type[cfg->line_out_type]);
1864 for (i = 0; i < cfg->line_outs; i++)
1865 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001866 if (spec->multi_ios > 0)
1867 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1868 spec->multi_ios,
1869 spec->multi_io[0].pin, spec->multi_io[1].pin,
1870 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001871 for (i = 0; i < spec->multi_ios; i++)
1872 print_nid_path_idx(codec, " mio",
1873 spec->out_paths[cfg->line_outs + i]);
1874 if (cfg->hp_outs)
1875 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001876 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001877 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001878 spec->multiout.hp_out_nid[0],
1879 spec->multiout.hp_out_nid[1],
1880 spec->multiout.hp_out_nid[2],
1881 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001882 for (i = 0; i < cfg->hp_outs; i++)
1883 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1884 if (cfg->speaker_outs)
1885 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001886 cfg->speaker_pins[0], cfg->speaker_pins[1],
1887 cfg->speaker_pins[2], cfg->speaker_pins[3],
1888 spec->multiout.extra_out_nid[0],
1889 spec->multiout.extra_out_nid[1],
1890 spec->multiout.extra_out_nid[2],
1891 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001892 for (i = 0; i < cfg->speaker_outs; i++)
1893 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1894 for (i = 0; i < 3; i++)
1895 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001896}
Takashi Iwaia7694092013-01-21 10:43:18 +01001897#else
1898#define debug_show_configs(codec, cfg) /* NOP */
1899#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001900
1901/* find all available DACs of the codec */
1902static void fill_all_dac_nids(struct hda_codec *codec)
1903{
1904 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001905 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001906
1907 spec->num_all_dacs = 0;
1908 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
Takashi Iwai7639a062015-03-03 10:07:24 +01001909 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001910 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1911 continue;
1912 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001913 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001914 break;
1915 }
1916 spec->all_dacs[spec->num_all_dacs++] = nid;
1917 }
1918}
1919
1920static int parse_output_paths(struct hda_codec *codec)
1921{
1922 struct hda_gen_spec *spec = codec->spec;
1923 struct auto_pin_cfg *cfg = &spec->autocfg;
1924 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001925 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001926 int best_badness = INT_MAX;
1927 int badness;
1928 bool fill_hardwired = true, fill_mio_first = true;
1929 bool best_wired = true, best_mio = true;
1930 bool hp_spk_swapped = false;
1931
Takashi Iwai352f7f92012-12-19 12:52:06 +01001932 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1933 if (!best_cfg)
1934 return -ENOMEM;
1935 *best_cfg = *cfg;
1936
1937 for (;;) {
1938 badness = fill_and_eval_dacs(codec, fill_hardwired,
1939 fill_mio_first);
1940 if (badness < 0) {
1941 kfree(best_cfg);
1942 return badness;
1943 }
1944 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1945 cfg->line_out_type, fill_hardwired, fill_mio_first,
1946 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001947 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001948 if (badness < best_badness) {
1949 best_badness = badness;
1950 *best_cfg = *cfg;
1951 best_wired = fill_hardwired;
1952 best_mio = fill_mio_first;
1953 }
1954 if (!badness)
1955 break;
1956 fill_mio_first = !fill_mio_first;
1957 if (!fill_mio_first)
1958 continue;
1959 fill_hardwired = !fill_hardwired;
1960 if (!fill_hardwired)
1961 continue;
1962 if (hp_spk_swapped)
1963 break;
1964 hp_spk_swapped = true;
1965 if (cfg->speaker_outs > 0 &&
1966 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1967 cfg->hp_outs = cfg->line_outs;
1968 memcpy(cfg->hp_pins, cfg->line_out_pins,
1969 sizeof(cfg->hp_pins));
1970 cfg->line_outs = cfg->speaker_outs;
1971 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1972 sizeof(cfg->speaker_pins));
1973 cfg->speaker_outs = 0;
1974 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1975 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1976 fill_hardwired = true;
1977 continue;
1978 }
1979 if (cfg->hp_outs > 0 &&
1980 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1981 cfg->speaker_outs = cfg->line_outs;
1982 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1983 sizeof(cfg->speaker_pins));
1984 cfg->line_outs = cfg->hp_outs;
1985 memcpy(cfg->line_out_pins, cfg->hp_pins,
1986 sizeof(cfg->hp_pins));
1987 cfg->hp_outs = 0;
1988 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1989 cfg->line_out_type = AUTO_PIN_HP_OUT;
1990 fill_hardwired = true;
1991 continue;
1992 }
1993 break;
1994 }
1995
1996 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001997 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001998 *cfg = *best_cfg;
1999 fill_and_eval_dacs(codec, best_wired, best_mio);
2000 }
2001 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2002 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01002003 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002004
2005 if (cfg->line_out_pins[0]) {
2006 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01002007 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002008 if (path)
2009 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002010 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01002011 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2012 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002013 if (spec->dac_min_mute)
2014 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2015 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002016 }
2017
Takashi Iwai9314a582013-01-21 10:49:05 +01002018 /* set initial pinctl targets */
2019 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2020 val = PIN_HP;
2021 else
2022 val = PIN_OUT;
2023 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2024 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2025 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2026 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2027 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2028 set_pin_targets(codec, cfg->speaker_outs,
2029 cfg->speaker_pins, val);
2030 }
2031
Takashi Iwai55a63d42013-03-21 17:20:12 +01002032 /* clear indep_hp flag if not available */
2033 if (spec->indep_hp && !indep_hp_possible(codec))
2034 spec->indep_hp = 0;
2035
Takashi Iwai352f7f92012-12-19 12:52:06 +01002036 kfree(best_cfg);
2037 return 0;
2038}
2039
2040/* add playback controls from the parsed DAC table */
2041static int create_multi_out_ctls(struct hda_codec *codec,
2042 const struct auto_pin_cfg *cfg)
2043{
2044 struct hda_gen_spec *spec = codec->spec;
2045 int i, err, noutputs;
2046
2047 noutputs = cfg->line_outs;
2048 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2049 noutputs += spec->multi_ios;
2050
2051 for (i = 0; i < noutputs; i++) {
2052 const char *name;
2053 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002054 struct nid_path *path;
2055
Takashi Iwai196c17662013-01-04 15:01:40 +01002056 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002057 if (!path)
2058 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002059
2060 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002061 if (!name || !strcmp(name, "CLFE")) {
2062 /* Center/LFE */
2063 err = add_vol_ctl(codec, "Center", 0, 1, path);
2064 if (err < 0)
2065 return err;
2066 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2067 if (err < 0)
2068 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002069 } else {
2070 err = add_stereo_vol(codec, name, index, path);
2071 if (err < 0)
2072 return err;
2073 }
2074
2075 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2076 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002077 err = add_sw_ctl(codec, "Center", 0, 1, path);
2078 if (err < 0)
2079 return err;
2080 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2081 if (err < 0)
2082 return err;
2083 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002084 err = add_stereo_sw(codec, name, index, path);
2085 if (err < 0)
2086 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
2088 }
2089 return 0;
2090}
2091
Takashi Iwaic2c80382013-01-07 10:33:57 +01002092static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002093 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002095 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 int err;
2097
Takashi Iwai196c17662013-01-04 15:01:40 +01002098 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002099 if (!path)
2100 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002101 err = add_stereo_vol(codec, pfx, cidx, path);
2102 if (err < 0)
2103 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002104 err = add_stereo_sw(codec, pfx, cidx, path);
2105 if (err < 0)
2106 return err;
2107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
Takashi Iwai352f7f92012-12-19 12:52:06 +01002110/* add playback controls for speaker and HP outputs */
2111static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002112 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002114 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002115
2116 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002117 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002118 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002119 int err, idx = 0;
2120
2121 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2122 name = "Bass Speaker";
2123 else if (num_pins >= 3) {
2124 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002125 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002126 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002127 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002128 name = pfx;
2129 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002131 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002132 if (err < 0)
2133 return err;
2134 }
2135 return 0;
2136}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002137
Takashi Iwai352f7f92012-12-19 12:52:06 +01002138static int create_hp_out_ctls(struct hda_codec *codec)
2139{
2140 struct hda_gen_spec *spec = codec->spec;
2141 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002142 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002143 "Headphone");
2144}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Takashi Iwai352f7f92012-12-19 12:52:06 +01002146static int create_speaker_out_ctls(struct hda_codec *codec)
2147{
2148 struct hda_gen_spec *spec = codec->spec;
2149 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002150 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002151 "Speaker");
2152}
2153
2154/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002155 * independent HP controls
2156 */
2157
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002158static void call_hp_automute(struct hda_codec *codec,
2159 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002160static int indep_hp_info(struct snd_kcontrol *kcontrol,
2161 struct snd_ctl_elem_info *uinfo)
2162{
2163 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2164}
2165
2166static int indep_hp_get(struct snd_kcontrol *kcontrol,
2167 struct snd_ctl_elem_value *ucontrol)
2168{
2169 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2170 struct hda_gen_spec *spec = codec->spec;
2171 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2172 return 0;
2173}
2174
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002175static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2176 int nomix_path_idx, int mix_path_idx,
2177 int out_type);
2178
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002179static int indep_hp_put(struct snd_kcontrol *kcontrol,
2180 struct snd_ctl_elem_value *ucontrol)
2181{
2182 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2183 struct hda_gen_spec *spec = codec->spec;
2184 unsigned int select = ucontrol->value.enumerated.item[0];
2185 int ret = 0;
2186
2187 mutex_lock(&spec->pcm_mutex);
2188 if (spec->active_streams) {
2189 ret = -EBUSY;
2190 goto unlock;
2191 }
2192
2193 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002194 hda_nid_t *dacp;
2195 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2196 dacp = &spec->private_dac_nids[0];
2197 else
2198 dacp = &spec->multiout.hp_out_nid[0];
2199
2200 /* update HP aamix paths in case it conflicts with indep HP */
2201 if (spec->have_aamix_ctl) {
2202 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2203 update_aamix_paths(codec, spec->aamix_mode,
2204 spec->out_paths[0],
2205 spec->aamix_out_paths[0],
2206 spec->autocfg.line_out_type);
2207 else
2208 update_aamix_paths(codec, spec->aamix_mode,
2209 spec->hp_paths[0],
2210 spec->aamix_out_paths[1],
2211 AUTO_PIN_HP_OUT);
2212 }
2213
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002214 spec->indep_hp_enabled = select;
2215 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002216 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002217 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002218 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002219
Takashi Iwai963afde2013-05-31 15:20:31 +02002220 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002221 ret = 1;
2222 }
2223 unlock:
2224 mutex_unlock(&spec->pcm_mutex);
2225 return ret;
2226}
2227
2228static const struct snd_kcontrol_new indep_hp_ctl = {
2229 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2230 .name = "Independent HP",
2231 .info = indep_hp_info,
2232 .get = indep_hp_get,
2233 .put = indep_hp_put,
2234};
2235
2236
2237static int create_indep_hp_ctls(struct hda_codec *codec)
2238{
2239 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002240 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002241
2242 if (!spec->indep_hp)
2243 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002244 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2245 dac = spec->multiout.dac_nids[0];
2246 else
2247 dac = spec->multiout.hp_out_nid[0];
2248 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002249 spec->indep_hp = 0;
2250 return 0;
2251 }
2252
2253 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002254 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002255 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2256 return -ENOMEM;
2257 return 0;
2258}
2259
2260/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002261 * channel mode enum control
2262 */
2263
2264static int ch_mode_info(struct snd_kcontrol *kcontrol,
2265 struct snd_ctl_elem_info *uinfo)
2266{
2267 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2268 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002269 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002270
2271 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2272 uinfo->count = 1;
2273 uinfo->value.enumerated.items = spec->multi_ios + 1;
2274 if (uinfo->value.enumerated.item > spec->multi_ios)
2275 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002276 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2277 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002278 return 0;
2279}
2280
2281static int ch_mode_get(struct snd_kcontrol *kcontrol,
2282 struct snd_ctl_elem_value *ucontrol)
2283{
2284 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2285 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002286 ucontrol->value.enumerated.item[0] =
2287 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002288 return 0;
2289}
2290
Takashi Iwai196c17662013-01-04 15:01:40 +01002291static inline struct nid_path *
2292get_multiio_path(struct hda_codec *codec, int idx)
2293{
2294 struct hda_gen_spec *spec = codec->spec;
2295 return snd_hda_get_path_from_idx(codec,
2296 spec->out_paths[spec->autocfg.line_outs + idx]);
2297}
2298
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002299static void update_automute_all(struct hda_codec *codec);
2300
Takashi Iwai65033cc2013-04-16 12:31:05 +02002301/* Default value to be passed as aamix argument for snd_hda_activate_path();
2302 * used for output paths
2303 */
2304static bool aamix_default(struct hda_gen_spec *spec)
2305{
2306 return !spec->have_aamix_ctl || spec->aamix_mode;
2307}
2308
Takashi Iwai352f7f92012-12-19 12:52:06 +01002309static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2310{
2311 struct hda_gen_spec *spec = codec->spec;
2312 hda_nid_t nid = spec->multi_io[idx].pin;
2313 struct nid_path *path;
2314
Takashi Iwai196c17662013-01-04 15:01:40 +01002315 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002316 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002318
2319 if (path->active == output)
2320 return 0;
2321
2322 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002323 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002324 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002325 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002326 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002327 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002328 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002329 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002330 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002332
2333 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002334 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002335
Takashi Iwai352f7f92012-12-19 12:52:06 +01002336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
Takashi Iwai352f7f92012-12-19 12:52:06 +01002339static int ch_mode_put(struct snd_kcontrol *kcontrol,
2340 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002342 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2343 struct hda_gen_spec *spec = codec->spec;
2344 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Takashi Iwai352f7f92012-12-19 12:52:06 +01002346 ch = ucontrol->value.enumerated.item[0];
2347 if (ch < 0 || ch > spec->multi_ios)
2348 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002349 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002350 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002351 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002352 for (i = 0; i < spec->multi_ios; i++)
2353 set_multi_io(codec, i, i < ch);
2354 spec->multiout.max_channels = max(spec->ext_channel_count,
2355 spec->const_channel_count);
2356 if (spec->need_dac_fix)
2357 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 return 1;
2359}
2360
Takashi Iwai352f7f92012-12-19 12:52:06 +01002361static const struct snd_kcontrol_new channel_mode_enum = {
2362 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2363 .name = "Channel Mode",
2364 .info = ch_mode_info,
2365 .get = ch_mode_get,
2366 .put = ch_mode_put,
2367};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Takashi Iwai352f7f92012-12-19 12:52:06 +01002369static int create_multi_channel_mode(struct hda_codec *codec)
2370{
2371 struct hda_gen_spec *spec = codec->spec;
2372
2373 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002374 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002375 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return 0;
2378}
2379
Takashi Iwai352f7f92012-12-19 12:52:06 +01002380/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002381 * aamix loopback enable/disable switch
2382 */
2383
2384#define loopback_mixing_info indep_hp_info
2385
2386static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2387 struct snd_ctl_elem_value *ucontrol)
2388{
2389 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2390 struct hda_gen_spec *spec = codec->spec;
2391 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2392 return 0;
2393}
2394
2395static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002396 int nomix_path_idx, int mix_path_idx,
2397 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002398{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002399 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002400 struct nid_path *nomix_path, *mix_path;
2401
2402 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2403 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2404 if (!nomix_path || !mix_path)
2405 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002406
2407 /* if HP aamix path is driven from a different DAC and the
2408 * independent HP mode is ON, can't turn on aamix path
2409 */
2410 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2411 mix_path->path[0] != spec->alt_dac_nid)
2412 do_mix = false;
2413
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002414 if (do_mix) {
2415 snd_hda_activate_path(codec, nomix_path, false, true);
2416 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002417 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002418 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002419 snd_hda_activate_path(codec, mix_path, false, false);
2420 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002421 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002422 }
2423}
2424
2425static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_value *ucontrol)
2427{
2428 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2429 struct hda_gen_spec *spec = codec->spec;
2430 unsigned int val = ucontrol->value.enumerated.item[0];
2431
2432 if (val == spec->aamix_mode)
2433 return 0;
2434 spec->aamix_mode = val;
2435 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002436 spec->aamix_out_paths[0],
2437 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002438 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002439 spec->aamix_out_paths[1],
2440 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002441 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002442 spec->aamix_out_paths[2],
2443 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002444 return 1;
2445}
2446
2447static const struct snd_kcontrol_new loopback_mixing_enum = {
2448 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2449 .name = "Loopback Mixing",
2450 .info = loopback_mixing_info,
2451 .get = loopback_mixing_get,
2452 .put = loopback_mixing_put,
2453};
2454
2455static int create_loopback_mixing_ctl(struct hda_codec *codec)
2456{
2457 struct hda_gen_spec *spec = codec->spec;
2458
2459 if (!spec->mixer_nid)
2460 return 0;
2461 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2462 spec->aamix_out_paths[2]))
2463 return 0;
2464 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2465 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002466 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002467 return 0;
2468}
2469
2470/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002471 * shared headphone/mic handling
2472 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002473
Takashi Iwai352f7f92012-12-19 12:52:06 +01002474static void call_update_outputs(struct hda_codec *codec);
2475
2476/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002477static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002478{
2479 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002480 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002481 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002482 hda_nid_t pin;
2483
2484 pin = spec->hp_mic_pin;
2485 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2486
2487 if (!force) {
2488 val = snd_hda_codec_get_pin_target(codec, pin);
2489 if (as_mic) {
2490 if (val & PIN_IN)
2491 return;
2492 } else {
2493 if (val & PIN_OUT)
2494 return;
2495 }
2496 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002497
2498 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002499 /* if the HP pin doesn't support VREF and the codec driver gives an
2500 * alternative pin, set up the VREF on that pin instead
2501 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002502 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2503 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2504 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2505 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002506 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002507 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002508 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002509
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002510 if (!spec->hp_mic_jack_modes) {
2511 if (as_mic)
2512 val |= PIN_IN;
2513 else
2514 val = PIN_HP;
2515 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002516 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002517 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002518}
2519
2520/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002521static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002522{
2523 struct hda_gen_spec *spec = codec->spec;
2524 struct auto_pin_cfg *cfg = &spec->autocfg;
2525 unsigned int defcfg;
2526 hda_nid_t nid;
2527
Takashi Iwai967303d2013-02-19 17:12:42 +01002528 if (!spec->hp_mic) {
2529 if (spec->suppress_hp_mic_detect)
2530 return 0;
2531 /* automatic detection: only if no input or a single internal
2532 * input pin is found, try to detect the shared hp/mic
2533 */
2534 if (cfg->num_inputs > 1)
2535 return 0;
2536 else if (cfg->num_inputs == 1) {
2537 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2538 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2539 return 0;
2540 }
2541 }
2542
2543 spec->hp_mic = 0; /* clear once */
2544 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002545 return 0;
2546
Takashi Iwai967303d2013-02-19 17:12:42 +01002547 nid = 0;
2548 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2549 nid = cfg->line_out_pins[0];
2550 else if (cfg->hp_outs > 0)
2551 nid = cfg->hp_pins[0];
2552 if (!nid)
2553 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002554
2555 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2556 return 0; /* no input */
2557
Takashi Iwai967303d2013-02-19 17:12:42 +01002558 cfg->inputs[cfg->num_inputs].pin = nid;
2559 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002560 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002561 cfg->num_inputs++;
2562 spec->hp_mic = 1;
2563 spec->hp_mic_pin = nid;
2564 /* we can't handle auto-mic together with HP-mic */
2565 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002566 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002567 return 0;
2568}
2569
Takashi Iwai978e77e2013-01-10 16:57:58 +01002570/*
2571 * output jack mode
2572 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002573
2574static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2575
2576static const char * const out_jack_texts[] = {
2577 "Line Out", "Headphone Out",
2578};
2579
Takashi Iwai978e77e2013-01-10 16:57:58 +01002580static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_info *uinfo)
2582{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002583 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002584}
2585
2586static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2587 struct snd_ctl_elem_value *ucontrol)
2588{
2589 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2590 hda_nid_t nid = kcontrol->private_value;
2591 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2592 ucontrol->value.enumerated.item[0] = 1;
2593 else
2594 ucontrol->value.enumerated.item[0] = 0;
2595 return 0;
2596}
2597
2598static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_value *ucontrol)
2600{
2601 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2602 hda_nid_t nid = kcontrol->private_value;
2603 unsigned int val;
2604
2605 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2606 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2607 return 0;
2608 snd_hda_set_pin_ctl_cache(codec, nid, val);
2609 return 1;
2610}
2611
2612static const struct snd_kcontrol_new out_jack_mode_enum = {
2613 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2614 .info = out_jack_mode_info,
2615 .get = out_jack_mode_get,
2616 .put = out_jack_mode_put,
2617};
2618
2619static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2620{
2621 struct hda_gen_spec *spec = codec->spec;
2622 int i;
2623
2624 for (i = 0; i < spec->kctls.used; i++) {
2625 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2626 if (!strcmp(kctl->name, name) && kctl->index == idx)
2627 return true;
2628 }
2629 return false;
2630}
2631
2632static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2633 char *name, size_t name_len)
2634{
2635 struct hda_gen_spec *spec = codec->spec;
2636 int idx = 0;
2637
2638 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2639 strlcat(name, " Jack Mode", name_len);
2640
2641 for (; find_kctl_name(codec, name, idx); idx++)
2642 ;
2643}
2644
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002645static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2646{
2647 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002648 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002649 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2650 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2651 return 2;
2652 }
2653 return 1;
2654}
2655
Takashi Iwai978e77e2013-01-10 16:57:58 +01002656static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2657 hda_nid_t *pins)
2658{
2659 struct hda_gen_spec *spec = codec->spec;
2660 int i;
2661
2662 for (i = 0; i < num_pins; i++) {
2663 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002664 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002665 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002666 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002667 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002668 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002669 get_jack_mode_name(codec, pin, name, sizeof(name));
2670 knew = snd_hda_gen_add_kctl(spec, name,
2671 &out_jack_mode_enum);
2672 if (!knew)
2673 return -ENOMEM;
2674 knew->private_value = pin;
2675 }
2676 }
2677
2678 return 0;
2679}
2680
Takashi Iwai294765582013-01-17 09:52:11 +01002681/*
2682 * input jack mode
2683 */
2684
2685/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2686#define NUM_VREFS 6
2687
2688static const char * const vref_texts[NUM_VREFS] = {
2689 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2690 "", "Mic 80pc Bias", "Mic 100pc Bias"
2691};
2692
2693static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2694{
2695 unsigned int pincap;
2696
2697 pincap = snd_hda_query_pin_caps(codec, pin);
2698 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2699 /* filter out unusual vrefs */
2700 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2701 return pincap;
2702}
2703
2704/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2705static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2706{
2707 unsigned int i, n = 0;
2708
2709 for (i = 0; i < NUM_VREFS; i++) {
2710 if (vref_caps & (1 << i)) {
2711 if (n == item_idx)
2712 return i;
2713 n++;
2714 }
2715 }
2716 return 0;
2717}
2718
2719/* convert back from the vref ctl index to the enum item index */
2720static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2721{
2722 unsigned int i, n = 0;
2723
2724 for (i = 0; i < NUM_VREFS; i++) {
2725 if (i == idx)
2726 return n;
2727 if (vref_caps & (1 << i))
2728 n++;
2729 }
2730 return 0;
2731}
2732
2733static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2734 struct snd_ctl_elem_info *uinfo)
2735{
2736 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2737 hda_nid_t nid = kcontrol->private_value;
2738 unsigned int vref_caps = get_vref_caps(codec, nid);
2739
2740 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2741 vref_texts);
2742 /* set the right text */
2743 strcpy(uinfo->value.enumerated.name,
2744 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2745 return 0;
2746}
2747
2748static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2749 struct snd_ctl_elem_value *ucontrol)
2750{
2751 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2752 hda_nid_t nid = kcontrol->private_value;
2753 unsigned int vref_caps = get_vref_caps(codec, nid);
2754 unsigned int idx;
2755
2756 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2757 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2758 return 0;
2759}
2760
2761static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2762 struct snd_ctl_elem_value *ucontrol)
2763{
2764 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2765 hda_nid_t nid = kcontrol->private_value;
2766 unsigned int vref_caps = get_vref_caps(codec, nid);
2767 unsigned int val, idx;
2768
2769 val = snd_hda_codec_get_pin_target(codec, nid);
2770 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2771 if (idx == ucontrol->value.enumerated.item[0])
2772 return 0;
2773
2774 val &= ~AC_PINCTL_VREFEN;
2775 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2776 snd_hda_set_pin_ctl_cache(codec, nid, val);
2777 return 1;
2778}
2779
2780static const struct snd_kcontrol_new in_jack_mode_enum = {
2781 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2782 .info = in_jack_mode_info,
2783 .get = in_jack_mode_get,
2784 .put = in_jack_mode_put,
2785};
2786
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002787static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2788{
2789 struct hda_gen_spec *spec = codec->spec;
2790 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002791 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002792 nitems = hweight32(get_vref_caps(codec, pin));
2793 return nitems ? nitems : 1;
2794}
2795
Takashi Iwai294765582013-01-17 09:52:11 +01002796static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2797{
2798 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002799 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002800 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002801 unsigned int defcfg;
2802
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002803 if (pin == spec->hp_mic_pin)
2804 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002805
2806 /* no jack mode for fixed pins */
2807 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2808 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2809 return 0;
2810
2811 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002812 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002813 return 0;
2814
2815 get_jack_mode_name(codec, pin, name, sizeof(name));
2816 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2817 if (!knew)
2818 return -ENOMEM;
2819 knew->private_value = pin;
2820 return 0;
2821}
2822
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002823/*
2824 * HP/mic shared jack mode
2825 */
2826static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2827 struct snd_ctl_elem_info *uinfo)
2828{
2829 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2830 hda_nid_t nid = kcontrol->private_value;
2831 int out_jacks = get_out_jack_num_items(codec, nid);
2832 int in_jacks = get_in_jack_num_items(codec, nid);
2833 const char *text = NULL;
2834 int idx;
2835
2836 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2837 uinfo->count = 1;
2838 uinfo->value.enumerated.items = out_jacks + in_jacks;
2839 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2840 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2841 idx = uinfo->value.enumerated.item;
2842 if (idx < out_jacks) {
2843 if (out_jacks > 1)
2844 text = out_jack_texts[idx];
2845 else
2846 text = "Headphone Out";
2847 } else {
2848 idx -= out_jacks;
2849 if (in_jacks > 1) {
2850 unsigned int vref_caps = get_vref_caps(codec, nid);
2851 text = vref_texts[get_vref_idx(vref_caps, idx)];
2852 } else
2853 text = "Mic In";
2854 }
2855
2856 strcpy(uinfo->value.enumerated.name, text);
2857 return 0;
2858}
2859
2860static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2861{
2862 int out_jacks = get_out_jack_num_items(codec, nid);
2863 int in_jacks = get_in_jack_num_items(codec, nid);
2864 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2865 int idx = 0;
2866
2867 if (val & PIN_OUT) {
2868 if (out_jacks > 1 && val == PIN_HP)
2869 idx = 1;
2870 } else if (val & PIN_IN) {
2871 idx = out_jacks;
2872 if (in_jacks > 1) {
2873 unsigned int vref_caps = get_vref_caps(codec, nid);
2874 val &= AC_PINCTL_VREFEN;
2875 idx += cvt_from_vref_idx(vref_caps, val);
2876 }
2877 }
2878 return idx;
2879}
2880
2881static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2882 struct snd_ctl_elem_value *ucontrol)
2883{
2884 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2885 hda_nid_t nid = kcontrol->private_value;
2886 ucontrol->value.enumerated.item[0] =
2887 get_cur_hp_mic_jack_mode(codec, nid);
2888 return 0;
2889}
2890
2891static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2892 struct snd_ctl_elem_value *ucontrol)
2893{
2894 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2895 hda_nid_t nid = kcontrol->private_value;
2896 int out_jacks = get_out_jack_num_items(codec, nid);
2897 int in_jacks = get_in_jack_num_items(codec, nid);
2898 unsigned int val, oldval, idx;
2899
2900 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2901 idx = ucontrol->value.enumerated.item[0];
2902 if (oldval == idx)
2903 return 0;
2904
2905 if (idx < out_jacks) {
2906 if (out_jacks > 1)
2907 val = idx ? PIN_HP : PIN_OUT;
2908 else
2909 val = PIN_HP;
2910 } else {
2911 idx -= out_jacks;
2912 if (in_jacks > 1) {
2913 unsigned int vref_caps = get_vref_caps(codec, nid);
2914 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002915 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2916 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002917 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002918 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002919 }
2920 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002921 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002922
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002923 return 1;
2924}
2925
2926static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2927 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2928 .info = hp_mic_jack_mode_info,
2929 .get = hp_mic_jack_mode_get,
2930 .put = hp_mic_jack_mode_put,
2931};
2932
2933static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2934{
2935 struct hda_gen_spec *spec = codec->spec;
2936 struct snd_kcontrol_new *knew;
2937
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002938 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2939 &hp_mic_jack_mode_enum);
2940 if (!knew)
2941 return -ENOMEM;
2942 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002943 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002944 return 0;
2945}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002946
2947/*
2948 * Parse input paths
2949 */
2950
Takashi Iwai352f7f92012-12-19 12:52:06 +01002951/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002952static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002953{
2954 struct hda_amp_list *list;
2955
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002956 list = snd_array_new(&spec->loopback_list);
2957 if (!list)
2958 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002959 list->nid = mix;
2960 list->dir = HDA_INPUT;
2961 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002962 spec->loopback.amplist = spec->loopback_list.list;
2963 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002964}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002965
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002966/* return true if either a volume or a mute amp is found for the given
2967 * aamix path; the amp has to be either in the mixer node or its direct leaf
2968 */
2969static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2970 hda_nid_t pin, unsigned int *mix_val,
2971 unsigned int *mute_val)
2972{
2973 int idx, num_conns;
2974 const hda_nid_t *list;
2975 hda_nid_t nid;
2976
2977 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2978 if (idx < 0)
2979 return false;
2980
2981 *mix_val = *mute_val = 0;
2982 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2983 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2984 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2985 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2986 if (*mix_val && *mute_val)
2987 return true;
2988
2989 /* check leaf node */
2990 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2991 if (num_conns < idx)
2992 return false;
2993 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01002994 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2995 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002996 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01002997 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2998 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002999 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3000
3001 return *mix_val || *mute_val;
3002}
3003
Takashi Iwai352f7f92012-12-19 12:52:06 +01003004/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01003005static int new_analog_input(struct hda_codec *codec, int input_idx,
3006 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003007 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003009 struct hda_gen_spec *spec = codec->spec;
3010 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003011 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003012 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003014 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3015 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003016
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003017 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003018 if (!path)
3019 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003020 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01003021 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003022
3023 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003024 if (mix_val) {
3025 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003026 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003028 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
3030
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003031 if (mute_val) {
3032 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003033 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003035 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 }
3037
Takashi Iwai352f7f92012-12-19 12:52:06 +01003038 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003039 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003040 err = add_loopback_list(spec, mix_nid, idx);
3041 if (err < 0)
3042 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003043
3044 if (spec->mixer_nid != spec->mixer_merge_nid &&
3045 !spec->loopback_merge_path) {
3046 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3047 spec->mixer_merge_nid, 0);
3048 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003049 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003050 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003051 path->pin_fixed = true; /* static route */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003052 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003053 spec->loopback_merge_path =
3054 snd_hda_get_path_idx(codec, path);
3055 }
3056 }
3057
Takashi Iwai352f7f92012-12-19 12:52:06 +01003058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060
Takashi Iwai352f7f92012-12-19 12:52:06 +01003061static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003063 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3064 return (pincap & AC_PINCAP_IN) != 0;
3065}
3066
3067/* Parse the codec tree and retrieve ADCs */
3068static int fill_adc_nids(struct hda_codec *codec)
3069{
3070 struct hda_gen_spec *spec = codec->spec;
3071 hda_nid_t nid;
3072 hda_nid_t *adc_nids = spec->adc_nids;
3073 int max_nums = ARRAY_SIZE(spec->adc_nids);
Takashi Iwai7639a062015-03-03 10:07:24 +01003074 int nums = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003075
Takashi Iwai7639a062015-03-03 10:07:24 +01003076 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003077 unsigned int caps = get_wcaps(codec, nid);
3078 int type = get_wcaps_type(caps);
3079
3080 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3081 continue;
3082 adc_nids[nums] = nid;
3083 if (++nums >= max_nums)
3084 break;
3085 }
3086 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003087
3088 /* copy the detected ADCs to all_adcs[] */
3089 spec->num_all_adcs = nums;
3090 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3091
Takashi Iwai352f7f92012-12-19 12:52:06 +01003092 return nums;
3093}
3094
3095/* filter out invalid adc_nids that don't give all active input pins;
3096 * if needed, check whether dynamic ADC-switching is available
3097 */
3098static int check_dyn_adc_switch(struct hda_codec *codec)
3099{
3100 struct hda_gen_spec *spec = codec->spec;
3101 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003102 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003103 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003104
Takashi Iwai352f7f92012-12-19 12:52:06 +01003105 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003106 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003107 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003108 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003109 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003110 break;
3111 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003112 if (i >= imux->num_items) {
3113 ok_bits |= (1 << n);
3114 nums++;
3115 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003116 }
3117
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003118 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003119 /* check whether ADC-switch is possible */
3120 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003121 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003122 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003123 spec->dyn_adc_idx[i] = n;
3124 break;
3125 }
3126 }
3127 }
3128
Takashi Iwai4e76a882014-02-25 12:21:03 +01003129 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003130 spec->dyn_adc_switch = 1;
3131 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003132 /* shrink the invalid adcs and input paths */
3133 nums = 0;
3134 for (n = 0; n < spec->num_adc_nids; n++) {
3135 if (!(ok_bits & (1 << n)))
3136 continue;
3137 if (n != nums) {
3138 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003139 for (i = 0; i < imux->num_items; i++) {
3140 invalidate_nid_path(codec,
3141 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003142 spec->input_paths[i][nums] =
3143 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003144 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003145 }
3146 nums++;
3147 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003148 spec->num_adc_nids = nums;
3149 }
3150
Takashi Iwai967303d2013-02-19 17:12:42 +01003151 if (imux->num_items == 1 ||
3152 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003153 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003154 spec->num_adc_nids = 1; /* reduce to a single ADC */
3155 }
3156
3157 /* single index for individual volumes ctls */
3158 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3159 spec->num_adc_nids = 1;
3160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 return 0;
3162}
3163
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003164/* parse capture source paths from the given pin and create imux items */
3165static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003166 int cfg_idx, int num_adcs,
3167 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003168{
3169 struct hda_gen_spec *spec = codec->spec;
3170 struct hda_input_mux *imux = &spec->input_mux;
3171 int imux_idx = imux->num_items;
3172 bool imux_added = false;
3173 int c;
3174
3175 for (c = 0; c < num_adcs; c++) {
3176 struct nid_path *path;
3177 hda_nid_t adc = spec->adc_nids[c];
3178
3179 if (!is_reachable_path(codec, pin, adc))
3180 continue;
3181 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3182 if (!path)
3183 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003184 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003185 spec->input_paths[imux_idx][c] =
3186 snd_hda_get_path_idx(codec, path);
3187
3188 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003189 if (spec->hp_mic_pin == pin)
3190 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003191 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003192 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003193 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003194 if (spec->dyn_adc_switch)
3195 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003196 }
3197 }
3198
3199 return 0;
3200}
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003203 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003205
Takashi Iwaic9700422013-01-18 10:17:30 +01003206/* fill the label for each input at first */
3207static int fill_input_pin_labels(struct hda_codec *codec)
3208{
3209 struct hda_gen_spec *spec = codec->spec;
3210 const struct auto_pin_cfg *cfg = &spec->autocfg;
3211 int i;
3212
3213 for (i = 0; i < cfg->num_inputs; i++) {
3214 hda_nid_t pin = cfg->inputs[i].pin;
3215 const char *label;
3216 int j, idx;
3217
3218 if (!is_input_pin(codec, pin))
3219 continue;
3220
3221 label = hda_get_autocfg_input_label(codec, cfg, i);
3222 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003223 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003224 if (spec->input_labels[j] &&
3225 !strcmp(spec->input_labels[j], label)) {
3226 idx = spec->input_label_idxs[j] + 1;
3227 break;
3228 }
3229 }
3230
3231 spec->input_labels[i] = label;
3232 spec->input_label_idxs[i] = idx;
3233 }
3234
3235 return 0;
3236}
3237
Takashi Iwai9dba2052013-01-18 10:01:15 +01003238#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3239
Takashi Iwai352f7f92012-12-19 12:52:06 +01003240static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003241{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003242 struct hda_gen_spec *spec = codec->spec;
3243 const struct auto_pin_cfg *cfg = &spec->autocfg;
3244 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003245 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003246 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003247 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003248
Takashi Iwai352f7f92012-12-19 12:52:06 +01003249 num_adcs = fill_adc_nids(codec);
3250 if (num_adcs < 0)
3251 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003252
Takashi Iwaic9700422013-01-18 10:17:30 +01003253 err = fill_input_pin_labels(codec);
3254 if (err < 0)
3255 return err;
3256
Takashi Iwai352f7f92012-12-19 12:52:06 +01003257 for (i = 0; i < cfg->num_inputs; i++) {
3258 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Takashi Iwai352f7f92012-12-19 12:52:06 +01003260 pin = cfg->inputs[i].pin;
3261 if (!is_input_pin(codec, pin))
3262 continue;
3263
Takashi Iwai2c12c302013-01-10 09:33:29 +01003264 val = PIN_IN;
3265 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3266 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai3e1b0c42015-04-27 10:43:22 +02003267 if (pin != spec->hp_mic_pin &&
3268 !snd_hda_codec_get_pin_target(codec, pin))
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003269 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003270
Takashi Iwai352f7f92012-12-19 12:52:06 +01003271 if (mixer) {
3272 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003273 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003274 spec->input_labels[i],
3275 spec->input_label_idxs[i],
3276 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003277 if (err < 0)
3278 return err;
3279 }
3280 }
3281
Takashi Iwaic9700422013-01-18 10:17:30 +01003282 err = parse_capture_source(codec, pin, i, num_adcs,
3283 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003284 if (err < 0)
3285 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003286
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003287 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003288 err = create_in_jack_mode(codec, pin);
3289 if (err < 0)
3290 return err;
3291 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003292 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003293
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003294 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003295 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003296 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003297 "Stereo Mix", 0);
3298 if (err < 0)
3299 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003300 else
3301 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003302 }
3303
3304 return 0;
3305}
3306
3307
3308/*
3309 * input source mux
3310 */
3311
Takashi Iwaic697b712013-01-07 17:09:26 +01003312/* get the input path specified by the given adc and imux indices */
3313static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003314{
3315 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003316 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3317 snd_BUG();
3318 return NULL;
3319 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003320 if (spec->dyn_adc_switch)
3321 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003322 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003323 snd_BUG();
3324 return NULL;
3325 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003326 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003327}
3328
3329static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3330 unsigned int idx);
3331
3332static int mux_enum_info(struct snd_kcontrol *kcontrol,
3333 struct snd_ctl_elem_info *uinfo)
3334{
3335 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3336 struct hda_gen_spec *spec = codec->spec;
3337 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3338}
3339
3340static int mux_enum_get(struct snd_kcontrol *kcontrol,
3341 struct snd_ctl_elem_value *ucontrol)
3342{
3343 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3344 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003345 /* the ctls are created at once with multiple counts */
3346 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003347
3348 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3349 return 0;
3350}
3351
3352static int mux_enum_put(struct snd_kcontrol *kcontrol,
3353 struct snd_ctl_elem_value *ucontrol)
3354{
3355 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003356 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003357 return mux_select(codec, adc_idx,
3358 ucontrol->value.enumerated.item[0]);
3359}
3360
Takashi Iwai352f7f92012-12-19 12:52:06 +01003361static const struct snd_kcontrol_new cap_src_temp = {
3362 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3363 .name = "Input Source",
3364 .info = mux_enum_info,
3365 .get = mux_enum_get,
3366 .put = mux_enum_put,
3367};
3368
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003369/*
3370 * capture volume and capture switch ctls
3371 */
3372
Takashi Iwai352f7f92012-12-19 12:52:06 +01003373typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3374 struct snd_ctl_elem_value *ucontrol);
3375
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003376/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003377static int cap_put_caller(struct snd_kcontrol *kcontrol,
3378 struct snd_ctl_elem_value *ucontrol,
3379 put_call_t func, int type)
3380{
3381 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3382 struct hda_gen_spec *spec = codec->spec;
3383 const struct hda_input_mux *imux;
3384 struct nid_path *path;
3385 int i, adc_idx, err = 0;
3386
3387 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003388 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003389 mutex_lock(&codec->control_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003390 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003391 path = get_input_path(codec, adc_idx, i);
3392 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003393 continue;
3394 kcontrol->private_value = path->ctls[type];
3395 err = func(kcontrol, ucontrol);
3396 if (err < 0)
Takashi Iwaia551d912015-02-26 12:34:49 +01003397 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003398 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003399 mutex_unlock(&codec->control_mutex);
3400 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003401 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003402 return err;
3403}
3404
3405/* capture volume ctl callbacks */
3406#define cap_vol_info snd_hda_mixer_amp_volume_info
3407#define cap_vol_get snd_hda_mixer_amp_volume_get
3408#define cap_vol_tlv snd_hda_mixer_amp_tlv
3409
3410static int cap_vol_put(struct snd_kcontrol *kcontrol,
3411 struct snd_ctl_elem_value *ucontrol)
3412{
3413 return cap_put_caller(kcontrol, ucontrol,
3414 snd_hda_mixer_amp_volume_put,
3415 NID_PATH_VOL_CTL);
3416}
3417
3418static const struct snd_kcontrol_new cap_vol_temp = {
3419 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3420 .name = "Capture Volume",
3421 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3422 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3423 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3424 .info = cap_vol_info,
3425 .get = cap_vol_get,
3426 .put = cap_vol_put,
3427 .tlv = { .c = cap_vol_tlv },
3428};
3429
3430/* capture switch ctl callbacks */
3431#define cap_sw_info snd_ctl_boolean_stereo_info
3432#define cap_sw_get snd_hda_mixer_amp_switch_get
3433
3434static int cap_sw_put(struct snd_kcontrol *kcontrol,
3435 struct snd_ctl_elem_value *ucontrol)
3436{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003437 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003438 snd_hda_mixer_amp_switch_put,
3439 NID_PATH_MUTE_CTL);
3440}
3441
3442static const struct snd_kcontrol_new cap_sw_temp = {
3443 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3444 .name = "Capture Switch",
3445 .info = cap_sw_info,
3446 .get = cap_sw_get,
3447 .put = cap_sw_put,
3448};
3449
3450static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3451{
3452 hda_nid_t nid;
3453 int i, depth;
3454
3455 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3456 for (depth = 0; depth < 3; depth++) {
3457 if (depth >= path->depth)
3458 return -EINVAL;
3459 i = path->depth - depth - 1;
3460 nid = path->path[i];
3461 if (!path->ctls[NID_PATH_VOL_CTL]) {
3462 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3463 path->ctls[NID_PATH_VOL_CTL] =
3464 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3465 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3466 int idx = path->idx[i];
3467 if (!depth && codec->single_adc_amp)
3468 idx = 0;
3469 path->ctls[NID_PATH_VOL_CTL] =
3470 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3471 }
3472 }
3473 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3474 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3475 path->ctls[NID_PATH_MUTE_CTL] =
3476 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3477 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3478 int idx = path->idx[i];
3479 if (!depth && codec->single_adc_amp)
3480 idx = 0;
3481 path->ctls[NID_PATH_MUTE_CTL] =
3482 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3483 }
3484 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 return 0;
3487}
3488
Takashi Iwai352f7f92012-12-19 12:52:06 +01003489static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003491 struct hda_gen_spec *spec = codec->spec;
3492 struct auto_pin_cfg *cfg = &spec->autocfg;
3493 unsigned int val;
3494 int i;
3495
3496 if (!spec->inv_dmic_split)
3497 return false;
3498 for (i = 0; i < cfg->num_inputs; i++) {
3499 if (cfg->inputs[i].pin != nid)
3500 continue;
3501 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3502 return false;
3503 val = snd_hda_codec_get_pincfg(codec, nid);
3504 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3505 }
3506 return false;
3507}
3508
Takashi Iwaia90229e2013-01-18 14:10:00 +01003509/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003510static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3511 struct snd_ctl_elem_value *ucontrol)
3512{
3513 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3514 struct hda_gen_spec *spec = codec->spec;
3515 int ret;
3516
3517 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3518 if (ret < 0)
3519 return ret;
3520
Takashi Iwaia90229e2013-01-18 14:10:00 +01003521 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003522 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003523
3524 return ret;
3525}
3526
Takashi Iwai352f7f92012-12-19 12:52:06 +01003527static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3528 int idx, bool is_switch, unsigned int ctl,
3529 bool inv_dmic)
3530{
3531 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003532 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003533 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3534 const char *sfx = is_switch ? "Switch" : "Volume";
3535 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003536 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003537
3538 if (!ctl)
3539 return 0;
3540
3541 if (label)
3542 snprintf(tmpname, sizeof(tmpname),
3543 "%s Capture %s", label, sfx);
3544 else
3545 snprintf(tmpname, sizeof(tmpname),
3546 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003547 knew = add_control(spec, type, tmpname, idx,
3548 amp_val_replace_channels(ctl, chs));
3549 if (!knew)
3550 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003551 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003552 knew->put = cap_single_sw_put;
3553 if (!inv_dmic)
3554 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003555
3556 /* Make independent right kcontrol */
3557 if (label)
3558 snprintf(tmpname, sizeof(tmpname),
3559 "Inverted %s Capture %s", label, sfx);
3560 else
3561 snprintf(tmpname, sizeof(tmpname),
3562 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003563 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003564 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003565 if (!knew)
3566 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003567 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003568 knew->put = cap_single_sw_put;
3569 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003570}
3571
3572/* create single (and simple) capture volume and switch controls */
3573static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3574 unsigned int vol_ctl, unsigned int sw_ctl,
3575 bool inv_dmic)
3576{
3577 int err;
3578 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3579 if (err < 0)
3580 return err;
3581 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3582 if (err < 0)
3583 return err;
3584 return 0;
3585}
3586
3587/* create bound capture volume and switch controls */
3588static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3589 unsigned int vol_ctl, unsigned int sw_ctl)
3590{
3591 struct hda_gen_spec *spec = codec->spec;
3592 struct snd_kcontrol_new *knew;
3593
3594 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003595 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003596 if (!knew)
3597 return -ENOMEM;
3598 knew->index = idx;
3599 knew->private_value = vol_ctl;
3600 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3601 }
3602 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003603 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003604 if (!knew)
3605 return -ENOMEM;
3606 knew->index = idx;
3607 knew->private_value = sw_ctl;
3608 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3609 }
3610 return 0;
3611}
3612
3613/* return the vol ctl when used first in the imux list */
3614static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3615{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003616 struct nid_path *path;
3617 unsigned int ctl;
3618 int i;
3619
Takashi Iwaic697b712013-01-07 17:09:26 +01003620 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003621 if (!path)
3622 return 0;
3623 ctl = path->ctls[type];
3624 if (!ctl)
3625 return 0;
3626 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003627 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003628 if (path && path->ctls[type] == ctl)
3629 return 0;
3630 }
3631 return ctl;
3632}
3633
3634/* create individual capture volume and switch controls per input */
3635static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3636{
3637 struct hda_gen_spec *spec = codec->spec;
3638 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003639 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003640
3641 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003642 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003643 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003644
Takashi Iwaic9700422013-01-18 10:17:30 +01003645 idx = imux->items[i].index;
3646 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003647 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003648 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3649
3650 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003651 err = add_single_cap_ctl(codec,
3652 spec->input_labels[idx],
3653 spec->input_label_idxs[idx],
3654 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003655 get_first_cap_ctl(codec, i, type),
3656 inv_dmic);
3657 if (err < 0)
3658 return err;
3659 }
3660 }
3661 return 0;
3662}
3663
3664static int create_capture_mixers(struct hda_codec *codec)
3665{
3666 struct hda_gen_spec *spec = codec->spec;
3667 struct hda_input_mux *imux = &spec->input_mux;
3668 int i, n, nums, err;
3669
3670 if (spec->dyn_adc_switch)
3671 nums = 1;
3672 else
3673 nums = spec->num_adc_nids;
3674
3675 if (!spec->auto_mic && imux->num_items > 1) {
3676 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003677 const char *name;
3678 name = nums > 1 ? "Input Source" : "Capture Source";
3679 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003680 if (!knew)
3681 return -ENOMEM;
3682 knew->count = nums;
3683 }
3684
3685 for (n = 0; n < nums; n++) {
3686 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003687 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003688 bool inv_dmic = false;
3689 int vol, sw;
3690
3691 vol = sw = 0;
3692 for (i = 0; i < imux->num_items; i++) {
3693 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003694 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003695 if (!path)
3696 continue;
3697 parse_capvol_in_path(codec, path);
3698 if (!vol)
3699 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003700 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003701 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003702 if (!same_amp_caps(codec, vol,
3703 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3704 multi_cap_vol = true;
3705 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003706 if (!sw)
3707 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003708 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003709 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003710 if (!same_amp_caps(codec, sw,
3711 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3712 multi_cap_vol = true;
3713 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003714 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3715 inv_dmic = true;
3716 }
3717
3718 if (!multi)
3719 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3720 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003721 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003722 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3723 else
3724 err = create_multi_cap_vol_ctl(codec);
3725 if (err < 0)
3726 return err;
3727 }
3728
3729 return 0;
3730}
3731
3732/*
3733 * add mic boosts if needed
3734 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003735
3736/* check whether the given amp is feasible as a boost volume */
3737static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3738 int dir, int idx)
3739{
3740 unsigned int step;
3741
3742 if (!nid_has_volume(codec, nid, dir) ||
3743 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3744 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3745 return false;
3746
3747 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3748 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3749 if (step < 0x20)
3750 return false;
3751 return true;
3752}
3753
3754/* look for a boost amp in a widget close to the pin */
3755static unsigned int look_for_boost_amp(struct hda_codec *codec,
3756 struct nid_path *path)
3757{
3758 unsigned int val = 0;
3759 hda_nid_t nid;
3760 int depth;
3761
3762 for (depth = 0; depth < 3; depth++) {
3763 if (depth >= path->depth - 1)
3764 break;
3765 nid = path->path[depth];
3766 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3767 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3768 break;
3769 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3770 path->idx[depth])) {
3771 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3772 HDA_INPUT);
3773 break;
3774 }
3775 }
3776
3777 return val;
3778}
3779
Takashi Iwai352f7f92012-12-19 12:52:06 +01003780static int parse_mic_boost(struct hda_codec *codec)
3781{
3782 struct hda_gen_spec *spec = codec->spec;
3783 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003784 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003785 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003786
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003787 if (!spec->num_adc_nids)
3788 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003789
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003790 for (i = 0; i < imux->num_items; i++) {
3791 struct nid_path *path;
3792 unsigned int val;
3793 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003794 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003795
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003796 idx = imux->items[i].index;
3797 if (idx >= imux->num_items)
3798 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003799
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003800 /* check only line-in and mic pins */
Takashi Iwai1799cdd2013-01-18 14:37:16 +01003801 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003802 continue;
3803
3804 path = get_input_path(codec, 0, i);
3805 if (!path)
3806 continue;
3807
3808 val = look_for_boost_amp(codec, path);
3809 if (!val)
3810 continue;
3811
3812 /* create a boost control */
3813 snprintf(boost_label, sizeof(boost_label),
3814 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003815 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3816 spec->input_label_idxs[idx], val))
3817 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003818
3819 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003820 }
3821 return 0;
3822}
3823
3824/*
3825 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3826 */
3827static void parse_digital(struct hda_codec *codec)
3828{
3829 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003830 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003831 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003832 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003833
3834 /* support multiple SPDIFs; the secondary is set up as a slave */
3835 nums = 0;
3836 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003837 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003838 dig_nid = look_for_dac(codec, pin, true);
3839 if (!dig_nid)
3840 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003841 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003842 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003843 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003844 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003845 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003846 path->pin_fixed = true; /* no jack detection */
Takashi Iwai196c17662013-01-04 15:01:40 +01003847 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003848 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003849 if (!nums) {
3850 spec->multiout.dig_out_nid = dig_nid;
3851 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3852 } else {
3853 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3854 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd5764222014-05-14 17:18:31 +03003855 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003856 spec->slave_dig_outs[nums - 1] = dig_nid;
3857 }
3858 nums++;
3859 }
3860
3861 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003862 pin = spec->autocfg.dig_in_pin;
Takashi Iwai7639a062015-03-03 10:07:24 +01003863 for_each_hda_codec_node(dig_nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003864 unsigned int wcaps = get_wcaps(codec, dig_nid);
3865 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3866 continue;
3867 if (!(wcaps & AC_WCAP_DIGITAL))
3868 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003869 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003870 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003871 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003872 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003873 path->pin_fixed = true; /* no jack */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003874 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003875 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003876 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003877 break;
3878 }
3879 }
3880 }
3881}
3882
3883
3884/*
3885 * input MUX handling
3886 */
3887
3888static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3889
3890/* select the given imux item; either unmute exclusively or select the route */
3891static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3892 unsigned int idx)
3893{
3894 struct hda_gen_spec *spec = codec->spec;
3895 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003896 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003897
3898 imux = &spec->input_mux;
3899 if (!imux->num_items)
3900 return 0;
3901
3902 if (idx >= imux->num_items)
3903 idx = imux->num_items - 1;
3904 if (spec->cur_mux[adc_idx] == idx)
3905 return 0;
3906
Takashi Iwai55196ff2013-01-24 17:32:56 +01003907 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3908 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003909 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003910 if (old_path->active)
3911 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003912
3913 spec->cur_mux[adc_idx] = idx;
3914
Takashi Iwai967303d2013-02-19 17:12:42 +01003915 if (spec->hp_mic)
3916 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003917
3918 if (spec->dyn_adc_switch)
3919 dyn_adc_pcm_resetup(codec, idx);
3920
Takashi Iwaic697b712013-01-07 17:09:26 +01003921 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003922 if (!path)
3923 return 0;
3924 if (path->active)
3925 return 0;
3926 snd_hda_activate_path(codec, path, true, false);
3927 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003928 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003929 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003930 return 1;
3931}
3932
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003933/* power up/down widgets in the all paths that match with the given NID
3934 * as terminals (either start- or endpoint)
3935 *
3936 * returns the last changed NID, or zero if unchanged.
3937 */
3938static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3939 int pin_state, int stream_state)
3940{
3941 struct hda_gen_spec *spec = codec->spec;
3942 hda_nid_t last, changed = 0;
3943 struct nid_path *path;
3944 int n;
3945
3946 for (n = 0; n < spec->paths.used; n++) {
3947 path = snd_array_elem(&spec->paths, n);
3948 if (path->path[0] == nid ||
3949 path->path[path->depth - 1] == nid) {
3950 bool pin_old = path->pin_enabled;
3951 bool stream_old = path->stream_enabled;
3952
3953 if (pin_state >= 0)
3954 path->pin_enabled = pin_state;
3955 if (stream_state >= 0)
3956 path->stream_enabled = stream_state;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003957 if ((!path->pin_fixed && path->pin_enabled != pin_old)
3958 || path->stream_enabled != stream_old) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003959 last = path_power_update(codec, path, true);
3960 if (last)
3961 changed = last;
3962 }
3963 }
3964 }
3965 return changed;
3966}
3967
Takashi Iwaid5ac0102015-04-09 10:21:30 +02003968/* check the jack status for power control */
3969static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
3970{
3971 if (!is_jack_detectable(codec, pin))
3972 return true;
3973 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
3974}
3975
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003976/* power up/down the paths of the given pin according to the jack state;
3977 * power = 0/1 : only power up/down if it matches with the jack state,
3978 * < 0 : force power up/down to follow the jack sate
3979 *
3980 * returns the last changed NID, or zero if unchanged.
3981 */
3982static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
3983 int power)
3984{
3985 bool on;
3986
Takashi Iwai967b1302015-03-20 18:21:03 +01003987 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003988 return 0;
3989
Takashi Iwaid5ac0102015-04-09 10:21:30 +02003990 on = detect_pin_state(codec, pin);
3991
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003992 if (power >= 0 && on != power)
3993 return 0;
3994 return set_path_power(codec, pin, on, -1);
3995}
3996
3997static void pin_power_callback(struct hda_codec *codec,
3998 struct hda_jack_callback *jack,
3999 bool on)
4000{
4001 if (jack && jack->tbl->nid)
4002 sync_power_state_change(codec,
4003 set_pin_power_jack(codec, jack->tbl->nid, on));
4004}
4005
4006/* callback only doing power up -- called at first */
4007static void pin_power_up_callback(struct hda_codec *codec,
4008 struct hda_jack_callback *jack)
4009{
4010 pin_power_callback(codec, jack, true);
4011}
4012
4013/* callback only doing power down -- called at last */
4014static void pin_power_down_callback(struct hda_codec *codec,
4015 struct hda_jack_callback *jack)
4016{
4017 pin_power_callback(codec, jack, false);
4018}
4019
4020/* set up the power up/down callbacks */
4021static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4022 const hda_nid_t *pins, bool on)
4023{
4024 int i;
4025 hda_jack_callback_fn cb =
4026 on ? pin_power_up_callback : pin_power_down_callback;
4027
4028 for (i = 0; i < num_pins && pins[i]; i++) {
4029 if (is_jack_detectable(codec, pins[i]))
4030 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4031 else
4032 set_path_power(codec, pins[i], true, -1);
4033 }
4034}
4035
4036/* enabled power callback to each available I/O pin with jack detections;
4037 * the digital I/O pins are excluded because of the unreliable detectsion
4038 */
4039static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4040{
4041 struct hda_gen_spec *spec = codec->spec;
4042 struct auto_pin_cfg *cfg = &spec->autocfg;
4043 int i;
4044
Takashi Iwai967b1302015-03-20 18:21:03 +01004045 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004046 return;
4047 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4048 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4049 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4050 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4051 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4052 for (i = 0; i < cfg->num_inputs; i++)
4053 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4054}
4055
4056/* sync path power up/down with the jack states of given pins */
4057static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4058 const hda_nid_t *pins)
4059{
4060 int i;
4061
4062 for (i = 0; i < num_pins && pins[i]; i++)
4063 if (is_jack_detectable(codec, pins[i]))
4064 set_pin_power_jack(codec, pins[i], -1);
4065}
4066
4067/* sync path power up/down with pins; called at init and resume */
4068static void sync_all_pin_power_ctls(struct hda_codec *codec)
4069{
4070 struct hda_gen_spec *spec = codec->spec;
4071 struct auto_pin_cfg *cfg = &spec->autocfg;
4072 int i;
4073
Takashi Iwai967b1302015-03-20 18:21:03 +01004074 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004075 return;
4076 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4077 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4078 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4079 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4080 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4081 for (i = 0; i < cfg->num_inputs; i++)
4082 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4083}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004084
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004085/* add fake paths if not present yet */
4086static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4087 int num_pins, const hda_nid_t *pins)
4088{
4089 struct hda_gen_spec *spec = codec->spec;
4090 struct nid_path *path;
4091 int i;
4092
4093 for (i = 0; i < num_pins; i++) {
4094 if (!pins[i])
4095 break;
4096 if (get_nid_path(codec, nid, pins[i], 0))
4097 continue;
4098 path = snd_array_new(&spec->paths);
4099 if (!path)
4100 return -ENOMEM;
4101 memset(path, 0, sizeof(*path));
4102 path->depth = 2;
4103 path->path[0] = nid;
4104 path->path[1] = pins[i];
4105 path->active = true;
4106 }
4107 return 0;
4108}
4109
4110/* create fake paths to all outputs from beep */
4111static int add_fake_beep_paths(struct hda_codec *codec)
4112{
4113 struct hda_gen_spec *spec = codec->spec;
4114 struct auto_pin_cfg *cfg = &spec->autocfg;
4115 hda_nid_t nid = spec->beep_nid;
4116 int err;
4117
Takashi Iwai967b1302015-03-20 18:21:03 +01004118 if (!codec->power_save_node || !nid)
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004119 return 0;
4120 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4121 if (err < 0)
4122 return err;
4123 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4124 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4125 if (err < 0)
4126 return err;
4127 }
4128 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4129 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4130 cfg->speaker_pins);
4131 if (err < 0)
4132 return err;
4133 }
4134 return 0;
4135}
4136
4137/* power up/down beep widget and its output paths */
4138static void beep_power_hook(struct hda_beep *beep, bool on)
4139{
4140 set_path_power(beep->codec, beep->nid, -1, on);
4141}
4142
Takashi Iwai6b275b12015-03-20 18:11:05 +01004143/**
4144 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4145 * @codec: the HDA codec
4146 * @pin: NID of pin to fix
4147 */
4148int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4149{
4150 struct hda_gen_spec *spec = codec->spec;
4151 struct nid_path *path;
4152
4153 path = snd_array_new(&spec->paths);
4154 if (!path)
4155 return -ENOMEM;
4156 memset(path, 0, sizeof(*path));
4157 path->depth = 1;
4158 path->path[0] = pin;
4159 path->active = true;
4160 path->pin_fixed = true;
4161 path->stream_enabled = true;
4162 return 0;
4163}
4164EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4165
Takashi Iwai352f7f92012-12-19 12:52:06 +01004166/*
4167 * Jack detections for HP auto-mute and mic-switch
4168 */
4169
4170/* check each pin in the given array; returns true if any of them is plugged */
4171static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4172{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004173 int i;
4174 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004175
4176 for (i = 0; i < num_pins; i++) {
4177 hda_nid_t nid = pins[i];
4178 if (!nid)
4179 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01004180 /* don't detect pins retasked as inputs */
4181 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4182 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004183 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4184 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004185 }
4186 return present;
4187}
4188
4189/* standard HP/line-out auto-mute helper */
4190static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004191 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004192{
4193 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004194 int i;
4195
4196 for (i = 0; i < num_pins; i++) {
4197 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01004198 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004199 if (!nid)
4200 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004201
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004202 oldval = snd_hda_codec_get_pin_target(codec, nid);
4203 if (oldval & PIN_IN)
4204 continue; /* no mute for inputs */
4205
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004206 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004207 struct nid_path *path;
4208 hda_nid_t mute_nid;
4209
4210 path = snd_hda_get_path_from_idx(codec, paths[i]);
4211 if (!path)
4212 continue;
4213 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4214 if (!mute_nid)
4215 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004216 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004217 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004218 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004219 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004220 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004221 } else {
4222 /* don't reset VREF value in case it's controlling
4223 * the amp (see alc861_fixup_asus_amp_vref_0f())
4224 */
4225 if (spec->keep_vref_in_automute)
4226 val = oldval & ~PIN_HP;
4227 else
4228 val = 0;
4229 if (!mute)
4230 val |= oldval;
4231 /* here we call update_pin_ctl() so that the pinctl is
4232 * changed without changing the pinctl target value;
4233 * the original target value will be still referred at
4234 * the init / resume again
4235 */
4236 update_pin_ctl(codec, nid, val);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004237 }
4238
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01004239 set_pin_eapd(codec, nid, !mute);
Takashi Iwai967b1302015-03-20 18:21:03 +01004240 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004241 bool on = !mute;
4242 if (on)
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004243 on = detect_pin_state(codec, nid);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004244 set_path_power(codec, nid, on, -1);
4245 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004246 }
4247}
4248
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004249/**
4250 * snd_hda_gen_update_outputs - Toggle outputs muting
4251 * @codec: the HDA codec
4252 *
4253 * Update the mute status of all outputs based on the current jack states.
4254 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004255void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004256{
4257 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004258 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004259 int on;
4260
4261 /* Control HP pins/amps depending on master_mute state;
4262 * in general, HP pins/amps control should be enabled in all cases,
4263 * but currently set only for master_mute, just to be safe
4264 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004265 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4266 paths = spec->out_paths;
4267 else
4268 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01004269 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004270 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004271
4272 if (!spec->automute_speaker)
4273 on = 0;
4274 else
4275 on = spec->hp_jack_present | spec->line_jack_present;
4276 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004277 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004278 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4279 paths = spec->out_paths;
4280 else
4281 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004282 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004283 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004284
4285 /* toggle line-out mutes if needed, too */
4286 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4287 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4288 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4289 return;
4290 if (!spec->automute_lo)
4291 on = 0;
4292 else
4293 on = spec->hp_jack_present;
4294 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004295 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004296 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004297 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004298 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004299}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004300EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004301
4302static void call_update_outputs(struct hda_codec *codec)
4303{
4304 struct hda_gen_spec *spec = codec->spec;
4305 if (spec->automute_hook)
4306 spec->automute_hook(codec);
4307 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004308 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004309
4310 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4311 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4312 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004313}
4314
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004315/**
4316 * snd_hda_gen_hp_automute - standard HP-automute helper
4317 * @codec: the HDA codec
4318 * @jack: jack object, NULL for the whole
4319 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004320void snd_hda_gen_hp_automute(struct hda_codec *codec,
4321 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004322{
4323 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004324 hda_nid_t *pins = spec->autocfg.hp_pins;
4325 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004326
Takashi Iwai92603c52013-01-22 07:46:31 +01004327 /* No detection for the first HP jack during indep-HP mode */
4328 if (spec->indep_hp_enabled) {
4329 pins++;
4330 num_pins--;
4331 }
4332
4333 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004334 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4335 return;
4336 call_update_outputs(codec);
4337}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004338EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004339
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004340/**
4341 * snd_hda_gen_line_automute - standard line-out-automute helper
4342 * @codec: the HDA codec
4343 * @jack: jack object, NULL for the whole
4344 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004345void snd_hda_gen_line_automute(struct hda_codec *codec,
4346 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004347{
4348 struct hda_gen_spec *spec = codec->spec;
4349
4350 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4351 return;
4352 /* check LO jack only when it's different from HP */
4353 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4354 return;
4355
4356 spec->line_jack_present =
4357 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4358 spec->autocfg.line_out_pins);
4359 if (!spec->automute_speaker || !spec->detect_lo)
4360 return;
4361 call_update_outputs(codec);
4362}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004363EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004364
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004365/**
4366 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4367 * @codec: the HDA codec
4368 * @jack: jack object, NULL for the whole
4369 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004370void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4371 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004372{
4373 struct hda_gen_spec *spec = codec->spec;
4374 int i;
4375
4376 if (!spec->auto_mic)
4377 return;
4378
4379 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004380 hda_nid_t pin = spec->am_entry[i].pin;
4381 /* don't detect pins retasked as outputs */
4382 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4383 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004384 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004385 mux_select(codec, 0, spec->am_entry[i].idx);
4386 return;
4387 }
4388 }
4389 mux_select(codec, 0, spec->am_entry[0].idx);
4390}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004391EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004392
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004393/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004394static void call_hp_automute(struct hda_codec *codec,
4395 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004396{
4397 struct hda_gen_spec *spec = codec->spec;
4398 if (spec->hp_automute_hook)
4399 spec->hp_automute_hook(codec, jack);
4400 else
4401 snd_hda_gen_hp_automute(codec, jack);
4402}
4403
4404static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004405 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004406{
4407 struct hda_gen_spec *spec = codec->spec;
4408 if (spec->line_automute_hook)
4409 spec->line_automute_hook(codec, jack);
4410 else
4411 snd_hda_gen_line_automute(codec, jack);
4412}
4413
4414static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004415 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004416{
4417 struct hda_gen_spec *spec = codec->spec;
4418 if (spec->mic_autoswitch_hook)
4419 spec->mic_autoswitch_hook(codec, jack);
4420 else
4421 snd_hda_gen_mic_autoswitch(codec, jack);
4422}
4423
Takashi Iwai963afde2013-05-31 15:20:31 +02004424/* update jack retasking */
4425static void update_automute_all(struct hda_codec *codec)
4426{
4427 call_hp_automute(codec, NULL);
4428 call_line_automute(codec, NULL);
4429 call_mic_autoswitch(codec, NULL);
4430}
4431
Takashi Iwai352f7f92012-12-19 12:52:06 +01004432/*
4433 * Auto-Mute mode mixer enum support
4434 */
4435static int automute_mode_info(struct snd_kcontrol *kcontrol,
4436 struct snd_ctl_elem_info *uinfo)
4437{
4438 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4439 struct hda_gen_spec *spec = codec->spec;
4440 static const char * const texts3[] = {
4441 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004442 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Takashi Iwai352f7f92012-12-19 12:52:06 +01004444 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4445 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4446 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4447}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
Takashi Iwai352f7f92012-12-19 12:52:06 +01004449static int automute_mode_get(struct snd_kcontrol *kcontrol,
4450 struct snd_ctl_elem_value *ucontrol)
4451{
4452 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4453 struct hda_gen_spec *spec = codec->spec;
4454 unsigned int val = 0;
4455 if (spec->automute_speaker)
4456 val++;
4457 if (spec->automute_lo)
4458 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004459
Takashi Iwai352f7f92012-12-19 12:52:06 +01004460 ucontrol->value.enumerated.item[0] = val;
4461 return 0;
4462}
4463
4464static int automute_mode_put(struct snd_kcontrol *kcontrol,
4465 struct snd_ctl_elem_value *ucontrol)
4466{
4467 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4468 struct hda_gen_spec *spec = codec->spec;
4469
4470 switch (ucontrol->value.enumerated.item[0]) {
4471 case 0:
4472 if (!spec->automute_speaker && !spec->automute_lo)
4473 return 0;
4474 spec->automute_speaker = 0;
4475 spec->automute_lo = 0;
4476 break;
4477 case 1:
4478 if (spec->automute_speaker_possible) {
4479 if (!spec->automute_lo && spec->automute_speaker)
4480 return 0;
4481 spec->automute_speaker = 1;
4482 spec->automute_lo = 0;
4483 } else if (spec->automute_lo_possible) {
4484 if (spec->automute_lo)
4485 return 0;
4486 spec->automute_lo = 1;
4487 } else
4488 return -EINVAL;
4489 break;
4490 case 2:
4491 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4492 return -EINVAL;
4493 if (spec->automute_speaker && spec->automute_lo)
4494 return 0;
4495 spec->automute_speaker = 1;
4496 spec->automute_lo = 1;
4497 break;
4498 default:
4499 return -EINVAL;
4500 }
4501 call_update_outputs(codec);
4502 return 1;
4503}
4504
4505static const struct snd_kcontrol_new automute_mode_enum = {
4506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4507 .name = "Auto-Mute Mode",
4508 .info = automute_mode_info,
4509 .get = automute_mode_get,
4510 .put = automute_mode_put,
4511};
4512
4513static int add_automute_mode_enum(struct hda_codec *codec)
4514{
4515 struct hda_gen_spec *spec = codec->spec;
4516
Takashi Iwai12c93df2012-12-19 14:38:33 +01004517 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004518 return -ENOMEM;
4519 return 0;
4520}
4521
4522/*
4523 * Check the availability of HP/line-out auto-mute;
4524 * Set up appropriately if really supported
4525 */
4526static int check_auto_mute_availability(struct hda_codec *codec)
4527{
4528 struct hda_gen_spec *spec = codec->spec;
4529 struct auto_pin_cfg *cfg = &spec->autocfg;
4530 int present = 0;
4531 int i, err;
4532
Takashi Iwaif72706b2013-01-16 18:20:07 +01004533 if (spec->suppress_auto_mute)
4534 return 0;
4535
Takashi Iwai352f7f92012-12-19 12:52:06 +01004536 if (cfg->hp_pins[0])
4537 present++;
4538 if (cfg->line_out_pins[0])
4539 present++;
4540 if (cfg->speaker_pins[0])
4541 present++;
4542 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004543 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004544
4545 if (!cfg->speaker_pins[0] &&
4546 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4547 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4548 sizeof(cfg->speaker_pins));
4549 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
Takashi Iwai352f7f92012-12-19 12:52:06 +01004552 if (!cfg->hp_pins[0] &&
4553 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4554 memcpy(cfg->hp_pins, cfg->line_out_pins,
4555 sizeof(cfg->hp_pins));
4556 cfg->hp_outs = cfg->line_outs;
4557 }
4558
4559 for (i = 0; i < cfg->hp_outs; i++) {
4560 hda_nid_t nid = cfg->hp_pins[i];
4561 if (!is_jack_detectable(codec, nid))
4562 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004563 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004564 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004565 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004566 spec->detect_hp = 1;
4567 }
4568
4569 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4570 if (cfg->speaker_outs)
4571 for (i = 0; i < cfg->line_outs; i++) {
4572 hda_nid_t nid = cfg->line_out_pins[i];
4573 if (!is_jack_detectable(codec, nid))
4574 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004575 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004576 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004577 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004578 spec->detect_lo = 1;
4579 }
4580 spec->automute_lo_possible = spec->detect_hp;
4581 }
4582
4583 spec->automute_speaker_possible = cfg->speaker_outs &&
4584 (spec->detect_hp || spec->detect_lo);
4585
4586 spec->automute_lo = spec->automute_lo_possible;
4587 spec->automute_speaker = spec->automute_speaker_possible;
4588
4589 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4590 /* create a control for automute mode */
4591 err = add_automute_mode_enum(codec);
4592 if (err < 0)
4593 return err;
4594 }
4595 return 0;
4596}
4597
Takashi Iwai352f7f92012-12-19 12:52:06 +01004598/* check whether all auto-mic pins are valid; setup indices if OK */
4599static bool auto_mic_check_imux(struct hda_codec *codec)
4600{
4601 struct hda_gen_spec *spec = codec->spec;
4602 const struct hda_input_mux *imux;
4603 int i;
4604
4605 imux = &spec->input_mux;
4606 for (i = 0; i < spec->am_num_entries; i++) {
4607 spec->am_entry[i].idx =
4608 find_idx_in_nid_list(spec->am_entry[i].pin,
4609 spec->imux_pins, imux->num_items);
4610 if (spec->am_entry[i].idx < 0)
4611 return false; /* no corresponding imux */
4612 }
4613
4614 /* we don't need the jack detection for the first pin */
4615 for (i = 1; i < spec->am_num_entries; i++)
4616 snd_hda_jack_detect_enable_callback(codec,
4617 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004618 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004619 return true;
4620}
4621
4622static int compare_attr(const void *ap, const void *bp)
4623{
4624 const struct automic_entry *a = ap;
4625 const struct automic_entry *b = bp;
4626 return (int)(a->attr - b->attr);
4627}
4628
4629/*
4630 * Check the availability of auto-mic switch;
4631 * Set up if really supported
4632 */
4633static int check_auto_mic_availability(struct hda_codec *codec)
4634{
4635 struct hda_gen_spec *spec = codec->spec;
4636 struct auto_pin_cfg *cfg = &spec->autocfg;
4637 unsigned int types;
4638 int i, num_pins;
4639
Takashi Iwaid12daf62013-01-07 16:32:11 +01004640 if (spec->suppress_auto_mic)
4641 return 0;
4642
Takashi Iwai352f7f92012-12-19 12:52:06 +01004643 types = 0;
4644 num_pins = 0;
4645 for (i = 0; i < cfg->num_inputs; i++) {
4646 hda_nid_t nid = cfg->inputs[i].pin;
4647 unsigned int attr;
4648 attr = snd_hda_codec_get_pincfg(codec, nid);
4649 attr = snd_hda_get_input_pin_attr(attr);
4650 if (types & (1 << attr))
4651 return 0; /* already occupied */
4652 switch (attr) {
4653 case INPUT_PIN_ATTR_INT:
4654 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4655 return 0; /* invalid type */
4656 break;
4657 case INPUT_PIN_ATTR_UNUSED:
4658 return 0; /* invalid entry */
4659 default:
4660 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4661 return 0; /* invalid type */
4662 if (!spec->line_in_auto_switch &&
4663 cfg->inputs[i].type != AUTO_PIN_MIC)
4664 return 0; /* only mic is allowed */
4665 if (!is_jack_detectable(codec, nid))
4666 return 0; /* no unsol support */
4667 break;
4668 }
4669 if (num_pins >= MAX_AUTO_MIC_PINS)
4670 return 0;
4671 types |= (1 << attr);
4672 spec->am_entry[num_pins].pin = nid;
4673 spec->am_entry[num_pins].attr = attr;
4674 num_pins++;
4675 }
4676
4677 if (num_pins < 2)
4678 return 0;
4679
4680 spec->am_num_entries = num_pins;
4681 /* sort the am_entry in the order of attr so that the pin with a
4682 * higher attr will be selected when the jack is plugged.
4683 */
4684 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4685 compare_attr, NULL);
4686
4687 if (!auto_mic_check_imux(codec))
4688 return 0;
4689
4690 spec->auto_mic = 1;
4691 spec->num_adc_nids = 1;
4692 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004693 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004694 spec->am_entry[0].pin,
4695 spec->am_entry[1].pin,
4696 spec->am_entry[2].pin);
4697
4698 return 0;
4699}
4700
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004701/**
4702 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4703 * into power down
4704 * @codec: the HDA codec
4705 * @nid: NID to evalute
4706 * @power_state: target power state
4707 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004708unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004709 hda_nid_t nid,
4710 unsigned int power_state)
4711{
Takashi Iwaib6c09b32015-04-09 10:25:03 +02004712 struct hda_gen_spec *spec = codec->spec;
4713
4714 if (!spec->power_down_unused && !codec->power_save_node)
4715 return power_state;
Takashi Iwai7639a062015-03-03 10:07:24 +01004716 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004717 return power_state;
4718 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4719 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004720 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004721 return power_state;
4722 return AC_PWRST_D3;
4723}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004724EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004725
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004726/* mute all aamix inputs initially; parse up to the first leaves */
4727static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4728{
4729 int i, nums;
4730 const hda_nid_t *conn;
4731 bool has_amp;
4732
4733 nums = snd_hda_get_conn_list(codec, mix, &conn);
4734 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4735 for (i = 0; i < nums; i++) {
4736 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004737 update_amp(codec, mix, HDA_INPUT, i,
4738 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004739 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004740 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4741 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004742 }
4743}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004744
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004745/**
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004746 * snd_hda_gen_stream_pm - Stream power management callback
4747 * @codec: the HDA codec
4748 * @nid: audio widget
4749 * @on: power on/off flag
4750 *
Takashi Iwai967b1302015-03-20 18:21:03 +01004751 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004752 */
4753void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4754{
Takashi Iwai967b1302015-03-20 18:21:03 +01004755 if (codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004756 set_path_power(codec, nid, -1, on);
4757}
4758EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4759
4760/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004761 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4762 * set up the hda_gen_spec
4763 * @codec: the HDA codec
4764 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004765 *
4766 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004767 * or a negative error code
4768 */
4769int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004770 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004771{
4772 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004773 int err;
4774
Takashi Iwai1c70a582013-01-11 17:48:22 +01004775 parse_user_hints(codec);
4776
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004777 if (spec->mixer_nid && !spec->mixer_merge_nid)
4778 spec->mixer_merge_nid = spec->mixer_nid;
4779
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004780 if (cfg != &spec->autocfg) {
4781 spec->autocfg = *cfg;
4782 cfg = &spec->autocfg;
4783 }
4784
Takashi Iwai98bd1112013-03-22 14:53:50 +01004785 if (!spec->main_out_badness)
4786 spec->main_out_badness = &hda_main_out_badness;
4787 if (!spec->extra_out_badness)
4788 spec->extra_out_badness = &hda_extra_out_badness;
4789
David Henningsson6fc4cb92013-01-16 15:58:45 +01004790 fill_all_dac_nids(codec);
4791
Takashi Iwai352f7f92012-12-19 12:52:06 +01004792 if (!cfg->line_outs) {
4793 if (cfg->dig_outs || cfg->dig_in_pin) {
4794 spec->multiout.max_channels = 2;
4795 spec->no_analog = 1;
4796 goto dig_only;
4797 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004798 if (!cfg->num_inputs && !cfg->dig_in_pin)
4799 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004800 }
4801
4802 if (!spec->no_primary_hp &&
4803 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4804 cfg->line_outs <= cfg->hp_outs) {
4805 /* use HP as primary out */
4806 cfg->speaker_outs = cfg->line_outs;
4807 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4808 sizeof(cfg->speaker_pins));
4809 cfg->line_outs = cfg->hp_outs;
4810 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4811 cfg->hp_outs = 0;
4812 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4813 cfg->line_out_type = AUTO_PIN_HP_OUT;
4814 }
4815
4816 err = parse_output_paths(codec);
4817 if (err < 0)
4818 return err;
4819 err = create_multi_channel_mode(codec);
4820 if (err < 0)
4821 return err;
4822 err = create_multi_out_ctls(codec, cfg);
4823 if (err < 0)
4824 return err;
4825 err = create_hp_out_ctls(codec);
4826 if (err < 0)
4827 return err;
4828 err = create_speaker_out_ctls(codec);
4829 if (err < 0)
4830 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004831 err = create_indep_hp_ctls(codec);
4832 if (err < 0)
4833 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004834 err = create_loopback_mixing_ctl(codec);
4835 if (err < 0)
4836 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004837 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004838 if (err < 0)
4839 return err;
4840 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004841 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004842 return err;
4843
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004844 /* add power-down pin callbacks at first */
4845 add_all_pin_power_ctls(codec, false);
4846
Takashi Iwaia07a9492013-01-07 16:44:06 +01004847 spec->const_channel_count = spec->ext_channel_count;
4848 /* check the multiple speaker and headphone pins */
4849 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4850 spec->const_channel_count = max(spec->const_channel_count,
4851 cfg->speaker_outs * 2);
4852 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4853 spec->const_channel_count = max(spec->const_channel_count,
4854 cfg->hp_outs * 2);
4855 spec->multiout.max_channels = max(spec->ext_channel_count,
4856 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004857
4858 err = check_auto_mute_availability(codec);
4859 if (err < 0)
4860 return err;
4861
4862 err = check_dyn_adc_switch(codec);
4863 if (err < 0)
4864 return err;
4865
Takashi Iwai967303d2013-02-19 17:12:42 +01004866 err = check_auto_mic_availability(codec);
4867 if (err < 0)
4868 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004869
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004870 /* add stereo mix if available and not enabled yet */
4871 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01004872 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4873 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004874 err = parse_capture_source(codec, spec->mixer_nid,
4875 CFG_IDX_MIX, spec->num_all_adcs,
4876 "Stereo Mix", 0);
4877 if (err < 0)
4878 return err;
4879 }
4880
4881
Takashi Iwai352f7f92012-12-19 12:52:06 +01004882 err = create_capture_mixers(codec);
4883 if (err < 0)
4884 return err;
4885
4886 err = parse_mic_boost(codec);
4887 if (err < 0)
4888 return err;
4889
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004890 /* create "Headphone Mic Jack Mode" if no input selection is
4891 * available (or user specifies add_jack_modes hint)
4892 */
4893 if (spec->hp_mic_pin &&
4894 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4895 spec->add_jack_modes)) {
4896 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4897 if (err < 0)
4898 return err;
4899 }
4900
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004901 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004902 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4903 err = create_out_jack_modes(codec, cfg->line_outs,
4904 cfg->line_out_pins);
4905 if (err < 0)
4906 return err;
4907 }
4908 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4909 err = create_out_jack_modes(codec, cfg->hp_outs,
4910 cfg->hp_pins);
4911 if (err < 0)
4912 return err;
4913 }
4914 }
4915
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004916 /* add power-up pin callbacks at last */
4917 add_all_pin_power_ctls(codec, true);
4918
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004919 /* mute all aamix input initially */
4920 if (spec->mixer_nid)
4921 mute_all_mixer_nid(codec, spec->mixer_nid);
4922
Takashi Iwai352f7f92012-12-19 12:52:06 +01004923 dig_only:
4924 parse_digital(codec);
4925
Takashi Iwai49fb1892015-05-27 08:37:19 +02004926 if (spec->power_down_unused || codec->power_save_node) {
Takashi Iwai24fef902015-04-09 10:28:15 +02004927 if (!codec->power_filter)
4928 codec->power_filter = snd_hda_gen_path_power_filter;
Takashi Iwai49fb1892015-05-27 08:37:19 +02004929 if (!codec->patch_ops.stream_pm)
4930 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
4931 }
Takashi Iwai55196ff2013-01-24 17:32:56 +01004932
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004933 if (!spec->no_analog && spec->beep_nid) {
4934 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4935 if (err < 0)
4936 return err;
Takashi Iwai967b1302015-03-20 18:21:03 +01004937 if (codec->beep && codec->power_save_node) {
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004938 err = add_fake_beep_paths(codec);
4939 if (err < 0)
4940 return err;
4941 codec->beep->power_hook = beep_power_hook;
4942 }
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004943 }
4944
Takashi Iwai352f7f92012-12-19 12:52:06 +01004945 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004947EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
4949
4950/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004951 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004953
4954/* slave controls for virtual master */
4955static const char * const slave_pfxs[] = {
4956 "Front", "Surround", "Center", "LFE", "Side",
4957 "Headphone", "Speaker", "Mono", "Line Out",
4958 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004959 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4960 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02004961 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004962 NULL,
4963};
4964
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004965/**
4966 * snd_hda_gen_build_controls - Build controls from the parsed results
4967 * @codec: the HDA codec
4968 *
4969 * Pass this to build_controls patch_ops.
4970 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004971int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004973 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975
Takashi Iwai36502d02012-12-19 15:15:10 +01004976 if (spec->kctls.used) {
4977 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4978 if (err < 0)
4979 return err;
4980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
Takashi Iwai352f7f92012-12-19 12:52:06 +01004982 if (spec->multiout.dig_out_nid) {
4983 err = snd_hda_create_dig_out_ctls(codec,
4984 spec->multiout.dig_out_nid,
4985 spec->multiout.dig_out_nid,
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004986 spec->pcm_rec[1]->pcm_type);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004987 if (err < 0)
4988 return err;
4989 if (!spec->no_analog) {
4990 err = snd_hda_create_spdif_share_sw(codec,
4991 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 if (err < 0)
4993 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004994 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 }
4996 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004997 if (spec->dig_in_nid) {
4998 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4999 if (err < 0)
5000 return err;
5001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
Takashi Iwai352f7f92012-12-19 12:52:06 +01005003 /* if we have no master control, let's create it */
5004 if (!spec->no_analog &&
5005 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01005006 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01005007 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005008 "Playback Volume");
5009 if (err < 0)
5010 return err;
5011 }
5012 if (!spec->no_analog &&
5013 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5014 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5015 NULL, slave_pfxs,
5016 "Playback Switch",
5017 true, &spec->vmaster_mute.sw_kctl);
5018 if (err < 0)
5019 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02005020 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01005021 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5022 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02005023 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5024 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Takashi Iwai352f7f92012-12-19 12:52:06 +01005027 free_kctls(spec); /* no longer needed */
5028
Takashi Iwai352f7f92012-12-19 12:52:06 +01005029 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5030 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 return err;
5032
5033 return 0;
5034}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005035EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005036
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
5038/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005039 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005042static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5043 struct hda_codec *codec,
5044 struct snd_pcm_substream *substream,
5045 int action)
5046{
5047 struct hda_gen_spec *spec = codec->spec;
5048 if (spec->pcm_playback_hook)
5049 spec->pcm_playback_hook(hinfo, codec, substream, action);
5050}
5051
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005052static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5053 struct hda_codec *codec,
5054 struct snd_pcm_substream *substream,
5055 int action)
5056{
5057 struct hda_gen_spec *spec = codec->spec;
5058 if (spec->pcm_capture_hook)
5059 spec->pcm_capture_hook(hinfo, codec, substream, action);
5060}
5061
Takashi Iwai352f7f92012-12-19 12:52:06 +01005062/*
5063 * Analog playback callbacks
5064 */
5065static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5066 struct hda_codec *codec,
5067 struct snd_pcm_substream *substream)
5068{
5069 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005070 int err;
5071
5072 mutex_lock(&spec->pcm_mutex);
5073 err = snd_hda_multi_out_analog_open(codec,
5074 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005075 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005076 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005077 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005078 call_pcm_playback_hook(hinfo, codec, substream,
5079 HDA_GEN_PCM_ACT_OPEN);
5080 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005081 mutex_unlock(&spec->pcm_mutex);
5082 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005083}
5084
5085static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01005086 struct hda_codec *codec,
5087 unsigned int stream_tag,
5088 unsigned int format,
5089 struct snd_pcm_substream *substream)
5090{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005091 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005092 int err;
5093
5094 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5095 stream_tag, format, substream);
5096 if (!err)
5097 call_pcm_playback_hook(hinfo, codec, substream,
5098 HDA_GEN_PCM_ACT_PREPARE);
5099 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005100}
Takashi Iwai97ec5582006-03-21 11:29:07 +01005101
Takashi Iwai352f7f92012-12-19 12:52:06 +01005102static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5103 struct hda_codec *codec,
5104 struct snd_pcm_substream *substream)
5105{
5106 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005107 int err;
5108
5109 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5110 if (!err)
5111 call_pcm_playback_hook(hinfo, codec, substream,
5112 HDA_GEN_PCM_ACT_CLEANUP);
5113 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005114}
5115
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005116static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5117 struct hda_codec *codec,
5118 struct snd_pcm_substream *substream)
5119{
5120 struct hda_gen_spec *spec = codec->spec;
5121 mutex_lock(&spec->pcm_mutex);
5122 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005123 call_pcm_playback_hook(hinfo, codec, substream,
5124 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005125 mutex_unlock(&spec->pcm_mutex);
5126 return 0;
5127}
5128
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005129static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5130 struct hda_codec *codec,
5131 struct snd_pcm_substream *substream)
5132{
5133 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5134 return 0;
5135}
5136
5137static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5138 struct hda_codec *codec,
5139 unsigned int stream_tag,
5140 unsigned int format,
5141 struct snd_pcm_substream *substream)
5142{
5143 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5144 call_pcm_capture_hook(hinfo, codec, substream,
5145 HDA_GEN_PCM_ACT_PREPARE);
5146 return 0;
5147}
5148
5149static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5150 struct hda_codec *codec,
5151 struct snd_pcm_substream *substream)
5152{
5153 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5154 call_pcm_capture_hook(hinfo, codec, substream,
5155 HDA_GEN_PCM_ACT_CLEANUP);
5156 return 0;
5157}
5158
5159static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5160 struct hda_codec *codec,
5161 struct snd_pcm_substream *substream)
5162{
5163 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5164 return 0;
5165}
5166
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005167static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5168 struct hda_codec *codec,
5169 struct snd_pcm_substream *substream)
5170{
5171 struct hda_gen_spec *spec = codec->spec;
5172 int err = 0;
5173
5174 mutex_lock(&spec->pcm_mutex);
Takashi Iwaid1f15e02015-07-08 09:22:10 +02005175 if (spec->indep_hp && !spec->indep_hp_enabled)
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005176 err = -EBUSY;
5177 else
5178 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005179 call_pcm_playback_hook(hinfo, codec, substream,
5180 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005181 mutex_unlock(&spec->pcm_mutex);
5182 return err;
5183}
5184
5185static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5186 struct hda_codec *codec,
5187 struct snd_pcm_substream *substream)
5188{
5189 struct hda_gen_spec *spec = codec->spec;
5190 mutex_lock(&spec->pcm_mutex);
5191 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005192 call_pcm_playback_hook(hinfo, codec, substream,
5193 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005194 mutex_unlock(&spec->pcm_mutex);
5195 return 0;
5196}
5197
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005198static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5199 struct hda_codec *codec,
5200 unsigned int stream_tag,
5201 unsigned int format,
5202 struct snd_pcm_substream *substream)
5203{
5204 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5205 call_pcm_playback_hook(hinfo, codec, substream,
5206 HDA_GEN_PCM_ACT_PREPARE);
5207 return 0;
5208}
5209
5210static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5211 struct hda_codec *codec,
5212 struct snd_pcm_substream *substream)
5213{
5214 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5215 call_pcm_playback_hook(hinfo, codec, substream,
5216 HDA_GEN_PCM_ACT_CLEANUP);
5217 return 0;
5218}
5219
Takashi Iwai352f7f92012-12-19 12:52:06 +01005220/*
5221 * Digital out
5222 */
5223static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5224 struct hda_codec *codec,
5225 struct snd_pcm_substream *substream)
5226{
5227 struct hda_gen_spec *spec = codec->spec;
5228 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5229}
5230
5231static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5232 struct hda_codec *codec,
5233 unsigned int stream_tag,
5234 unsigned int format,
5235 struct snd_pcm_substream *substream)
5236{
5237 struct hda_gen_spec *spec = codec->spec;
5238 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5239 stream_tag, format, substream);
5240}
5241
5242static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5243 struct hda_codec *codec,
5244 struct snd_pcm_substream *substream)
5245{
5246 struct hda_gen_spec *spec = codec->spec;
5247 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5248}
5249
5250static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5251 struct hda_codec *codec,
5252 struct snd_pcm_substream *substream)
5253{
5254 struct hda_gen_spec *spec = codec->spec;
5255 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5256}
5257
5258/*
5259 * Analog capture
5260 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005261#define alt_capture_pcm_open capture_pcm_open
5262#define alt_capture_pcm_close capture_pcm_close
5263
Takashi Iwai352f7f92012-12-19 12:52:06 +01005264static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5265 struct hda_codec *codec,
5266 unsigned int stream_tag,
5267 unsigned int format,
5268 struct snd_pcm_substream *substream)
5269{
5270 struct hda_gen_spec *spec = codec->spec;
5271
5272 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01005273 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005274 call_pcm_capture_hook(hinfo, codec, substream,
5275 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005276 return 0;
5277}
5278
Takashi Iwai352f7f92012-12-19 12:52:06 +01005279static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5280 struct hda_codec *codec,
5281 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01005282{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005283 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01005284
Takashi Iwai352f7f92012-12-19 12:52:06 +01005285 snd_hda_codec_cleanup_stream(codec,
5286 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005287 call_pcm_capture_hook(hinfo, codec, substream,
5288 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005289 return 0;
5290}
5291
Takashi Iwai352f7f92012-12-19 12:52:06 +01005292/*
5293 */
5294static const struct hda_pcm_stream pcm_analog_playback = {
5295 .substreams = 1,
5296 .channels_min = 2,
5297 .channels_max = 8,
5298 /* NID is set in build_pcms */
5299 .ops = {
5300 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005301 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005302 .prepare = playback_pcm_prepare,
5303 .cleanup = playback_pcm_cleanup
5304 },
5305};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306
Takashi Iwai352f7f92012-12-19 12:52:06 +01005307static const struct hda_pcm_stream pcm_analog_capture = {
5308 .substreams = 1,
5309 .channels_min = 2,
5310 .channels_max = 2,
5311 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005312 .ops = {
5313 .open = capture_pcm_open,
5314 .close = capture_pcm_close,
5315 .prepare = capture_pcm_prepare,
5316 .cleanup = capture_pcm_cleanup
5317 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005318};
5319
5320static const struct hda_pcm_stream pcm_analog_alt_playback = {
5321 .substreams = 1,
5322 .channels_min = 2,
5323 .channels_max = 2,
5324 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005325 .ops = {
5326 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005327 .close = alt_playback_pcm_close,
5328 .prepare = alt_playback_pcm_prepare,
5329 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005330 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005331};
5332
5333static const struct hda_pcm_stream pcm_analog_alt_capture = {
5334 .substreams = 2, /* can be overridden */
5335 .channels_min = 2,
5336 .channels_max = 2,
5337 /* NID is set in build_pcms */
5338 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005339 .open = alt_capture_pcm_open,
5340 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005341 .prepare = alt_capture_pcm_prepare,
5342 .cleanup = alt_capture_pcm_cleanup
5343 },
5344};
5345
5346static const struct hda_pcm_stream pcm_digital_playback = {
5347 .substreams = 1,
5348 .channels_min = 2,
5349 .channels_max = 2,
5350 /* NID is set in build_pcms */
5351 .ops = {
5352 .open = dig_playback_pcm_open,
5353 .close = dig_playback_pcm_close,
5354 .prepare = dig_playback_pcm_prepare,
5355 .cleanup = dig_playback_pcm_cleanup
5356 },
5357};
5358
5359static const struct hda_pcm_stream pcm_digital_capture = {
5360 .substreams = 1,
5361 .channels_min = 2,
5362 .channels_max = 2,
5363 /* NID is set in build_pcms */
5364};
5365
5366/* Used by build_pcms to flag that a PCM has no playback stream */
5367static const struct hda_pcm_stream pcm_null_stream = {
5368 .substreams = 0,
5369 .channels_min = 0,
5370 .channels_max = 0,
5371};
5372
5373/*
5374 * dynamic changing ADC PCM streams
5375 */
5376static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5377{
5378 struct hda_gen_spec *spec = codec->spec;
5379 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5380
5381 if (spec->cur_adc && spec->cur_adc != new_adc) {
5382 /* stream is running, let's swap the current ADC */
5383 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5384 spec->cur_adc = new_adc;
5385 snd_hda_codec_setup_stream(codec, new_adc,
5386 spec->cur_adc_stream_tag, 0,
5387 spec->cur_adc_format);
5388 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005390 return false;
5391}
5392
5393/* analog capture with dynamic dual-adc changes */
5394static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5395 struct hda_codec *codec,
5396 unsigned int stream_tag,
5397 unsigned int format,
5398 struct snd_pcm_substream *substream)
5399{
5400 struct hda_gen_spec *spec = codec->spec;
5401 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5402 spec->cur_adc_stream_tag = stream_tag;
5403 spec->cur_adc_format = format;
5404 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5405 return 0;
5406}
5407
5408static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5409 struct hda_codec *codec,
5410 struct snd_pcm_substream *substream)
5411{
5412 struct hda_gen_spec *spec = codec->spec;
5413 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5414 spec->cur_adc = 0;
5415 return 0;
5416}
5417
5418static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5419 .substreams = 1,
5420 .channels_min = 2,
5421 .channels_max = 2,
5422 .nid = 0, /* fill later */
5423 .ops = {
5424 .prepare = dyn_adc_capture_pcm_prepare,
5425 .cleanup = dyn_adc_capture_pcm_cleanup
5426 },
5427};
5428
Takashi Iwaif873e532012-12-20 16:58:39 +01005429static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5430 const char *chip_name)
5431{
5432 char *p;
5433
5434 if (*str)
5435 return;
5436 strlcpy(str, chip_name, len);
5437
5438 /* drop non-alnum chars after a space */
5439 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5440 if (!isalnum(p[1])) {
5441 *p = 0;
5442 break;
5443 }
5444 }
5445 strlcat(str, sfx, len);
5446}
5447
Takashi Iwaifb83b632015-03-16 23:34:34 +01005448/* copy PCM stream info from @default_str, and override non-NULL entries
5449 * from @spec_str and @nid
5450 */
5451static void setup_pcm_stream(struct hda_pcm_stream *str,
5452 const struct hda_pcm_stream *default_str,
5453 const struct hda_pcm_stream *spec_str,
5454 hda_nid_t nid)
5455{
5456 *str = *default_str;
5457 if (nid)
5458 str->nid = nid;
5459 if (spec_str) {
5460 if (spec_str->substreams)
5461 str->substreams = spec_str->substreams;
5462 if (spec_str->channels_min)
5463 str->channels_min = spec_str->channels_min;
5464 if (spec_str->channels_max)
5465 str->channels_max = spec_str->channels_max;
5466 if (spec_str->rates)
5467 str->rates = spec_str->rates;
5468 if (spec_str->formats)
5469 str->formats = spec_str->formats;
5470 if (spec_str->maxbps)
5471 str->maxbps = spec_str->maxbps;
5472 }
5473}
5474
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005475/**
5476 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5477 * @codec: the HDA codec
5478 *
5479 * Pass this to build_pcms patch_ops.
5480 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005481int snd_hda_gen_build_pcms(struct hda_codec *codec)
5482{
5483 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005484 struct hda_pcm *info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005485 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486
Takashi Iwai352f7f92012-12-19 12:52:06 +01005487 if (spec->no_analog)
5488 goto skip_analog;
5489
Takashi Iwaif873e532012-12-20 16:58:39 +01005490 fill_pcm_stream_name(spec->stream_name_analog,
5491 sizeof(spec->stream_name_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005492 " Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005493 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5494 if (!info)
5495 return -ENOMEM;
5496 spec->pcm_rec[0] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005497
5498 if (spec->multiout.num_dacs > 0) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005499 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5500 &pcm_analog_playback,
5501 spec->stream_analog_playback,
5502 spec->multiout.dac_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005503 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5504 spec->multiout.max_channels;
5505 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5506 spec->autocfg.line_outs == 2)
5507 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5508 snd_pcm_2_1_chmaps;
5509 }
5510 if (spec->num_adc_nids) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005511 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5512 (spec->dyn_adc_switch ?
5513 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5514 spec->stream_analog_capture,
5515 spec->adc_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005516 }
5517
Takashi Iwai352f7f92012-12-19 12:52:06 +01005518 skip_analog:
5519 /* SPDIF for stream index #1 */
5520 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005521 fill_pcm_stream_name(spec->stream_name_digital,
5522 sizeof(spec->stream_name_digital),
Takashi Iwai7639a062015-03-03 10:07:24 +01005523 " Digital", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005524 info = snd_hda_codec_pcm_new(codec, "%s",
5525 spec->stream_name_digital);
5526 if (!info)
5527 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005528 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005529 spec->pcm_rec[1] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005530 if (spec->dig_out_type)
5531 info->pcm_type = spec->dig_out_type;
5532 else
5533 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005534 if (spec->multiout.dig_out_nid)
5535 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5536 &pcm_digital_playback,
5537 spec->stream_digital_playback,
5538 spec->multiout.dig_out_nid);
5539 if (spec->dig_in_nid)
5540 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5541 &pcm_digital_capture,
5542 spec->stream_digital_capture,
5543 spec->dig_in_nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005544 }
5545
5546 if (spec->no_analog)
5547 return 0;
5548
5549 /* If the use of more than one ADC is requested for the current
5550 * model, configure a second analog capture-only PCM.
5551 */
5552 have_multi_adcs = (spec->num_adc_nids > 1) &&
5553 !spec->dyn_adc_switch && !spec->auto_mic;
5554 /* Additional Analaog capture for index #2 */
5555 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005556 fill_pcm_stream_name(spec->stream_name_alt_analog,
5557 sizeof(spec->stream_name_alt_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005558 " Alt Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005559 info = snd_hda_codec_pcm_new(codec, "%s",
5560 spec->stream_name_alt_analog);
5561 if (!info)
5562 return -ENOMEM;
5563 spec->pcm_rec[2] = info;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005564 if (spec->alt_dac_nid)
5565 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5566 &pcm_analog_alt_playback,
5567 spec->stream_analog_alt_playback,
5568 spec->alt_dac_nid);
5569 else
5570 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5571 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005572 if (have_multi_adcs) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005573 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5574 &pcm_analog_alt_capture,
5575 spec->stream_analog_alt_capture,
5576 spec->adc_nids[1]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005577 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5578 spec->num_adc_nids - 1;
5579 } else {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005580 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5581 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 }
5584
5585 return 0;
5586}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005587EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005588
5589
5590/*
5591 * Standard auto-parser initializations
5592 */
5593
Takashi Iwaid4156932013-01-07 10:08:02 +01005594/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005595static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005596{
5597 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005598 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005599
Takashi Iwai196c17662013-01-04 15:01:40 +01005600 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005601 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005602 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005603 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005604 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005605 snd_hda_activate_path(codec, path, path->active,
5606 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005607 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005608}
5609
5610/* initialize primary output paths */
5611static void init_multi_out(struct hda_codec *codec)
5612{
5613 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005614 int i;
5615
Takashi Iwaid4156932013-01-07 10:08:02 +01005616 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005617 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005618}
5619
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005620
Takashi Iwai2c12c302013-01-10 09:33:29 +01005621static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005622{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005623 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005624
Takashi Iwaid4156932013-01-07 10:08:02 +01005625 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005626 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005627}
5628
5629/* initialize hp and speaker paths */
5630static void init_extra_out(struct hda_codec *codec)
5631{
5632 struct hda_gen_spec *spec = codec->spec;
5633
5634 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005635 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005636 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5637 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005638 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005639}
5640
5641/* initialize multi-io paths */
5642static void init_multi_io(struct hda_codec *codec)
5643{
5644 struct hda_gen_spec *spec = codec->spec;
5645 int i;
5646
5647 for (i = 0; i < spec->multi_ios; i++) {
5648 hda_nid_t pin = spec->multi_io[i].pin;
5649 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005650 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005651 if (!path)
5652 continue;
5653 if (!spec->multi_io[i].ctl_in)
5654 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005655 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005656 snd_hda_activate_path(codec, path, path->active,
5657 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005658 }
5659}
5660
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005661static void init_aamix_paths(struct hda_codec *codec)
5662{
5663 struct hda_gen_spec *spec = codec->spec;
5664
5665 if (!spec->have_aamix_ctl)
5666 return;
5667 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5668 spec->aamix_out_paths[0],
5669 spec->autocfg.line_out_type);
5670 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5671 spec->aamix_out_paths[1],
5672 AUTO_PIN_HP_OUT);
5673 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5674 spec->aamix_out_paths[2],
5675 AUTO_PIN_SPEAKER_OUT);
5676}
5677
Takashi Iwai352f7f92012-12-19 12:52:06 +01005678/* set up input pins and loopback paths */
5679static void init_analog_input(struct hda_codec *codec)
5680{
5681 struct hda_gen_spec *spec = codec->spec;
5682 struct auto_pin_cfg *cfg = &spec->autocfg;
5683 int i;
5684
5685 for (i = 0; i < cfg->num_inputs; i++) {
5686 hda_nid_t nid = cfg->inputs[i].pin;
5687 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005688 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005689
5690 /* init loopback inputs */
5691 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005692 resume_path_from_idx(codec, spec->loopback_paths[i]);
5693 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005694 }
5695 }
5696}
5697
5698/* initialize ADC paths */
5699static void init_input_src(struct hda_codec *codec)
5700{
5701 struct hda_gen_spec *spec = codec->spec;
5702 struct hda_input_mux *imux = &spec->input_mux;
5703 struct nid_path *path;
5704 int i, c, nums;
5705
5706 if (spec->dyn_adc_switch)
5707 nums = 1;
5708 else
5709 nums = spec->num_adc_nids;
5710
5711 for (c = 0; c < nums; c++) {
5712 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005713 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005714 if (path) {
5715 bool active = path->active;
5716 if (i == spec->cur_mux[c])
5717 active = true;
5718 snd_hda_activate_path(codec, path, active, false);
5719 }
5720 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005721 if (spec->hp_mic)
5722 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005723 }
5724
Takashi Iwai352f7f92012-12-19 12:52:06 +01005725 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005726 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005727}
5728
5729/* set right pin controls for digital I/O */
5730static void init_digital(struct hda_codec *codec)
5731{
5732 struct hda_gen_spec *spec = codec->spec;
5733 int i;
5734 hda_nid_t pin;
5735
Takashi Iwaid4156932013-01-07 10:08:02 +01005736 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005737 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005738 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005739 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005740 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005741 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005742 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005743}
5744
Takashi Iwai973e4972012-12-20 15:16:09 +01005745/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5746 * invalid unsol tags by some reason
5747 */
5748static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5749{
5750 int i;
5751
5752 for (i = 0; i < codec->init_pins.used; i++) {
5753 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5754 hda_nid_t nid = pin->nid;
5755 if (is_jack_detectable(codec, nid) &&
5756 !snd_hda_jack_tbl_get(codec, nid))
5757 snd_hda_codec_update_cache(codec, nid, 0,
5758 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5759 }
5760}
5761
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005762/**
5763 * snd_hda_gen_init - initialize the generic spec
5764 * @codec: the HDA codec
5765 *
5766 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005767 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005768int snd_hda_gen_init(struct hda_codec *codec)
5769{
5770 struct hda_gen_spec *spec = codec->spec;
5771
5772 if (spec->init_hook)
5773 spec->init_hook(codec);
5774
5775 snd_hda_apply_verbs(codec);
5776
5777 init_multi_out(codec);
5778 init_extra_out(codec);
5779 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005780 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005781 init_analog_input(codec);
5782 init_input_src(codec);
5783 init_digital(codec);
5784
Takashi Iwai973e4972012-12-20 15:16:09 +01005785 clear_unsol_on_unused_pins(codec);
5786
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005787 sync_all_pin_power_ctls(codec);
5788
Takashi Iwai352f7f92012-12-19 12:52:06 +01005789 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005790 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005791
Takashi Iwaia551d912015-02-26 12:34:49 +01005792 regcache_sync(codec->core.regmap);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005793
Takashi Iwai352f7f92012-12-19 12:52:06 +01005794 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5795 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5796
5797 hda_call_check_power_status(codec, 0x01);
5798 return 0;
5799}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005800EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005801
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005802/**
5803 * snd_hda_gen_free - free the generic spec
5804 * @codec: the HDA codec
5805 *
5806 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005807 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005808void snd_hda_gen_free(struct hda_codec *codec)
5809{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005810 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005811 snd_hda_gen_spec_free(codec->spec);
5812 kfree(codec->spec);
5813 codec->spec = NULL;
5814}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005815EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005816
5817#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005818/**
5819 * snd_hda_gen_check_power_status - check the loopback power save state
5820 * @codec: the HDA codec
5821 * @nid: NID to inspect
5822 *
5823 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005824 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005825int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5826{
5827 struct hda_gen_spec *spec = codec->spec;
5828 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5829}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005830EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005831#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005832
5833
5834/*
5835 * the generic codec support
5836 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837
Takashi Iwai352f7f92012-12-19 12:52:06 +01005838static const struct hda_codec_ops generic_patch_ops = {
5839 .build_controls = snd_hda_gen_build_controls,
5840 .build_pcms = snd_hda_gen_build_pcms,
5841 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005842 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005843 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005844#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005845 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005846#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847};
5848
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005849/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005850 * snd_hda_parse_generic_codec - Generic codec parser
5851 * @codec: the HDA codec
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005852 */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005853static int snd_hda_parse_generic_codec(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005855 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 int err;
5857
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005858 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005859 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005861 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005864 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5865 if (err < 0)
5866 return err;
5867
5868 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005869 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 goto error;
5871
5872 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 return 0;
5874
Takashi Iwai352f7f92012-12-19 12:52:06 +01005875error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005876 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 return err;
5878}
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005879
5880static const struct hda_codec_preset snd_hda_preset_generic[] = {
5881 { .id = HDA_CODEC_ID_GENERIC, .patch = snd_hda_parse_generic_codec },
5882 {} /* terminator */
5883};
5884
5885static struct hda_codec_driver generic_driver = {
5886 .preset = snd_hda_preset_generic,
5887};
5888
5889module_hda_codec_driver(generic_driver);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005890
5891MODULE_LICENSE("GPL");
5892MODULE_DESCRIPTION("Generic HD-audio codec parser");